[committed] Avoid -Wunused-value warnings on #pragma omp atomic capture (PR c++/89796)

Jakub Jelinek jakub@redhat.com
Tue Mar 26 08:13:00 GMT 2019


Hi!

c-omp.c calls omit_one_operand which in several cases can wrap a MODIFY_EXPR
into NON_LVALUE_EXPR and similar.  An atomic is always a whole
statement that has its side-effect(s), so -Wunused-value for that is not
really useful.

Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux,
committed to trunk, queued for backports.

2019-03-26  Jakub Jelinek  <jakub@redhat.com>

	PR c++/89796
	* semantics.c (finish_omp_atomic): Add warning_sentinel for
	-Wunused-value around finish_expr_stmt call.

	* g++.dg/gomp/pr89796.C: New test.
	* gcc.dg/gomp/pr89796.c: New test.

--- gcc/cp/semantics.c.jj	2019-03-22 15:42:02.469993735 +0100
+++ gcc/cp/semantics.c	2019-03-25 16:51:27.411936064 +0100
@@ -8997,6 +8997,11 @@ finish_omp_atomic (location_t loc, enum
       OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
       SET_EXPR_LOCATION (stmt, loc);
     }
+
+  /* Avoid -Wunused-value warnings here, the whole construct has side-effects
+     and even if it might be wrapped from fold-const.c or c-omp.c wrapped
+     in some tree that appears to be unused, the value is not unused.  */
+  warning_sentinel w (warn_unused_value);
   finish_expr_stmt (stmt);
 }
 
--- gcc/testsuite/g++.dg/gomp/pr89796.C.jj	2019-03-25 17:06:08.144778018 +0100
+++ gcc/testsuite/g++.dg/gomp/pr89796.C	2019-03-25 17:04:51.054018606 +0100
@@ -0,0 +1,53 @@
+// PR c++/89796
+// { dg-do compile }
+// { dg-additional-options "-Wunused-value" }
+
+int
+f1 (int &c)
+{
+  int r;
+  #pragma omp atomic capture	// { dg-bogus "value computed is not used" }
+  { r = c; c++; }
+  return r;
+}
+
+template <int N>
+int
+f2 (int &c)
+{
+  int r;
+  #pragma omp atomic capture	// { dg-bogus "value computed is not used" }
+  { r = c; c++; }
+  return r;
+}
+
+int
+f3 (int &c)
+{
+  return f2 <0> (c);
+}
+
+int
+f4 (int *p)
+{
+  int r;
+  #pragma omp atomic capture	// { dg-bogus "value computed is not used" }
+  { r = *p; (*p)++; }
+  return r;
+}
+
+template <int N>
+int
+f5 (int *p)
+{
+  int r;
+  #pragma omp atomic capture	// { dg-bogus "value computed is not used" }
+  { r = *p; (*p)++; }
+  return r;
+}
+
+int
+f6 (int *p)
+{
+  return f5 <0> (p);
+}
--- gcc/testsuite/gcc.dg/gomp/pr89796.c.jj	2019-03-25 17:06:28.446451316 +0100
+++ gcc/testsuite/gcc.dg/gomp/pr89796.c	2019-03-25 17:05:16.944601962 +0100
@@ -0,0 +1,23 @@
+/* PR c++/89796 */
+/* { dg-do compile } */
+/* { dg-additional-options "-Wunused-value" } */
+
+int
+f1 (int *p)
+{
+  int r;
+  #pragma omp atomic capture		/* { dg-bogus "value computed is not used" } */
+  { r = *p; (*p)++; }
+  return r;
+}
+
+int
+f2 (int *p)
+{
+  int s
+    = ({ int r;
+	 #pragma omp atomic capture	/* { dg-bogus "value computed is not used" } */
+	 { r = *p; (*p)++; }
+	 r; });
+  return s;
+}

	Jakub



More information about the Gcc-patches mailing list