LLDB  mainline
OptionValueFileSpec.cpp
Go to the documentation of this file.
1 //===-- OptionValueFileSpec.cpp ---------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
10 
12 #include "lldb/Host/FileSystem.h"
15 #include "lldb/Utility/Args.h"
16 #include "lldb/Utility/State.h"
17 
18 using namespace lldb;
19 using namespace lldb_private;
20 
21 OptionValueFileSpec::OptionValueFileSpec(bool resolve)
22  : OptionValue(), m_current_value(), m_default_value(), m_data_sp(),
23  m_data_mod_time(),
24  m_completion_mask(CommandCompletions::eDiskFileCompletion),
25  m_resolve(resolve) {}
26 
28  : OptionValue(), m_current_value(value), m_default_value(value),
30  m_completion_mask(CommandCompletions::eDiskFileCompletion),
31  m_resolve(resolve) {}
32 
34  const FileSpec &default_value,
35  bool resolve)
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),
39  m_resolve(resolve) {}
40 
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(" = ");
48 
49  if (m_current_value) {
50  strm << '"' << m_current_value.GetPath().c_str() << '"';
51  }
52  }
53 }
54 
57  Status error;
58  switch (op) {
60  Clear();
62  break;
63 
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,
73  // or whitespace.
74  value = value.trim("\"' \t");
75  m_value_was_set = true;
76  m_current_value.SetFile(value.str(), FileSpec::Style::native);
77  if (m_resolve)
79  m_data_sp.reset();
80  m_data_mod_time = llvm::sys::TimePoint<>();
82  } else {
83  error.SetErrorString("invalid value string");
84  }
85  break;
86 
92  error = OptionValue::SetValueFromString(value, op);
93  break;
94  }
95  return error;
96 }
97 
98 lldb::OptionValueSP OptionValueFileSpec::DeepCopy() const {
99  return OptionValueSP(new OptionValueFileSpec(*this));
100 }
101 
103  CompletionRequest &request) {
104  request.SetWordComplete(false);
106  interpreter, m_completion_mask, request, nullptr);
107  return request.GetNumberOfMatches();
108 }
109 
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)
114  return m_data_sp;
115  m_data_sp =
117  m_data_mod_time = file_mod_time;
118  }
119  return m_data_sp;
120 }
static bool InvokeCommonCompletionCallbacks(CommandInterpreter &interpreter, uint32_t completion_mask, lldb_private::CompletionRequest &request, SearchFilter *searcher)
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:61
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
std::shared_ptr< DataBufferLLVM > CreateDataBuffer(const llvm::Twine &path, uint64_t size=0, uint64_t offset=0)
Create memory buffer from path.
A file utility class.
Definition: FileSpec.h:55
std::size_t GetNumberOfMatches() const
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
virtual Status SetValueFromString(llvm::StringRef value, VarSetOperationType op=eVarSetOperationAssign)
static FileSystem & Instance()
"lldb/Utility/ArgCompletionRequest.h"
lldb::OptionValueSP DeepCopy() const override
VarSetOperationType
Settable state variable types.
Status SetValueFromString(llvm::StringRef value, VarSetOperationType op=eVarSetOperationAssign) override
void SetErrorString(llvm::StringRef err_str)
Set the current error string to err_str.
Definition: Status.cpp:241
size_t AutoComplete(CommandInterpreter &interpreter, CompletionRequest &request) override
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
void Resolve(llvm::SmallVectorImpl< char > &path)
Resolve path to make it canonical.
const lldb::DataBufferSP & GetFileContents()
virtual const char * GetTypeAsCString() const
Definition: OptionValue.h:79
Definition: SBAddress.h:15
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
Definition: FileSpec.cpp:198
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:376
llvm::sys::TimePoint GetModificationTime(const FileSpec &file_spec) const
Returns the modification time of the given file.
void DumpValue(const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask) override
An error handling class.
Definition: Status.h:44