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 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 44 | void OptionValueFormatEntity::DumpValue(const ExecutionContext *exe_ctx, |
| 45 | Stream &strm, uint32_t dump_mask) { |
| 46 | if (dump_mask & eDumpOptionType) |
| 47 | strm.Printf("(%s)", GetTypeAsCString()); |
| 48 | if (dump_mask & eDumpOptionValue) { |
Greg Clayton | 554f68d | 2015-02-04 22:00:53 +0000 | [diff] [blame] | 49 | if (dump_mask & eDumpOptionType) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 50 | strm.PutCString(" = \""); |
| 51 | strm << m_current_format.c_str() << '"'; |
| 52 | } |
| 53 | } |
| 54 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame^] | 55 | Status OptionValueFormatEntity::SetValueFromString(llvm::StringRef value_str, |
| 56 | VarSetOperationType op) { |
| 57 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 58 | switch (op) { |
| 59 | case eVarSetOperationClear: |
| 60 | Clear(); |
| 61 | NotifyValueChanged(); |
| 62 | break; |
| 63 | |
| 64 | case eVarSetOperationReplace: |
| 65 | case eVarSetOperationAssign: { |
| 66 | // Check if the string starts with a quote character after removing leading |
| 67 | // and trailing spaces. |
| 68 | // If it does start with a quote character, make sure it ends with the same |
| 69 | // quote character |
| 70 | // and remove the quotes before we parse the format string. If the string |
| 71 | // doesn't start with |
| 72 | // a quote, leave the string alone and parse as is. |
| 73 | llvm::StringRef trimmed_value_str = value_str.trim(); |
| 74 | if (!trimmed_value_str.empty()) { |
| 75 | const char first_char = trimmed_value_str[0]; |
| 76 | if (first_char == '"' || first_char == '\'') { |
| 77 | const size_t trimmed_len = trimmed_value_str.size(); |
| 78 | if (trimmed_len == 1 || value_str[trimmed_len - 1] != first_char) { |
| 79 | error.SetErrorStringWithFormat("mismatched quotes"); |
| 80 | return error; |
| 81 | } |
| 82 | value_str = trimmed_value_str.substr(1, trimmed_len - 2); |
| 83 | } |
Greg Clayton | 554f68d | 2015-02-04 22:00:53 +0000 | [diff] [blame] | 84 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 85 | FormatEntity::Entry entry; |
| 86 | error = FormatEntity::Parse(value_str, entry); |
| 87 | if (error.Success()) { |
| 88 | m_current_entry = std::move(entry); |
| 89 | m_current_format = value_str; |
| 90 | m_value_was_set = true; |
| 91 | NotifyValueChanged(); |
Greg Clayton | 554f68d | 2015-02-04 22:00:53 +0000 | [diff] [blame] | 92 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 93 | } break; |
| 94 | |
| 95 | case eVarSetOperationInsertBefore: |
| 96 | case eVarSetOperationInsertAfter: |
| 97 | case eVarSetOperationRemove: |
| 98 | case eVarSetOperationAppend: |
| 99 | case eVarSetOperationInvalid: |
| 100 | error = OptionValue::SetValueFromString(value_str, op); |
| 101 | break; |
| 102 | } |
| 103 | return error; |
Greg Clayton | 554f68d | 2015-02-04 22:00:53 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 106 | lldb::OptionValueSP OptionValueFormatEntity::DeepCopy() const { |
| 107 | return OptionValueSP(new OptionValueFormatEntity(*this)); |
Greg Clayton | 554f68d | 2015-02-04 22:00:53 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 110 | size_t OptionValueFormatEntity::AutoComplete( |
Zachary Turner | 4aa8753 | 2016-11-17 01:37:42 +0000 | [diff] [blame] | 111 | CommandInterpreter &interpreter, llvm::StringRef s, int match_start_point, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 112 | int max_return_elements, bool &word_complete, StringList &matches) { |
| 113 | return FormatEntity::AutoComplete(s, match_start_point, max_return_elements, |
| 114 | word_complete, matches); |
Greg Clayton | 554f68d | 2015-02-04 22:00:53 +0000 | [diff] [blame] | 115 | } |