Path: blob/main/contrib/llvm-project/lldb/source/Interpreter/Property.cpp
39587 views
//===-- Property.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/Property.h"910#include "lldb/Core/UserSettingsController.h"11#include "lldb/Interpreter/CommandInterpreter.h"12#include "lldb/Interpreter/OptionArgParser.h"13#include "lldb/Interpreter/OptionValues.h"14#include "lldb/Target/Language.h"1516#include <memory>1718using namespace lldb;19using namespace lldb_private;2021Property::Property(const PropertyDefinition &definition)22: m_name(definition.name), m_description(definition.description),23m_is_global(definition.global) {24switch (definition.type) {25case OptionValue::eTypeInvalid:26case OptionValue::eTypeProperties:27break;28case OptionValue::eTypeArch:29// "definition.default_uint_value" is not used30// "definition.default_cstr_value" as a string value that represents the31// default string value for the architecture/triple32m_value_sp =33std::make_shared<OptionValueArch>(definition.default_cstr_value);34break;3536case OptionValue::eTypeArgs:37// "definition.default_uint_value" is always a OptionValue::Type38m_value_sp = std::make_shared<OptionValueArgs>();39break;4041case OptionValue::eTypeArray:42// "definition.default_uint_value" is always a OptionValue::Type43m_value_sp =44std::make_shared<OptionValueArray>(OptionValue::ConvertTypeToMask(45(OptionValue::Type)definition.default_uint_value));46break;4748case OptionValue::eTypeBoolean:49// "definition.default_uint_value" is the default boolean value if50// "definition.default_cstr_value" is NULL, otherwise interpret51// "definition.default_cstr_value" as a string value that represents the52// default value.53if (definition.default_cstr_value)54m_value_sp =55std::make_shared<OptionValueBoolean>(OptionArgParser::ToBoolean(56llvm::StringRef(definition.default_cstr_value), false, nullptr));57else58m_value_sp = std::make_shared<OptionValueBoolean>(59definition.default_uint_value != 0);60break;6162case OptionValue::eTypeChar: {63llvm::StringRef s(definition.default_cstr_value ? definition.default_cstr_value : "");64m_value_sp = std::make_shared<OptionValueChar>(65OptionArgParser::ToChar(s, '\0', nullptr));66break;67}68case OptionValue::eTypeDictionary:69// "definition.default_uint_value" is always a OptionValue::Type70m_value_sp = std::make_shared<OptionValueDictionary>(71OptionValue::ConvertTypeToMask(72(OptionValue::Type)definition.default_uint_value),73definition.enum_values);74break;7576case OptionValue::eTypeEnum:77// "definition.default_uint_value" is the default enumeration value if78// "definition.default_cstr_value" is NULL, otherwise interpret79// "definition.default_cstr_value" as a string value that represents the80// default value.81{82OptionValueEnumeration *enum_value = new OptionValueEnumeration(83definition.enum_values, definition.default_uint_value);84m_value_sp.reset(enum_value);85if (definition.default_cstr_value) {86if (enum_value87->SetValueFromString(88llvm::StringRef(definition.default_cstr_value))89.Success()) {90enum_value->SetDefaultValue(enum_value->GetCurrentValue());91// Call Clear() since we don't want the value to appear as having92// been set since we called SetValueFromString() above. Clear will93// set the current value to the default and clear the boolean that94// says that the value has been set.95enum_value->Clear();96}97}98}99break;100101case OptionValue::eTypeFileLineColumn:102// "definition.default_uint_value" is not used for a103// OptionValue::eTypeFileSpecList104m_value_sp = std::make_shared<OptionValueFileColonLine>();105break;106107case OptionValue::eTypeFileSpec: {108// "definition.default_uint_value" represents if the109// "definition.default_cstr_value" should be resolved or not110const bool resolve = definition.default_uint_value != 0;111FileSpec file_spec = FileSpec(definition.default_cstr_value);112if (resolve)113FileSystem::Instance().Resolve(file_spec);114m_value_sp = std::make_shared<OptionValueFileSpec>(file_spec, resolve);115break;116}117118case OptionValue::eTypeFileSpecList:119// "definition.default_uint_value" is not used for a120// OptionValue::eTypeFileSpecList121m_value_sp = std::make_shared<OptionValueFileSpecList>();122break;123124case OptionValue::eTypeFormat:125// "definition.default_uint_value" is the default format enumeration value126// if "definition.default_cstr_value" is NULL, otherwise interpret127// "definition.default_cstr_value" as a string value that represents the128// default value.129{130Format new_format = eFormatInvalid;131if (definition.default_cstr_value)132OptionArgParser::ToFormat(definition.default_cstr_value, new_format,133nullptr);134else135new_format = (Format)definition.default_uint_value;136m_value_sp = std::make_shared<OptionValueFormat>(new_format);137}138break;139140case OptionValue::eTypeLanguage:141// "definition.default_uint_value" is the default language enumeration142// value if "definition.default_cstr_value" is NULL, otherwise interpret143// "definition.default_cstr_value" as a string value that represents the144// default value.145{146LanguageType new_lang = eLanguageTypeUnknown;147if (definition.default_cstr_value)148Language::GetLanguageTypeFromString(149llvm::StringRef(definition.default_cstr_value));150else151new_lang = (LanguageType)definition.default_uint_value;152m_value_sp = std::make_shared<OptionValueLanguage>(new_lang);153}154break;155156case OptionValue::eTypeFormatEntity:157// "definition.default_cstr_value" as a string value that represents the158// default159m_value_sp = std::make_shared<OptionValueFormatEntity>(160definition.default_cstr_value);161break;162163case OptionValue::eTypePathMap:164// "definition.default_uint_value" tells us if notifications should occur165// for path mappings166m_value_sp = std::make_shared<OptionValuePathMappings>(167definition.default_uint_value != 0);168break;169170case OptionValue::eTypeRegex:171// "definition.default_uint_value" is used to the regular expression flags172// "definition.default_cstr_value" the default regular expression value173// value.174m_value_sp =175std::make_shared<OptionValueRegex>(definition.default_cstr_value);176break;177178case OptionValue::eTypeSInt64: {179// "definition.default_uint_value" is the default integer value if180// "definition.default_cstr_value" is NULL, otherwise interpret181// "definition.default_cstr_value" as a string value that represents the182// default value.183int64_t value = 0;184// FIXME: improve error handling for llvm::to_integer()185if (definition.default_cstr_value)186llvm::to_integer(definition.default_cstr_value, value);187m_value_sp = std::make_shared<OptionValueSInt64>(188definition.default_cstr_value ? value : definition.default_uint_value);189break;190}191case OptionValue::eTypeUInt64: {192uint64_t value = 0;193// FIXME: improve error handling for llvm::to_integer()194if (definition.default_cstr_value)195llvm::to_integer(definition.default_cstr_value, value);196// "definition.default_uint_value" is the default unsigned integer value if197// "definition.default_cstr_value" is NULL, otherwise interpret198// "definition.default_cstr_value" as a string value that represents the199// default value.200m_value_sp = std::make_shared<OptionValueUInt64>(201definition.default_cstr_value ? value : definition.default_uint_value);202break;203}204case OptionValue::eTypeUUID:205// "definition.default_uint_value" is not used for a OptionValue::eTypeUUID206// "definition.default_cstr_value" can contain a default UUID value207{208UUID uuid;209if (definition.default_cstr_value)210uuid.SetFromStringRef(definition.default_cstr_value);211m_value_sp = std::make_shared<OptionValueUUID>(uuid);212}213break;214215case OptionValue::eTypeString:216// "definition.default_uint_value" can contain the string option flags217// OR'ed together "definition.default_cstr_value" can contain a default218// string value219{220OptionValueString *string_value =221new OptionValueString(definition.default_cstr_value);222if (definition.default_uint_value != 0)223string_value->GetOptions().Reset(definition.default_uint_value);224m_value_sp.reset(string_value);225}226break;227}228assert(m_value_sp && "invalid property definition");229}230231Property::Property(llvm::StringRef name, llvm::StringRef desc, bool is_global,232const lldb::OptionValueSP &value_sp)233: m_name(name), m_description(desc), m_value_sp(value_sp),234m_is_global(is_global) {}235236bool Property::DumpQualifiedName(Stream &strm) const {237if (!m_name.empty()) {238if (m_value_sp->DumpQualifiedName(strm))239strm.PutChar('.');240strm << m_name;241return true;242}243return false;244}245246void Property::Dump(const ExecutionContext *exe_ctx, Stream &strm,247uint32_t dump_mask) const {248if (m_value_sp) {249const bool dump_desc = dump_mask & OptionValue::eDumpOptionDescription;250const bool dump_cmd = dump_mask & OptionValue::eDumpOptionCommand;251const bool transparent = m_value_sp->ValueIsTransparent();252if (dump_cmd && !transparent)253strm << "settings set -f ";254if (dump_desc || !transparent) {255if ((dump_mask & OptionValue::eDumpOptionName) && !m_name.empty()) {256DumpQualifiedName(strm);257if (dump_mask & ~OptionValue::eDumpOptionName)258strm.PutChar(' ');259}260}261if (dump_desc) {262llvm::StringRef desc = GetDescription();263if (!desc.empty())264strm << "-- " << desc;265266if (transparent && (dump_mask == (OptionValue::eDumpOptionName |267OptionValue::eDumpOptionDescription)))268strm.EOL();269}270m_value_sp->DumpValue(exe_ctx, strm, dump_mask);271}272}273274void Property::DumpDescription(CommandInterpreter &interpreter, Stream &strm,275uint32_t output_width,276bool display_qualified_name) const {277if (!m_value_sp)278return;279llvm::StringRef desc = GetDescription();280281if (desc.empty())282return;283284StreamString qualified_name;285const OptionValueProperties *sub_properties = m_value_sp->GetAsProperties();286if (sub_properties) {287strm.EOL();288289if (m_value_sp->DumpQualifiedName(qualified_name))290strm.Printf("'%s' variables:\n\n", qualified_name.GetData());291sub_properties->DumpAllDescriptions(interpreter, strm);292} else {293if (display_qualified_name) {294StreamString qualified_name;295DumpQualifiedName(qualified_name);296interpreter.OutputFormattedHelpText(strm, qualified_name.GetString(),297"--", desc, output_width);298} else {299interpreter.OutputFormattedHelpText(strm, m_name, "--", desc,300output_width);301}302}303}304305void Property::SetValueChangedCallback(std::function<void()> callback) {306if (m_value_sp)307m_value_sp->SetValueChangedCallback(std::move(callback));308}309310311