ROSE Compiler Framework/How to visualize AST

Overview
Three things are needed to visualize ROSE AST:
 * Sample input code: you provide it
 * a dot graph generator to generate a dot file from AST: ROSE provides dot graph generators
 * a visualization tool to open the dot graph: ZGRViewer and Graphviz are used by ROSE developers

If you don't want to install ROSE+ZGRview + Graphvis from scratch, you can directly use ROSE virtual machine image, which has everything you need installed and configured so you can just visualize your sample code.

Sample input code
Please prepare simplest input code without including any headers so you can get a small enough AST to digest.

Dot Graph Generator
We provide ROSE_INSTALLATION_TREE/bin/dotGeneratorWholeASTGraph (complex graph) and dotGenerator (a simpler version) to generate a dot graph of the detailed AST of input code.

Tools to generate AST graph in dot format. There are two versions
 * dotGenerator: simple AST graph generator showing essential nodes and edges
 * dotGeneratorWholeASTGraph: whole AST graph showing more details. It provides filter options to show/hide certain AST information.

command line: dotGeneratorWholeASTGraph yourcode.c  // it is best to avoid including any header into your input code to have a small enough tree to visualize.

To skip builtin functions
 * dotGeneratorWholeASTGraph -DSKIP_ROSE_BUILTIN_DECLARATIONS yourcode.c

dotGeneratorWholeASTGraph -rose:help -rose:help                    show this help message -rose:dotgraph:asmFileFormatFilter          [0|1]  Disable or enable asmFileFormat filter -rose:dotgraph:asmTypeFilter                [0|1]  Disable or enable asmType filter -rose:dotgraph:binaryExecutableFormatFilter [0|1]  Disable or enable binaryExecutableFormat filter -rose:dotgraph:commentAndDirectiveFilter    [0|1]  Disable or enable commentAndDirective filter -rose:dotgraph:ctorInitializerListFilter    [0|1]  Disable or enable ctorInitializerList filter -rose:dotgraph:defaultFilter                [0|1]  Disable or enable default filter -rose:dotgraph:defaultColorFilter           [0|1]  Disable or enable defaultColor filter -rose:dotgraph:edgeFilter                   [0|1]  Disable or enable edge filter -rose:dotgraph:expressionFilter             [0|1]  Disable or enable expression filter -rose:dotgraph:fileInfoFilter               [0|1]  Disable or enable fileInfo filter -rose:dotgraph:frontendCompatibilityFilter  [0|1]  Disable or enable frontendCompatibility filter -rose:dotgraph:symbolFilter                 [0|1]  Disable or enable symbol filter -rose:dotgraph:emptySymbolTableFilter       [0|1]  Disable or enable emptySymbolTable filter -rose:dotgraph:typeFilter                   [0|1]  Disable or enable type filter -rose:dotgraph:variableDeclarationFilter    [0|1]  Disable or enable variableDeclaration filter -rose:dotgraph:variableDefinitionFilter     [0|1]  Disable or enable variableDefinitionFilter filter -rose:dotgraph:noFilter                     [0|1]  Disable or enable no filtering Current filter flags' values are: m_asmFileFormat = 0 m_asmType = 0 m_binaryExecutableFormat = 0 m_commentAndDirective = 1 m_ctorInitializer = 0 m_default = 1 m_defaultColor = 1 m_edge = 1 m_emptySymbolTable = 0 m_expression = 0 m_fileInfo = 1 m_frontendCompatibility = 0 m_symbol = 0 m_type = 0 m_variableDeclaration = 0 m_variableDefinition = 0 m_noFilter = 0

Dot Graph Visualization
To visualize the generated dot graph, you have to install
 * Graphviz: http://www.graphviz.org/Download.php.
 * ZGRViewer: http://zvtm.sourceforge.net/zgrviewer.html#download. (Version 0.8.x is recommended since 0.9.x has a bugs like inversed (reversed) direction to drag a graph around.)

Please note that you have to configure ZGRViewer to have correct paths to some commands it uses. You can do it from its configuration/setting menu item. Or directly modify the text configuration file (.zgrviewer).

