blob: 2c3e3728a37f8ae17e3719ebf18c26124065be7f [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"
Pavel Labath47cbf4a2018-04-10 09:03:59 +000019#include "lldb/Interpreter/OptionArgParser.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000020#include "lldb/Interpreter/OptionValues.h"
Jim Ingham0e0984e2015-09-02 01:06:46 +000021#include "lldb/Target/Language.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000022
23using namespace lldb;
24using namespace lldb_private;
25
Kate Stoneb9c1b512016-09-06 20:57:50 +000026Property::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
Zachary Turner7b2e5a32016-09-16 19:09:12 +000055 // default value.
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 if (definition.default_cstr_value)
Pavel Labath47cbf4a2018-04-10 09:03:59 +000057 m_value_sp.reset(new OptionValueBoolean(OptionArgParser::ToBoolean(
Zachary Turnerecbb0bb2016-09-19 17:54:06 +000058 llvm::StringRef(definition.default_cstr_value), false, nullptr)));
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 else
60 m_value_sp.reset(
61 new OptionValueBoolean(definition.default_uint_value != 0));
62 break;
63
Zachary Turner7b2e5a32016-09-16 19:09:12 +000064 case OptionValue::eTypeChar: {
65 llvm::StringRef s(definition.default_cstr_value ? definition.default_cstr_value : "");
Pavel Labath47cbf4a2018-04-10 09:03:59 +000066 m_value_sp = std::make_shared<OptionValueChar>(
67 OptionArgParser::ToChar(s, '\0', nullptr));
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 break;
Zachary Turner7b2e5a32016-09-16 19:09:12 +000069 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000070 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
Adrian Prantl05097242018-04-30 16:49:04 +000080 // default value.
Greg Clayton67cc0632012-08-22 17:17:09 +000081 {
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 OptionValueEnumeration *enum_value = new OptionValueEnumeration(
83 definition.enum_values, definition.default_uint_value);
84 m_value_sp.reset(enum_value);
85 if (definition.default_cstr_value) {
Zachary Turner8cef4b02016-09-23 17:48:13 +000086 if (enum_value
87 ->SetValueFromString(
88 llvm::StringRef(definition.default_cstr_value))
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 .Success()) {
90 enum_value->SetDefaultValue(enum_value->GetCurrentValue());
Adrian Prantl05097242018-04-30 16:49:04 +000091 // Call Clear() since we don't want the value to appear as having
92 // been set since we called SetValueFromString() above. Clear will
93 // set the current value to the default and clear the boolean that
94 // says that the value has been set.
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 enum_value->Clear();
Greg Clayton67cc0632012-08-22 17:17:09 +000096 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000097 }
Greg Clayton67cc0632012-08-22 17:17:09 +000098 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000099 break;
Greg Clayton67cc0632012-08-22 17:17:09 +0000100
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101 case OptionValue::eTypeFileSpec: {
102 // "definition.default_uint_value" represents if the
Adrian Prantl05097242018-04-30 16:49:04 +0000103 // "definition.default_cstr_value" should be resolved or not
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104 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
Adrian Prantl05097242018-04-30 16:49:04 +0000118 // if "definition.default_cstr_value" is NULL, otherwise interpret
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119 // "definition.default_cstr_value" as a string value that represents the
Adrian Prantl05097242018-04-30 16:49:04 +0000120 // default 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)
Pavel Labath47cbf4a2018-04-10 09:03:59 +0000124 OptionArgParser::ToFormat(definition.default_cstr_value, new_format,
125 nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126 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:
Adrian Prantl05097242018-04-30 16:49:04 +0000133 // "definition.default_uint_value" is the default language enumeration
134 // value if "definition.default_cstr_value" is NULL, otherwise interpret
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135 // "definition.default_cstr_value" as a string value that represents the
Adrian Prantl05097242018-04-30 16:49:04 +0000136 // default value.
Greg Clayton67cc0632012-08-22 17:17:09 +0000137 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138 LanguageType new_lang = eLanguageTypeUnknown;
139 if (definition.default_cstr_value)
Zachary Turner6fa7681b2016-09-17 02:00:02 +0000140 Language::GetLanguageTypeFromString(
141 llvm::StringRef(definition.default_cstr_value));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142 else
143 new_lang = (LanguageType)definition.default_uint_value;
144 m_value_sp.reset(new OptionValueLanguage(new_lang));
Greg Clayton67cc0632012-08-22 17:17:09 +0000145 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146 break;
147
148 case OptionValue::eTypeFormatEntity:
149 // "definition.default_cstr_value" as a string value that represents the
150 // default
151 m_value_sp.reset(
152 new OptionValueFormatEntity(definition.default_cstr_value));
153 break;
154
155 case OptionValue::eTypePathMap:
156 // "definition.default_uint_value" tells us if notifications should occur
Adrian Prantl05097242018-04-30 16:49:04 +0000157 // for path mappings
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158 m_value_sp.reset(
159 new OptionValuePathMappings(definition.default_uint_value != 0));
160 break;
161
162 case OptionValue::eTypeRegex:
163 // "definition.default_uint_value" is used to the regular expression flags
164 // "definition.default_cstr_value" the default regular expression value
165 // value.
166 m_value_sp.reset(new OptionValueRegex(definition.default_cstr_value));
167 break;
168
169 case OptionValue::eTypeSInt64:
170 // "definition.default_uint_value" is the default integer value if
171 // "definition.default_cstr_value" is NULL, otherwise interpret
172 // "definition.default_cstr_value" as a string value that represents the
Adrian Prantl05097242018-04-30 16:49:04 +0000173 // default value.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174 m_value_sp.reset(new OptionValueSInt64(
175 definition.default_cstr_value
176 ? StringConvert::ToSInt64(definition.default_cstr_value)
177 : definition.default_uint_value));
178 break;
179
180 case OptionValue::eTypeUInt64:
181 // "definition.default_uint_value" is the default unsigned integer value if
182 // "definition.default_cstr_value" is NULL, otherwise interpret
183 // "definition.default_cstr_value" as a string value that represents the
Adrian Prantl05097242018-04-30 16:49:04 +0000184 // default value.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185 m_value_sp.reset(new OptionValueUInt64(
186 definition.default_cstr_value
187 ? StringConvert::ToUInt64(definition.default_cstr_value)
188 : definition.default_uint_value));
189 break;
190
191 case OptionValue::eTypeUUID:
192 // "definition.default_uint_value" is not used for a OptionValue::eTypeUUID
193 // "definition.default_cstr_value" can contain a default UUID value
194 {
195 UUID uuid;
196 if (definition.default_cstr_value)
Pavel Labatha174bcb2018-06-21 15:24:39 +0000197 uuid.SetFromStringRef(definition.default_cstr_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000198 m_value_sp.reset(new OptionValueUUID(uuid));
199 }
200 break;
201
202 case OptionValue::eTypeString:
Adrian Prantl05097242018-04-30 16:49:04 +0000203 // "definition.default_uint_value" can contain the string option flags
204 // OR'ed together "definition.default_cstr_value" can contain a default
205 // string value
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206 {
207 OptionValueString *string_value =
208 new OptionValueString(definition.default_cstr_value);
209 if (definition.default_uint_value != 0)
210 string_value->GetOptions().Reset(definition.default_uint_value);
211 m_value_sp.reset(string_value);
212 }
213 break;
214 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000215}
216
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217Property::Property(const ConstString &name, const ConstString &desc,
218 bool is_global, const lldb::OptionValueSP &value_sp)
219 : m_name(name), m_description(desc), m_value_sp(value_sp),
220 m_is_global(is_global) {}
Greg Clayton67cc0632012-08-22 17:17:09 +0000221
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222bool Property::DumpQualifiedName(Stream &strm) const {
223 if (m_name) {
224 if (m_value_sp->DumpQualifiedName(strm))
225 strm.PutChar('.');
226 strm << m_name;
227 return true;
228 }
229 return false;
230}
Greg Clayton67cc0632012-08-22 17:17:09 +0000231
Kate Stoneb9c1b512016-09-06 20:57:50 +0000232void Property::Dump(const ExecutionContext *exe_ctx, Stream &strm,
233 uint32_t dump_mask) const {
234 if (m_value_sp) {
235 const bool dump_desc = dump_mask & OptionValue::eDumpOptionDescription;
Jonas Devlieghereb76e25a2018-10-26 00:00:17 +0000236 const bool dump_cmd = dump_mask & OptionValue::eDumpOptionCommand;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000237 const bool transparent = m_value_sp->ValueIsTransparent();
Jonas Devlieghereb76e25a2018-10-26 00:00:17 +0000238 if (dump_cmd && !transparent)
239 strm << "settings set -f ";
Kate Stoneb9c1b512016-09-06 20:57:50 +0000240 if (dump_desc || !transparent) {
241 if ((dump_mask & OptionValue::eDumpOptionName) && m_name) {
242 DumpQualifiedName(strm);
243 if (dump_mask & ~OptionValue::eDumpOptionName)
244 strm.PutChar(' ');
245 }
246 }
247 if (dump_desc) {
Zachary Turnere6f6d4c2016-11-15 23:36:43 +0000248 llvm::StringRef desc = GetDescription();
249 if (!desc.empty())
250 strm << "-- " << desc;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000251
252 if (transparent && (dump_mask == (OptionValue::eDumpOptionName |
253 OptionValue::eDumpOptionDescription)))
254 strm.EOL();
255 }
256 m_value_sp->DumpValue(exe_ctx, strm, dump_mask);
257 }
258}
259
260void Property::DumpDescription(CommandInterpreter &interpreter, Stream &strm,
261 uint32_t output_width,
262 bool display_qualified_name) const {
Zachary Turnere6f6d4c2016-11-15 23:36:43 +0000263 if (!m_value_sp)
264 return;
265 llvm::StringRef desc = GetDescription();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000266
Zachary Turnere6f6d4c2016-11-15 23:36:43 +0000267 if (desc.empty())
268 return;
269
270 StreamString qualified_name;
271 const OptionValueProperties *sub_properties = m_value_sp->GetAsProperties();
272 if (sub_properties) {
273 strm.EOL();
274
275 if (m_value_sp->DumpQualifiedName(qualified_name))
Zachary Turnerc1564272016-11-16 21:15:24 +0000276 strm.Printf("'%s' variables:\n\n", qualified_name.GetData());
Zachary Turnere6f6d4c2016-11-15 23:36:43 +0000277 sub_properties->DumpAllDescriptions(interpreter, strm);
278 } else {
279 if (display_qualified_name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280 StreamString qualified_name;
Zachary Turnere6f6d4c2016-11-15 23:36:43 +0000281 DumpQualifiedName(qualified_name);
Zachary Turnerc1564272016-11-16 21:15:24 +0000282 interpreter.OutputFormattedHelpText(strm, qualified_name.GetString(),
283 "--", desc, output_width);
Zachary Turnere6f6d4c2016-11-15 23:36:43 +0000284 } else {
Zachary Turnerc1564272016-11-16 21:15:24 +0000285 interpreter.OutputFormattedHelpText(strm, m_name.GetStringRef(), "--",
286 desc, output_width);
Greg Clayton67cc0632012-08-22 17:17:09 +0000287 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000288 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000289}
290
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291void Property::SetValueChangedCallback(OptionValueChangedCallback callback,
292 void *baton) {
293 if (m_value_sp)
294 m_value_sp->SetValueChangedCallback(callback, baton);
Greg Clayton332e8b12015-01-13 21:13:08 +0000295}