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