]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Interpreter/Property.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Interpreter / Property.cpp
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 #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"
18
19 using namespace lldb;
20 using namespace lldb_private;
21
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:
28     break;
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));
34     break;
35
36   case OptionValue::eTypeArgs:
37     // "definition.default_uint_value" is always a OptionValue::Type
38     m_value_sp.reset(new OptionValueArgs());
39     break;
40
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)));
45     break;
46
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
51     // default value.
52     if (definition.default_cstr_value)
53       m_value_sp.reset(new OptionValueBoolean(OptionArgParser::ToBoolean(
54           llvm::StringRef(definition.default_cstr_value), false, nullptr)));
55     else
56       m_value_sp.reset(
57           new OptionValueBoolean(definition.default_uint_value != 0));
58     break;
59
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));
64     break;
65   }
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)));
70     break;
71
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
76     // default value.
77     {
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) {
82         if (enum_value
83                 ->SetValueFromString(
84                     llvm::StringRef(definition.default_cstr_value))
85                 .Success()) {
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.
91           enum_value->Clear();
92         }
93       }
94     }
95     break;
96
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);
102     if (resolve)
103       FileSystem::Instance().Resolve(file_spec);
104     m_value_sp.reset(new OptionValueFileSpec(file_spec, resolve));
105     break;
106   }
107
108   case OptionValue::eTypeFileSpecList:
109     // "definition.default_uint_value" is not used for a
110     // OptionValue::eTypeFileSpecList
111     m_value_sp.reset(new OptionValueFileSpecList());
112     break;
113
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
118     // default value.
119     {
120       Format new_format = eFormatInvalid;
121       if (definition.default_cstr_value)
122         OptionArgParser::ToFormat(definition.default_cstr_value, new_format,
123                                   nullptr);
124       else
125         new_format = (Format)definition.default_uint_value;
126       m_value_sp.reset(new OptionValueFormat(new_format));
127     }
128     break;
129
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
134     // default value.
135     {
136       LanguageType new_lang = eLanguageTypeUnknown;
137       if (definition.default_cstr_value)
138         Language::GetLanguageTypeFromString(
139             llvm::StringRef(definition.default_cstr_value));
140       else
141         new_lang = (LanguageType)definition.default_uint_value;
142       m_value_sp.reset(new OptionValueLanguage(new_lang));
143     }
144     break;
145
146   case OptionValue::eTypeFormatEntity:
147     // "definition.default_cstr_value" as a string value that represents the
148     // default
149     m_value_sp.reset(
150         new OptionValueFormatEntity(definition.default_cstr_value));
151     break;
152
153   case OptionValue::eTypePathMap:
154     // "definition.default_uint_value" tells us if notifications should occur
155     // for path mappings
156     m_value_sp.reset(
157         new OptionValuePathMappings(definition.default_uint_value != 0));
158     break;
159
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
163     // value.
164     m_value_sp.reset(new OptionValueRegex(definition.default_cstr_value));
165     break;
166
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
171     // default value.
172     m_value_sp.reset(new OptionValueSInt64(
173         definition.default_cstr_value
174             ? StringConvert::ToSInt64(definition.default_cstr_value)
175             : definition.default_uint_value));
176     break;
177
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
182     // default value.
183     m_value_sp.reset(new OptionValueUInt64(
184         definition.default_cstr_value
185             ? StringConvert::ToUInt64(definition.default_cstr_value)
186             : definition.default_uint_value));
187     break;
188
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
192     {
193       UUID uuid;
194       if (definition.default_cstr_value)
195         uuid.SetFromStringRef(definition.default_cstr_value);
196       m_value_sp.reset(new OptionValueUUID(uuid));
197     }
198     break;
199
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
203     // string value
204     {
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);
210     }
211     break;
212   }
213 }
214
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) {}
219
220 bool Property::DumpQualifiedName(Stream &strm) const {
221   if (m_name) {
222     if (m_value_sp->DumpQualifiedName(strm))
223       strm.PutChar('.');
224     strm << m_name;
225     return true;
226   }
227   return false;
228 }
229
230 void Property::Dump(const ExecutionContext *exe_ctx, Stream &strm,
231                     uint32_t dump_mask) const {
232   if (m_value_sp) {
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)
242           strm.PutChar(' ');
243       }
244     }
245     if (dump_desc) {
246       llvm::StringRef desc = GetDescription();
247       if (!desc.empty())
248         strm << "-- " << desc;
249
250       if (transparent && (dump_mask == (OptionValue::eDumpOptionName |
251                                         OptionValue::eDumpOptionDescription)))
252         strm.EOL();
253     }
254     m_value_sp->DumpValue(exe_ctx, strm, dump_mask);
255   }
256 }
257
258 void Property::DumpDescription(CommandInterpreter &interpreter, Stream &strm,
259                                uint32_t output_width,
260                                bool display_qualified_name) const {
261   if (!m_value_sp)
262     return;
263   llvm::StringRef desc = GetDescription();
264
265   if (desc.empty())
266     return;
267
268   StreamString qualified_name;
269   const OptionValueProperties *sub_properties = m_value_sp->GetAsProperties();
270   if (sub_properties) {
271     strm.EOL();
272
273     if (m_value_sp->DumpQualifiedName(qualified_name))
274       strm.Printf("'%s' variables:\n\n", qualified_name.GetData());
275     sub_properties->DumpAllDescriptions(interpreter, strm);
276   } else {
277     if (display_qualified_name) {
278       StreamString qualified_name;
279       DumpQualifiedName(qualified_name);
280       interpreter.OutputFormattedHelpText(strm, qualified_name.GetString(),
281                                           "--", desc, output_width);
282     } else {
283       interpreter.OutputFormattedHelpText(strm, m_name.GetStringRef(), "--",
284                                           desc, output_width);
285     }
286   }
287 }
288
289 void Property::SetValueChangedCallback(OptionValueChangedCallback callback,
290                                        void *baton) {
291   if (m_value_sp)
292     m_value_sp->SetValueChangedCallback(callback, baton);
293 }