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),
70 };
71
72 OptionValue() = default;
73
74 virtual ~OptionValue() = default;
75
76 OptionValue(const OptionValue &other);
77
78 OptionValue& operator=(const OptionValue &other);
79
80 // Subclasses should override these functions
81 virtual Type GetType() const = 0;
82
83 // If this value is always hidden, the avoid showing any info on this value,
84 // just show the info for the child values.
85 virtual bool ValueIsTransparent() const {
86 return GetType() == eTypeProperties;
87 }
88
89 virtual const char *GetTypeAsCString() const {
91 }
92
93 static const char *GetBuiltinTypeAsCString(Type t);
94
95 virtual void DumpValue(const ExecutionContext *exe_ctx, Stream &strm,
96 uint32_t dump_mask) = 0;
97
98 virtual llvm::json::Value ToJSON(const ExecutionContext *exe_ctx) const = 0;
99
100 virtual Status
101 SetValueFromString(llvm::StringRef value,
103
104 virtual void Clear() = 0;
105
106 virtual lldb::OptionValueSP
107 DeepCopy(const lldb::OptionValueSP &new_parent) const;
108
109 virtual void AutoComplete(CommandInterpreter &interpreter,
110 CompletionRequest &request);
111
112 // Subclasses can override these functions
114 llvm::StringRef name,
115 Status &error) const {
116 error = Status::FromErrorStringWithFormatv("'{0}' is not a valid subvalue",
117 name);
118 return lldb::OptionValueSP();
119 }
120
121 virtual Status SetSubValue(const ExecutionContext *exe_ctx,
122 VarSetOperationType op, llvm::StringRef name,
123 llvm::StringRef value);
124
125 virtual bool IsAggregateValue() const { return false; }
126
127 virtual llvm::StringRef GetName() const { return llvm::StringRef(); }
128
129 virtual bool DumpQualifiedName(Stream &strm) const;
130
131 // Subclasses should NOT override these functions as they use the above
132 // functions to implement functionality
133 uint32_t GetTypeAsMask() { return 1u << GetType(); }
134
135 static uint32_t ConvertTypeToMask(OptionValue::Type type) {
136 return 1u << type;
137 }
138
139 static OptionValue::Type ConvertTypeMaskToType(uint32_t type_mask) {
140 // If only one bit is set, then return an appropriate enumeration
141 switch (type_mask) {
142 case 1u << eTypeArch:
143 return eTypeArch;
144 case 1u << eTypeArgs:
145 return eTypeArgs;
146 case 1u << eTypeArray:
147 return eTypeArray;
148 case 1u << eTypeBoolean:
149 return eTypeBoolean;
150 case 1u << eTypeChar:
151 return eTypeChar;
152 case 1u << eTypeDictionary:
153 return eTypeDictionary;
154 case 1u << eTypeEnum:
155 return eTypeEnum;
156 case 1u << eTypeFileLineColumn:
157 return eTypeFileLineColumn;
158 case 1u << eTypeFileSpec:
159 return eTypeFileSpec;
160 case 1u << eTypeFileSpecList:
161 return eTypeFileSpecList;
162 case 1u << eTypeFormat:
163 return eTypeFormat;
164 case 1u << eTypeLanguage:
165 return eTypeLanguage;
166 case 1u << eTypePathMap:
167 return eTypePathMap;
168 case 1u << eTypeProperties:
169 return eTypeProperties;
170 case 1u << eTypeRegex:
171 return eTypeRegex;
172 case 1u << eTypeSInt64:
173 return eTypeSInt64;
174 case 1u << eTypeString:
175 return eTypeString;
176 case 1u << eTypeUInt64:
177 return eTypeUInt64;
178 case 1u << eTypeUUID:
179 return eTypeUUID;
180 }
181 // Else return invalid
182 return eTypeInvalid;
183 }
184
186 CreateValueFromCStringForTypeMask(const char *value_cstr, uint32_t type_mask,
187 Status &error);
188
190 const OptionValueArch *GetAsArch() const;
191
193 const OptionValueArray *GetAsArray() const;
194
196 const OptionValueArgs *GetAsArgs() const;
197
199 const OptionValueBoolean *GetAsBoolean() const;
200
202 const OptionValueChar *GetAsChar() const;
203
206
209
211 const OptionValueFileSpec *GetAsFileSpec() const;
212
215
217 const OptionValueFormat *GetAsFormat() const;
218
220 const OptionValueLanguage *GetAsLanguage() const;
221
224
227
229 const OptionValueRegex *GetAsRegex() const;
230
232 const OptionValueSInt64 *GetAsSInt64() const;
233
235 const OptionValueString *GetAsString() const;
236
238 const OptionValueUInt64 *GetAsUInt64() const;
239
241 const OptionValueUUID *GetAsUUID() const;
242
245
246 bool AppendFileSpecValue(FileSpec file_spec);
247
248 bool OptionWasSet() const { return m_value_was_set; }
249
251
252 void SetParent(const lldb::OptionValueSP &parent_sp) {
253 m_parent_wp = parent_sp;
254 }
255
256 lldb::OptionValueSP GetParent() const { return m_parent_wp.lock(); }
257
258 void SetValueChangedCallback(std::function<void()> callback) {
259 m_callback = std::move(callback);
260 }
261
263 if (m_callback)
264 m_callback();
265 }
266
267 template <typename T, std::enable_if_t<!std::is_pointer_v<T>, bool> = true>
268 std::optional<T> GetValueAs() const {
269 if constexpr (std::is_same_v<T, uint64_t>)
270 return GetUInt64Value();
271 if constexpr (std::is_same_v<T, int64_t>)
272 return GetSInt64Value();
273 if constexpr (std::is_same_v<T, bool>)
274 return GetBooleanValue();
275 if constexpr (std::is_same_v<T, char>)
276 return GetCharValue();
277 if constexpr (std::is_same_v<T, lldb::Format>)
278 return GetFormatValue();
279 if constexpr (std::is_same_v<T, FileSpec>)
280 return GetFileSpecValue();
281 if constexpr (std::is_same_v<T, FileSpecList>)
282 return GetFileSpecListValue();
283 if constexpr (std::is_same_v<T, lldb::LanguageType>)
284 return GetLanguageValue();
285 if constexpr (std::is_same_v<T, llvm::StringRef>)
286 return GetStringValue();
287 if constexpr (std::is_same_v<T, ArchSpec>)
288 return GetArchSpecValue();
289 if constexpr (std::is_same_v<T, FormatEntity::Entry>)
290 return GetFormatEntityValue();
291 if constexpr (std::is_enum_v<T>)
292 if (std::optional<int64_t> value = GetEnumerationValue())
293 return static_cast<T>(*value);
294 return {};
295 }
296
297 template <typename T,
298 typename U = typename std::remove_const<
299 typename std::remove_pointer<T>::type>::type,
300 std::enable_if_t<std::is_pointer_v<T>, bool> = true>
301 T GetValueAs() const {
302 static_assert(std::is_same_v<U, RegularExpression>,
303 "only for RegularExpression");
304 return GetRegexValue();
305 }
306
307 bool SetValueAs(bool v) { return SetBooleanValue(v); }
308
309 bool SetValueAs(char v) { return SetCharValue(v); }
310
311 bool SetValueAs(uint64_t v) { return SetUInt64Value(v); }
312
313 bool SetValueAs(int64_t v) { return SetSInt64Value(v); }
314
315 bool SetValueAs(UUID v) { return SetUUIDValue(v); }
316
317 bool SetValueAs(llvm::StringRef v) { return SetStringValue(v); }
318
320
322
323 bool SetValueAs(FileSpec v) { return SetFileSpecValue(v); }
324
325 bool SetValueAs(ArchSpec v) { return SetArchSpecValue(v); }
326
328 return SetFormatEntityValue(v);
329 }
330
331 template <typename T, std::enable_if_t<std::is_enum_v<T>, bool> = true>
332 bool SetValueAs(T t) {
333 return SetEnumerationValue(t);
334 }
335
336protected:
338
339 // Must be overriden by a derived class for correct downcasting the result of
340 // DeepCopy to it. Inherit from Cloneable to avoid doing this manually.
341 virtual lldb::OptionValueSP Clone() const = 0;
342
344 public:
346 stream.PutCString(" (default: ");
347 }
348 ~DefaultValueFormat() { stream.PutChar(')'); }
349
352
353 private:
355 };
356
358 std::function<void()> m_callback;
359 bool m_value_was_set = false; // This can be used to see if a value has been
360 // set by a call to SetValueFromCString(). It is
361 // often handy to know if an option value was
362 // set from the command line or as a setting,
363 // versus if we just have the default value that
364 // was already populated in the option value.
365private:
366 std::optional<ArchSpec> GetArchSpecValue() const;
367 bool SetArchSpecValue(ArchSpec arch_spec);
368
369 std::optional<bool> GetBooleanValue() const;
370 bool SetBooleanValue(bool new_value);
371
372 std::optional<char> GetCharValue() const;
373 bool SetCharValue(char new_value);
374
375 std::optional<int64_t> GetEnumerationValue() const;
376 bool SetEnumerationValue(int64_t value);
377
378 std::optional<FileSpec> GetFileSpecValue() const;
379 bool SetFileSpecValue(FileSpec file_spec);
380
381 std::optional<FileSpecList> GetFileSpecListValue() const;
382
383 std::optional<int64_t> GetSInt64Value() const;
384 bool SetSInt64Value(int64_t new_value);
385
386 std::optional<uint64_t> GetUInt64Value() const;
387 bool SetUInt64Value(uint64_t new_value);
388
389 std::optional<lldb::Format> GetFormatValue() const;
390 bool SetFormatValue(lldb::Format new_value);
391
392 std::optional<lldb::LanguageType> GetLanguageValue() const;
393 bool SetLanguageValue(lldb::LanguageType new_language);
394
395 std::optional<llvm::StringRef> GetStringValue() const;
396 bool SetStringValue(llvm::StringRef new_value);
397
398 std::optional<UUID> GetUUIDValue() const;
399 bool SetUUIDValue(const UUID &uuid);
400
402 bool SetFormatEntityValue(const FormatEntity::Entry &entry);
403
404 const RegularExpression *GetRegexValue() const;
405
406 mutable std::mutex m_mutex;
407};
408
409} // namespace lldb_private
410
411#endif // LLDB_INTERPRETER_OPTIONVALUE_H
static llvm::raw_ostream & error(Stream &strm)
An architecture specification class.
Definition ArchSpec.h:31
"lldb/Utility/ArgCompletionRequest.h"
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
A file utility class.
Definition FileSpec.h:57
DefaultValueFormat & operator=(const DefaultValueFormat &)=delete
DefaultValueFormat(const DefaultValueFormat &)=delete
virtual bool DumpQualifiedName(Stream &strm) 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:85
OptionValueRegex * GetAsRegex()
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)
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()
virtual void Clear()=0
bool SetValueAs(uint64_t v)
lldb::OptionValueSP GetParent() const
OptionValueArgs * GetAsArgs()
virtual const char * GetTypeAsCString() const
Definition OptionValue.h:89
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)
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