AST parsing with CLang: get whole AST tree instead of events (like XML DOM instead of SAX) ?

i’d like to use CLang to parse c++ files to AST. The examples from look nice, but I don’t see any parsing to a structure that would hold the whole parsed AST (i.e. comparable to the DOM tree/document generated by DOM-based XML parsers). Instead, what I see is an event-based parsing API (comparable to SAX for XML parsers) where I subclass clang::ASTConsumer and my class gets events and has to store the tree structure itself somehow.

So before i do that (and probably re-invent the wheel in the process), I thought I’d better ask: is there some default tree structure that would save the whole parsed AST and that i can keep and use after the parsing is done (i.e. the AST equivalent of a DOM/XML DomDocument) and some easy way to automatically get that structure filled with the whole AST tree instead of getting events?

Best Regards,

Hello Kevin,

If I understand you correctly, then TranslationUnitDecl is the root of
the tree you are looking for.


if you trap ASTConsumer::HandleTranslationUnit, it provides you clang::ASTContext (for the TU). Is that what you are looking for?

Btw this is just for one compilation unit. I am still not sure how to do it across multiple translation units.

Here is my test project although i am not using ASTContext directly too though..

I use lib tooling in the example.