1 //===-- OptionValueFormatEntity.cpp -----------------------------*- C++ -*-===//
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-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Interpreter/OptionValueFormatEntity.h"
11 #include "lldb/Core/Module.h"
12 #include "lldb/Interpreter/CommandInterpreter.h"
13 #include "lldb/Utility/Stream.h"
14 #include "lldb/Utility/StringList.h"
16 using namespace lldb_private;
18 OptionValueFormatEntity::OptionValueFormatEntity(const char *default_format)
19 : OptionValue(), m_current_format(), m_default_format(), m_current_entry(),
21 if (default_format && default_format[0]) {
22 llvm::StringRef default_format_str(default_format);
23 Status error = FormatEntity::Parse(default_format_str, m_default_entry);
24 if (error.Success()) {
25 m_default_format = default_format;
26 m_current_format = default_format;
27 m_current_entry = m_default_entry;
32 bool OptionValueFormatEntity::Clear() {
33 m_current_entry = m_default_entry;
34 m_current_format = m_default_format;
35 m_value_was_set = false;
39 static void EscapeBackticks(llvm::StringRef str, std::string &dst) {
41 dst.reserve(str.size());
43 for (size_t i = 0, e = str.size(); i != e; ++i) {
46 if (i == 0 || str[i - 1] != '\\')
53 void OptionValueFormatEntity::DumpValue(const ExecutionContext *exe_ctx,
54 Stream &strm, uint32_t dump_mask) {
55 if (dump_mask & eDumpOptionType)
56 strm.Printf("(%s)", GetTypeAsCString());
57 if (dump_mask & eDumpOptionValue) {
58 if (dump_mask & eDumpOptionType)
59 strm.PutCString(" = ");
61 EscapeBackticks(m_current_format, escaped);
62 strm << '"' << escaped << '"';
66 Status OptionValueFormatEntity::SetValueFromString(llvm::StringRef value_str,
67 VarSetOperationType op) {
70 case eVarSetOperationClear:
75 case eVarSetOperationReplace:
76 case eVarSetOperationAssign: {
77 // Check if the string starts with a quote character after removing leading
78 // and trailing spaces. If it does start with a quote character, make sure
79 // it ends with the same quote character and remove the quotes before we
80 // parse the format string. If the string doesn't start with a quote, leave
81 // the string alone and parse as is.
82 llvm::StringRef trimmed_value_str = value_str.trim();
83 if (!trimmed_value_str.empty()) {
84 const char first_char = trimmed_value_str[0];
85 if (first_char == '"' || first_char == '\'') {
86 const size_t trimmed_len = trimmed_value_str.size();
87 if (trimmed_len == 1 || value_str[trimmed_len - 1] != first_char) {
88 error.SetErrorStringWithFormat("mismatched quotes");
91 value_str = trimmed_value_str.substr(1, trimmed_len - 2);
94 FormatEntity::Entry entry;
95 error = FormatEntity::Parse(value_str, entry);
96 if (error.Success()) {
97 m_current_entry = std::move(entry);
98 m_current_format = value_str;
99 m_value_was_set = true;
100 NotifyValueChanged();
104 case eVarSetOperationInsertBefore:
105 case eVarSetOperationInsertAfter:
106 case eVarSetOperationRemove:
107 case eVarSetOperationAppend:
108 case eVarSetOperationInvalid:
109 error = OptionValue::SetValueFromString(value_str, op);
115 lldb::OptionValueSP OptionValueFormatEntity::DeepCopy() const {
116 return OptionValueSP(new OptionValueFormatEntity(*this));
119 void OptionValueFormatEntity::AutoComplete(CommandInterpreter &interpreter,
120 CompletionRequest &request) {
121 FormatEntity::AutoComplete(request);