[clang] Template Diagnostic Improvements (PR #99933)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Jul 22 13:52:45 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Braden Helmer (bradenhelmer)
<details>
<summary>Changes</summary>
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
---
Full diff: https://github.com/llvm/llvm-project/pull/99933.diff
2 Files Affected:
- (modified) clang/lib/Parse/ParseStmt.cpp (+9)
- (modified) clang/test/Parser/cxx-template-decl.cpp (+4)
``````````diff
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}}
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/99933
More information about the cfe-commits
mailing list