Search code examples
gccclangabstract-syntax-tree

Analog of Clang AST in GCC


Is there analog of Clang AST in GCC?

Sometimes it's usefull to see code as the compiler sees it. Clang AST options -ast-view, -ast-tree and -ast-print are very helpful here.

I'm interested if there are such options for GCC or some tools to produce AST tree that work with GCC.


Solution

  • You can dump the (result of the) tree passes by means of -fdump-tree-all and the inter-procedural analysis passes by -fdump-ipa-all. For a compilation unit module.c, this will write dump files module.c.<num>t.<name> and module.c.<num>i.<name>, respectively. <num> indicated the order in which these passes are run, <name> indicates the pass name.

    There are also 100s of options controlling dumping for specific passes only, see GCC Developer Options.

    These dumps' syntax is mostly C-ish. After the tree passes there are the machine-dependent RTL passes. You can dump these with -fdump-rtl-all or -da. These dumps are LISP-ish and named module.c.<num>r.<name>. You can also dump the final RTL with -fdump-final-insns.

    In order to see which RTL is associated to which assembly instructions, you can -save-temps -dP and then inspect the assembly file module.s.

    In the case you are using LTO (-flto) the assembly file will only contain lto1 gibberish. You can add the assembly code by means of -ffat-lto-objects. Notice however that with LTO, this is not the final code as produced by the lto run. To see that code, use -save-temps -flto -o program.exe and the dumps from the LTO run are named program.exe.ltrans<id>.<num>[tir].<name>.