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}}
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
test6(unsigned i,unsigned power)288 int test6(unsigned i, unsigned power) {
289 unsigned x = (i < (1 << power) ? i : 0);
290 return x != 3 ? 1 << power : i;
291 }
292
293 // <rdar://problem/8414119> enum >= (enum)0 comparison should not generate any warnings
294 enum rdar8414119_Vals { X, Y, Z };
295 #define ZERO 0
296 #define CHECK(x) (x >= X)
rdar8414119_foo(enum rdar8414119_Vals v)297 void rdar8414119_foo(enum rdar8414119_Vals v) {
298 if (CHECK(v)) // no-warning
299 return;
300 if (v >= X) // no-warning
301 return;
302 }
rdar8414119_bar(unsigned x)303 int rdar8414119_bar(unsigned x) {
304 return x >= ZERO; // no-warning
305 }
306 #undef ZERO
307 #undef CHECK
308
rdar8511238()309 int rdar8511238() {
310 enum A { A_foo, A_bar };
311 enum A a;
312
313 if (a == 0)
314 return 0;
315 if (a != 0)
316 return 0;
317 if (a < 0) // expected-warning {{comparison of unsigned enum expression < 0 is always false}}
318 return 0;
319 if (a <= 0)
320 return 0;
321 if (a > 0)
322 return 0;
323 if (a >= 0) // expected-warning {{comparison of unsigned enum expression >= 0 is always true}}
324 return 0;
325
326 if (0 == a)
327 return 0;
328 if (0 != a)
329 return 0;
330 if (0 < a)
331 return 0;
332 if (0 <= a) // expected-warning {{comparison of 0 <= unsigned enum expression is always true}}
333 return 0;
334 if (0 > a) // expected-warning {{comparison of 0 > unsigned enum expression is always false}}
335 return 0;
336 if (0 >= a)
337 return 0;
338
339 if (a == 0U)
340 return 0;
341 if (a != 0U)
342 return 0;
343 if (a < 0U) // expected-warning {{comparison of unsigned enum expression < 0 is always false}}
344 return 0;
345 if (a <= 0U)
346 return 0;
347 if (a > 0U)
348 return 0;
349 if (a >= 0U) // expected-warning {{comparison of unsigned enum expression >= 0 is always true}}
350 return 0;
351
352 if (0U == a)
353 return 0;
354 if (0U != a)
355 return 0;
356 if (0U < a)
357 return 0;
358 if (0U <= a) // expected-warning {{comparison of 0 <= unsigned enum expression is always true}}
359 return 0;
360 if (0U > a) // expected-warning {{comparison of 0 > unsigned enum expression is always false}}
361 return 0;
362 if (0U >= a)
363 return 0;
364
365 return 20;
366 }
367
368 // PR10336
test9(int sv,unsigned uv,long slv)369 int test9(int sv, unsigned uv, long slv) {
370 return sv == (uv ^= slv); // expected-warning {{comparison of integers of different signs: 'int' and 'unsigned int'}}
371 }
372
test10(void)373 void test10(void) {
374 int si;
375 unsigned int ui;
376 long sl;
377
378 _Bool b;
379 b = (si == (ui = sl)); // expected-warning {{comparison of integers of different signs: 'int' and 'unsigned int'}}
380 b = (si == (ui = sl&15));
381 }
382
383 // PR11572
384 struct test11S { unsigned x : 30; };
test11(unsigned y,struct test11S * p)385 int test11(unsigned y, struct test11S *p) {
386 return y > (p->x >> 24); // no-warning
387 }
388
389 typedef char one_char[1];
390 typedef char two_chars[2];
391
test12(unsigned a)392 void test12(unsigned a) {
393 if (0 && -1 > a) { }
394 }
395
396 // PR36008
397
398 enum PR36008EnumTest {
399 kPR36008Value = 0,
400 };
401
pr36008(enum PR36008EnumTest lhs)402 void pr36008(enum PR36008EnumTest lhs) {
403 __typeof__(lhs) x = lhs;
404 __typeof__(kPR36008Value) y = (kPR36008Value);
405 if (x == y) x = y; // no warning
406 if (y == x) y = x; // no warning
407 }
408