[clang] e69138d - PR46859: Fix crash if declaring a template in a DeclScope with no DeclContext.
Richard Smith via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 29 12:03:05 PDT 2020
Author: Richard Smith
Date: 2020-07-29T12:02:55-07:00
New Revision: e69138dad5a535c4027a1931f05cabab0f484974
URL: https://github.com/llvm/llvm-project/commit/e69138dad5a535c4027a1931f05cabab0f484974
DIFF: https://github.com/llvm/llvm-project/commit/e69138dad5a535c4027a1931f05cabab0f484974.diff
LOG: PR46859: Fix crash if declaring a template in a DeclScope with no DeclContext.
This can happen during error recovery; it could also happen at block
scope if we ever parsed a template declaration at block scope.
Added:
Modified:
clang/lib/Sema/SemaTemplate.cpp
clang/test/SemaCXX/extern-c.cpp
Removed:
################################################################################
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index c05ed0b14e3e..76ba12303d84 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -7774,15 +7774,14 @@ Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
// C++ [temp]p4:
// A template [...] shall not have C linkage.
DeclContext *Ctx = S->getEntity();
- assert(Ctx && "Unknown context");
- if (Ctx->isExternCContext()) {
+ if (Ctx && Ctx->isExternCContext()) {
Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
<< TemplateParams->getSourceRange();
if (const LinkageSpecDecl *LSD = Ctx->getExternCContext())
Diag(LSD->getExternLoc(), diag::note_extern_c_begins_here);
return true;
}
- Ctx = Ctx->getRedeclContext();
+ Ctx = Ctx ? Ctx->getRedeclContext() : nullptr;
// C++ [temp]p2:
// A template-declaration can appear only as a namespace scope or
diff --git a/clang/test/SemaCXX/extern-c.cpp b/clang/test/SemaCXX/extern-c.cpp
index 1a32beec7baa..68d1494b9491 100644
--- a/clang/test/SemaCXX/extern-c.cpp
+++ b/clang/test/SemaCXX/extern-c.cpp
@@ -259,3 +259,8 @@ namespace PR35697 {
}
}
}
+
+namespace PR46859 {
+ extern "bogus" // expected-error {{unknown linkage language}}
+ template<int> struct X {}; // expected-error {{templates can only be declared in namespace or class scope}}
+}
More information about the cfe-commits
mailing list