1 //===-- OptionValueFormatEntity.cpp ---------------------------------------===//
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 if (default_format
&& default_format
[0]) {
20 llvm::StringRef
default_format_str(default_format
);
21 Status error
= FormatEntity::Parse(default_format_str
, m_default_entry
);
22 if (error
.Success()) {
23 m_default_format
= default_format
;
24 m_current_format
= default_format
;
25 m_current_entry
= m_default_entry
;
30 void OptionValueFormatEntity::Clear() {
31 m_current_entry
= m_default_entry
;
32 m_current_format
= m_default_format
;
33 m_value_was_set
= false;
36 static void EscapeBackticks(llvm::StringRef str
, std::string
&dst
) {
38 dst
.reserve(str
.size());
40 for (size_t i
= 0, e
= str
.size(); i
!= e
; ++i
) {
43 if (i
== 0 || str
[i
- 1] != '\\')
50 void OptionValueFormatEntity::DumpValue(const ExecutionContext
*exe_ctx
,
51 Stream
&strm
, uint32_t dump_mask
) {
52 if (dump_mask
& eDumpOptionType
)
53 strm
.Printf("(%s)", GetTypeAsCString());
54 if (dump_mask
& eDumpOptionValue
) {
55 if (dump_mask
& eDumpOptionType
)
56 strm
.PutCString(" = ");
58 EscapeBackticks(m_current_format
, escaped
);
59 strm
<< '"' << escaped
<< '"';
64 OptionValueFormatEntity::ToJSON(const ExecutionContext
*exe_ctx
) {
66 EscapeBackticks(m_current_format
, escaped
);
70 Status
OptionValueFormatEntity::SetValueFromString(llvm::StringRef value_str
,
71 VarSetOperationType op
) {
74 case eVarSetOperationClear
:
79 case eVarSetOperationReplace
:
80 case eVarSetOperationAssign
: {
81 // Check if the string starts with a quote character after removing leading
82 // and trailing spaces. If it does start with a quote character, make sure
83 // it ends with the same quote character and remove the quotes before we
84 // parse the format string. If the string doesn't start with a quote, leave
85 // the string alone and parse as is.
86 llvm::StringRef trimmed_value_str
= value_str
.trim();
87 if (!trimmed_value_str
.empty()) {
88 const char first_char
= trimmed_value_str
[0];
89 if (first_char
== '"' || first_char
== '\'') {
90 const size_t trimmed_len
= trimmed_value_str
.size();
91 if (trimmed_len
== 1 || value_str
[trimmed_len
- 1] != first_char
) {
92 error
= Status::FromErrorString("mismatched quotes");
95 value_str
= trimmed_value_str
.substr(1, trimmed_len
- 2);
98 FormatEntity::Entry entry
;
99 error
= FormatEntity::Parse(value_str
, entry
);
100 if (error
.Success()) {
101 m_current_entry
= std::move(entry
);
102 m_current_format
= std::string(value_str
);
103 m_value_was_set
= true;
104 NotifyValueChanged();
108 case eVarSetOperationInsertBefore
:
109 case eVarSetOperationInsertAfter
:
110 case eVarSetOperationRemove
:
111 case eVarSetOperationAppend
:
112 case eVarSetOperationInvalid
:
113 error
= OptionValue::SetValueFromString(value_str
, op
);
119 void OptionValueFormatEntity::AutoComplete(CommandInterpreter
&interpreter
,
120 CompletionRequest
&request
) {
121 FormatEntity::AutoComplete(request
);