[clang] Reapply "[clang][CodeGen] Zero init unspecified fields in initializers in C" (#109898) (PR #110051)

Eli Friedman via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 26 17:28:10 PDT 2024


================
@@ -1748,6 +1771,26 @@ void AggExprEmitter::VisitCXXParenListOrInitListExpr(
         CGF.getTypes().isZeroInitializable(ExprToVisit->getType()))
       break;
 
+    if (ZeroInitPadding) {
+      uint64_t StartBitOffset = Layout.getFieldOffset(field->getFieldIndex());
+      CharUnits StartOffset =
+          CGF.getContext().toCharUnitsFromBits(StartBitOffset);
+      DoZeroInitPadding(SizeSoFar, StartOffset - SizeSoFar);
+      if (!field->isBitField()) {
+        CharUnits FieldSize =
+            CGF.getContext().getTypeSizeInChars(field->getType());
+        SizeSoFar = StartOffset + FieldSize;
+      } else {
+        const CGRecordLayout &RL =
+            CGF.getTypes().getCGRecordLayout(field->getParent());
+        const CGBitFieldInfo &Info = RL.getBitFieldInfo(field);
+        uint64_t EndBitOffset = StartBitOffset + Info.Size;
+        SizeSoFar = CGF.getContext().toCharUnitsFromBits(EndBitOffset);
+        if (EndBitOffset % CGF.getContext().getCharWidth() != 0) {
+          SizeSoFar++;
----------------
efriedma-quic wrote:

I'm not sure this actually initializes everything that needs to be initialized?  If you end up with padding between bitfields for whatever reason, nothing actually writes those bits, I think.

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


More information about the cfe-commits mailing list