1 // 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
4
5 #include <conversion.h>
6
7 #define BIG 0x7f7f7f7f7f7f7f7fL
8
test0(char c,short s,int i,long l,long long ll)9 void test0(char c, short s, int i, long l, long long ll) {
10 c = c;
11 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}}
14 s = c;
15 s = s;
16 s = i; // expected-warning {{implicit conversion loses integer precision}}
17 s = l; // expected-warning {{implicit conversion loses integer precision}}
18 i = c;
19 i = s;
20 i = i;
21 i = l; // expected-warning {{implicit conversion loses integer precision}}
22 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;
45 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}}
48 s = (char) BIG;
49 s = (short) BIG;
50 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}}
52 i = (char) BIG;
53 i = (short) BIG;
54 i = (int) BIG;
55 i = (long) BIG; // expected-warning {{implicit conversion from 'long' to 'int' changes value}}
56 l = (char) BIG;
57 l = (short) BIG;
58 l = (int) BIG;
59 l = (long) BIG;
60 }
61
test1(long long ll)62 char test1(long long ll) {
63 return (long long) ll; // expected-warning {{implicit conversion loses integer precision}}
64 }
test1_a(long long ll)65 char test1_a(long long ll) {
66 return (long) ll; // expected-warning {{implicit conversion loses integer precision}}
67 }
test1_b(long long ll)68 char test1_b(long long ll) {
69 return (int) ll; // expected-warning {{implicit conversion loses integer precision}}
70 }
test1_c(long long ll)71 char test1_c(long long ll) {
72 return (short) ll; // expected-warning {{implicit conversion loses integer precision}}
73 }
test1_d(long long ll)74 char test1_d(long long ll) {
75 return (char) ll;
76 }
test1_e(long long ll)77 char test1_e(long long ll) {
78 return (long long) BIG; // expected-warning {{implicit conversion from 'long long' to 'char' changes value}}
79 }
test1_f(long long ll)80 char test1_f(long long ll) {
81 return (long) BIG; // expected-warning {{implicit conversion from 'long' to 'char' changes value}}
82 }
test1_g(long long ll)83 char test1_g(long long ll) {
84 return (int) BIG; // expected-warning {{implicit conversion from 'int' to 'char' changes value}}
85 }
test1_h(long long ll)86 char test1_h(long long ll) {
87 return (short) BIG; // expected-warning {{implicit conversion from 'short' to 'char' changes value}}
88 }
test1_i(long long ll)89 char test1_i(long long ll) {
90 return (char) BIG;
91 }
92
test2(long long ll)93 short test2(long long ll) {
94 return (long long) ll; // expected-warning {{implicit conversion loses integer precision}}
95 }
test2_a(long long ll)96 short test2_a(long long ll) {
97 return (long) ll; // expected-warning {{implicit conversion loses integer precision}}
98 }
test2_b(long long ll)99 short test2_b(long long ll) {
100 return (int) ll; // expected-warning {{implicit conversion loses integer precision}}
101 }
test2_c(long long ll)102 short test2_c(long long ll) {
103 return (short) ll;
104 }
test2_d(long long ll)105 short test2_d(long long ll) {
106 return (char) ll;
107 }
test2_e(long long ll)108 short test2_e(long long ll) {
109 return (long long) BIG; // expected-warning {{implicit conversion from 'long long' to 'short' changes value}}
110 }
test2_f(long long ll)111 short test2_f(long long ll) {
112 return (long) BIG; // expected-warning {{implicit conversion from 'long' to 'short' changes value}}
113 }
test2_g(long long ll)114 short test2_g(long long ll) {
115 return (int) BIG; // expected-warning {{implicit conversion from 'int' to 'short' changes value}}
116 }
test2_h(long long ll)117 short test2_h(long long ll) {
118 return (short) BIG;
119 }
test2_i(long long ll)120 short test2_i(long long ll) {
121 return (char) BIG;
122 }
123
test3(long long ll)124 int test3(long long ll) {
125 return (long long) ll; // expected-warning {{implicit conversion loses integer precision}}
126 }
test3_b(long long ll)127 int test3_b(long long ll) {
128 return (long) ll; // expected-warning {{implicit conversion loses integer precision}}
129 }
test3_c(long long ll)130 int test3_c(long long ll) {
131 return (int) ll;
132 }
test3_d(long long ll)133 int test3_d(long long ll) {
134 return (short) ll;
135 }
test3_e(long long ll)136 int test3_e(long long ll) {
137 return (char) ll;
138 }
test3_f(long long ll)139 int test3_f(long long ll) {
140 return (long long) BIG; // expected-warning {{implicit conversion from 'long long' to 'int' changes value}}
141 }
test3_g(long long ll)142 int test3_g(long long ll) {
143 return (long) BIG; // expected-warning {{implicit conversion from 'long' to 'int' changes value}}
144 }
test3_h(long long ll)145 int test3_h(long long ll) {
146 return (int) BIG;
147 }
test3_i(long long ll)148 int test3_i(long long ll) {
149 return (short) BIG;
150 }
test3_j(long long ll)151 int test3_j(long long ll) {
152 return (char) BIG;
153 }
154
test4(long long ll)155 long test4(long long ll) {
156 return (long long) ll;
157 }
test4_a(long long ll)158 long test4_a(long long ll) {
159 return (long) ll;
160 }
test4_b(long long ll)161 long test4_b(long long ll) {
162 return (int) ll;
163 }
test4_c(long long ll)164 long test4_c(long long ll) {
165 return (short) ll;
166 }
test4_d(long long ll)167 long test4_d(long long ll) {
168 return (char) ll;
169 }
test4_e(long long ll)170 long test4_e(long long ll) {
171 return (long long) BIG;
172 }
test4_f(long long ll)173 long test4_f(long long ll) {
174 return (long) BIG;
175 }
test4_g(long long ll)176 long test4_g(long long ll) {
177 return (int) BIG;
178 }
test4_h(long long ll)179 long test4_h(long long ll) {
180 return (short) BIG;
181 }
test4_i(long long ll)182 long test4_i(long long ll) {
183 return (char) BIG;
184 }
185
test5(long long ll)186 long 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
199 void takes_char(char);
200 void takes_short(short);
201 void takes_int(int);
202 void takes_long(long);
203 void takes_longlong(long long);
204 void takes_float(float);
205 void takes_double(double);
206 void takes_longdouble(long double);
207
test6(char v)208 void 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
test7(short v)219 void test7(short v) {
220 takes_char(v); // expected-warning {{implicit conversion loses integer precision}}
221 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
test8(int v)230 void test8(int v) {
231 takes_char(v); // expected-warning {{implicit conversion loses integer precision}}
232 takes_short(v); // expected-warning {{implicit conversion loses integer precision}}
233 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
test9(long v)241 void test9(long v) {
242 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}}
245 takes_long(v);
246 takes_longlong(v);
247 takes_float(v);
248 takes_double(v);
249 takes_longdouble(v);
250 }
251
test10(long long v)252 void test10(long long v) {
253 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}}
256 takes_long(v);
257 takes_longlong(v);
258 takes_float(v);
259 takes_double(v);
260 takes_longdouble(v);
261 }
262
test11(float v)263 void test11(float v) {
264 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}}
269 takes_float(v);
270 takes_double(v);
271 takes_longdouble(v);
272 }
273
test12(double v)274 void test12(double v) {
275 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}}
281 takes_double(v);
282 takes_longdouble(v);
283 }
284
test13(long double v)285 void test13(long double v) {
286 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}}
293 takes_longdouble(v);
294 }
295
test14(long l)296 void 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 }
303
test15(char c)304 void test15(char c) {
305 c = c + 1 + c * 2;
306 c = (short) c + 1 + c * 2; // expected-warning {{implicit conversion loses integer precision}}
307 }
308
309 // PR 5422
310 extern void *test16_external;
test16(void)311 void test16(void) {
312 int a = (unsigned long) &test16_external; // expected-warning {{implicit conversion loses integer precision}}
313 }
314
315 // PR 5938
test17()316 void 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;
326 x = U.c; // expected-warning {{implicit conversion loses integer precision}}
327 }
328
329 // PR 5939
test18()330 void 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 }
340
341 // None of these should warn.
test19(unsigned long u64)342 unsigned 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 }
350
351 // <rdar://problem/7631400>
test_7631400(void)352 void test_7631400(void) {
353 // This should show up despite the caret being inside a macro substitution
354 char s = LONG_MAX; // expected-warning {{implicit conversion from 'long' to 'char' changes value}}
355 }
356
357 // <rdar://problem/7676608>: assertion for compound operators with non-integral RHS
358 void f7676608(int);
test_7676608(void)359 void test_7676608(void) {
360 float q = 0.7f;
361 char c = 5;
362 f7676608(c *= q);
363 }
364
365 // <rdar://problem/7904686>
test_7904686(void)366 void test_7904686(void) {
367 const int i = -1;
368 unsigned u1 = i; // expected-warning {{implicit conversion changes signedness}}
369 u1 = i; // expected-warning {{implicit conversion changes signedness}}
370
371 unsigned u2 = -1; // expected-warning {{implicit conversion changes signedness}}
372 u2 = -1; // expected-warning {{implicit conversion changes signedness}}
373 }
374
375 // <rdar://problem/8232669>: don't warn about conversions required by
376 // contexts in system headers
test_8232669(void)377 void 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 }
387
388 // <rdar://problem/8559831>
389 enum E8559831a { E8559831a_val };
390 enum E8559831b { E8559831b_val };
391 typedef enum { E8559831c_val } E8559831c;
392 enum { E8559831d_val } value_d;
393
394 void test_8559831_a(enum E8559831a value);
test_8559831(enum E8559831b value_a,E8559831c value_c)395 void 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
400 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
404 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 }
412
test26(int si,long sl)413 void 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 }
420
421 // rdar://16502418
422 typedef unsigned short uint16_t;
423 typedef unsigned int uint32_t;
424 typedef __attribute__ ((ext_vector_type(16),__aligned__(32))) uint16_t ushort16;
425 typedef __attribute__ ((ext_vector_type( 8),__aligned__( 32))) uint32_t uint8;
426
test27(ushort16 constants)427 void test27(ushort16 constants) {
428 uint8 pairedConstants = (uint8) constants;
429 ushort16 crCbScale = pairedConstants.s4; // expected-warning {{implicit conversion loses integer precision: 'uint32_t' (aka 'unsigned int') to 'ushort16'}}
430 ushort16 brBias = pairedConstants.s6; // expected-warning {{implicit conversion loses integer precision: 'uint32_t' (aka 'unsigned int') to 'ushort16'}}
431 }
432