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