Traversing AST node by node

Hi all,

I’m currently doing a project that needs me to analyse the AST of source codes from top to bottom without traversing specific parts of the AST. I want to simple traverse each and every node in the tree structure and collect information. Is there an easy way to get this done ?

I’m new to clang so any help would be greatly appreciated.

Thanks in advance,



Yes! Clang provides a really nice API to achieve this.
You can use Clang RecursiveASTVisitor to traverse the AST:

or you can match on specific nodes using AST matchers:

With Clang libtooling, you can compile your program into a standalone tool that gets as input the source code to be analyzed.

Hope it helps,