public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* c-family PATCH to improve -Wbool-compare (PR c/64610)
@ 2015-04-28 12:06 Marek Polacek
  2015-04-28 17:16 ` Jeff Law
  2015-04-30 10:00 ` Andreas Schwab
  0 siblings, 2 replies; 5+ messages in thread
From: Marek Polacek @ 2015-04-28 12:06 UTC (permalink / raw)
  To: GCC Patches

This improves -Wbool-compare a bit: boolean >= 0 is always true, on the other
hand boolean < 0 is always false, and similar.

One feature/bug is that in C++ we don't warn for e.g. bool >= false, since the
warning cares about <bool> CMP <non-bool> or <non-bool> CMP <bool>.  I can fix
that in a follow-up if anyone wants me to.

Bootstrapped (-> GCC itself is clean)/regtested on x86_64-linux, ok for trunk?

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

	PR c/64610
	* c-common.c (maybe_warn_bool_compare): Warn when comparing a boolean
	with 0/1.

	* c-c++-common/Wbool-compare-1.c (fn1): Remove a few lines.
	* c-c++-common/Wbool-compare-2.c: New test.

diff --git gcc/c-family/c-common.c gcc/c-family/c-common.c
index 9797e17..afd6edf2 100644
--- gcc/c-family/c-common.c
+++ gcc/c-family/c-common.c
@@ -11906,8 +11906,8 @@ maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
 
   if (!integer_zerop (cst) && !integer_onep (cst))
     {
-      int sign = (TREE_CODE (op0) == INTEGER_CST)
-		 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst);
+      int sign = (TREE_CODE (op0) == INTEGER_CST
+		 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
       if (code == EQ_EXPR
 	  || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
 	  || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
@@ -11917,6 +11917,18 @@ maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
 	warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
 		    "with boolean expression is always true", cst);
     }
+  else if (integer_zerop (cst) || integer_onep (cst))
+    {
+      /* Do some magic to get the right diagnostics.  */
+      bool flag = TREE_CODE (op0) == INTEGER_CST;
+      flag = integer_zerop (cst) ? flag : !flag;
+      if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
+	warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
+		    "with boolean expression is always true", cst);
+      else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
+	warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
+		    "with boolean expression is always false", cst);
+    }
 }
 
 /* The C and C++ parsers both use vectors to hold function arguments.
diff --git gcc/testsuite/c-c++-common/Wbool-compare-1.c gcc/testsuite/c-c++-common/Wbool-compare-1.c
index 5b03e06..2f435f6 100644
--- gcc/testsuite/c-c++-common/Wbool-compare-1.c
+++ gcc/testsuite/c-c++-common/Wbool-compare-1.c
@@ -72,15 +72,10 @@ fn1 (bool b)
   r = b == true;
   r = b != true;
 
-  /* Some of these don't make much sense, but we don't warn.  */
-  r = b < false;
-  r = b >= false;
   r = b <= false;
   r = b > false;
   r = b < true;
   r = b >= true;
-  r = b <= true;
-  r = b > true;
 }
 
 void
