diff options
| author | Serge Pavlov <sepavloff@gmail.com> | 2016-10-04 10:11:43 +0000 |
|---|---|---|
| committer | Serge Pavlov <sepavloff@gmail.com> | 2016-10-04 10:11:43 +0000 |
| commit | 06b7a87298d2be1597b2c36ca3cd9ce2a381a140 (patch) | |
| tree | 7cf568c4718a1c4f017e145256ce49f7bbe0b072 /clang/lib/Sema/SemaDecl.cpp | |
| parent | 8a70450590d811b440c7652be9886f7b16800bb9 (diff) | |
| download | bcm5719-llvm-06b7a87298d2be1597b2c36ca3cd9ce2a381a140.tar.gz bcm5719-llvm-06b7a87298d2be1597b2c36ca3cd9ce2a381a140.zip | |
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
Diffstat (limited to 'clang/lib/Sema/SemaDecl.cpp')
| -rw-r--r-- | clang/lib/Sema/SemaDecl.cpp | 39 |
1 files changed, 34 insertions, 5 deletions
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 21778e8b62c..32201c99d3d 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -8664,6 +8664,32 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC, return NewFD; } +/// \brief Checks if the new declaration declared in dependent context must be +/// put in the same redeclaration chain as the specified declaration. +/// +/// \param D Declaration that is checked. +/// \param PrevDecl Previous declaration found with proper lookup method for the +/// same declaration name. +/// \returns True if D must be added to the redeclaration chain which PrevDecl +/// belongs to. +/// +bool Sema::shouldLinkDependentDeclWithPrevious(Decl *D, Decl *PrevDecl) { + // Any declarations should be put into redeclaration chains except for + // friend declaration in a dependent context that names a function in + // namespace scope. + // + // This allows to compile code like: + // + // void func(); + // template<typename T> class C1 { friend void func() { } }; + // template<typename T> class C2 { friend void func() { } }; + // + // This code snippet is a valid code unless both templates are instantiated. + return !(D->getLexicalDeclContext()->isDependentContext() && + D->getDeclContext()->isFileContext() && + D->getFriendObjectKind() != Decl::FOK_None); +} + /// \brief Perform semantic checking of a new function declaration. /// /// Performs semantic analysis of the new function declaration @@ -8847,11 +8873,14 @@ bool Sema::CheckFunctionDeclaration(Scope *S, FunctionDecl *NewFD, } } else { - // This needs to happen first so that 'inline' propagates. - NewFD->setPreviousDeclaration(cast<FunctionDecl>(OldDecl)); - - if (isa<CXXMethodDecl>(NewFD)) - NewFD->setAccess(OldDecl->getAccess()); + if (shouldLinkDependentDeclWithPrevious(NewFD, OldDecl)) { + // This needs to happen first so that 'inline' propagates. + NewFD->setPreviousDeclaration(cast<FunctionDecl>(OldDecl)); + if (isa<CXXMethodDecl>(NewFD)) + NewFD->setAccess(OldDecl->getAccess()); + } else { + Redeclaration = false; + } } } |

