1 //===-- OptionValueFileSpec.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/OptionValueFileSpec.h"
11 #include "lldb/DataFormatters/FormatManager.h"
12 #include "lldb/Host/FileSystem.h"
13 #include "lldb/Interpreter/CommandCompletions.h"
14 #include "lldb/Interpreter/CommandInterpreter.h"
15 #include "lldb/Utility/Args.h"
16 #include "lldb/Utility/State.h"
19 using namespace lldb_private
;
21 OptionValueFileSpec::OptionValueFileSpec(bool resolve
) : m_resolve(resolve
) {}
23 OptionValueFileSpec::OptionValueFileSpec(const FileSpec
&value
, bool resolve
)
24 : m_current_value(value
), m_default_value(value
),
28 OptionValueFileSpec::OptionValueFileSpec(const FileSpec
¤t_value
,
29 const FileSpec
&default_value
,
31 : m_current_value(current_value
), m_default_value(default_value
),
35 void OptionValueFileSpec::DumpValue(const ExecutionContext
*exe_ctx
,
36 Stream
&strm
, uint32_t dump_mask
) {
37 if (dump_mask
& eDumpOptionType
)
38 strm
.Printf("(%s)", GetTypeAsCString());
39 if (dump_mask
& eDumpOptionValue
) {
40 if (dump_mask
& eDumpOptionType
)
41 strm
.PutCString(" = ");
43 if (m_current_value
) {
44 strm
<< '"' << m_current_value
.GetPath().c_str() << '"';
49 Status
OptionValueFileSpec::SetValueFromString(llvm::StringRef value
,
50 VarSetOperationType op
) {
53 case eVarSetOperationClear
:
58 case eVarSetOperationReplace
:
59 case eVarSetOperationAssign
:
60 if (value
.size() > 0) {
61 value
= value
.trim("\"' \t");
62 m_value_was_set
= true;
63 m_current_value
.SetFile(value
.str(), FileSpec::Style::native
);
65 FileSystem::Instance().Resolve(m_current_value
);
67 m_data_mod_time
= llvm::sys::TimePoint
<>();
70 error
= Status::FromErrorString("invalid value string");
74 case eVarSetOperationInsertBefore
:
75 case eVarSetOperationInsertAfter
:
76 case eVarSetOperationRemove
:
77 case eVarSetOperationAppend
:
78 case eVarSetOperationInvalid
:
79 error
= OptionValue::SetValueFromString(value
, op
);
85 void OptionValueFileSpec::AutoComplete(CommandInterpreter
&interpreter
,
86 CompletionRequest
&request
) {
87 lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
88 interpreter
, m_completion_mask
, request
, nullptr);
91 const lldb::DataBufferSP
&OptionValueFileSpec::GetFileContents() {
92 if (m_current_value
) {
93 const auto file_mod_time
= FileSystem::Instance().GetModificationTime(m_current_value
);
94 if (m_data_sp
&& m_data_mod_time
== file_mod_time
)
97 FileSystem::Instance().CreateDataBuffer(m_current_value
.GetPath());
98 m_data_mod_time
= file_mod_time
;