How to view Clang AST?
Asked Answered
I

4

58

I am trying to get hold on Clang. So, I would like to view the AST generated by Clang after parsing the given program. Is it possible to dump AST in .dot or .viz format? Is there any tool out there?

Incentive answered 1/9, 2013 at 15:40 Comment(0)
S
60

Clang supports showing the AST with Graphviz's dotty -- you can grab the temporary .dot file generated (name is printed out) to get the graph source.

clang -cc1 -ast-view your_file.c

You can also print to the command line with:

clang -cc1 -ast-dump your_file.c

or:

clang -cc1 -ast-print your_file.c

or in 3.3:

clang -cc1 -ast-dump-xml your_file.c

but this was removed later as pointed by Lukas Kubanek in the comment.

Shawn answered 3/9, 2013 at 10:17 Comment(4)
The XML printer is no longer supported. See llvm.org/viewvc/llvm-project?view=revision&revision=127141Snobbery
-ast-view also seems to require compile time support, which is not on by default on Ubuntu 14.04 :-( -ast-dump works beautifully. What is -ast-print supposed to do? It just prints the code itself with some empty lines afterwards.Sorkin
According to the revision comment the xml printer never actually worked properly.Reinold
In recent clang you can also use -ast-dump=json, very useful! You can add this to your answer, if you don't mind.Uncharted
B
73

The method with -cc1 invocation will have problem with includes and recognizing C++.

For full-featured parsing, use:

clang -Xclang -ast-dump file.cpp
Breakfast answered 16/9, 2017 at 17:46 Comment(1)
Ah, this is the switch I always forget. This prints the AST with pretty colours in Windows ^^Anticlimax
S
60

Clang supports showing the AST with Graphviz's dotty -- you can grab the temporary .dot file generated (name is printed out) to get the graph source.

clang -cc1 -ast-view your_file.c

You can also print to the command line with:

clang -cc1 -ast-dump your_file.c

or:

clang -cc1 -ast-print your_file.c

or in 3.3:

clang -cc1 -ast-dump-xml your_file.c

but this was removed later as pointed by Lukas Kubanek in the comment.

Shawn answered 3/9, 2013 at 10:17 Comment(4)
The XML printer is no longer supported. See llvm.org/viewvc/llvm-project?view=revision&revision=127141Snobbery
-ast-view also seems to require compile time support, which is not on by default on Ubuntu 14.04 :-( -ast-dump works beautifully. What is -ast-print supposed to do? It just prints the code itself with some empty lines afterwards.Sorkin
According to the revision comment the xml printer never actually worked properly.Reinold
In recent clang you can also use -ast-dump=json, very useful! You can add this to your answer, if you don't mind.Uncharted
M
6

For viewing the AST

clang-check -ast-dump filename.c

For to view the specific functions in a program

clang-check -ast-dump -ast-dump-filter=function_name filename.c

Mujik answered 26/8, 2019 at 12:24 Comment(0)
M
3

I am using following:

clang my_file.h -I. -Xclang -ast-dump -fsyntax-only -fno-color-diagnostics -Wno-visibility

IMHO This is more suitable for machine parsing.

Mallorca answered 17/9, 2019 at 20:7 Comment(1)
When I run this on a single file from a library I get errors about missing header files, even with the -fsyntax-only flag. Any ideas?Putto

© 2022 - 2024 — McMap. All rights reserved.