xref: /freebsd/contrib/llvm-project/lldb/source/Interpreter/OptionValueFormatEntity.cpp (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
1 //===-- OptionValueFormatEntity.cpp ---------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Interpreter/OptionValueFormatEntity.h"
10 
11 #include "lldb/Core/Module.h"
12 #include "lldb/Interpreter/CommandInterpreter.h"
13 #include "lldb/Utility/Stream.h"
14 #include "lldb/Utility/StringList.h"
15 using namespace lldb;
16 using namespace lldb_private;
17 
18 OptionValueFormatEntity::OptionValueFormatEntity(const char *default_format)
19     : OptionValue(), m_current_format(), m_default_format(), m_current_entry(),
20       m_default_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;
28     }
29   }
30 }
31 
32 bool OptionValueFormatEntity::Clear() {
33   m_current_entry = m_default_entry;
34   m_current_format = m_default_format;
35   m_value_was_set = false;
36   return true;
37 }
38 
39 static void EscapeBackticks(llvm::StringRef str, std::string &dst) {
40   dst.clear();
41   dst.reserve(str.size());
42 
43   for (size_t i = 0, e = str.size(); i != e; ++i) {
44     char c = str[i];
45     if (c == '`') {
46       if (i == 0 || str[i - 1] != '\\')
47         dst += '\\';
48     }
49     dst += c;
50   }
51 }
52 
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(" = ");
60     std::string escaped;
61     EscapeBackticks(m_current_format, escaped);
62     strm << '"' << escaped << '"';
63   }
64 }
65 
66 Status OptionValueFormatEntity::SetValueFromString(llvm::StringRef value_str,
67                                                    VarSetOperationType op) {
68   Status error;
69   switch (op) {
70   case eVarSetOperationClear:
71     Clear();
72     NotifyValueChanged();
73     break;
74 
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");
89           return error;
90         }
91         value_str = trimmed_value_str.substr(1, trimmed_len - 2);
92       }
93     }
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 = std::string(value_str);
99       m_value_was_set = true;
100       NotifyValueChanged();
101     }
102   } break;
103 
104   case eVarSetOperationInsertBefore:
105   case eVarSetOperationInsertAfter:
106   case eVarSetOperationRemove:
107   case eVarSetOperationAppend:
108   case eVarSetOperationInvalid:
109     error = OptionValue::SetValueFromString(value_str, op);
110     break;
111   }
112   return error;
113 }
114 
115 lldb::OptionValueSP OptionValueFormatEntity::DeepCopy() const {
116   return OptionValueSP(new OptionValueFormatEntity(*this));
117 }
118 
119 void OptionValueFormatEntity::AutoComplete(CommandInterpreter &interpreter,
120                                            CompletionRequest &request) {
121   FormatEntity::AutoComplete(request);
122 }
123