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