1 //===-- Property.cpp --------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/Interpreter/Property.h"
11 
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project includes
16 #include "lldb/Core/UserSettingsController.h"
17 #include "lldb/Host/StringConvert.h"
18 #include "lldb/Interpreter/CommandInterpreter.h"
19 #include "lldb/Interpreter/OptionArgParser.h"
20 #include "lldb/Interpreter/OptionValues.h"
21 #include "lldb/Target/Language.h"
22 
23 using namespace lldb;
24 using namespace lldb_private;
25 
26 Property::Property(const PropertyDefinition &definition)
27     : m_name(definition.name), m_description(definition.description),
28       m_value_sp(), m_is_global(definition.global) {
29   switch (definition.type) {
30   case OptionValue::eTypeInvalid:
31   case OptionValue::eTypeProperties:
32     break;
33   case OptionValue::eTypeArch:
34     // "definition.default_uint_value" is not used
35     // "definition.default_cstr_value" as a string value that represents the
36     // default string value for the architecture/triple
37     m_value_sp.reset(new OptionValueArch(definition.default_cstr_value));
38     break;
39 
40   case OptionValue::eTypeArgs:
41     // "definition.default_uint_value" is always a OptionValue::Type
42     m_value_sp.reset(new OptionValueArgs());
43     break;
44 
45   case OptionValue::eTypeArray:
46     // "definition.default_uint_value" is always a OptionValue::Type
47     m_value_sp.reset(new OptionValueArray(OptionValue::ConvertTypeToMask(
48         (OptionValue::Type)definition.default_uint_value)));
49     break;
50 
51   case OptionValue::eTypeBoolean:
52     // "definition.default_uint_value" is the default boolean value if
53     // "definition.default_cstr_value" is NULL, otherwise interpret
54     // "definition.default_cstr_value" as a string value that represents the
55     // default value.
56     if (definition.default_cstr_value)
57       m_value_sp.reset(new OptionValueBoolean(OptionArgParser::ToBoolean(
58           llvm::StringRef(definition.default_cstr_value), false, nullptr)));
59     else
60       m_value_sp.reset(
61           new OptionValueBoolean(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   }
70   case OptionValue::eTypeDictionary:
71     // "definition.default_uint_value" is always a OptionValue::Type
72     m_value_sp.reset(new OptionValueDictionary(OptionValue::ConvertTypeToMask(
73         (OptionValue::Type)definition.default_uint_value)));
74     break;
75 
76   case OptionValue::eTypeEnum:
77     // "definition.default_uint_value" is the default enumeration value if
78     // "definition.default_cstr_value" is NULL, otherwise interpret
79     // "definition.default_cstr_value" as a string value that represents the
80     // default
81     // value.
82     {
83       OptionValueEnumeration *enum_value = new OptionValueEnumeration(
84           definition.enum_values, definition.default_uint_value);
85       m_value_sp.reset(enum_value);
86       if (definition.default_cstr_value) {
87         if (enum_value
88                 ->SetValueFromString(
89                     llvm::StringRef(definition.default_cstr_value))
90                 .Success()) {
91           enum_value->SetDefaultValue(enum_value->GetCurrentValue());
92           // Call Clear() since we don't want the value to appear as
93           // having been set since we called SetValueFromString() above.
94           // Clear will set the current value to the default and clear
95           // the boolean that says that the value has been set.
96           enum_value->Clear();
97         }
98       }
99     }
100     break;
101 
102   case OptionValue::eTypeFileSpec: {
103     // "definition.default_uint_value" represents if the
104     // "definition.default_cstr_value" should
105     // be resolved or not
106     const bool resolve = definition.default_uint_value != 0;
107     m_value_sp.reset(new OptionValueFileSpec(
108         FileSpec(definition.default_cstr_value, resolve), resolve));
109     break;
110   }
111 
112   case OptionValue::eTypeFileSpecList:
113     // "definition.default_uint_value" is not used for a
114     // OptionValue::eTypeFileSpecList
115     m_value_sp.reset(new OptionValueFileSpecList());
116     break;
117 
118   case OptionValue::eTypeFormat:
119     // "definition.default_uint_value" is the default format enumeration value
120     // if
121     // "definition.default_cstr_value" is NULL, otherwise interpret
122     // "definition.default_cstr_value" as a string value that represents the
123     // default
124     // value.
125     {
126       Format new_format = eFormatInvalid;
127       if (definition.default_cstr_value)
128         OptionArgParser::ToFormat(definition.default_cstr_value, new_format,
129                                   nullptr);
130       else
131         new_format = (Format)definition.default_uint_value;
132       m_value_sp.reset(new OptionValueFormat(new_format));
133     }
134     break;
135 
136   case OptionValue::eTypeLanguage:
137     // "definition.default_uint_value" is the default language enumeration value
138     // if
139     // "definition.default_cstr_value" is NULL, otherwise interpret
140     // "definition.default_cstr_value" as a string value that represents the
141     // default
142     // value.
143     {
144       LanguageType new_lang = eLanguageTypeUnknown;
145       if (definition.default_cstr_value)
146         Language::GetLanguageTypeFromString(
147             llvm::StringRef(definition.default_cstr_value));
148       else
149         new_lang = (LanguageType)definition.default_uint_value;
150       m_value_sp.reset(new OptionValueLanguage(new_lang));
151     }
152     break;
153 
154   case OptionValue::eTypeFormatEntity:
155     // "definition.default_cstr_value" as a string value that represents the
156     // default
157     m_value_sp.reset(
158         new OptionValueFormatEntity(definition.default_cstr_value));
159     break;
160 
161   case OptionValue::eTypePathMap:
162     // "definition.default_uint_value" tells us if notifications should occur
163     // for
164     // path mappings
165     m_value_sp.reset(
166         new OptionValuePathMappings(definition.default_uint_value != 0));
167     break;
168 
169   case OptionValue::eTypeRegex:
170     // "definition.default_uint_value" is used to the regular expression flags
171     // "definition.default_cstr_value" the default regular expression value
172     // value.
173     m_value_sp.reset(new OptionValueRegex(definition.default_cstr_value));
174     break;
175 
176   case OptionValue::eTypeSInt64:
177     // "definition.default_uint_value" is the default integer value if
178     // "definition.default_cstr_value" is NULL, otherwise interpret
179     // "definition.default_cstr_value" as a string value that represents the
180     // default
181     // value.
182     m_value_sp.reset(new OptionValueSInt64(
183         definition.default_cstr_value
184             ? StringConvert::ToSInt64(definition.default_cstr_value)
185             : definition.default_uint_value));
186     break;
187 
188   case OptionValue::eTypeUInt64:
189     // "definition.default_uint_value" is the default unsigned integer value if
190     // "definition.default_cstr_value" is NULL, otherwise interpret
191     // "definition.default_cstr_value" as a string value that represents the
192     // default
193     // value.
194     m_value_sp.reset(new OptionValueUInt64(
195         definition.default_cstr_value
196             ? StringConvert::ToUInt64(definition.default_cstr_value)
197             : definition.default_uint_value));
198     break;
199 
200   case OptionValue::eTypeUUID:
201     // "definition.default_uint_value" is not used for a OptionValue::eTypeUUID
202     // "definition.default_cstr_value" can contain a default UUID value
203     {
204       UUID uuid;
205       if (definition.default_cstr_value)
206         uuid.SetFromCString(definition.default_cstr_value);
207       m_value_sp.reset(new OptionValueUUID(uuid));
208     }
209     break;
210 
211   case OptionValue::eTypeString:
212     // "definition.default_uint_value" can contain the string option flags OR'ed
213     // together
214     // "definition.default_cstr_value" can contain a default string value
215     {
216       OptionValueString *string_value =
217           new OptionValueString(definition.default_cstr_value);
218       if (definition.default_uint_value != 0)
219         string_value->GetOptions().Reset(definition.default_uint_value);
220       m_value_sp.reset(string_value);
221     }
222     break;
223   }
224 }
225 
226 Property::Property(const ConstString &name, const ConstString &desc,
227                    bool is_global, const lldb::OptionValueSP &value_sp)
228     : m_name(name), m_description(desc), m_value_sp(value_sp),
229       m_is_global(is_global) {}
230 
231 bool Property::DumpQualifiedName(Stream &strm) const {
232   if (m_name) {
233     if (m_value_sp->DumpQualifiedName(strm))
234       strm.PutChar('.');
235     strm << m_name;
236     return true;
237   }
238   return false;
239 }
240 
241 void Property::Dump(const ExecutionContext *exe_ctx, Stream &strm,
242                     uint32_t dump_mask) const {
243   if (m_value_sp) {
244     const bool dump_desc = dump_mask & OptionValue::eDumpOptionDescription;
245     const bool transparent = m_value_sp->ValueIsTransparent();
246     if (dump_desc || !transparent) {
247       if ((dump_mask & OptionValue::eDumpOptionName) && m_name) {
248         DumpQualifiedName(strm);
249         if (dump_mask & ~OptionValue::eDumpOptionName)
250           strm.PutChar(' ');
251       }
252     }
253     if (dump_desc) {
254       llvm::StringRef desc = GetDescription();
255       if (!desc.empty())
256         strm << "-- " << desc;
257 
258       if (transparent && (dump_mask == (OptionValue::eDumpOptionName |
259                                         OptionValue::eDumpOptionDescription)))
260         strm.EOL();
261     }
262     m_value_sp->DumpValue(exe_ctx, strm, dump_mask);
263   }
264 }
265 
266 void Property::DumpDescription(CommandInterpreter &interpreter, Stream &strm,
267                                uint32_t output_width,
268                                bool display_qualified_name) const {
269   if (!m_value_sp)
270     return;
271   llvm::StringRef desc = GetDescription();
272 
273   if (desc.empty())
274     return;
275 
276   StreamString qualified_name;
277   const OptionValueProperties *sub_properties = m_value_sp->GetAsProperties();
278   if (sub_properties) {
279     strm.EOL();
280 
281     if (m_value_sp->DumpQualifiedName(qualified_name))
282       strm.Printf("'%s' variables:\n\n", qualified_name.GetData());
283     sub_properties->DumpAllDescriptions(interpreter, strm);
284   } else {
285     if (display_qualified_name) {
286       StreamString qualified_name;
287       DumpQualifiedName(qualified_name);
288       interpreter.OutputFormattedHelpText(strm, qualified_name.GetString(),
289                                           "--", desc, output_width);
290     } else {
291       interpreter.OutputFormattedHelpText(strm, m_name.GetStringRef(), "--",
292                                           desc, output_width);
293     }
294   }
295 }
296 
297 void Property::SetValueChangedCallback(OptionValueChangedCallback callback,
298                                        void *baton) {
299   if (m_value_sp)
300     m_value_sp->SetValueChangedCallback(callback, baton);
301 }
302