| 1 | //===-- OptionValueFileSpec.cpp -------------------------------------------===// |
| 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 | |
| 18 | using namespace lldb; |
| 19 | using namespace lldb_private; |
| 20 | |
| 21 | OptionValueFileSpec::OptionValueFileSpec(bool resolve) : m_resolve(resolve) {} |
| 22 | |
| 23 | OptionValueFileSpec::OptionValueFileSpec(const FileSpec &value, bool resolve) |
| 24 | : m_current_value(value), m_default_value(value), |
| 25 | |
| 26 | m_resolve(resolve) {} |
| 27 | |
| 28 | OptionValueFileSpec::OptionValueFileSpec(const FileSpec ¤t_value, |
| 29 | const FileSpec &default_value, |
| 30 | bool resolve) |
| 31 | : m_current_value(current_value), m_default_value(default_value), |
| 32 | |
| 33 | m_resolve(resolve) {} |
| 34 | |
| 35 | void OptionValueFileSpec::DumpValue(const ExecutionContext *exe_ctx, |
| 36 | Stream &strm, uint32_t dump_mask) { |
| 37 | if (dump_mask & eDumpOptionType) |
| 38 | strm.Printf(format: "(%s)" , GetTypeAsCString()); |
| 39 | if (dump_mask & eDumpOptionValue) { |
| 40 | if (dump_mask & eDumpOptionType) |
| 41 | strm.PutCString(cstr: " = " ); |
| 42 | |
| 43 | if (m_current_value) { |
| 44 | strm << '"' << m_current_value.GetPath().c_str() << '"'; |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | Status OptionValueFileSpec::SetValueFromString(llvm::StringRef value, |
| 50 | VarSetOperationType op) { |
| 51 | Status error; |
| 52 | switch (op) { |
| 53 | case eVarSetOperationClear: |
| 54 | Clear(); |
| 55 | NotifyValueChanged(); |
| 56 | break; |
| 57 | |
| 58 | case eVarSetOperationReplace: |
| 59 | case eVarSetOperationAssign: |
| 60 | if (value.size() > 0) { |
| 61 | value = value.trim(Chars: "\"' \t" ); |
| 62 | m_value_was_set = true; |
| 63 | m_current_value.SetFile(path: value.str(), style: FileSpec::Style::native); |
| 64 | if (m_resolve) |
| 65 | FileSystem::Instance().Resolve(file_spec&: m_current_value); |
| 66 | m_data_sp.reset(); |
| 67 | m_data_mod_time = llvm::sys::TimePoint<>(); |
| 68 | NotifyValueChanged(); |
| 69 | } else { |
| 70 | error = Status::FromErrorString(str: "invalid value string" ); |
| 71 | } |
| 72 | break; |
| 73 | |
| 74 | case eVarSetOperationInsertBefore: |
| 75 | case eVarSetOperationInsertAfter: |
| 76 | case eVarSetOperationRemove: |
| 77 | case eVarSetOperationAppend: |
| 78 | case eVarSetOperationInvalid: |
| 79 | error = OptionValue::SetValueFromString(value, op); |
| 80 | break; |
| 81 | } |
| 82 | return error; |
| 83 | } |
| 84 | |
| 85 | void OptionValueFileSpec::AutoComplete(CommandInterpreter &interpreter, |
| 86 | CompletionRequest &request) { |
| 87 | lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks( |
| 88 | interpreter, completion_mask: m_completion_mask, request, searcher: nullptr); |
| 89 | } |
| 90 | |
| 91 | const lldb::DataBufferSP &OptionValueFileSpec::GetFileContents() { |
| 92 | if (m_current_value) { |
| 93 | const auto file_mod_time = FileSystem::Instance().GetModificationTime(file_spec: m_current_value); |
| 94 | if (m_data_sp && m_data_mod_time == file_mod_time) |
| 95 | return m_data_sp; |
| 96 | m_data_sp = |
| 97 | FileSystem::Instance().CreateDataBuffer(path: m_current_value.GetPath()); |
| 98 | m_data_mod_time = file_mod_time; |
| 99 | } |
| 100 | return m_data_sp; |
| 101 | } |
| 102 | |