OptionValueFileSpec.cpp revision 360784
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
9#include "lldb/Interpreter/OptionValueFileSpec.h"
10
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"
17
18using namespace lldb;
19using namespace lldb_private;
20
21OptionValueFileSpec::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
27OptionValueFileSpec::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),
31      m_resolve(resolve) {}
32
33OptionValueFileSpec::OptionValueFileSpec(const FileSpec &current_value,
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
41void 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(" = ");
48
49    if (m_current_value) {
50      strm << '"' << m_current_value.GetPath().c_str() << '"';
51    }
52  }
53}
54
55Status OptionValueFileSpec::SetValueFromString(llvm::StringRef value,
56                                               VarSetOperationType op) {
57  Status error;
58  switch (op) {
59  case eVarSetOperationClear:
60    Clear();
61    NotifyValueChanged();
62    break;
63
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,
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)
78        FileSystem::Instance().Resolve(m_current_value);
79      m_data_sp.reset();
80      m_data_mod_time = llvm::sys::TimePoint<>();
81      NotifyValueChanged();
82    } else {
83      error.SetErrorString("invalid value string");
84    }
85    break;
86
87  case eVarSetOperationInsertBefore:
88  case eVarSetOperationInsertAfter:
89  case eVarSetOperationRemove:
90  case eVarSetOperationAppend:
91  case eVarSetOperationInvalid:
92    error = OptionValue::SetValueFromString(value, op);
93    break;
94  }
95  return error;
96}
97
98lldb::OptionValueSP OptionValueFileSpec::DeepCopy() const {
99  return OptionValueSP(new OptionValueFileSpec(*this));
100}
101
102void OptionValueFileSpec::AutoComplete(CommandInterpreter &interpreter,
103                                       CompletionRequest &request) {
104  CommandCompletions::InvokeCommonCompletionCallbacks(
105      interpreter, m_completion_mask, request, nullptr);
106}
107
108const lldb::DataBufferSP &OptionValueFileSpec::GetFileContents() {
109  if (m_current_value) {
110    const auto file_mod_time = FileSystem::Instance().GetModificationTime(m_current_value);
111    if (m_data_sp && m_data_mod_time == file_mod_time)
112      return m_data_sp;
113    m_data_sp =
114        FileSystem::Instance().CreateDataBuffer(m_current_value.GetPath());
115    m_data_mod_time = file_mod_time;
116  }
117  return m_data_sp;
118}
119