]> granicus.if.org Git - clang/commitdiff
[CodeGen] Do not run initializers for imported variables
authorDavid Majnemer <david.majnemer@gmail.com>
Tue, 21 Jun 2016 03:40:16 +0000 (03:40 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Tue, 21 Jun 2016 03:40:16 +0000 (03:40 +0000)
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
test/CodeGenCXX/PR28216.cpp [new file with mode: 0644]

index 89d142e44b49e5ba4d53f7a4efdd58f6213c9992..8f8ffe66b6ada2323883531f0c54e5a0f4080564 100644 (file)
@@ -323,6 +323,10 @@ CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
        D->hasAttr<CUDASharedAttr>()))
     return;
 
+  // DLL imported variables will be initialized by the export side.
+  if (D->hasAttr<DLLImportAttr>())
+    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 (file)
index 0000000..6262c87
--- /dev/null
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 %s -triple i686-pc-win32 -fms-extensions -emit-llvm -o - | FileCheck %s
+
+template <typename>
+struct __declspec(dllimport) S {
+  S();
+};
+
+template <typename T>
+struct __declspec(dllimport) U {
+  static S<T> u;
+};
+
+template <typename T>
+S<T> U<T>::u;
+
+template S<int> U<int>::u;
+// CHECK-NOT: define internal void @"\01??__Eu@?$U@H@@2U?$S@H@@A@YAXXZ"(
+
+S<int> &i = U<int>::u;