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