1 //===-- OptionValueFormatEntity.cpp -----------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/Interpreter/OptionValueFormatEntity.h"
11
12 #include "lldb/Core/Module.h"
13 #include "lldb/Interpreter/CommandInterpreter.h"
14 #include "lldb/Utility/Stream.h"
15 #include "lldb/Utility/StringList.h"
16 using namespace lldb;
17 using namespace lldb_private;
18
OptionValueFormatEntity(const char * default_format)19 OptionValueFormatEntity::OptionValueFormatEntity(const char *default_format)
20 : OptionValue(), m_current_format(), m_default_format(), m_current_entry(),
21 m_default_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;
29 }
30 }
31 }
32
Clear()33 bool OptionValueFormatEntity::Clear() {
34 m_current_entry = m_default_entry;
35 m_current_format = m_default_format;
36 m_value_was_set = false;
37 return true;
38 }
39
EscapeBackticks(llvm::StringRef str,std::string & dst)40 static void EscapeBackticks(llvm::StringRef str, std::string &dst) {
41 dst.clear();
42 dst.reserve(str.size());
43
44 for (size_t i = 0, e = str.size(); i != e; ++i) {
45 char c = str[i];
46 if (c == '`') {
47 if (i == 0 || str[i - 1] != '\\')
48 dst += '\\';
49 }
50 dst += c;
51 }
52 }
53
DumpValue(const ExecutionContext * exe_ctx,Stream & strm,uint32_t dump_mask)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(" = ");
61 std::string escaped;
62 EscapeBackticks(m_current_format, escaped);
63 strm << '"' << escaped << '"';
64 }
65 }
66
SetValueFromString(llvm::StringRef value_str,VarSetOperationType op)67 Status OptionValueFormatEntity::SetValueFromString(llvm::StringRef value_str,
68 VarSetOperationType op) {
69 Status error;
70 switch (op) {
71 case eVarSetOperationClear:
72 Clear();
73 NotifyValueChanged();
74 break;
75
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");
90 return error;
91 }
92 value_str = trimmed_value_str.substr(1, trimmed_len - 2);
93 }
94 }
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();
102 }
103 } break;
104
105 case eVarSetOperationInsertBefore:
106 case eVarSetOperationInsertAfter:
107 case eVarSetOperationRemove:
108 case eVarSetOperationAppend:
109 case eVarSetOperationInvalid:
110 error = OptionValue::SetValueFromString(value_str, op);
111 break;
112 }
113 return error;
114 }
115
DeepCopy() const116 lldb::OptionValueSP OptionValueFormatEntity::DeepCopy() const {
117 return OptionValueSP(new OptionValueFormatEntity(*this));
118 }
119
AutoComplete(CommandInterpreter & interpreter,CompletionRequest & request)120 size_t OptionValueFormatEntity::AutoComplete(CommandInterpreter &interpreter,
121 CompletionRequest &request) {
122 return FormatEntity::AutoComplete(request);
123 }
124