1 //===-- OptionValueFileSpec.cpp ---------------------------------*- C++ -*-===//
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)
22 : OptionValue(), m_current_value(), m_default_value(), m_data_sp(),
24 m_completion_mask(CommandCompletions::eDiskFileCompletion),
27 OptionValueFileSpec::OptionValueFileSpec(const FileSpec &value, bool resolve)
28 : OptionValue(), m_current_value(value), m_default_value(value),
29 m_data_sp(), m_data_mod_time(),
30 m_completion_mask(CommandCompletions::eDiskFileCompletion),
33 OptionValueFileSpec::OptionValueFileSpec(const FileSpec ¤t_value,
34 const FileSpec &default_value,
36 : OptionValue(), m_current_value(current_value),
37 m_default_value(default_value), m_data_sp(), m_data_mod_time(),
38 m_completion_mask(CommandCompletions::eDiskFileCompletion),
41 void OptionValueFileSpec::DumpValue(const ExecutionContext *exe_ctx,
42 Stream &strm, uint32_t dump_mask) {
43 if (dump_mask & eDumpOptionType)
44 strm.Printf("(%s)", GetTypeAsCString());
45 if (dump_mask & eDumpOptionValue) {
46 if (dump_mask & eDumpOptionType)
47 strm.PutCString(" = ");
49 if (m_current_value) {
50 strm << '"' << m_current_value.GetPath().c_str() << '"';
55 Status OptionValueFileSpec::SetValueFromString(llvm::StringRef value,
56 VarSetOperationType op) {
59 case eVarSetOperationClear:
64 case eVarSetOperationReplace:
65 case eVarSetOperationAssign:
66 if (value.size() > 0) {
67 // The setting value may have whitespace, double-quotes, or single-quotes
68 // around the file path to indicate that internal spaces are not word
69 // breaks. Strip off any ws & quotes from the start and end of the file
70 // path - we aren't doing any word // breaking here so the quoting is
71 // unnecessary. NB this will cause a problem if someone tries to specify
72 // a file path that legitimately begins or ends with a " or ' character,
74 value = value.trim("\"' \t");
75 m_value_was_set = true;
76 m_current_value.SetFile(value.str(), FileSpec::Style::native);
78 FileSystem::Instance().Resolve(m_current_value);
80 m_data_mod_time = llvm::sys::TimePoint<>();
83 error.SetErrorString("invalid value string");
87 case eVarSetOperationInsertBefore:
88 case eVarSetOperationInsertAfter:
89 case eVarSetOperationRemove:
90 case eVarSetOperationAppend:
91 case eVarSetOperationInvalid:
92 error = OptionValue::SetValueFromString(value, op);
98 lldb::OptionValueSP OptionValueFileSpec::DeepCopy() const {
99 return OptionValueSP(new OptionValueFileSpec(*this));
102 size_t OptionValueFileSpec::AutoComplete(CommandInterpreter &interpreter,
103 CompletionRequest &request) {
104 request.SetWordComplete(false);
105 CommandCompletions::InvokeCommonCompletionCallbacks(
106 interpreter, m_completion_mask, request, nullptr);
107 return request.GetNumberOfMatches();
110 const lldb::DataBufferSP &OptionValueFileSpec::GetFileContents() {
111 if (m_current_value) {
112 const auto file_mod_time = FileSystem::Instance().GetModificationTime(m_current_value);
113 if (m_data_sp && m_data_mod_time == file_mod_time)
116 FileSystem::Instance().CreateDataBuffer(m_current_value.GetPath());
117 m_data_mod_time = file_mod_time;