patch for compare1.c test failure

Zack Weinberg zack@bitmover.com
Mon Oct 25 18:08:00 GMT 1999


This patch fixes the compare1.c test failure, which is a spurious
warning (comparison between signed and unsigned) for code like

 enum machine_mode mode = DImode;
 
 return mode == (target_flags ?	DImode : SImode);

The approach taken has, I hope, minimal effects on the rest of the
compiler.  Another option would be to make enums with all nonnegative
values signed when they will fit in signed int.  This would be closer
to the way numeric integer constants work, but would affect stuff all
over the place.

zw

1999-10-25 18:07 -0700  Zack Weinberg  <zack@bitmover.com>

	* c-typeck.c (build_binary_op): Don't warn about
	signed/unsigned comparison when the unsigned quantity is an
	enum whose values fit entirely in the range of the signed
	quantity.

===================================================================
Index: c-typeck.c
--- c-typeck.c	1999/10/20 00:18:09	1.41
+++ c-typeck.c	1999/10/26 01:06:56
@@ -2418,6 +2418,18 @@ build_binary_op (code, orig_op0, orig_op
 		       || (op1_signed && TREE_CODE (xop1) == INTEGER_CST
 			   && tree_int_cst_sgn (xop1) >= 0))
 		/* OK */;
+	      /* Do not warn if the unsigned quantity is an enumeration
+		 and its maximum value is representable in the type of
+		 the signed quantity.  */
+	      else if ((!op0_signed
+			&& TREE_CODE (TREE_TYPE (xop0)) == ENUMERAL_TYPE
+			&& int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (xop0)),
+					    TREE_TYPE (xop1)))
+		       || (!op1_signed
+			   && TREE_CODE (TREE_TYPE (xop0)) == ENUMERAL_TYPE
+			   && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (xop0)),
+					       TREE_TYPE (xop1))))
+		/* OK */;
 	      /* Do not warn if the comparison is an equality operation,
                  the unsigned quantity is an integral constant and it does
                  not use the most significant bit of result_type.  */


More information about the Gcc-patches mailing list