[clang] 603c1a6 - [clang] Don't crash on an incomplete-type base specifier in template context.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 9 13:18:05 PST 2021


Author: Haojian Wu
Date: 2021-11-09T22:17:47+01:00
New Revision: 603c1a62f8595f70c3e96ecbec8976d411c0cc08

URL: https://github.com/llvm/llvm-project/commit/603c1a62f8595f70c3e96ecbec8976d411c0cc08
DIFF: https://github.com/llvm/llvm-project/commit/603c1a62f8595f70c3e96ecbec8976d411c0cc08.diff

LOG: [clang] Don't crash on an incomplete-type base specifier in template context.

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

Added: 
    

Modified: 
    clang/lib/AST/DeclCXX.cpp
    clang/lib/Sema/SemaDeclCXX.cpp
    clang/test/SemaCXX/base-class-ambiguity-check.cpp
    clang/test/SemaCXX/ms-interface.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/DeclCXX.cpp b/clang/lib/AST/DeclCXX.cpp
index 945c935640d54..1780358cc3484 100644
--- a/clang/lib/AST/DeclCXX.cpp
+++ b/clang/lib/AST/DeclCXX.cpp
@@ -178,6 +178,8 @@ static bool hasRepeatedBaseClass(const CXXRecordDecl *StartRD) {
   SmallVector<const CXXRecordDecl*, 8> WorkList = {StartRD};
   while (!WorkList.empty()) {
     const CXXRecordDecl *RD = WorkList.pop_back_val();
+    if (RD->getTypeForDecl()->isDependentType())
+      continue;
     for (const CXXBaseSpecifier &BaseSpec : RD->bases()) {
       if (const CXXRecordDecl *B = BaseSpec.getType()->getAsCXXRecordDecl()) {
         if (!SeenBaseTypes.insert(B).second)

diff  --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 562d10313ec93..d67e039dd26cc 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -2729,6 +2729,8 @@ bool Sema::AttachBaseSpecifiers(CXXRecordDecl *Class,
       KnownBase = Bases[idx];
       Bases[NumGoodBases++] = Bases[idx];
 
+      if (NewBaseType->isDependentType())
+        continue;
       // Note this base's direct & indirect bases, if there could be ambiguity.
       if (Bases.size() > 1)
         NoteIndirectBases(Context, IndirectBaseTypes, NewBaseType);

diff  --git a/clang/test/SemaCXX/base-class-ambiguity-check.cpp b/clang/test/SemaCXX/base-class-ambiguity-check.cpp
index fc1c4c2cea5ec..a7f5ee1d77106 100644
--- a/clang/test/SemaCXX/base-class-ambiguity-check.cpp
+++ b/clang/test/SemaCXX/base-class-ambiguity-check.cpp
@@ -1,5 +1,4 @@
 // RUN: %clang_cc1 -fsyntax-only -verify %s
-// expected-no-diagnostics
 
 template <typename T> class Foo {
   struct Base : T {};
@@ -7,3 +6,14 @@ template <typename T> class Foo {
   // Test that this code no longer causes a crash in Sema. rdar://23291875
   struct Derived : Base, T {};
 };
+
+
+template <typename T> struct Foo2 {
+  struct Base1; // expected-note{{member is declared here}}
+  struct Base2; // expected-note{{member is declared here}}
+  // Should not crash on an incomplete-type and dependent base specifier.
+  struct Derived : Base1, Base2 {}; // expected-error {{implicit instantiation of undefined member 'Foo2<int>::Base1'}} \
+                                       expected-error {{implicit instantiation of undefined member 'Foo2<int>::Base2'}}
+};
+
+Foo2<int>::Derived a; // expected-note{{in instantiation of member class}}

diff  --git a/clang/test/SemaCXX/ms-interface.cpp b/clang/test/SemaCXX/ms-interface.cpp
index c827f4c5703c4..67294f761d030 100644
--- a/clang/test/SemaCXX/ms-interface.cpp
+++ b/clang/test/SemaCXX/ms-interface.cpp
@@ -106,3 +106,10 @@ static_assert(!__is_interface_class(HasProp), "oops");
 static_assert(!__is_interface_class(IUnknown), "oops");
 static_assert(!__is_interface_class(IFaceStruct), "oops");
 static_assert(!__is_interface_class(IFaceInheritsStruct), "oops");
+
+template<typename>
+class TemplateContext {
+  class Base;
+  // Should not crash on an incomplete-type and dependent base specifier.
+  __interface Foo : Base {};
+};


        


More information about the cfe-commits mailing list