xref: /minix3/external/bsd/llvm/dist/clang/test/Sema/conversion.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify -Wconversion \
2f4a2713aSLionel Sambuc // RUN:   -nostdsysteminc -nobuiltininc -isystem %S/Inputs \
3f4a2713aSLionel Sambuc // RUN:   -triple x86_64-apple-darwin %s -Wno-unreachable-code
4f4a2713aSLionel Sambuc 
5f4a2713aSLionel Sambuc #include <conversion.h>
6f4a2713aSLionel Sambuc 
7f4a2713aSLionel Sambuc #define BIG 0x7f7f7f7f7f7f7f7fL
8f4a2713aSLionel Sambuc 
test0(char c,short s,int i,long l,long long ll)9f4a2713aSLionel Sambuc void test0(char c, short s, int i, long l, long long ll) {
10f4a2713aSLionel Sambuc   c = c;
11f4a2713aSLionel Sambuc   c = s; // expected-warning {{implicit conversion loses integer precision}}
12f4a2713aSLionel Sambuc   c = i; // expected-warning {{implicit conversion loses integer precision}}
13f4a2713aSLionel Sambuc   c = l; // expected-warning {{implicit conversion loses integer precision}}
14f4a2713aSLionel Sambuc   s = c;
15f4a2713aSLionel Sambuc   s = s;
16f4a2713aSLionel Sambuc   s = i; // expected-warning {{implicit conversion loses integer precision}}
17f4a2713aSLionel Sambuc   s = l; // expected-warning {{implicit conversion loses integer precision}}
18f4a2713aSLionel Sambuc   i = c;
19f4a2713aSLionel Sambuc   i = s;
20f4a2713aSLionel Sambuc   i = i;
21f4a2713aSLionel Sambuc   i = l; // expected-warning {{implicit conversion loses integer precision}}
22f4a2713aSLionel Sambuc   l = c;
23f4a2713aSLionel Sambuc   l = s;
24f4a2713aSLionel Sambuc   l = i;
25f4a2713aSLionel Sambuc   l = l;
26f4a2713aSLionel Sambuc 
27f4a2713aSLionel Sambuc   c = (char) 0;
28f4a2713aSLionel Sambuc   c = (short) 0;
29f4a2713aSLionel Sambuc   c = (int) 0;
30f4a2713aSLionel Sambuc   c = (long) 0;
31f4a2713aSLionel Sambuc   s = (char) 0;
32f4a2713aSLionel Sambuc   s = (short) 0;
33f4a2713aSLionel Sambuc   s = (int) 0;
34f4a2713aSLionel Sambuc   s = (long) 0;
35f4a2713aSLionel Sambuc   i = (char) 0;
36f4a2713aSLionel Sambuc   i = (short) 0;
37f4a2713aSLionel Sambuc   i = (int) 0;
38f4a2713aSLionel Sambuc   i = (long) 0;
39f4a2713aSLionel Sambuc   l = (char) 0;
40f4a2713aSLionel Sambuc   l = (short) 0;
41f4a2713aSLionel Sambuc   l = (int) 0;
42f4a2713aSLionel Sambuc   l = (long) 0;
43f4a2713aSLionel Sambuc 
44f4a2713aSLionel Sambuc   c = (char) BIG;
45f4a2713aSLionel Sambuc   c = (short) BIG; // expected-warning {{implicit conversion from 'short' to 'char' changes value}}
46f4a2713aSLionel Sambuc   c = (int) BIG; // expected-warning {{implicit conversion from 'int' to 'char' changes value}}
47f4a2713aSLionel Sambuc   c = (long) BIG; // expected-warning {{implicit conversion from 'long' to 'char' changes value}}
48f4a2713aSLionel Sambuc   s = (char) BIG;
49f4a2713aSLionel Sambuc   s = (short) BIG;
50f4a2713aSLionel Sambuc   s = (int) BIG; // expected-warning {{implicit conversion from 'int' to 'short' changes value}}
51f4a2713aSLionel Sambuc   s = (long) BIG; // expected-warning {{implicit conversion from 'long' to 'short' changes value}}
52f4a2713aSLionel Sambuc   i = (char) BIG;
53f4a2713aSLionel Sambuc   i = (short) BIG;
54f4a2713aSLionel Sambuc   i = (int) BIG;
55f4a2713aSLionel Sambuc   i = (long) BIG; // expected-warning {{implicit conversion from 'long' to 'int' changes value}}
56f4a2713aSLionel Sambuc   l = (char) BIG;
57f4a2713aSLionel Sambuc   l = (short) BIG;
58f4a2713aSLionel Sambuc   l = (int) BIG;
59f4a2713aSLionel Sambuc   l = (long) BIG;
60f4a2713aSLionel Sambuc }
61f4a2713aSLionel Sambuc 
test1(long long ll)62f4a2713aSLionel Sambuc char test1(long long ll) {
63f4a2713aSLionel Sambuc   return (long long) ll; // expected-warning {{implicit conversion loses integer precision}}
64f4a2713aSLionel Sambuc }
test1_a(long long ll)65f4a2713aSLionel Sambuc char test1_a(long long ll) {
66f4a2713aSLionel Sambuc   return (long) ll; // expected-warning {{implicit conversion loses integer precision}}
67f4a2713aSLionel Sambuc }
test1_b(long long ll)68f4a2713aSLionel Sambuc char test1_b(long long ll) {
69f4a2713aSLionel Sambuc   return (int) ll; // expected-warning {{implicit conversion loses integer precision}}
70f4a2713aSLionel Sambuc }
test1_c(long long ll)71f4a2713aSLionel Sambuc char test1_c(long long ll) {
72f4a2713aSLionel Sambuc   return (short) ll; // expected-warning {{implicit conversion loses integer precision}}
73f4a2713aSLionel Sambuc }
test1_d(long long ll)74f4a2713aSLionel Sambuc char test1_d(long long ll) {
75f4a2713aSLionel Sambuc   return (char) ll;
76f4a2713aSLionel Sambuc }
test1_e(long long ll)77f4a2713aSLionel Sambuc char test1_e(long long ll) {
78f4a2713aSLionel Sambuc   return (long long) BIG; // expected-warning {{implicit conversion from 'long long' to 'char' changes value}}
79f4a2713aSLionel Sambuc }
test1_f(long long ll)80f4a2713aSLionel Sambuc char test1_f(long long ll) {
81f4a2713aSLionel Sambuc   return (long) BIG; // expected-warning {{implicit conversion from 'long' to 'char' changes value}}
82f4a2713aSLionel Sambuc }
test1_g(long long ll)83f4a2713aSLionel Sambuc char test1_g(long long ll) {
84f4a2713aSLionel Sambuc   return (int) BIG; // expected-warning {{implicit conversion from 'int' to 'char' changes value}}
85f4a2713aSLionel Sambuc }
test1_h(long long ll)86f4a2713aSLionel Sambuc char test1_h(long long ll) {
87f4a2713aSLionel Sambuc   return (short) BIG; // expected-warning {{implicit conversion from 'short' to 'char' changes value}}
88f4a2713aSLionel Sambuc }
test1_i(long long ll)89f4a2713aSLionel Sambuc char test1_i(long long ll) {
90f4a2713aSLionel Sambuc   return (char) BIG;
91f4a2713aSLionel Sambuc }
92f4a2713aSLionel Sambuc 
test2(long long ll)93f4a2713aSLionel Sambuc short test2(long long ll) {
94f4a2713aSLionel Sambuc   return (long long) ll; // expected-warning {{implicit conversion loses integer precision}}
95f4a2713aSLionel Sambuc }
test2_a(long long ll)96f4a2713aSLionel Sambuc short test2_a(long long ll) {
97f4a2713aSLionel Sambuc   return (long) ll; // expected-warning {{implicit conversion loses integer precision}}
98f4a2713aSLionel Sambuc }
test2_b(long long ll)99f4a2713aSLionel Sambuc short test2_b(long long ll) {
100f4a2713aSLionel Sambuc   return (int) ll; // expected-warning {{implicit conversion loses integer precision}}
101f4a2713aSLionel Sambuc }
test2_c(long long ll)102f4a2713aSLionel Sambuc short test2_c(long long ll) {
103f4a2713aSLionel Sambuc   return (short) ll;
104f4a2713aSLionel Sambuc }
test2_d(long long ll)105f4a2713aSLionel Sambuc short test2_d(long long ll) {
106f4a2713aSLionel Sambuc   return (char) ll;
107f4a2713aSLionel Sambuc }
test2_e(long long ll)108f4a2713aSLionel Sambuc short test2_e(long long ll) {
109f4a2713aSLionel Sambuc   return (long long) BIG;  // expected-warning {{implicit conversion from 'long long' to 'short' changes value}}
110f4a2713aSLionel Sambuc }
test2_f(long long ll)111f4a2713aSLionel Sambuc short test2_f(long long ll) {
112f4a2713aSLionel Sambuc   return (long) BIG;  // expected-warning {{implicit conversion from 'long' to 'short' changes value}}
113f4a2713aSLionel Sambuc }
test2_g(long long ll)114f4a2713aSLionel Sambuc short test2_g(long long ll) {
115f4a2713aSLionel Sambuc   return (int) BIG;  // expected-warning {{implicit conversion from 'int' to 'short' changes value}}
116f4a2713aSLionel Sambuc }
test2_h(long long ll)117f4a2713aSLionel Sambuc short test2_h(long long ll) {
118f4a2713aSLionel Sambuc   return (short) BIG;
119f4a2713aSLionel Sambuc }
test2_i(long long ll)120f4a2713aSLionel Sambuc short test2_i(long long ll) {
121f4a2713aSLionel Sambuc   return (char) BIG;
122f4a2713aSLionel Sambuc }
123f4a2713aSLionel Sambuc 
test3(long long ll)124f4a2713aSLionel Sambuc int test3(long long ll) {
125f4a2713aSLionel Sambuc   return (long long) ll;  // expected-warning {{implicit conversion loses integer precision}}
126f4a2713aSLionel Sambuc }
test3_b(long long ll)127f4a2713aSLionel Sambuc int test3_b(long long ll) {
128f4a2713aSLionel Sambuc   return (long) ll;  // expected-warning {{implicit conversion loses integer precision}}
129f4a2713aSLionel Sambuc }
test3_c(long long ll)130f4a2713aSLionel Sambuc int test3_c(long long ll) {
131f4a2713aSLionel Sambuc   return (int) ll;
132f4a2713aSLionel Sambuc }
test3_d(long long ll)133f4a2713aSLionel Sambuc int test3_d(long long ll) {
134f4a2713aSLionel Sambuc   return (short) ll;
135f4a2713aSLionel Sambuc }
test3_e(long long ll)136f4a2713aSLionel Sambuc int test3_e(long long ll) {
137f4a2713aSLionel Sambuc   return (char) ll;
138f4a2713aSLionel Sambuc }
test3_f(long long ll)139f4a2713aSLionel Sambuc int test3_f(long long ll) {
140f4a2713aSLionel Sambuc   return (long long) BIG;  // expected-warning {{implicit conversion from 'long long' to 'int' changes value}}
141f4a2713aSLionel Sambuc }
test3_g(long long ll)142f4a2713aSLionel Sambuc int test3_g(long long ll) {
143f4a2713aSLionel Sambuc   return (long) BIG; // expected-warning {{implicit conversion from 'long' to 'int' changes value}}
144f4a2713aSLionel Sambuc }
test3_h(long long ll)145f4a2713aSLionel Sambuc int test3_h(long long ll) {
146f4a2713aSLionel Sambuc   return (int) BIG;
147f4a2713aSLionel Sambuc }
test3_i(long long ll)148f4a2713aSLionel Sambuc int test3_i(long long ll) {
149f4a2713aSLionel Sambuc   return (short) BIG;
150f4a2713aSLionel Sambuc }
test3_j(long long ll)151f4a2713aSLionel Sambuc int test3_j(long long ll) {
152f4a2713aSLionel Sambuc   return (char) BIG;
153f4a2713aSLionel Sambuc }
154f4a2713aSLionel Sambuc 
test4(long long ll)155f4a2713aSLionel Sambuc long test4(long long ll) {
156f4a2713aSLionel Sambuc   return (long long) ll;
157f4a2713aSLionel Sambuc }
test4_a(long long ll)158f4a2713aSLionel Sambuc long test4_a(long long ll) {
159f4a2713aSLionel Sambuc   return (long) ll;
160f4a2713aSLionel Sambuc }
test4_b(long long ll)161f4a2713aSLionel Sambuc long test4_b(long long ll) {
162f4a2713aSLionel Sambuc   return (int) ll;
163f4a2713aSLionel Sambuc }
test4_c(long long ll)164f4a2713aSLionel Sambuc long test4_c(long long ll) {
165f4a2713aSLionel Sambuc   return (short) ll;
166f4a2713aSLionel Sambuc }
test4_d(long long ll)167f4a2713aSLionel Sambuc long test4_d(long long ll) {
168f4a2713aSLionel Sambuc   return (char) ll;
169f4a2713aSLionel Sambuc }
test4_e(long long ll)170f4a2713aSLionel Sambuc long test4_e(long long ll) {
171f4a2713aSLionel Sambuc   return (long long) BIG;
172f4a2713aSLionel Sambuc }
test4_f(long long ll)173f4a2713aSLionel Sambuc long test4_f(long long ll) {
174f4a2713aSLionel Sambuc   return (long) BIG;
175f4a2713aSLionel Sambuc }
test4_g(long long ll)176f4a2713aSLionel Sambuc long test4_g(long long ll) {
177f4a2713aSLionel Sambuc   return (int) BIG;
178f4a2713aSLionel Sambuc }
test4_h(long long ll)179f4a2713aSLionel Sambuc long test4_h(long long ll) {
180f4a2713aSLionel Sambuc   return (short) BIG;
181f4a2713aSLionel Sambuc }
test4_i(long long ll)182f4a2713aSLionel Sambuc long test4_i(long long ll) {
183f4a2713aSLionel Sambuc   return (char) BIG;
184f4a2713aSLionel Sambuc }
185f4a2713aSLionel Sambuc 
test5(long long ll)186f4a2713aSLionel Sambuc long long test5(long long ll) {
187f4a2713aSLionel Sambuc   return (long long) ll;
188f4a2713aSLionel Sambuc   return (long) ll;
189f4a2713aSLionel Sambuc   return (int) ll;
190f4a2713aSLionel Sambuc   return (short) ll;
191f4a2713aSLionel Sambuc   return (char) ll;
192f4a2713aSLionel Sambuc   return (long long) BIG;
193f4a2713aSLionel Sambuc   return (long) BIG;
194f4a2713aSLionel Sambuc   return (int) BIG;
195f4a2713aSLionel Sambuc   return (short) BIG;
196f4a2713aSLionel Sambuc   return (char) BIG;
197f4a2713aSLionel Sambuc }
198f4a2713aSLionel Sambuc 
199f4a2713aSLionel Sambuc void takes_char(char);
200f4a2713aSLionel Sambuc void takes_short(short);
201f4a2713aSLionel Sambuc void takes_int(int);
202f4a2713aSLionel Sambuc void takes_long(long);
203f4a2713aSLionel Sambuc void takes_longlong(long long);
204f4a2713aSLionel Sambuc void takes_float(float);
205f4a2713aSLionel Sambuc void takes_double(double);
206f4a2713aSLionel Sambuc void takes_longdouble(long double);
207f4a2713aSLionel Sambuc 
test6(char v)208f4a2713aSLionel Sambuc void test6(char v) {
209f4a2713aSLionel Sambuc   takes_char(v);
210f4a2713aSLionel Sambuc   takes_short(v);
211f4a2713aSLionel Sambuc   takes_int(v);
212f4a2713aSLionel Sambuc   takes_long(v);
213f4a2713aSLionel Sambuc   takes_longlong(v);
214f4a2713aSLionel Sambuc   takes_float(v);
215f4a2713aSLionel Sambuc   takes_double(v);
216f4a2713aSLionel Sambuc   takes_longdouble(v);
217f4a2713aSLionel Sambuc }
218f4a2713aSLionel Sambuc 
test7(short v)219f4a2713aSLionel Sambuc void test7(short v) {
220f4a2713aSLionel Sambuc   takes_char(v); // expected-warning {{implicit conversion loses integer precision}}
221f4a2713aSLionel Sambuc   takes_short(v);
222f4a2713aSLionel Sambuc   takes_int(v);
223f4a2713aSLionel Sambuc   takes_long(v);
224f4a2713aSLionel Sambuc   takes_longlong(v);
225f4a2713aSLionel Sambuc   takes_float(v);
226f4a2713aSLionel Sambuc   takes_double(v);
227f4a2713aSLionel Sambuc   takes_longdouble(v);
228f4a2713aSLionel Sambuc }
229f4a2713aSLionel Sambuc 
test8(int v)230f4a2713aSLionel Sambuc void test8(int v) {
231f4a2713aSLionel Sambuc   takes_char(v); // expected-warning {{implicit conversion loses integer precision}}
232f4a2713aSLionel Sambuc   takes_short(v); // expected-warning {{implicit conversion loses integer precision}}
233f4a2713aSLionel Sambuc   takes_int(v);
234f4a2713aSLionel Sambuc   takes_long(v);
235f4a2713aSLionel Sambuc   takes_longlong(v);
236f4a2713aSLionel Sambuc   takes_float(v);
237f4a2713aSLionel Sambuc   takes_double(v);
238f4a2713aSLionel Sambuc   takes_longdouble(v);
239f4a2713aSLionel Sambuc }
240f4a2713aSLionel Sambuc 
test9(long v)241f4a2713aSLionel Sambuc void test9(long v) {
242f4a2713aSLionel Sambuc   takes_char(v); // expected-warning {{implicit conversion loses integer precision}}
243f4a2713aSLionel Sambuc   takes_short(v); // expected-warning {{implicit conversion loses integer precision}}
244f4a2713aSLionel Sambuc   takes_int(v); // expected-warning {{implicit conversion loses integer precision}}
245f4a2713aSLionel Sambuc   takes_long(v);
246f4a2713aSLionel Sambuc   takes_longlong(v);
247f4a2713aSLionel Sambuc   takes_float(v);
248f4a2713aSLionel Sambuc   takes_double(v);
249f4a2713aSLionel Sambuc   takes_longdouble(v);
250f4a2713aSLionel Sambuc }
251f4a2713aSLionel Sambuc 
test10(long long v)252f4a2713aSLionel Sambuc void test10(long long v) {
253f4a2713aSLionel Sambuc   takes_char(v); // expected-warning {{implicit conversion loses integer precision}}
254f4a2713aSLionel Sambuc   takes_short(v); // expected-warning {{implicit conversion loses integer precision}}
255f4a2713aSLionel Sambuc   takes_int(v); // expected-warning {{implicit conversion loses integer precision}}
256f4a2713aSLionel Sambuc   takes_long(v);
257f4a2713aSLionel Sambuc   takes_longlong(v);
258f4a2713aSLionel Sambuc   takes_float(v);
259f4a2713aSLionel Sambuc   takes_double(v);
260f4a2713aSLionel Sambuc   takes_longdouble(v);
261f4a2713aSLionel Sambuc }
262f4a2713aSLionel Sambuc 
test11(float v)263f4a2713aSLionel Sambuc void test11(float v) {
264f4a2713aSLionel Sambuc   takes_char(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
265f4a2713aSLionel Sambuc   takes_short(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
266f4a2713aSLionel Sambuc   takes_int(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
267f4a2713aSLionel Sambuc   takes_long(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
268f4a2713aSLionel Sambuc   takes_longlong(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
269f4a2713aSLionel Sambuc   takes_float(v);
270f4a2713aSLionel Sambuc   takes_double(v);
271f4a2713aSLionel Sambuc   takes_longdouble(v);
272f4a2713aSLionel Sambuc }
273f4a2713aSLionel Sambuc 
test12(double v)274f4a2713aSLionel Sambuc void test12(double v) {
275f4a2713aSLionel Sambuc   takes_char(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
276f4a2713aSLionel Sambuc   takes_short(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
277f4a2713aSLionel Sambuc   takes_int(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
278f4a2713aSLionel Sambuc   takes_long(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
279f4a2713aSLionel Sambuc   takes_longlong(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
280f4a2713aSLionel Sambuc   takes_float(v); // expected-warning {{implicit conversion loses floating-point precision}}
281f4a2713aSLionel Sambuc   takes_double(v);
282f4a2713aSLionel Sambuc   takes_longdouble(v);
283f4a2713aSLionel Sambuc }
284f4a2713aSLionel Sambuc 
test13(long double v)285f4a2713aSLionel Sambuc void test13(long double v) {
286f4a2713aSLionel Sambuc   takes_char(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
287f4a2713aSLionel Sambuc   takes_short(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
288f4a2713aSLionel Sambuc   takes_int(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
289f4a2713aSLionel Sambuc   takes_long(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
290f4a2713aSLionel Sambuc   takes_longlong(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
291f4a2713aSLionel Sambuc   takes_float(v); // expected-warning {{implicit conversion loses floating-point precision}}
292f4a2713aSLionel Sambuc   takes_double(v); // expected-warning {{implicit conversion loses floating-point precision}}
293f4a2713aSLionel Sambuc   takes_longdouble(v);
294f4a2713aSLionel Sambuc }
295f4a2713aSLionel Sambuc 
test14(long l)296f4a2713aSLionel Sambuc void test14(long l) {
297f4a2713aSLionel Sambuc   // Fine because of the boolean whitelist.
298f4a2713aSLionel Sambuc   char c;
299f4a2713aSLionel Sambuc   c = (l == 4);
300f4a2713aSLionel Sambuc   c = ((l <= 4) && (l >= 0));
301f4a2713aSLionel Sambuc   c = ((l <= 4) && (l >= 0)) || (l > 20);
302f4a2713aSLionel Sambuc }
303f4a2713aSLionel Sambuc 
test15(char c)304f4a2713aSLionel Sambuc void test15(char c) {
305f4a2713aSLionel Sambuc   c = c + 1 + c * 2;
306f4a2713aSLionel Sambuc   c = (short) c + 1 + c * 2; // expected-warning {{implicit conversion loses integer precision}}
307f4a2713aSLionel Sambuc }
308f4a2713aSLionel Sambuc 
309f4a2713aSLionel Sambuc // PR 5422
310f4a2713aSLionel Sambuc extern void *test16_external;
test16(void)311f4a2713aSLionel Sambuc void test16(void) {
312f4a2713aSLionel Sambuc   int a = (unsigned long) &test16_external; // expected-warning {{implicit conversion loses integer precision}}
313f4a2713aSLionel Sambuc }
314f4a2713aSLionel Sambuc 
315f4a2713aSLionel Sambuc // PR 5938
test17()316f4a2713aSLionel Sambuc void test17() {
317f4a2713aSLionel Sambuc   union {
318f4a2713aSLionel Sambuc     unsigned long long a : 8;
319f4a2713aSLionel Sambuc     unsigned long long b : 32;
320f4a2713aSLionel Sambuc     unsigned long long c;
321f4a2713aSLionel Sambuc   } U;
322f4a2713aSLionel Sambuc 
323f4a2713aSLionel Sambuc   unsigned int x;
324f4a2713aSLionel Sambuc   x = U.a;
325f4a2713aSLionel Sambuc   x = U.b;
326f4a2713aSLionel Sambuc   x = U.c; // expected-warning {{implicit conversion loses integer precision}}
327f4a2713aSLionel Sambuc }
328f4a2713aSLionel Sambuc 
329f4a2713aSLionel Sambuc // PR 5939
test18()330f4a2713aSLionel Sambuc void test18() {
331f4a2713aSLionel Sambuc   union {
332f4a2713aSLionel Sambuc     unsigned long long a : 1;
333f4a2713aSLionel Sambuc     unsigned long long b;
334f4a2713aSLionel Sambuc   } U;
335f4a2713aSLionel Sambuc 
336f4a2713aSLionel Sambuc   int x;
337f4a2713aSLionel Sambuc   x = (U.a ? 0 : 1);
338f4a2713aSLionel Sambuc   x = (U.b ? 0 : 1);
339f4a2713aSLionel Sambuc }
340f4a2713aSLionel Sambuc 
341f4a2713aSLionel Sambuc // None of these should warn.
test19(unsigned long u64)342f4a2713aSLionel Sambuc unsigned char test19(unsigned long u64) {
343f4a2713aSLionel Sambuc   unsigned char x1 = u64 & 0xff;
344f4a2713aSLionel Sambuc   unsigned char x2 = u64 >> 56;
345f4a2713aSLionel Sambuc 
346f4a2713aSLionel Sambuc   unsigned char mask = 0xee;
347f4a2713aSLionel Sambuc   unsigned char x3 = u64 & mask;
348f4a2713aSLionel Sambuc   return x1 + x2 + x3;
349f4a2713aSLionel Sambuc }
350f4a2713aSLionel Sambuc 
351f4a2713aSLionel Sambuc // <rdar://problem/7631400>
test_7631400(void)352f4a2713aSLionel Sambuc void test_7631400(void) {
353f4a2713aSLionel Sambuc   // This should show up despite the caret being inside a macro substitution
354f4a2713aSLionel Sambuc   char s = LONG_MAX; // expected-warning {{implicit conversion from 'long' to 'char' changes value}}
355f4a2713aSLionel Sambuc }
356f4a2713aSLionel Sambuc 
357f4a2713aSLionel Sambuc // <rdar://problem/7676608>: assertion for compound operators with non-integral RHS
358f4a2713aSLionel Sambuc void f7676608(int);
test_7676608(void)359f4a2713aSLionel Sambuc void test_7676608(void) {
360f4a2713aSLionel Sambuc   float q = 0.7f;
361f4a2713aSLionel Sambuc   char c = 5;
362f4a2713aSLionel Sambuc   f7676608(c *= q);
363f4a2713aSLionel Sambuc }
364f4a2713aSLionel Sambuc 
365f4a2713aSLionel Sambuc // <rdar://problem/7904686>
test_7904686(void)366f4a2713aSLionel Sambuc void test_7904686(void) {
367f4a2713aSLionel Sambuc   const int i = -1;
368f4a2713aSLionel Sambuc   unsigned u1 = i; // expected-warning {{implicit conversion changes signedness}}
369f4a2713aSLionel Sambuc   u1 = i; // expected-warning {{implicit conversion changes signedness}}
370f4a2713aSLionel Sambuc 
371f4a2713aSLionel Sambuc   unsigned u2 = -1; // expected-warning {{implicit conversion changes signedness}}
372f4a2713aSLionel Sambuc   u2 = -1; // expected-warning {{implicit conversion changes signedness}}
373f4a2713aSLionel Sambuc }
374f4a2713aSLionel Sambuc 
375f4a2713aSLionel Sambuc // <rdar://problem/8232669>: don't warn about conversions required by
376f4a2713aSLionel Sambuc // contexts in system headers
test_8232669(void)377f4a2713aSLionel Sambuc void test_8232669(void) {
378f4a2713aSLionel Sambuc   unsigned bitset[20];
379f4a2713aSLionel Sambuc   SETBIT(bitset, 0);
380f4a2713aSLionel Sambuc 
381f4a2713aSLionel Sambuc   unsigned y = 50;
382f4a2713aSLionel Sambuc   SETBIT(bitset, y);
383f4a2713aSLionel Sambuc 
384f4a2713aSLionel Sambuc #define USER_SETBIT(set,bit) do { int i = bit; set[i/(8*sizeof(set[0]))] |= (1 << (i%(8*sizeof(set)))); } while(0)
385f4a2713aSLionel Sambuc   USER_SETBIT(bitset, 0); // expected-warning 2 {{implicit conversion changes signedness}}
386f4a2713aSLionel Sambuc }
387f4a2713aSLionel Sambuc 
388f4a2713aSLionel Sambuc // <rdar://problem/8559831>
389f4a2713aSLionel Sambuc enum E8559831a { E8559831a_val };
390f4a2713aSLionel Sambuc enum E8559831b { E8559831b_val };
391f4a2713aSLionel Sambuc typedef enum { E8559831c_val } E8559831c;
392f4a2713aSLionel Sambuc enum { E8559831d_val } value_d;
393f4a2713aSLionel Sambuc 
394f4a2713aSLionel Sambuc void test_8559831_a(enum E8559831a value);
test_8559831(enum E8559831b value_a,E8559831c value_c)395f4a2713aSLionel Sambuc void test_8559831(enum E8559831b value_a, E8559831c value_c) {
396f4a2713aSLionel Sambuc   test_8559831_a(value_a); // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}}
397f4a2713aSLionel Sambuc   enum E8559831a a1 = value_a; // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}}
398f4a2713aSLionel Sambuc   a1 = value_a; // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}}
399f4a2713aSLionel Sambuc 
400f4a2713aSLionel Sambuc   test_8559831_a(E8559831b_val); // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}}
401f4a2713aSLionel Sambuc   enum E8559831a a1a = E8559831b_val; // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}}
402f4a2713aSLionel Sambuc   a1 = E8559831b_val; // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}}
403f4a2713aSLionel Sambuc 
404f4a2713aSLionel Sambuc   test_8559831_a(value_c); // expected-warning{{implicit conversion from enumeration type 'E8559831c' to different enumeration type 'enum E8559831a'}}
405f4a2713aSLionel Sambuc   enum E8559831a a2 = value_c; // expected-warning{{implicit conversion from enumeration type 'E8559831c' to different enumeration type 'enum E8559831a'}}
406f4a2713aSLionel Sambuc   a2 = value_c; // expected-warning{{implicit conversion from enumeration type 'E8559831c' to different enumeration type 'enum E8559831a'}}
407f4a2713aSLionel Sambuc 
408f4a2713aSLionel Sambuc    test_8559831_a(value_d);
409f4a2713aSLionel Sambuc    enum E8559831a a3 = value_d;
410f4a2713aSLionel Sambuc    a3 = value_d;
411f4a2713aSLionel Sambuc }
412f4a2713aSLionel Sambuc 
test26(int si,long sl)413f4a2713aSLionel Sambuc void test26(int si, long sl) {
414f4a2713aSLionel Sambuc   si = sl % sl; // expected-warning {{implicit conversion loses integer precision: 'long' to 'int'}}
415f4a2713aSLionel Sambuc   si = sl % si;
416f4a2713aSLionel Sambuc   si = si % sl;
417f4a2713aSLionel Sambuc   si = si / sl;
418f4a2713aSLionel Sambuc   si = sl / si; // expected-warning {{implicit conversion loses integer precision: 'long' to 'int'}}
419f4a2713aSLionel Sambuc }
420*0a6a1f1dSLionel Sambuc 
421*0a6a1f1dSLionel Sambuc // rdar://16502418
422*0a6a1f1dSLionel Sambuc typedef unsigned short uint16_t;
423*0a6a1f1dSLionel Sambuc typedef unsigned int uint32_t;
424*0a6a1f1dSLionel Sambuc typedef __attribute__ ((ext_vector_type(16),__aligned__(32))) uint16_t ushort16;
425*0a6a1f1dSLionel Sambuc typedef __attribute__ ((ext_vector_type( 8),__aligned__( 32))) uint32_t uint8;
426*0a6a1f1dSLionel Sambuc 
test27(ushort16 constants)427*0a6a1f1dSLionel Sambuc void test27(ushort16 constants) {
428*0a6a1f1dSLionel Sambuc     uint8 pairedConstants = (uint8) constants;
429*0a6a1f1dSLionel Sambuc     ushort16 crCbScale = pairedConstants.s4; // expected-warning {{implicit conversion loses integer precision: 'uint32_t' (aka 'unsigned int') to 'ushort16'}}
430*0a6a1f1dSLionel Sambuc     ushort16 brBias = pairedConstants.s6; // expected-warning {{implicit conversion loses integer precision: 'uint32_t' (aka 'unsigned int') to 'ushort16'}}
431*0a6a1f1dSLionel Sambuc }
432