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