views:

404

answers:

3

In gcc, certain warnings require optimization to be enabled. For example:

int foo() {
    int x;
    return x;
}

In order to detect the uninitialized variable, -O must be passed.

$ gcc -W -Wall -c test.c
$ gcc -W -Wall -c test.c -O
test.c: In function ‘foo’:
test.c:3: warning: ‘x’ is used uninitialized in this function

However, this can interfere with debugging. Is there a way to enable just the analysis phases needed for warnings (and not just this particular warning, but as many as possible), without affecting the generated code too much?

I'm using gcc version 4.3.3 (Ubuntu 4.3.3-5ubuntu4) on x86-64.

+2  A: 

Try using -Wall instead of -W. -W is deprecated IIRC. (As Jonathan Leffler points out in a comment, -W's replacement is -Wextra, not -Wall.)

[Edit]

-Wunused-variable
Warn whenever a local variable or non-constant static variable is unused aside from its declaration. This warning is enabled by -Wall.

http://gcc.gnu.org/onlinedocs/gcc-4.4.0/gcc/Warning-Options.html#Warning-Options

[Edit]

This behavior has changed in GCC 4.4:

Uninitialized warnings do not require enabling optimization anymore, that is, -Wuninitialized can be used together with -O0. Nonetheless, the warnings given by -Wuninitialized will probably be more accurate if optimization is enabled.

Bastien Léonard
As shown in my example above, passing -W -Wall is insufficient to perform dataflow analysis with optimization disabled
bdonlan
I don't have this problem. Whenever I use -Wall, I get the warning (but not with -W). Which GCC version are you using?
Bastien Léonard
Added my gcc version to the question.
bdonlan
Updated with an example.
Bastien Léonard
FWIW, I get the same result as bdonlan on gcc 3.4.4 on cygwin 32bit. No warning without an optimization level of 1 or more.
Steve Jessop
@Bastien: aha - you changed the code. You made x completely unreferenced, and got the unused variable warning. bdonlan is after the uninitialized warning.
Steve Jessop
Got it, it's new in 4.4. See here, under the title “C family”: http://gcc.gnu.org/gcc-4.4/changes.html.
Bastien Léonard
@onebyone: luckily, the same thing seems to apply to both warnings. But I really need to sleep...
Bastien Léonard
Nah, you get the unused variable warning without -O since forever, since that doesn't require DFA, just flagging each variable. Congratulations: you're psychic to have known that 4.4 is different for uninitialized, without actually provoking that warning :-)
Steve Jessop
-W is deprecated; its replacement is -Wextra, though, not -Wall.
Jonathan Leffler
Removed the command-line session, since it's useless to the PO. Added some information provided in the comments.
Bastien Léonard
A: 
Norman Ramsey
A: 

This is what you have your automated build for. Let your automated build engine build with -Werror -Wall -O2, and you'll catch all the warnings triggered by higher optimization levels.

JesperE
For small hobby projects, I don't necessarily have an automated build engine...
bdonlan
Why not? You like pain?
Jonathan Leffler
A system such as Hudson is *really* easy to setup and configure, you can without problem use it even for small hobby projects.
JesperE