LLDB mainline
OptionValueEnumeration.cpp
Go to the documentation of this file.
1//===-- OptionValueEnumeration.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
13
14using namespace lldb;
15using namespace lldb_private;
16
18 const OptionEnumValues &enumerators, enum_type value)
19 : m_current_value(value), m_default_value(value) {
20 SetEnumerations(enumerators);
21}
22
24 const size_t count = m_enumerations.GetSize();
25 for (size_t i = 0; i < count; ++i)
26 if (m_enumerations.GetValueAtIndexUnchecked(i).value == value) {
27 strm.PutCString(m_enumerations.GetCStringAtIndex(i));
28 return;
29 }
30
31 strm.Printf("%" PRIu64, (uint64_t)value);
32}
33
35 Stream &strm, uint32_t dump_mask) {
36 if (dump_mask & eDumpOptionType)
37 strm.Printf("(%s)", GetTypeAsCString());
38 if (dump_mask & eDumpOptionValue) {
39 if (dump_mask & eDumpOptionType)
40 strm.PutCString(" = ");
42 if (dump_mask & eDumpOptionDefaultValue &&
44 DefaultValueFormat label(strm);
46 }
47 }
48}
49
50llvm::json::Value
52 for (const auto &enums : m_enumerations) {
53 if (enums.value.value == m_current_value)
54 return enums.cstring.GetStringRef();
55 }
56
57 return std::to_string(static_cast<uint64_t>(m_current_value));
58}
59
63 switch (op) {
65 Clear();
67 break;
68
71 ConstString const_enumerator_name(value.trim());
72 const EnumerationMapEntry *enumerator_entry =
73 m_enumerations.FindFirstValueForName(const_enumerator_name);
74 if (enumerator_entry) {
75 m_current_value = enumerator_entry->value.value;
77 } else {
78 StreamString error_strm;
79 error_strm.Printf("invalid enumeration value '%s'", value.str().c_str());
80 const size_t count = m_enumerations.GetSize();
81 if (count) {
82 error_strm.Printf(", valid values are: %s",
83 m_enumerations.GetCStringAtIndex(0).GetCString());
84 for (size_t i = 1; i < count; ++i) {
85 error_strm.Printf(", %s",
86 m_enumerations.GetCStringAtIndex(i).GetCString());
87 }
88 }
89 error = Status(error_strm.GetString().str());
90 }
91 break;
92 }
93
100 break;
101 }
102 return error;
103}
104
106 const OptionEnumValues &enumerators) {
107 m_enumerations.Clear();
108
109 for (const auto &enumerator : enumerators) {
110 ConstString const_enumerator_name(enumerator.string_value);
111 EnumeratorInfo enumerator_info = {enumerator.value, enumerator.usage};
112 m_enumerations.Append(const_enumerator_name, enumerator_info);
113 }
114
115 m_enumerations.Sort();
116}
117
119 CompletionRequest &request) {
120 const uint32_t num_enumerators = m_enumerations.GetSize();
121 if (!request.GetCursorArgumentPrefix().empty()) {
122 for (size_t i = 0; i < num_enumerators; ++i) {
123 llvm::StringRef name = m_enumerations.GetCStringAtIndex(i).GetStringRef();
124 request.TryCompleteCurrentArg(name);
125 }
126 return;
127 }
128 for (size_t i = 0; i < num_enumerators; ++i)
129 request.AddCompletion(m_enumerations.GetCStringAtIndex(i).GetStringRef());
130}
static llvm::raw_ostream & error(Stream &strm)
"lldb/Utility/ArgCompletionRequest.h"
void AddCompletion(llvm::StringRef completion, llvm::StringRef description="", CompletionMode mode=CompletionMode::Normal)
Adds a possible completion string.
llvm::StringRef GetCursorArgumentPrefix() const
void TryCompleteCurrentArg(llvm::StringRef completion, llvm::StringRef description="")
Adds a possible completion string if the completion would complete the current argument.
A uniqued constant string class.
Definition ConstString.h:40
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
void DumpEnum(Stream &strm, enum_type value)
void SetEnumerations(const OptionEnumValues &enumerators)
Status SetValueFromString(llvm::StringRef value, VarSetOperationType op=eVarSetOperationAssign) override
llvm::json::Value ToJSON(const ExecutionContext *exe_ctx) const override
void DumpValue(const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask) override
OptionValueEnumeration(const OptionEnumValues &enumerators, enum_type value)
void AutoComplete(CommandInterpreter &interpreter, CompletionRequest &request) 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
llvm::StringRef GetString() const
A stream class that can stream formatted output to a file.
Definition Stream.h:28
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
A class that represents a running process on the host machine.
VarSetOperationType
Settable state variable types.
llvm::ArrayRef< OptionEnumValueElement > OptionEnumValues