Fix PR6473.

Clang's support for weakref is now better than llvm-gcc's :-)

We don't introduce a new symbol and we correctly mark undefined references weak only if there is no
definition or regular undefined references in the same file.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@97733 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp
index 030d2c9..dc9ecd6 100644
--- a/lib/CodeGen/CGExpr.cpp
+++ b/lib/CodeGen/CGExpr.cpp
@@ -1062,6 +1062,16 @@
 LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
   const NamedDecl *ND = E->getDecl();
 
+  if (ND->hasAttr<WeakRefAttr>()) {
+    const ValueDecl* VD = cast<ValueDecl>(ND);
+    llvm::Constant *Aliasee = CGM.GetWeakRefReference(VD);
+
+    Qualifiers Quals = MakeQualifiers(E->getType());
+    LValue LV = LValue::MakeAddr(Aliasee, Quals);
+
+    return LV;
+  }
+
   if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
     
     // Check if this is a global variable.
diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp
index bc2bd6c..e6e9b1a 100644
--- a/lib/CodeGen/CodeGenModule.cpp
+++ b/lib/CodeGen/CodeGenModule.cpp
@@ -619,9 +619,41 @@
   return false;
 }
 
+llvm::Constant *CodeGenModule::GetWeakRefReference(const ValueDecl *VD) {
+  const AliasAttr *AA = VD->getAttr<AliasAttr>();
+  assert(AA && "No alias?");
+
+  const llvm::Type *DeclTy = getTypes().ConvertTypeForMem(VD->getType());
+
+  // Unique the name through the identifier table.
+  const char *AliaseeName =
+    getContext().Idents.get(AA->getAliasee()).getNameStart();
+
+  // See if there is already something with the target's name in the module.
+  llvm::GlobalValue *Entry = GlobalDeclMap[AliaseeName];
+
+  llvm::Constant *Aliasee;
+  if (isa<llvm::FunctionType>(DeclTy))
+    Aliasee = GetOrCreateLLVMFunction(AliaseeName, DeclTy, GlobalDecl());
+  else
+    Aliasee = GetOrCreateLLVMGlobal(AliaseeName,
+                                    llvm::PointerType::getUnqual(DeclTy), 0);
+  if (!Entry) {
+    llvm::GlobalValue* F = cast<llvm::GlobalValue>(Aliasee);
+    F->setLinkage(llvm::Function::ExternalWeakLinkage);    
+    WeakRefReferences.insert(F);
+  }
+
+  return Aliasee;
+}
+
 void CodeGenModule::EmitGlobal(GlobalDecl GD) {
   const ValueDecl *Global = cast<ValueDecl>(GD.getDecl());
 
+  // Weak references don't produce any output by themselves.
+  if (Global->hasAttr<WeakRefAttr>())
+    return;
+
   // If this is an alias definition (which otherwise looks like a declaration)
   // emit it now.
   if (Global->hasAttr<AliasAttr>())
@@ -708,6 +740,14 @@
   // Lookup the entry, lazily creating it if necessary.
   llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName];
   if (Entry) {
+    if (WeakRefReferences.count(Entry)) {
+      const FunctionDecl *FD = cast_or_null<FunctionDecl>(D.getDecl());
+      if (FD && !FD->hasAttr<WeakAttr>())
+	Entry->setLinkage(llvm::Function::ExternalLinkage);
+
+      WeakRefReferences.erase(Entry);
+    }
+
     if (Entry->getType()->getElementType() == Ty)
       return Entry;
 
@@ -817,6 +857,13 @@
   // Lookup the entry, lazily creating it if necessary.
   llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName];
   if (Entry) {
+    if (WeakRefReferences.count(Entry)) {
+      if (D && !D->hasAttr<WeakAttr>())
+	Entry->setLinkage(llvm::Function::ExternalLinkage);
+
+      WeakRefReferences.erase(Entry);
+    }
+
     if (Entry->getType() == Ty)
       return Entry;
 
diff --git a/lib/CodeGen/CodeGenModule.h b/lib/CodeGen/CodeGenModule.h
index ac83326..c839b42 100644
--- a/lib/CodeGen/CodeGenModule.h
+++ b/lib/CodeGen/CodeGenModule.h
@@ -29,6 +29,7 @@
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringSet.h"
+#include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/Support/ValueHandle.h"
 #include <list>
 
@@ -117,6 +118,11 @@
   /// pointer lookups instead of full string lookups.
   llvm::DenseMap<const char*, llvm::GlobalValue*> GlobalDeclMap;
 
+  // WeakRefReferences - A set of references that have only been seen via
+  // a weakref so far. This is used to remove the weak of the reference if we ever
+  // see a direct reference or a definition.
+  llvm::SmallPtrSet<llvm::GlobalValue*, 10> WeakRefReferences;
+
   /// \brief Contains the strings used for mangled names.
   ///
   /// FIXME: Eventually, this should map from the semantic/canonical
@@ -243,6 +249,9 @@
   void BuildThunksForVirtual(GlobalDecl GD);
   void BuildThunksForVirtualRecursive(GlobalDecl GD, GlobalDecl BaseOGD);
 
+  /// GetWeakRefReference - Get a reference to the target of VD.
+  llvm::Constant *GetWeakRefReference(const ValueDecl *VD);
+
   /// BuildThunk - Build a thunk for the given method.
   llvm::Constant *BuildThunk(GlobalDecl GD, bool Extern, 
                              const ThunkAdjustment &ThisAdjustment);