From 4c712374189739d77d7eaa7a3977ce8533e15163 Mon Sep 17 00:00:00 2001 From: Bernd Edlinger Date: Wed, 19 Oct 2016 21:00:39 +0000 Subject: [PATCH] c-common.c (c_common_truthvalue_conversion): Warn only for signed integer shift ops in boolean context. 2016-10-19 Bernd Edlinger * c-common.c (c_common_truthvalue_conversion): Warn only for signed integer shift ops in boolean context. testsuite: 2016-10-19 Bernd Edlinger * c-c++-common/Wint-in-bool-context-2.c: New test. From-SVN: r241354 --- gcc/c-family/ChangeLog | 7 ++++++- gcc/c-family/c-common.c | 11 +++++++++-- gcc/testsuite/ChangeLog | 4 ++++ .../c-c++-common/Wint-in-bool-context-2.c | 17 +++++++++++++++++ 4 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/Wint-in-bool-context-2.c diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index f3055930065..0ef8ef7aad9 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,4 +1,9 @@ -2016-06-16 Aldy Hernandez +2016-10-19 Bernd Edlinger + + * c-common.c (c_common_truthvalue_conversion): Warn only for signed + integer shift ops in boolean context. + +2016-10-18 Aldy Hernandez * c.opt (Walloca): New. (Walloca-larger-than=): New. diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index 326ff6f49e2..8af3ad53a3d 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -3328,8 +3328,15 @@ c_common_truthvalue_conversion (location_t location, tree expr) TREE_OPERAND (expr, 0)); case LSHIFT_EXPR: - warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context, - "<< in boolean context, did you mean '<' ?"); + /* We will only warn on unsigned shifts here, because the majority of + false positive warnings happen in code where unsigned arithmetic + was used in anticipation of a possible overflow. + Furthermore, if we see an unsigned type here we know that the + result of the shift is not subject to integer promotion rules. */ + if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE + && !TYPE_UNSIGNED (TREE_TYPE (expr))) + warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context, + "<< in boolean context, did you mean '<' ?"); break; case COND_EXPR: diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 1e833253761..8f5230f515a 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2016-10-19 Bernd Edlinger + + * c-c++-common/Wint-in-bool-context-2.c: New test. + 2016-10-19 Jerry DeLisle * gfortran.dg/dtio_17.f90: Fix test. diff --git a/gcc/testsuite/c-c++-common/Wint-in-bool-context-2.c b/gcc/testsuite/c-c++-common/Wint-in-bool-context-2.c new file mode 100644 index 00000000000..04c064d778f --- /dev/null +++ b/gcc/testsuite/c-c++-common/Wint-in-bool-context-2.c @@ -0,0 +1,17 @@ +/* { dg-options "-Wint-in-bool-context" } */ +/* { dg-do compile } */ + +typedef unsigned u32; +typedef unsigned char u8; +#define KEYLENGTH 8 + +int foo (u8 plen, u32 key) +{ + if ((plen < KEYLENGTH) && (key << plen)) /* { dg-bogus "boolean context" } */ + return -1; + + if ((plen << KEYLENGTH) && (key < plen)) /* { dg-warning "boolean context" } */ + return -2; + + return 0; +} -- 2.30.2