From 7f34baeb4b28f5d96c2b60113cb5e7808ecf7963 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Fri, 9 Oct 2009 21:11:42 +0000 Subject: [PATCH] When declaring a friend class template, we may end up finding an injected-class-name (e.g., when we're referring to other specializations of the current class template). Make sure that we see the template rather than the injected-class-name. Fixes PR4768. llvm-svn: 83672 --- clang/lib/Sema/SemaTemplate.cpp | 16 ++++++++++++++++ clang/test/SemaTemplate/friend-template.cpp | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp index 219d73146ea3..3ee257a94eb5 100644 --- a/clang/lib/Sema/SemaTemplate.cpp +++ b/clang/lib/Sema/SemaTemplate.cpp @@ -616,6 +616,22 @@ Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, // whether this is a valid redeclaration. ClassTemplateDecl *PrevClassTemplate = dyn_cast_or_null(PrevDecl); + + // We may have found the injected-class-name of a class template, + // class template partial specialization, or class template specialization. + // In these cases, grab the template that is being defined or specialized. + if (!PrevClassTemplate && PrevDecl && isa(PrevDecl) && + cast(PrevDecl)->isInjectedClassName()) { + PrevDecl = cast(PrevDecl->getDeclContext()); + PrevClassTemplate + = cast(PrevDecl)->getDescribedClassTemplate(); + if (!PrevClassTemplate && isa(PrevDecl)) { + PrevClassTemplate + = cast(PrevDecl) + ->getSpecializedTemplate(); + } + } + if (PrevClassTemplate) { // Ensure that the template parameter lists are compatible. if (!TemplateParameterListsAreEqual(TemplateParams, diff --git a/clang/test/SemaTemplate/friend-template.cpp b/clang/test/SemaTemplate/friend-template.cpp index df27c920b7bc..9a483aeb5b1e 100644 --- a/clang/test/SemaTemplate/friend-template.cpp +++ b/clang/test/SemaTemplate/friend-template.cpp @@ -28,3 +28,19 @@ namespace N { template void f0(T) { } template void f1(T) { } // expected-error{{redefinition}} } + +// PR4768 +template +struct X0 { + template friend struct X0; +}; + +template +struct X0 { + template friend struct X0; +}; + +template<> +struct X0 { + template friend struct X0; +};