LLDB  mainline
OptionValueEnumeration.cpp
Go to the documentation of this file.
1 //===-- OptionValueEnumeration.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 
13 using namespace lldb;
14 using namespace lldb_private;
15 
16 OptionValueEnumeration::OptionValueEnumeration(
17  const OptionEnumValues &enumerators, enum_type value)
18  : OptionValue(), m_current_value(value), m_default_value(value),
19  m_enumerations() {
20  SetEnumerations(enumerators);
21 }
22 
24 
26  Stream &strm, uint32_t dump_mask) {
27  if (dump_mask & eDumpOptionType)
28  strm.Printf("(%s)", GetTypeAsCString());
29  if (dump_mask & eDumpOptionValue) {
30  if (dump_mask & eDumpOptionType)
31  strm.PutCString(" = ");
32  const size_t count = m_enumerations.GetSize();
33  for (size_t i = 0; i < count; ++i) {
36  return;
37  }
38  }
39  strm.Printf("%" PRIu64, (uint64_t)m_current_value);
40  }
41 }
42 
45  Status error;
46  switch (op) {
48  Clear();
50  break;
51 
54  ConstString const_enumerator_name(value.trim());
55  const EnumerationMapEntry *enumerator_entry =
56  m_enumerations.FindFirstValueForName(const_enumerator_name);
57  if (enumerator_entry) {
58  m_current_value = enumerator_entry->value.value;
60  } else {
61  StreamString error_strm;
62  error_strm.Printf("invalid enumeration value '%s'", value.str().c_str());
63  const size_t count = m_enumerations.GetSize();
64  if (count) {
65  error_strm.Printf(", valid values are: %s",
67  for (size_t i = 1; i < count; ++i) {
68  error_strm.Printf(", %s",
70  }
71  }
72  error.SetErrorString(error_strm.GetString());
73  }
74  break;
75  }
76 
82  error = OptionValue::SetValueFromString(value, op);
83  break;
84  }
85  return error;
86 }
87 
89  const OptionEnumValues &enumerators) {
91 
92  for (const auto &enumerator : enumerators) {
93  ConstString const_enumerator_name(enumerator.string_value);
94  EnumeratorInfo enumerator_info = {enumerator.value, enumerator.usage};
95  m_enumerations.Append(const_enumerator_name, enumerator_info);
96  }
97 
99 }
100 
101 lldb::OptionValueSP OptionValueEnumeration::DeepCopy() const {
102  return OptionValueSP(new OptionValueEnumeration(*this));
103 }
104 
106  CompletionRequest &request) {
107  request.SetWordComplete(false);
108 
109  const uint32_t num_enumerators = m_enumerations.GetSize();
110  if (!request.GetCursorArgumentPrefix().empty()) {
111  for (size_t i = 0; i < num_enumerators; ++i) {
112  llvm::StringRef name = m_enumerations.GetCStringAtIndex(i).GetStringRef();
113  if (name.startswith(request.GetCursorArgumentPrefix()))
114  request.AddCompletion(name);
115  }
116  } else {
117  // only suggest "true" or "false" by default
118  for (size_t i = 0; i < num_enumerators; ++i)
120  }
121  return request.GetNumberOfMatches();
122 }
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:61
size_t AutoComplete(CommandInterpreter &interpreter, CompletionRequest &request) override
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
void SetEnumerations(const OptionEnumValues &enumerators)
const Entry * FindFirstValueForName(ConstString unique_cstr) const
std::size_t GetNumberOfMatches() const
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
virtual Status SetValueFromString(llvm::StringRef value, VarSetOperationType op=eVarSetOperationAssign)
void AddCompletion(llvm::StringRef completion, llvm::StringRef description="")
Adds a possible completion string.
OptionValueEnumeration(const OptionEnumValues &enumerators, enum_type value)
"lldb/Utility/ArgCompletionRequest.h"
llvm::StringRef GetCursorArgumentPrefix() const
VarSetOperationType
Settable state variable types.
T GetValueAtIndexUnchecked(uint32_t idx) const
void DumpValue(const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask) override
void Append(ConstString unique_cstr, const T &value)
llvm::StringRef GetString() const
void SetErrorString(llvm::StringRef err_str)
Set the current error string to err_str.
Definition: Status.cpp:241
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
Definition: ConstString.h:233
Status SetValueFromString(llvm::StringRef value, VarSetOperationType op=eVarSetOperationAssign) override
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
lldb::OptionValueSP DeepCopy() const override
A uniqued constant string class.
Definition: ConstString.h:38
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:247
virtual const char * GetTypeAsCString() const
Definition: OptionValue.h:79
Definition: SBAddress.h:15
ConstString GetCStringAtIndex(uint32_t idx) const
An error handling class.
Definition: Status.h:44