From 4e34b338f703d72e1d4a90116e9d2c6aa0a3d5d8 Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Fri, 6 Oct 2017 09:11:25 +0200 Subject: [PATCH] re PR c/82437 (false-positive -Wtautological-compare warning with -std=gnu89) PR c/82437 * c-warn.c (warn_tautological_bitwise_comparison): Instead of using to_widest use wide_int with the larger of the two precisions. * c-c++-common/Wtautological-compare-6.c: New test. From-SVN: r253476 --- gcc/c-family/ChangeLog | 6 ++++++ gcc/c-family/c-warn.c | 18 +++++++++++++----- gcc/testsuite/ChangeLog | 5 +++++ .../c-c++-common/Wtautological-compare-6.c | 11 +++++++++++ 4 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/Wtautological-compare-6.c diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index 639fd2254cc..f70b6f83832 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,9 @@ +2017-10-06 Jakub Jelinek + + PR c/82437 + * c-warn.c (warn_tautological_bitwise_comparison): Instead of + using to_widest use wide_int with the larger of the two precisions. + 2017-10-05 Bernd Edlinger * c-pretty-print.c (pp_c_parameter_type_list): Print ... for variadic diff --git a/gcc/c-family/c-warn.c b/gcc/c-family/c-warn.c index f86de10fdd9..2eb4cf5dd41 100644 --- a/gcc/c-family/c-warn.c +++ b/gcc/c-family/c-warn.c @@ -356,16 +356,24 @@ warn_tautological_bitwise_comparison (location_t loc, tree_code code, return; /* Note that the two operands are from before the usual integer - conversions, so their types might not be the same. */ - widest_int res; + conversions, so their types might not be the same. + Use the larger of the two precisions and ignore bits outside + of that. */ + int prec = MAX (TYPE_PRECISION (TREE_TYPE (cst)), + TYPE_PRECISION (TREE_TYPE (bitopcst))); + + wide_int bitopcstw = wide_int::from (bitopcst, prec, UNSIGNED); + wide_int cstw = wide_int::from (cst, prec, UNSIGNED); + + wide_int res; if (TREE_CODE (bitop) == BIT_AND_EXPR) - res = wi::to_widest (bitopcst) & wi::to_widest (cst); + res = bitopcstw & cstw; else - res = wi::to_widest (bitopcst) | wi::to_widest (cst); + res = bitopcstw | cstw; /* For BIT_AND only warn if (CST2 & CST1) != CST1, and for BIT_OR only if (CST2 | CST1) != CST1. */ - if (res == wi::to_widest (cst)) + if (res == cstw) return; if (code == EQ_EXPR) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index f01325a3f0a..1021c6ab563 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2017-10-06 Jakub Jelinek + + PR c/82437 + * c-c++-common/Wtautological-compare-6.c: New test. + 2017-10-06 Richard Biener * gcc.dg/graphite/id-15.c: No longer expect a code generation error. diff --git a/gcc/testsuite/c-c++-common/Wtautological-compare-6.c b/gcc/testsuite/c-c++-common/Wtautological-compare-6.c new file mode 100644 index 00000000000..fcdca72e3f3 --- /dev/null +++ b/gcc/testsuite/c-c++-common/Wtautological-compare-6.c @@ -0,0 +1,11 @@ +/* PR c/82437 */ +/* { dg-do compile { target int32plus } } */ +/* { dg-options "-Wtautological-compare" } */ + +int +foo (unsigned int x) +{ + if ((x & -1879048192) != -1879048192) /* { dg-bogus "bitwise comparison always evaluates to" } */ + return 0; + return 1; +} -- 2.30.2