1 // RUN: %clang_cc1 -verify -fsyntax-only -triple x86_64-pc-linux-gnu %s -Wno-literal-conversion -Wfloat-conversion -DFLOAT_CONVERSION -DZERO -DBOOL -DCONSTANT_BOOL -DOVERFLOW 2 // RUN: %clang_cc1 -verify -fsyntax-only -triple x86_64-pc-linux-gnu %s -Wno-conversion -Wfloat-overflow-conversion -DOVERFLOW 3 // RUN: %clang_cc1 -verify -fsyntax-only -triple x86_64-pc-linux-gnu %s -Wno-conversion -Wfloat-zero-conversion -DZERO 4 5 float ReturnFloat(); 6 7 #ifdef FLOAT_CONVERSION 8 bool ReturnBool(float f) { 9 return f; //expected-warning{{conversion}} 10 } 11 12 char ReturnChar(float f) { 13 return f; //expected-warning{{conversion}} 14 } 15 16 int ReturnInt(float f) { 17 return f; //expected-warning{{conversion}} 18 } 19 20 long ReturnLong(float f) { 21 return f; //expected-warning{{conversion}} 22 } 23 24 void Convert(float f, double d, long double ld) { 25 bool b; 26 char c; 27 int i; 28 long l; 29 30 b = f; //expected-warning{{conversion}} 31 b = d; //expected-warning{{conversion}} 32 b = ld; //expected-warning{{conversion}} 33 c = f; //expected-warning{{conversion}} 34 c = d; //expected-warning{{conversion}} 35 c = ld; //expected-warning{{conversion}} 36 i = f; //expected-warning{{conversion}} 37 i = d; //expected-warning{{conversion}} 38 i = ld; //expected-warning{{conversion}} 39 l = f; //expected-warning{{conversion}} 40 l = d; //expected-warning{{conversion}} 41 l = ld; //expected-warning{{conversion}} 42 } 43 44 void Test() { 45 int a1 = 10.0/2.0; //expected-warning{{conversion}} 46 int a2 = 1.0/2.0; //expected-warning{{conversion}} 47 bool a3 = ReturnFloat(); //expected-warning{{conversion}} 48 int a4 = 1e30 + 1; //expected-warning{{conversion}} 49 } 50 51 void TestConstantFloat() { 52 // Don't warn on exact floating literals. 53 int a1 = 5.0; 54 int a2 = 1e3; 55 56 int a3 = 5.5; // caught by -Wliteral-conversion 57 int a4 = 500.44; // caught by -Wliteral-convserion 58 59 int b1 = 5.0 / 1.0; //expected-warning{{conversion}} 60 int b2 = 5.0 / 2.0; //expected-warning{{conversion}} 61 62 const float five = 5.0; 63 64 int b3 = five / 1.0; //expected-warning{{conversion}} 65 int b4 = five / 2.0; //expected-warning{{conversion}} 66 67 int f = 2147483646.5 + 1; // expected-warning{{implicit conversion from 'double' to 'int' changes value from 2147483647.5 to 2147483647}} 68 unsigned g = -.5 + .01; // expected-warning{{implicit conversion from 'double' to 'unsigned int' changes non-zero value from -0.49 to 0}} 69 } 70 #endif // FLOAT_CONVERSION 71 72 #ifdef ZERO 73 void TestZero() { 74 const float half = .5; 75 int a1 = half; // expected-warning{{implicit conversion from 'const float' to 'int' changes non-zero value from 0.5 to 0}} 76 int a2 = 1.0 / 2.0; // expected-warning{{implicit conversion from 'double' to 'int' changes non-zero value from 0.5 to 0}} 77 int a3 = 5; 78 } 79 #endif // ZERO 80 81 #ifdef OVERFLOW 82 void TestOverflow() { 83 char a = 500.0; // caught by -Wliteral-conversion 84 char b = -500.0; // caught by -Wliteral-conversion 85 86 const float LargeNumber = 1024; 87 char c = LargeNumber; // expected-warning{{implicit conversion of out of range value from 'const float' to 'char' is undefined}} 88 char d = 400.0 + 400.0; // expected-warning{{implicit conversion of out of range value from 'double' to 'char' is undefined}} 89 90 char e = 1.0 / 0.0; // expected-warning{{implicit conversion of out of range value from 'double' to 'char' is undefined}} 91 } 92 93 94 template <typename T> 95 class Check { 96 public: 97 static constexpr bool Safe(); 98 }; 99 100 template<> 101 constexpr bool Check<char>::Safe() { return false; } 102 103 template<> 104 constexpr bool Check<float>::Safe() { return true; } 105 106 template <typename T> 107 T run1(T t) { 108 const float ret = 800; 109 return ret; // expected-warning {{implicit conversion of out of range value from 'const float' to 'char' is undefined}} 110 } 111 112 template <typename T> 113 T run2(T t) { 114 const float ret = 800; 115 if (Check<T>::Safe()) 116 return ret; 117 else 118 return t; 119 } 120 121 void test() { 122 float a = run1(a) + run2(a); 123 char b = run1(b) + run2(b); // expected-note {{in instantiation of function template specialization 'run1<char>' requested here}} 124 } 125 126 #endif // OVERFLOW 127