One example configuration is shown below (cat .zgrviewer)    /tmp /home/liao6/svnrepos /home/liao6/opt/graphviz-2.18/bin/dot /home/liao6/opt/graphviz-2.18/bin/neato /home/liao6/opt/graphviz-2.18/bin/circo /home/liao6/opt/graphviz-2.18/bin/twopi /home/liao6/opt/graphviz-2.18/bin</zgrv:graphvizFontDir> </zgrv:directories> <zgrv:webBrowser autoDetect="true" options="" path=""/> <zgrv:proxy enable="false" host="" port="80"/> <zgrv:preferences antialiasing="false" cmdL_options="" highlightColor="-65536" magFactor="2.0" saveWindowLayout="false" sdZoom="false" sdZoomFactor="2" silent="true"/> <zgrv:plugins/> <zgrv:commandLines/> </zgrv:config>

You have to configure the run.sh script to have correct path also

cat run.sh
 * 1) !/bin/sh


 * 1) If you want to be able to run ZGRViewer from any directory,
 * 2) set ZGRV_HOME to the absolute path of ZGRViewer's main directory
 * 3) e.g. ZGRV_HOME=/usr/local/zgrviewer

ZGRV_HOME=/home/liao6/opt/zgrviewer-0.8.1

java -jar $ZGRV_HOME/target/zgrviewer-0.8.1.jar "$@"

Example session
A complete example

which dotGeneratorWholeASTGraph ~/workspace/masterClean/build64/install/bin/dotGeneratorWholeASTGraph
 * 1) make sure the environment variables(PATH, LD_LIBRARY_PATH) for the installed rose are correctly set

dotGeneratorWholeASTGraph -c ttt.c
 * 1) run the dot graph generator

which run.sh ~/64home/opt/zgrviewer-0.8.2/run.sh
 * 1) see it

run.sh ttt.c_WholeAST.dot

example output
We put some example source files and their AST dump files into: https://github.com/chunhualiao/rose-ast
 * for example: https://github.com/chunhualiao/rose-ast/blob/master/func1.c_WholeAST.dot.png
 * https://github.com/chunhualiao/rose-ast/blob/master/parallelfor.c_WholeAST.dot.png

Print AST as horizontal tree
SageInterface functions

// You can call the following functions with gdb

//! Pretty print AST horizontally, output to std output void SageInterface::printAST (SgNode* node);

//! Pretty print AST horizontally, output to a specified text file void SageInterface::printAST (SgNode* node, const char* filename);

//! Pretty print AST horizontally, output to a specified text file. void SageInterface::printAST2TextFile (SgNode* node, const char* filename, bool printTypes=true); A translator (textASTGenerator) is also available, with its source code under exampleTranslators/defaultTranslator.


 * make install-tools will install this tool
 * textASTGenerator input.c will generate a text output of the entire AST

Example use inside of gdb

 * to print a portion of AST to the screen
 * to print a portion of AST into a text file

(gdb) up
 * 1) 7 0x00007ffff418ab5d in Unparse_ExprStmt::unparseExprStmt (this=0x1a1bf950, stmt=0x7fffda63ce30, info=...) at ../../../sourcetree/src/backend/unparser/CxxCodeGeneration/unparseCxx_statements.C:9889

(gdb) p SageInterface::printAST(stmt) └──@0x7fffda63ce30 SgExprStatement transformation 0:0 └──@0x7fffd8488790 SgFunctionCallExp transformation 0:0 ├──@0x7fffe6211910 SgMemberFunctionRefExp transformation 0:0 └──@0x7fffd7f2c370 SgExprListExp transformation 0:0 └──@0x7fffd8488720 SgFunctionCallExp transformation 0:0 ├──@0x7fffe6211988 SgMemberFunctionRefExp transformation 0:0 └──@0x7fffd7f2c3d8 SgExprListExp transformation 0:0 $2 = void

(gdb) up 10 (gdb) p SageInterface::printAST2TextFile(file,"test.txt")
 * 1) 48 0x00007ffff40dce69 in Unparser::unparseFile (this=0x7fffffff8c60, file=0x7fffeb786010, info=..., unparseScope=0x0) at ../../../sourcetree/src/backend/unparser/unparser.C:945

textASTGenerator
Example command line use:

textASTGenerator -c test_qualifiedName.cpp

cat test_qualifiedName.cpp.AST.txt

