xref: /minix3/external/bsd/llvm/dist/clang/test/Sema/compare.c (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -pedantic -verify -Wsign-compare %s -Wno-unreachable-code
2*f4a2713aSLionel Sambuc 
test(char * C)3*f4a2713aSLionel Sambuc int test(char *C) { // nothing here should warn.
4*f4a2713aSLionel Sambuc   return C != ((void*)0);
5*f4a2713aSLionel Sambuc   return C != (void*)0;
6*f4a2713aSLionel Sambuc   return C != 0;
7*f4a2713aSLionel Sambuc   return C != 1;  // expected-warning {{comparison between pointer and integer ('char *' and 'int')}}
8*f4a2713aSLionel Sambuc }
9*f4a2713aSLionel Sambuc 
ints(long a,unsigned long b)10*f4a2713aSLionel Sambuc int ints(long a, unsigned long b) {
11*f4a2713aSLionel Sambuc   enum EnumA {A};
12*f4a2713aSLionel Sambuc   enum EnumB {B};
13*f4a2713aSLionel Sambuc   enum EnumC {C = 0x10000};
14*f4a2713aSLionel Sambuc   return
15*f4a2713aSLionel Sambuc          // (a,b)
16*f4a2713aSLionel Sambuc          (a == (unsigned long) b) +  // expected-warning {{comparison of integers of different signs}}
17*f4a2713aSLionel Sambuc          (a == (unsigned int) b) +
18*f4a2713aSLionel Sambuc          (a == (unsigned short) b) +
19*f4a2713aSLionel Sambuc          (a == (unsigned char) b) +
20*f4a2713aSLionel Sambuc          ((long) a == b) +  // expected-warning {{comparison of integers of different signs}}
21*f4a2713aSLionel Sambuc          ((int) a == b) +  // expected-warning {{comparison of integers of different signs}}
22*f4a2713aSLionel Sambuc          ((short) a == b) +  // expected-warning {{comparison of integers of different signs}}
23*f4a2713aSLionel Sambuc          ((signed char) a == b) +  // expected-warning {{comparison of integers of different signs}}
24*f4a2713aSLionel Sambuc          ((long) a == (unsigned long) b) +  // expected-warning {{comparison of integers of different signs}}
25*f4a2713aSLionel Sambuc          ((int) a == (unsigned int) b) +  // expected-warning {{comparison of integers of different signs}}
26*f4a2713aSLionel Sambuc          ((short) a == (unsigned short) b) +
27*f4a2713aSLionel Sambuc          ((signed char) a == (unsigned char) b) +
28*f4a2713aSLionel Sambuc          (a < (unsigned long) b) +  // expected-warning {{comparison of integers of different signs}}
29*f4a2713aSLionel Sambuc          (a < (unsigned int) b) +
30*f4a2713aSLionel Sambuc          (a < (unsigned short) b) +
31*f4a2713aSLionel Sambuc          (a < (unsigned char) b) +
32*f4a2713aSLionel Sambuc          ((long) a < b) +  // expected-warning {{comparison of integers of different signs}}
33*f4a2713aSLionel Sambuc          ((int) a < b) +  // expected-warning {{comparison of integers of different signs}}
34*f4a2713aSLionel Sambuc          ((short) a < b) +  // expected-warning {{comparison of integers of different signs}}
35*f4a2713aSLionel Sambuc          ((signed char) a < b) +  // expected-warning {{comparison of integers of different signs}}
36*f4a2713aSLionel Sambuc          ((long) a < (unsigned long) b) +  // expected-warning {{comparison of integers of different signs}}
37*f4a2713aSLionel Sambuc          ((int) a < (unsigned int) b) +  // expected-warning {{comparison of integers of different signs}}
38*f4a2713aSLionel Sambuc          ((short) a < (unsigned short) b) +
39*f4a2713aSLionel Sambuc          ((signed char) a < (unsigned char) b) +
40*f4a2713aSLionel Sambuc 
41*f4a2713aSLionel Sambuc          // (A,b)
42*f4a2713aSLionel Sambuc          (A == (unsigned long) b) +
43*f4a2713aSLionel Sambuc          (A == (unsigned int) b) +
44*f4a2713aSLionel Sambuc          (A == (unsigned short) b) +
45*f4a2713aSLionel Sambuc          (A == (unsigned char) b) +
46*f4a2713aSLionel Sambuc          ((long) A == b) +
47*f4a2713aSLionel Sambuc          ((int) A == b) +
48*f4a2713aSLionel Sambuc          ((short) A == b) +
49*f4a2713aSLionel Sambuc          ((signed char) A == b) +
50*f4a2713aSLionel Sambuc          ((long) A == (unsigned long) b) +
51*f4a2713aSLionel Sambuc          ((int) A == (unsigned int) b) +
52*f4a2713aSLionel Sambuc          ((short) A == (unsigned short) b) +
53*f4a2713aSLionel Sambuc          ((signed char) A == (unsigned char) b) +
54*f4a2713aSLionel Sambuc          (A < (unsigned long) b) +
55*f4a2713aSLionel Sambuc          (A < (unsigned int) b) +
56*f4a2713aSLionel Sambuc          (A < (unsigned short) b) +
57*f4a2713aSLionel Sambuc          (A < (unsigned char) b) +
58*f4a2713aSLionel Sambuc          ((long) A < b) +
59*f4a2713aSLionel Sambuc          ((int) A < b) +
60*f4a2713aSLionel Sambuc          ((short) A < b) +
61*f4a2713aSLionel Sambuc          ((signed char) A < b) +
62*f4a2713aSLionel Sambuc          ((long) A < (unsigned long) b) +
63*f4a2713aSLionel Sambuc          ((int) A < (unsigned int) b) +
64*f4a2713aSLionel Sambuc          ((short) A < (unsigned short) b) +
65*f4a2713aSLionel Sambuc          ((signed char) A < (unsigned char) b) +
66*f4a2713aSLionel Sambuc 
67*f4a2713aSLionel Sambuc          // (a,B)
68*f4a2713aSLionel Sambuc          (a == (unsigned long) B) +
69*f4a2713aSLionel Sambuc          (a == (unsigned int) B) +
70*f4a2713aSLionel Sambuc          (a == (unsigned short) B) +
71*f4a2713aSLionel Sambuc          (a == (unsigned char) B) +
72*f4a2713aSLionel Sambuc          ((long) a == B) +
73*f4a2713aSLionel Sambuc          ((int) a == B) +
74*f4a2713aSLionel Sambuc          ((short) a == B) +
75*f4a2713aSLionel Sambuc          ((signed char) a == B) +
76*f4a2713aSLionel Sambuc          ((long) a == (unsigned long) B) +
77*f4a2713aSLionel Sambuc          ((int) a == (unsigned int) B) +
78*f4a2713aSLionel Sambuc          ((short) a == (unsigned short) B) +
79*f4a2713aSLionel Sambuc          ((signed char) a == (unsigned char) B) +
80*f4a2713aSLionel Sambuc          (a < (unsigned long) B) +  // expected-warning {{comparison of integers of different signs}}
81*f4a2713aSLionel Sambuc          (a < (unsigned int) B) +
82*f4a2713aSLionel Sambuc          (a < (unsigned short) B) +
83*f4a2713aSLionel Sambuc          (a < (unsigned char) B) +
84*f4a2713aSLionel Sambuc          ((long) a < B) +
85*f4a2713aSLionel Sambuc          ((int) a < B) +
86*f4a2713aSLionel Sambuc          ((short) a < B) +
87*f4a2713aSLionel Sambuc          ((signed char) a < B) +
88*f4a2713aSLionel Sambuc          ((long) a < (unsigned long) B) +  // expected-warning {{comparison of integers of different signs}}
89*f4a2713aSLionel Sambuc          ((int) a < (unsigned int) B) +  // expected-warning {{comparison of integers of different signs}}
90*f4a2713aSLionel Sambuc          ((short) a < (unsigned short) B) +
91*f4a2713aSLionel Sambuc          ((signed char) a < (unsigned char) B) +
92*f4a2713aSLionel Sambuc 
93*f4a2713aSLionel Sambuc          // (C,b)
94*f4a2713aSLionel Sambuc          (C == (unsigned long) b) +
95*f4a2713aSLionel Sambuc          (C == (unsigned int) b) +
96*f4a2713aSLionel Sambuc          (C == (unsigned short) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned short' is always false}}
97*f4a2713aSLionel Sambuc          (C == (unsigned char) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned char' is always false}}
98*f4a2713aSLionel Sambuc          ((long) C == b) +
99*f4a2713aSLionel Sambuc          ((int) C == b) +
100*f4a2713aSLionel Sambuc          ((short) C == b) +
101*f4a2713aSLionel Sambuc          ((signed char) C == b) +
102*f4a2713aSLionel Sambuc          ((long) C == (unsigned long) b) +
103*f4a2713aSLionel Sambuc          ((int) C == (unsigned int) b) +
104*f4a2713aSLionel Sambuc          ((short) C == (unsigned short) b) +
105*f4a2713aSLionel Sambuc          ((signed char) C == (unsigned char) b) +
106*f4a2713aSLionel Sambuc          (C < (unsigned long) b) +
107*f4a2713aSLionel Sambuc          (C < (unsigned int) b) +
108*f4a2713aSLionel Sambuc          (C < (unsigned short) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned short' is always false}}
109*f4a2713aSLionel Sambuc          (C < (unsigned char) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned char' is always false}}
110*f4a2713aSLionel Sambuc          ((long) C < b) +
111*f4a2713aSLionel Sambuc          ((int) C < b) +
112*f4a2713aSLionel Sambuc          ((short) C < b) +
113*f4a2713aSLionel Sambuc          ((signed char) C < b) +
114*f4a2713aSLionel Sambuc          ((long) C < (unsigned long) b) +
115*f4a2713aSLionel Sambuc          ((int) C < (unsigned int) b) +
116*f4a2713aSLionel Sambuc          ((short) C < (unsigned short) b) +
117*f4a2713aSLionel Sambuc          ((signed char) C < (unsigned char) b) +
118*f4a2713aSLionel Sambuc 
119*f4a2713aSLionel Sambuc          // (a,C)
120*f4a2713aSLionel Sambuc          (a == (unsigned long) C) +
121*f4a2713aSLionel Sambuc          (a == (unsigned int) C) +
122*f4a2713aSLionel Sambuc          (a == (unsigned short) C) +
123*f4a2713aSLionel Sambuc          (a == (unsigned char) C) +
124*f4a2713aSLionel Sambuc          ((long) a == C) +
125*f4a2713aSLionel Sambuc          ((int) a == C) +
126*f4a2713aSLionel Sambuc          ((short) a == C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'short' is always false}}
127*f4a2713aSLionel Sambuc          ((signed char) a == C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'signed char' is always false}}
128*f4a2713aSLionel Sambuc          ((long) a == (unsigned long) C) +
129*f4a2713aSLionel Sambuc          ((int) a == (unsigned int) C) +
130*f4a2713aSLionel Sambuc          ((short) a == (unsigned short) C) +
131*f4a2713aSLionel Sambuc          ((signed char) a == (unsigned char) C) +
132*f4a2713aSLionel Sambuc          (a < (unsigned long) C) +  // expected-warning {{comparison of integers of different signs}}
133*f4a2713aSLionel Sambuc          (a < (unsigned int) C) +
134*f4a2713aSLionel Sambuc          (a < (unsigned short) C) +
135*f4a2713aSLionel Sambuc          (a < (unsigned char) C) +
136*f4a2713aSLionel Sambuc          ((long) a < C) +
137*f4a2713aSLionel Sambuc          ((int) a < C) +
138*f4a2713aSLionel Sambuc          ((short) a < C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'short' is always true}}
139*f4a2713aSLionel Sambuc          ((signed char) a < C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'signed char' is always true}}
140*f4a2713aSLionel Sambuc          ((long) a < (unsigned long) C) +  // expected-warning {{comparison of integers of different signs}}
141*f4a2713aSLionel Sambuc          ((int) a < (unsigned int) C) +  // expected-warning {{comparison of integers of different signs}}
142*f4a2713aSLionel Sambuc          ((short) a < (unsigned short) C) +
143*f4a2713aSLionel Sambuc          ((signed char) a < (unsigned char) C) +
144*f4a2713aSLionel Sambuc 
145*f4a2713aSLionel Sambuc          // (0x80000,b)
146*f4a2713aSLionel Sambuc          (0x80000 == (unsigned long) b) +
147*f4a2713aSLionel Sambuc          (0x80000 == (unsigned int) b) +
148*f4a2713aSLionel Sambuc          (0x80000 == (unsigned short) b) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned short' is always false}}
149*f4a2713aSLionel Sambuc          (0x80000 == (unsigned char) b) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned char' is always false}}
150*f4a2713aSLionel Sambuc          ((long) 0x80000 == b) +
151*f4a2713aSLionel Sambuc          ((int) 0x80000 == b) +
152*f4a2713aSLionel Sambuc          ((short) 0x80000 == b) +
153*f4a2713aSLionel Sambuc          ((signed char) 0x80000 == b) +
154*f4a2713aSLionel Sambuc          ((long) 0x80000 == (unsigned long) b) +
155*f4a2713aSLionel Sambuc          ((int) 0x80000 == (unsigned int) b) +
156*f4a2713aSLionel Sambuc          ((short) 0x80000 == (unsigned short) b) +
157*f4a2713aSLionel Sambuc          ((signed char) 0x80000 == (unsigned char) b) +
158*f4a2713aSLionel Sambuc          (0x80000 < (unsigned long) b) +
159*f4a2713aSLionel Sambuc          (0x80000 < (unsigned int) b) +
160*f4a2713aSLionel Sambuc          (0x80000 < (unsigned short) b) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned short' is always false}}
161*f4a2713aSLionel Sambuc          (0x80000 < (unsigned char) b) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned char' is always false}}
162*f4a2713aSLionel Sambuc          ((long) 0x80000 < b) +
163*f4a2713aSLionel Sambuc          ((int) 0x80000 < b) +
164*f4a2713aSLionel Sambuc          ((short) 0x80000 < b) +
165*f4a2713aSLionel Sambuc          ((signed char) 0x80000 < b) +
166*f4a2713aSLionel Sambuc          ((long) 0x80000 < (unsigned long) b) +
167*f4a2713aSLionel Sambuc          ((int) 0x80000 < (unsigned int) b) +
168*f4a2713aSLionel Sambuc          ((short) 0x80000 < (unsigned short) b) +
169*f4a2713aSLionel Sambuc          ((signed char) 0x80000 < (unsigned char) b) +
170*f4a2713aSLionel Sambuc 
171*f4a2713aSLionel Sambuc          // (a,0x80000)
172*f4a2713aSLionel Sambuc          (a == (unsigned long) 0x80000) +
173*f4a2713aSLionel Sambuc          (a == (unsigned int) 0x80000) +
174*f4a2713aSLionel Sambuc          (a == (unsigned short) 0x80000) +
175*f4a2713aSLionel Sambuc          (a == (unsigned char) 0x80000) +
176*f4a2713aSLionel Sambuc          ((long) a == 0x80000) +
177*f4a2713aSLionel Sambuc          ((int) a == 0x80000) +
178*f4a2713aSLionel Sambuc          ((short) a == 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'short' is always false}}
179*f4a2713aSLionel Sambuc          ((signed char) a == 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'signed char' is always false}}
180*f4a2713aSLionel Sambuc          ((long) a == (unsigned long) 0x80000) +
181*f4a2713aSLionel Sambuc          ((int) a == (unsigned int) 0x80000) +
182*f4a2713aSLionel Sambuc          ((short) a == (unsigned short) 0x80000) +
183*f4a2713aSLionel Sambuc          ((signed char) a == (unsigned char) 0x80000) +
184*f4a2713aSLionel Sambuc          (a < (unsigned long) 0x80000) +  // expected-warning {{comparison of integers of different signs}}
185*f4a2713aSLionel Sambuc          (a < (unsigned int) 0x80000) +
186*f4a2713aSLionel Sambuc          (a < (unsigned short) 0x80000) +
187*f4a2713aSLionel Sambuc          (a < (unsigned char) 0x80000) +
188*f4a2713aSLionel Sambuc          ((long) a < 0x80000) +
189*f4a2713aSLionel Sambuc          ((int) a < 0x80000) +
190*f4a2713aSLionel Sambuc          ((short) a < 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'short' is always true}}
191*f4a2713aSLionel Sambuc          ((signed char) a < 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'signed char' is always true}}
192*f4a2713aSLionel Sambuc          ((long) a < (unsigned long) 0x80000) +  // expected-warning {{comparison of integers of different signs}}
193*f4a2713aSLionel Sambuc          ((int) a < (unsigned int) 0x80000) +  // expected-warning {{comparison of integers of different signs}}
194*f4a2713aSLionel Sambuc          ((short) a < (unsigned short) 0x80000) +
195*f4a2713aSLionel Sambuc          ((signed char) a < (unsigned char) 0x80000) +
196*f4a2713aSLionel Sambuc 
197*f4a2713aSLionel Sambuc          // We should be able to avoid warning about this.
198*f4a2713aSLionel Sambuc          (b != (a < 4 ? 1 : 2)) +
199*f4a2713aSLionel Sambuc 
200*f4a2713aSLionel Sambuc          10
201*f4a2713aSLionel Sambuc     ;
202*f4a2713aSLionel Sambuc }
203*f4a2713aSLionel Sambuc 
equal(char * a,const char * b)204*f4a2713aSLionel Sambuc int equal(char *a, const char *b) {
205*f4a2713aSLionel Sambuc     return a == b;
206*f4a2713aSLionel Sambuc }
207*f4a2713aSLionel Sambuc 
arrays(char (* a)[5],char (* b)[10],char (* c)[5])208*f4a2713aSLionel Sambuc int arrays(char (*a)[5], char(*b)[10], char(*c)[5]) {
209*f4a2713aSLionel Sambuc   int d = (a == c);
210*f4a2713aSLionel Sambuc   return a == b; // expected-warning {{comparison of distinct pointer types}}
211*f4a2713aSLionel Sambuc }
212*f4a2713aSLionel Sambuc 
pointers(int * a)213*f4a2713aSLionel Sambuc int pointers(int *a) {
214*f4a2713aSLionel Sambuc   return a > 0; // expected-warning {{ordered comparison between pointer and zero ('int *' and 'int') is an extension}}
215*f4a2713aSLionel Sambuc   return a > 42; // expected-warning {{ordered comparison between pointer and integer ('int *' and 'int')}}
216*f4a2713aSLionel Sambuc   return a > (void *)0; // expected-warning {{comparison of distinct pointer types}}
217*f4a2713aSLionel Sambuc }
218*f4a2713aSLionel Sambuc 
function_pointers(int (* a)(int),int (* b)(int),void (* c)(int))219*f4a2713aSLionel Sambuc int function_pointers(int (*a)(int), int (*b)(int), void (*c)(int)) {
220*f4a2713aSLionel Sambuc   return a > b; // expected-warning {{ordered comparison of function pointers}}
221*f4a2713aSLionel Sambuc   return function_pointers > function_pointers; // expected-warning {{self-comparison always evaluates to false}} expected-warning{{ordered comparison of function pointers}}
222*f4a2713aSLionel Sambuc   return a > c; // expected-warning {{comparison of distinct pointer types}}
223*f4a2713aSLionel Sambuc   return a == (void *) 0;
224*f4a2713aSLionel Sambuc   return a == (void *) 1; // expected-warning {{equality comparison between function pointer and void pointer}}
225*f4a2713aSLionel Sambuc }
226*f4a2713aSLionel Sambuc 
void_pointers(void * foo)227*f4a2713aSLionel Sambuc int void_pointers(void* foo) {
228*f4a2713aSLionel Sambuc   return foo == (void*) 0;
229*f4a2713aSLionel Sambuc   return foo == (void*) 1;
230*f4a2713aSLionel Sambuc }
231*f4a2713aSLionel Sambuc 
232*f4a2713aSLionel Sambuc 
test1(int i)233*f4a2713aSLionel Sambuc int test1(int i) {
234*f4a2713aSLionel Sambuc   enum en { zero };
235*f4a2713aSLionel Sambuc   return i > zero;
236*f4a2713aSLionel Sambuc }
237*f4a2713aSLionel Sambuc 
238*f4a2713aSLionel Sambuc // PR5937
test2(int i32)239*f4a2713aSLionel Sambuc int test2(int i32) {
240*f4a2713aSLionel Sambuc   struct foo {
241*f4a2713aSLionel Sambuc     unsigned int u8 : 8;
242*f4a2713aSLionel Sambuc     unsigned long long u31 : 31;
243*f4a2713aSLionel Sambuc     unsigned long long u32 : 32;
244*f4a2713aSLionel Sambuc     unsigned long long u63 : 63;
245*f4a2713aSLionel Sambuc     unsigned long long u64 : 64;
246*f4a2713aSLionel Sambuc   } *x;
247*f4a2713aSLionel Sambuc 
248*f4a2713aSLionel Sambuc   if (x->u8 == i32) { // comparison in int32, exact
249*f4a2713aSLionel Sambuc     return 0;
250*f4a2713aSLionel Sambuc   } else if (x->u31 == i32) { // comparison in int32, exact
251*f4a2713aSLionel Sambuc     return 1;
252*f4a2713aSLionel Sambuc   } else if (x->u32 == i32) { // expected-warning {{comparison of integers of different signs}}
253*f4a2713aSLionel Sambuc     return 2;
254*f4a2713aSLionel Sambuc   } else if (x->u63 == i32) { // comparison in uint64, exact because ==
255*f4a2713aSLionel Sambuc     return 3;
256*f4a2713aSLionel Sambuc   } else if (x->u64 == i32) { // expected-warning {{comparison of integers of different signs}}
257*f4a2713aSLionel Sambuc     return 4;
258*f4a2713aSLionel Sambuc   } else {
259*f4a2713aSLionel Sambuc     return 5;
260*f4a2713aSLionel Sambuc   }
261*f4a2713aSLionel Sambuc }
262*f4a2713aSLionel Sambuc 
263*f4a2713aSLionel Sambuc // PR5887
test3()264*f4a2713aSLionel Sambuc void test3() {
265*f4a2713aSLionel Sambuc   unsigned short x, y;
266*f4a2713aSLionel Sambuc   unsigned int z;
267*f4a2713aSLionel Sambuc   if ((x > y ? x : y) > z)
268*f4a2713aSLionel Sambuc     (void) 0;
269*f4a2713aSLionel Sambuc }
270*f4a2713aSLionel Sambuc 
271*f4a2713aSLionel Sambuc // PR5961
272*f4a2713aSLionel Sambuc extern char *ptr4;
test4()273*f4a2713aSLionel Sambuc void test4() {
274*f4a2713aSLionel Sambuc   long value;
275*f4a2713aSLionel Sambuc   if (value < (unsigned long) &ptr4) // expected-warning {{comparison of integers of different signs}}
276*f4a2713aSLionel Sambuc     return;
277*f4a2713aSLionel Sambuc }
278*f4a2713aSLionel Sambuc 
279*f4a2713aSLionel Sambuc // PR4807
test5(unsigned int x)280*f4a2713aSLionel Sambuc int test5(unsigned int x) {
281*f4a2713aSLionel Sambuc   return (x < 0) // expected-warning {{comparison of unsigned expression < 0 is always false}}
282*f4a2713aSLionel Sambuc     && (0 > x)   // expected-warning {{comparison of 0 > unsigned expression is always false}}
283*f4a2713aSLionel Sambuc     && (x >= 0)  // expected-warning {{comparison of unsigned expression >= 0 is always true}}
284*f4a2713aSLionel Sambuc     && (0 <= x); // expected-warning {{comparison of 0 <= unsigned expression is always true}}
285*f4a2713aSLionel Sambuc }
286*f4a2713aSLionel Sambuc 
test6(unsigned i,unsigned power)287*f4a2713aSLionel Sambuc int test6(unsigned i, unsigned power) {
288*f4a2713aSLionel Sambuc   unsigned x = (i < (1 << power) ? i : 0);
289*f4a2713aSLionel Sambuc   return x != 3 ? 1 << power : i;
290*f4a2713aSLionel Sambuc }
291*f4a2713aSLionel Sambuc 
292*f4a2713aSLionel Sambuc // <rdar://problem/8414119> enum >= (enum)0 comparison should not generate any warnings
293*f4a2713aSLionel Sambuc enum rdar8414119_Vals { X, Y, Z };
294*f4a2713aSLionel Sambuc #define ZERO 0
295*f4a2713aSLionel Sambuc #define CHECK(x) (x >= X)
rdar8414119_foo(enum rdar8414119_Vals v)296*f4a2713aSLionel Sambuc void rdar8414119_foo(enum rdar8414119_Vals v) {
297*f4a2713aSLionel Sambuc   if (CHECK(v)) // no-warning
298*f4a2713aSLionel Sambuc    return;
299*f4a2713aSLionel Sambuc   if (v >= X) // no-warning
300*f4a2713aSLionel Sambuc    return;
301*f4a2713aSLionel Sambuc }
rdar8414119_bar(unsigned x)302*f4a2713aSLionel Sambuc int rdar8414119_bar(unsigned x) {
303*f4a2713aSLionel Sambuc   return x >= ZERO; // no-warning
304*f4a2713aSLionel Sambuc }
305*f4a2713aSLionel Sambuc #undef ZERO
306*f4a2713aSLionel Sambuc #undef CHECK
307*f4a2713aSLionel Sambuc 
rdar8511238()308*f4a2713aSLionel Sambuc int rdar8511238() {
309*f4a2713aSLionel Sambuc   enum A { A_foo, A_bar };
310*f4a2713aSLionel Sambuc   enum A a;
311*f4a2713aSLionel Sambuc   if (a < 0) // expected-warning {{comparison of unsigned enum expression < 0 is always false}}
312*f4a2713aSLionel Sambuc     return 0;
313*f4a2713aSLionel Sambuc   return 20;
314*f4a2713aSLionel Sambuc }
315*f4a2713aSLionel Sambuc 
316*f4a2713aSLionel Sambuc // PR10336
test9(int sv,unsigned uv,long slv)317*f4a2713aSLionel Sambuc int test9(int sv, unsigned uv, long slv) {
318*f4a2713aSLionel Sambuc   return sv == (uv ^= slv); // expected-warning {{comparison of integers of different signs: 'int' and 'unsigned int'}}
319*f4a2713aSLionel Sambuc }
320*f4a2713aSLionel Sambuc 
test10(void)321*f4a2713aSLionel Sambuc void test10(void) {
322*f4a2713aSLionel Sambuc   int si;
323*f4a2713aSLionel Sambuc   unsigned int ui;
324*f4a2713aSLionel Sambuc   long sl;
325*f4a2713aSLionel Sambuc 
326*f4a2713aSLionel Sambuc   _Bool b;
327*f4a2713aSLionel Sambuc   b = (si == (ui = sl)); // expected-warning {{comparison of integers of different signs: 'int' and 'unsigned int'}}
328*f4a2713aSLionel Sambuc   b = (si == (ui = sl&15));
329*f4a2713aSLionel Sambuc }
330*f4a2713aSLionel Sambuc 
331*f4a2713aSLionel Sambuc // PR11572
332*f4a2713aSLionel Sambuc struct test11S { unsigned x : 30; };
test11(unsigned y,struct test11S * p)333*f4a2713aSLionel Sambuc int test11(unsigned y, struct test11S *p) {
334*f4a2713aSLionel Sambuc   return y > (p->x >> 24); // no-warning
335*f4a2713aSLionel Sambuc }
336*f4a2713aSLionel Sambuc 
337*f4a2713aSLionel Sambuc typedef char one_char[1];
338*f4a2713aSLionel Sambuc typedef char two_chars[2];
339*f4a2713aSLionel Sambuc 
test12(unsigned a)340*f4a2713aSLionel Sambuc void test12(unsigned a) {
341*f4a2713aSLionel Sambuc   if (0 && -1 > a) { }
342*f4a2713aSLionel Sambuc }
343