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