From 700fff34d219e33b11fa63ac95bc524180781803 Mon Sep 17 00:00:00 2001 From: Bernd Edlinger Date: Wed, 5 Oct 2016 18:34:17 +0000 Subject: [PATCH] c-common.c (c_common_truthvalue_conversion): Warn also for suspicious conditional expression in boolean context when... 2016-10-05 Bernd Edlinger * c-common.c (c_common_truthvalue_conversion): Warn also for suspicious conditional expression in boolean context when only one arm is non-boolean. testsuite: 2016-10-05 Bernd Edlinger * c-c++-common/Wint-in-bool-context.c: Update test. From-SVN: r240799 --- gcc/c-family/ChangeLog | 6 ++++++ gcc/c-family/c-common.c | 8 ++++++++ gcc/testsuite/ChangeLog | 4 ++++ gcc/testsuite/c-c++-common/Wint-in-bool-context.c | 2 +- 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index d3c7cd5c2c0..2bfce86c443 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,9 @@ +2016-10-05 Bernd Edlinger + + * c-common.c (c_common_truthvalue_conversion): Warn also for suspicious + conditional expression in boolean context when only one arm is + non-boolean. + 2016-10-05 Jakub Jelinek PR sanitizer/77823 diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index dbef1999b55..035afbceeb8 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -4675,6 +4675,14 @@ c_common_truthvalue_conversion (location_t location, tree expr) warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context, "?: using integer constants in boolean context, " "the expression will always evaluate to %"); + else if ((TREE_CODE (val1) == INTEGER_CST + && !integer_zerop (val1) + && !integer_onep (val1)) + || (TREE_CODE (val2) == INTEGER_CST + && !integer_zerop (val2) + && !integer_onep (val2))) + warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context, + "?: using integer constants in boolean context"); } /* Distribute the conversion into the arms of a COND_EXPR. */ if (c_dialect_cxx ()) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 6c587f69d25..fa8cce764c6 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2016-10-05 Bernd Edlinger + + * c-c++-common/Wint-in-bool-context.c: Update test. + 2016-10-05 Marek Polacek * g++.dg/cpp1z/init-statement1.C: New test. diff --git a/gcc/testsuite/c-c++-common/Wint-in-bool-context.c b/gcc/testsuite/c-c++-common/Wint-in-bool-context.c index 832eb0a0ee3..36daf546834 100644 --- a/gcc/testsuite/c-c++-common/Wint-in-bool-context.c +++ b/gcc/testsuite/c-c++-common/Wint-in-bool-context.c @@ -10,7 +10,7 @@ int foo (int a, int b) if (a > 0 && a <= (b == 2) ? 1 : 1) /* { dg-bogus "boolean context" } */ return 2; - if (a > 0 && a <= (b == 3) ? 0 : 2) /* { dg-bogus "boolean context" } */ + if (a > 0 && a <= (b == 3) ? 0 : 2) /* { dg-warning "boolean context" } */ return 3; if (a == b ? 0 : 0) /* { dg-bogus "boolean context" } */ -- 2.30.2