This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[RFA] Close PR 150: division by zero [take 2]
- To: gcc-patches at gcc dot gnu dot org
- Subject: [RFA] Close PR 150: division by zero [take 2]
- From: Neil Booth <neil at daikokuya dot demon dot co dot uk>
- Date: Mon, 22 Oct 2001 18:49:33 +0100
- Cc: "Joseph S. Myers" <jsm28 at cam dot ac dot uk>, Richard Henderson <rth at redhat dot com>
- References: <20011021212845.A558@daikokuya.demon.co.uk>
This does the division by zero warning for the C front end only, does
not warn for the unevaluated part of various expressions, and adds a
command line option.
The C++ front end requires more work, since it has appears to have no
concept of skip_evaluation, so I won't do that in this patch.
Bootstrapping x86 Linux. OK to commit?
Neil.
* c-common.c (warn_div_by_zero): New.
* c-common.h (warn_div_by_zero): New.
* c-decl.c (c_decode_option): Take it on the command line.
* c-typeck.c (build_binary_op): Warn about division by zero.
* doc/invoke.texi: Document the new command line option.
* testsuite/gcc.dg/divbyzero.c: New tests.
* testsuite/gcc.dg/noncompile/20010524-1.c: Update.
Index: c-common.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-common.c,v
retrieving revision 1.264
diff -u -p -r1.264 c-common.c
--- c-common.c 2001/10/17 09:40:21 1.264
+++ c-common.c 2001/10/22 17:38:17
@@ -194,6 +194,9 @@ int flag_short_wchar;
int warn_sequence_point;
+/* Nonzero means to warn about compile-time division by zero. */
+int warn_div_by_zero = 1;
+
/* The elements of `ridpointers' are identifier nodes for the reserved
type names and storage classes. It is indexed by a RID_... value. */
tree *ridpointers;
Index: c-common.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-common.h,v
retrieving revision 1.90
diff -u -p -r1.90 c-common.h
--- c-common.h 2001/10/09 14:03:10 1.90
+++ c-common.h 2001/10/22 17:38:19
@@ -401,6 +401,9 @@ extern int warn_missing_format_attribute
extern int warn_pointer_arith;
+/* Nonzero means to warn about compile-time division by zero. */
+extern int warn_div_by_zero;
+
/* Nonzero means do some things the same way PCC does. */
extern int flag_traditional;
Index: c-decl.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-decl.c,v
retrieving revision 1.256
diff -u -p -r1.256 c-decl.c
--- c-decl.c 2001/10/20 10:03:49 1.256
+++ c-decl.c 2001/10/22 17:38:37
@@ -750,6 +750,10 @@ c_decode_option (argc, argv)
warn_multichar = 1;
else if (!strcmp (p, "-Wno-multichar"))
warn_multichar = 0;
+ else if (!strcmp (p, "-Wdiv-by-zero"))
+ warn_div_by_zero = 1;
+ else if (!strcmp (p, "-Wno-div-by-zero"))
+ warn_div_by_zero = 0;
else if (!strcmp (p, "-Wunknown-pragmas"))
/* Set to greater than 1, so that even unknown pragmas in system
headers will be warned about. */
Index: c-typeck.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-typeck.c,v
retrieving revision 1.141
diff -u -p -r1.141 c-typeck.c
--- c-typeck.c 2001/10/11 03:15:23 1.141
+++ c-typeck.c 2001/10/22 17:38:51
@@ -1973,6 +1973,11 @@ build_binary_op (code, orig_op0, orig_op
case FLOOR_DIV_EXPR:
case ROUND_DIV_EXPR:
case EXACT_DIV_EXPR:
+ /* Floating point division by zero is a legitimate way to obtain
+ infinities and NaNs. */
+ if (skip_evaluation == 0 && integer_zerop (op1))
+ warning ("division by zero");
+
if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
|| code0 == COMPLEX_TYPE)
&& (code1 == INTEGER_TYPE || code1 == REAL_TYPE
@@ -2026,6 +2031,9 @@ build_binary_op (code, orig_op0, orig_op
case TRUNC_MOD_EXPR:
case FLOOR_MOD_EXPR:
+ if (skip_evaluation == 0 && integer_zerop (op1))
+ warning ("division by zero");
+
if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
{
/* Although it would be tempting to shorten always here, that loses
Index: testsuite/gcc.dg/divbyzero.c
===================================================================
RCS file: divbyzero.c
diff -N divbyzero.c
--- /dev/null Tue May 5 13:32:27 1998
+++ divbyzero.c Mon Oct 22 10:38:51 2001
@@ -0,0 +1,22 @@
+/* Copyright (C) 2001 Free Software Foundation, Inc. */
+
+/* { dg-do compile } */
+
+/* Source: Neil Booth, Oct 22 2001. PR 150 - warn about division by
+ zero. */
+
+#define ZERO (4 - 6 + 2)
+#define DZERO (2.0 - 2.0)
+
+int main (int argc, char *argv[])
+{
+ int w = argc % ZERO; /* { dg-warning "division by zero" } */
+ int x = argc / 0; /* { dg-warning "division by zero" } */
+ int y = argc / ZERO; /* { dg-warning "division by zero" } */
+
+ double z = 0.0 / 0.0 ; /* { dg-bogus "division by zero" } */
+ w = (ZERO ? y / ZERO : x); /* { dg-bogus "division by zero" } */
+ x = (ZERO ? argc % ZERO: x); /* { dg-bogus "division by zero" } */
+
+ return 0;
+}
Index: testsuite/gcc.dg/noncompile/20010524-1.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/gcc.dg/noncompile/20010524-1.c,v
retrieving revision 1.2
diff -u -p -r1.2 20010524-1.c
--- 20010524-1.c 2001/05/25 06:34:16 1.2
+++ 20010524-1.c 2001/10/22 17:38:51
@@ -1,2 +1,2 @@
int i = 7 / 0; /* { dg-error "not constant" } */
-
+ /* { dg-warning "division by zero" "div by zero" { target *-*-* } 1 } */
Index: doc/invoke.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/doc/invoke.texi,v
retrieving revision 1.65
diff -u -p -r1.65 invoke.texi
--- invoke.texi 2001/10/22 07:42:23 1.65
+++ invoke.texi 2001/10/22 17:39:23
@@ -208,7 +208,7 @@ in the following sections.
-fsyntax-only -pedantic -pedantic-errors @gol
-w -W -Wall -Waggregate-return @gol
-Wcast-align -Wcast-qual -Wchar-subscripts -Wcomment @gol
--Wconversion -Wdisabled-optimization -Werror @gol
+-Wconversion -Wdisabled-optimization -Wdiv-by-zero -Werror @gol
-Wfloat-equal -Wformat -Wformat=2 @gol
-Wformat-nonliteral -Wformat-security @gol
-Wimplicit -Wimplicit-int @gol
@@ -1773,6 +1773,12 @@ machines.
@opindex Wcomment
Warn whenever a comment-start sequence @samp{/*} appears in a @samp{/*}
comment, or whenever a Backslash-Newline appears in a @samp{//} comment.
+
+@item -Wdiv-by-zero
+@opindex Wdiv-by-zero
+Warn about compile-time integer division by zero. Floating point division
+by zero is not warned about, as it can be a legitimate way of obtaining
+infinities and NaNs.
@item -Wformat
@opindex Wformat