This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/25995] switch/case does not detect invalid enum values if default-case is used
- From: "roger at eyesopen dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 8 Jul 2006 14:31:33 -0000
- Subject: [Bug c/25995] switch/case does not detect invalid enum values if default-case is used
- References: <bug-25995-12084@http.gcc.gnu.org/bugzilla/>
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
------- Comment #3 from roger at eyesopen dot com 2006-07-08 14:31 -------
I tried fixing this bug, only to discover why things are exactly as they are.
The short answer is that GCC will warn about the example code if you
specify the -Wswitch-enum command line option. Specifying -Wall implies
the weaker -Wswitch, which intentionally disables the checking of enumeration
literals in switch statements.
But why would anyone want to disable warning for the example code, I thought
to myself, until bootstrapping GCC itself discovered a large number of cases
identical to the one reported. Internally, GCC itself uses an enumeration
called tree_code that tracks the different types of node of GCC's abstract
syntax tree (AST). However, numerous front-ends, supplement this enumeration
with their own front-end specific tree codes, for example,
COMPOUND_LITERAL_EXPR. Hence, the various GCC front-ends are littered with
source code that looks like:
switch (TREE_CODE (t))
{
case COMPOUND_LITERAL_EXPR:
...
where the case value isn't one of the values of the original enum tree_code
enumeration. Similar problems appeared in dwarf2out.c and other GCC source
files. At first I started changing things to "switch ((int) TREE_CODE (t))"
to silence the warning, but quickly became overwhelmed by the number of
source files that needed updating.
Hence, the current status quo. GCC uses the "default: break;" idiom to
indicate which switch statements may be bending the rules, to turn off this
warning with the default -Wall/-Wswitch used during bootstrap. Well written
user code, on the other hand, should probably always use -Wswitch-enum.
If you read the documentation of -Wswitch vs. -Wswitch-enum, you'll see that
the disabling of these warnings when a default case is specified, is a curious
"feature", purely to aid GCC to compile itself. As Andrew Pinskia points out
in comment #2, it's valid C/C++ so shouldn't warrant an immediate warning, so
the explicit -Wswitch-enum, requesting stricter checking seems reasonable.
I hope this helps, and the -Wswitch-enum fulfils this enhancement request.
--
roger at eyesopen dot com changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |RESOLVED
Resolution| |INVALID
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25995