diff options
author | Richard Trieu <rtrieu@google.com> | 2016-04-21 21:04:55 +0000 |
---|---|---|
committer | Richard Trieu <rtrieu@google.com> | 2016-04-21 21:04:55 +0000 |
commit | be234c30ada79e38b47ee7b58abf4c87d419a60f (patch) | |
tree | fa97f34720cb6aeda17f9e2eebcea2b93f003a55 /clang/test/SemaCXX/warn-float-conversion.cpp | |
parent | 1725bde4cc55419ee8758b6f8fd42eec32fb0513 (diff) | |
download | bcm5719-llvm-be234c30ada79e38b47ee7b58abf4c87d419a60f.tar.gz bcm5719-llvm-be234c30ada79e38b47ee7b58abf4c87d419a60f.zip |
Split interesting warnings off from -Wfloat-conversion
Restructure the implict floating point to integer conversions so that
interesting sub-groups are under different flags. Breakdown of warnings:
No warning:
Exact conversions from floating point to integer:
int x = 10.0;
int x = 1e10;
-Wliteral-conversion - Floating point literal to integer with rounding:
int x = 5.5;
int x = -3.4;
-Wfloat-conversion - All conversions not covered by the above two:
int x = GetFloat();
int x = 5.5 + 3.5;
-Wfloat-zero-conversion - The expression converted has a non-zero floating
point value that gets converted to a zero integer value, excluded the cases
falling under -Wliteral-conversion. Subset of -Wfloat-conversion.
int x = 1.0 / 2.0;
-Wfloat-overflow-conversion - The floating point value is outside the range
of the integer type, exluding cases from -Wliteral conversion. Subset of
-Wfloat-conversion.
char x = 500;
char x = -1000;
-Wfloat-bool-conversion - Any conversion of a floating point type to bool.
Subset of -Wfloat-conversion.
if (GetFloat()) {}
bool x = 5.0;
-Wfloat-bool-constant-conversion - Conversion of a compile time evaluatable
floating point value to bool. Subset of -Wfloat-bool-conversion.
bool x = 1.0;
bool x = 4.0 / 20.0;
Also add EvaluateAsFloat to Sema, which is similar to EvaluateAsInt, but for
floating point values.
llvm-svn: 267054
Diffstat (limited to 'clang/test/SemaCXX/warn-float-conversion.cpp')
-rw-r--r-- | clang/test/SemaCXX/warn-float-conversion.cpp | 86 |
1 files changed, 85 insertions, 1 deletions
diff --git a/clang/test/SemaCXX/warn-float-conversion.cpp b/clang/test/SemaCXX/warn-float-conversion.cpp index 22c33040b26..5a5532572a8 100644 --- a/clang/test/SemaCXX/warn-float-conversion.cpp +++ b/clang/test/SemaCXX/warn-float-conversion.cpp @@ -1,5 +1,12 @@ -// RUN: %clang_cc1 -verify -fsyntax-only %s -Wfloat-conversion +// 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 +// RUN: %clang_cc1 -verify -fsyntax-only -triple x86_64-pc-linux-gnu %s -Wno-conversion -Wfloat-overflow-conversion -DOVERFLOW +// RUN: %clang_cc1 -verify -fsyntax-only -triple x86_64-pc-linux-gnu %s -Wno-conversion -Wfloat-zero-conversion -DZERO +// RUN: %clang_cc1 -verify -fsyntax-only -triple x86_64-pc-linux-gnu %s -Wno-conversion -Wfloat-bool-constant-conversion -DCONSTANT_BOOL +// RUN: %clang_cc1 -verify -fsyntax-only -triple x86_64-pc-linux-gnu %s -Wno-conversion -Wfloat-bool-conversion -DCONSTANT_BOOL -DBOOL +float ReturnFloat(); + +#ifdef FLOAT_CONVERSION bool ReturnBool(float f) { return f; //expected-warning{{conversion}} } @@ -36,3 +43,80 @@ void Convert(float f, double d, long double ld) { l = ld; //expected-warning{{conversion}} } +void Test() { + int a1 = 10.0/2.0; //expected-warning{{conversion}} + int a2 = 1.0/2.0; //expected-warning{{conversion}} + bool a3 = ReturnFloat(); //expected-warning{{conversion}} + int a4 = 1e30 + 1; //expected-warning{{conversion}} +} + +void TestConstantFloat() { + // Don't warn on exact floating literals. + int a1 = 5.0; + int a2 = 1e3; + + int a3 = 5.5; // caught by -Wliteral-conversion + int a4 = 500.44; // caught by -Wliteral-convserion + + int b1 = 5.0 / 1.0; //expected-warning{{conversion}} + int b2 = 5.0 / 2.0; //expected-warning{{conversion}} + + const float five = 5.0; + + int b3 = five / 1.0; //expected-warning{{conversion}} + int b4 = five / 2.0; //expected-warning{{conversion}} +} +#endif // FLOAT_CONVERSION + +#ifdef CONSTANT_BOOL +const float pi = 3.1415; + +void TestConstantBool() { + bool b1 = 0.99f; // expected-warning {{implicit conversion from 'float' to 'bool' changes value from 0.99 to true}} + bool b2 = 0.99; // expected-warning {{implicit conversion from 'double' to 'bool' changes value from 0.99 to true}} + bool b3 = 0.0f; // expected-warning {{implicit conversion from 'float' to 'bool' changes value from 0 to false}} + bool b4 = 0.0; // expected-warning {{implicit conversion from 'double' to 'bool' changes value from 0 to false}} + bool b5 = 1.0f; // expected-warning {{implicit conversion from 'float' to 'bool' changes value from 1 to true}} + bool b6 = 1.0; // expected-warning {{implicit conversion from 'double' to 'bool' changes value from 1 to true}} + bool b7 = pi; // expected-warning {{implicit conversion from 'const float' to 'bool' changes value from 3.1415 to true}} + bool b8 = pi - pi; // expected-warning {{implicit conversion from 'float' to 'bool' changes value from 0 to false}} +} +#endif // CONSTANT_BOOL + +#ifdef BOOL +const float E = 2.718; + +float GetFloat(); +double GetDouble(); + +void TestBool() { + bool b1 = GetFloat(); // expected-warning {{implicit conversion turns floating-point number into boolean: 'float' to 'bool'}} + bool b2 = GetDouble(); // expected-warning {{implicit conversion turns floating-point number into boolean: 'double' to 'bool'}} + bool b3 = 0.0 * GetDouble(); // expected-warning {{implicit conversion turns floating-point number into boolean: 'double' to 'bool'}} + bool b4 = GetFloat() + GetDouble(); // expected-warning {{implicit conversion turns floating-point number into boolean: 'double' to 'bool'}} + bool b5 = E + GetFloat(); // expected-warning {{implicit conversion turns floating-point number into boolean: 'float' to 'bool'}} +} + +#endif // BOOL + +#ifdef ZERO +void TestZero() { + const float half = .5; + int a1 = half; // expected-warning{{implicit conversion from 'const float' to 'int' changes non-zero value from 0.5 to 0}} + int a2 = 1.0 / 2.0; // expected-warning{{implicit conversion from 'double' to 'int' changes non-zero value from 0.5 to 0}} + int a3 = 5; +} +#endif // ZERO + +#ifdef OVERFLOW +void TestOverflow() { + char a = 500.0; // caught by -Wliteral-conversion + char b = -500.0; // caught by -Wliteral-conversion + + const float LargeNumber = 1024; + char c = LargeNumber; // expected-warning{{implicit conversion of out of range value from 'const float' to 'char' changes value from 1024 to 127}} + 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}} + + 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}} +} +#endif // OVERFLOW |