public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c, c++: Fix up -Wunused-value on COMPLEX_EXPRs [PR97748]
@ 2020-11-09  9:51 Jakub Jelinek
  2020-11-09 19:35 ` Jason Merrill
  0 siblings, 1 reply; 5+ messages in thread
From: Jakub Jelinek @ 2020-11-09  9:51 UTC (permalink / raw)
  To: Joseph S. Myers, Marek Polacek, Jason Merrill, Nathan Sidwell; +Cc: gcc-patches

Hi!

The -Wunused-value warning in both C and C++ FEs (implemented
significantly differently between the two) sees the COMPLEX_EXPRs created
e.g. for complex pre/post increment and many other expressions as useless
and warns about it.

For the C warning implementation, on e.g.
COMPLEX_EXPR < ++REALPART_EXPR <x>, IMAGPART_EXPR <x>>;
would warn even on the IMAGPART_EXPR <x> there alone etc., so what works
is check if we'd warn about both operands of COMPLEX_EXPR and if yes,
warn on the whole COMPLEX_EXPR, otherwise don't warn.

The C++ warning implementation is significantly different and for that one
the only warn if both would be warned about doesn't really work,
we then miss warnings e.g. about
COMPLEX_EXPR <REALPART_EXPR <SAVE_EXPR <x>> + 1.0e+0, IMAGPART_EXPR <SAVE_EXPR <x>>> >>>>>
so the patch instead warns if it would warn on any of the operands.

On the testcase which after the initial new tests contains pretty much
everything from gcc.dg/Wunused-value-1.c both approaches seem to work
nicely.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2020-11-09  Jakub Jelinek  <jakub@redhat.com>

	PR c/97748
gcc/c-family/
	* c-common.h (warn_if_unused_value): Add quiet argument defaulted
	to false.
	* c-warn.c (warn_if_unused_value): Likewise.  Pass it down
	recursively and just return true instead of warning if it is true.
	Handle COMPLEX_EXPR.
gcc/cp/
	* cvt.c (warn_if_unused_value_p): New function.
	(convert_to_void): Use it.
gcc/testsuite/
	* c-c++-common/Wunused-value-1.c: New test.

--- gcc/c-family/c-common.h.jj	2020-11-03 11:15:07.170681001 +0100
+++ gcc/c-family/c-common.h	2020-11-07 09:37:48.597233063 +0100
@@ -1362,7 +1362,7 @@ extern void warn_tautological_cmp (const
 				   tree, tree);
 extern void warn_logical_not_parentheses (location_t, enum tree_code, tree,
 					  tree);
-extern bool warn_if_unused_value (const_tree, location_t);
+extern bool warn_if_unused_value (const_tree, location_t, bool = false);
 extern bool strict_aliasing_warning (location_t, tree, tree);
 extern void sizeof_pointer_memaccess_warning (location_t *, tree,
 					      vec<tree, va_gc> *, tree *,
--- gcc/c-family/c-warn.c.jj	2020-10-26 10:53:56.533885147 +0100
+++ gcc/c-family/c-warn.c	2020-11-07 09:40:51.011170825 +0100
@@ -585,7 +585,7 @@ warn_logical_not_parentheses (location_t
    (potential) location of the expression.  */
 
 bool
-warn_if_unused_value (const_tree exp, location_t locus)
+warn_if_unused_value (const_tree exp, location_t locus, bool quiet)
 {
  restart:
   if (TREE_USED (exp) || TREE_NO_WARNING (exp))
@@ -633,7 +633,7 @@ warn_if_unused_value (const_tree exp, lo
       goto restart;
 
     case COMPOUND_EXPR:
-      if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
+      if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus, quiet))
 	return true;
       /* Let people do `(foo (), 0)' without a warning.  */
       if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
@@ -648,6 +648,13 @@ warn_if_unused_value (const_tree exp, lo
 	return false;
       goto warn;
 
+    case COMPLEX_EXPR:
+      /* Warn only if both operands are unused.  */
+      if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus, true)
+	  && warn_if_unused_value (TREE_OPERAND (exp, 1), locus, true))
+	goto warn;
+      return false;
+
     case INDIRECT_REF:
       /* Don't warn about automatic dereferencing of references, since
 	 the user cannot control it.  */
@@ -671,6 +678,8 @@ warn_if_unused_value (const_tree exp, lo
 	return false;
 
     warn:
+      if (quiet)
+	return true;
       return warning_at (locus, OPT_Wunused_value, "value computed is not used");
     }
 }
