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