xref: /freebsd/contrib/llvm-project/lldb/source/Interpreter/OptionValueArray.cpp (revision 700637cbb5e582861067a11aaca4d053546871d2)
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 
9 #include "lldb/Interpreter/OptionValueArray.h"
10 
11 #include "lldb/Utility/Args.h"
12 #include "lldb/Utility/Stream.h"
13 
14 using namespace lldb;
15 using namespace lldb_private;
16 
DumpValue(const ExecutionContext * exe_ctx,Stream & strm,uint32_t dump_mask)17 void OptionValueArray::DumpValue(const ExecutionContext *exe_ctx, Stream &strm,
18                                  uint32_t dump_mask) {
19   const Type array_element_type = ConvertTypeMaskToType(m_type_mask);
20   if (dump_mask & eDumpOptionType) {
21     if ((GetType() == eTypeArray) && (m_type_mask != eTypeInvalid))
22       strm.Printf("(%s of %ss)", GetTypeAsCString(),
23                   GetBuiltinTypeAsCString(array_element_type));
24     else
25       strm.Printf("(%s)", GetTypeAsCString());
26   }
27   if (dump_mask & eDumpOptionValue) {
28     const bool one_line = dump_mask & eDumpOptionCommand;
29     const uint32_t size = m_values.size();
30     if (dump_mask & eDumpOptionType)
31       strm.Printf(" =%s", (m_values.size() > 0 && !one_line) ? "\n" : "");
32     if (!one_line)
33       strm.IndentMore();
34     for (uint32_t i = 0; i < size; ++i) {
35       if (!one_line) {
36         strm.Indent();
37         strm.Printf("[%u]: ", i);
38       }
39       const uint32_t extra_dump_options = m_raw_value_dump ? eDumpOptionRaw : 0;
40       switch (array_element_type) {
41       default:
42       case eTypeArray:
43       case eTypeDictionary:
44       case eTypeProperties:
45       case eTypeFileSpecList:
46       case eTypePathMap:
47         m_values[i]->DumpValue(exe_ctx, strm, dump_mask | extra_dump_options);
48         break;
49 
50       case eTypeBoolean:
51       case eTypeChar:
52       case eTypeEnum:
53       case eTypeFileSpec:
54       case eTypeFileLineColumn:
55       case eTypeFormat:
56       case eTypeSInt64:
57       case eTypeString:
58       case eTypeUInt64:
59       case eTypeUUID:
60         // No need to show the type for dictionaries of simple items
61         m_values[i]->DumpValue(exe_ctx, strm, (dump_mask & (~eDumpOptionType)) |
62                                                   extra_dump_options);
63         break;
64       }
65 
66       if (!one_line) {
67         if (i < (size - 1))
68           strm.EOL();
69       } else {
70         strm << ' ';
71       }
72     }
73     if (!one_line)
74       strm.IndentLess();
75   }
76 }
77 
78 llvm::json::Value
ToJSON(const ExecutionContext * exe_ctx) const79 OptionValueArray::ToJSON(const ExecutionContext *exe_ctx) const {
80   llvm::json::Array json_array;
81   const uint32_t size = m_values.size();
82   for (uint32_t i = 0; i < size; ++i)
83     json_array.emplace_back(m_values[i]->ToJSON(exe_ctx));
84   return json_array;
85 }
86 
SetValueFromString(llvm::StringRef value,VarSetOperationType op)87 Status OptionValueArray::SetValueFromString(llvm::StringRef value,
88                                             VarSetOperationType op) {
89   Args args(value.str());
90   Status error = SetArgs(args, op);
91   if (error.Success())
92     NotifyValueChanged();
93   return error;
94 }
95 
96 lldb::OptionValueSP
GetSubValue(const ExecutionContext * exe_ctx,llvm::StringRef name,Status & error) const97 OptionValueArray::GetSubValue(const ExecutionContext *exe_ctx,
98                               llvm::StringRef name, Status &error) const {
99   if (name.empty() || name.front() != '[') {
100     error = Status::FromErrorStringWithFormat(
101         "invalid value path '%s', %s values only support '[<index>]' subvalues "
102         "where <index> is a positive or negative array index",
103         name.str().c_str(), GetTypeAsCString());
104     return nullptr;
105   }
106 
107   name = name.drop_front();
108   llvm::StringRef index, sub_value;
109   std::tie(index, sub_value) = name.split(']');
110   if (index.size() == name.size()) {
111     // Couldn't find a closing bracket
112     return nullptr;
113   }
114 
115   const size_t array_count = m_values.size();
116   int32_t idx = 0;
117   if (index.getAsInteger(0, idx))
118     return nullptr;
119 
120   uint32_t new_idx = UINT32_MAX;
121   if (idx < 0) {
122     // Access from the end of the array if the index is negative
123     new_idx = array_count - idx;
124   } else {
125     // Just a standard index
126     new_idx = idx;
127   }
128 
129   if (new_idx < array_count) {
130     if (m_values[new_idx]) {
131       if (!sub_value.empty())
132         return m_values[new_idx]->GetSubValue(exe_ctx, sub_value, error);
133       else
134         return m_values[new_idx];
135     }
136   } else {
137     if (array_count == 0)
138       error = Status::FromErrorStringWithFormat(
139           "index %i is not valid for an empty array", idx);
140     else if (idx > 0)
141       error = Status::FromErrorStringWithFormat(
142           "index %i out of range, valid values are 0 through %" PRIu64, idx,
143           (uint64_t)(array_count - 1));
144     else
145       error =
146           Status::FromErrorStringWithFormat("negative index %i out of range, "
147                                             "valid values are -1 through "
148                                             "-%" PRIu64,
149                                             idx, (uint64_t)array_count);
150   }
151   return OptionValueSP();
152 }
153 
GetArgs(Args & args) const154 size_t OptionValueArray::GetArgs(Args &args) const {
155   args.Clear();
156   const uint32_t size = m_values.size();
157   for (uint32_t i = 0; i < size; ++i) {
158     auto string_value = m_values[i]->GetValueAs<llvm::StringRef>();
159     if (string_value)
160       args.AppendArgument(*string_value);
161   }
162 
163   return args.GetArgumentCount();
164 }
165 
SetArgs(const Args & args,VarSetOperationType op)166 Status OptionValueArray::SetArgs(const Args &args, VarSetOperationType op) {
167   Status error;
168   const size_t argc = args.GetArgumentCount();
169   switch (op) {
170   case eVarSetOperationInvalid:
171     error = Status::FromErrorString("unsupported operation");
172     break;
173 
174   case eVarSetOperationInsertBefore:
175   case eVarSetOperationInsertAfter:
176     if (argc > 1) {
177       uint32_t idx;
178       const uint32_t count = GetSize();
179       if (!llvm::to_integer(args.GetArgumentAtIndex(0), idx) || idx > count) {
180         error = Status::FromErrorStringWithFormat(
181             "invalid insert array index %s, index must be 0 through %u",
182             args.GetArgumentAtIndex(0), count);
183       } else {
184         if (op == eVarSetOperationInsertAfter)
185           ++idx;
186         for (size_t i = 1; i < argc; ++i, ++idx) {
187           lldb::OptionValueSP value_sp(CreateValueFromCStringForTypeMask(
188               args.GetArgumentAtIndex(i), m_type_mask, error));
189           if (value_sp) {
190             if (error.Fail())
191               return error;
192             if (idx >= m_values.size())
193               m_values.push_back(value_sp);
194             else
195               m_values.insert(m_values.begin() + idx, value_sp);
196           } else {
197             error = Status::FromErrorString(
198                 "array of complex types must subclass OptionValueArray");
199             return error;
200           }
201         }
202       }
203     } else {
204       error = Status::FromErrorString(
205           "insert operation takes an array index followed by "
206           "one or more values");
207     }
208     break;
209 
210   case eVarSetOperationRemove:
211     if (argc > 0) {
212       const uint32_t size = m_values.size();
213       std::vector<int> remove_indexes;
214       bool all_indexes_valid = true;
215       size_t i;
216       for (i = 0; i < argc; ++i) {
217         size_t idx;
218         if (!llvm::to_integer(args.GetArgumentAtIndex(i), idx) || idx >= size) {
219           all_indexes_valid = false;
220           break;
221         } else
222           remove_indexes.push_back(idx);
223       }
224 
225       if (all_indexes_valid) {
226         size_t num_remove_indexes = remove_indexes.size();
227         if (num_remove_indexes) {
228           // Sort and then erase in reverse so indexes are always valid
229           if (num_remove_indexes > 1) {
230             llvm::sort(remove_indexes);
231             for (std::vector<int>::const_reverse_iterator
232                      pos = remove_indexes.rbegin(),
233                      end = remove_indexes.rend();
234                  pos != end; ++pos) {
235               m_values.erase(m_values.begin() + *pos);
236             }
237           } else {
238             // Only one index
239             m_values.erase(m_values.begin() + remove_indexes.front());
240           }
241         }
242       } else {
243         error = Status::FromErrorStringWithFormat(
244             "invalid array index '%s', aborting remove operation",
245             args.GetArgumentAtIndex(i));
246       }
247     } else {
248       error = Status::FromErrorString(
249           "remove operation takes one or more array indices");
250     }
251     break;
252 
253   case eVarSetOperationClear:
254     Clear();
255     break;
256 
257   case eVarSetOperationReplace:
258     if (argc > 1) {
259       uint32_t idx;
260       const uint32_t count = GetSize();
261       if (!llvm::to_integer(args.GetArgumentAtIndex(0), idx) || idx > count) {
262         error = Status::FromErrorStringWithFormat(
263             "invalid replace array index %s, index must be 0 through %u",
264             args.GetArgumentAtIndex(0), count);
265       } else {
266         for (size_t i = 1; i < argc; ++i, ++idx) {
267           lldb::OptionValueSP value_sp(CreateValueFromCStringForTypeMask(
268               args.GetArgumentAtIndex(i), m_type_mask, error));
269           if (value_sp) {
270             if (error.Fail())
271               return error;
272             if (idx < count)
273               m_values[idx] = value_sp;
274             else
275               m_values.push_back(value_sp);
276           } else {
277             error = Status::FromErrorString(
278                 "array of complex types must subclass OptionValueArray");
279             return error;
280           }
281         }
282       }
283     } else {
284       error = Status::FromErrorString(
285           "replace operation takes an array index followed by "
286           "one or more values");
287     }
288     break;
289 
290   case eVarSetOperationAssign:
291     m_values.clear();
292     // Fall through to append case
293     [[fallthrough]];
294   case eVarSetOperationAppend:
295     for (size_t i = 0; i < argc; ++i) {
296       lldb::OptionValueSP value_sp(CreateValueFromCStringForTypeMask(
297           args.GetArgumentAtIndex(i), m_type_mask, error));
298       if (value_sp) {
299         if (error.Fail())
300           return error;
301         m_value_was_set = true;
302         AppendValue(value_sp);
303       } else {
304         error = Status::FromErrorString(
305             "array of complex types must subclass OptionValueArray");
306       }
307     }
308     break;
309   }
310   return error;
311 }
312 
313 OptionValueSP
DeepCopy(const OptionValueSP & new_parent) const314 OptionValueArray::DeepCopy(const OptionValueSP &new_parent) const {
315   auto copy_sp = OptionValue::DeepCopy(new_parent);
316   // copy_sp->GetAsArray cannot be used here as it doesn't work for derived
317   // types that override GetType returning a different value.
318   auto *array_value_ptr = static_cast<OptionValueArray *>(copy_sp.get());
319   lldbassert(array_value_ptr);
320 
321   for (auto &value : array_value_ptr->m_values)
322     value = value->DeepCopy(copy_sp);
323 
324   return copy_sp;
325 }
326