From 5112212e3b8f5e8f8ea29c97ec42b8f78adae866 Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Tue, 21 Jun 2016 03:40:16 +0000 Subject: [PATCH] [CodeGen] Do not run initializers for imported variables The export side is responsible for running any initializers, they are run when the module is first loaded. Attempting to run an initializer for the import side is not possible. This fixes PR28216. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@273237 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGDeclCXX.cpp | 4 ++++ test/CodeGenCXX/PR28216.cpp | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 test/CodeGenCXX/PR28216.cpp diff --git a/lib/CodeGen/CGDeclCXX.cpp b/lib/CodeGen/CGDeclCXX.cpp index 89d142e44b..8f8ffe66b6 100644 --- a/lib/CodeGen/CGDeclCXX.cpp +++ b/lib/CodeGen/CGDeclCXX.cpp @@ -323,6 +323,10 @@ CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D, D->hasAttr())) return; + // DLL imported variables will be initialized by the export side. + if (D->hasAttr()) + return; + // Check if we've already initialized this decl. auto I = DelayedCXXInitPosition.find(D); if (I != DelayedCXXInitPosition.end() && I->second == ~0U) diff --git a/test/CodeGenCXX/PR28216.cpp b/test/CodeGenCXX/PR28216.cpp new file mode 100644 index 0000000000..6262c87de2 --- /dev/null +++ b/test/CodeGenCXX/PR28216.cpp @@ -0,0 +1,19 @@ +// RUN: %clang_cc1 %s -triple i686-pc-win32 -fms-extensions -emit-llvm -o - | FileCheck %s + +template +struct __declspec(dllimport) S { + S(); +}; + +template +struct __declspec(dllimport) U { + static S u; +}; + +template +S U::u; + +template S U::u; +// CHECK-NOT: define internal void @"\01??__Eu@?$U@H@@2U?$S@H@@A@YAXXZ"( + +S &i = U::u; -- 2.40.0