[clang] [Clang][OpenMP] Fix crash on an OpenMP directive inside a requires-expression or expansion statement (PR #247194)

Akash Manna via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 30 06:58:05 PST 2026


https://github.com/akash-manna-sky created https://github.com/llvm/llvm-project/pull/247194

Fixes #210018

`Sema::CreateCapturedStmtRecordDecl` walks up from `CurContext` to the nearest function, record or file context for the captured record, and appended the `CapturedDecl` to that context too, although it was created in `CurContext`. With anything else in between, such as a requires-expression body or, before #223005, an expansion statement, `addHiddenDecl` asserts.

Add the `CapturedDecl` to the context it was created in, which is what `PushDeclContext` and `PopDeclContext` already assume; the captured record keeps its placement.

AI Aided!

>From 0d901b805a42b2d89bc252558fd3b1c16a09c486 Mon Sep 17 00:00:00 2001
From: Akash Manna <akash.manna.mymail at gmail.com>
Date: Thu, 5 Nov 2026 16:26:56 +0530
Subject: [PATCH] Fixes #210018

`Sema::CreateCapturedStmtRecordDecl` walks up from `CurContext` to the nearest function, record or file context for the captured record, and appended the `CapturedDecl` to that context too, although it was created in `CurContext`. With anything else in between, such as a requires-expression body or, before #223005, an expansion statement, `addHiddenDecl` asserts.

Add the `CapturedDecl` to the context it was created in, which is what `PushDeclContext` and `PopDeclContext` already assume; the captured record keeps its placement.

LLM tools were used for this contribution.
---
 clang/docs/ReleaseNotes.md                 |   2 +
 clang/lib/Sema/SemaStmt.cpp                |   3 +-
 clang/test/OpenMP/GH210018.cpp             | 224 +++++++++++++++++++++
 clang/test/OpenMP/GH210018_codegen.cpp     |  65 ++++++
 clang/test/SemaCXX/captured-statements.cpp |  48 +++++
 5 files changed, 341 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/OpenMP/GH210018.cpp
 create mode 100644 clang/test/OpenMP/GH210018_codegen.cpp

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 6d1dbbbb6eb77b..e7e7867833341f 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1029,6 +1029,8 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
   using ``__is_constructible`` on a nested class template inside the definition
   of the containing class. (#GH215166)
 
+- Fixed a crash when an OpenMP directive appears inside a requires-expression or a C++26 expansion statement. (#GH210018)
+
 - Fixed a crash issue when a value dependent recovery init appeared in constant
   evaluation context in default constant evaluator.
 
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index 4bf5b3f3ec6b74..c4f4eec2be8f9f 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -4759,8 +4759,9 @@ Sema::CreateCapturedStmtRecordDecl(CapturedDecl *&CD, SourceLocation Loc,
   RD->startDefinition();
 
   assert(NumParams > 0 && "CapturedStmt requires context parameter");
+  // CurContext need not be DC (e.g. the body of a requires-expression).
   CD = CapturedDecl::Create(Context, CurContext, NumParams);
-  DC->addDecl(CD);
+  CurContext->addDecl(CD);
   return RD;
 }
 
diff --git a/clang/test/OpenMP/GH210018.cpp b/clang/test/OpenMP/GH210018.cpp
new file mode 100644
index 00000000000000..f628a04decd9d9
--- /dev/null
+++ b/clang/test/OpenMP/GH210018.cpp
@@ -0,0 +1,224 @@
+// RUN: %clang_cc1 -std=c++2c -fopenmp -fsyntax-only -verify %s
+// RUN: %clang_cc1 -std=c++2c -fopenmp-simd -fsyntax-only -verify %s
+// expected-no-diagnostics
+
+int g(int);
+struct S { int a; long b; };
+
+void simd_in_expansion() {
+  template for (auto x : {1, 2}) {
+#pragma omp simd
+    for (int i = 0; i < 2; i++)
+      ;
+  }
+}
+
+void directives_in_expansion(int n) {
+  template for (auto x : {1, 2}) {
+#pragma omp parallel
+    g(x);
+#pragma omp parallel for
+    for (int i = 0; i < x; i++)
+      g(x + n);
+#pragma omp for
+    for (int i = 0; i < x; i++)
+      g(i);
+#pragma omp target
+    g(x);
+#pragma omp target teams distribute parallel for
+    for (int i = 0; i < x; i++)
+      g(x);
+#pragma omp teams
+    g(x);
+#pragma omp task
+    g(x);
+#pragma omp taskloop
+    for (int i = 0; i < x; i++)
+      g(x);
+#pragma omp sections
+    {
+#pragma omp section
+      g(x);
+    }
+#pragma omp single
+    g(x);
+#pragma omp critical
+    g(x);
+    int v = 0;
+#pragma omp atomic
+    v += x;
+#pragma omp parallel
+    {
+#pragma omp for
+      for (int i = 0; i < x; i++)
+        g(i);
+#pragma omp task
+      g(x);
+    }
+#pragma omp parallel firstprivate(n)
+    g(x + n);
+#pragma omp for
+#pragma omp unroll partial(2)
+    for (int i = 0; i < 8; i++)
+      g(x + i);
+#pragma omp tile sizes(2)
+    for (int i = 0; i < 8; i++)
+      g(x + i);
+  }
+}
+
+void nested_expansion() {
+  template for (auto x : {1, 2}) {
+    template for (auto y : {3, 4}) {
+#pragma omp parallel for
+      for (int i = 0; i < x; i++)
+        g(x + y);
+    }
+  }
+}
+
+void destructuring_expansion(S s) {
+  template for (auto x : s) {
+#pragma omp parallel for
+    for (int i = 0; i < 2; i++)
+      g(x);
+  }
+  template for (constexpr auto x : {1, 2}) {
+#pragma omp simd
+    for (int i = 0; i < x; i++)
+      g(x);
+    static_assert(x > 0);
+  }
+  constexpr int arr[] = {1, 2, 3};
+  template for (auto x : arr) {
+#pragma omp parallel
+    g(x);
+  }
+}
+
+template <typename T>
+void expansion_in_template(T t) {
+  T local = t;
+  template for (auto x : {1, 2}) {
+#pragma omp parallel for
+    for (int i = 0; i < 2; i++)
+      g(x + local + i);
+  }
+}
+template void expansion_in_template<int>(int);
+
+template <typename... Ts>
+void dependent_expansion(Ts... ts) {
+  template for (auto x : {ts...}) {
+#pragma omp parallel
+    g(x);
+#pragma omp simd
+    for (int i = 0; i < x; i++)
+      g(x + i);
+  }
+}
+template void dependent_expansion<int, int, int>(int, int, int);
+
+template <typename T>
+void dependent_destructuring(T t) {
+  template for (auto x : t) {
+#pragma omp parallel for
+    for (int i = 0; i < 2; i++)
+      g(x);
+  }
+}
+template void dependent_destructuring<S>(S);
+
+template <typename T>
+struct C {
+  T m;
+  void mem() {
+    template for (auto x : {1, 2}) {
+#pragma omp parallel
+      g(x + m);
+    }
+  }
+};
+template struct C<int>;
+
+void expansion_in_lambda() {
+  auto l = [] {
+    template for (auto x : {1, 2}) {
+#pragma omp parallel
+      g(x);
+    }
+  };
+  l();
+}
+
+void lambda_and_local_class_in_region() {
+  template for (auto x : {1, 2}) {
+#pragma omp parallel
+    {
+      struct L { int m; } l{x};
+      [&] { g(l.m + x); }();
+    }
+  }
+}
+
+struct R { int v; };
+#pragma omp declare reduction(plus : R : omp_out.v += omp_in.v) initializer(omp_priv = {0})
+
+void declare_reduction_in_expansion() {
+  template for (auto x : {1, 2}) {
+#pragma omp declare reduction(mine : int : omp_out += omp_in) initializer(omp_priv = 0)
+    int r = 0;
+#pragma omp parallel reduction(mine : r)
+    r += x;
+    R rr{0};
+#pragma omp parallel reduction(plus : rr)
+    rr.v += x;
+  }
+}
+
+void lambda_in_expansion_control() {
+  template for (auto x : {1, 2}) {
+    [&] { return x; }();
+  }
+}
+
+void region_in_requires_expression() {
+  bool b = requires {
+    ({
+#pragma omp parallel
+      g(1);
+      7;
+    });
+  };
+  (void)b;
+}
+
+template <typename T>
+void region_in_requires_expression_in_template(T t) {
+  bool b = requires {
+    ({
+#pragma omp parallel for
+      for (int i = 0; i < 2; i++)
+        g(t + i);
+      7;
+    });
+  };
+  (void)b;
+}
+template void region_in_requires_expression_in_template<int>(int);
+
+void controls(int n) {
+#pragma omp parallel
+  g(n);
+  [&] {
+#pragma omp parallel for
+    for (int i = 0; i < n; i++)
+      g(i);
+  }();
+  struct Local {
+    void f(int m) {
+#pragma omp parallel
+      g(m);
+    }
+  };
+}
diff --git a/clang/test/OpenMP/GH210018_codegen.cpp b/clang/test/OpenMP/GH210018_codegen.cpp
new file mode 100644
index 00000000000000..6b5078f86d2daf
--- /dev/null
+++ b/clang/test/OpenMP/GH210018_codegen.cpp
@@ -0,0 +1,65 @@
+// RUN: %clang_cc1 -std=c++2c -fopenmp -triple x86_64-unknown-linux-gnu -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,OMP
+// RUN: %clang_cc1 -std=c++2c -fopenmp-simd -triple x86_64-unknown-linux-gnu -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,SIMD
+
+int g(int);
+
+void parallel_in_expansion(int n) {
+  template for (auto x : {1, 2}) {
+#pragma omp parallel
+    g(x + n);
+  }
+}
+
+void simd_in_expansion() {
+  template for (auto x : {1, 2}) {
+#pragma omp simd
+    for (int i = 0; i < x; i++)
+      g(i);
+  }
+}
+
+template <typename... Ts>
+void dependent_expansion(Ts... ts) {
+  template for (auto x : {ts...}) {
+#pragma omp parallel for
+    for (int i = 0; i < 2; i++)
+      g(x + i);
+  }
+}
+template void dependent_expansion<int, int>(int, int);
+
+// CHECK-LABEL: define {{.*}}void @_Z21parallel_in_expansioni(
+// OMP: call void (ptr, i32, ptr, ...) @__kmpc_fork_call(ptr @{{.+}}, i32 2, ptr @[[OUTLINED1:_Z21parallel_in_expansioni.omp_outlined]], ptr %{{.+}}, ptr %{{.+}})
+// OMP: call void (ptr, i32, ptr, ...) @__kmpc_fork_call(ptr @{{.+}}, i32 2, ptr @[[OUTLINED2:_Z21parallel_in_expansioni.omp_outlined.[0-9]+]], ptr %{{.+}}, ptr %{{.+}})
+// SIMD-NOT: @__kmpc_fork_call
+// SIMD: call {{.*}}i32 @_Z1gi(
+// SIMD-NOT: @__kmpc_fork_call
+// SIMD: call {{.*}}i32 @_Z1gi(
+// SIMD-NOT: @__kmpc_fork_call
+
+// OMP: define internal void @[[OUTLINED1]](
+// OMP: call {{.*}}i32 @_Z1gi(
+// OMP: define internal void @[[OUTLINED2]](
+// OMP: call {{.*}}i32 @_Z1gi(
+
+// CHECK-LABEL: define {{.*}}void @_Z17simd_in_expansionv(
+// CHECK: br label %{{.+}}, !llvm.loop ![[LOOP1:[0-9]+]]
+// CHECK: br label %{{.+}}, !llvm.loop ![[LOOP2:[0-9]+]]
+
+// CHECK-LABEL: define {{.*}}void @_Z19dependent_expansionIJiiEEvDpT_(
+// OMP: call void (ptr, i32, ptr, ...) @__kmpc_fork_call(ptr @{{.+}}, i32 1, ptr @[[OUTLINED3:_Z19dependent_expansionIJiiEEvDpT_.omp_outlined]], ptr %{{.+}})
+// OMP: call void (ptr, i32, ptr, ...) @__kmpc_fork_call(ptr @{{.+}}, i32 1, ptr @[[OUTLINED4:_Z19dependent_expansionIJiiEEvDpT_.omp_outlined.[0-9]+]], ptr %{{.+}})
+// SIMD-NOT: @__kmpc_fork_call
+// SIMD: call {{.*}}i32 @_Z1gi(
+// SIMD-NOT: @__kmpc_fork_call
+// SIMD: call {{.*}}i32 @_Z1gi(
+// SIMD-NOT: @__kmpc_fork_call
+
+// OMP: define internal void @[[OUTLINED3]](
+// OMP: call void @__kmpc_for_static_init_4(
+// OMP: define internal void @[[OUTLINED4]](
+// OMP: call void @__kmpc_for_static_init_4(
+
+// CHECK: ![[LOOP1]] = distinct !{![[LOOP1]], !{{[0-9]+}}, ![[VEC:[0-9]+]]}
+// CHECK: ![[VEC]] = !{!"llvm.loop.vectorize.enable"}
+// CHECK: ![[LOOP2]] = distinct !{![[LOOP2]], !{{[0-9]+}}, ![[VEC]]}
diff --git a/clang/test/SemaCXX/captured-statements.cpp b/clang/test/SemaCXX/captured-statements.cpp
index d8f77e6017ab89..717ed5a04e4d91 100644
--- a/clang/test/SemaCXX/captured-statements.cpp
+++ b/clang/test/SemaCXX/captured-statements.cpp
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s -fblocks
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++2c %s -fblocks
 
 void test_nest_lambda() {
   int x;
@@ -171,3 +172,50 @@ void test_capture_with_attributes() {
   {
   }
 }
+
+#if __cplusplus > 202302L
+void test_capture_in_expansion_statement(int n) {
+  template for (auto x : {1, 2}) {
+    #pragma clang __debug captured
+    {
+      n += x;
+    }
+    template for (auto y : {3, 4}) {
+      #pragma clang __debug captured
+      {
+        n += x + y;
+      }
+    }
+  }
+}
+
+template <typename T>
+void test_capture_in_dependent_expansion_statement(T &t) {
+  template for (auto x : t) {
+    #pragma clang __debug captured
+    {
+      x += 1;
+    }
+  }
+}
+
+struct Pair { int a; long b; };
+void test_dependent_expansion_statement() {
+  Pair p{1, 2};
+  test_capture_in_dependent_expansion_statement(p);
+}
+
+void test_capture_in_requires_expression() {
+  bool b = requires {
+    ({
+      int n = 0;
+      #pragma clang __debug captured
+      {
+        n += 1;
+      }
+      n;
+    });
+  };
+  (void)b;
+}
+#endif



More information about the cfe-commits mailing list