--- gcc/cp/cvt.c.jj	2020-07-28 15:39:09.000000000 +0200
+++ gcc/cp/cvt.c	2020-11-08 21:02:08.306584085 +0100
@@ -1111,6 +1111,46 @@ maybe_warn_nodiscard (tree expr, impl_co
     }
 }
 
+/* Return true if -Wunused-value warning should be emitted for EXPR.  */
+
+static bool
+warn_if_unused_value_p (tree expr)
+{
+  /* We might like to warn about (say) "(int) f()", as the
+     cast has no effect, but the compiler itself will
+     generate implicit conversions under some
+     circumstances.  (For example a block copy will be
+     turned into a call to "__builtin_memcpy", with a
+     conversion of the return value to an appropriate
+     type.)  So, to avoid false positives, we strip
+     conversions.  Do not use STRIP_NOPs because it will
+     not strip conversions to "void", as that is not a
+     mode-preserving conversion.  */
+  while (TREE_CODE (expr) == NOP_EXPR)
+    expr = TREE_OPERAND (expr, 0);
+
+  enum tree_code code = TREE_CODE (expr);
+  if (code == COMPLEX_EXPR)
+    /* For COMPLEX_EXPR, return true if either operand is unused.  */
+    return (warn_if_unused_value_p (TREE_OPERAND (expr, 0))
+	    || warn_if_unused_value_p (TREE_OPERAND (expr, 1)));
+
+  enum tree_code_class tclass = TREE_CODE_CLASS (code);
+  if (tclass == tcc_comparison
+      || tclass == tcc_unary
+      || (tclass == tcc_binary
+	  && !(code == MODIFY_EXPR
+	       || code == INIT_EXPR
+	       || code == PREDECREMENT_EXPR
+	       || code == PREINCREMENT_EXPR
+	       || code == POSTDECREMENT_EXPR
+	       || code == POSTINCREMENT_EXPR))
+      || code == VEC_PERM_EXPR
+      || code == VEC_COND_EXPR)
+    return true;
+  return false;
+}
+
 /* When an expression is used in a void context, its value is discarded and
    no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
    stmt.expr/1, expr.comma/1].  This permits dereferencing an incomplete type
@@ -1606,42 +1646,8 @@ convert_to_void (tree expr, impl_conv_vo
 		    gcc_unreachable ();
 		}
           }
-	  else
-	    {
-	      tree e;
-	      enum tree_code code;
-	      enum tree_code_class tclass;
-
-	      e = expr;
-	      /* We might like to warn about (say) "(int) f()", as the
-		 cast has no effect, but the compiler itself will
-		 generate implicit conversions under some
-		 circumstances.  (For example a block copy will be
-		 turned into a call to "__builtin_memcpy", with a
-		 conversion of the return value to an appropriate
-		 type.)  So, to avoid false positives, we strip
-		 conversions.  Do not use STRIP_NOPs because it will
-		 not strip conversions to "void", as that is not a
-		 mode-preserving conversion.  */
-	      while (TREE_CODE (e) == NOP_EXPR)
-		e = TREE_OPERAND (e, 0);
-
-	      code = TREE_CODE (e);
-	      tclass = TREE_CODE_CLASS (code);
-	      if ((tclass == tcc_comparison
-		   || tclass == tcc_unary
-		   || (tclass == tcc_binary
-		       && !(code == MODIFY_EXPR
-			    || code == INIT_EXPR
-			    || code == PREDECREMENT_EXPR
-			    || code == PREINCREMENT_EXPR
-			    || code == POSTDECREMENT_EXPR
-			    || code == POSTINCREMENT_EXPR))
-		   || code == VEC_PERM_EXPR
-		   || code == VEC_COND_EXPR)
-                  && (complain & tf_warning))
-		warning_at (loc, OPT_Wunused_value, "value computed is not used");
-	    }
+	  else if ((complain & tf_warning) && warn_if_unused_value_p (expr))
+	    warning_at (loc, OPT_Wunused_value, "value computed is not used");
 	}
       expr = build1 (CONVERT_EXPR, void_type_node, expr);
     }
