blob: 5221b172a602720d0865f26af3da0f51b0c8a514 [file] [log] [blame]
Mike Stump46171912010-01-23 20:12:18 +00001// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -pedantic -verify -Wsign-compare %s -Wno-unreachable-code
Chris Lattnerd28f8152007-08-26 01:10:14 +00002
3int test(char *C) { // nothing here should warn.
4 return C != ((void*)0);
5 return C != (void*)0;
Douglas Gregord64fdd02010-06-08 19:50:34 +00006 return C != 0;
Chris Lattner06c0f5b2009-08-23 00:03:44 +00007 return C != 1; // expected-warning {{comparison between pointer and integer ('char *' and 'int')}}
Chris Lattnerd28f8152007-08-26 01:10:14 +00008}
9
John McCall45aa4552009-11-05 00:40:04 +000010int ints(long a, unsigned long b) {
John McCall5dbad3d2009-11-06 08:49:08 +000011 enum EnumA {A};
12 enum EnumB {B};
13 enum EnumC {C = 0x10000};
14 return
15 // (a,b)
16 (a == (unsigned long) b) + // expected-warning {{comparison of integers of different signs}}
17 (a == (unsigned int) b) +
18 (a == (unsigned short) b) +
19 (a == (unsigned char) b) +
20 ((long) a == b) + // expected-warning {{comparison of integers of different signs}}
21 ((int) a == b) + // expected-warning {{comparison of integers of different signs}}
22 ((short) a == b) + // expected-warning {{comparison of integers of different signs}}
23 ((signed char) a == b) + // expected-warning {{comparison of integers of different signs}}
24 ((long) a == (unsigned long) b) + // expected-warning {{comparison of integers of different signs}}
25 ((int) a == (unsigned int) b) + // expected-warning {{comparison of integers of different signs}}
John McCall323ed742010-05-06 08:58:33 +000026 ((short) a == (unsigned short) b) +
27 ((signed char) a == (unsigned char) b) +
John McCall5dbad3d2009-11-06 08:49:08 +000028 (a < (unsigned long) b) + // expected-warning {{comparison of integers of different signs}}
29 (a < (unsigned int) b) +
30 (a < (unsigned short) b) +
31 (a < (unsigned char) b) +
32 ((long) a < b) + // expected-warning {{comparison of integers of different signs}}
33 ((int) a < b) + // expected-warning {{comparison of integers of different signs}}
34 ((short) a < b) + // expected-warning {{comparison of integers of different signs}}
35 ((signed char) a < b) + // expected-warning {{comparison of integers of different signs}}
36 ((long) a < (unsigned long) b) + // expected-warning {{comparison of integers of different signs}}
37 ((int) a < (unsigned int) b) + // expected-warning {{comparison of integers of different signs}}
John McCall323ed742010-05-06 08:58:33 +000038 ((short) a < (unsigned short) b) +
39 ((signed char) a < (unsigned char) b) +
John McCallb13c87f2009-11-05 09:23:39 +000040
John McCall5dbad3d2009-11-06 08:49:08 +000041 // (A,b)
42 (A == (unsigned long) b) +
43 (A == (unsigned int) b) +
44 (A == (unsigned short) b) +
45 (A == (unsigned char) b) +
46 ((long) A == b) +
47 ((int) A == b) +
48 ((short) A == b) +
49 ((signed char) A == b) +
50 ((long) A == (unsigned long) b) +
51 ((int) A == (unsigned int) b) +
52 ((short) A == (unsigned short) b) +
53 ((signed char) A == (unsigned char) b) +
54 (A < (unsigned long) b) +
55 (A < (unsigned int) b) +
56 (A < (unsigned short) b) +
57 (A < (unsigned char) b) +
58 ((long) A < b) +
59 ((int) A < b) +
60 ((short) A < b) +
61 ((signed char) A < b) +
62 ((long) A < (unsigned long) b) +
63 ((int) A < (unsigned int) b) +
64 ((short) A < (unsigned short) b) +
65 ((signed char) A < (unsigned char) b) +
John McCallb13c87f2009-11-05 09:23:39 +000066
John McCall5dbad3d2009-11-06 08:49:08 +000067 // (a,B)
68 (a == (unsigned long) B) +
69 (a == (unsigned int) B) +
70 (a == (unsigned short) B) +
71 (a == (unsigned char) B) +
72 ((long) a == B) +
73 ((int) a == B) +
74 ((short) a == B) +
75 ((signed char) a == B) +
76 ((long) a == (unsigned long) B) +
77 ((int) a == (unsigned int) B) +
78 ((short) a == (unsigned short) B) +
79 ((signed char) a == (unsigned char) B) +
80 (a < (unsigned long) B) + // expected-warning {{comparison of integers of different signs}}
81 (a < (unsigned int) B) +
82 (a < (unsigned short) B) +
83 (a < (unsigned char) B) +
84 ((long) a < B) +
85 ((int) a < B) +
86 ((short) a < B) +
87 ((signed char) a < B) +
88 ((long) a < (unsigned long) B) + // expected-warning {{comparison of integers of different signs}}
89 ((int) a < (unsigned int) B) + // expected-warning {{comparison of integers of different signs}}
John McCall323ed742010-05-06 08:58:33 +000090 ((short) a < (unsigned short) B) +
91 ((signed char) a < (unsigned char) B) +
John McCall5dbad3d2009-11-06 08:49:08 +000092
93 // (C,b)
94 (C == (unsigned long) b) +
95 (C == (unsigned int) b) +
96 (C == (unsigned short) b) +
97 (C == (unsigned char) b) +
98 ((long) C == b) +
99 ((int) C == b) +
100 ((short) C == b) +
101 ((signed char) C == b) +
102 ((long) C == (unsigned long) b) +
103 ((int) C == (unsigned int) b) +
104 ((short) C == (unsigned short) b) +
105 ((signed char) C == (unsigned char) b) +
106 (C < (unsigned long) b) +
107 (C < (unsigned int) b) +
108 (C < (unsigned short) b) +
109 (C < (unsigned char) b) +
110 ((long) C < b) +
111 ((int) C < b) +
112 ((short) C < b) +
113 ((signed char) C < b) +
114 ((long) C < (unsigned long) b) +
115 ((int) C < (unsigned int) b) +
116 ((short) C < (unsigned short) b) +
117 ((signed char) C < (unsigned char) b) +
118
119 // (a,C)
120 (a == (unsigned long) C) +
121 (a == (unsigned int) C) +
122 (a == (unsigned short) C) +
123 (a == (unsigned char) C) +
124 ((long) a == C) +
125 ((int) a == C) +
126 ((short) a == C) +
127 ((signed char) a == C) +
128 ((long) a == (unsigned long) C) +
129 ((int) a == (unsigned int) C) +
130 ((short) a == (unsigned short) C) +
131 ((signed char) a == (unsigned char) C) +
132 (a < (unsigned long) C) + // expected-warning {{comparison of integers of different signs}}
133 (a < (unsigned int) C) +
134 (a < (unsigned short) C) +
135 (a < (unsigned char) C) +
136 ((long) a < C) +
137 ((int) a < C) +
138 ((short) a < C) +
139 ((signed char) a < C) +
140 ((long) a < (unsigned long) C) + // expected-warning {{comparison of integers of different signs}}
141 ((int) a < (unsigned int) C) + // expected-warning {{comparison of integers of different signs}}
John McCall323ed742010-05-06 08:58:33 +0000142 ((short) a < (unsigned short) C) +
143 ((signed char) a < (unsigned char) C) +
John McCall5dbad3d2009-11-06 08:49:08 +0000144
145 // (0x80000,b)
146 (0x80000 == (unsigned long) b) +
147 (0x80000 == (unsigned int) b) +
148 (0x80000 == (unsigned short) b) +
149 (0x80000 == (unsigned char) b) +
150 ((long) 0x80000 == b) +
151 ((int) 0x80000 == b) +
152 ((short) 0x80000 == b) +
153 ((signed char) 0x80000 == b) +
154 ((long) 0x80000 == (unsigned long) b) +
155 ((int) 0x80000 == (unsigned int) b) +
156 ((short) 0x80000 == (unsigned short) b) +
157 ((signed char) 0x80000 == (unsigned char) b) +
158 (0x80000 < (unsigned long) b) +
159 (0x80000 < (unsigned int) b) +
160 (0x80000 < (unsigned short) b) +
161 (0x80000 < (unsigned char) b) +
162 ((long) 0x80000 < b) +
163 ((int) 0x80000 < b) +
164 ((short) 0x80000 < b) +
165 ((signed char) 0x80000 < b) +
166 ((long) 0x80000 < (unsigned long) b) +
167 ((int) 0x80000 < (unsigned int) b) +
168 ((short) 0x80000 < (unsigned short) b) +
169 ((signed char) 0x80000 < (unsigned char) b) +
170
171 // (a,0x80000)
172 (a == (unsigned long) 0x80000) +
173 (a == (unsigned int) 0x80000) +
174 (a == (unsigned short) 0x80000) +
175 (a == (unsigned char) 0x80000) +
176 ((long) a == 0x80000) +
177 ((int) a == 0x80000) +
178 ((short) a == 0x80000) +
179 ((signed char) a == 0x80000) +
180 ((long) a == (unsigned long) 0x80000) +
181 ((int) a == (unsigned int) 0x80000) +
182 ((short) a == (unsigned short) 0x80000) +
183 ((signed char) a == (unsigned char) 0x80000) +
184 (a < (unsigned long) 0x80000) + // expected-warning {{comparison of integers of different signs}}
185 (a < (unsigned int) 0x80000) +
186 (a < (unsigned short) 0x80000) +
187 (a < (unsigned char) 0x80000) +
188 ((long) a < 0x80000) +
189 ((int) a < 0x80000) +
190 ((short) a < 0x80000) +
191 ((signed char) a < 0x80000) +
192 ((long) a < (unsigned long) 0x80000) + // expected-warning {{comparison of integers of different signs}}
193 ((int) a < (unsigned int) 0x80000) + // expected-warning {{comparison of integers of different signs}}
John McCall323ed742010-05-06 08:58:33 +0000194 ((short) a < (unsigned short) 0x80000) +
195 ((signed char) a < (unsigned char) 0x80000) +
John McCall5dbad3d2009-11-06 08:49:08 +0000196
John McCallbc021702010-01-04 22:35:07 +0000197 // We should be able to avoid warning about this.
198 (b != (a < 4 ? 1 : 2)) +
199
John McCall5dbad3d2009-11-06 08:49:08 +0000200 10
201 ;
John McCall45aa4552009-11-05 00:40:04 +0000202}
203
Chris Lattner6365e3e2009-08-22 18:58:31 +0000204int equal(char *a, const char *b) {
Steve Naroff77878cc2007-08-27 04:08:11 +0000205 return a == b;
206}
Eli Friedman4e92acf2008-02-06 04:53:22 +0000207
208int arrays(char (*a)[5], char(*b)[10], char(*c)[5]) {
209 int d = (a == c);
210 return a == b; // expected-warning {{comparison of distinct pointer types}}
211}
Chris Lattner149f1382009-06-30 06:24:05 +0000212
Chris Lattner6365e3e2009-08-22 18:58:31 +0000213int pointers(int *a) {
Chris Lattner06c0f5b2009-08-23 00:03:44 +0000214 return a > 0; // expected-warning {{ordered comparison between pointer and zero ('int *' and 'int') is an extension}}
215 return a > 42; // expected-warning {{ordered comparison between pointer and integer ('int *' and 'int')}}
Chris Lattner149f1382009-06-30 06:24:05 +0000216 return a > (void *)0; // expected-warning {{comparison of distinct pointer types}}
217}
218
Eli Friedman3075e762009-08-23 00:27:47 +0000219int function_pointers(int (*a)(int), int (*b)(int), void (*c)(int)) {
Chris Lattner149f1382009-06-30 06:24:05 +0000220 return a > b; // expected-warning {{ordered comparison of function pointers}}
Douglas Gregord64fdd02010-06-08 19:50:34 +0000221 return function_pointers > function_pointers; // expected-warning {{self-comparison always evaluates to false}} expected-warning{{ordered comparison of function pointers}}
Eli Friedman3075e762009-08-23 00:27:47 +0000222 return a > c; // expected-warning {{comparison of distinct pointer types}}
Chris Lattner149f1382009-06-30 06:24:05 +0000223 return a == (void *) 0;
Eli Friedman3075e762009-08-23 00:27:47 +0000224 return a == (void *) 1; // expected-warning {{equality comparison between function pointer and void pointer}}
Chris Lattner149f1382009-06-30 06:24:05 +0000225}
Douglas Gregorf9334372009-07-06 20:14:23 +0000226
Eli Friedman3075e762009-08-23 00:27:47 +0000227int void_pointers(void* foo) {
228 return foo == (void*) 0;
229 return foo == (void*) 1;
Douglas Gregorf9334372009-07-06 20:14:23 +0000230}
John McCall842aef82009-12-09 09:09:27 +0000231
Douglas Gregord64fdd02010-06-08 19:50:34 +0000232
John McCall842aef82009-12-09 09:09:27 +0000233int test1(int i) {
234 enum en { zero };
235 return i > zero;
236}
John McCallf2370c92010-01-06 05:24:50 +0000237
238// PR5937
239int test2(int i32) {
240 struct foo {
241 unsigned int u8 : 8;
242 unsigned long long u31 : 31;
243 unsigned long long u32 : 32;
244 unsigned long long u63 : 63;
245 unsigned long long u64 : 64;
246 } *x;
247
248 if (x->u8 == i32) { // comparison in int32, exact
249 return 0;
250 } else if (x->u31 == i32) { // comparison in int32, exact
251 return 1;
252 } else if (x->u32 == i32) { // expected-warning {{comparison of integers of different signs}}
253 return 2;
254 } else if (x->u63 == i32) { // comparison in uint64, exact because ==
255 return 3;
256 } else if (x->u64 == i32) { // expected-warning {{comparison of integers of different signs}}
257 return 4;
258 } else {
259 return 5;
260 }
261}
262
263// PR5887
264void test3() {
265 unsigned short x, y;
266 unsigned int z;
267 if ((x > y ? x : y) > z)
268 (void) 0;
269}
John McCall0acc3112010-01-06 22:57:21 +0000270
271// PR5961
272extern char *ptr4;
273void test4() {
274 long value;
275 if (value < (unsigned long) &ptr4) // expected-warning {{comparison of integers of different signs}}
276 return;
277}
John McCalld1b47bf2010-03-11 19:43:18 +0000278
279// PR4807
280int test5(unsigned int x) {
281 return (x < 0) // expected-warning {{comparison of unsigned expression < 0 is always false}}
282 && (0 > x) // expected-warning {{comparison of 0 > unsigned expression is always false}}
283 && (x >= 0) // expected-warning {{comparison of unsigned expression >= 0 is always true}}
284 && (0 <= x); // expected-warning {{comparison of 0 <= unsigned expression is always true}}
285}
John McCall3aae6092010-04-07 01:14:35 +0000286
287int test6(unsigned i, unsigned power) {
288 unsigned x = (i < (1 << power) ? i : 0);
289 return x != 3 ? 1 << power : i;
290}
Ted Kremeneke3b159c2010-09-23 21:43:44 +0000291
292// <rdar://problem/8414119> enum >= (enum)0 comparison should not generate any warnings
293enum rdar8414119_Vals { X, Y, Z };
294#define ZERO 0
295#define CHECK(x) (x >= X)
296void rdar8414119_foo(enum rdar8414119_Vals v) {
297 if (CHECK(v)) // no-warning
298 return;
299 if (v >= X) // no-warning
300 return;
301}
302int rdar8414119_bar(unsigned x) {
303 return x >= ZERO; // no-warning
304}
305#undef ZERO
306#undef CHECK
307
John McCall372e1032010-10-06 00:25:24 +0000308int rdar8511238() {
309 enum A { A_foo, A_bar };
310 enum A a;
311 if (a < 0) // expected-warning {{comparison of unsigned enum expression < 0 is always false}}
312 return 0;
313 return 20;
314}