Patchwork [C++] Fix -Wdiv-by-zero regression (PR c++/56607)

login
register
mail settings
Submitter Jakub Jelinek
Date March 13, 2013, 6:18 p.m.
Message ID <20130313181802.GW12913@tucnak.redhat.com>
Download mbox | patch
Permalink /patch/227316/
State New
Headers show

Comments

Jakub Jelinek - March 13, 2013, 6:18 p.m.
Hi!

My sizeof deferred folding changes led to the following regression.

Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, acked
by Jason in the PR, queued for 4.8.1 and 4.9.

2013-03-13  Jakub Jelinek  <jakub@redhat.com>

	PR c++/56607
	* typeck.c (cp_build_binary_op): When calling warn_for_div_by_zero,
	pass op1 through maybe_constant_value first.

	* g++.dg/warn/Wdiv-by-zero-2.C: New test.
	* c-c++-common/pr56607.c: New test.


	Jakub

Patch

--- gcc/cp/typeck.c.jj	2013-03-12 09:59:36.000000000 +0100
+++ gcc/cp/typeck.c	2013-03-13 09:04:26.011917793 +0100
@@ -4015,7 +4015,7 @@  cp_build_binary_op (location_t location,
 	{
 	  enum tree_code tcode0 = code0, tcode1 = code1;
 
-	  warn_for_div_by_zero (location, op1);
+	  warn_for_div_by_zero (location, maybe_constant_value (op1));
 
 	  if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
 	    tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
@@ -4051,7 +4051,7 @@  cp_build_binary_op (location_t location,
 
     case TRUNC_MOD_EXPR:
     case FLOOR_MOD_EXPR:
-      warn_for_div_by_zero (location, op1);
+      warn_for_div_by_zero (location, maybe_constant_value (op1));
 
       if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
 	  && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
--- gcc/testsuite/g++.dg/warn/Wdiv-by-zero-2.C.jj	2013-03-13 09:16:50.550483072 +0100
+++ gcc/testsuite/g++.dg/warn/Wdiv-by-zero-2.C	2013-03-13 09:22:28.213474528 +0100
@@ -0,0 +1,34 @@ 
+// PR c++/56607
+// { dg-do compile { target { { lp64 || ilp32 } || llp64 } } }
+// { dg-options "-O2 -Wdiv-by-zero -std=c++11" }
+
+constexpr int sc () { return sizeof (char); }
+constexpr int si () { return sizeof (int); }
+constexpr int zc () { return sc () - 1; }
+constexpr int zi (int d) { return si () / d - 1; }
+
+int
+f1 (void)
+{
+  return 1 / zc ();			// { dg-warning "division by zero" }
+}
+
+int
+f2 (void)
+{
+  constexpr int x = zc ();
+  return 1 / x;				// { dg-warning "division by zero" }
+}
+
+int
+f3 (void)
+{
+  return 1 / zi (3);			// { dg-warning "division by zero" }
+}
+
+int
+f4 (void)
+{
+  constexpr int x = zi (3);
+  return 1 / x;				// { dg-warning "division by zero" }
+}
--- gcc/testsuite/c-c++-common/pr56607.c.jj	2013-03-13 09:17:03.671405262 +0100
+++ gcc/testsuite/c-c++-common/pr56607.c	2013-03-13 09:21:54.892673827 +0100
@@ -0,0 +1,29 @@ 
+/* PR c++/56607 */
+/* { dg-do compile { target { { lp64 || ilp32 } || llp64 } } } */
+/* { dg-options "-O2 -Wdiv-by-zero" } */
+
+int
+f1 (void)
+{
+  return 1 / (sizeof (char) - 1);	/* { dg-warning "division by zero" } */
+}
+
+int
+f2 (void)
+{
+  const int x = sizeof (char) - 1;
+  return 1 / x;				/* { dg-warning "division by zero" "" { target c++ } } */
+}
+
+int
+f3 (void)
+{
+  return 1 / (sizeof (int) / 3 - 1);	/* { dg-warning "division by zero" } */
+}
+
+int
+f4 (void)
+{
+  const int x = sizeof (int) / 3 - 1;
+  return 1 / x;				/* { dg-warning "division by zero" "" { target c++ } } */
+}