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 /// \brief 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>";
58 /// \brief Command-line option parser that parses source locations.
60 /// Source locations are of the form filename:line:column.
62 class parser<clang::ParsedSourceLocation>
63 : public basic_parser<clang::ParsedSourceLocation> {
65 inline bool parse(Option &O, StringRef ArgName, StringRef ArgValue,
66 clang::ParsedSourceLocation &Val);
70 parser<clang::ParsedSourceLocation>::
71 parse(Option &O, StringRef ArgName, StringRef ArgValue,
72 clang::ParsedSourceLocation &Val) {
73 using namespace clang;
75 Val = ParsedSourceLocation::FromString(ArgValue);
76 if (Val.FileName.empty()) {
78 << "source location must be of the form filename:line:column\n";