[Bug c/25995] switch/case does not detect invalid enum values if default-case is used

2006-07-08 Thread roger at eyesopen dot com


--- 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



[Bug c++/25995] switch/case does not detect invalid enum values if default-case is used

2006-01-27 Thread pinskia at gcc dot gnu dot org


--- Comment #1 from pinskia at gcc dot gnu dot org  2006-01-27 13:57 ---
Why do you think we should warn here?  The enum is promoted to an int by the
C/C++ standard for switch.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25995



[Bug c/25995] switch/case does not detect invalid enum values if default-case is used

2006-01-27 Thread pinskia at gcc dot gnu dot org


--- Comment #2 from pinskia at gcc dot gnu dot org  2006-01-27 14:03 ---
Never mind, I see what you mean now, I was too tired when I first read this
bug.

Confirmed.  the default label is causing many things to be ignored.


-- 

pinskia at gcc dot gnu dot org changed:

   What|Removed |Added

 Status|UNCONFIRMED |NEW
  Component|c++ |c
 Ever Confirmed|0   |1
   Keywords||diagnostic
  Known to fail||3.2.3 3.3.3 3.4.0 4.0.0
   ||4.1.0
   Last reconfirmed|-00-00 00:00:00 |2006-01-27 14:03:27
   date||


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25995