[clang] [clang] Handle constructor closures with consteval default args (PR #203554)

Hans Wennborg via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 1 05:11:17 PDT 2026


================
@@ -19804,3 +19803,36 @@ void Sema::ActOnFinishFunctionDeclarationDeclarator(Declarator &Declarator) {
   }
   InventedParameterInfos.pop_back();
 }
+
+bool Sema::BuildCtorClosureDefaultArgs(SourceLocation Loc,
+                                       CXXConstructorDecl *Ctor, bool IsCopy) {
+  assert(Context.getTargetInfo().getCXXABI().isMicrosoft());
+
+  if (!Ctor->getCtorClosureDefaultArgs().empty()) {
+    // If we build args for default constructor closures, those will have
+    // been generated *before* building args for any copy constructor closures.
+    assert(IsCopy || Ctor->getCtorClosureDefaultArgs()[0] != nullptr);
+    return false;
+  }
+
+  unsigned NumParams = Ctor->getNumParams();
+  if (NumParams == 0)
+    return false;
+
+  CXXDefaultArgExpr **Args =
+      new (getASTContext()) CXXDefaultArgExpr *[NumParams];
+
+  if (IsCopy)
+    Args[0] = nullptr; // Copy ctor closure will provide the first argument.
+
+  for (unsigned I = IsCopy ? 1 : 0; I != NumParams; ++I) {
+    ExprResult R = BuildCXXDefaultArgExpr(Loc, Ctor, Ctor->getParamDecl(I));
+    CleanupVarDeclMarking();
----------------
zmodem wrote:

Another way to view this: When calling constructors in regular code, the call will be wrapped in `Sema::MaybeCreateExprWithCleanups()` which takes care of calling `CleanupVarDeclMarking()`.

In the case of these closures, there is no call expression to wrap with `MaybeCreateExprWithCleanups()`, so it makes sense to call `CleanupVarDeclMarking()` ourselves.

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


More information about the cfe-commits mailing list