1 //===-- OptionValuePathMappings.cpp -----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Interpreter/OptionValuePathMappings.h"
12 #include "lldb/Host/FileSystem.h"
13 #include "lldb/Host/StringConvert.h"
14 #include "lldb/Utility/Args.h"
15 #include "lldb/Utility/FileSpec.h"
16 #include "lldb/Utility/Stream.h"
19 using namespace lldb_private;
21 static bool VerifyPathExists(const char *path) {
23 return FileSystem::Instance().Exists(path);
29 void OptionValuePathMappings::DumpValue(const ExecutionContext *exe_ctx,
30 Stream &strm, uint32_t dump_mask) {
31 if (dump_mask & eDumpOptionType)
32 strm.Printf("(%s)", GetTypeAsCString());
33 if (dump_mask & eDumpOptionValue) {
34 if (dump_mask & eDumpOptionType)
35 strm.Printf(" =%s", (m_path_mappings.GetSize() > 0) ? "\n" : "");
36 m_path_mappings.Dump(&strm);
40 Status OptionValuePathMappings::SetValueFromString(llvm::StringRef value,
41 VarSetOperationType op) {
43 Args args(value.str());
44 const size_t argc = args.GetArgumentCount();
47 case eVarSetOperationClear:
52 case eVarSetOperationReplace:
53 // Must be at least one index + 1 pair of paths, and the pair count must be
55 if (argc >= 3 && (((argc - 1) & 1) == 0)) {
57 StringConvert::ToUInt32(args.GetArgumentAtIndex(0), UINT32_MAX);
58 const uint32_t count = m_path_mappings.GetSize();
60 error.SetErrorStringWithFormat(
61 "invalid file list index %u, index must be 0 through %u", idx,
65 for (size_t i = 1; i < argc; i += 2, ++idx) {
66 const char *orginal_path = args.GetArgumentAtIndex(i);
67 const char *replace_path = args.GetArgumentAtIndex(i + 1);
68 if (VerifyPathExists(replace_path)) {
69 ConstString a(orginal_path);
70 ConstString b(replace_path);
71 if (!m_path_mappings.Replace(a, b, idx, m_notify_changes))
72 m_path_mappings.Append(a, b, m_notify_changes);
75 error.SetErrorStringWithFormat(
76 "the replacement path doesn't exist: \"%s\"", replace_path);
84 error.SetErrorString("replace operation takes an array index followed by "
85 "one or more path pairs");
89 case eVarSetOperationAssign:
90 if (argc < 2 || (argc & 1)) {
91 error.SetErrorString("assign operation takes one or more path pairs");
94 m_path_mappings.Clear(m_notify_changes);
95 // Fall through to append case
97 case eVarSetOperationAppend:
98 if (argc < 2 || (argc & 1)) {
99 error.SetErrorString("append operation takes one or more path pairs");
102 bool changed = false;
103 for (size_t i = 0; i < argc; i += 2) {
104 const char *orginal_path = args.GetArgumentAtIndex(i);
105 const char *replace_path = args.GetArgumentAtIndex(i + 1);
106 if (VerifyPathExists(replace_path)) {
107 ConstString a(orginal_path);
108 ConstString b(replace_path);
109 m_path_mappings.Append(a, b, m_notify_changes);
110 m_value_was_set = true;
113 error.SetErrorStringWithFormat(
114 "the replacement path doesn't exist: \"%s\"", replace_path);
119 NotifyValueChanged();
123 case eVarSetOperationInsertBefore:
124 case eVarSetOperationInsertAfter:
125 // Must be at least one index + 1 pair of paths, and the pair count must be
127 if (argc >= 3 && (((argc - 1) & 1) == 0)) {
129 StringConvert::ToUInt32(args.GetArgumentAtIndex(0), UINT32_MAX);
130 const uint32_t count = m_path_mappings.GetSize();
132 error.SetErrorStringWithFormat(
133 "invalid file list index %u, index must be 0 through %u", idx,
136 bool changed = false;
137 if (op == eVarSetOperationInsertAfter)
139 for (size_t i = 1; i < argc; i += 2, ++idx) {
140 const char *orginal_path = args.GetArgumentAtIndex(i);
141 const char *replace_path = args.GetArgumentAtIndex(i + 1);
142 if (VerifyPathExists(replace_path)) {
143 ConstString a(orginal_path);
144 ConstString b(replace_path);
145 m_path_mappings.Insert(a, b, idx, m_notify_changes);
148 error.SetErrorStringWithFormat(
149 "the replacement path doesn't exist: \"%s\"", replace_path);
154 NotifyValueChanged();
157 error.SetErrorString("insert operation takes an array index followed by "
158 "one or more path pairs");
162 case eVarSetOperationRemove:
164 std::vector<int> remove_indexes;
165 bool all_indexes_valid = true;
167 for (i = 0; all_indexes_valid && i < argc; ++i) {
169 StringConvert::ToSInt32(args.GetArgumentAtIndex(i), INT32_MAX);
170 if (idx == INT32_MAX)
171 all_indexes_valid = false;
173 remove_indexes.push_back(idx);
176 if (all_indexes_valid) {
177 size_t num_remove_indexes = remove_indexes.size();
178 if (num_remove_indexes) {
179 // Sort and then erase in reverse so indexes are always valid
180 llvm::sort(remove_indexes.begin(), remove_indexes.end());
181 for (size_t j = num_remove_indexes - 1; j < num_remove_indexes; ++j) {
182 m_path_mappings.Remove(j, m_notify_changes);
185 NotifyValueChanged();
187 error.SetErrorStringWithFormat(
188 "invalid array index '%s', aborting remove operation",
189 args.GetArgumentAtIndex(i));
192 error.SetErrorString("remove operation takes one or more array index");
196 case eVarSetOperationInvalid:
197 error = OptionValue::SetValueFromString(value, op);
203 lldb::OptionValueSP OptionValuePathMappings::DeepCopy() const {
204 return OptionValueSP(new OptionValuePathMappings(*this));