Patch for bug 21159

Joseph S. Myers joseph@codesourcery.com
Thu Apr 28 01:04:00 GMT 2005


On Wed, 27 Apr 2005, Daniel Jacobowitz wrote:

> Previously the test was !(CONVERT_EXPR && VOID_TYPE_P).  That's the
> same as !CONVERT_EXPR || !VOID_TYPE_P.  The new test is !VOID_TYPE_P
> && (!CONVERT_EXPR || !COMPOUND_EXPR).
> 
> If the left hand item has no side effects, is of void type,
> and is not a CONVERT_EXPR, the previous code would have warned but the
> new code won't.  I have no idea if that's possible.

Fixed thus, mainline and 4.0 branch, after bootstrap with no
regressions on x86_64-unknown-linux-gnu.  Thanks for pointing out this
problem.

-- 
Joseph S. Myers               http://www.srcf.ucam.org/~jsm28/gcc/
    jsm@polyomino.org.uk (personal mail)
    joseph@codesourcery.com (CodeSourcery mail)
    jsm28@gcc.gnu.org (Bugzilla assignments and CCs)

2005-04-28  Joseph S. Myers  <joseph@codesourcery.com>

	* c-typeck.c (build_compound_expr): Correct logic in last change.

testsuite:
2005-04-28  Joseph S. Myers  <joseph@codesourcery.com>

	* gcc.dg/void-cast-2.c: New test.

diff -rupN GCC.orig/gcc/c-typeck.c GCC/gcc/c-typeck.c
--- GCC.orig/gcc/c-typeck.c	2005-04-27 21:38:09.000000000 +0000
+++ GCC/gcc/c-typeck.c	2005-04-27 23:14:50.000000000 +0000
@@ -3108,12 +3108,13 @@ build_compound_expr (tree expr1, tree ex
       /* The left-hand operand of a comma expression is like an expression
          statement: with -Wextra or -Wunused, we should warn if it doesn't have
 	 any side-effects, unless it was explicitly cast to (void).  */
-      if (warn_unused_value
-	  && !VOID_TYPE_P (TREE_TYPE (expr1)))
+      if (warn_unused_value)
 	{
-	  if (TREE_CODE (expr1) == CONVERT_EXPR)
+	  if (VOID_TYPE_P (TREE_TYPE (expr1))
+	      && TREE_CODE (expr1) == CONVERT_EXPR)
 	    ; /* (void) a, b */
-	  else if (TREE_CODE (expr1) == COMPOUND_EXPR
+	  else if (VOID_TYPE_P (TREE_TYPE (expr1))
+		   && TREE_CODE (expr1) == COMPOUND_EXPR
 		   && TREE_CODE (TREE_OPERAND (expr1, 1)) == CONVERT_EXPR)
 	    ; /* (void) a, (void) b, c */
 	  else
diff -rupN GCC.orig/gcc/testsuite/gcc.dg/void-cast-2.c GCC/gcc/testsuite/gcc.dg/void-cast-2.c
--- GCC.orig/gcc/testsuite/gcc.dg/void-cast-2.c	1970-01-01 00:00:00.000000000 +0000
+++ GCC/gcc/testsuite/gcc.dg/void-cast-2.c	2005-04-27 23:16:49.000000000 +0000
@@ -0,0 +1,8 @@
+/* Test further cases of warnings for comma expressions, with and
+   without casts to void.  */
+/* Origin: Joseph Myers <joseph@codesourcery.com> */
+/* { dg-do compile } */
+/* { dg-options "-Wall" } */
+int a, b, c, d;
+int e(void) { return (char)a, b; } /* { dg-warning "warning: left-hand operand of comma expression has no effect" } */
+int f(void) { return (a ? (void)b : (void)c), d; } /* { dg-warning "warning: left-hand operand of comma expression has no effect" } */



More information about the Gcc-patches mailing list