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
7 #ifdef FLOAT_CONVERSION
8 bool ReturnBool(float f
) {
9 return f
; //expected-warning{{conversion}}
12 char ReturnChar(float f
) {
13 return f
; //expected-warning{{conversion}}
16 int ReturnInt(float f
) {
17 return f
; //expected-warning{{conversion}}
20 long ReturnLong(float f
) {
21 return f
; //expected-warning{{conversion}}
24 void Convert(float f
, double d
, long double ld
) {
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}}
44 void CompoundAssignment() {
47 x
+= 1.234; // expected-warning {{implicit conversion turns floating-point number into integer: 'double' to 'int'}}
48 x
-= -0.0; // expected-warning {{implicit conversion turns floating-point number into integer: 'double' to 'int'}}
49 x
*= 1.1f
; // expected-warning {{implicit conversion turns floating-point number into integer: 'float' to 'int'}}
50 x
/= -2.2f
; // expected-warning {{implicit conversion turns floating-point number into integer: 'float' to 'int'}}
52 int y
= x
+= 1.4f
; // expected-warning {{implicit conversion turns floating-point number into integer: 'float' to 'int'}}
57 y
+= z
+ w
; // expected-warning {{implicit conversion turns floating-point number into integer: 'double' to 'int'}}
61 // ^ the following text comes from a system header file.
62 #define SYSTEM_MACRO_FLOAT(x) do { (x) += 1.1; } while(0)
63 # 1 "warn-float-conversion.cpp" 1
64 // ^ start of a new file.
67 SYSTEM_MACRO_FLOAT(x
);
71 int a1
= 10.0/2.0; //expected-warning{{conversion}}
72 int a2
= 1.0/2.0; //expected-warning{{conversion}}
73 bool a3
= ReturnFloat(); //expected-warning{{conversion}}
74 int a4
= 1e30
+ 1; //expected-warning{{conversion}}
77 void TestConstantFloat() {
78 // Don't warn on exact floating literals.
82 int a3
= 5.5; // caught by -Wliteral-conversion
83 int a4
= 500.44; // caught by -Wliteral-convserion
85 int b1
= 5.0 / 1.0; //expected-warning{{conversion}}
86 int b2
= 5.0 / 2.0; //expected-warning{{conversion}}
88 const float five
= 5.0;
90 int b3
= five
/ 1.0; //expected-warning{{conversion}}
91 int b4
= five
/ 2.0; //expected-warning{{conversion}}
93 int f
= 2147483646.5 + 1; // expected-warning{{implicit conversion from 'double' to 'int' changes value from 2147483647.5 to 2147483647}}
94 unsigned g
= -.5 + .01; // expected-warning{{implicit conversion from 'double' to 'unsigned int' changes non-zero value from -0.49 to 0}}
96 #endif // FLOAT_CONVERSION
100 const float half
= .5;
101 int a1
= half
; // expected-warning{{implicit conversion from 'const float' to 'int' changes non-zero value from 0.5 to 0}}
102 int a2
= 1.0 / 2.0; // expected-warning{{implicit conversion from 'double' to 'int' changes non-zero value from 0.5 to 0}}
108 void TestOverflow() {
109 char a
= 500.0; // caught by -Wliteral-conversion
110 char b
= -500.0; // caught by -Wliteral-conversion
112 const float LargeNumber
= 1024;
113 char c
= LargeNumber
; // expected-warning{{implicit conversion of out of range value from 'const float' to 'char' is undefined}}
114 char d
= 400.0 + 400.0; // expected-warning{{implicit conversion of out of range value from 'double' to 'char' is undefined}}
116 char e
= 1.0 / 0.0; // expected-warning{{implicit conversion of out of range value from 'double' to 'char' is undefined}}
120 template <typename T
>
123 static constexpr bool Safe();
127 constexpr bool Check
<char>::Safe() { return false; }
130 constexpr bool Check
<float>::Safe() { return true; }
132 template <typename T
>
134 const float ret
= 800;
135 return ret
; // expected-warning {{implicit conversion of out of range value from 'const float' to 'char' is undefined}}
138 template <typename T
>
140 const float ret
= 800;
141 if (Check
<T
>::Safe())
148 float a
= run1(a
) + run2(a
);
149 char b
= run1(b
) + run2(b
); // expected-note {{in instantiation of function template specialization 'run1<char>' requested here}}