blob: 842641bf105614f3946802e67f374ae86fa8db6b [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}}
64 return (long) ll; // expected-warning {{implicit conversion loses integer precision}}
65 return (int) ll; // expected-warning {{implicit conversion loses integer precision}}
66 return (short) ll; // expected-warning {{implicit conversion loses integer precision}}
John McCalld5c376e2009-11-07 03:30:38 +000067 return (char) ll;
John McCall091f23f2010-11-09 22:22:12 +000068 return (long long) BIG; // expected-warning {{implicit conversion from 'long long' to 'char' changes value}}
69 return (long) BIG; // expected-warning {{implicit conversion from 'long' to 'char' changes value}}
70 return (int) BIG; // expected-warning {{implicit conversion from 'int' to 'char' changes value}}
71 return (short) BIG; // expected-warning {{implicit conversion from 'short' to 'char' changes value}}
John McCalld5c376e2009-11-07 03:30:38 +000072 return (char) BIG;
73}
74
75short test2(long long ll) {
Douglas Gregor27f46ee2010-07-09 18:18:35 +000076 return (long long) ll; // expected-warning {{implicit conversion loses integer precision}}
77 return (long) ll; // expected-warning {{implicit conversion loses integer precision}}
78 return (int) ll; // expected-warning {{implicit conversion loses integer precision}}
John McCalld5c376e2009-11-07 03:30:38 +000079 return (short) ll;
80 return (char) ll;
John McCall091f23f2010-11-09 22:22:12 +000081 return (long long) BIG; // expected-warning {{implicit conversion from 'long long' to 'short' changes value}}
82 return (long) BIG; // expected-warning {{implicit conversion from 'long' to 'short' changes value}}
83 return (int) BIG; // expected-warning {{implicit conversion from 'int' to 'short' changes value}}
John McCalld5c376e2009-11-07 03:30:38 +000084 return (short) BIG;
85 return (char) BIG;
86}
87
88int test3(long long ll) {
Douglas Gregor27f46ee2010-07-09 18:18:35 +000089 return (long long) ll; // expected-warning {{implicit conversion loses integer precision}}
90 return (long) ll; // expected-warning {{implicit conversion loses integer precision}}
John McCalld5c376e2009-11-07 03:30:38 +000091 return (int) ll;
92 return (short) ll;
93 return (char) ll;
John McCall091f23f2010-11-09 22:22:12 +000094 return (long long) BIG; // expected-warning {{implicit conversion from 'long long' to 'int' changes value}}
95 return (long) BIG; // expected-warning {{implicit conversion from 'long' to 'int' changes value}}
John McCalld5c376e2009-11-07 03:30:38 +000096 return (int) BIG;
97 return (short) BIG;
98 return (char) BIG;
99}
100
101long test4(long long ll) {
102 return (long long) ll;
103 return (long) ll;
104 return (int) ll;
105 return (short) ll;
106 return (char) ll;
107 return (long long) BIG;
108 return (long) BIG;
109 return (int) BIG;
110 return (short) BIG;
111 return (char) BIG;
112}
113
114long long test5(long long ll) {
115 return (long long) ll;
116 return (long) ll;
117 return (int) ll;
118 return (short) ll;
119 return (char) ll;
120 return (long long) BIG;
121 return (long) BIG;
122 return (int) BIG;
123 return (short) BIG;
124 return (char) BIG;
125}
126
127void takes_char(char);
128void takes_short(short);
129void takes_int(int);
130void takes_long(long);
131void takes_longlong(long long);
132void takes_float(float);
133void takes_double(double);
134void takes_longdouble(long double);
135
136void test6(char v) {
137 takes_char(v);
138 takes_short(v);
139 takes_int(v);
140 takes_long(v);
141 takes_longlong(v);
142 takes_float(v);
143 takes_double(v);
144 takes_longdouble(v);
145}
146
147void test7(short v) {
Douglas Gregor27f46ee2010-07-09 18:18:35 +0000148 takes_char(v); // expected-warning {{implicit conversion loses integer precision}}
John McCalld5c376e2009-11-07 03:30:38 +0000149 takes_short(v);
150 takes_int(v);
151 takes_long(v);
152 takes_longlong(v);
153 takes_float(v);
154 takes_double(v);
155 takes_longdouble(v);
156}
157
158void test8(int v) {
Douglas Gregor27f46ee2010-07-09 18:18:35 +0000159 takes_char(v); // expected-warning {{implicit conversion loses integer precision}}
160 takes_short(v); // expected-warning {{implicit conversion loses integer precision}}
John McCalld5c376e2009-11-07 03:30:38 +0000161 takes_int(v);
162 takes_long(v);
163 takes_longlong(v);
164 takes_float(v);
165 takes_double(v);
166 takes_longdouble(v);
167}
168
169void test9(long v) {
Douglas Gregor27f46ee2010-07-09 18:18:35 +0000170 takes_char(v); // expected-warning {{implicit conversion loses integer precision}}
171 takes_short(v); // expected-warning {{implicit conversion loses integer precision}}
172 takes_int(v); // expected-warning {{implicit conversion loses integer precision}}
John McCalld5c376e2009-11-07 03:30:38 +0000173 takes_long(v);
174 takes_longlong(v);
175 takes_float(v);
176 takes_double(v);
177 takes_longdouble(v);
178}
179
180void test10(long long v) {
Douglas Gregor27f46ee2010-07-09 18:18:35 +0000181 takes_char(v); // expected-warning {{implicit conversion loses integer precision}}
182 takes_short(v); // expected-warning {{implicit conversion loses integer precision}}
183 takes_int(v); // expected-warning {{implicit conversion loses integer precision}}
John McCalld5c376e2009-11-07 03:30:38 +0000184 takes_long(v);
185 takes_longlong(v);
186 takes_float(v);
187 takes_double(v);
188 takes_longdouble(v);
189}
190
191void test11(float v) {
Douglas Gregor27f46ee2010-07-09 18:18:35 +0000192 takes_char(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
193 takes_short(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
194 takes_int(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
195 takes_long(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
196 takes_longlong(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
John McCalld5c376e2009-11-07 03:30:38 +0000197 takes_float(v);
198 takes_double(v);
199 takes_longdouble(v);
200}
201
202void test12(double v) {
Douglas Gregor27f46ee2010-07-09 18:18:35 +0000203 takes_char(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
204 takes_short(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
205 takes_int(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
206 takes_long(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
207 takes_longlong(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
208 takes_float(v); // expected-warning {{implicit conversion loses floating-point precision}}
John McCalld5c376e2009-11-07 03:30:38 +0000209 takes_double(v);
210 takes_longdouble(v);
211}
212
213void test13(long double v) {
Douglas Gregor27f46ee2010-07-09 18:18:35 +0000214 takes_char(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
215 takes_short(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
216 takes_int(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
217 takes_long(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
218 takes_longlong(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
219 takes_float(v); // expected-warning {{implicit conversion loses floating-point precision}}
220 takes_double(v); // expected-warning {{implicit conversion loses floating-point precision}}
John McCalld5c376e2009-11-07 03:30:38 +0000221 takes_longdouble(v);
222}
223
224void test14(long l) {
225 // Fine because of the boolean whitelist.
226 char c;
227 c = (l == 4);
228 c = ((l <= 4) && (l >= 0));
229 c = ((l <= 4) && (l >= 0)) || (l > 20);
230}
John McCalle8babd12009-11-07 08:15:46 +0000231
232void test15(char c) {
233 c = c + 1 + c * 2;
Douglas Gregor27f46ee2010-07-09 18:18:35 +0000234 c = (short) c + 1 + c * 2; // expected-warning {{implicit conversion loses integer precision}}
John McCalle8babd12009-11-07 08:15:46 +0000235}
John McCall8406aed2009-11-11 22:52:37 +0000236
237// PR 5422
238extern void *test16_external;
239void test16(void) {
Douglas Gregor27f46ee2010-07-09 18:18:35 +0000240 int a = (unsigned long) &test16_external; // expected-warning {{implicit conversion loses integer precision}}
John McCall8406aed2009-11-11 22:52:37 +0000241}
John McCallf2370c92010-01-06 05:24:50 +0000242
243// PR 5938
244void test17() {
245 union {
246 unsigned long long a : 8;
247 unsigned long long b : 32;
248 unsigned long long c;
249 } U;
250
251 unsigned int x;
252 x = U.a;
253 x = U.b;
Douglas Gregor27f46ee2010-07-09 18:18:35 +0000254 x = U.c; // expected-warning {{implicit conversion loses integer precision}}
John McCallf2370c92010-01-06 05:24:50 +0000255}
256
257// PR 5939
258void test18() {
259 union {
260 unsigned long long a : 1;
261 unsigned long long b;
262 } U;
263
264 int x;
265 x = (U.a ? 0 : 1);
266 x = (U.b ? 0 : 1);
267}
John McCall60fad452010-01-06 22:07:33 +0000268
269// None of these should warn.
270unsigned char test19(unsigned long u64) {
271 unsigned char x1 = u64 & 0xff;
272 unsigned char x2 = u64 >> 56;
273
274 unsigned char mask = 0xee;
275 unsigned char x3 = u64 & mask;
276 return x1 + x2 + x3;
277}
John McCallfb6289a2010-02-11 18:52:49 +0000278
279// <rdar://problem/7631400>
280void test_7631400(void) {
281 // This should show up despite the caret being inside a macro substitution
John McCall091f23f2010-11-09 22:22:12 +0000282 char s = LONG_MAX; // expected-warning {{implicit conversion from 'long' to 'char' changes value}}
John McCallfb6289a2010-02-11 18:52:49 +0000283}
John McCallc0cd21d2010-02-23 19:22:29 +0000284
285// <rdar://problem/7676608>: assertion for compound operators with non-integral RHS
286void f7676608(int);
287void test_7676608(void) {
288 float q = 0.7f;
289 char c = 5;
290 f7676608(c *= q);
291}
John McCall323ed742010-05-06 08:58:33 +0000292
293// <rdar://problem/7904686>
294void test_7904686(void) {
295 const int i = -1;
Douglas Gregor27f46ee2010-07-09 18:18:35 +0000296 unsigned u1 = i; // expected-warning {{implicit conversion changes signedness}}
297 u1 = i; // expected-warning {{implicit conversion changes signedness}}
John McCall323ed742010-05-06 08:58:33 +0000298
Douglas Gregor27f46ee2010-07-09 18:18:35 +0000299 unsigned u2 = -1; // expected-warning {{implicit conversion changes signedness}}
300 u2 = -1; // expected-warning {{implicit conversion changes signedness}}
John McCall323ed742010-05-06 08:58:33 +0000301}
John McCallb4eb64d2010-10-08 02:01:28 +0000302
303// <rdar://problem/8232669>: don't warn about conversions required by
304// contexts in system headers
305void test_8232669(void) {
306 unsigned bitset[20];
307 SETBIT(bitset, 0);
308
309 unsigned y = 50;
310 SETBIT(bitset, y);
311
312#define USER_SETBIT(set,bit) do { int i = bit; set[i/(8*sizeof(set[0]))] |= (1 << (i%(8*sizeof(set)))); } while(0)
313 USER_SETBIT(bitset, 0); // expected-warning 2 {{implicit conversion changes signedness}}
314}
Douglas Gregor284cc8d2011-02-22 02:45:07 +0000315
316// <rdar://problem/8559831>
317enum E8559831a { E8559831a_val };
318enum E8559831b { E8559831b_val };
319typedef enum { E8559831c_val } E8559831c;
320enum { E8559831d_val } value_d;
321
322void test_8559831_a(enum E8559831a value);
323void test_8559831(enum E8559831b value_a, E8559831c value_c) {
324 test_8559831_a(value_a); // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}}
325 enum E8559831a a1 = value_a; // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}}
326 a1 = value_a; // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}}
327
Douglas Gregor5a5b38f2011-03-12 00:14:31 +0000328 test_8559831_a(E8559831b_val); // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}}
329 enum E8559831a a1a = E8559831b_val; // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}}
330 a1 = E8559831b_val; // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}}
331
Douglas Gregor284cc8d2011-02-22 02:45:07 +0000332 test_8559831_a(value_c); // expected-warning{{implicit conversion from enumeration type 'E8559831c' to different enumeration type 'enum E8559831a'}}
333 enum E8559831a a2 = value_c; // expected-warning{{implicit conversion from enumeration type 'E8559831c' to different enumeration type 'enum E8559831a'}}
334 a2 = value_c; // expected-warning{{implicit conversion from enumeration type 'E8559831c' to different enumeration type 'enum E8559831a'}}
335
336 test_8559831_a(value_d);
337 enum E8559831a a3 = value_d;
338 a3 = value_d;
339}
John McCall00fe7612011-07-14 22:39:48 +0000340
341void test26(int si, long sl) {
342 si = sl % sl; // expected-warning {{implicit conversion loses integer precision: 'long' to 'int'}}
343 si = sl % si;
344 si = si % sl;
345 si = si / sl;
346 si = sl / si; // expected-warning {{implicit conversion loses integer precision: 'long' to 'int'}}
347}