1 // RUN: %clang_cc1 -std=c++11 -triple x86_64-windows-msvc -verify %s -Wbitfield-enum-conversion
2 // RUN: %clang_cc1 -std=c++11 -triple x86_64-linux -verify %s -Wbitfield-enum-conversion
3 
4 enum TwoBits { Hi1 = 3 } two_bits;
5 enum TwoBitsSigned { Lo2 = -2, Hi2 = 1 } two_bits_signed;
6 enum ThreeBits { Hi3 = 7 } three_bits;
7 enum ThreeBitsSigned { Lo4 = -4, Hi4 = 3 } three_bits_signed;
8 enum TwoBitsFixed : unsigned { Hi5 = 3 } two_bits_fixed;
9 
10 struct Foo {
11   unsigned two_bits : 2;        // expected-note 2 {{widen this field to 3 bits}} expected-note 2 {{type signed}}
12   int two_bits_signed : 2;      // expected-note 2 {{widen this field to 3 bits}} expected-note 1 {{type unsigned}}
13   unsigned three_bits : 3;      // expected-note 2 {{type signed}}
14   int three_bits_signed : 3;    // expected-note 1 {{type unsigned}}
15 
16 #ifdef _WIN32
17   // expected-note@+2 {{type unsigned}}
18 #endif
19   ThreeBits three_bits_enum : 3;
20   ThreeBits four_bits_enum : 4;
21 };
22 
f()23 void f() {
24   Foo f;
25 
26   f.two_bits = two_bits;
27   f.two_bits = two_bits_signed;            // expected-warning {{negative enumerators}}
28   f.two_bits = three_bits;                 // expected-warning {{not wide enough}}
29   f.two_bits = three_bits_signed;          // expected-warning {{negative enumerators}} expected-warning {{not wide enough}}
30   f.two_bits = two_bits_fixed;
31 
32   f.two_bits_signed = two_bits;            // expected-warning {{needs an extra bit}}
33   f.two_bits_signed = two_bits_signed;
34   f.two_bits_signed = three_bits;          // expected-warning {{not wide enough}}
35   f.two_bits_signed = three_bits_signed;   // expected-warning {{not wide enough}}
36 
37   f.three_bits = two_bits;
38   f.three_bits = two_bits_signed;          // expected-warning {{negative enumerators}}
39   f.three_bits = three_bits;
40   f.three_bits = three_bits_signed;        // expected-warning {{negative enumerators}}
41 
42   f.three_bits_signed = two_bits;
43   f.three_bits_signed = two_bits_signed;
44   f.three_bits_signed = three_bits;        // expected-warning {{needs an extra bit}}
45   f.three_bits_signed = three_bits_signed;
46 
47 #ifdef _WIN32
48   // Enums on Windows are always implicitly 'int', which is signed, so you need
49   // an extra bit to store values that set the MSB. This is not true on SysV
50   // platforms like Linux.
51   // expected-warning@+2 {{needs an extra bit}}
52 #endif
53   f.three_bits_enum = three_bits;
54   f.four_bits_enum = three_bits;
55 
56   // Explicit casts suppress the warning.
57   f.two_bits = (unsigned)three_bits_signed;
58   f.two_bits = static_cast<unsigned>(three_bits_signed);
59 }
60