Parsing C++ code to an AST and processing the result

I’m trying to parse C++ code to an abstract syntax tree and do something with the result. (Basically I’m trying to create a DSL that has the syntax and denotational semantics of a subset of C++, but different operational semantics.)

To parse C++ with default compiler options and the ability to add more on the command line and/or use a compilation database, I’m going by http://clang.llvm.org/docs/LibASTMatchersTutorial.html which gets as far as

int main(int argc, const char **argv) {
CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
ClangTool Tool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());

LoopPrinter Printer;
MatchFinder Finder;
Finder.addMatcher(LoopMatcher, &Printer);

return Tool.run(newFrontendActionFactory(&Finder).get());
}

The only thing not applicable here is that the tutorial is about using the pattern matching facility to match fragments of code wherever they might occur in the AST, whereas I’m looking to recur over the entire AST. The ideal would be if I could just say ‘parse, give me the AST and I’ll take it from there’. Is there an easy way to do that?

An alternative possibility would be to use an AST visitor to match function and other declarations at the top level and go from there. http://clang.llvm.org/docs/RAVFrontendAction.html discusses this, getting as far as

int main(int argc, char **argv) {
if (argc > 1) {
clang::tooling::runToolOnCode(new FindNamedClassAction, argv[1]);
}
}

However, this only parses code fragments passed as literal text. Is there a way to combine these and get the features of CommonOptionsParser and ClangTool but run over the entire AST rather than using local pattern matching?

I’m trying to parse C++ code to an abstract syntax tree and do something with the result. (Basically I’m trying to create a DSL that has the syntax and denotational semantics of a subset of C++, but different operational semantics.)

To parse C++ with default compiler options and the ability to add more on the command line and/or use a compilation database, I’m going by http://clang.llvm.org/docs/LibASTMatchersTutorial.html which gets as far as

int main(int argc, const char **argv) {
CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
ClangTool Tool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());

LoopPrinter Printer;
MatchFinder Finder;
Finder.addMatcher(LoopMatcher, &Printer);

return Tool.run(newFrontendActionFactory(&Finder).get());

You can implement your own FrontendAction / FrontendActionFactory, where you’ll just get called with a TranslationUnitDecl (which is basically the whole AST).

Alternatively, you can use the buildASTFromCode function from Tooling.h.

That works, thanks! I initially tripped over the use of templates in the
factory, but the relevant line ends up being

  return Tool.run(newFrontendActionFactory<MainAction>().get());