Home | History | Annotate | Download | only in SemaCXX
      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 #endif  // FLOAT_CONVERSION
     68 
     69 #ifdef ZERO
     70 void TestZero() {
     71   const float half = .5;
     72   int a1 = half;  // expected-warning{{implicit conversion from 'const float' to 'int' changes non-zero value from 0.5 to 0}}
     73   int a2 = 1.0 / 2.0;  // expected-warning{{implicit conversion from 'double' to 'int' changes non-zero value from 0.5 to 0}}
     74   int a3 = 5;
     75 }
     76 #endif  // ZERO
     77 
     78 #ifdef OVERFLOW
     79 void TestOverflow() {
     80   char a = 500.0;  // caught by -Wliteral-conversion
     81   char b = -500.0;  // caught by -Wliteral-conversion
     82 
     83   const float LargeNumber = 1024;
     84   char c = LargeNumber;  // expected-warning{{implicit conversion of out of range value from 'const float' to 'char' changes value from 1024 to 127}}
     85   char d = 400.0 + 400.0;  // expected-warning{{implicit conversion of out of range value from 'double' to 'char' changes value from 800 to 127}}
     86 
     87   char e = 1.0 / 0.0;  // expected-warning{{implicit conversion of out of range value from 'double' to 'char' changes value from +Inf to 127}}
     88 }
     89 #endif  // OVERFLOW
     90