This fixes a GCC 12-to-16 Regression related to an
unexpected empty BIND_EXPR_BLOCK, where nonetheless
aBIND_EXPR exists - just by not walking the hence non-existing variables binding to that block. Any comments before I commit this to mainline? I also intent to backport it relatively soonish to GCC 15, but also GCC 14 (14.4 or still 14.3 ?) seems to be useful target.

* * *

The issue reminds me a bit of r15-4104-ga8caeaacf499d5 where
in a some fancy way a TREE_USED is created but all what was
left was a DECL_EXPR in the BIND_EXPR_BODY and no statement
list. [Otherwise, a completely different code path.]

Here, the situation is a bit similar: GCC generates also a
BIND_EXPR - but the BIND_EXPR_BLOCK is empty, which the prexisting
code also did not handle. - Contrary to the not-quite used
variable in the previous issue, the constructor might actually
do something observable (but doesn't in this case).
Tobias
OpenMP/C++: Avoid ICE for BIND_EXPR with empty BIND_EXPR_BLOCK [PR120413]

	PR c++/120413

gcc/cp/ChangeLog:

	* semantics.cc (finish_omp_target_clauses_r): Handle
	BIND_EXPR with empty BIND_EXPR_BLOCK.

gcc/testsuite/ChangeLog:

	* g++.dg/gomp/target-4.C: New test.

 gcc/cp/semantics.cc                  |  7 ++++---
 gcc/testsuite/g++.dg/gomp/target-4.C | 22 ++++++++++++++++++++++
 2 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 43a0eabfa12..8651009a43f 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -10566,9 +10566,10 @@ finish_omp_target_clauses_r (tree *tp, int *walk_subtrees, void *ptr)
   if (TREE_CODE (t) == BIND_EXPR)
     {
       tree block = BIND_EXPR_BLOCK (t);
-      for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
-	if (!data->local_decls.contains (var))
-	  data->local_decls.add (var);
+      if (block)
+	for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
+	  if (!data->local_decls.contains (var))
+	    data->local_decls.add (var);
       return NULL_TREE;
     }
 
diff --git a/gcc/testsuite/g++.dg/gomp/target-4.C b/gcc/testsuite/g++.dg/gomp/target-4.C
new file mode 100644
index 00000000000..80fc9dfe936
--- /dev/null
+++ b/gcc/testsuite/g++.dg/gomp/target-4.C
@@ -0,0 +1,22 @@
+// { dg-do compile { target c++11 } }
+// PR c++/120413
+
+struct S
+{
+  S() {}
+  ~S() {}
+};
+
+struct array
+{
+  S _arr[1];
+};
+
+int main()
+{
+#pragma omp target
+  {
+    array arr{};
+  }
+  return 0;
+}

Reply via email to