diff mbox

[C] Make -Wdiv-by-zero work even for const ints (PR c/64440)

Message ID 20150106185613.GE16430@redhat.com
State New
Headers show

Commit Message

Marek Polacek Jan. 6, 2015, 6:56 p.m. UTC
Currently the C FE's -Wdiv-by-zero warns only for INTEGER_CSTs,
unlike C++, which can also handle const ints (yes, different
constant expression rules).  But since it's easy to warn for
consts in the C FE as well, and we already warn for shifts
with const ints, I think we can go with the following.  The PR
is a request for this.

Bootstrapped/regtested on {x86_64,ppc64}-linux, ok for trunk?

2015-01-06  Marek Polacek  <polacek@redhat.com>

	PR c/64440
	* c-common.c (c_fully_fold_internal): Warn for division and modulo
	if orig_op1 isn't INTEGER_CST, op1 is INTEGER_CST and is zero.

	* gcc.dg/pr64440.c: New test.
	* c-c++-common/pr56607.c: Don't limit dg-warnings to C++.


	Marek

Comments

Joseph Myers Jan. 6, 2015, 6:58 p.m. UTC | #1
On Tue, 6 Jan 2015, Marek Polacek wrote:

> Currently the C FE's -Wdiv-by-zero warns only for INTEGER_CSTs,
> unlike C++, which can also handle const ints (yes, different
> constant expression rules).  But since it's easy to warn for
> consts in the C FE as well, and we already warn for shifts
> with const ints, I think we can go with the following.  The PR
> is a request for this.
> 
> Bootstrapped/regtested on {x86_64,ppc64}-linux, ok for trunk?

OK.
diff mbox

Patch

diff --git gcc/c-family/c-common.c gcc/c-family/c-common.c
index 4d9dd4d..df4fddd 100644
--- gcc/c-family/c-common.c
+++ gcc/c-family/c-common.c
@@ -1364,6 +1364,17 @@  c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
 				 ? G_("left shift count >= width of type")
 				 : G_("right shift count >= width of type")));
 	}
+      if ((code == TRUNC_DIV_EXPR
+	   || code == CEIL_DIV_EXPR
+	   || code == FLOOR_DIV_EXPR
+	   || code == EXACT_DIV_EXPR
+	   || code == TRUNC_MOD_EXPR)
+	  && TREE_CODE (orig_op1) != INTEGER_CST
+	  && TREE_CODE (op1) == INTEGER_CST
+	  && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
+	      || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
+	  && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE)
+	warn_for_div_by_zero (loc, op1);
       goto out;
 
     case INDIRECT_REF:
diff --git gcc/testsuite/c-c++-common/pr56607.c gcc/testsuite/c-c++-common/pr56607.c
index d7faa81..ba13956 100644
--- gcc/testsuite/c-c++-common/pr56607.c
+++ gcc/testsuite/c-c++-common/pr56607.c
@@ -12,7 +12,7 @@  int
 f2 (void)
 {
   const int x = sizeof (char) - 1;
-  return 1 / x;				/* { dg-warning "division by zero" "" { target c++ } } */
+  return 1 / x;				/* { dg-warning "division by zero" } */
 }
 
 int
@@ -25,5 +25,5 @@  int
 f4 (void)
 {
   const int x = sizeof (int) / 3 - 1;
-  return 1 / x;				/* { dg-warning "division by zero" "" { target c++ } } */
+  return 1 / x;				/* { dg-warning "division by zero" } */
 }
diff --git gcc/testsuite/gcc.dg/pr64440.c gcc/testsuite/gcc.dg/pr64440.c
index e69de29..f9cc46d 100644
--- gcc/testsuite/gcc.dg/pr64440.c
+++ gcc/testsuite/gcc.dg/pr64440.c
@@ -0,0 +1,15 @@ 
+/* PR c/64440 */
+/* { dg-do compile } */
+/* { dg-options "-Wall -O2" } */
+
+int
+foo (int x)
+{
+  const int y = 0;
+  int r = 0;
+  r += x / y; /* { dg-warning "division by zero" } */
+  r += x / 0; /* { dg-warning "division by zero" } */
+  r += x % y; /* { dg-warning "division by zero" } */
+  r += x % 0; /* { dg-warning "division by zero" } */
+  return r;
+}