1 //===-- Property.cpp --------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Interpreter/Property.h"
12 #include "lldb/Core/UserSettingsController.h"
13 #include "lldb/Host/StringConvert.h"
14 #include "lldb/Interpreter/CommandInterpreter.h"
15 #include "lldb/Interpreter/OptionArgParser.h"
16 #include "lldb/Interpreter/OptionValues.h"
17 #include "lldb/Target/Language.h"
20 using namespace lldb_private;
22 Property::Property(const PropertyDefinition &definition)
23 : m_name(definition.name), m_description(definition.description),
24 m_value_sp(), m_is_global(definition.global) {
25 switch (definition.type) {
26 case OptionValue::eTypeInvalid:
27 case OptionValue::eTypeProperties:
29 case OptionValue::eTypeArch:
30 // "definition.default_uint_value" is not used
31 // "definition.default_cstr_value" as a string value that represents the
32 // default string value for the architecture/triple
33 m_value_sp.reset(new OptionValueArch(definition.default_cstr_value));
36 case OptionValue::eTypeArgs:
37 // "definition.default_uint_value" is always a OptionValue::Type
38 m_value_sp.reset(new OptionValueArgs());
41 case OptionValue::eTypeArray:
42 // "definition.default_uint_value" is always a OptionValue::Type
43 m_value_sp.reset(new OptionValueArray(OptionValue::ConvertTypeToMask(
44 (OptionValue::Type)definition.default_uint_value)));
47 case OptionValue::eTypeBoolean:
48 // "definition.default_uint_value" is the default boolean value if
49 // "definition.default_cstr_value" is NULL, otherwise interpret
50 // "definition.default_cstr_value" as a string value that represents the
52 if (definition.default_cstr_value)
53 m_value_sp.reset(new OptionValueBoolean(OptionArgParser::ToBoolean(
54 llvm::StringRef(definition.default_cstr_value), false, nullptr)));
57 new OptionValueBoolean(definition.default_uint_value != 0));
60 case OptionValue::eTypeChar: {
61 llvm::StringRef s(definition.default_cstr_value ? definition.default_cstr_value : "");
62 m_value_sp = std::make_shared<OptionValueChar>(
63 OptionArgParser::ToChar(s, '\0', nullptr));
66 case OptionValue::eTypeDictionary:
67 // "definition.default_uint_value" is always a OptionValue::Type
68 m_value_sp.reset(new OptionValueDictionary(OptionValue::ConvertTypeToMask(
69 (OptionValue::Type)definition.default_uint_value)));
72 case OptionValue::eTypeEnum:
73 // "definition.default_uint_value" is the default enumeration value if
74 // "definition.default_cstr_value" is NULL, otherwise interpret
75 // "definition.default_cstr_value" as a string value that represents the
78 OptionValueEnumeration *enum_value = new OptionValueEnumeration(
79 definition.enum_values, definition.default_uint_value);
80 m_value_sp.reset(enum_value);
81 if (definition.default_cstr_value) {
84 llvm::StringRef(definition.default_cstr_value))
86 enum_value->SetDefaultValue(enum_value->GetCurrentValue());
87 // Call Clear() since we don't want the value to appear as having
88 // been set since we called SetValueFromString() above. Clear will
89 // set the current value to the default and clear the boolean that
90 // says that the value has been set.
97 case OptionValue::eTypeFileSpec: {
98 // "definition.default_uint_value" represents if the
99 // "definition.default_cstr_value" should be resolved or not
100 const bool resolve = definition.default_uint_value != 0;
101 FileSpec file_spec = FileSpec(definition.default_cstr_value);
103 FileSystem::Instance().Resolve(file_spec);
104 m_value_sp.reset(new OptionValueFileSpec(file_spec, resolve));
108 case OptionValue::eTypeFileSpecList:
109 // "definition.default_uint_value" is not used for a
110 // OptionValue::eTypeFileSpecList
111 m_value_sp.reset(new OptionValueFileSpecList());
114 case OptionValue::eTypeFormat:
115 // "definition.default_uint_value" is the default format enumeration value
116 // if "definition.default_cstr_value" is NULL, otherwise interpret
117 // "definition.default_cstr_value" as a string value that represents the
120 Format new_format = eFormatInvalid;
121 if (definition.default_cstr_value)
122 OptionArgParser::ToFormat(definition.default_cstr_value, new_format,
125 new_format = (Format)definition.default_uint_value;
126 m_value_sp.reset(new OptionValueFormat(new_format));
130 case OptionValue::eTypeLanguage:
131 // "definition.default_uint_value" is the default language enumeration
132 // value if "definition.default_cstr_value" is NULL, otherwise interpret
133 // "definition.default_cstr_value" as a string value that represents the
136 LanguageType new_lang = eLanguageTypeUnknown;
137 if (definition.default_cstr_value)
138 Language::GetLanguageTypeFromString(
139 llvm::StringRef(definition.default_cstr_value));
141 new_lang = (LanguageType)definition.default_uint_value;
142 m_value_sp.reset(new OptionValueLanguage(new_lang));
146 case OptionValue::eTypeFormatEntity:
147 // "definition.default_cstr_value" as a string value that represents the
150 new OptionValueFormatEntity(definition.default_cstr_value));
153 case OptionValue::eTypePathMap:
154 // "definition.default_uint_value" tells us if notifications should occur
157 new OptionValuePathMappings(definition.default_uint_value != 0));
160 case OptionValue::eTypeRegex:
161 // "definition.default_uint_value" is used to the regular expression flags
162 // "definition.default_cstr_value" the default regular expression value
164 m_value_sp.reset(new OptionValueRegex(definition.default_cstr_value));
167 case OptionValue::eTypeSInt64:
168 // "definition.default_uint_value" is the default integer value if
169 // "definition.default_cstr_value" is NULL, otherwise interpret
170 // "definition.default_cstr_value" as a string value that represents the
172 m_value_sp.reset(new OptionValueSInt64(
173 definition.default_cstr_value
174 ? StringConvert::ToSInt64(definition.default_cstr_value)
175 : definition.default_uint_value));
178 case OptionValue::eTypeUInt64:
179 // "definition.default_uint_value" is the default unsigned integer value if
180 // "definition.default_cstr_value" is NULL, otherwise interpret
181 // "definition.default_cstr_value" as a string value that represents the
183 m_value_sp.reset(new OptionValueUInt64(
184 definition.default_cstr_value
185 ? StringConvert::ToUInt64(definition.default_cstr_value)
186 : definition.default_uint_value));
189 case OptionValue::eTypeUUID:
190 // "definition.default_uint_value" is not used for a OptionValue::eTypeUUID
191 // "definition.default_cstr_value" can contain a default UUID value
194 if (definition.default_cstr_value)
195 uuid.SetFromStringRef(definition.default_cstr_value);
196 m_value_sp.reset(new OptionValueUUID(uuid));
200 case OptionValue::eTypeString:
201 // "definition.default_uint_value" can contain the string option flags
202 // OR'ed together "definition.default_cstr_value" can contain a default
205 OptionValueString *string_value =
206 new OptionValueString(definition.default_cstr_value);
207 if (definition.default_uint_value != 0)
208 string_value->GetOptions().Reset(definition.default_uint_value);
209 m_value_sp.reset(string_value);
215 Property::Property(const ConstString &name, const ConstString &desc,
216 bool is_global, const lldb::OptionValueSP &value_sp)
217 : m_name(name), m_description(desc), m_value_sp(value_sp),
218 m_is_global(is_global) {}
220 bool Property::DumpQualifiedName(Stream &strm) const {
222 if (m_value_sp->DumpQualifiedName(strm))
230 void Property::Dump(const ExecutionContext *exe_ctx, Stream &strm,
231 uint32_t dump_mask) const {
233 const bool dump_desc = dump_mask & OptionValue::eDumpOptionDescription;
234 const bool dump_cmd = dump_mask & OptionValue::eDumpOptionCommand;
235 const bool transparent = m_value_sp->ValueIsTransparent();
236 if (dump_cmd && !transparent)
237 strm << "settings set -f ";
238 if (dump_desc || !transparent) {
239 if ((dump_mask & OptionValue::eDumpOptionName) && m_name) {
240 DumpQualifiedName(strm);
241 if (dump_mask & ~OptionValue::eDumpOptionName)
246 llvm::StringRef desc = GetDescription();
248 strm << "-- " << desc;
250 if (transparent && (dump_mask == (OptionValue::eDumpOptionName |
251 OptionValue::eDumpOptionDescription)))
254 m_value_sp->DumpValue(exe_ctx, strm, dump_mask);
258 void Property::DumpDescription(CommandInterpreter &interpreter, Stream &strm,
259 uint32_t output_width,
260 bool display_qualified_name) const {
263 llvm::StringRef desc = GetDescription();
268 StreamString qualified_name;
269 const OptionValueProperties *sub_properties = m_value_sp->GetAsProperties();
270 if (sub_properties) {
273 if (m_value_sp->DumpQualifiedName(qualified_name))
274 strm.Printf("'%s' variables:\n\n", qualified_name.GetData());
275 sub_properties->DumpAllDescriptions(interpreter, strm);
277 if (display_qualified_name) {
278 StreamString qualified_name;
279 DumpQualifiedName(qualified_name);
280 interpreter.OutputFormattedHelpText(strm, qualified_name.GetString(),
281 "--", desc, output_width);
283 interpreter.OutputFormattedHelpText(strm, m_name.GetStringRef(), "--",
289 void Property::SetValueChangedCallback(OptionValueChangedCallback callback,
292 m_value_sp->SetValueChangedCallback(callback, baton);