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