[clang] [Sema] Add check for bitfield assignments to larger integral types (PR #68276)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 6 09:23:21 PDT 2023


================
@@ -6171,6 +6171,9 @@ def warn_signed_bitfield_enum_conversion : Warning<
   "signed bit-field %0 needs an extra bit to represent the largest positive "
   "enumerators of %1">,
   InGroup<BitFieldEnumConversion>, DefaultIgnore;
+def warn_bitfield_too_small_for_integral_type : Warning<
+  "bit-field %0 (%1 bits) is not wide enough to store type of %2 ">,
+  InGroup<Conversion>, DefaultIgnore;
----------------
AaronBallman wrote:

We typically do not add diagnostics that are opt-in because we've had evidence in the past that they are not enabled enough to be worth the maintenance costs. So the goal is to have very, very few false positives -- for things that are more prone to false positives, we usually put them in clang-tidy instead. However, this one is a bit of an outlier because many diagnostics under `-Wconversion` are ignored by default. So, I think this one may actually be fine to be off-by-default, but I do expect it to be quite chatty.

Have you tried seeing how many diagnostics are generated when building Clang itself? It would be interesting to see what kind of results that gives you because we use bit-fields relatively frequently.

Btw, I think the GCC phrasing for the diagnostic is an improvement over the current wording. (You don't store a type, so that's a bit awkward.)

https://github.com/llvm/llvm-project/pull/68276


More information about the cfe-commits mailing list