@@ -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
@@ -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" }
+}
@@ -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++ } } */
+}