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