conversion.c revision 091f23f1d6d4bcffd6641cda72a6831e08c02ea7
105efad5f3a234639bb28a2483d38bbb4cb2b2370John McCall// RUN: %clang_cc1 -fsyntax-only -verify -Wconversion -nostdinc -isystem %S/Inputs -triple x86_64-apple-darwin %s -Wno-unreachable-code 2d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall 305efad5f3a234639bb28a2483d38bbb4cb2b2370John McCall#include <conversion.h> 4fb6289aac127b3752a5ebedfe05ba30dfee387f5John McCall 5d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall#define BIG 0x7f7f7f7f7f7f7f7fL 6d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall 7d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid test0(char c, short s, int i, long l, long long ll) { 8d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall c = c; 927f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor c = s; // expected-warning {{implicit conversion loses integer precision}} 1027f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor c = i; // expected-warning {{implicit conversion loses integer precision}} 1127f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor c = l; // expected-warning {{implicit conversion loses integer precision}} 12d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall s = c; 13d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall s = s; 1427f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor s = i; // expected-warning {{implicit conversion loses integer precision}} 1527f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor s = l; // expected-warning {{implicit conversion loses integer precision}} 16d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall i = c; 17d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall i = s; 18d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall i = i; 1927f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor i = l; // expected-warning {{implicit conversion loses integer precision}} 20d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall l = c; 21d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall l = s; 22d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall l = i; 23d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall l = l; 24d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall 25d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall c = (char) 0; 26d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall c = (short) 0; 27d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall c = (int) 0; 28d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall c = (long) 0; 29d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall s = (char) 0; 30d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall s = (short) 0; 31d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall s = (int) 0; 32d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall s = (long) 0; 33d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall i = (char) 0; 34d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall i = (short) 0; 35d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall i = (int) 0; 36d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall i = (long) 0; 37d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall l = (char) 0; 38d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall l = (short) 0; 39d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall l = (int) 0; 40d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall l = (long) 0; 41d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall 42d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall c = (char) BIG; 43091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall c = (short) BIG; // expected-warning {{implicit conversion from 'short' to 'char' changes value}} 44091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall c = (int) BIG; // expected-warning {{implicit conversion from 'int' to 'char' changes value}} 45091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall c = (long) BIG; // expected-warning {{implicit conversion from 'long' to 'char' changes value}} 46d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall s = (char) BIG; 47d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall s = (short) BIG; 48091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall s = (int) BIG; // expected-warning {{implicit conversion from 'int' to 'short' changes value}} 49091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall s = (long) BIG; // expected-warning {{implicit conversion from 'long' to 'short' changes value}} 50d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall i = (char) BIG; 51d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall i = (short) BIG; 52d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall i = (int) BIG; 53091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall i = (long) BIG; // expected-warning {{implicit conversion from 'long' to 'int' changes value}} 54d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall l = (char) BIG; 55d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall l = (short) BIG; 56d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall l = (int) BIG; 57d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall l = (long) BIG; 58d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall} 59d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall 60d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallchar test1(long long ll) { 6127f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor return (long long) ll; // expected-warning {{implicit conversion loses integer precision}} 6227f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor return (long) ll; // expected-warning {{implicit conversion loses integer precision}} 6327f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor return (int) ll; // expected-warning {{implicit conversion loses integer precision}} 6427f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor return (short) ll; // expected-warning {{implicit conversion loses integer precision}} 65d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (char) ll; 66091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall return (long long) BIG; // expected-warning {{implicit conversion from 'long long' to 'char' changes value}} 67091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall return (long) BIG; // expected-warning {{implicit conversion from 'long' to 'char' changes value}} 68091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall return (int) BIG; // expected-warning {{implicit conversion from 'int' to 'char' changes value}} 69091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall return (short) BIG; // expected-warning {{implicit conversion from 'short' to 'char' changes value}} 70d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (char) BIG; 71d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall} 72d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall 73d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallshort test2(long long ll) { 7427f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor return (long long) ll; // expected-warning {{implicit conversion loses integer precision}} 7527f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor return (long) ll; // expected-warning {{implicit conversion loses integer precision}} 7627f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor return (int) ll; // expected-warning {{implicit conversion loses integer precision}} 77d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (short) ll; 78d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (char) ll; 79091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall return (long long) BIG; // expected-warning {{implicit conversion from 'long long' to 'short' changes value}} 80091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall return (long) BIG; // expected-warning {{implicit conversion from 'long' to 'short' changes value}} 81091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall return (int) BIG; // expected-warning {{implicit conversion from 'int' to 'short' changes value}} 82d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (short) BIG; 83d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (char) BIG; 84d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall} 85d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall 86d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallint test3(long long ll) { 8727f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor return (long long) ll; // expected-warning {{implicit conversion loses integer precision}} 8827f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor return (long) ll; // expected-warning {{implicit conversion loses integer precision}} 89d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (int) ll; 90d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (short) ll; 91d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (char) ll; 92091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall return (long long) BIG; // expected-warning {{implicit conversion from 'long long' to 'int' changes value}} 93091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall return (long) BIG; // expected-warning {{implicit conversion from 'long' to 'int' changes value}} 94d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (int) BIG; 95d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (short) BIG; 96d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (char) BIG; 97d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall} 98d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall 99d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCalllong test4(long long ll) { 100d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (long long) ll; 101d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (long) ll; 102d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (int) ll; 103d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (short) ll; 104d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (char) ll; 105d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (long long) BIG; 106d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (long) BIG; 107d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (int) BIG; 108d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (short) BIG; 109d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (char) BIG; 110d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall} 111d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall 112d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCalllong long test5(long long ll) { 113d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (long long) ll; 114d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (long) ll; 115d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (int) ll; 116d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (short) ll; 117d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (char) ll; 118d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (long long) BIG; 119d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (long) BIG; 120d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (int) BIG; 121d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (short) BIG; 122d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall return (char) BIG; 123d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall} 124d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall 125d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid takes_char(char); 126d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid takes_short(short); 127d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid takes_int(int); 128d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid takes_long(long); 129d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid takes_longlong(long long); 130d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid takes_float(float); 131d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid takes_double(double); 132d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid takes_longdouble(long double); 133d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall 134d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid test6(char v) { 135d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_char(v); 136d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_short(v); 137d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_int(v); 138d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_long(v); 139d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_longlong(v); 140d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_float(v); 141d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_double(v); 142d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_longdouble(v); 143d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall} 144d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall 145d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid test7(short v) { 14627f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor takes_char(v); // expected-warning {{implicit conversion loses integer precision}} 147d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_short(v); 148d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_int(v); 149d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_long(v); 150d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_longlong(v); 151d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_float(v); 152d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_double(v); 153d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_longdouble(v); 154d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall} 155d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall 156d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid test8(int v) { 15727f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor takes_char(v); // expected-warning {{implicit conversion loses integer precision}} 15827f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor takes_short(v); // expected-warning {{implicit conversion loses integer precision}} 159d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_int(v); 160d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_long(v); 161d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_longlong(v); 162d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_float(v); 163d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_double(v); 164d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_longdouble(v); 165d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall} 166d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall 167d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid test9(long v) { 16827f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor takes_char(v); // expected-warning {{implicit conversion loses integer precision}} 16927f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor takes_short(v); // expected-warning {{implicit conversion loses integer precision}} 17027f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor takes_int(v); // expected-warning {{implicit conversion loses integer precision}} 171d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_long(v); 172d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_longlong(v); 173d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_float(v); 174d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_double(v); 175d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_longdouble(v); 176d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall} 177d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall 178d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid test10(long long v) { 17927f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor takes_char(v); // expected-warning {{implicit conversion loses integer precision}} 18027f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor takes_short(v); // expected-warning {{implicit conversion loses integer precision}} 18127f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor takes_int(v); // expected-warning {{implicit conversion loses integer precision}} 182d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_long(v); 183d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_longlong(v); 184d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_float(v); 185d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_double(v); 186d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_longdouble(v); 187d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall} 188d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall 189d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid test11(float v) { 19027f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor takes_char(v); // expected-warning {{implicit conversion turns floating-point number into integer}} 19127f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor takes_short(v); // expected-warning {{implicit conversion turns floating-point number into integer}} 19227f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor takes_int(v); // expected-warning {{implicit conversion turns floating-point number into integer}} 19327f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor takes_long(v); // expected-warning {{implicit conversion turns floating-point number into integer}} 19427f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor takes_longlong(v); // expected-warning {{implicit conversion turns floating-point number into integer}} 195d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_float(v); 196d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_double(v); 197d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_longdouble(v); 198d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall} 199d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall 200d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid test12(double v) { 20127f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor takes_char(v); // expected-warning {{implicit conversion turns floating-point number into integer}} 20227f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor takes_short(v); // expected-warning {{implicit conversion turns floating-point number into integer}} 20327f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor takes_int(v); // expected-warning {{implicit conversion turns floating-point number into integer}} 20427f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor takes_long(v); // expected-warning {{implicit conversion turns floating-point number into integer}} 20527f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor takes_longlong(v); // expected-warning {{implicit conversion turns floating-point number into integer}} 20627f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor takes_float(v); // expected-warning {{implicit conversion loses floating-point precision}} 207d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_double(v); 208d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_longdouble(v); 209d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall} 210d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall 211d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid test13(long double v) { 21227f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor takes_char(v); // expected-warning {{implicit conversion turns floating-point number into integer}} 21327f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor takes_short(v); // expected-warning {{implicit conversion turns floating-point number into integer}} 21427f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor takes_int(v); // expected-warning {{implicit conversion turns floating-point number into integer}} 21527f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor takes_long(v); // expected-warning {{implicit conversion turns floating-point number into integer}} 21627f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor takes_longlong(v); // expected-warning {{implicit conversion turns floating-point number into integer}} 21727f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor takes_float(v); // expected-warning {{implicit conversion loses floating-point precision}} 21827f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor takes_double(v); // expected-warning {{implicit conversion loses floating-point precision}} 219d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall takes_longdouble(v); 220d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall} 221d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall 222d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCallvoid test14(long l) { 223d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall // Fine because of the boolean whitelist. 224d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall char c; 225d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall c = (l == 4); 226d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall c = ((l <= 4) && (l >= 0)); 227d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall c = ((l <= 4) && (l >= 0)) || (l > 20); 228d5c376e4a034233a8efee81404041e33d8f0df5aJohn McCall} 229e8babd198c28bf1a443ef6a989dd2fe0ab58cc08John McCall 230e8babd198c28bf1a443ef6a989dd2fe0ab58cc08John McCallvoid test15(char c) { 231e8babd198c28bf1a443ef6a989dd2fe0ab58cc08John McCall c = c + 1 + c * 2; 23227f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor c = (short) c + 1 + c * 2; // expected-warning {{implicit conversion loses integer precision}} 233e8babd198c28bf1a443ef6a989dd2fe0ab58cc08John McCall} 2348406aedf4782771e520614ee379594dc0a4f7d5fJohn McCall 2358406aedf4782771e520614ee379594dc0a4f7d5fJohn McCall// PR 5422 2368406aedf4782771e520614ee379594dc0a4f7d5fJohn McCallextern void *test16_external; 2378406aedf4782771e520614ee379594dc0a4f7d5fJohn McCallvoid test16(void) { 23827f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor int a = (unsigned long) &test16_external; // expected-warning {{implicit conversion loses integer precision}} 2398406aedf4782771e520614ee379594dc0a4f7d5fJohn McCall} 240f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall 241f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall// PR 5938 242f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCallvoid test17() { 243f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall union { 244f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall unsigned long long a : 8; 245f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall unsigned long long b : 32; 246f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall unsigned long long c; 247f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall } U; 248f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall 249f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall unsigned int x; 250f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall x = U.a; 251f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall x = U.b; 25227f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor x = U.c; // expected-warning {{implicit conversion loses integer precision}} 253f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall} 254f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall 255f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall// PR 5939 256f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCallvoid test18() { 257f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall union { 258f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall unsigned long long a : 1; 259f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall unsigned long long b; 260f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall } U; 261f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall 262f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall int x; 263f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall x = (U.a ? 0 : 1); 264f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall x = (U.b ? 0 : 1); 265f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall} 26660fad45739b764886f707bd204eae9ecce6db1f2John McCall 26760fad45739b764886f707bd204eae9ecce6db1f2John McCall// None of these should warn. 26860fad45739b764886f707bd204eae9ecce6db1f2John McCallunsigned char test19(unsigned long u64) { 26960fad45739b764886f707bd204eae9ecce6db1f2John McCall unsigned char x1 = u64 & 0xff; 27060fad45739b764886f707bd204eae9ecce6db1f2John McCall unsigned char x2 = u64 >> 56; 27160fad45739b764886f707bd204eae9ecce6db1f2John McCall 27260fad45739b764886f707bd204eae9ecce6db1f2John McCall unsigned char mask = 0xee; 27360fad45739b764886f707bd204eae9ecce6db1f2John McCall unsigned char x3 = u64 & mask; 27460fad45739b764886f707bd204eae9ecce6db1f2John McCall return x1 + x2 + x3; 27560fad45739b764886f707bd204eae9ecce6db1f2John McCall} 276fb6289aac127b3752a5ebedfe05ba30dfee387f5John McCall 277fb6289aac127b3752a5ebedfe05ba30dfee387f5John McCall// <rdar://problem/7631400> 278fb6289aac127b3752a5ebedfe05ba30dfee387f5John McCallvoid test_7631400(void) { 279fb6289aac127b3752a5ebedfe05ba30dfee387f5John McCall // This should show up despite the caret being inside a macro substitution 280091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall char s = LONG_MAX; // expected-warning {{implicit conversion from 'long' to 'char' changes value}} 281fb6289aac127b3752a5ebedfe05ba30dfee387f5John McCall} 282c0cd21d2a3301a7a88e0052aebdd09c2441f826dJohn McCall 283c0cd21d2a3301a7a88e0052aebdd09c2441f826dJohn McCall// <rdar://problem/7676608>: assertion for compound operators with non-integral RHS 284c0cd21d2a3301a7a88e0052aebdd09c2441f826dJohn McCallvoid f7676608(int); 285c0cd21d2a3301a7a88e0052aebdd09c2441f826dJohn McCallvoid test_7676608(void) { 286c0cd21d2a3301a7a88e0052aebdd09c2441f826dJohn McCall float q = 0.7f; 287c0cd21d2a3301a7a88e0052aebdd09c2441f826dJohn McCall char c = 5; 288c0cd21d2a3301a7a88e0052aebdd09c2441f826dJohn McCall f7676608(c *= q); 289c0cd21d2a3301a7a88e0052aebdd09c2441f826dJohn McCall} 290323ed74658bc8375278eabf074b4777458376540John McCall 291323ed74658bc8375278eabf074b4777458376540John McCall// <rdar://problem/7904686> 292323ed74658bc8375278eabf074b4777458376540John McCallvoid test_7904686(void) { 293323ed74658bc8375278eabf074b4777458376540John McCall const int i = -1; 29427f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor unsigned u1 = i; // expected-warning {{implicit conversion changes signedness}} 29527f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor u1 = i; // expected-warning {{implicit conversion changes signedness}} 296323ed74658bc8375278eabf074b4777458376540John McCall 29727f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor unsigned u2 = -1; // expected-warning {{implicit conversion changes signedness}} 29827f46ee38d9a6a28cf0438218a198a9da1cee5d6Douglas Gregor u2 = -1; // expected-warning {{implicit conversion changes signedness}} 299323ed74658bc8375278eabf074b4777458376540John McCall} 300b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall 301b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall// <rdar://problem/8232669>: don't warn about conversions required by 302b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall// contexts in system headers 303b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCallvoid test_8232669(void) { 304b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall unsigned bitset[20]; 305b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall SETBIT(bitset, 0); 306b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall 307b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall unsigned y = 50; 308b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall SETBIT(bitset, y); 309b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall 310b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall#define USER_SETBIT(set,bit) do { int i = bit; set[i/(8*sizeof(set[0]))] |= (1 << (i%(8*sizeof(set)))); } while(0) 311b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall USER_SETBIT(bitset, 0); // expected-warning 2 {{implicit conversion changes signedness}} 312b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall} 313