1 //===-- OptionValueFileColonLine.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/OptionValueFileColonLine.h" 10 11 #include "lldb/DataFormatters/FormatManager.h" 12 #include "lldb/Interpreter/CommandCompletions.h" 13 #include "lldb/Interpreter/CommandInterpreter.h" 14 #include "lldb/Utility/Args.h" 15 #include "lldb/Utility/State.h" 16 17 using namespace lldb; 18 using namespace lldb_private; 19 20 // This is an OptionValue for parsing file:line:column specifications. 21 // I set the completer to "source file" which isn't quite right, but we can 22 // only usefully complete in the file name part of it so it should be good 23 // enough. 24 OptionValueFileColonLine::OptionValueFileColonLine() 25 : OptionValue(), m_file_spec(), m_line_number(LLDB_INVALID_LINE_NUMBER), 26 m_column_number(LLDB_INVALID_COLUMN_NUMBER), 27 m_completion_mask(CommandCompletions::eSourceFileCompletion) {} 28 29 OptionValueFileColonLine::OptionValueFileColonLine(llvm::StringRef input) 30 : OptionValue(), m_file_spec(), m_line_number(LLDB_INVALID_LINE_NUMBER), 31 m_column_number(LLDB_INVALID_COLUMN_NUMBER), 32 m_completion_mask(CommandCompletions::eSourceFileCompletion) { 33 SetValueFromString(input, eVarSetOperationAssign); 34 } 35 36 void OptionValueFileColonLine::DumpValue(const ExecutionContext *exe_ctx, 37 Stream &strm, uint32_t dump_mask) { 38 if (dump_mask & eDumpOptionType) 39 strm.Printf("(%s)", GetTypeAsCString()); 40 if (dump_mask & eDumpOptionValue) { 41 if (dump_mask & eDumpOptionType) 42 strm.PutCString(" = "); 43 44 if (m_file_spec) 45 strm << '"' << m_file_spec.GetPath().c_str() << '"'; 46 if (m_line_number != LLDB_INVALID_LINE_NUMBER) 47 strm.Printf(":%d", m_line_number); 48 if (m_column_number != LLDB_INVALID_COLUMN_NUMBER) 49 strm.Printf(":%d", m_column_number); 50 } 51 } 52 53 Status OptionValueFileColonLine::SetValueFromString(llvm::StringRef value, 54 VarSetOperationType op) { 55 Status error; 56 switch (op) { 57 case eVarSetOperationClear: 58 Clear(); 59 NotifyValueChanged(); 60 break; 61 62 case eVarSetOperationReplace: 63 case eVarSetOperationAssign: 64 if (value.size() > 0) { 65 // This is in the form filename:linenumber:column. 66 // I wish we could use filename:linenumber.column, that would make the 67 // parsing unambiguous and so much easier... 68 // But clang & gcc both print the output with two : so we're stuck with 69 // the two colons. Practically, the only actual ambiguity this introduces 70 // is with files like "foo:10", which doesn't seem terribly likely. 71 72 // Providing the column is optional, so the input value might have one or 73 // two colons. First pick off the last colon separated piece. 74 // It has to be there, since the line number is required: 75 llvm::StringRef last_piece; 76 llvm::StringRef left_of_last_piece; 77 78 std::tie(left_of_last_piece, last_piece) = value.rsplit(':'); 79 if (last_piece.empty()) { 80 error.SetErrorStringWithFormat("Line specifier must include file and " 81 "line: '%s'", 82 value.str().c_str()); 83 return error; 84 } 85 86 // Now see if there's another colon and if so pull out the middle piece: 87 // Then check whether the middle piece is an integer. If it is, then it 88 // was the line number, and if it isn't we're going to assume that there 89 // was a colon in the filename (see note at the beginning of the function) 90 // and ignore it. 91 llvm::StringRef file_name; 92 llvm::StringRef middle_piece; 93 94 std::tie(file_name, middle_piece) = left_of_last_piece.rsplit(':'); 95 if (middle_piece.empty() || !llvm::to_integer(middle_piece, 96 m_line_number)) { 97 // The middle piece was empty or not an integer, so there were only two 98 // legit pieces; our original division was right. Reassign the file 99 // name and pull out the line number: 100 file_name = left_of_last_piece; 101 if (!llvm::to_integer(last_piece, m_line_number)) { 102 error.SetErrorStringWithFormat("Bad line number value '%s' in: '%s'", 103 last_piece.str().c_str(), 104 value.str().c_str()); 105 return error; 106 } 107 } else { 108 // There were three pieces, and we've got the line number. So now 109 // we just need to check the column number which was the last peice. 110 if (!llvm::to_integer(last_piece, m_column_number)) { 111 error.SetErrorStringWithFormat("Bad column value '%s' in: '%s'", 112 last_piece.str().c_str(), 113 value.str().c_str()); 114 return error; 115 } 116 } 117 118 m_value_was_set = true; 119 m_file_spec.SetFile(file_name, FileSpec::Style::native); 120 NotifyValueChanged(); 121 } else { 122 error.SetErrorString("invalid value string"); 123 } 124 break; 125 126 case eVarSetOperationInsertBefore: 127 case eVarSetOperationInsertAfter: 128 case eVarSetOperationRemove: 129 case eVarSetOperationAppend: 130 case eVarSetOperationInvalid: 131 error = OptionValue::SetValueFromString(value, op); 132 break; 133 } 134 return error; 135 } 136 137 lldb::OptionValueSP OptionValueFileColonLine::DeepCopy() const { 138 return OptionValueSP(new OptionValueFileColonLine(*this)); 139 } 140 141 void OptionValueFileColonLine::AutoComplete(CommandInterpreter &interpreter, 142 CompletionRequest &request) { 143 CommandCompletions::InvokeCommonCompletionCallbacks( 144 interpreter, m_completion_mask, request, nullptr); 145 } 146