From 3b304531b77b2a672da182dedfae39c97344e1eb Mon Sep 17 00:00:00 2001
From: David Majnemer <david.majnemer@gmail.com>
Date: Wed, 2 Sep 2015 15:50:38 +0000
Subject: [PATCH] [MS ABI] Number unnamed TagDecls which aren't externally
 visible

TagDecls (structs, enums, etc.) may have the same name for linkage
purposes of one another; to disambiguate, we add a number to the mangled
named.  However, we didn't do this if the TagDecl has a pseudo-name for
linkage purposes (it was defined alongside a DeclaratorDecl or a
TypeNameDecl).

This fixes PR24651.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@246659 91177308-0d34-0410-b5e6-96231b3b80d8
---
 lib/AST/MicrosoftMangle.cpp         |  4 +++-
 test/CodeGenCXX/mangle-ms-cxx11.cpp | 16 ++++++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/lib/AST/MicrosoftMangle.cpp b/lib/AST/MicrosoftMangle.cpp
index 95ae8f55b4b..dd6e952bbae 100644
--- a/lib/AST/MicrosoftMangle.cpp
+++ b/lib/AST/MicrosoftMangle.cpp
@@ -180,7 +180,9 @@ public:
 
     // Anonymous tags are already numbered.
     if (const TagDecl *Tag = dyn_cast<TagDecl>(ND)) {
-      if (Tag->getName().empty() && !Tag->getTypedefNameForAnonDecl())
+      if (!Tag->hasNameForLinkage() &&
+          !getASTContext().getDeclaratorForUnnamedTagDecl(Tag) &&
+          !getASTContext().getTypedefNameForUnnamedTagDecl(Tag))
         return false;
     }
 
diff --git a/test/CodeGenCXX/mangle-ms-cxx11.cpp b/test/CodeGenCXX/mangle-ms-cxx11.cpp
index 7cb09704de0..0a4c602df62 100644
--- a/test/CodeGenCXX/mangle-ms-cxx11.cpp
+++ b/test/CodeGenCXX/mangle-ms-cxx11.cpp
@@ -261,3 +261,19 @@ struct B {
 void f(decltype(B<int>::e)) {}
 // CHECK-DAG: @"\01?f@UnnamedType@@YAXPAW4<unnamed-type-e>@?$B@H@1@@Z
 }
+
+namespace PR24651 {
+template <typename T>
+void f(T) {}
+
+void g() {
+  enum {} E;
+  f(E);
+  {
+    enum {} E;
+    f(E);
+  }
+}
+// CHECK-DAG: @"\01??$f@W4<unnamed-type-E>@?1??g@PR24651@@YAXXZ@@PR24651@@YAXW4<unnamed-type-E>@?1??g@0@YAXXZ@@Z"
+// CHECK-DAG: @"\01??$f@W4<unnamed-type-E>@?2??g@PR24651@@YAXXZ@@PR24651@@YAXW4<unnamed-type-E>@?2??g@0@YAXXZ@@Z"
+}
-- 
GitLab