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;
27 OptionValueProperties::OptionValueProperties(const ConstString &name)
28 : OptionValue(), m_name(name), m_properties(), m_name_to_index() {}
30 OptionValueProperties::OptionValueProperties(
31 const OptionValueProperties &global_properties)
32 : OptionValue(global_properties),
33 std::enable_shared_from_this<OptionValueProperties>(),
34 m_name(global_properties.m_name),
35 m_properties(global_properties.m_properties),
36 m_name_to_index(global_properties.m_name_to_index) {
37 // We now have an exact copy of "global_properties". We need to now
38 // find all non-global settings and copy the property values so that
39 // all non-global settings get new OptionValue instances created for
41 const size_t num_properties = m_properties.size();
42 for (size_t i = 0; i < num_properties; ++i) {
43 // Duplicate any values that are not global when constructing properties
46 if (m_properties[i].IsGlobal() == false) {
47 lldb::OptionValueSP new_value_sp(m_properties[i].GetValue()->DeepCopy());
48 m_properties[i].SetOptionValue(new_value_sp);
53 size_t OptionValueProperties::GetNumProperties() const {
54 return m_properties.size();
57 void OptionValueProperties::Initialize(const PropertyDefinition *defs) {
58 for (size_t i = 0; defs[i].name; ++i) {
59 Property property(defs[i]);
60 assert(property.IsValid());
61 m_name_to_index.Append(property.GetName(), m_properties.size());
62 property.GetValue()->SetParent(shared_from_this());
63 m_properties.push_back(property);
65 m_name_to_index.Sort();
68 void OptionValueProperties::SetValueChangedCallback(
69 uint32_t property_idx, OptionValueChangedCallback callback, void *baton) {
70 Property *property = ProtectedGetPropertyAtIndex(property_idx);
72 property->SetValueChangedCallback(callback, baton);
75 void OptionValueProperties::AppendProperty(const ConstString &name,
76 const ConstString &desc,
78 const OptionValueSP &value_sp) {
79 Property property(name, desc, is_global, value_sp);
80 m_name_to_index.Append(name.GetStringRef(), m_properties.size());
81 m_properties.push_back(property);
82 value_sp->SetParent(shared_from_this());
83 m_name_to_index.Sort();
87 // OptionValueProperties::GetQualifiedName (Stream &strm)
89 // bool dumped_something = false;
90 //// lldb::OptionValuePropertiesSP parent_sp(GetParent ());
93 //// parent_sp->GetQualifiedName (strm);
94 //// strm.PutChar('.');
95 //// dumped_something = true;
100 // dumped_something = true;
102 // return dumped_something;
106 OptionValueProperties::GetValueForKey(const ExecutionContext *exe_ctx,
107 const ConstString &key,
108 bool will_modify) const {
109 lldb::OptionValueSP value_sp;
110 size_t idx = m_name_to_index.Find(key.GetStringRef(), SIZE_MAX);
111 if (idx < m_properties.size())
112 value_sp = GetPropertyAtIndex(exe_ctx, will_modify, idx)->GetValue();
117 OptionValueProperties::GetSubValue(const ExecutionContext *exe_ctx,
118 llvm::StringRef name, bool will_modify,
119 Error &error) const {
120 lldb::OptionValueSP value_sp;
122 return OptionValueSP();
124 llvm::StringRef sub_name;
126 size_t key_len = name.find_first_of(".[{");
127 if (key_len != llvm::StringRef::npos) {
128 key.SetString(name.take_front(key_len));
129 sub_name = name.drop_front(key_len);
133 value_sp = GetValueForKey(exe_ctx, key, will_modify);
134 if (sub_name.empty() || !value_sp)
137 switch (sub_name[0]) {
139 lldb::OptionValueSP return_val_sp;
141 value_sp->GetSubValue(exe_ctx, sub_name.drop_front(), will_modify, error);
142 if (!return_val_sp) {
143 if (Properties::IsSettingExperimental(sub_name.drop_front())) {
144 size_t experimental_len =
145 strlen(Properties::GetExperimentalSettingsName());
146 if (sub_name[experimental_len + 1] == '.')
147 return_val_sp = value_sp->GetSubValue(
148 exe_ctx, sub_name.drop_front(experimental_len + 2), will_modify, error);
149 // It isn't an error if an experimental setting is not present.
154 return return_val_sp;
157 // Predicate matching for predicates like
158 // "<setting-name>{<predicate>}"
159 // strings are parsed by the current OptionValueProperties subclass
160 // to mean whatever they want to. For instance a subclass of
161 // OptionValueProperties for a lldb_private::Target might implement:
162 // "target.run-args{arch==i386}" -- only set run args if the arch is
164 // "target.run-args{path=/tmp/a/b/c/a.out}" -- only set run args if the
166 // "target.run-args{basename==test&&arch==x86_64}" -- only set run args
167 // if executable basename is "test" and arch is "x86_64"
169 llvm::StringRef predicate_start = sub_name.drop_front();
170 size_t pos = predicate_start.find_first_of('}');
171 if (pos != llvm::StringRef::npos) {
172 auto predicate = predicate_start.take_front(pos);
173 auto rest = predicate_start.drop_front(pos);
174 if (PredicateMatches(exe_ctx, predicate)) {
176 // Still more subvalue string to evaluate
177 return value_sp->GetSubValue(exe_ctx, rest,
186 // Predicate didn't match or wasn't correctly formed
191 // Array or dictionary access for subvalues like:
192 // "[12]" -- access 12th array element
193 // "['hello']" -- dictionary access of key named hello
194 return value_sp->GetSubValue(exe_ctx, sub_name, will_modify, error);
203 Error OptionValueProperties::SetSubValue(const ExecutionContext *exe_ctx,
204 VarSetOperationType op,
205 llvm::StringRef name, llvm::StringRef value) {
207 const bool will_modify = true;
208 lldb::OptionValueSP value_sp(GetSubValue(exe_ctx, name, will_modify, error));
210 error = value_sp->SetValueFromString(value, op);
212 if (error.AsCString() == nullptr)
213 error.SetErrorStringWithFormat("invalid value path '%s'", name.str().c_str());
219 OptionValueProperties::GetPropertyIndex(const ConstString &name) const {
220 return m_name_to_index.Find(name.GetStringRef(), SIZE_MAX);
224 OptionValueProperties::GetProperty(const ExecutionContext *exe_ctx,
226 const ConstString &name) const {
227 return GetPropertyAtIndex(
228 exe_ctx, will_modify,
229 m_name_to_index.Find(name.GetStringRef(), SIZE_MAX));
232 const Property *OptionValueProperties::GetPropertyAtIndex(
233 const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const {
234 return ProtectedGetPropertyAtIndex(idx);
237 lldb::OptionValueSP OptionValueProperties::GetPropertyValueAtIndex(
238 const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const {
239 const Property *setting = GetPropertyAtIndex(exe_ctx, will_modify, idx);
241 return setting->GetValue();
242 return OptionValueSP();
245 OptionValuePathMappings *
246 OptionValueProperties::GetPropertyAtIndexAsOptionValuePathMappings(
247 const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const {
248 OptionValueSP value_sp(GetPropertyValueAtIndex(exe_ctx, will_modify, idx));
250 return value_sp->GetAsPathMappings();
254 OptionValueFileSpecList *
255 OptionValueProperties::GetPropertyAtIndexAsOptionValueFileSpecList(
256 const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const {
257 OptionValueSP value_sp(GetPropertyValueAtIndex(exe_ctx, will_modify, idx));
259 return value_sp->GetAsFileSpecList();
263 OptionValueArch *OptionValueProperties::GetPropertyAtIndexAsOptionValueArch(
264 const ExecutionContext *exe_ctx, uint32_t idx) const {
265 const Property *property = GetPropertyAtIndex(exe_ctx, false, idx);
267 return property->GetValue()->GetAsArch();
271 OptionValueLanguage *
272 OptionValueProperties::GetPropertyAtIndexAsOptionValueLanguage(
273 const ExecutionContext *exe_ctx, uint32_t idx) const {
274 const Property *property = GetPropertyAtIndex(exe_ctx, false, idx);
276 return property->GetValue()->GetAsLanguage();
280 bool OptionValueProperties::GetPropertyAtIndexAsArgs(
281 const ExecutionContext *exe_ctx, uint32_t idx, Args &args) const {
282 const Property *property = GetPropertyAtIndex(exe_ctx, false, idx);
284 OptionValue *value = property->GetValue().get();
286 const OptionValueArray *array = value->GetAsArray();
288 return array->GetArgs(args);
290 const OptionValueDictionary *dict = value->GetAsDictionary();
292 return dict->GetArgs(args);
299 bool OptionValueProperties::SetPropertyAtIndexFromArgs(
300 const ExecutionContext *exe_ctx, uint32_t idx, const Args &args) {
301 const Property *property = GetPropertyAtIndex(exe_ctx, true, idx);
303 OptionValue *value = property->GetValue().get();
305 OptionValueArray *array = value->GetAsArray();
307 return array->SetArgs(args, eVarSetOperationAssign).Success();
309 OptionValueDictionary *dict = value->GetAsDictionary();
311 return dict->SetArgs(args, eVarSetOperationAssign).Success();
318 bool OptionValueProperties::GetPropertyAtIndexAsBoolean(
319 const ExecutionContext *exe_ctx, uint32_t idx, bool fail_value) const {
320 const Property *property = GetPropertyAtIndex(exe_ctx, false, idx);
322 OptionValue *value = property->GetValue().get();
324 return value->GetBooleanValue(fail_value);
329 bool OptionValueProperties::SetPropertyAtIndexAsBoolean(
330 const ExecutionContext *exe_ctx, uint32_t idx, bool new_value) {
331 const Property *property = GetPropertyAtIndex(exe_ctx, true, idx);
333 OptionValue *value = property->GetValue().get();
335 value->SetBooleanValue(new_value);
342 OptionValueDictionary *
343 OptionValueProperties::GetPropertyAtIndexAsOptionValueDictionary(
344 const ExecutionContext *exe_ctx, uint32_t idx) const {
345 const Property *property = GetPropertyAtIndex(exe_ctx, false, idx);
347 return property->GetValue()->GetAsDictionary();
351 int64_t OptionValueProperties::GetPropertyAtIndexAsEnumeration(
352 const ExecutionContext *exe_ctx, uint32_t idx, int64_t fail_value) const {
353 const Property *property = GetPropertyAtIndex(exe_ctx, false, idx);
355 OptionValue *value = property->GetValue().get();
357 return value->GetEnumerationValue(fail_value);
362 bool OptionValueProperties::SetPropertyAtIndexAsEnumeration(
363 const ExecutionContext *exe_ctx, uint32_t idx, int64_t new_value) {
364 const Property *property = GetPropertyAtIndex(exe_ctx, true, idx);
366 OptionValue *value = property->GetValue().get();
368 return value->SetEnumerationValue(new_value);
373 const FormatEntity::Entry *
374 OptionValueProperties::GetPropertyAtIndexAsFormatEntity(
375 const ExecutionContext *exe_ctx, uint32_t idx) {
376 const Property *property = GetPropertyAtIndex(exe_ctx, true, idx);
378 OptionValue *value = property->GetValue().get();
380 return value->GetFormatEntity();
385 OptionValueFileSpec *
386 OptionValueProperties::GetPropertyAtIndexAsOptionValueFileSpec(
387 const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const {
388 const Property *property = GetPropertyAtIndex(exe_ctx, false, idx);
390 OptionValue *value = property->GetValue().get();
392 return value->GetAsFileSpec();
397 FileSpec OptionValueProperties::GetPropertyAtIndexAsFileSpec(
398 const ExecutionContext *exe_ctx, uint32_t idx) const {
399 const Property *property = GetPropertyAtIndex(exe_ctx, false, idx);
401 OptionValue *value = property->GetValue().get();
403 return value->GetFileSpecValue();
408 bool OptionValueProperties::SetPropertyAtIndexAsFileSpec(
409 const ExecutionContext *exe_ctx, uint32_t idx,
410 const FileSpec &new_file_spec) {
411 const Property *property = GetPropertyAtIndex(exe_ctx, true, idx);
413 OptionValue *value = property->GetValue().get();
415 return value->SetFileSpecValue(new_file_spec);
420 const RegularExpression *
421 OptionValueProperties::GetPropertyAtIndexAsOptionValueRegex(
422 const ExecutionContext *exe_ctx, uint32_t idx) const {
423 const Property *property = GetPropertyAtIndex(exe_ctx, false, idx);
425 OptionValue *value = property->GetValue().get();
427 return value->GetRegexValue();
432 OptionValueSInt64 *OptionValueProperties::GetPropertyAtIndexAsOptionValueSInt64(
433 const ExecutionContext *exe_ctx, uint32_t idx) const {
434 const Property *property = GetPropertyAtIndex(exe_ctx, false, idx);
436 OptionValue *value = property->GetValue().get();
438 return value->GetAsSInt64();
443 int64_t OptionValueProperties::GetPropertyAtIndexAsSInt64(
444 const ExecutionContext *exe_ctx, uint32_t idx, int64_t fail_value) const {
445 const Property *property = GetPropertyAtIndex(exe_ctx, false, idx);
447 OptionValue *value = property->GetValue().get();
449 return value->GetSInt64Value(fail_value);
454 bool OptionValueProperties::SetPropertyAtIndexAsSInt64(
455 const ExecutionContext *exe_ctx, uint32_t idx, int64_t new_value) {
456 const Property *property = GetPropertyAtIndex(exe_ctx, true, idx);
458 OptionValue *value = property->GetValue().get();
460 return value->SetSInt64Value(new_value);
465 llvm::StringRef OptionValueProperties::GetPropertyAtIndexAsString(
466 const ExecutionContext *exe_ctx, uint32_t idx,
467 llvm::StringRef fail_value) const {
468 const Property *property = GetPropertyAtIndex(exe_ctx, false, idx);
470 OptionValue *value = property->GetValue().get();
472 return value->GetStringValue(fail_value);
477 bool OptionValueProperties::SetPropertyAtIndexAsString(
478 const ExecutionContext *exe_ctx, uint32_t idx, llvm::StringRef new_value) {
479 const Property *property = GetPropertyAtIndex(exe_ctx, true, idx);
481 OptionValue *value = property->GetValue().get();
483 return value->SetStringValue(new_value);
488 OptionValueString *OptionValueProperties::GetPropertyAtIndexAsOptionValueString(
489 const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const {
490 OptionValueSP value_sp(GetPropertyValueAtIndex(exe_ctx, will_modify, idx));
492 return value_sp->GetAsString();
496 uint64_t OptionValueProperties::GetPropertyAtIndexAsUInt64(
497 const ExecutionContext *exe_ctx, uint32_t idx, uint64_t fail_value) const {
498 const Property *property = GetPropertyAtIndex(exe_ctx, false, idx);
500 OptionValue *value = property->GetValue().get();
502 return value->GetUInt64Value(fail_value);
507 bool OptionValueProperties::SetPropertyAtIndexAsUInt64(
508 const ExecutionContext *exe_ctx, uint32_t idx, uint64_t new_value) {
509 const Property *property = GetPropertyAtIndex(exe_ctx, true, idx);
511 OptionValue *value = property->GetValue().get();
513 return value->SetUInt64Value(new_value);
518 bool OptionValueProperties::Clear() {
519 const size_t num_properties = m_properties.size();
520 for (size_t i = 0; i < num_properties; ++i)
521 m_properties[i].GetValue()->Clear();
525 Error OptionValueProperties::SetValueFromString(llvm::StringRef value,
526 VarSetOperationType op) {
529 // Args args(value_cstr);
530 // const size_t argc = args.GetArgumentCount();
532 case eVarSetOperationClear:
536 case eVarSetOperationReplace:
537 case eVarSetOperationAssign:
538 case eVarSetOperationRemove:
539 case eVarSetOperationInsertBefore:
540 case eVarSetOperationInsertAfter:
541 case eVarSetOperationAppend:
542 case eVarSetOperationInvalid:
543 error = OptionValue::SetValueFromString(value, op);
550 void OptionValueProperties::DumpValue(const ExecutionContext *exe_ctx,
551 Stream &strm, uint32_t dump_mask) {
552 const size_t num_properties = m_properties.size();
553 for (size_t i = 0; i < num_properties; ++i) {
554 const Property *property = GetPropertyAtIndex(exe_ctx, false, i);
556 OptionValue *option_value = property->GetValue().get();
557 assert(option_value);
558 const bool transparent_value = option_value->ValueIsTransparent();
559 property->Dump(exe_ctx, strm, dump_mask);
560 if (!transparent_value)
566 Error OptionValueProperties::DumpPropertyValue(const ExecutionContext *exe_ctx,
568 llvm::StringRef property_path,
569 uint32_t dump_mask) {
571 const bool will_modify = false;
572 lldb::OptionValueSP value_sp(
573 GetSubValue(exe_ctx, property_path, will_modify, error));
575 if (!value_sp->ValueIsTransparent()) {
576 if (dump_mask & eDumpOptionName)
577 strm.PutCString(property_path);
578 if (dump_mask & ~eDumpOptionName)
581 value_sp->DumpValue(exe_ctx, strm, dump_mask);
586 lldb::OptionValueSP OptionValueProperties::DeepCopy() const {
587 llvm_unreachable("this shouldn't happen");
590 const Property *OptionValueProperties::GetPropertyAtPath(
591 const ExecutionContext *exe_ctx, bool will_modify, llvm::StringRef name) const {
592 const Property *property = nullptr;
595 llvm::StringRef sub_name;
597 size_t key_len = name.find_first_of(".[{");
599 if (key_len != llvm::StringRef::npos) {
600 key.SetString(name.take_front(key_len));
601 sub_name = name.drop_front(key_len);
605 property = GetProperty(exe_ctx, will_modify, key);
606 if (sub_name.empty() || !property)
609 if (sub_name[0] == '.') {
610 OptionValueProperties *sub_properties =
611 property->GetValue()->GetAsProperties();
613 return sub_properties->GetPropertyAtPath(exe_ctx, will_modify,
614 sub_name.drop_front());
619 void OptionValueProperties::DumpAllDescriptions(CommandInterpreter &interpreter,
620 Stream &strm) const {
621 size_t max_name_len = 0;
622 const size_t num_properties = m_properties.size();
623 for (size_t i = 0; i < num_properties; ++i) {
624 const Property *property = ProtectedGetPropertyAtIndex(i);
626 max_name_len = std::max<size_t>(property->GetName().size(), max_name_len);
628 for (size_t i = 0; i < num_properties; ++i) {
629 const Property *property = ProtectedGetPropertyAtIndex(i);
631 property->DumpDescription(interpreter, strm, max_name_len, false);
635 void OptionValueProperties::Apropos(
636 llvm::StringRef keyword,
637 std::vector<const Property *> &matching_properties) const {
638 const size_t num_properties = m_properties.size();
640 for (size_t i = 0; i < num_properties; ++i) {
641 const Property *property = ProtectedGetPropertyAtIndex(i);
643 const OptionValueProperties *properties =
644 property->GetValue()->GetAsProperties();
646 properties->Apropos(keyword, matching_properties);
649 llvm::StringRef name = property->GetName();
650 if (name.contains_lower(keyword))
653 llvm::StringRef desc = property->GetDescription();
654 if (desc.contains_lower(keyword))
658 matching_properties.push_back(property);
665 lldb::OptionValuePropertiesSP
666 OptionValueProperties::GetSubProperty(const ExecutionContext *exe_ctx,
667 const ConstString &name) {
668 lldb::OptionValueSP option_value_sp(GetValueForKey(exe_ctx, name, false));
669 if (option_value_sp) {
670 OptionValueProperties *ov_properties = option_value_sp->GetAsProperties();
672 return ov_properties->shared_from_this();
674 return lldb::OptionValuePropertiesSP();