xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/enum.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -pedantic -std=c++98 -verify -triple x86_64-apple-darwin %s
2*f4a2713aSLionel Sambuc enum E { // expected-note{{previous definition is here}}
3*f4a2713aSLionel Sambuc   Val1,
4*f4a2713aSLionel Sambuc   Val2
5*f4a2713aSLionel Sambuc };
6*f4a2713aSLionel Sambuc 
7*f4a2713aSLionel Sambuc enum E; // expected-warning{{redeclaration of already-defined enum 'E' is a GNU extension}}
8*f4a2713aSLionel Sambuc 
9*f4a2713aSLionel Sambuc int& enumerator_type(int);
10*f4a2713aSLionel Sambuc float& enumerator_type(E);
11*f4a2713aSLionel Sambuc 
f()12*f4a2713aSLionel Sambuc void f() {
13*f4a2713aSLionel Sambuc   E e = Val1;
14*f4a2713aSLionel Sambuc   float& fr = enumerator_type(Val2);
15*f4a2713aSLionel Sambuc }
16*f4a2713aSLionel Sambuc 
17*f4a2713aSLionel Sambuc // <rdar://problem/6502934>
18*f4a2713aSLionel Sambuc typedef enum Foo {
19*f4a2713aSLionel Sambuc   A = 0,
20*f4a2713aSLionel Sambuc   B = 1
21*f4a2713aSLionel Sambuc } Foo;
22*f4a2713aSLionel Sambuc 
bar()23*f4a2713aSLionel Sambuc void bar() {
24*f4a2713aSLionel Sambuc   Foo myvar = A;
25*f4a2713aSLionel Sambuc   myvar = B;
26*f4a2713aSLionel Sambuc }
27*f4a2713aSLionel Sambuc 
28*f4a2713aSLionel Sambuc /// PR3688
29*f4a2713aSLionel Sambuc struct s1 {
30*f4a2713aSLionel Sambuc   enum e1 (*bar)(void); // expected-error{{ISO C++ forbids forward references to 'enum' types}}
31*f4a2713aSLionel Sambuc };
32*f4a2713aSLionel Sambuc 
33*f4a2713aSLionel Sambuc enum e1 { YES, NO };
34*f4a2713aSLionel Sambuc 
badfunc(struct s1 * q)35*f4a2713aSLionel Sambuc static enum e1 badfunc(struct s1 *q) {
36*f4a2713aSLionel Sambuc   return q->bar();
37*f4a2713aSLionel Sambuc }
38*f4a2713aSLionel Sambuc 
39*f4a2713aSLionel Sambuc enum e2; // expected-error{{ISO C++ forbids forward references to 'enum' types}}
40*f4a2713aSLionel Sambuc 
41*f4a2713aSLionel Sambuc namespace test1 {
42*f4a2713aSLionel Sambuc   template <class A, class B> struct is_same { static const int value = -1; };
43*f4a2713aSLionel Sambuc   template <class A> struct is_same<A,A> { static const int value = 1; };
44*f4a2713aSLionel Sambuc 
45*f4a2713aSLionel Sambuc   enum enum0 { v0 };
46*f4a2713aSLionel Sambuc   int test0[is_same<__typeof(+v0), int>::value];
47*f4a2713aSLionel Sambuc 
48*f4a2713aSLionel Sambuc   enum enum1 { v1 = __INT_MAX__ };
49*f4a2713aSLionel Sambuc   int test1[is_same<__typeof(+v1), int>::value];
50*f4a2713aSLionel Sambuc 
51*f4a2713aSLionel Sambuc   enum enum2 { v2 = __INT_MAX__ * 2U };
52*f4a2713aSLionel Sambuc   int test2[is_same<__typeof(+v2), unsigned int>::value];
53*f4a2713aSLionel Sambuc 
54*f4a2713aSLionel Sambuc   enum enum3 { v3 = __LONG_MAX__ };
55*f4a2713aSLionel Sambuc   int test3[is_same<__typeof(+v3), long>::value];
56*f4a2713aSLionel Sambuc 
57*f4a2713aSLionel Sambuc   enum enum4 { v4 = __LONG_MAX__ * 2UL };
58*f4a2713aSLionel Sambuc   int test4[is_same<__typeof(+v4), unsigned long>::value];
59*f4a2713aSLionel Sambuc }
60*f4a2713aSLionel Sambuc 
61*f4a2713aSLionel Sambuc // PR6061
62*f4a2713aSLionel Sambuc namespace PR6061 {
63*f4a2713aSLionel Sambuc   struct A { enum { id }; };
64*f4a2713aSLionel Sambuc   struct B { enum { id }; };
65*f4a2713aSLionel Sambuc 
66*f4a2713aSLionel Sambuc   struct C : public A, public B
67*f4a2713aSLionel Sambuc   {
68*f4a2713aSLionel Sambuc     enum { id };
69*f4a2713aSLionel Sambuc   };
70*f4a2713aSLionel Sambuc }
71*f4a2713aSLionel Sambuc 
72*f4a2713aSLionel Sambuc namespace Conditional {
x(const enum a x)73*f4a2713aSLionel Sambuc   enum a { A }; a x(const enum a x) { return 1?x:A; }
74*f4a2713aSLionel Sambuc }
75*f4a2713aSLionel Sambuc 
76*f4a2713aSLionel Sambuc namespace PR7051 {
77*f4a2713aSLionel Sambuc   enum E { e0 };
f()78*f4a2713aSLionel Sambuc   void f() {
79*f4a2713aSLionel Sambuc     E e;
80*f4a2713aSLionel Sambuc     e = 1; // expected-error{{assigning to 'PR7051::E' from incompatible type 'int'}}
81*f4a2713aSLionel Sambuc     e |= 1; // expected-error{{assigning to 'PR7051::E' from incompatible type 'int'}}
82*f4a2713aSLionel Sambuc   }
83*f4a2713aSLionel Sambuc }
84*f4a2713aSLionel Sambuc 
85*f4a2713aSLionel Sambuc // PR7466
86*f4a2713aSLionel Sambuc enum { }; // expected-warning{{declaration does not declare anything}}
87*f4a2713aSLionel Sambuc typedef enum { }; // expected-warning{{typedef requires a name}}
88*f4a2713aSLionel Sambuc 
89*f4a2713aSLionel Sambuc // PR7921
90*f4a2713aSLionel Sambuc enum PR7921E {
91*f4a2713aSLionel Sambuc     PR7921V = (PR7921E)(123) // expected-error {{expression is not an integral constant expression}}
92*f4a2713aSLionel Sambuc };
93*f4a2713aSLionel Sambuc 
PR8089()94*f4a2713aSLionel Sambuc void PR8089() {
95*f4a2713aSLionel Sambuc   enum E; // expected-error{{ISO C++ forbids forward references to 'enum' types}}
96*f4a2713aSLionel Sambuc   int a = (E)3; // expected-error{{cannot initialize a variable of type 'int' with an rvalue of type 'E'}}
97*f4a2713aSLionel Sambuc }
98