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] [5of5] looking for feedback on Wcoercion warning (for stage1)


(The following patch implements part of the functionality of the
Wcoercion project as explained in
http://gcc.gnu.org/wiki/Wcoercion#Background ).

The function unsigned_conversion_warning() in gcc/c-common.c
encapsulates two different and very particular warnings:
1) Warn for negative integer values assigned to unsigned type if
-Wcoercion option is used.
2) Warn for  a large constant truncated to unsigned type.

In order to tidy up convert_and_check() and keep toghether the
warnings produced by Wcoercion, I propose the removal of
unsigned_conversion_warning(). Warning 1) is moved into
coercion_warning() while warning 2) is moved directly into
convert_and_check().

This is the last patch of a series of 5 patches. The previous patch
can be found at
http://gcc.gnu.org/ml/gcc-patches/2006-08/msg00289.html .

Bootstrapped and tested with --enable-languages=all for trunk revision
115951 on i686-pc-linux-gnu

:ADDPATCH c/c++:
diff -Epaur --unidirectional-new-file --exclude='*svn*' --exclude='#*' --exclude='*~' 4-gplusplus/gcc/c-common.c 5-kill-unsigned_conversion_warning/gcc/c-common.c
--- 4-gplusplus/gcc/c-common.c	2006-08-07 19:22:11.000000000 +0100
+++ 5-kill-unsigned_conversion_warning/gcc/c-common.c	2006-08-07 19:23:09.000000000 +0100
@@ -948,31 +948,6 @@ overflow_warning (tree value)
     }
 }
 
-/* Print a warning if a large constant is truncated to unsigned,
-   or if -Wcoercion is used and a constant < 0 is converted to unsigned.
-   Invoke this function on every expression that might be implicitly
-   converted to an unsigned type.  */
-
-static void
-unsigned_conversion_warning (tree result, tree operand)
-{
-  tree type = TREE_TYPE (result);
-
-  if (TREE_CODE (operand) == INTEGER_CST
-      && TREE_CODE (type) == INTEGER_TYPE
-      && TYPE_UNSIGNED (type)
-      && skip_evaluation == 0
-      && !int_fits_type_p (operand, type))
-    {
-      if (!int_fits_type_p (operand, c_common_signed_type (type)))
-	/* This detects cases like converting -129 or 256 to unsigned char.  */
-	warning (OPT_Woverflow,
-		 "large integer implicitly truncated to unsigned type");
-      else
-	warning (OPT_Wcoercion,
-		 "negative integer implicitly converted to unsigned type");
-    }
-}
 
 /* Print a warning about casts that might indicate violation
    of strict aliasing rules if -Wstrict-aliasing is used and
@@ -1091,10 +1066,14 @@ coercion_warning (tree type, tree expr)
          unsigned types are detected by unsigned_conversion_warning.  */
       else if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
                && TREE_CODE (type) == INTEGER_TYPE
-               && !int_fits_type_p (expr, type)
-               && !(TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (TREE_TYPE (expr))))
-        give_warning = true;
-
+               && !int_fits_type_p (expr, type))
+        {
+          if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (TREE_TYPE (expr)))
+            warning (OPT_Wcoercion,
+                     "negative integer implicitly converted to unsigned type");
+          else
+            give_warning = true;
+        }
       else if (TREE_CODE (type) == REAL_TYPE)
         {
           if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE)
@@ -1209,7 +1188,21 @@ convert_and_check (tree type, tree expr)
                        "overflow in implicit constant conversion");
 	}
       else
-	unsigned_conversion_warning (t, expr);
+        {
+          /* Print a warning if a large constant is truncated to unsigned.  */
+          if (TREE_CODE (expr) == INTEGER_CST
+              && TREE_CODE (type) == INTEGER_TYPE
+              && TYPE_UNSIGNED (type)
+              && skip_evaluation == 0
+              && !int_fits_type_p (expr, type)
+              && !int_fits_type_p (expr, c_common_signed_type (type)))
+            {
+              /* This detects cases like converting -129 or 256 to
+                 unsigned char.  */
+              warning (OPT_Woverflow,
+                       "large integer implicitly truncated to unsigned type");
+            }
+        }
     }
   return t;
 }

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