This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
-Wswitch vs -Wuninitialized
- 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