1a268fc0f2229eb132ebc8501b140093aeb5516bfDaniel Dunbar// RUN: %clang_cc1 -fsyntax-only -verify -Wconversion \
2a268fc0f2229eb132ebc8501b140093aeb5516bfDaniel Dunbar// RUN:   -nostdsysteminc -nobuiltininc -isystem %S/Inputs \
3a268fc0f2229eb132ebc8501b140093aeb5516bfDaniel Dunbar// RUN:   -triple x86_64-apple-darwin %s -Wno-unreachable-code
4d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall
505efad5f3a234639bb28a2483d38bbb4cb2b2370John McCall#include <conversion.h>
6fb6289aac127b3752a5ebedfe05ba30dfee387f5John McCall
7d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall#define BIG 0x7f7f7f7f7f7f7f7fL
8d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall
9d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid test0(char c, short s, int i, long l, long long ll) {
10d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  c = c;
1127f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  c = s; // expected-warning {{implicit conversion loses integer precision}}
1227f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  c = i; // expected-warning {{implicit conversion loses integer precision}}
1327f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  c = l; // expected-warning {{implicit conversion loses integer precision}}
14d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  s = c;
15d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  s = s;
1627f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  s = i; // expected-warning {{implicit conversion loses integer precision}}
1727f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  s = l; // expected-warning {{implicit conversion loses integer precision}}
18d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  i = c;
19d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  i = s;
20d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  i = i;
2127f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  i = l; // expected-warning {{implicit conversion loses integer precision}}
22d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  l = c;
23d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  l = s;
24d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  l = i;
25d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  l = l;
26d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall
27d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  c = (char) 0;
28d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  c = (short) 0;
29d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  c = (int) 0;
30d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  c = (long) 0;
31d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  s = (char) 0;
32d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  s = (short) 0;
33d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  s = (int) 0;
34d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  s = (long) 0;
35d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  i = (char) 0;
36d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  i = (short) 0;
37d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  i = (int) 0;
38d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  i = (long) 0;
39d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  l = (char) 0;
40d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  l = (short) 0;
41d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  l = (int) 0;
42d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  l = (long) 0;
43d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall
44d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  c = (char) BIG;
45091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall  c = (short) BIG; // expected-warning {{implicit conversion from 'short' to 'char' changes value}}
46091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall  c = (int) BIG; // expected-warning {{implicit conversion from 'int' to 'char' changes value}}
47091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall  c = (long) BIG; // expected-warning {{implicit conversion from 'long' to 'char' changes value}}
48d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  s = (char) BIG;
49d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  s = (short) BIG;
50091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall  s = (int) BIG; // expected-warning {{implicit conversion from 'int' to 'short' changes value}}
51091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall  s = (long) BIG; // expected-warning {{implicit conversion from 'long' to 'short' changes value}}
52d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  i = (char) BIG;
53d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  i = (short) BIG;
54d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  i = (int) BIG;
55091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall  i = (long) BIG; // expected-warning {{implicit conversion from 'long' to 'int' changes value}}
56d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  l = (char) BIG;
57d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  l = (short) BIG;
58d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  l = (int) BIG;
59d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  l = (long) BIG;
60d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall}
61d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall
62d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallchar test1(long long ll) {
6327f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  return (long long) ll; // expected-warning {{implicit conversion loses integer precision}}
6411005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
6511005638f651c2a0809b01abf1fd4025fb562021Ted Kremenekchar test1_a(long long ll) {
6627f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  return (long) ll; // expected-warning {{implicit conversion loses integer precision}}
6711005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
6811005638f651c2a0809b01abf1fd4025fb562021Ted Kremenekchar test1_b(long long ll) {
6927f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  return (int) ll; // expected-warning {{implicit conversion loses integer precision}}
7011005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
7111005638f651c2a0809b01abf1fd4025fb562021Ted Kremenekchar test1_c(long long ll) {
7227f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  return (short) ll; // expected-warning {{implicit conversion loses integer precision}}
7311005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
7411005638f651c2a0809b01abf1fd4025fb562021Ted Kremenekchar test1_d(long long ll) {
75d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (char) ll;
7611005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
7711005638f651c2a0809b01abf1fd4025fb562021Ted Kremenekchar test1_e(long long ll) {
78091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall  return (long long) BIG; // expected-warning {{implicit conversion from 'long long' to 'char' changes value}}
7911005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
8011005638f651c2a0809b01abf1fd4025fb562021Ted Kremenekchar test1_f(long long ll) {
81091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall  return (long) BIG; // expected-warning {{implicit conversion from 'long' to 'char' changes value}}
8211005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
8311005638f651c2a0809b01abf1fd4025fb562021Ted Kremenekchar test1_g(long long ll) {
84091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall  return (int) BIG; // expected-warning {{implicit conversion from 'int' to 'char' changes value}}
8511005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
8611005638f651c2a0809b01abf1fd4025fb562021Ted Kremenekchar test1_h(long long ll) {
87091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall  return (short) BIG; // expected-warning {{implicit conversion from 'short' to 'char' changes value}}
8811005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
8911005638f651c2a0809b01abf1fd4025fb562021Ted Kremenekchar test1_i(long long ll) {
90d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (char) BIG;
91d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall}
92d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall
93d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallshort test2(long long ll) {
9427f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  return (long long) ll; // expected-warning {{implicit conversion loses integer precision}}
9511005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
9611005638f651c2a0809b01abf1fd4025fb562021Ted Kremenekshort test2_a(long long ll) {
9727f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  return (long) ll; // expected-warning {{implicit conversion loses integer precision}}
9811005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
9911005638f651c2a0809b01abf1fd4025fb562021Ted Kremenekshort test2_b(long long ll) {
10027f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  return (int) ll; // expected-warning {{implicit conversion loses integer precision}}
10111005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
10211005638f651c2a0809b01abf1fd4025fb562021Ted Kremenekshort test2_c(long long ll) {
103d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (short) ll;
10411005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
10511005638f651c2a0809b01abf1fd4025fb562021Ted Kremenekshort test2_d(long long ll) {
106d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (char) ll;
10711005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
10811005638f651c2a0809b01abf1fd4025fb562021Ted Kremenekshort test2_e(long long ll) {
109091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall  return (long long) BIG;  // expected-warning {{implicit conversion from 'long long' to 'short' changes value}}
11011005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
11111005638f651c2a0809b01abf1fd4025fb562021Ted Kremenekshort test2_f(long long ll) {
112091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall  return (long) BIG;  // expected-warning {{implicit conversion from 'long' to 'short' changes value}}
11311005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
11411005638f651c2a0809b01abf1fd4025fb562021Ted Kremenekshort test2_g(long long ll) {
115091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall  return (int) BIG;  // expected-warning {{implicit conversion from 'int' to 'short' changes value}}
11611005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
11711005638f651c2a0809b01abf1fd4025fb562021Ted Kremenekshort test2_h(long long ll) {
118d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (short) BIG;
11911005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
12011005638f651c2a0809b01abf1fd4025fb562021Ted Kremenekshort test2_i(long long ll) {
121d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (char) BIG;
122d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall}
123d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall
124d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallint test3(long long ll) {
12527f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  return (long long) ll;  // expected-warning {{implicit conversion loses integer precision}}
12611005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
12711005638f651c2a0809b01abf1fd4025fb562021Ted Kremenekint test3_b(long long ll) {
12827f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  return (long) ll;  // expected-warning {{implicit conversion loses integer precision}}
12911005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
13011005638f651c2a0809b01abf1fd4025fb562021Ted Kremenekint test3_c(long long ll) {
131d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (int) ll;
13211005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
13311005638f651c2a0809b01abf1fd4025fb562021Ted Kremenekint test3_d(long long ll) {
134d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (short) ll;
13511005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
13611005638f651c2a0809b01abf1fd4025fb562021Ted Kremenekint test3_e(long long ll) {
137d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (char) ll;
13811005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
13911005638f651c2a0809b01abf1fd4025fb562021Ted Kremenekint test3_f(long long ll) {
140091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall  return (long long) BIG;  // expected-warning {{implicit conversion from 'long long' to 'int' changes value}}
14111005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
14211005638f651c2a0809b01abf1fd4025fb562021Ted Kremenekint test3_g(long long ll) {
143091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall  return (long) BIG; // expected-warning {{implicit conversion from 'long' to 'int' changes value}}
14411005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
14511005638f651c2a0809b01abf1fd4025fb562021Ted Kremenekint test3_h(long long ll) {
146d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (int) BIG;
14711005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
14811005638f651c2a0809b01abf1fd4025fb562021Ted Kremenekint test3_i(long long ll) {
149d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (short) BIG;
15011005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
15111005638f651c2a0809b01abf1fd4025fb562021Ted Kremenekint test3_j(long long ll) {
152d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (char) BIG;
153d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall}
154d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall
155d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCalllong test4(long long ll) {
156d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (long long) ll;
15711005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
15811005638f651c2a0809b01abf1fd4025fb562021Ted Kremeneklong test4_a(long long ll) {
159d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (long) ll;
16011005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
16111005638f651c2a0809b01abf1fd4025fb562021Ted Kremeneklong test4_b(long long ll) {
162d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (int) ll;
16311005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
16411005638f651c2a0809b01abf1fd4025fb562021Ted Kremeneklong test4_c(long long ll) {
165d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (short) ll;
16611005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
16711005638f651c2a0809b01abf1fd4025fb562021Ted Kremeneklong test4_d(long long ll) {
168d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (char) ll;
16911005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
17011005638f651c2a0809b01abf1fd4025fb562021Ted Kremeneklong test4_e(long long ll) {
171d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (long long) BIG;
17211005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
17311005638f651c2a0809b01abf1fd4025fb562021Ted Kremeneklong test4_f(long long ll) {
174d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (long) BIG;
17511005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
17611005638f651c2a0809b01abf1fd4025fb562021Ted Kremeneklong test4_g(long long ll) {
177d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (int) BIG;
17811005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
17911005638f651c2a0809b01abf1fd4025fb562021Ted Kremeneklong test4_h(long long ll) {
180d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (short) BIG;
18111005638f651c2a0809b01abf1fd4025fb562021Ted Kremenek}
18211005638f651c2a0809b01abf1fd4025fb562021Ted Kremeneklong test4_i(long long ll) {
183d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (char) BIG;
184d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall}
185d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall
186d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCalllong long test5(long long ll) {
187d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (long long) ll;
188d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (long) ll;
189d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (int) ll;
190d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (short) ll;
191d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (char) ll;
192d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (long long) BIG;
193d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (long) BIG;
194d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (int) BIG;
195d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (short) BIG;
196d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  return (char) BIG;
197d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall}
198d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall
199d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid takes_char(char);
200d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid takes_short(short);
201d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid takes_int(int);
202d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid takes_long(long);
203d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid takes_longlong(long long);
204d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid takes_float(float);
205d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid takes_double(double);
206d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid takes_longdouble(long double);
207d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall
208d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid test6(char v) {
209d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_char(v);
210d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_short(v);
211d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_int(v);
212d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_long(v);
213d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_longlong(v);
214d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_float(v);
215d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_double(v);
216d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_longdouble(v);
217d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall}
218d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall
219d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid test7(short v) {
22027f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  takes_char(v); // expected-warning {{implicit conversion loses integer precision}}
221d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_short(v);
222d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_int(v);
223d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_long(v);
224d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_longlong(v);
225d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_float(v);
226d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_double(v);
227d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_longdouble(v);
228d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall}
229d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall
230d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid test8(int v) {
23127f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  takes_char(v); // expected-warning {{implicit conversion loses integer precision}}
23227f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  takes_short(v); // expected-warning {{implicit conversion loses integer precision}}
233d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_int(v);
234d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_long(v);
235d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_longlong(v);
236d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_float(v);
237d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_double(v);
238d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_longdouble(v);
239d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall}
240d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall
241d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid test9(long v) {
24227f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  takes_char(v); // expected-warning {{implicit conversion loses integer precision}}
24327f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  takes_short(v); // expected-warning {{implicit conversion loses integer precision}}
24427f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  takes_int(v); // expected-warning {{implicit conversion loses integer precision}}
245d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_long(v);
246d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_longlong(v);
247d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_float(v);
248d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_double(v);
249d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_longdouble(v);
250d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall}
251d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall
252d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid test10(long long v) {
25327f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  takes_char(v); // expected-warning {{implicit conversion loses integer precision}}
25427f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  takes_short(v); // expected-warning {{implicit conversion loses integer precision}}
25527f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  takes_int(v); // expected-warning {{implicit conversion loses integer precision}}
256d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_long(v);
257d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_longlong(v);
258d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_float(v);
259d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_double(v);
260d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_longdouble(v);
261d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall}
262d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall
263d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid test11(float v) {
26427f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  takes_char(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
26527f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  takes_short(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
26627f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  takes_int(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
26727f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  takes_long(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
26827f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  takes_longlong(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
269d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_float(v);
270d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_double(v);
271d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_longdouble(v);
272d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall}
273d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall
274d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid test12(double v) {
27527f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  takes_char(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
27627f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  takes_short(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
27727f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  takes_int(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
27827f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  takes_long(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
27927f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  takes_longlong(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
28027f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  takes_float(v); // expected-warning {{implicit conversion loses floating-point precision}}
281d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_double(v);
282d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_longdouble(v);
283d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall}
284d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall
285d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid test13(long double v) {
28627f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  takes_char(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
28727f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  takes_short(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
28827f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  takes_int(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
28927f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  takes_long(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
29027f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  takes_longlong(v); // expected-warning {{implicit conversion turns floating-point number into integer}}
29127f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  takes_float(v); // expected-warning {{implicit conversion loses floating-point precision}}
29227f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  takes_double(v); // expected-warning {{implicit conversion loses floating-point precision}}
293d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  takes_longdouble(v);
294d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall}
295d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall
296d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid test14(long l) {
297d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  // Fine because of the boolean whitelist.
298d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  char c;
299d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  c = (l == 4);
300d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  c = ((l <= 4) && (l >= 0));
301d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall  c = ((l <= 4) && (l >= 0)) || (l > 20);
302d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall}
303e8babd198c28bf1a443ef6a989dd2fe0ab58cc08John McCall
304e8babd198c28bf1a443ef6a989dd2fe0ab58cc08John McCallvoid test15(char c) {
305e8babd198c28bf1a443ef6a989dd2fe0ab58cc08John McCall  c = c + 1 + c * 2;
30627f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  c = (short) c + 1 + c * 2; // expected-warning {{implicit conversion loses integer precision}}
307e8babd198c28bf1a443ef6a989dd2fe0ab58cc08John McCall}
3088406aedf4782771e520614ee379594dc0a4f7d5fJohn McCall
3098406aedf4782771e520614ee379594dc0a4f7d5fJohn McCall// PR 5422
3108406aedf4782771e520614ee379594dc0a4f7d5fJohn McCallextern void *test16_external;
3118406aedf4782771e520614ee379594dc0a4f7d5fJohn McCallvoid test16(void) {
31227f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  int a = (unsigned long) &test16_external; // expected-warning {{implicit conversion loses integer precision}}
3138406aedf4782771e520614ee379594dc0a4f7d5fJohn McCall}
314f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall
315f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall// PR 5938
316f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCallvoid test17() {
317f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall  union {
318f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall    unsigned long long a : 8;
319f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall    unsigned long long b : 32;
320f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall    unsigned long long c;
321f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall  } U;
322f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall
323f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall  unsigned int x;
324f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall  x = U.a;
325f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall  x = U.b;
32627f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  x = U.c; // expected-warning {{implicit conversion loses integer precision}}
327f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall}
328f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall
329f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall// PR 5939
330f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCallvoid test18() {
331f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall  union {
332f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall    unsigned long long a : 1;
333f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall    unsigned long long b;
334f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall  } U;
335f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall
336f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall  int x;
337f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall  x = (U.a ? 0 : 1);
338f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall  x = (U.b ? 0 : 1);
339f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall}
34060fad45739b764886f707bd204eae9ecce6db1f2John McCall
34160fad45739b764886f707bd204eae9ecce6db1f2John McCall// None of these should warn.
34260fad45739b764886f707bd204eae9ecce6db1f2John McCallunsigned char test19(unsigned long u64) {
34360fad45739b764886f707bd204eae9ecce6db1f2John McCall  unsigned char x1 = u64 & 0xff;
34460fad45739b764886f707bd204eae9ecce6db1f2John McCall  unsigned char x2 = u64 >> 56;
34560fad45739b764886f707bd204eae9ecce6db1f2John McCall
34660fad45739b764886f707bd204eae9ecce6db1f2John McCall  unsigned char mask = 0xee;
34760fad45739b764886f707bd204eae9ecce6db1f2John McCall  unsigned char x3 = u64 & mask;
34860fad45739b764886f707bd204eae9ecce6db1f2John McCall  return x1 + x2 + x3;
34960fad45739b764886f707bd204eae9ecce6db1f2John McCall}
350fb6289aac127b3752a5ebedfe05ba30dfee387f5John McCall
351fb6289aac127b3752a5ebedfe05ba30dfee387f5John McCall// <rdar://problem/7631400>
352fb6289aac127b3752a5ebedfe05ba30dfee387f5John McCallvoid test_7631400(void) {
353fb6289aac127b3752a5ebedfe05ba30dfee387f5John McCall  // This should show up despite the caret being inside a macro substitution
354091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall  char s = LONG_MAX; // expected-warning {{implicit conversion from 'long' to 'char' changes value}}
355fb6289aac127b3752a5ebedfe05ba30dfee387f5John McCall}
356c0cd21d2a3301a7a88e0052aebdd09c2441f826dJohn McCall
357c0cd21d2a3301a7a88e0052aebdd09c2441f826dJohn McCall// <rdar://problem/7676608>: assertion for compound operators with non-integral RHS
358c0cd21d2a3301a7a88e0052aebdd09c2441f826dJohn McCallvoid f7676608(int);
359c0cd21d2a3301a7a88e0052aebdd09c2441f826dJohn McCallvoid test_7676608(void) {
360c0cd21d2a3301a7a88e0052aebdd09c2441f826dJohn McCall  float q = 0.7f;
361c0cd21d2a3301a7a88e0052aebdd09c2441f826dJohn McCall  char c = 5;
362c0cd21d2a3301a7a88e0052aebdd09c2441f826dJohn McCall  f7676608(c *= q);
363c0cd21d2a3301a7a88e0052aebdd09c2441f826dJohn McCall}
364323ed74658bc8375278eabf074b4777458376540John McCall
365323ed74658bc8375278eabf074b4777458376540John McCall// <rdar://problem/7904686>
366323ed74658bc8375278eabf074b4777458376540John McCallvoid test_7904686(void) {
367323ed74658bc8375278eabf074b4777458376540John McCall  const int i = -1;
36827f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  unsigned u1 = i; // expected-warning {{implicit conversion changes signedness}}
36927f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  u1 = i; // expected-warning {{implicit conversion changes signedness}}
370323ed74658bc8375278eabf074b4777458376540John McCall
37127f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  unsigned u2 = -1; // expected-warning {{implicit conversion changes signedness}}
37227f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor  u2 = -1; // expected-warning {{implicit conversion changes signedness}}
373323ed74658bc8375278eabf074b4777458376540John McCall}
374b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall
375b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall// <rdar://problem/8232669>: don't warn about conversions required by
376b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall// contexts in system headers
377b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCallvoid test_8232669(void) {
378b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall  unsigned bitset[20];
379b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall  SETBIT(bitset, 0);
380b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall
381b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall  unsigned y = 50;
382b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall  SETBIT(bitset, y);
383b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall
384b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall#define USER_SETBIT(set,bit) do { int i = bit; set[i/(8*sizeof(set[0]))] |= (1 << (i%(8*sizeof(set)))); } while(0)
385b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall  USER_SETBIT(bitset, 0); // expected-warning 2 {{implicit conversion changes signedness}}
386b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall}
387284cc8d8a90ae6558e0a4b60b7dc1ddcfd220758Douglas Gregor
388284cc8d8a90ae6558e0a4b60b7dc1ddcfd220758Douglas Gregor// <rdar://problem/8559831>
389284cc8d8a90ae6558e0a4b60b7dc1ddcfd220758Douglas Gregorenum E8559831a { E8559831a_val };
390284cc8d8a90ae6558e0a4b60b7dc1ddcfd220758Douglas Gregorenum E8559831b { E8559831b_val };
391284cc8d8a90ae6558e0a4b60b7dc1ddcfd220758Douglas Gregortypedef enum { E8559831c_val } E8559831c;
392284cc8d8a90ae6558e0a4b60b7dc1ddcfd220758Douglas Gregorenum { E8559831d_val } value_d;
393284cc8d8a90ae6558e0a4b60b7dc1ddcfd220758Douglas Gregor
394284cc8d8a90ae6558e0a4b60b7dc1ddcfd220758Douglas Gregorvoid test_8559831_a(enum E8559831a value);
395284cc8d8a90ae6558e0a4b60b7dc1ddcfd220758Douglas Gregorvoid test_8559831(enum E8559831b value_a, E8559831c value_c) {
396284cc8d8a90ae6558e0a4b60b7dc1ddcfd220758Douglas Gregor  test_8559831_a(value_a); // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}}
397284cc8d8a90ae6558e0a4b60b7dc1ddcfd220758Douglas Gregor  enum E8559831a a1 = value_a; // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}}
398284cc8d8a90ae6558e0a4b60b7dc1ddcfd220758Douglas Gregor  a1 = value_a; // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}}
399284cc8d8a90ae6558e0a4b60b7dc1ddcfd220758Douglas Gregor
4005a5b38f4afaf4f203b96a11ba79890c7cd4cc4b8Douglas Gregor  test_8559831_a(E8559831b_val); // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}}
4015a5b38f4afaf4f203b96a11ba79890c7cd4cc4b8Douglas Gregor  enum E8559831a a1a = E8559831b_val; // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}}
4025a5b38f4afaf4f203b96a11ba79890c7cd4cc4b8Douglas Gregor  a1 = E8559831b_val; // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}}
4035a5b38f4afaf4f203b96a11ba79890c7cd4cc4b8Douglas Gregor
404284cc8d8a90ae6558e0a4b60b7dc1ddcfd220758Douglas Gregor  test_8559831_a(value_c); // expected-warning{{implicit conversion from enumeration type 'E8559831c' to different enumeration type 'enum E8559831a'}}
405284cc8d8a90ae6558e0a4b60b7dc1ddcfd220758Douglas Gregor  enum E8559831a a2 = value_c; // expected-warning{{implicit conversion from enumeration type 'E8559831c' to different enumeration type 'enum E8559831a'}}
406284cc8d8a90ae6558e0a4b60b7dc1ddcfd220758Douglas Gregor  a2 = value_c; // expected-warning{{implicit conversion from enumeration type 'E8559831c' to different enumeration type 'enum E8559831a'}}
407284cc8d8a90ae6558e0a4b60b7dc1ddcfd220758Douglas Gregor
408284cc8d8a90ae6558e0a4b60b7dc1ddcfd220758Douglas Gregor   test_8559831_a(value_d);
409284cc8d8a90ae6558e0a4b60b7dc1ddcfd220758Douglas Gregor   enum E8559831a a3 = value_d;
410284cc8d8a90ae6558e0a4b60b7dc1ddcfd220758Douglas Gregor   a3 = value_d;
411284cc8d8a90ae6558e0a4b60b7dc1ddcfd220758Douglas Gregor}
41200fe761849278180b0d73f276e468cf4c54ff2adJohn McCall
41300fe761849278180b0d73f276e468cf4c54ff2adJohn McCallvoid test26(int si, long sl) {
41400fe761849278180b0d73f276e468cf4c54ff2adJohn McCall  si = sl % sl; // expected-warning {{implicit conversion loses integer precision: 'long' to 'int'}}
41500fe761849278180b0d73f276e468cf4c54ff2adJohn McCall  si = sl % si;
41600fe761849278180b0d73f276e468cf4c54ff2adJohn McCall  si = si % sl;
41700fe761849278180b0d73f276e468cf4c54ff2adJohn McCall  si = si / sl;
41800fe761849278180b0d73f276e468cf4c54ff2adJohn McCall  si = sl / si; // expected-warning {{implicit conversion loses integer precision: 'long' to 'int'}}
41900fe761849278180b0d73f276e468cf4c54ff2adJohn McCall}
420