1 //===-- OptionValueArray.cpp ------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/Interpreter/OptionValueArray.h"
11 
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project includes
16 #include "lldb/Core/Stream.h"
17 #include "lldb/Interpreter/Args.h"
18 
19 using namespace lldb;
20 using namespace lldb_private;
21 
22 void
23 OptionValueArray::DumpValue (const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask)
24 {
25     const Type array_element_type = ConvertTypeMaskToType (m_type_mask);
26     if (dump_mask & eDumpOptionType)
27     {
28         if ((GetType() == eTypeArray) && (m_type_mask != eTypeInvalid))
29             strm.Printf ("(%s of %ss)", GetTypeAsCString(), GetBuiltinTypeAsCString(array_element_type));
30         else
31             strm.Printf ("(%s)", GetTypeAsCString());
32     }
33     if (dump_mask & eDumpOptionValue)
34     {
35         if (dump_mask & eDumpOptionType)
36             strm.Printf (" =%s", (m_values.size() > 0) ? "\n" : "");
37         strm.IndentMore();
38         const uint32_t size = m_values.size();
39         for (uint32_t i = 0; i<size; ++i)
40         {
41             strm.Indent();
42             strm.Printf("[%u]: ", i);
43             const uint32_t extra_dump_options = m_raw_value_dump ? eDumpOptionRaw : 0;
44             switch (array_element_type)
45             {
46                 default:
47                 case eTypeArray:
48                 case eTypeDictionary:
49                 case eTypeProperties:
50                 case eTypeFileSpecList:
51                 case eTypePathMap:
52                     m_values[i]->DumpValue(exe_ctx, strm, dump_mask | extra_dump_options);
53                     break;
54 
55                 case eTypeBoolean:
56                 case eTypeChar:
57                 case eTypeEnum:
58                 case eTypeFileSpec:
59                 case eTypeFormat:
60                 case eTypeSInt64:
61                 case eTypeString:
62                 case eTypeUInt64:
63                 case eTypeUUID:
64                     // No need to show the type for dictionaries of simple items
65                     m_values[i]->DumpValue(exe_ctx, strm, (dump_mask & (~eDumpOptionType)) | extra_dump_options);
66                     break;
67             }
68             if (i < (size - 1))
69                 strm.EOL();
70         }
71         strm.IndentLess();
72     }
73 }
74 
75 Error
76 OptionValueArray::SetValueFromCString (const char *value, VarSetOperationType op)
77 {
78     Args args(value);
79     NotifyValueChanged();
80     return SetArgs (args, op);
81 }
82 
83 
84 lldb::OptionValueSP
85 OptionValueArray::GetSubValue (const ExecutionContext *exe_ctx,
86                                const char *name,
87                                bool will_modify,
88                                Error &error) const
89 {
90     if (name && name[0] == '[')
91     {
92         const char *end_bracket = strchr (name+1, ']');
93         if (end_bracket)
94         {
95             const char *sub_value = nullptr;
96             if (end_bracket[1])
97                 sub_value = end_bracket + 1;
98             std::string index_str (name+1, end_bracket);
99             const size_t array_count = m_values.size();
100             int32_t idx = Args::StringToSInt32(index_str.c_str(), INT32_MAX, 0, nullptr);
101             if (idx != INT32_MAX)
102             {
103                 ;
104                 uint32_t new_idx = UINT32_MAX;
105                 if (idx < 0)
106                 {
107                     // Access from the end of the array if the index is negative
108                     new_idx = array_count - idx;
109                 }
110                 else
111                 {
112                     // Just a standard index
113                     new_idx = idx;
114                 }
115 
116                 if (new_idx < array_count)
117                 {
118                     if (m_values[new_idx])
119                     {
120                         if (sub_value)
121                             return m_values[new_idx]->GetSubValue (exe_ctx, sub_value, will_modify, error);
122                         else
123                             return m_values[new_idx];
124                     }
125                 }
126                 else
127                 {
128                     if (array_count == 0)
129                         error.SetErrorStringWithFormat("index %i is not valid for an empty array", idx);
130                     else if (idx > 0)
131                         error.SetErrorStringWithFormat("index %i out of range, valid values are 0 through %" PRIu64, idx, (uint64_t)(array_count - 1));
132                     else
133                         error.SetErrorStringWithFormat("negative index %i out of range, valid values are -1 through -%" PRIu64, idx, (uint64_t)array_count);
134                 }
135             }
136         }
137     }
138     else
139     {
140         error.SetErrorStringWithFormat("invalid value path '%s', %s values only support '[<index>]' subvalues where <index> is a positive or negative array index", name, GetTypeAsCString());
141     }
142     return OptionValueSP();
143 }
144 
145 
146 size_t
147 OptionValueArray::GetArgs (Args &args) const
148 {
149     const uint32_t size = m_values.size();
150     std::vector<const char *> argv;
151     for (uint32_t i = 0; i<size; ++i)
152     {
153         const char *string_value = m_values[i]->GetStringValue ();
154         if (string_value)
155             argv.push_back(string_value);
156     }
157 
158     if (argv.empty())
159         args.Clear();
160     else
161         args.SetArguments(argv.size(), &argv[0]);
162     return args.GetArgumentCount();
163 }
164 
165 Error
166 OptionValueArray::SetArgs (const Args &args, VarSetOperationType op)
167 {
168     Error error;
169     const size_t argc = args.GetArgumentCount();
170     switch (op)
171     {
172     case eVarSetOperationInvalid:
173         error.SetErrorString("unsupported operation");
174         break;
175 
176     case eVarSetOperationInsertBefore:
177     case eVarSetOperationInsertAfter:
178         if (argc > 1)
179         {
180             uint32_t idx = Args::StringToUInt32(args.GetArgumentAtIndex(0), UINT32_MAX);
181             const uint32_t count = GetSize();
182             if (idx > count)
183             {
184                 error.SetErrorStringWithFormat("invalid insert array index %u, index must be 0 through %u", idx, count);
185             }
186             else
187             {
188                 if (op == eVarSetOperationInsertAfter)
189                     ++idx;
190                 for (size_t i=1; i<argc; ++i, ++idx)
191                 {
192                     lldb::OptionValueSP value_sp (CreateValueFromCStringForTypeMask (args.GetArgumentAtIndex(i),
193                                                                                      m_type_mask,
194                                                                                      error));
195                     if (value_sp)
196                     {
197                         if (error.Fail())
198                             return error;
199                         if (idx >= m_values.size())
200                             m_values.push_back(value_sp);
201                         else
202                             m_values.insert(m_values.begin() + idx, value_sp);
203                     }
204                     else
205                     {
206                         error.SetErrorString("array of complex types must subclass OptionValueArray");
207                         return error;
208                     }
209                 }
210             }
211         }
212         else
213         {
214             error.SetErrorString("insert operation takes an array index followed by one or more values");
215         }
216         break;
217 
218     case eVarSetOperationRemove:
219         if (argc > 0)
220         {
221             const uint32_t size = m_values.size();
222             std::vector<int> remove_indexes;
223             bool all_indexes_valid = true;
224             size_t i;
225             for (i=0; i<argc; ++i)
226             {
227                 const size_t idx =
228                   Args::StringToSInt32(args.GetArgumentAtIndex(i), INT32_MAX);
229                 if (idx >= size)
230                 {
231                     all_indexes_valid = false;
232                     break;
233                 }
234                 else
235                     remove_indexes.push_back(idx);
236             }
237 
238             if (all_indexes_valid)
239             {
240                 size_t num_remove_indexes = remove_indexes.size();
241                 if (num_remove_indexes)
242                 {
243                     // Sort and then erase in reverse so indexes are always valid
244                     if (num_remove_indexes > 1)
245                     {
246                         std::sort(remove_indexes.begin(), remove_indexes.end());
247                         for (std::vector<int>::const_reverse_iterator pos = remove_indexes.rbegin(), end = remove_indexes.rend(); pos != end; ++pos)
248                         {
249                             m_values.erase(m_values.begin() + *pos);
250                         }
251                     }
252                     else
253                     {
254                         // Only one index
255                         m_values.erase(m_values.begin() + remove_indexes.front());
256                     }
257                 }
258             }
259             else
260             {
261                 error.SetErrorStringWithFormat("invalid array index '%s', aborting remove operation", args.GetArgumentAtIndex(i));
262             }
263         }
264         else
265         {
266             error.SetErrorString("remove operation takes one or more array indices");
267         }
268         break;
269 
270     case eVarSetOperationClear:
271         Clear ();
272         break;
273 
274     case eVarSetOperationReplace:
275         if (argc > 1)
276         {
277             uint32_t idx = Args::StringToUInt32(args.GetArgumentAtIndex(0), UINT32_MAX);
278             const uint32_t count = GetSize();
279             if (idx > count)
280             {
281                 error.SetErrorStringWithFormat("invalid replace array index %u, index must be 0 through %u", idx, count);
282             }
283             else
284             {
285                 for (size_t i=1; i<argc; ++i, ++idx)
286                 {
287                     lldb::OptionValueSP value_sp (CreateValueFromCStringForTypeMask (args.GetArgumentAtIndex(i),
288                                                                                      m_type_mask,
289                                                                                      error));
290                     if (value_sp)
291                     {
292                         if (error.Fail())
293                             return error;
294                         if (idx < count)
295                             m_values[idx] = value_sp;
296                         else
297                             m_values.push_back(value_sp);
298                     }
299                     else
300                     {
301                         error.SetErrorString("array of complex types must subclass OptionValueArray");
302                         return error;
303                     }
304                 }
305             }
306         }
307         else
308         {
309             error.SetErrorString("replace operation takes an array index followed by one or more values");
310         }
311         break;
312 
313     case eVarSetOperationAssign:
314         m_values.clear();
315         // Fall through to append case
316     case eVarSetOperationAppend:
317         for (size_t i=0; i<argc; ++i)
318         {
319             lldb::OptionValueSP value_sp (CreateValueFromCStringForTypeMask (args.GetArgumentAtIndex(i),
320                                                                              m_type_mask,
321                                                                              error));
322             if (value_sp)
323             {
324                 if (error.Fail())
325                     return error;
326                 m_value_was_set = true;
327                 AppendValue(value_sp);
328             }
329             else
330             {
331                 error.SetErrorString("array of complex types must subclass OptionValueArray");
332             }
333         }
334         break;
335     }
336     return error;
337 }
338 
339 lldb::OptionValueSP
340 OptionValueArray::DeepCopy () const
341 {
342     OptionValueArray *copied_array = new OptionValueArray (m_type_mask, m_raw_value_dump);
343     lldb::OptionValueSP copied_value_sp(copied_array);
344     const uint32_t size = m_values.size();
345     for (uint32_t i = 0; i<size; ++i)
346     {
347         copied_array->AppendValue (m_values[i]->DeepCopy());
348     }
349     return copied_value_sp;
350 }
351 
352 
353 
354