LLDB  mainline
OptionValueFileColonLine.cpp
Go to the documentation of this file.
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 
10 
14 #include "lldb/Utility/Args.h"
15 #include "lldb/Utility/State.h"
16 
17 using namespace lldb;
18 using 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.
24 OptionValueFileColonLine::OptionValueFileColonLine() = default;
25 
26 OptionValueFileColonLine::OptionValueFileColonLine(llvm::StringRef input)
27  : m_line_number(LLDB_INVALID_LINE_NUMBER),
28  m_column_number(LLDB_INVALID_COLUMN_NUMBER),
29  m_completion_mask(CommandCompletions::eSourceFileCompletion) {
31 }
32 
34  Stream &strm, uint32_t dump_mask) {
35  if (dump_mask & eDumpOptionType)
36  strm.Printf("(%s)", GetTypeAsCString());
37  if (dump_mask & eDumpOptionValue) {
38  if (dump_mask & eDumpOptionType)
39  strm.PutCString(" = ");
40 
41  if (m_file_spec)
42  strm << '"' << m_file_spec.GetPath().c_str() << '"';
44  strm.Printf(":%d", m_line_number);
46  strm.Printf(":%d", m_column_number);
47  }
48 }
49 
52  Status error;
53  switch (op) {
55  Clear();
57  break;
58 
61  if (value.size() > 0) {
62  // This is in the form filename:linenumber:column.
63  // I wish we could use filename:linenumber.column, that would make the
64  // parsing unambiguous and so much easier...
65  // But clang & gcc both print the output with two : so we're stuck with
66  // the two colons. Practically, the only actual ambiguity this introduces
67  // is with files like "foo:10", which doesn't seem terribly likely.
68 
69  // Providing the column is optional, so the input value might have one or
70  // two colons. First pick off the last colon separated piece.
71  // It has to be there, since the line number is required:
72  llvm::StringRef last_piece;
73  llvm::StringRef left_of_last_piece;
74 
75  std::tie(left_of_last_piece, last_piece) = value.rsplit(':');
76  if (last_piece.empty()) {
77  error.SetErrorStringWithFormat("Line specifier must include file and "
78  "line: '%s'",
79  value.str().c_str());
80  return error;
81  }
82 
83  // Now see if there's another colon and if so pull out the middle piece:
84  // Then check whether the middle piece is an integer. If it is, then it
85  // was the line number, and if it isn't we're going to assume that there
86  // was a colon in the filename (see note at the beginning of the function)
87  // and ignore it.
88  llvm::StringRef file_name;
89  llvm::StringRef middle_piece;
90 
91  std::tie(file_name, middle_piece) = left_of_last_piece.rsplit(':');
92  if (middle_piece.empty() ||
93  !llvm::to_integer(middle_piece, m_line_number)) {
94  // The middle piece was empty or not an integer, so there were only two
95  // legit pieces; our original division was right. Reassign the file
96  // name and pull out the line number:
97  file_name = left_of_last_piece;
98  if (!llvm::to_integer(last_piece, m_line_number)) {
99  error.SetErrorStringWithFormat("Bad line number value '%s' in: '%s'",
100  last_piece.str().c_str(),
101  value.str().c_str());
102  return error;
103  }
104  } else {
105  // There were three pieces, and we've got the line number. So now
106  // we just need to check the column number which was the last peice.
107  if (!llvm::to_integer(last_piece, m_column_number)) {
108  error.SetErrorStringWithFormat("Bad column value '%s' in: '%s'",
109  last_piece.str().c_str(),
110  value.str().c_str());
111  return error;
112  }
113  }
114 
115  m_value_was_set = true;
116  m_file_spec.SetFile(file_name, FileSpec::Style::native);
118  } else {
119  error.SetErrorString("invalid value string");
120  }
121  break;
122 
129  break;
130  }
131  return error;
132 }
133 
135  CompletionRequest &request) {
137  interpreter, m_completion_mask, request, nullptr);
138 }
lldb_private::eVarSetOperationInsertBefore
@ eVarSetOperationInsertBefore
Definition: lldb-private-enumerations.h:85
lldb_private::eVarSetOperationReplace
@ eVarSetOperationReplace
Definition: lldb-private-enumerations.h:84
lldb_private::ExecutionContext
Definition: ExecutionContext.h:292
LLDB_INVALID_COLUMN_NUMBER
#define LLDB_INVALID_COLUMN_NUMBER
Definition: lldb-defines.h:99
lldb_private::OptionValueFileColonLine::m_file_spec
FileSpec m_file_spec
Definition: OptionValueFileColonLine.h:52
FormatManager.h
lldb_private::Stream
Definition: Stream.h:28
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::VarSetOperationType
VarSetOperationType
Settable state variable types.
Definition: lldb-private-enumerations.h:83
lldb_private::OptionValueFileColonLine::Clear
void Clear() override
Definition: OptionValueFileColonLine.h:36
lldb_private::CommandCompletions
Definition: CommandCompletions.h:25
lldb_private::OptionValueFileColonLine::DumpValue
void DumpValue(const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask) override
Definition: OptionValueFileColonLine.cpp:33
lldb_private::OptionValue::NotifyValueChanged
void NotifyValueChanged()
Definition: OptionValue.h:318
Args.h
lldb_private::OptionValue::SetValueFromString
virtual Status SetValueFromString(llvm::StringRef value, VarSetOperationType op=eVarSetOperationAssign)
Definition: OptionValue.cpp:579
lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks
static bool InvokeCommonCompletionCallbacks(CommandInterpreter &interpreter, uint32_t completion_mask, lldb_private::CompletionRequest &request, SearchFilter *searcher)
Definition: CommandCompletions.cpp:51
lldb_private::eVarSetOperationAppend
@ eVarSetOperationAppend
Definition: lldb-private-enumerations.h:88
lldb_private::CommandInterpreter
Definition: CommandInterpreter.h:214
lldb_private::OptionValue::eDumpOptionValue
@ eDumpOptionValue
Definition: OptionValue.h:53
lldb_private::OptionValue::GetTypeAsCString
virtual const char * GetTypeAsCString() const
Definition: OptionValue.h:76
lldb_private::OptionValueFileColonLine::SetValueFromString
Status SetValueFromString(llvm::StringRef value, VarSetOperationType op=eVarSetOperationAssign) override
Definition: OptionValueFileColonLine.cpp:50
LLDB_INVALID_LINE_NUMBER
#define LLDB_INVALID_LINE_NUMBER
Definition: lldb-defines.h:98
lldb_private::OptionValueFileColonLine::m_column_number
uint32_t m_column_number
Definition: OptionValueFileColonLine.h:54
lldb_private::Status
Definition: Status.h:44
uint32_t
lldb_private::OptionValue::m_value_was_set
bool m_value_was_set
Definition: OptionValue.h:332
lldb_private::OptionValue::eDumpOptionType
@ eDumpOptionType
Definition: OptionValue.h:52
lldb_private::OptionValueFileColonLine::AutoComplete
void AutoComplete(CommandInterpreter &interpreter, CompletionRequest &request) override
Definition: OptionValueFileColonLine.cpp:134
lldb_private::eVarSetOperationRemove
@ eVarSetOperationRemove
Definition: lldb-private-enumerations.h:87
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::OptionValueFileColonLine::m_line_number
uint32_t m_line_number
Definition: OptionValueFileColonLine.h:53
CommandInterpreter.h
State.h
lldb_private::OptionValueFileColonLine::m_completion_mask
uint32_t m_completion_mask
Definition: OptionValueFileColonLine.h:55
lldb_private::eVarSetOperationInvalid
@ eVarSetOperationInvalid
Definition: lldb-private-enumerations.h:91
lldb_private::Stream::PutCString
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:63
lldb_private::FileSpec::SetFile
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
Definition: FileSpec.cpp:174
lldb_private::FileSpec::GetPath
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:348
OptionValueFileColonLine.h
lldb
Definition: SBAddress.h:15
lldb_private::eVarSetOperationClear
@ eVarSetOperationClear
Definition: lldb-private-enumerations.h:89
CommandCompletions.h
lldb_private::CompletionRequest
Definition: CompletionRequest.h:100
lldb_private::eVarSetOperationInsertAfter
@ eVarSetOperationInsertAfter
Definition: lldb-private-enumerations.h:86
lldb_private::eVarSetOperationAssign
@ eVarSetOperationAssign
Definition: lldb-private-enumerations.h:90