2 //===--- CommandLineSourceLoc.h - Parsing for source locations-*- C++ -*---===//
4 // The LLVM Compiler Infrastructure
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 // Command line parsing for source locations.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_FRONTEND_COMMANDLINESOURCELOC_H
16 #define LLVM_CLANG_FRONTEND_COMMANDLINESOURCELOC_H
18 #include "clang/Basic/LLVM.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/raw_ostream.h"
24 /// A source location that has been parsed on the command line.
25 struct ParsedSourceLocation {
31 /// Construct a parsed source location from a string; the Filename is empty on
33 static ParsedSourceLocation FromString(StringRef Str) {
34 ParsedSourceLocation PSL;
35 std::pair<StringRef, StringRef> ColSplit = Str.rsplit(':');
36 std::pair<StringRef, StringRef> LineSplit =
37 ColSplit.first.rsplit(':');
39 // If both tail splits were valid integers, return success.
40 if (!ColSplit.second.getAsInteger(10, PSL.Column) &&
41 !LineSplit.second.getAsInteger(10, PSL.Line)) {
42 PSL.FileName = LineSplit.first;
44 // On the command-line, stdin may be specified via "-". Inside the
45 // compiler, stdin is called "<stdin>".
46 if (PSL.FileName == "-")
47 PSL.FileName = "<stdin>";
54 /// A source range that has been parsed on the command line.
55 struct ParsedSourceRange {
57 /// The starting location of the range. The first element is the line and
58 /// the second element is the column.
59 std::pair<unsigned, unsigned> Begin;
60 /// The ending location of the range. The first element is the line and the
61 /// second element is the column.
62 std::pair<unsigned, unsigned> End;
64 /// Returns a parsed source range from a string or None if the string is
67 /// These source string has the following format:
69 /// file:start_line:start_column[-end_line:end_column]
71 /// If the end line and column are omitted, the starting line and columns
72 /// are used as the end values.
73 static Optional<ParsedSourceRange> fromString(StringRef Str) {
74 std::pair<StringRef, StringRef> RangeSplit = Str.rsplit('-');
75 unsigned EndLine, EndColumn;
76 bool HasEndLoc = false;
77 if (!RangeSplit.second.empty()) {
78 std::pair<StringRef, StringRef> Split = RangeSplit.second.rsplit(':');
79 if (Split.first.getAsInteger(10, EndLine) ||
80 Split.second.getAsInteger(10, EndColumn)) {
81 // The string does not end in end_line:end_column, so the '-'
82 // probably belongs to the filename which menas the whole
83 // string should be parsed.
84 RangeSplit.first = Str;
88 auto Begin = ParsedSourceLocation::FromString(RangeSplit.first);
89 if (Begin.FileName.empty())
93 EndColumn = Begin.Column;
95 return ParsedSourceRange{std::move(Begin.FileName),
96 {Begin.Line, Begin.Column},
97 {EndLine, EndColumn}};
104 /// Command-line option parser that parses source locations.
106 /// Source locations are of the form filename:line:column.
108 class parser<clang::ParsedSourceLocation> final
109 : public basic_parser<clang::ParsedSourceLocation> {
111 inline bool parse(Option &O, StringRef ArgName, StringRef ArgValue,
112 clang::ParsedSourceLocation &Val);
116 parser<clang::ParsedSourceLocation>::
117 parse(Option &O, StringRef ArgName, StringRef ArgValue,
118 clang::ParsedSourceLocation &Val) {
119 using namespace clang;
121 Val = ParsedSourceLocation::FromString(ArgValue);
122 if (Val.FileName.empty()) {
124 << "source location must be of the form filename:line:column\n";