This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/46457] New: Bogus warning about bitwise combination of enum flags in case statement


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

           Summary: Bogus warning about bitwise combination of enum flags
                    in case statement
           Product: gcc
           Version: 4.5.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: bhutchings@solarflare.com


The -Wswitch warning option is documented as resulting in warnings about
"`case' labels outside the enumeration range". This appears to be a change from
gcc 4.4. I have no objection to the documented behaviour, but the actual
behaviour is to warn about any value that does not equal an enumerator.

It is common practice to define flags as enumerators and to combine them with
the '|' operator. The C++98 standard supports this practice by requiring that
the underlying type for an enumerated type must be capable of storing such
bitwise combinations and zero (7.2p6).

Test case:

enum flags {
    one = 1,
    two = 2,
};

const char *f(enum flags fl)
{
    switch (fl) {
    case 0:
    return "zero";
    case one:
    return "one";
    case two:
    return "two";
    case one | two:
    return "three";
    default:
    return "bogus";
    }
}

$ c++ -c -Wall test.cc
test.cc: In function âconst char* f(flags)â:
test.cc:9:5: warning: case value â0â not in enumerated type âflagsâ
test.cc:15:5: warning: case value â3â not in enumerated type âflagsâ

This also applies to the C front-end, though the C standard does not explicit
specify this requirement on the underlying type (or "compatible type").


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