[PATCH] D120881: [Clang] Diagnose invalid member variable with template parameters
Corentin Jabot via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 3 05:42:02 PST 2022
cor3ntin updated this revision to Diff 412689.
cor3ntin added a comment.
- Fix redundant check
- move test to p17.cpp which seems more approriate
- add a couple of tests
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D120881/new/
https://reviews.llvm.org/D120881
Files:
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/lib/Sema/SemaDeclCXX.cpp
clang/test/CXX/temp/temp.spec/temp.expl.spec/p17.cpp
Index: clang/test/CXX/temp/temp.spec/temp.expl.spec/p17.cpp
===================================================================
--- clang/test/CXX/temp/temp.spec/temp.expl.spec/p17.cpp
+++ clang/test/CXX/temp/temp.spec/temp.expl.spec/p17.cpp
@@ -32,3 +32,23 @@
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
Index: clang/lib/Sema/SemaDeclCXX.cpp
===================================================================
--- clang/lib/Sema/SemaDeclCXX.cpp
+++ clang/lib/Sema/SemaDeclCXX.cpp
@@ -3395,6 +3395,14 @@
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:
Index: clang/include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -4790,6 +4790,7 @@
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<
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D120881.412689.patch
Type: text/x-patch
Size: 2543 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220303/f6798cb4/attachment.bin>
More information about the cfe-commits
mailing list