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/Host/StringConvert.h"
17 #include "lldb/Interpreter/Args.h"
18 #include "lldb/Utility/Stream.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 Status OptionValueArray::SetValueFromString(llvm::StringRef value,
74 VarSetOperationType op) {
75 Args args(value.str());
76 Status error = SetArgs(args, op);
83 OptionValueArray::GetSubValue(const ExecutionContext *exe_ctx,
84 llvm::StringRef name, bool will_modify,
85 Status &error) const {
86 if (name.empty() || name.front() != '[') {
87 error.SetErrorStringWithFormat(
88 "invalid value path '%s', %s values only support '[<index>]' subvalues "
89 "where <index> is a positive or negative array index",
90 name.str().c_str(), GetTypeAsCString());
94 name = name.drop_front();
95 llvm::StringRef index, sub_value;
96 std::tie(index, sub_value) = name.split(']');
97 if (index.size() == name.size()) {
98 // Couldn't find a closing bracket
102 const size_t array_count = m_values.size();
104 if (index.getAsInteger(0, idx))
107 uint32_t new_idx = UINT32_MAX;
109 // Access from the end of the array if the index is negative
110 new_idx = array_count - idx;
112 // Just a standard index
116 if (new_idx < array_count) {
117 if (m_values[new_idx]) {
118 if (!sub_value.empty())
119 return m_values[new_idx]->GetSubValue(exe_ctx, sub_value,
122 return m_values[new_idx];
125 if (array_count == 0)
126 error.SetErrorStringWithFormat(
127 "index %i is not valid for an empty array", idx);
129 error.SetErrorStringWithFormat(
130 "index %i out of range, valid values are 0 through %" PRIu64,
131 idx, (uint64_t)(array_count - 1));
133 error.SetErrorStringWithFormat("negative index %i out of range, "
134 "valid values are -1 through "
136 idx, (uint64_t)array_count);
138 return OptionValueSP();
141 size_t OptionValueArray::GetArgs(Args &args) const {
143 const uint32_t size = m_values.size();
144 for (uint32_t i = 0; i < size; ++i) {
145 llvm::StringRef string_value = m_values[i]->GetStringValue();
146 if (!string_value.empty())
147 args.AppendArgument(string_value);
150 return args.GetArgumentCount();
153 Status OptionValueArray::SetArgs(const Args &args, VarSetOperationType op) {
155 const size_t argc = args.GetArgumentCount();
157 case eVarSetOperationInvalid:
158 error.SetErrorString("unsupported operation");
161 case eVarSetOperationInsertBefore:
162 case eVarSetOperationInsertAfter:
165 StringConvert::ToUInt32(args.GetArgumentAtIndex(0), UINT32_MAX);
166 const uint32_t count = GetSize();
168 error.SetErrorStringWithFormat(
169 "invalid insert array index %u, index must be 0 through %u", idx,
172 if (op == eVarSetOperationInsertAfter)
174 for (size_t i = 1; i < argc; ++i, ++idx) {
175 lldb::OptionValueSP value_sp(CreateValueFromCStringForTypeMask(
176 args.GetArgumentAtIndex(i), m_type_mask, error));
180 if (idx >= m_values.size())
181 m_values.push_back(value_sp);
183 m_values.insert(m_values.begin() + idx, value_sp);
185 error.SetErrorString(
186 "array of complex types must subclass OptionValueArray");
192 error.SetErrorString("insert operation takes an array index followed by "
193 "one or more values");
197 case eVarSetOperationRemove:
199 const uint32_t size = m_values.size();
200 std::vector<int> remove_indexes;
201 bool all_indexes_valid = true;
203 for (i = 0; i < argc; ++i) {
205 StringConvert::ToSInt32(args.GetArgumentAtIndex(i), INT32_MAX);
207 all_indexes_valid = false;
210 remove_indexes.push_back(idx);
213 if (all_indexes_valid) {
214 size_t num_remove_indexes = remove_indexes.size();
215 if (num_remove_indexes) {
216 // Sort and then erase in reverse so indexes are always valid
217 if (num_remove_indexes > 1) {
218 std::sort(remove_indexes.begin(), remove_indexes.end());
219 for (std::vector<int>::const_reverse_iterator
220 pos = remove_indexes.rbegin(),
221 end = remove_indexes.rend();
223 m_values.erase(m_values.begin() + *pos);
227 m_values.erase(m_values.begin() + remove_indexes.front());
231 error.SetErrorStringWithFormat(
232 "invalid array index '%s', aborting remove operation",
233 args.GetArgumentAtIndex(i));
236 error.SetErrorString("remove operation takes one or more array indices");
240 case eVarSetOperationClear:
244 case eVarSetOperationReplace:
247 StringConvert::ToUInt32(args.GetArgumentAtIndex(0), UINT32_MAX);
248 const uint32_t count = GetSize();
250 error.SetErrorStringWithFormat(
251 "invalid replace array index %u, index must be 0 through %u", idx,
254 for (size_t i = 1; i < argc; ++i, ++idx) {
255 lldb::OptionValueSP value_sp(CreateValueFromCStringForTypeMask(
256 args.GetArgumentAtIndex(i), m_type_mask, error));
261 m_values[idx] = value_sp;
263 m_values.push_back(value_sp);
265 error.SetErrorString(
266 "array of complex types must subclass OptionValueArray");
272 error.SetErrorString("replace operation takes an array index followed by "
273 "one or more values");
277 case eVarSetOperationAssign:
279 // Fall through to append case
281 case eVarSetOperationAppend:
282 for (size_t i = 0; i < argc; ++i) {
283 lldb::OptionValueSP value_sp(CreateValueFromCStringForTypeMask(
284 args.GetArgumentAtIndex(i), m_type_mask, error));
288 m_value_was_set = true;
289 AppendValue(value_sp);
291 error.SetErrorString(
292 "array of complex types must subclass OptionValueArray");
300 lldb::OptionValueSP OptionValueArray::DeepCopy() const {
301 OptionValueArray *copied_array =
302 new OptionValueArray(m_type_mask, m_raw_value_dump);
303 lldb::OptionValueSP copied_value_sp(copied_array);
304 *static_cast<OptionValue *>(copied_array) = *this;
305 copied_array->m_callback = m_callback;
306 const uint32_t size = m_values.size();
307 for (uint32_t i = 0; i < size; ++i) {
308 copied_array->AppendValue(m_values[i]->DeepCopy());
310 return copied_value_sp;