[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / Sema / warn-bitwise-compare.c
blobe66c58ee5ae3a3b923e8349df5f3d3e7fcd547fb
1 // RUN: %clang_cc1 -fsyntax-only -verify -Wtautological-bitwise-compare %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -Wall -Wno-unused %s
4 #define mydefine 2
5 #define mydefine2 -2
7 enum {
8 ZERO,
9 ONE,
12 void f(int x) {
13 if ((8 & x) == 3) {} // expected-warning {{bitwise comparison always evaluates to false}}
14 if ((x & 8) == 4) {} // expected-warning {{bitwise comparison always evaluates to false}}
15 if ((-8 & x) == 3) {} // expected-warning {{bitwise comparison always evaluates to false}}
16 if ((x & -8) == 4) {} // expected-warning {{bitwise comparison always evaluates to false}}
18 if ((x & 8) != 4) {} // expected-warning {{bitwise comparison always evaluates to true}}
19 if ((2 & x) != 4) {} // expected-warning {{bitwise comparison always evaluates to true}}
20 if ((x & -8) != 4) {} // expected-warning {{bitwise comparison always evaluates to true}}
21 if ((-2 & x) != 3) {} // expected-warning {{bitwise comparison always evaluates to true}}
23 if ((x | 4) == 3) {} // expected-warning {{bitwise comparison always evaluates to false}}
24 if ((x | 3) != 4) {} // expected-warning {{bitwise comparison always evaluates to true}}
25 if ((5 | x) != 3) {} // expected-warning {{bitwise comparison always evaluates to true}}
27 if ((x | -4) == 3) {} // expected-warning {{bitwise comparison always evaluates to false}}
28 if ((x | -3) != 4) {} // expected-warning {{bitwise comparison always evaluates to true}}
29 if ((-5 | x) != 3) {} // expected-warning {{bitwise comparison always evaluates to true}}
31 if ((x & 0x15) == 0x13) {} // expected-warning {{bitwise comparison always evaluates to false}}
32 if ((x & 0xFFEB) == 0x13) {} // expected-warning {{bitwise comparison always evaluates to false}}
34 if ((0x23 | x) == 0x155){} // expected-warning {{bitwise comparison always evaluates to false}}
35 if ((0xFFDD | x) == 0x155){} // expected-warning {{bitwise comparison always evaluates to false}}
37 if (!!((8 & x) == 3)) {} // expected-warning {{bitwise comparison always evaluates to false}}
38 if (!!((-8 & x) == 3)) {} // expected-warning {{bitwise comparison always evaluates to false}}
40 int y = ((8 & x) == 3) ? 1 : 2; // expected-warning {{bitwise comparison always evaluates to false}}
41 y = ((-8 & x) == 3) ? 1 : 2; // expected-warning {{bitwise comparison always evaluates to false}}
42 y = ((3 | x) != 5) ? 1 : 2; // expected-warning {{bitwise comparison always evaluates to true}}
43 y = ((-3 | x) != 5) ? 1 : 2; // expected-warning {{bitwise comparison always evaluates to true}}
45 if ((x & 0) != !0) {} // expected-warning {{bitwise comparison always evaluates to true}}
46 if ((x & 0) == !0) {} // expected-warning {{bitwise comparison always evaluates to false}}
47 if ((x & 2) == !0) {} // expected-warning {{bitwise comparison always evaluates to false}}
49 if ((x | 4) == +3) {} // expected-warning {{bitwise comparison always evaluates to false}}
50 if ((x | 3) != +4) {} // expected-warning {{bitwise comparison always evaluates to true}}
52 if ((x & 8) != ~4) {} // expected-warning {{bitwise comparison always evaluates to true}}
53 if ((x & 0) == ~4) {} // expected-warning {{bitwise comparison always evaluates to false}}
55 if ((x & 8) == 8) {}
56 if ((x & 8) != 8) {}
57 if ((x | 4) == 4) {}
58 if ((x | 4) != 4) {}
60 if ((x | 4) == +4) {}
61 if ((x | 4) != +4) {}
63 if ((x & 1) == !12) {}
64 if ((x | 0) == !0) {}
66 if ((x | 1) == ~4) {}
67 if ((x | 1) != ~0) {}
69 if ((-2 & x) != 4) {}
70 if ((x & -8) == -8) {}
71 if ((x & -8) != -8) {}
72 if ((x | -4) == -4) {}
73 if ((x | -4) != -4) {}
75 if ((x & 9) == 8) {}
76 if ((x & 9) != 8) {}
77 if ((x | 4) == 5) {}
78 if ((x | 4) != 5) {}
80 if ((x & -9) == -10) {}
81 if ((x & -9) != -10) {}
82 if ((x | -4) == -3) {}
83 if ((x | -4) != -3) {}
85 if ((x^0) == 0) {}
87 if ((x & mydefine) == 8) {}
88 if ((x | mydefine) == 4) {}
90 if ((x & mydefine2) == 8) {}
91 if ((x | mydefine2) == 4) {}
93 if ((x & 1) == 1L) {}
95 if (0 != (x | !0LL)) {} // expected-warning {{bitwise comparison always evaluates to true}}
98 void g(int x) {
99 if (x | 5) {} // expected-warning {{bitwise or with non-zero value always evaluates to true}}
100 if (5 | x) {} // expected-warning {{bitwise or with non-zero value always evaluates to true}}
101 if (!((x | 5))) {} // expected-warning {{bitwise or with non-zero value always evaluates to true}}
103 if (x | -1) {} // expected-warning {{bitwise or with non-zero value always evaluates to true}}
104 if (x | ONE) {} // expected-warning {{bitwise or with non-zero value always evaluates to true}}
106 if (x | ZERO) {}