]> granicus.if.org Git - clang/commitdiff
When using a symbol with attribute weakref, search for it first and
authorJoerg Sonnenberger <joerg@bec.de>
Tue, 16 Oct 2012 17:45:27 +0000 (17:45 +0000)
committerJoerg Sonnenberger <joerg@bec.de>
Tue, 16 Oct 2012 17:45:27 +0000 (17:45 +0000)
don't try the normal GetOrCreateLLVM. The latter could drop the weak
atrtibute on the second reference, if there is no explicit declaration
of the aliasee.

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

lib/CodeGen/CodeGenModule.cpp
test/CodeGen/attr-weakref.c

index 6be225c6b5223df1e442c20b7ea8ed8f7a8f2cbf..3353612c905db77c7b756c21e52600eb9ede7a46 100644 (file)
@@ -884,6 +884,10 @@ llvm::Constant *CodeGenModule::GetWeakRefReference(const ValueDecl *VD) {
 
   // See if there is already something with the target's name in the module.
   llvm::GlobalValue *Entry = GetGlobalValue(AA->getAliasee());
+  if (Entry) {
+    unsigned AS = getContext().getTargetAddressSpace(VD->getType());
+    return llvm::ConstantExpr::getBitCast(Entry, DeclTy->getPointerTo(AS));
+  }
 
   llvm::Constant *Aliasee;
   if (isa<llvm::FunctionType>(DeclTy))
@@ -893,11 +897,10 @@ llvm::Constant *CodeGenModule::GetWeakRefReference(const ValueDecl *VD) {
   else
     Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(),
                                     llvm::PointerType::getUnqual(DeclTy), 0);
-  if (!Entry) {
-    llvm::GlobalValue* F = cast<llvm::GlobalValue>(Aliasee);
-    F->setLinkage(llvm::Function::ExternalWeakLinkage);    
-    WeakRefReferences.insert(F);
-  }
+
+  llvm::GlobalValue* F = cast<llvm::GlobalValue>(Aliasee);
+  F->setLinkage(llvm::Function::ExternalWeakLinkage);
+  WeakRefReferences.insert(F);
 
   return Aliasee;
 }
index c1cc03b668d999d95910c2f72c578b86671df106..560d39141ca9385fa41a3509cc048fa0a6ecd0fd 100644 (file)
@@ -53,6 +53,12 @@ void test6_foo(void) {
   test6_f();
 }
 
+// CHECK: declare extern_weak void @test8_f()
+static void test8_g(void) __attribute__((weakref("test8_f")));
+void test8_h(void) {
+  if (test8_g)
+    test8_g();
+}
 // CHECK: declare extern_weak void @test7_f()
 void test7_f(void);
 static void test7_g(void) __attribute__((weakref("test7_f")));