• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 //===--- CommandLineSourceLoc.h - Parsing for source locations-*- C++ -*---===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10 //
11 // Command line parsing for source locations.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_FRONTEND_COMMANDLINESOURCELOC_H
16 #define LLVM_CLANG_FRONTEND_COMMANDLINESOURCELOC_H
17 
18 #include "llvm/Support/CommandLine.h"
19 #include "llvm/Support/raw_ostream.h"
20 
21 namespace clang {
22 
23 /// \brief A source location that has been parsed on the command line.
24 struct ParsedSourceLocation {
25   std::string FileName;
26   unsigned Line;
27   unsigned Column;
28 
29 public:
30   /// Construct a parsed source location from a string; the Filename is empty on
31   /// error.
FromStringParsedSourceLocation32   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(':');
37 
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;
42 
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>";
47     }
48 
49     return PSL;
50   }
51 };
52 
53 }
54 
55 namespace llvm {
56   namespace cl {
57     /// \brief Command-line option parser that parses source locations.
58     ///
59     /// Source locations are of the form filename:line:column.
60     template<>
61     class parser<clang::ParsedSourceLocation>
62       : public basic_parser<clang::ParsedSourceLocation> {
63     public:
64       inline bool parse(Option &O, StringRef ArgName, StringRef ArgValue,
65                  clang::ParsedSourceLocation &Val);
66     };
67 
68     bool
69     parser<clang::ParsedSourceLocation>::
parse(Option & O,StringRef ArgName,StringRef ArgValue,clang::ParsedSourceLocation & Val)70     parse(Option &O, StringRef ArgName, StringRef ArgValue,
71           clang::ParsedSourceLocation &Val) {
72       using namespace clang;
73 
74       Val = ParsedSourceLocation::FromString(ArgValue);
75       if (Val.FileName.empty()) {
76         errs() << "error: "
77                << "source location must be of the form filename:line:column\n";
78         return true;
79       }
80 
81       return false;
82     }
83   }
84 }
85 
86 #endif
87