1 //===-- OptionValueDictionary.cpp -----------------------------------------===//
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/OptionValueDictionary.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "lldb/DataFormatters/FormatManager.h"
13 #include "lldb/Interpreter/OptionValueString.h"
14 #include "lldb/Utility/Args.h"
15 #include "lldb/Utility/State.h"
18 using namespace lldb_private;
20 void OptionValueDictionary::DumpValue(const ExecutionContext *exe_ctx,
21 Stream &strm, uint32_t dump_mask) {
22 const Type dict_type = ConvertTypeMaskToType(m_type_mask);
23 if (dump_mask & eDumpOptionType) {
24 if (m_type_mask != eTypeInvalid)
25 strm.Printf("(%s of %ss)", GetTypeAsCString(),
26 GetBuiltinTypeAsCString(dict_type));
28 strm.Printf("(%s)", GetTypeAsCString());
30 if (dump_mask & eDumpOptionValue) {
31 const bool one_line = dump_mask & eDumpOptionCommand;
32 if (dump_mask & eDumpOptionType)
33 strm.PutCString(" =");
35 collection::iterator pos, end = m_values.end();
40 for (pos = m_values.begin(); pos != end; ++pos) {
41 OptionValue *option_value = pos->second.get();
48 strm.Indent(pos->first.GetStringRef());
50 const uint32_t extra_dump_options = m_raw_value_dump ? eDumpOptionRaw : 0;
56 case eTypeFileSpecList:
59 option_value->DumpValue(exe_ctx, strm, dump_mask | extra_dump_options);
65 case eTypeFileLineColumn:
72 // No need to show the type for dictionaries of simple items
74 option_value->DumpValue(exe_ctx, strm,
75 (dump_mask & (~eDumpOptionType)) |
85 size_t OptionValueDictionary::GetArgs(Args &args) const {
87 collection::const_iterator pos, end = m_values.end();
88 for (pos = m_values.begin(); pos != end; ++pos) {
90 strm.Printf("%s=", pos->first.GetCString());
91 pos->second->DumpValue(nullptr, strm, eDumpOptionValue | eDumpOptionRaw);
92 args.AppendArgument(strm.GetString());
94 return args.GetArgumentCount();
97 Status OptionValueDictionary::SetArgs(const Args &args,
98 VarSetOperationType op) {
100 const size_t argc = args.GetArgumentCount();
102 case eVarSetOperationClear:
106 case eVarSetOperationAppend:
107 case eVarSetOperationReplace:
108 case eVarSetOperationAssign:
110 error.SetErrorString(
111 "assign operation takes one or more key=value arguments");
114 for (const auto &entry : args) {
115 if (entry.ref().empty()) {
116 error.SetErrorString("empty argument");
119 if (!entry.ref().contains('=')) {
120 error.SetErrorString(
121 "assign operation takes one or more key=value arguments");
125 llvm::StringRef key, value;
126 std::tie(key, value) = entry.ref().split('=');
127 bool key_valid = false;
129 error.SetErrorString("empty dictionary key");
133 if (key.front() == '[') {
134 // Key name starts with '[', so the key value must be in single or
135 // double quotes like: ['<key>'] ["<key>"]
136 if ((key.size() > 2) && (key.back() == ']')) {
137 // Strip leading '[' and trailing ']'
138 key = key.substr(1, key.size() - 2);
139 const char quote_char = key.front();
140 if ((quote_char == '\'') || (quote_char == '"')) {
141 if ((key.size() > 2) && (key.back() == quote_char)) {
143 key = key.substr(1, key.size() - 2);
147 // square brackets, no quotes
152 // No square brackets or quotes
156 error.SetErrorStringWithFormat(
157 "invalid key \"%s\", the key must be a bare string or "
158 "surrounded by brackets with optional quotes: [<key>] or "
159 "['<key>'] or [\"<key>\"]",
164 lldb::OptionValueSP value_sp(CreateValueFromCStringForTypeMask(
165 value.str().c_str(), m_type_mask, error));
169 m_value_was_set = true;
170 SetValueForKey(ConstString(key), value_sp, true);
172 error.SetErrorString("dictionaries that can contain multiple types "
173 "must subclass OptionValueArray");
178 case eVarSetOperationRemove:
180 for (size_t i = 0; i < argc; ++i) {
181 ConstString key(args.GetArgumentAtIndex(i));
182 if (!DeleteValueForKey(key)) {
183 error.SetErrorStringWithFormat(
184 "no value found named '%s', aborting remove operation",
190 error.SetErrorString("remove operation takes one or more key arguments");
194 case eVarSetOperationInsertBefore:
195 case eVarSetOperationInsertAfter:
196 case eVarSetOperationInvalid:
197 error = OptionValue::SetValueFromString(llvm::StringRef(), op);
203 Status OptionValueDictionary::SetValueFromString(llvm::StringRef value,
204 VarSetOperationType op) {
205 Args args(value.str());
206 Status error = SetArgs(args, op);
208 NotifyValueChanged();
213 OptionValueDictionary::GetSubValue(const ExecutionContext *exe_ctx,
214 llvm::StringRef name, bool will_modify,
215 Status &error) const {
216 lldb::OptionValueSP value_sp;
220 llvm::StringRef left, temp;
221 std::tie(left, temp) = name.split('[');
222 if (left.size() == name.size()) {
223 error.SetErrorStringWithFormat("invalid value path '%s', %s values only "
224 "support '[<key>]' subvalues where <key> "
225 "a string value optionally delimited by "
226 "single or double quotes",
227 name.str().c_str(), GetTypeAsCString());
230 assert(!temp.empty());
232 llvm::StringRef key, quote_char;
234 if (temp[0] == '\"' || temp[0] == '\'') {
235 quote_char = temp.take_front();
236 temp = temp.drop_front();
239 llvm::StringRef sub_name;
240 std::tie(key, sub_name) = temp.split(']');
242 if (!key.consume_back(quote_char) || key.empty()) {
243 error.SetErrorStringWithFormat("invalid value path '%s', "
244 "key names must be formatted as ['<key>'] where <key> "
245 "is a string that doesn't contain quotes and the quote"
246 " char is optional", name.str().c_str());
250 value_sp = GetValueForKey(ConstString(key));
252 error.SetErrorStringWithFormat(
253 "dictionary does not contain a value for the key name '%s'",
258 if (sub_name.empty())
260 return value_sp->GetSubValue(exe_ctx, sub_name, will_modify, error);
263 Status OptionValueDictionary::SetSubValue(const ExecutionContext *exe_ctx,
264 VarSetOperationType op,
265 llvm::StringRef name,
266 llvm::StringRef value) {
268 const bool will_modify = true;
269 lldb::OptionValueSP value_sp(GetSubValue(exe_ctx, name, will_modify, error));
271 error = value_sp->SetValueFromString(value, op);
273 if (error.AsCString() == nullptr)
274 error.SetErrorStringWithFormat("invalid value path '%s'", name.str().c_str());
280 OptionValueDictionary::GetValueForKey(ConstString key) const {
281 lldb::OptionValueSP value_sp;
282 collection::const_iterator pos = m_values.find(key);
283 if (pos != m_values.end())
284 value_sp = pos->second;
288 bool OptionValueDictionary::SetValueForKey(ConstString key,
289 const lldb::OptionValueSP &value_sp,
291 // Make sure the value_sp object is allowed to contain values of the type
293 if (value_sp && (m_type_mask & value_sp->GetTypeAsMask())) {
295 collection::const_iterator pos = m_values.find(key);
296 if (pos != m_values.end())
299 m_values[key] = value_sp;
305 bool OptionValueDictionary::DeleteValueForKey(ConstString key) {
306 collection::iterator pos = m_values.find(key);
307 if (pos != m_values.end()) {
315 OptionValueDictionary::DeepCopy(const OptionValueSP &new_parent) const {
316 auto copy_sp = OptionValue::DeepCopy(new_parent);
317 // copy_sp->GetAsDictionary cannot be used here as it doesn't work for derived
318 // types that override GetType returning a different value.
319 auto *dict_value_ptr = static_cast<OptionValueDictionary *>(copy_sp.get());
320 lldbassert(dict_value_ptr);
322 for (auto &value : dict_value_ptr->m_values)
323 value.second = value.second->DeepCopy(copy_sp);