LLDB  mainline
OptionValueFileSpecLIst.cpp
Go to the documentation of this file.
1 //===-- OptionValueFileSpecLIst.cpp -----------------------------*- C++ -*-===//
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 
15 using namespace lldb;
16 using namespace lldb_private;
17 
19  Stream &strm, uint32_t dump_mask) {
20  if (dump_mask & eDumpOptionType)
21  strm.Printf("(%s)", GetTypeAsCString());
22  if (dump_mask & eDumpOptionValue) {
23  const bool one_line = dump_mask & eDumpOptionCommand;
24  const uint32_t size = m_current_value.GetSize();
25  if (dump_mask & eDumpOptionType)
26  strm.Printf(" =%s",
27  (m_current_value.GetSize() > 0 && !one_line) ? "\n" : "");
28  if (!one_line)
29  strm.IndentMore();
30  for (uint32_t i = 0; i < size; ++i) {
31  if (!one_line) {
32  strm.Indent();
33  strm.Printf("[%u]: ", i);
34  }
35  m_current_value.GetFileSpecAtIndex(i).Dump(&strm);
36  if (one_line)
37  strm << ' ';
38  }
39  if (!one_line)
40  strm.IndentLess();
41  }
42 }
43 
44 Status OptionValueFileSpecList::SetValueFromString(llvm::StringRef value,
46  Status error;
47  Args args(value.str());
48  const size_t argc = args.GetArgumentCount();
49 
50  switch (op) {
52  Clear();
53  NotifyValueChanged();
54  break;
55 
57  if (argc > 1) {
58  uint32_t idx =
59  StringConvert::ToUInt32(args.GetArgumentAtIndex(0), UINT32_MAX);
60  const uint32_t count = m_current_value.GetSize();
61  if (idx > count) {
63  "invalid file list index %u, index must be 0 through %u", idx,
64  count);
65  } else {
66  for (size_t i = 1; i < argc; ++i, ++idx) {
67  FileSpec file(args.GetArgumentAtIndex(i));
68  if (idx < count)
69  m_current_value.Replace(idx, file);
70  else
71  m_current_value.Append(file);
72  }
73  NotifyValueChanged();
74  }
75  } else {
76  error.SetErrorString("replace operation takes an array index followed by "
77  "one or more values");
78  }
79  break;
80 
82  m_current_value.Clear();
83  // Fall through to append case
84  LLVM_FALLTHROUGH;
86  if (argc > 0) {
87  m_value_was_set = true;
88  for (size_t i = 0; i < argc; ++i) {
89  FileSpec file(args.GetArgumentAtIndex(i));
90  m_current_value.Append(file);
91  }
92  NotifyValueChanged();
93  } else {
94  error.SetErrorString(
95  "assign operation takes at least one file path argument");
96  }
97  break;
98 
101  if (argc > 1) {
102  uint32_t idx =
103  StringConvert::ToUInt32(args.GetArgumentAtIndex(0), UINT32_MAX);
104  const uint32_t count = m_current_value.GetSize();
105  if (idx > count) {
107  "invalid insert file list index %u, index must be 0 through %u",
108  idx, count);
109  } else {
110  if (op == eVarSetOperationInsertAfter)
111  ++idx;
112  for (size_t i = 1; i < argc; ++i, ++idx) {
113  FileSpec file(args.GetArgumentAtIndex(i));
114  m_current_value.Insert(idx, file);
115  }
116  NotifyValueChanged();
117  }
118  } else {
119  error.SetErrorString("insert operation takes an array index followed by "
120  "one or more values");
121  }
122  break;
123 
125  if (argc > 0) {
126  std::vector<int> remove_indexes;
127  bool all_indexes_valid = true;
128  size_t i;
129  for (i = 0; all_indexes_valid && i < argc; ++i) {
130  const int idx =
131  StringConvert::ToSInt32(args.GetArgumentAtIndex(i), INT32_MAX);
132  if (idx == INT32_MAX)
133  all_indexes_valid = false;
134  else
135  remove_indexes.push_back(idx);
136  }
137 
138  if (all_indexes_valid) {
139  size_t num_remove_indexes = remove_indexes.size();
140  if (num_remove_indexes) {
141  // Sort and then erase in reverse so indexes are always valid
142  llvm::sort(remove_indexes.begin(), remove_indexes.end());
143  for (size_t j = num_remove_indexes - 1; j < num_remove_indexes; ++j) {
144  m_current_value.Remove(j);
145  }
146  }
147  NotifyValueChanged();
148  } else {
150  "invalid array index '%s', aborting remove operation",
151  args.GetArgumentAtIndex(i));
152  }
153  } else {
154  error.SetErrorString("remove operation takes one or more array index");
155  }
156  break;
157 
159  error = OptionValue::SetValueFromString(value, op);
160  break;
161  }
162  return error;
163 }
164 
165 lldb::OptionValueSP OptionValueFileSpecList::DeepCopy() const {
166  std::lock_guard<std::mutex> lock(m_mutex);
167  return OptionValueSP(new OptionValueFileSpecList(m_current_value));
168 }
A command line argument class.
Definition: Args.h:32
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t GetArgumentCount() const
Gets the number of arguments left in this command object.
Definition: Args.cpp:254
A file utility class.
Definition: FileSpec.h:55
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
int32_t ToSInt32(const char *s, int32_t fail_value=0, int base=0, bool *success_ptr=nullptr)
#define UINT32_MAX
Definition: lldb-defines.h:31
VarSetOperationType
Settable state variable types.
void SetErrorString(llvm::StringRef err_str)
Set the current error string to err_str.
Definition: Status.cpp:241
void IndentLess(int amount=2)
Decrement the current indentation level.
Definition: Stream.cpp:221
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
Definition: SBAddress.h:15
size_t Indent(const char *s=nullptr)
Indent the current line in the stream.
Definition: Stream.cpp:131
uint32_t ToUInt32(const char *s, uint32_t fail_value=0, int base=0, bool *success_ptr=nullptr)
int SetErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Set the current error string to a formatted error string.
Definition: Status.cpp:255
void IndentMore(int amount=2)
Increment the current indentation level.
Definition: Stream.cpp:218
#define INT32_MAX
Definition: lldb-defines.h:27
static bool DumpValue(Stream &s, const SymbolContext *sc, const ExecutionContext *exe_ctx, const FormatEntity::Entry &entry, ValueObject *valobj)
An error handling class.
Definition: Status.h:44