llvm-project
166 строк · 5.2 Кб
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-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/Interpreter/OptionValueFileSpecList.h"
10
11#include "lldb/Utility/Args.h"
12#include "lldb/Utility/Stream.h"
13
14using namespace lldb;
15using namespace lldb_private;
16
17void OptionValueFileSpecList::DumpValue(const ExecutionContext *exe_ctx,
18Stream &strm, uint32_t dump_mask) {
19std::lock_guard<std::recursive_mutex> lock(m_mutex);
20if (dump_mask & eDumpOptionType)
21strm.Printf("(%s)", GetTypeAsCString());
22if (dump_mask & eDumpOptionValue) {
23const bool one_line = dump_mask & eDumpOptionCommand;
24const uint32_t size = m_current_value.GetSize();
25if (dump_mask & eDumpOptionType)
26strm.Printf(" =%s",
27(m_current_value.GetSize() > 0 && !one_line) ? "\n" : "");
28if (!one_line)
29strm.IndentMore();
30for (uint32_t i = 0; i < size; ++i) {
31if (!one_line) {
32strm.Indent();
33strm.Printf("[%u]: ", i);
34}
35m_current_value.GetFileSpecAtIndex(i).Dump(strm.AsRawOstream());
36if (one_line)
37strm << ' ';
38}
39if (!one_line)
40strm.IndentLess();
41}
42}
43
44Status OptionValueFileSpecList::SetValueFromString(llvm::StringRef value,
45VarSetOperationType op) {
46std::lock_guard<std::recursive_mutex> lock(m_mutex);
47Status error;
48Args args(value.str());
49const size_t argc = args.GetArgumentCount();
50
51switch (op) {
52case eVarSetOperationClear:
53Clear();
54NotifyValueChanged();
55break;
56
57case eVarSetOperationReplace:
58if (argc > 1) {
59uint32_t idx;
60const uint32_t count = m_current_value.GetSize();
61if (!llvm::to_integer(args.GetArgumentAtIndex(0), idx) || idx > count) {
62error.SetErrorStringWithFormat(
63"invalid file list index %s, index must be 0 through %u",
64args.GetArgumentAtIndex(0), count);
65} else {
66for (size_t i = 1; i < argc; ++i, ++idx) {
67FileSpec file(args.GetArgumentAtIndex(i));
68if (idx < count)
69m_current_value.Replace(idx, file);
70else
71m_current_value.Append(file);
72}
73NotifyValueChanged();
74}
75} else {
76error.SetErrorString("replace operation takes an array index followed by "
77"one or more values");
78}
79break;
80
81case eVarSetOperationAssign:
82m_current_value.Clear();
83// Fall through to append case
84[[fallthrough]];
85case eVarSetOperationAppend:
86if (argc > 0) {
87m_value_was_set = true;
88for (size_t i = 0; i < argc; ++i) {
89FileSpec file(args.GetArgumentAtIndex(i));
90m_current_value.Append(file);
91}
92NotifyValueChanged();
93} else {
94error.SetErrorString(
95"assign operation takes at least one file path argument");
96}
97break;
98
99case eVarSetOperationInsertBefore:
100case eVarSetOperationInsertAfter:
101if (argc > 1) {
102uint32_t idx;
103const uint32_t count = m_current_value.GetSize();
104if (!llvm::to_integer(args.GetArgumentAtIndex(0), idx) || idx > count) {
105error.SetErrorStringWithFormat(
106"invalid insert file list index %s, index must be 0 through %u",
107args.GetArgumentAtIndex(0), count);
108} else {
109if (op == eVarSetOperationInsertAfter)
110++idx;
111for (size_t i = 1; i < argc; ++i, ++idx) {
112FileSpec file(args.GetArgumentAtIndex(i));
113m_current_value.Insert(idx, file);
114}
115NotifyValueChanged();
116}
117} else {
118error.SetErrorString("insert operation takes an array index followed by "
119"one or more values");
120}
121break;
122
123case eVarSetOperationRemove:
124if (argc > 0) {
125std::vector<int> remove_indexes;
126bool all_indexes_valid = true;
127size_t i;
128for (i = 0; all_indexes_valid && i < argc; ++i) {
129int idx;
130if (!llvm::to_integer(args.GetArgumentAtIndex(i), idx))
131all_indexes_valid = false;
132else
133remove_indexes.push_back(idx);
134}
135
136if (all_indexes_valid) {
137size_t num_remove_indexes = remove_indexes.size();
138if (num_remove_indexes) {
139// Sort and then erase in reverse so indexes are always valid
140llvm::sort(remove_indexes);
141for (size_t j = num_remove_indexes - 1; j < num_remove_indexes; ++j) {
142m_current_value.Remove(j);
143}
144}
145NotifyValueChanged();
146} else {
147error.SetErrorStringWithFormat(
148"invalid array index '%s', aborting remove operation",
149args.GetArgumentAtIndex(i));
150}
151} else {
152error.SetErrorString("remove operation takes one or more array index");
153}
154break;
155
156case eVarSetOperationInvalid:
157error = OptionValue::SetValueFromString(value, op);
158break;
159}
160return error;
161}
162
163OptionValueSP OptionValueFileSpecList::Clone() const {
164std::lock_guard<std::recursive_mutex> lock(m_mutex);
165return Cloneable::Clone();
166}
167