[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / test / Sema / warn-bitwise-compare.c
blobccd83613b80655ea0ca8fefc4f26397f90983aaa
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) {}
96 void g(int x) {
97 if (x | 5) {} // expected-warning {{bitwise or with non-zero value always evaluates to true}}
98 if (5 | x) {} // expected-warning {{bitwise or with non-zero value always evaluates to true}}
99 if (!((x | 5))) {} // expected-warning {{bitwise or with non-zero value always evaluates to true}}
101 if (x | -1) {} // expected-warning {{bitwise or with non-zero value always evaluates to true}}
102 if (x | ONE) {} // expected-warning {{bitwise or with non-zero value always evaluates to true}}
104 if (x | ZERO) {}