Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run GCC/Clang for static analysis? (warnings only)

Without compiling code, I would like GCC or Clang to report warnings.
Is it possible to run the compiler for static analysis only?
I can't find a way to pass the compiler warning flags and tell it not to compile.

edit: just found that clang has a static analyser

like image 505
Trevor Hickey Avatar asked Dec 28 '12 16:12

Trevor Hickey


People also ask

Which option of GCC inhibit all warning messages?

If -Wfatal-errors is also specified, then -Wfatal-errors takes precedence over this option. Inhibit all warning messages. Make all warnings into errors.

Which GCC flag is used to enable all compiler warnings?

gcc -Wall enables all compiler's warning messages. This option should always be used, in order to generate better code.

Which option can be used to display compiler warnings?

You can use a #pragma warning directive to control the level of warning that's reported at compile time in specific source files. Warning pragma directives in source code are unaffected by the /w option.


2 Answers

Both GCC and Clang have an option -fsyntax-only that makes the compiler only perform syntax checking without any actual compilation.

like image 189
Kerrek SB Avatar answered Oct 14 '22 15:10

Kerrek SB


In addition to the other replies, gcc is doing some analysis during compilation (and even during some optimization passes). So you could discard the generated code and still get all the warnings with e.g. gcc -Wall -O -c code.c -o /dev/null

Notice that you could extend GCC with your additional passes doing some additional, application specific, checks and warnings, e.g. with MELT (a high level domain specific language to extend GCC).

If you want strong static analysis and are willing to give additional annotations for that purpose consider also Frama C.

like image 40
Basile Starynkevitch Avatar answered Oct 14 '22 13:10

Basile Starynkevitch