]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Fix ICE with weird copy assignment operator [PR114572]
authorJakub Jelinek <jakub@redhat.com>
Fri, 5 Apr 2024 07:31:28 +0000 (09:31 +0200)
committerJakub Jelinek <jakub@redhat.com>
Fri, 5 Apr 2024 07:31:28 +0000 (09:31 +0200)
While ctors/dtors don't return anything (undeclared void or this pointer
on arm) and copy assignment operators normally return a reference to *this,
it isn't invalid to return uselessly some class object which might need
destructing, but the OpenMP clause handling code wasn't expecting that.

The following patch fixes that.

2024-04-05  Jakub Jelinek  <jakub@redhat.com>

PR c++/114572
* cp-gimplify.cc (cxx_omp_clause_apply_fn): Call build_cplus_new
on build_call_a result if it has class type.

* testsuite/libgomp.c++/pr114572.C: New test.

gcc/cp/cp-gimplify.cc
libgomp/testsuite/libgomp.c++/pr114572.C [new file with mode: 0644]

index f3baae6ade7527a1c99626cc7eff268fca9deba5..ab5acd18c99308126f150a9e55824b7e4ec77d17 100644 (file)
@@ -2480,6 +2480,8 @@ cxx_omp_clause_apply_fn (tree fn, tree arg1, tree arg2)
                                           TREE_PURPOSE (parm), fn,
                                           i - is_method, tf_warning_or_error);
       t = build_call_a (fn, i, argarray);
+      if (MAYBE_CLASS_TYPE_P (TREE_TYPE (t)))
+       t = build_cplus_new (TREE_TYPE (t), t, tf_warning_or_error);
       t = fold_convert (void_type_node, t);
       t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
       append_to_statement_list (t, &ret);
@@ -2513,6 +2515,8 @@ cxx_omp_clause_apply_fn (tree fn, tree arg1, tree arg2)
                                           TREE_PURPOSE (parm), fn,
                                           i - is_method, tf_warning_or_error);
       t = build_call_a (fn, i, argarray);
+      if (MAYBE_CLASS_TYPE_P (TREE_TYPE (t)))
+       t = build_cplus_new (TREE_TYPE (t), t, tf_warning_or_error);
       t = fold_convert (void_type_node, t);
       return fold_build_cleanup_point_expr (TREE_TYPE (t), t);
     }
diff --git a/libgomp/testsuite/libgomp.c++/pr114572.C b/libgomp/testsuite/libgomp.c++/pr114572.C
new file mode 100644 (file)
index 0000000..21d5c84
--- /dev/null
@@ -0,0 +1,24 @@
+// PR c++/114572
+// { dg-do run }
+// { dg-options "-fopenmp -O0" }
+
+#include <stdlib.h>
+
+struct S
+{
+  S () : s (0) {}
+  ~S () {}
+  S operator= (const S &x) { s = x.s; return *this; }
+  int s;
+};
+
+int
+main ()
+{
+  S s;
+  #pragma omp parallel for lastprivate(s)
+  for (int i = 0; i < 10; ++i)
+    s.s = i;
+  if (s.s != 9)
+    abort ();
+}