1 //===-- OptionValueString.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/OptionValueString.h"
11 #include "lldb/Host/OptionParser.h"
12 #include "lldb/Utility/Args.h"
13 #include "lldb/Utility/Stream.h"
16 using namespace lldb_private
;
18 void OptionValueString::DumpValue(const ExecutionContext
*exe_ctx
, Stream
&strm
,
20 if (dump_mask
& eDumpOptionType
)
21 strm
.Printf("(%s)", GetTypeAsCString());
22 if (dump_mask
& eDumpOptionValue
) {
23 if (dump_mask
& eDumpOptionType
)
24 strm
.PutCString(" = ");
25 if (!m_current_value
.empty() || m_value_was_set
) {
26 if (m_options
.Test(eOptionEncodeCharacterEscapeSequences
)) {
27 std::string expanded_escape_value
;
28 Args::ExpandEscapedCharacters(m_current_value
.c_str(),
29 expanded_escape_value
);
30 if (dump_mask
& eDumpOptionRaw
)
31 strm
.Printf("%s", expanded_escape_value
.c_str());
33 strm
.Printf("\"%s\"", expanded_escape_value
.c_str());
35 if (dump_mask
& eDumpOptionRaw
)
36 strm
.Printf("%s", m_current_value
.c_str());
38 strm
.Printf("\"%s\"", m_current_value
.c_str());
44 Status
OptionValueString::SetValueFromString(llvm::StringRef value
,
45 VarSetOperationType op
) {
48 std::string value_str
= value
.str();
50 if (value
.size() > 0) {
51 switch (value
.front()) {
54 if (value
.size() <= 1 || value
.back() != value
.front()) {
55 error
= Status::FromErrorString("mismatched quotes");
58 value
= value
.drop_front().drop_back();
61 value_str
= value
.str();
65 case eVarSetOperationInvalid
:
66 case eVarSetOperationInsertBefore
:
67 case eVarSetOperationInsertAfter
:
68 case eVarSetOperationRemove
:
70 error
= m_validator(value_str
.c_str(), m_validator_baton
);
74 error
= OptionValue::SetValueFromString(value
, op
);
77 case eVarSetOperationAppend
: {
78 std::string
new_value(m_current_value
);
79 if (value
.size() > 0) {
80 if (m_options
.Test(eOptionEncodeCharacterEscapeSequences
)) {
82 Args::EncodeEscapeSequences(value_str
.c_str(), str
);
83 new_value
.append(str
);
85 new_value
.append(std::string(value
));
88 error
= m_validator(new_value
.c_str(), m_validator_baton
);
92 m_current_value
.assign(new_value
);
96 case eVarSetOperationClear
:
101 case eVarSetOperationReplace
:
102 case eVarSetOperationAssign
:
104 error
= m_validator(value_str
.c_str(), m_validator_baton
);
108 m_value_was_set
= true;
109 if (m_options
.Test(eOptionEncodeCharacterEscapeSequences
)) {
110 Args::EncodeEscapeSequences(value_str
.c_str(), m_current_value
);
112 SetCurrentValue(value_str
);
114 NotifyValueChanged();
120 Status
OptionValueString::SetCurrentValue(llvm::StringRef value
) {
122 Status
error(m_validator(value
.str().c_str(), m_validator_baton
));
126 m_current_value
.assign(std::string(value
));
130 Status
OptionValueString::AppendToCurrentValue(const char *value
) {
131 if (value
&& value
[0]) {
133 std::string
new_value(m_current_value
);
134 new_value
.append(value
);
135 Status
error(m_validator(value
, m_validator_baton
));
138 m_current_value
.assign(new_value
);
140 m_current_value
.append(value
);