1 //===-- OptionValueProperties.cpp --------------------------------*- C++-*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Interpreter/OptionValueProperties.h"
11 #include "lldb/Utility/Flags.h"
13 #include "lldb/Core/UserSettingsController.h"
14 #include "lldb/Interpreter/OptionValues.h"
15 #include "lldb/Interpreter/Property.h"
16 #include "lldb/Utility/Args.h"
17 #include "lldb/Utility/Stream.h"
18 #include "lldb/Utility/StringList.h"
21 using namespace lldb_private;
23 OptionValueProperties::OptionValueProperties(ConstString name)
24 : OptionValue(), m_name(name), m_properties(), m_name_to_index() {}
26 OptionValueProperties::OptionValueProperties(
27 const OptionValueProperties &global_properties)
28 : OptionValue(global_properties),
29 std::enable_shared_from_this<OptionValueProperties>(),
30 m_name(global_properties.m_name),
31 m_properties(global_properties.m_properties),
32 m_name_to_index(global_properties.m_name_to_index) {
33 // We now have an exact copy of "global_properties". We need to now find all
34 // non-global settings and copy the property values so that all non-global
35 // settings get new OptionValue instances created for them.
36 const size_t num_properties = m_properties.size();
37 for (size_t i = 0; i < num_properties; ++i) {
38 // Duplicate any values that are not global when constructing properties
40 if (!m_properties[i].IsGlobal()) {
41 lldb::OptionValueSP new_value_sp(m_properties[i].GetValue()->DeepCopy());
42 m_properties[i].SetOptionValue(new_value_sp);
47 size_t OptionValueProperties::GetNumProperties() const {
48 return m_properties.size();
51 void OptionValueProperties::Initialize(const PropertyDefinitions &defs) {
52 for (const auto &definition : defs) {
53 Property property(definition);
54 assert(property.IsValid());
55 m_name_to_index.Append(ConstString(property.GetName()), m_properties.size());
56 property.GetValue()->SetParent(shared_from_this());
57 m_properties.push_back(property);
59 m_name_to_index.Sort();
62 void OptionValueProperties::SetValueChangedCallback(
63 uint32_t property_idx, OptionValueChangedCallback callback, void *baton) {
64 Property *property = ProtectedGetPropertyAtIndex(property_idx);
66 property->SetValueChangedCallback(callback, baton);
69 void OptionValueProperties::AppendProperty(ConstString name,
72 const OptionValueSP &value_sp) {
73 Property property(name, desc, is_global, value_sp);
74 m_name_to_index.Append(name, m_properties.size());
75 m_properties.push_back(property);
76 value_sp->SetParent(shared_from_this());
77 m_name_to_index.Sort();
81 // OptionValueProperties::GetQualifiedName (Stream &strm)
83 // bool dumped_something = false;
84 //// lldb::OptionValuePropertiesSP parent_sp(GetParent ());
87 //// parent_sp->GetQualifiedName (strm);
88 //// strm.PutChar('.');
89 //// dumped_something = true;
94 // dumped_something = true;
96 // return dumped_something;
100 OptionValueProperties::GetValueForKey(const ExecutionContext *exe_ctx,
102 bool will_modify) const {
103 lldb::OptionValueSP value_sp;
104 size_t idx = m_name_to_index.Find(key, SIZE_MAX);
105 if (idx < m_properties.size())
106 value_sp = GetPropertyAtIndex(exe_ctx, will_modify, idx)->GetValue();
111 OptionValueProperties::GetSubValue(const ExecutionContext *exe_ctx,
112 llvm::StringRef name, bool will_modify,
113 Status &error) const {
114 lldb::OptionValueSP value_sp;
116 return OptionValueSP();
118 llvm::StringRef sub_name;
120 size_t key_len = name.find_first_of(".[{");
121 if (key_len != llvm::StringRef::npos) {
122 key.SetString(name.take_front(key_len));
123 sub_name = name.drop_front(key_len);
127 value_sp = GetValueForKey(exe_ctx, key, will_modify);
128 if (sub_name.empty() || !value_sp)
131 switch (sub_name[0]) {
133 lldb::OptionValueSP return_val_sp;
135 value_sp->GetSubValue(exe_ctx, sub_name.drop_front(), will_modify, error);
136 if (!return_val_sp) {
137 if (Properties::IsSettingExperimental(sub_name.drop_front())) {
138 size_t experimental_len =
139 strlen(Properties::GetExperimentalSettingsName());
140 if (sub_name[experimental_len + 1] == '.')
141 return_val_sp = value_sp->GetSubValue(
142 exe_ctx, sub_name.drop_front(experimental_len + 2), will_modify, error);
143 // It isn't an error if an experimental setting is not present.
148 return return_val_sp;
151 // Predicate matching for predicates like
152 // "<setting-name>{<predicate>}"
153 // strings are parsed by the current OptionValueProperties subclass to mean
154 // whatever they want to. For instance a subclass of OptionValueProperties
155 // for a lldb_private::Target might implement: "target.run-
156 // args{arch==i386}" -- only set run args if the arch is i386 "target
157 // .run-args{path=/tmp/a/b/c/a.out}" -- only set run args if the path
158 // matches "target.run-args{basename==test&&arch==x86_64}" -- only set run
159 // args if executable basename is "test" and arch is "x86_64"
161 llvm::StringRef predicate_start = sub_name.drop_front();
162 size_t pos = predicate_start.find('}');
163 if (pos != llvm::StringRef::npos) {
164 auto predicate = predicate_start.take_front(pos);
165 auto rest = predicate_start.drop_front(pos);
166 if (PredicateMatches(exe_ctx, predicate)) {
168 // Still more subvalue string to evaluate
169 return value_sp->GetSubValue(exe_ctx, rest,
178 // Predicate didn't match or wasn't correctly formed
183 // Array or dictionary access for subvalues like: "[12]" -- access
184 // 12th array element "['hello']" -- dictionary access of key named hello
185 return value_sp->GetSubValue(exe_ctx, sub_name, will_modify, error);
194 Status OptionValueProperties::SetSubValue(const ExecutionContext *exe_ctx,
195 VarSetOperationType op,
196 llvm::StringRef name,
197 llvm::StringRef value) {
199 const bool will_modify = true;
200 llvm::SmallVector<llvm::StringRef, 8> components;
201 name.split(components, '.');
202 bool name_contains_experimental = false;
203 for (const auto &part : components)
204 if (Properties::IsSettingExperimental(part))
205 name_contains_experimental = true;
207 lldb::OptionValueSP value_sp(GetSubValue(exe_ctx, name, will_modify, error));
209 error = value_sp->SetValueFromString(value, op);
211 // Don't set an error if the path contained .experimental. - those are
212 // allowed to be missing and should silently fail.
213 if (!name_contains_experimental && error.AsCString() == nullptr) {
214 error.SetErrorStringWithFormat("invalid value path '%s'", name.str().c_str());
221 OptionValueProperties::GetPropertyIndex(ConstString name) const {
222 return m_name_to_index.Find(name, SIZE_MAX);
226 OptionValueProperties::GetProperty(const ExecutionContext *exe_ctx,
228 ConstString name) const {
229 return GetPropertyAtIndex(
230 exe_ctx, will_modify,
231 m_name_to_index.Find(name, SIZE_MAX));
234 const Property *OptionValueProperties::GetPropertyAtIndex(
235 const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const {
236 return ProtectedGetPropertyAtIndex(idx);
239 lldb::OptionValueSP OptionValueProperties::GetPropertyValueAtIndex(
240 const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const {
241 const Property *setting = GetPropertyAtIndex(exe_ctx, will_modify, idx);
243 return setting->GetValue();
244 return OptionValueSP();
247 OptionValuePathMappings *
248 OptionValueProperties::GetPropertyAtIndexAsOptionValuePathMappings(
249 const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const {
250 OptionValueSP value_sp(GetPropertyValueAtIndex(exe_ctx, will_modify, idx));
252 return value_sp->GetAsPathMappings();
256 OptionValueFileSpecList *
257 OptionValueProperties::GetPropertyAtIndexAsOptionValueFileSpecList(
258 const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const {
259 OptionValueSP value_sp(GetPropertyValueAtIndex(exe_ctx, will_modify, idx));
261 return value_sp->GetAsFileSpecList();
265 OptionValueArch *OptionValueProperties::GetPropertyAtIndexAsOptionValueArch(
266 const ExecutionContext *exe_ctx, uint32_t idx) const {
267 const Property *property = GetPropertyAtIndex(exe_ctx, false, idx);
269 return property->GetValue()->GetAsArch();
273 OptionValueLanguage *
274 OptionValueProperties::GetPropertyAtIndexAsOptionValueLanguage(
275 const ExecutionContext *exe_ctx, uint32_t idx) const {
276 const Property *property = GetPropertyAtIndex(exe_ctx, false, idx);
278 return property->GetValue()->GetAsLanguage();
282 bool OptionValueProperties::GetPropertyAtIndexAsArgs(
283 const ExecutionContext *exe_ctx, uint32_t idx, Args &args) const {
284 const Property *property = GetPropertyAtIndex(exe_ctx, false, idx);
286 OptionValue *value = property->GetValue().get();
288 const OptionValueArray *array = value->GetAsArray();
290 return array->GetArgs(args);
292 const OptionValueDictionary *dict = value->GetAsDictionary();
294 return dict->GetArgs(args);
301 bool OptionValueProperties::SetPropertyAtIndexFromArgs(
302 const ExecutionContext *exe_ctx, uint32_t idx, const Args &args) {
303 const Property *property = GetPropertyAtIndex(exe_ctx, true, idx);
305 OptionValue *value = property->GetValue().get();
307 OptionValueArray *array = value->GetAsArray();
309 return array->SetArgs(args, eVarSetOperationAssign).Success();
311 OptionValueDictionary *dict = value->GetAsDictionary();
313 return dict->SetArgs(args, eVarSetOperationAssign).Success();
320 bool OptionValueProperties::GetPropertyAtIndexAsBoolean(
321 const ExecutionContext *exe_ctx, uint32_t idx, bool fail_value) const {
322 const Property *property = GetPropertyAtIndex(exe_ctx, false, idx);
324 OptionValue *value = property->GetValue().get();
326 return value->GetBooleanValue(fail_value);
331 bool OptionValueProperties::SetPropertyAtIndexAsBoolean(
332 const ExecutionContext *exe_ctx, uint32_t idx, bool new_value) {
333 const Property *property = GetPropertyAtIndex(exe_ctx, true, idx);
335 OptionValue *value = property->GetValue().get();
337 value->SetBooleanValue(new_value);
344 OptionValueDictionary *
345 OptionValueProperties::GetPropertyAtIndexAsOptionValueDictionary(
346 const ExecutionContext *exe_ctx, uint32_t idx) const {
347 const Property *property = GetPropertyAtIndex(exe_ctx, false, idx);
349 return property->GetValue()->GetAsDictionary();
353 int64_t OptionValueProperties::GetPropertyAtIndexAsEnumeration(
354 const ExecutionContext *exe_ctx, uint32_t idx, int64_t fail_value) const {
355 const Property *property = GetPropertyAtIndex(exe_ctx, false, idx);
357 OptionValue *value = property->GetValue().get();
359 return value->GetEnumerationValue(fail_value);
364 bool OptionValueProperties::SetPropertyAtIndexAsEnumeration(
365 const ExecutionContext *exe_ctx, uint32_t idx, int64_t new_value) {
366 const Property *property = GetPropertyAtIndex(exe_ctx, true, idx);
368 OptionValue *value = property->GetValue().get();
370 return value->SetEnumerationValue(new_value);
375 const FormatEntity::Entry *
376 OptionValueProperties::GetPropertyAtIndexAsFormatEntity(
377 const ExecutionContext *exe_ctx, uint32_t idx) {
378 const Property *property = GetPropertyAtIndex(exe_ctx, true, idx);
380 OptionValue *value = property->GetValue().get();
382 return value->GetFormatEntity();
387 OptionValueFileSpec *
388 OptionValueProperties::GetPropertyAtIndexAsOptionValueFileSpec(
389 const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const {
390 const Property *property = GetPropertyAtIndex(exe_ctx, false, idx);
392 OptionValue *value = property->GetValue().get();
394 return value->GetAsFileSpec();
399 FileSpec OptionValueProperties::GetPropertyAtIndexAsFileSpec(
400 const ExecutionContext *exe_ctx, uint32_t idx) const {
401 const Property *property = GetPropertyAtIndex(exe_ctx, false, idx);
403 OptionValue *value = property->GetValue().get();
405 return value->GetFileSpecValue();
410 bool OptionValueProperties::SetPropertyAtIndexAsFileSpec(
411 const ExecutionContext *exe_ctx, uint32_t idx,
412 const FileSpec &new_file_spec) {
413 const Property *property = GetPropertyAtIndex(exe_ctx, true, idx);
415 OptionValue *value = property->GetValue().get();
417 return value->SetFileSpecValue(new_file_spec);
422 const RegularExpression *
423 OptionValueProperties::GetPropertyAtIndexAsOptionValueRegex(
424 const ExecutionContext *exe_ctx, uint32_t idx) const {
425 const Property *property = GetPropertyAtIndex(exe_ctx, false, idx);
427 OptionValue *value = property->GetValue().get();
429 return value->GetRegexValue();
434 OptionValueSInt64 *OptionValueProperties::GetPropertyAtIndexAsOptionValueSInt64(
435 const ExecutionContext *exe_ctx, uint32_t idx) const {
436 const Property *property = GetPropertyAtIndex(exe_ctx, false, idx);
438 OptionValue *value = property->GetValue().get();
440 return value->GetAsSInt64();
445 int64_t OptionValueProperties::GetPropertyAtIndexAsSInt64(
446 const ExecutionContext *exe_ctx, uint32_t idx, int64_t fail_value) const {
447 const Property *property = GetPropertyAtIndex(exe_ctx, false, idx);
449 OptionValue *value = property->GetValue().get();
451 return value->GetSInt64Value(fail_value);
456 bool OptionValueProperties::SetPropertyAtIndexAsSInt64(
457 const ExecutionContext *exe_ctx, uint32_t idx, int64_t new_value) {
458 const Property *property = GetPropertyAtIndex(exe_ctx, true, idx);
460 OptionValue *value = property->GetValue().get();
462 return value->SetSInt64Value(new_value);
467 llvm::StringRef OptionValueProperties::GetPropertyAtIndexAsString(
468 const ExecutionContext *exe_ctx, uint32_t idx,
469 llvm::StringRef fail_value) const {
470 const Property *property = GetPropertyAtIndex(exe_ctx, false, idx);
472 OptionValue *value = property->GetValue().get();
474 return value->GetStringValue(fail_value);
479 bool OptionValueProperties::SetPropertyAtIndexAsString(
480 const ExecutionContext *exe_ctx, uint32_t idx, llvm::StringRef new_value) {
481 const Property *property = GetPropertyAtIndex(exe_ctx, true, idx);
483 OptionValue *value = property->GetValue().get();
485 return value->SetStringValue(new_value);
490 OptionValueString *OptionValueProperties::GetPropertyAtIndexAsOptionValueString(
491 const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const {
492 OptionValueSP value_sp(GetPropertyValueAtIndex(exe_ctx, will_modify, idx));
494 return value_sp->GetAsString();
498 uint64_t OptionValueProperties::GetPropertyAtIndexAsUInt64(
499 const ExecutionContext *exe_ctx, uint32_t idx, uint64_t fail_value) const {
500 const Property *property = GetPropertyAtIndex(exe_ctx, false, idx);
502 OptionValue *value = property->GetValue().get();
504 return value->GetUInt64Value(fail_value);
509 bool OptionValueProperties::SetPropertyAtIndexAsUInt64(
510 const ExecutionContext *exe_ctx, uint32_t idx, uint64_t new_value) {
511 const Property *property = GetPropertyAtIndex(exe_ctx, true, idx);
513 OptionValue *value = property->GetValue().get();
515 return value->SetUInt64Value(new_value);
520 bool OptionValueProperties::Clear() {
521 const size_t num_properties = m_properties.size();
522 for (size_t i = 0; i < num_properties; ++i)
523 m_properties[i].GetValue()->Clear();
527 Status OptionValueProperties::SetValueFromString(llvm::StringRef value,
528 VarSetOperationType op) {
531 // Args args(value_cstr);
532 // const size_t argc = args.GetArgumentCount();
534 case eVarSetOperationClear:
538 case eVarSetOperationReplace:
539 case eVarSetOperationAssign:
540 case eVarSetOperationRemove:
541 case eVarSetOperationInsertBefore:
542 case eVarSetOperationInsertAfter:
543 case eVarSetOperationAppend:
544 case eVarSetOperationInvalid:
545 error = OptionValue::SetValueFromString(value, op);
552 void OptionValueProperties::DumpValue(const ExecutionContext *exe_ctx,
553 Stream &strm, uint32_t dump_mask) {
554 const size_t num_properties = m_properties.size();
555 for (size_t i = 0; i < num_properties; ++i) {
556 const Property *property = GetPropertyAtIndex(exe_ctx, false, i);
558 OptionValue *option_value = property->GetValue().get();
559 assert(option_value);
560 const bool transparent_value = option_value->ValueIsTransparent();
561 property->Dump(exe_ctx, strm, dump_mask);
562 if (!transparent_value)
568 Status OptionValueProperties::DumpPropertyValue(const ExecutionContext *exe_ctx,
570 llvm::StringRef property_path,
571 uint32_t dump_mask) {
573 const bool will_modify = false;
574 lldb::OptionValueSP value_sp(
575 GetSubValue(exe_ctx, property_path, will_modify, error));
577 if (!value_sp->ValueIsTransparent()) {
578 if (dump_mask & eDumpOptionName)
579 strm.PutCString(property_path);
580 if (dump_mask & ~eDumpOptionName)
583 value_sp->DumpValue(exe_ctx, strm, dump_mask);
588 lldb::OptionValueSP OptionValueProperties::DeepCopy() const {
589 llvm_unreachable("this shouldn't happen");
592 const Property *OptionValueProperties::GetPropertyAtPath(
593 const ExecutionContext *exe_ctx, bool will_modify, llvm::StringRef name) const {
594 const Property *property = nullptr;
597 llvm::StringRef sub_name;
599 size_t key_len = name.find_first_of(".[{");
601 if (key_len != llvm::StringRef::npos) {
602 key.SetString(name.take_front(key_len));
603 sub_name = name.drop_front(key_len);
607 property = GetProperty(exe_ctx, will_modify, key);
608 if (sub_name.empty() || !property)
611 if (sub_name[0] == '.') {
612 OptionValueProperties *sub_properties =
613 property->GetValue()->GetAsProperties();
615 return sub_properties->GetPropertyAtPath(exe_ctx, will_modify,
616 sub_name.drop_front());
621 void OptionValueProperties::DumpAllDescriptions(CommandInterpreter &interpreter,
622 Stream &strm) const {
623 size_t max_name_len = 0;
624 const size_t num_properties = m_properties.size();
625 for (size_t i = 0; i < num_properties; ++i) {
626 const Property *property = ProtectedGetPropertyAtIndex(i);
628 max_name_len = std::max<size_t>(property->GetName().size(), max_name_len);
630 for (size_t i = 0; i < num_properties; ++i) {
631 const Property *property = ProtectedGetPropertyAtIndex(i);
633 property->DumpDescription(interpreter, strm, max_name_len, false);
637 void OptionValueProperties::Apropos(
638 llvm::StringRef keyword,
639 std::vector<const Property *> &matching_properties) const {
640 const size_t num_properties = m_properties.size();
642 for (size_t i = 0; i < num_properties; ++i) {
643 const Property *property = ProtectedGetPropertyAtIndex(i);
645 const OptionValueProperties *properties =
646 property->GetValue()->GetAsProperties();
648 properties->Apropos(keyword, matching_properties);
651 llvm::StringRef name = property->GetName();
652 if (name.contains_lower(keyword))
655 llvm::StringRef desc = property->GetDescription();
656 if (desc.contains_lower(keyword))
660 matching_properties.push_back(property);
667 lldb::OptionValuePropertiesSP
668 OptionValueProperties::GetSubProperty(const ExecutionContext *exe_ctx,
670 lldb::OptionValueSP option_value_sp(GetValueForKey(exe_ctx, name, false));
671 if (option_value_sp) {
672 OptionValueProperties *ov_properties = option_value_sp->GetAsProperties();
674 return ov_properties->shared_from_this();
676 return lldb::OptionValuePropertiesSP();