Hi!
This is a next step in extending the -Wint-in-bool-context warning
to cover the case when a conditional expression has only
one arm which evaluates to a non-boolean integer value.
With a previous version of this warning, we found PR 77574,
among with several more or less false positives, but meanwhile,
mostly due to excluding conditional expressions that originate
from macro expansion, there are no false positives any more,
so I think this is fine now with -Wall.
Bootstrapped and reg-tested on x86_64-pc-linux-gnu.
Is it OK for trunk?
Thanks
Bernd.
c-family:
2016-10-03 Bernd Edlinger <bernd.edlin...@hotmail.de>
* 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-03 Bernd Edlinger <bernd.edlin...@hotmail.de>
* c-c++-common/Wint-in-bool-context.c: Update test.
Index: gcc/c-family/c-common.c
===================================================================
--- gcc/c-family/c-common.c (revision 240713)
+++ gcc/c-family/c-common.c (working copy)
@@ -4675,6 +4675,14 @@ c_common_truthvalue_conversion (location_t locatio
warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
"?: using integer constants in boolean context, "
"the expression will always evaluate to %<true%>");
+ 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 ())
Index: gcc/testsuite/c-c++-common/Wint-in-bool-context.c
===================================================================
--- gcc/testsuite/c-c++-common/Wint-in-bool-context.c (revision 240713)
+++ gcc/testsuite/c-c++-common/Wint-in-bool-context.c (working copy)
@@ -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" } */