Do not find friend function definitions inside non-instantiated class.
authorSerge Pavlov <sepavloff@gmail.com>
Tue, 4 Oct 2016 10:11:43 +0000 (10:11 +0000)
committerSerge Pavlov <sepavloff@gmail.com>
Tue, 4 Oct 2016 10:11:43 +0000 (10:11 +0000)
commit06b7a87298d2be1597b2c36ca3cd9ce2a381a140
tree7cf568c4718a1c4f017e145256ce49f7bbe0b072
parent8a70450590d811b440c7652be9886f7b16800bb9
Do not find friend function definitions inside non-instantiated class.

Previously if a file-level function was defined inside befriending
template class, it always was treated as defined. For instance, the code like:
```
  int func(int x);
  template<typename T> class C1 {
    friend int func(int x) { return x; }
  };
  template<typename T> class C2 {
    friend int func(int x) { return x; }
  };
```
could not be compiled due to function redefinition, although not of the templates
is instantiated. Moreover, the body of friend function can contain use of template
parameters, attempt to get definition of such function outside any instantiation
causes compiler abnormal termination.

Other compilers (gcc, icc) follow viewpoint that the body of the function defined
in friend declaration becomes available when corresponding class is instantiated.
This patch implements this viewpoint in clang.

Definitions introduced by friend declarations in template classes are not added
to the redeclaration chain of corresponding function. Only when the template is
instantiated, instantiation of the function definition is placed to the chain.

The fix was made in collaboration with Richard Smith.

This change fixes PR8035, PR17923, PR22307 and PR25848.

Differential Revision: http://reviews.llvm.org/D16989

llvm-svn: 283207
clang/include/clang/Sema/Sema.h
clang/lib/Sema/SemaDecl.cpp
clang/lib/Sema/SemaDeclCXX.cpp
clang/test/SemaCXX/PR25848.cpp [new file with mode: 0644]
clang/test/SemaCXX/friend2.cpp [new file with mode: 0644]
clang/test/SemaCXX/function-redecl-2.cpp [new file with mode: 0644]