diff --git gcc/testsuite/c-c++-common/Wbool-compare-2.c gcc/testsuite/c-c++-common/Wbool-compare-2.c
index e69de29..6330322 100644
--- gcc/testsuite/c-c++-common/Wbool-compare-2.c
+++ gcc/testsuite/c-c++-common/Wbool-compare-2.c
@@ -0,0 +1,100 @@
+/* PR c/64610 */
+/* { dg-do compile } */
+/* { dg-options "-Wbool-compare" } */
+
+#ifndef __cplusplus
+# define bool _Bool
+# define true 1
+# define false 0
+#endif
+
+extern bool foo (void);
+
+enum { A, B };
+
+int
+fn1 (bool b)
+{
+  int r = 0;
+
+  r += b >= 0; /* { dg-warning "with boolean expression is always true" } */
+  r += b > 0;
+  r += b < 0; /* { dg-warning "with boolean expression is always false" } */
+  r += b <= 0;
+
+  r += b >= 1;
+  r += b > 1; /* { dg-warning "with boolean expression is always false" } */
+  r += b < 1;
+  r += b <= 1; /* { dg-warning "with boolean expression is always true" } */
+
+  r += foo () >= 0; /* { dg-warning "with boolean expression is always true" } */
+  r += foo () > 0;
+  r += foo () < 0; /* { dg-warning "with boolean expression is always false" } */
+  r += foo () <= 0;
+
+  r += foo () >= 1;
+  r += foo () > 1; /* { dg-warning "with boolean expression is always false" } */
+  r += foo () < 1;
+  r += foo () <= 1; /* { dg-warning "with boolean expression is always true" } */
+
+  r += b >= A; /* { dg-warning "with boolean expression is always true" } */
+  r += b > A;
+  r += b < A; /* { dg-warning "with boolean expression is always false" } */
+  r += b <= A;
+
+  r += b >= B;
+  r += b > B; /* { dg-warning "with boolean expression is always false" } */
+  r += b < B;
+  r += b <= B; /* { dg-warning "with boolean expression is always true" } */
+
+  /* Swap LHS and RHS.  */
+  r += 0 >= b;
+  r += 0 > b; /* { dg-warning "with boolean expression is always false" } */
+  r += 0 < b;
+  r += 0 <= b; /* { dg-warning "with boolean expression is always true" } */
+
+  r += 1 >= b; /* { dg-warning "with boolean expression is always true" } */
+  r += 1 > b;
+  r += 1 < b; /* { dg-warning "with boolean expression is always false" } */
+  r += 1 <= b;
+
+  r += 0 >= foo ();
+  r += 0 > foo (); /* { dg-warning "with boolean expression is always false" } */
+  r += 0 < foo ();
+  r += 0 <= foo (); /* { dg-warning "with boolean expression is always true" } */
+
+  r += 1 >= foo (); /* { dg-warning "with boolean expression is always true" } */
+  r += 1 > foo ();
+  r += 1 < foo (); /* { dg-warning "with boolean expression is always false" } */
+  r += 1 <= foo ();
+
+  r += A >= b;
+  r += A > b; /* { dg-warning "with boolean expression is always false" } */
+  r += A < b;
+  r += A <= b; /* { dg-warning "with boolean expression is always true" } */
+
+  r += B >= b; /* { dg-warning "with boolean expression is always true" } */
+  r += B > b;
+  r += B < b; /* { dg-warning "with boolean expression is always false" } */
+  r += B <= b;
+
+  return r;
+}
+
+int
+fn2 (int i, int j)
+{
+  int r = 0;
+
+  r += (i == j) >= 0; /* { dg-warning "with boolean expression is always true" } */
+  r += (i == j) > 0;
+  r += (i == j) < 0; /* { dg-warning "with boolean expression is always false" } */
+  r += (i == j) <= 0;
+
+  r += (i == j) >= 1;
+  r += (i == j) > 1; /* { dg-warning "with boolean expression is always false" } */
+  r += (i == j) < 1;
+  r += (i == j) <= 1; /* { dg-warning "with boolean expression is always true" } */
+
+  return r;
+}

	Marek

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

* Re: c-family PATCH to improve -Wbool-compare (PR c/64610)
  2015-04-28 12:06 c-family PATCH to improve -Wbool-compare (PR c/64610) Marek Polacek
@ 2015-04-28 17:16 ` Jeff Law
  2015-04-30 10:00 ` Andreas Schwab
  1 sibling, 0 replies; 5+ messages in thread
From: Jeff Law @ 2015-04-28 17:16 UTC (permalink / raw)
  To: Marek Polacek, GCC Patches

