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