xref: /llvm-project-15.0.7/clang/test/Sema/enum.c (revision bbabd39c)
1 // RUN: clang-cc %s -fsyntax-only -verify -pedantic
2 enum e {A,
3         B = 42LL << 32,        // expected-warning {{ISO C restricts enumerator values to range of 'int'}}
4       C = -4, D = 12456 };
5 
6 enum f { a = -2147483648, b = 2147483647 }; // ok.
7 
8 enum g {  // too negative
9    c = -2147483649,         // expected-warning {{ISO C restricts enumerator values to range of 'int'}}
10    d = 2147483647 };
11 enum h { e = -2147483648, // too pos
12    f = 2147483648           // expected-warning {{ISO C restricts enumerator values to range of 'int'}}
13 };
14 
15 // minll maxull
16 enum x                      // expected-warning {{enumeration values exceed range of largest integer}}
17 { y = -9223372036854775807LL-1,  // expected-warning {{ISO C restricts enumerator values to range of 'int'}}
18 z = 9223372036854775808ULL };    // expected-warning {{ISO C restricts enumerator values to range of 'int'}}
19 
20 int test() {
21   return sizeof(enum e) ;
22 }
23 
24 enum gccForwardEnumExtension ve; // expected-warning{{ISO C forbids forward references to 'enum' types}} \
25 // expected-error{{tentative definition has type 'enum gccForwardEnumExtension' that is never completed}} \
26 // expected-note{{forward declaration of 'enum gccForwardEnumExtension'}}
27 
28 int test2(int i)
29 {
30   ve + i; // expected-error{{invalid operands to binary expression}}
31 }
32 
33 // PR2020
34 union u0;    // expected-note {{previous use is here}}
35 enum u0 { U0A }; // expected-error {{use of 'u0' with tag type that does not match previous declaration}}
36 
37 
38 // rdar://6095136
39 extern enum some_undefined_enum ve2; // expected-warning {{ISO C forbids forward references to 'enum' types}}
40 
41 void test4() {
42   for (; ve2;) // expected-error {{statement requires expression of scalar type}}
43     ;
44   (_Bool)ve2;  // expected-error {{arithmetic or pointer type is required}}
45 
46   for (; ;ve2) // expected-warning {{expression result unused}}
47     ;
48   (void)ve2;
49   ve2;         // expected-warning {{expression result unused}}
50 }
51 
52 // PR2416
53 enum someenum {};  // expected-warning {{use of empty enum extension}}
54 
55 // <rdar://problem/6093889>
56 enum e0 { // expected-note {{previous definition is here}}
57   E0 = sizeof(enum e0 { E1 }), // expected-error {{nested redefinition}}
58 };
59 
60 // PR3173
61 enum { PR3173A, PR3173B = PR3173A+50 };
62 
63 // PR2753
64 void foo() {
65   enum xpto; // expected-warning{{ISO C forbids forward references to 'enum' types}}
66   enum xpto; // expected-warning{{ISO C forbids forward references to 'enum' types}}
67 }
68 
69 // <rdar://problem/6503878>
70 typedef enum { X = 0 }; // expected-warning{{typedef requires a name}}
71 
72 
73 enum NotYetComplete { // expected-note{{definition of 'enum NotYetComplete' is not complete until the closing '}'}}
74   NYC1 = sizeof(enum NotYetComplete) // expected-error{{invalid application of 'sizeof' to an incomplete type 'enum NotYetComplete'}}
75 };
76 
77 /// PR3688
78 struct s1 {
79   enum e1 (*bar)(void); // expected-warning{{ISO C forbids forward references to 'enum' types}}
80 };
81 
82 enum e1 { YES, NO };
83 
84 static enum e1 badfunc(struct s1 *q) {
85   return q->bar();
86 }
87