[PATCH] Sema: Don't crash when trying to instantiate a local class with an invalid base specifier

David Majnemer david.majnemer at gmail.com
Thu Feb 20 11:56:38 PST 2014


Hi rsmith,

It was previously thought that Sema::InstantiateClass could not fail from within this point in instantiate.

However, it can happen if the class is invalid some way (i.e. invalid base specifier).

http://llvm-reviews.chandlerc.com/D2850

Files:
  lib/Sema/SemaTemplateInstantiateDecl.cpp
  test/SemaTemplate/instantiate-local-class.cpp

Index: lib/Sema/SemaTemplateInstantiateDecl.cpp
===================================================================
--- lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -1187,14 +1187,11 @@
   // DR1484 clarifies that the members of a local class are instantiated as part
   // of the instantiation of their enclosing entity.
   if (D->isCompleteDefinition() && D->isLocalClass()) {
-    if (SemaRef.InstantiateClass(D->getLocation(), Record, D, TemplateArgs,
-                                 TSK_ImplicitInstantiation,
-                                 /*Complain=*/true)) {
-      llvm_unreachable("InstantiateClass shouldn't fail here!");
-    } else {
-      SemaRef.InstantiateClassMembers(D->getLocation(), Record, TemplateArgs,
-                                      TSK_ImplicitInstantiation);
-    }
+    SemaRef.InstantiateClass(D->getLocation(), Record, D, TemplateArgs,
+                             TSK_ImplicitInstantiation,
+                             /*Complain=*/true);
+    SemaRef.InstantiateClassMembers(D->getLocation(), Record, TemplateArgs,
+                                    TSK_ImplicitInstantiation);
   }
   return Record;
 }
Index: test/SemaTemplate/instantiate-local-class.cpp
===================================================================
--- test/SemaTemplate/instantiate-local-class.cpp
+++ test/SemaTemplate/instantiate-local-class.cpp
@@ -1,5 +1,4 @@
 // RUN: %clang_cc1 -verify -std=c++11 %s
-// expected-no-diagnostics
 template<typename T>
 void f0() {
   struct X;
@@ -181,3 +180,17 @@
     return 0;
   }
 }
+
+namespace PR18907 {
+template <typename>
+class C : public C<int> {}; // expected-error{{within its own definition}}
+
+template <typename X>
+void F() {
+  struct A : C<X> {};
+}
+
+struct B {
+  void f() { F<int>(); }
+};
+}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2850.1.patch
Type: text/x-patch
Size: 1827 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140220/5d48990f/attachment.bin>


More information about the cfe-commits mailing list