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"
12 #include "lldb/Host/StringConvert.h"
13 #include "lldb/Utility/Args.h"
14 #include "lldb/Utility/Stream.h"
17 using namespace lldb_private;
19 void OptionValueArray::DumpValue(const ExecutionContext *exe_ctx, Stream &strm,
21 const Type array_element_type = ConvertTypeMaskToType(m_type_mask);
22 if (dump_mask & eDumpOptionType) {
23 if ((GetType() == eTypeArray) && (m_type_mask != eTypeInvalid))
24 strm.Printf("(%s of %ss)", GetTypeAsCString(),
25 GetBuiltinTypeAsCString(array_element_type));
27 strm.Printf("(%s)", GetTypeAsCString());
29 if (dump_mask & eDumpOptionValue) {
30 const bool one_line = dump_mask & eDumpOptionCommand;
31 const uint32_t size = m_values.size();
32 if (dump_mask & eDumpOptionType)
33 strm.Printf(" =%s", (m_values.size() > 0 && !one_line) ? "\n" : "");
36 for (uint32_t i = 0; i < size; ++i) {
39 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)) |
79 Status OptionValueArray::SetValueFromString(llvm::StringRef value,
80 VarSetOperationType op) {
81 Args args(value.str());
82 Status error = SetArgs(args, op);
89 OptionValueArray::GetSubValue(const ExecutionContext *exe_ctx,
90 llvm::StringRef name, bool will_modify,
91 Status &error) const {
92 if (name.empty() || name.front() != '[') {
93 error.SetErrorStringWithFormat(
94 "invalid value path '%s', %s values only support '[<index>]' subvalues "
95 "where <index> is a positive or negative array index",
96 name.str().c_str(), GetTypeAsCString());
100 name = name.drop_front();
101 llvm::StringRef index, sub_value;
102 std::tie(index, sub_value) = name.split(']');
103 if (index.size() == name.size()) {
104 // Couldn't find a closing bracket
108 const size_t array_count = m_values.size();
110 if (index.getAsInteger(0, idx))
113 uint32_t new_idx = UINT32_MAX;
115 // Access from the end of the array if the index is negative
116 new_idx = array_count - idx;
118 // Just a standard index
122 if (new_idx < array_count) {
123 if (m_values[new_idx]) {
124 if (!sub_value.empty())
125 return m_values[new_idx]->GetSubValue(exe_ctx, sub_value,
128 return m_values[new_idx];
131 if (array_count == 0)
132 error.SetErrorStringWithFormat(
133 "index %i is not valid for an empty array", idx);
135 error.SetErrorStringWithFormat(
136 "index %i out of range, valid values are 0 through %" PRIu64,
137 idx, (uint64_t)(array_count - 1));
139 error.SetErrorStringWithFormat("negative index %i out of range, "
140 "valid values are -1 through "
142 idx, (uint64_t)array_count);
144 return OptionValueSP();
147 size_t OptionValueArray::GetArgs(Args &args) const {
149 const uint32_t size = m_values.size();
150 for (uint32_t i = 0; i < size; ++i) {
151 llvm::StringRef string_value = m_values[i]->GetStringValue();
152 if (!string_value.empty())
153 args.AppendArgument(string_value);
156 return args.GetArgumentCount();
159 Status OptionValueArray::SetArgs(const Args &args, VarSetOperationType op) {
161 const size_t argc = args.GetArgumentCount();
163 case eVarSetOperationInvalid:
164 error.SetErrorString("unsupported operation");
167 case eVarSetOperationInsertBefore:
168 case eVarSetOperationInsertAfter:
171 StringConvert::ToUInt32(args.GetArgumentAtIndex(0), UINT32_MAX);
172 const uint32_t count = GetSize();
174 error.SetErrorStringWithFormat(
175 "invalid insert array index %u, index must be 0 through %u", idx,
178 if (op == eVarSetOperationInsertAfter)
180 for (size_t i = 1; i < argc; ++i, ++idx) {
181 lldb::OptionValueSP value_sp(CreateValueFromCStringForTypeMask(
182 args.GetArgumentAtIndex(i), m_type_mask, error));
186 if (idx >= m_values.size())
187 m_values.push_back(value_sp);
189 m_values.insert(m_values.begin() + idx, value_sp);
191 error.SetErrorString(
192 "array of complex types must subclass OptionValueArray");
198 error.SetErrorString("insert operation takes an array index followed by "
199 "one or more values");
203 case eVarSetOperationRemove:
205 const uint32_t size = m_values.size();
206 std::vector<int> remove_indexes;
207 bool all_indexes_valid = true;
209 for (i = 0; i < argc; ++i) {
211 StringConvert::ToSInt32(args.GetArgumentAtIndex(i), INT32_MAX);
213 all_indexes_valid = false;
216 remove_indexes.push_back(idx);
219 if (all_indexes_valid) {
220 size_t num_remove_indexes = remove_indexes.size();
221 if (num_remove_indexes) {
222 // Sort and then erase in reverse so indexes are always valid
223 if (num_remove_indexes > 1) {
224 llvm::sort(remove_indexes.begin(), remove_indexes.end());
225 for (std::vector<int>::const_reverse_iterator
226 pos = remove_indexes.rbegin(),
227 end = remove_indexes.rend();
229 m_values.erase(m_values.begin() + *pos);
233 m_values.erase(m_values.begin() + remove_indexes.front());
237 error.SetErrorStringWithFormat(
238 "invalid array index '%s', aborting remove operation",
239 args.GetArgumentAtIndex(i));
242 error.SetErrorString("remove operation takes one or more array indices");
246 case eVarSetOperationClear:
250 case eVarSetOperationReplace:
253 StringConvert::ToUInt32(args.GetArgumentAtIndex(0), UINT32_MAX);
254 const uint32_t count = GetSize();
256 error.SetErrorStringWithFormat(
257 "invalid replace array index %u, index must be 0 through %u", idx,
260 for (size_t i = 1; i < argc; ++i, ++idx) {
261 lldb::OptionValueSP value_sp(CreateValueFromCStringForTypeMask(
262 args.GetArgumentAtIndex(i), m_type_mask, error));
267 m_values[idx] = value_sp;
269 m_values.push_back(value_sp);
271 error.SetErrorString(
272 "array of complex types must subclass OptionValueArray");
278 error.SetErrorString("replace operation takes an array index followed by "
279 "one or more values");
283 case eVarSetOperationAssign:
285 // Fall through to append case
287 case eVarSetOperationAppend:
288 for (size_t i = 0; i < argc; ++i) {
289 lldb::OptionValueSP value_sp(CreateValueFromCStringForTypeMask(
290 args.GetArgumentAtIndex(i), m_type_mask, error));
294 m_value_was_set = true;
295 AppendValue(value_sp);
297 error.SetErrorString(
298 "array of complex types must subclass OptionValueArray");
306 lldb::OptionValueSP OptionValueArray::DeepCopy() const {
307 OptionValueArray *copied_array =
308 new OptionValueArray(m_type_mask, m_raw_value_dump);
309 lldb::OptionValueSP copied_value_sp(copied_array);
310 *static_cast<OptionValue *>(copied_array) = *this;
311 copied_array->m_callback = m_callback;
312 const uint32_t size = m_values.size();
313 for (uint32_t i = 0; i < size; ++i) {
314 copied_array->AppendValue(m_values[i]->DeepCopy());
316 return copied_value_sp;