[clang] 355f1c7 - [Clang] Fix PR28101

PoYao Chang via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 23 09:39:18 PDT 2022


Author: PoYao Chang
Date: 2022-03-24T00:38:45+08:00
New Revision: 355f1c75aa66fc3d9bef897375f5e0979a55001d

URL: https://github.com/llvm/llvm-project/commit/355f1c75aa66fc3d9bef897375f5e0979a55001d
DIFF: https://github.com/llvm/llvm-project/commit/355f1c75aa66fc3d9bef897375f5e0979a55001d.diff

LOG: [Clang] Fix PR28101

Fixes https://github.com/llvm/llvm-project/issues/28475 (PR28101)
by setting identifier for invalid member variables with template parameters,
so that the invalid declarators would not crash clang.

See also: https://github.com/llvm/llvm-project/commit/942c03910a

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

Added: 
    clang/test/SemaCXX/PR28101.cpp

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaDeclCXX.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 865da1eb2aa1b..ebbed4b5ce008 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -69,6 +69,9 @@ Bug Fixes
   like ``auto&`` or ``auto**`` were added. These constraints are now checked.
   This fixes  `Issue 53911 <https://github.com/llvm/llvm-project/issues/53911>`_
   and  `Issue 54443 <https://github.com/llvm/llvm-project/issues/54443>`_.
+- Previously invalid member variables with template parameters would crash clang.
+  Now fixed by setting identifiers for them.
+  This fixes `Issue 28475 (PR28101) <https://github.com/llvm/llvm-project/issues/28475>`_.
 
 Improvements to Clang's diagnostics
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

diff  --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 008c65673ed26..7992e4a349611 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -3427,6 +3427,7 @@ Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D,
           << SourceRange(D.getName().TemplateId->LAngleLoc,
                          D.getName().TemplateId->RAngleLoc)
           << D.getName().TemplateId->LAngleLoc;
+      D.SetIdentifier(Name.getAsIdentifierInfo(), Loc);
     }
 
     if (SS.isSet() && !SS.isInvalid()) {

diff  --git a/clang/test/SemaCXX/PR28101.cpp b/clang/test/SemaCXX/PR28101.cpp
new file mode 100644
index 0000000000000..311c0f0db8174
--- /dev/null
+++ b/clang/test/SemaCXX/PR28101.cpp
@@ -0,0 +1,36 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++17 %s
+
+template <typename T> struct A {
+  A(void *) {}
+  T(A<T>){}; // expected-error{{member 'A' cannot have template arguments}}\
+  // expected-error2{{member 'A' has the same name as its class}}
+};
+// Don't crash.
+A<int> instantiate1() { return {nullptr}; } // expected-note{{in instantiation of template class 'A<int>' requested here}}
+
+template <typename T> struct B {
+  B(void *) {}
+  T B<T>{}; // expected-error{{member 'B' cannot have template arguments}}\
+  // expected-error2{{member 'B' has the same name as its class}}
+};
+// Don't crash.
+B<int> instantiate2() { return {nullptr}; } // expected-note{{in instantiation of template class 'B<int>' requested here}}
+
+template <typename T> struct S {};
+
+template <typename T> struct C {
+  C(void *) {}
+  T S<T>{}; // expected-error{{member 'S' cannot have template arguments}}
+};
+// Don't crash.
+C<int> instantiate3() { return {nullptr}; }
+
+template <typename T, template <typename> typename U> class D {
+public:
+  D(void *) {}
+  T(D<T, U<T>>) {} // expected-error{{member 'D' cannot have template arguments}}\
+  // expected-error{{expected ';' at end of declaration list}}\
+  // expected-error2{{member 'D' has the same name as its class}}
+};
+// Don't crash.
+D<int, S> instantiate4() { return D<int, S>(nullptr); } // expected-note{{in instantiation of template class 'D<int, S>' requested here}}


        


More information about the cfe-commits mailing list