[PING^3] PR 21438 : Warning about division by zero depends on lexical form

Manuel López-Ibáñez lopezibanez@gmail.com
Mon Mar 12 04:40:00 GMT 2007


On 02/03/07, Mark Mitchell <mark@codesourcery.com> wrote:
>
> But, the first thing to do is to make the C++ front end behave like the
> C front end.  Then, we can decide what, if anything to do next.
>

Done. Bootstrapped and regression tested. Funny enough, no C++
testcase needs adjustment.

:ADDPATCH C/C++:

2007-03-12  Manuel Lopez-Ibanez  <manu@gcc.gnu.org>

  PR 21438
  * c-common.h (warn_for_div_by_zero): Declare.
  * c-common.c (warn_for_div_by_zero): Define.
  * c-typeck.c (build_binary_op): Call warn_for_div_zero instead of warning.

cp/
  * typeck.c (build_binary_op): Call warn_for_div_zero instead of warning.
-------------- next part --------------
Index: gcc/cp/typeck.c
===================================================================
--- gcc/cp/typeck.c	(revision 122755)
+++ gcc/cp/typeck.c	(working copy)
@@ -3130,14 +3130,11 @@ build_binary_op (enum tree_code code, tr
 	  && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
 	      || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
 	{
 	  enum tree_code tcode0 = code0, tcode1 = code1;
 
-	  if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
-	    warning (OPT_Wdiv_by_zero, "division by zero in %<%E / 0%>", op0);
-	  else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
-	    warning (OPT_Wdiv_by_zero, "division by zero in %<%E / 0.%>", op0);
+	  warn_for_div_by_zero (op0, op1);
 
 	  if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
 	    tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
 	  if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
 	    tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
@@ -3167,14 +3164,11 @@ build_binary_op (enum tree_code code, tr
 	shorten = -1;
       break;
 
     case TRUNC_MOD_EXPR:
     case FLOOR_MOD_EXPR:
-      if (code1 == INTEGER_TYPE && integer_zerop (op1))
-	warning (OPT_Wdiv_by_zero, "division by zero in %<%E %% 0%>", op0);
-      else if (code1 == REAL_TYPE && real_zerop (op1))
-	warning (OPT_Wdiv_by_zero, "division by zero in %<%E %% 0.%>", op0);
+      warn_for_div_by_zero (op0, op1);
 
       if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
 	{
 	  /* Although it would be tempting to shorten always here, that loses
 	     on some targets, since the modulo instruction is undefined if the
Index: gcc/c-typeck.c
===================================================================
--- gcc/c-typeck.c	(revision 122755)
+++ gcc/c-typeck.c	(working copy)
@@ -7846,14 +7846,11 @@ build_binary_op (enum tree_code code, tr
     case TRUNC_DIV_EXPR:
     case CEIL_DIV_EXPR:
     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 (OPT_Wdiv_by_zero, "division by zero");
+      warn_for_div_by_zero (op0, op1);
 
       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
 	   || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
 	  && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
 	      || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
@@ -7889,12 +7886,11 @@ build_binary_op (enum tree_code code, tr
 	common = 1;
       break;
 
     case TRUNC_MOD_EXPR:
     case FLOOR_MOD_EXPR:
-      if (skip_evaluation == 0 && integer_zerop (op1))
-	warning (OPT_Wdiv_by_zero, "division by zero");
+      warn_for_div_by_zero (op0, op1);
 
       if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
 	{
 	  /* Although it would be tempting to shorten always here, that loses
 	     on some targets, since the modulo instruction is undefined if the
Index: gcc/c-common.c
===================================================================
--- gcc/c-common.c	(revision 122755)
+++ gcc/c-common.c	(working copy)
@@ -7032,6 +7032,16 @@ c_build_cdtor_fns (void)
 
 #ifndef TARGET_HAS_TARGETCM
 struct gcc_targetcm targetcm = TARGETCM_INITIALIZER;
 #endif
 
+/* Warn for division by zero.  */
+void
+warn_for_div_by_zero (tree ARG_UNUSED (op0), tree op1)
+{
+  /* Floating point division by zero is a legitimate way to obtain
+     infinities and NaNs.  */
+  if (skip_evaluation == 0 && integer_zerop (op1))
+    warning (OPT_Wdiv_by_zero, "division by zero");
+}
+
 #include "gt-c-common.h"
Index: gcc/c-common.h
===================================================================
--- gcc/c-common.h	(revision 122755)
+++ gcc/c-common.h	(working copy)
@@ -869,10 +869,11 @@ extern tree builtin_type_for_size (int, 
 
 extern void warn_array_subscript_with_type_char (tree);
 extern void warn_about_parentheses (enum tree_code, enum tree_code,
 				    enum tree_code);
 extern void warn_for_unused_label (tree label);
+extern void warn_for_div_by_zero (tree op0, tree op1);
 
 
 /* In c-gimplify.c  */
 extern void c_genericize (tree);
 extern int c_gimplify_expr (tree *, tree *, tree *);


More information about the Gcc-patches mailing list