[clang] Add an off-by-default warning to complain about MSVC bitfield padding (PR #117428)

Reid Kleckner via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 26 11:29:20 PST 2024


================
@@ -19213,6 +19213,29 @@ void Sema::ActOnFields(Scope *S, SourceLocation RecLoc, Decl *EnclosingDecl,
 
     if (Record && FD->getType().isVolatileQualified())
       Record->setHasVolatileMember(true);
+    auto IsNonDependentBitField = [](const FieldDecl *FD) {
+      if (!FD->isBitField())
+        return false;
+      if (FD->getType()->isDependentType())
+        return false;
+      return true;
+    };
+
+    if (Record && PreviousField && IsNonDependentBitField(FD) &&
+        IsNonDependentBitField(*PreviousField)) {
+      unsigned FDStorageSize =
+          Context.getTypeSizeInChars(FD->getType()).getQuantity();
----------------
rnk wrote:

`CharUnits` are 64-bit, so you've got an implicit truncation here, not that anyone would ever define an `i(1<<32)` type and use it in a bit field. However, I think it's cleaner and more straightforward to skip `getQuantity` and just compare the CharUnits directly for equality. I'm also curious if the diagnostic printer can handle CharUnits, maybe it can, but if not, that's only one extra getQuantity call.

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


More information about the cfe-commits mailing list