fix several problems with asm renaming, by pulling it into the mangling code:

1. it wasn't applying to definitions, only declarations, e.g. int x __asm("foo")
2. multiple definitions were conflicting, they weren't getting merged.
3. the code was duplicated in several places.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67442 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp
index c46db96..97f989d 100644
--- a/lib/CodeGen/CodeGenModule.cpp
+++ b/lib/CodeGen/CodeGenModule.cpp
@@ -305,11 +305,6 @@
     setGlobalVisibility(GV, attr->getVisibility());
   // FIXME: else handle -fvisibility
 
-  // Prefaced with special LLVM marker to indicate that the name
-  // should not be munged.
-  if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>())
-    GV->setName("\01" + ALA->getLabel());
-
   if (const SectionAttr *SA = D->getAttr<SectionAttr>())
     GV->setSection(SA->getName());
 
@@ -629,12 +624,6 @@
   if (D->getAttr<WeakAttr>() || D->getAttr<WeakImportAttr>())
     GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
 
-  // FIXME: This should be handled by the mangler!
-  if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
-    // Prefaced with special LLVM marker to indicate that the name
-    // should not be munged.
-    GV->setName("\01" + ALA->getLabel());
-  }
   return Entry = GV;
 }
 
@@ -746,13 +735,6 @@
     setGlobalVisibility(GV, attr->getVisibility());
   // FIXME: else handle -fvisibility
 
-  // FIXME: This should be a mangling issue.
-  if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
-    // Prefaced with special LLVM marker to indicate that the name
-    // should not be munged.
-    GV->setName("\01" + ALA->getLabel());
-  }
-  
   // Set the llvm linkage type as appropriate.
   if (D->getStorageClass() == VarDecl::Static)
     GV->setLinkage(llvm::Function::InternalLinkage);
diff --git a/lib/CodeGen/Mangle.cpp b/lib/CodeGen/Mangle.cpp
index e760b83..7a7a480 100644
--- a/lib/CodeGen/Mangle.cpp
+++ b/lib/CodeGen/Mangle.cpp
@@ -57,6 +57,15 @@
 
 
 bool CXXNameMangler::mangle(const NamedDecl *D) {
+  // Any decl can be declared with __asm("foo") on it, and this takes
+  // precedence over all other naming in the .o file.
+  if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
+    // If we have an asm name, then we use it as the mangling.
+    Out << '\01';  // LLVM IR Marker for __asm("foo")
+    Out << ALA->getLabel();
+    return true;
+  }
+  
   // <mangled-name> ::= _Z <encoding>
   //            ::= <data name>
   //            ::= <special-name>
@@ -68,15 +77,15 @@
   
   // Clang's "overloadable" attribute extension to C/C++ implies
   // name mangling (always).
-  if (FD->getAttr<OverloadableAttr>())
+  if (FD->getAttr<OverloadableAttr>()) {
     ; // fall into mangling code unconditionally.
-  else if (// C functions are not mangled
-           !Context.getLangOptions().CPlusPlus ||
-           // "main" is not mangled in C++
-           FD->isMain() ||
-           // No mangling in an "implicit extern C" header.
-           Context.getSourceManager().getFileCharacteristic(FD->getLocation())
-                == SrcMgr::C_ExternCSystem)
+  } else if (// C functions are not mangled
+             !Context.getLangOptions().CPlusPlus ||
+             // "main" is not mangled in C++
+             FD->isMain() ||
+             // No mangling in an "implicit extern C" header.
+             Context.getSourceManager().getFileCharacteristic(FD->getLocation())
+               == SrcMgr::C_ExternCSystem)
     return false;
   else {
     // No name mangling in a C linkage specification.
diff --git a/lib/CodeGen/Mangle.h b/lib/CodeGen/Mangle.h
index 60a5113..627c16a 100644
--- a/lib/CodeGen/Mangle.h
+++ b/lib/CodeGen/Mangle.h
@@ -14,6 +14,7 @@
 //   http://www.codesourcery.com/public/cxx-abi/abi.html
 //
 //===----------------------------------------------------------------------===//
+
 #ifndef LLVM_CLANG_CODEGEN_MANGLE_H
 #define LLVM_CLANG_CODEGEN_MANGLE_H