]> granicus.if.org Git - clang/commitdiff
Emit guard variables for any weak global that has a run-time
authorDouglas Gregor <dgregor@apple.com>
Fri, 1 Jul 2011 21:54:36 +0000 (21:54 +0000)
committerDouglas Gregor <dgregor@apple.com>
Fri, 1 Jul 2011 21:54:36 +0000 (21:54 +0000)
initializer. Previously, we only used guard variables for weak static
data members. Fixes <rdar://problem/9692249>.

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

lib/CodeGen/CGDeclCXX.cpp
test/CodeGenCXX/global-init.cpp

index c305f01fe3a734418abd944c2f3069cbdfda107f..42a07df5e47d8b44d4ade167bce04b287075e153 100644 (file)
@@ -269,12 +269,11 @@ void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
                 getTypes().getNullaryFunctionInfo(),
                 FunctionArgList(), SourceLocation());
 
-  // Use guarded initialization if the global variable is weak due to
-  // being a class template's static data member.  These will always
-  // have weak_odr linkage.
-  if (Addr->getLinkage() == llvm::GlobalValue::WeakODRLinkage &&
-      D->isStaticDataMember() &&
-      D->getInstantiatedFromStaticDataMember()) {
+  // Use guarded initialization if the global variable is weak. This
+  // occurs for, e.g., instantiated static data members and
+  // definitions explicitly marked weak.
+  if (Addr->getLinkage() == llvm::GlobalValue::WeakODRLinkage ||
+      Addr->getLinkage() == llvm::GlobalValue::WeakAnyLinkage) {
     EmitCXXGuardedInit(*D, Addr);
   } else {
     EmitCXXGlobalVarDeclInit(*D, Addr);
index 9bd7390d076ceedd84dd661368d56757957156d5..fd8734c5fada6186ecc5fc2cd4fcb1197304e03f 100644 (file)
@@ -70,6 +70,20 @@ namespace test3 {
   const char *test() { return var; }
 }
 
+namespace test6 {
+  struct A {
+    A();
+  };
+  extern int foo();
+
+  // This needs an initialization function and guard variables.
+  // CHECK: load i8* bitcast (i64* @_ZGVN5test61xE
+  // CHECK: [[CALL:%.*]] = call i32 @_ZN5test63fooEv
+  // CHECK-NEXT: store i32 [[CALL]], i32* @_ZN5test61xE
+  // CHECK-NEXT: store i64 1, i64* @_ZGVN5test61xE
+  __attribute__((weak)) int x = foo();
+}
+
 namespace PR5974 {
   struct A { int a; };
   struct B { int b; };
@@ -97,15 +111,6 @@ namespace test5 {
   };
 }
 
-namespace test6 {
-  struct A {
-    A();
-  };
-  extern int foo();
-
-  // This needs an initialization function but not guard variables.
-  __attribute__((weak)) int x = foo();
-}
 
 // At the end of the file, we check that y is initialized before z.