--- gcc/testsuite/c-c++-common/Wunused-value-1.c.jj	2020-11-07 09:51:46.407757100 +0100
+++ gcc/testsuite/c-c++-common/Wunused-value-1.c	2020-11-07 10:15:34.295574782 +0100
@@ -0,0 +1,33 @@
+/* PR c/97748 */
+/* { dg-do compile } */
+/* { dg-options "-Wunused-value" } */
+
+double _Complex f ();
+double _Complex *p;
+
+double _Complex
+foo (double _Complex x)
+{
+  ++x;			/* { dg-bogus "value computed is not used" } */
+  --x;			/* { dg-bogus "value computed is not used" } */
+  x += 1;		/* { dg-bogus "value computed is not used" } */
+  x += 1.0iF;		/* { dg-bogus "value computed is not used" } */
+  x++;			/* { dg-bogus "value computed is not used" } */
+  x--;			/* { dg-bogus "value computed is not used" } */
+  x + 1;		/* { dg-warning "value computed is not used" } */
+  (void) (x + 1);	/* { dg-bogus "value computed is not used" } */
+  1 + f (); 		/* { dg-warning "value computed is not used" } */
+  f () + f (); 		/* { dg-warning "value computed is not used" } */
+  f () + f (), f (); 	/* { dg-warning "value computed is not used" } */
+  f ();
+  (void) f ();
+  *p++;			/* { dg-warning "value computed is not used" } */
+  ++*p;			/* { dg-bogus "value computed is not used" } */
+  (*p ? f () : 0);
+  ({ f (); });
+  ({ f () + 1; });
+  ({ f (); 0; });
+  ({ f () + 1; 0; });	/* { dg-warning "value computed is not used" } */
+  1 + ({ f (); });	/* { dg-warning "value computed is not used" } */
+  return x;
+}

	Jakub


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] c, c++: Fix up -Wunused-value on COMPLEX_EXPRs [PR97748]
  2020-11-09  9:51 [PATCH] c, c++: Fix up -Wunused-value on COMPLEX_EXPRs [PR97748] Jakub Jelinek
@ 2020-11-09 19:35 ` Jason Merrill
  2020-11-09 21:34   ` Jakub Jelinek
  0 siblings, 1 reply; 5+ messages in thread
From: Jason Merrill @ 2020-11-09 19:35 UTC (permalink / raw)
  To: Jakub Jelinek, Joseph S. Myers, Marek Polacek, Nathan Sidwell; +Cc: gcc-patches

On 11/9/20 4:51 AM, Jakub Jelinek wrote:
> Hi!
> 
> The -Wunused-value warning in both C and C++ FEs (implemented
> significantly differently between the two) sees the COMPLEX_EXPRs created
> e.g. for complex pre/post increment and many other expressions as useless
> and warns about it.
> 
> For the C warning implementation, on e.g.
> COMPLEX_EXPR < ++REALPART_EXPR <x>, IMAGPART_EXPR <x>>;
> would warn even on the IMAGPART_EXPR <x> there alone etc., so what works
> is check if we'd warn about both operands of COMPLEX_EXPR and if yes,
> warn on the whole COMPLEX_EXPR, otherwise don't warn.
> 
> The C++ warning implementation is significantly different and for that one
> the only warn if both would be warned about doesn't really work,
> we then miss warnings e.g. about
> COMPLEX_EXPR <REALPART_EXPR <SAVE_EXPR <x>> + 1.0e+0, IMAGPART_EXPR <SAVE_EXPR <x>>> >>>>>
> so the patch instead warns if it would warn on any of the operands.

I don't understand the rationale for this difference between C and C++. 
Are you saying that the C++ front end is confused by the SAVE_EXPR? 
warn_if_unused_value properly looks through it.

It's also weird that the C++ front end calls warn_if_unused_value (which 
is in a shared file) in gimplify_expr_stmt AND has its own version of 
the warning in convert_to_void.

How about calling warn_if_unused_value instead of the new 
warn_if_unused_value_p?

