annotate clang/test/SemaCXX/class.cpp @ 221:79ff65ed7e25

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