[clang] Template Diagnostic Improvements (PR #99933)
Braden Helmer via cfe-commits
cfe-commits at lists.llvm.org
Mon Jul 22 13:52:17 PDT 2024
https://github.com/bradenhelmer created https://github.com/llvm/llvm-project/pull/99933
This is for #17959.
It turns out `SemaTemplate` handles this type of diagnostic already, however when template gets encountered, it never gets parsed as a possible statement or declaration, only as an expression.
Would like some feedback as I am unsure if this is the right fix. Thanks
>From 1aaa6ff0f1843f10606064f6b285c7070aaa5b44 Mon Sep 17 00:00:00 2001
From: Braden Helmer <bradenhelmeraus at gmail.com>
Date: Mon, 22 Jul 2024 16:46:04 -0400
Subject: [PATCH] Fix template error message
---
clang/lib/Parse/ParseStmt.cpp | 9 +++++++++
clang/test/Parser/cxx-template-decl.cpp | 4 ++++
2 files changed, 13 insertions(+)
diff --git a/clang/lib/Parse/ParseStmt.cpp b/clang/lib/Parse/ParseStmt.cpp
index 22d38adc28ebe..bdb3fc051d0b3 100644
--- a/clang/lib/Parse/ParseStmt.cpp
+++ b/clang/lib/Parse/ParseStmt.cpp
@@ -299,6 +299,15 @@ StmtResult Parser::ParseStatementOrDeclarationAfterAttributes(
goto Retry;
}
+ case tok::kw_template: {
+ SourceLocation DeclEnd;
+ ParsedAttributes Attrs(AttrFactory);
+ ParseTemplateDeclarationOrSpecialization(DeclaratorContext::Block, DeclEnd,
+ Attrs,
+ getAccessSpecifierIfPresent());
+ return StmtError();
+ }
+
case tok::kw_case: // C99 6.8.1: labeled-statement
return ParseCaseStatement(StmtCtx);
case tok::kw_default: // C99 6.8.1: labeled-statement
diff --git a/clang/test/Parser/cxx-template-decl.cpp b/clang/test/Parser/cxx-template-decl.cpp
index 734438069b9ae..5a2d1cec9ad31 100644
--- a/clang/test/Parser/cxx-template-decl.cpp
+++ b/clang/test/Parser/cxx-template-decl.cpp
@@ -297,3 +297,7 @@ namespace PR46231 {
template<> int; // expected-error {{declaration does not declare anything}}
template<int> int; // expected-error {{declaration does not declare anything}}
}
+
+namespace NoTemplateInBlockScope {
+ void foo() { template <typename> int i; } // expected-error {{templates can only be declared in namespace or class scope}}
+}
More information about the cfe-commits
mailing list