// Declares clang::SyntaxOnlyAction. #include "clang/Frontend/FrontendActions.h" #include "clang/Tooling/CommonOptionsParser.h" #include "clang/Tooling/Tooling.h" // Declares llvm::cl::extrahelp. #include "llvm/Support/CommandLine.h" #include "OperationFinder.hpp" using namespace clang::tooling; using namespace clang::ast_matchers; using namespace llvm; // Apply a custom category to all command-line options so that they are the // only ones displayed. static llvm::cl::OptionCategory MyToolCategory("op-finder options"); static llvm::cl::opt OutputFile("o", cl::desc("File to output the JSON to."), cl::cat(MyToolCategory)); // CommonOptionsParser declares HelpMessage with a description of the common // command-line options related to the compilation database and input files. // It's nice to have this help message in all tools. static llvm::cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage); // A help message for this specific tool can be added afterwards. static llvm::cl::extrahelp MoreHelp("\nThe program takes the input ... files, parses their\n" "AST and outputs a singular file containing a list of all noteworthy operations\n" "for later analysis.\n"); int main(int argc, const char** argv) { auto ExpectedParser = CommonOptionsParser::create(argc, argv, MyToolCategory); if (!ExpectedParser) { // Fail gracefully for unsupported options. llvm::errs() << ExpectedParser.takeError(); return 1; } CommonOptionsParser& OptionsParser = ExpectedParser.get(); ClangTool Tool(OptionsParser.getCompilations(), OptionsParser.getSourcePathList()); OperationFinder op_finder(OutputFile.getValue()); MatchFinder finder; op_finder.addMatcher(finder); return Tool.run(newFrontendActionFactory(&finder).get()); }