> On the testcase which after the initial new tests contains pretty much
> everything from gcc.dg/Wunused-value-1.c both approaches seem to work
> nicely.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
> 
> 2020-11-09  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR c/97748
> gcc/c-family/
> 	* c-common.h (warn_if_unused_value): Add quiet argument defaulted
> 	to false.
> 	* c-warn.c (warn_if_unused_value): Likewise.  Pass it down
> 	recursively and just return true instead of warning if it is true.
> 	Handle COMPLEX_EXPR.
> gcc/cp/
> 	* cvt.c (warn_if_unused_value_p): New function.
> 	(convert_to_void): Use it.
> gcc/testsuite/
> 	* c-c++-common/Wunused-value-1.c: New test.
> 
> --- gcc/c-family/c-common.h.jj	2020-11-03 11:15:07.170681001 +0100
> +++ gcc/c-family/c-common.h	2020-11-07 09:37:48.597233063 +0100
> @@ -1362,7 +1362,7 @@ extern void warn_tautological_cmp (const
>   				   tree, tree);
>   extern void warn_logical_not_parentheses (location_t, enum tree_code, tree,
>   					  tree);
> -extern bool warn_if_unused_value (const_tree, location_t);
> +extern bool warn_if_unused_value (const_tree, location_t, bool = false);
>   extern bool strict_aliasing_warning (location_t, tree, tree);
>   extern void sizeof_pointer_memaccess_warning (location_t *, tree,
>   					      vec<tree, va_gc> *, tree *,
> --- gcc/c-family/c-warn.c.jj	2020-10-26 10:53:56.533885147 +0100
> +++ gcc/c-family/c-warn.c	2020-11-07 09:40:51.011170825 +0100
> @@ -585,7 +585,7 @@ warn_logical_not_parentheses (location_t
>      (potential) location of the expression.  */
>   
>   bool
> -warn_if_unused_value (const_tree exp, location_t locus)
> +warn_if_unused_value (const_tree exp, location_t locus, bool quiet)
>   {
>    restart:
>     if (TREE_USED (exp) || TREE_NO_WARNING (exp))
> @@ -633,7 +633,7 @@ warn_if_unused_value (const_tree exp, lo
>         goto restart;
>   
>       case COMPOUND_EXPR:
> -      if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
> +      if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus, quiet))
>   	return true;
>         /* Let people do `(foo (), 0)' without a warning.  */
>         if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
> @@ -648,6 +648,13 @@ warn_if_unused_value (const_tree exp, lo
>   	return false;
>         goto warn;
>   
> +    case COMPLEX_EXPR:
> +      /* Warn only if both operands are unused.  */
> +      if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus, true)
> +	  && warn_if_unused_value (TREE_OPERAND (exp, 1), locus, true))
> +	goto warn;
> +      return false;
> +
>       case INDIRECT_REF:
>         /* Don't warn about automatic dereferencing of references, since
>   	 the user cannot control it.  */
> @@ -671,6 +678,8 @@ warn_if_unused_value (const_tree exp, lo
>   	return false;
>   
>       warn:
> +      if (quiet)
> +	return true;
>         return warning_at (locus, OPT_Wunused_value, "value computed is not used");
>       }
>   }
> --- gcc/cp/cvt.c.jj	2020-07-28 15:39:09.000000000 +0200
> +++ gcc/cp/cvt.c	2020-11-08 21:02:08.306584085 +0100
> @@ -1111,6 +1111,46 @@ maybe_warn_nodiscard (tree expr, impl_co
>       }
>   }
>   
> +/* Return true if -Wunused-value warning should be emitted for EXPR.  */
> +
> +static bool
> +warn_if_unused_value_p (tree expr)
> +{
> +  /* We might like to warn about (say) "(int) f()", as the
> +     cast has no effect, but the compiler itself will
> +     generate implicit conversions under some
> +     circumstances.  (For example a block copy will be
> +     turned into a call to "__builtin_memcpy", with a
> +     conversion of the return value to an appropriate
> +     type.)  So, to avoid false positives, we strip
> +     conversions.  Do not use STRIP_NOPs because it will
> +     not strip conversions to "void", as that is not a
> +     mode-preserving conversion.  */
> +  while (TREE_CODE (expr) == NOP_EXPR)
> +    expr = TREE_OPERAND (expr, 0);
> +
> +  enum tree_code code = TREE_CODE (expr);
> +  if (code == COMPLEX_EXPR)
> +    /* For COMPLEX_EXPR, return true if either operand is unused.  */
> +    return (warn_if_unused_value_p (TREE_OPERAND (expr, 0))
> +	    || warn_if_unused_value_p (TREE_OPERAND (expr, 1)));
> +
> +  enum tree_code_class tclass = TREE_CODE_CLASS (code);
> +  if (tclass == tcc_comparison
> +      || tclass == tcc_unary
> +      || (tclass == tcc_binary
> +	  && !(code == MODIFY_EXPR
> +	       || code == INIT_EXPR
> +	       || code == PREDECREMENT_EXPR
> +	       || code == PREINCREMENT_EXPR
> +	       || code == POSTDECREMENT_EXPR
> +	       || code == POSTINCREMENT_EXPR))
> +      || code == VEC_PERM_EXPR
> +      || code == VEC_COND_EXPR)
> +    return true;
> +  return false;
> +}
> +
>   /* When an expression is used in a void context, its value is discarded and
>      no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
>      stmt.expr/1, expr.comma/1].  This permits dereferencing an incomplete type
> @@ -1606,42 +1646,8 @@ convert_to_void (tree expr, impl_conv_vo
>   		    gcc_unreachable ();
>   		}
>             }
> -	  else
> -	    {
> -	      tree e;
> -	      enum tree_code code;
> -	      enum tree_code_class tclass;
> -
> -	      e = expr;
> -	      /* We might like to warn about (say) "(int) f()", as the
> -		 cast has no effect, but the compiler itself will
> -		 generate implicit conversions under some
> -		 circumstances.  (For example a block copy will be
> -		 turned into a call to "__builtin_memcpy", with a
> -		 conversion of the return value to an appropriate
> -		 type.)  So, to avoid false positives, we strip
> -		 conversions.  Do not use STRIP_NOPs because it will
> -		 not strip conversions to "void", as that is not a
> -		 mode-preserving conversion.  */
> -	      while (TREE_CODE (e) == NOP_EXPR)
> -		e = TREE_OPERAND (e, 0);
> -
> -	      code = TREE_CODE (e);
> -	      tclass = TREE_CODE_CLASS (code);
> -	      if ((tclass == tcc_comparison
> -		   || tclass == tcc_unary
> -		   || (tclass == tcc_binary
> -		       && !(code == MODIFY_EXPR
> -			    || code == INIT_EXPR
> -			    || code == PREDECREMENT_EXPR
> -			    || code == PREINCREMENT_EXPR
> -			    || code == POSTDECREMENT_EXPR
> -			    || code == POSTINCREMENT_EXPR))
> -		   || code == VEC_PERM_EXPR
> -		   || code == VEC_COND_EXPR)
> -                  && (complain & tf_warning))
> -		warning_at (loc, OPT_Wunused_value, "value computed is not used");
> -	    }
> +	  else if ((complain & tf_warning) && warn_if_unused_value_p (expr))
> +	    warning_at (loc, OPT_Wunused_value, "value computed is not used");
>   	}
>         expr = build1 (CONVERT_EXPR, void_type_node, expr);
>       }
> --- gcc/testsuite/c-c++-common/Wunused-value-1.c.jj	2020-11-07 09:51:46.407757100 +0100
> +++ gcc/testsuite/c-c++-common/Wunused-value-1.c	2020-11-07 10:15:34.295574782 +0100
> @@ -0,0 +1,33 @@
> +/* PR c/97748 */
> +/* { dg-do compile } */
> +/* { dg-options "-Wunused-value" } */
> +
> +double _Complex f ();
> +double _Complex *p;
> +
> +double _Complex
> +foo (double _Complex x)
> +{
> +  ++x;			/* { dg-bogus "value computed is not used" } */
> +  --x;			/* { dg-bogus "value computed is not used" } */
> +  x += 1;		/* { dg-bogus "value computed is not used" } */
> +  x += 1.0iF;		/* { dg-bogus "value computed is not used" } */
> +  x++;			/* { dg-bogus "value computed is not used" } */
> +  x--;			/* { dg-bogus "value computed is not used" } */
> +  x + 1;		/* { dg-warning "value computed is not used" } */
> +  (void) (x + 1);	/* { dg-bogus "value computed is not used" } */
> +  1 + f (); 		/* { dg-warning "value computed is not used" } */
> +  f () + f (); 		/* { dg-warning "value computed is not used" } */
> +  f () + f (), f (); 	/* { dg-warning "value computed is not used" } */
> +  f ();
> +  (void) f ();
> +  *p++;			/* { dg-warning "value computed is not used" } */
> +  ++*p;			/* { dg-bogus "value computed is not used" } */
> +  (*p ? f () : 0);
> +  ({ f (); });
> +  ({ f () + 1; });
> +  ({ f (); 0; });
> +  ({ f () + 1; 0; });	/* { dg-warning "value computed is not used" } */
> +  1 + ({ f (); });	/* { dg-warning "value computed is not used" } */
> +  return x;
> +}
> 
> 	Jakub
> 


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] c, c++: Fix up -Wunused-value on COMPLEX_EXPRs [PR97748]
  2020-11-09 19:35 ` Jason Merrill
@ 2020-11-09 21:34   ` Jakub Jelinek
  2020-11-10  8:48     ` Jakub Jelinek
  0 siblings, 1 reply; 5+ messages in thread
From: Jakub Jelinek @ 2020-11-09 21:34 UTC (permalink / raw)
  To: Jason Merrill; +Cc: Joseph S. Myers, Marek Polacek, Nathan Sidwell, gcc-patches

On Mon, Nov 09, 2020 at 02:35:58PM -0500, Jason Merrill wrote:
> How about calling warn_if_unused_value instead of the new
> warn_if_unused_value_p?

That seems to work if I just replace the warning_at call with
warn_if_unused_value call (at least no regression in check-c++-all and
libstdc++ testsuite).
Initially I've tried just calling warn_if_unused_value without the NOP_EXPR
stripping and code/tclass checks, but that regressed a few tests, e.g.
g++.dg/warn/Wunused-14.C or c-c++-common/Wunused-var-9.c or 3 lines
in the new test, e.g. because STATEMENT_LIST or CLEANUP_POINT_EXPRs would
make it through and resulted in bogus warnings.

2020-11-09  Jakub Jelinek  <jakub@redhat.com>

	PR c/97748
gcc/c-family/
	* c-common.h (warn_if_unused_value): Add quiet argument defaulted
	to false.
	* c-warn.c (warn_if_unused_value): Likewise.  Pass it down
	recursively and just return true instead of warning if it is true.
	Handle COMPLEX_EXPR.
gcc/cp/
	* cvt.c (convert_to_void): Check (complain & tf_warning) in the outer
	if rather than twice times in the inner one.  Use warn_if_unused_value.
	Formatting fix.
gcc/testsuite/
	* c-c++-common/Wunused-value-1.c: New test.

--- gcc/c-family/c-common.h.jj	2020-11-03 11:15:07.170681001 +0100
+++ gcc/c-family/c-common.h	2020-11-07 09:37:48.597233063 +0100
@@ -1362,7 +1362,7 @@ extern void warn_tautological_cmp (const
 				   tree, tree);
 extern void warn_logical_not_parentheses (location_t, enum tree_code, tree,
 					  tree);
-extern bool warn_if_unused_value (const_tree, location_t);
+extern bool warn_if_unused_value (const_tree, location_t, bool = false);
 extern bool strict_aliasing_warning (location_t, tree, tree);
 extern void sizeof_pointer_memaccess_warning (location_t *, tree,
 					      vec<tree, va_gc> *, tree *,
--- gcc/c-family/c-warn.c.jj	2020-10-26 10:53:56.533885147 +0100
+++ gcc/c-family/c-warn.c	2020-11-07 09:40:51.011170825 +0100
@@ -585,7 +585,7 @@ warn_logical_not_parentheses (location_t
    (potential) location of the expression.  */
 
 bool
-warn_if_unused_value (const_tree exp, location_t locus)
+warn_if_unused_value (const_tree exp, location_t locus, bool quiet)
 {
  restart:
   if (TREE_USED (exp) || TREE_NO_WARNING (exp))
@@ -633,7 +633,7 @@ warn_if_unused_value (const_tree exp, lo
       goto restart;
 
     case COMPOUND_EXPR:
-      if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
+      if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus, quiet))
 	return true;
       /* Let people do `(foo (), 0)' without a warning.  */
       if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
