[PATCH] D28505: CGDecl: Skip static variable initializers in unreachable code

Matthias Braun via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 9 17:31:34 PST 2017


MatzeB created this revision.
MatzeB added a reviewer: rsmith.
MatzeB added a subscriber: cfe-commits.
MatzeB set the repository for this revision to rL LLVM.
Herald added a subscriber: mcrosier.

This fixes http://llvm.org/PR31054

I don't know whether that is the correct fix: Are we actually allowed to skip the destructor registration if the static variable is unreachable?


Repository:
  rL LLVM

https://reviews.llvm.org/D28505

Files:
  lib/CodeGen/CGDecl.cpp
  test/CodeGenCXX/pr31054.cpp


Index: test/CodeGenCXX/pr31054.cpp
===================================================================
--- /dev/null
+++ test/CodeGenCXX/pr31054.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -o - %s | FileCheck %s
+
+struct A { ~A(); };
+void func() {
+  return;
+  static A k;
+}
+
+// Test that we did not crash, by checking whether function was created.
+// CHECK-LABEL: define void @_Z4funcv() #0 {
+// CHECK: ret void
+// CHECK: }
Index: lib/CodeGen/CGDecl.cpp
===================================================================
--- lib/CodeGen/CGDecl.cpp
+++ lib/CodeGen/CGDecl.cpp
@@ -311,7 +311,7 @@
   if (!Init) {
     if (!getLangOpts().CPlusPlus)
       CGM.ErrorUnsupported(D.getInit(), "constant l-value expression");
-    else if (Builder.GetInsertBlock()) {
+    else if (HaveInsertPoint()) {
       // Since we have a static initializer, this global variable can't
       // be constant.
       GV->setConstant(false);
@@ -352,7 +352,7 @@
   GV->setConstant(CGM.isTypeConstant(D.getType(), true));
   GV->setInitializer(Init);
 
-  if (hasNontrivialDestruction(D.getType())) {
+  if (hasNontrivialDestruction(D.getType()) && HaveInsertPoint()) {
     // We have a constant initializer, but a nontrivial destructor. We still
     // need to perform a guarded "initialization" in order to register the
     // destructor.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D28505.83754.patch
Type: text/x-patch
Size: 1374 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170110/73430423/attachment.bin>


More information about the cfe-commits mailing list