xref: /llvm-project/clang/test/SemaCXX/enum.cpp (revision c00c901f1cb714ebd053de5c6af564dc81754d3e)
1 // RUN: %clang_cc1 -fsyntax-only -pedantic -std=c++98 -verify -triple x86_64-apple-darwin %s
2 // RUN: %clang_cc1 -fsyntax-only -pedantic -std=c++11 -verify -triple x86_64-apple-darwin %s
3 enum E { // expected-note{{previous definition is here}}
4   Val1,
5   Val2
6 };
7 
8 enum E; // expected-warning{{redeclaration of already-defined enum 'E' is a GNU extension}}
9 
10 int& enumerator_type(int);
11 float& enumerator_type(E);
12 
13 void f() {
14   E e = Val1;
15   float& fr = enumerator_type(Val2);
16 }
17 
18 typedef enum Foo {
19   A = 0,
20   B = 1
21 } Foo;
22 
23 void bar() {
24   Foo myvar = A;
25   myvar = B;
26 }
27 
28 /// PR3688
29 struct s1 {
30   enum e1 (*bar)(void); // expected-error{{ISO C++ forbids forward references to 'enum' types}}
31 };
32 
33 enum e1 { YES, NO };
34 
35 static enum e1 badfunc(struct s1 *q) {
36   return q->bar();
37 }
38 
39 enum e2; // expected-error{{ISO C++ forbids forward references to 'enum' types}}
40 
41 namespace test1 {
42   template <class A, class B> struct is_same { static const int value = -1; };
43   template <class A> struct is_same<A,A> { static const int value = 1; };
44 
45   enum enum0 { v0 };
46   int test0[is_same<__typeof(+v0), int>::value];
47 
48   enum enum1 { v1 = __INT_MAX__ };
49   int test1[is_same<__typeof(+v1), int>::value];
50 
51   enum enum2 { v2 = __INT_MAX__ * 2U };
52   int test2[is_same<__typeof(+v2), unsigned int>::value];
53 
54   enum enum3 { v3 = __LONG_MAX__ };
55   int test3[is_same<__typeof(+v3), long>::value];
56 
57   enum enum4 { v4 = __LONG_MAX__ * 2UL };
58   int test4[is_same<__typeof(+v4), unsigned long>::value];
59 }
60 
61 // PR6061
62 namespace PR6061 {
63   struct A { enum { id }; };
64   struct B { enum { id }; };
65 
66   struct C : public A, public B
67   {
68     enum { id };
69   };
70 }
71 
72 namespace Conditional {
73   enum a { A }; a x(const enum a x) { return 1?x:A; }
74 }
75 
76 namespace PR7051 {
77   enum E { e0 };
78   void f() {
79     E e;
80     e = 1; // expected-error{{assigning to 'E' from incompatible type 'int'}}
81     e |= 1; // expected-error{{assigning to 'E' from incompatible type 'int'}}
82   }
83 }
84 
85 // PR7466
86 enum { }; // expected-warning{{declaration does not declare anything}}
87 typedef enum { }; // expected-warning{{typedef requires a name}}
88 
89 // PR7921
90 enum PR7921E { // expected-note {{not complete until the closing '}'}}
91     PR7921V = (PR7921E)(123) // expected-error {{'PR7921E' is an incomplete type}}
92 };
93 
94 void PR8089() {
95   enum E; // expected-error{{ISO C++ forbids forward references to 'enum' types}} expected-note {{forward declaration}}
96   int a = (E)3; // expected-error {{'E' is an incomplete type}}
97 }
98 
99 // This is accepted as a GNU extension. In C++98, there was no provision for
100 // expressions with UB to be non-constant.
101 enum { overflow = 123456 * 234567 };
102 #if __cplusplus >= 201103L
103 // expected-warning@-2 {{not an integral constant expression}}
104 // expected-note@-3 {{value 28958703552 is outside the range of representable values}}
105 #else
106 // expected-warning@-5 {{overflow in expression; result is -1'106'067'520 with type 'int'}}
107 #endif
108 
109 // FIXME: This is not consistent with the above case.
110 enum NoFold : int { overflow2 = 123456 * 234567 };
111 #if __cplusplus >= 201103L
112 // expected-error@-2 {{enumerator value is not a constant expression}}
113 // expected-note@-3 {{value 28958703552 is outside the range of representable values}}
114 #else
115 // expected-warning@-5 {{overflow in expression; result is -1'106'067'520 with type 'int'}}
116 // expected-warning@-6 {{extension}}
117 #endif
118 
119 // PR28903
120 struct PR28903 {
121   enum {
122     PR28903_A = (enum { // expected-error-re {{'PR28903::(unnamed enum at {{.*}})' cannot be defined in an enumeration}}
123       PR28903_B,
124       PR28903_C = PR28903_B
125     })
126   };
127 };
128