Path: blob/main/contrib/llvm-project/lldb/source/Interpreter/OptionValueFormatEntity.cpp
39587 views
//===-- OptionValueFormatEntity.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/OptionValueFormatEntity.h"910#include "lldb/Core/Module.h"11#include "lldb/Interpreter/CommandInterpreter.h"12#include "lldb/Utility/Stream.h"13#include "lldb/Utility/StringList.h"14using namespace lldb;15using namespace lldb_private;1617OptionValueFormatEntity::OptionValueFormatEntity(const char *default_format) {18if (default_format && default_format[0]) {19llvm::StringRef default_format_str(default_format);20Status error = FormatEntity::Parse(default_format_str, m_default_entry);21if (error.Success()) {22m_default_format = default_format;23m_current_format = default_format;24m_current_entry = m_default_entry;25}26}27}2829void OptionValueFormatEntity::Clear() {30m_current_entry = m_default_entry;31m_current_format = m_default_format;32m_value_was_set = false;33}3435static void EscapeBackticks(llvm::StringRef str, std::string &dst) {36dst.clear();37dst.reserve(str.size());3839for (size_t i = 0, e = str.size(); i != e; ++i) {40char c = str[i];41if (c == '`') {42if (i == 0 || str[i - 1] != '\\')43dst += '\\';44}45dst += c;46}47}4849void OptionValueFormatEntity::DumpValue(const ExecutionContext *exe_ctx,50Stream &strm, uint32_t dump_mask) {51if (dump_mask & eDumpOptionType)52strm.Printf("(%s)", GetTypeAsCString());53if (dump_mask & eDumpOptionValue) {54if (dump_mask & eDumpOptionType)55strm.PutCString(" = ");56std::string escaped;57EscapeBackticks(m_current_format, escaped);58strm << '"' << escaped << '"';59}60}6162llvm::json::Value63OptionValueFormatEntity::ToJSON(const ExecutionContext *exe_ctx) {64std::string escaped;65EscapeBackticks(m_current_format, escaped);66return escaped;67}6869Status OptionValueFormatEntity::SetValueFromString(llvm::StringRef value_str,70VarSetOperationType op) {71Status error;72switch (op) {73case eVarSetOperationClear:74Clear();75NotifyValueChanged();76break;7778case eVarSetOperationReplace:79case eVarSetOperationAssign: {80// Check if the string starts with a quote character after removing leading81// and trailing spaces. If it does start with a quote character, make sure82// it ends with the same quote character and remove the quotes before we83// parse the format string. If the string doesn't start with a quote, leave84// the string alone and parse as is.85llvm::StringRef trimmed_value_str = value_str.trim();86if (!trimmed_value_str.empty()) {87const char first_char = trimmed_value_str[0];88if (first_char == '"' || first_char == '\'') {89const size_t trimmed_len = trimmed_value_str.size();90if (trimmed_len == 1 || value_str[trimmed_len - 1] != first_char) {91error.SetErrorString("mismatched quotes");92return error;93}94value_str = trimmed_value_str.substr(1, trimmed_len - 2);95}96}97FormatEntity::Entry entry;98error = FormatEntity::Parse(value_str, entry);99if (error.Success()) {100m_current_entry = std::move(entry);101m_current_format = std::string(value_str);102m_value_was_set = true;103NotifyValueChanged();104}105} break;106107case eVarSetOperationInsertBefore:108case eVarSetOperationInsertAfter:109case eVarSetOperationRemove:110case eVarSetOperationAppend:111case eVarSetOperationInvalid:112error = OptionValue::SetValueFromString(value_str, op);113break;114}115return error;116}117118void OptionValueFormatEntity::AutoComplete(CommandInterpreter &interpreter,119CompletionRequest &request) {120FormatEntity::AutoComplete(request);121}122123124