1 //===- CommonOptionsParser.h - common options for clang tools -*- C++ -*-=====// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the CommonOptionsParser class used to parse common 10 // command-line options for clang tools, so that they can be run as separate 11 // command-line applications with a consistent common interface for handling 12 // compilation database and input files. 13 // 14 // It provides a common subset of command-line options, common algorithm 15 // for locating a compilation database and source files, and help messages 16 // for the basic command-line interface. 17 // 18 // It creates a CompilationDatabase and reads common command-line options. 19 // 20 // This class uses the Clang Tooling infrastructure, see 21 // http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html 22 // for details on setting it up with LLVM source tree. 23 // 24 //===----------------------------------------------------------------------===// 25 26 #ifndef LLVM_CLANG_TOOLING_COMMONOPTIONSPARSER_H 27 #define LLVM_CLANG_TOOLING_COMMONOPTIONSPARSER_H 28 29 #include "clang/Tooling/ArgumentsAdjusters.h" 30 #include "clang/Tooling/CompilationDatabase.h" 31 #include "llvm/Support/CommandLine.h" 32 #include "llvm/Support/Error.h" 33 34 namespace clang { 35 namespace tooling { 36 /// A parser for options common to all command-line Clang tools. 37 /// 38 /// Parses a common subset of command-line arguments, locates and loads a 39 /// compilation commands database and runs a tool with user-specified action. It 40 /// also contains a help message for the common command-line options. 41 /// 42 /// An example of usage: 43 /// \code 44 /// #include "clang/Frontend/FrontendActions.h" 45 /// #include "clang/Tooling/CommonOptionsParser.h" 46 /// #include "clang/Tooling/Tooling.h" 47 /// #include "llvm/Support/CommandLine.h" 48 /// 49 /// using namespace clang::tooling; 50 /// using namespace llvm; 51 /// 52 /// static cl::OptionCategory MyToolCategory("My tool options"); 53 /// static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage); 54 /// static cl::extrahelp MoreHelp("\nMore help text...\n"); 55 /// static cl::opt<bool> YourOwnOption(...); 56 /// ... 57 /// 58 /// int main(int argc, const char **argv) { 59 /// CommonOptionsParser OptionsParser(argc, argv, MyToolCategory); 60 /// ClangTool Tool(OptionsParser.getCompilations(), 61 /// OptionsParser.getSourcePathList()); 62 /// return Tool.run(newFrontendActionFactory<SyntaxOnlyAction>().get()); 63 /// } 64 /// \endcode 65 class CommonOptionsParser { 66 public: 67 /// Parses command-line, initializes a compilation database. 68 /// 69 /// This constructor can change argc and argv contents, e.g. consume 70 /// command-line options used for creating FixedCompilationDatabase. 71 /// 72 /// All options not belonging to \p Category become hidden. 73 /// 74 /// This constructor exits program in case of error. 75 CommonOptionsParser(int &argc, const char **argv, 76 llvm::cl::OptionCategory &Category, 77 const char *Overview = nullptr) CommonOptionsParser(argc,argv,Category,llvm::cl::OneOrMore,Overview)78 : CommonOptionsParser(argc, argv, Category, llvm::cl::OneOrMore, 79 Overview) {} 80 81 /// Parses command-line, initializes a compilation database. 82 /// 83 /// This constructor can change argc and argv contents, e.g. consume 84 /// command-line options used for creating FixedCompilationDatabase. 85 /// 86 /// All options not belonging to \p Category become hidden. 87 /// 88 /// It also allows calls to set the required number of positional parameters. 89 CommonOptionsParser(int &argc, const char **argv, 90 llvm::cl::OptionCategory &Category, 91 llvm::cl::NumOccurrencesFlag OccurrencesFlag, 92 const char *Overview = nullptr); 93 94 /// A factory method that is similar to the above constructor, except 95 /// this returns an error instead exiting the program on error. 96 static llvm::Expected<CommonOptionsParser> 97 create(int &argc, const char **argv, llvm::cl::OptionCategory &Category, 98 llvm::cl::NumOccurrencesFlag OccurrencesFlag, 99 const char *Overview = nullptr); 100 101 /// Returns a reference to the loaded compilations database. getCompilations()102 CompilationDatabase &getCompilations() { 103 return *Compilations; 104 } 105 106 /// Returns a list of source file paths to process. getSourcePathList()107 const std::vector<std::string> &getSourcePathList() const { 108 return SourcePathList; 109 } 110 111 /// Returns the argument adjuster calculated from "--extra-arg" and 112 //"--extra-arg-before" options. getArgumentsAdjuster()113 ArgumentsAdjuster getArgumentsAdjuster() { return Adjuster; } 114 115 static const char *const HelpMessage; 116 117 private: 118 CommonOptionsParser() = default; 119 120 llvm::Error init(int &argc, const char **argv, 121 llvm::cl::OptionCategory &Category, 122 llvm::cl::NumOccurrencesFlag OccurrencesFlag, 123 const char *Overview); 124 125 std::unique_ptr<CompilationDatabase> Compilations; 126 std::vector<std::string> SourcePathList; 127 ArgumentsAdjuster Adjuster; 128 }; 129 130 class ArgumentsAdjustingCompilations : public CompilationDatabase { 131 public: ArgumentsAdjustingCompilations(std::unique_ptr<CompilationDatabase> Compilations)132 ArgumentsAdjustingCompilations( 133 std::unique_ptr<CompilationDatabase> Compilations) 134 : Compilations(std::move(Compilations)) {} 135 136 void appendArgumentsAdjuster(ArgumentsAdjuster Adjuster); 137 138 std::vector<CompileCommand> 139 getCompileCommands(StringRef FilePath) const override; 140 141 std::vector<std::string> getAllFiles() const override; 142 143 std::vector<CompileCommand> getAllCompileCommands() const override; 144 145 private: 146 std::unique_ptr<CompilationDatabase> Compilations; 147 std::vector<ArgumentsAdjuster> Adjusters; 148 149 std::vector<CompileCommand> 150 adjustCommands(std::vector<CompileCommand> Commands) const; 151 }; 152 153 } // namespace tooling 154 } // namespace clang 155 156 #endif // LLVM_TOOLS_CLANG_INCLUDE_CLANG_TOOLING_COMMONOPTIONSPARSER_H 157