LLDB mainline
OptionValue.h
Go to the documentation of this file.
1//===-- OptionValue.h -------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_INTERPRETER_OPTIONVALUE_H
10#define LLDB_INTERPRETER_OPTIONVALUE_H
11
19#include "lldb/Utility/Status.h"
20#include "lldb/Utility/Stream.h"
22#include "lldb/Utility/UUID.h"
23#include "lldb/lldb-defines.h"
26#include "llvm/Support/JSON.h"
27#include <mutex>
28
29namespace lldb_private {
30
31// OptionValue
33public:
57
58 enum {
59 eDumpOptionName = (1u << 0),
60 eDumpOptionType = (1u << 1),
61 eDumpOptionValue = (1u << 2),
63 eDumpOptionRaw = (1u << 4),
64 eDumpOptionCommand = (1u << 5),
67 /// Also dump deprecated settings that still have their default value.
69 /// The options that choose which settings are dumped, rather than what is
70 /// dumped for each one.
76 };
77
78 OptionValue() = default;
79
80 virtual ~OptionValue() = default;
81
82 OptionValue(const OptionValue &other);
83
84 OptionValue& operator=(const OptionValue &other);
85
86 // Subclasses should override these functions
87 virtual Type GetType() const = 0;
88
89 // If this value is always hidden, the avoid showing any info on this value,
90 // just show the info for the child values.
91 virtual bool ValueIsTransparent() const {
92 return GetType() == eTypeProperties;
93 }
94
95 virtual const char *GetTypeAsCString() const {
97 }
98
99 static const char *GetBuiltinTypeAsCString(Type t);
100
101 virtual void DumpValue(const ExecutionContext *exe_ctx, Stream &strm,
102 uint32_t dump_mask) = 0;
103
104 virtual llvm::json::Value ToJSON(const ExecutionContext *exe_ctx) const = 0;
105
106 virtual Status
107 SetValueFromString(llvm::StringRef value,
109
110 void Clear() {
111 std::lock_guard<std::mutex> lock(m_mutex);
112 ClearImpl();
113 }
114
115 virtual lldb::OptionValueSP
116 DeepCopy(const lldb::OptionValueSP &new_parent) const;
117
118 virtual void AutoComplete(CommandInterpreter &interpreter,
119 CompletionRequest &request);
120
121 // Subclasses can override these functions
123 llvm::StringRef name,
124 Status &error) const {
125 error = Status::FromErrorStringWithFormatv("'{0}' is not a valid subvalue",
126 name);
127 return lldb::OptionValueSP();
128 }
129
130 virtual Status SetSubValue(const ExecutionContext *exe_ctx,
131 VarSetOperationType op, llvm::StringRef name,
132 llvm::StringRef value);
133
134 virtual bool IsAggregateValue() const { return false; }
135
136 virtual llvm::StringRef GetName() const { return llvm::StringRef(); }
137
138 virtual bool DumpQualifiedName(
139 Stream &strm,
140 std::optional<Stream::HighlightSettings> highlight = std::nullopt) const;
141
142 // Subclasses should NOT override these functions as they use the above
143 // functions to implement functionality
144 uint32_t GetTypeAsMask() { return 1u << GetType(); }
145
146 static uint32_t ConvertTypeToMask(OptionValue::Type type) {
147 return 1u << type;
148 }
149
150 static OptionValue::Type ConvertTypeMaskToType(uint32_t type_mask) {
151 // If only one bit is set, then return an appropriate enumeration
152 switch (type_mask) {
153 case 1u << eTypeArch:
154 return eTypeArch;
155 case 1u << eTypeArgs:
156 return eTypeArgs;
157 case 1u << eTypeArray:
158 return eTypeArray;
159 case 1u << eTypeBoolean:
160 return eTypeBoolean;
161 case 1u << eTypeChar:
162 return eTypeChar;
163 case 1u << eTypeDictionary:
164 return eTypeDictionary;
165 case 1u << eTypeEnum:
166 return eTypeEnum;
167 case 1u << eTypeFileLineColumn:
168 return eTypeFileLineColumn;
169 case 1u << eTypeFileSpec:
170 return eTypeFileSpec;
171 case 1u << eTypeFileSpecList:
172 return eTypeFileSpecList;
173 case 1u << eTypeFormat:
174 return eTypeFormat;
175 case 1u << eTypeLanguage:
176 return eTypeLanguage;
177 case 1u << eTypePathMap:
178 return eTypePathMap;
179 case 1u << eTypeProperties:
180 return eTypeProperties;
181 case 1u << eTypeRegex:
182 return eTypeRegex;
183 case 1u << eTypeSInt64:
184 return eTypeSInt64;
185 case 1u << eTypeString:
186 return eTypeString;
187 case 1u << eTypeUInt64:
188 return eTypeUInt64;
189 case 1u << eTypeUUID:
190 return eTypeUUID;
191 }
192 // Else return invalid
193 return eTypeInvalid;
194 }
195
197 CreateValueFromCStringForTypeMask(const char *value_cstr, uint32_t type_mask,
198 Status &error);
199
201 const OptionValueArch *GetAsArch() const;
202
204 const OptionValueArray *GetAsArray() const;
205
207 const OptionValueArgs *GetAsArgs() const;
208
210 const OptionValueBoolean *GetAsBoolean() const;
211
213 const OptionValueChar *GetAsChar() const;
214
217
220
222 const OptionValueFileSpec *GetAsFileSpec() const;
223
226
228 const OptionValueFormat *GetAsFormat() const;
229
231 const OptionValueLanguage *GetAsLanguage() const;
232
235
238
240 const OptionValueRegex *GetAsRegex() const;
241
243 const OptionValueSInt64 *GetAsSInt64() const;
244
246 const OptionValueString *GetAsString() const;
247
249 const OptionValueUInt64 *GetAsUInt64() const;
250
252 const OptionValueUUID *GetAsUUID() const;
253
256
257 bool AppendFileSpecValue(FileSpec file_spec);
258
259 bool OptionWasSet() const { return m_value_was_set; }
260
262
263 /// Return true if the current value equals the default value.
264 ///
265 /// Subclasses that store a default value should override this to compare
266 /// against it. The base implementation falls back to `OptionWasSet()`, which
267 /// is a reasonable approximation for types without an explicit default.
268 virtual bool IsDefault() const { return !OptionWasSet(); }
269
270 void SetParent(const lldb::OptionValueSP &parent_sp) {
271 m_parent_wp = parent_sp;
272 }
273
274 lldb::OptionValueSP GetParent() const { return m_parent_wp.lock(); }
275
276 void SetValueChangedCallback(std::function<void()> callback) {
277 m_callback = std::move(callback);
278 }
279
281 if (m_callback)
282 m_callback();
283 }
284
285 template <typename T, std::enable_if_t<!std::is_pointer_v<T>, bool> = true>
286 std::optional<T> GetValueAs() const {
287 if constexpr (std::is_same_v<T, uint64_t>)
288 return GetUInt64Value();
289 if constexpr (std::is_same_v<T, int64_t>)
290 return GetSInt64Value();
291 if constexpr (std::is_same_v<T, bool>)
292 return GetBooleanValue();
293 if constexpr (std::is_same_v<T, char>)
294 return GetCharValue();
295 if constexpr (std::is_same_v<T, lldb::Format>)
296 return GetFormatValue();
297 if constexpr (std::is_same_v<T, FileSpec>)
298 return GetFileSpecValue();
299 if constexpr (std::is_same_v<T, FileSpecList>)
300 return GetFileSpecListValue();
301 if constexpr (std::is_same_v<T, lldb::LanguageType>)
302 return GetLanguageValue();
303 if constexpr (std::is_same_v<T, llvm::StringRef>)
304 return GetStringValue();
305 if constexpr (std::is_same_v<T, ArchSpec>)
306 return GetArchSpecValue();
307 if constexpr (std::is_same_v<T, FormatEntity::Entry>)
308 return GetFormatEntityValue();
309 if constexpr (std::is_enum_v<T>)
310 if (std::optional<int64_t> value = GetEnumerationValue())
311 return static_cast<T>(*value);
312 return {};
313 }
314
315 template <typename T,
316 typename U = typename std::remove_const<
317 typename std::remove_pointer<T>::type>::type,
318 std::enable_if_t<std::is_pointer_v<T>, bool> = true>
319 T GetValueAs() const {
320 static_assert(std::is_same_v<U, RegularExpression>,
321 "only for RegularExpression");
322 return GetRegexValue();
323 }
324
325 bool SetValueAs(bool v) { return SetBooleanValue(v); }
326
327 bool SetValueAs(char v) { return SetCharValue(v); }
328
329 bool SetValueAs(uint64_t v) { return SetUInt64Value(v); }
330
331 bool SetValueAs(int64_t v) { return SetSInt64Value(v); }
332
333 bool SetValueAs(UUID v) { return SetUUIDValue(v); }
334
335 bool SetValueAs(llvm::StringRef v) { return SetStringValue(v); }
336
338
340
341 bool SetValueAs(FileSpec v) { return SetFileSpecValue(v); }
342
343 bool SetValueAs(ArchSpec v) { return SetArchSpecValue(v); }
344
346 return SetFormatEntityValue(v);
347 }
348
349 template <typename T, std::enable_if_t<std::is_enum_v<T>, bool> = true>
350 bool SetValueAs(T t) {
351 return SetEnumerationValue(t);
352 }
353
354protected:
356
357 // Must be overriden by a derived class for correct downcasting the result of
358 // DeepCopy to it. Inherit from Cloneable to avoid doing this manually.
359 virtual lldb::OptionValueSP Clone() const = 0;
360
361 virtual void ClearImpl() = 0;
362
364 public:
366 stream.PutCString(" (default: ");
367 }
368 ~DefaultValueFormat() { stream.PutChar(')'); }
369
372
373 private:
375 };
376
378 std::function<void()> m_callback;
379 bool m_value_was_set = false; // This can be used to see if a value has been
380 // set by a call to SetValueFromCString(). It is
381 // often handy to know if an option value was
382 // set from the command line or as a setting,
383 // versus if we just have the default value that
384 // was already populated in the option value.
385 mutable std::mutex m_mutex;
386
387private:
388 std::optional<ArchSpec> GetArchSpecValue() const;
389 bool SetArchSpecValue(ArchSpec arch_spec);
390
391 std::optional<bool> GetBooleanValue() const;
392 bool SetBooleanValue(bool new_value);
393
394 std::optional<char> GetCharValue() const;
395 bool SetCharValue(char new_value);
396
397 std::optional<int64_t> GetEnumerationValue() const;
398 bool SetEnumerationValue(int64_t value);
399
400 std::optional<FileSpec> GetFileSpecValue() const;
401 bool SetFileSpecValue(FileSpec file_spec);
402
403 std::optional<FileSpecList> GetFileSpecListValue() const;
404
405 std::optional<int64_t> GetSInt64Value() const;
406 bool SetSInt64Value(int64_t new_value);
407
408 std::optional<uint64_t> GetUInt64Value() const;
409 bool SetUInt64Value(uint64_t new_value);
410
411 std::optional<lldb::Format> GetFormatValue() const;
412 bool SetFormatValue(lldb::Format new_value);
413
414 std::optional<lldb::LanguageType> GetLanguageValue() const;
415 bool SetLanguageValue(lldb::LanguageType new_language);
416
417 std::optional<llvm::StringRef> GetStringValue() const;
418 bool SetStringValue(llvm::StringRef new_value);
419
420 std::optional<UUID> GetUUIDValue() const;
421 bool SetUUIDValue(const UUID &uuid);
422
424 bool SetFormatEntityValue(const FormatEntity::Entry &entry);
425
426 const RegularExpression *GetRegexValue() const;
427};
428
429} // namespace lldb_private
430
431#endif // LLDB_INTERPRETER_OPTIONVALUE_H
static llvm::raw_ostream & error(Stream &strm)
An architecture specification class.
Definition ArchSpec.h:32
"lldb/Utility/ArgCompletionRequest.h"
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
A file utility class.
Definition FileSpec.h:56
DefaultValueFormat & operator=(const DefaultValueFormat &)=delete
DefaultValueFormat(const DefaultValueFormat &)=delete
virtual bool DumpQualifiedName(Stream &strm, std::optional< Stream::HighlightSettings > highlight=std::nullopt) const
std::optional< char > GetCharValue() const
std::optional< FileSpecList > GetFileSpecListValue() const
bool SetArchSpecValue(ArchSpec arch_spec)
virtual ~OptionValue()=default
void SetValueChangedCallback(std::function< void()> callback)
std::optional< ArchSpec > GetArchSpecValue() const
OptionValueDictionary * GetAsDictionary()
bool SetFormatEntityValue(const FormatEntity::Entry &entry)
virtual lldb::OptionValueSP GetSubValue(const ExecutionContext *exe_ctx, llvm::StringRef name, Status &error) const
virtual Status SetValueFromString(llvm::StringRef value, VarSetOperationType op=eVarSetOperationAssign)
OptionValueSInt64 * GetAsSInt64()
virtual llvm::json::Value ToJSON(const ExecutionContext *exe_ctx) const =0
std::optional< FileSpec > GetFileSpecValue() const
static lldb::OptionValueSP CreateValueFromCStringForTypeMask(const char *value_cstr, uint32_t type_mask, Status &error)
std::optional< uint64_t > GetUInt64Value() const
bool SetFileSpecValue(FileSpec file_spec)
virtual void DumpValue(const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask)=0
virtual lldb::OptionValueSP Clone() const =0
virtual Type GetType() const =0
lldb::OptionValueWP m_parent_wp
OptionValueFileSpecList * GetAsFileSpecList()
virtual bool ValueIsTransparent() const
Definition OptionValue.h:91
OptionValueRegex * GetAsRegex()
virtual void ClearImpl()=0
virtual bool IsAggregateValue() const
void SetParent(const lldb::OptionValueSP &parent_sp)
OptionValueUInt64 * GetAsUInt64()
OptionValue & operator=(const OptionValue &other)
OptionValueFormatEntity * GetAsFormatEntity()
bool SetValueAs(lldb::Format v)
virtual bool IsDefault() const
Return true if the current value equals the default value.
bool SetCharValue(char new_value)
OptionValuePathMappings * GetAsPathMappings()
virtual void AutoComplete(CommandInterpreter &interpreter, CompletionRequest &request)
OptionValueProperties * GetAsProperties()
static OptionValue::Type ConvertTypeMaskToType(uint32_t type_mask)
OptionValueEnumeration * GetAsEnumeration()
virtual lldb::OptionValueSP DeepCopy(const lldb::OptionValueSP &new_parent) const
static const char * GetBuiltinTypeAsCString(Type t)
bool SetValueAs(lldb::LanguageType v)
OptionValueFormat * GetAsFormat()
bool SetValueAs(uint64_t v)
lldb::OptionValueSP GetParent() const
OptionValueArgs * GetAsArgs()
virtual const char * GetTypeAsCString() const
Definition OptionValue.h:95
OptionValueChar * GetAsChar()
bool AppendFileSpecValue(FileSpec file_spec)
OptionValueArray * GetAsArray()
std::optional< lldb::LanguageType > GetLanguageValue() const
bool SetStringValue(llvm::StringRef new_value)
std::optional< int64_t > GetSInt64Value() const
bool SetUUIDValue(const UUID &uuid)
std::optional< lldb::Format > GetFormatValue() const
bool SetValueAs(const FormatEntity::Entry &v)
std::optional< int64_t > GetEnumerationValue() const
std::optional< bool > GetBooleanValue() const
bool SetValueAs(llvm::StringRef v)
bool SetBooleanValue(bool new_value)
std::optional< llvm::StringRef > GetStringValue() const
OptionValueBoolean * GetAsBoolean()
OptionValueFileSpec * GetAsFileSpec()
const RegularExpression * GetRegexValue() const
bool SetEnumerationValue(int64_t value)
OptionValueUUID * GetAsUUID()
std::optional< T > GetValueAs() const
bool SetUInt64Value(uint64_t new_value)
virtual llvm::StringRef GetName() const
OptionValueArch * GetAsArch()
bool SetValueAs(int64_t v)
bool SetValueAs(ArchSpec v)
bool SetFormatValue(lldb::Format new_value)
bool SetLanguageValue(lldb::LanguageType new_language)
static uint32_t ConvertTypeToMask(OptionValue::Type type)
@ eDumpOptionIncludeDeprecated
Also dump deprecated settings that still have their default value.
Definition OptionValue.h:68
@ eDumpGroupFilter
The options that choose which settings are dumped, rather than what is dumped for each one.
Definition OptionValue.h:71
std::function< void()> m_callback
OptionValueLanguage * GetAsLanguage()
std::optional< UUID > GetUUIDValue() const
bool SetValueAs(FileSpec v)
bool SetSInt64Value(int64_t new_value)
virtual Status SetSubValue(const ExecutionContext *exe_ctx, VarSetOperationType op, llvm::StringRef name, llvm::StringRef value)
FormatEntity::Entry GetFormatEntityValue() const
OptionValueString * GetAsString()
An error handling class.
Definition Status.h:118
static Status static Status FromErrorStringWithFormatv(const char *format, Args &&...args)
Definition Status.h:151
A stream class that can stream formatted output to a file.
Definition Stream.h:28
Represents UUID's of various sizes.
Definition UUID.h:27
A class that represents a running process on the host machine.
VarSetOperationType
Settable state variable types.
Format
Display format definitions.
LanguageType
Programming language type.
std::weak_ptr< lldb_private::OptionValue > OptionValueWP
std::shared_ptr< lldb_private::OptionValue > OptionValueSP