LLDB mainline
Property.cpp
Go to the documentation of this file.
1//===-- Property.cpp ------------------------------------------------------===//
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
10
16
17#include <memory>
18
19using namespace lldb;
20using namespace lldb_private;
21
23 : m_name(definition.name), m_description(definition.description),
24 m_is_global(definition.global), m_is_deprecated(definition.deprecated),
25 m_deprecation_message(definition.deprecation_message) {
26 switch (definition.type) {
27 case OptionValue::eTypeInvalid:
28 case OptionValue::eTypeProperties:
29 break;
30 case OptionValue::eTypeArch:
31 // "definition.default_uint_value" is not used
32 // "definition.default_cstr_value" as a string value that represents the
33 // default string value for the architecture/triple
34 m_value_sp =
35 std::make_shared<OptionValueArch>(definition.default_cstr_value);
36 break;
37
38 case OptionValue::eTypeArgs:
39 // "definition.default_uint_value" is always a OptionValue::Type
40 m_value_sp = std::make_shared<OptionValueArgs>();
41 break;
42
43 case OptionValue::eTypeArray:
44 // "definition.default_uint_value" is always a OptionValue::Type
45 m_value_sp =
46 std::make_shared<OptionValueArray>(OptionValue::ConvertTypeToMask(
47 (OptionValue::Type)definition.default_uint_value));
48 break;
49
50 case OptionValue::eTypeBoolean:
51 // "definition.default_uint_value" is the default boolean value if
52 // "definition.default_cstr_value" is NULL, otherwise interpret
53 // "definition.default_cstr_value" as a string value that represents the
54 // default value.
55 if (definition.default_cstr_value)
56 m_value_sp =
57 std::make_shared<OptionValueBoolean>(OptionArgParser::ToBoolean(
58 llvm::StringRef(definition.default_cstr_value), false, nullptr));
59 else
60 m_value_sp = std::make_shared<OptionValueBoolean>(
61 definition.default_uint_value != 0);
62 break;
63
64 case OptionValue::eTypeChar: {
65 llvm::StringRef s(definition.default_cstr_value ? definition.default_cstr_value : "");
66 m_value_sp = std::make_shared<OptionValueChar>(
67 OptionArgParser::ToChar(s, '\0', nullptr));
68 break;
69 }
71 // "definition.default_uint_value" is always a OptionValue::Type
72 m_value_sp = std::make_shared<OptionValueDictionary>(
75 definition.enum_values);
76 break;
77
79 // "definition.default_uint_value" is the default enumeration value if
80 // "definition.default_cstr_value" is NULL, otherwise interpret
81 // "definition.default_cstr_value" as a string value that represents the
82 // default value.
83 {
84 OptionValueEnumeration *enum_value = new OptionValueEnumeration(
85 definition.enum_values, definition.default_uint_value);
86 m_value_sp.reset(enum_value);
87 if (definition.default_cstr_value) {
88 if (enum_value
89 ->SetValueFromString(
90 llvm::StringRef(definition.default_cstr_value))
91 .Success()) {
92 enum_value->SetDefaultValue(enum_value->GetCurrentValue());
93 // Call Clear() since we don't want the value to appear as having
94 // been set since we called SetValueFromString() above. Clear will
95 // set the current value to the default and clear the boolean that
96 // says that the value has been set.
97 enum_value->Clear();
98 }
99 }
100 }
101 break;
102
104 // "definition.default_uint_value" is not used for a
105 // OptionValue::eTypeFileSpecList
106 m_value_sp = std::make_shared<OptionValueFileColonLine>();
107 break;
108
110 // "definition.default_uint_value" represents if the
111 // "definition.default_cstr_value" should be resolved or not
112 const bool resolve = definition.default_uint_value != 0;
113 FileSpec file_spec = FileSpec(definition.default_cstr_value);
114 if (resolve)
115 FileSystem::Instance().Resolve(file_spec);
116 m_value_sp = std::make_shared<OptionValueFileSpec>(file_spec, resolve);
117 break;
118 }
119
121 // "definition.default_uint_value" is not used for a
122 // OptionValue::eTypeFileSpecList
123 m_value_sp = std::make_shared<OptionValueFileSpecList>();
124 break;
125
127 // "definition.default_uint_value" is the default format enumeration value
128 // if "definition.default_cstr_value" is NULL, otherwise interpret
129 // "definition.default_cstr_value" as a string value that represents the
130 // default value.
131 {
132 Format new_format = eFormatInvalid;
133 if (definition.default_cstr_value)
134 OptionArgParser::ToFormat(definition.default_cstr_value, new_format,
135 nullptr);
136 else
137 new_format = (Format)definition.default_uint_value;
138 m_value_sp = std::make_shared<OptionValueFormat>(new_format);
139 }
140 break;
141
143 // "definition.default_uint_value" is the default language enumeration
144 // value if "definition.default_cstr_value" is NULL, otherwise interpret
145 // "definition.default_cstr_value" as a string value that represents the
146 // default value.
147 {
149 if (definition.default_cstr_value)
151 llvm::StringRef(definition.default_cstr_value));
152 else
153 new_lang = (LanguageType)definition.default_uint_value;
154 m_value_sp = std::make_shared<OptionValueLanguage>(new_lang);
155 }
156 break;
157
159 // "definition.default_cstr_value" as a string value that represents the
160 // default
161 m_value_sp = std::make_shared<OptionValueFormatEntity>(
162 definition.default_cstr_value);
163 break;
164
166 // "definition.default_uint_value" tells us if notifications should occur
167 // for path mappings
168 m_value_sp = std::make_shared<OptionValuePathMappings>(
169 definition.default_uint_value != 0);
170 break;
171
173 // "definition.default_uint_value" is used to the regular expression flags
174 // "definition.default_cstr_value" the default regular expression value
175 // value.
176 m_value_sp =
177 std::make_shared<OptionValueRegex>(definition.default_cstr_value);
178 break;
179
181 // "definition.default_uint_value" is the default integer value if
182 // "definition.default_cstr_value" is NULL, otherwise interpret
183 // "definition.default_cstr_value" as a string value that represents the
184 // default value.
185 int64_t value = 0;
186 // FIXME: improve error handling for llvm::to_integer()
187 if (definition.default_cstr_value)
188 llvm::to_integer(definition.default_cstr_value, value);
189 m_value_sp = std::make_shared<OptionValueSInt64>(
190 definition.default_cstr_value ? value : definition.default_uint_value);
191 break;
192 }
194 uint64_t value = 0;
195 // FIXME: improve error handling for llvm::to_integer()
196 if (definition.default_cstr_value)
197 llvm::to_integer(definition.default_cstr_value, value);
198 // "definition.default_uint_value" is the default unsigned integer value if
199 // "definition.default_cstr_value" is NULL, otherwise interpret
200 // "definition.default_cstr_value" as a string value that represents the
201 // default value.
202 m_value_sp = std::make_shared<OptionValueUInt64>(
203 definition.default_cstr_value ? value : definition.default_uint_value);
204 break;
205 }
207 // "definition.default_uint_value" is not used for a OptionValue::eTypeUUID
208 // "definition.default_cstr_value" can contain a default UUID value
209 {
210 UUID uuid;
211 if (definition.default_cstr_value)
212 uuid.SetFromStringRef(definition.default_cstr_value);
213 m_value_sp = std::make_shared<OptionValueUUID>(uuid);
214 }
215 break;
216
218 // "definition.default_uint_value" can contain the string option flags
219 // OR'ed together "definition.default_cstr_value" can contain a default
220 // string value
221 {
222 OptionValueString *string_value =
223 new OptionValueString(definition.default_cstr_value);
224 if (definition.default_uint_value != 0)
225 string_value->GetOptions().Reset(definition.default_uint_value);
226 m_value_sp.reset(string_value);
227 }
228 break;
229 }
230 assert(m_value_sp && "invalid property definition");
231}
232
233Property::Property(llvm::StringRef name, llvm::StringRef desc, bool is_global,
234 const lldb::OptionValueSP &value_sp)
235 : m_name(name), m_description(desc), m_value_sp(value_sp),
236 m_is_global(is_global) {}
237
239 Stream &strm, std::optional<Stream::HighlightSettings> highlight) const {
240 if (!m_name.empty()) {
241 bool has_sub_properties = static_cast<bool>(m_value_sp->GetAsProperties());
242 bool dumped_something = m_value_sp->DumpQualifiedName(strm, highlight);
243 if (!has_sub_properties) {
244 if (dumped_something)
245 strm.PutChar('.');
246 strm.PutCStringColorHighlighted(m_name, highlight);
247 }
248 return true;
249 }
250 return false;
251}
252
253static constexpr llvm::StringLiteral g_deprecated_tag("(deprecated)");
254
255void Property::DumpDeprecatedTag(Stream &strm, uint32_t dump_mask) const {
256 // Names dumped on their own are used for completion, and commands are read
257 // back by "settings read", so neither can have a deprecation tag included.
258 if (m_is_deprecated &&
259 (dump_mask &
261 !(dump_mask & OptionValue::eDumpOptionCommand))
262 strm << ' ' << g_deprecated_tag;
263}
264
266 if (!m_is_deprecated)
267 return m_description;
268 return (g_deprecated_tag + " " + m_description).str();
269}
270
271void Property::Dump(const ExecutionContext *exe_ctx, Stream &strm,
272 uint32_t dump_mask) const {
273 if (m_value_sp) {
274 const bool dump_desc = dump_mask & OptionValue::eDumpOptionDescription;
275 const bool dump_cmd = dump_mask & OptionValue::eDumpOptionCommand;
276 const bool transparent = m_value_sp->ValueIsTransparent();
277 if (dump_cmd && !transparent)
278 strm << "settings set -f ";
279 if (dump_desc || !transparent) {
280 if ((dump_mask & OptionValue::eDumpOptionName) && !m_name.empty()) {
281 DumpQualifiedName(strm);
282 DumpDeprecatedTag(strm, dump_mask);
283 if (dump_mask &
285 strm.PutChar(' ');
286 }
287 }
288 if (dump_desc) {
289 llvm::StringRef desc = GetDescription();
290 if (!desc.empty())
291 strm << "-- " << desc;
292
293 if (transparent && ((dump_mask & ~OptionValue::eDumpGroupFilter) ==
296 strm.EOL();
297 }
298 m_value_sp->DumpValue(exe_ctx, strm, dump_mask);
299 }
300}
301
303 CommandInterpreter &interpreter, Stream &strm, uint32_t output_width,
304 bool display_qualified_name, bool include_deprecated,
305 std::optional<Stream::HighlightSettings> highlight) const {
306 if (!m_value_sp)
307 return;
308
309 if (GetDescription().empty())
310 return;
311
312 StreamString qualified_name;
313 const OptionValueProperties *sub_properties = m_value_sp->GetAsProperties();
314 if (sub_properties) {
315 strm.EOL();
316
317 if (m_value_sp->DumpQualifiedName(qualified_name))
318 strm.Printf("'%s' variables:\n\n", qualified_name.GetData());
319 sub_properties->DumpAllDescriptions(interpreter, strm, include_deprecated);
320 } else {
321 // A deprecated property is only described when the user named it or asked
322 // for deprecated properties, so always say that it's deprecated. Only
323 // build a tagged copy of the description in that case.
324 std::string tagged_desc;
325 llvm::StringRef desc = GetDescription();
326 if (m_is_deprecated) {
327 tagged_desc = GetTaggedDescription();
328 desc = tagged_desc;
329 }
330 if (display_qualified_name) {
331 StreamString qualified_name;
332 DumpQualifiedName(qualified_name);
333 interpreter.OutputFormattedHelpText(strm, qualified_name.GetString(),
334 "--", desc, output_width, highlight);
335 } else {
336 interpreter.OutputFormattedHelpText(strm, m_name, "--", desc,
337 output_width, highlight);
338 }
339 }
340}
341
342void Property::SetValueChangedCallback(std::function<void()> callback) {
343 if (m_value_sp)
344 m_value_sp->SetValueChangedCallback(std::move(callback));
345}
static constexpr llvm::StringLiteral g_deprecated_tag("(deprecated)")
void OutputFormattedHelpText(Stream &strm, llvm::StringRef prefix, llvm::StringRef help_text, std::optional< Stream::HighlightSettings > highlight=std::nullopt)
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
A file utility class.
Definition FileSpec.h:56
static FileSystem & Instance()
void Resolve(llvm::SmallVectorImpl< char > &path, bool force_make_absolute=false)
Resolve path to make it canonical.
void Reset(ValueType flags)
Set accessor for all flags.
Definition Flags.h:52
static lldb::LanguageType GetLanguageTypeFromString(const char *string)=delete
virtual void DumpAllDescriptions(CommandInterpreter &interpreter, Stream &strm, bool include_deprecated) const
static uint32_t ConvertTypeToMask(OptionValue::Type type)
@ eDumpGroupFilter
The options that choose which settings are dumped, rather than what is dumped for each one.
Definition OptionValue.h:71
const char * m_deprecation_message
Points into the static PropertyDefinition table, or is nullptr.
Definition Property.h:107
void DumpDeprecatedTag(Stream &strm, uint32_t dump_mask) const
If this property is deprecated, and output produced with dump_mask can be tagged, write the deprecati...
Definition Property.cpp:255
llvm::StringRef GetDescription() const
Definition Property.h:53
lldb::OptionValueSP m_value_sp
Definition Property.h:103
bool DumpQualifiedName(Stream &strm, std::optional< Stream::HighlightSettings > highlight=std::nullopt) const
Definition Property.cpp:238
void SetValueChangedCallback(std::function< void()> callback)
Definition Property.cpp:342
std::string GetTaggedDescription() const
The description, starting with a deprecation tag if this property is deprecated.
Definition Property.cpp:265
void Dump(const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask) const
Definition Property.cpp:271
Property(const PropertyDefinition &definition)
Definition Property.cpp:22
std::string m_description
Definition Property.h:102
void DumpDescription(CommandInterpreter &interpreter, Stream &strm, uint32_t output_width, bool display_qualified_name, bool include_deprecated, std::optional< Stream::HighlightSettings > highlight=std::nullopt) const
Definition Property.cpp:302
const char * GetData() const
llvm::StringRef GetString() const
A stream class that can stream formatted output to a file.
Definition Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
size_t PutChar(char ch)
Definition Stream.cpp:131
void PutCStringColorHighlighted(llvm::StringRef text, std::optional< HighlightSettings > settings=std::nullopt)
Output a C string to the stream with color highlighting.
Definition Stream.cpp:73
size_t EOL()
Output and End of Line character to the stream.
Definition Stream.cpp:155
Represents UUID's of various sizes.
Definition UUID.h:27
bool SetFromStringRef(llvm::StringRef str)
Definition UUID.cpp:101
A class that represents a running process on the host machine.
Format
Display format definitions.
LanguageType
Programming language type.
@ eLanguageTypeUnknown
Unknown or invalid language value.
std::shared_ptr< lldb_private::OptionValue > OptionValueSP
static Status ToFormat(const char *s, lldb::Format &format, size_t *byte_size_ptr)
OptionEnumValues enum_values
Definition Property.h:31