This is the mail archive of the
gdb@sources.redhat.com
mailing list for the GDB project.
[Fwd: -Wswitch vs -Wuninitialized]
- To: GDB Discussion <gdb at sourceware dot cygnus dot com>
- Subject: [Fwd: -Wswitch vs -Wuninitialized]
- From: Andrew Cagney <ac131313 at cygnus dot com>
- Date: Mon, 20 Nov 2000 15:22:21 +1100
Just FYI,
I've posted this to the GCC discussion list. It is directly relevant to
the GDB group as I'm already seeing this problem. The file remote.c
contains a case very like the example ``code#2''.
I'm trying to get a better understanding of the underlying issues so
that, like -Wunused vs -Wunused-params, we do more than just cover our
eyes and ignore the problem :-)
enjoy,
Andrew
- To: gcc at gcc dot gnu dot org
- Subject: -Wswitch vs -Wuninitialized
- From: Andrew Cagney <ac131313 at cygnus dot com>
- Date: Mon, 20 Nov 2000 15:08:32 +1100
- Cc: Andrew Cagney <ac131313 at cygnus dot com>
Hello,
I'm looking at the interaction between -Wswitch and -Wuninitialized.
Given the ``buggy'' code:
code#1
enum e { E_A, E_B, E_C };
int
foo (enum e ev)
{
int i;
switch (ev)
{
case E_A: i = 1; break;
case E_B: i = 2; break;
}
return i;
}
GCC (2.95.2 19991024) reports:
enumeration value `E_C' not handled in switch
`i' might be used uninitialized in this function
Adding the missing enum vis:
code#2
...
switch (ev)
{
case E_A: i = 1; break;
case E_B: i = 2; break;
case E_C: i = 3; break;
}
...
still gives the warning:
`i' might be used uninitialized in this function
If instead a ``default:'' case had been present:
code#3
switch (ev)
{
case E_A: i = 1; break;
case E_B: i = 2; break;
default: abort (); /* bad switch */
}
the -Wuninitialized issue is addressed (GCC knows that all branches are
covered). Unfortunatly, it also supresses the -Wswitch warning - which
may still be a second bug.
Has anyone looked at refining the way -Wswitch works so that it is still
useful in cases such as the above? Alternativly, is there a suggested
coding pratice that addreses this?
enjoy,
Andrew