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