Friday, January 25, 2013

ast2dot - A script to help visualize the GCC AST

Recently I have been creating GCC plugins to automatically and transparently add security mechanisms to software at compile time. I enjoy the work tremendously. Part of the work requires an understanding of GCCs intermediate representations including the abstract syntax tree (AST).

The AST can be difficult to grok. Therefore, I created a simple utility that converts a GCC AST dump into the DOT graph description language. Once the tree is represented by the DOT format it can be visualized using various diagramming tools such as GraphViz, or my favorite for the Mac, OmniGraffle.

Following is an example. First the source code of a simple C program:

int main()
   int i;

   i = 2 + 2;
   return i;

During compilation with GCC, this simple C code gets transformed into the following (mildly pruned) AST:

The script has a few routines for pruning the tree. For example you can specify certain attributes to ignore and/or specify a starting node. I have found the script to be more useful for inspecting specific subtrees rather than an entire tree all at once.

The script is written in Ruby. It can be found on github: Maybe it will be useful for others.

No comments: