[backport from gcc-4.7/trunk r183178 <mikpe@it.uu.se>]

gcc/

2012-01-13  Ian Lance Taylor  <iant@google.com>

	PR c++/50012
	* tree.h (TYPE_QUALS): Add cast to int.
	(TYPE_QUALS_NO_ADDR_SPACE): Likewise.

gcc/cp/

2012-01-13  Ian Lance Taylor  <iant@google.com>

	PR c++/50012
	* typeck.c (enum_cast_to_int): New static function.
	(cp_build_binary_op): When handling warn_sign_compare, don't test
	for TREE_NO_WARNING.  Do call enum_cast_to_int.
	* call.c (avoid_sign_compare_warnings): Remove static function.
	(build_new_op_1): Don't call avoid_sign_compare_warnings.

gcc/testsuite/

2012-01-13  Ian Lance Taylor  <iant@google.com>

	PR c++/50012
	* g++.dg/warn/Wsign-compare-4.C: New.

--- gcc-4.6.2/gcc/cp/call.c.~1~	2011-10-11 20:18:25.000000000 +0200
+++ gcc-4.6.2/gcc/cp/call.c	2012-01-18 14:44:32.000000000 +0100
@@ -4627,32 +4627,10 @@ add_candidates (tree fns, tree first_arg
     }
 }
 
