]> granicus.if.org Git - clang/commitdiff
CGDecl: Skip static variable initializers in unreachable code
authorMatthias Braun <matze@braunis.de>
Tue, 10 Jan 2017 17:43:01 +0000 (17:43 +0000)
committerMatthias Braun <matze@braunis.de>
Tue, 10 Jan 2017 17:43:01 +0000 (17:43 +0000)
This fixes http://llvm.org/PR31054

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@291576 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGDecl.cpp
test/CodeGenCXX/pr31054.cpp [new file with mode: 0644]

index d76136380160bfb8b878bd583d62ff0ba7cbc6a7..0a88b2310beb4e825733ecf722176d893f607c3d 100644 (file)
@@ -311,7 +311,7 @@ CodeGenFunction::AddInitializerToStaticVarDecl(const VarDecl &D,
   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::AddInitializerToStaticVarDecl(const VarDecl &D,
   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.
diff --git a/test/CodeGenCXX/pr31054.cpp b/test/CodeGenCXX/pr31054.cpp
new file mode 100644 (file)
index 0000000..33b17b9
--- /dev/null
@@ -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: }