brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.3 KiB · eb31bde Raw
131 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "lldb/Interpreter/OptionValueEnumeration.h"10 11#include "lldb/Interpreter/OptionValue.h"12#include "lldb/Utility/StringList.h"13 14using namespace lldb;15using namespace lldb_private;16 17OptionValueEnumeration::OptionValueEnumeration(18    const OptionEnumValues &enumerators, enum_type value)19    : m_current_value(value), m_default_value(value) {20  SetEnumerations(enumerators);21}22 23void OptionValueEnumeration::DumpEnum(Stream &strm, enum_type value) {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 34void OptionValueEnumeration::DumpValue(const ExecutionContext *exe_ctx,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(" = ");41    DumpEnum(strm, m_current_value);42    if (dump_mask & eDumpOptionDefaultValue &&43        m_current_value != m_default_value) {44      DefaultValueFormat label(strm);45      DumpEnum(strm, m_default_value);46    }47  }48}49 50llvm::json::Value51OptionValueEnumeration::ToJSON(const ExecutionContext *exe_ctx) const {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 60Status OptionValueEnumeration::SetValueFromString(llvm::StringRef value,61                                                  VarSetOperationType op) {62  Status error;63  switch (op) {64  case eVarSetOperationClear:65    Clear();66    NotifyValueChanged();67    break;68 69  case eVarSetOperationReplace:70  case eVarSetOperationAssign: {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;76      NotifyValueChanged();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 94  case eVarSetOperationInsertBefore:95  case eVarSetOperationInsertAfter:96  case eVarSetOperationRemove:97  case eVarSetOperationAppend:98  case eVarSetOperationInvalid:99    error = OptionValue::SetValueFromString(value, op);100    break;101  }102  return error;103}104 105void OptionValueEnumeration::SetEnumerations(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 118void OptionValueEnumeration::AutoComplete(CommandInterpreter &interpreter,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}131