[PATCH] D24289: Add warning when assigning enums to bitfields without an explicit unsigned underlying type

Sasha Bermeister via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 8 16:40:13 PDT 2016


sashab updated this revision to Diff 70760.
sashab marked an inline comment as done.
sashab added a comment.

Thanks for your feedback everyone; left the flag as DefaultIgnore but added it to -Wall.

Keep in mind I am planning on adding two additional warnings after this, namely
"%0 is too small to hold all values of %1"
and
"%0 is too large to hold all values of %1"
for all enum bitfields.

Also, just going back to rnk's original comment, I don't think I properly replied -- you are correct that the following code behaves strangely:

  enum E : signed { E1, E2 };
  struct { E e1 : 1; E e2; F f1 : 1; F f2; } s;
  s.e2 = E2; // comes out as -1 instead of 1

If you are storing signed enums in a bitfield, you need 1 additional bit to store the sign bit otherwise the enums will not serialize back correctly. This is equivalent to storing unsigned enums in bitfields that are too small; the whole value is not stored.

This is handled by the warning I'm adding next "%0 is too small to hold all values of %1", which can fire for signed enum bitfields when you don't store max(numberOfBitsNeededForUnsignedValues, numberOfBitsNeededForSignedValues) + 1.


https://reviews.llvm.org/D24289

Files:
  include/clang/Basic/DiagnosticGroups.td
  include/clang/Basic/DiagnosticSemaKinds.td
  lib/Sema/SemaChecking.cpp
  test/SemaCXX/warn-msvc-enum-bitfield.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24289.70760.patch
Type: text/x-patch
Size: 4364 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160908/8d948013/attachment.bin>


More information about the cfe-commits mailing list