1 //===-- OptionValueArray.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/OptionValueArray.h"
11
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project includes
16 #include "lldb/Core/Stream.h"
17 #include "lldb/Host/StringConvert.h"
18 #include "lldb/Interpreter/Args.h"
19
20 using namespace lldb;
21 using namespace lldb_private;
22
23 void
DumpValue(const ExecutionContext * exe_ctx,Stream & strm,uint32_t dump_mask)24 OptionValueArray::DumpValue (const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask)
25 {
26 const Type array_element_type = ConvertTypeMaskToType (m_type_mask);
27 if (dump_mask & eDumpOptionType)
28 {
29 if ((GetType() == eTypeArray) && (m_type_mask != eTypeInvalid))
30 strm.Printf ("(%s of %ss)", GetTypeAsCString(), GetBuiltinTypeAsCString(array_element_type));
31 else
32 strm.Printf ("(%s)", GetTypeAsCString());
33 }
34 if (dump_mask & eDumpOptionValue)
35 {
36 if (dump_mask & eDumpOptionType)
37 strm.Printf (" =%s", (m_values.size() > 0) ? "\n" : "");
38 strm.IndentMore();
39 const uint32_t size = m_values.size();
40 for (uint32_t i = 0; i<size; ++i)
41 {
42 strm.Indent();
43 strm.Printf("[%u]: ", i);
44 const uint32_t extra_dump_options = m_raw_value_dump ? eDumpOptionRaw : 0;
45 switch (array_element_type)
46 {
47 default:
48 case eTypeArray:
49 case eTypeDictionary:
50 case eTypeProperties:
51 case eTypeFileSpecList:
52 case eTypePathMap:
53 m_values[i]->DumpValue(exe_ctx, strm, dump_mask | extra_dump_options);
54 break;
55
56 case eTypeBoolean:
57 case eTypeChar:
58 case eTypeEnum:
59 case eTypeFileSpec:
60 case eTypeFormat:
61 case eTypeSInt64:
62 case eTypeString:
63 case eTypeUInt64:
64 case eTypeUUID:
65 // No need to show the type for dictionaries of simple items
66 m_values[i]->DumpValue(exe_ctx, strm, (dump_mask & (~eDumpOptionType)) | extra_dump_options);
67 break;
68 }
69 if (i < (size - 1))
70 strm.EOL();
71 }
72 strm.IndentLess();
73 }
74 }
75
76 Error
SetValueFromString(llvm::StringRef value,VarSetOperationType op)77 OptionValueArray::SetValueFromString (llvm::StringRef value, VarSetOperationType op)
78 {
79 Args args(value.str().c_str());
80 Error error = SetArgs (args, op);
81 if (error.Success())
82 NotifyValueChanged();
83 return error;
84 }
85
86
87 lldb::OptionValueSP
GetSubValue(const ExecutionContext * exe_ctx,const char * name,bool will_modify,Error & error) const88 OptionValueArray::GetSubValue (const ExecutionContext *exe_ctx,
89 const char *name,
90 bool will_modify,
91 Error &error) const
92 {
93 if (name && name[0] == '[')
94 {
95 const char *end_bracket = strchr (name+1, ']');
96 if (end_bracket)
97 {
98 const char *sub_value = nullptr;
99 if (end_bracket[1])
100 sub_value = end_bracket + 1;
101 std::string index_str (name+1, end_bracket);
102 const size_t array_count = m_values.size();
103 int32_t idx = StringConvert::ToSInt32(index_str.c_str(), INT32_MAX, 0, nullptr);
104 if (idx != INT32_MAX)
105 {
106 ;
107 uint32_t new_idx = UINT32_MAX;
108 if (idx < 0)
109 {
110 // Access from the end of the array if the index is negative
111 new_idx = array_count - idx;
112 }
113 else
114 {
115 // Just a standard index
116 new_idx = idx;
117 }
118
119 if (new_idx < array_count)
120 {
121 if (m_values[new_idx])
122 {
123 if (sub_value)
124 return m_values[new_idx]->GetSubValue (exe_ctx, sub_value, will_modify, error);
125 else
126 return m_values[new_idx];
127 }
128 }
129 else
130 {
131 if (array_count == 0)
132 error.SetErrorStringWithFormat("index %i is not valid for an empty array", idx);
133 else if (idx > 0)
134 error.SetErrorStringWithFormat("index %i out of range, valid values are 0 through %" PRIu64, idx, (uint64_t)(array_count - 1));
135 else
136 error.SetErrorStringWithFormat("negative index %i out of range, valid values are -1 through -%" PRIu64, idx, (uint64_t)array_count);
137 }
138 }
139 }
140 }
141 else
142 {
143 error.SetErrorStringWithFormat("invalid value path '%s', %s values only support '[<index>]' subvalues where <index> is a positive or negative array index", name, GetTypeAsCString());
144 }
145 return OptionValueSP();
146 }
147
148
149 size_t
GetArgs(Args & args) const150 OptionValueArray::GetArgs (Args &args) const
151 {
152 const uint32_t size = m_values.size();
153 std::vector<const char *> argv;
154 for (uint32_t i = 0; i<size; ++i)
155 {
156 const char *string_value = m_values[i]->GetStringValue ();
157 if (string_value)
158 argv.push_back(string_value);
159 }
160
161 if (argv.empty())
162 args.Clear();
163 else
164 args.SetArguments(argv.size(), &argv[0]);
165 return args.GetArgumentCount();
166 }
167
168 Error
SetArgs(const Args & args,VarSetOperationType op)169 OptionValueArray::SetArgs (const Args &args, VarSetOperationType op)
170 {
171 Error error;
172 const size_t argc = args.GetArgumentCount();
173 switch (op)
174 {
175 case eVarSetOperationInvalid:
176 error.SetErrorString("unsupported operation");
177 break;
178
179 case eVarSetOperationInsertBefore:
180 case eVarSetOperationInsertAfter:
181 if (argc > 1)
182 {
183 uint32_t idx = StringConvert::ToUInt32(args.GetArgumentAtIndex(0), UINT32_MAX);
184 const uint32_t count = GetSize();
185 if (idx > count)
186 {
187 error.SetErrorStringWithFormat("invalid insert array index %u, index must be 0 through %u", idx, count);
188 }
189 else
190 {
191 if (op == eVarSetOperationInsertAfter)
192 ++idx;
193 for (size_t i=1; i<argc; ++i, ++idx)
194 {
195 lldb::OptionValueSP value_sp (CreateValueFromCStringForTypeMask (args.GetArgumentAtIndex(i),
196 m_type_mask,
197 error));
198 if (value_sp)
199 {
200 if (error.Fail())
201 return error;
202 if (idx >= m_values.size())
203 m_values.push_back(value_sp);
204 else
205 m_values.insert(m_values.begin() + idx, value_sp);
206 }
207 else
208 {
209 error.SetErrorString("array of complex types must subclass OptionValueArray");
210 return error;
211 }
212 }
213 }
214 }
215 else
216 {
217 error.SetErrorString("insert operation takes an array index followed by one or more values");
218 }
219 break;
220
221 case eVarSetOperationRemove:
222 if (argc > 0)
223 {
224 const uint32_t size = m_values.size();
225 std::vector<int> remove_indexes;
226 bool all_indexes_valid = true;
227 size_t i;
228 for (i=0; i<argc; ++i)
229 {
230 const size_t idx =
231 StringConvert::ToSInt32(args.GetArgumentAtIndex(i), INT32_MAX);
232 if (idx >= size)
233 {
234 all_indexes_valid = false;
235 break;
236 }
237 else
238 remove_indexes.push_back(idx);
239 }
240
241 if (all_indexes_valid)
242 {
243 size_t num_remove_indexes = remove_indexes.size();
244 if (num_remove_indexes)
245 {
246 // Sort and then erase in reverse so indexes are always valid
247 if (num_remove_indexes > 1)
248 {
249 std::sort(remove_indexes.begin(), remove_indexes.end());
250 for (std::vector<int>::const_reverse_iterator pos = remove_indexes.rbegin(), end = remove_indexes.rend(); pos != end; ++pos)
251 {
252 m_values.erase(m_values.begin() + *pos);
253 }
254 }
255 else
256 {
257 // Only one index
258 m_values.erase(m_values.begin() + remove_indexes.front());
259 }
260 }
261 }
262 else
263 {
264 error.SetErrorStringWithFormat("invalid array index '%s', aborting remove operation", args.GetArgumentAtIndex(i));
265 }
266 }
267 else
268 {
269 error.SetErrorString("remove operation takes one or more array indices");
270 }
271 break;
272
273 case eVarSetOperationClear:
274 Clear ();
275 break;
276
277 case eVarSetOperationReplace:
278 if (argc > 1)
279 {
280 uint32_t idx = StringConvert::ToUInt32(args.GetArgumentAtIndex(0), UINT32_MAX);
281 const uint32_t count = GetSize();
282 if (idx > count)
283 {
284 error.SetErrorStringWithFormat("invalid replace array index %u, index must be 0 through %u", idx, count);
285 }
286 else
287 {
288 for (size_t i=1; i<argc; ++i, ++idx)
289 {
290 lldb::OptionValueSP value_sp (CreateValueFromCStringForTypeMask (args.GetArgumentAtIndex(i),
291 m_type_mask,
292 error));
293 if (value_sp)
294 {
295 if (error.Fail())
296 return error;
297 if (idx < count)
298 m_values[idx] = value_sp;
299 else
300 m_values.push_back(value_sp);
301 }
302 else
303 {
304 error.SetErrorString("array of complex types must subclass OptionValueArray");
305 return error;
306 }
307 }
308 }
309 }
310 else
311 {
312 error.SetErrorString("replace operation takes an array index followed by one or more values");
313 }
314 break;
315
316 case eVarSetOperationAssign:
317 m_values.clear();
318 // Fall through to append case
319 case eVarSetOperationAppend:
320 for (size_t i=0; i<argc; ++i)
321 {
322 lldb::OptionValueSP value_sp (CreateValueFromCStringForTypeMask (args.GetArgumentAtIndex(i),
323 m_type_mask,
324 error));
325 if (value_sp)
326 {
327 if (error.Fail())
328 return error;
329 m_value_was_set = true;
330 AppendValue(value_sp);
331 }
332 else
333 {
334 error.SetErrorString("array of complex types must subclass OptionValueArray");
335 }
336 }
337 break;
338 }
339 return error;
340 }
341
342 lldb::OptionValueSP
DeepCopy() const343 OptionValueArray::DeepCopy () const
344 {
345 OptionValueArray *copied_array = new OptionValueArray (m_type_mask, m_raw_value_dump);
346 lldb::OptionValueSP copied_value_sp(copied_array);
347 *static_cast<OptionValue *>(copied_array) = *this;
348 copied_array->m_callback = m_callback;
349 const uint32_t size = m_values.size();
350 for (uint32_t i = 0; i<size; ++i)
351 {
352 copied_array->AppendValue (m_values[i]->DeepCopy());
353 }
354 return copied_value_sp;
355 }
356
357
358
359