blob: a591ac0eef832c89fe8509aa442041e0c150c9ab [file] [log] [blame]
Daniel Dunbara268fc02011-10-11 18:20:10 +00001// RUN: %clang_cc1 -fsyntax-only -verify -Wconversion \
2// RUN: -nostdsysteminc -nobuiltininc -isystem %S/Inputs \
3// RUN: -triple x86_64-apple-darwin %s -Wno-unreachable-code
John McCalld5c376e2009-11-07 03:30:38 +00004
John McCall05efad52010-02-11 22:45:16 +00005#include <conversion.h>
John McCallfb6289a2010-02-11 18:52:49 +00006
John McCalld5c376e2009-11-07 03:30:38 +00007#define BIG 0x7f7f7f7f7f7f7f7fL
8
9void test0(char c, short s, int i, long l, long long ll) {
10 c = c;
Douglas Gregor27f46ee2010-07-09 18:18:35 +000011 c = s; // expected-warning {{implicit conversion loses integer precision}}
12 c = i; // expected-warning {{implicit conversion loses integer precision}}
13 c = l; // expected-warning {{implicit conversion loses integer precision}}
John McCalld5c376e2009-11-07 03:30:38 +000014 s = c;
15 s = s;
Douglas Gregor27f46ee2010-07-09 18:18:35 +000016 s = i; // expected-warning {{implicit conversion loses integer precision}}
17 s = l; // expected-warning {{implicit conversion loses integer precision}}
John McCalld5c376e2009-11-07 03:30:38 +000018 i = c;
19 i = s;
20 i = i;
Douglas Gregor27f46ee2010-07-09 18:18:35 +000021 i = l; // expected-warning {{implicit conversion loses integer precision}}
John McCalld5c376e2009-11-07 03:30:38 +000022 l = c;
23 l = s;
24 l = i;
25 l = l;
26
27 c = (char) 0;
28 c = (short) 0;
29 c = (int) 0;
30 c = (long) 0;
31 s = (char) 0;
32 s = (short) 0;
33 s = (int) 0;
34 s = (long) 0;
35 i = (char) 0;
36 i = (short) 0;
37 i = (int) 0;
38 i = (long) 0;
39 l = (char) 0;
40 l = (short) 0;
41 l = (int) 0;
42 l = (long) 0;
43
44 c = (char) BIG;
John McCall091f23f2010-11-09 22:22:12 +000045 c = (short) BIG; // expected-warning {{implicit conversion from 'short' to 'char' changes value}}
46 c = (int) BIG; // expected-warning {{implicit conversion from 'int' to 'char' changes value}}
47 c = (long) BIG; // expected-warning {{implicit conversion from 'long' to 'char' changes value}}
John McCalld5c376e2009-11-07 03:30:38 +000048 s = (char) BIG;
49 s = (short) BIG;
John McCall091f23f2010-11-09 22:22:12 +000050 s = (int) BIG; // expected-warning {{implicit conversion from 'int' to 'short' changes value}}
51 s = (long) BIG; // expected-warning {{implicit conversion from 'long' to 'short' changes value}}
John McCalld5c376e2009-11-07 03:30:38 +000052 i = (char) BIG;
53 i = (short) BIG;
54 i = (int) BIG;
John McCall091f23f2010-11-09 22:22:12 +000055 i = (long) BIG; // expected-warning {{implicit conversion from 'long' to 'int' changes value}}
John McCalld5c376e2009-11-07 03:30:38 +000056 l = (char) BIG;
57 l = (short) BIG;
58 l = (int) BIG;
59 l = (long) BIG;
60}
61
62char test1(long long ll) {
Douglas Gregor27f46ee2010-07-09 18:18:35 +000063 return (long long) ll; // expected-warning {{implicit conversion loses integer precision}}
Ted Kremenek5e745da2011-10-22 02:37:33 +000064}
65char test1_a(long long ll) {
Douglas Gregor27f46ee2010-07-09 18:18:35 +000066 return (long) ll; // expected-warning {{implicit conversion loses integer precision}}
Ted Kremenek5e745da2011-10-22 02:37:33 +000067}
68char test1_b(long long ll) {
Douglas Gregor27f46ee2010-07-09 18:18:35 +000069 return (int) ll; // expected-warning {{implicit conversion loses integer precision}}
Ted Kremenek5e745da2011-10-22 02:37:33 +000070}
71char test1_c(long long ll) {
Douglas Gregor27f46ee2010-07-09 18:18:35 +000072 return (short) ll; // expected-warning {{implicit conversion loses integer precision}}
Ted Kremenek5e745da2011-10-22 02:37:33 +000073}
74char test1_d(long long ll) {
John McCalld5c376e2009-11-07 03:30:38 +000075 return (char) ll;
Ted Kremenek5e745da2011-10-22 02:37:33 +000076}
77char test1_e(long long ll) {
John McCall091f23f2010-11-09 22:22:12 +000078 return (long long) BIG; // expected-warning {{implicit conversion from 'long long' to 'char' changes value}}
Ted Kremenek5e745da2011-10-22 02:37:33 +000079}
80char test1_f(long long ll) {
John McCall091f23f2010-11-09 22:22:12 +000081 return (long) BIG; // expected-warning {{implicit conversion from 'long' to 'char' changes value}}
Ted Kremenek5e745da2011-10-22 02:37:33 +000082}
83char test1_g(long long ll) {
John McCall091f23f2010-11-09 22:22:12 +000084 return (int) BIG; // expected-warning {{implicit conversion from 'int' to 'char' changes value}}
Ted Kremenek5e745da2011-10-22 02:37:33 +000085}
86char test1_h(long long ll) {
John McCall091f23f2010-11-09 22:22:12 +000087 return (short) BIG; // expected-warning {{implicit conversion from 'short' to 'char' changes value}}
Ted Kremenek5e745da2011-10-22 02:37:33 +000088}
89char test1_i(long long ll) {
John McCalld5c376e2009-11-07 03:30:38 +000090 return (char) BIG;
91}
92
93short test2(long long ll) {
Douglas Gregor27f46ee2010-07-09 18:18:35 +000094 return (long long) ll; // expected-warning {{implicit conversion loses integer precision}}
Ted Kremenek5e745da2011-10-22 02:37:33 +000095}
96short test2_a(long long ll) {
Douglas Gregor27f46ee2010-07-09 18:18:35 +000097 return (long) ll; // expected-warning {{implicit conversion loses integer precision}}
Ted Kremenek5e745da2011-10-22 02:37:33 +000098}
99short test2_b(long long ll) {
Douglas Gregor27f46ee2010-07-09 18:18:35 +0000100 return (int) ll; // expected-warning {{implicit conversion loses integer precision}}
Ted Kremenek5e745da2011-10-22 02:37:33 +0000101}
102short test2_c(long long ll) {
John McCalld5c376e2009-11-07 03:30:38 +0000103 return (short) ll;
Ted Kremenek5e745da2011-10-22 02:37:33 +0000104}
105short test2_d(long long ll) {
John McCalld5c376e2009-11-07 03:30:38 +0000106 return (char) ll;
Ted Kremenek5e745da2011-10-22 02:37:33 +0000107}
108short test2_e(long long ll) {
John McCall091f23f2010-11-09 22:22:12 +0000109 return (long long) BIG; // expected-warning {{implicit conversion from 'long long' to 'short' changes value}}
Ted Kremenek5e745da2011-10-22 02:37:33 +0000110}
111short test2_f(long long ll) {
John McCall091f23f2010-11-09 22:22:12 +0000112 return (long) BIG; // expected-warning {{implicit conversion from 'long' to 'short' changes value}}
Ted Kremenek5e745da2011-10-22 02:37:33 +0000113}
114short test2_g(long long ll) {
John McCall091f23f2010-11-09 22:22:12 +0000115 return (int) BIG; // expected-warning {{implicit conversion from 'int' to 'short' changes value}}
Ted Kremenek5e745da2011-10-22 02:37:33 +0000116}
117short test2_h(long long ll) {
John McCalld5c376e2009-11-07 03:30:38 +0000118 return (short) BIG;
Ted Kremenek5e745da2011-10-22 02:37:33 +0000119}
120short test2_i(long long ll) {
John McCalld5c376e2009-11-07 03:30:38 +0000121 return (char) BIG;
122}
123
124int test3(long long ll) {
Douglas Gregor27f46ee2010-07-09 18:18:35 +0000125 return (long long) ll; // expected-warning {{implicit conversion loses integer precision}}
Ted Kremenek5e745da2011-10-22 02:37:33 +0000126}
127int test3_b(long long ll) {
Douglas Gregor27f46ee2010-07-09 18:18:35 +0000128 return (long) ll; // expected-warning {{implicit conversion loses integer precision}}
Ted Kremenek5e745da2011-10-22 02:37:33 +0000129}
130int test3_c(long long ll) {
John McCalld5c376e2009-11-07 03:30:38 +0000131 return (int) ll;
Ted Kremenek5e745da2011-10-22 02:37:33 +0000132}
133int test3_d(long long ll) {
John McCalld5c376e2009-11-07 03:30:38 +0000134 return (short) ll;
Ted Kremenek5e745da2011-10-22 02:37:33 +0000135}
136int test3_e(long long ll) {
John McCalld5c376e2009-11-07 03:30:38 +0000137 return (char) ll;
Ted Kremenek5e745da2011-10-22 02:37:33 +0000138}
139int test3_f(long long ll) {
John McCall091f23f2010-11-09 22:22:12 +0000140 return (long long) BIG; // expected-warning {{implicit conversion from 'long long' to 'int' changes value}}
Ted Kremenek5e745da2011-10-22 02:37:33 +0000141}
142int test3_g(long long ll) {
John McCall091f23f2010-11-09 22:22:12 +0000143 return (long) BIG; // expected-warning {{implicit conversion from 'long' to 'int' changes value}}
Ted Kremenek5e745da2011-10-22 02:37:33 +0000144}
145int test3_h(long long ll) {
John McCalld5c376e2009-11-07 03:30:38 +0000146 return (int) BIG;
Ted Kremenek5e745da2011-10-22 02:37:33 +0000147}
148int test3_i(long long ll) {
John McCalld5c376e2009-11-07 03:30:38 +0000149 return (short) BIG;
Ted Kremenek5e745da2011-10-22 02:37:33 +0000150}
151int test3_j(long long ll) {
John McCalld5c376e2009-11-07 03:30:38 +0000152 return (char) BIG;
153}
154
155long test4(long long ll) {
156 return (long long) ll;
Ted Kremenek5e745da2011-10-22 02:37:33 +0000157}
158long test4_a(long long ll) {
John McCalld5c376e2009-11-07 03:30:38 +0000159 return (long) ll;
Ted Kremenek5e745da2011-10-22 02:37:33 +0000160}
161long test4_b(long long ll) {
John McCalld5c376e2009-11-07 03:30:38 +0000162 return (int) ll;
Ted Kremenek5e745da2011-10-22 02:37:33 +0000163}
164long test4_c(long long ll) {
John McCalld5c376e2009-11-07 03:30:38 +0000165 return (short) ll;
Ted Kremenek5e745da2011-10-22 02:37:33 +0000166}
167long test4_d(long long ll) {
John McCalld5c376e2009-11-07 03:30:38 +0000168 return (char) ll;
Ted Kremenek5e745da2011-10-22 02:37:33 +0000169}
170long test4_e(long long ll) {
John McCalld5c376e2009-11-07 03:30:38 +0000171 return (long long) BIG;
Ted Kremenek5e745da2011-10-22 02:37:33 +0000172}
173long test4_f(long long ll) {
John McCalld5c376e2009-11-07 03:30:38 +0000174 return (long) BIG;
Ted Kremenek5e745da2011-10-22 02:37:33 +0000175}
176long test4_g(long long ll) {
John McCalld5c376e2009-11-07 03:30:38 +0000177 return (int) BIG;
Ted Kremenek5e745da2011-10-22 02:37:33 +0000178}
179long test4_h(long long ll) {
John McCalld5c376e2009-11-07 03:30:38 +0000180 return (short) BIG;
Ted Kremenek5e745da2011-10-22 02:37:33 +0000181}
182long test4_i(long long ll) {
John McCalld5c376e2009-11-07 03:30:38 +0000183 return (char) BIG;
184}
185
186long long test5(long long ll) {
187 return (long long) ll;
188 return (long) ll;
189 return (int) ll;
190 return (short) ll;
191 return (char) ll;
192 return (long long) BIG;
193 return (long) BIG;
194 return (int) BIG;
195 return (short) BIG;
196 return (char) BIG;
197}
198
199void takes_char(char);
200void takes_short(short);
201void takes_int(int);
202void takes_long(long);
203void takes_longlong(long long);
204void takes_float(float);
205void takes_double(double);
206void takes_longdouble(long double);
207
208void test6(char v) {
209 takes_char(v);
210 takes_short(v);
211 takes_int(v);
212 takes_long(v);
213 takes_longlong(v);
214 takes_float(v);
215 takes_double(v);
216 takes_longdouble(v);
217}
218
219void test7(short v) {
Douglas Gregor27f46ee2010-07-09 18:18:35 +0000220 takes_char(v); // expected-warning {{implicit conversion loses integer precision}}
John McCalld5c376e2009-11-07 03:30:38 +0000221 takes_short(v);
222 takes_int(v);
223 takes_long(v);
224 takes_longlong(v);
225 takes_float(v);
226 takes_double(v);
227 takes_longdouble(v);
228}
229
230void test8(int v) {
Douglas Gregor27f46ee2010-07-09 18:18:35 +0000231 takes_char(v); // expected-warning {{implicit conversion loses integer precision}}
232 takes_short(v); // expected-warning {{implicit conversion loses integer precision}}
John McCalld5c376e2009-11-07 03:30:38 +0000233 takes_int(v);
234 takes_long(v);
235 takes_longlong(v);
236 takes_float(v);
237 takes_double(v);
238 takes_longdouble(v);
239}
240
241void test9(long v) {
Douglas Gregor27f46ee2010-07-09 18:18:35 +0000242 takes_char(v); // expected-warning {{implicit conversion loses integer precision}}
243 takes_short(v); // expected-warning {{implicit conversion loses integer precision}}
244 takes_int(v); // expected-warning {{implicit conversion loses integer precision}}
John McCalld5c376e2009-11-07 03:30:38 +0000245 takes_long(v);
246 takes_longlong(v);
247 takes_float(v);
248 takes_double(v);
249 takes_longdouble(v);
250}
251
252void test10(long long v) {
Douglas Gregor27f46ee2010-07-09 18:18:35 +0000253 takes_char(v); // expected-warning {{implicit conversion loses integer precision}}
254 takes_short(v); // expected-warning {{implicit conversion loses integer precision}}
255 takes_int(v); // expected-warning {{implicit conversion loses integer precision}}
John McCalld5c376e2009-11-07 03:30:38 +0000256 takes_long(v);
257 takes_longlong(v);
258 takes_float(v);
259 takes_double(v);
260 takes_longdouble(v);
261}
262
263void test11(float v) {
Douglas Gregor27f46ee2010-07-09 18:18:35 +0000264 takes_char(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
265 takes_short(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
266 takes_int(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
267 takes_long(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
268 takes_longlong(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
John McCalld5c376e2009-11-07 03:30:38 +0000269 takes_float(v);
270 takes_double(v);
271 takes_longdouble(v);
272}
273
274void test12(double v) {
Douglas Gregor27f46ee2010-07-09 18:18:35 +0000275 takes_char(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
276 takes_short(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
277 takes_int(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
278 takes_long(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
279 takes_longlong(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
280 takes_float(v); // expected-warning {{implicit conversion loses floating-point precision}}
John McCalld5c376e2009-11-07 03:30:38 +0000281 takes_double(v);
282 takes_longdouble(v);
283}
284
285void test13(long double v) {
Douglas Gregor27f46ee2010-07-09 18:18:35 +0000286 takes_char(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
287 takes_short(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
288 takes_int(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
289 takes_long(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
290 takes_longlong(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
291 takes_float(v); // expected-warning {{implicit conversion loses floating-point precision}}
292 takes_double(v); // expected-warning {{implicit conversion loses floating-point precision}}
John McCalld5c376e2009-11-07 03:30:38 +0000293 takes_longdouble(v);
294}
295
296void test14(long l) {
297 // Fine because of the boolean whitelist.
298 char c;
299 c = (l == 4);
300 c = ((l <= 4) && (l >= 0));
301 c = ((l <= 4) && (l >= 0)) || (l > 20);
302}
John McCalle8babd12009-11-07 08:15:46 +0000303
304void test15(char c) {
305 c = c + 1 + c * 2;
Douglas Gregor27f46ee2010-07-09 18:18:35 +0000306 c = (short) c + 1 + c * 2; // expected-warning {{implicit conversion loses integer precision}}
John McCalle8babd12009-11-07 08:15:46 +0000307}
John McCall8406aed2009-11-11 22:52:37 +0000308
309// PR 5422
310extern void *test16_external;
311void test16(void) {
Douglas Gregor27f46ee2010-07-09 18:18:35 +0000312 int a = (unsigned long) &test16_external; // expected-warning {{implicit conversion loses integer precision}}
John McCall8406aed2009-11-11 22:52:37 +0000313}
John McCallf2370c92010-01-06 05:24:50 +0000314
315// PR 5938
316void test17() {
317 union {
318 unsigned long long a : 8;
319 unsigned long long b : 32;
320 unsigned long long c;
321 } U;
322
323 unsigned int x;
324 x = U.a;
325 x = U.b;
Douglas Gregor27f46ee2010-07-09 18:18:35 +0000326 x = U.c; // expected-warning {{implicit conversion loses integer precision}}
John McCallf2370c92010-01-06 05:24:50 +0000327}
328
329// PR 5939
330void test18() {
331 union {
332 unsigned long long a : 1;
333 unsigned long long b;
334 } U;
335
336 int x;
337 x = (U.a ? 0 : 1);
338 x = (U.b ? 0 : 1);
339}
John McCall60fad452010-01-06 22:07:33 +0000340
341// None of these should warn.
342unsigned char test19(unsigned long u64) {
343 unsigned char x1 = u64 & 0xff;
344 unsigned char x2 = u64 >> 56;
345
346 unsigned char mask = 0xee;
347 unsigned char x3 = u64 & mask;
348 return x1 + x2 + x3;
349}
John McCallfb6289a2010-02-11 18:52:49 +0000350
351// <rdar://problem/7631400>
352void test_7631400(void) {
353 // This should show up despite the caret being inside a macro substitution
John McCall091f23f2010-11-09 22:22:12 +0000354 char s = LONG_MAX; // expected-warning {{implicit conversion from 'long' to 'char' changes value}}
John McCallfb6289a2010-02-11 18:52:49 +0000355}
John McCallc0cd21d2010-02-23 19:22:29 +0000356
357// <rdar://problem/7676608>: assertion for compound operators with non-integral RHS
358void f7676608(int);
359void test_7676608(void) {
360 float q = 0.7f;
361 char c = 5;
362 f7676608(c *= q);
363}
John McCall323ed742010-05-06 08:58:33 +0000364
365// <rdar://problem/7904686>
366void test_7904686(void) {
367 const int i = -1;
Douglas Gregor27f46ee2010-07-09 18:18:35 +0000368 unsigned u1 = i; // expected-warning {{implicit conversion changes signedness}}
369 u1 = i; // expected-warning {{implicit conversion changes signedness}}
John McCall323ed742010-05-06 08:58:33 +0000370
Douglas Gregor27f46ee2010-07-09 18:18:35 +0000371 unsigned u2 = -1; // expected-warning {{implicit conversion changes signedness}}
372 u2 = -1; // expected-warning {{implicit conversion changes signedness}}
John McCall323ed742010-05-06 08:58:33 +0000373}
John McCallb4eb64d2010-10-08 02:01:28 +0000374
375// <rdar://problem/8232669>: don't warn about conversions required by
376// contexts in system headers
377void test_8232669(void) {
378 unsigned bitset[20];
379 SETBIT(bitset, 0);
380
381 unsigned y = 50;
382 SETBIT(bitset, y);
383
384#define USER_SETBIT(set,bit) do { int i = bit; set[i/(8*sizeof(set[0]))] |= (1 << (i%(8*sizeof(set)))); } while(0)
385 USER_SETBIT(bitset, 0); // expected-warning 2 {{implicit conversion changes signedness}}
386}
Douglas Gregor284cc8d2011-02-22 02:45:07 +0000387
388// <rdar://problem/8559831>
389enum E8559831a { E8559831a_val };
390enum E8559831b { E8559831b_val };
391typedef enum { E8559831c_val } E8559831c;
392enum { E8559831d_val } value_d;
393
394void test_8559831_a(enum E8559831a value);
395void test_8559831(enum E8559831b value_a, E8559831c value_c) {
396 test_8559831_a(value_a); // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}}
397 enum E8559831a a1 = value_a; // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}}
398 a1 = value_a; // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}}
399
Douglas Gregor5a5b38f2011-03-12 00:14:31 +0000400 test_8559831_a(E8559831b_val); // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}}
401 enum E8559831a a1a = E8559831b_val; // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}}
402 a1 = E8559831b_val; // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}}
403
Douglas Gregor284cc8d2011-02-22 02:45:07 +0000404 test_8559831_a(value_c); // expected-warning{{implicit conversion from enumeration type 'E8559831c' to different enumeration type 'enum E8559831a'}}
405 enum E8559831a a2 = value_c; // expected-warning{{implicit conversion from enumeration type 'E8559831c' to different enumeration type 'enum E8559831a'}}
406 a2 = value_c; // expected-warning{{implicit conversion from enumeration type 'E8559831c' to different enumeration type 'enum E8559831a'}}
407
408 test_8559831_a(value_d);
409 enum E8559831a a3 = value_d;
410 a3 = value_d;
411}
John McCall00fe7612011-07-14 22:39:48 +0000412
413void test26(int si, long sl) {
414 si = sl % sl; // expected-warning {{implicit conversion loses integer precision: 'long' to 'int'}}
415 si = sl % si;
416 si = si % sl;
417 si = si / sl;
418 si = sl / si; // expected-warning {{implicit conversion loses integer precision: 'long' to 'int'}}
419}