Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify %s |
Anders Carlsson | 0033836 | 2009-05-11 22:55:49 +0000 | [diff] [blame] | 2 | |
| 3 | friend class A; // expected-error {{'friend' used outside of class}} |
| 4 | void f() { friend class A; } // expected-error {{'friend' used outside of class}} |
| 5 | class C { friend class A; }; |
| 6 | class D { void f() { friend class A; } }; // expected-error {{'friend' used outside of class}} |
John McCall | e7e278b | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 7 | |
| 8 | // PR5760 |
| 9 | namespace test0 { |
| 10 | namespace ns { |
| 11 | void f(int); |
| 12 | } |
| 13 | |
| 14 | struct A { |
| 15 | friend void ns::f(int a); |
| 16 | }; |
| 17 | } |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 18 | |
| 19 | // Test derived from LLVM's Registry.h |
| 20 | namespace test1 { |
| 21 | template <class T> struct Outer { |
| 22 | void foo(T); |
| 23 | struct Inner { |
| 24 | friend void Outer::foo(T); |
| 25 | }; |
| 26 | }; |
| 27 | |
| 28 | void test() { |
| 29 | (void) Outer<int>::Inner(); |
| 30 | } |
| 31 | } |
John McCall | df37000 | 2009-12-23 00:44:38 +0000 | [diff] [blame] | 32 | |
| 33 | // PR5476 |
| 34 | namespace test2 { |
| 35 | namespace foo { |
| 36 | void Func(int x); |
| 37 | } |
| 38 | |
| 39 | class Bar { |
| 40 | friend void ::test2::foo::Func(int x); |
| 41 | }; |
| 42 | } |
John McCall | bc12044 | 2009-12-23 01:09:14 +0000 | [diff] [blame] | 43 | |
| 44 | // PR5134 |
| 45 | namespace test3 { |
| 46 | class Foo { |
Chandler Carruth | 5495f37 | 2010-07-14 06:36:18 +0000 | [diff] [blame] | 47 | friend const int getInt(int inInt = 0); |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 48 | |
John McCall | bc12044 | 2009-12-23 01:09:14 +0000 | [diff] [blame] | 49 | }; |
| 50 | } |
Douglas Gregor | e1aa9f3 | 2010-06-08 21:27:36 +0000 | [diff] [blame] | 51 | |
| 52 | namespace test4 { |
| 53 | class T4A { |
| 54 | friend class T4B; |
| 55 | |
| 56 | public: |
| 57 | T4A(class T4B *); |
| 58 | |
| 59 | protected: |
| 60 | T4B *mB; // error here |
| 61 | }; |
| 62 | |
| 63 | class T4B {}; |
| 64 | } |
Argyrios Kyrtzidis | 1d1e70e | 2010-10-09 04:39:54 +0000 | [diff] [blame] | 65 | |
| 66 | namespace rdar8529993 { |
Douglas Gregor | cb710a4 | 2011-03-04 22:45:55 +0000 | [diff] [blame] | 67 | struct A { ~A(); }; |
Argyrios Kyrtzidis | 1d1e70e | 2010-10-09 04:39:54 +0000 | [diff] [blame] | 68 | |
| 69 | struct B : A |
| 70 | { |
Douglas Gregor | cb710a4 | 2011-03-04 22:45:55 +0000 | [diff] [blame] | 71 | template<int> friend A::~A(); // expected-error {{destructor cannot be declared as a template}} |
Argyrios Kyrtzidis | 1d1e70e | 2010-10-09 04:39:54 +0000 | [diff] [blame] | 72 | }; |
| 73 | } |
John McCall | d7945c6 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 74 | |
| 75 | // PR7915 |
| 76 | namespace test5 { |
| 77 | struct A; |
| 78 | struct A1 { friend void A(); }; |
| 79 | |
| 80 | struct B { friend void B(); }; |
| 81 | } |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 82 | |
| 83 | // PR8479 |
| 84 | namespace test6_1 { |
| 85 | class A { |
| 86 | public: |
| 87 | private: |
| 88 | friend class vectorA; |
| 89 | A() {} |
| 90 | }; |
| 91 | class vectorA { |
| 92 | public: |
| 93 | vectorA(int i, const A& t = A()) {} |
| 94 | }; |
| 95 | void f() { |
| 96 | vectorA v(1); |
| 97 | } |
| 98 | } |
| 99 | namespace test6_2 { |
| 100 | template<class T> |
| 101 | class vector { |
| 102 | public: |
| 103 | vector(int i, const T& t = T()) {} |
| 104 | }; |
| 105 | class A { |
| 106 | public: |
| 107 | private: |
| 108 | friend class vector<A>; |
| 109 | A() {} |
| 110 | }; |
| 111 | void f() { |
| 112 | vector<A> v(1); |
| 113 | } |
| 114 | } |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 115 | namespace test6_3 { |
| 116 | template<class T> |
| 117 | class vector { |
| 118 | public: |
| 119 | vector(int i) {} |
| 120 | void f(const T& t = T()) {} |
| 121 | }; |
| 122 | class A { |
| 123 | public: |
| 124 | private: |
| 125 | friend void vector<A>::f(const A&); |
| 126 | A() {} |
| 127 | }; |
| 128 | void f() { |
| 129 | vector<A> v(1); |
| 130 | v.f(); |
| 131 | } |
| 132 | } |
Nick Lewycky | 9c6fde5 | 2012-03-16 19:51:19 +0000 | [diff] [blame] | 133 | |
| 134 | namespace test7 { |
| 135 | extern "C" { |
| 136 | class X { |
| 137 | friend int f() { return 42; } |
| 138 | }; |
| 139 | } |
| 140 | } |
John McCall | 78037ac | 2013-04-03 21:19:47 +0000 | [diff] [blame] | 141 | |
| 142 | // PR15485 |
| 143 | namespace test8 { |
| 144 | namespace ns1 { |
| 145 | namespace ns2 { |
| 146 | template<class T> void f(T t); // expected-note {{target of using declaration}} |
| 147 | } |
| 148 | using ns2::f; // expected-note {{using declaration}} |
| 149 | } |
| 150 | struct A { void f(); }; // expected-note {{target of using declaration}} |
| 151 | struct B : public A { using A::f; }; // expected-note {{using declaration}} |
| 152 | struct X { |
| 153 | template<class T> friend void ns1::f(T t); // expected-error {{cannot befriend target of using declaration}} |
| 154 | friend void B::f(); // expected-error {{cannot befriend target of using declaration}} |
| 155 | }; |
| 156 | } |