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