blob: feb1ccb9a20dddc5bcc2a36f6ec4ae3fd35cc58d [file] [log] [blame]
John McCall5dbad3d2009-11-06 08:49:08 +00001// Force x86-64 because some of our heuristics are actually based
2// on integer sizes.
3
Eli Friedmand87de7b2012-11-30 23:09:29 +00004// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -pedantic -verify -Wsign-compare -std=c++11 %s
John McCallb13c87f2009-11-05 09:23:39 +00005
6int test0(long a, unsigned long b) {
John McCall5dbad3d2009-11-06 08:49:08 +00007 enum EnumA {A};
8 enum EnumB {B};
9 enum EnumC {C = 0x10000};
10 return
11 // (a,b)
12 (a == (unsigned long) b) + // expected-warning {{comparison of integers of different signs}}
13 (a == (unsigned int) b) +
14 (a == (unsigned short) b) +
15 (a == (unsigned char) b) +
16 ((long) a == b) + // expected-warning {{comparison of integers of different signs}}
17 ((int) a == b) + // expected-warning {{comparison of integers of different signs}}
18 ((short) a == b) + // expected-warning {{comparison of integers of different signs}}
19 ((signed char) a == b) + // expected-warning {{comparison of integers of different signs}}
20 ((long) a == (unsigned long) b) + // expected-warning {{comparison of integers of different signs}}
21 ((int) a == (unsigned int) b) + // expected-warning {{comparison of integers of different signs}}
John McCall323ed742010-05-06 08:58:33 +000022 ((short) a == (unsigned short) b) +
23 ((signed char) a == (unsigned char) b) +
John McCall5dbad3d2009-11-06 08:49:08 +000024 (a < (unsigned long) b) + // expected-warning {{comparison of integers of different signs}}
25 (a < (unsigned int) b) +
26 (a < (unsigned short) b) +
27 (a < (unsigned char) b) +
28 ((long) a < b) + // expected-warning {{comparison of integers of different signs}}
29 ((int) a < b) + // expected-warning {{comparison of integers of different signs}}
30 ((short) a < b) + // expected-warning {{comparison of integers of different signs}}
31 ((signed char) a < b) + // expected-warning {{comparison of integers of different signs}}
32 ((long) a < (unsigned long) b) + // expected-warning {{comparison of integers of different signs}}
33 ((int) a < (unsigned int) b) + // expected-warning {{comparison of integers of different signs}}
John McCall323ed742010-05-06 08:58:33 +000034 ((short) a < (unsigned short) b) +
35 ((signed char) a < (unsigned char) b) +
John McCallb13c87f2009-11-05 09:23:39 +000036
John McCall5dbad3d2009-11-06 08:49:08 +000037 // (A,b)
38 (A == (unsigned long) b) +
39 (A == (unsigned int) b) +
40 (A == (unsigned short) b) +
41 (A == (unsigned char) b) +
42 ((long) A == b) +
43 ((int) A == b) +
44 ((short) A == b) +
45 ((signed char) A == b) +
46 ((long) A == (unsigned long) b) +
47 ((int) A == (unsigned int) b) +
48 ((short) A == (unsigned short) b) +
49 ((signed char) A == (unsigned char) b) +
50 (A < (unsigned long) b) +
51 (A < (unsigned int) b) +
John McCall842aef82009-12-09 09:09:27 +000052 (A < (unsigned short) b) +
53 (A < (unsigned char) b) +
John McCall5dbad3d2009-11-06 08:49:08 +000054 ((long) A < b) +
55 ((int) A < b) +
56 ((short) A < b) +
57 ((signed char) A < b) +
58 ((long) A < (unsigned long) b) +
59 ((int) A < (unsigned int) b) +
60 ((short) A < (unsigned short) b) +
61 ((signed char) A < (unsigned char) b) +
62
63 // (a,B)
64 (a == (unsigned long) B) +
65 (a == (unsigned int) B) +
66 (a == (unsigned short) B) +
67 (a == (unsigned char) B) +
68 ((long) a == B) +
69 ((int) a == B) +
70 ((short) a == B) +
71 ((signed char) a == B) +
72 ((long) a == (unsigned long) B) +
73 ((int) a == (unsigned int) B) +
74 ((short) a == (unsigned short) B) +
75 ((signed char) a == (unsigned char) B) +
76 (a < (unsigned long) B) + // expected-warning {{comparison of integers of different signs}}
77 (a < (unsigned int) B) +
78 (a < (unsigned short) B) +
79 (a < (unsigned char) B) +
80 ((long) a < B) +
John McCall842aef82009-12-09 09:09:27 +000081 ((int) a < B) +
82 ((short) a < B) +
83 ((signed char) a < B) +
John McCall5dbad3d2009-11-06 08:49:08 +000084 ((long) a < (unsigned long) B) + // expected-warning {{comparison of integers of different signs}}
85 ((int) a < (unsigned int) B) + // expected-warning {{comparison of integers of different signs}}
John McCall323ed742010-05-06 08:58:33 +000086 ((short) a < (unsigned short) B) +
87 ((signed char) a < (unsigned char) B) +
John McCall5dbad3d2009-11-06 08:49:08 +000088
89 // (C,b)
90 (C == (unsigned long) b) +
91 (C == (unsigned int) b) +
Ted Kremenek7adf3a92013-03-15 21:50:10 +000092 (C == (unsigned short) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned short' is always false}}
93 (C == (unsigned char) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned char' is always false}}
John McCall5dbad3d2009-11-06 08:49:08 +000094 ((long) C == b) +
95 ((int) C == b) +
96 ((short) C == b) +
97 ((signed char) C == b) +
98 ((long) C == (unsigned long) b) +
99 ((int) C == (unsigned int) b) +
100 ((short) C == (unsigned short) b) +
101 ((signed char) C == (unsigned char) b) +
102 (C < (unsigned long) b) +
103 (C < (unsigned int) b) +
Ted Kremenek7adf3a92013-03-15 21:50:10 +0000104 (C < (unsigned short) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned short' is always false}}
105 (C < (unsigned char) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned char' is always false}}
John McCall5dbad3d2009-11-06 08:49:08 +0000106 ((long) C < b) +
107 ((int) C < b) +
108 ((short) C < b) +
109 ((signed char) C < b) +
110 ((long) C < (unsigned long) b) +
111 ((int) C < (unsigned int) b) +
112 ((short) C < (unsigned short) b) +
113 ((signed char) C < (unsigned char) b) +
114
115 // (a,C)
116 (a == (unsigned long) C) +
117 (a == (unsigned int) C) +
118 (a == (unsigned short) C) +
119 (a == (unsigned char) C) +
120 ((long) a == C) +
121 ((int) a == C) +
Ted Kremenek7adf3a92013-03-15 21:50:10 +0000122 ((short) a == C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'short' is always false}}
123 ((signed char) a == C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'signed char' is always false}}
John McCall5dbad3d2009-11-06 08:49:08 +0000124 ((long) a == (unsigned long) C) +
125 ((int) a == (unsigned int) C) +
126 ((short) a == (unsigned short) C) +
127 ((signed char) a == (unsigned char) C) +
128 (a < (unsigned long) C) + // expected-warning {{comparison of integers of different signs}}
129 (a < (unsigned int) C) +
130 (a < (unsigned short) C) +
131 (a < (unsigned char) C) +
132 ((long) a < C) +
John McCall842aef82009-12-09 09:09:27 +0000133 ((int) a < C) +
Ted Kremenek7adf3a92013-03-15 21:50:10 +0000134 ((short) a < C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'short' is always true}}
135 ((signed char) a < C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'signed char' is always true}}
John McCall5dbad3d2009-11-06 08:49:08 +0000136 ((long) a < (unsigned long) C) + // expected-warning {{comparison of integers of different signs}}
137 ((int) a < (unsigned int) C) + // expected-warning {{comparison of integers of different signs}}
John McCall323ed742010-05-06 08:58:33 +0000138 ((short) a < (unsigned short) C) +
139 ((signed char) a < (unsigned char) C) +
John McCall5dbad3d2009-11-06 08:49:08 +0000140
141 // (0x80000,b)
142 (0x80000 == (unsigned long) b) +
143 (0x80000 == (unsigned int) b) +
Fariborz Jahaniana193f202012-09-20 19:36:41 +0000144 (0x80000 == (unsigned short) b) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned short' is always false}}
145 (0x80000 == (unsigned char) b) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned char' is always false}}
John McCall5dbad3d2009-11-06 08:49:08 +0000146 ((long) 0x80000 == b) +
147 ((int) 0x80000 == b) +
148 ((short) 0x80000 == b) +
149 ((signed char) 0x80000 == b) +
150 ((long) 0x80000 == (unsigned long) b) +
151 ((int) 0x80000 == (unsigned int) b) +
152 ((short) 0x80000 == (unsigned short) b) +
153 ((signed char) 0x80000 == (unsigned char) b) +
154 (0x80000 < (unsigned long) b) +
155 (0x80000 < (unsigned int) b) +
Fariborz Jahaniana193f202012-09-20 19:36:41 +0000156 (0x80000 < (unsigned short) b) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned short' is always false}}
157 (0x80000 < (unsigned char) b) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned char' is always false}}
John McCall5dbad3d2009-11-06 08:49:08 +0000158 ((long) 0x80000 < b) +
159 ((int) 0x80000 < b) +
160 ((short) 0x80000 < b) +
161 ((signed char) 0x80000 < b) +
162 ((long) 0x80000 < (unsigned long) b) +
163 ((int) 0x80000 < (unsigned int) b) +
164 ((short) 0x80000 < (unsigned short) b) +
165 ((signed char) 0x80000 < (unsigned char) b) +
166
167 // (a,0x80000)
168 (a == (unsigned long) 0x80000) +
169 (a == (unsigned int) 0x80000) +
170 (a == (unsigned short) 0x80000) +
171 (a == (unsigned char) 0x80000) +
172 ((long) a == 0x80000) +
173 ((int) a == 0x80000) +
Fariborz Jahaniana193f202012-09-20 19:36:41 +0000174 ((short) a == 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'short' is always false}}
175 ((signed char) a == 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'signed char' is always false}}
John McCall5dbad3d2009-11-06 08:49:08 +0000176 ((long) a == (unsigned long) 0x80000) +
177 ((int) a == (unsigned int) 0x80000) +
178 ((short) a == (unsigned short) 0x80000) +
179 ((signed char) a == (unsigned char) 0x80000) +
180 (a < (unsigned long) 0x80000) + // expected-warning {{comparison of integers of different signs}}
181 (a < (unsigned int) 0x80000) +
182 (a < (unsigned short) 0x80000) +
183 (a < (unsigned char) 0x80000) +
184 ((long) a < 0x80000) +
185 ((int) a < 0x80000) +
Fariborz Jahaniana193f202012-09-20 19:36:41 +0000186 ((short) a < 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'short' is always true}}
187 ((signed char) a < 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'signed char' is always true}}
John McCall5dbad3d2009-11-06 08:49:08 +0000188 ((long) a < (unsigned long) 0x80000) + // expected-warning {{comparison of integers of different signs}}
189 ((int) a < (unsigned int) 0x80000) + // expected-warning {{comparison of integers of different signs}}
John McCall323ed742010-05-06 08:58:33 +0000190 ((short) a < (unsigned short) 0x80000) +
191 ((signed char) a < (unsigned char) 0x80000) +
John McCall5dbad3d2009-11-06 08:49:08 +0000192
193 10
194 ;
John McCallb13c87f2009-11-05 09:23:39 +0000195}
John McCall842aef82009-12-09 09:09:27 +0000196
197int test1(int i) {
198 enum en { zero };
199 return i > zero;
200}
Douglas Gregor6e5122c2010-06-15 21:38:40 +0000201
202enum E { e };
203void test2(int i, void *vp) {
204 if (test1 == vp) { } // expected-warning{{equality comparison between function pointer and void pointer}}
205 if (test1 == e) { } // expected-error{{comparison between pointer and integer}}
206 if (vp < 0) { }
207 if (test1 < e) { } // expected-error{{comparison between pointer and integer}}
208}
Douglas Gregor3e026e32011-02-19 22:34:59 +0000209
210// PR7536
211static const unsigned int kMax = 0;
212int pr7536() {
213 return (kMax > 0);
214}
Richard Trieu52541612011-07-21 02:46:28 +0000215
216// -Wsign-compare should not warn when ?: operands have different signedness.
217// This will be caught by -Wsign-conversion
218void test3() {
219 unsigned long a;
220 signed long b;
221 (void) (true ? a : b);
222 (void) (true ? (unsigned int)a : (signed int)b);
223 (void) (true ? b : a);
224 (void) (true ? (unsigned char)b : (signed char)a);
225}
Richard Trieu526e6272012-11-14 22:50:24 +0000226
227// Test comparison of short to unsigned. If tautological compare does not
228// trigger, then the signed comparision warning will.
229void test4(short s) {
230 // A is max short plus 1. All zero and positive shorts are smaller than it.
231 // All negative shorts are cast towards the max unsigned range. Relation
232 // comparisons are possible, but equality comparisons are tautological.
233 const unsigned A = 32768;
234 void (s < A); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
235 void (s > A); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
236 void (s <= A); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
237 void (s >= A); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
238
239 void (s == A); // expected-warning{{comparison of constant 32768 with expression of type 'short' is always false}}
240 void (s != A); // expected-warning{{comparison of constant 32768 with expression of type 'short' is always true}}
241
242 // When negative one is converted to an unsigned value, it becomes the max
243 // unsigned. Likewise, a negative one short can also be converted to max
244 // unsigned.
245 const unsigned B = -1;
246 void (s < B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
247 void (s > B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
248 void (s <= B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
249 void (s >= B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
250 void (s == B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
251 void (s != B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
252
253}
254
255void test5(bool b) {
256 (void) (b < -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always false}}
257 (void) (b > -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always true}}
258 (void) (b == -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always false}}
259 (void) (b != -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always true}}
260 (void) (b <= -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always false}}
261 (void) (b >= -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always true}}
262
263 (void) (b < -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always false}}
264 (void) (b > -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always true}}
265 (void) (b == -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always false}}
266 (void) (b != -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always true}}
267 (void) (b <= -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always false}}
268 (void) (b >= -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always true}}
269
270 (void) (b < 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always true}}
271 (void) (b > 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always false}}
272 (void) (b == 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always false}}
273 (void) (b != 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always true}}
274 (void) (b <= 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always true}}
275 (void) (b >= 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always false}}
276
277 (void) (b < 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always true}}
278 (void) (b > 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always false}}
279 (void) (b == 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always false}}
280 (void) (b != 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always true}}
281 (void) (b <= 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always true}}
282 (void) (b >= 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always false}}
283}
284
285void test6(signed char sc) {
286 (void)(sc < 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}}
287 (void)(sc > 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}}
288 (void)(sc <= 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}}
289 (void)(sc >= 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}}
290 (void)(sc == 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}}
291 (void)(sc != 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}}
292
293 (void)(200 < sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}}
294 (void)(200 > sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}}
295 (void)(200 <= sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}}
296 (void)(200 >= sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}}
297 (void)(200 == sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}}
298 (void)(200 != sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}}
299}
300
301// Test many signedness combinations.
302void test7(unsigned long other) {
303 // Common unsigned, other unsigned, constant unsigned
304 (void)((unsigned)other != (unsigned long)(0x1ffffffff)); // expected-warning{{true}}
305 (void)((unsigned)other != (unsigned long)(0xffffffff));
306 (void)((unsigned long)other != (unsigned)(0x1ffffffff));
307 (void)((unsigned long)other != (unsigned)(0xffffffff));
308
309 // Common unsigned, other signed, constant unsigned
310 (void)((int)other != (unsigned long)(0xffffffffffffffff)); // expected-warning{{different signs}}
311 (void)((int)other != (unsigned long)(0x00000000ffffffff)); // expected-warning{{true}}
312 (void)((int)other != (unsigned long)(0x000000000fffffff));
313 (void)((int)other < (unsigned long)(0x00000000ffffffff)); // expected-warning{{different signs}}
Richard Trieu5d1cf4f2012-11-15 03:43:50 +0000314 (void)((int)other == (unsigned)(0x800000000));
Richard Trieu526e6272012-11-14 22:50:24 +0000315
316 // Common unsigned, other unsigned, constant signed
317 (void)((unsigned long)other != (int)(0xffffffff)); // expected-warning{{different signs}}
318
319 // Common unsigned, other signed, constant signed
320 // Should not be possible as the common type should also be signed.
321
322 // Common signed, other signed, constant signed
323 (void)((int)other != (long)(0xffffffff)); // expected-warning{{true}}
324 (void)((int)other != (long)(0xffffffff00000000)); // expected-warning{{true}}
325 (void)((int)other != (long)(0xfffffff));
326 (void)((int)other != (long)(0xfffffffff0000000));
327
328 // Common signed, other signed, constant unsigned
329 (void)((int)other != (unsigned char)(0xffff));
330 (void)((int)other != (unsigned char)(0xff));
331
332 // Common signed, other unsigned, constant signed
333 (void)((unsigned char)other != (int)(0xff));
334 (void)((unsigned char)other != (int)(0xffff)); // expected-warning{{true}}
335
336 // Common signed, other unsigned, constant unsigned
337 (void)((unsigned char)other != (unsigned short)(0xff));
338 (void)((unsigned char)other != (unsigned short)(0x100)); // expected-warning{{true}}
339 (void)((unsigned short)other != (unsigned char)(0xff));
340}
Richard Trieu8f50b242012-11-16 01:32:40 +0000341
342void test8(int x) {
343 enum E {
344 Negative = -1,
345 Positive = 1
346 };
347
348 (void)((E)x == 1);
349 (void)((E)x == -1);
350}
Eli Friedmand87de7b2012-11-30 23:09:29 +0000351
352void test9(int x) {
353 enum E : int {
354 Positive = 1
355 };
356 (void)((E)x == 1);
357}