This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Patch] bogus signed/unsigned warning


Hello,

this is a resubmission of a patch I proposed a few days ago.
I have cleaned up the source code formatting and resolved a mix-up
of language-independent and language-specific files.

Description:
gcc used to issue bogus warnings with -Wsign-compare if one of the 
operands was a statement expression. This caused testcases 10 and 12 in
gcc.dg/compare2.c to fail.

ChangeLog:

2002-08-28  Ansgar Esztermann  <ansgar@thphy.uni-duesseldorf.de>

	* c-typeck.c (c_tree_expr_nonnegative_p): New function.
  (build_binary_op): Call c_tree_expr_nonnegative_p rather than
  tree_expr_nonnegative_p.  
  (build_conditional_expr): Likewise.

	* c-tree.h (c_tree_expr_nonnegative_p): Declare.

	* gcc.dg/compare2.c: Remove xfail from cases 10 and 12.

Testing:
make bootstrap on i686-pc-linux-gnu succeeded.
make check yielded the same results as before, except for the two 
testcases mentioned above.

Patch:

Index: c-tree.h
===================================================================
RCS file: /cvsroot/gcc/gcc/gcc/c-tree.h,v
retrieving revision 1.105
diff -c -3 -p -r1.105 c-tree.h
*** c-tree.h	15 Aug 2002 21:16:23 -0000	1.105
--- c-tree.h	28 Aug 2002 08:46:50 -0000
*************** extern tree build_array_ref             
*** 268,273 ****
--- 268,274 ----
  extern tree build_external_ref			PARAMS ((tree, int));
  extern tree parser_build_binary_op              PARAMS ((enum tree_code,
  							 tree, tree));
+ extern int c_tree_expr_nonnegative_p          	PARAMS ((tree));
  extern void readonly_warning			PARAMS ((tree, const char *));
  extern tree build_conditional_expr              PARAMS ((tree, tree, tree));
  extern tree build_compound_expr                 PARAMS ((tree));
Index: c-typeck.c
===================================================================
RCS file: /cvsroot/gcc/gcc/gcc/c-typeck.c,v
retrieving revision 1.203
diff -c -3 -p -r1.203 c-typeck.c
*** c-typeck.c	15 Aug 2002 21:16:23 -0000	1.203
--- c-typeck.c	28 Aug 2002 08:46:53 -0000
*************** build_binary_op (code, orig_op0, orig_op
*** 2463,2469 ****
  		     constant expression involving such literals or a
  		     conditional expression involving such literals)
  		     and it is non-negative.  */
! 		  if (tree_expr_nonnegative_p (sop))
  		    /* OK */;
  		  /* Do not warn if the comparison is an equality operation,
  		     the unsigned quantity is an integral constant, and it
--- 2463,2469 ----
  		     constant expression involving such literals or a
  		     conditional expression involving such literals)
  		     and it is non-negative.  */
! 		  if (c_tree_expr_nonnegative_p (sop))
  		    /* OK */;
  		  /* Do not warn if the comparison is an equality operation,
  		     the unsigned quantity is an integral constant, and it
*************** build_binary_op (code, orig_op0, orig_op
*** 2579,2584 ****
--- 2579,2605 ----
    }
  }
  
+ 
+ /* Return true if `t' is known to be non-negative.  */
+ 
+ int
+ c_tree_expr_nonnegative_p (t)
+      tree t;
+ {
+   if (TREE_CODE (t) == STMT_EXPR)
+     {
+       t=COMPOUND_BODY (STMT_EXPR_STMT (t));
+ 
+       /* Find the last statement in the chain, ignoring the final
+ 	     * scope statement */
+       while (TREE_CHAIN (t) != NULL_TREE 
+              && TREE_CODE (TREE_CHAIN (t)) != SCOPE_STMT)
+         t=TREE_CHAIN (t);
+       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
+     }
+   return tree_expr_nonnegative_p (t);
+ }
+ 
  /* Return a tree for the difference of pointers OP0 and OP1.
     The resulting tree has type int.  */
  
*************** build_conditional_expr (ifexp, op1, op2)
*** 3406,3413 ****
  	      /* Do not warn if the signed quantity is an unsuffixed
  		 integer literal (or some static constant expression
  		 involving such literals) and it is non-negative.  */
! 	      else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
! 		       || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
  		/* OK */;
  	      else
  		warning ("signed and unsigned type in conditional expression");
--- 3427,3434 ----
  	      /* Do not warn if the signed quantity is an unsuffixed
  		 integer literal (or some static constant expression
  		 involving such literals) and it is non-negative.  */
! 	      else if ((unsigned_op2 && c_tree_expr_nonnegative_p (op1))
! 		       || (unsigned_op1 && c_tree_expr_nonnegative_p (op2)))
  		/* OK */;
  	      else
  		warning ("signed and unsigned type in conditional expression");
Index: testsuite/gcc.dg/compare2.c
===================================================================
RCS file: /cvsroot/gcc/gcc/gcc/testsuite/gcc.dg/compare2.c,v
retrieving revision 1.5
diff -c -3 -p -r1.5 compare2.c
*** testsuite/gcc.dg/compare2.c	19 Sep 2000 07:33:44 -0000	1.5
--- testsuite/gcc.dg/compare2.c	28 Aug 2002 08:47:05 -0000
*************** void f(int x, unsigned int y)
*** 26,36 ****
  
    /* Statement expression.  */
    x > ({tf; 64;}); /* { dg-bogus "signed and unsigned" "case 9" } */
!   y > ({tf; 64;}); /* { dg-bogus "signed and unsigned" "case 10" { xfail *-*-* } } */
  
    /* Statement expression with recursive ?: .  */
    x > ({tf; tf?64:(tf?128:256);}); /* { dg-bogus "signed and unsigned" "case 11" } */
!   y > ({tf; tf?64:(tf?128:256);}); /* { dg-bogus "signed and unsigned" "case 12" { xfail *-*-* } } */
  
    /* Statement expression with signed ?:.  */
    x > ({tf; tf?64:-1;}); /* { dg-bogus "signed and unsigned" "case 13" } */
--- 26,36 ----
  
    /* Statement expression.  */
    x > ({tf; 64;}); /* { dg-bogus "signed and unsigned" "case 9" } */
!   y > ({tf; 64;}); /* { dg-bogus "signed and unsigned" "case 10" } */
  
    /* Statement expression with recursive ?: .  */
    x > ({tf; tf?64:(tf?128:256);}); /* { dg-bogus "signed and unsigned" "case 11" } */
!   y > ({tf; tf?64:(tf?128:256);}); /* { dg-bogus "signed and unsigned" "case 12" } */
  
    /* Statement expression with signed ?:.  */
    x > ({tf; tf?64:-1;}); /* { dg-bogus "signed and unsigned" "case 13" } */

Ansgar

-- 
Institut fuer theoretische Physik II    | Geb. 25.32.02                           
Heinrich-Heine-Universitaet Duesseldorf | +49-211-8112059 (phone)
Universitaetsstr. 1                     | +49-211-8112262 (fax)
D-40225 Duesseldorf, Germany            | 










Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]