ROSE 编译器框架/如何可视化 AST
可视化 ROSE AST 需要三件事
- 示例输入代码:由您提供
- 从 AST 生成 dot 文件的 dot 图生成器:ROSE 提供 dot 图生成器
- 用于打开 dot 图的可视化工具:ROSE 开发人员使用 ZGRViewer 和 Graphviz
如果您不想从头开始安装 ROSE+ZGRview + Graphvis,您可以直接使用 ROSE 虚拟机映像,其中包含您需要安装和配置的所有内容,因此您只需可视化您的示例代码即可。
请准备最简单的输入代码,不要包含任何头文件,这样您就可以获得一个足够小的 AST 来消化。
我们提供 ROSE_INSTALLATION_TREE/bin/dotGeneratorWholeASTGraph(复杂图)和 dotGenerator(一个更简单的版本)来生成输入代码详细 AST 的 dot 图。
用于以 dot 格式生成 AST 图的工具。有两个版本
- dotGenerator:简单的 AST 图生成器,显示基本节点和边
- dotGeneratorWholeASTGraph:整个 AST 图,显示更多详细信息。它提供过滤器选项来显示/隐藏某些 AST 信息。
命令行
dotGeneratorWholeASTGraph yourcode.c // it is best to avoid including any header into your input code to have a small enough tree to visualize.
要跳过内置函数
- 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 图,您必须安装
- Graphviz:http://www.graphviz.org/Download.php。
- ZGRViewer:http://zvtm.sourceforge.net/zgrviewer.html#download。(建议使用 0.8.x 版本,因为 0.9.x 存在一些错误,例如拖动图形时方向反转(反向)。)
请注意,您必须配置 ZGRViewer 以使其具有使用的一些命令的正确路径。您可以从其配置/设置菜单项中执行此操作。或者直接修改文本配置文件(.zgrviewer)。
下面显示了一个示例配置(cat .zgrviewer)
<?xml version="1.0" encoding="UTF-8"?> <zgrv:config xmlns:zgrv="http://zvtm.sourceforge.net/zgrviewer"> <zgrv:directories> <zgrv:tmpDir value="true">/tmp</zgrv:tmpDir> <zgrv:graphDir>/home/liao6/svnrepos</zgrv:graphDir> <zgrv:dot>/home/liao6/opt/graphviz-2.18/bin/dot</zgrv:dot> <zgrv:neato>/home/liao6/opt/graphviz-2.18/bin/neato</zgrv:neato> <zgrv:circo>/home/liao6/opt/graphviz-2.18/bin/circo</zgrv:circo> <zgrv:twopi>/home/liao6/opt/graphviz-2.18/bin/twopi</zgrv:twopi> <zgrv:graphvizFontDir>/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>
您还必须配置 run.sh 脚本以具有正确的路径
cat run.sh
#!/bin/sh # If you want to be able to run ZGRViewer from any directory, # set ZGRV_HOME to the absolute path of ZGRViewer's main directory # 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 "$@"
一个完整的例子
# make sure the environment variables(PATH, LD_LIBRARY_PATH) for the installed rose are correctly set which dotGeneratorWholeASTGraph ~/workspace/masterClean/build64/install/bin/dotGeneratorWholeASTGraph # run the dot graph generator dotGeneratorWholeASTGraph -c ttt.c #see it which run.sh ~/64home/opt/zgrviewer-0.8.2/run.sh run.sh ttt.c_WholeAST.dot
我们将一些示例源文件及其 AST 转储文件放入:https://github.com/chunhualiao/rose-ast
- 例如: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
SageInterface 函数
// 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);
还有一个翻译器(textASTGenerator),其源代码位于 exampleTranslators/defaultTranslator 下。
- make install-tools 将安装此工具
- textASTGenerator input.c 将生成整个 AST 的文本输出
- 将 AST 的一部分打印到屏幕
- 将 AST 的一部分打印到文本文件
(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 $2 = void (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(file,"test.txt")
示例命令行使用
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
存储库 errington1/ast-to-html
包含一个工具,用于将 Rose 抽象语法“图”渲染为可折叠的 HTML,其中共享节点和循环由 HTML 链接表示。目前,它只能从命令行使用。计划添加命令行选项以省略树的某些部分,并使该工具作为库可用。目前,它有点随意地省略了源自文件 rose_edg_required_macros_and_functions.h
的树的某些部分。
该命令
astToHTML file.C
将生成 file.C.html
,可以使用浏览器查看
firefox file.C.html