• 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 "clang/Basic/LLVM.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/raw_ostream.h"
21 
22 namespace clang {
23 
24 /// \brief A source location that has been parsed on the command line.
25 struct ParsedSourceLocation {
26   std::string FileName;
27   unsigned Line;
28   unsigned Column;
29 
30 public:
31   /// Construct a parsed source location from a string; the Filename is empty on
32   /// error.
FromStringParsedSourceLocation33   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(':');
38 
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;
43 
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>";
48     }
49 
50     return PSL;
51   }
52 };
53 
54 }
55 
56 namespace llvm {
57   namespace cl {
58     /// \brief Command-line option parser that parses source locations.
59     ///
60     /// Source locations are of the form filename:line:column.
61     template<>
62     class parser<clang::ParsedSourceLocation> final
63       : public basic_parser<clang::ParsedSourceLocation> {
64     public:
65       inline bool parse(Option &O, StringRef ArgName, StringRef ArgValue,
66                  clang::ParsedSourceLocation &Val);
67     };
68 
69     bool
70     parser<clang::ParsedSourceLocation>::
parse(Option & O,StringRef ArgName,StringRef ArgValue,clang::ParsedSourceLocation & Val)71     parse(Option &O, StringRef ArgName, StringRef ArgValue,
72           clang::ParsedSourceLocation &Val) {
73       using namespace clang;
74 
75       Val = ParsedSourceLocation::FromString(ArgValue);
76       if (Val.FileName.empty()) {
77         errs() << "error: "
78                << "source location must be of the form filename:line:column\n";
79         return true;
80       }
81 
82       return false;
83     }
84   }
85 }
86 
87 #endif
88