1 //===-- OptionValueDictionary.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/OptionValueDictionary.h"
14 // Other libraries and framework includes
15 #include "llvm/ADT/StringRef.h"
17 #include "lldb/Core/State.h"
18 #include "lldb/DataFormatters/FormatManager.h"
19 #include "lldb/Interpreter/Args.h"
20 #include "lldb/Interpreter/OptionValueString.h"
23 using namespace lldb_private;
25 void OptionValueDictionary::DumpValue(const ExecutionContext *exe_ctx,
26 Stream &strm, uint32_t dump_mask) {
27 const Type dict_type = ConvertTypeMaskToType(m_type_mask);
28 if (dump_mask & eDumpOptionType) {
29 if (m_type_mask != eTypeInvalid)
30 strm.Printf("(%s of %ss)", GetTypeAsCString(),
31 GetBuiltinTypeAsCString(dict_type));
33 strm.Printf("(%s)", GetTypeAsCString());
35 if (dump_mask & eDumpOptionValue) {
36 if (dump_mask & eDumpOptionType)
37 strm.PutCString(" =");
39 collection::iterator pos, end = m_values.end();
43 for (pos = m_values.begin(); pos != end; ++pos) {
44 OptionValue *option_value = pos->second.get();
46 strm.Indent(pos->first.GetCString());
48 const uint32_t extra_dump_options = m_raw_value_dump ? eDumpOptionRaw : 0;
54 case eTypeFileSpecList:
57 option_value->DumpValue(exe_ctx, strm, dump_mask | extra_dump_options);
69 // No need to show the type for dictionaries of simple items
71 option_value->DumpValue(exe_ctx, strm,
72 (dump_mask & (~eDumpOptionType)) |
81 size_t OptionValueDictionary::GetArgs(Args &args) const {
83 collection::const_iterator pos, end = m_values.end();
84 for (pos = m_values.begin(); pos != end; ++pos) {
86 strm.Printf("%s=", pos->first.GetCString());
87 pos->second->DumpValue(nullptr, strm, eDumpOptionValue | eDumpOptionRaw);
88 args.AppendArgument(strm.GetString());
90 return args.GetArgumentCount();
93 Status OptionValueDictionary::SetArgs(const Args &args,
94 VarSetOperationType op) {
96 const size_t argc = args.GetArgumentCount();
98 case eVarSetOperationClear:
102 case eVarSetOperationAppend:
103 case eVarSetOperationReplace:
104 case eVarSetOperationAssign:
106 error.SetErrorString(
107 "assign operation takes one or more key=value arguments");
110 for (const auto &entry : args) {
111 if (entry.ref.empty()) {
112 error.SetErrorString("empty argument");
115 if (!entry.ref.contains('=')) {
116 error.SetErrorString(
117 "assign operation takes one or more key=value arguments");
121 llvm::StringRef key, value;
122 std::tie(key, value) = entry.ref.split('=');
123 bool key_valid = false;
125 error.SetErrorString("empty dictionary key");
129 if (key.front() == '[') {
130 // Key name starts with '[', so the key value must be in single or
131 // double quotes like:
134 if ((key.size() > 2) && (key.back() == ']')) {
135 // Strip leading '[' and trailing ']'
136 key = key.substr(1, key.size() - 2);
137 const char quote_char = key.front();
138 if ((quote_char == '\'') || (quote_char == '"')) {
139 if ((key.size() > 2) && (key.back() == quote_char)) {
141 key = key.substr(1, key.size() - 2);
145 // square brackets, no quotes
150 // No square brackets or quotes
154 error.SetErrorStringWithFormat(
155 "invalid key \"%s\", the key must be a bare string or "
156 "surrounded by brackets with optional quotes: [<key>] or "
157 "['<key>'] or [\"<key>\"]",
162 lldb::OptionValueSP value_sp(CreateValueFromCStringForTypeMask(
163 value.str().c_str(), m_type_mask, error));
167 m_value_was_set = true;
168 SetValueForKey(ConstString(key), value_sp, true);
170 error.SetErrorString("dictionaries that can contain multiple types "
171 "must subclass OptionValueArray");
176 case eVarSetOperationRemove:
178 for (size_t i = 0; i < argc; ++i) {
179 ConstString key(args.GetArgumentAtIndex(i));
180 if (!DeleteValueForKey(key)) {
181 error.SetErrorStringWithFormat(
182 "no value found named '%s', aborting remove operation",
188 error.SetErrorString("remove operation takes one or more key arguments");
192 case eVarSetOperationInsertBefore:
193 case eVarSetOperationInsertAfter:
194 case eVarSetOperationInvalid:
195 error = OptionValue::SetValueFromString(llvm::StringRef(), op);
201 Status OptionValueDictionary::SetValueFromString(llvm::StringRef value,
202 VarSetOperationType op) {
203 Args args(value.str());
204 Status error = SetArgs(args, op);
206 NotifyValueChanged();
211 OptionValueDictionary::GetSubValue(const ExecutionContext *exe_ctx,
212 llvm::StringRef name, bool will_modify,
213 Status &error) const {
214 lldb::OptionValueSP value_sp;
218 llvm::StringRef left, temp;
219 std::tie(left, temp) = name.split('[');
220 if (left.size() == name.size()) {
221 error.SetErrorStringWithFormat("invalid value path '%s', %s values only "
222 "support '[<key>]' subvalues where <key> "
223 "a string value optionally delimited by "
224 "single or double quotes",
225 name.str().c_str(), GetTypeAsCString());
228 assert(!temp.empty());
230 llvm::StringRef key, value;
231 llvm::StringRef quote_char;
233 if (temp[0] == '\"' || temp[0] == '\'') {
234 quote_char = temp.take_front();
235 temp = temp.drop_front();
238 llvm::StringRef sub_name;
239 std::tie(key, sub_name) = temp.split(']');
241 if (!key.consume_back(quote_char) || key.empty()) {
242 error.SetErrorStringWithFormat("invalid value path '%s', "
243 "key names must be formatted as ['<key>'] where <key> "
244 "is a string that doesn't contain quotes and the quote"
245 " char is optional", name.str().c_str());
249 value_sp = GetValueForKey(ConstString(key));
251 error.SetErrorStringWithFormat(
252 "dictionary does not contain a value for the key name '%s'",
257 if (sub_name.empty())
259 return value_sp->GetSubValue(exe_ctx, sub_name, will_modify, error);
262 Status OptionValueDictionary::SetSubValue(const ExecutionContext *exe_ctx,
263 VarSetOperationType op,
264 llvm::StringRef name,
265 llvm::StringRef value) {
267 const bool will_modify = true;
268 lldb::OptionValueSP value_sp(GetSubValue(exe_ctx, name, will_modify, error));
270 error = value_sp->SetValueFromString(value, op);
272 if (error.AsCString() == nullptr)
273 error.SetErrorStringWithFormat("invalid value path '%s'", name.str().c_str());
279 OptionValueDictionary::GetValueForKey(const ConstString &key) const {
280 lldb::OptionValueSP value_sp;
281 collection::const_iterator pos = m_values.find(key);
282 if (pos != m_values.end())
283 value_sp = pos->second;
287 bool OptionValueDictionary::SetValueForKey(const ConstString &key,
288 const lldb::OptionValueSP &value_sp,
290 // Make sure the value_sp object is allowed to contain
291 // values of the type passed in...
292 if (value_sp && (m_type_mask & value_sp->GetTypeAsMask())) {
294 collection::const_iterator pos = m_values.find(key);
295 if (pos != m_values.end())
298 m_values[key] = value_sp;
304 bool OptionValueDictionary::DeleteValueForKey(const ConstString &key) {
305 collection::iterator pos = m_values.find(key);
306 if (pos != m_values.end()) {
313 lldb::OptionValueSP OptionValueDictionary::DeepCopy() const {
314 OptionValueDictionary *copied_dict =
315 new OptionValueDictionary(m_type_mask, m_raw_value_dump);
316 lldb::OptionValueSP copied_value_sp(copied_dict);
317 collection::const_iterator pos, end = m_values.end();
318 for (pos = m_values.begin(); pos != end; ++pos) {
320 strm.Printf("%s=", pos->first.GetCString());
321 copied_dict->SetValueForKey(pos->first, pos->second->DeepCopy(), true);
323 return copied_value_sp;