1 // RUN: %clang_cc1 -triple %itanium_abi_triple -verify -fsyntax-only -std=c11 -Wassign-enum %s
2 
3 enum __attribute__((flag_enum)) flag {
4   ea = 0x1,
5   eb = 0x2,
6   ec = 0x8,
7 };
8 
9 enum __attribute__((flag_enum)) {
10   g = 0x7,  // expected-warning {{enumeration value 'g' is out of range of flags in enumeration type ''}}
11 };
12 
13 enum __attribute__((flag_enum)) flag2 {
14   ga = 0x1,
15   gb = 0x4,
16 
17   gc = 0x5, // no-warning
18   gd = 0x7, // expected-warning {{enumeration value 'gd' is out of range}}
19   ge = ~0x2, // expected-warning {{enumeration value 'ge' is out of range}}
20   gf = ~0x4, // no-warning
21   gg = ~0x1, // no-warning
22   gh = ~0x5, // no-warning
23   gi = ~0x11, // expected-warning {{enumeration value 'gi' is out of range}}
24 };
25 
26 enum __attribute__((flag_enum)) flag3 {
27   fa = 0x1,
28   fb = ~0x1u, // no-warning
29 };
30 
31 // What happens here is that ~0x2 is negative, and so the enum must be signed.
32 // But ~0x1u is unsigned and has the high bit set, so the enum must be 64-bit.
33 // The result is that ~0x1u does not have high bits set, and so it is considered
34 // to be an invalid value. See Sema::IsValueInFlagEnum in SemaDecl.cpp for more
35 // discussion.
36 enum __attribute__((flag_enum)) flag4 {
37   ha = 0x1,
38   hb = 0x2,
39 
40   hc = ~0x1u, // expected-warning {{enumeration value 'hc' is out of range}}
41   hd = ~0x2, // no-warning
42 };
43 
f(void)44 void f(void) {
45   enum flag e = 0; // no-warning
46   e = 0x1; // no-warning
47   e = 0x3; // no-warning
48   e = 0xa; // no-warning
49   e = 0x4; // expected-warning {{integer constant not in range of enumerated type}}
50   e = 0xf; // expected-warning {{integer constant not in range of enumerated type}}
51   e = ~0; // no-warning
52   e = ~0x1; // no-warning
53   e = ~0x2; // no-warning
54   e = ~0x3; // no-warning
55   e = ~0x4; // expected-warning {{integer constant not in range of enumerated type}}
56 
57   switch (e) {
58     case 0: break; // no-warning
59     case 0x1: break; // no-warning
60     case 0x3: break; // no-warning
61     case 0xa: break; // no-warning
62     case 0x4: break; // expected-warning {{case value not in enumerated type}}
63     case 0xf: break; // expected-warning {{case value not in enumerated type}}
64     case ~0: break; // expected-warning {{case value not in enumerated type}}
65     case ~0x1: break; // expected-warning {{case value not in enumerated type}}
66     case ~0x2: break; // expected-warning {{case value not in enumerated type}}
67     case ~0x3: break; // expected-warning {{case value not in enumerated type}}
68     case ~0x4: break; // expected-warning {{case value not in enumerated type}}
69     default: break;
70   }
71 
72   enum flag2 f = ~0x1; // no-warning
73   f = ~0x1u; // no-warning
74 
75   enum flag4 h = ~0x1; // no-warning
76   h = ~0x1u; // expected-warning {{integer constant not in range of enumerated type}}
77 }
78