1 // RUN: %clang_cc1 -fsyntax-only -verify -Wconversion -nostdinc -isystem %S/Inputs -triple x86_64-apple-darwin %s -Wno-unreachable-code
2
3 #include <conversion.h>
4
5 #define BIG 0x7f7f7f7f7f7f7f7fL
6
test0(char c,short s,int i,long l,long long ll)7 void test0(char c, short s, int i, long l, long long ll) {
8 c = c;
9 c = s; // expected-warning {{implicit conversion loses integer precision}}
10 c = i; // expected-warning {{implicit conversion loses integer precision}}
11 c = l; // expected-warning {{implicit conversion loses integer precision}}
12 s = c;
13 s = s;
14 s = i; // expected-warning {{implicit conversion loses integer precision}}
15 s = l; // expected-warning {{implicit conversion loses integer precision}}
16 i = c;
17 i = s;
18 i = i;
19 i = l; // expected-warning {{implicit conversion loses integer precision}}
20 l = c;
21 l = s;
22 l = i;
23 l = l;
24
25 c = (char) 0;
26 c = (short) 0;
27 c = (int) 0;
28 c = (long) 0;
29 s = (char) 0;
30 s = (short) 0;
31 s = (int) 0;
32 s = (long) 0;
33 i = (char) 0;
34 i = (short) 0;
35 i = (int) 0;
36 i = (long) 0;
37 l = (char) 0;
38 l = (short) 0;
39 l = (int) 0;
40 l = (long) 0;
41
42 c = (char) BIG;
43 c = (short) BIG; // expected-warning {{implicit conversion from 'short' to 'char' changes value}}
44 c = (int) BIG; // expected-warning {{implicit conversion from 'int' to 'char' changes value}}
45 c = (long) BIG; // expected-warning {{implicit conversion from 'long' to 'char' changes value}}
46 s = (char) BIG;
47 s = (short) BIG;
48 s = (int) BIG; // expected-warning {{implicit conversion from 'int' to 'short' changes value}}
49 s = (long) BIG; // expected-warning {{implicit conversion from 'long' to 'short' changes value}}
50 i = (char) BIG;
51 i = (short) BIG;
52 i = (int) BIG;
53 i = (long) BIG; // expected-warning {{implicit conversion from 'long' to 'int' changes value}}
54 l = (char) BIG;
55 l = (short) BIG;
56 l = (int) BIG;
57 l = (long) BIG;
58 }
59
test1(long long ll)60 char test1(long long ll) {
61 return (long long) ll; // expected-warning {{implicit conversion loses integer precision}}
62 return (long) ll; // expected-warning {{implicit conversion loses integer precision}}
63 return (int) ll; // expected-warning {{implicit conversion loses integer precision}}
64 return (short) ll; // expected-warning {{implicit conversion loses integer precision}}
65 return (char) ll;
66 return (long long) BIG; // expected-warning {{implicit conversion from 'long long' to 'char' changes value}}
67 return (long) BIG; // expected-warning {{implicit conversion from 'long' to 'char' changes value}}
68 return (int) BIG; // expected-warning {{implicit conversion from 'int' to 'char' changes value}}
69 return (short) BIG; // expected-warning {{implicit conversion from 'short' to 'char' changes value}}
70 return (char) BIG;
71 }
72
test2(long long ll)73 short test2(long long ll) {
74 return (long long) ll; // expected-warning {{implicit conversion loses integer precision}}
75 return (long) ll; // expected-warning {{implicit conversion loses integer precision}}
76 return (int) ll; // expected-warning {{implicit conversion loses integer precision}}
77 return (short) ll;
78 return (char) ll;
79 return (long long) BIG; // expected-warning {{implicit conversion from 'long long' to 'short' changes value}}
80 return (long) BIG; // expected-warning {{implicit conversion from 'long' to 'short' changes value}}
81 return (int) BIG; // expected-warning {{implicit conversion from 'int' to 'short' changes value}}
82 return (short) BIG;
83 return (char) BIG;
84 }
85
test3(long long ll)86 int test3(long long ll) {
87 return (long long) ll; // expected-warning {{implicit conversion loses integer precision}}
88 return (long) ll; // expected-warning {{implicit conversion loses integer precision}}
89 return (int) ll;
90 return (short) ll;
91 return (char) ll;
92 return (long long) BIG; // expected-warning {{implicit conversion from 'long long' to 'int' changes value}}
93 return (long) BIG; // expected-warning {{implicit conversion from 'long' to 'int' changes value}}
94 return (int) BIG;
95 return (short) BIG;
96 return (char) BIG;
97 }
98
test4(long long ll)99 long test4(long long ll) {
100 return (long long) ll;
101 return (long) ll;
102 return (int) ll;
103 return (short) ll;
104 return (char) ll;
105 return (long long) BIG;
106 return (long) BIG;
107 return (int) BIG;
108 return (short) BIG;
109 return (char) BIG;
110 }
111
test5(long long ll)112 long long test5(long long ll) {
113 return (long long) ll;
114 return (long) ll;
115 return (int) ll;
116 return (short) ll;
117 return (char) ll;
118 return (long long) BIG;
119 return (long) BIG;
120 return (int) BIG;
121 return (short) BIG;
122 return (char) BIG;
123 }
124
125 void takes_char(char);
126 void takes_short(short);
127 void takes_int(int);
128 void takes_long(long);
129 void takes_longlong(long long);
130 void takes_float(float);
131 void takes_double(double);
132 void takes_longdouble(long double);
133
test6(char v)134 void test6(char v) {
135 takes_char(v);
136 takes_short(v);
137 takes_int(v);
138 takes_long(v);
139 takes_longlong(v);
140 takes_float(v);
141 takes_double(v);
142 takes_longdouble(v);
143 }
144
test7(short v)145 void test7(short v) {
146 takes_char(v); // expected-warning {{implicit conversion loses integer precision}}
147 takes_short(v);
148 takes_int(v);
149 takes_long(v);
150 takes_longlong(v);
151 takes_float(v);
152 takes_double(v);
153 takes_longdouble(v);
154 }
155
test8(int v)156 void test8(int v) {
157 takes_char(v); // expected-warning {{implicit conversion loses integer precision}}
158 takes_short(v); // expected-warning {{implicit conversion loses integer precision}}
159 takes_int(v);
160 takes_long(v);
161 takes_longlong(v);
162 takes_float(v);
163 takes_double(v);
164 takes_longdouble(v);
165 }
166
test9(long v)167 void test9(long v) {
168 takes_char(v); // expected-warning {{implicit conversion loses integer precision}}
169 takes_short(v); // expected-warning {{implicit conversion loses integer precision}}
170 takes_int(v); // expected-warning {{implicit conversion loses integer precision}}
171 takes_long(v);
172 takes_longlong(v);
173 takes_float(v);
174 takes_double(v);
175 takes_longdouble(v);
176 }
177
test10(long long v)178 void test10(long long v) {
179 takes_char(v); // expected-warning {{implicit conversion loses integer precision}}
180 takes_short(v); // expected-warning {{implicit conversion loses integer precision}}
181 takes_int(v); // expected-warning {{implicit conversion loses integer precision}}
182 takes_long(v);
183 takes_longlong(v);
184 takes_float(v);
185 takes_double(v);
186 takes_longdouble(v);
187 }
188
test11(float v)189 void test11(float v) {
190 takes_char(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
191 takes_short(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
192 takes_int(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
193 takes_long(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
194 takes_longlong(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
195 takes_float(v);
196 takes_double(v);
197 takes_longdouble(v);
198 }
199
test12(double v)200 void test12(double v) {
201 takes_char(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
202 takes_short(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
203 takes_int(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
204 takes_long(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
205 takes_longlong(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
206 takes_float(v); // expected-warning {{implicit conversion loses floating-point precision}}
207 takes_double(v);
208 takes_longdouble(v);
209 }
210
test13(long double v)211 void test13(long double v) {
212 takes_char(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
213 takes_short(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
214 takes_int(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
215 takes_long(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
216 takes_longlong(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
217 takes_float(v); // expected-warning {{implicit conversion loses floating-point precision}}
218 takes_double(v); // expected-warning {{implicit conversion loses floating-point precision}}
219 takes_longdouble(v);
220 }
221
test14(long l)222 void test14(long l) {
223 // Fine because of the boolean whitelist.
224 char c;
225 c = (l == 4);
226 c = ((l <= 4) && (l >= 0));
227 c = ((l <= 4) && (l >= 0)) || (l > 20);
228 }
229
test15(char c)230 void test15(char c) {
231 c = c + 1 + c * 2;
232 c = (short) c + 1 + c * 2; // expected-warning {{implicit conversion loses integer precision}}
233 }
234
235 // PR 5422
236 extern void *test16_external;
test16(void)237 void test16(void) {
238 int a = (unsigned long) &test16_external; // expected-warning {{implicit conversion loses integer precision}}
239 }
240
241 // PR 5938
test17()242 void test17() {
243 union {
244 unsigned long long a : 8;
245 unsigned long long b : 32;
246 unsigned long long c;
247 } U;
248
249 unsigned int x;
250 x = U.a;
251 x = U.b;
252 x = U.c; // expected-warning {{implicit conversion loses integer precision}}
253 }
254
255 // PR 5939
test18()256 void test18() {
257 union {
258 unsigned long long a : 1;
259 unsigned long long b;
260 } U;
261
262 int x;
263 x = (U.a ? 0 : 1);
264 x = (U.b ? 0 : 1);
265 }
266
267 // None of these should warn.
test19(unsigned long u64)268 unsigned char test19(unsigned long u64) {
269 unsigned char x1 = u64 & 0xff;
270 unsigned char x2 = u64 >> 56;
271
272 unsigned char mask = 0xee;
273 unsigned char x3 = u64 & mask;
274 return x1 + x2 + x3;
275 }
276
277 // <rdar://problem/7631400>
test_7631400(void)278 void test_7631400(void) {
279 // This should show up despite the caret being inside a macro substitution
280 char s = LONG_MAX; // expected-warning {{implicit conversion from 'long' to 'char' changes value}}
281 }
282
283 // <rdar://problem/7676608>: assertion for compound operators with non-integral RHS
284 void f7676608(int);
test_7676608(void)285 void test_7676608(void) {
286 float q = 0.7f;
287 char c = 5;
288 f7676608(c *= q);
289 }
290
291 // <rdar://problem/7904686>
test_7904686(void)292 void test_7904686(void) {
293 const int i = -1;
294 unsigned u1 = i; // expected-warning {{implicit conversion changes signedness}}
295 u1 = i; // expected-warning {{implicit conversion changes signedness}}
296
297 unsigned u2 = -1; // expected-warning {{implicit conversion changes signedness}}
298 u2 = -1; // expected-warning {{implicit conversion changes signedness}}
299 }
300
301 // <rdar://problem/8232669>: don't warn about conversions required by
302 // contexts in system headers
test_8232669(void)303 void test_8232669(void) {
304 unsigned bitset[20];
305 SETBIT(bitset, 0);
306
307 unsigned y = 50;
308 SETBIT(bitset, y);
309
310 #define USER_SETBIT(set,bit) do { int i = bit; set[i/(8*sizeof(set[0]))] |= (1 << (i%(8*sizeof(set)))); } while(0)
311 USER_SETBIT(bitset, 0); // expected-warning 2 {{implicit conversion changes signedness}}
312 }
313
314 // <rdar://problem/8559831>
315 enum E8559831a { E8559831a_val };
316 enum E8559831b { E8559831b_val };
317 typedef enum { E8559831c_val } E8559831c;
318 enum { E8559831d_val } value_d;
319
320 void test_8559831_a(enum E8559831a value);
test_8559831(enum E8559831b value_a,E8559831c value_c)321 void test_8559831(enum E8559831b value_a, E8559831c value_c) {
322 test_8559831_a(value_a); // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}}
323 enum E8559831a a1 = value_a; // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}}
324 a1 = value_a; // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}}
325
326 test_8559831_a(E8559831b_val); // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}}
327 enum E8559831a a1a = E8559831b_val; // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}}
328 a1 = E8559831b_val; // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}}
329
330 test_8559831_a(value_c); // expected-warning{{implicit conversion from enumeration type 'E8559831c' to different enumeration type 'enum E8559831a'}}
331 enum E8559831a a2 = value_c; // expected-warning{{implicit conversion from enumeration type 'E8559831c' to different enumeration type 'enum E8559831a'}}
332 a2 = value_c; // expected-warning{{implicit conversion from enumeration type 'E8559831c' to different enumeration type 'enum E8559831a'}}
333
334 test_8559831_a(value_d);
335 enum E8559831a a3 = value_d;
336 a3 = value_d;
337 }
338
test26(int si,long sl)339 void test26(int si, long sl) {
340 si = sl % sl; // expected-warning {{implicit conversion loses integer precision: 'long' to 'int'}}
341 si = sl % si;
342 si = si % sl;
343 si = si / sl;
344 si = sl / si; // expected-warning {{implicit conversion loses integer precision: 'long' to 'int'}}
345 }
346