1 // RUN: %clang_cc1 -fsyntax-only -verify -triple x86_64-apple-darwin %s
3 // This file tests -Wconstant-conversion, a subcategory of -Wconversion
4 // which is on by default.
6 // rdar://problem/6792488
7 void test_6792488(void) {
8 int x = 0x3ff0000000000000U; // expected-warning {{implicit conversion from 'unsigned long' to 'int' changes value from 4607182418800017408 to 0}}
11 void test_7809123(void) {
12 struct { int i5 : 5; } a;
14 a.i5 = 36; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 36 to 4}}
18 struct { int bit : 1; } a;
19 a.bit = 1; // shouldn't warn
22 enum Test2 { K_zero, K_one };
23 enum Test2 test2(enum Test2 *t) {
25 return 10; // shouldn't warn
34 struct A a = { 0, 10 }; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to -2}}
35 struct A b[] = { 0, 10, 0, 0 }; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to -2}}
36 struct A c[] = {{10, 0}}; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}}
37 struct A d = (struct A) { 10, 0 }; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}}
38 struct A e = { .foo = 10 }; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}}
46 a.c = 0x101; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 257 to 1}}
54 // Don't warn about this implicit conversion to bool, or at least
55 // don't warn about it just because it's a bitfield.
60 // Test that unreachable code doesn't trigger the truncation warning.
61 unsigned char x = 0 ? 65535 : 1; // no-warning
62 unsigned char y = 1 ? 65535 : 1; // expected-warning {{changes value}}
67 unsigned int twoBits1:2;
68 unsigned int twoBits2:2;
69 unsigned int reserved:28;
72 f.twoBits1 = ~1; // expected-warning {{implicit truncation from 'int' to bitfield changes value from -2 to 2}}
73 f.twoBits2 = ~2; // expected-warning {{implicit truncation from 'int' to bitfield changes value from -3 to 1}}
74 f.twoBits1 &= ~1; // no-warning
75 f.twoBits2 &= ~2; // no-warning
80 struct { enum E x : 1; } f;
81 f.x = C; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 2 to 0}}