On 04/28/2015 04:49 AM, Marek Polacek wrote:
> This improves -Wbool-compare a bit: boolean >= 0 is always true, on the other
> hand boolean < 0 is always false, and similar.
>
> One feature/bug is that in C++ we don't warn for e.g. bool >= false, since the
> warning cares about <bool> CMP <non-bool> or <non-bool> CMP <bool>.  I can fix
> that in a follow-up if anyone wants me to.
>
> Bootstrapped (-> GCC itself is clean)/regtested on x86_64-linux, ok for trunk?
>
> 2015-04-28  Marek Polacek  <polacek@redhat.com>
>
> 	PR c/64610
> 	* c-common.c (maybe_warn_bool_compare): Warn when comparing a boolean
> 	with 0/1.
>
> 	* c-c++-common/Wbool-compare-1.c (fn1): Remove a few lines.
> 	* c-c++-common/Wbool-compare-2.c: New test.
OK.
jeff

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

* Re: c-family PATCH to improve -Wbool-compare (PR c/64610)
  2015-04-28 12:06 c-family PATCH to improve -Wbool-compare (PR c/64610) Marek Polacek
  2015-04-28 17:16 ` Jeff Law
@ 2015-04-30 10:00 ` Andreas Schwab
  2015-04-30 11:34   ` Marek Polacek
  1 sibling, 1 reply; 5+ messages in thread
From: Andreas Schwab @ 2015-04-30 10:00 UTC (permalink / raw)
  To: Marek Polacek; +Cc: GCC Patches

Marek Polacek <polacek@redhat.com> writes:

> 	PR c/64610
> 	* c-common.c (maybe_warn_bool_compare): Warn when comparing a boolean
> 	with 0/1.

/usr/local/gcc/gcc-20150430/Build/./prev-gcc/xg++ -B/usr/local/gcc/gcc-20150430/Build/./prev-gcc/ -B/usr/aarch64-suse-linux/bin/ -nostdinc++ -B/usr/local/gcc/gcc-20150430/Build/prev-aarch64-suse-linux/libstdc++-v3/src/.libs -B/usr/local/gcc/gcc-20150430/Build/prev-aarch64-suse-linux/libstdc++-v3/libsupc++/.libs  -I/usr/local/gcc/gcc-20150430/Build/prev-aarch64-suse-linux/libstdc++-v3/include/aarch64-suse-linux  -I/usr/local/gcc/gcc-20150430/Build/prev-aarch64-suse-linux/libstdc++-v3/include  -I/usr/local/gcc/gcc-20150430/libstdc++-v3/libsupc++ -L/usr/local/gcc/gcc-20150430/Build/prev-aarch64-suse-linux/libstdc++-v3/src/.libs -L/usr/local/gcc/gcc-20150430/Build/prev-aarch64-suse-linux/libstdc++-v3/libsupc++/.libs -c   -g -O2 -gtoggle -DIN_GCC    -fno-exceptions -fno-rtti -fasynchronous-unwind-tables -W -Wall -Wno-narrowing -Wwrite-strings -Wcast-qual -Wmissing-format-attribute -Woverloaded-virtual -pedantic -Wno-long-long -Wno-variadic-macros -Wno-overlength-strings -Werror -fno-common  -DHAVE_CONFIG_H -I. -I. -I../../gcc -I../../gcc/. -I../../gcc/../include -I../../gcc/../libcpp/include  -I../../gcc/../libdecnumber -I../../gcc/../libdecnumber/dpd -I../libdecnumber -I../../gcc/../libbacktrace   -o expr.o -MT expr.o -MMD -MP -MF ./.deps/expr.TPo ../../gcc/expr.c
../../gcc/expr.c: In function 'int can_store_by_pieces(long unsigned int, rtx_def* (*)(void*, long int, machine_mode), void*, unsigned int, bool)':
../../gcc/expr.c:2496:16: error: comparison of constant 'true' with boolean expression is always true [-Werror=bool-compare]
        reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
                ^

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

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

* Re: c-family PATCH to improve -Wbool-compare (PR c/64610)
  2015-04-30 10:00 ` Andreas Schwab
