[clang] Handle __declspec() attributes in using
[llvm-project.git] / clang / test / SemaCXX / enum-scoped.cpp
blob1c6a6d7212665dfb6900701e9eebd7a460ddaf59
1 // RUN: %clang_cc1 -fsyntax-only -pedantic -std=c++11 -verify -triple x86_64-apple-darwin %s
2 // RUN: %clang_cc1 -fsyntax-only -pedantic -std=c++17 -verify -triple x86_64-apple-darwin %s
4 enum class E1 {
5 Val1 = 1L
6 };
8 enum struct E2 {
9 Val1 = '\0'
12 E1 v1 = Val1; // expected-error{{undeclared identifier}}
13 E1 v2 = E1::Val1;
15 static_assert(sizeof(E1) == sizeof(int), "bad size");
16 static_assert(sizeof(E1::Val1) == sizeof(int), "bad size");
17 static_assert(sizeof(E2) == sizeof(int), "bad size");
18 static_assert(sizeof(E2::Val1) == sizeof(int), "bad size");
20 E1 v3 = E2::Val1; // expected-error{{cannot initialize a variable}}
21 int x1 = E1::Val1; // expected-error{{cannot initialize a variable}}
23 enum E3 : char {
24 Val2 = 1
27 E3 v4 = Val2;
28 E1 v5 = Val2; // expected-error{{cannot initialize a variable}}
30 static_assert(sizeof(E3) == 1, "bad size");
32 int x2 = Val2;
34 int a1[Val2];
35 int a2[E1::Val1];
37 #if __cplusplus >= 201703L
38 // expected-error@-3 {{type 'E1' is not implicitly convertible to 'unsigned long'}}
39 #else
40 // expected-error@-5 {{size of array has non-integer type}}
41 #endif
43 int* p1 = new int[Val2];
44 int* p2 = new int[E1::Val1];
46 #if __cplusplus >= 201703L
47 // expected-error@-3 {{converting 'E1' to incompatible type 'unsigned long'}}
48 #else
49 // expected-error@-5 {{array size expression must have integral or unscoped enumeration type, not 'E1'}}
50 #endif
52 enum class E4 {
53 e1 = -2147483648, // ok
54 e2 = 2147483647, // ok
55 e3 = 2147483648 // expected-error{{enumerator value evaluates to 2147483648, which cannot be narrowed to type 'int'}}
58 enum class E5 {
59 e1 = 2147483647, // ok
60 e2 // expected-error{{2147483648 is not representable in the underlying}}
63 enum class E6 : bool {
64 e1 = false, e2 = true,
65 e3 // expected-error{{2 is not representable in the underlying}}
68 enum E7 : bool {
69 e1 = false, e2 = true,
70 e3 // expected-error{{2 is not representable in the underlying}}
73 template <class T>
74 struct X {
75 enum E : T {
76 e1, e2,
77 e3 // expected-error{{2 is not representable in the underlying}}
81 X<bool> X2; // expected-note{{in instantiation of template}}
83 enum Incomplete1; // expected-error{{C++ forbids forward references}}
85 enum Complete1 : int;
86 Complete1 complete1;
88 enum class Complete2;
89 Complete2 complete2;
91 // All the redeclarations below are done twice on purpose. Tests that the type
92 // of the declaration isn't changed.
94 enum class Redeclare2; // expected-note{{previous declaration is here}} expected-note{{previous declaration is here}}
95 enum Redeclare2; // expected-error{{previously declared as scoped}}
96 enum Redeclare2; // expected-error{{previously declared as scoped}}
98 enum Redeclare3 : int; // expected-note{{previous declaration is here}} expected-note{{previous declaration is here}}
99 enum Redeclare3; // expected-error{{previously declared with fixed underlying type}}
100 enum Redeclare3; // expected-error{{previously declared with fixed underlying type}}
102 enum class Redeclare5;
103 enum class Redeclare5 : int; // ok
105 enum Redeclare6 : int; // expected-note{{previous declaration is here}} expected-note{{previous declaration is here}}
106 enum Redeclare6 : short; // expected-error{{redeclared with different underlying type}}
107 enum Redeclare6 : short; // expected-error{{redeclared with different underlying type}}
109 enum class Redeclare7; // expected-note{{previous declaration is here}} expected-note{{previous declaration is here}}
110 enum class Redeclare7 : short; // expected-error{{redeclared with different underlying type}}
111 enum class Redeclare7 : short; // expected-error{{redeclared with different underlying type}}
113 enum : long {
114 long_enum_val = 10000
117 enum : long x; // expected-error{{unnamed enumeration must be a definition}}
119 void PR9333() {
120 enum class scoped_enum { yes, no, maybe };
121 scoped_enum e = scoped_enum::yes;
122 if (e == scoped_enum::no) { }
125 // <rdar://problem/9366066>
126 namespace rdar9366066 {
127 enum class X : unsigned { value };
129 void f(X x) {
130 x % X::value; // expected-error{{invalid operands to binary expression ('X' and 'rdar9366066::X')}}
131 x % 8; // expected-error{{invalid operands to binary expression ('X' and 'int')}}
135 // Part 1 of PR10264
136 namespace test5 {
137 namespace ns {
138 typedef unsigned Atype;
139 enum A : Atype;
141 enum ns::A : ns::Atype {
142 x, y, z
146 // Part 2 of PR10264
147 namespace test6 {
148 enum A : unsigned;
149 struct A::a; // expected-error {{incomplete type 'test6::A' named in nested name specifier}}
150 enum A::b; // expected-error {{incomplete type 'test6::A' named in nested name specifier}}
151 int A::c; // expected-error {{incomplete type 'test6::A' named in nested name specifier}}
152 void A::d(); // expected-error {{incomplete type 'test6::A' named in nested name specifier}}
153 void test() {
154 (void) A::e; // expected-error {{incomplete type 'test6::A' named in nested name specifier}}
158 namespace PR11484 {
159 const int val = 104;
160 enum class test1 { owner_dead = val, };
163 namespace N2764 {
164 enum class E *x0a; // expected-error {{reference to enumeration must use 'enum' not 'enum class'}}
165 enum E2 *x0b; // OK
166 enum class E { a, b };
167 enum E x1 = E::a; // ok
168 enum class E x2 = E::a; // expected-error {{reference to enumeration must use 'enum' not 'enum class'}}
170 enum F { a, b };
171 enum F y1 = a; // ok
172 enum class F y2 = a; // expected-error {{reference to enumeration must use 'enum' not 'enum class'}}
174 struct S {
175 friend enum class E; // expected-error {{reference to enumeration must use 'enum' not 'enum class'}}
176 friend enum class F; // expected-error {{reference to enumeration must use 'enum' not 'enum class'}}
178 friend enum G {}; // expected-error {{forward reference}} expected-error {{cannot define a type in a friend declaration}}
179 friend enum class H {}; // expected-error {{forward reference}} expected-error {{cannot define a type in a friend declaration}}
180 friend enum I : int {}; // expected-error {{forward reference}} expected-error {{cannot define a type in a friend declaration}}
182 enum A : int;
183 A a;
184 } s;
186 enum S::A : int {};
188 enum class B;
191 enum class N2764::B {};
193 namespace PR12106 {
194 template<typename E> struct Enum {
195 Enum() : m_e(E::Last) {}
196 E m_e;
199 enum eCOLORS { Last };
200 Enum<eCOLORS> e;
203 namespace test7 {
204 enum class E { e = (struct S*)0 == (struct S*)0 };
205 S *p;
208 namespace test8 {
209 template<typename T> struct S {
210 enum A : int; // expected-note {{here}}
211 enum class B; // expected-note {{here}}
212 enum class C : int; // expected-note {{here}}
213 enum class D : int; // expected-note {{here}}
215 template<typename T> enum S<T>::A { a }; // expected-error {{previously declared with fixed underlying type}}
216 template<typename T> enum class S<T>::B : char { b }; // expected-error {{redeclared with different underlying}}
217 template<typename T> enum S<T>::C : int { c }; // expected-error {{previously declared as scoped}}
218 template<typename T> enum class S<T>::D : char { d }; // expected-error {{redeclared with different underlying}}
221 namespace test9 {
222 template<typename T> struct S {
223 enum class ET : T; // expected-note 2{{here}}
224 enum class Eint : int; // expected-note 2{{here}}
226 template<> enum class S<int>::ET : int {};
227 template<> enum class S<char>::ET : short {}; // expected-error {{different underlying type}}
228 template<> enum class S<int>::Eint : short {}; // expected-error {{different underlying type}}
229 template<> enum class S<char>::Eint : int {};
231 template<typename T> enum class S<T>::ET : int {}; // expected-error {{different underlying type 'int' (was 'short')}}
232 template<typename T> enum class S<T>::Eint : T {}; // expected-error {{different underlying type 'short' (was 'int')}}
234 // The implicit instantiation of S<short> causes the implicit instantiation of
235 // all declarations of member enumerations, so is ill-formed, even though we
236 // never instantiate the definitions of S<short>::ET nor S<short>::Eint.
237 S<short> s; // expected-note {{in instantiation of}}
240 namespace test10 {
241 template<typename T> int f() {
242 enum E : int;
243 enum E : T; // expected-note {{here}}
244 E x;
245 enum E : int { e }; // expected-error {{different underlying}}
246 x = e;
247 return x;
249 int k = f<int>();
250 int l = f<short>(); // expected-note {{here}}
252 template<typename T> int g() {
253 enum class E : int;
254 enum class E : T; // expected-note {{here}}
255 E x;
256 enum class E : int { e }; // expected-error {{different underlying}}
257 x = E::e;
258 return (int)x;
260 int m = g<int>();
261 int n = g<short>(); // expected-note {{here}}
264 namespace pr13128 {
265 // This should compile cleanly
266 class C {
267 enum class E { C };
271 namespace PR15633 {
272 template<typename T> struct A {
273 struct B {
274 enum class E : T;
275 enum class E2 : T;
278 template<typename T> enum class A<T>::B::E { e };
279 template class A<int>;
281 struct B { enum class E; };
282 template<typename T> enum class B::E { e }; // expected-error {{enumeration cannot be a template}}
285 namespace PR16900 {
286 enum class A;
287 A f(A a) { return -a; } // expected-error {{invalid argument type 'A' to unary expression}}
290 namespace PR18551 {
291 enum class A { A };
292 bool f() { return !A::A; } // expected-error {{invalid argument type 'PR18551::A' to unary expression}}
295 namespace rdar15124329 {
296 enum class B : bool { F, T };
298 const rdar15124329::B T1 = B::T;
299 typedef B C; const C T2 = B::T;
301 static_assert(T1 != B::F, "");
302 static_assert(T2 == B::T, "");
305 namespace PR18044 {
306 enum class E { a };
308 int E::e = 0; // expected-error {{does not refer into a class}}
309 void E::f() {} // expected-error {{does not refer into a class}}
310 struct E::S {}; // expected-error {{no struct named 'S'}}
311 struct T : E::S {}; // expected-error {{expected class name}}
312 enum E::E {}; // expected-error {{no enum named 'E'}}
313 int E::*p; // expected-error {{does not point into a class}}
314 using E::f; // expected-error {{no member named 'f'}}
316 using E::a; // expected-warning {{using declaration naming a scoped enumerator is a C++20 extension}}
317 E b = a;
320 namespace test11 {
321 enum class E { a };
322 typedef E E2;
323 E2 f1() { return E::a; }
325 bool f() { return !f1(); } // expected-error {{invalid argument type 'E2' (aka 'test11::E') to unary expression}}
328 namespace PR35586 {
329 enum C { R=-1, G, B };
330 enum B { F = (enum C) -1, T}; // this should compile cleanly, it used to assert.
333 namespace test12 {
334 // Check that clang rejects this code without crashing in c++17.
335 enum class A;
336 enum class B;
337 A a;
338 B b{a}; // expected-error {{cannot initialize}}