[clang] Fix zero initialization of virtual bases in delegating constructors (PR #235011)
Nikunj Pokharel via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 23 22:41:15 PDT 2026
https://github.com/Nikunj-Pokharel updated https://github.com/llvm/llvm-project/pull/235011
>From 0cdbd5874830d9d966b8975ef29f64a695b7b06f Mon Sep 17 00:00:00 2001
From: Xen00n <cs26btkmu11001 at iith.ac.in>
Date: Fri, 23 Oct 2026 19:59:25 +0530
Subject: [PATCH 1/2] Fix zero initialization of virtual bases in delegating
constructors
---
clang/lib/CodeGen/CGExprCXX.cpp | 6 ++++++
clang/test/CodeGenCXX/issue-234795.cpp | 30 ++++++++++++++++++++++++++
2 files changed, 36 insertions(+)
create mode 100644 clang/test/CodeGenCXX/issue-234795.cpp
diff --git a/clang/lib/CodeGen/CGExprCXX.cpp b/clang/lib/CodeGen/CGExprCXX.cpp
index 60bb27a6fab69d..e41f5c24da1c79 100644
--- a/clang/lib/CodeGen/CGExprCXX.cpp
+++ b/clang/lib/CodeGen/CGExprCXX.cpp
@@ -633,6 +633,12 @@ void CodeGenFunction::EmitCXXConstructExpr(const CXXConstructExpr *E,
if (E->requiresZeroInitialization() && !Dest.isZeroed()) {
switch (E->getConstructionKind()) {
case CXXConstructionKind::Delegating:
+ if (CurGD.getCtorType() == Ctor_Base)
+ EmitNullBaseClassInitialization(*this, Dest.getAddress(),
+ CD->getParent());
+ else
+ EmitNullInitialization(Dest.getAddress(), E->getType());
+ break;
case CXXConstructionKind::Complete:
EmitNullInitialization(Dest.getAddress(), E->getType());
break;
diff --git a/clang/test/CodeGenCXX/issue-234795.cpp b/clang/test/CodeGenCXX/issue-234795.cpp
new file mode 100644
index 00000000000000..c1ec6db6b54025
--- /dev/null
+++ b/clang/test/CodeGenCXX/issue-234795.cpp
@@ -0,0 +1,30 @@
+// RUN: %clang_cc1 -std=c++17 -O0 -emit-llvm -o - %s | FileCheck %s
+
+struct A {
+ int x = 42;
+ virtual ~A() {}
+};
+
+struct B : virtual A {
+ explicit B(int) : B() {}
+ B() = default;
+};
+
+struct D : B {
+ D() : B(0) {}
+};
+
+int main() {
+ D d;
+ B b(0);
+ return d.x + b.x;
+}
+
+// The complete-object constructor initializes the full object.
+// CHECK-LABEL: define{{.*}} @_ZN1BC1Ei
+// CHECK: call void @llvm.memset{{.*}}i64 24
+
+// The base-object constructor must not zero-initialize its virtual base.
+// CHECK-LABEL: define{{.*}} @_ZN1BC2Ei
+// CHECK: call void @llvm.memset{{.*}}i64 8
+// CHECK: call{{.*}} @_ZN1BC2Ev
\ No newline at end of file
>From 80ff5c6354ca91c8d19204faf0e9dce647ad5481 Mon Sep 17 00:00:00 2001
From: Xen00n <cs26btkmu11001 at iith.ac.in>
Date: Sat, 24 Oct 2026 11:10:57 +0530
Subject: [PATCH 2/2] Fix Windows compatibility of virtual base initialization
test
---
clang/test/CodeGenCXX/issue-234795.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/test/CodeGenCXX/issue-234795.cpp b/clang/test/CodeGenCXX/issue-234795.cpp
index c1ec6db6b54025..2c3a511c28e145 100644
--- a/clang/test/CodeGenCXX/issue-234795.cpp
+++ b/clang/test/CodeGenCXX/issue-234795.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -std=c++17 -O0 -emit-llvm -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++17 -O0 -emit-llvm -o - %s | FileCheck %s
struct A {
int x = 42;
More information about the cfe-commits
mailing list