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