xref: /minix3/external/bsd/llvm/dist/clang/test/Sema/enum.c (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 %s -fsyntax-only -verify -pedantic
2*f4a2713aSLionel Sambuc enum e {A,
3*f4a2713aSLionel Sambuc         B = 42LL << 32,        // expected-warning {{ISO C restricts enumerator values to range of 'int'}}
4*f4a2713aSLionel Sambuc       C = -4, D = 12456 };
5*f4a2713aSLionel Sambuc 
6*f4a2713aSLionel Sambuc enum f { a = -2147483648, b = 2147483647 }; // ok.
7*f4a2713aSLionel Sambuc 
8*f4a2713aSLionel Sambuc enum g {  // too negative
9*f4a2713aSLionel Sambuc    c = -2147483649,         // expected-warning {{ISO C restricts enumerator values to range of 'int'}}
10*f4a2713aSLionel Sambuc    d = 2147483647 };
11*f4a2713aSLionel Sambuc enum h { e = -2147483648, // too pos
12*f4a2713aSLionel Sambuc    f = 2147483648,           // expected-warning {{ISO C restricts enumerator values to range of 'int'}}
13*f4a2713aSLionel Sambuc   i = 0xFFFF0000 // expected-warning {{too large}}
14*f4a2713aSLionel Sambuc };
15*f4a2713aSLionel Sambuc 
16*f4a2713aSLionel Sambuc // minll maxull
17*f4a2713aSLionel Sambuc enum x                      // expected-warning {{enumeration values exceed range of largest integer}}
18*f4a2713aSLionel Sambuc { y = -9223372036854775807LL-1,  // expected-warning {{ISO C restricts enumerator values to range of 'int'}}
19*f4a2713aSLionel Sambuc z = 9223372036854775808ULL };    // expected-warning {{ISO C restricts enumerator values to range of 'int'}}
20*f4a2713aSLionel Sambuc 
test()21*f4a2713aSLionel Sambuc int test() {
22*f4a2713aSLionel Sambuc   return sizeof(enum e) ;
23*f4a2713aSLionel Sambuc }
24*f4a2713aSLionel Sambuc 
25*f4a2713aSLionel Sambuc enum gccForwardEnumExtension ve; // expected-warning{{ISO C forbids forward references to 'enum' types}} \
26*f4a2713aSLionel Sambuc // expected-error{{tentative definition has type 'enum gccForwardEnumExtension' that is never completed}} \
27*f4a2713aSLionel Sambuc // expected-note{{forward declaration of 'enum gccForwardEnumExtension'}}
28*f4a2713aSLionel Sambuc 
test2(int i)29*f4a2713aSLionel Sambuc int test2(int i)
30*f4a2713aSLionel Sambuc {
31*f4a2713aSLionel Sambuc   ve + i; // expected-error{{invalid operands to binary expression}}
32*f4a2713aSLionel Sambuc }
33*f4a2713aSLionel Sambuc 
34*f4a2713aSLionel Sambuc // PR2020
35*f4a2713aSLionel Sambuc union u0;    // expected-note {{previous use is here}}
36*f4a2713aSLionel Sambuc enum u0 { U0A }; // expected-error {{use of 'u0' with tag type that does not match previous declaration}}
37*f4a2713aSLionel Sambuc 
38*f4a2713aSLionel Sambuc 
39*f4a2713aSLionel Sambuc // rdar://6095136
40*f4a2713aSLionel Sambuc extern enum some_undefined_enum ve2; // expected-warning {{ISO C forbids forward references to 'enum' types}}
41*f4a2713aSLionel Sambuc 
test4()42*f4a2713aSLionel Sambuc void test4() {
43*f4a2713aSLionel Sambuc   for (; ve2;) // expected-error {{statement requires expression of scalar type}}
44*f4a2713aSLionel Sambuc     ;
45*f4a2713aSLionel Sambuc   (_Bool)ve2;  // expected-error {{arithmetic or pointer type is required}}
46*f4a2713aSLionel Sambuc 
47*f4a2713aSLionel Sambuc   for (; ;ve2) // expected-warning {{expression result unused}}
48*f4a2713aSLionel Sambuc     ;
49*f4a2713aSLionel Sambuc   (void)ve2;
50*f4a2713aSLionel Sambuc   ve2;         // expected-warning {{expression result unused}}
51*f4a2713aSLionel Sambuc }
52*f4a2713aSLionel Sambuc 
53*f4a2713aSLionel Sambuc // PR2416
54*f4a2713aSLionel Sambuc enum someenum {};  // expected-error {{use of empty enum}}
55*f4a2713aSLionel Sambuc 
56*f4a2713aSLionel Sambuc // <rdar://problem/6093889>
57*f4a2713aSLionel Sambuc enum e0 { // expected-note {{previous definition is here}}
58*f4a2713aSLionel Sambuc   E0 = sizeof(enum e0 { E1 }), // expected-error {{nested redefinition}}
59*f4a2713aSLionel Sambuc };
60*f4a2713aSLionel Sambuc 
61*f4a2713aSLionel Sambuc // PR3173
62*f4a2713aSLionel Sambuc enum { PR3173A, PR3173B = PR3173A+50 };
63*f4a2713aSLionel Sambuc 
64*f4a2713aSLionel Sambuc // PR2753
foo()65*f4a2713aSLionel Sambuc void foo() {
66*f4a2713aSLionel Sambuc   enum xpto; // expected-warning{{ISO C forbids forward references to 'enum' types}}
67*f4a2713aSLionel Sambuc   enum xpto; // expected-warning{{ISO C forbids forward references to 'enum' types}}
68*f4a2713aSLionel Sambuc }
69*f4a2713aSLionel Sambuc 
70*f4a2713aSLionel Sambuc // <rdar://problem/6503878>
71*f4a2713aSLionel Sambuc typedef enum { X = 0 }; // expected-warning{{typedef requires a name}}
72*f4a2713aSLionel Sambuc 
73*f4a2713aSLionel Sambuc 
74*f4a2713aSLionel Sambuc enum NotYetComplete { // expected-note{{definition of 'enum NotYetComplete' is not complete until the closing '}'}}
75*f4a2713aSLionel Sambuc   NYC1 = sizeof(enum NotYetComplete) // expected-error{{invalid application of 'sizeof' to an incomplete type 'enum NotYetComplete'}}
76*f4a2713aSLionel Sambuc };
77*f4a2713aSLionel Sambuc 
78*f4a2713aSLionel Sambuc /// PR3688
79*f4a2713aSLionel Sambuc struct s1 {
80*f4a2713aSLionel Sambuc   enum e1 (*bar)(void); // expected-warning{{ISO C forbids forward references to 'enum' types}}
81*f4a2713aSLionel Sambuc };
82*f4a2713aSLionel Sambuc 
83*f4a2713aSLionel Sambuc enum e1 { YES, NO };
84*f4a2713aSLionel Sambuc 
badfunc(struct s1 * q)85*f4a2713aSLionel Sambuc static enum e1 badfunc(struct s1 *q) {
86*f4a2713aSLionel Sambuc   return q->bar();
87*f4a2713aSLionel Sambuc }
88*f4a2713aSLionel Sambuc 
89*f4a2713aSLionel Sambuc 
90*f4a2713aSLionel Sambuc // Make sure we don't a.k.a. anonymous enums.
91*f4a2713aSLionel Sambuc typedef enum {
92*f4a2713aSLionel Sambuc   an_enumerator = 20
93*f4a2713aSLionel Sambuc } an_enum;
94*f4a2713aSLionel Sambuc char * s = (an_enum) an_enumerator; // expected-warning {{incompatible integer to pointer conversion initializing 'char *' with an expression of type 'an_enum'}}
95*f4a2713aSLionel Sambuc 
96*f4a2713aSLionel Sambuc // PR4515
97*f4a2713aSLionel Sambuc enum PR4515 {PR4515a=1u,PR4515b=(PR4515a-2)/2};
98*f4a2713aSLionel Sambuc int CheckPR4515[PR4515b==0?1:-1];
99*f4a2713aSLionel Sambuc 
100*f4a2713aSLionel Sambuc // PR7911
101*f4a2713aSLionel Sambuc extern enum PR7911T PR7911V; // expected-warning{{ISO C forbids forward references to 'enum' types}}
PR7911F()102*f4a2713aSLionel Sambuc void PR7911F() {
103*f4a2713aSLionel Sambuc   switch (PR7911V); // expected-error {{statement requires expression of integer type}}
104*f4a2713aSLionel Sambuc }
105*f4a2713aSLionel Sambuc 
106*f4a2713aSLionel Sambuc char test5[__has_feature(enumerator_attributes) ? 1 : -1];
107*f4a2713aSLionel Sambuc 
108*f4a2713aSLionel Sambuc // PR8694
109*f4a2713aSLionel Sambuc // rdar://8707031
PR8694(int * e)110*f4a2713aSLionel Sambuc void PR8694(int* e) // expected-note {{passing argument to parameter 'e' here}}
111*f4a2713aSLionel Sambuc {
112*f4a2713aSLionel Sambuc }
113*f4a2713aSLionel Sambuc 
crash(enum E * e)114*f4a2713aSLionel Sambuc void crash(enum E* e) // expected-warning {{declaration of 'enum E' will not be visible outside of this function}} \
115*f4a2713aSLionel Sambuc                       // expected-warning {{ISO C forbids forward references to 'enum' types}}
116*f4a2713aSLionel Sambuc {
117*f4a2713aSLionel Sambuc         PR8694(e); // expected-warning {{incompatible pointer types passing 'enum E *' to parameter of type 'int *'}}
118*f4a2713aSLionel Sambuc }
119*f4a2713aSLionel Sambuc 
120*f4a2713aSLionel Sambuc typedef enum { NegativeShort = (short)-1 } NegativeShortEnum;
121*f4a2713aSLionel Sambuc int NegativeShortTest[NegativeShort == -1 ? 1 : -1];
122