PR 21438 : Warning about division by zero depends on lexical form

Manuel López-Ibáñez lopezibanez@gmail.com
Sun Jan 14 15:16:00 GMT 2007


:ADDPATCH c:

The following patch was proposed by Andrew Pinski but never properly submitted.

The C front-end does not warning about division by zero for floating
point because it is a legitimate way of obtaining infinities and NaNs.
With this patch we don't emit the warning if the integer zero is
promoted to real type.

Bootstrapped and regression tested with --enable-languages=all for
revision 120588 on i686-pc-gnu-linux.


The C++ front-end, on the contrary, warns always. Is this an inconsistency?

2007-01-14  Andrew Pinski  <pinskia@gmail.com>
                    Manuel Lopez-Ibanez  <manu@gcc.gnu.org>

  PR c/21438
  * gcc/c-typeck.c (build_binary_op): Don't give division by zero
warning if integer zero is promoted to REAL_TYPE.

testsuite:
2007-01-14  Andrew Pinski  <pinskia@gmail.com>
                    Manuel Lopez-Ibanez  <manu@gcc.gnu.org>

  PR c/21438
  * gcc.dg/Wdiv-by-zero.c: New.
-------------- next part --------------
Index: gcc/testsuite/gcc.dg/Wdiv-by-zero.c
===================================================================
--- gcc/testsuite/gcc.dg/Wdiv-by-zero.c	(revision 0)
+++ gcc/testsuite/gcc.dg/Wdiv-by-zero.c	(revision 0)
@@ -0,0 +1,10 @@
+/* PR 21438 : Floating point division by zero is a legitimate way to
+obtain infinities and NaNs.  */
+/* { dg-do compile } */
+/* { dg-options "-Wdiv-by-zero" } */
+
+float f[] = {
+  1.0f/0.0f,  /* { dg-bogus "division by zero" } */
+  1.0f/0,      /* { dg-bogus "division by zero" } */
+  1/0.f       /* { dg-bogus "division by zero" } */
+};
Index: gcc/c-typeck.c
===================================================================
--- gcc/c-typeck.c	(revision 120588)
+++ gcc/c-typeck.c	(working copy)
@@ -7836,7 +7836,7 @@ build_binary_op (enum tree_code code, tr
     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))
+      if (skip_evaluation == 0 && integer_zerop (op1) && code0 != REAL_TYPE)
 	warning (OPT_Wdiv_by_zero, "division by zero");
 
       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
@@ -7877,7 +7877,7 @@ build_binary_op (enum tree_code code, tr
 
     case TRUNC_MOD_EXPR:
     case FLOOR_MOD_EXPR:
-      if (skip_evaluation == 0 && integer_zerop (op1))
+      if (skip_evaluation == 0 && integer_zerop (op1) && code0 != REAL_TYPE)
 	warning (OPT_Wdiv_by_zero, "division by zero");
 
       if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)


More information about the Gcc-patches mailing list