blob: 1bb8c9f6955ab2461fde984500116671ec47292b [file] [log] [blame]
Greg Clayton554f68d2015-02-04 22:00:53 +00001//===-- OptionValueFormatEntity.cpp -----------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Greg Clayton554f68d2015-02-04 22:00:53 +00006//
7//===----------------------------------------------------------------------===//
8
Greg Clayton554f68d2015-02-04 22:00:53 +00009#include "lldb/Interpreter/OptionValueFormatEntity.h"
10
Greg Clayton554f68d2015-02-04 22:00:53 +000011#include "lldb/Core/Module.h"
Greg Clayton554f68d2015-02-04 22:00:53 +000012#include "lldb/Interpreter/CommandInterpreter.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000013#include "lldb/Utility/Stream.h"
Zachary Turner573ab902017-03-21 18:25:04 +000014#include "lldb/Utility/StringList.h"
Greg Clayton554f68d2015-02-04 22:00:53 +000015using namespace lldb;
16using namespace lldb_private;
17
Kate Stoneb9c1b512016-09-06 20:57:50 +000018OptionValueFormatEntity::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);
Zachary Turner97206d52017-05-12 04:51:55 +000023 Status error = FormatEntity::Parse(default_format_str, m_default_entry);
Kate Stoneb9c1b512016-09-06 20:57:50 +000024 if (error.Success()) {
25 m_default_format = default_format;
26 m_current_format = default_format;
27 m_current_entry = m_default_entry;
Greg Clayton554f68d2015-02-04 22:00:53 +000028 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000029 }
Greg Clayton554f68d2015-02-04 22:00:53 +000030}
31
Kate Stoneb9c1b512016-09-06 20:57:50 +000032bool 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;
Greg Clayton554f68d2015-02-04 22:00:53 +000037}
38
Jonas Devlieghere64011592018-10-01 13:22:24 +000039static 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
Kate Stoneb9c1b512016-09-06 20:57:50 +000053void 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) {
Greg Clayton554f68d2015-02-04 22:00:53 +000058 if (dump_mask & eDumpOptionType)
Jonas Devlieghereb76e25a2018-10-26 00:00:17 +000059 strm.PutCString(" = ");
Jonas Devlieghere64011592018-10-01 13:22:24 +000060 std::string escaped;
61 EscapeBackticks(m_current_format, escaped);
Jonas Devlieghereb76e25a2018-10-26 00:00:17 +000062 strm << '"' << escaped << '"';
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 }
64}
65
Zachary Turner97206d52017-05-12 04:51:55 +000066Status OptionValueFormatEntity::SetValueFromString(llvm::StringRef value_str,
67 VarSetOperationType op) {
68 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +000069 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
Adrian Prantl05097242018-04-30 16:49:04 +000078 // 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.
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 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 }
Greg Clayton554f68d2015-02-04 22:00:53 +000093 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000094 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();
Greg Clayton554f68d2015-02-04 22:00:53 +0000101 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102 } 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;
Greg Clayton554f68d2015-02-04 22:00:53 +0000113}
114
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115lldb::OptionValueSP OptionValueFormatEntity::DeepCopy() const {
116 return OptionValueSP(new OptionValueFormatEntity(*this));
Greg Clayton554f68d2015-02-04 22:00:53 +0000117}
118
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000119size_t OptionValueFormatEntity::AutoComplete(CommandInterpreter &interpreter,
120 CompletionRequest &request) {
121 return FormatEntity::AutoComplete(request);
Greg Clayton554f68d2015-02-04 22:00:53 +0000122}