[clang] [OpenMP] Fix OpenMP reduction segfault with non-copyable types and user initializers. (PR #219265)

Alexey Bataev via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 13 10:39:38 PDT 2026


================
@@ -24377,6 +24378,39 @@ VarDecl *SemaOpenMP::ActOnOpenMPDeclareReductionInitializerStart(Scope *S,
 void SemaOpenMP::ActOnOpenMPDeclareReductionInitializerEnd(
     Decl *D, Expr *Initializer, VarDecl *OmpPrivParm) {
   auto *DRD = cast<OMPDeclareReductionDecl>(D);
+
+  // For non-trivial types with user initializers, build an AST that
+  // includes default construction first to initialize members before user
+  // initializer. This must be done before popping contexts.
+  if (Initializer && !DRD->getDeclContext()->isDependentContext()) {
+    QualType ReductionType = DRD->getType();
+    if (const auto *RD = ReductionType->getAsCXXRecordDecl()) {
+      CXXConstructorDecl *DefaultCtor =
+          SemaRef.LookupDefaultConstructor(const_cast<CXXRecordDecl *>(RD));
----------------
alexey-bataev wrote:

```suggestion
    if (CXXRecordDecl *RD = ReductionType->getAsCXXRecordDecl()) {
      CXXConstructorDecl *DefaultCtor = SemaRef.LookupDefaultConstructor(RD);
```

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


More information about the cfe-commits mailing list