Greg Clayton | 554f68d | 2015-02-04 22:00:53 +0000 | [diff] [blame] | 1 | //===-- OptionValueFormatEntity.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 | |
Greg Clayton | 554f68d | 2015-02-04 22:00:53 +0000 | [diff] [blame] | 10 | #include "lldb/Interpreter/OptionValueFormatEntity.h" |
| 11 | |
| 12 | // C Includes |
| 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
| 15 | // Project includes |
| 16 | #include "lldb/Core/Module.h" |
Greg Clayton | 554f68d | 2015-02-04 22:00:53 +0000 | [diff] [blame] | 17 | #include "lldb/Interpreter/CommandInterpreter.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 18 | #include "lldb/Utility/Stream.h" |
Zachary Turner | 573ab90 | 2017-03-21 18:25:04 +0000 | [diff] [blame] | 19 | #include "lldb/Utility/StringList.h" |
Greg Clayton | 554f68d | 2015-02-04 22:00:53 +0000 | [diff] [blame] | 20 | using namespace lldb; |
| 21 | using namespace lldb_private; |
| 22 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 23 | OptionValueFormatEntity::OptionValueFormatEntity(const char *default_format) |
| 24 | : OptionValue(), m_current_format(), m_default_format(), m_current_entry(), |
| 25 | m_default_entry() { |
| 26 | if (default_format && default_format[0]) { |
| 27 | llvm::StringRef default_format_str(default_format); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 28 | Status error = FormatEntity::Parse(default_format_str, m_default_entry); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 29 | if (error.Success()) { |
| 30 | m_default_format = default_format; |
| 31 | m_current_format = default_format; |
| 32 | m_current_entry = m_default_entry; |
Greg Clayton | 554f68d | 2015-02-04 22:00:53 +0000 | [diff] [blame] | 33 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 34 | } |
Greg Clayton | 554f68d | 2015-02-04 22:00:53 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 37 | bool OptionValueFormatEntity::Clear() { |
| 38 | m_current_entry = m_default_entry; |
| 39 | m_current_format = m_default_format; |
| 40 | m_value_was_set = false; |
| 41 | return true; |
Greg Clayton | 554f68d | 2015-02-04 22:00:53 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Jonas Devlieghere | 6401159 | 2018-10-01 13:22:24 +0000 | [diff] [blame] | 44 | static void EscapeBackticks(llvm::StringRef str, std::string &dst) { |
| 45 | dst.clear(); |
| 46 | dst.reserve(str.size()); |
| 47 | |
| 48 | for (size_t i = 0, e = str.size(); i != e; ++i) { |
| 49 | char c = str[i]; |
| 50 | if (c == '`') { |
| 51 | if (i == 0 || str[i - 1] != '\\') |
| 52 | dst += '\\'; |
| 53 | } |
| 54 | dst += c; |
| 55 | } |
| 56 | } |
| 57 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 58 | void OptionValueFormatEntity::DumpValue(const ExecutionContext *exe_ctx, |
| 59 | Stream &strm, uint32_t dump_mask) { |
| 60 | if (dump_mask & eDumpOptionType) |
| 61 | strm.Printf("(%s)", GetTypeAsCString()); |
| 62 | if (dump_mask & eDumpOptionValue) { |
Greg Clayton | 554f68d | 2015-02-04 22:00:53 +0000 | [diff] [blame] | 63 | if (dump_mask & eDumpOptionType) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 64 | strm.PutCString(" = \""); |
Jonas Devlieghere | 6401159 | 2018-10-01 13:22:24 +0000 | [diff] [blame] | 65 | std::string escaped; |
| 66 | EscapeBackticks(m_current_format, escaped); |
| 67 | strm << escaped << '"'; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 71 | Status OptionValueFormatEntity::SetValueFromString(llvm::StringRef value_str, |
| 72 | VarSetOperationType op) { |
| 73 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | switch (op) { |
| 75 | case eVarSetOperationClear: |
| 76 | Clear(); |
| 77 | NotifyValueChanged(); |
| 78 | break; |
| 79 | |
| 80 | case eVarSetOperationReplace: |
| 81 | case eVarSetOperationAssign: { |
| 82 | // Check if the string starts with a quote character after removing leading |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 83 | // and trailing spaces. If it does start with a quote character, make sure |
| 84 | // it ends with the same quote character and remove the quotes before we |
| 85 | // parse the format string. If the string doesn't start with a quote, leave |
| 86 | // the string alone and parse as is. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 87 | llvm::StringRef trimmed_value_str = value_str.trim(); |
| 88 | if (!trimmed_value_str.empty()) { |
| 89 | const char first_char = trimmed_value_str[0]; |
| 90 | if (first_char == '"' || first_char == '\'') { |
| 91 | const size_t trimmed_len = trimmed_value_str.size(); |
| 92 | if (trimmed_len == 1 || value_str[trimmed_len - 1] != first_char) { |
| 93 | error.SetErrorStringWithFormat("mismatched quotes"); |
| 94 | return error; |
| 95 | } |
| 96 | value_str = trimmed_value_str.substr(1, trimmed_len - 2); |
| 97 | } |
Greg Clayton | 554f68d | 2015-02-04 22:00:53 +0000 | [diff] [blame] | 98 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 99 | FormatEntity::Entry entry; |
| 100 | error = FormatEntity::Parse(value_str, entry); |
| 101 | if (error.Success()) { |
| 102 | m_current_entry = std::move(entry); |
| 103 | m_current_format = value_str; |
| 104 | m_value_was_set = true; |
| 105 | NotifyValueChanged(); |
Greg Clayton | 554f68d | 2015-02-04 22:00:53 +0000 | [diff] [blame] | 106 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 107 | } break; |
| 108 | |
| 109 | case eVarSetOperationInsertBefore: |
| 110 | case eVarSetOperationInsertAfter: |
| 111 | case eVarSetOperationRemove: |
| 112 | case eVarSetOperationAppend: |
| 113 | case eVarSetOperationInvalid: |
| 114 | error = OptionValue::SetValueFromString(value_str, op); |
| 115 | break; |
| 116 | } |
| 117 | return error; |
Greg Clayton | 554f68d | 2015-02-04 22:00:53 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 120 | lldb::OptionValueSP OptionValueFormatEntity::DeepCopy() const { |
| 121 | return OptionValueSP(new OptionValueFormatEntity(*this)); |
Greg Clayton | 554f68d | 2015-02-04 22:00:53 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Raphael Isemann | a2e76c0 | 2018-07-13 18:28:14 +0000 | [diff] [blame] | 124 | size_t OptionValueFormatEntity::AutoComplete(CommandInterpreter &interpreter, |
| 125 | CompletionRequest &request) { |
| 126 | return FormatEntity::AutoComplete(request); |
Greg Clayton | 554f68d | 2015-02-04 22:00:53 +0000 | [diff] [blame] | 127 | } |