]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Interpreter/OptionValueDictionary.cpp
MFV r322223: 8378 crash due to bp in-memory modification of nopwrite block
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Interpreter / OptionValueDictionary.cpp
1 //===-- OptionValueDictionary.cpp -------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/Interpreter/OptionValueDictionary.h"
11
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 #include "llvm/ADT/StringRef.h"
16 // Project includes
17 #include "lldb/Core/State.h"
18 #include "lldb/DataFormatters/FormatManager.h"
19 #include "lldb/Interpreter/Args.h"
20 #include "lldb/Interpreter/OptionValueString.h"
21
22 using namespace lldb;
23 using namespace lldb_private;
24
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));
32     else
33       strm.Printf("(%s)", GetTypeAsCString());
34   }
35   if (dump_mask & eDumpOptionValue) {
36     if (dump_mask & eDumpOptionType)
37       strm.PutCString(" =");
38
39     collection::iterator pos, end = m_values.end();
40
41     strm.IndentMore();
42
43     for (pos = m_values.begin(); pos != end; ++pos) {
44       OptionValue *option_value = pos->second.get();
45       strm.EOL();
46       strm.Indent(pos->first.GetCString());
47
48       const uint32_t extra_dump_options = m_raw_value_dump ? eDumpOptionRaw : 0;
49       switch (dict_type) {
50       default:
51       case eTypeArray:
52       case eTypeDictionary:
53       case eTypeProperties:
54       case eTypeFileSpecList:
55       case eTypePathMap:
56         strm.PutChar(' ');
57         option_value->DumpValue(exe_ctx, strm, dump_mask | extra_dump_options);
58         break;
59
60       case eTypeBoolean:
61       case eTypeChar:
62       case eTypeEnum:
63       case eTypeFileSpec:
64       case eTypeFormat:
65       case eTypeSInt64:
66       case eTypeString:
67       case eTypeUInt64:
68       case eTypeUUID:
69         // No need to show the type for dictionaries of simple items
70         strm.PutCString("=");
71         option_value->DumpValue(exe_ctx, strm,
72                                 (dump_mask & (~eDumpOptionType)) |
73                                     extra_dump_options);
74         break;
75       }
76     }
77     strm.IndentLess();
78   }
79 }
80
81 size_t OptionValueDictionary::GetArgs(Args &args) const {
82   args.Clear();
83   collection::const_iterator pos, end = m_values.end();
84   for (pos = m_values.begin(); pos != end; ++pos) {
85     StreamString strm;
86     strm.Printf("%s=", pos->first.GetCString());
87     pos->second->DumpValue(nullptr, strm, eDumpOptionValue | eDumpOptionRaw);
88     args.AppendArgument(strm.GetString());
89   }
90   return args.GetArgumentCount();
91 }
92
93 Status OptionValueDictionary::SetArgs(const Args &args,
94                                       VarSetOperationType op) {
95   Status error;
96   const size_t argc = args.GetArgumentCount();
97   switch (op) {
98   case eVarSetOperationClear:
99     Clear();
100     break;
101
102   case eVarSetOperationAppend:
103   case eVarSetOperationReplace:
104   case eVarSetOperationAssign:
105     if (argc == 0) {
106       error.SetErrorString(
107           "assign operation takes one or more key=value arguments");
108       return error;
109     }
110     for (const auto &entry : args) {
111       if (entry.ref.empty()) {
112         error.SetErrorString("empty argument");
113         return error;
114       }
115       if (!entry.ref.contains('=')) {
116         error.SetErrorString(
117             "assign operation takes one or more key=value arguments");
118         return error;
119       }
120
121       llvm::StringRef key, value;
122       std::tie(key, value) = entry.ref.split('=');
123       bool key_valid = false;
124       if (key.empty()) {
125         error.SetErrorString("empty dictionary key");
126         return error;
127       }
128
129       if (key.front() == '[') {
130         // Key name starts with '[', so the key value must be in single or
131         // double quotes like:
132         // ['<key>']
133         // ["<key>"]
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)) {
140               // Strip the quotes
141               key = key.substr(1, key.size() - 2);
142               key_valid = true;
143             }
144           } else {
145             // square brackets, no quotes
146             key_valid = true;
147           }
148         }
149       } else {
150         // No square brackets or quotes
151         key_valid = true;
152       }
153       if (!key_valid) {
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>\"]",
158             key.str().c_str());
159         return error;
160       }
161
162       lldb::OptionValueSP value_sp(CreateValueFromCStringForTypeMask(
163           value.str().c_str(), m_type_mask, error));
164       if (value_sp) {
165         if (error.Fail())
166           return error;
167         m_value_was_set = true;
168         SetValueForKey(ConstString(key), value_sp, true);
169       } else {
170         error.SetErrorString("dictionaries that can contain multiple types "
171                              "must subclass OptionValueArray");
172       }
173     }
174     break;
175
176   case eVarSetOperationRemove:
177     if (argc > 0) {
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",
183               key.GetCString());
184           break;
185         }
186       }
187     } else {
188       error.SetErrorString("remove operation takes one or more key arguments");
189     }
190     break;
191
192   case eVarSetOperationInsertBefore:
193   case eVarSetOperationInsertAfter:
194   case eVarSetOperationInvalid:
195     error = OptionValue::SetValueFromString(llvm::StringRef(), op);
196     break;
197   }
198   return error;
199 }
200
201 Status OptionValueDictionary::SetValueFromString(llvm::StringRef value,
202                                                  VarSetOperationType op) {
203   Args args(value.str());
204   Status error = SetArgs(args, op);
205   if (error.Success())
206     NotifyValueChanged();
207   return error;
208 }
209
210 lldb::OptionValueSP
211 OptionValueDictionary::GetSubValue(const ExecutionContext *exe_ctx,
212                                    llvm::StringRef name, bool will_modify,
213                                    Status &error) const {
214   lldb::OptionValueSP value_sp;
215   if (name.empty())
216     return nullptr;
217
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());
226     return nullptr;
227   }
228   assert(!temp.empty());
229
230   llvm::StringRef key, value;
231   llvm::StringRef quote_char;
232
233   if (temp[0] == '\"' || temp[0] == '\'') {
234     quote_char = temp.take_front();
235     temp = temp.drop_front();
236   }
237
238   llvm::StringRef sub_name;
239   std::tie(key, sub_name) = temp.split(']');
240
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());
246     return nullptr;
247   }
248
249   value_sp = GetValueForKey(ConstString(key));
250   if (!value_sp) {
251     error.SetErrorStringWithFormat(
252       "dictionary does not contain a value for the key name '%s'",
253       key.str().c_str());
254     return nullptr;
255   }
256
257   if (sub_name.empty())
258     return value_sp;
259   return value_sp->GetSubValue(exe_ctx, sub_name, will_modify, error);
260 }
261
262 Status OptionValueDictionary::SetSubValue(const ExecutionContext *exe_ctx,
263                                           VarSetOperationType op,
264                                           llvm::StringRef name,
265                                           llvm::StringRef value) {
266   Status error;
267   const bool will_modify = true;
268   lldb::OptionValueSP value_sp(GetSubValue(exe_ctx, name, will_modify, error));
269   if (value_sp)
270     error = value_sp->SetValueFromString(value, op);
271   else {
272     if (error.AsCString() == nullptr)
273       error.SetErrorStringWithFormat("invalid value path '%s'", name.str().c_str());
274   }
275   return error;
276 }
277
278 lldb::OptionValueSP
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;
284   return value_sp;
285 }
286
287 bool OptionValueDictionary::SetValueForKey(const ConstString &key,
288                                            const lldb::OptionValueSP &value_sp,
289                                            bool can_replace) {
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())) {
293     if (!can_replace) {
294       collection::const_iterator pos = m_values.find(key);
295       if (pos != m_values.end())
296         return false;
297     }
298     m_values[key] = value_sp;
299     return true;
300   }
301   return false;
302 }
303
304 bool OptionValueDictionary::DeleteValueForKey(const ConstString &key) {
305   collection::iterator pos = m_values.find(key);
306   if (pos != m_values.end()) {
307     m_values.erase(pos);
308     return true;
309   }
310   return false;
311 }
312
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) {
319     StreamString strm;
320     strm.Printf("%s=", pos->first.GetCString());
321     copied_dict->SetValueForKey(pos->first, pos->second->DeepCopy(), true);
322   }
323   return copied_value_sp;
324 }