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