└──@0x7fe9f1916010 SgProject └──@0xb45730 SgFileList └──@0x7fe9f17be010 SgSourceFile ├──@0x7fe9fdf19120 SgGlobal test_qualifiedName.cpp 0:0 │  ├──@0x7fe9f159a010 SgTypedefDeclaration rose_edg_required_macros_and_functions.h 0:0 │  │   └── NULL │  ├──@0x7fe9f159a390 SgTypedefDeclaration rose_edg_required_macros_and_functions.h 0:0 │  │   └── NULL │  ├──@0x7fe9f0f59010 SgFunctionDeclaration rose_edg_required_macros_and_functions.h 0:0 "::feclearexcept" │  │   ├──@0x7fe9f1391010 SgFunctionParameterList rose_edg_required_macros_and_functions.h 0:0 │  │   │   └──@0x7fe9f1258010 SgInitializedName rose_edg_required_macros_and_functions.h 0:0 "::__excepts" │  │   │       └── NULL │  │   ├── NULL │  │   └── NULL │  ├──@0x7fe9f0f59540 SgFunctionDeclaration rose_edg_required_macros_and_functions.h 0:0 "::fegetexceptflag" │  │   ├──@0x7fe9f1391630 SgFunctionParameterList rose_edg_required_macros_and_functions.h 0:0 │  │   │   ├──@0x7fe9f1258420 SgInitializedName rose_edg_required_macros_and_functions.h 0:0 "::__flagp" │  │   │   │   └── NULL │  │   │   └──@0x7fe9f1258628 SgInitializedName rose_edg_required_macros_and_functions.h 0:0 "::__excepts" │  │   │       └── NULL │  │   ├── NULL │  │   └── NULL

...

│  └──@0x7fe9eff218c0 SgFunctionDeclaration test_qualifiedName.cpp 14:1 "::foo" │      ├──@0x7fe9ef5e0320 SgFunctionParameterList test_qualifiedName.cpp 14:1 │      │   ├──@0x7fe9ef495278 SgInitializedName test_qualifiedName.cpp 14:13 "x" │      │   │   └── NULL │      │   └──@0x7fe9ef495480 SgInitializedName test_qualifiedName.cpp 14:20 "y" │      │       └── NULL │      ├── NULL │      └──@0x7fe9ee8f3010 SgFunctionDefinition test_qualifiedName.cpp 15:1 │          └──@0x7fe9ee988010 SgBasicBlock test_qualifiedName.cpp 15:1 │              ├──@0x7fe9eee1ba90 SgVariableDeclaration test_qualifiedName.cpp 16:3 │              │   ├── NULL │              │   └──@0x7fe9ef495688 SgInitializedName test_qualifiedName.cpp 16:3 "z" │              │       └── NULL │              ├──@0x7fe9ee7ad010 SgExprStatement test_qualifiedName.cpp 17:3 │              │   └──@0x7fe9ee7dc010 SgAssignOp test_qualifiedName.cpp 17:5 │              │       ├──@0x7fe9ee8c0010 SgVarRefExp test_qualifiedName.cpp 17:3 │              │       └──@0x7fe9ee813010 SgAddOp test_qualifiedName.cpp 17:9 │              │           ├──@0x7fe9ee8c0078 SgVarRefExp test_qualifiedName.cpp 17:7 │              │           └──@0x7fe9ee84a010 SgMultiplyOp test_qualifiedName.cpp 17:12 │              │               ├──@0x7fe9ee8c00e0 SgVarRefExp test_qualifiedName.cpp 17:11 │              │               └──@0x7fe9ee881010 SgIntVal test_qualifiedName.cpp 17:13 │              └──@0x7fe9ee77e010 SgReturnStmt test_qualifiedName.cpp 18:3 │                  └──@0x7fe9ee8c0148 SgVarRefExp test_qualifiedName.cpp 18:10 ├── NULL ├── NULL └── NULL

Render the AST in HTML
The repo  contains a tool for rendering the Rose abstract syntax "graph" as collapsible HTML with shared nodes and cycles represented by HTML links. For now, it's available only from the command line. The plan is to add command-line options to omit parts of the tree and to make the tool available as a library. For now, it somewhat arbitrarily omit portions of the tree that originate from the file.

The command: astToHTML file.C will produce  which can be viewed with a browser: firefox file.C.html