1 //===- CommonOptionsParser.h - common options for clang tools -*- C++ -*-=====// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the CommonOptionsParser class used to parse common 11 // command-line options for clang tools, so that they can be run as separate 12 // command-line applications with a consistent common interface for handling 13 // compilation database and input files. 14 // 15 // It provides a common subset of command-line options, common algorithm 16 // for locating a compilation database and source files, and help messages 17 // for the basic command-line interface. 18 // 19 // It creates a CompilationDatabase and reads common command-line options. 20 // 21 // This class uses the Clang Tooling infrastructure, see 22 // http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html 23 // for details on setting it up with LLVM source tree. 24 // 25 //===----------------------------------------------------------------------===// 26 27 #ifndef LLVM_TOOLS_CLANG_INCLUDE_CLANG_TOOLING_COMMONOPTIONSPARSER_H 28 #define LLVM_TOOLS_CLANG_INCLUDE_CLANG_TOOLING_COMMONOPTIONSPARSER_H 29 30 #include "clang/Tooling/CompilationDatabase.h" 31 32 namespace clang { 33 namespace tooling { 34 /// \brief A parser for options common to all command-line Clang tools. 35 /// 36 /// Parses a common subset of command-line arguments, locates and loads a 37 /// compilation commands database and runs a tool with user-specified action. It 38 /// also contains a help message for the common command-line options. 39 /// 40 /// An example of usage: 41 /// \code 42 /// #include "clang/Frontend/FrontendActions.h" 43 /// #include "clang/Tooling/CommonOptionsParser.h" 44 /// #include "llvm/Support/CommandLine.h" 45 /// 46 /// using namespace clang::tooling; 47 /// using namespace llvm; 48 /// 49 /// static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage); 50 /// static cl::extrahelp MoreHelp("\nMore help text..."); 51 /// static cl:opt<bool> YourOwnOption(...); 52 /// ... 53 /// 54 /// int main(int argc, const char **argv) { 55 /// CommonOptionsParser OptionsParser(argc, argv); 56 /// ClangTool Tool(OptionsParser.getCompilations(), 57 /// OptionsParser.getSourcePathListi()); 58 /// return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>()); 59 /// } 60 /// \endcode 61 class CommonOptionsParser { 62 public: 63 /// \brief Parses command-line, initializes a compilation database. 64 /// This constructor can change argc and argv contents, e.g. consume 65 /// command-line options used for creating FixedCompilationDatabase. 66 /// This constructor exits program in case of error. 67 CommonOptionsParser(int &argc, const char **argv, const char *Overview = 0); 68 69 /// Returns a reference to the loaded compilations database. 70 CompilationDatabase &getCompilations() { 71 return *Compilations; 72 } 73 74 /// Returns a list of source file paths to process. 75 std::vector<std::string> getSourcePathList() { 76 return SourcePathList; 77 } 78 79 static const char *const HelpMessage; 80 81 private: 82 OwningPtr<CompilationDatabase> Compilations; 83 std::vector<std::string> SourcePathList; 84 }; 85 86 } // namespace tooling 87 } // namespace clang 88 89 #endif // LLVM_TOOLS_CLANG_INCLUDE_CLANG_TOOLING_COMMONOPTIONSPARSER_H 90