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