Sema: Reject templates in all extern "C" contexts.

Otherwise we'd accept them if the LinkageDecl was not the direct
parent DeclContext. PR17968.

llvm-svn: 200641
This commit is contained in:
Benjamin Kramer 2014-02-02 16:35:43 +00:00
parent 3a7cc81d52
commit 35e6fee3e9
3 changed files with 14 additions and 6 deletions

View File

@ -5453,18 +5453,19 @@ Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
(S->getFlags() & Scope::TemplateParamScope) != 0)
S = S->getParent();
// C++ [temp]p2:
// A template-declaration can appear only as a namespace scope or
// class scope declaration.
// C++ [temp]p4:
// A template [...] shall not have C linkage.
DeclContext *Ctx = S->getEntity();
if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
if (Ctx && Ctx->isExternCContext())
return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
<< TemplateParams->getSourceRange();
while (Ctx && isa<LinkageSpecDecl>(Ctx))
Ctx = Ctx->getParent();
// C++ [temp]p2:
// A template-declaration can appear only as a namespace scope or
// class scope declaration.
if (Ctx) {
if (Ctx->isFileContext())
return false;

View File

@ -32,7 +32,7 @@ void b(Foo f1, Foo f2) {
}
namespace test2 {
extern "C" {
extern "C++" {
namespace std {
template<typename T> struct basic_string {
struct X {};

View File

@ -14,6 +14,13 @@ extern "C" {
template<typename T> class D; // expected-error{{templates must have C++ linkage}}
}
extern "C" {
class PR17968 {
template<typename T> class D; // expected-error{{templates must have C++ linkage}}
template<typename T> void f(); // expected-error{{templates must have C++ linkage}}
};
}
template<class U> class A; // expected-note{{previous template declaration is here}}
template<int N> class A; // expected-error{{template parameter has a different kind in template redeclaration}}