clang API Documentation

CommonOptionsParser.h
Go to the documentation of this file.
00001 //===- CommonOptionsParser.h - common options for clang tools -*- C++ -*-=====//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file implements the CommonOptionsParser class used to parse common
00011 //  command-line options for clang tools, so that they can be run as separate
00012 //  command-line applications with a consistent common interface for handling
00013 //  compilation database and input files.
00014 //
00015 //  It provides a common subset of command-line options, common algorithm
00016 //  for locating a compilation database and source files, and help messages
00017 //  for the basic command-line interface.
00018 //
00019 //  It creates a CompilationDatabase and reads common command-line options.
00020 //
00021 //  This class uses the Clang Tooling infrastructure, see
00022 //    http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
00023 //  for details on setting it up with LLVM source tree.
00024 //
00025 //===----------------------------------------------------------------------===//
00026 
00027 #ifndef LLVM_CLANG_TOOLING_COMMONOPTIONSPARSER_H
00028 #define LLVM_CLANG_TOOLING_COMMONOPTIONSPARSER_H
00029 
00030 #include "clang/Tooling/CompilationDatabase.h"
00031 #include "llvm/Support/CommandLine.h"
00032 
00033 namespace clang {
00034 namespace tooling {
00035 /// \brief A parser for options common to all command-line Clang tools.
00036 ///
00037 /// Parses a common subset of command-line arguments, locates and loads a
00038 /// compilation commands database and runs a tool with user-specified action. It
00039 /// also contains a help message for the common command-line options.
00040 ///
00041 /// An example of usage:
00042 /// \code
00043 /// #include "clang/Frontend/FrontendActions.h"
00044 /// #include "clang/Tooling/CommonOptionsParser.h"
00045 /// #include "llvm/Support/CommandLine.h"
00046 ///
00047 /// using namespace clang::tooling;
00048 /// using namespace llvm;
00049 ///
00050 /// static cl::OptionCategory MyToolCategory("My tool options");
00051 /// static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
00052 /// static cl::extrahelp MoreHelp("\nMore help text...");
00053 /// static cl::opt<bool> YourOwnOption(...);
00054 /// ...
00055 ///
00056 /// int main(int argc, const char **argv) {
00057 ///   CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
00058 ///   ClangTool Tool(OptionsParser.getCompilations(),
00059 ///                  OptionsParser.getSourcePathListi());
00060 ///   return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>());
00061 /// }
00062 /// \endcode
00063 class CommonOptionsParser {
00064 public:
00065   /// \brief Parses command-line, initializes a compilation database.
00066   ///
00067   /// This constructor can change argc and argv contents, e.g. consume
00068   /// command-line options used for creating FixedCompilationDatabase.
00069   ///
00070   /// All options not belonging to \p Category become hidden.
00071   ///
00072   /// This constructor exits program in case of error.
00073   CommonOptionsParser(int &argc, const char **argv,
00074                       llvm::cl::OptionCategory &Category,
00075                       const char *Overview = nullptr);
00076 
00077   /// Returns a reference to the loaded compilations database.
00078   CompilationDatabase &getCompilations() {
00079     return *Compilations;
00080   }
00081 
00082   /// Returns a list of source file paths to process.
00083   std::vector<std::string> getSourcePathList() {
00084     return SourcePathList;
00085   }
00086 
00087   static const char *const HelpMessage;
00088 
00089 private:
00090   std::unique_ptr<CompilationDatabase> Compilations;
00091   std::vector<std::string> SourcePathList;
00092   std::vector<std::string> ExtraArgsBefore;
00093   std::vector<std::string> ExtraArgsAfter;
00094 };
00095 
00096 }  // namespace tooling
00097 }  // namespace clang
00098 
00099 #endif  // LLVM_TOOLS_CLANG_INCLUDE_CLANG_TOOLING_COMMONOPTIONSPARSER_H