1//===-- OptionValueDictionary.cpp -----------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/Interpreter/OptionValueDictionary.h"
10
11#include "lldb/DataFormatters/FormatManager.h"
12#include "lldb/Interpreter/OptionValueEnumeration.h"
13#include "lldb/Interpreter/OptionValueString.h"
14#include "lldb/Utility/Args.h"
15#include "lldb/Utility/State.h"
16#include "llvm/ADT/StringRef.h"
17
18using namespace lldb;
19using namespace lldb_private;
20
21void OptionValueDictionary::DumpValue(const ExecutionContext *exe_ctx,
22 Stream &strm, uint32_t dump_mask) {
23 const Type dict_type = ConvertTypeMaskToType(type_mask: m_type_mask);
24 if (dump_mask & eDumpOptionType) {
25 if (m_type_mask != eTypeInvalid)
26 strm.Printf(format: "(%s of %ss)", GetTypeAsCString(),
27 GetBuiltinTypeAsCString(t: dict_type));
28 else
29 strm.Printf(format: "(%s)", GetTypeAsCString());
30 }
31 if (dump_mask & eDumpOptionValue) {
32 const bool one_line = dump_mask & eDumpOptionCommand;
33 if (dump_mask & eDumpOptionType)
34 strm.PutCString(cstr: " =");
35
36 if (!one_line)
37 strm.IndentMore();
38
39 // m_values is not guaranteed to be sorted alphabetically, so for
40 // consistentcy we will sort them here before dumping
41 std::map<llvm::StringRef, OptionValue *> sorted_values;
42 for (const auto &value : m_values) {
43 sorted_values[value.first()] = value.second.get();
44 }
45 for (const auto &value : sorted_values) {
46 OptionValue *option_value = value.second;
47
48 if (one_line)
49 strm << ' ';
50 else
51 strm.EOL();
52
53 strm.Indent(s: value.first);
54
55 const uint32_t extra_dump_options = m_raw_value_dump ? eDumpOptionRaw : 0;
56 switch (dict_type) {
57 default:
58 case eTypeArray:
59 case eTypeDictionary:
60 case eTypeProperties:
61 case eTypeFileSpecList:
62 case eTypePathMap:
63 strm.PutChar(ch: ' ');
64 option_value->DumpValue(exe_ctx, strm, dump_mask: dump_mask | extra_dump_options);
65 break;
66
67 case eTypeBoolean:
68 case eTypeChar:
69 case eTypeEnum:
70 case eTypeFileLineColumn:
71 case eTypeFileSpec:
72 case eTypeFormat:
73 case eTypeSInt64:
74 case eTypeString:
75 case eTypeUInt64:
76 case eTypeUUID:
77 // No need to show the type for dictionaries of simple items
78 strm.PutCString(cstr: "=");
79 option_value->DumpValue(exe_ctx, strm,
80 dump_mask: (dump_mask & (~eDumpOptionType)) |
81 extra_dump_options);
82 break;
83 }
84 }
85 if (!one_line)
86 strm.IndentLess();
87 }
88}
89
90llvm::json::Value
91OptionValueDictionary::ToJSON(const ExecutionContext *exe_ctx) const {
92 llvm::json::Object dict;
93 for (const auto &value : m_values) {
94 dict.try_emplace(K: value.first(), Args: value.second->ToJSON(exe_ctx));
95 }
96 return dict;
97}
98
99size_t OptionValueDictionary::GetArgs(Args &args) const {
100 args.Clear();
101 for (const auto &value : m_values) {
102 StreamString strm;
103 strm.Printf(format: "%s=", value.first().data());
104 value.second->DumpValue(exe_ctx: nullptr, strm, dump_mask: eDumpOptionValue | eDumpOptionRaw);
105 args.AppendArgument(arg_str: strm.GetString());
106 }
107 return args.GetArgumentCount();
108}
109
110Status OptionValueDictionary::SetArgs(const Args &args,
111 VarSetOperationType op) {
112 Status error;
113 const size_t argc = args.GetArgumentCount();
114 switch (op) {
115 case eVarSetOperationClear:
116 Clear();
117 break;
118
119 case eVarSetOperationAppend:
120 case eVarSetOperationReplace:
121 case eVarSetOperationAssign:
122 if (argc == 0) {
123 error = Status::FromErrorString(
124 str: "assign operation takes one or more key=value arguments");
125 return error;
126 }
127 for (const auto &entry : args) {
128 if (entry.ref().empty()) {
129 error = Status::FromErrorString(str: "empty argument");
130 return error;
131 }
132 if (!entry.ref().contains(C: '=')) {
133 error = Status::FromErrorString(
134 str: "assign operation takes one or more key=value arguments");
135 return error;
136 }
137
138 llvm::StringRef key, value;
139 std::tie(args&: key, args&: value) = entry.ref().split(Separator: '=');
140 bool key_valid = false;
141 if (key.empty()) {
142 error = Status::FromErrorString(str: "empty dictionary key");
143 return error;
144 }
145
146 if (key.front() == '[') {
147 // Key name starts with '[', so the key value must be in single or
148 // double quotes like: ['<key>'] ["<key>"]
149 if ((key.size() > 2) && (key.back() == ']')) {
150 // Strip leading '[' and trailing ']'
151 key = key.substr(Start: 1, N: key.size() - 2);
152 const char quote_char = key.front();
153 if ((quote_char == '\'') || (quote_char == '"')) {
154 if ((key.size() > 2) && (key.back() == quote_char)) {
155 // Strip the quotes
156 key = key.substr(Start: 1, N: key.size() - 2);
157 key_valid = true;
158 }
159 } else {
160 // square brackets, no quotes
161 key_valid = true;
162 }
163 }
164 } else {
165 // No square brackets or quotes
166 key_valid = true;
167 }
168 if (!key_valid) {
169 error = Status::FromErrorStringWithFormat(
170 format: "invalid key \"%s\", the key must be a bare string or "
171 "surrounded by brackets with optional quotes: [<key>] or "
172 "['<key>'] or [\"<key>\"]",
173 key.str().c_str());
174 return error;
175 }
176
177 if (m_type_mask == 1u << eTypeEnum) {
178 auto enum_value =
179 std::make_shared<OptionValueEnumeration>(args&: m_enum_values, args: 0);
180 error = enum_value->SetValueFromString(value);
181 if (error.Fail())
182 return error;
183 m_value_was_set = true;
184 SetValueForKey(key, value_sp: enum_value, can_replace: true);
185 } else {
186 lldb::OptionValueSP value_sp(CreateValueFromCStringForTypeMask(
187 value_cstr: value.str().c_str(), type_mask: m_type_mask, error));
188 if (value_sp) {
189 if (error.Fail())
190 return error;
191 m_value_was_set = true;
192 SetValueForKey(key, value_sp, can_replace: true);
193 } else {
194 error = Status::FromErrorString(
195 str: "dictionaries that can contain multiple types "
196 "must subclass OptionValueArray");
197 }
198 }
199 }
200 break;
201
202 case eVarSetOperationRemove:
203 if (argc > 0) {
204 for (size_t i = 0; i < argc; ++i) {
205 llvm::StringRef key(args.GetArgumentAtIndex(idx: i));
206 if (!DeleteValueForKey(key)) {
207 error = Status::FromErrorStringWithFormat(
208 format: "no value found named '%s', aborting remove operation",
209 key.data());
210 break;
211 }
212 }
213 } else {
214 error = Status::FromErrorString(
215 str: "remove operation takes one or more key arguments");
216 }
217 break;
218
219 case eVarSetOperationInsertBefore:
220 case eVarSetOperationInsertAfter:
221 case eVarSetOperationInvalid:
222 error = OptionValue::SetValueFromString(value: llvm::StringRef(), op);
223 break;
224 }
225 return error;
226}
227
228Status OptionValueDictionary::SetValueFromString(llvm::StringRef value,
229 VarSetOperationType op) {
230 Args args(value.str());
231 Status error = SetArgs(args, op);
232 if (error.Success())
233 NotifyValueChanged();
234 return error;
235}
236
237lldb::OptionValueSP
238OptionValueDictionary::GetSubValue(const ExecutionContext *exe_ctx,
239 llvm::StringRef name, Status &error) const {
240 lldb::OptionValueSP value_sp;
241 if (name.empty())
242 return nullptr;
243
244 llvm::StringRef left, temp;
245 std::tie(args&: left, args&: temp) = name.split(Separator: '[');
246 if (left.size() == name.size()) {
247 error = Status::FromErrorStringWithFormat(
248 format: "invalid value path '%s', %s values only "
249 "support '[<key>]' subvalues where <key> "
250 "a string value optionally delimited by "
251 "single or double quotes",
252 name.str().c_str(), GetTypeAsCString());
253 return nullptr;
254 }
255 assert(!temp.empty());
256
257 llvm::StringRef key, quote_char;
258
259 if (temp[0] == '\"' || temp[0] == '\'') {
260 quote_char = temp.take_front();
261 temp = temp.drop_front();
262 }
263
264 llvm::StringRef sub_name;
265 std::tie(args&: key, args&: sub_name) = temp.split(Separator: ']');
266
267 if (!key.consume_back(Suffix: quote_char) || key.empty()) {
268 error = Status::FromErrorStringWithFormat(
269 format: "invalid value path '%s', "
270 "key names must be formatted as ['<key>'] where <key> "
271 "is a string that doesn't contain quotes and the quote"
272 " char is optional",
273 name.str().c_str());
274 return nullptr;
275 }
276
277 value_sp = GetValueForKey(key);
278 if (!value_sp) {
279 error = Status::FromErrorStringWithFormat(
280 format: "dictionary does not contain a value for the key name '%s'",
281 key.str().c_str());
282 return nullptr;
283 }
284
285 if (sub_name.empty())
286 return value_sp;
287 return value_sp->GetSubValue(exe_ctx, name: sub_name, error);
288}
289
290Status OptionValueDictionary::SetSubValue(const ExecutionContext *exe_ctx,
291 VarSetOperationType op,
292 llvm::StringRef name,
293 llvm::StringRef value) {
294 Status error;
295 lldb::OptionValueSP value_sp(GetSubValue(exe_ctx, name, error));
296 if (value_sp)
297 error = value_sp->SetValueFromString(value, op);
298 else {
299 if (error.AsCString() == nullptr)
300 error = Status::FromErrorStringWithFormat(format: "invalid value path '%s'",
301 name.str().c_str());
302 }
303 return error;
304}
305
306lldb::OptionValueSP
307OptionValueDictionary::GetValueForKey(llvm::StringRef key) const {
308 lldb::OptionValueSP value_sp;
309 auto pos = m_values.find(Key: key);
310 if (pos != m_values.end())
311 value_sp = pos->second;
312 return value_sp;
313}
314
315bool OptionValueDictionary::SetValueForKey(llvm::StringRef key,
316 const lldb::OptionValueSP &value_sp,
317 bool can_replace) {
318 // Make sure the value_sp object is allowed to contain values of the type
319 // passed in...
320 if (value_sp && (m_type_mask & value_sp->GetTypeAsMask())) {
321 if (!can_replace) {
322 auto pos = m_values.find(Key: key);
323 if (pos != m_values.end())
324 return false;
325 }
326 m_values[key] = value_sp;
327 return true;
328 }
329 return false;
330}
331
332bool OptionValueDictionary::DeleteValueForKey(llvm::StringRef key) {
333 auto pos = m_values.find(Key: key);
334 if (pos != m_values.end()) {
335 m_values.erase(I: pos);
336 return true;
337 }
338 return false;
339}
340
341OptionValueSP
342OptionValueDictionary::DeepCopy(const OptionValueSP &new_parent) const {
343 auto copy_sp = OptionValue::DeepCopy(new_parent);
344 // copy_sp->GetAsDictionary cannot be used here as it doesn't work for derived
345 // types that override GetType returning a different value.
346 auto *dict_value_ptr = static_cast<OptionValueDictionary *>(copy_sp.get());
347 lldbassert(dict_value_ptr);
348
349 for (auto &value : dict_value_ptr->m_values)
350 value.second = value.second->DeepCopy(new_parent: copy_sp);
351
352 return copy_sp;
353}
354

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of lldb/source/Interpreter/OptionValueDictionary.cpp