[clang] 942c039 - [Clang] Diagnose invalid member variable with template parameters

Corentin Jabot via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 3 06:51:53 PST 2022


Author: Corentin Jabot
Date: 2022-03-03T15:51:46+01:00
New Revision: 942c03910aef43de1cd2c733a82370fd3e2c9981

URL: https://github.com/llvm/llvm-project/commit/942c03910aef43de1cd2c733a82370fd3e2c9981
DIFF: https://github.com/llvm/llvm-project/commit/942c03910aef43de1cd2c733a82370fd3e2c9981.diff

LOG: [Clang] Diagnose invalid member variable with template parameters

Fixes https://github.com/llvm/llvm-project/issues/54151

Reviewed By: erichkeane, aaron.ballman

Differential Revision: https://reviews.llvm.org/D120881

Added: 
    

Modified: 
    clang/include/clang/Basic/DiagnosticSemaKinds.td
    clang/lib/Sema/SemaDeclCXX.cpp
    clang/test/CXX/temp/temp.spec/temp.expl.spec/p17.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 945d4e2976503..0468370536fc0 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -4811,6 +4811,7 @@ def warn_cxx11_compat_variable_template : Warning<
 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_member_with_template_arguments : Error<"member %0 cannot have template arguments">;
 def err_template_member_noparams : Error<
   "extraneous 'template<>' in declaration of member %0">;
 def err_template_tag_noparams : Error<

diff  --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 90a1cf514eb6e..a845d60f6d880 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -3395,6 +3395,14 @@ Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D,
       return nullptr;
     }
 
+    if (D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId) {
+      Diag(D.getIdentifierLoc(), diag::err_member_with_template_arguments)
+          << II
+          << SourceRange(D.getName().TemplateId->LAngleLoc,
+                         D.getName().TemplateId->RAngleLoc)
+          << D.getName().TemplateId->LAngleLoc;
+    }
+
     if (SS.isSet() && !SS.isInvalid()) {
       // The user provided a superfluous scope specifier inside a class
       // definition:

diff  --git a/clang/test/CXX/temp/temp.spec/temp.expl.spec/p17.cpp b/clang/test/CXX/temp/temp.spec/temp.expl.spec/p17.cpp
index 56231e2f725f6..1b039627a1d3a 100644
--- a/clang/test/CXX/temp/temp.spec/temp.expl.spec/p17.cpp
+++ b/clang/test/CXX/temp/temp.spec/temp.expl.spec/p17.cpp
@@ -32,3 +32,23 @@ namespace test1 {
   template <> int AB::foo = 0; // expected-error{{extraneous 'template<>'}}
   int AB::bar = 1;
 }
+
+namespace GH54151 {
+
+struct S {
+  int i<0>;   // expected-error  {{member 'i' cannot have template arguments}}
+  int j<int>; // expected-error  {{member 'j' cannot have template arguments}}
+
+  static int k<12>; // expected-error {{template specialization requires 'template<>'}} \
+                       expected-error{{no variable template matches specialization}}
+  void f<12>();     // expected-error {{template specialization requires 'template<>'}} \
+                    // expected-error {{no function template matches function template specialization 'f'}}
+};
+
+template <typename T, int N>
+struct U {
+  int i<N>; // expected-error {{member 'i' cannot have template arguments}}
+  int j<T>; // expected-error {{member 'j' cannot have template arguments}}
+};
+
+} // namespace GH54151


        


More information about the cfe-commits mailing list