From 4a9a42abc4f30bd63ef666e972dcbce0f05f0d0d Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Wed, 7 Jan 2015 08:21:50 +0000 Subject: [PATCH] re PR c/64440 (-Wdiv-by-zero false negative on const variables) 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 | 6 ++++++ gcc/c-family/c-common.c | 11 +++++++++++ gcc/testsuite/ChangeLog | 6 ++++++ gcc/testsuite/c-c++-common/pr56607.c | 4 ++-- gcc/testsuite/gcc.dg/pr64440.c | 15 +++++++++++++++ 5 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/pr64440.c diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index 4c86fa511d3..4bcff011315 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,9 @@ +2015-01-07 Marek Polacek + + 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 PR c++/31397 diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index 4d9dd4defb1..df4fddd1926 100644 --- a/gcc/c-family/c-common.c +++ b/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 a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 214cfd682c0..259d87f30eb 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2015-01-07 Marek Polacek + + 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 PR c/64417 diff --git a/gcc/testsuite/c-c++-common/pr56607.c b/gcc/testsuite/c-c++-common/pr56607.c index d7faa81151a..ba1395612da 100644 --- a/gcc/testsuite/c-c++-common/pr56607.c +++ b/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 a/gcc/testsuite/gcc.dg/pr64440.c b/gcc/testsuite/gcc.dg/pr64440.c new file mode 100644 index 00000000000..f9cc46d0a5c --- /dev/null +++ b/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; +} -- 2.30.2