clang API Documentation

CommandLineSourceLoc.h
Go to the documentation of this file.
00001 
00002 //===--- CommandLineSourceLoc.h - Parsing for source locations-*- C++ -*---===//
00003 //
00004 //                     The LLVM Compiler Infrastructure
00005 //
00006 // This file is distributed under the University of Illinois Open Source
00007 // License. See LICENSE.TXT for details.
00008 //
00009 //===----------------------------------------------------------------------===//
00010 //
00011 // Command line parsing for source locations.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_CLANG_FRONTEND_COMMANDLINESOURCELOC_H
00016 #define LLVM_CLANG_FRONTEND_COMMANDLINESOURCELOC_H
00017 
00018 #include "clang/Basic/LLVM.h"
00019 #include "llvm/Support/CommandLine.h"
00020 #include "llvm/Support/raw_ostream.h"
00021 
00022 namespace clang {
00023 
00024 /// \brief A source location that has been parsed on the command line.
00025 struct ParsedSourceLocation {
00026   std::string FileName;
00027   unsigned Line;
00028   unsigned Column;
00029 
00030 public:
00031   /// Construct a parsed source location from a string; the Filename is empty on
00032   /// error.
00033   static ParsedSourceLocation FromString(StringRef Str) {
00034     ParsedSourceLocation PSL;
00035     std::pair<StringRef, StringRef> ColSplit = Str.rsplit(':');
00036     std::pair<StringRef, StringRef> LineSplit =
00037       ColSplit.first.rsplit(':');
00038 
00039     // If both tail splits were valid integers, return success.
00040     if (!ColSplit.second.getAsInteger(10, PSL.Column) &&
00041         !LineSplit.second.getAsInteger(10, PSL.Line)) {
00042       PSL.FileName = LineSplit.first;
00043 
00044       // On the command-line, stdin may be specified via "-". Inside the
00045       // compiler, stdin is called "<stdin>".
00046       if (PSL.FileName == "-")
00047         PSL.FileName = "<stdin>";
00048     }
00049 
00050     return PSL;
00051   }
00052 };
00053 
00054 }
00055 
00056 namespace llvm {
00057   namespace cl {
00058     /// \brief Command-line option parser that parses source locations.
00059     ///
00060     /// Source locations are of the form filename:line:column.
00061     template<>
00062     class parser<clang::ParsedSourceLocation>
00063       : public basic_parser<clang::ParsedSourceLocation> {
00064     public:
00065       inline bool parse(Option &O, StringRef ArgName, StringRef ArgValue,
00066                  clang::ParsedSourceLocation &Val);
00067     };
00068 
00069     bool
00070     parser<clang::ParsedSourceLocation>::
00071     parse(Option &O, StringRef ArgName, StringRef ArgValue,
00072           clang::ParsedSourceLocation &Val) {
00073       using namespace clang;
00074 
00075       Val = ParsedSourceLocation::FromString(ArgValue);
00076       if (Val.FileName.empty()) {
00077         errs() << "error: "
00078                << "source location must be of the form filename:line:column\n";
00079         return true;
00080       }
00081 
00082       return false;
00083     }
00084   }
00085 }
00086 
00087 #endif