1cfca06d7SDimitry Andric //===-- OptionValueArray.cpp ----------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
9f034231aSEd Maste #include "lldb/Interpreter/OptionValueArray.h"
10f034231aSEd Maste
11f73363f1SDimitry Andric #include "lldb/Utility/Args.h"
1274a628f7SDimitry Andric #include "lldb/Utility/Stream.h"
13f034231aSEd Maste
14f034231aSEd Maste using namespace lldb;
15f034231aSEd Maste using namespace lldb_private;
16f034231aSEd Maste
DumpValue(const ExecutionContext * exe_ctx,Stream & strm,uint32_t dump_mask)1714f1b3e8SDimitry Andric void OptionValueArray::DumpValue(const ExecutionContext *exe_ctx, Stream &strm,
1814f1b3e8SDimitry Andric uint32_t dump_mask) {
19f034231aSEd Maste const Type array_element_type = ConvertTypeMaskToType(m_type_mask);
2014f1b3e8SDimitry Andric if (dump_mask & eDumpOptionType) {
21f034231aSEd Maste if ((GetType() == eTypeArray) && (m_type_mask != eTypeInvalid))
2214f1b3e8SDimitry Andric strm.Printf("(%s of %ss)", GetTypeAsCString(),
2314f1b3e8SDimitry Andric GetBuiltinTypeAsCString(array_element_type));
24f034231aSEd Maste else
25f034231aSEd Maste strm.Printf("(%s)", GetTypeAsCString());
26f034231aSEd Maste }
2714f1b3e8SDimitry Andric if (dump_mask & eDumpOptionValue) {
2894994d37SDimitry Andric const bool one_line = dump_mask & eDumpOptionCommand;
29f034231aSEd Maste const uint32_t size = m_values.size();
3094994d37SDimitry Andric if (dump_mask & eDumpOptionType)
3194994d37SDimitry Andric strm.Printf(" =%s", (m_values.size() > 0 && !one_line) ? "\n" : "");
3294994d37SDimitry Andric if (!one_line)
3394994d37SDimitry Andric strm.IndentMore();
3414f1b3e8SDimitry Andric for (uint32_t i = 0; i < size; ++i) {
3594994d37SDimitry Andric if (!one_line) {
36f034231aSEd Maste strm.Indent();
37f034231aSEd Maste strm.Printf("[%u]: ", i);
3894994d37SDimitry Andric }
39f034231aSEd Maste const uint32_t extra_dump_options = m_raw_value_dump ? eDumpOptionRaw : 0;
4014f1b3e8SDimitry Andric switch (array_element_type) {
41f034231aSEd Maste default:
42f034231aSEd Maste case eTypeArray:
43f034231aSEd Maste case eTypeDictionary:
44f034231aSEd Maste case eTypeProperties:
45f034231aSEd Maste case eTypeFileSpecList:
46f034231aSEd Maste case eTypePathMap:
47f034231aSEd Maste m_values[i]->DumpValue(exe_ctx, strm, dump_mask | extra_dump_options);
48f034231aSEd Maste break;
49f034231aSEd Maste
50f034231aSEd Maste case eTypeBoolean:
51205afe67SEd Maste case eTypeChar:
52f034231aSEd Maste case eTypeEnum:
53f034231aSEd Maste case eTypeFileSpec:
54b60736ecSDimitry Andric case eTypeFileLineColumn:
55f034231aSEd Maste case eTypeFormat:
56f034231aSEd Maste case eTypeSInt64:
57f034231aSEd Maste case eTypeString:
58f034231aSEd Maste case eTypeUInt64:
59f034231aSEd Maste case eTypeUUID:
60f034231aSEd Maste // No need to show the type for dictionaries of simple items
6114f1b3e8SDimitry Andric m_values[i]->DumpValue(exe_ctx, strm, (dump_mask & (~eDumpOptionType)) |
6214f1b3e8SDimitry Andric extra_dump_options);
63f034231aSEd Maste break;
64f034231aSEd Maste }
6594994d37SDimitry Andric
6694994d37SDimitry Andric if (!one_line) {
67f034231aSEd Maste if (i < (size - 1))
68f034231aSEd Maste strm.EOL();
6994994d37SDimitry Andric } else {
7094994d37SDimitry Andric strm << ' ';
71f034231aSEd Maste }
7294994d37SDimitry Andric }
7394994d37SDimitry Andric if (!one_line)
74f034231aSEd Maste strm.IndentLess();
75f034231aSEd Maste }
76f034231aSEd Maste }
77f034231aSEd Maste
ToJSON(const ExecutionContext * exe_ctx)78e3b55780SDimitry Andric llvm::json::Value OptionValueArray::ToJSON(const ExecutionContext *exe_ctx) {
79e3b55780SDimitry Andric llvm::json::Array json_array;
80e3b55780SDimitry Andric const uint32_t size = m_values.size();
81e3b55780SDimitry Andric for (uint32_t i = 0; i < size; ++i)
82e3b55780SDimitry Andric json_array.emplace_back(m_values[i]->ToJSON(exe_ctx));
83e3b55780SDimitry Andric return json_array;
84e3b55780SDimitry Andric }
85e3b55780SDimitry Andric
SetValueFromString(llvm::StringRef value,VarSetOperationType op)86b76161e4SDimitry Andric Status OptionValueArray::SetValueFromString(llvm::StringRef value,
8714f1b3e8SDimitry Andric VarSetOperationType op) {
8814f1b3e8SDimitry Andric Args args(value.str());
89b76161e4SDimitry Andric Status error = SetArgs(args, op);
905e95aa85SEd Maste if (error.Success())
91205afe67SEd Maste NotifyValueChanged();
925e95aa85SEd Maste return error;
93f034231aSEd Maste }
94f034231aSEd Maste
95f034231aSEd Maste lldb::OptionValueSP
GetSubValue(const ExecutionContext * exe_ctx,llvm::StringRef name,Status & error) const96b76161e4SDimitry Andric OptionValueArray::GetSubValue(const ExecutionContext *exe_ctx,
977fa27ce4SDimitry Andric llvm::StringRef name, Status &error) const {
9814f1b3e8SDimitry Andric if (name.empty() || name.front() != '[') {
9914f1b3e8SDimitry Andric error.SetErrorStringWithFormat(
10014f1b3e8SDimitry Andric "invalid value path '%s', %s values only support '[<index>]' subvalues "
10114f1b3e8SDimitry Andric "where <index> is a positive or negative array index",
10214f1b3e8SDimitry Andric name.str().c_str(), GetTypeAsCString());
10314f1b3e8SDimitry Andric return nullptr;
10414f1b3e8SDimitry Andric }
10514f1b3e8SDimitry Andric
10614f1b3e8SDimitry Andric name = name.drop_front();
10714f1b3e8SDimitry Andric llvm::StringRef index, sub_value;
10814f1b3e8SDimitry Andric std::tie(index, sub_value) = name.split(']');
10914f1b3e8SDimitry Andric if (index.size() == name.size()) {
11014f1b3e8SDimitry Andric // Couldn't find a closing bracket
11114f1b3e8SDimitry Andric return nullptr;
11214f1b3e8SDimitry Andric }
11314f1b3e8SDimitry Andric
114f034231aSEd Maste const size_t array_count = m_values.size();
11514f1b3e8SDimitry Andric int32_t idx = 0;
11614f1b3e8SDimitry Andric if (index.getAsInteger(0, idx))
11714f1b3e8SDimitry Andric return nullptr;
11814f1b3e8SDimitry Andric
119f034231aSEd Maste uint32_t new_idx = UINT32_MAX;
12014f1b3e8SDimitry Andric if (idx < 0) {
121f034231aSEd Maste // Access from the end of the array if the index is negative
122f034231aSEd Maste new_idx = array_count - idx;
12314f1b3e8SDimitry Andric } else {
124f034231aSEd Maste // Just a standard index
125f034231aSEd Maste new_idx = idx;
126f034231aSEd Maste }
127f034231aSEd Maste
12814f1b3e8SDimitry Andric if (new_idx < array_count) {
12914f1b3e8SDimitry Andric if (m_values[new_idx]) {
13014f1b3e8SDimitry Andric if (!sub_value.empty())
1317fa27ce4SDimitry Andric return m_values[new_idx]->GetSubValue(exe_ctx, sub_value, error);
132f034231aSEd Maste else
133f034231aSEd Maste return m_values[new_idx];
134f034231aSEd Maste }
13514f1b3e8SDimitry Andric } else {
136f034231aSEd Maste if (array_count == 0)
13714f1b3e8SDimitry Andric error.SetErrorStringWithFormat(
13814f1b3e8SDimitry Andric "index %i is not valid for an empty array", idx);
139f034231aSEd Maste else if (idx > 0)
14014f1b3e8SDimitry Andric error.SetErrorStringWithFormat(
14114f1b3e8SDimitry Andric "index %i out of range, valid values are 0 through %" PRIu64,
14214f1b3e8SDimitry Andric idx, (uint64_t)(array_count - 1));
143f034231aSEd Maste else
14414f1b3e8SDimitry Andric error.SetErrorStringWithFormat("negative index %i out of range, "
14514f1b3e8SDimitry Andric "valid values are -1 through "
14614f1b3e8SDimitry Andric "-%" PRIu64,
14714f1b3e8SDimitry Andric idx, (uint64_t)array_count);
148f034231aSEd Maste }
149f034231aSEd Maste return OptionValueSP();
150f034231aSEd Maste }
151f034231aSEd Maste
GetArgs(Args & args) const15214f1b3e8SDimitry Andric size_t OptionValueArray::GetArgs(Args &args) const {
15314f1b3e8SDimitry Andric args.Clear();
154f034231aSEd Maste const uint32_t size = m_values.size();
15514f1b3e8SDimitry Andric for (uint32_t i = 0; i < size; ++i) {
1567fa27ce4SDimitry Andric auto string_value = m_values[i]->GetValueAs<llvm::StringRef>();
1577fa27ce4SDimitry Andric if (string_value)
1587fa27ce4SDimitry Andric args.AppendArgument(*string_value);
159f034231aSEd Maste }
160f034231aSEd Maste
161f034231aSEd Maste return args.GetArgumentCount();
162f034231aSEd Maste }
163f034231aSEd Maste
SetArgs(const Args & args,VarSetOperationType op)164b76161e4SDimitry Andric Status OptionValueArray::SetArgs(const Args &args, VarSetOperationType op) {
165b76161e4SDimitry Andric Status error;
166f034231aSEd Maste const size_t argc = args.GetArgumentCount();
16714f1b3e8SDimitry Andric switch (op) {
168f034231aSEd Maste case eVarSetOperationInvalid:
169f034231aSEd Maste error.SetErrorString("unsupported operation");
170f034231aSEd Maste break;
171f034231aSEd Maste
172f034231aSEd Maste case eVarSetOperationInsertBefore:
173f034231aSEd Maste case eVarSetOperationInsertAfter:
17414f1b3e8SDimitry Andric if (argc > 1) {
175c0981da4SDimitry Andric uint32_t idx;
176f034231aSEd Maste const uint32_t count = GetSize();
177c0981da4SDimitry Andric if (!llvm::to_integer(args.GetArgumentAtIndex(0), idx) || idx > count) {
17814f1b3e8SDimitry Andric error.SetErrorStringWithFormat(
179c0981da4SDimitry Andric "invalid insert array index %s, index must be 0 through %u",
180c0981da4SDimitry Andric args.GetArgumentAtIndex(0), count);
18114f1b3e8SDimitry Andric } else {
182f034231aSEd Maste if (op == eVarSetOperationInsertAfter)
183f034231aSEd Maste ++idx;
18414f1b3e8SDimitry Andric for (size_t i = 1; i < argc; ++i, ++idx) {
18514f1b3e8SDimitry Andric lldb::OptionValueSP value_sp(CreateValueFromCStringForTypeMask(
18614f1b3e8SDimitry Andric args.GetArgumentAtIndex(i), m_type_mask, error));
18714f1b3e8SDimitry Andric if (value_sp) {
188f034231aSEd Maste if (error.Fail())
189f034231aSEd Maste return error;
190f034231aSEd Maste if (idx >= m_values.size())
191f034231aSEd Maste m_values.push_back(value_sp);
192f034231aSEd Maste else
193f034231aSEd Maste m_values.insert(m_values.begin() + idx, value_sp);
19414f1b3e8SDimitry Andric } else {
19514f1b3e8SDimitry Andric error.SetErrorString(
19614f1b3e8SDimitry Andric "array of complex types must subclass OptionValueArray");
197f034231aSEd Maste return error;
198f034231aSEd Maste }
199f034231aSEd Maste }
200f034231aSEd Maste }
20114f1b3e8SDimitry Andric } else {
20214f1b3e8SDimitry Andric error.SetErrorString("insert operation takes an array index followed by "
20314f1b3e8SDimitry Andric "one or more values");
204f034231aSEd Maste }
205f034231aSEd Maste break;
206f034231aSEd Maste
207f034231aSEd Maste case eVarSetOperationRemove:
20814f1b3e8SDimitry Andric if (argc > 0) {
209f034231aSEd Maste const uint32_t size = m_values.size();
210f034231aSEd Maste std::vector<int> remove_indexes;
211f034231aSEd Maste bool all_indexes_valid = true;
212f034231aSEd Maste size_t i;
21314f1b3e8SDimitry Andric for (i = 0; i < argc; ++i) {
214c0981da4SDimitry Andric size_t idx;
215c0981da4SDimitry Andric if (!llvm::to_integer(args.GetArgumentAtIndex(i), idx) || idx >= size) {
216f034231aSEd Maste all_indexes_valid = false;
217f034231aSEd Maste break;
21814f1b3e8SDimitry Andric } else
219f034231aSEd Maste remove_indexes.push_back(idx);
220f034231aSEd Maste }
221f034231aSEd Maste
22214f1b3e8SDimitry Andric if (all_indexes_valid) {
223f034231aSEd Maste size_t num_remove_indexes = remove_indexes.size();
22414f1b3e8SDimitry Andric if (num_remove_indexes) {
225f034231aSEd Maste // Sort and then erase in reverse so indexes are always valid
22614f1b3e8SDimitry Andric if (num_remove_indexes > 1) {
2274b4fe385SDimitry Andric llvm::sort(remove_indexes);
22814f1b3e8SDimitry Andric for (std::vector<int>::const_reverse_iterator
22914f1b3e8SDimitry Andric pos = remove_indexes.rbegin(),
23014f1b3e8SDimitry Andric end = remove_indexes.rend();
23114f1b3e8SDimitry Andric pos != end; ++pos) {
232f034231aSEd Maste m_values.erase(m_values.begin() + *pos);
233f034231aSEd Maste }
23414f1b3e8SDimitry Andric } else {
235f034231aSEd Maste // Only one index
236f034231aSEd Maste m_values.erase(m_values.begin() + remove_indexes.front());
237f034231aSEd Maste }
238f034231aSEd Maste }
23914f1b3e8SDimitry Andric } else {
24014f1b3e8SDimitry Andric error.SetErrorStringWithFormat(
24114f1b3e8SDimitry Andric "invalid array index '%s', aborting remove operation",
24214f1b3e8SDimitry Andric args.GetArgumentAtIndex(i));
243f034231aSEd Maste }
24414f1b3e8SDimitry Andric } else {
245f034231aSEd Maste error.SetErrorString("remove operation takes one or more array indices");
246f034231aSEd Maste }
247f034231aSEd Maste break;
248f034231aSEd Maste
249f034231aSEd Maste case eVarSetOperationClear:
250f034231aSEd Maste Clear();
251f034231aSEd Maste break;
252f034231aSEd Maste
253f034231aSEd Maste case eVarSetOperationReplace:
25414f1b3e8SDimitry Andric if (argc > 1) {
255c0981da4SDimitry Andric uint32_t idx;
256f034231aSEd Maste const uint32_t count = GetSize();
257c0981da4SDimitry Andric if (!llvm::to_integer(args.GetArgumentAtIndex(0), idx) || idx > count) {
25814f1b3e8SDimitry Andric error.SetErrorStringWithFormat(
259c0981da4SDimitry Andric "invalid replace array index %s, index must be 0 through %u",
260c0981da4SDimitry Andric args.GetArgumentAtIndex(0), count);
26114f1b3e8SDimitry Andric } else {
26214f1b3e8SDimitry Andric for (size_t i = 1; i < argc; ++i, ++idx) {
26314f1b3e8SDimitry Andric lldb::OptionValueSP value_sp(CreateValueFromCStringForTypeMask(
26414f1b3e8SDimitry Andric args.GetArgumentAtIndex(i), m_type_mask, error));
26514f1b3e8SDimitry Andric if (value_sp) {
266f034231aSEd Maste if (error.Fail())
267f034231aSEd Maste return error;
268f034231aSEd Maste if (idx < count)
269f034231aSEd Maste m_values[idx] = value_sp;
270f034231aSEd Maste else
271f034231aSEd Maste m_values.push_back(value_sp);
27214f1b3e8SDimitry Andric } else {
27314f1b3e8SDimitry Andric error.SetErrorString(
27414f1b3e8SDimitry Andric "array of complex types must subclass OptionValueArray");
275f034231aSEd Maste return error;
276f034231aSEd Maste }
277f034231aSEd Maste }
278f034231aSEd Maste }
27914f1b3e8SDimitry Andric } else {
28014f1b3e8SDimitry Andric error.SetErrorString("replace operation takes an array index followed by "
28114f1b3e8SDimitry Andric "one or more values");
282f034231aSEd Maste }
283f034231aSEd Maste break;
284f034231aSEd Maste
285f034231aSEd Maste case eVarSetOperationAssign:
286f034231aSEd Maste m_values.clear();
287f034231aSEd Maste // Fall through to append case
288e3b55780SDimitry Andric [[fallthrough]];
289f034231aSEd Maste case eVarSetOperationAppend:
29014f1b3e8SDimitry Andric for (size_t i = 0; i < argc; ++i) {
29114f1b3e8SDimitry Andric lldb::OptionValueSP value_sp(CreateValueFromCStringForTypeMask(
29214f1b3e8SDimitry Andric args.GetArgumentAtIndex(i), m_type_mask, error));
29314f1b3e8SDimitry Andric if (value_sp) {
294f034231aSEd Maste if (error.Fail())
295f034231aSEd Maste return error;
296f034231aSEd Maste m_value_was_set = true;
297f034231aSEd Maste AppendValue(value_sp);
29814f1b3e8SDimitry Andric } else {
29914f1b3e8SDimitry Andric error.SetErrorString(
30014f1b3e8SDimitry Andric "array of complex types must subclass OptionValueArray");
301f034231aSEd Maste }
302f034231aSEd Maste }
303f034231aSEd Maste break;
304f034231aSEd Maste }
305f034231aSEd Maste return error;
306f034231aSEd Maste }
307f034231aSEd Maste
308344a3780SDimitry Andric OptionValueSP
DeepCopy(const OptionValueSP & new_parent) const309344a3780SDimitry Andric OptionValueArray::DeepCopy(const OptionValueSP &new_parent) const {
310344a3780SDimitry Andric auto copy_sp = OptionValue::DeepCopy(new_parent);
311344a3780SDimitry Andric // copy_sp->GetAsArray cannot be used here as it doesn't work for derived
312344a3780SDimitry Andric // types that override GetType returning a different value.
313344a3780SDimitry Andric auto *array_value_ptr = static_cast<OptionValueArray *>(copy_sp.get());
314344a3780SDimitry Andric lldbassert(array_value_ptr);
315344a3780SDimitry Andric
316344a3780SDimitry Andric for (auto &value : array_value_ptr->m_values)
317344a3780SDimitry Andric value = value->DeepCopy(copy_sp);
318344a3780SDimitry Andric
319344a3780SDimitry Andric return copy_sp;
320f034231aSEd Maste }
321