aboutsummaryrefslogtreecommitdiff
path: root/test/SemaCXX/enum-scoped.cpp
blob: 73e7578ecb6e6810b917e2d8d83a690f2229caaf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// RUN: %clang_cc1 -fsyntax-only -pedantic -std=c++0x -verify -triple x86_64-apple-darwin %s

enum class E1 {
  Val1 = 1L
};

enum struct E2 {
  Val1 = '\0'
};

E1 v1 = Val1; // expected-error{{undeclared identifier}}
E1 v2 = E1::Val1;

static_assert(sizeof(E1) == sizeof(int), "bad size");
static_assert(sizeof(E1::Val1) == sizeof(int), "bad size");
static_assert(sizeof(E2) == sizeof(int), "bad size");
static_assert(sizeof(E2::Val1) == sizeof(int), "bad size");

E1 v3 = E2::Val1; // expected-error{{cannot initialize a variable}}
int x1 = E1::Val1; // expected-error{{cannot initialize a variable}}

enum E3 : char {
  Val2 = 1
};

E3 v4 = Val2;
E1 v5 = Val2; // expected-error{{cannot initialize a variable}}

static_assert(sizeof(E3) == 1, "bad size");

int x2 = Val2;

int a1[Val2];
int a2[E1::Val1]; // expected-error{{size of array has non-integer type}}

int* p1 = new int[Val2];
int* p2 = new int[E1::Val1]; // FIXME Expected-error{{must have integral}}

enum class E4 {
  e1 = -2147483648, // ok
  e2 = 2147483647, // ok
  e3 = 2147483648 // expected-error{{value is not representable}}
};

enum class E5 {
  e1 = 2147483647, // ok
  e2 // expected-error{{2147483648 is not representable in the underlying}}
};

enum class E6 : bool {
    e1 = false, e2 = true,
    e3 // expected-error{{2 is not representable in the underlying}}
};

enum E7 : bool {
    e1 = false, e2 = true,
    e3 // expected-error{{2 is not representable in the underlying}}
};

template <class T>
struct X {
  enum E : T {
    e1, e2,
    e3 // expected-error{{2 is not representable in the underlying}}
  };
};

X<bool> X2; // expected-note{{in instantiation of template}}

enum Incomplete1; // expected-error{{C++ forbids forward references}}

enum Complete1 : int;
Complete1 complete1;

enum class Complete2;
Complete2 complete2;

// All the redeclarations below are done twice on purpose. Tests that the type
// of the declaration isn't changed.

enum class Redeclare2; // expected-note{{previous use is here}} expected-note{{previous use is here}}
enum Redeclare2; // expected-error{{previously declared as scoped}}
enum Redeclare2; // expected-error{{previously declared as scoped}}

enum Redeclare3 : int; // expected-note{{previous use is here}} expected-note{{previous use is here}}
enum Redeclare3; // expected-error{{previously declared with fixed underlying type}}
enum Redeclare3; // expected-error{{previously declared with fixed underlying type}}

enum class Redeclare5;
enum class Redeclare5 : int; // ok

enum Redeclare6 : int; // expected-note{{previous use is here}} expected-note{{previous use is here}}
enum Redeclare6 : short; // expected-error{{redeclared with different underlying type}}
enum Redeclare6 : short; // expected-error{{redeclared with different underlying type}}

enum class Redeclare7; // expected-note{{previous use is here}} expected-note{{previous use is here}}
enum class Redeclare7 : short; // expected-error{{redeclared with different underlying type}}
enum class Redeclare7 : short; // expected-error{{redeclared with different underlying type}}

enum : long {
  long_enum_val = 10000
};

enum : long x; // expected-error{{unnamed enumeration must be a definition}} \
// expected-warning{{declaration does not declare anything}}

void PR9333() {
  enum class scoped_enum { yes, no, maybe };
  scoped_enum e = scoped_enum::yes;
  if (e == scoped_enum::no) { }
}

// <rdar://problem/9366066>
namespace rdar9366066 {
  enum class X : unsigned { value };

  void f(X x) {
    x % X::value; // expected-error{{invalid operands to binary expression ('rdar9366066::X' and 'rdar9366066::X')}}
    x % 8; // expected-error{{invalid operands to binary expression ('rdar9366066::X' and 'int')}}
  }
}

// Part 1 of PR10264
namespace test5 {
  namespace ns {
    typedef unsigned Atype;
    enum A : Atype;
  }
  enum ns::A : ns::Atype {
    x, y, z
  };
}

// Part 2 of PR10264
namespace test6 {
  enum A : unsigned;
  struct A::a; // expected-error {{incomplete type 'test6::A' named in nested name specifier}}
  enum A::b; // expected-error {{incomplete type 'test6::A' named in nested name specifier}}
  int A::c; // expected-error {{incomplete type 'test6::A' named in nested name specifier}}
  void A::d(); // expected-error {{incomplete type 'test6::A' named in nested name specifier}}
  void test() {
    (void) A::e; // expected-error {{incomplete type 'test6::A' named in nested name specifier}}
  }
}