186 lines · cpp
1//===-- OptionValueFileSpecList.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/OptionValueFileSpecList.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 OptionValueFileSpecList::DumpValue(const ExecutionContext *exe_ctx,19 Stream &strm, uint32_t dump_mask) {20 std::lock_guard<std::recursive_mutex> lock(m_mutex);21 if (dump_mask & eDumpOptionType)22 strm.Printf("(%s)", GetTypeAsCString());23 if (dump_mask & eDumpOptionValue) {24 const bool one_line = dump_mask & eDumpOptionCommand;25 const uint32_t size = m_current_value.GetSize();26 if (dump_mask & (eDumpOptionType | eDumpOptionDefaultValue)) {27 strm.Printf(" =");28 if (dump_mask & eDumpOptionDefaultValue && !m_current_value.IsEmpty()) {29 DefaultValueFormat label(strm);30 strm.PutCString("empty");31 }32 if (!m_current_value.IsEmpty() && !one_line)33 strm.PutCString("\n");34 }35 if (!one_line)36 strm.IndentMore();37 for (uint32_t i = 0; i < size; ++i) {38 if (!one_line) {39 strm.Indent();40 strm.Printf("[%u]: ", i);41 }42 m_current_value.GetFileSpecAtIndex(i).Dump(strm.AsRawOstream());43 if (one_line)44 strm << ' ';45 }46 if (!one_line)47 strm.IndentLess();48 }49}50 51llvm::json::Value52OptionValueFileSpecList::ToJSON(const ExecutionContext *exe_ctx) const {53 std::lock_guard<std::recursive_mutex> lock(m_mutex);54 llvm::json::Array array;55 for (const auto &file_spec : m_current_value)56 array.emplace_back(file_spec.ToJSON());57 return array;58}59 60Status OptionValueFileSpecList::SetValueFromString(llvm::StringRef value,61 VarSetOperationType op) {62 std::lock_guard<std::recursive_mutex> lock(m_mutex);63 Status error;64 Args args(value.str());65 const size_t argc = args.GetArgumentCount();66 67 switch (op) {68 case eVarSetOperationClear:69 Clear();70 NotifyValueChanged();71 break;72 73 case eVarSetOperationReplace:74 if (argc > 1) {75 uint32_t idx;76 const uint32_t count = m_current_value.GetSize();77 if (!llvm::to_integer(args.GetArgumentAtIndex(0), idx) || idx > count) {78 error = Status::FromErrorStringWithFormat(79 "invalid file list index %s, index must be 0 through %u",80 args.GetArgumentAtIndex(0), count);81 } else {82 for (size_t i = 1; i < argc; ++i, ++idx) {83 FileSpec file(args.GetArgumentAtIndex(i));84 if (idx < count)85 m_current_value.Replace(idx, file);86 else87 m_current_value.Append(file);88 }89 NotifyValueChanged();90 }91 } else {92 error = Status::FromErrorString(93 "replace operation takes an array index followed by "94 "one or more values");95 }96 break;97 98 case eVarSetOperationAssign:99 m_current_value.Clear();100 // Fall through to append case101 [[fallthrough]];102 case eVarSetOperationAppend:103 if (argc > 0) {104 m_value_was_set = true;105 for (size_t i = 0; i < argc; ++i) {106 FileSpec file(args.GetArgumentAtIndex(i));107 m_current_value.Append(file);108 }109 NotifyValueChanged();110 } else {111 error = Status::FromErrorString(112 "assign operation takes at least one file path argument");113 }114 break;115 116 case eVarSetOperationInsertBefore:117 case eVarSetOperationInsertAfter:118 if (argc > 1) {119 uint32_t idx;120 const uint32_t count = m_current_value.GetSize();121 if (!llvm::to_integer(args.GetArgumentAtIndex(0), idx) || idx > count) {122 error = Status::FromErrorStringWithFormat(123 "invalid insert file list index %s, index must be 0 through %u",124 args.GetArgumentAtIndex(0), count);125 } else {126 if (op == eVarSetOperationInsertAfter)127 ++idx;128 for (size_t i = 1; i < argc; ++i, ++idx) {129 FileSpec file(args.GetArgumentAtIndex(i));130 m_current_value.Insert(idx, file);131 }132 NotifyValueChanged();133 }134 } else {135 error = Status::FromErrorString(136 "insert operation takes an array index followed by "137 "one or more values");138 }139 break;140 141 case eVarSetOperationRemove:142 if (argc > 0) {143 std::vector<int> remove_indexes;144 bool all_indexes_valid = true;145 size_t i;146 for (i = 0; all_indexes_valid && i < argc; ++i) {147 int idx;148 if (!llvm::to_integer(args.GetArgumentAtIndex(i), idx))149 all_indexes_valid = false;150 else151 remove_indexes.push_back(idx);152 }153 154 if (all_indexes_valid) {155 size_t num_remove_indexes = remove_indexes.size();156 if (num_remove_indexes) {157 // Sort and then erase in reverse so indexes are always valid158 llvm::sort(remove_indexes);159 for (size_t j = num_remove_indexes - 1; j < num_remove_indexes; ++j) {160 m_current_value.Remove(j);161 }162 }163 NotifyValueChanged();164 } else {165 error = Status::FromErrorStringWithFormat(166 "invalid array index '%s', aborting remove operation",167 args.GetArgumentAtIndex(i));168 }169 } else {170 error = Status::FromErrorString(171 "remove operation takes one or more array index");172 }173 break;174 175 case eVarSetOperationInvalid:176 error = OptionValue::SetValueFromString(value, op);177 break;178 }179 return error;180}181 182OptionValueSP OptionValueFileSpecList::Clone() const {183 std::lock_guard<std::recursive_mutex> lock(m_mutex);184 return Cloneable::Clone();185}186