1091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall// RUN: %clang_cc1 -fsyntax-only -verify -triple x86_64-apple-darwin %s 2091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall 3091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall// This file tests -Wconstant-conversion, a subcategory of -Wconversion 4091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall// which is on by default. 5091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall 6091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall// rdar://problem/6792488 7091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCallvoid test_6792488(void) { 8091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall int x = 0x3ff0000000000000U; // expected-warning {{implicit conversion from 'unsigned long' to 'int' changes value from 4607182418800017408 to 0}} 9091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall} 10beb22aaa22e255241d6a81e8b0a9239f5fa584f3John McCall 11beb22aaa22e255241d6a81e8b0a9239f5fa584f3John McCallvoid test_7809123(void) { 12beb22aaa22e255241d6a81e8b0a9239f5fa584f3John McCall struct { int i5 : 5; } a; 13beb22aaa22e255241d6a81e8b0a9239f5fa584f3John McCall 145dac4c222d9ff25488ac6f0b9642fa0d939ba2f8John McCall a.i5 = 36; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 36 to 4}} 15beb22aaa22e255241d6a81e8b0a9239f5fa584f3John McCall} 16935cd6e8529edbb3b0eba660bd934b508766de37John McCall 17935cd6e8529edbb3b0eba660bd934b508766de37John McCallvoid test() { 18935cd6e8529edbb3b0eba660bd934b508766de37John McCall struct { int bit : 1; } a; 19935cd6e8529edbb3b0eba660bd934b508766de37John McCall a.bit = 1; // shouldn't warn 20935cd6e8529edbb3b0eba660bd934b508766de37John McCall} 211844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall 221844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCallenum Test2 { K_zero, K_one }; 231844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCallenum Test2 test2(enum Test2 *t) { 241844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall *t = 20; 251844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall return 10; // shouldn't warn 261844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall} 2715d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall 2815d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCallvoid test3() { 2915d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall struct A { 3015d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall unsigned int foo : 2; 3115d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall int bar : 2; 3215d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall }; 3315d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall 343a643afa402d51b5fc4faccb4329e9c4392d4551Eli Friedman struct A a = { 0, 10 }; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to -2}} 353a643afa402d51b5fc4faccb4329e9c4392d4551Eli Friedman struct A b[] = { 0, 10, 0, 0 }; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to -2}} 3615d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall struct A c[] = {{10, 0}}; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}} 3715d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall struct A d = (struct A) { 10, 0 }; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}} 3815d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall struct A e = { .foo = 10 }; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}} 3915d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall} 4091b6014876e9186f968f74a8975ab8fc08ef1b68John McCall 4191b6014876e9186f968f74a8975ab8fc08ef1b68John McCallvoid test4() { 4291b6014876e9186f968f74a8975ab8fc08ef1b68John McCall struct A { 4391b6014876e9186f968f74a8975ab8fc08ef1b68John McCall char c : 2; 4491b6014876e9186f968f74a8975ab8fc08ef1b68John McCall } a; 4591b6014876e9186f968f74a8975ab8fc08ef1b68John McCall 4691b6014876e9186f968f74a8975ab8fc08ef1b68John McCall a.c = 0x101; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 257 to 1}} 4791b6014876e9186f968f74a8975ab8fc08ef1b68John McCall} 4891b6014876e9186f968f74a8975ab8fc08ef1b68John McCall 4991b6014876e9186f968f74a8975ab8fc08ef1b68John McCallvoid test5() { 5091b6014876e9186f968f74a8975ab8fc08ef1b68John McCall struct A { 5191b6014876e9186f968f74a8975ab8fc08ef1b68John McCall _Bool b : 1; 5291b6014876e9186f968f74a8975ab8fc08ef1b68John McCall } a; 5391b6014876e9186f968f74a8975ab8fc08ef1b68John McCall 5491b6014876e9186f968f74a8975ab8fc08ef1b68John McCall // Don't warn about this implicit conversion to bool, or at least 5591b6014876e9186f968f74a8975ab8fc08ef1b68John McCall // don't warn about it just because it's a bitfield. 5691b6014876e9186f968f74a8975ab8fc08ef1b68John McCall a.b = 100; 5791b6014876e9186f968f74a8975ab8fc08ef1b68John McCall} 585e745da03df27ac620b0a5cc280d0d1757cfbac1Ted Kremenek 595e745da03df27ac620b0a5cc280d0d1757cfbac1Ted Kremenekvoid test6() { 605e745da03df27ac620b0a5cc280d0d1757cfbac1Ted Kremenek // Test that unreachable code doesn't trigger the truncation warning. 615e745da03df27ac620b0a5cc280d0d1757cfbac1Ted Kremenek unsigned char x = 0 ? 65535 : 1; // no-warning 625e745da03df27ac620b0a5cc280d0d1757cfbac1Ted Kremenek unsigned char y = 1 ? 65535 : 1; // expected-warning {{changes value}} 635e745da03df27ac620b0a5cc280d0d1757cfbac1Ted Kremenek} 645e745da03df27ac620b0a5cc280d0d1757cfbac1Ted Kremenek 653a643afa402d51b5fc4faccb4329e9c4392d4551Eli Friedmanvoid test7() { 663a643afa402d51b5fc4faccb4329e9c4392d4551Eli Friedman struct { 673a643afa402d51b5fc4faccb4329e9c4392d4551Eli Friedman unsigned int twoBits1:2; 683a643afa402d51b5fc4faccb4329e9c4392d4551Eli Friedman unsigned int twoBits2:2; 693a643afa402d51b5fc4faccb4329e9c4392d4551Eli Friedman unsigned int reserved:28; 703a643afa402d51b5fc4faccb4329e9c4392d4551Eli Friedman } f; 713a643afa402d51b5fc4faccb4329e9c4392d4551Eli Friedman 723a643afa402d51b5fc4faccb4329e9c4392d4551Eli Friedman f.twoBits1 = ~1; // expected-warning {{implicit truncation from 'int' to bitfield changes value from -2 to 2}} 733a643afa402d51b5fc4faccb4329e9c4392d4551Eli Friedman f.twoBits2 = ~2; // expected-warning {{implicit truncation from 'int' to bitfield changes value from -3 to 1}} 740fa063816818ac53a0015a5d2b0328812005b40eEli Friedman f.twoBits1 &= ~1; // no-warning 750fa063816818ac53a0015a5d2b0328812005b40eEli Friedman f.twoBits2 &= ~2; // no-warning 763a643afa402d51b5fc4faccb4329e9c4392d4551Eli Friedman} 7734ff062936ac78142a2c0dc8d1ae6a40df456819Eli Friedman 7834ff062936ac78142a2c0dc8d1ae6a40df456819Eli Friedmanvoid test8() { 7934ff062936ac78142a2c0dc8d1ae6a40df456819Eli Friedman enum E { A, B, C }; 8034ff062936ac78142a2c0dc8d1ae6a40df456819Eli Friedman struct { enum E x : 1; } f; 8134ff062936ac78142a2c0dc8d1ae6a40df456819Eli Friedman f.x = C; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 2 to 0}} 8234ff062936ac78142a2c0dc8d1ae6a40df456819Eli Friedman} 83