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) 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 27 OptionValueFileSpec::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 33 OptionValueFileSpec::OptionValueFileSpec(const FileSpec ¤t_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 41 void 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 55 Status 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 value = value.trim("\"' \t"); 68 m_value_was_set = true; 69 m_current_value.SetFile(value.str(), FileSpec::Style::native); 70 if (m_resolve) 71 FileSystem::Instance().Resolve(m_current_value); 72 m_data_sp.reset(); 73 m_data_mod_time = llvm::sys::TimePoint<>(); 74 NotifyValueChanged(); 75 } else { 76 error.SetErrorString("invalid value string"); 77 } 78 break; 79 80 case eVarSetOperationInsertBefore: 81 case eVarSetOperationInsertAfter: 82 case eVarSetOperationRemove: 83 case eVarSetOperationAppend: 84 case eVarSetOperationInvalid: 85 error = OptionValue::SetValueFromString(value, op); 86 break; 87 } 88 return error; 89 } 90 91 lldb::OptionValueSP OptionValueFileSpec::DeepCopy() const { 92 return OptionValueSP(new OptionValueFileSpec(*this)); 93 } 94 95 void OptionValueFileSpec::AutoComplete(CommandInterpreter &interpreter, 96 CompletionRequest &request) { 97 CommandCompletions::InvokeCommonCompletionCallbacks( 98 interpreter, m_completion_mask, request, nullptr); 99 } 100 101 const lldb::DataBufferSP &OptionValueFileSpec::GetFileContents() { 102 if (m_current_value) { 103 const auto file_mod_time = FileSystem::Instance().GetModificationTime(m_current_value); 104 if (m_data_sp && m_data_mod_time == file_mod_time) 105 return m_data_sp; 106 m_data_sp = 107 FileSystem::Instance().CreateDataBuffer(m_current_value.GetPath()); 108 m_data_mod_time = file_mod_time; 109 } 110 return m_data_sp; 111 } 112