编译器|GCC抽象语法树(AST)可视化------适用于2019最新版本gcc-8.2.0

gcc多版本更替, 使得很多ast 可视化工具都不能用了,向gcc插件VCG也不能在 高版本上使用
现在我们找到了一个还适用于 最新版本的方法
首先我们创建一个文件命名test.c的Hello World程序。
test.c

#include int main(int arg_count,char ** arg_values) { printf("Hello World\n"); return 0; }

运行脚本ast-raw.sh
gcc -fdump-tree-original-raw ./test.c


就会生成AST的dump文件
test.c.003t.original
; ; Function main (null) ; ; enabled by -tree-original @1bind_exprtype: @2body: @3 @2void_typename: @4algn: 8 @3statement_list0: @51: @6 @4type_declname: @7type: @2srcp: :0 @5call_exprtype: @8fn: @90: @10 @6return_exprtype: @2expr: @11 @7identifier_nodestrg: voidlngt: 4 @8integer_typename: @12size: @13algn: 32 prec: 32sign: signedmin : @14 max : @15 @9addr_exprtype: @16op 0: @17 @10nop_exprtype: @18op 0: @19 @11modify_exprtype: @8op 0: @20op 1: @21 @12type_declname: @22type: @8srcp: :0 @13integer_csttype: @23low : 32 @14integer_csttype: @8high: -1low : -2147483648 @15integer_csttype: @8low : 2147483647 @16pointer_typesize: @24algn: 64ptd : @25 @17function_declname: @26type: @25srcp: stdio.h:339 body: undefinedlink: extern @18pointer_typequal:runql: @27size: @24 algn: 64ptd : @28 @19addr_exprtype: @29op 0: @30 @20result_decltype: @8scpe: @31srcp: test.c:4 note: artificialsize: @13 algn: 32 @21integer_csttype: @8low : 0 @22identifier_nodestrg: intlngt: 3 @23integer_typename: @32size: @24algn: 64 prec: 64sign: unsigned min : @33 max : @34 @24integer_csttype: @23low : 64 @25function_typesize: @35algn: 8retn: @8 prms: @36 @26identifier_nodestrg: printflngt: 6 @27pointer_typesize: @24algn: 64ptd : @28 @28integer_typequal: cname: @37unql: @38 size: @35algn: 8prec: 8 sign: signedmin : @39max : @40 @29pointer_typesize: @24algn: 64ptd : @41 @30string_csttype: @41strg: Hello World gt: 13 @31function_declname: @42type: @43srcp: test.c:3 args: @44link: extern @32identifier_nodestrg: bit_size_typelngt: 13 @33integer_csttype: @23low : 0 @34integer_csttype: @23low : -1 @35integer_csttype: @23low : 8 @36tree_listvalu: @18 @37type_declname: @45type: @38srcp: :0 @38integer_typename: @37size: @35algn: 8 prec: 8sign: signedmin : @39 max : @40 @39integer_csttype: @38high: -1low : -128 @40integer_csttype: @38low : 127 @41array_typesize: @46algn: 8elts: @38 domn: @47 @42identifier_nodestrg: mainlngt: 4 @43function_typesize: @35algn: 8retn: @8 prms: @48 @44parm_declname: @49type: @8scpe: @31 srcp: test.c:3argt: @8 size: @13algn: 32used: 0 @45identifier_nodestrg: charlngt: 4 @46integer_csttype: @23low : 104 @47integer_typesize: @24algn: 64prec: 64 sign: signedmin : @50max : @51 @48tree_listvalu: @8chan: @52 @49identifier_nodestrg: arg_countlngt: 9 @50integer_csttype: @53low : 0 @51integer_csttype: @53low : 12 @52tree_listvalu: @54chan: @55 @53integer_typename: @56size: @24algn: 64 prec: 64sign: unsigned min : @57 max : @58 @54pointer_typesize: @24algn: 64ptd : @59 @55tree_listvalu: @2 @56type_declname: @60type: @61srcp: :0 @57integer_csttype: @61low : 0 @58integer_csttype: @53high: -1low : -1 @59pointer_typesize: @24algn: 64ptd : @38 @60identifier_nodestrg: long unsigned intlngt: 17 @61integer_typename: @56size: @24algn: 64 prec: 64sign: unsigned min : @57 max : @62 @62integer_csttype: @61low : -1

准备下面的两个文件
pre.awk
#! /usr/bin/gawk -f /^[^; ]/{ gsub(/^@/, "~@", $0); gsub(/( *):( *)/, ":", $0); print; }

treeviz.awk
#! /usr/bin/gawk -f #https://blog.csdn.net/l919898756 BEGIN {RS = "~@"; printf "digraph G {\n node [shape = record]; "; } /^[0-9]/{ s = sprintf("%s [label = \"{%s | {", $1, $1); for(i = 2; i < NF - 1; i++) s = s sprintf("%s | ", $i); s = s sprintf("%s}}\"]; \n", $i); $0 = s; while (/([a-zA-Z]+):@([0-9]+)/){ format = sprintf("\\1 \\3\n %s:\\1 -> \\2; ", $1); $0 = gensub(/([a-zA-Z]+):@([0-9]+)(.*)$/, format, "g"); }; printf " %s\n", $0; } END {print "}"}

然后执行如下两个脚本
ast2dot.sh
./pre.awk test.c.* | ./treeviz.awk > tree.dot

dot2png.sh
dot -Tpng tree.dot -o tree.png

【编译器|GCC抽象语法树(AST)可视化------适用于2019最新版本gcc-8.2.0】即可 生成图片,也可以生成pdf,或者bmp文件会 高清一些

    推荐阅读