@@ -648,6 +648,13 @@ warn_if_unused_value (const_tree exp, lo
 	return false;
       goto warn;
 
+    case COMPLEX_EXPR:
+      /* Warn only if both operands are unused.  */
+      if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus, true)
+	  && warn_if_unused_value (TREE_OPERAND (exp, 1), locus, true))
+	goto warn;
+      return false;
+
     case INDIRECT_REF:
       /* Don't warn about automatic dereferencing of references, since
 	 the user cannot control it.  */
@@ -671,6 +678,8 @@ warn_if_unused_value (const_tree exp, lo
 	return false;
 
     warn:
+      if (quiet)
+	return true;
       return warning_at (locus, OPT_Wunused_value, "value computed is not used");
     }
 }
--- gcc/cp/cvt.c.jj	2020-07-28 15:39:09.000000000 +0200
+++ gcc/cp/cvt.c	2020-11-09 21:28:47.180254399 +0100
@@ -1568,12 +1568,13 @@ convert_to_void (tree expr, impl_conv_vo
 	  && warn_unused_value
 	  && !TREE_NO_WARNING (expr)
 	  && !processing_template_decl
-	  && !cp_unevaluated_operand)
+	  && !cp_unevaluated_operand
+	  && (complain & tf_warning))
 	{
 	  /* The middle end does not warn about expressions that have
 	     been explicitly cast to void, so we must do so here.  */
-	  if (!TREE_SIDE_EFFECTS (expr)) {
-            if (complain & tf_warning)
+	  if (!TREE_SIDE_EFFECTS (expr))
+	    {
 	      switch (implicit)
 		{
 		  case ICV_SECOND_OF_COND:
@@ -1605,14 +1606,10 @@ convert_to_void (tree expr, impl_conv_vo
 		  default:
 		    gcc_unreachable ();
 		}
-          }
+	    }
 	  else
 	    {
-	      tree e;
-	      enum tree_code code;
-	      enum tree_code_class tclass;
-
-	      e = expr;
+	      tree e = expr;
 	      /* We might like to warn about (say) "(int) f()", as the
 		 cast has no effect, but the compiler itself will
 		 generate implicit conversions under some
@@ -1626,21 +1623,20 @@ convert_to_void (tree expr, impl_conv_vo
 	      while (TREE_CODE (e) == NOP_EXPR)
 		e = TREE_OPERAND (e, 0);
 
-	      code = TREE_CODE (e);
-	      tclass = TREE_CODE_CLASS (code);
-	      if ((tclass == tcc_comparison
-		   || tclass == tcc_unary
-		   || (tclass == tcc_binary
-		       && !(code == MODIFY_EXPR
-			    || code == INIT_EXPR
-			    || code == PREDECREMENT_EXPR
-			    || code == PREINCREMENT_EXPR
-			    || code == POSTDECREMENT_EXPR
-			    || code == POSTINCREMENT_EXPR))
-		   || code == VEC_PERM_EXPR
-		   || code == VEC_COND_EXPR)
-                  && (complain & tf_warning))
-		warning_at (loc, OPT_Wunused_value, "value computed is not used");
+	      enum tree_code code = TREE_CODE (e);
+	      enum tree_code_class tclass = TREE_CODE_CLASS (code);
+	      if (tclass == tcc_comparison
+		  || tclass == tcc_unary
+		  || (tclass == tcc_binary
+		      && !(code == MODIFY_EXPR
+			   || code == INIT_EXPR
+			   || code == PREDECREMENT_EXPR
+			   || code == PREINCREMENT_EXPR
+			   || code == POSTDECREMENT_EXPR
+			   || code == POSTINCREMENT_EXPR))
+		  || code == VEC_PERM_EXPR
+		  || code == VEC_COND_EXPR)
+		warn_if_unused_value (e, loc);
 	    }
 	}
       expr = build1 (CONVERT_EXPR, void_type_node, expr);
--- gcc/testsuite/c-c++-common/Wunused-value-1.c.jj	2020-11-07 09:51:46.407757100 +0100
+++ gcc/testsuite/c-c++-common/Wunused-value-1.c	2020-11-07 10:15:34.295574782 +0100
@@ -0,0 +1,33 @@
+/* PR c/97748 */
+/* { dg-do compile } */
+/* { dg-options "-Wunused-value" } */
+
+double _Complex f ();
+double _Complex *p;
+
+double _Complex
+foo (double _Complex x)
+{
+  ++x;			/* { dg-bogus "value computed is not used" } */
+  --x;			/* { dg-bogus "value computed is not used" } */
+  x += 1;		/* { dg-bogus "value computed is not used" } */
+  x += 1.0iF;		/* { dg-bogus "value computed is not used" } */
+  x++;			/* { dg-bogus "value computed is not used" } */
+  x--;			/* { dg-bogus "value computed is not used" } */
+  x + 1;		/* { dg-warning "value computed is not used" } */
+  (void) (x + 1);	/* { dg-bogus "value computed is not used" } */
+  1 + f (); 		/* { dg-warning "value computed is not used" } */
+  f () + f (); 		/* { dg-warning "value computed is not used" } */
+  f () + f (), f (); 	/* { dg-warning "value computed is not used" } */
+  f ();
+  (void) f ();
+  *p++;			/* { dg-warning "value computed is not used" } */
+  ++*p;			/* { dg-bogus "value computed is not used" } */
+  (*p ? f () : 0);
+  ({ f (); });
+  ({ f () + 1; });
+  ({ f (); 0; });
+  ({ f () + 1; 0; });	/* { dg-warning "value computed is not used" } */
+  1 + ({ f (); });	/* { dg-warning "value computed is not used" } */
+  return x;
+}


	Jakub


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] c, c++: Fix up -Wunused-value on COMPLEX_EXPRs [PR97748]
  2020-11-09 21:34   ` Jakub Jelinek
@ 2020-11-10  8:48     ` Jakub Jelinek
  2020-11-10 14:32       ` Jason Merrill
  0 siblings, 1 reply; 5+ messages in thread
From: Jakub Jelinek @ 2020-11-10  8:48 UTC (permalink / raw)
  To: Jason Merrill, Marek Polacek, gcc-patches, Nathan Sidwell,
	Joseph S. Myers

On Mon, Nov 09, 2020 at 10:34:00PM +0100, Jakub Jelinek via Gcc-patches wrote:
> On Mon, Nov 09, 2020 at 02:35:58PM -0500, Jason Merrill wrote:
> > How about calling warn_if_unused_value instead of the new
> > warn_if_unused_value_p?
> 
> That seems to work if I just replace the warning_at call with
> warn_if_unused_value call (at least no regression in check-c++-all and
> libstdc++ testsuite).
> Initially I've tried just calling warn_if_unused_value without the NOP_EXPR
> stripping and code/tclass checks, but that regressed a few tests, e.g.
> g++.dg/warn/Wunused-14.C or c-c++-common/Wunused-var-9.c or 3 lines
> in the new test, e.g. because STATEMENT_LIST or CLEANUP_POINT_EXPRs would
> make it through and resulted in bogus warnings.

Bootstrapped/regtested on x86_64-linux and i686-linux successfully now.
If we wanted to simplify, I think the && !(code == ....) part could be
dropped too, i.e. just
	      if (tclass == tcc_comparison
		  || tclass == tcc_unary
		  || tclass == tcc_binary
		  || code == VEC_PERM_EXPR
		  || code == VEC_COND_EXPR)
		warn_if_unused_value (e, loc);
because warn_if_unused_value already returns false on those codes.

> +	      enum tree_code code = TREE_CODE (e);
> +	      enum tree_code_class tclass = TREE_CODE_CLASS (code);
> +	      if (tclass == tcc_comparison
> +		  || tclass == tcc_unary
> +		  || (tclass == tcc_binary
> +		      && !(code == MODIFY_EXPR
> +			   || code == INIT_EXPR
> +			   || code == PREDECREMENT_EXPR
> +			   || code == PREINCREMENT_EXPR
> +			   || code == POSTDECREMENT_EXPR
> +			   || code == POSTINCREMENT_EXPR))
> +		  || code == VEC_PERM_EXPR
> +		  || code == VEC_COND_EXPR)
> +		warn_if_unused_value (e, loc);
>  	    }
>  	}
>        expr = build1 (CONVERT_EXPR, void_type_node, expr);

	Jakub


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] c, c++: Fix up -Wunused-value on COMPLEX_EXPRs [PR97748]
  2020-11-10  8:48     ` Jakub Jelinek
@ 2020-11-10 14:32       ` Jason Merrill
  0 siblings, 0 replies; 5+ messages in thread
From: Jason Merrill @ 2020-11-10 14:32 UTC (permalink / raw)
  To: Jakub Jelinek, Marek Polacek, gcc-patches, Nathan Sidwell,
	Joseph S. Myers

On 11/10/20 3:48 AM, Jakub Jelinek wrote:
> On Mon, Nov 09, 2020 at 10:34:00PM +0100, Jakub Jelinek via Gcc-patches wrote:
>> On Mon, Nov 09, 2020 at 02:35:58PM -0500, Jason Merrill wrote:
>>> How about calling warn_if_unused_value instead of the new
>>> warn_if_unused_value_p?
>>
>> That seems to work if I just replace the warning_at call with
>> warn_if_unused_value call (at least no regression in check-c++-all and
>> libstdc++ testsuite).
>> Initially I've tried just calling warn_if_unused_value without the NOP_EXPR
>> stripping and code/tclass checks, but that regressed a few tests, e.g.
>> g++.dg/warn/Wunused-14.C or c-c++-common/Wunused-var-9.c or 3 lines
>> in the new test, e.g. because STATEMENT_LIST or CLEANUP_POINT_EXPRs would
>> make it through and resulted in bogus warnings.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux successfully now.
> If we wanted to simplify, I think the && !(code == ....) part could be
> dropped too, i.e. just
> 	      if (tclass == tcc_comparison
> 		  || tclass == tcc_unary
> 		  || tclass == tcc_binary
> 		  || code == VEC_PERM_EXPR
> 		  || code == VEC_COND_EXPR)
> 		warn_if_unused_value (e, loc);
> because warn_if_unused_value already returns false on those codes.

OK with that change.

Jason


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-11-10 14:32 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-09  9:51 [PATCH] c, c++: Fix up -Wunused-value on COMPLEX_EXPRs [PR97748] Jakub Jelinek
2020-11-09 19:35 ` Jason Merrill
2020-11-09 21:34   ` Jakub Jelinek
2020-11-10  8:48     ` Jakub Jelinek
2020-11-10 14:32       ` Jason Merrill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).