ahatanak created this revision.
ahatanak added reviewers: rsmith, rjmccall.
If an initializer in a braced-init-list is a C++ class that has a non-trivial 
destructor, mark the destructor as referenced. This fixes a crash in 
CodeGenFunction::destroyCXXObject that occurs when it tries to emit a call to 
the destructor on the stack unwinding path but the CXXRecordDecl of the class 
doesn't have a CXXDestructorDecl for the destructor.


Repository:
  rC Clang

https://reviews.llvm.org/D45898

Files:
  lib/Sema/SemaInit.cpp
  test/CodeGenObjCXX/arc-list-init-destruct.mm


Index: test/CodeGenObjCXX/arc-list-init-destruct.mm
===================================================================
--- /dev/null
+++ test/CodeGenObjCXX/arc-list-init-destruct.mm
@@ -0,0 +1,34 @@
+// RUN: %clang_cc1 -triple x86_64-apple-macosx10.13.0 -std=c++1z -fobjc-arc 
-fobjc-exceptions -fcxx-exceptions -fexceptions -emit-llvm -o - %s | FileCheck 
%s
+
+// CHECK: %[[V0:.*]] = type opaque
+// CHECK: %[[STRUCT_CLASS1:.*]] = type { %[[V0]]* }
+
+@interface Class0;
+@end
+
+struct Class1 {
+  Class0 *f;
+};
+
+struct Container {
+  Class1 a;
+  bool b;
+};
+
+bool getBool() {
+  return false;
+}
+
+Class0 *g;
+
+// CHECK: define {{.*}} @_Z4testv()
+// CHECK: invoke zeroext i1 @_Z7getBoolv()
+// CHECK: landingpad { i8*, i32 }
+// CHECK: call void @_ZN6Class1D1Ev(%[[STRUCT_CLASS1]]* %{{.*}})
+// CHECK: br label
+
+// CHECK: define linkonce_odr void @_ZN6Class1D1Ev(
+
+Container test() {
+  return {{g}, getBool()};
+}
Index: lib/Sema/SemaInit.cpp
===================================================================
--- lib/Sema/SemaInit.cpp
+++ lib/Sema/SemaInit.cpp
@@ -7065,6 +7065,14 @@
         *ResultType = Ty;
       }
 
+      for (Expr *E : InitList->inits())
+        // If this initializer's type is a C++ class that has a non-trivial
+        // destructor, mark the destructor as referenced. The destructor can be
+        // invoked during stack unwinding.
+        if (auto *RD = E->getType()->getAsCXXRecordDecl())
+          if (RD->hasDefinition() && RD->hasNonTrivialDestructor())
+            S.MarkFunctionReferenced(E->getExprLoc(), S.LookupDestructor(RD));
+
       InitListExpr *StructuredInitList =
           PerformInitList.getFullyStructuredList();
       CurInit.get();


Index: test/CodeGenObjCXX/arc-list-init-destruct.mm
===================================================================
--- /dev/null
+++ test/CodeGenObjCXX/arc-list-init-destruct.mm
@@ -0,0 +1,34 @@
+// RUN: %clang_cc1 -triple x86_64-apple-macosx10.13.0 -std=c++1z -fobjc-arc -fobjc-exceptions -fcxx-exceptions -fexceptions -emit-llvm -o - %s | FileCheck %s
+
+// CHECK: %[[V0:.*]] = type opaque
+// CHECK: %[[STRUCT_CLASS1:.*]] = type { %[[V0]]* }
+
+@interface Class0;
+@end
+
+struct Class1 {
+  Class0 *f;
+};
+
+struct Container {
+  Class1 a;
+  bool b;
+};
+
+bool getBool() {
+  return false;
+}
+
+Class0 *g;
+
+// CHECK: define {{.*}} @_Z4testv()
+// CHECK: invoke zeroext i1 @_Z7getBoolv()
+// CHECK: landingpad { i8*, i32 }
+// CHECK: call void @_ZN6Class1D1Ev(%[[STRUCT_CLASS1]]* %{{.*}})
+// CHECK: br label
+
+// CHECK: define linkonce_odr void @_ZN6Class1D1Ev(
+
+Container test() {
+  return {{g}, getBool()};
+}
Index: lib/Sema/SemaInit.cpp
===================================================================
--- lib/Sema/SemaInit.cpp
+++ lib/Sema/SemaInit.cpp
@@ -7065,6 +7065,14 @@
         *ResultType = Ty;
       }
 
+      for (Expr *E : InitList->inits())
+        // If this initializer's type is a C++ class that has a non-trivial
+        // destructor, mark the destructor as referenced. The destructor can be
+        // invoked during stack unwinding.
+        if (auto *RD = E->getType()->getAsCXXRecordDecl())
+          if (RD->hasDefinition() && RD->hasNonTrivialDestructor())
+            S.MarkFunctionReferenced(E->getExprLoc(), S.LookupDestructor(RD));
+
       InitListExpr *StructuredInitList =
           PerformInitList.getFullyStructuredList();
       CurInit.get();
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D45898: [... Akira Hatanaka via Phabricator via cfe-commits

Reply via email to