LLDB mainline
OptionValuePathMappings.cpp
Go to the documentation of this file.
1//===-- OptionValuePathMappings.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-exception
6//
7//===----------------------------------------------------------------------===//
8
10
13#include "lldb/Utility/Args.h"
15#include "lldb/Utility/Stream.h"
16
17using namespace lldb;
18using namespace lldb_private;
19
20static bool VerifyPathExists(const char *path) {
21 if (path && path[0])
22 return FileSystem::Instance().Exists(path);
23 else
24 return false;
25}
26
28 Stream &strm, uint32_t dump_mask) {
29 if (dump_mask & eDumpOptionType)
30 strm.Printf("(%s)", GetTypeAsCString());
31 if (dump_mask & eDumpOptionValue) {
32 if (dump_mask & (eDumpOptionType | eDumpOptionDefaultValue)) {
33 strm.Printf(" =");
34 if (dump_mask & eDumpOptionDefaultValue && !m_path_mappings.IsEmpty()) {
35 DefaultValueFormat label(strm);
36 strm.PutCString("empty");
37 }
38 if (!m_path_mappings.IsEmpty())
39 strm.PutCString("\n");
40 }
41 m_path_mappings.Dump(&strm);
42 }
43}
44
45llvm::json::Value
47 return m_path_mappings.ToJSON();
48}
49
53 Args args(value.str());
54 const size_t argc = args.GetArgumentCount();
55
56 switch (op) {
58 Clear();
60 break;
61
63 // Must be at least one index + 1 pair of paths, and the pair count must be
64 // even
65 if (argc >= 3 && (((argc - 1) & 1) == 0)) {
66 uint32_t idx;
67 const uint32_t count = m_path_mappings.GetSize();
68 if (!llvm::to_integer(args.GetArgumentAtIndex(0), idx) || idx > count) {
70 "invalid file list index %s, index must be 0 through %u",
71 args.GetArgumentAtIndex(0), count);
72 } else {
73 bool changed = false;
74 for (size_t i = 1; i < argc; idx++, i += 2) {
75 const char *orginal_path = args.GetArgumentAtIndex(i);
76 const char *replace_path = args.GetArgumentAtIndex(i + 1);
77 if (VerifyPathExists(replace_path)) {
78 if (!m_path_mappings.Replace(orginal_path, replace_path, idx,
80 m_path_mappings.Append(orginal_path, replace_path,
82 changed = true;
83 } else {
84 std::string previousError =
85 error.Fail() ? std::string(error.AsCString()) + "\n" : "";
87 "%sthe replacement path doesn't exist: \"%s\"",
88 previousError.c_str(), replace_path);
89 }
90 }
91 if (changed)
93 }
94 } else {
96 "replace operation takes an array index followed by "
97 "one or more path pairs");
98 }
99 break;
100
102 if (argc < 2 || (argc & 1)) {
104 "assign operation takes one or more path pairs");
105 break;
106 }
108 // Fall through to append case
109 [[fallthrough]];
111 if (argc < 2 || (argc & 1)) {
113 "append operation takes one or more path pairs");
114 break;
115 } else {
116 bool changed = false;
117 for (size_t i = 0; i < argc; i += 2) {
118 const char *orginal_path = args.GetArgumentAtIndex(i);
119 const char *replace_path = args.GetArgumentAtIndex(i + 1);
120 if (VerifyPathExists(replace_path)) {
121 m_path_mappings.Append(orginal_path, replace_path, m_notify_changes);
122 m_value_was_set = true;
123 changed = true;
124 } else {
125 std::string previousError =
126 error.Fail() ? std::string(error.AsCString()) + "\n" : "";
128 "%sthe replacement path doesn't exist: \"%s\"",
129 previousError.c_str(), replace_path);
130 }
131 }
132 if (changed)
134 }
135 break;
136
139 // Must be at least one index + 1 pair of paths, and the pair count must be
140 // even
141 if (argc >= 3 && (((argc - 1) & 1) == 0)) {
142 uint32_t idx;
143 const uint32_t count = m_path_mappings.GetSize();
144 if (!llvm::to_integer(args.GetArgumentAtIndex(0), idx) || idx > count) {
146 "invalid file list index %s, index must be 0 through %u",
147 args.GetArgumentAtIndex(0), count);
148 } else {
149 bool changed = false;
151 ++idx;
152 for (size_t i = 1; i < argc; i += 2) {
153 const char *orginal_path = args.GetArgumentAtIndex(i);
154 const char *replace_path = args.GetArgumentAtIndex(i + 1);
155 if (VerifyPathExists(replace_path)) {
156 m_path_mappings.Insert(orginal_path, replace_path, idx,
158 changed = true;
159 idx++;
160 } else {
161 std::string previousError =
162 error.Fail() ? std::string(error.AsCString()) + "\n" : "";
164 "%sthe replacement path doesn't exist: \"%s\"",
165 previousError.c_str(), replace_path);
166 }
167 }
168 if (changed)
170 }
171 } else {
173 "insert operation takes an array index followed by "
174 "one or more path pairs");
175 }
176 break;
177
179 if (argc > 0) {
180 std::vector<int> remove_indexes;
181 for (size_t i = 0; i < argc; ++i) {
182 int idx;
183 if (!llvm::to_integer(args.GetArgumentAtIndex(i), idx) || idx < 0 ||
184 idx >= (int)m_path_mappings.GetSize()) {
186 "invalid array index '%s', aborting remove operation",
187 args.GetArgumentAtIndex(i));
188 break;
189 } else
190 remove_indexes.push_back(idx);
191 }
192
193 // Sort and then erase in reverse so indexes are always valid
194 llvm::sort(remove_indexes);
195 for (auto index : llvm::reverse(remove_indexes))
196 m_path_mappings.Remove(index, m_notify_changes);
198 } else {
200 "remove operation takes one or more array index");
201 }
202 break;
203
206 break;
207 }
208 return error;
209}
static llvm::raw_ostream & error(Stream &strm)
static bool VerifyPathExists(const char *path)
A command line argument class.
Definition Args.h:33
size_t GetArgumentCount() const
Gets the number of arguments left in this command object.
Definition Args.h:120
const char * GetArgumentAtIndex(size_t idx) const
Gets the NULL terminated C string argument pointer for the argument at index idx.
Definition Args.cpp:273
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
bool Exists(const FileSpec &file_spec) const
Returns whether the given file exists.
static FileSystem & Instance()
Status SetValueFromString(llvm::StringRef value, VarSetOperationType op=eVarSetOperationAssign) override
void DumpValue(const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask) override
llvm::json::Value ToJSON(const ExecutionContext *exe_ctx) const override
virtual Status SetValueFromString(llvm::StringRef value, VarSetOperationType op=eVarSetOperationAssign)
virtual const char * GetTypeAsCString() const
Definition OptionValue.h:89
An error handling class.
Definition Status.h:118
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Definition Status.cpp:106
static Status FromErrorString(const char *str)
Definition Status.h:141
A stream class that can stream formatted output to a file.
Definition Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:65
A class that represents a running process on the host machine.
VarSetOperationType
Settable state variable types.