LLDB mainline
OptionValueFileSpecList.cpp
Go to the documentation of this file.
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
10
12#include "lldb/Utility/Args.h"
13#include "lldb/Utility/Stream.h"
14
15using namespace lldb;
16using namespace lldb_private;
17
19 Stream &strm, uint32_t dump_mask) {
20 std::lock_guard<std::recursive_mutex> lock(m_mutex);
21 if (dump_mask & eDumpOptionType)
22 strm.Printf("(%s)", GetTypeAsCString());
23 if (dump_mask & eDumpOptionValue) {
24 const bool one_line = dump_mask & eDumpOptionCommand;
25 const uint32_t size = m_current_value.GetSize();
26 if (dump_mask & (eDumpOptionType | eDumpOptionDefaultValue)) {
27 strm.Printf(" =");
28 if (dump_mask & eDumpOptionDefaultValue && !m_current_value.IsEmpty()) {
29 DefaultValueFormat label(strm);
30 strm.PutCString("empty");
31 }
32 if (!m_current_value.IsEmpty() && !one_line)
33 strm.PutCString("\n");
34 }
35 if (!one_line)
36 strm.IndentMore();
37 for (uint32_t i = 0; i < size; ++i) {
38 if (!one_line) {
39 strm.Indent();
40 strm.Printf("[%u]: ", i);
41 }
42 m_current_value.GetFileSpecAtIndex(i).Dump(strm.AsRawOstream());
43 if (one_line)
44 strm << ' ';
45 }
46 if (!one_line)
47 strm.IndentLess();
48 }
49}
50
51llvm::json::Value
53 std::lock_guard<std::recursive_mutex> lock(m_mutex);
54 llvm::json::Array array;
55 for (const auto &file_spec : m_current_value)
56 array.emplace_back(file_spec.ToJSON());
57 return array;
58}
59
62 std::lock_guard<std::recursive_mutex> lock(m_mutex);
64 Args args(value.str());
65 const size_t argc = args.GetArgumentCount();
66
67 switch (op) {
69 Clear();
71 break;
72
74 if (argc > 1) {
75 uint32_t idx;
76 const uint32_t count = m_current_value.GetSize();
77 if (!llvm::to_integer(args.GetArgumentAtIndex(0), idx) || idx > count) {
79 "invalid file list index %s, index must be 0 through %u",
80 args.GetArgumentAtIndex(0), count);
81 } else {
82 for (size_t i = 1; i < argc; ++i, ++idx) {
83 FileSpec file(args.GetArgumentAtIndex(i));
84 if (idx < count)
85 m_current_value.Replace(idx, file);
86 else
87 m_current_value.Append(file);
88 }
90 }
91 } else {
93 "replace operation takes an array index followed by "
94 "one or more values");
95 }
96 break;
97
99 m_current_value.Clear();
100 // Fall through to append case
101 [[fallthrough]];
103 if (argc > 0) {
104 m_value_was_set = true;
105 for (size_t i = 0; i < argc; ++i) {
106 FileSpec file(args.GetArgumentAtIndex(i));
107 m_current_value.Append(file);
108 }
110 } else {
112 "assign operation takes at least one file path argument");
113 }
114 break;
115
118 if (argc > 1) {
119 uint32_t idx;
120 const uint32_t count = m_current_value.GetSize();
121 if (!llvm::to_integer(args.GetArgumentAtIndex(0), idx) || idx > count) {
123 "invalid insert file list index %s, index must be 0 through %u",
124 args.GetArgumentAtIndex(0), count);
125 } else {
127 ++idx;
128 for (size_t i = 1; i < argc; ++i, ++idx) {
129 FileSpec file(args.GetArgumentAtIndex(i));
130 m_current_value.Insert(idx, file);
131 }
133 }
134 } else {
136 "insert operation takes an array index followed by "
137 "one or more values");
138 }
139 break;
140
142 if (argc > 0) {
143 std::vector<int> remove_indexes;
144 bool all_indexes_valid = true;
145 size_t i;
146 for (i = 0; all_indexes_valid && i < argc; ++i) {
147 int idx;
148 if (!llvm::to_integer(args.GetArgumentAtIndex(i), idx))
149 all_indexes_valid = false;
150 else
151 remove_indexes.push_back(idx);
152 }
153
154 if (all_indexes_valid) {
155 size_t num_remove_indexes = remove_indexes.size();
156 if (num_remove_indexes) {
157 // Sort and then erase in reverse so indexes are always valid
158 llvm::sort(remove_indexes);
159 for (size_t j = num_remove_indexes - 1; j < num_remove_indexes; ++j) {
160 m_current_value.Remove(j);
161 }
162 }
164 } else {
166 "invalid array index '%s', aborting remove operation",
167 args.GetArgumentAtIndex(i));
168 }
169 } else {
171 "remove operation takes one or more array index");
172 }
173 break;
174
177 break;
178 }
179 return error;
180}
181
183 std::lock_guard<std::recursive_mutex> lock(m_mutex);
184 return Cloneable::Clone();
185}
static llvm::raw_ostream & error(Stream &strm)
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
std::shared_ptr< typename Base::TopmostBase > Clone() const override
Definition Cloneable.h:44
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
A file utility class.
Definition FileSpec.h:57
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
lldb::OptionValueSP Clone() 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
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition Stream.h:400
size_t Indent(llvm::StringRef s="")
Indent the current line in the stream.
Definition Stream.cpp:157
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
void IndentLess(unsigned amount=2)
Decrement the current indentation level.
Definition Stream.cpp:198
void IndentMore(unsigned amount=2)
Increment the current indentation level.
Definition Stream.cpp:195
A class that represents a running process on the host machine.
VarSetOperationType
Settable state variable types.
std::shared_ptr< lldb_private::OptionValue > OptionValueSP