-/* Even unsigned enum types promote to signed int.  We don't want to
-   issue -Wsign-compare warnings for this case.  Here ORIG_ARG is the
-   original argument and ARG is the argument after any conversions
-   have been applied.  We set TREE_NO_WARNING if we have added a cast
-   from an unsigned enum type to a signed integer type.  */
-
-static void
-avoid_sign_compare_warnings (tree orig_arg, tree arg)
-{
-  if (orig_arg != NULL_TREE
-      && arg != NULL_TREE
-      && orig_arg != arg
-      && TREE_CODE (TREE_TYPE (orig_arg)) == ENUMERAL_TYPE
-      && TYPE_UNSIGNED (TREE_TYPE (orig_arg))
-      && INTEGRAL_TYPE_P (TREE_TYPE (arg))
-      && !TYPE_UNSIGNED (TREE_TYPE (arg)))
-    TREE_NO_WARNING (arg) = 1;
-}
-
 tree
 build_new_op (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3,
 	      bool *overloaded_p, tsubst_flags_t complain)
 {
-  tree orig_arg1 = arg1;
-  tree orig_arg2 = arg2;
-  tree orig_arg3 = arg3;
   struct z_candidate *candidates = 0, *cand;
   VEC(tree,gc) *arglist;
   tree fnname;
@@ -4947,10 +4925,6 @@ build_new_op (enum tree_code code, int f
     return result;
 
  builtin:
-  avoid_sign_compare_warnings (orig_arg1, arg1);
-  avoid_sign_compare_warnings (orig_arg2, arg2);
-  avoid_sign_compare_warnings (orig_arg3, arg3);
-
   switch (code)
     {
     case MODIFY_EXPR:
--- gcc-4.6.2/gcc/cp/typeck.c.~1~	2011-09-27 04:19:05.000000000 +0200
+++ gcc-4.6.2/gcc/cp/typeck.c	2012-01-18 14:42:28.000000000 +0100
@@ -3564,6 +3564,29 @@ build_x_array_ref (tree arg1, tree arg2,
   return expr;
 }
 
+/* Return whether OP is an expression of enum type cast to integer
+   type.  In C++ even unsigned enum types are cast to signed integer
+   types.  We do not want to issue warnings about comparisons between
+   signed and unsigned types when one of the types is an enum type.
+   Those warnings are always false positives in practice.  */
+
+static bool
+enum_cast_to_int (tree op)
+{
+  if (TREE_CODE (op) == NOP_EXPR
+      && TREE_TYPE (op) == integer_type_node
+      && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
+      && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
+    return true;
+
+  /* The cast may have been pushed into a COND_EXPR.  */
+  if (TREE_CODE (op) == COND_EXPR)
+    return (enum_cast_to_int (TREE_OPERAND (op, 1))
+	    || enum_cast_to_int (TREE_OPERAND (op, 2)));
+
+  return false;
+}
+
 /* For the c-common bits.  */
 tree
 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
@@ -4413,13 +4436,15 @@ cp_build_binary_op (location_t location,
 
       if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
 	  && warn_sign_compare
-	  && !TREE_NO_WARNING (orig_op0)
-	  && !TREE_NO_WARNING (orig_op1)
 	  /* Do not warn until the template is instantiated; we cannot
 	     bound the ranges of the arguments until that point.  */
 	  && !processing_template_decl
           && (complain & tf_warning)
-	  && c_inhibit_evaluation_warnings == 0)
+	  && c_inhibit_evaluation_warnings == 0
+	  /* Even unsigned enum types promote to signed int.  We don't
+	     want to issue -Wsign-compare warnings for this case.  */
+	  && !enum_cast_to_int (orig_op0)
+	  && !enum_cast_to_int (orig_op1))
 	{
 	  warn_for_sign_compare (location, orig_op0, orig_op1, op0, op1, 
 				 result_type, resultcode);
--- gcc-4.6.2/gcc/testsuite/g++.dg/warn/Wsign-compare-4.C.~1~	1970-01-01 01:00:00.000000000 +0100
+++ gcc-4.6.2/gcc/testsuite/g++.dg/warn/Wsign-compare-4.C	2012-01-18 14:42:28.000000000 +0100
@@ -0,0 +1,12 @@
+//PR c++/50012
+// { dg-options "-Wsign-compare" }
+
+int foo(unsigned int *a, int b)
+{
+  return (*a) <= b; // { dg-warning "comparison between signed and unsigned" }
+}
+
+int bar(unsigned int *a, int b)
+{
+  return *a <= b; // { dg-warning "comparison between signed and unsigned" }
+}
--- gcc-4.6.2/gcc/tree.h.~1~	2011-10-06 21:57:52.000000000 +0200
+++ gcc-4.6.2/gcc/tree.h	2012-01-18 14:42:28.000000000 +0100
@@ -2223,16 +2223,16 @@ extern enum machine_mode vector_type_mod
 
 /* The set of type qualifiers for this type.  */
 #define TYPE_QUALS(NODE)					\
-  ((TYPE_READONLY (NODE) * TYPE_QUAL_CONST)			\
-   | (TYPE_VOLATILE (NODE) * TYPE_QUAL_VOLATILE)		\
-   | (TYPE_RESTRICT (NODE) * TYPE_QUAL_RESTRICT)		\
-   | (ENCODE_QUAL_ADDR_SPACE (TYPE_ADDR_SPACE (NODE))))
+  ((int) ((TYPE_READONLY (NODE) * TYPE_QUAL_CONST)		\
+	  | (TYPE_VOLATILE (NODE) * TYPE_QUAL_VOLATILE)		\
+	  | (TYPE_RESTRICT (NODE) * TYPE_QUAL_RESTRICT)		\
+	  | (ENCODE_QUAL_ADDR_SPACE (TYPE_ADDR_SPACE (NODE)))))
 
 /* The same as TYPE_QUALS without the address space qualifications.  */
 #define TYPE_QUALS_NO_ADDR_SPACE(NODE)				\
-  ((TYPE_READONLY (NODE) * TYPE_QUAL_CONST)			\
-   | (TYPE_VOLATILE (NODE) * TYPE_QUAL_VOLATILE)		\
-   | (TYPE_RESTRICT (NODE) * TYPE_QUAL_RESTRICT))
+  ((int) ((TYPE_READONLY (NODE) * TYPE_QUAL_CONST)		\
+	  | (TYPE_VOLATILE (NODE) * TYPE_QUAL_VOLATILE)		\
+	  | (TYPE_RESTRICT (NODE) * TYPE_QUAL_RESTRICT)))
 
 /* These flags are available for each language front end to use internally.  */
 #define TYPE_LANG_FLAG_0(NODE) (TYPE_CHECK (NODE)->type.lang_flag_0)
