looking for an AST traversal example

Hi there,

I am still on my search for a relatively simple example to work with which creates an AST from a source file and traverses it doing even the most rudimentary tasks. Does anyone have such an example? Or does anyone have any tips on how to go about building one?

What I am attempting to do is build a stand-alone application (ie not a plugin) that sets up the AST (ie build preprocessor, set appropriate options (header search, etc), initialize ParseAST routine, …) and then use an ASTConsumer to traverse the tree.

Any feed back would be greatly appreciated.


Attached is a very non-complete, and simple example which I created awhile
back. Even though this it uses RecursiveASTVisitor, one can get
the basics on how to use ASTConsumers from it. One caveat here is that
if you do want to use RecursiveASTVisitor, you must really have a good
understanding of tablegen generated header files such as DeclNodes.inc.
Using these files, along with redefined macros, is where the real power
of RecursiveASTVisitor manifests itself (IMHO). The second caveat is that there
is no cleanup here. For example I'm including headers (such as some JIT
stuff), which I do not need. This was just a quick hack which got me started.

Hope this helps


PS: This example was tested with TOT

recurseVisitAst.cpp (8.04 KB)

Thanks Garrison. I appreciate the example.