Path: blob/main/contrib/llvm-project/lldb/source/Interpreter/OptionValueFileColonLine.cpp
39587 views
//===-- OptionValueFileColonLine.cpp---------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/Interpreter/OptionValueFileColonLine.h"910#include "lldb/DataFormatters/FormatManager.h"11#include "lldb/Interpreter/CommandCompletions.h"12#include "lldb/Interpreter/CommandInterpreter.h"13#include "lldb/Utility/Args.h"14#include "lldb/Utility/State.h"1516using namespace lldb;17using namespace lldb_private;1819// This is an OptionValue for parsing file:line:column specifications.20// I set the completer to "source file" which isn't quite right, but we can21// only usefully complete in the file name part of it so it should be good22// enough.23OptionValueFileColonLine::OptionValueFileColonLine() = default;2425OptionValueFileColonLine::OptionValueFileColonLine(llvm::StringRef input)2627{28SetValueFromString(input, eVarSetOperationAssign);29}3031void OptionValueFileColonLine::DumpValue(const ExecutionContext *exe_ctx,32Stream &strm, uint32_t dump_mask) {33if (dump_mask & eDumpOptionType)34strm.Printf("(%s)", GetTypeAsCString());35if (dump_mask & eDumpOptionValue) {36if (dump_mask & eDumpOptionType)37strm.PutCString(" = ");3839if (m_file_spec)40strm << '"' << m_file_spec.GetPath().c_str() << '"';41if (m_line_number != LLDB_INVALID_LINE_NUMBER)42strm.Printf(":%d", m_line_number);43if (m_column_number != LLDB_INVALID_COLUMN_NUMBER)44strm.Printf(":%d", m_column_number);45}46}4748Status OptionValueFileColonLine::SetValueFromString(llvm::StringRef value,49VarSetOperationType op) {50Status error;51switch (op) {52case eVarSetOperationClear:53Clear();54NotifyValueChanged();55break;5657case eVarSetOperationReplace:58case eVarSetOperationAssign:59if (value.size() > 0) {60// This is in the form filename:linenumber:column.61// I wish we could use filename:linenumber.column, that would make the62// parsing unambiguous and so much easier...63// But clang & gcc both print the output with two : so we're stuck with64// the two colons. Practically, the only actual ambiguity this introduces65// is with files like "foo:10", which doesn't seem terribly likely.6667// Providing the column is optional, so the input value might have one or68// two colons. First pick off the last colon separated piece.69// It has to be there, since the line number is required:70llvm::StringRef last_piece;71llvm::StringRef left_of_last_piece;7273std::tie(left_of_last_piece, last_piece) = value.rsplit(':');74if (last_piece.empty()) {75error.SetErrorStringWithFormat("Line specifier must include file and "76"line: '%s'",77value.str().c_str());78return error;79}8081// Now see if there's another colon and if so pull out the middle piece:82// Then check whether the middle piece is an integer. If it is, then it83// was the line number, and if it isn't we're going to assume that there84// was a colon in the filename (see note at the beginning of the function)85// and ignore it.86llvm::StringRef file_name;87llvm::StringRef middle_piece;8889std::tie(file_name, middle_piece) = left_of_last_piece.rsplit(':');90if (middle_piece.empty() ||91!llvm::to_integer(middle_piece, m_line_number)) {92// The middle piece was empty or not an integer, so there were only two93// legit pieces; our original division was right. Reassign the file94// name and pull out the line number:95file_name = left_of_last_piece;96if (!llvm::to_integer(last_piece, m_line_number)) {97error.SetErrorStringWithFormat("Bad line number value '%s' in: '%s'",98last_piece.str().c_str(),99value.str().c_str());100return error;101}102} else {103// There were three pieces, and we've got the line number. So now104// we just need to check the column number which was the last peice.105if (!llvm::to_integer(last_piece, m_column_number)) {106error.SetErrorStringWithFormat("Bad column value '%s' in: '%s'",107last_piece.str().c_str(),108value.str().c_str());109return error;110}111}112113m_value_was_set = true;114m_file_spec.SetFile(file_name, FileSpec::Style::native);115NotifyValueChanged();116} else {117error.SetErrorString("invalid value string");118}119break;120121case eVarSetOperationInsertBefore:122case eVarSetOperationInsertAfter:123case eVarSetOperationRemove:124case eVarSetOperationAppend:125case eVarSetOperationInvalid:126error = OptionValue::SetValueFromString(value, op);127break;128}129return error;130}131132void OptionValueFileColonLine::AutoComplete(CommandInterpreter &interpreter,133CompletionRequest &request) {134lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(135interpreter, m_completion_mask, request, nullptr);136}137138139