56

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?

Stanislav Pankevich
  • 11,044
  • 8
  • 69
  • 129
username_4567
  • 4,737
  • 12
  • 56
  • 92

4 Answers4

68

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

For full-featured parsing, use:

clang -Xclang -ast-dump file.cpp
Kornel
  • 97,764
  • 37
  • 219
  • 309
  • 4
    Ah, this is the switch I always forget. This prints the AST with pretty colours in Windows ^^ – Cameron Oct 18 '17 at 13:43
59

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.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
cpt. jazz
  • 1,336
  • 12
  • 21
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

Jon marsh
  • 279
  • 7
  • 12
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.

shapkin
  • 358
  • 3
  • 10