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]

[C PATCH] New -Woverflow option, pass OPT_Woverflow to warning.


The following patch is the next installment in the continuing series of
patches to clean-up TREE_OVERFLOW handling in GCC.  This simple patch
introduces the -Woverflow command line option so that OPT_Woverflow can
be passed as the first argument to warning to control these diagnostics
using -Wno-overflow.  Future patches may potentially introduce new
instances of overflow warnings, so having a command line option to silence
them if necessary should minimize any inconvenience.

The following patch has been tested on i686-pc-linux-gnu with a full
"make bootstrap", all default languages, and regression tested with a
top-level "make -k check" with no new failures.  The documentation
change was tested with a top-level "make dvi".

Ok for mainline?



2006-04-30  Roger Sayle  <roger@eyesopen.com>

	* common.opt (Woverflow): New command line option.
	* c-common.c (constant_expression_warning): Check warn_overflow.
	(overflow_waring): Pass OPT_Woverflow to warning.
	(unsigned_conversion_warning): Likewise.
	(convert_and_check): Likewise.
	* doc/invoke.texi: Document new command line option.

	* gcc.dg/Woverflow-1.c: New test case.
	* gcc.dg/Woverflow-2.c: Likewise.
	* gcc.dg/Woverflow-3.c: Likewise.


Index: common.opt
===================================================================
--- common.opt	(revision 113318)
+++ common.opt	(working copy)
@@ -109,6 +109,10 @@
 Common Var(warn_missing_noreturn)
 Warn about functions which might be candidates for __attribute__((noreturn))

+Woverflow
+Common Var(warn_overflow) Init(1)
+Warn about overflow in arithmetic expressions
+
 Wpacked
 Common Var(warn_packed)
 Warn when the packed attribute has no effect on struct layout
Index: c-common.c
===================================================================
--- c-common.c	(revision 113318)
+++ c-common.c	(working copy)
@@ -906,7 +906,9 @@
   if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
        || TREE_CODE (value) == VECTOR_CST
        || TREE_CODE (value) == COMPLEX_CST)
-      && TREE_CONSTANT_OVERFLOW (value) && pedantic)
+      && TREE_CONSTANT_OVERFLOW (value)
+      && warn_overflow
+      && pedantic)
     pedwarn ("overflow in constant expression");
 }

@@ -927,7 +929,7 @@
     {
       TREE_OVERFLOW (value) = 0;
       if (skip_evaluation == 0)
-	warning (0, "integer overflow in expression");
+	warning (OPT_Woverflow, "integer overflow in expression");
     }
   else if ((TREE_CODE (value) == REAL_CST
 	    || (TREE_CODE (value) == COMPLEX_CST
@@ -936,13 +938,13 @@
     {
       TREE_OVERFLOW (value) = 0;
       if (skip_evaluation == 0)
-	warning (0, "floating point overflow in expression");
+	warning (OPT_Woverflow, "floating point overflow in expression");
     }
   else if (TREE_CODE (value) == VECTOR_CST && TREE_OVERFLOW (value))
     {
       TREE_OVERFLOW (value) = 0;
       if (skip_evaluation == 0)
-	warning (0, "vector overflow in expression");
+	warning (OPT_Woverflow, "vector overflow in expression");
     }
 }

@@ -964,7 +966,8 @@
     {
       if (!int_fits_type_p (operand, c_common_signed_type (type)))
 	/* This detects cases like converting -129 or 256 to unsigned char.  */
-	warning (0, "large integer implicitly truncated to unsigned type");
+	warning (OPT_Woverflow,
+		 "large integer implicitly truncated to unsigned type");
       else
 	warning (OPT_Wconversion,
 		 "negative integer implicitly converted to unsigned type");
@@ -1093,7 +1096,8 @@
 		 || !constant_fits_type_p (expr,
 					   c_common_unsigned_type (type)))
 		&& skip_evaluation == 0)
-	      warning (0, "overflow in implicit constant conversion");
+	      warning (OPT_Woverflow,
+                       "overflow in implicit constant conversion");
 	}
       else
 	unsigned_conversion_warning (t, expr);
Index: doc/invoke.texi
===================================================================
--- doc/invoke.texi	(revision 113318)
+++ doc/invoke.texi	(working copy)
@@ -238,7 +238,8 @@
 -Wmain  -Wmissing-braces  -Wmissing-field-initializers @gol
 -Wmissing-format-attribute  -Wmissing-include-dirs @gol
 -Wmissing-noreturn @gol
--Wno-multichar  -Wnonnull  -Woverlength-strings  -Wpacked  -Wpadded @gol
+-Wno-multichar  -Wnonnull  -Wno-overflow @gol
+-Woverlength-strings  -Wpacked  -Wpadded @gol
 -Wparentheses  -Wpointer-arith  -Wno-pointer-to-int-cast @gol
 -Wredundant-decls @gol
 -Wreturn-type  -Wsequence-point  -Wshadow @gol
@@ -3284,6 +3285,10 @@
 (@pxref{Function Attributes}, @pxref{Variable Attributes},
 @pxref{Type Attributes}.)

+@item -Wno-overflow
+@opindex Wno-overflow
+Do not warn about compile-time overflow in constant expressions.
+
 @item -Wpacked
 @opindex Wpacked
 Warn if a structure is given the packed attribute, but the packed


/* { dg-do compile } */
/* { dg-options "-O2" } */

#include <limits.h>

int foo = INT_MAX + 1;  /* { dg-warning "integer overflow" } */


/* { dg-do compile } */
/* { dg-options "-O2 -Woverflow" } */

#include <limits.h>

int foo = INT_MAX + 1;  /* { dg-warning "integer overflow" } */


/* { dg-do compile } */
/* { dg-options "-O2 -Wno-overflow" } */

#include <limits.h>

int foo = INT_MAX + 1;


Roger
--


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