Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 35 additions & 19 deletions clang/lib/Sema/SemaTemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8421,7 +8421,7 @@ bool Sema::TemplateParameterListsAreEqual(
return true;
}

bool
bool
Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
if (!S)
return false;
Expand All @@ -8445,33 +8445,49 @@ Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
}
Ctx = Ctx ? Ctx->getRedeclContext() : nullptr;

// C++ [temp]p2:
// A template-declaration can appear only as a namespace scope or
// class scope declaration.
// C++ [temp.expl.spec]p3:
// An explicit specialization may be declared in any scope in which the
// corresponding primary template may be defined.
// C++ [temp.class.spec]p6: [P2096]
// A partial specialization may be declared in any scope in which the
// corresponding primary template may be defined.
// Compute a SourceLocation to use for diagnostics. Prefer the explicit
// template location, but fall back to nearby Decl locations when needed.
SourceLocation Loc = TemplateParams->getTemplateLoc();
if (Loc.isInvalid())
Loc = TemplateParams->getSourceRange().getBegin();

if (Loc.isInvalid() && Ctx) {
if (const Decl *D = dyn_cast<Decl>(Ctx))
Loc = D->getBeginLoc();
}

// Try to extract class context if present.
CXXRecordDecl *RD = Ctx ? dyn_cast<CXXRecordDecl>(Ctx) : nullptr;
if (Loc.isInvalid() && RD)
Loc = RD->getLocation();

if (Ctx) {
if (Ctx->isFileContext())
return false;
if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Ctx)) {

if (RD) {
// C++ [temp.mem]p2:
// A local class shall not have member templates.
if (RD->isLocalClass())
return Diag(TemplateParams->getTemplateLoc(),
diag::err_template_inside_local_class)
<< TemplateParams->getSourceRange();
else
if (RD->isLocalClass()) {
// when the template location is not valid we are trying to use fallback SourceLocation such that diagnostic prints a usable file:line:col location
if (Loc.isInvalid())
Loc = TemplateParams->getSourceRange().getBegin();

return Diag(Loc, diag::err_template_inside_local_class)
<< TemplateParams->getSourceRange();
}
else {
return false;
}
}
}

return Diag(TemplateParams->getTemplateLoc(),
diag::err_template_outside_namespace_or_class_scope)
<< TemplateParams->getSourceRange();
// when teplate declared outside the namspace or class scope it Fallbacks and it give valid SourceLocation with file:line info.
if (Loc.isInvalid())
Loc = TemplateParams->getSourceRange().getBegin();

return Diag(Loc, diag::err_template_outside_namespace_or_class_scope)
<< TemplateParams->getSourceRange();
}

/// Determine what kind of template specialization the given declaration
Expand Down
11 changes: 11 additions & 0 deletions clang/test/SemaCXX/lambda-local-c++17.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: %clang_cc1 -fsyntax-only -verify %s

int main() {
auto L = []() {
struct LocalInLambda {
void qux(auto x) {} // expected-error {{'auto' not allowed in function prototype}}
};
(void)sizeof(LocalInLambda);
};
L();
}
11 changes: 11 additions & 0 deletions clang/test/SemaCXX/lambda-local-c++20.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: %clang_cc1 -fsyntax-only -std=c++20 -verify %s

int main() {
auto L = []() {
struct LocalInLambda { // expected-error {{templates cannot be declared inside of a local class}}
void qux(auto x) {}
};
(void)sizeof(LocalInLambda);
};
L();
}
9 changes: 9 additions & 0 deletions clang/test/SemaCXX/nested-local.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: %clang_cc1 -fsyntax-only -std=c++17 -verify %s

void fn() {
struct Outer {
struct Inner {
void foo(auto x) {} // expected-error {{'auto' not allowed in function prototype}}
};
};
}
8 changes: 8 additions & 0 deletions clang/test/SemaCXX/static-local.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// RUN: %clang_cc1 -fsyntax-only -std=c++20 -verify %s

int main() {
static struct StaticLocal { // expected-error {{templates cannot be declared inside of a local class}}
void bar(auto x) {}
} s;
(void)s;
}
16 changes: 16 additions & 0 deletions clang/test/SemaCXX/template-member-local-c++17.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: %clang_cc1 -fsyntax-only -verify %s

template<typename T>
struct Outer {
void member() {
struct Local {
void baz(auto x) {} // expected-error {{'auto' not allowed in function prototype}}
};
(void)sizeof(Local);
}
};

int main() {
Outer<int> o;
o.member();
}
16 changes: 16 additions & 0 deletions clang/test/SemaCXX/template-member-local-c++20.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: %clang_cc1 -fsyntax-only -std=c++20 -verify %s

template<typename T>
struct Outer {
void member() {
struct Local { // expected-error {{templates cannot be declared inside of a local class}}
void baz(auto x) {}
};
(void)sizeof(Local);
}
};

int main() {
Outer<int> o;
o.member();
}