Path: blob/main/contrib/llvm-project/lldb/source/Interpreter/OptionValue.cpp
39587 views
//===-- OptionValue.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/OptionValue.h"9#include "lldb/Interpreter/OptionValues.h"10#include "lldb/Utility/StringList.h"1112#include <memory>1314using namespace lldb;15using namespace lldb_private;1617OptionValue::OptionValue(const OptionValue &other) {18std::lock_guard<std::mutex> lock(other.m_mutex);1920m_parent_wp = other.m_parent_wp;21m_callback = other.m_callback;22m_value_was_set = other.m_value_was_set;2324}2526OptionValue& OptionValue::operator=(const OptionValue &other) {27std::scoped_lock<std::mutex, std::mutex> lock(m_mutex, other.m_mutex);2829m_parent_wp = other.m_parent_wp;30m_callback = other.m_callback;31m_value_was_set = other.m_value_was_set;3233return *this;34}3536Status OptionValue::SetSubValue(const ExecutionContext *exe_ctx,37VarSetOperationType op, llvm::StringRef name,38llvm::StringRef value) {39Status error;40error.SetErrorString("SetSubValue is not supported");41return error;42}4344OptionValueBoolean *OptionValue::GetAsBoolean() {45if (GetType() == OptionValue::eTypeBoolean)46return static_cast<OptionValueBoolean *>(this);47return nullptr;48}4950const OptionValueBoolean *OptionValue::GetAsBoolean() const {51if (GetType() == OptionValue::eTypeBoolean)52return static_cast<const OptionValueBoolean *>(this);53return nullptr;54}5556const OptionValueChar *OptionValue::GetAsChar() const {57if (GetType() == OptionValue::eTypeChar)58return static_cast<const OptionValueChar *>(this);59return nullptr;60}6162OptionValueChar *OptionValue::GetAsChar() {63if (GetType() == OptionValue::eTypeChar)64return static_cast<OptionValueChar *>(this);65return nullptr;66}6768OptionValueFileSpec *OptionValue::GetAsFileSpec() {69if (GetType() == OptionValue::eTypeFileSpec)70return static_cast<OptionValueFileSpec *>(this);71return nullptr;72}7374const OptionValueFileSpec *OptionValue::GetAsFileSpec() const {75if (GetType() == OptionValue::eTypeFileSpec)76return static_cast<const OptionValueFileSpec *>(this);77return nullptr;78}7980OptionValueFileSpecList *OptionValue::GetAsFileSpecList() {81if (GetType() == OptionValue::eTypeFileSpecList)82return static_cast<OptionValueFileSpecList *>(this);83return nullptr;84}8586const OptionValueFileSpecList *OptionValue::GetAsFileSpecList() const {87if (GetType() == OptionValue::eTypeFileSpecList)88return static_cast<const OptionValueFileSpecList *>(this);89return nullptr;90}9192OptionValueArch *OptionValue::GetAsArch() {93if (GetType() == OptionValue::eTypeArch)94return static_cast<OptionValueArch *>(this);95return nullptr;96}9798const OptionValueArch *OptionValue::GetAsArch() const {99if (GetType() == OptionValue::eTypeArch)100return static_cast<const OptionValueArch *>(this);101return nullptr;102}103104OptionValueArray *OptionValue::GetAsArray() {105if (GetType() == OptionValue::eTypeArray)106return static_cast<OptionValueArray *>(this);107return nullptr;108}109110const OptionValueArray *OptionValue::GetAsArray() const {111if (GetType() == OptionValue::eTypeArray)112return static_cast<const OptionValueArray *>(this);113return nullptr;114}115116OptionValueArgs *OptionValue::GetAsArgs() {117if (GetType() == OptionValue::eTypeArgs)118return static_cast<OptionValueArgs *>(this);119return nullptr;120}121122const OptionValueArgs *OptionValue::GetAsArgs() const {123if (GetType() == OptionValue::eTypeArgs)124return static_cast<const OptionValueArgs *>(this);125return nullptr;126}127128OptionValueDictionary *OptionValue::GetAsDictionary() {129if (GetType() == OptionValue::eTypeDictionary)130return static_cast<OptionValueDictionary *>(this);131return nullptr;132}133134const OptionValueDictionary *OptionValue::GetAsDictionary() const {135if (GetType() == OptionValue::eTypeDictionary)136return static_cast<const OptionValueDictionary *>(this);137return nullptr;138}139140OptionValueEnumeration *OptionValue::GetAsEnumeration() {141if (GetType() == OptionValue::eTypeEnum)142return static_cast<OptionValueEnumeration *>(this);143return nullptr;144}145146const OptionValueEnumeration *OptionValue::GetAsEnumeration() const {147if (GetType() == OptionValue::eTypeEnum)148return static_cast<const OptionValueEnumeration *>(this);149return nullptr;150}151152OptionValueFormat *OptionValue::GetAsFormat() {153if (GetType() == OptionValue::eTypeFormat)154return static_cast<OptionValueFormat *>(this);155return nullptr;156}157158const OptionValueFormat *OptionValue::GetAsFormat() const {159if (GetType() == OptionValue::eTypeFormat)160return static_cast<const OptionValueFormat *>(this);161return nullptr;162}163164OptionValueLanguage *OptionValue::GetAsLanguage() {165if (GetType() == OptionValue::eTypeLanguage)166return static_cast<OptionValueLanguage *>(this);167return nullptr;168}169170const OptionValueLanguage *OptionValue::GetAsLanguage() const {171if (GetType() == OptionValue::eTypeLanguage)172return static_cast<const OptionValueLanguage *>(this);173return nullptr;174}175176OptionValueFormatEntity *OptionValue::GetAsFormatEntity() {177if (GetType() == OptionValue::eTypeFormatEntity)178return static_cast<OptionValueFormatEntity *>(this);179return nullptr;180}181182const OptionValueFormatEntity *OptionValue::GetAsFormatEntity() const {183if (GetType() == OptionValue::eTypeFormatEntity)184return static_cast<const OptionValueFormatEntity *>(this);185return nullptr;186}187188OptionValuePathMappings *OptionValue::GetAsPathMappings() {189if (GetType() == OptionValue::eTypePathMap)190return static_cast<OptionValuePathMappings *>(this);191return nullptr;192}193194const OptionValuePathMappings *OptionValue::GetAsPathMappings() const {195if (GetType() == OptionValue::eTypePathMap)196return static_cast<const OptionValuePathMappings *>(this);197return nullptr;198}199200OptionValueProperties *OptionValue::GetAsProperties() {201if (GetType() == OptionValue::eTypeProperties)202return static_cast<OptionValueProperties *>(this);203return nullptr;204}205206const OptionValueProperties *OptionValue::GetAsProperties() const {207if (GetType() == OptionValue::eTypeProperties)208return static_cast<const OptionValueProperties *>(this);209return nullptr;210}211212OptionValueRegex *OptionValue::GetAsRegex() {213if (GetType() == OptionValue::eTypeRegex)214return static_cast<OptionValueRegex *>(this);215return nullptr;216}217218const OptionValueRegex *OptionValue::GetAsRegex() const {219if (GetType() == OptionValue::eTypeRegex)220return static_cast<const OptionValueRegex *>(this);221return nullptr;222}223224OptionValueSInt64 *OptionValue::GetAsSInt64() {225if (GetType() == OptionValue::eTypeSInt64)226return static_cast<OptionValueSInt64 *>(this);227return nullptr;228}229230const OptionValueSInt64 *OptionValue::GetAsSInt64() const {231if (GetType() == OptionValue::eTypeSInt64)232return static_cast<const OptionValueSInt64 *>(this);233return nullptr;234}235236OptionValueString *OptionValue::GetAsString() {237if (GetType() == OptionValue::eTypeString)238return static_cast<OptionValueString *>(this);239return nullptr;240}241242const OptionValueString *OptionValue::GetAsString() const {243if (GetType() == OptionValue::eTypeString)244return static_cast<const OptionValueString *>(this);245return nullptr;246}247248OptionValueUInt64 *OptionValue::GetAsUInt64() {249if (GetType() == OptionValue::eTypeUInt64)250return static_cast<OptionValueUInt64 *>(this);251return nullptr;252}253254const OptionValueUInt64 *OptionValue::GetAsUInt64() const {255if (GetType() == OptionValue::eTypeUInt64)256return static_cast<const OptionValueUInt64 *>(this);257return nullptr;258}259260OptionValueUUID *OptionValue::GetAsUUID() {261if (GetType() == OptionValue::eTypeUUID)262return static_cast<OptionValueUUID *>(this);263return nullptr;264}265266const OptionValueUUID *OptionValue::GetAsUUID() const {267if (GetType() == OptionValue::eTypeUUID)268return static_cast<const OptionValueUUID *>(this);269return nullptr;270}271272std::optional<bool> OptionValue::GetBooleanValue() const {273std::lock_guard<std::mutex> lock(m_mutex);274if (const OptionValueBoolean *option_value = GetAsBoolean())275return option_value->GetCurrentValue();276return {};277}278279bool OptionValue::SetBooleanValue(bool new_value) {280std::lock_guard<std::mutex> lock(m_mutex);281if (OptionValueBoolean *option_value = GetAsBoolean()) {282option_value->SetCurrentValue(new_value);283return true;284}285return false;286}287288std::optional<char> OptionValue::GetCharValue() const {289std::lock_guard<std::mutex> lock(m_mutex);290if (const OptionValueChar *option_value = GetAsChar())291return option_value->GetCurrentValue();292return {};293}294295bool OptionValue::SetCharValue(char new_value) {296std::lock_guard<std::mutex> lock(m_mutex);297if (OptionValueChar *option_value = GetAsChar()) {298option_value->SetCurrentValue(new_value);299return true;300}301return false;302}303304std::optional<int64_t> OptionValue::GetEnumerationValue() const {305std::lock_guard<std::mutex> lock(m_mutex);306if (const OptionValueEnumeration *option_value = GetAsEnumeration())307return option_value->GetCurrentValue();308return {};309}310311bool OptionValue::SetEnumerationValue(int64_t value) {312std::lock_guard<std::mutex> lock(m_mutex);313if (OptionValueEnumeration *option_value = GetAsEnumeration()) {314option_value->SetCurrentValue(value);315return true;316}317return false;318}319320std::optional<FileSpec> OptionValue::GetFileSpecValue() const {321std::lock_guard<std::mutex> lock(m_mutex);322if (const OptionValueFileSpec *option_value = GetAsFileSpec())323return option_value->GetCurrentValue();324return {};325}326327bool OptionValue::SetFileSpecValue(FileSpec file_spec) {328std::lock_guard<std::mutex> lock(m_mutex);329if (OptionValueFileSpec *option_value = GetAsFileSpec()) {330option_value->SetCurrentValue(file_spec, false);331return true;332}333return false;334}335336bool OptionValue::AppendFileSpecValue(FileSpec file_spec) {337std::lock_guard<std::mutex> lock(m_mutex);338if (OptionValueFileSpecList *option_value = GetAsFileSpecList()) {339option_value->AppendCurrentValue(file_spec);340return true;341}342return false;343}344345std::optional<FileSpecList> OptionValue::GetFileSpecListValue() const {346std::lock_guard<std::mutex> lock(m_mutex);347if (const OptionValueFileSpecList *option_value = GetAsFileSpecList())348return option_value->GetCurrentValue();349return {};350}351352std::optional<lldb::Format> OptionValue::GetFormatValue() const {353std::lock_guard<std::mutex> lock(m_mutex);354if (const OptionValueFormat *option_value = GetAsFormat())355return option_value->GetCurrentValue();356return {};357}358359bool OptionValue::SetFormatValue(lldb::Format new_value) {360std::lock_guard<std::mutex> lock(m_mutex);361if (OptionValueFormat *option_value = GetAsFormat()) {362option_value->SetCurrentValue(new_value);363return true;364}365return false;366}367368std::optional<lldb::LanguageType> OptionValue::GetLanguageValue() const {369std::lock_guard<std::mutex> lock(m_mutex);370if (const OptionValueLanguage *option_value = GetAsLanguage())371return option_value->GetCurrentValue();372return {};373}374375bool OptionValue::SetLanguageValue(lldb::LanguageType new_language) {376std::lock_guard<std::mutex> lock(m_mutex);377if (OptionValueLanguage *option_value = GetAsLanguage()) {378option_value->SetCurrentValue(new_language);379return true;380}381return false;382}383384const FormatEntity::Entry *OptionValue::GetFormatEntity() const {385std::lock_guard<std::mutex> lock(m_mutex);386if (const OptionValueFormatEntity *option_value = GetAsFormatEntity())387return &option_value->GetCurrentValue();388return nullptr;389}390391const RegularExpression *OptionValue::GetRegexValue() const {392std::lock_guard<std::mutex> lock(m_mutex);393if (const OptionValueRegex *option_value = GetAsRegex())394return option_value->GetCurrentValue();395return nullptr;396}397398std::optional<int64_t> OptionValue::GetSInt64Value() const {399std::lock_guard<std::mutex> lock(m_mutex);400if (const OptionValueSInt64 *option_value = GetAsSInt64())401return option_value->GetCurrentValue();402return {};403}404405bool OptionValue::SetSInt64Value(int64_t new_value) {406std::lock_guard<std::mutex> lock(m_mutex);407if (OptionValueSInt64 *option_value = GetAsSInt64()) {408option_value->SetCurrentValue(new_value);409return true;410}411return false;412}413414std::optional<llvm::StringRef> OptionValue::GetStringValue() const {415std::lock_guard<std::mutex> lock(m_mutex);416if (const OptionValueString *option_value = GetAsString())417return option_value->GetCurrentValueAsRef();418return {};419}420421bool OptionValue::SetStringValue(llvm::StringRef new_value) {422std::lock_guard<std::mutex> lock(m_mutex);423if (OptionValueString *option_value = GetAsString()) {424option_value->SetCurrentValue(new_value);425return true;426}427return false;428}429430std::optional<uint64_t> OptionValue::GetUInt64Value() const {431std::lock_guard<std::mutex> lock(m_mutex);432if (const OptionValueUInt64 *option_value = GetAsUInt64())433return option_value->GetCurrentValue();434return {};435}436437bool OptionValue::SetUInt64Value(uint64_t new_value) {438std::lock_guard<std::mutex> lock(m_mutex);439if (OptionValueUInt64 *option_value = GetAsUInt64()) {440option_value->SetCurrentValue(new_value);441return true;442}443return false;444}445446std::optional<UUID> OptionValue::GetUUIDValue() const {447std::lock_guard<std::mutex> lock(m_mutex);448if (const OptionValueUUID *option_value = GetAsUUID())449return option_value->GetCurrentValue();450return {};451}452453bool OptionValue::SetUUIDValue(const UUID &uuid) {454std::lock_guard<std::mutex> lock(m_mutex);455if (OptionValueUUID *option_value = GetAsUUID()) {456option_value->SetCurrentValue(uuid);457return true;458}459return false;460}461462std::optional<ArchSpec> OptionValue::GetArchSpecValue() const {463std::lock_guard<std::mutex> lock(m_mutex);464if (const OptionValueArch *option_value = GetAsArch())465return option_value->GetCurrentValue();466return {};467}468469bool OptionValue::SetArchSpecValue(ArchSpec arch_spec) {470std::lock_guard<std::mutex> lock(m_mutex);471if (OptionValueArch *option_value = GetAsArch()) {472option_value->SetCurrentValue(arch_spec, false);473return true;474}475return false;476}477478const char *OptionValue::GetBuiltinTypeAsCString(Type t) {479switch (t) {480case eTypeInvalid:481return "invalid";482case eTypeArch:483return "arch";484case eTypeArgs:485return "arguments";486case eTypeArray:487return "array";488case eTypeBoolean:489return "boolean";490case eTypeChar:491return "char";492case eTypeDictionary:493return "dictionary";494case eTypeEnum:495return "enum";496case eTypeFileLineColumn:497return "file:line:column specifier";498case eTypeFileSpec:499return "file";500case eTypeFileSpecList:501return "file-list";502case eTypeFormat:503return "format";504case eTypeFormatEntity:505return "format-string";506case eTypeLanguage:507return "language";508case eTypePathMap:509return "path-map";510case eTypeProperties:511return "properties";512case eTypeRegex:513return "regex";514case eTypeSInt64:515return "int";516case eTypeString:517return "string";518case eTypeUInt64:519return "unsigned";520case eTypeUUID:521return "uuid";522}523return nullptr;524}525526lldb::OptionValueSP OptionValue::CreateValueFromCStringForTypeMask(527const char *value_cstr, uint32_t type_mask, Status &error) {528// If only 1 bit is set in the type mask for a dictionary or array then we529// know how to decode a value from a cstring530lldb::OptionValueSP value_sp;531switch (type_mask) {532case 1u << eTypeArch:533value_sp = std::make_shared<OptionValueArch>();534break;535case 1u << eTypeBoolean:536value_sp = std::make_shared<OptionValueBoolean>(false);537break;538case 1u << eTypeChar:539value_sp = std::make_shared<OptionValueChar>('\0');540break;541case 1u << eTypeFileSpec:542value_sp = std::make_shared<OptionValueFileSpec>();543break;544case 1u << eTypeFormat:545value_sp = std::make_shared<OptionValueFormat>(eFormatInvalid);546break;547case 1u << eTypeFormatEntity:548value_sp = std::make_shared<OptionValueFormatEntity>(nullptr);549break;550case 1u << eTypeLanguage:551value_sp = std::make_shared<OptionValueLanguage>(eLanguageTypeUnknown);552break;553case 1u << eTypeSInt64:554value_sp = std::make_shared<OptionValueSInt64>();555break;556case 1u << eTypeString:557value_sp = std::make_shared<OptionValueString>();558break;559case 1u << eTypeUInt64:560value_sp = std::make_shared<OptionValueUInt64>();561break;562case 1u << eTypeUUID:563value_sp = std::make_shared<OptionValueUUID>();564break;565}566567if (value_sp)568error = value_sp->SetValueFromString(value_cstr, eVarSetOperationAssign);569else570error.SetErrorString("unsupported type mask");571return value_sp;572}573574bool OptionValue::DumpQualifiedName(Stream &strm) const {575bool dumped_something = false;576lldb::OptionValueSP m_parent_sp(m_parent_wp.lock());577if (m_parent_sp) {578if (m_parent_sp->DumpQualifiedName(strm))579dumped_something = true;580}581llvm::StringRef name(GetName());582if (!name.empty()) {583if (dumped_something)584strm.PutChar('.');585else586dumped_something = true;587strm << name;588}589return dumped_something;590}591592OptionValueSP OptionValue::DeepCopy(const OptionValueSP &new_parent) const {593auto clone = Clone();594clone->SetParent(new_parent);595return clone;596}597598void OptionValue::AutoComplete(CommandInterpreter &interpreter,599CompletionRequest &request) {}600601Status OptionValue::SetValueFromString(llvm::StringRef value,602VarSetOperationType op) {603Status error;604switch (op) {605case eVarSetOperationReplace:606error.SetErrorStringWithFormat(607"%s objects do not support the 'replace' operation",608GetTypeAsCString());609break;610case eVarSetOperationInsertBefore:611error.SetErrorStringWithFormat(612"%s objects do not support the 'insert-before' operation",613GetTypeAsCString());614break;615case eVarSetOperationInsertAfter:616error.SetErrorStringWithFormat(617"%s objects do not support the 'insert-after' operation",618GetTypeAsCString());619break;620case eVarSetOperationRemove:621error.SetErrorStringWithFormat(622"%s objects do not support the 'remove' operation", GetTypeAsCString());623break;624case eVarSetOperationAppend:625error.SetErrorStringWithFormat(626"%s objects do not support the 'append' operation", GetTypeAsCString());627break;628case eVarSetOperationClear:629error.SetErrorStringWithFormat(630"%s objects do not support the 'clear' operation", GetTypeAsCString());631break;632case eVarSetOperationAssign:633error.SetErrorStringWithFormat(634"%s objects do not support the 'assign' operation", GetTypeAsCString());635break;636case eVarSetOperationInvalid:637error.SetErrorStringWithFormat("invalid operation performed on a %s object",638GetTypeAsCString());639break;640}641return error;642}643644645