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