Chandler Carruth | a5b9332 | 2011-02-17 11:05:49 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -Wliteral-conversion -verify %s |
| 2 | |
| 3 | void foo(int y); |
| 4 | |
| 5 | // Warn when a literal float or double is assigned or bound to an integer. |
| 6 | void test0() { |
| 7 | // Float |
| 8 | int y0 = 1.2222F; // expected-warning {{implicit conversion turns literal floating-point number into integer}} |
| 9 | int y1 = (1.2222F); // expected-warning {{implicit conversion turns literal floating-point number into integer}} |
| 10 | int y2 = (((1.2222F))); // expected-warning {{implicit conversion turns literal floating-point number into integer}} |
| 11 | int y3 = 12E1F; // expected-warning {{implicit conversion turns literal floating-point number into integer}} |
| 12 | int y4 = 1.2E1F; // expected-warning {{implicit conversion turns literal floating-point number into integer}} |
| 13 | // Double |
| 14 | int y5 = 1.2222; // expected-warning {{implicit conversion turns literal floating-point number into integer}} |
| 15 | int y6 = 12E1; // expected-warning {{implicit conversion turns literal floating-point number into integer}} |
| 16 | int y7 = 1.2E1; // expected-warning {{implicit conversion turns literal floating-point number into integer}} |
| 17 | int y8 = (1.2E1); // expected-warning {{implicit conversion turns literal floating-point number into integer}} |
| 18 | |
| 19 | // Test assignment to an existing variable. |
| 20 | y8 = 2.22F; // expected-warning {{implicit conversion turns literal floating-point number into integer}} |
| 21 | |
| 22 | // Test direct initialization. |
| 23 | int y9(1.23F); // expected-warning {{implicit conversion turns literal floating-point number into integer}} |
| 24 | |
| 25 | // Test passing a literal floating-point value to a function that takes an integer. |
| 26 | foo(1.2F); // expected-warning {{implicit conversion turns literal floating-point number into integer}} |
| 27 | |
| 28 | // FIXME: -Wconversion-literal doesn't catch "-1.2F". |
| 29 | int y10 = -1.2F; |
| 30 | |
| 31 | // -Wconversion-literal does NOT catch const values. |
| 32 | // (-Wconversion DOES catch them.) |
| 33 | static const float sales_tax_rate = .095F; |
| 34 | int z = sales_tax_rate; |
| 35 | foo(sales_tax_rate); |
| 36 | |
| 37 | // Expressions, such as those that indicate rounding-down, should NOT produce warnings. |
| 38 | int x = 24 * 0.5; |
| 39 | int y = (24*60*60) * 0.25; |
| 40 | int pennies = 123.45 * 100; |
| 41 | } |