public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* C/C++ PATCH to fix latest -Wbool-compare extension
@ 2015-04-30 13:37 Marek Polacek
  2015-04-30 16:02 ` Jeff Law
  0 siblings, 1 reply; 3+ messages in thread
From: Marek Polacek @ 2015-04-30 13:37 UTC (permalink / raw)
  To: GCC Patches

The problem here was that the -Wbool-compare warning about always false/true
comparisons with 0/1 was assuming that both operands are of a boolean type.
That was wrong so check for that, but don't get confused about bools promoted
to int.

This bug is blocking aarch64 bootstrap, so I'm taking the liberty of committing
it right away.

Bootstrapped/regtested on x86_64-linux, applying to trunk.

2015-04-30  Marek Polacek  <polacek@redhat.com>

	* c-common.c (maybe_warn_bool_compare): When comparing with 0/1,
	require that the non-constant be of a boolean type.

	* c-c++-common/Wbool-compare-3.c: New test.

diff --git gcc/c-family/c-common.c gcc/c-family/c-common.c
index 7d314f8..ada8e8a 100644
--- gcc/c-family/c-common.c
+++ gcc/c-family/c-common.c
@@ -11924,6 +11924,17 @@ maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
     }
   else if (integer_zerop (cst) || integer_onep (cst))
     {
+      /* If the non-constant operand isn't of a boolean type, we
+	 don't want to warn here.  */
+      tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
+      /* Handle booleans promoted to integers.  */
+      if (CONVERT_EXPR_P (noncst)
+	  && TREE_TYPE (noncst) == integer_type_node
+	  && TREE_CODE (TREE_TYPE (TREE_OPERAND (noncst, 0))) == BOOLEAN_TYPE)
+	/* Warn.  */;
+      else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
+	       && !truth_value_p (TREE_CODE (noncst)))
+	return;
       /* Do some magic to get the right diagnostics.  */
       bool flag = TREE_CODE (op0) == INTEGER_CST;
       flag = integer_zerop (cst) ? flag : !flag;
diff --git gcc/testsuite/c-c++-common/Wbool-compare-3.c gcc/testsuite/c-c++-common/Wbool-compare-3.c
index e69de29..bac4f47 100644
--- gcc/testsuite/c-c++-common/Wbool-compare-3.c
+++ gcc/testsuite/c-c++-common/Wbool-compare-3.c
@@ -0,0 +1,30 @@
+/* { dg-do compile } */
+/* { dg-options "-Wbool-compare" } */
+
+#ifndef __cplusplus
+# define bool _Bool
+#endif
+
+#define A 0
+#define B 1
+
+int
+foo (int i, bool b)
+{
+  int r = 0;
+
+  r += i <= (A || B);
+  r += i <= b;
+  r += i <= A;
+  r += i < (A || B);
+  r += i < b;
+  r += i < A;
+  r += i > (A || B);
+  r += i > b;
+  r += i > A;
+  r += i >= (A || B);
+  r += i >= b;
+  r += i >= A;
+
+  return r;
+}

	Marek

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: C/C++ PATCH to fix latest -Wbool-compare extension
  2015-04-30 13:37 C/C++ PATCH to fix latest -Wbool-compare extension Marek Polacek
@ 2015-04-30 16:02 ` Jeff Law
  2015-04-30 16:27   ` Marek Polacek
  0 siblings, 1 reply; 3+ messages in thread
From: Jeff Law @ 2015-04-30 16:02 UTC (permalink / raw)
  To: Marek Polacek, GCC Patches

On 04/30/2015 07:17 AM, Marek Polacek wrote:
> The problem here was that the -Wbool-compare warning about always false/true
> comparisons with 0/1 was assuming that both operands are of a boolean type.
> That was wrong so check for that, but don't get confused about bools promoted
> to int.
>
> This bug is blocking aarch64 bootstrap, so I'm taking the liberty of committing
> it right away.
>
> Bootstrapped/regtested on x86_64-linux, applying to trunk.
>
> 2015-04-30  Marek Polacek  <polacek@redhat.com>
>
> 	* c-common.c (maybe_warn_bool_compare): When comparing with 0/1,
> 	require that the non-constant be of a boolean type.
>
> 	* c-c++-common/Wbool-compare-3.c: New test.
OK.

BTW, you may also want to consider warning for integer types with a 
precision of 1 bit.

Jeff

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: C/C++ PATCH to fix latest -Wbool-compare extension
  2015-04-30 16:02 ` Jeff Law
@ 2015-04-30 16:27   ` Marek Polacek
  0 siblings, 0 replies; 3+ messages in thread
From: Marek Polacek @ 2015-04-30 16:27 UTC (permalink / raw)
  To: Jeff Law; +Cc: GCC Patches

On Thu, Apr 30, 2015 at 09:55:02AM -0600, Jeff Law wrote:
> OK.
 
Thanks.

> BTW, you may also want to consider warning for integer types with a
> precision of 1 bit.

Yup, this is being tracked in
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=49706#c2
I suppose I'll get back to that in this stage1.

	Marek

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-04-30 16:04 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-04-30 13:37 C/C++ PATCH to fix latest -Wbool-compare extension Marek Polacek
2015-04-30 16:02 ` Jeff Law
2015-04-30 16:27   ` Marek Polacek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).