]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR c/64440 (-Wdiv-by-zero false negative on const variables)
authorMarek Polacek <polacek@redhat.com>
Wed, 7 Jan 2015 08:21:50 +0000 (08:21 +0000)
committerMarek Polacek <mpolacek@gcc.gnu.org>
Wed, 7 Jan 2015 08:21:50 +0000 (08:21 +0000)
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++.

From-SVN: r219279

gcc/c-family/ChangeLog
gcc/c-family/c-common.c
gcc/testsuite/ChangeLog
gcc/testsuite/c-c++-common/pr56607.c
gcc/testsuite/gcc.dg/pr64440.c [new file with mode: 0644]

index 4c86fa511d35800548c6f9cbe2f3807d958548d4..4bcff011315109cb6c25652b36589ffe9f41861a 100644 (file)
@@ -1,3 +1,9 @@
+2015-01-07  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.
+
 2015-01-05  Trevor Saunders  <tsaunders@mozilla.com>
 
        PR c++/31397
index 4d9dd4defb103b3ce1d31742f419a3dbcc733c8d..df4fddd19265e27d1c52e4c060cb00f36a176a0b 100644 (file)
@@ -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:
index 214cfd682c0652a680bd4c1f498fafe690f9686b..259d87f30eb59d2ee57246adf224861500c4cd13 100644 (file)
@@ -1,3 +1,9 @@
+2015-01-07  Marek Polacek  <polacek@redhat.com>
+
+       PR c/64440
+       * gcc.dg/pr64440.c: New test.
+       * c-c++-common/pr56607.c: Don't limit dg-warnings to C++.
+
 2015-01-07  Marek Polacek  <polacek@redhat.com>
 
        PR c/64417
index d7faa81151a261024b06f3b9156ac60cd5da9f8c..ba1395612da3a08ddc220373beddf7e5708c0494 100644 (file)
@@ -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 a/gcc/testsuite/gcc.dg/pr64440.c b/gcc/testsuite/gcc.dg/pr64440.c
new file mode 100644 (file)
index 0000000..f9cc46d
--- /dev/null
@@ -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;
+}