How to view .ast file?

Hi,

I'm creating some .ast files using the following command

clang++ -emit-ast file1.cc -o file1.ast

But when I open file1.ast directly I only see corrupted characters. Is there
any way I can view them as normal ast file? Or I misunderstood something?

Thanks,
Chen

AST files are binary files. They are not human readable. What are you trying to do?

Hi,

Maybe this is what you want.

$ clang++ -cc1 -ast-print file1.cc

2011/5/18 Ted Kremenek <kremenek@apple.com>

So AST files don't contain actual abstract syntax tree? How can I build
abstract syntax tree for this source code?

Thanks. I've tried that but it doesn't look like the abstract syntax tree,
which is what I hope to see.

Yes, they contain the AST, with information on the declarations, expressions, and everything. An AST is a compiler data structure, not a specific textual dump. There are a few ways to render (parts of) the AST to a human readable form; that's essentially a printout.

Thanks. Do you refer to -ast-print?

Also, is it implemented by Clang to generate a CFG from a given .ast file?

Thanks. Do you refer to -ast-print?

-ast-print and -ast-dump indeed provide different views of the AST.

Also, is it implemented by Clang to generate a CFG from a given .ast file?

Like an AST, the CFG is an internal data structure. Within the compiler, CFGs are generated on-demand from ASTs when they are needed.

Are you asking if it is possible to dump a textual representation of the CFG of a given function from an AST file? If so, there is currently no driver logic for that, but it is definitely possible.

Maybe you want to try -ast-dump-xml. This gives a structure-like output.