Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 1 | //===-- 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 Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 12 | #include "lldb/Core/State.h" |
Enrico Granata | 5548cb5 | 2013-01-28 23:47:25 +0000 | [diff] [blame] | 13 | #include "lldb/DataFormatters/FormatManager.h" |
Pavel Labath | 1408bf7 | 2016-11-01 16:11:14 +0000 | [diff] [blame] | 14 | #include "lldb/Host/FileSystem.h" |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 15 | #include "lldb/Interpreter/CommandCompletions.h" |
Todd Fiala | e1cfbc7 | 2016-08-11 23:51:28 +0000 | [diff] [blame] | 16 | #include "lldb/Interpreter/CommandInterpreter.h" |
Pavel Labath | 145d95c | 2018-04-17 18:53:35 +0000 | [diff] [blame] | 17 | #include "lldb/Utility/Args.h" |
Zachary Turner | 7f6a7a3 | 2017-03-06 23:42:14 +0000 | [diff] [blame] | 18 | #include "lldb/Utility/DataBufferLLVM.h" |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace lldb; |
| 21 | using namespace lldb_private; |
| 22 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 23 | OptionValueFileSpec::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 Clayton | b5f0fea | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 28 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 29 | OptionValueFileSpec::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 Clayton | b5f0fea | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 34 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 35 | OptionValueFileSpec::OptionValueFileSpec(const FileSpec ¤t_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 Clayton | b5f0fea | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 42 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 43 | void 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 Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 48 | if (dump_mask & eDumpOptionType) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 49 | strm.PutCString(" = "); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 50 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 51 | if (m_current_value) { |
| 52 | strm << '"' << m_current_value.GetPath().c_str() << '"'; |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 53 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 54 | } |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 57 | Status OptionValueFileSpec::SetValueFromString(llvm::StringRef value, |
| 58 | VarSetOperationType op) { |
| 59 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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 |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 70 | // around the file path to indicate that internal spaces are not word |
| 71 | // breaks. Strip off any ws & quotes from the start and end of the file |
| 72 | // path - we aren't doing any word // breaking here so the quoting is |
| 73 | // unnecessary. NB this will cause a problem if someone tries to specify |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | // a file path that legitimately begins or ends with a " or ' character, |
| 75 | // or whitespace. |
| 76 | value = value.trim("\"' \t"); |
| 77 | m_value_was_set = true; |
Jonas Devlieghere | 937348c | 2018-06-13 22:08:14 +0000 | [diff] [blame] | 78 | m_current_value.SetFile(value.str(), m_resolve, FileSpec::Style::native); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 79 | m_data_sp.reset(); |
Pavel Labath | e877baa | 2016-11-09 14:33:22 +0000 | [diff] [blame] | 80 | m_data_mod_time = llvm::sys::TimePoint<>(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 81 | NotifyValueChanged(); |
| 82 | } else { |
| 83 | error.SetErrorString("invalid value string"); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 84 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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; |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 98 | lldb::OptionValueSP OptionValueFileSpec::DeepCopy() const { |
| 99 | return OptionValueSP(new OptionValueFileSpec(*this)); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Raphael Isemann | a2e76c0 | 2018-07-13 18:28:14 +0000 | [diff] [blame^] | 102 | size_t OptionValueFileSpec::AutoComplete(CommandInterpreter &interpreter, |
| 103 | CompletionRequest &request) { |
| 104 | request.SetWordComplete(false); |
| 105 | request.GetMatches().Clear(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 106 | CommandCompletions::InvokeCommonCompletionCallbacks( |
Raphael Isemann | a2e76c0 | 2018-07-13 18:28:14 +0000 | [diff] [blame^] | 107 | interpreter, m_completion_mask, request, nullptr); |
| 108 | return request.GetMatches().GetSize(); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Pavel Labath | 50251fc | 2017-12-21 10:54:30 +0000 | [diff] [blame] | 111 | const lldb::DataBufferSP &OptionValueFileSpec::GetFileContents() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 112 | if (m_current_value) { |
Pavel Labath | e877baa | 2016-11-09 14:33:22 +0000 | [diff] [blame] | 113 | const auto file_mod_time = FileSystem::GetModificationTime(m_current_value); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 114 | if (m_data_sp && m_data_mod_time == file_mod_time) |
| 115 | return m_data_sp; |
Pavel Labath | 50251fc | 2017-12-21 10:54:30 +0000 | [diff] [blame] | 116 | m_data_sp = DataBufferLLVM::CreateFromPath(m_current_value.GetPath()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 117 | m_data_mod_time = file_mod_time; |
| 118 | } |
| 119 | return m_data_sp; |
Greg Clayton | 6920b52 | 2012-08-22 18:39:03 +0000 | [diff] [blame] | 120 | } |