Skip to content

How to Visualize PORTIONS of the AST

Jim Leek edited this page Jan 5, 2022 · 2 revisions

Quite often an entire AST is too large to be really useful, and you only want to visualize a certain node and it's descendants. This is especially pertinent with attempting to debug ROSE with gdb. It can be very helpful to see where you are in the AST.

Several functions are provided that allow you to do this.

To print a portion of AST to the screen

(gdb) up
#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

To print a portion of AST into a text file

(gdb) p SageInterface::printAST(stmt, "file.ast")
└──@0x7fffda63ce30 SgExprStatement transformation 0:0
    └──@0x7fffd8488790 SgFunctionCallExp transformation 0:0
        ├──@0x7fffe6211910 SgMemberFunctionRefExp transformation 0:0

To print a portion of AST into a text file, with all types

This function will print all the types in the AST to a file after the printing the small portion of the AST comprised by 'node' and it's descendants. It will print "Types encountered ...." and then print every single type seen in the entire AST, which is always a gigantic list. Good if you need to know about types

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

To print a portion of the AST to a PDF file

SageInterface::saveToPDF(SgNode* node, std:string filename)
Clone this wiki locally