Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
608 views
in Technique[技术] by (71.8m points)

graphviz - How can I dump an abstract syntax tree generated by gcc into a .dot file?

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 :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are two methods, both including two steps

  1. Using GCC internal vcg support

    1. Compile your code (say test.c) with vcg dumps

      gcc -fdump-tree-vcg -g test.c

    2. Use any third party tool to get dot output from vcg

      graph-easy test.c.006t.vcg --as_dot

  2. 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...