This is the mail archive of the
gdb-patches@sourceware.cygnus.com
mailing list for the GDB project.
Re: Clean up compiler warnings
- To: Andrew Cagney <ac131313 at cygnus dot com>
- Subject: Re: Clean up compiler warnings
- From: jtc at redback dot com (J.T. Conklin)
- Date: 07 Feb 2000 15:50:59 -0800
- Cc: Jim Kingdon <kingdon at redhat dot com>, gdb-patches at sourceware dot cygnus dot com
- References: <200002071950.OAA29599@devserv.devel.redhat.com> <389F583E.65831CE5@cygnus.com>
- Reply-To: jtc at redback dot com
>>>>> "Andrew" == Andrew Cagney <ac131313@cygnus.com> writes:
Andrew> When switching on an enum (as in this case) I've typically
Andrew> found it better to explicitly list all cases rather than use a
Andrew> default. That way, when a new element is added to the enum,
Andrew> the -Wswitch warning is available as a tool to the programmer
Andrew> to identify where code needs to be altered. As an aside, that
Andrew> is why a switch on an enum rather than an if/elsif chain often
Andrew> proves more resilient to long term maintenance.
What happens when an enum gets assigned a value that's not in the set
of elements? If there isn't a default case, we won't catch the error.
For example:
enum foo {
RED,
GREEN,
BLUE
} foo;
...
foo = 10; /* bogus assignment */
...
switch (foo) {
case RED:
...; break;
case GREEN:
...; break;
case BLUE:
...; break;
}
Without a default case, program flow will continue after the switch
statement. With one, your -Wswitch trick won't work. I'm not sure
which is worse.
--jtc
--
J.T. Conklin
RedBack Networks