180814287SRaphael Isemann //===-- OptionValueEnumeration.cpp ----------------------------------------===//
267cc0636SGreg Clayton //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
667cc0636SGreg Clayton //
767cc0636SGreg Clayton //===----------------------------------------------------------------------===//
867cc0636SGreg Clayton
967cc0636SGreg Clayton #include "lldb/Interpreter/OptionValueEnumeration.h"
1067cc0636SGreg Clayton
11573ab909SZachary Turner #include "lldb/Utility/StringList.h"
1267cc0636SGreg Clayton
1367cc0636SGreg Clayton using namespace lldb;
1467cc0636SGreg Clayton using namespace lldb_private;
1567cc0636SGreg Clayton
OptionValueEnumeration(const OptionEnumValues & enumerators,enum_type value)16b9c1b51eSKate Stone OptionValueEnumeration::OptionValueEnumeration(
178fe53c49STatyana Krasnukha const OptionEnumValues &enumerators, enum_type value)
18*8cdcd41eSTatyana Krasnukha : m_current_value(value), m_default_value(value) {
1967cc0636SGreg Clayton SetEnumerations(enumerators);
2067cc0636SGreg Clayton }
2167cc0636SGreg Clayton
DumpValue(const ExecutionContext * exe_ctx,Stream & strm,uint32_t dump_mask)22b9c1b51eSKate Stone void OptionValueEnumeration::DumpValue(const ExecutionContext *exe_ctx,
23b9c1b51eSKate Stone Stream &strm, uint32_t dump_mask) {
2467cc0636SGreg Clayton if (dump_mask & eDumpOptionType)
2567cc0636SGreg Clayton strm.Printf("(%s)", GetTypeAsCString());
26b9c1b51eSKate Stone if (dump_mask & eDumpOptionValue) {
2767cc0636SGreg Clayton if (dump_mask & eDumpOptionType)
2867cc0636SGreg Clayton strm.PutCString(" = ");
2967cc0636SGreg Clayton const size_t count = m_enumerations.GetSize();
30b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) {
31b9c1b51eSKate Stone if (m_enumerations.GetValueAtIndexUnchecked(i).value == m_current_value) {
324d35d6b3SPavel Labath strm.PutCString(m_enumerations.GetCStringAtIndex(i).GetStringRef());
3367cc0636SGreg Clayton return;
3467cc0636SGreg Clayton }
3567cc0636SGreg Clayton }
36d01b2953SDaniel Malea strm.Printf("%" PRIu64, (uint64_t)m_current_value);
3767cc0636SGreg Clayton }
3867cc0636SGreg Clayton }
3967cc0636SGreg Clayton
SetValueFromString(llvm::StringRef value,VarSetOperationType op)4097206d57SZachary Turner Status OptionValueEnumeration::SetValueFromString(llvm::StringRef value,
41b9c1b51eSKate Stone VarSetOperationType op) {
4297206d57SZachary Turner Status error;
43b9c1b51eSKate Stone switch (op) {
4467cc0636SGreg Clayton case eVarSetOperationClear:
4567cc0636SGreg Clayton Clear();
46332e8b1cSGreg Clayton NotifyValueChanged();
4767cc0636SGreg Clayton break;
4867cc0636SGreg Clayton
4967cc0636SGreg Clayton case eVarSetOperationReplace:
50b9c1b51eSKate Stone case eVarSetOperationAssign: {
51c95f7e2aSPavel Labath ConstString const_enumerator_name(value.trim());
52b9c1b51eSKate Stone const EnumerationMapEntry *enumerator_entry =
534d35d6b3SPavel Labath m_enumerations.FindFirstValueForName(const_enumerator_name);
54b9c1b51eSKate Stone if (enumerator_entry) {
5567cc0636SGreg Clayton m_current_value = enumerator_entry->value.value;
56332e8b1cSGreg Clayton NotifyValueChanged();
57b9c1b51eSKate Stone } else {
5867cc0636SGreg Clayton StreamString error_strm;
59c95f7e2aSPavel Labath error_strm.Printf("invalid enumeration value '%s'", value.str().c_str());
6067cc0636SGreg Clayton const size_t count = m_enumerations.GetSize();
61b9c1b51eSKate Stone if (count) {
62b9c1b51eSKate Stone error_strm.Printf(", valid values are: %s",
634d35d6b3SPavel Labath m_enumerations.GetCStringAtIndex(0).GetCString());
64b9c1b51eSKate Stone for (size_t i = 1; i < count; ++i) {
654fa098a5SZachary Turner error_strm.Printf(", %s",
664d35d6b3SPavel Labath m_enumerations.GetCStringAtIndex(i).GetCString());
6767cc0636SGreg Clayton }
6867cc0636SGreg Clayton }
69c156427dSZachary Turner error.SetErrorString(error_strm.GetString());
7067cc0636SGreg Clayton }
7167cc0636SGreg Clayton break;
72c95f7e2aSPavel Labath }
7367cc0636SGreg Clayton
7467cc0636SGreg Clayton case eVarSetOperationInsertBefore:
7567cc0636SGreg Clayton case eVarSetOperationInsertAfter:
7667cc0636SGreg Clayton case eVarSetOperationRemove:
7767cc0636SGreg Clayton case eVarSetOperationAppend:
7867cc0636SGreg Clayton case eVarSetOperationInvalid:
79c95f7e2aSPavel Labath error = OptionValue::SetValueFromString(value, op);
8067cc0636SGreg Clayton break;
8167cc0636SGreg Clayton }
8267cc0636SGreg Clayton return error;
8367cc0636SGreg Clayton }
8467cc0636SGreg Clayton
SetEnumerations(const OptionEnumValues & enumerators)85b9c1b51eSKate Stone void OptionValueEnumeration::SetEnumerations(
868fe53c49STatyana Krasnukha const OptionEnumValues &enumerators) {
8767cc0636SGreg Clayton m_enumerations.Clear();
888fe53c49STatyana Krasnukha
898fe53c49STatyana Krasnukha for (const auto &enumerator : enumerators) {
908fe53c49STatyana Krasnukha ConstString const_enumerator_name(enumerator.string_value);
918fe53c49STatyana Krasnukha EnumeratorInfo enumerator_info = {enumerator.value, enumerator.usage};
928fe53c49STatyana Krasnukha m_enumerations.Append(const_enumerator_name, enumerator_info);
9367cc0636SGreg Clayton }
948fe53c49STatyana Krasnukha
9567cc0636SGreg Clayton m_enumerations.Sort();
9667cc0636SGreg Clayton }
9767cc0636SGreg Clayton
AutoComplete(CommandInterpreter & interpreter,CompletionRequest & request)98ae34ed2cSRaphael Isemann void OptionValueEnumeration::AutoComplete(CommandInterpreter &interpreter,
99a2e76c0bSRaphael Isemann CompletionRequest &request) {
100754a9369SGreg Clayton const uint32_t num_enumerators = m_enumerations.GetSize();
101a2e76c0bSRaphael Isemann if (!request.GetCursorArgumentPrefix().empty()) {
102b9c1b51eSKate Stone for (size_t i = 0; i < num_enumerators; ++i) {
1034d35d6b3SPavel Labath llvm::StringRef name = m_enumerations.GetCStringAtIndex(i).GetStringRef();
10493ca36d7SRaphael Isemann request.TryCompleteCurrentArg(name);
105754a9369SGreg Clayton }
1061153dc96SRaphael Isemann return;
1071153dc96SRaphael Isemann }
108754a9369SGreg Clayton for (size_t i = 0; i < num_enumerators; ++i)
1091a6d7ab5SRaphael Isemann request.AddCompletion(m_enumerations.GetCStringAtIndex(i).GetStringRef());
110754a9369SGreg Clayton }
111