Debian Codegraph @

clang/test/SemaCXX/class.cpp

raw
1 // RUN: %clang_cc1 -fsyntax-only -verify=expected,cxx11 -Wc++11-compat %s
2 // RUN: %clang_cc1 -fsyntax-only -verify=expected,cxx98 -Wc++11-compat %s -std=c++98
3 class C {
4 public:
5 auto int errx; // expected-error {{storage class specified for a member declaration}}
6 #if __cplusplus <= 199711L
7 // expected-warning@-2 {{'auto' storage class specifier is redundant}}
8 #else
9 // expected-warning@-4 {{'auto' storage class specifier is not permitted in C++11, and will not be supported in future releases}}
10 #endif
11 register int erry; // expected-error {{storage class specified for a member declaration}}
12 extern int errz; // expected-error {{storage class specified for a member declaration}}
13
14 static void sm() {
15 sx = 0;
16 this->x = 0; // expected-error {{invalid use of 'this' outside of a non-static member function}}
17 x = 0; // expected-error {{invalid use of member 'x' in static member function}}
18 }
19
20 class NestedC {
21 public:
22 NestedC(int);
23 void f() {
24 sx = 0;
25 x = 0; // expected-error {{use of non-static data member 'x' of 'C' from nested type 'NestedC'}}
26 sm();
27 m(); // expected-error {{call to non-static member function 'm' of 'C' from nested type 'NestedC'}}
28 }
29 };
30
31 int b : 1, w : 2;
32 int : 1, : 2;
33 typedef int E : 1; // expected-error {{typedef member 'E' cannot be a bit-field}}
34 static int sb : 1; // expected-error {{static member 'sb' cannot be a bit-field}}
35 static int vs; // cxx11-note {{declared here}}
36
37 typedef int func();
38 func tm;
39 func *ptm;
40 func btm : 1; // expected-error {{bit-field 'btm' has non-integral type}}
41 NestedC bc : 1; // expected-error {{bit-field 'bc' has non-integral type}}
42
43 enum E1 { en1, en2 };
44
45 int i = 0;
46 #if __cplusplus <= 199711L
47 // expected-warning@-2 {{default member initializer for non-static data member is a C++11 extension}}
48 #endif
49 static int si = 0; // expected-error {{non-const static data member must be initialized out of line}}
50 static const NestedC ci = 0; // expected-error {{static data member of type 'const NestedC' must be initialized out of line}}
51 static const int nci = vs; // expected-error {{in-class initializer for static data member is not a constant expression}} \
52 // cxx11-note {{read of non-const variable 'vs' is not allowed in a constant expression}} \
53 // cxx98-note {{subexpression not valid in a constant expression}}
54 static const int vi = 0;
55 static const volatile int cvi = 0; // ok, illegal in C++11
56 #if __cplusplus >= 201103L
57 // expected-error@-2 {{static const volatile data member must be initialized out of line}}
58 #endif
59 static const E evi = 0;
60 static const int overflow = 1000000*1000000; // cxx11-error {{in-class initializer for static data member is not a constant expression}} \
61 // cxx11-note {{value 1000000000000 is outside the range of representable values of type 'int'}} \
62 // expected-warning {{overflow in expression}}
63 static const int overflow_shift = 1<<32; // cxx11-error {{in-class initializer for static data member is not a constant expression}} \
64 // cxx11-note {{shift count 32 >= width of type 'int' (32 bits)}}
65 static const int overflow_shift2 = 1>>32; // cxx11-error {{in-class initializer for static data member is not a constant expression}}\
66 // cxx11-note {{shift count 32 >= width of type 'int' (32 bits)}}
67 static const int overflow_shift3 = 1<<-1; // cxx11-error {{in-class initializer for static data member is not a constant expression}} \
68 // cxx11-note {{negative shift count -1}}
69 static const int overflow_shift4 = 1<<-1; // cxx11-error {{in-class initializer for static data member is not a constant expression}} \
70 // cxx11-note {{negative shift count -1}}
71 static const int overflow_shift5 = -1<<1; // cxx11-error {{in-class initializer for static data member is not a constant expression}} \
72 // cxx11-note {{left shift of negative value -1}}
73
74 void m() {
75 sx = 0;
76 this->x = 0;
77 y = 0;
78 this = 0; // expected-error {{expression is not assignable}}
79 }
80
81 int f1(int p) {
82 A z = 6;
83 return p + x + this->y + z;
84 }
85
86 typedef int A;
87
88 virtual int viv; // expected-error {{'virtual' can only appear on non-static member functions}}
89 virtual static int vsif(); // expected-error {{'virtual' can only appear on non-static member functions}}
90 virtual int vif();
91
92 private:
93 int x,y;
94 static int sx;
95
96 mutable int mi;
97 mutable int &mir; // expected-error {{'mutable' cannot be applied to references}}
98 mutable void mfn(); // expected-error {{'mutable' cannot be applied to functions}}
99 mutable const int mci; // expected-error {{'mutable' and 'const' cannot be mixed}}
100
101 static const int number = 50;
102 static int arr[number];
103 };
104
105 class C2 {
106 void f() {
107 static int lx;
108 class LC1 {
109 int m() { return lx; }
110 };
111 class LC2 {
112 int m() { return lx; }
113 };
114 }
115 };
116
117 struct C3 {
118 int i;
119 mutable int j;
120 };
121 void f()
122 {
123 const C3 c3 = { 1, 2 };
124 (void)static_cast<int*>(&c3.i); // expected-error {{static_cast from 'const int *' to 'int *' is not allowed}}
125 // but no error here
126 (void)static_cast<int*>(&c3.j);
127 }
128
129 // Play with mutable a bit more, to make sure it doesn't crash anything.
130 mutable int gi; // expected-error {{'mutable' can only be applied to member variables}}
131 mutable void gfn(); // expected-error {{illegal storage class on function}}
132 void ogfn()
133 {
134 mutable int ml; // expected-error {{'mutable' can only be applied to member variables}}
135
136 // PR3020: This used to crash due to double ownership of C4.
137 struct C4;
138 C4; // expected-warning {{declaration does not declare anything}}
139 }
140
141 struct C4 {
142 void f(); // expected-note{{previous declaration is here}}
143 int f; // expected-error{{duplicate member 'f'}}
144 };
145
146 // PR5415 - don't hang!
147 struct S
148 {
149 void f(); // expected-note 1 {{previous declaration}} expected-note {{previous declaration}}
150 void S::f() {} // expected-error {{extra qualification on member}} expected-error {{class member cannot be redeclared}}
151 void f() {} // expected-error {{class member cannot be redeclared}}
152 };
153
154 // Don't crash on this bogus code.
155 namespace pr6629 {
156 template<class T1, class T2> struct foo :
157 bogus<foo<T1,T2> > // expected-error {{no template named 'bogus'}}
158 { };
159
160 template<> struct foo<unknown,unknown> { // expected-error {{undeclared identifier 'unknown'}}
161 template <typename U1, typename U2> struct bar {
162 typedef bar type;
163 static const int value = 0;
164 };
165 };
166 }
167
168 namespace PR7153 {
169 class EnclosingClass {
170 public:
171 struct A { } mutable *member;
172 };
173
174 void f(const EnclosingClass &ec) {
175 ec.member = 0;
176 }
177 }
178
179 namespace PR7196 {
180 struct A {
181 int a;
182
183 void f() {
184 char i[sizeof(a)];
185 enum { x = sizeof(i) };
186 enum { y = sizeof(a) };
187 }
188 };
189 }
190
191 namespace rdar8066414 {
192 class C {
193 C() {}
194 } // expected-error{{expected ';' after class}}
195 }
196
197 namespace rdar8367341 {
198 float foo();
199 #if __cplusplus >= 201103L
200 // expected-note@-2 {{declared here}}
201 #endif
202
203 struct A {
204 #if __cplusplus <= 199711L
205 static const float x = 5.0f; // expected-warning {{in-class initializer for static data member of type 'const float' is a GNU extension}}
206 static const float y = foo(); // expected-warning {{in-class initializer for static data member of type 'const float' is a GNU extension}} expected-error {{in-class initializer for static data member is not a constant expression}}
207 #else
208 static constexpr float x = 5.0f;
209 static constexpr float y = foo(); // expected-error {{constexpr variable 'y' must be initialized by a constant expression}} expected-note {{non-constexpr function 'foo' cannot be used in a constant expression}}
210 #endif
211 };
212 }
213
214 namespace with_anon {
215 struct S {
216 union {
217 char c;
218 };
219 };
220
221 void f() {
222 S::c; // expected-error {{invalid use of non-static data member}}
223 }
224 }
225
226 struct PR9989 {
227 static int const PR9989_Member = sizeof PR9989_Member;
228 };