问题描述
我认为问题的标题不言自明,我想将由gcc生成的抽象语法树转储为.dot文件(这些文件由Graphviz生成),因为这样我想以.png文件或类似方式查看它。有没有什么办法可以做到这一点?
在此先感谢:) >
有两种方法,包括两个步骤
-
使用GCC内部vcg支持
-
用vcg转储编译你的代码(比如test.c)
gcc -fdump-tree-vcg -g test.c
使用任何第三方工具从vcg
graph-easy test.c.006t.vcg --as_dot
-
编译原始转储文件,然后用一些脚本预处理它们以形成点文件(如)
这两种方法都有各自的好处和坏处 - 首先,在gimple翻译之前,你真的只能得到AST的一个转储,但它很容易。其次,您可以将任何原始转储转换为点格式,但是您必须支持脚本,这是开销。
更喜欢什么 - 取决于您自己的选择。 / p>
UPD:时代正在改变。 gcc 4.8.2的全新选项可以立即生成点文件。只需提供:
gcc test.c -fdump-tree-all-graph
,您将获得大量已为您格式化的点文件:
test.c.008t.lower.dot
test.c.012t.cfg.dot
test.c.016t.ssa.dot
...等。 ..
请确保使用此版本的GCC新版本。
I think the question's title is self explanatory, I want to dump an abstract syntax tree generated by gcc into a .dot file (Those files generated by Graphviz) because then I want to view it in a .png file or similar. Is there any way I can do that?
Thanks in advance :)
解决方案There are two methods, both including two steps
Using GCC internal vcg support
Compile your code (say test.c) with vcg dumps
gcc -fdump-tree-vcg -g test.c
Use any third party tool to get dot output from vcg
graph-easy test.c.006t.vcg --as_dot
Compile with raw dumps and then preprocess them with some scripts to form dot files (like in this useful article)
Both methods have their own good and bad sides -- with first you can really get only one dump of AST before gimple translation, but it is easy. With second you may convert any raw dump to dot-format, but you must support scripts, that is overhead.
What to prefer -- is on your own choice.
UPD: times are changing. Brand new option for gcc 4.8.2 makes it possible to generate dot files immediately. Just supply:
gcc test.c -fdump-tree-all-graph
and you will get a plenty of already formatted for you dot files:
test.c.008t.lower.dot test.c.012t.cfg.dot test.c.016t.ssa.dot ... etc ...
Please be sure to use new versions of GCC with this option.
这篇关于如何将由gcc生成的抽象语法树转储为.dot文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!