Path: blob/main/contrib/llvm-project/lldb/source/Interpreter/OptionValueFileSpecList.cpp
39587 views
//===-- OptionValueFileSpecList.cpp ---------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/Interpreter/OptionValueFileSpecList.h"910#include "lldb/Utility/Args.h"11#include "lldb/Utility/Stream.h"1213using namespace lldb;14using namespace lldb_private;1516void OptionValueFileSpecList::DumpValue(const ExecutionContext *exe_ctx,17Stream &strm, uint32_t dump_mask) {18std::lock_guard<std::recursive_mutex> lock(m_mutex);19if (dump_mask & eDumpOptionType)20strm.Printf("(%s)", GetTypeAsCString());21if (dump_mask & eDumpOptionValue) {22const bool one_line = dump_mask & eDumpOptionCommand;23const uint32_t size = m_current_value.GetSize();24if (dump_mask & eDumpOptionType)25strm.Printf(" =%s",26(m_current_value.GetSize() > 0 && !one_line) ? "\n" : "");27if (!one_line)28strm.IndentMore();29for (uint32_t i = 0; i < size; ++i) {30if (!one_line) {31strm.Indent();32strm.Printf("[%u]: ", i);33}34m_current_value.GetFileSpecAtIndex(i).Dump(strm.AsRawOstream());35if (one_line)36strm << ' ';37}38if (!one_line)39strm.IndentLess();40}41}4243Status OptionValueFileSpecList::SetValueFromString(llvm::StringRef value,44VarSetOperationType op) {45std::lock_guard<std::recursive_mutex> lock(m_mutex);46Status error;47Args args(value.str());48const size_t argc = args.GetArgumentCount();4950switch (op) {51case eVarSetOperationClear:52Clear();53NotifyValueChanged();54break;5556case eVarSetOperationReplace:57if (argc > 1) {58uint32_t idx;59const uint32_t count = m_current_value.GetSize();60if (!llvm::to_integer(args.GetArgumentAtIndex(0), idx) || idx > count) {61error.SetErrorStringWithFormat(62"invalid file list index %s, index must be 0 through %u",63args.GetArgumentAtIndex(0), count);64} else {65for (size_t i = 1; i < argc; ++i, ++idx) {66FileSpec file(args.GetArgumentAtIndex(i));67if (idx < count)68m_current_value.Replace(idx, file);69else70m_current_value.Append(file);71}72NotifyValueChanged();73}74} else {75error.SetErrorString("replace operation takes an array index followed by "76"one or more values");77}78break;7980case eVarSetOperationAssign:81m_current_value.Clear();82// Fall through to append case83[[fallthrough]];84case eVarSetOperationAppend:85if (argc > 0) {86m_value_was_set = true;87for (size_t i = 0; i < argc; ++i) {88FileSpec file(args.GetArgumentAtIndex(i));89m_current_value.Append(file);90}91NotifyValueChanged();92} else {93error.SetErrorString(94"assign operation takes at least one file path argument");95}96break;9798case eVarSetOperationInsertBefore:99case eVarSetOperationInsertAfter:100if (argc > 1) {101uint32_t idx;102const uint32_t count = m_current_value.GetSize();103if (!llvm::to_integer(args.GetArgumentAtIndex(0), idx) || idx > count) {104error.SetErrorStringWithFormat(105"invalid insert file list index %s, index must be 0 through %u",106args.GetArgumentAtIndex(0), count);107} else {108if (op == eVarSetOperationInsertAfter)109++idx;110for (size_t i = 1; i < argc; ++i, ++idx) {111FileSpec file(args.GetArgumentAtIndex(i));112m_current_value.Insert(idx, file);113}114NotifyValueChanged();115}116} else {117error.SetErrorString("insert operation takes an array index followed by "118"one or more values");119}120break;121122case eVarSetOperationRemove:123if (argc > 0) {124std::vector<int> remove_indexes;125bool all_indexes_valid = true;126size_t i;127for (i = 0; all_indexes_valid && i < argc; ++i) {128int idx;129if (!llvm::to_integer(args.GetArgumentAtIndex(i), idx))130all_indexes_valid = false;131else132remove_indexes.push_back(idx);133}134135if (all_indexes_valid) {136size_t num_remove_indexes = remove_indexes.size();137if (num_remove_indexes) {138// Sort and then erase in reverse so indexes are always valid139llvm::sort(remove_indexes);140for (size_t j = num_remove_indexes - 1; j < num_remove_indexes; ++j) {141m_current_value.Remove(j);142}143}144NotifyValueChanged();145} else {146error.SetErrorStringWithFormat(147"invalid array index '%s', aborting remove operation",148args.GetArgumentAtIndex(i));149}150} else {151error.SetErrorString("remove operation takes one or more array index");152}153break;154155case eVarSetOperationInvalid:156error = OptionValue::SetValueFromString(value, op);157break;158}159return error;160}161162OptionValueSP OptionValueFileSpecList::Clone() const {163std::lock_guard<std::recursive_mutex> lock(m_mutex);164return Cloneable::Clone();165}166167168