1 //===-- OptionValueArray.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/OptionValueArray.h"
14 // Other libraries and framework includes
16 #include "lldb/Core/Stream.h"
17 #include "lldb/Host/StringConvert.h"
18 #include "lldb/Interpreter/Args.h"
21 using namespace lldb_private;
23 void OptionValueArray::DumpValue(const ExecutionContext *exe_ctx, Stream &strm,
25 const Type array_element_type = ConvertTypeMaskToType(m_type_mask);
26 if (dump_mask & eDumpOptionType) {
27 if ((GetType() == eTypeArray) && (m_type_mask != eTypeInvalid))
28 strm.Printf("(%s of %ss)", GetTypeAsCString(),
29 GetBuiltinTypeAsCString(array_element_type));
31 strm.Printf("(%s)", GetTypeAsCString());
33 if (dump_mask & eDumpOptionValue) {
34 if (dump_mask & eDumpOptionType)
35 strm.Printf(" =%s", (m_values.size() > 0) ? "\n" : "");
37 const uint32_t size = m_values.size();
38 for (uint32_t i = 0; i < size; ++i) {
40 strm.Printf("[%u]: ", i);
41 const uint32_t extra_dump_options = m_raw_value_dump ? eDumpOptionRaw : 0;
42 switch (array_element_type) {
47 case eTypeFileSpecList:
49 m_values[i]->DumpValue(exe_ctx, strm, dump_mask | extra_dump_options);
61 // No need to show the type for dictionaries of simple items
62 m_values[i]->DumpValue(exe_ctx, strm, (dump_mask & (~eDumpOptionType)) |
73 Error OptionValueArray::SetValueFromString(llvm::StringRef value,
74 VarSetOperationType op) {
75 Args args(value.str());
76 Error error = SetArgs(args, op);
83 OptionValueArray::GetSubValue(const ExecutionContext *exe_ctx, llvm::StringRef name,
84 bool will_modify, Error &error) const {
85 if (name.empty() || name.front() != '[') {
86 error.SetErrorStringWithFormat(
87 "invalid value path '%s', %s values only support '[<index>]' subvalues "
88 "where <index> is a positive or negative array index",
89 name.str().c_str(), GetTypeAsCString());
93 name = name.drop_front();
94 llvm::StringRef index, sub_value;
95 std::tie(index, sub_value) = name.split(']');
96 if (index.size() == name.size()) {
97 // Couldn't find a closing bracket
101 const size_t array_count = m_values.size();
103 if (index.getAsInteger(0, idx))
106 uint32_t new_idx = UINT32_MAX;
108 // Access from the end of the array if the index is negative
109 new_idx = array_count - idx;
111 // Just a standard index
115 if (new_idx < array_count) {
116 if (m_values[new_idx]) {
117 if (!sub_value.empty())
118 return m_values[new_idx]->GetSubValue(exe_ctx, sub_value,
121 return m_values[new_idx];
124 if (array_count == 0)
125 error.SetErrorStringWithFormat(
126 "index %i is not valid for an empty array", idx);
128 error.SetErrorStringWithFormat(
129 "index %i out of range, valid values are 0 through %" PRIu64,
130 idx, (uint64_t)(array_count - 1));
132 error.SetErrorStringWithFormat("negative index %i out of range, "
133 "valid values are -1 through "
135 idx, (uint64_t)array_count);
137 return OptionValueSP();
140 size_t OptionValueArray::GetArgs(Args &args) const {
142 const uint32_t size = m_values.size();
143 for (uint32_t i = 0; i < size; ++i) {
144 llvm::StringRef string_value = m_values[i]->GetStringValue();
145 if (!string_value.empty())
146 args.AppendArgument(string_value);
149 return args.GetArgumentCount();
152 Error OptionValueArray::SetArgs(const Args &args, VarSetOperationType op) {
154 const size_t argc = args.GetArgumentCount();
156 case eVarSetOperationInvalid:
157 error.SetErrorString("unsupported operation");
160 case eVarSetOperationInsertBefore:
161 case eVarSetOperationInsertAfter:
164 StringConvert::ToUInt32(args.GetArgumentAtIndex(0), UINT32_MAX);
165 const uint32_t count = GetSize();
167 error.SetErrorStringWithFormat(
168 "invalid insert array index %u, index must be 0 through %u", idx,
171 if (op == eVarSetOperationInsertAfter)
173 for (size_t i = 1; i < argc; ++i, ++idx) {
174 lldb::OptionValueSP value_sp(CreateValueFromCStringForTypeMask(
175 args.GetArgumentAtIndex(i), m_type_mask, error));
179 if (idx >= m_values.size())
180 m_values.push_back(value_sp);
182 m_values.insert(m_values.begin() + idx, value_sp);
184 error.SetErrorString(
185 "array of complex types must subclass OptionValueArray");
191 error.SetErrorString("insert operation takes an array index followed by "
192 "one or more values");
196 case eVarSetOperationRemove:
198 const uint32_t size = m_values.size();
199 std::vector<int> remove_indexes;
200 bool all_indexes_valid = true;
202 for (i = 0; i < argc; ++i) {
204 StringConvert::ToSInt32(args.GetArgumentAtIndex(i), INT32_MAX);
206 all_indexes_valid = false;
209 remove_indexes.push_back(idx);
212 if (all_indexes_valid) {
213 size_t num_remove_indexes = remove_indexes.size();
214 if (num_remove_indexes) {
215 // Sort and then erase in reverse so indexes are always valid
216 if (num_remove_indexes > 1) {
217 std::sort(remove_indexes.begin(), remove_indexes.end());
218 for (std::vector<int>::const_reverse_iterator
219 pos = remove_indexes.rbegin(),
220 end = remove_indexes.rend();
222 m_values.erase(m_values.begin() + *pos);
226 m_values.erase(m_values.begin() + remove_indexes.front());
230 error.SetErrorStringWithFormat(
231 "invalid array index '%s', aborting remove operation",
232 args.GetArgumentAtIndex(i));
235 error.SetErrorString("remove operation takes one or more array indices");
239 case eVarSetOperationClear:
243 case eVarSetOperationReplace:
246 StringConvert::ToUInt32(args.GetArgumentAtIndex(0), UINT32_MAX);
247 const uint32_t count = GetSize();
249 error.SetErrorStringWithFormat(
250 "invalid replace array index %u, index must be 0 through %u", idx,
253 for (size_t i = 1; i < argc; ++i, ++idx) {
254 lldb::OptionValueSP value_sp(CreateValueFromCStringForTypeMask(
255 args.GetArgumentAtIndex(i), m_type_mask, error));
260 m_values[idx] = value_sp;
262 m_values.push_back(value_sp);
264 error.SetErrorString(
265 "array of complex types must subclass OptionValueArray");
271 error.SetErrorString("replace operation takes an array index followed by "
272 "one or more values");
276 case eVarSetOperationAssign:
278 // Fall through to append case
280 case eVarSetOperationAppend:
281 for (size_t i = 0; i < argc; ++i) {
282 lldb::OptionValueSP value_sp(CreateValueFromCStringForTypeMask(
283 args.GetArgumentAtIndex(i), m_type_mask, error));
287 m_value_was_set = true;
288 AppendValue(value_sp);
290 error.SetErrorString(
291 "array of complex types must subclass OptionValueArray");
299 lldb::OptionValueSP OptionValueArray::DeepCopy() const {
300 OptionValueArray *copied_array =
301 new OptionValueArray(m_type_mask, m_raw_value_dump);
302 lldb::OptionValueSP copied_value_sp(copied_array);
303 *static_cast<OptionValue *>(copied_array) = *this;
304 copied_array->m_callback = m_callback;
305 const uint32_t size = m_values.size();
306 for (uint32_t i = 0; i < size; ++i) {
307 copied_array->AppendValue(m_values[i]->DeepCopy());
309 return copied_value_sp;