1 //===-- OptionValueFormatEntity.cpp -----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Interpreter/OptionValueFormatEntity.h"
12 #include "lldb/Core/Module.h"
13 #include "lldb/Interpreter/CommandInterpreter.h"
14 #include "lldb/Utility/Stream.h"
15 #include "lldb/Utility/StringList.h"
17 using namespace lldb_private;
19 OptionValueFormatEntity::OptionValueFormatEntity(const char *default_format)
20 : OptionValue(), m_current_format(), m_default_format(), m_current_entry(),
22 if (default_format && default_format[0]) {
23 llvm::StringRef default_format_str(default_format);
24 Status error = FormatEntity::Parse(default_format_str, m_default_entry);
25 if (error.Success()) {
26 m_default_format = default_format;
27 m_current_format = default_format;
28 m_current_entry = m_default_entry;
33 bool OptionValueFormatEntity::Clear() {
34 m_current_entry = m_default_entry;
35 m_current_format = m_default_format;
36 m_value_was_set = false;
40 static void EscapeBackticks(llvm::StringRef str, std::string &dst) {
42 dst.reserve(str.size());
44 for (size_t i = 0, e = str.size(); i != e; ++i) {
47 if (i == 0 || str[i - 1] != '\\')
54 void OptionValueFormatEntity::DumpValue(const ExecutionContext *exe_ctx,
55 Stream &strm, uint32_t dump_mask) {
56 if (dump_mask & eDumpOptionType)
57 strm.Printf("(%s)", GetTypeAsCString());
58 if (dump_mask & eDumpOptionValue) {
59 if (dump_mask & eDumpOptionType)
60 strm.PutCString(" = ");
62 EscapeBackticks(m_current_format, escaped);
63 strm << '"' << escaped << '"';
67 Status OptionValueFormatEntity::SetValueFromString(llvm::StringRef value_str,
68 VarSetOperationType op) {
71 case eVarSetOperationClear:
76 case eVarSetOperationReplace:
77 case eVarSetOperationAssign: {
78 // Check if the string starts with a quote character after removing leading
79 // and trailing spaces. If it does start with a quote character, make sure
80 // it ends with the same quote character and remove the quotes before we
81 // parse the format string. If the string doesn't start with a quote, leave
82 // the string alone and parse as is.
83 llvm::StringRef trimmed_value_str = value_str.trim();
84 if (!trimmed_value_str.empty()) {
85 const char first_char = trimmed_value_str[0];
86 if (first_char == '"' || first_char == '\'') {
87 const size_t trimmed_len = trimmed_value_str.size();
88 if (trimmed_len == 1 || value_str[trimmed_len - 1] != first_char) {
89 error.SetErrorStringWithFormat("mismatched quotes");
92 value_str = trimmed_value_str.substr(1, trimmed_len - 2);
95 FormatEntity::Entry entry;
96 error = FormatEntity::Parse(value_str, entry);
97 if (error.Success()) {
98 m_current_entry = std::move(entry);
99 m_current_format = value_str;
100 m_value_was_set = true;
101 NotifyValueChanged();
105 case eVarSetOperationInsertBefore:
106 case eVarSetOperationInsertAfter:
107 case eVarSetOperationRemove:
108 case eVarSetOperationAppend:
109 case eVarSetOperationInvalid:
110 error = OptionValue::SetValueFromString(value_str, op);
116 lldb::OptionValueSP OptionValueFormatEntity::DeepCopy() const {
117 return OptionValueSP(new OptionValueFormatEntity(*this));
120 size_t OptionValueFormatEntity::AutoComplete(CommandInterpreter &interpreter,
121 CompletionRequest &request) {
122 return FormatEntity::AutoComplete(request);