[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
Mon Oct 14 13:57:09 PDT 2024
================
@@ -1774,6 +1795,54 @@ void AggExprEmitter::VisitCXXParenListOrInitListExpr(
}
}
}
+ if (ZeroInitPadding) {
+ uint64_t TotalSize = CGF.getContext().toBits(
+ Dest.getPreferredSize(CGF.getContext(), DestLV.getType()));
+ DoZeroInitPadding(PaddingStart, TotalSize, nullptr);
+ }
+}
+
+void AggExprEmitter::DoZeroInitPadding(uint64_t &PaddingStart,
+ uint64_t PaddingEnd,
+ const FieldDecl *NextField) {
+
+ auto InitBytes = [&](uint64_t StartBit, uint64_t EndBit) {
+ CharUnits Start = CGF.getContext().toCharUnitsFromBits(StartBit);
+ CharUnits End = CGF.getContext().toCharUnitsFromBits(EndBit);
+ Address Addr = Dest.getAddress().withElementType(CGF.CharTy);
+ if (!Start.isZero())
+ Addr = Builder.CreateConstGEP(Addr, Start.getQuantity());
+ llvm::Constant *SizeVal = Builder.getInt64((End - Start).getQuantity());
+ CGF.Builder.CreateMemSet(Addr, Builder.getInt8(0), SizeVal, false);
+ };
+
+ if (NextField != nullptr && NextField->isBitField()) {
----------------
efriedma-quic wrote:
I had to spend a little time checking this actually works... but I think you're fine here: we should do initialization in field order, and any out-of-order access is undefined behavior.
https://github.com/llvm/llvm-project/pull/110051
More information about the cfe-commits
mailing list