xref: /src/contrib/llvm-project/lldb/source/Interpreter/OptionValueFileColonLine.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1b60736ecSDimitry Andric //===-- OptionValueFileColonLine.cpp---------------------------------------===//
2b60736ecSDimitry Andric //
3b60736ecSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4b60736ecSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5b60736ecSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6b60736ecSDimitry Andric //
7b60736ecSDimitry Andric //===----------------------------------------------------------------------===//
8b60736ecSDimitry Andric 
9b60736ecSDimitry Andric #include "lldb/Interpreter/OptionValueFileColonLine.h"
10b60736ecSDimitry Andric 
11b60736ecSDimitry Andric #include "lldb/DataFormatters/FormatManager.h"
12b60736ecSDimitry Andric #include "lldb/Interpreter/CommandCompletions.h"
13b60736ecSDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h"
14b60736ecSDimitry Andric #include "lldb/Utility/Args.h"
15b60736ecSDimitry Andric #include "lldb/Utility/State.h"
16b60736ecSDimitry Andric 
17b60736ecSDimitry Andric using namespace lldb;
18b60736ecSDimitry Andric using namespace lldb_private;
19b60736ecSDimitry Andric 
20b60736ecSDimitry Andric // This is an OptionValue for parsing file:line:column specifications.
21b60736ecSDimitry Andric // I set the completer to "source file" which isn't quite right, but we can
22b60736ecSDimitry Andric // only usefully complete in the file name part of it so it should be good
23b60736ecSDimitry Andric // enough.
24344a3780SDimitry Andric OptionValueFileColonLine::OptionValueFileColonLine() = default;
25b60736ecSDimitry Andric 
OptionValueFileColonLine(llvm::StringRef input)26b60736ecSDimitry Andric OptionValueFileColonLine::OptionValueFileColonLine(llvm::StringRef input)
27145449b1SDimitry Andric 
28145449b1SDimitry Andric {
29b60736ecSDimitry Andric   SetValueFromString(input, eVarSetOperationAssign);
30b60736ecSDimitry Andric }
31b60736ecSDimitry Andric 
DumpValue(const ExecutionContext * exe_ctx,Stream & strm,uint32_t dump_mask)32b60736ecSDimitry Andric void OptionValueFileColonLine::DumpValue(const ExecutionContext *exe_ctx,
33b60736ecSDimitry Andric                                          Stream &strm, uint32_t dump_mask) {
34b60736ecSDimitry Andric   if (dump_mask & eDumpOptionType)
35b60736ecSDimitry Andric     strm.Printf("(%s)", GetTypeAsCString());
36b60736ecSDimitry Andric   if (dump_mask & eDumpOptionValue) {
37b60736ecSDimitry Andric     if (dump_mask & eDumpOptionType)
38b60736ecSDimitry Andric       strm.PutCString(" = ");
39b60736ecSDimitry Andric 
40b60736ecSDimitry Andric     if (m_file_spec)
41b60736ecSDimitry Andric       strm << '"' << m_file_spec.GetPath().c_str() << '"';
42b60736ecSDimitry Andric     if (m_line_number != LLDB_INVALID_LINE_NUMBER)
43b60736ecSDimitry Andric       strm.Printf(":%d", m_line_number);
44b60736ecSDimitry Andric     if (m_column_number != LLDB_INVALID_COLUMN_NUMBER)
45b60736ecSDimitry Andric       strm.Printf(":%d", m_column_number);
46b60736ecSDimitry Andric   }
47b60736ecSDimitry Andric }
48b60736ecSDimitry Andric 
SetValueFromString(llvm::StringRef value,VarSetOperationType op)49b60736ecSDimitry Andric Status OptionValueFileColonLine::SetValueFromString(llvm::StringRef value,
50b60736ecSDimitry Andric                                                     VarSetOperationType op) {
51b60736ecSDimitry Andric   Status error;
52b60736ecSDimitry Andric   switch (op) {
53b60736ecSDimitry Andric   case eVarSetOperationClear:
54b60736ecSDimitry Andric     Clear();
55b60736ecSDimitry Andric     NotifyValueChanged();
56b60736ecSDimitry Andric     break;
57b60736ecSDimitry Andric 
58b60736ecSDimitry Andric   case eVarSetOperationReplace:
59b60736ecSDimitry Andric   case eVarSetOperationAssign:
60b60736ecSDimitry Andric     if (value.size() > 0) {
61b60736ecSDimitry Andric       // This is in the form filename:linenumber:column.
62b60736ecSDimitry Andric       // I wish we could use filename:linenumber.column, that would make the
63b60736ecSDimitry Andric       // parsing unambiguous and so much easier...
64b60736ecSDimitry Andric       // But clang & gcc both print the output with two : so we're stuck with
65b60736ecSDimitry Andric       // the two colons.  Practically, the only actual ambiguity this introduces
66b60736ecSDimitry Andric       // is with files like "foo:10", which doesn't seem terribly likely.
67b60736ecSDimitry Andric 
68b60736ecSDimitry Andric       // Providing the column is optional, so the input value might have one or
69b60736ecSDimitry Andric       // two colons.  First pick off the last colon separated piece.
70b60736ecSDimitry Andric       // It has to be there, since the line number is required:
71b60736ecSDimitry Andric       llvm::StringRef last_piece;
72b60736ecSDimitry Andric       llvm::StringRef left_of_last_piece;
73b60736ecSDimitry Andric 
74b60736ecSDimitry Andric       std::tie(left_of_last_piece, last_piece) = value.rsplit(':');
75b60736ecSDimitry Andric       if (last_piece.empty()) {
76b60736ecSDimitry Andric         error.SetErrorStringWithFormat("Line specifier must include file and "
77b60736ecSDimitry Andric                                        "line: '%s'",
78b60736ecSDimitry Andric                                        value.str().c_str());
79b60736ecSDimitry Andric         return error;
80b60736ecSDimitry Andric       }
81b60736ecSDimitry Andric 
82b60736ecSDimitry Andric       // Now see if there's another colon and if so pull out the middle piece:
83b60736ecSDimitry Andric       // Then check whether the middle piece is an integer.  If it is, then it
84b60736ecSDimitry Andric       // was the line number, and if it isn't we're going to assume that there
85b60736ecSDimitry Andric       // was a colon in the filename (see note at the beginning of the function)
86b60736ecSDimitry Andric       // and ignore it.
87b60736ecSDimitry Andric       llvm::StringRef file_name;
88b60736ecSDimitry Andric       llvm::StringRef middle_piece;
89b60736ecSDimitry Andric 
90b60736ecSDimitry Andric       std::tie(file_name, middle_piece) = left_of_last_piece.rsplit(':');
91344a3780SDimitry Andric       if (middle_piece.empty() ||
92344a3780SDimitry Andric           !llvm::to_integer(middle_piece, m_line_number)) {
93b60736ecSDimitry Andric         // The middle piece was empty or not an integer, so there were only two
94b60736ecSDimitry Andric         // legit pieces; our original division was right.  Reassign the file
95b60736ecSDimitry Andric         // name and pull out the line number:
96b60736ecSDimitry Andric         file_name = left_of_last_piece;
97b60736ecSDimitry Andric         if (!llvm::to_integer(last_piece, m_line_number)) {
98b60736ecSDimitry Andric           error.SetErrorStringWithFormat("Bad line number value '%s' in: '%s'",
99b60736ecSDimitry Andric                                          last_piece.str().c_str(),
100b60736ecSDimitry Andric                                          value.str().c_str());
101b60736ecSDimitry Andric           return error;
102b60736ecSDimitry Andric         }
103b60736ecSDimitry Andric       } else {
104b60736ecSDimitry Andric         // There were three pieces, and we've got the line number.  So now
105b60736ecSDimitry Andric         // we just need to check the column number which was the last peice.
106b60736ecSDimitry Andric         if (!llvm::to_integer(last_piece, m_column_number)) {
107b60736ecSDimitry Andric           error.SetErrorStringWithFormat("Bad column value '%s' in: '%s'",
108b60736ecSDimitry Andric                                          last_piece.str().c_str(),
109b60736ecSDimitry Andric                                          value.str().c_str());
110b60736ecSDimitry Andric           return error;
111b60736ecSDimitry Andric         }
112b60736ecSDimitry Andric       }
113b60736ecSDimitry Andric 
114b60736ecSDimitry Andric       m_value_was_set = true;
115b60736ecSDimitry Andric       m_file_spec.SetFile(file_name, FileSpec::Style::native);
116b60736ecSDimitry Andric       NotifyValueChanged();
117b60736ecSDimitry Andric     } else {
118b60736ecSDimitry Andric       error.SetErrorString("invalid value string");
119b60736ecSDimitry Andric     }
120b60736ecSDimitry Andric     break;
121b60736ecSDimitry Andric 
122b60736ecSDimitry Andric   case eVarSetOperationInsertBefore:
123b60736ecSDimitry Andric   case eVarSetOperationInsertAfter:
124b60736ecSDimitry Andric   case eVarSetOperationRemove:
125b60736ecSDimitry Andric   case eVarSetOperationAppend:
126b60736ecSDimitry Andric   case eVarSetOperationInvalid:
127b60736ecSDimitry Andric     error = OptionValue::SetValueFromString(value, op);
128b60736ecSDimitry Andric     break;
129b60736ecSDimitry Andric   }
130b60736ecSDimitry Andric   return error;
131b60736ecSDimitry Andric }
132b60736ecSDimitry Andric 
AutoComplete(CommandInterpreter & interpreter,CompletionRequest & request)133b60736ecSDimitry Andric void OptionValueFileColonLine::AutoComplete(CommandInterpreter &interpreter,
134b60736ecSDimitry Andric                                             CompletionRequest &request) {
1357fa27ce4SDimitry Andric   lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
136b60736ecSDimitry Andric       interpreter, m_completion_mask, request, nullptr);
137b60736ecSDimitry Andric }
138