xref: /llvm-project/lldb/source/Interpreter/OptionValueFileColonLine.cpp (revision 0642cd768b80665585c8500bed2933a3b99123dc)
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 
26bc0a9a17SJim Ingham OptionValueFileColonLine::OptionValueFileColonLine(llvm::StringRef input)
2728c878aeSShafik Yaghmour 
2828c878aeSShafik Yaghmour {
29bc0a9a17SJim Ingham   SetValueFromString(input, eVarSetOperationAssign);
30bc0a9a17SJim Ingham }
31bc0a9a17SJim Ingham 
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 
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()) {
76*0642cd76SAdrian Prantl         error = Status::FromErrorStringWithFormat(
77*0642cd76SAdrian Prantl             "Line specifier must include file and "
78bc0a9a17SJim Ingham             "line: '%s'",
79bc0a9a17SJim Ingham             value.str().c_str());
80bc0a9a17SJim Ingham         return error;
81bc0a9a17SJim Ingham       }
82bc0a9a17SJim Ingham 
83bc0a9a17SJim Ingham       // Now see if there's another colon and if so pull out the middle piece:
84bc0a9a17SJim Ingham       // Then check whether the middle piece is an integer.  If it is, then it
85bc0a9a17SJim Ingham       // was the line number, and if it isn't we're going to assume that there
86bc0a9a17SJim Ingham       // was a colon in the filename (see note at the beginning of the function)
87bc0a9a17SJim Ingham       // and ignore it.
88bc0a9a17SJim Ingham       llvm::StringRef file_name;
89bc0a9a17SJim Ingham       llvm::StringRef middle_piece;
90bc0a9a17SJim Ingham 
91bc0a9a17SJim Ingham       std::tie(file_name, middle_piece) = left_of_last_piece.rsplit(':');
92fd2433e1SJonas Devlieghere       if (middle_piece.empty() ||
93fd2433e1SJonas Devlieghere           !llvm::to_integer(middle_piece, m_line_number)) {
94bc0a9a17SJim Ingham         // The middle piece was empty or not an integer, so there were only two
95bc0a9a17SJim Ingham         // legit pieces; our original division was right.  Reassign the file
96bc0a9a17SJim Ingham         // name and pull out the line number:
97bc0a9a17SJim Ingham         file_name = left_of_last_piece;
98bc0a9a17SJim Ingham         if (!llvm::to_integer(last_piece, m_line_number)) {
99*0642cd76SAdrian Prantl           error = Status::FromErrorStringWithFormat(
100*0642cd76SAdrian Prantl               "Bad line number value '%s' in: '%s'", last_piece.str().c_str(),
101bc0a9a17SJim Ingham               value.str().c_str());
102bc0a9a17SJim Ingham           return error;
103bc0a9a17SJim Ingham         }
104bc0a9a17SJim Ingham       } else {
105bc0a9a17SJim Ingham         // There were three pieces, and we've got the line number.  So now
106bc0a9a17SJim Ingham         // we just need to check the column number which was the last peice.
107bc0a9a17SJim Ingham         if (!llvm::to_integer(last_piece, m_column_number)) {
108*0642cd76SAdrian Prantl           error = Status::FromErrorStringWithFormat(
109*0642cd76SAdrian Prantl               "Bad column value '%s' in: '%s'", last_piece.str().c_str(),
110bc0a9a17SJim Ingham               value.str().c_str());
111bc0a9a17SJim Ingham           return error;
112bc0a9a17SJim Ingham         }
113bc0a9a17SJim Ingham       }
114bc0a9a17SJim Ingham 
115bc0a9a17SJim Ingham       m_value_was_set = true;
116bc0a9a17SJim Ingham       m_file_spec.SetFile(file_name, FileSpec::Style::native);
117bc0a9a17SJim Ingham       NotifyValueChanged();
118bc0a9a17SJim Ingham     } else {
119*0642cd76SAdrian Prantl       error = Status::FromErrorString("invalid value string");
120bc0a9a17SJim Ingham     }
121bc0a9a17SJim Ingham     break;
122bc0a9a17SJim Ingham 
123bc0a9a17SJim Ingham   case eVarSetOperationInsertBefore:
124bc0a9a17SJim Ingham   case eVarSetOperationInsertAfter:
125bc0a9a17SJim Ingham   case eVarSetOperationRemove:
126bc0a9a17SJim Ingham   case eVarSetOperationAppend:
127bc0a9a17SJim Ingham   case eVarSetOperationInvalid:
128bc0a9a17SJim Ingham     error = OptionValue::SetValueFromString(value, op);
129bc0a9a17SJim Ingham     break;
130bc0a9a17SJim Ingham   }
131bc0a9a17SJim Ingham   return error;
132bc0a9a17SJim Ingham }
133bc0a9a17SJim Ingham 
134bc0a9a17SJim Ingham void OptionValueFileColonLine::AutoComplete(CommandInterpreter &interpreter,
135bc0a9a17SJim Ingham                                             CompletionRequest &request) {
1366a9c3e61SMed Ismail Bennani   lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
137bc0a9a17SJim Ingham       interpreter, m_completion_mask, request, nullptr);
138bc0a9a17SJim Ingham }
139