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?
The method with -cc1 invocation will have problem with includes and recognizing C++.
For full-featured parsing, use:
clang -Xclang -ast-dump file.cpp
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.
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
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.
Related
I'm trying to tailor compiler flags of clang-11 to the apple-m1 CPU, which clang-11 doesn't know about yet.
The output of /usr/bin/clang -E - -mcpu=apple-m1 -### on macOS outputs a command with flags like these in it: "-target-feature" "+zcz"
From that you can infer the following features of the CPU:
armv8.5a+fp-armv8+neon+crc+crypto+dotprod+fp16fml+ras+lse+rdm+rcpc+zcm+zcz+fullfp16+sm4+sha3+sha2+aes
However, out of these, +fp-armv8+neon+zcm+zcz+fullfp16 are not recognised to be valid by any clang compiler:
$ cc -march=armv8.5a+zcz test.c
clang-11: error: the clang compiler does not support '-march=armv8.5a+zcz'
How can I tell clang to optimise for those target flags?
clang -cc1 -analyze -analyzer-checker=debug.ViewCFG ***.c
I want to use ViewCFG to get a visual image of the CFG, and after executing the above command there is no file output and no error message. But DumpCFG works fine,like this
clang -cc1 -analyze -analyzer-checker=debug.DumpCFG ***.c
How should this problem be solved?
I'm trying to make sense of clang's AST output and can't really find any enlightenment on the web.
Yes, there is this page but its content is very limited and I can't really do much with their Doxygen documentation.
Command I'm using to generate the AST is:
$ clang -Xclang -ast-dump=json -fsyntax-only file.c
For example, when I run clang with clang ... -Wno-error -Werror, I don't want to it to show -Wno-error, since it's being overridden. I could use -v or -### to print out what the driver's executing, but that doesn't help because it still shows -Wno-error -Werror at the end. How might I show the computed compiler flags?
The -cc1 flag -compiler-options-dump appears to be the best option at the moment.
clang has an option, -x, which can be used to specify the language of subsequent source files passed to it. This caused problems when used like this:
clang -x c++ one.cc a.o b.o c.o
clang will try to interpret the object files a.o, b.o, c.o as source code.
Is there a way to cancel the effect of the -x option so I can pass object files on the same command line?
clang -x c++ one.cc SOMEOPTION a.o b.o c.o
What should SOMEOPTION be to allow clang to interpret the .o files as object files?
I need to use this convoluted command line because I am using a system that calls the compiler automatically to compile some code it generates and there are limits to how much it can be hacked.
could you put the arguments the other way 'round
clang a.o b.o c.o -x c++ one.cc
or compile each file and then link them in a later run
clang -x c++ one.cc -o one.cc.o
clang a.o b.o c.o one.cc.o
This is how in my experience it is actualy used.