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

source code of lldb/source/Interpreter/OptionValueFileColonLine.cpp