Fix for PR5522 and PR5666: fix a bunch of mangling issues with extern variables
and funcctions declared locally within a function.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@90344 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/Mangle.cpp b/lib/CodeGen/Mangle.cpp
index 833fc47..2a11ee2 100644
--- a/lib/CodeGen/Mangle.cpp
+++ b/lib/CodeGen/Mangle.cpp
@@ -89,8 +89,6 @@
   void addSubstitution(QualType T);
   void addSubstitution(uintptr_t Ptr);
 
-  bool mangleFunctionDecl(const FunctionDecl *FD);
-
   void mangleName(const TemplateDecl *TD,
                   const TemplateArgument *TemplateArgs,
                   unsigned NumTemplateArgs);
@@ -99,7 +97,7 @@
   void mangleUnscopedTemplateName(const TemplateDecl *ND);
   void mangleSourceName(const IdentifierInfo *II);
   void mangleLocalName(const NamedDecl *ND);
-  void mangleNestedName(const NamedDecl *ND);
+  void mangleNestedName(const NamedDecl *ND, const DeclContext *DC);
   void mangleNestedName(const TemplateDecl *TD,
                         const TemplateArgument *TemplateArgs,
                         unsigned NumTemplateArgs);
@@ -169,11 +167,19 @@
       isInExternCSystemHeader(D->getLocation()))
     return false;
 
-  // C functions, "main", and variables at global scope are not
-  // mangled.
-  if ((FD && FD->isMain()) ||
-      (!FD && D->getDeclContext()->isTranslationUnit()) ||
-      isInCLinkageSpecification(D))
+  // Variables at global scope are not mangled.
+  if (!FD) {
+    const DeclContext *DC = D->getDeclContext();
+    // Check for extern variable declared locally.
+    if (isa<FunctionDecl>(DC) && D->hasLinkage())
+      while (!DC->isNamespace() && !DC->isTranslationUnit())
+        DC = DC->getParent();
+    if (DC->isTranslationUnit())
+      return false;
+  }
+
+  // C functions and "main" are not mangled.
+  if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
     return false;
 
   return true;
@@ -278,6 +284,13 @@
   //         ::= <local-name>
   //
   const DeclContext *DC = ND->getDeclContext();
+
+  // If this is an extern variable declared locally, the relevant DeclContext
+  // is that of the containing namespace, or the translation unit.
+  if (isa<FunctionDecl>(DC) && ND->hasLinkage())
+    while (!DC->isNamespace() && !DC->isTranslationUnit())
+      DC = DC->getParent();
+
   while (isa<LinkageSpecDecl>(DC))
     DC = DC->getParent();
 
@@ -299,7 +312,7 @@
     return;
   }
 
-  mangleNestedName(ND);
+  mangleNestedName(ND, DC);
 }
 void CXXNameMangler::mangleName(const TemplateDecl *TD,
                                 const TemplateArgument *TemplateArgs,
@@ -474,7 +487,8 @@
   Out << II->getLength() << II->getName();
 }
 
-void CXXNameMangler::mangleNestedName(const NamedDecl *ND) {
+void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
+                                      const DeclContext *DC) {
   // <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
   //               ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
 
@@ -488,7 +502,7 @@
     mangleTemplatePrefix(TD);
     mangleTemplateArgumentList(*TemplateArgs);
   } else {
-    manglePrefix(ND->getDeclContext());
+    manglePrefix(DC);
     mangleUnqualifiedName(ND);
   }