blob: 0df581af5d67bc40aa4ca321c4e66fef8147f9a5 [file] [log] [blame]
Greg Clayton67cc0632012-08-22 17:17:09 +00001//===-- OptionValueFileSpec.cpp ---------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Interpreter/OptionValueFileSpec.h"
11
Greg Clayton67cc0632012-08-22 17:17:09 +000012#include "lldb/Core/State.h"
Enrico Granata5548cb52013-01-28 23:47:25 +000013#include "lldb/DataFormatters/FormatManager.h"
Pavel Labath1408bf72016-11-01 16:11:14 +000014#include "lldb/Host/FileSystem.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000015#include "lldb/Interpreter/Args.h"
16#include "lldb/Interpreter/CommandCompletions.h"
Todd Fialae1cfbc72016-08-11 23:51:28 +000017#include "lldb/Interpreter/CommandInterpreter.h"
Zachary Turner7f6a7a32017-03-06 23:42:14 +000018#include "lldb/Utility/DataBufferLLVM.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000019
20using namespace lldb;
21using namespace lldb_private;
22
Kate Stoneb9c1b512016-09-06 20:57:50 +000023OptionValueFileSpec::OptionValueFileSpec(bool resolve)
24 : OptionValue(), m_current_value(), m_default_value(), m_data_sp(),
25 m_data_mod_time(),
26 m_completion_mask(CommandCompletions::eDiskFileCompletion),
27 m_resolve(resolve) {}
Greg Claytonb5f0fea2012-09-27 22:26:11 +000028
Kate Stoneb9c1b512016-09-06 20:57:50 +000029OptionValueFileSpec::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),
33 m_resolve(resolve) {}
Greg Claytonb5f0fea2012-09-27 22:26:11 +000034
Kate Stoneb9c1b512016-09-06 20:57:50 +000035OptionValueFileSpec::OptionValueFileSpec(const FileSpec &current_value,
36 const FileSpec &default_value,
37 bool resolve)
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),
41 m_resolve(resolve) {}
Greg Claytonb5f0fea2012-09-27 22:26:11 +000042
Kate Stoneb9c1b512016-09-06 20:57:50 +000043void 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) {
Greg Clayton67cc0632012-08-22 17:17:09 +000048 if (dump_mask & eDumpOptionType)
Kate Stoneb9c1b512016-09-06 20:57:50 +000049 strm.PutCString(" = ");
Greg Clayton67cc0632012-08-22 17:17:09 +000050
Kate Stoneb9c1b512016-09-06 20:57:50 +000051 if (m_current_value) {
52 strm << '"' << m_current_value.GetPath().c_str() << '"';
Greg Clayton67cc0632012-08-22 17:17:09 +000053 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000054 }
Greg Clayton67cc0632012-08-22 17:17:09 +000055}
56
Kate Stoneb9c1b512016-09-06 20:57:50 +000057Error OptionValueFileSpec::SetValueFromString(llvm::StringRef value,
58 VarSetOperationType op) {
59 Error error;
60 switch (op) {
61 case eVarSetOperationClear:
62 Clear();
63 NotifyValueChanged();
64 break;
65
66 case eVarSetOperationReplace:
67 case eVarSetOperationAssign:
68 if (value.size() > 0) {
69 // The setting value may have whitespace, double-quotes, or single-quotes
70 // around the file
71 // path to indicate that internal spaces are not word breaks. Strip off
72 // any ws & quotes
73 // from the start and end of the file path - we aren't doing any word //
74 // breaking here so
75 // the quoting is unnecessary. NB this will cause a problem if someone
76 // tries to specify
77 // a file path that legitimately begins or ends with a " or ' character,
78 // or whitespace.
79 value = value.trim("\"' \t");
80 m_value_was_set = true;
Malcolm Parsons771ef6d2016-11-02 20:34:10 +000081 m_current_value.SetFile(value.str(), m_resolve);
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 m_data_sp.reset();
Pavel Labathe877baa2016-11-09 14:33:22 +000083 m_data_mod_time = llvm::sys::TimePoint<>();
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 NotifyValueChanged();
85 } else {
86 error.SetErrorString("invalid value string");
Greg Clayton67cc0632012-08-22 17:17:09 +000087 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 break;
89
90 case eVarSetOperationInsertBefore:
91 case eVarSetOperationInsertAfter:
92 case eVarSetOperationRemove:
93 case eVarSetOperationAppend:
94 case eVarSetOperationInvalid:
95 error = OptionValue::SetValueFromString(value, op);
96 break;
97 }
98 return error;
Greg Clayton67cc0632012-08-22 17:17:09 +000099}
100
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101lldb::OptionValueSP OptionValueFileSpec::DeepCopy() const {
102 return OptionValueSP(new OptionValueFileSpec(*this));
Greg Clayton67cc0632012-08-22 17:17:09 +0000103}
104
Zachary Turner4aa87532016-11-17 01:37:42 +0000105size_t OptionValueFileSpec::AutoComplete(
106 CommandInterpreter &interpreter, llvm::StringRef s, int match_start_point,
107 int max_return_elements, bool &word_complete, StringList &matches) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108 word_complete = false;
109 matches.Clear();
110 CommandCompletions::InvokeCommonCompletionCallbacks(
111 interpreter, m_completion_mask, s, match_start_point, max_return_elements,
112 nullptr, word_complete, matches);
113 return matches.GetSize();
Greg Clayton67cc0632012-08-22 17:17:09 +0000114}
115
Greg Clayton6920b522012-08-22 18:39:03 +0000116const lldb::DataBufferSP &
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117OptionValueFileSpec::GetFileContents(bool null_terminate) {
118 if (m_current_value) {
Pavel Labathe877baa2016-11-09 14:33:22 +0000119 const auto file_mod_time = FileSystem::GetModificationTime(m_current_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120 if (m_data_sp && m_data_mod_time == file_mod_time)
121 return m_data_sp;
Zachary Turner7f6a7a32017-03-06 23:42:14 +0000122 m_data_sp = DataBufferLLVM::CreateFromPath(m_current_value.GetPath(),
123 null_terminate);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124 m_data_mod_time = file_mod_time;
125 }
126 return m_data_sp;
Greg Clayton6920b522012-08-22 18:39:03 +0000127}