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