[clang] [clang][CodeGen] Zero init unspecified fields in initializers in C (PR #97121)

Eli Friedman via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 27 13:43:06 PDT 2024


================
@@ -361,6 +368,13 @@ CodeGenFunction::AddInitializerToStaticVarDecl(const VarDecl &D,
     }
     return GV;
   }
+  if (!getLangOpts().CPlusPlus) {
+    // In C, when an initializer is given, the Linux kernel relies on clang to
+    // zero-initialize all members not explicitly initialized, including padding
+    // bits.
----------------
efriedma-quic wrote:

Adding handling for C++ is tricky: C++ allows fields and base classes to overlap.  For static initialization, it's straightforward to check for overlap, but for dynamic initialization it's problematic.  In some cases (AggValueSlot::DoesNotOverlap), we can prove there is no overlap, but in general it's ambiguous.   In such cases, we have to leave padding uninitialized.  So I think sticking to C for the first iteration makes sense; we'll have to carefully consider exactly what guarantees we can provide for C++.

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


More information about the cfe-commits mailing list