// RUN: %clang_cc1 -verify -emit-llvm-only %s namespace test0 { template struct Num { T value_; public: Num(T value) : value_(value) {} T get() const { return value_; } template struct Rep { U count_; Rep(U count) : count_(count) {} friend Num operator*(const Num &a, const Rep &n) { Num x = 0; for (U count = n.count_; count; --count) x += a; return x; } }; friend Num operator+(const Num &a, const Num &b) { return a.value_ + b.value_; } Num& operator+=(const Num& b) { value_ += b.value_; return *this; } class Representation {}; friend class Representation; }; class A { template friend bool iszero(const A &a) throw(); }; template class B_iterator; template class B { friend class B_iterator; }; int calc1() { Num left = -1; Num right = 1; Num result = left + right; return result.get(); } int calc2() { Num x = 3; Num::Rep n = (char) 10; Num result = x * n; return result.get(); } } // Reduced from GNU namespace test1 { class A { bool b; // expected-note {{declared private here}} template friend bool has(const A&); }; template bool has(const A &x) { return x.b; } template bool hasnot(const A &x) { return x.b; // expected-error {{'b' is a private member of 'test1::A'}} } } namespace test2 { class A { bool b; // expected-note {{declared private here}} template friend class HasChecker; }; template class HasChecker { bool check(A *a) { return a->b; } }; template class HasNotChecker { bool check(A *a) { return a->b; // expected-error {{'b' is a private member of 'test2::A'}} } }; } namespace test3 { class Bool; template class User; template T transform(class Bool, T); class Bool { friend class User; friend bool transform<>(Bool, bool); bool value; // expected-note 2 {{declared private here}} }; template class User { static T compute(Bool b) { return b.value; // expected-error {{'value' is a private member of 'test3::Bool'}} } }; template T transform(Bool b, T value) { if (b.value) // expected-error {{'value' is a private member of 'test3::Bool'}} return value; return value + 1; } template bool transform(Bool, bool); template int transform(Bool, int); // expected-note {{requested here}} template class User; template class User; // expected-note {{requested here}} } namespace test4 { template class A { template friend class B; bool foo(const A *) const; }; template class B { bool bar(const A *a, const A *b) { return a->foo(b); } }; template class B; } namespace test5 { template class A {}; template class B { template friend class A; }; template class B; template class A; } namespace Dependent { template class X; template X operator+(const X&, const T*); template class X { typedef typename Traits::value_type value_type; friend X operator+<>(const X&, const value_type*); }; } namespace test7 { template class A { // expected-note {{declared here}} friend class B; int x; // expected-note {{declared private here}} }; class B { int foo(A &a) { return a.x; } }; class C { int foo(A &a) { return a.x; // expected-error {{'x' is a private member of 'test7::A'}} } }; // This shouldn't crash. template class D { friend class A; // expected-error {{template 'A' cannot be referenced with a class specifier}} }; template class D; } namespace test8 { template class A { static int x; template friend void foo(); }; template class A; template void foo() { A::x = 0; } template void foo(); } namespace test9 { template class A { class B; class C; int foo(B *b) { return b->x; } int foo(C *c) { return c->x; // expected-error {{'x' is a private member}} } class B { int x; friend int A::foo(B*); }; class C { int x; // expected-note {{declared private here}} }; }; template class A; // expected-note {{in instantiation}} } namespace test10 { template class A; template A bar(const T*, const A&); template class A { private: void foo(); // expected-note {{declared private here}} friend A bar<>(const T*, const A&); }; template A bar(const T *l, const A &r) { A l1; l1.foo(); A l2; l2.foo(); // expected-error {{'foo' is a private member of 'test10::A'}} return l1; } template A bar(const int *, const A &); // expected-note {{in instantiation}} } // PR6752: this shouldn't crash. namespace test11 { struct Foo { template struct IteratorImpl { template friend class IteratorImpl; }; }; template struct Foo::IteratorImpl; template struct Foo::IteratorImpl; } // PR6827 namespace test12 { template class Foo; template Foo foo(T* t){ return Foo(t, true); } template class Foo { public: Foo(T*); friend Foo foo(T*); private: Foo(T*, bool); // expected-note {{declared private here}} }; // Should work. int globalInt; Foo f = foo(&globalInt); // Shouldn't work. long globalLong; template <> Foo foo(long *t) { Foo s(&globalInt, false); // expected-error {{calling a private constructor}} return Foo(t, true); } } // PR6514 namespace test13 { template class Temp> class Role : public Temp { friend class Temp; int x; }; template class Foo { void foo(Role &role) { (void) role.x; } }; template class Foo<0>; } namespace test14 { template class B; template class A { friend void B::foo(); static void foo(); // expected-note {{declared private here}} }; template class B { public: void foo() { return A::foo(); } // expected-error {{'foo' is a private member of 'test14::A'}} }; template class B; // expected-note {{in instantiation}} } namespace test15 { template class B; template class A { friend void B::foo(); // This shouldn't be misrecognized as a templated-scoped reference. template friend void B::bar(U); static void foo(); // expected-note {{declared private here}} }; template class B { public: void foo() { return A::foo(); } // expected-error {{'foo' is a private member of 'test15::A'}} }; template <> class B { public: void foo() { return A::foo(); } template void bar(U u) { (void) A::foo(); } }; template class B; // expected-note {{in instantiation}} } namespace PR10913 { template class X; template void f(X *x) { x->member = 0; } template void f2(X *x) { x->member = 0; // expected-error{{'member' is a protected member of 'PR10913::X'}} } template class X { friend void f(X *x); friend void f2(X *x); protected: int member; // expected-note{{declared protected here}} }; template void f(X *); template void f2(X *); template void f2(X *); // expected-note{{in instantiation of function template specialization 'PR10913::f2' requested here}} } namespace test16 { template struct foo {}; // expected-note{{candidate ignored: not a function template}} template class A { friend void foo(); // expected-error{{no candidate function template was found for dependent friend function template specialization}} }; } namespace test17 { namespace ns { template void foo() {} // expected-note{{candidate ignored: not a member of the enclosing namespace; did you mean to explicitly qualify the specialization?}} } using ns::foo; template struct A { friend void foo() {} // expected-error{{no candidate function template was found for dependent friend function template specialization}} }; } namespace test18 { namespace ns1 { template struct foo {}; } // expected-note{{candidate ignored: not a function template}} namespace ns2 { void foo() {} } // expected-note{{candidate ignored: not a function template}} using ns1::foo; using ns2::foo; template class A { friend void foo() {} // expected-error{{no candidate function template was found for dependent friend function template specialization}} }; }