r291576 - CGDecl: Skip static variable initializers in unreachable code

Matthias Braun via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 10 09:43:01 PST 2017


Author: matze
Date: Tue Jan 10 11:43:01 2017
New Revision: 291576

URL: http://llvm.org/viewvc/llvm-project?rev=291576&view=rev
Log:
CGDecl: Skip static variable initializers in unreachable code

This fixes http://llvm.org/PR31054

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

Added:
    cfe/trunk/test/CodeGenCXX/pr31054.cpp
Modified:
    cfe/trunk/lib/CodeGen/CGDecl.cpp

Modified: cfe/trunk/lib/CodeGen/CGDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDecl.cpp?rev=291576&r1=291575&r2=291576&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGDecl.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGDecl.cpp Tue Jan 10 11:43:01 2017
@@ -311,7 +311,7 @@ CodeGenFunction::AddInitializerToStaticV
   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 @@ CodeGenFunction::AddInitializerToStaticV
   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.

Added: cfe/trunk/test/CodeGenCXX/pr31054.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/pr31054.cpp?rev=291576&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenCXX/pr31054.cpp (added)
+++ cfe/trunk/test/CodeGenCXX/pr31054.cpp Tue Jan 10 11:43:01 2017
@@ -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: }




More information about the cfe-commits mailing list