[clang] [CIR] Handle default arguments in ctors (PR #168649)
Andy Kaylor via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 19 11:00:17 PST 2025
https://github.com/andykaylor updated https://github.com/llvm/llvm-project/pull/168649
>From 93aee4ffd29fd6ca0570e04f90e8009594faccb2 Mon Sep 17 00:00:00 2001
From: Andy Kaylor <akaylor at nvidia.com>
Date: Tue, 18 Nov 2025 18:33:51 -0800
Subject: [PATCH 1/2] [CIR] Handle default arguments in ctors
This adds the scalar expression visitor needed to handle default
arguments being passed to constructors.
---
clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 4 ++++
clang/test/CIR/CodeGen/defaultarg.cpp | 22 ++++++++++++++++++++++
2 files changed, 26 insertions(+)
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index f777562ba6309..26e329250b6f3 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -711,6 +711,10 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
return Visit(e->getSubExpr());
}
+ mlir::Value VisitCXXDefaultArgExpr(CXXDefaultArgExpr *dae) {
+ CIRGenFunction::CXXDefaultArgExprScope Scope(cgf, dae);
+ return Visit(dae->getExpr());
+ }
mlir::Value VisitCXXDefaultInitExpr(CXXDefaultInitExpr *die) {
CIRGenFunction::CXXDefaultInitExprScope scope(cgf, die);
return Visit(die->getExpr());
diff --git a/clang/test/CIR/CodeGen/defaultarg.cpp b/clang/test/CIR/CodeGen/defaultarg.cpp
index 807230bd003f5..29c929ccd7838 100644
--- a/clang/test/CIR/CodeGen/defaultarg.cpp
+++ b/clang/test/CIR/CodeGen/defaultarg.cpp
@@ -30,3 +30,25 @@ void foo() {
// OGCG: %[[TMP0:.*]] = alloca i32
// OGCG: store i32 42, ptr %[[TMP0]]
// OGCG: call void @_Z3barRKi(ptr {{.*}} %[[TMP0]])
+
+struct S
+{
+ S(int n = 2);
+};
+
+void test_ctor_defaultarg() {
+ S s;
+}
+
+// CIR: cir.func {{.*}} @_Z20test_ctor_defaultargv()
+// CIR: %[[S:.*]] = cir.alloca !rec_S, !cir.ptr<!rec_S>, ["s", init]
+// CIR: %[[TWO:.*]] = cir.const #cir.int<2> : !s32i
+// CIR: cir.call @_ZN1SC1Ei(%[[S]], %[[TWO]]) : (!cir.ptr<!rec_S>, !s32i) -> ()
+
+// LLVM: define{{.*}} @_Z20test_ctor_defaultargv()
+// LLVM: %[[S:.*]] = alloca %struct.S
+// LLVM: call void @_ZN1SC1Ei(ptr %[[S]], i32 2)
+
+// OGCG: define{{.*}} @_Z20test_ctor_defaultargv()
+// OGCG: %[[S:.*]] = alloca %struct.S
+// OGCG: call void @_ZN1SC1Ei(ptr{{.*}} %[[S]], i32 {{.*}} 2)
>From 7636f83b1d6260710d7f4c81d667bc3e0d8d20b4 Mon Sep 17 00:00:00 2001
From: Andy Kaylor <akaylor at nvidia.com>
Date: Wed, 19 Nov 2025 10:59:49 -0800
Subject: [PATCH 2/2] Fix variable naming
---
clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index 26e329250b6f3..382d9b2a06315 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -712,7 +712,7 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
}
mlir::Value VisitCXXDefaultArgExpr(CXXDefaultArgExpr *dae) {
- CIRGenFunction::CXXDefaultArgExprScope Scope(cgf, dae);
+ CIRGenFunction::CXXDefaultArgExprScope scope(cgf, dae);
return Visit(dae->getExpr());
}
mlir::Value VisitCXXDefaultInitExpr(CXXDefaultInitExpr *die) {
More information about the cfe-commits
mailing list