Path: blob/main/contrib/llvm-project/lldb/source/Interpreter/OptionValuePathMappings.cpp
39587 views
//===-- OptionValuePathMappings.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/OptionValuePathMappings.h"910#include "lldb/Host/FileSystem.h"11#include "lldb/Utility/Args.h"12#include "lldb/Utility/FileSpec.h"13#include "lldb/Utility/Stream.h"1415using namespace lldb;16using namespace lldb_private;1718static bool VerifyPathExists(const char *path) {19if (path && path[0])20return FileSystem::Instance().Exists(path);21else22return false;23}2425void OptionValuePathMappings::DumpValue(const ExecutionContext *exe_ctx,26Stream &strm, uint32_t dump_mask) {27if (dump_mask & eDumpOptionType)28strm.Printf("(%s)", GetTypeAsCString());29if (dump_mask & eDumpOptionValue) {30if (dump_mask & eDumpOptionType)31strm.Printf(" =%s", (m_path_mappings.GetSize() > 0) ? "\n" : "");32m_path_mappings.Dump(&strm);33}34}3536llvm::json::Value37OptionValuePathMappings::ToJSON(const ExecutionContext *exe_ctx) {38return m_path_mappings.ToJSON();39}4041Status OptionValuePathMappings::SetValueFromString(llvm::StringRef value,42VarSetOperationType op) {43Status error;44Args args(value.str());45const size_t argc = args.GetArgumentCount();4647switch (op) {48case eVarSetOperationClear:49Clear();50NotifyValueChanged();51break;5253case eVarSetOperationReplace:54// Must be at least one index + 1 pair of paths, and the pair count must be55// even56if (argc >= 3 && (((argc - 1) & 1) == 0)) {57uint32_t idx;58const uint32_t count = m_path_mappings.GetSize();59if (!llvm::to_integer(args.GetArgumentAtIndex(0), idx) || idx > count) {60error.SetErrorStringWithFormat(61"invalid file list index %s, index must be 0 through %u",62args.GetArgumentAtIndex(0), count);63} else {64bool changed = false;65for (size_t i = 1; i < argc; idx++, i += 2) {66const char *orginal_path = args.GetArgumentAtIndex(i);67const char *replace_path = args.GetArgumentAtIndex(i + 1);68if (VerifyPathExists(replace_path)) {69if (!m_path_mappings.Replace(orginal_path, replace_path, idx,70m_notify_changes))71m_path_mappings.Append(orginal_path, replace_path,72m_notify_changes);73changed = true;74} else {75std::string previousError =76error.Fail() ? std::string(error.AsCString()) + "\n" : "";77error.SetErrorStringWithFormat(78"%sthe replacement path doesn't exist: \"%s\"",79previousError.c_str(), replace_path);80}81}82if (changed)83NotifyValueChanged();84}85} else {86error.SetErrorString("replace operation takes an array index followed by "87"one or more path pairs");88}89break;9091case eVarSetOperationAssign:92if (argc < 2 || (argc & 1)) {93error.SetErrorString("assign operation takes one or more path pairs");94break;95}96m_path_mappings.Clear(m_notify_changes);97// Fall through to append case98[[fallthrough]];99case eVarSetOperationAppend:100if (argc < 2 || (argc & 1)) {101error.SetErrorString("append operation takes one or more path pairs");102break;103} else {104bool changed = false;105for (size_t i = 0; i < argc; i += 2) {106const char *orginal_path = args.GetArgumentAtIndex(i);107const char *replace_path = args.GetArgumentAtIndex(i + 1);108if (VerifyPathExists(replace_path)) {109m_path_mappings.Append(orginal_path, replace_path, m_notify_changes);110m_value_was_set = true;111changed = true;112} else {113std::string previousError =114error.Fail() ? std::string(error.AsCString()) + "\n" : "";115error.SetErrorStringWithFormat(116"%sthe replacement path doesn't exist: \"%s\"",117previousError.c_str(), replace_path);118}119}120if (changed)121NotifyValueChanged();122}123break;124125case eVarSetOperationInsertBefore:126case eVarSetOperationInsertAfter:127// Must be at least one index + 1 pair of paths, and the pair count must be128// even129if (argc >= 3 && (((argc - 1) & 1) == 0)) {130uint32_t idx;131const uint32_t count = m_path_mappings.GetSize();132if (!llvm::to_integer(args.GetArgumentAtIndex(0), idx) || idx > count) {133error.SetErrorStringWithFormat(134"invalid file list index %s, index must be 0 through %u",135args.GetArgumentAtIndex(0), count);136} else {137bool changed = false;138if (op == eVarSetOperationInsertAfter)139++idx;140for (size_t i = 1; i < argc; i += 2) {141const char *orginal_path = args.GetArgumentAtIndex(i);142const char *replace_path = args.GetArgumentAtIndex(i + 1);143if (VerifyPathExists(replace_path)) {144m_path_mappings.Insert(orginal_path, replace_path, idx,145m_notify_changes);146changed = true;147idx++;148} else {149std::string previousError =150error.Fail() ? std::string(error.AsCString()) + "\n" : "";151error.SetErrorStringWithFormat(152"%sthe replacement path doesn't exist: \"%s\"",153previousError.c_str(), replace_path);154}155}156if (changed)157NotifyValueChanged();158}159} else {160error.SetErrorString("insert operation takes an array index followed by "161"one or more path pairs");162}163break;164165case eVarSetOperationRemove:166if (argc > 0) {167std::vector<int> remove_indexes;168for (size_t i = 0; i < argc; ++i) {169int idx;170if (!llvm::to_integer(args.GetArgumentAtIndex(i), idx) || idx < 0 ||171idx >= (int)m_path_mappings.GetSize()) {172error.SetErrorStringWithFormat(173"invalid array index '%s', aborting remove operation",174args.GetArgumentAtIndex(i));175break;176} else177remove_indexes.push_back(idx);178}179180// Sort and then erase in reverse so indexes are always valid181llvm::sort(remove_indexes);182for (auto index : llvm::reverse(remove_indexes))183m_path_mappings.Remove(index, m_notify_changes);184NotifyValueChanged();185} else {186error.SetErrorString("remove operation takes one or more array index");187}188break;189190case eVarSetOperationInvalid:191error = OptionValue::SetValueFromString(value, op);192break;193}194return error;195}196197198