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();
314 OptionValueLanguage *
315 OptionValueProperties::GetPropertyAtIndexAsOptionValueLanguage (const ExecutionContext *exe_ctx, uint32_t idx) const
317 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
319 return property->GetValue()->GetAsLanguage();
324 OptionValueProperties::GetPropertyAtIndexAsArgs (const ExecutionContext *exe_ctx, uint32_t idx, Args &args) const
326 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
329 OptionValue *value = property->GetValue().get();
332 const OptionValueArray *array = value->GetAsArray();
334 return array->GetArgs(args);
337 const OptionValueDictionary *dict = value->GetAsDictionary();
339 return dict->GetArgs(args);
347 OptionValueProperties::SetPropertyAtIndexFromArgs (const ExecutionContext *exe_ctx, uint32_t idx, const Args &args)
349 const Property *property = GetPropertyAtIndex (exe_ctx, true, idx);
352 OptionValue *value = property->GetValue().get();
355 OptionValueArray *array = value->GetAsArray();
357 return array->SetArgs(args, eVarSetOperationAssign).Success();
360 OptionValueDictionary *dict = value->GetAsDictionary();
362 return dict->SetArgs(args, eVarSetOperationAssign).Success();
370 OptionValueProperties::GetPropertyAtIndexAsBoolean (const ExecutionContext *exe_ctx, uint32_t idx, bool fail_value) const
372 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
375 OptionValue *value = property->GetValue().get();
377 return value->GetBooleanValue(fail_value);
383 OptionValueProperties::SetPropertyAtIndexAsBoolean (const ExecutionContext *exe_ctx, uint32_t idx, bool new_value)
385 const Property *property = GetPropertyAtIndex (exe_ctx, true, idx);
388 OptionValue *value = property->GetValue().get();
391 value->SetBooleanValue(new_value);
398 OptionValueDictionary *
399 OptionValueProperties::GetPropertyAtIndexAsOptionValueDictionary (const ExecutionContext *exe_ctx, uint32_t idx) const
401 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
403 return property->GetValue()->GetAsDictionary();
408 OptionValueProperties::GetPropertyAtIndexAsEnumeration (const ExecutionContext *exe_ctx, uint32_t idx, int64_t fail_value) const
410 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
413 OptionValue *value = property->GetValue().get();
415 return value->GetEnumerationValue(fail_value);
421 OptionValueProperties::SetPropertyAtIndexAsEnumeration (const ExecutionContext *exe_ctx, uint32_t idx, int64_t new_value)
423 const Property *property = GetPropertyAtIndex (exe_ctx, true, idx);
426 OptionValue *value = property->GetValue().get();
428 return value->SetEnumerationValue(new_value);
433 const FormatEntity::Entry *
434 OptionValueProperties::GetPropertyAtIndexAsFormatEntity (const ExecutionContext *exe_ctx, uint32_t idx)
436 const Property *property = GetPropertyAtIndex (exe_ctx, true, idx);
439 OptionValue *value = property->GetValue().get();
441 return value->GetFormatEntity();
446 OptionValueFileSpec *
447 OptionValueProperties::GetPropertyAtIndexAsOptionValueFileSpec (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
449 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
452 OptionValue *value = property->GetValue().get();
454 return value->GetAsFileSpec();
461 OptionValueProperties::GetPropertyAtIndexAsFileSpec (const ExecutionContext *exe_ctx, uint32_t idx) const
463 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
466 OptionValue *value = property->GetValue().get();
468 return value->GetFileSpecValue();
475 OptionValueProperties::SetPropertyAtIndexAsFileSpec (const ExecutionContext *exe_ctx, uint32_t idx, const FileSpec &new_file_spec)
477 const Property *property = GetPropertyAtIndex (exe_ctx, true, idx);
480 OptionValue *value = property->GetValue().get();
482 return value->SetFileSpecValue(new_file_spec);
487 const RegularExpression *
488 OptionValueProperties::GetPropertyAtIndexAsOptionValueRegex (const ExecutionContext *exe_ctx, uint32_t idx) const
490 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
493 OptionValue *value = property->GetValue().get();
495 return value->GetRegexValue();
501 OptionValueProperties::GetPropertyAtIndexAsOptionValueSInt64 (const ExecutionContext *exe_ctx, uint32_t idx) const
503 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
506 OptionValue *value = property->GetValue().get();
508 return value->GetAsSInt64();
514 OptionValueProperties::GetPropertyAtIndexAsSInt64 (const ExecutionContext *exe_ctx, uint32_t idx, int64_t fail_value) const
516 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
519 OptionValue *value = property->GetValue().get();
521 return value->GetSInt64Value(fail_value);
527 OptionValueProperties::SetPropertyAtIndexAsSInt64 (const ExecutionContext *exe_ctx, uint32_t idx, int64_t new_value)
529 const Property *property = GetPropertyAtIndex (exe_ctx, true, idx);
532 OptionValue *value = property->GetValue().get();
534 return value->SetSInt64Value(new_value);
540 OptionValueProperties::GetPropertyAtIndexAsString (const ExecutionContext *exe_ctx, uint32_t idx, const char *fail_value) const
542 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
545 OptionValue *value = property->GetValue().get();
547 return value->GetStringValue(fail_value);
553 OptionValueProperties::SetPropertyAtIndexAsString (const ExecutionContext *exe_ctx, uint32_t idx, const char *new_value)
555 const Property *property = GetPropertyAtIndex (exe_ctx, true, idx);
558 OptionValue *value = property->GetValue().get();
560 return value->SetStringValue(new_value);
566 OptionValueProperties::GetPropertyAtIndexAsOptionValueString (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
568 OptionValueSP value_sp(GetPropertyValueAtIndex (exe_ctx, will_modify, idx));
570 return value_sp->GetAsString();
576 OptionValueProperties::GetPropertyAtIndexAsUInt64 (const ExecutionContext *exe_ctx, uint32_t idx, uint64_t fail_value) const
578 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
581 OptionValue *value = property->GetValue().get();
583 return value->GetUInt64Value(fail_value);
589 OptionValueProperties::SetPropertyAtIndexAsUInt64 (const ExecutionContext *exe_ctx, uint32_t idx, uint64_t new_value)
591 const Property *property = GetPropertyAtIndex (exe_ctx, true, idx);
594 OptionValue *value = property->GetValue().get();
596 return value->SetUInt64Value(new_value);
602 OptionValueProperties::Clear ()
604 const size_t num_properties = m_properties.size();
605 for (size_t i=0; i<num_properties; ++i)
606 m_properties[i].GetValue()->Clear();
612 OptionValueProperties::SetValueFromString (llvm::StringRef value, VarSetOperationType op)
616 // Args args(value_cstr);
617 // const size_t argc = args.GetArgumentCount();
620 case eVarSetOperationClear:
624 case eVarSetOperationReplace:
625 case eVarSetOperationAssign:
626 case eVarSetOperationRemove:
627 case eVarSetOperationInsertBefore:
628 case eVarSetOperationInsertAfter:
629 case eVarSetOperationAppend:
630 case eVarSetOperationInvalid:
631 error = OptionValue::SetValueFromString (value, op);
639 OptionValueProperties::DumpValue (const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask)
641 const size_t num_properties = m_properties.size();
642 for (size_t i=0; i<num_properties; ++i)
644 const Property *property = GetPropertyAtIndex(exe_ctx, false, i);
647 OptionValue *option_value = property->GetValue().get();
648 assert (option_value);
649 const bool transparent_value = option_value->ValueIsTransparent ();
650 property->Dump (exe_ctx,
653 if (!transparent_value)
660 OptionValueProperties::DumpPropertyValue (const ExecutionContext *exe_ctx,
662 const char *property_path,
666 const bool will_modify = false;
667 lldb::OptionValueSP value_sp (GetSubValue (exe_ctx, property_path, will_modify, error));
670 if (!value_sp->ValueIsTransparent ())
672 if (dump_mask & eDumpOptionName)
673 strm.PutCString (property_path);
674 if (dump_mask & ~eDumpOptionName)
677 value_sp->DumpValue (exe_ctx, strm, dump_mask);
683 OptionValueProperties::DeepCopy () const
685 assert(!"this shouldn't happen");
686 return lldb::OptionValueSP();
690 OptionValueProperties::GetPropertyAtPath (const ExecutionContext *exe_ctx,
692 const char *name) const
694 const Property *property = nullptr;
697 const char *sub_name = nullptr;
699 size_t key_len = ::strcspn (name, ".[{");
703 key.SetCStringWithLength (name, key_len);
704 sub_name = name + key_len;
707 key.SetCString (name);
709 property = GetProperty (exe_ctx, will_modify, key);
710 if (sub_name && property)
712 if (sub_name[0] == '.')
714 OptionValueProperties *sub_properties = property->GetValue()->GetAsProperties();
716 return sub_properties->GetPropertyAtPath(exe_ctx, will_modify, sub_name + 1);
725 OptionValueProperties::DumpAllDescriptions (CommandInterpreter &interpreter,
728 size_t max_name_len = 0;
729 const size_t num_properties = m_properties.size();
730 for (size_t i=0; i<num_properties; ++i)
732 const Property *property = ProtectedGetPropertyAtIndex(i);
734 max_name_len = std::max<size_t>(property->GetName().GetLength(), max_name_len);
736 for (size_t i=0; i<num_properties; ++i)
738 const Property *property = ProtectedGetPropertyAtIndex(i);
740 property->DumpDescription (interpreter, strm, max_name_len, false);
745 OptionValueProperties::Apropos (const char *keyword, std::vector<const Property *> &matching_properties) const
747 const size_t num_properties = m_properties.size();
749 for (size_t i=0; i<num_properties; ++i)
751 const Property *property = ProtectedGetPropertyAtIndex(i);
754 const OptionValueProperties *properties = property->GetValue()->GetAsProperties();
757 properties->Apropos (keyword, matching_properties);
762 const char *name = property->GetName().GetCString();
763 if (name && ::strcasestr(name, keyword))
767 const char *desc = property->GetDescription();
768 if (desc && ::strcasestr(desc, keyword))
773 matching_properties.push_back (property);
780 lldb::OptionValuePropertiesSP
781 OptionValueProperties::GetSubProperty (const ExecutionContext *exe_ctx,
782 const ConstString &name)
784 lldb::OptionValueSP option_value_sp(GetValueForKey(exe_ctx, name, false));
787 OptionValueProperties *ov_properties = option_value_sp->GetAsProperties ();
789 return ov_properties->shared_from_this();
791 return lldb::OptionValuePropertiesSP();