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_CLANG_TOOLING_COMMONOPTIONSPARSER_H 28 #define LLVM_CLANG_TOOLING_COMMONOPTIONSPARSER_H 29 30 #include "clang/Tooling/CompilationDatabase.h" 31 #include "llvm/Support/CommandLine.h" 32 33 namespace clang { 34 namespace tooling { 35 /// \brief A parser for options common to all command-line Clang tools. 36 /// 37 /// Parses a common subset of command-line arguments, locates and loads a 38 /// compilation commands database and runs a tool with user-specified action. It 39 /// also contains a help message for the common command-line options. 40 /// 41 /// An example of usage: 42 /// \code 43 /// #include "clang/Frontend/FrontendActions.h" 44 /// #include "clang/Tooling/CommonOptionsParser.h" 45 /// #include "clang/Tooling/Tooling.h" 46 /// #include "llvm/Support/CommandLine.h" 47 /// 48 /// using namespace clang::tooling; 49 /// using namespace llvm; 50 /// 51 /// static cl::OptionCategory MyToolCategory("My tool options"); 52 /// static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage); 53 /// static cl::extrahelp MoreHelp("\nMore help text..."); 54 /// static cl::opt<bool> YourOwnOption(...); 55 /// ... 56 /// 57 /// int main(int argc, const char **argv) { 58 /// CommonOptionsParser OptionsParser(argc, argv, MyToolCategory); 59 /// ClangTool Tool(OptionsParser.getCompilations(), 60 /// OptionsParser.getSourcePathList()); 61 /// return Tool.run(newFrontendActionFactory<SyntaxOnlyAction>().get()); 62 /// } 63 /// \endcode 64 class CommonOptionsParser { 65 public: 66 /// \brief Parses command-line, initializes a compilation database. 67 /// 68 /// This constructor can change argc and argv contents, e.g. consume 69 /// command-line options used for creating FixedCompilationDatabase. 70 /// 71 /// All options not belonging to \p Category become hidden. 72 /// 73 /// This constructor exits program in case of error. 74 CommonOptionsParser(int &argc, const char **argv, 75 llvm::cl::OptionCategory &Category, 76 const char *Overview = nullptr) 77 : CommonOptionsParser(argc, argv, Category, llvm::cl::OneOrMore, 78 Overview) {} 79 80 /// \brief Parses command-line, initializes a compilation database. 81 /// 82 /// This constructor can change argc and argv contents, e.g. consume 83 /// command-line options used for creating FixedCompilationDatabase. 84 /// 85 /// All options not belonging to \p Category become hidden. 86 /// 87 /// I also allows calls to set the required number of positional parameters. 88 /// 89 /// This constructor exits program in case of error. 90 CommonOptionsParser(int &argc, const char **argv, 91 llvm::cl::OptionCategory &Category, 92 llvm::cl::NumOccurrencesFlag OccurrencesFlag, 93 const char *Overview = nullptr); 94 95 /// Returns a reference to the loaded compilations database. 96 CompilationDatabase &getCompilations() { 97 return *Compilations; 98 } 99 100 /// Returns a list of source file paths to process. 101 std::vector<std::string> getSourcePathList() { 102 return SourcePathList; 103 } 104 105 static const char *const HelpMessage; 106 107 private: 108 std::unique_ptr<CompilationDatabase> Compilations; 109 std::vector<std::string> SourcePathList; 110 std::vector<std::string> ExtraArgsBefore; 111 std::vector<std::string> ExtraArgsAfter; 112 }; 113 114 } // namespace tooling 115 } // namespace clang 116 117 #endif // LLVM_TOOLS_CLANG_INCLUDE_CLANG_TOOLING_COMMONOPTIONSPARSER_H 118