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 Error OptionValueDictionary::SetArgs(const Args &args, VarSetOperationType op) {
95 const size_t argc = args.GetArgumentCount();
97 case eVarSetOperationClear:
101 case eVarSetOperationAppend:
102 case eVarSetOperationReplace:
103 case eVarSetOperationAssign:
105 error.SetErrorString(
106 "assign operation takes one or more key=value arguments");
109 for (const auto &entry : args) {
110 if (entry.ref.empty()) {
111 error.SetErrorString("empty argument");
114 if (!entry.ref.contains('=')) {
115 error.SetErrorString(
116 "assign operation takes one or more key=value arguments");
120 llvm::StringRef key, value;
121 std::tie(key, value) = entry.ref.split('=');
122 bool key_valid = false;
124 error.SetErrorString("empty dictionary key");
128 if (key.front() == '[') {
129 // Key name starts with '[', so the key value must be in single or
130 // double quotes like:
133 if ((key.size() > 2) && (key.back() == ']')) {
134 // Strip leading '[' and trailing ']'
135 key = key.substr(1, key.size() - 2);
136 const char quote_char = key.front();
137 if ((quote_char == '\'') || (quote_char == '"')) {
138 if ((key.size() > 2) && (key.back() == quote_char)) {
140 key = key.substr(1, key.size() - 2);
144 // square brackets, no quotes
149 // No square brackets or quotes
153 error.SetErrorStringWithFormat(
154 "invalid key \"%s\", the key must be a bare string or "
155 "surrounded by brackets with optional quotes: [<key>] or "
156 "['<key>'] or [\"<key>\"]",
161 lldb::OptionValueSP value_sp(CreateValueFromCStringForTypeMask(
162 value.str().c_str(), m_type_mask, error));
166 m_value_was_set = true;
167 SetValueForKey(ConstString(key), value_sp, true);
169 error.SetErrorString("dictionaries that can contain multiple types "
170 "must subclass OptionValueArray");
175 case eVarSetOperationRemove:
177 for (size_t i = 0; i < argc; ++i) {
178 ConstString key(args.GetArgumentAtIndex(i));
179 if (!DeleteValueForKey(key)) {
180 error.SetErrorStringWithFormat(
181 "no value found named '%s', aborting remove operation",
187 error.SetErrorString("remove operation takes one or more key arguments");
191 case eVarSetOperationInsertBefore:
192 case eVarSetOperationInsertAfter:
193 case eVarSetOperationInvalid:
194 error = OptionValue::SetValueFromString(llvm::StringRef(), op);
200 Error OptionValueDictionary::SetValueFromString(llvm::StringRef value,
201 VarSetOperationType op) {
202 Args args(value.str());
203 Error error = SetArgs(args, op);
205 NotifyValueChanged();
210 OptionValueDictionary::GetSubValue(const ExecutionContext *exe_ctx,
211 llvm::StringRef name, bool will_modify,
212 Error &error) const {
213 lldb::OptionValueSP value_sp;
217 llvm::StringRef left, temp;
218 std::tie(left, temp) = name.split('[');
219 if (left.size() == name.size()) {
220 error.SetErrorStringWithFormat("invalid value path '%s', %s values only "
221 "support '[<key>]' subvalues where <key> "
222 "a string value optionally delimited by "
223 "single or double quotes",
224 name.str().c_str(), GetTypeAsCString());
227 assert(!temp.empty());
229 llvm::StringRef key, value;
230 llvm::StringRef quote_char;
232 if (temp[0] == '\"' || temp[0] == '\'') {
233 quote_char = temp.take_front();
234 temp = temp.drop_front();
237 llvm::StringRef sub_name;
238 std::tie(key, sub_name) = temp.split(']');
240 if (!key.consume_back(quote_char) || key.empty()) {
241 error.SetErrorStringWithFormat("invalid value path '%s', "
242 "key names must be formatted as ['<key>'] where <key> "
243 "is a string that doesn't contain quotes and the quote"
244 " char is optional", name.str().c_str());
248 value_sp = GetValueForKey(ConstString(key));
250 error.SetErrorStringWithFormat(
251 "dictionary does not contain a value for the key name '%s'",
256 if (sub_name.empty())
258 return value_sp->GetSubValue(exe_ctx, sub_name, will_modify, error);
261 Error OptionValueDictionary::SetSubValue(const ExecutionContext *exe_ctx,
262 VarSetOperationType op,
263 llvm::StringRef name, llvm::StringRef value) {
265 const bool will_modify = true;
266 lldb::OptionValueSP value_sp(GetSubValue(exe_ctx, name, will_modify, error));
268 error = value_sp->SetValueFromString(value, op);
270 if (error.AsCString() == nullptr)
271 error.SetErrorStringWithFormat("invalid value path '%s'", name.str().c_str());
277 OptionValueDictionary::GetValueForKey(const ConstString &key) const {
278 lldb::OptionValueSP value_sp;
279 collection::const_iterator pos = m_values.find(key);
280 if (pos != m_values.end())
281 value_sp = pos->second;
285 bool OptionValueDictionary::SetValueForKey(const ConstString &key,
286 const lldb::OptionValueSP &value_sp,
288 // Make sure the value_sp object is allowed to contain
289 // values of the type passed in...
290 if (value_sp && (m_type_mask & value_sp->GetTypeAsMask())) {
292 collection::const_iterator pos = m_values.find(key);
293 if (pos != m_values.end())
296 m_values[key] = value_sp;
302 bool OptionValueDictionary::DeleteValueForKey(const ConstString &key) {
303 collection::iterator pos = m_values.find(key);
304 if (pos != m_values.end()) {
311 lldb::OptionValueSP OptionValueDictionary::DeepCopy() const {
312 OptionValueDictionary *copied_dict =
313 new OptionValueDictionary(m_type_mask, m_raw_value_dump);
314 lldb::OptionValueSP copied_value_sp(copied_dict);
315 collection::const_iterator pos, end = m_values.end();
316 for (pos = m_values.begin(); pos != end; ++pos) {
318 strm.Printf("%s=", pos->first.GetCString());
319 copied_dict->SetValueForKey(pos->first, pos->second->DeepCopy(), true);
321 return copied_value_sp;