@ 2015-04-30 11:34   ` Marek Polacek
  2015-04-30 14:21     ` Andreas Schwab
  0 siblings, 1 reply; 5+ messages in thread
From: Marek Polacek @ 2015-04-30 11:34 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: GCC Patches

On Thu, Apr 30, 2015 at 11:42:18AM +0200, Andreas Schwab wrote:
> Marek Polacek <polacek@redhat.com> writes:
> 
> > 	PR c/64610
> > 	* c-common.c (maybe_warn_bool_compare): Warn when comparing a boolean
> > 	with 0/1.
> 
> /usr/local/gcc/gcc-20150430/Build/./prev-gcc/xg++ -B/usr/local/gcc/gcc-20150430/Build/./prev-gcc/ -B/usr/aarch64-suse-linux/bin/ -nostdinc++ -B/usr/local/gcc/gcc-20150430/Build/prev-aarch64-suse-linux/libstdc++-v3/src/.libs -B/usr/local/gcc/gcc-20150430/Build/prev-aarch64-suse-linux/libstdc++-v3/libsupc++/.libs  -I/usr/local/gcc/gcc-20150430/Build/prev-aarch64-suse-linux/libstdc++-v3/include/aarch64-suse-linux  -I/usr/local/gcc/gcc-20150430/Build/prev-aarch64-suse-linux/libstdc++-v3/include  -I/usr/local/gcc/gcc-20150430/libstdc++-v3/libsupc++ -L/usr/local/gcc/gcc-20150430/Build/prev-aarch64-suse-linux/libstdc++-v3/src/.libs -L/usr/local/gcc/gcc-20150430/Build/prev-aarch64-suse-linux/libstdc++-v3/libsupc++/.libs -c   -g -O2 -gtoggle -DIN_GCC    -fno-exceptions -fno-rtti -fasynchronous-unwind-tables -W -Wall -Wno-narrowing -Wwrite-strings -Wcast-qual -Wmissing-format-attribute -Woverloaded-virtual -pedantic -Wno-long-long -Wno-variadic-macros -Wno-overlength-strings -Werror -fno-common  -DHAVE_CONFIG_H -I. -I. -I../../gcc -I../../gcc/. -I../../gcc/../include -I../../gcc/../libcpp/include  -I../../gcc/../libdecnumber -I../../gcc/../libdecnumber/dpd -I../libdecnumber -I../../gcc/../libbacktrace   -o expr.o -MT expr.o -MMD -MP -MF ./.deps/expr.TPo ../../gcc/expr.c
> ../../gcc/expr.c: In function 'int can_store_by_pieces(long unsigned int, rtx_def* (*)(void*, long int, machine_mode), void*, unsigned int, bool)':
> ../../gcc/expr.c:2496:16: error: comparison of constant 'true' with boolean expression is always true [-Werror=bool-compare]
>         reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
>                 ^

Yes, that is a bug in my code; I think I'll apply the following after
regtest/bootstrap (with a proper test).  If you could perhaps try the
patch as well, it'd be appreciated.

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;

	Marek

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

* Re: c-family PATCH to improve -Wbool-compare (PR c/64610)
  2015-04-30 11:34   ` Marek Polacek
@ 2015-04-30 14:21     ` Andreas Schwab
  0 siblings, 0 replies; 5+ messages in thread
From: Andreas Schwab @ 2015-04-30 14:21 UTC (permalink / raw)
  To: Marek Polacek; +Cc: GCC Patches

Marek Polacek <polacek@redhat.com> writes:

> 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;

Looks good, successfully built stage3.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

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

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

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-04-28 12:06 c-family PATCH to improve -Wbool-compare (PR c/64610) Marek Polacek
2015-04-28 17:16 ` Jeff Law
2015-04-30 10:00 ` Andreas Schwab
2015-04-30 11:34   ` Marek Polacek
2015-04-30 14:21     ` Andreas Schwab

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).