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