blob: b6f6507083dd52fd7e95b538ee7c8bb80c5fb2af [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(
Zachary Turnerecbb0bb2016-09-19 17:54:06 +000057 llvm::StringRef(definition.default_cstr_value), false, nullptr)));
Kate Stoneb9c1b512016-09-06 20:57:50 +000058 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) {
Zachary Turner8cef4b02016-09-23 17:48:13 +000085 if (enum_value
86 ->SetValueFromString(
87 llvm::StringRef(definition.default_cstr_value))
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 .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();
Greg Clayton67cc0632012-08-22 17:17:09 +000095 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000096 }
Greg Clayton67cc0632012-08-22 17:17:09 +000097 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000098 break;
Greg Clayton67cc0632012-08-22 17:17:09 +000099
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100 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 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000109
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110 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.
Greg Clayton67cc0632012-08-22 17:17:09 +0000123 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124 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));
Greg Clayton67cc0632012-08-22 17:17:09 +0000131 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132 break;
Greg Clayton67cc0632012-08-22 17:17:09 +0000133
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 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.
Greg Clayton67cc0632012-08-22 17:17:09 +0000141 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142 LanguageType new_lang = eLanguageTypeUnknown;
143 if (definition.default_cstr_value)
Zachary Turner6fa7681b2016-09-17 02:00:02 +0000144 Language::GetLanguageTypeFromString(
145 llvm::StringRef(definition.default_cstr_value));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146 else
147 new_lang = (LanguageType)definition.default_uint_value;
148 m_value_sp.reset(new OptionValueLanguage(new_lang));
Greg Clayton67cc0632012-08-22 17:17:09 +0000149 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150 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 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000222}
223
Kate Stoneb9c1b512016-09-06 20:57:50 +0000224Property::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) {}
Greg Clayton67cc0632012-08-22 17:17:09 +0000228
Kate Stoneb9c1b512016-09-06 20:57:50 +0000229bool 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}
Greg Clayton67cc0632012-08-22 17:17:09 +0000238
Kate Stoneb9c1b512016-09-06 20:57:50 +0000239void 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) {
Zachary Turnere6f6d4c2016-11-15 23:36:43 +0000252 llvm::StringRef desc = GetDescription();
253 if (!desc.empty())
254 strm << "-- " << desc;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000255
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 {
Zachary Turnere6f6d4c2016-11-15 23:36:43 +0000267 if (!m_value_sp)
268 return;
269 llvm::StringRef desc = GetDescription();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270
Zachary Turnere6f6d4c2016-11-15 23:36:43 +0000271 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.GetString().c_str());
281 sub_properties->DumpAllDescriptions(interpreter, strm);
282 } else {
283 if (display_qualified_name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000284 StreamString qualified_name;
Zachary Turnere6f6d4c2016-11-15 23:36:43 +0000285 DumpQualifiedName(qualified_name);
286 interpreter.OutputFormattedHelpText(
287 strm, qualified_name.GetString().c_str(), "--", desc, output_width);
288 } else {
289 interpreter.OutputFormattedHelpText(strm, m_name.GetCString(), "--", desc,
290 output_width);
Greg Clayton67cc0632012-08-22 17:17:09 +0000291 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000292 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000293}
294
Kate Stoneb9c1b512016-09-06 20:57:50 +0000295void Property::SetValueChangedCallback(OptionValueChangedCallback callback,
296 void *baton) {
297 if (m_value_sp)
298 m_value_sp->SetValueChangedCallback(callback, baton);
Greg Clayton332e8b12015-01-13 21:13:08 +0000299}