blob: 0030d375adff864afa744020f97efeb4cf449b06 [file] [log] [blame]
Greg Clayton67cc0632012-08-22 17:17:09 +00001//===-- 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"
Vince Harron5275aaa2015-01-15 20:08:35 +000017#include "lldb/Host/StringConvert.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000018#include "lldb/Interpreter/CommandInterpreter.h"
19#include "lldb/Interpreter/OptionValues.h"
Jim Ingham0e0984e2015-09-02 01:06:46 +000020#include "lldb/Target/Language.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000021
22using namespace lldb;
23using namespace lldb_private;
24
Kate Stoneb9c1b512016-09-06 20:57:50 +000025Property::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
Zachary Turner7b2e5a32016-09-16 19:09:12 +000054 // default value.
Kate Stoneb9c1b512016-09-06 20:57:50 +000055 if (definition.default_cstr_value)
56 m_value_sp.reset(new OptionValueBoolean(Args::StringToBoolean(
57 definition.default_cstr_value, false, nullptr)));
58 else
59 m_value_sp.reset(
60 new OptionValueBoolean(definition.default_uint_value != 0));
61 break;
62
Zachary Turner7b2e5a32016-09-16 19:09:12 +000063 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));
Kate Stoneb9c1b512016-09-06 20:57:50 +000066 break;
Zachary Turner7b2e5a32016-09-16 19:09:12 +000067 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 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.
Greg Clayton67cc0632012-08-22 17:17:09 +000080 {
Kate Stoneb9c1b512016-09-06 20:57:50 +000081 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->SetValueFromString(definition.default_cstr_value)
86 .Success()) {
87 enum_value->SetDefaultValue(enum_value->GetCurrentValue());
88 // Call Clear() since we don't want the value to appear as
89 // having been set since we called SetValueFromString() above.
90 // Clear will set the current value to the default and clear
91 // the boolean that says that the value has been set.
92 enum_value->Clear();
Greg Clayton67cc0632012-08-22 17:17:09 +000093 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000094 }
Greg Clayton67cc0632012-08-22 17:17:09 +000095 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000096 break;
Greg Clayton67cc0632012-08-22 17:17:09 +000097
Kate Stoneb9c1b512016-09-06 20:57:50 +000098 case OptionValue::eTypeFileSpec: {
99 // "definition.default_uint_value" represents if the
100 // "definition.default_cstr_value" should
101 // be resolved or not
102 const bool resolve = definition.default_uint_value != 0;
103 m_value_sp.reset(new OptionValueFileSpec(
104 FileSpec(definition.default_cstr_value, resolve), resolve));
105 break;
106 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000107
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108 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
117 // "definition.default_cstr_value" is NULL, otherwise interpret
118 // "definition.default_cstr_value" as a string value that represents the
119 // default
120 // value.
Greg Clayton67cc0632012-08-22 17:17:09 +0000121 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000122 Format new_format = eFormatInvalid;
123 if (definition.default_cstr_value)
124 Args::StringToFormat(definition.default_cstr_value, new_format,
125 nullptr);
126 else
127 new_format = (Format)definition.default_uint_value;
128 m_value_sp.reset(new OptionValueFormat(new_format));
Greg Clayton67cc0632012-08-22 17:17:09 +0000129 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 break;
Greg Clayton67cc0632012-08-22 17:17:09 +0000131
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132 case OptionValue::eTypeLanguage:
133 // "definition.default_uint_value" is the default language enumeration value
134 // if
135 // "definition.default_cstr_value" is NULL, otherwise interpret
136 // "definition.default_cstr_value" as a string value that represents the
137 // default
138 // value.
Greg Clayton67cc0632012-08-22 17:17:09 +0000139 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140 LanguageType new_lang = eLanguageTypeUnknown;
141 if (definition.default_cstr_value)
142 Language::GetLanguageTypeFromString(definition.default_cstr_value);
143 else
144 new_lang = (LanguageType)definition.default_uint_value;
145 m_value_sp.reset(new OptionValueLanguage(new_lang));
Greg Clayton67cc0632012-08-22 17:17:09 +0000146 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000147 break;
148
149 case OptionValue::eTypeFormatEntity:
150 // "definition.default_cstr_value" as a string value that represents the
151 // default
152 m_value_sp.reset(
153 new OptionValueFormatEntity(definition.default_cstr_value));
154 break;
155
156 case OptionValue::eTypePathMap:
157 // "definition.default_uint_value" tells us if notifications should occur
158 // for
159 // path mappings
160 m_value_sp.reset(
161 new OptionValuePathMappings(definition.default_uint_value != 0));
162 break;
163
164 case OptionValue::eTypeRegex:
165 // "definition.default_uint_value" is used to the regular expression flags
166 // "definition.default_cstr_value" the default regular expression value
167 // value.
168 m_value_sp.reset(new OptionValueRegex(definition.default_cstr_value));
169 break;
170
171 case OptionValue::eTypeSInt64:
172 // "definition.default_uint_value" is the default integer value if
173 // "definition.default_cstr_value" is NULL, otherwise interpret
174 // "definition.default_cstr_value" as a string value that represents the
175 // default
176 // value.
177 m_value_sp.reset(new OptionValueSInt64(
178 definition.default_cstr_value
179 ? StringConvert::ToSInt64(definition.default_cstr_value)
180 : definition.default_uint_value));
181 break;
182
183 case OptionValue::eTypeUInt64:
184 // "definition.default_uint_value" is the default unsigned integer value if
185 // "definition.default_cstr_value" is NULL, otherwise interpret
186 // "definition.default_cstr_value" as a string value that represents the
187 // default
188 // value.
189 m_value_sp.reset(new OptionValueUInt64(
190 definition.default_cstr_value
191 ? StringConvert::ToUInt64(definition.default_cstr_value)
192 : definition.default_uint_value));
193 break;
194
195 case OptionValue::eTypeUUID:
196 // "definition.default_uint_value" is not used for a OptionValue::eTypeUUID
197 // "definition.default_cstr_value" can contain a default UUID value
198 {
199 UUID uuid;
200 if (definition.default_cstr_value)
201 uuid.SetFromCString(definition.default_cstr_value);
202 m_value_sp.reset(new OptionValueUUID(uuid));
203 }
204 break;
205
206 case OptionValue::eTypeString:
207 // "definition.default_uint_value" can contain the string option flags OR'ed
208 // together
209 // "definition.default_cstr_value" can contain a default string value
210 {
211 OptionValueString *string_value =
212 new OptionValueString(definition.default_cstr_value);
213 if (definition.default_uint_value != 0)
214 string_value->GetOptions().Reset(definition.default_uint_value);
215 m_value_sp.reset(string_value);
216 }
217 break;
218 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000219}
220
Kate Stoneb9c1b512016-09-06 20:57:50 +0000221Property::Property(const ConstString &name, const ConstString &desc,
222 bool is_global, const lldb::OptionValueSP &value_sp)
223 : m_name(name), m_description(desc), m_value_sp(value_sp),
224 m_is_global(is_global) {}
Greg Clayton67cc0632012-08-22 17:17:09 +0000225
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226bool Property::DumpQualifiedName(Stream &strm) const {
227 if (m_name) {
228 if (m_value_sp->DumpQualifiedName(strm))
229 strm.PutChar('.');
230 strm << m_name;
231 return true;
232 }
233 return false;
234}
Greg Clayton67cc0632012-08-22 17:17:09 +0000235
Kate Stoneb9c1b512016-09-06 20:57:50 +0000236void Property::Dump(const ExecutionContext *exe_ctx, Stream &strm,
237 uint32_t dump_mask) const {
238 if (m_value_sp) {
239 const bool dump_desc = dump_mask & OptionValue::eDumpOptionDescription;
240 const bool transparent = m_value_sp->ValueIsTransparent();
241 if (dump_desc || !transparent) {
242 if ((dump_mask & OptionValue::eDumpOptionName) && m_name) {
243 DumpQualifiedName(strm);
244 if (dump_mask & ~OptionValue::eDumpOptionName)
245 strm.PutChar(' ');
246 }
247 }
248 if (dump_desc) {
249 const char *desc = GetDescription();
250 if (desc)
251 strm.Printf("-- %s", desc);
252
253 if (transparent && (dump_mask == (OptionValue::eDumpOptionName |
254 OptionValue::eDumpOptionDescription)))
255 strm.EOL();
256 }
257 m_value_sp->DumpValue(exe_ctx, strm, dump_mask);
258 }
259}
260
261void Property::DumpDescription(CommandInterpreter &interpreter, Stream &strm,
262 uint32_t output_width,
263 bool display_qualified_name) const {
264 if (m_value_sp) {
265 const char *desc = GetDescription();
266
267 if (desc) {
268 StreamString qualified_name;
269 const OptionValueProperties *sub_properties =
270 m_value_sp->GetAsProperties();
271 if (sub_properties) {
272 strm.EOL();
273
274 if (m_value_sp->DumpQualifiedName(qualified_name))
275 strm.Printf("'%s' variables:\n\n",
276 qualified_name.GetString().c_str());
277 sub_properties->DumpAllDescriptions(interpreter, strm);
278 } else {
279 if (desc) {
280 if (display_qualified_name) {
Greg Clayton67cc0632012-08-22 17:17:09 +0000281 StreamString qualified_name;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000282 DumpQualifiedName(qualified_name);
283 interpreter.OutputFormattedHelpText(
284 strm, qualified_name.GetString().c_str(), "--", desc,
285 output_width);
286 } else {
287 interpreter.OutputFormattedHelpText(strm, m_name.GetCString(), "--",
288 desc, output_width);
289 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000290 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000292 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000293 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000294}
295
Kate Stoneb9c1b512016-09-06 20:57:50 +0000296void Property::SetValueChangedCallback(OptionValueChangedCallback callback,
297 void *baton) {
298 if (m_value_sp)
299 m_value_sp->SetValueChangedCallback(callback, baton);
Greg Clayton332e8b12015-01-13 21:13:08 +0000300}