Skip to content

Conversation

@andykaylor
Copy link
Contributor

This adds the scalar expression visitor needed to handle default arguments being passed to constructors.

This adds the scalar expression visitor needed to handle default
arguments being passed to constructors.
@llvmbot llvmbot added clang Clang issues not falling into any other category ClangIR Anything related to the ClangIR project labels Nov 19, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 19, 2025

@llvm/pr-subscribers-clangir

@llvm/pr-subscribers-clang

Author: Andy Kaylor (andykaylor)

Changes

This adds the scalar expression visitor needed to handle default arguments being passed to constructors.


Full diff: https://github.com/llvm/llvm-project/pull/168649.diff

2 Files Affected:

  • (modified) clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp (+4)
  • (modified) clang/test/CIR/CodeGen/defaultarg.cpp (+22)
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)

@github-actions
Copy link

🐧 Linux x64 Test Results

  • 112019 tests passed
  • 4077 tests skipped

Copy link
Contributor

@xlauko xlauko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm % nit

}

mlir::Value VisitCXXDefaultArgExpr(CXXDefaultArgExpr *dae) {
CIRGenFunction::CXXDefaultArgExprScope Scope(cgf, dae);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
CIRGenFunction::CXXDefaultArgExprScope Scope(cgf, dae);
CIRGenFunction::CXXDefaultArgExprScope scope(cgf, dae);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang Clang issues not falling into any other category ClangIR Anything related to the ClangIR project

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants