[cfe-commits] r160667 - in /cfe/trunk: lib/AST/MicrosoftMangle.cpp test/CodeGenCXX/mangle-ms-back-references-pr13207.cpp

Timur Iskhodzhanov timurrrr at google.com
Tue Jul 24 02:37:54 PDT 2012


Author: timurrrr
Date: Tue Jul 24 04:37:54 2012
New Revision: 160667

URL: http://llvm.org/viewvc/llvm-project?rev=160667&view=rev
Log:
Fix PR13207 (Mangling of templates with back references when using -cxx-abi microsoft)

Modified:
    cfe/trunk/lib/AST/MicrosoftMangle.cpp
    cfe/trunk/test/CodeGenCXX/mangle-ms-back-references-pr13207.cpp

Modified: cfe/trunk/lib/AST/MicrosoftMangle.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/MicrosoftMangle.cpp?rev=160667&r1=160666&r2=160667&view=diff
==============================================================================
--- cfe/trunk/lib/AST/MicrosoftMangle.cpp (original)
+++ cfe/trunk/lib/AST/MicrosoftMangle.cpp Tue Jul 24 04:37:54 2012
@@ -21,6 +21,8 @@
 #include "clang/AST/ExprCXX.h"
 #include "clang/Basic/ABI.h"
 
+#include <map>
+
 using namespace clang;
 
 namespace {
@@ -31,14 +33,18 @@
   MangleContext &Context;
   raw_ostream &Out;
 
-  typedef llvm::DenseMap<void*, unsigned> BackRefMap;
-  BackRefMap NameBackReferences, TypeBackReferences;
+  typedef std::map<std::string, unsigned> BackRefMap;
+  BackRefMap NameBackReferences;
+  bool UseNameBackReferences;
+
+  typedef llvm::DenseMap<void*, unsigned> ArgBackRefMap;
+  ArgBackRefMap TypeBackReferences;
 
   ASTContext &getASTContext() const { return Context.getASTContext(); }
 
 public:
   MicrosoftCXXNameMangler(MangleContext &C, raw_ostream &Out_)
-  : Context(C), Out(Out_) { }
+  : Context(C), Out(Out_), UseNameBackReferences(true) { }
 
   raw_ostream &getStream() const { return Out; }
 
@@ -51,6 +57,7 @@
   void mangleType(QualType T, SourceRange Range);
 
 private:
+  void disableBackReferences() { UseNameBackReferences = false; }
   void mangleUnqualifiedName(const NamedDecl *ND) {
     mangleUnqualifiedName(ND, ND->getDeclName());
   }
@@ -406,7 +413,42 @@
   SmallVector<TemplateArgumentLoc, 2> TemplateArgs;
   // Check if we have a template.
   if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
-    mangleTemplateInstantiationName(TD, TemplateArgs);
+    // We have a template.
+    // Here comes the tricky thing: if we need to mangle something like
+    //   void foo(A::X<Y>, B::X<Y>),
+    // the X<Y> part is aliased. However, if you need to mangle
+    //   void foo(A::X<A::Y>, A::X<B::Y>),
+    // the A::X<> part is not aliased.
+    // That said, from the mangler's perspective we have a structure like this:
+    //   namespace[s] -> type[ -> template-parameters]
+    // but from the Clang perspective we have
+    //   type [ -> template-parameters]
+    //      \-> namespace[s]
+    // What we do is we create a new mangler, mangle the same type (without
+    // a namespace suffix) using the extra mangler with back references
+    // disabled (to avoid infinite recursion) and then use the mangled type
+    // name as a key to check the mangling of different types for aliasing.
+
+    std::string BackReferenceKey;
+    BackRefMap::iterator Found;
+    if (UseNameBackReferences) {
+      llvm::raw_string_ostream Stream(BackReferenceKey);
+      MicrosoftCXXNameMangler Extra(Context, Stream);
+      Extra.disableBackReferences();
+      Extra.mangleUnqualifiedName(ND, Name);
+      Stream.flush();
+
+      Found = NameBackReferences.find(BackReferenceKey);
+    }
+    if (!UseNameBackReferences || Found == NameBackReferences.end()) {
+      mangleTemplateInstantiationName(TD, TemplateArgs);
+      if (UseNameBackReferences && NameBackReferences.size() < 10) {
+        size_t Size = NameBackReferences.size();
+        NameBackReferences[BackReferenceKey] = Size;
+      }
+    } else {
+      Out << Found->second;
+    }
     return;
   }
 
@@ -646,12 +688,15 @@
 
 void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
   // <source name> ::= <identifier> @
-  BackRefMap::iterator Found = NameBackReferences.find((void*)II);
-  if (Found == NameBackReferences.end()) {
+  std::string key = II->getNameStart();
+  BackRefMap::iterator Found;
+  if (UseNameBackReferences)
+    Found = NameBackReferences.find(key);
+  if (!UseNameBackReferences || Found == NameBackReferences.end()) {
     Out << II->getName() << '@';
-    if (NameBackReferences.size() < 10) {
+    if (UseNameBackReferences && NameBackReferences.size() < 10) {
       size_t Size = NameBackReferences.size();
-      NameBackReferences[(void*)II] = Size;
+      NameBackReferences[key] = Size;
     }
   } else {
     Out << Found->second;
@@ -713,7 +758,7 @@
 
   mangleUnscopedTemplateName(TD);
   mangleTemplateArgs(TemplateArgs);
-  
+
   NameBackReferences.swap(TemplateContext);
 }
 
@@ -863,7 +908,7 @@
 
 void MicrosoftCXXNameMangler::mangleTypeRepeated(QualType T, SourceRange Range) {
   void *TypePtr = getASTContext().getCanonicalType(T).getAsOpaquePtr();
-  BackRefMap::iterator Found = TypeBackReferences.find(TypePtr);
+  ArgBackRefMap::iterator Found = TypeBackReferences.find(TypePtr);
 
   if (Found == TypeBackReferences.end()) {
     size_t OutSizeBefore = Out.GetNumBytesInBuffer();

Modified: cfe/trunk/test/CodeGenCXX/mangle-ms-back-references-pr13207.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/mangle-ms-back-references-pr13207.cpp?rev=160667&r1=160666&r2=160667&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/mangle-ms-back-references-pr13207.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/mangle-ms-back-references-pr13207.cpp Tue Jul 24 04:37:54 2012
@@ -1,10 +1,61 @@
-// RUN: %clang_cc1 -fms-extensions -fblocks -emit-llvm %s -o - -cxx-abi microsoft -triple=i386-pc-win32 | FileCheck %s
+// RUN: %clang_cc1 -emit-llvm %s -o - -cxx-abi microsoft -triple=i386-pc-win32 | FileCheck %s
 
-// NOTE on the "CURRENT" prefix: some things are mangled incorrectly as of
-// writing. If you find a CURRENT-test that fails with your patch, please test
-// if your patch has actually fixed a problem in the mangler and replace the
-// corresponding CORRECT line with a CHECK.
-// RUN: %clang_cc1 -fms-extensions -fblocks -emit-llvm %s -o - -cxx-abi microsoft -triple=i386-pc-win32 | FileCheck -check-prefix CURRENT %s
+// FIXME: add tests for return types with complex templates when PR13389 is fixed.
+
+template<class X, class Y, class Z>
+class A {};
+template<class X>
+class B {};
+template<class X>
+class C {};
+
+void foo_abbb(A<B<char>, B<char>, B<char> >) {}
+// CHECK: "\01?foo_abbb@@YAXV?$A at V?$B at D@@V1 at V1@@@@Z"
+void foo_abb(A<char, B<char>, B<char> >) {}
+// CHECK: "\01?foo_abb@@YAXV?$A at DV?$B at D@@V1@@@@Z"
+void foo_abc(A<char, B<char>, C<char> >) {}
+// CHECK: "\01?foo_abc@@YAXV?$A at DV?$B at D@@V?$C at D@@@@@Z"
+
+namespace N {
+template<class X, class Y, class Z>
+class A {};
+template<class X>
+class B {};
+template<class X>
+class C {};
+template<class X, class Y>
+class D {};
+class Z {};
+}
+
+void foo_abbb(N::A<N::B<char>, N::B<char>, N::B<char> >) {}
+// CHECK: "\01?foo_abbb@@YAXV?$A at V?$B at D@N@@V12 at V12@@N@@@Z"
+void foo_abb(N::A<char, N::B<char>, N::B<char> >) {}
+// CHECK: "\01?foo_abb@@YAXV?$A at DV?$B at D@N@@V12@@N@@@Z"
+void foo_abc(N::A<char, N::B<char>, N::C<char> >) {}
+// CHECK: "\01?foo_abc@@YAXV?$A at DV?$B at D@N@@V?$C at D@2@@N@@@Z"
+
+namespace NA {
+class X {};
+template<class T> class Y {};
+}
+
+namespace NB {
+class X {};
+template<class T> class Y {};
+}
+
+void foo5(NA::Y<NB::Y<NA::Y<NB::Y<NA::X> > > > arg) {}
+// CHECK: "\01?foo5@@YAXV?$Y at V?$Y at V?$Y at V?$Y at VX@NA@@@NB@@@NA@@@NB@@@NA@@@Z"
+
+void foo11(NA::Y<NA::X>, NB::Y<NA::X>) {}
+// CHECK: "\01?foo11@@YAXV?$Y at VX@NA@@@NA@@V1NB@@@Z"
+
+void foo112(NA::Y<NA::X>, NB::Y<NB::X>) {}
+// CHECK: "\01?foo112@@YAXV?$Y at VX@NA@@@NA@@V?$Y at VX@NB@@@NB@@@Z"
+
+void foo22(NA::Y<NB::Y<NA::X> >, NB::Y<NA::Y<NA::X> >) {}
+// CHECK: "\01?foo22@@YAXV?$Y at V?$Y at VX@NA@@@NB@@@NA@@V?$Y at V?$Y at VX@NA@@@NA@@@NB@@@Z"
 
 namespace PR13207 {
 class A {};
@@ -37,23 +88,18 @@
 void spam(K<A,B,C> x) {}
 // CHECK: "\01?spam at PR13207@@YAXV?$K at VA@PR13207@@VB at 2@VC at 2@@1@@Z"
 
-// The following CURRENT line is here to improve the precision of the "scanning
-// from here" reports of FileCheck.
-// CURRENT: "\01?spam at PR13207@@YAXV?$K at VA@PR13207@@VB at 2@VC at 2@@1@@Z"
-
-// The tests below currently fail:
 void baz(K<char, F<char>, I<char> >) {}
-// CURRENT: "\01?baz at PR13207@@YAXV?$K at DV?$F at D@PR13207@@V?$I at D@1@@1@@Z"
-// CORRECT: "\01?baz at PR13207@@YAXV?$K at DV?$F at D@PR13207@@V?$I at D@2@@1@@Z"
+// CHECK: "\01?baz at PR13207@@YAXV?$K at DV?$F at D@PR13207@@V?$I at D@2@@1@@Z"
 void qux(K<char, I<char>, I<char> >) {}
-// CURRENT: "\01?qux at PR13207@@YAXV?$K at DV?$I at D@PR13207@@V?$I at D@1@@1@@Z"
-// CORRECT: "\01?qux at PR13207@@YAXV?$K at DV?$I at D@PR13207@@V12@@1@@Z
+// CHECK: "\01?qux at PR13207@@YAXV?$K at DV?$I at D@PR13207@@V12@@1@@Z"
 
 namespace NA {
 class X {};
 template<class T> class Y {};
 void foo(Y<X> x) {}
 // CHECK: "\01?foo at NA@PR13207@@YAXV?$Y at VX@NA at PR13207@@@12@@Z"
+void foofoo(Y<Y<X> > x) {}
+// CHECK: "\01?foofoo at NA@PR13207@@YAXV?$Y at V?$Y at VX@NA at PR13207@@@NA at PR13207@@@12@@Z"
 }
 
 namespace NB {
@@ -68,22 +114,17 @@
 void spam(NA::Y<NA::X> x) {}
 // CHECK: "\01?spam at NB@PR13207@@YAXV?$Y at VX@NA at PR13207@@@NA at 2@@Z"
 
-// The tests below currently fail:
 void foobar(NA::Y<Y<X> > a, Y<Y<X> >) {}
-// CURRENT: "\01?foobar at NB@PR13207@@YAXV?$Y at V?$Y at VX@NB at PR13207@@@NB at PR13207@@@NA at 2@V?$Y at V?$Y at VX@NB at PR13207@@@NB at PR13207@@@12@@Z"
-// CORRECT: "\01?foobar at NB@PR13207@@YAXV?$Y at V?$Y at VX@NB at PR13207@@@NB at PR13207@@@NA at 2@V312@@Z"
+// CHECK: "\01?foobar at NB@PR13207@@YAXV?$Y at V?$Y at VX@NB at PR13207@@@NB at PR13207@@@NA at 2@V312@@Z"
 
 void foobarspam(Y<X> a, NA::Y<Y<X> > b, Y<Y<X> >) {}
-// CURRENT: "\01?foobarspam at NB@PR13207@@YAXV?$Y at VX@NB at PR13207@@@12 at V?$Y at V?$Y at VX@NB at PR13207@@@NB at PR13207@@@NA at 2@V?$Y at V?$Y at VX@NB at PR13207@@@NB at PR13207@@@12@@Z"
-// CORRECT: "\01?foobarspam at NB@PR13207@@YAXV?$Y at VX@NB at PR13207@@@12 at V?$Y at V?$Y at VX@NB at PR13207@@@NB at PR13207@@@NA at 2@V412@@Z"
+// CHECK: "\01?foobarspam at NB@PR13207@@YAXV?$Y at VX@NB at PR13207@@@12 at V?$Y at V?$Y at VX@NB at PR13207@@@NB at PR13207@@@NA at 2@V412@@Z"
 
 void foobarbaz(Y<X> a, NA::Y<Y<X> > b, Y<Y<X> >, Y<Y<X> > c) {}
-// CURRENT: "\01?foobarbaz at NB@PR13207@@YAXV?$Y at VX@NB at PR13207@@@12 at V?$Y at V?$Y at VX@NB at PR13207@@@NB at PR13207@@@NA at 2@V?$Y at V?$Y at VX@NB at PR13207@@@NB at PR13207@@@12 at 2@Z"
-// CORRECT: "\01?foobarbaz at NB@PR13207@@YAXV?$Y at VX@NB at PR13207@@@12 at V?$Y at V?$Y at VX@NB at PR13207@@@NB at PR13207@@@NA at 2@V412 at 2@Z"
+// CHECK: "\01?foobarbaz at NB@PR13207@@YAXV?$Y at VX@NB at PR13207@@@12 at V?$Y at V?$Y at VX@NB at PR13207@@@NB at PR13207@@@NA at 2@V412 at 2@Z"
 
 void foobarbazqux(Y<X> a, NA::Y<Y<X> > b, Y<Y<X> >, Y<Y<X> > c , NA::Y<Y<Y<X> > > d) {}
-// CURRENT: "\01?foobarbazqux at NB@PR13207@@YAXV?$Y at VX@NB at PR13207@@@12 at V?$Y at V?$Y at VX@NB at PR13207@@@NB at PR13207@@@NA at 2@V?$Y at V?$Y at VX@NB at PR13207@@@NB at PR13207@@@12 at 2V?$Y at V?$Y at V?$Y at VX@NB at PR13207@@@NB at PR13207@@@NB at PR13207@@@32@@Z"
-// CORRECT: "\01?foobarbazqux at NB@PR13207@@YAXV?$Y at VX@NB at PR13207@@@12 at V?$Y at V?$Y at VX@NB at PR13207@@@NB at PR13207@@@NA at 2@V412 at 2V?$Y at V?$Y at V?$Y at VX@NB at PR13207@@@NB at PR13207@@@NB at PR13207@@@52@@Z"
+// CHECK: "\01?foobarbazqux at NB@PR13207@@YAXV?$Y at VX@NB at PR13207@@@12 at V?$Y at V?$Y at VX@NB at PR13207@@@NB at PR13207@@@NA at 2@V412 at 2V?$Y at V?$Y at V?$Y at VX@NB at PR13207@@@NB at PR13207@@@NB at PR13207@@@52@@Z"
 }
 
 namespace NC {





More information about the cfe-commits mailing list