blob: fc221893aeeea403f21ed5392670b76af5fa3a77 [file] [log] [blame]
Richard Trieube234c32016-04-21 21:04:55 +00001// 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
Richard Trieu393197a2014-04-22 01:01:05 +00004
Richard Trieube234c32016-04-21 21:04:55 +00005float ReturnFloat();
6
7#ifdef FLOAT_CONVERSION
Richard Trieu393197a2014-04-22 01:01:05 +00008bool ReturnBool(float f) {
9 return f; //expected-warning{{conversion}}
10}
11
12char ReturnChar(float f) {
13 return f; //expected-warning{{conversion}}
14}
15
16int ReturnInt(float f) {
17 return f; //expected-warning{{conversion}}
18}
19
20long ReturnLong(float f) {
21 return f; //expected-warning{{conversion}}
22}
23
24void 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
Richard Trieube234c32016-04-21 21:04:55 +000044void 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
51void 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
Richard Trieube234c32016-04-21 21:04:55 +000069#ifdef ZERO
70void 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
79void 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