This is the mail archive of the gdb-patches@sourceware.cygnus.com mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Clean up compiler warnings


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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]