From b72839f22820b010628a9a4003bfb14a05b0102c Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Sun, 2 Feb 2014 16:35:43 +0000 Subject: [PATCH] Sema: Reject templates in all extern "C" contexts. Otherwise we'd accept them if the LinkageDecl was not the direct parent DeclContext. PR17968. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@200641 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaTemplate.cpp | 11 ++++++----- test/SemaCXX/warn-unused-value.cpp | 2 +- test/SemaTemplate/class-template-decl.cpp | 7 +++++++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/Sema/SemaTemplate.cpp b/lib/Sema/SemaTemplate.cpp index 008ed2755c..bc66fdedb6 100644 --- a/lib/Sema/SemaTemplate.cpp +++ b/lib/Sema/SemaTemplate.cpp @@ -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(Ctx) && - cast(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) + if (Ctx && Ctx->isExternCContext()) return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) << TemplateParams->getSourceRange(); while (Ctx && isa(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; diff --git a/test/SemaCXX/warn-unused-value.cpp b/test/SemaCXX/warn-unused-value.cpp index 5e43d3ec04..4e1347cc30 100644 --- a/test/SemaCXX/warn-unused-value.cpp +++ b/test/SemaCXX/warn-unused-value.cpp @@ -32,7 +32,7 @@ void b(Foo f1, Foo f2) { } namespace test2 { - extern "C" { + extern "C++" { namespace std { template struct basic_string { struct X {}; diff --git a/test/SemaTemplate/class-template-decl.cpp b/test/SemaTemplate/class-template-decl.cpp index e65da2b312..b721aab354 100644 --- a/test/SemaTemplate/class-template-decl.cpp +++ b/test/SemaTemplate/class-template-decl.cpp @@ -14,6 +14,13 @@ extern "C" { template class D; // expected-error{{templates must have C++ linkage}} } +extern "C" { + class PR17968 { + template class D; // expected-error{{templates must have C++ linkage}} + template void f(); // expected-error{{templates must have C++ linkage}} + }; +} + template class A; // expected-note{{previous template declaration is here}} template class A; // expected-error{{template parameter has a different kind in template redeclaration}} -- 2.49.0