1 //===-- OptionValueProperties.cpp ---------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Interpreter/OptionValueProperties.h"
14 // Other libraries and framework includes
16 #include "lldb/Core/Flags.h"
17 #include "lldb/Core/Stream.h"
18 #include "lldb/Core/StringList.h"
19 #include "lldb/Core/UserSettingsController.h"
20 #include "lldb/Interpreter/Args.h"
21 #include "lldb/Interpreter/OptionValues.h"
22 #include "lldb/Interpreter/Property.h"
25 using namespace lldb_private;
28 OptionValueProperties::OptionValueProperties (const ConstString &name) :
36 OptionValueProperties::OptionValueProperties (const OptionValueProperties &global_properties) :
37 OptionValue (global_properties),
38 std::enable_shared_from_this<OptionValueProperties> (),
39 m_name (global_properties.m_name),
40 m_properties (global_properties.m_properties),
41 m_name_to_index (global_properties.m_name_to_index)
43 // We now have an exact copy of "global_properties". We need to now
44 // find all non-global settings and copy the property values so that
45 // all non-global settings get new OptionValue instances created for
47 const size_t num_properties = m_properties.size();
48 for (size_t i=0; i<num_properties; ++i)
50 // Duplicate any values that are not global when constructing properties from
52 if (m_properties[i].IsGlobal() == false)
54 lldb::OptionValueSP new_value_sp (m_properties[i].GetValue()->DeepCopy());
55 m_properties[i].SetOptionValue(new_value_sp);
63 OptionValueProperties::GetNumProperties() const
65 return m_properties.size();
70 OptionValueProperties::Initialize (const PropertyDefinition *defs)
72 for (size_t i=0; defs[i].name; ++i)
74 Property property(defs[i]);
75 assert(property.IsValid());
76 m_name_to_index.Append(property.GetName().GetCString(),m_properties.size());
77 property.GetValue()->SetParent(shared_from_this());
78 m_properties.push_back(property);
80 m_name_to_index.Sort();
84 OptionValueProperties::SetValueChangedCallback (uint32_t property_idx,
85 OptionValueChangedCallback callback,
88 Property *property = ProtectedGetPropertyAtIndex (property_idx);
90 property->SetValueChangedCallback (callback, baton);
94 OptionValueProperties::AppendProperty(const ConstString &name,
95 const ConstString &desc,
97 const OptionValueSP &value_sp)
99 Property property(name, desc, is_global, value_sp);
100 m_name_to_index.Append(name.GetCString(),m_properties.size());
101 m_properties.push_back(property);
102 value_sp->SetParent (shared_from_this());
103 m_name_to_index.Sort();
109 //OptionValueProperties::GetQualifiedName (Stream &strm)
111 // bool dumped_something = false;
112 //// lldb::OptionValuePropertiesSP parent_sp(GetParent ());
115 //// parent_sp->GetQualifiedName (strm);
116 //// strm.PutChar('.');
117 //// dumped_something = true;
122 // dumped_something = true;
124 // return dumped_something;
128 OptionValueProperties::GetValueForKey (const ExecutionContext *exe_ctx,
129 const ConstString &key,
130 bool will_modify) const
132 lldb::OptionValueSP value_sp;
133 size_t idx = m_name_to_index.Find (key.GetCString(), SIZE_MAX);
134 if (idx < m_properties.size())
135 value_sp = GetPropertyAtIndex(exe_ctx, will_modify, idx)->GetValue();
140 OptionValueProperties::GetSubValue (const ExecutionContext *exe_ctx,
145 lldb::OptionValueSP value_sp;
149 const char *sub_name = nullptr;
151 size_t key_len = ::strcspn (name, ".[{");
155 key.SetCStringWithLength (name, key_len);
156 sub_name = name + key_len;
159 key.SetCString (name);
161 value_sp = GetValueForKey (exe_ctx, key, will_modify);
162 if (sub_name && value_sp)
167 return value_sp->GetSubValue (exe_ctx, sub_name + 1, will_modify, error);
170 // Predicate matching for predicates like
171 // "<setting-name>{<predicate>}"
172 // strings are parsed by the current OptionValueProperties subclass
173 // to mean whatever they want to. For instance a subclass of
174 // OptionValueProperties for a lldb_private::Target might implement:
175 // "target.run-args{arch==i386}" -- only set run args if the arch is i386
176 // "target.run-args{path=/tmp/a/b/c/a.out}" -- only set run args if the path matches
177 // "target.run-args{basename==test&&arch==x86_64}" -- only set run args if executable basename is "test" and arch is "x86_64"
180 const char *predicate_start = sub_name + 1;
181 const char *predicate_end = strchr(predicate_start, '}');
184 std::string predicate(predicate_start, predicate_end);
185 if (PredicateMatches(exe_ctx, predicate.c_str()))
187 if (predicate_end[1])
189 // Still more subvalue string to evaluate
190 return value_sp->GetSubValue (exe_ctx, predicate_end + 1, will_modify, error);
200 // Predicate didn't match or wasn't correctly formed
205 // Array or dictionary access for subvalues like:
206 // "[12]" -- access 12th array element
207 // "['hello']" -- dictionary access of key named hello
208 return value_sp->GetSubValue (exe_ctx, sub_name, will_modify, error);
220 OptionValueProperties::SetSubValue (const ExecutionContext *exe_ctx,
221 VarSetOperationType op,
226 const bool will_modify = true;
227 lldb::OptionValueSP value_sp (GetSubValue (exe_ctx, name, will_modify, error));
229 error = value_sp->SetValueFromString(value ? llvm::StringRef(value) : llvm::StringRef(), op);
232 if (error.AsCString() == nullptr)
233 error.SetErrorStringWithFormat("invalid value path '%s'", name);
240 OptionValueProperties::GetPropertyNameAtIndex (uint32_t idx) const
242 const Property *property = GetPropertyAtIndex(nullptr, false, idx);
244 return property->GetName();
245 return ConstString();
250 OptionValueProperties::GetPropertyDescriptionAtIndex (uint32_t idx) const
252 const Property *property = GetPropertyAtIndex(nullptr, false, idx);
254 return property->GetDescription();
259 OptionValueProperties::GetPropertyIndex (const ConstString &name) const
261 return m_name_to_index.Find (name.GetCString(), SIZE_MAX);
265 OptionValueProperties::GetProperty (const ExecutionContext *exe_ctx, bool will_modify, const ConstString &name) const
267 return GetPropertyAtIndex (exe_ctx, will_modify, m_name_to_index.Find (name.GetCString(), SIZE_MAX));
271 OptionValueProperties::GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
273 return ProtectedGetPropertyAtIndex (idx);
277 OptionValueProperties::GetPropertyValueAtIndex (const ExecutionContext *exe_ctx,
281 const Property *setting = GetPropertyAtIndex (exe_ctx, will_modify, idx);
283 return setting->GetValue();
284 return OptionValueSP();
287 OptionValuePathMappings *
288 OptionValueProperties::GetPropertyAtIndexAsOptionValuePathMappings (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
290 OptionValueSP value_sp(GetPropertyValueAtIndex (exe_ctx, will_modify, idx));
292 return value_sp->GetAsPathMappings();
296 OptionValueFileSpecList *
297 OptionValueProperties::GetPropertyAtIndexAsOptionValueFileSpecList (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
299 OptionValueSP value_sp(GetPropertyValueAtIndex (exe_ctx, will_modify, idx));
301 return value_sp->GetAsFileSpecList();
306 OptionValueProperties::GetPropertyAtIndexAsOptionValueArch (const ExecutionContext *exe_ctx, uint32_t idx) const
308 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
310 return property->GetValue()->GetAsArch();
315 OptionValueProperties::GetPropertyAtIndexAsArgs (const ExecutionContext *exe_ctx, uint32_t idx, Args &args) const
317 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
320 OptionValue *value = property->GetValue().get();
323 const OptionValueArray *array = value->GetAsArray();
325 return array->GetArgs(args);
328 const OptionValueDictionary *dict = value->GetAsDictionary();
330 return dict->GetArgs(args);
338 OptionValueProperties::SetPropertyAtIndexFromArgs (const ExecutionContext *exe_ctx, uint32_t idx, const Args &args)
340 const Property *property = GetPropertyAtIndex (exe_ctx, true, idx);
343 OptionValue *value = property->GetValue().get();
346 OptionValueArray *array = value->GetAsArray();
348 return array->SetArgs(args, eVarSetOperationAssign).Success();
351 OptionValueDictionary *dict = value->GetAsDictionary();
353 return dict->SetArgs(args, eVarSetOperationAssign).Success();
361 OptionValueProperties::GetPropertyAtIndexAsBoolean (const ExecutionContext *exe_ctx, uint32_t idx, bool fail_value) const
363 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
366 OptionValue *value = property->GetValue().get();
368 return value->GetBooleanValue(fail_value);
374 OptionValueProperties::SetPropertyAtIndexAsBoolean (const ExecutionContext *exe_ctx, uint32_t idx, bool new_value)
376 const Property *property = GetPropertyAtIndex (exe_ctx, true, idx);
379 OptionValue *value = property->GetValue().get();
382 value->SetBooleanValue(new_value);
389 OptionValueDictionary *
390 OptionValueProperties::GetPropertyAtIndexAsOptionValueDictionary (const ExecutionContext *exe_ctx, uint32_t idx) const
392 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
394 return property->GetValue()->GetAsDictionary();
399 OptionValueProperties::GetPropertyAtIndexAsEnumeration (const ExecutionContext *exe_ctx, uint32_t idx, int64_t fail_value) const
401 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
404 OptionValue *value = property->GetValue().get();
406 return value->GetEnumerationValue(fail_value);
412 OptionValueProperties::SetPropertyAtIndexAsEnumeration (const ExecutionContext *exe_ctx, uint32_t idx, int64_t new_value)
414 const Property *property = GetPropertyAtIndex (exe_ctx, true, idx);
417 OptionValue *value = property->GetValue().get();
419 return value->SetEnumerationValue(new_value);
424 const FormatEntity::Entry *
425 OptionValueProperties::GetPropertyAtIndexAsFormatEntity (const ExecutionContext *exe_ctx, uint32_t idx)
427 const Property *property = GetPropertyAtIndex (exe_ctx, true, idx);
430 OptionValue *value = property->GetValue().get();
432 return value->GetFormatEntity();
437 OptionValueFileSpec *
438 OptionValueProperties::GetPropertyAtIndexAsOptionValueFileSpec (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
440 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
443 OptionValue *value = property->GetValue().get();
445 return value->GetAsFileSpec();
452 OptionValueProperties::GetPropertyAtIndexAsFileSpec (const ExecutionContext *exe_ctx, uint32_t idx) const
454 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
457 OptionValue *value = property->GetValue().get();
459 return value->GetFileSpecValue();
466 OptionValueProperties::SetPropertyAtIndexAsFileSpec (const ExecutionContext *exe_ctx, uint32_t idx, const FileSpec &new_file_spec)
468 const Property *property = GetPropertyAtIndex (exe_ctx, true, idx);
471 OptionValue *value = property->GetValue().get();
473 return value->SetFileSpecValue(new_file_spec);
478 const RegularExpression *
479 OptionValueProperties::GetPropertyAtIndexAsOptionValueRegex (const ExecutionContext *exe_ctx, uint32_t idx) const
481 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
484 OptionValue *value = property->GetValue().get();
486 return value->GetRegexValue();
492 OptionValueProperties::GetPropertyAtIndexAsOptionValueSInt64 (const ExecutionContext *exe_ctx, uint32_t idx) const
494 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
497 OptionValue *value = property->GetValue().get();
499 return value->GetAsSInt64();
505 OptionValueProperties::GetPropertyAtIndexAsSInt64 (const ExecutionContext *exe_ctx, uint32_t idx, int64_t fail_value) const
507 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
510 OptionValue *value = property->GetValue().get();
512 return value->GetSInt64Value(fail_value);
518 OptionValueProperties::SetPropertyAtIndexAsSInt64 (const ExecutionContext *exe_ctx, uint32_t idx, int64_t new_value)
520 const Property *property = GetPropertyAtIndex (exe_ctx, true, idx);
523 OptionValue *value = property->GetValue().get();
525 return value->SetSInt64Value(new_value);
531 OptionValueProperties::GetPropertyAtIndexAsString (const ExecutionContext *exe_ctx, uint32_t idx, const char *fail_value) const
533 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
536 OptionValue *value = property->GetValue().get();
538 return value->GetStringValue(fail_value);
544 OptionValueProperties::SetPropertyAtIndexAsString (const ExecutionContext *exe_ctx, uint32_t idx, const char *new_value)
546 const Property *property = GetPropertyAtIndex (exe_ctx, true, idx);
549 OptionValue *value = property->GetValue().get();
551 return value->SetStringValue(new_value);
557 OptionValueProperties::GetPropertyAtIndexAsOptionValueString (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
559 OptionValueSP value_sp(GetPropertyValueAtIndex (exe_ctx, will_modify, idx));
561 return value_sp->GetAsString();
567 OptionValueProperties::GetPropertyAtIndexAsUInt64 (const ExecutionContext *exe_ctx, uint32_t idx, uint64_t fail_value) const
569 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
572 OptionValue *value = property->GetValue().get();
574 return value->GetUInt64Value(fail_value);
580 OptionValueProperties::SetPropertyAtIndexAsUInt64 (const ExecutionContext *exe_ctx, uint32_t idx, uint64_t new_value)
582 const Property *property = GetPropertyAtIndex (exe_ctx, true, idx);
585 OptionValue *value = property->GetValue().get();
587 return value->SetUInt64Value(new_value);
593 OptionValueProperties::Clear ()
595 const size_t num_properties = m_properties.size();
596 for (size_t i=0; i<num_properties; ++i)
597 m_properties[i].GetValue()->Clear();
603 OptionValueProperties::SetValueFromString (llvm::StringRef value, VarSetOperationType op)
607 // Args args(value_cstr);
608 // const size_t argc = args.GetArgumentCount();
611 case eVarSetOperationClear:
615 case eVarSetOperationReplace:
616 case eVarSetOperationAssign:
617 case eVarSetOperationRemove:
618 case eVarSetOperationInsertBefore:
619 case eVarSetOperationInsertAfter:
620 case eVarSetOperationAppend:
621 case eVarSetOperationInvalid:
622 error = OptionValue::SetValueFromString (value, op);
630 OptionValueProperties::DumpValue (const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask)
632 const size_t num_properties = m_properties.size();
633 for (size_t i=0; i<num_properties; ++i)
635 const Property *property = GetPropertyAtIndex(exe_ctx, false, i);
638 OptionValue *option_value = property->GetValue().get();
639 assert (option_value);
640 const bool transparent_value = option_value->ValueIsTransparent ();
641 property->Dump (exe_ctx,
644 if (!transparent_value)
651 OptionValueProperties::DumpPropertyValue (const ExecutionContext *exe_ctx,
653 const char *property_path,
657 const bool will_modify = false;
658 lldb::OptionValueSP value_sp (GetSubValue (exe_ctx, property_path, will_modify, error));
661 if (!value_sp->ValueIsTransparent ())
663 if (dump_mask & eDumpOptionName)
664 strm.PutCString (property_path);
665 if (dump_mask & ~eDumpOptionName)
668 value_sp->DumpValue (exe_ctx, strm, dump_mask);
674 OptionValueProperties::DeepCopy () const
676 assert(!"this shouldn't happen");
677 return lldb::OptionValueSP();
681 OptionValueProperties::GetPropertyAtPath (const ExecutionContext *exe_ctx,
683 const char *name) const
685 const Property *property = nullptr;
688 const char *sub_name = nullptr;
690 size_t key_len = ::strcspn (name, ".[{");
694 key.SetCStringWithLength (name, key_len);
695 sub_name = name + key_len;
698 key.SetCString (name);
700 property = GetProperty (exe_ctx, will_modify, key);
701 if (sub_name && property)
703 if (sub_name[0] == '.')
705 OptionValueProperties *sub_properties = property->GetValue()->GetAsProperties();
707 return sub_properties->GetPropertyAtPath(exe_ctx, will_modify, sub_name + 1);
716 OptionValueProperties::DumpAllDescriptions (CommandInterpreter &interpreter,
719 size_t max_name_len = 0;
720 const size_t num_properties = m_properties.size();
721 for (size_t i=0; i<num_properties; ++i)
723 const Property *property = ProtectedGetPropertyAtIndex(i);
725 max_name_len = std::max<size_t>(property->GetName().GetLength(), max_name_len);
727 for (size_t i=0; i<num_properties; ++i)
729 const Property *property = ProtectedGetPropertyAtIndex(i);
731 property->DumpDescription (interpreter, strm, max_name_len, false);
736 OptionValueProperties::Apropos (const char *keyword, std::vector<const Property *> &matching_properties) const
738 const size_t num_properties = m_properties.size();
740 for (size_t i=0; i<num_properties; ++i)
742 const Property *property = ProtectedGetPropertyAtIndex(i);
745 const OptionValueProperties *properties = property->GetValue()->GetAsProperties();
748 properties->Apropos (keyword, matching_properties);
753 const char *name = property->GetName().GetCString();
754 if (name && ::strcasestr(name, keyword))
758 const char *desc = property->GetDescription();
759 if (desc && ::strcasestr(desc, keyword))
764 matching_properties.push_back (property);
771 lldb::OptionValuePropertiesSP
772 OptionValueProperties::GetSubProperty (const ExecutionContext *exe_ctx,
773 const ConstString &name)
775 lldb::OptionValueSP option_value_sp(GetValueForKey(exe_ctx, name, false));
778 OptionValueProperties *ov_properties = option_value_sp->GetAsProperties ();
780 return ov_properties->shared_from_this();
782 return lldb::OptionValuePropertiesSP();