xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/access.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2f4a2713aSLionel Sambuc 
3f4a2713aSLionel Sambuc class C {
4f4a2713aSLionel Sambuc     struct S; // expected-note {{previously declared 'private' here}}
5f4a2713aSLionel Sambuc public:
6f4a2713aSLionel Sambuc 
7f4a2713aSLionel Sambuc     struct S {}; // expected-error {{'S' redeclared with 'public' access}}
8f4a2713aSLionel Sambuc };
9f4a2713aSLionel Sambuc 
10f4a2713aSLionel Sambuc struct S {
11f4a2713aSLionel Sambuc     class C; // expected-note {{previously declared 'public' here}}
12f4a2713aSLionel Sambuc 
13f4a2713aSLionel Sambuc private:
14f4a2713aSLionel Sambuc     class C { }; // expected-error {{'C' redeclared with 'private' access}}
15f4a2713aSLionel Sambuc };
16f4a2713aSLionel Sambuc 
17f4a2713aSLionel Sambuc class T {
18f4a2713aSLionel Sambuc protected:
19f4a2713aSLionel Sambuc     template<typename T> struct A; // expected-note {{previously declared 'protected' here}}
20f4a2713aSLionel Sambuc 
21f4a2713aSLionel Sambuc private:
22f4a2713aSLionel Sambuc     template<typename T> struct A {}; // expected-error {{'A' redeclared with 'private' access}}
23f4a2713aSLionel Sambuc };
24f4a2713aSLionel Sambuc 
25f4a2713aSLionel Sambuc // PR5573
26f4a2713aSLionel Sambuc namespace test1 {
27f4a2713aSLionel Sambuc   class A {
28f4a2713aSLionel Sambuc   private:
29f4a2713aSLionel Sambuc     class X; // expected-note {{previously declared 'private' here}} \
30f4a2713aSLionel Sambuc              // expected-note {{previous declaration is here}}
31f4a2713aSLionel Sambuc   public:
32f4a2713aSLionel Sambuc     class X; // expected-error {{'X' redeclared with 'public' access}} \
33f4a2713aSLionel Sambuc              // expected-warning {{class member cannot be redeclared}}
34f4a2713aSLionel Sambuc     class X {};
35f4a2713aSLionel Sambuc   };
36f4a2713aSLionel Sambuc }
37f4a2713aSLionel Sambuc 
38f4a2713aSLionel Sambuc // PR15209
39f4a2713aSLionel Sambuc namespace PR15209 {
40f4a2713aSLionel Sambuc   namespace alias_templates {
41f4a2713aSLionel Sambuc     template<typename T1, typename T2> struct U { };
42f4a2713aSLionel Sambuc     template<typename T1> using W = U<T1, float>;
43f4a2713aSLionel Sambuc 
44f4a2713aSLionel Sambuc     class A {
45f4a2713aSLionel Sambuc       typedef int I;
46f4a2713aSLionel Sambuc       static constexpr I x = 0; // expected-note {{implicitly declared private here}}
47f4a2713aSLionel Sambuc       static constexpr I y = 42; // expected-note {{implicitly declared private here}}
48f4a2713aSLionel Sambuc       friend W<int>;
49f4a2713aSLionel Sambuc     };
50f4a2713aSLionel Sambuc 
51f4a2713aSLionel Sambuc     template<typename T1>
52f4a2713aSLionel Sambuc     struct U<T1, float>  {
53f4a2713aSLionel Sambuc       int v_;
54f4a2713aSLionel Sambuc       // the following will trigger for U<float, float> instantiation, via W<float>
UPR15209::alias_templates::U55f4a2713aSLionel Sambuc       U() : v_(A::x) { } // expected-error {{'x' is a private member of 'PR15209::alias_templates::A'}}
56f4a2713aSLionel Sambuc     };
57f4a2713aSLionel Sambuc 
58f4a2713aSLionel Sambuc     template<typename T1>
59f4a2713aSLionel Sambuc     struct U<T1, int> {
60f4a2713aSLionel Sambuc       int v_;
UPR15209::alias_templates::U61f4a2713aSLionel Sambuc       U() : v_(A::y) { } // expected-error {{'y' is a private member of 'PR15209::alias_templates::A'}}
62f4a2713aSLionel Sambuc     };
63f4a2713aSLionel Sambuc 
64f4a2713aSLionel Sambuc     template struct U<int, int>; // expected-note {{in instantiation of member function 'PR15209::alias_templates::U<int, int>::U' requested here}}
65f4a2713aSLionel Sambuc 
f()66f4a2713aSLionel Sambuc     void f()
67f4a2713aSLionel Sambuc     {
68f4a2713aSLionel Sambuc       W<int>();
69f4a2713aSLionel Sambuc       // we should issue diagnostics for the following
70f4a2713aSLionel Sambuc       W<float>(); // expected-note {{in instantiation of member function 'PR15209::alias_templates::U<float, float>::U' requested here}}
71f4a2713aSLionel Sambuc     }
72f4a2713aSLionel Sambuc   }
73f4a2713aSLionel Sambuc 
74f4a2713aSLionel Sambuc   namespace templates {
75f4a2713aSLionel Sambuc     class A {
76f4a2713aSLionel Sambuc       typedef int I;  // expected-note {{implicitly declared private here}}
77f4a2713aSLionel Sambuc       static constexpr I x = 0; // expected-note {{implicitly declared private here}}
78f4a2713aSLionel Sambuc 
79f4a2713aSLionel Sambuc       template<int> friend struct B;
80f4a2713aSLionel Sambuc       template<int> struct C;
81f4a2713aSLionel Sambuc       template<template<int> class T> friend struct TT;
82f4a2713aSLionel Sambuc       template<typename T> friend void funct(T);
83f4a2713aSLionel Sambuc     };
84f4a2713aSLionel Sambuc     template<A::I> struct B { };
85f4a2713aSLionel Sambuc 
86f4a2713aSLionel Sambuc     template<A::I> struct A::C { };
87f4a2713aSLionel Sambuc 
88f4a2713aSLionel Sambuc     template<template<A::I> class T> struct TT {
89f4a2713aSLionel Sambuc       T<A::x> t;
90f4a2713aSLionel Sambuc     };
91f4a2713aSLionel Sambuc 
92f4a2713aSLionel Sambuc     template struct TT<B>;
93f4a2713aSLionel Sambuc     template<A::I> struct D { };  // expected-error {{'I' is a private member of 'PR15209::templates::A'}}
94f4a2713aSLionel Sambuc     template struct TT<D>;
95f4a2713aSLionel Sambuc 
96f4a2713aSLionel Sambuc     // function template case
97f4a2713aSLionel Sambuc     template<typename T>
funct(T)98f4a2713aSLionel Sambuc     void funct(T)
99f4a2713aSLionel Sambuc     {
100f4a2713aSLionel Sambuc       (void)A::x;
101f4a2713aSLionel Sambuc     }
102f4a2713aSLionel Sambuc 
103f4a2713aSLionel Sambuc     template void funct<int>(int);
104f4a2713aSLionel Sambuc 
f()105f4a2713aSLionel Sambuc     void f()
106f4a2713aSLionel Sambuc     {
107f4a2713aSLionel Sambuc       (void)A::x;  // expected-error {{'x' is a private member of 'PR15209::templates::A'}}
108f4a2713aSLionel Sambuc     }
109f4a2713aSLionel Sambuc   }
110f4a2713aSLionel Sambuc }
111f4a2713aSLionel Sambuc 
112f4a2713aSLionel Sambuc namespace PR7434 {
113f4a2713aSLionel Sambuc   namespace comment0 {
114f4a2713aSLionel Sambuc     template <typename T> struct X;
115f4a2713aSLionel Sambuc     namespace N {
116f4a2713aSLionel Sambuc     class Y {
117f4a2713aSLionel Sambuc       template<typename T> friend struct X;
118f4a2713aSLionel Sambuc       int t; // expected-note {{here}}
119f4a2713aSLionel Sambuc     };
120f4a2713aSLionel Sambuc     }
121f4a2713aSLionel Sambuc     template<typename T> struct X {
XPR7434::comment0::X122f4a2713aSLionel Sambuc       X() { (void)N::Y().t; } // expected-error {{private}}
123f4a2713aSLionel Sambuc     };
124f4a2713aSLionel Sambuc     X<char> x;
125f4a2713aSLionel Sambuc   }
126f4a2713aSLionel Sambuc   namespace comment2 {
127f4a2713aSLionel Sambuc     struct X;
128f4a2713aSLionel Sambuc     namespace N {
129f4a2713aSLionel Sambuc     class Y {
130f4a2713aSLionel Sambuc       friend struct X;
131f4a2713aSLionel Sambuc       int t; // expected-note {{here}}
132f4a2713aSLionel Sambuc     };
133f4a2713aSLionel Sambuc     }
134f4a2713aSLionel Sambuc     struct X {
XPR7434::comment2::X135f4a2713aSLionel Sambuc       X() { (void)N::Y().t; } // expected-error {{private}}
136f4a2713aSLionel Sambuc     };
137f4a2713aSLionel Sambuc   }
138f4a2713aSLionel Sambuc }
139*0a6a1f1dSLionel Sambuc 
140*0a6a1f1dSLionel Sambuc namespace LocalExternVar {
141*0a6a1f1dSLionel Sambuc   class test {
142*0a6a1f1dSLionel Sambuc   private:
143*0a6a1f1dSLionel Sambuc     struct private_struct { // expected-note 2{{here}}
144*0a6a1f1dSLionel Sambuc       int x;
145*0a6a1f1dSLionel Sambuc     };
146*0a6a1f1dSLionel Sambuc     int use_private();
147*0a6a1f1dSLionel Sambuc   };
148*0a6a1f1dSLionel Sambuc 
use_private()149*0a6a1f1dSLionel Sambuc   int test::use_private() {
150*0a6a1f1dSLionel Sambuc     extern int array[sizeof(test::private_struct)]; // ok
151*0a6a1f1dSLionel Sambuc     return array[0];
152*0a6a1f1dSLionel Sambuc   }
153*0a6a1f1dSLionel Sambuc 
f()154*0a6a1f1dSLionel Sambuc   int f() {
155*0a6a1f1dSLionel Sambuc     extern int array[sizeof(test::private_struct)]; // expected-error {{private}}
156*0a6a1f1dSLionel Sambuc     return array[0];
157*0a6a1f1dSLionel Sambuc   }
158*0a6a1f1dSLionel Sambuc 
159*0a6a1f1dSLionel Sambuc   int array[sizeof(test::private_struct)]; // expected-error {{private}}
160*0a6a1f1dSLionel Sambuc }
161