1 //===-- OptionValueFileSpec.cpp ---------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Interpreter/OptionValueFileSpec.h"
12 #include "lldb/Core/State.h"
13 #include "lldb/DataFormatters/FormatManager.h"
14 #include "lldb/Host/FileSystem.h"
15 #include "lldb/Interpreter/Args.h"
16 #include "lldb/Interpreter/CommandCompletions.h"
17 #include "lldb/Interpreter/CommandInterpreter.h"
18 #include "lldb/Utility/DataBufferLLVM.h"
21 using namespace lldb_private;
23 OptionValueFileSpec::OptionValueFileSpec(bool resolve)
24 : OptionValue(), m_current_value(), m_default_value(), m_data_sp(),
26 m_completion_mask(CommandCompletions::eDiskFileCompletion),
29 OptionValueFileSpec::OptionValueFileSpec(const FileSpec &value, bool resolve)
30 : OptionValue(), m_current_value(value), m_default_value(value),
31 m_data_sp(), m_data_mod_time(),
32 m_completion_mask(CommandCompletions::eDiskFileCompletion),
35 OptionValueFileSpec::OptionValueFileSpec(const FileSpec ¤t_value,
36 const FileSpec &default_value,
38 : OptionValue(), m_current_value(current_value),
39 m_default_value(default_value), m_data_sp(), m_data_mod_time(),
40 m_completion_mask(CommandCompletions::eDiskFileCompletion),
43 void OptionValueFileSpec::DumpValue(const ExecutionContext *exe_ctx,
44 Stream &strm, uint32_t dump_mask) {
45 if (dump_mask & eDumpOptionType)
46 strm.Printf("(%s)", GetTypeAsCString());
47 if (dump_mask & eDumpOptionValue) {
48 if (dump_mask & eDumpOptionType)
49 strm.PutCString(" = ");
51 if (m_current_value) {
52 strm << '"' << m_current_value.GetPath().c_str() << '"';
57 Status OptionValueFileSpec::SetValueFromString(llvm::StringRef value,
58 VarSetOperationType op) {
61 case eVarSetOperationClear:
66 case eVarSetOperationReplace:
67 case eVarSetOperationAssign:
68 if (value.size() > 0) {
69 // The setting value may have whitespace, double-quotes, or single-quotes
71 // path to indicate that internal spaces are not word breaks. Strip off
73 // from the start and end of the file path - we aren't doing any word //
75 // the quoting is unnecessary. NB this will cause a problem if someone
77 // a file path that legitimately begins or ends with a " or ' character,
79 value = value.trim("\"' \t");
80 m_value_was_set = true;
81 m_current_value.SetFile(value.str(), m_resolve);
83 m_data_mod_time = llvm::sys::TimePoint<>();
86 error.SetErrorString("invalid value string");
90 case eVarSetOperationInsertBefore:
91 case eVarSetOperationInsertAfter:
92 case eVarSetOperationRemove:
93 case eVarSetOperationAppend:
94 case eVarSetOperationInvalid:
95 error = OptionValue::SetValueFromString(value, op);
101 lldb::OptionValueSP OptionValueFileSpec::DeepCopy() const {
102 return OptionValueSP(new OptionValueFileSpec(*this));
105 size_t OptionValueFileSpec::AutoComplete(
106 CommandInterpreter &interpreter, llvm::StringRef s, int match_start_point,
107 int max_return_elements, bool &word_complete, StringList &matches) {
108 word_complete = false;
110 CommandCompletions::InvokeCommonCompletionCallbacks(
111 interpreter, m_completion_mask, s, match_start_point, max_return_elements,
112 nullptr, word_complete, matches);
113 return matches.GetSize();
116 const lldb::DataBufferSP &OptionValueFileSpec::GetFileContents() {
117 if (m_current_value) {
118 const auto file_mod_time = FileSystem::GetModificationTime(m_current_value);
119 if (m_data_sp && m_data_mod_time == file_mod_time)
121 m_data_sp = DataBufferLLVM::CreateFromPath(m_current_value.GetPath());
122 m_data_mod_time = file_mod_time;