summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorRichard Trieu <rtrieu@google.com>2019-10-19 00:57:23 +0000
committerRichard Trieu <rtrieu@google.com>2019-10-19 00:57:23 +0000
commita27ae544f30bfd7dc765b8a6080190d39d893b99 (patch)
tree20f6b66320ee0e421f33e6e54eab73db57932762 /test
parent7decfe83555385bebac2e4bed35035f3b0f4d666 (diff)
downloadclang-a27ae544f30bfd7dc765b8a6080190d39d893b99.tar.gz
New tautological warning for bitwise-or with non-zero constant always true.
Taking a value and the bitwise-or it with a non-zero constant will always result in a non-zero value. In a boolean context, this is always true. if (x | 0x4) {} // always true, intended '&' This patch creates a new warning group -Wtautological-bitwise-compare for this warning. It also moves in the existing tautological bitwise comparisons into this group. A few other changes were needed to the CFGBuilder so that all bool contexts would be checked. The warnings in -Wtautological-bitwise-compare will be off by default due to using the CFG. Fixes: https://bugs.llvm.org/show_bug.cgi?id=42666 Differential Revision: https://reviews.llvm.org/D66046 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@375318 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test')
-rw-r--r--test/Sema/warn-bitwise-compare.c21
-rw-r--r--test/SemaCXX/warn-bitwise-compare.cpp12
2 files changed, 32 insertions, 1 deletions
diff --git a/test/Sema/warn-bitwise-compare.c b/test/Sema/warn-bitwise-compare.c
index 175f8f5367..d08f1bf13f 100644
--- a/test/Sema/warn-bitwise-compare.c
+++ b/test/Sema/warn-bitwise-compare.c
@@ -1,7 +1,12 @@
-// RUN: %clang_cc1 -fsyntax-only -verify -Wtautological-compare %s
+// RUN: %clang_cc1 -fsyntax-only -verify -Wtautological-bitwise-compare %s
#define mydefine 2
+enum {
+ ZERO,
+ ONE,
+};
+
void f(int x) {
if ((8 & x) == 3) {} // expected-warning {{bitwise comparison always evaluates to false}}
if ((x & 8) == 4) {} // expected-warning {{bitwise comparison always evaluates to false}}
@@ -13,6 +18,9 @@ void f(int x) {
if ((x & 0x15) == 0x13) {} // expected-warning {{bitwise comparison always evaluates to false}}
if ((0x23 | x) == 0x155){} // expected-warning {{bitwise comparison always evaluates to false}}
+ if (!!((8 & x) == 3)) {} // expected-warning {{bitwise comparison always evaluates to false}}
+ int y = ((8 & x) == 3) ? 1 : 2; // expected-warning {{bitwise comparison always evaluates to false}}
+
if ((x & 8) == 8) {}
if ((x & 8) != 8) {}
if ((x | 4) == 4) {}
@@ -26,3 +34,14 @@ void f(int x) {
if ((x & mydefine) == 8) {}
if ((x | mydefine) == 4) {}
}
+
+void g(int x) {
+ if (x | 5) {} // expected-warning {{bitwise or with non-zero value always evaluates to true}}
+ if (5 | x) {} // expected-warning {{bitwise or with non-zero value always evaluates to true}}
+ if (!((x | 5))) {} // expected-warning {{bitwise or with non-zero value always evaluates to true}}
+
+ if (x | -1) {} // expected-warning {{bitwise or with non-zero value always evaluates to true}}
+ if (x | ONE) {} // expected-warning {{bitwise or with non-zero value always evaluates to true}}
+
+ if (x | ZERO) {}
+}
diff --git a/test/SemaCXX/warn-bitwise-compare.cpp b/test/SemaCXX/warn-bitwise-compare.cpp
new file mode 100644
index 0000000000..894d4c581e
--- /dev/null
+++ b/test/SemaCXX/warn-bitwise-compare.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wtautological-bitwise-compare %s
+
+void test(int x) {
+ bool b1 = (8 & x) == 3;
+ // expected-warning@-1 {{bitwise comparison always evaluates to false}}
+ bool b2 = x | 5;
+ // expected-warning@-1 {{bitwise or with non-zero value always evaluates to true}}
+ bool b3 = (x | 5);
+ // expected-warning@-1 {{bitwise or with non-zero value always evaluates to true}}
+ bool b4 = !!(x | 5);
+ // expected-warning@-1 {{bitwise or with non-zero value always evaluates to true}}
+}