[PATCH] D115248: [Clang] Fix PR28101

PoYao Chang via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 22 23:02:42 PDT 2022


rZhBoYao updated this revision to Diff 417504.
rZhBoYao set the repository for this revision to rG LLVM Github Monorepo.
rZhBoYao added a comment.

Diagnose "same name as its class" before setting the declarator invalid as otherwise it would not be diagnosed. This also aligns with gcc's behavior.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115248/new/

https://reviews.llvm.org/D115248

Files:
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/test/SemaCXX/PR28101.cpp


Index: clang/test/SemaCXX/PR28101.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/PR28101.cpp
@@ -0,0 +1,53 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -DCASE_1 %s
+// RUN: %clang_cc1 -fsyntax-only -verify -DCASE_2 %s
+// RUN: %clang_cc1 -fsyntax-only -verify -DCASE_3 %s
+// RUN: %clang_cc1 -fsyntax-only -verify -DCASE_4 -std=c++17 %s
+
+// Don't crash.
+
+#ifdef CASE_1
+
+template <typename T> struct A {
+  A(void *) {}
+  T(A<T>){}; // expected-error{{member 'A' cannot have template arguments}}\
+  // expected-error{{member 'A' has the same name as its class}}
+};
+
+A<int> instantiate() { return {nullptr}; }
+
+#elifdef CASE_2
+
+template <typename T> struct A {
+  A(void *) {}
+  T A<T>{}; // expected-error{{member 'A' cannot have template arguments}}\
+  // expected-error{{member 'A' has the same name as its class}}
+};
+
+A<int> instantiate() { return {nullptr}; }
+
+#elifdef CASE_3
+
+template <typename T> struct S {};
+
+template <typename T> struct A {
+  A(void *) {}
+  T S<T>{}; // expected-error{{member 'S' cannot have template arguments}}
+};
+
+A<int> instantiate() { return {nullptr}; }
+
+#elifdef CASE_4
+
+template <typename T, template <typename> typename U> class A {
+public:
+  A(void *) {}
+  T(A<T, U<T>>) {} // expected-error{{member 'A' cannot have template arguments}}\
+  // expected-error{{expected ';' at end of declaration list}}\
+  // expected-error{{member 'A' has the same name as its class}}
+};
+
+template <typename T> struct S {};
+
+A<int, S> foo() { return A<int, S>(nullptr); }
+
+#endif
Index: clang/lib/Sema/SemaDeclCXX.cpp
===================================================================
--- clang/lib/Sema/SemaDeclCXX.cpp
+++ clang/lib/Sema/SemaDeclCXX.cpp
@@ -3427,6 +3427,9 @@
           << SourceRange(D.getName().TemplateId->LAngleLoc,
                          D.getName().TemplateId->RAngleLoc)
           << D.getName().TemplateId->LAngleLoc;
+      if (cast<CXXRecordDecl>(CurContext)->getDeclName() == Name)
+        Diag(Loc, diag::err_member_name_of_class) << Name;
+      D.setInvalidType();
     }
 
     if (SS.isSet() && !SS.isInvalid()) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D115248.417504.patch
Type: text/x-patch
Size: 2182 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220323/9b10dcf9/attachment.bin>


More information about the cfe-commits mailing list