LLDB mainline
OptionValueArray.cpp
Go to the documentation of this file.
1//===-- OptionValueArray.cpp ----------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10
12#include "lldb/Utility/Args.h"
13#include "lldb/Utility/Stream.h"
14
15using namespace lldb;
16using namespace lldb_private;
17
19 uint32_t dump_mask) {
20 const Type array_element_type = ConvertTypeMaskToType(m_type_mask);
21 if (dump_mask & eDumpOptionType) {
22 if ((GetType() == eTypeArray) && (m_type_mask != eTypeInvalid))
23 strm.Printf("(%s of %ss)", GetTypeAsCString(),
24 GetBuiltinTypeAsCString(array_element_type));
25 else
26 strm.Printf("(%s)", GetTypeAsCString());
27 }
28 if (dump_mask & eDumpOptionValue) {
29 const bool one_line = dump_mask & eDumpOptionCommand;
30 const uint32_t size = m_values.size();
31 if (dump_mask & (eDumpOptionType | eDumpOptionDefaultValue)) {
32 strm.PutCString(" =");
33 if (dump_mask & eDumpOptionDefaultValue && !m_values.empty()) {
34 DefaultValueFormat label(strm);
35 strm.PutCString("empty");
36 }
37 if (!m_values.empty() && !one_line)
38 strm.PutCString("\n");
39 }
40 if (!one_line)
41 strm.IndentMore();
42 for (uint32_t i = 0; i < size; ++i) {
43 if (!one_line) {
44 strm.Indent();
45 strm.Printf("[%u]: ", i);
46 }
47 const uint32_t extra_dump_options = m_raw_value_dump ? eDumpOptionRaw : 0;
48 switch (array_element_type) {
49 default:
50 case eTypeArray:
51 case eTypeDictionary:
52 case eTypeProperties:
54 case eTypePathMap:
55 m_values[i]->DumpValue(exe_ctx, strm, dump_mask | extra_dump_options);
56 break;
57
58 case eTypeBoolean:
59 case eTypeChar:
60 case eTypeEnum:
61 case eTypeFileSpec:
63 case eTypeFormat:
64 case eTypeSInt64:
65 case eTypeString:
66 case eTypeUInt64:
67 case eTypeUUID:
68 // No need to show the type for dictionaries of simple items
69 m_values[i]->DumpValue(exe_ctx, strm, (dump_mask & (~eDumpOptionType)) |
70 extra_dump_options);
71 break;
72 }
73
74 if (!one_line) {
75 if (i < (size - 1))
76 strm.EOL();
77 } else {
78 strm << ' ';
79 }
80 }
81 if (!one_line)
82 strm.IndentLess();
83 }
84}
85
86llvm::json::Value
88 llvm::json::Array json_array;
89 const uint32_t size = m_values.size();
90 for (uint32_t i = 0; i < size; ++i)
91 json_array.emplace_back(m_values[i]->ToJSON(exe_ctx));
92 return json_array;
93}
94
97 Args args(value.str());
98 Status error = SetArgs(args, op);
99 if (error.Success())
101 return error;
102}
103
106 llvm::StringRef name, Status &error) const {
107 if (name.empty() || name.front() != '[') {
109 "invalid value path '%s', %s values only support '[<index>]' subvalues "
110 "where <index> is a positive or negative array index",
111 name.str().c_str(), GetTypeAsCString());
112 return nullptr;
113 }
114
115 name = name.drop_front();
116 llvm::StringRef index, sub_value;
117 std::tie(index, sub_value) = name.split(']');
118 if (index.size() == name.size()) {
119 // Couldn't find a closing bracket
120 return nullptr;
121 }
122
123 const size_t array_count = m_values.size();
124 int32_t idx = 0;
125 if (index.getAsInteger(0, idx))
126 return nullptr;
127
128 uint32_t new_idx = UINT32_MAX;
129 if (idx < 0) {
130 // Access from the end of the array if the index is negative
131 new_idx = array_count - idx;
132 } else {
133 // Just a standard index
134 new_idx = idx;
135 }
136
137 if (new_idx < array_count) {
138 if (m_values[new_idx]) {
139 if (!sub_value.empty())
140 return m_values[new_idx]->GetSubValue(exe_ctx, sub_value, error);
141 else
142 return m_values[new_idx];
143 }
144 } else {
145 if (array_count == 0)
147 "index %i is not valid for an empty array", idx);
148 else if (idx > 0)
150 "index %i out of range, valid values are 0 through %" PRIu64, idx,
151 (uint64_t)(array_count - 1));
152 else
153 error =
154 Status::FromErrorStringWithFormat("negative index %i out of range, "
155 "valid values are -1 through "
156 "-%" PRIu64,
157 idx, (uint64_t)array_count);
158 }
159 return OptionValueSP();
160}
161
162size_t OptionValueArray::GetArgs(Args &args) const {
163 args.Clear();
164 const uint32_t size = m_values.size();
165 for (uint32_t i = 0; i < size; ++i) {
166 auto string_value = m_values[i]->GetValueAs<llvm::StringRef>();
167 if (string_value)
168 args.AppendArgument(*string_value);
169 }
170
171 return args.GetArgumentCount();
172}
173
176 const size_t argc = args.GetArgumentCount();
177 switch (op) {
179 error = Status::FromErrorString("unsupported operation");
180 break;
181
184 if (argc > 1) {
185 uint32_t idx;
186 const uint32_t count = GetSize();
187 if (!llvm::to_integer(args.GetArgumentAtIndex(0), idx) || idx > count) {
189 "invalid insert array index %s, index must be 0 through %u",
190 args.GetArgumentAtIndex(0), count);
191 } else {
193 ++idx;
194 for (size_t i = 1; i < argc; ++i, ++idx) {
197 if (value_sp) {
198 if (error.Fail())
199 return error;
200 if (idx >= m_values.size())
201 m_values.push_back(value_sp);
202 else
203 m_values.insert(m_values.begin() + idx, value_sp);
204 } else {
206 "array of complex types must subclass OptionValueArray");
207 return error;
208 }
209 }
210 }
211 } else {
213 "insert operation takes an array index followed by "
214 "one or more values");
215 }
216 break;
217
219 if (argc > 0) {
220 const uint32_t size = m_values.size();
221 std::vector<int> remove_indexes;
222 bool all_indexes_valid = true;
223 size_t i;
224 for (i = 0; i < argc; ++i) {
225 size_t idx;
226 if (!llvm::to_integer(args.GetArgumentAtIndex(i), idx) || idx >= size) {
227 all_indexes_valid = false;
228 break;
229 } else
230 remove_indexes.push_back(idx);
231 }
232
233 if (all_indexes_valid) {
234 size_t num_remove_indexes = remove_indexes.size();
235 if (num_remove_indexes) {
236 // Sort and then erase in reverse so indexes are always valid
237 if (num_remove_indexes > 1) {
238 llvm::sort(remove_indexes);
239 for (std::vector<int>::const_reverse_iterator
240 pos = remove_indexes.rbegin(),
241 end = remove_indexes.rend();
242 pos != end; ++pos) {
243 m_values.erase(m_values.begin() + *pos);
244 }
245 } else {
246 // Only one index
247 m_values.erase(m_values.begin() + remove_indexes.front());
248 }
249 }
250 } else {
252 "invalid array index '%s', aborting remove operation",
253 args.GetArgumentAtIndex(i));
254 }
255 } else {
257 "remove operation takes one or more array indices");
258 }
259 break;
260
262 Clear();
263 break;
264
266 if (argc > 1) {
267 uint32_t idx;
268 const uint32_t count = GetSize();
269 if (!llvm::to_integer(args.GetArgumentAtIndex(0), idx) || idx > count) {
271 "invalid replace array index %s, index must be 0 through %u",
272 args.GetArgumentAtIndex(0), count);
273 } else {
274 for (size_t i = 1; i < argc; ++i, ++idx) {
277 if (value_sp) {
278 if (error.Fail())
279 return error;
280 if (idx < count)
281 m_values[idx] = value_sp;
282 else
283 m_values.push_back(value_sp);
284 } else {
286 "array of complex types must subclass OptionValueArray");
287 return error;
288 }
289 }
290 }
291 } else {
293 "replace operation takes an array index followed by "
294 "one or more values");
295 }
296 break;
297
299 m_values.clear();
300 // Fall through to append case
301 [[fallthrough]];
303 for (size_t i = 0; i < argc; ++i) {
306 if (value_sp) {
307 if (error.Fail())
308 return error;
309 m_value_was_set = true;
310 AppendValue(value_sp);
311 } else {
313 "array of complex types must subclass OptionValueArray");
314 }
315 }
316 break;
317 }
318 return error;
319}
320
323 auto copy_sp = OptionValue::DeepCopy(new_parent);
324 // copy_sp->GetAsArray cannot be used here as it doesn't work for derived
325 // types that override GetType returning a different value.
326 auto *array_value_ptr = static_cast<OptionValueArray *>(copy_sp.get());
327 lldbassert(array_value_ptr);
328
329 for (auto &value : array_value_ptr->m_values)
330 value = value->DeepCopy(copy_sp);
331
332 return copy_sp;
333}
static llvm::raw_ostream & error(Stream &strm)
#define lldbassert(x)
Definition LLDBAssert.h:16
A command line argument class.
Definition Args.h:33
size_t GetArgumentCount() const
Gets the number of arguments left in this command object.
Definition Args.h:120
void AppendArgument(llvm::StringRef arg_str, char quote_char='\0')
Appends a new argument to the end of the list argument list.
Definition Args.cpp:332
const char * GetArgumentAtIndex(size_t idx) const
Gets the NULL terminated C string argument pointer for the argument at index idx.
Definition Args.cpp:273
void Clear()
Clear the arguments.
Definition Args.cpp:388
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
bool AppendValue(const lldb::OptionValueSP &value_sp)
Status SetArgs(const Args &args, VarSetOperationType op)
OptionValue::Type GetType() const override
lldb::OptionValueSP GetSubValue(const ExecutionContext *exe_ctx, llvm::StringRef name, Status &error) const override
void DumpValue(const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask) override
lldb::OptionValueSP DeepCopy(const lldb::OptionValueSP &new_parent) const override
OptionValueArray(uint32_t type_mask=UINT32_MAX, bool raw_value_dump=false)
Status SetValueFromString(llvm::StringRef value, VarSetOperationType op=eVarSetOperationAssign) override
llvm::json::Value ToJSON(const ExecutionContext *exe_ctx) const override
size_t GetArgs(Args &args) const
static lldb::OptionValueSP CreateValueFromCStringForTypeMask(const char *value_cstr, uint32_t type_mask, Status &error)
static OptionValue::Type ConvertTypeMaskToType(uint32_t type_mask)
virtual lldb::OptionValueSP DeepCopy(const lldb::OptionValueSP &new_parent) const
static const char * GetBuiltinTypeAsCString(Type t)
virtual const char * GetTypeAsCString() const
Definition OptionValue.h:89
An error handling class.
Definition Status.h:118
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Definition Status.cpp:106
static Status FromErrorString(const char *str)
Definition Status.h:141
A stream class that can stream formatted output to a file.
Definition Stream.h:28
size_t Indent(llvm::StringRef s="")
Indent the current line in the stream.
Definition Stream.cpp:157
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:65
size_t EOL()
Output and End of Line character to the stream.
Definition Stream.cpp:155
void IndentLess(unsigned amount=2)
Decrement the current indentation level.
Definition Stream.cpp:198
void IndentMore(unsigned amount=2)
Increment the current indentation level.
Definition Stream.cpp:195
#define UINT32_MAX
A class that represents a running process on the host machine.
VarSetOperationType
Settable state variable types.
std::shared_ptr< lldb_private::OptionValue > OptionValueSP