[clang] 80c1adf - [clang] Invalidate a non-dependent-type RecordDecl when it has any dependent-type base class specifier.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Thu May 20 06:33:15 PDT 2021


Author: Haojian Wu
Date: 2021-05-20T15:33:05+02:00
New Revision: 80c1adfd18b5308422827f8372c28cc2ecfaa015

URL: https://github.com/llvm/llvm-project/commit/80c1adfd18b5308422827f8372c28cc2ecfaa015
DIFF: https://github.com/llvm/llvm-project/commit/80c1adfd18b5308422827f8372c28cc2ecfaa015.diff

LOG: [clang] Invalidate a non-dependent-type RecordDecl when it has any dependent-type base class specifier.

This happens during the error-recovery, and it would esacpe all
dependent-type check guards in getTypeInfo/constexpr-evaluator code
paths, which lead to crashes.

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

Added: 
    

Modified: 
    clang/lib/Sema/SemaDeclCXX.cpp
    clang/test/SemaTemplate/temp_class_spec.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 1a8ef40aa86b..f4d8f4d9aa33 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -2508,6 +2508,14 @@ Sema::CheckBaseSpecifier(CXXRecordDecl *Class,
       }
     }
 
+    // Make sure that we don't make an ill-formed AST where the type of the
+    // Class is non-dependent and its attached base class specifier is an
+    // dependent type, which violates invariants in many clang code paths (e.g.
+    // constexpr evaluator). If this case happens (in errory-recovery mode), we
+    // explicitly mark the Class decl invalid. The diagnostic was already
+    // emitted.
+    if (!Class->getTypeForDecl()->isDependentType())
+      Class->setInvalidDecl();
     return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual,
                                           Class->getTagKind() == TTK_Class,
                                           Access, TInfo, EllipsisLoc);

diff  --git a/clang/test/SemaTemplate/temp_class_spec.cpp b/clang/test/SemaTemplate/temp_class_spec.cpp
index f92c52e9624e..bf57b13725dc 100644
--- a/clang/test/SemaTemplate/temp_class_spec.cpp
+++ b/clang/test/SemaTemplate/temp_class_spec.cpp
@@ -375,3 +375,16 @@ class Bar<0> : public Foo<Z> { // expected-error{{partial specialization of 'Bar
   Bar() : Foo<Z>() {}
 };
 } // namespace
+
+namespace Crash {
+template<typename T>
+class Base {};
+
+template<typename T> class Foo;
+
+template <typename T>
+class Foo<int> : public Base<T> {}; // expected-error{{partial specialization of 'Foo' does not use any of its template parameters}}
+
+// verify that getASTRecordLayout doesn't crash on the ClassTemplateSpecializationDecl.
+constexpr int s = sizeof(Foo<int>);
+}


        


More information about the cfe-commits mailing list