diff mbox

Extend -Wint-in-bool-context to more conditional expressions

Message ID AM4PR0701MB216293247895872E4B231064E4C20@AM4PR0701MB2162.eurprd07.prod.outlook.com
State New
Headers show

Commit Message

Bernd Edlinger Oct. 3, 2016, 5:09 p.m. UTC
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.

Comments

Jeff Law Oct. 5, 2016, 5:54 p.m. UTC | #1
On 10/03/2016 11:09 AM, Bernd Edlinger wrote:
> 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.
>
>
> patch-bool-context2.diff
>
>
> c-family:
> 2016-10-03  Bernd Edlinger  <bernd.edlinger@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.edlinger@hotmail.de>
>
> 	* c-c++-common/Wint-in-bool-context.c: Update test.
OK.
jeff
diff mbox

Patch

c-family:
2016-10-03  Bernd Edlinger  <bernd.edlinger@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.edlinger@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" } */