https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51242

Sasha B <sashab at chromium dot org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |sashab at chromium dot org

--- Comment #28 from Sasha B <sashab at chromium dot org> ---
I contribute to Chromium and have also hit problems such as Comment 27. I'm in
the process of submitting a patch to Clang (https://reviews.llvm.org/D24289)
that warns when enums used in bitfields are not given an unsigned underlying
type, since MSVC defaults to signed underlying types and this gives
inconsistencies when reading/writing the bitfield in multi-platform code.

With the patch, the following code:

enum Foo { A, B };
struct Bar { Foo x : 1 };

will give the following warning with Clang:
"enums in the Microsoft ABI are signed integers by default; consider giving the
enum Foo an unsigned underlying type to make this code portable"

But then if it's changed to

enum Foo : unsigned { A, B };
struct Bar { Foo x : 1 };

GCC will give the warning:
"'Bar::x' is too small to hold all values of 'enum Foo'"

The C++ standard says:
9.6.4 says: If the value of an enumerator is stored into a bit-field of the
same enumeration type and the number of bits in the bit-field is large enough
to hold all the values of that enumeration type (7.2), the original enumerator
value and the value of the bit-field shall compare equal.

You can disregard whether the underlying type is fixed or not. So, GCC should
not give a warning unless a bitfield containing Foo really is too small to hold
a given value of Foo, warning on the assignment makes sense.

Reply via email to