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/OptionValueString.h"
20 #include "lldb/Utility/Args.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: ['<key>'] ["<key>"]
132 if ((key.size() > 2) && (key.back() == ']')) {
133 // Strip leading '[' and trailing ']'
134 key = key.substr(1, key.size() - 2);
135 const char quote_char = key.front();
136 if ((quote_char == '\'') || (quote_char == '"')) {
137 if ((key.size() > 2) && (key.back() == quote_char)) {
139 key = key.substr(1, key.size() - 2);
143 // square brackets, no quotes
148 // No square brackets or quotes
152 error.SetErrorStringWithFormat(
153 "invalid key \"%s\", the key must be a bare string or "
154 "surrounded by brackets with optional quotes: [<key>] or "
155 "['<key>'] or [\"<key>\"]",
160 lldb::OptionValueSP value_sp(CreateValueFromCStringForTypeMask(
161 value.str().c_str(), m_type_mask, error));
165 m_value_was_set = true;
166 SetValueForKey(ConstString(key), value_sp, true);
168 error.SetErrorString("dictionaries that can contain multiple types "
169 "must subclass OptionValueArray");
174 case eVarSetOperationRemove:
176 for (size_t i = 0; i < argc; ++i) {
177 ConstString key(args.GetArgumentAtIndex(i));
178 if (!DeleteValueForKey(key)) {
179 error.SetErrorStringWithFormat(
180 "no value found named '%s', aborting remove operation",
186 error.SetErrorString("remove operation takes one or more key arguments");
190 case eVarSetOperationInsertBefore:
191 case eVarSetOperationInsertAfter:
192 case eVarSetOperationInvalid:
193 error = OptionValue::SetValueFromString(llvm::StringRef(), op);
199 Status OptionValueDictionary::SetValueFromString(llvm::StringRef value,
200 VarSetOperationType op) {
201 Args args(value.str());
202 Status error = SetArgs(args, op);
204 NotifyValueChanged();
209 OptionValueDictionary::GetSubValue(const ExecutionContext *exe_ctx,
210 llvm::StringRef name, bool will_modify,
211 Status &error) const {
212 lldb::OptionValueSP value_sp;
216 llvm::StringRef left, temp;
217 std::tie(left, temp) = name.split('[');
218 if (left.size() == name.size()) {
219 error.SetErrorStringWithFormat("invalid value path '%s', %s values only "
220 "support '[<key>]' subvalues where <key> "
221 "a string value optionally delimited by "
222 "single or double quotes",
223 name.str().c_str(), GetTypeAsCString());
226 assert(!temp.empty());
228 llvm::StringRef key, quote_char;
230 if (temp[0] == '\"' || temp[0] == '\'') {
231 quote_char = temp.take_front();
232 temp = temp.drop_front();
235 llvm::StringRef sub_name;
236 std::tie(key, sub_name) = temp.split(']');
238 if (!key.consume_back(quote_char) || key.empty()) {
239 error.SetErrorStringWithFormat("invalid value path '%s', "
240 "key names must be formatted as ['<key>'] where <key> "
241 "is a string that doesn't contain quotes and the quote"
242 " char is optional", name.str().c_str());
246 value_sp = GetValueForKey(ConstString(key));
248 error.SetErrorStringWithFormat(
249 "dictionary does not contain a value for the key name '%s'",
254 if (sub_name.empty())
256 return value_sp->GetSubValue(exe_ctx, sub_name, will_modify, error);
259 Status OptionValueDictionary::SetSubValue(const ExecutionContext *exe_ctx,
260 VarSetOperationType op,
261 llvm::StringRef name,
262 llvm::StringRef value) {
264 const bool will_modify = true;
265 lldb::OptionValueSP value_sp(GetSubValue(exe_ctx, name, will_modify, error));
267 error = value_sp->SetValueFromString(value, op);
269 if (error.AsCString() == nullptr)
270 error.SetErrorStringWithFormat("invalid value path '%s'", name.str().c_str());
276 OptionValueDictionary::GetValueForKey(const ConstString &key) const {
277 lldb::OptionValueSP value_sp;
278 collection::const_iterator pos = m_values.find(key);
279 if (pos != m_values.end())
280 value_sp = pos->second;
284 bool OptionValueDictionary::SetValueForKey(const ConstString &key,
285 const lldb::OptionValueSP &value_sp,
287 // Make sure the value_sp object is allowed to contain values of the type
289 if (value_sp && (m_type_mask & value_sp->GetTypeAsMask())) {
291 collection::const_iterator pos = m_values.find(key);
292 if (pos != m_values.end())
295 m_values[key] = value_sp;
301 bool OptionValueDictionary::DeleteValueForKey(const ConstString &key) {
302 collection::iterator pos = m_values.find(key);
303 if (pos != m_values.end()) {
310 lldb::OptionValueSP OptionValueDictionary::DeepCopy() const {
311 OptionValueDictionary *copied_dict =
312 new OptionValueDictionary(m_type_mask, m_raw_value_dump);
313 lldb::OptionValueSP copied_value_sp(copied_dict);
314 collection::const_iterator pos, end = m_values.end();
315 for (pos = m_values.begin(); pos != end; ++pos) {
317 strm.Printf("%s=", pos->first.GetCString());
318 copied_dict->SetValueForKey(pos->first, pos->second->DeepCopy(), true);
320 return copied_value_sp;