[cfe-dev] Parsing C++ code to an AST and processing the result

Manuel Klimek via cfe-dev cfe-dev at lists.llvm.org
Fri Dec 4 04:22:12 PST 2015

On Fri, Dec 4, 2015 at 1:15 PM Russell Wallace via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> 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.

> }
> 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?
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20151204/412a6f31/attachment.html>

More information about the cfe-dev mailing list