https://github.com/bradenhelmer updated https://github.com/llvm/llvm-project/pull/99933
>From 1aaa6ff0f1843f10606064f6b285c7070aaa5b44 Mon Sep 17 00:00:00 2001 From: Braden Helmer <bradenhelmer...@gmail.com> Date: Mon, 22 Jul 2024 16:46:04 -0400 Subject: [PATCH 1/2] 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}} +} >From 26853b9c5d65f6bcc74d52cdaeec333e8bd0422c Mon Sep 17 00:00:00 2001 From: Braden Helmer <bradenhelmer...@gmail.com> Date: Mon, 22 Jul 2024 17:46:27 -0400 Subject: [PATCH 2/2] Update another template error message, add and fixed tests accordingly --- clang/include/clang/Basic/DiagnosticSemaKinds.td | 2 +- clang/test/Parser/cxx-template-decl.cpp | 2 +- clang/test/SemaCXX/invalid-template-declaration.cpp | 6 ++++++ clang/test/SemaTemplate/class-template-decl.cpp | 2 +- clang/test/SemaTemplate/nested-template.cpp | 2 +- 5 files changed, 10 insertions(+), 4 deletions(-) create mode 100644 clang/test/SemaCXX/invalid-template-declaration.cpp diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index c4a4e8064fac2..7790982301177 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -5165,7 +5165,7 @@ def warn_cxx11_compat_variable_template : Warning< InGroup<CXXPre14Compat>, DefaultIgnore; def err_template_variable_noparams : Error< "extraneous 'template<>' in declaration of variable %0">; -def err_template_member : Error<"member %0 declared as a template">; +def err_template_member : Error<"non-static data member %0 cannot be declared as a template">; def err_member_with_template_arguments : Error<"member %0 cannot have template arguments">; def err_template_member_noparams : Error< "extraneous 'template<>' in declaration of member %0">; diff --git a/clang/test/Parser/cxx-template-decl.cpp b/clang/test/Parser/cxx-template-decl.cpp index 5a2d1cec9ad31..476341686a64b 100644 --- a/clang/test/Parser/cxx-template-decl.cpp +++ b/clang/test/Parser/cxx-template-decl.cpp @@ -298,6 +298,6 @@ namespace PR46231 { template<int> int; // expected-error {{declaration does not declare anything}} } -namespace NoTemplateInBlockScope { +namespace PR99933 { void foo() { template <typename> int i; } // expected-error {{templates can only be declared in namespace or class scope}} } diff --git a/clang/test/SemaCXX/invalid-template-declaration.cpp b/clang/test/SemaCXX/invalid-template-declaration.cpp new file mode 100644 index 0000000000000..fda147520cca8 --- /dev/null +++ b/clang/test/SemaCXX/invalid-template-declaration.cpp @@ -0,0 +1,6 @@ +// RUN: %clang_cc1 %s -verify -fsyntax-only +// PR99933 + +struct S { + template <typename> int i; // expected-error {{non-static data member 'i' cannot be declared as a template}} +}; diff --git a/clang/test/SemaTemplate/class-template-decl.cpp b/clang/test/SemaTemplate/class-template-decl.cpp index c054a6a8d82f7..0374c4eba982a 100644 --- a/clang/test/SemaTemplate/class-template-decl.cpp +++ b/clang/test/SemaTemplate/class-template-decl.cpp @@ -63,7 +63,7 @@ class X { }; void f() { - template<typename T> class X; // expected-error{{expression}} + template<typename T> class X; // expected-error{{templates can only be declared in namespace or class scope}} } template<typename T> class X1 var; // expected-error {{variable has incomplete type 'class X1'}} \ diff --git a/clang/test/SemaTemplate/nested-template.cpp b/clang/test/SemaTemplate/nested-template.cpp index a6ede8e99037e..a54992a397187 100644 --- a/clang/test/SemaTemplate/nested-template.cpp +++ b/clang/test/SemaTemplate/nested-template.cpp @@ -132,7 +132,7 @@ namespace PR10896 { private: template<typename T> - T SomeField; // expected-error {{member 'SomeField' declared as a template}} + T SomeField; // expected-error {{non-static data member 'SomeField' cannot be declared as a template}} template<> int SomeField2; // expected-error {{extraneous 'template<>' in declaration of variable 'SomeField2'}} }; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits