summaryrefslogtreecommitdiff
path: root/gcc/testsuite/c-c++-common
diff options
context:
space:
mode:
authormpolacek <mpolacek@138bc75d-0d04-0410-961f-82ee72b054a4>2014-08-19 18:50:00 +0000
committermpolacek <mpolacek@138bc75d-0d04-0410-961f-82ee72b054a4>2014-08-19 18:50:00 +0000
commit78bf41568c66b61419865b36f101121c8f5918a5 (patch)
treec01c9d77dc28f6a4d017760a2f0f57809574d010 /gcc/testsuite/c-c++-common
parentdec0d4e18a3d88a552b6b01c1900053519aed20a (diff)
downloadgcc-78bf41568c66b61419865b36f101121c8f5918a5.tar.gz
PR c++/62153
* doc/invoke.texi: Document -Wbool-compare. c-family/ * c-common.c (maybe_warn_bool_compare): New function. * c-common.h (maybe_warn_bool_compare): Declare. * c.opt (Wbool-compare): New option. c/ * c-typeck.c (build_binary_op): If either operand of a comparison is a boolean expression, call maybe_warn_bool_compare. cp/ * call.c (build_new_op_1): Remember the type of arguments for a comparison. If either operand of a comparison is a boolean expression, call maybe_warn_bool_compare. testsuite/ * c-c++-common/Wbool-compare-1.c: New test. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@214183 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'gcc/testsuite/c-c++-common')
-rw-r--r--gcc/testsuite/c-c++-common/Wbool-compare-1.c128
1 files changed, 128 insertions, 0 deletions
diff --git a/gcc/testsuite/c-c++-common/Wbool-compare-1.c b/gcc/testsuite/c-c++-common/Wbool-compare-1.c
new file mode 100644
index 00000000000..5b03e068210
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/Wbool-compare-1.c
@@ -0,0 +1,128 @@
+/* PR c++/62153 */
+/* { dg-do compile } */
+/* { dg-options "-Wall" } */
+
+#ifndef __cplusplus
+# define bool _Bool
+# define true 1
+# define false 0
+#endif
+
+extern bool foo (void);
+bool r;
+
+enum { E = 4 };
+
+void
+fn1 (bool b)
+{
+ r = b == 2; /* { dg-warning "with boolean expression is always false" } */
+ r = b != 2; /* { dg-warning "with boolean expression is always true" } */
+ r = b < 2; /* { dg-warning "with boolean expression is always true" } */
+ r = b > 2; /* { dg-warning "with boolean expression is always false" } */
+ r = b <= 2; /* { dg-warning "with boolean expression is always true" } */
+ r = b >= 2; /* { dg-warning "with boolean expression is always false" } */
+
+ r = b == -1; /* { dg-warning "with boolean expression is always false" } */
+ r = b != -1; /* { dg-warning "with boolean expression is always true" } */
+ r = b < -1; /* { dg-warning "with boolean expression is always false" } */
+ r = b > -1; /* { dg-warning "with boolean expression is always true" } */
+ r = b <= -1; /* { dg-warning "with boolean expression is always false" } */
+ r = b >= -1; /* { dg-warning "with boolean expression is always true" } */
+
+ r = foo () == 2; /* { dg-warning "with boolean expression is always false" } */
+ r = foo () != 2; /* { dg-warning "with boolean expression is always true" } */
+ r = foo () < 2; /* { dg-warning "with boolean expression is always true" } */
+ r = foo () > 2; /* { dg-warning "with boolean expression is always false" } */
+ r = foo () <= 2; /* { dg-warning "with boolean expression is always true" } */
+ r = foo () >= 2; /* { dg-warning "with boolean expression is always false" } */
+
+ r = b == E; /* { dg-warning "with boolean expression is always false" } */
+ r = b != E; /* { dg-warning "with boolean expression is always true" } */
+ r = b < E; /* { dg-warning "with boolean expression is always true" } */
+ r = b > E; /* { dg-warning "with boolean expression is always false" } */
+ r = b <= E; /* { dg-warning "with boolean expression is always true" } */
+ r = b >= E; /* { dg-warning "with boolean expression is always false" } */
+
+ /* Swap LHS and RHS. */
+ r = 2 == b; /* { dg-warning "with boolean expression is always false" } */
+ r = 2 != b; /* { dg-warning "with boolean expression is always true" } */
+ r = 2 < b; /* { dg-warning "with boolean expression is always false" } */
+ r = 2 > b; /* { dg-warning "with boolean expression is always true" } */
+ r = 2 <= b; /* { dg-warning "with boolean expression is always false" } */
+ r = 2 >= b; /* { dg-warning "with boolean expression is always true" } */
+
+ r = -1 == b; /* { dg-warning "with boolean expression is always false" } */
+ r = -1 != b; /* { dg-warning "with boolean expression is always true" } */
+ r = -1 < b; /* { dg-warning "with boolean expression is always true" } */
+ r = -1 > b; /* { dg-warning "with boolean expression is always false" } */
+ r = -1 <= b; /* { dg-warning "with boolean expression is always true" } */
+ r = -1 >= b; /* { dg-warning "with boolean expression is always false" } */
+
+ r = E == b; /* { dg-warning "with boolean expression is always false" } */
+ r = E != b; /* { dg-warning "with boolean expression is always true" } */
+ r = E < b; /* { dg-warning "with boolean expression is always false" } */
+ r = E > b; /* { dg-warning "with boolean expression is always true" } */
+ r = E <= b; /* { dg-warning "with boolean expression is always false" } */
+ r = E >= b; /* { dg-warning "with boolean expression is always true" } */
+
+ /* These are of course fine. */
+ r = b == false;
+ r = b != false;
+ 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
+fn2 (int i1, int i2)
+{
+ r = (i1 == i2) == 2; /* { dg-warning "with boolean expression is always false" } */
+ r = (i1 == i2) != 2; /* { dg-warning "with boolean expression is always true" } */
+ r = (i1 == i2) < 2; /* { dg-warning "with boolean expression is always true" } */
+ r = (i1 == i2) > 2; /* { dg-warning "with boolean expression is always false" } */
+ r = (i1 == i2) <= 2; /* { dg-warning "with boolean expression is always true" } */
+ r = (i1 == i2) >= 2; /* { dg-warning "with boolean expression is always false" } */
+
+ r = (i1 == i2) == -1; /* { dg-warning "with boolean expression is always false" } */
+ r = (i1 == i2) != -1; /* { dg-warning "with boolean expression is always true" } */
+ r = (i1 == i2) < -1; /* { dg-warning "with boolean expression is always false" } */
+ r = (i1 == i2) > -1; /* { dg-warning "with boolean expression is always true" } */
+ r = (i1 == i2) <= -1; /* { dg-warning "with boolean expression is always false" } */
+ r = (i1 == i2) >= -1; /* { dg-warning "with boolean expression is always true" } */
+
+ r = (i1 == i2) == E; /* { dg-warning "with boolean expression is always false" } */
+ r = (i1 == i2) != E; /* { dg-warning "with boolean expression is always true" } */
+ r = (i1 == i2) < E; /* { dg-warning "with boolean expression is always true" } */
+ r = (i1 == i2) > E; /* { dg-warning "with boolean expression is always false" } */
+ r = (i1 == i2) <= E; /* { dg-warning "with boolean expression is always true" } */
+ r = (i1 == i2) >= E; /* { dg-warning "with boolean expression is always false" } */
+}
+
+void
+fn3 (int n, bool b)
+{
+ /* Don't warn here. */
+ r = b == n;
+ r = b != n;
+ r = b < n;
+ r = b > n;
+ r = b <= n;
+ r = b >= n;
+
+ r = n == E;
+ r = n != E;
+ r = n < E;
+ r = n > E;
+ r = n <= E;
+ r = n >= E;
+}