Path: blob/main/contrib/llvm-project/lldb/source/Interpreter/OptionValueEnumeration.cpp
39587 views
//===-- OptionValueEnumeration.cpp ----------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/Interpreter/OptionValueEnumeration.h"910#include "lldb/Utility/StringList.h"1112using namespace lldb;13using namespace lldb_private;1415OptionValueEnumeration::OptionValueEnumeration(16const OptionEnumValues &enumerators, enum_type value)17: m_current_value(value), m_default_value(value) {18SetEnumerations(enumerators);19}2021void OptionValueEnumeration::DumpValue(const ExecutionContext *exe_ctx,22Stream &strm, uint32_t dump_mask) {23if (dump_mask & eDumpOptionType)24strm.Printf("(%s)", GetTypeAsCString());25if (dump_mask & eDumpOptionValue) {26if (dump_mask & eDumpOptionType)27strm.PutCString(" = ");28const size_t count = m_enumerations.GetSize();29for (size_t i = 0; i < count; ++i) {30if (m_enumerations.GetValueAtIndexUnchecked(i).value == m_current_value) {31strm.PutCString(m_enumerations.GetCStringAtIndex(i).GetStringRef());32return;33}34}35strm.Printf("%" PRIu64, (uint64_t)m_current_value);36}37}3839Status OptionValueEnumeration::SetValueFromString(llvm::StringRef value,40VarSetOperationType op) {41Status error;42switch (op) {43case eVarSetOperationClear:44Clear();45NotifyValueChanged();46break;4748case eVarSetOperationReplace:49case eVarSetOperationAssign: {50ConstString const_enumerator_name(value.trim());51const EnumerationMapEntry *enumerator_entry =52m_enumerations.FindFirstValueForName(const_enumerator_name);53if (enumerator_entry) {54m_current_value = enumerator_entry->value.value;55NotifyValueChanged();56} else {57StreamString error_strm;58error_strm.Printf("invalid enumeration value '%s'", value.str().c_str());59const size_t count = m_enumerations.GetSize();60if (count) {61error_strm.Printf(", valid values are: %s",62m_enumerations.GetCStringAtIndex(0).GetCString());63for (size_t i = 1; i < count; ++i) {64error_strm.Printf(", %s",65m_enumerations.GetCStringAtIndex(i).GetCString());66}67}68error.SetErrorString(error_strm.GetString());69}70break;71}7273case eVarSetOperationInsertBefore:74case eVarSetOperationInsertAfter:75case eVarSetOperationRemove:76case eVarSetOperationAppend:77case eVarSetOperationInvalid:78error = OptionValue::SetValueFromString(value, op);79break;80}81return error;82}8384void OptionValueEnumeration::SetEnumerations(85const OptionEnumValues &enumerators) {86m_enumerations.Clear();8788for (const auto &enumerator : enumerators) {89ConstString const_enumerator_name(enumerator.string_value);90EnumeratorInfo enumerator_info = {enumerator.value, enumerator.usage};91m_enumerations.Append(const_enumerator_name, enumerator_info);92}9394m_enumerations.Sort();95}9697void OptionValueEnumeration::AutoComplete(CommandInterpreter &interpreter,98CompletionRequest &request) {99const uint32_t num_enumerators = m_enumerations.GetSize();100if (!request.GetCursorArgumentPrefix().empty()) {101for (size_t i = 0; i < num_enumerators; ++i) {102llvm::StringRef name = m_enumerations.GetCStringAtIndex(i).GetStringRef();103request.TryCompleteCurrentArg(name);104}105return;106}107for (size_t i = 0; i < num_enumerators; ++i)108request.AddCompletion(m_enumerations.GetCStringAtIndex(i).GetStringRef());109}110111112