2011-02-17 19:05:49 +08:00
|
|
|
// RUN: %clang_cc1 -fsyntax-only -Wliteral-conversion -verify %s
|
|
|
|
|
|
|
|
void foo(int y);
|
|
|
|
|
|
|
|
// Warn when a literal float or double is assigned or bound to an integer.
|
|
|
|
void test0() {
|
|
|
|
// Float
|
2012-05-16 00:56:36 +08:00
|
|
|
int y0 = 1.2222F; // expected-warning {{implicit conversion from 'float' to 'int' changes value from 1.2222 to 1}}
|
|
|
|
int y1 = (1.2222F); // expected-warning {{implicit conversion from 'float' to 'int' changes value from 1.2222 to 1}}
|
|
|
|
int y2 = (((1.2222F))); // expected-warning {{implicit conversion from 'float' to 'int' changes value from 1.2222 to 1}}
|
|
|
|
int y3 = 12E-1F; // expected-warning {{implicit conversion from 'float' to 'int' changes value from 1.2 to 1}}
|
|
|
|
int y4 = 1.23E1F; // expected-warning {{implicit conversion from 'float' to 'int' changes value from 12.3 to 12}}
|
2011-02-17 19:05:49 +08:00
|
|
|
// Double
|
2012-05-16 00:56:36 +08:00
|
|
|
int y5 = 1.2222; // expected-warning {{implicit conversion from 'double' to 'int' changes value from 1.2222 to 1}}
|
|
|
|
int y6 = 12E-1; // expected-warning {{implicit conversion from 'double' to 'int' changes value from 1.2 to 1}}
|
|
|
|
int y7 = 1.23E1; // expected-warning {{implicit conversion from 'double' to 'int' changes value from 12.3 to 12}}
|
|
|
|
int y8 = (1.23E1); // expected-warning {{implicit conversion from 'double' to 'int' changes value from 12.3 to 12}}
|
2011-02-17 19:05:49 +08:00
|
|
|
|
|
|
|
// Test assignment to an existing variable.
|
2012-05-16 00:56:36 +08:00
|
|
|
y8 = 2.22F; // expected-warning {{implicit conversion from 'float' to 'int' changes value from 2.22 to 2}}
|
2011-02-17 19:05:49 +08:00
|
|
|
|
|
|
|
// Test direct initialization.
|
2012-05-16 00:56:36 +08:00
|
|
|
int y9(1.23F); // expected-warning {{implicit conversion from 'float' to 'int' changes value from 1.23 to 1}}
|
2011-02-17 19:05:49 +08:00
|
|
|
|
|
|
|
// Test passing a literal floating-point value to a function that takes an integer.
|
2012-05-16 00:56:36 +08:00
|
|
|
foo(1.2F); // expected-warning {{implicit conversion from 'float' to 'int' changes value from 1.2 to 1}}
|
2011-02-17 19:05:49 +08:00
|
|
|
|
2016-04-22 05:04:55 +08:00
|
|
|
int y10 = -1.2F; // expected-warning {{implicit conversion from 'float' to 'int' changes value from -1.2 to -1}}
|
2011-02-17 19:05:49 +08:00
|
|
|
|
2012-05-16 00:56:36 +08:00
|
|
|
// -Wliteral-conversion does NOT catch const values.
|
2011-02-17 19:05:49 +08:00
|
|
|
// (-Wconversion DOES catch them.)
|
|
|
|
static const float sales_tax_rate = .095F;
|
|
|
|
int z = sales_tax_rate;
|
|
|
|
foo(sales_tax_rate);
|
|
|
|
|
|
|
|
// Expressions, such as those that indicate rounding-down, should NOT produce warnings.
|
|
|
|
int x = 24 * 0.5;
|
|
|
|
int y = (24*60*60) * 0.25;
|
|
|
|
int pennies = 123.45 * 100;
|
|
|
|
}
|
2016-04-23 06:14:32 +08:00
|
|
|
|
|
|
|
// Similarly, test floating point conversion to bool. Only float values of zero
|
|
|
|
// are converted to false; everything else is converted to true.
|
|
|
|
void test1() {
|
|
|
|
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}}
|
|
|
|
// These do not warn because they can be directly converted to integral
|
|
|
|
// values.
|
|
|
|
bool b3 = 0.0f;
|
|
|
|
bool b4 = 0.0;
|
2018-05-08 04:52:56 +08:00
|
|
|
|
|
|
|
// These all warn because they overflow the target type.
|
|
|
|
short s = 32768.0; // expected-warning{{implicit conversion of out of range value from 'double' to 'short' is undefined}}
|
|
|
|
unsigned short us = 65536.0; // expected-warning{{implicit conversion of out of range value from 'double' to 'unsigned short' is undefined}}
|
|
|
|
|
|
|
|
short s2 = -32769.0; // expected-warning{{implicit conversion of out of range value from 'double' to 'short' is undefined}}
|
|
|
|
unsigned short us2 = -65537.0; // expected-warning{{implicit conversion of out of range value from 'double' to 'unsigned short' is undefined}}
|
2016-04-23 06:14:32 +08:00
|
|
|
}
|
2018-05-09 05:26:21 +08:00
|
|
|
|
|
|
|
int a() { return 2147483647.5; } // expected-warning{{implicit conversion from 'double' to 'int' changes value from 2147483647.5 to 2147483647}}
|
|
|
|
unsigned b() { return -.5; } // expected-warning{{implicit conversion from 'double' to 'unsigned int' changes value from -0.5 to 0}}
|
|
|
|
|