1 //===-- OptionValueEnumeration.cpp ----------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Interpreter/OptionValueEnumeration.h"
11 #include "lldb/Utility/StringList.h"
14 using namespace lldb_private
;
16 OptionValueEnumeration::OptionValueEnumeration(
17 const OptionEnumValues
&enumerators
, enum_type value
)
18 : m_current_value(value
), m_default_value(value
) {
19 SetEnumerations(enumerators
);
22 void OptionValueEnumeration::DumpValue(const ExecutionContext
*exe_ctx
,
23 Stream
&strm
, uint32_t dump_mask
) {
24 if (dump_mask
& eDumpOptionType
)
25 strm
.Printf("(%s)", GetTypeAsCString());
26 if (dump_mask
& eDumpOptionValue
) {
27 if (dump_mask
& eDumpOptionType
)
28 strm
.PutCString(" = ");
29 const size_t count
= m_enumerations
.GetSize();
30 for (size_t i
= 0; i
< count
; ++i
) {
31 if (m_enumerations
.GetValueAtIndexUnchecked(i
).value
== m_current_value
) {
32 strm
.PutCString(m_enumerations
.GetCStringAtIndex(i
).GetStringRef());
36 strm
.Printf("%" PRIu64
, (uint64_t)m_current_value
);
40 Status
OptionValueEnumeration::SetValueFromString(llvm::StringRef value
,
41 VarSetOperationType op
) {
44 case eVarSetOperationClear
:
49 case eVarSetOperationReplace
:
50 case eVarSetOperationAssign
: {
51 ConstString
const_enumerator_name(value
.trim());
52 const EnumerationMapEntry
*enumerator_entry
=
53 m_enumerations
.FindFirstValueForName(const_enumerator_name
);
54 if (enumerator_entry
) {
55 m_current_value
= enumerator_entry
->value
.value
;
58 StreamString error_strm
;
59 error_strm
.Printf("invalid enumeration value '%s'", value
.str().c_str());
60 const size_t count
= m_enumerations
.GetSize();
62 error_strm
.Printf(", valid values are: %s",
63 m_enumerations
.GetCStringAtIndex(0).GetCString());
64 for (size_t i
= 1; i
< count
; ++i
) {
65 error_strm
.Printf(", %s",
66 m_enumerations
.GetCStringAtIndex(i
).GetCString());
69 error
= Status(error_strm
.GetString().str());
74 case eVarSetOperationInsertBefore
:
75 case eVarSetOperationInsertAfter
:
76 case eVarSetOperationRemove
:
77 case eVarSetOperationAppend
:
78 case eVarSetOperationInvalid
:
79 error
= OptionValue::SetValueFromString(value
, op
);
85 void OptionValueEnumeration::SetEnumerations(
86 const OptionEnumValues
&enumerators
) {
87 m_enumerations
.Clear();
89 for (const auto &enumerator
: enumerators
) {
90 ConstString
const_enumerator_name(enumerator
.string_value
);
91 EnumeratorInfo enumerator_info
= {enumerator
.value
, enumerator
.usage
};
92 m_enumerations
.Append(const_enumerator_name
, enumerator_info
);
95 m_enumerations
.Sort();
98 void OptionValueEnumeration::AutoComplete(CommandInterpreter
&interpreter
,
99 CompletionRequest
&request
) {
100 const uint32_t num_enumerators
= m_enumerations
.GetSize();
101 if (!request
.GetCursorArgumentPrefix().empty()) {
102 for (size_t i
= 0; i
< num_enumerators
; ++i
) {
103 llvm::StringRef name
= m_enumerations
.GetCStringAtIndex(i
).GetStringRef();
104 request
.TryCompleteCurrentArg(name
);
108 for (size_t i
= 0; i
< num_enumerators
; ++i
)
109 request
.AddCompletion(m_enumerations
.GetCStringAtIndex(i
).GetStringRef());