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 "llvm/Support/CommandLine.h"
19 #include "llvm/Support/raw_ostream.h"
23 /// \brief A source location that has been parsed on the command line.
24 struct ParsedSourceLocation {
30 /// Construct a parsed source location from a string; the Filename is empty on
32 static ParsedSourceLocation FromString(llvm::StringRef Str) {
33 ParsedSourceLocation PSL;
34 std::pair<llvm::StringRef, llvm::StringRef> ColSplit = Str.rsplit(':');
35 std::pair<llvm::StringRef, llvm::StringRef> LineSplit =
36 ColSplit.first.rsplit(':');
38 // If both tail splits were valid integers, return success.
39 if (!ColSplit.second.getAsInteger(10, PSL.Column) &&
40 !LineSplit.second.getAsInteger(10, PSL.Line)) {
41 PSL.FileName = LineSplit.first;
43 // On the command-line, stdin may be specified via "-". Inside the
44 // compiler, stdin is called "<stdin>".
45 if (PSL.FileName == "-")
46 PSL.FileName = "<stdin>";
57 /// \brief Command-line option parser that parses source locations.
59 /// Source locations are of the form filename:line:column.
61 class parser<clang::ParsedSourceLocation>
62 : public basic_parser<clang::ParsedSourceLocation> {
64 inline bool parse(Option &O, StringRef ArgName, StringRef ArgValue,
65 clang::ParsedSourceLocation &Val);
69 parser<clang::ParsedSourceLocation>::
70 parse(Option &O, StringRef ArgName, StringRef ArgValue,
71 clang::ParsedSourceLocation &Val) {
72 using namespace clang;
74 Val = ParsedSourceLocation::FromString(ArgValue);
75 if (Val.FileName.empty()) {
77 << "source location must be of the form filename:line:column\n";