xref: /aosp_15_r20/external/clang/test/CXX/class.access/class.access.dcl/p1.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -verify %s
2*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
3*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
4*67e74705SXin Li 
5*67e74705SXin Li // This is just the test for [namespace.udecl]p4 with 'using'
6*67e74705SXin Li // uniformly stripped out.
7*67e74705SXin Li 
8*67e74705SXin Li // C++03 [namespace.udecl]p4:
9*67e74705SXin Li //   A using-declaration used as a member-declaration shall refer to a
10*67e74705SXin Li //   member of a base class of the class being defined, shall refer to
11*67e74705SXin Li //   a member of an anonymous union that is a member of a base class
12*67e74705SXin Li //   of the class being defined, or shall refer to an enumerator for
13*67e74705SXin Li //   an enumeration type that is a member of a base class of the class
14*67e74705SXin Li //   being defined.
15*67e74705SXin Li 
16*67e74705SXin Li // There is no directly analogous paragraph in C++0x, and the feature
17*67e74705SXin Li // works sufficiently differently there that it needs a separate test.
18*67e74705SXin Li 
19*67e74705SXin Li namespace test0 {
20*67e74705SXin Li   namespace NonClass {
21*67e74705SXin Li     typedef int type;
22*67e74705SXin Li     struct hiding {};
23*67e74705SXin Li     int hiding;
24*67e74705SXin Li     static union { double union_member; };
25*67e74705SXin Li     enum tagname { enumerator };
26*67e74705SXin Li   }
27*67e74705SXin Li 
28*67e74705SXin Li   class Test0 {
29*67e74705SXin Li     NonClass::type; // expected-error {{not a class}}
30*67e74705SXin Li #if __cplusplus <= 199711L
31*67e74705SXin Li     // expected-warning@-2 {{access declarations are deprecated; use using declarations instead}}
32*67e74705SXin Li #else
33*67e74705SXin Li     // expected-error@-4 {{ISO C++11 does not allow access declarations; use using declarations instead}}
34*67e74705SXin Li #endif
35*67e74705SXin Li 
36*67e74705SXin Li     NonClass::hiding; // expected-error {{not a class}}
37*67e74705SXin Li #if __cplusplus <= 199711L
38*67e74705SXin Li     // expected-warning@-2 {{access declarations are deprecated; use using declarations instead}}
39*67e74705SXin Li #else
40*67e74705SXin Li     // expected-error@-4 {{ISO C++11 does not allow access declarations; use using declarations instead}}
41*67e74705SXin Li #endif
42*67e74705SXin Li 
43*67e74705SXin Li     NonClass::union_member; // expected-error {{not a class}}
44*67e74705SXin Li #if __cplusplus <= 199711L
45*67e74705SXin Li     // expected-warning@-2 {{access declarations are deprecated; use using declarations instead}}
46*67e74705SXin Li #else
47*67e74705SXin Li     // expected-error@-4 {{ISO C++11 does not allow access declarations; use using declarations instead}}
48*67e74705SXin Li #endif
49*67e74705SXin Li 
50*67e74705SXin Li     NonClass::enumerator; // expected-error {{not a class}}
51*67e74705SXin Li #if __cplusplus <= 199711L
52*67e74705SXin Li     // expected-warning@-2 {{access declarations are deprecated; use using declarations instead}}
53*67e74705SXin Li #else
54*67e74705SXin Li     // expected-error@-4 {{ISO C++11 does not allow access declarations; use using declarations instead}}
55*67e74705SXin Li #endif
56*67e74705SXin Li   };
57*67e74705SXin Li }
58*67e74705SXin Li 
59*67e74705SXin Li struct Opaque0 {};
60*67e74705SXin Li 
61*67e74705SXin Li namespace test1 {
62*67e74705SXin Li   struct A {
63*67e74705SXin Li     typedef int type;
64*67e74705SXin Li     struct hiding {}; // expected-note {{previous use is here}}
65*67e74705SXin Li     Opaque0 hiding;
66*67e74705SXin Li     union { double union_member; };
67*67e74705SXin Li     enum tagname { enumerator };
68*67e74705SXin Li   };
69*67e74705SXin Li 
70*67e74705SXin Li   struct B : A {
71*67e74705SXin Li     A::type;
72*67e74705SXin Li #if __cplusplus <= 199711L
73*67e74705SXin Li     // expected-warning@-2 {{access declarations are deprecated; use using declarations instead}}
74*67e74705SXin Li #else
75*67e74705SXin Li     // expected-error@-4 {{ISO C++11 does not allow access declarations; use using declarations instead}}
76*67e74705SXin Li #endif
77*67e74705SXin Li     A::hiding;
78*67e74705SXin Li #if __cplusplus <= 199711L
79*67e74705SXin Li     // expected-warning@-2 {{access declarations are deprecated; use using declarations instead}}
80*67e74705SXin Li #else
81*67e74705SXin Li     // expected-error@-4 {{ISO C++11 does not allow access declarations; use using declarations instead}}
82*67e74705SXin Li #endif
83*67e74705SXin Li 
84*67e74705SXin Li     A::union_member;
85*67e74705SXin Li #if __cplusplus <= 199711L
86*67e74705SXin Li     // expected-warning@-2 {{access declarations are deprecated; use using declarations instead}}
87*67e74705SXin Li #else
88*67e74705SXin Li     // expected-error@-4 {{ISO C++11 does not allow access declarations; use using declarations instead}}
89*67e74705SXin Li #endif
90*67e74705SXin Li 
91*67e74705SXin Li     A::enumerator;
92*67e74705SXin Li #if __cplusplus <= 199711L
93*67e74705SXin Li     // expected-warning@-2 {{access declarations are deprecated; use using declarations instead}}
94*67e74705SXin Li #else
95*67e74705SXin Li     // expected-error@-4 {{ISO C++11 does not allow access declarations; use using declarations instead}}
96*67e74705SXin Li #endif
97*67e74705SXin Li 
98*67e74705SXin Li     A::tagname;
99*67e74705SXin Li #if __cplusplus <= 199711L
100*67e74705SXin Li     // expected-warning@-2 {{access declarations are deprecated; use using declarations instead}}
101*67e74705SXin Li #else
102*67e74705SXin Li     // expected-error@-4 {{ISO C++11 does not allow access declarations; use using declarations instead}}
103*67e74705SXin Li #endif
104*67e74705SXin Li 
test0test1::B105*67e74705SXin Li     void test0() {
106*67e74705SXin Li       type t = 0;
107*67e74705SXin Li     }
108*67e74705SXin Li 
test1test1::B109*67e74705SXin Li     void test1() {
110*67e74705SXin Li       typedef struct A::hiding local;
111*67e74705SXin Li       struct hiding _ = local();
112*67e74705SXin Li     }
113*67e74705SXin Li 
test2test1::B114*67e74705SXin Li     void test2() {
115*67e74705SXin Li       union hiding _; // expected-error {{tag type that does not match previous}}
116*67e74705SXin Li     }
117*67e74705SXin Li 
test3test1::B118*67e74705SXin Li     void test3() {
119*67e74705SXin Li       char array[sizeof(union_member) == sizeof(double) ? 1 : -1];
120*67e74705SXin Li     }
121*67e74705SXin Li 
test4test1::B122*67e74705SXin Li     void test4() {
123*67e74705SXin Li       enum tagname _ = enumerator;
124*67e74705SXin Li     }
125*67e74705SXin Li 
test5test1::B126*67e74705SXin Li     void test5() {
127*67e74705SXin Li       Opaque0 _ = hiding;
128*67e74705SXin Li     }
129*67e74705SXin Li   };
130*67e74705SXin Li }
131*67e74705SXin Li 
132*67e74705SXin Li namespace test2 {
133*67e74705SXin Li   struct A {
134*67e74705SXin Li     typedef int type;
135*67e74705SXin Li     struct hiding {}; // expected-note {{previous use is here}}
136*67e74705SXin Li     int hiding;
137*67e74705SXin Li     union { double union_member; };
138*67e74705SXin Li     enum tagname { enumerator };
139*67e74705SXin Li   };
140*67e74705SXin Li 
141*67e74705SXin Li   template <class T> struct B : A {
142*67e74705SXin Li     A::type;
143*67e74705SXin Li #if __cplusplus <= 199711L
144*67e74705SXin Li     // expected-warning@-2 {{access declarations are deprecated; use using declarations instead}}
145*67e74705SXin Li #else
146*67e74705SXin Li     // expected-error@-4 {{ISO C++11 does not allow access declarations; use using declarations instead}}
147*67e74705SXin Li #endif
148*67e74705SXin Li 
149*67e74705SXin Li     A::hiding;
150*67e74705SXin Li #if __cplusplus <= 199711L
151*67e74705SXin Li     // expected-warning@-2 {{access declarations are deprecated; use using declarations instead}}
152*67e74705SXin Li #else
153*67e74705SXin Li     // expected-error@-4 {{ISO C++11 does not allow access declarations; use using declarations instead}}
154*67e74705SXin Li #endif
155*67e74705SXin Li 
156*67e74705SXin Li     A::union_member;
157*67e74705SXin Li #if __cplusplus <= 199711L
158*67e74705SXin Li     // expected-warning@-2 {{access declarations are deprecated; use using declarations instead}}
159*67e74705SXin Li #else
160*67e74705SXin Li     // expected-error@-4 {{ISO C++11 does not allow access declarations; use using declarations instead}}
161*67e74705SXin Li #endif
162*67e74705SXin Li 
163*67e74705SXin Li     A::enumerator;
164*67e74705SXin Li #if __cplusplus <= 199711L
165*67e74705SXin Li     // expected-warning@-2 {{access declarations are deprecated; use using declarations instead}}
166*67e74705SXin Li #else
167*67e74705SXin Li     // expected-error@-4 {{ISO C++11 does not allow access declarations; use using declarations instead}}
168*67e74705SXin Li #endif
169*67e74705SXin Li 
170*67e74705SXin Li     A::tagname;
171*67e74705SXin Li #if __cplusplus <= 199711L
172*67e74705SXin Li     // expected-warning@-2 {{access declarations are deprecated; use using declarations instead}}
173*67e74705SXin Li #else
174*67e74705SXin Li     // expected-error@-4 {{ISO C++11 does not allow access declarations; use using declarations instead}}
175*67e74705SXin Li #endif
176*67e74705SXin Li 
test0test2::B177*67e74705SXin Li     void test0() {
178*67e74705SXin Li       type t = 0;
179*67e74705SXin Li     }
180*67e74705SXin Li 
test1test2::B181*67e74705SXin Li     void test1() {
182*67e74705SXin Li       typedef struct A::hiding local;
183*67e74705SXin Li       struct hiding _ = local();
184*67e74705SXin Li     }
185*67e74705SXin Li 
test2test2::B186*67e74705SXin Li     void test2() {
187*67e74705SXin Li       union hiding _; // expected-error {{tag type that does not match previous}}
188*67e74705SXin Li     }
189*67e74705SXin Li 
test3test2::B190*67e74705SXin Li     void test3() {
191*67e74705SXin Li       char array[sizeof(union_member) == sizeof(double) ? 1 : -1];
192*67e74705SXin Li     }
193*67e74705SXin Li 
test4test2::B194*67e74705SXin Li     void test4() {
195*67e74705SXin Li       enum tagname _ = enumerator;
196*67e74705SXin Li     }
197*67e74705SXin Li 
test5test2::B198*67e74705SXin Li     void test5() {
199*67e74705SXin Li       Opaque0 _ = hiding;
200*67e74705SXin Li     }
201*67e74705SXin Li   };
202*67e74705SXin Li }
203*67e74705SXin Li 
204*67e74705SXin Li namespace test3 {
205*67e74705SXin Li   struct hiding {};
206*67e74705SXin Li 
207*67e74705SXin Li   template <class T> struct A {
208*67e74705SXin Li     typedef int type; // expected-note {{target of using declaration}}
209*67e74705SXin Li     struct hiding {};
210*67e74705SXin Li     Opaque0 hiding;
211*67e74705SXin Li     union { double union_member; };
212*67e74705SXin Li     enum tagname { enumerator }; // expected-note {{target of using declaration}}
213*67e74705SXin Li   };
214*67e74705SXin Li 
215*67e74705SXin Li   template <class T> struct B : A<T> {
216*67e74705SXin Li     A<T>::type; // expected-error {{dependent using declaration resolved to type without 'typename'}}
217*67e74705SXin Li #if __cplusplus <= 199711L
218*67e74705SXin Li     // expected-warning@-2 {{access declarations are deprecated; use using declarations instead}}
219*67e74705SXin Li #else
220*67e74705SXin Li     // expected-error@-4 {{ISO C++11 does not allow access declarations; use using declarations instead}}
221*67e74705SXin Li #endif
222*67e74705SXin Li 
223*67e74705SXin Li     A<T>::hiding;
224*67e74705SXin Li #if __cplusplus <= 199711L
225*67e74705SXin Li     // expected-warning@-2 {{access declarations are deprecated; use using declarations instead}}
226*67e74705SXin Li #else
227*67e74705SXin Li     // expected-error@-4 {{ISO C++11 does not allow access declarations; use using declarations instead}}
228*67e74705SXin Li #endif
229*67e74705SXin Li 
230*67e74705SXin Li     A<T>::union_member;
231*67e74705SXin Li #if __cplusplus <= 199711L
232*67e74705SXin Li     // expected-warning@-2 {{access declarations are deprecated; use using declarations instead}}
233*67e74705SXin Li #else
234*67e74705SXin Li     // expected-error@-4 {{ISO C++11 does not allow access declarations; use using declarations instead}}
235*67e74705SXin Li #endif
236*67e74705SXin Li 
237*67e74705SXin Li     A<T>::enumerator;
238*67e74705SXin Li #if __cplusplus <= 199711L
239*67e74705SXin Li     // expected-warning@-2 {{access declarations are deprecated; use using declarations instead}}
240*67e74705SXin Li #else
241*67e74705SXin Li     // expected-error@-4 {{ISO C++11 does not allow access declarations; use using declarations instead}}
242*67e74705SXin Li #endif
243*67e74705SXin Li 
244*67e74705SXin Li     A<T>::tagname; // expected-error {{dependent using declaration resolved to type without 'typename'}}
245*67e74705SXin Li #if __cplusplus <= 199711L
246*67e74705SXin Li     // expected-warning@-2 {{access declarations are deprecated; use using declarations instead}}
247*67e74705SXin Li #else
248*67e74705SXin Li     // expected-error@-4 {{ISO C++11 does not allow access declarations; use using declarations instead}}
249*67e74705SXin Li #endif
250*67e74705SXin Li 
251*67e74705SXin Li     // FIXME: re-enable these when the various bugs involving tags are fixed
252*67e74705SXin Li #if 0
253*67e74705SXin Li     void test1() {
254*67e74705SXin Li       typedef struct A<T>::hiding local;
255*67e74705SXin Li       struct hiding _ = local();
256*67e74705SXin Li     }
257*67e74705SXin Li 
258*67e74705SXin Li     void test2() {
259*67e74705SXin Li       typedef struct A<T>::hiding local;
260*67e74705SXin Li       union hiding _ = local();
261*67e74705SXin Li     }
262*67e74705SXin Li #endif
263*67e74705SXin Li 
test3test3::B264*67e74705SXin Li     void test3() {
265*67e74705SXin Li       char array[sizeof(union_member) == sizeof(double) ? 1 : -1];
266*67e74705SXin Li     }
267*67e74705SXin Li 
268*67e74705SXin Li #if 0
269*67e74705SXin Li     void test4() {
270*67e74705SXin Li       enum tagname _ = enumerator;
271*67e74705SXin Li     }
272*67e74705SXin Li #endif
273*67e74705SXin Li 
test5test3::B274*67e74705SXin Li     void test5() {
275*67e74705SXin Li       Opaque0 _ = hiding;
276*67e74705SXin Li     }
277*67e74705SXin Li   };
278*67e74705SXin Li 
279*67e74705SXin Li   template struct B<int>; // expected-note {{in instantiation}}
280*67e74705SXin Li }
281*67e74705SXin Li 
282*67e74705SXin Li namespace test4 {
283*67e74705SXin Li   struct Base {
284*67e74705SXin Li     int foo();
285*67e74705SXin Li   };
286*67e74705SXin Li 
287*67e74705SXin Li   struct Unrelated {
288*67e74705SXin Li     int foo();
289*67e74705SXin Li   };
290*67e74705SXin Li 
291*67e74705SXin Li   struct Subclass : Base {
292*67e74705SXin Li   };
293*67e74705SXin Li 
294*67e74705SXin Li   namespace InnerNS {
295*67e74705SXin Li     int foo();
296*67e74705SXin Li   }
297*67e74705SXin Li 
298*67e74705SXin Li   // We should be able to diagnose these without instantiation.
299*67e74705SXin Li   template <class T> struct C : Base {
300*67e74705SXin Li     InnerNS::foo; // expected-error {{not a class}}
301*67e74705SXin Li #if __cplusplus <= 199711L
302*67e74705SXin Li     // expected-warning@-2 {{access declarations are deprecated; use using declarations instead}}
303*67e74705SXin Li #else
304*67e74705SXin Li     // expected-error@-4 {{ISO C++11 does not allow access declarations; use using declarations instead}}
305*67e74705SXin Li #endif
306*67e74705SXin Li 
307*67e74705SXin Li     Base::bar; // expected-error {{no member named 'bar'}}
308*67e74705SXin Li #if __cplusplus <= 199711L
309*67e74705SXin Li     // expected-warning@-2 {{access declarations are deprecated; use using declarations instead}}
310*67e74705SXin Li #else
311*67e74705SXin Li     // expected-error@-4 {{ISO C++11 does not allow access declarations; use using declarations instead}}
312*67e74705SXin Li #endif
313*67e74705SXin Li 
314*67e74705SXin Li     Unrelated::foo; // expected-error {{not a base class}}
315*67e74705SXin Li #if __cplusplus <= 199711L
316*67e74705SXin Li     // expected-warning@-2 {{access declarations are deprecated; use using declarations instead}}
317*67e74705SXin Li #else
318*67e74705SXin Li     // expected-error@-4 {{ISO C++11 does not allow access declarations; use using declarations instead}}
319*67e74705SXin Li #endif
320*67e74705SXin Li 
321*67e74705SXin Li     C::foo; // legal in C++03
322*67e74705SXin Li #if __cplusplus <= 199711L
323*67e74705SXin Li     // expected-warning@-2 {{access declarations are deprecated; use using declarations instead}}
324*67e74705SXin Li #else
325*67e74705SXin Li     // expected-error@-4 {{ISO C++11 does not allow access declarations; use using declarations instead}}
326*67e74705SXin Li     // expected-error@-5 {{using declaration refers to its own class}}
327*67e74705SXin Li #endif
328*67e74705SXin Li 
329*67e74705SXin Li     Subclass::foo; // legal in C++03
330*67e74705SXin Li #if __cplusplus <= 199711L
331*67e74705SXin Li     // expected-warning@-2 {{access declarations are deprecated; use using declarations instead}}
332*67e74705SXin Li #else
333*67e74705SXin Li     // expected-error@-4 {{ISO C++11 does not allow access declarations; use using declarations instead}}
334*67e74705SXin Li     // expected-error@-5 {{using declaration refers into 'Subclass::', which is not a base class of 'C'}}
335*67e74705SXin Li #endif
336*67e74705SXin Li 
337*67e74705SXin Li     int bar();
338*67e74705SXin Li #if __cplusplus <= 199711L
339*67e74705SXin Li     //expected-note@-2 {{target of using declaration}}
340*67e74705SXin Li #endif
341*67e74705SXin Li     C::bar;
342*67e74705SXin Li #if __cplusplus <= 199711L
343*67e74705SXin Li     // expected-warning@-2 {{access declarations are deprecated; use using declarations instead}}
344*67e74705SXin Li #else
345*67e74705SXin Li     // expected-error@-4 {{ISO C++11 does not allow access declarations; use using declarations instead}}
346*67e74705SXin Li #endif
347*67e74705SXin Li     // expected-error@-6 {{using declaration refers to its own class}}
348*67e74705SXin Li   };
349*67e74705SXin Li }
350*67e74705SXin Li 
351