// 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 #include "OperationAstMatcher.hpp" #include "OperationFinder.hpp" #include "OperationStorage.hpp" #include "OperationFinderAstAction.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 cl::OptionCategory MyToolCategory("op-finder options"); static cl::opt OutputFile("o", cl::desc("File to output the JSON to."), cl::cat(MyToolCategory)); static cl::opt RootDirectory("r", cl::desc("The root directory of the source files."), cl::cat(MyToolCategory)); static cl::opt PrettyPrint("pretty", cl::desc("Pretty-print the output JSON.")); // 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 cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage); // A help message for this specific tool can be added afterwards. static 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()); OperationStorage storage; if (PrettyPrint.getValue()) storage.enablePrettyPrint(); OperationFinder op_finder(&storage); #if 0 MatchFinder matcher; OperationASTMatcher finder(&op_finder); finder.addToFinder(matcher); //Tool.run(newFrontendActionFactory().get()); return Tool.run(newFrontendActionFactory(&finder).get()); #endif OperationFinderAstAction action(&op_finder); Tool.run(newFrontendActionFactory(&action).get()); if (!OutputFile.getValue().empty()) storage.toFile(OutputFile.getValue()); else storage.toStream(std::cout); }