1 // RUN: %clang_cc1 -fsyntax-only -verify -Wtautological-bitwise-compare %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -Wall -Wno-unused %s
3 
4 #define mydefine 2
5 
6 enum {
7   ZERO,
8   ONE,
9 };
10 
f(int x)11 void f(int x) {
12   if ((8 & x) == 3) {}  // expected-warning {{bitwise comparison always evaluates to false}}
13   if ((x & 8) == 4) {}  // expected-warning {{bitwise comparison always evaluates to false}}
14   if ((x & 8) != 4) {}  // expected-warning {{bitwise comparison always evaluates to true}}
15   if ((2 & x) != 4) {}  // expected-warning {{bitwise comparison always evaluates to true}}
16   if ((x | 4) == 3) {}  // expected-warning {{bitwise comparison always evaluates to false}}
17   if ((x | 3) != 4) {}  // expected-warning {{bitwise comparison always evaluates to true}}
18   if ((5 | x) != 3) {}  // expected-warning {{bitwise comparison always evaluates to true}}
19   if ((x & 0x15) == 0x13) {} // expected-warning {{bitwise comparison always evaluates to false}}
20   if ((0x23 | x) == 0x155){} // expected-warning {{bitwise comparison always evaluates to false}}
21 
22   if (!!((8 & x) == 3)) {}  // expected-warning {{bitwise comparison always evaluates to false}}
23   int y = ((8 & x) == 3) ? 1 : 2;  // expected-warning {{bitwise comparison always evaluates to false}}
24 
25   if ((x & 8) == 8) {}
26   if ((x & 8) != 8) {}
27   if ((x | 4) == 4) {}
28   if ((x | 4) != 4) {}
29 
30   if ((x & 9) == 8) {}
31   if ((x & 9) != 8) {}
32   if ((x | 4) == 5) {}
33   if ((x | 4) != 5) {}
34 
35   if ((x & mydefine) == 8) {}
36   if ((x | mydefine) == 4) {}
37 }
38 
g(int x)39 void g(int x) {
40   if (x | 5) {}  // expected-warning {{bitwise or with non-zero value always evaluates to true}}
41   if (5 | x) {}  // expected-warning {{bitwise or with non-zero value always evaluates to true}}
42   if (!((x | 5))) {}  // expected-warning {{bitwise or with non-zero value always evaluates to true}}
43 
44   if (x | -1) {}  // expected-warning {{bitwise or with non-zero value always evaluates to true}}
45   if (x | ONE) {}  // expected-warning {{bitwise or with non-zero value always evaluates to true}}
46 
47   if (x | ZERO) {}
48 }
49