diff options
author | Timur Iskhodzhanov <timurrrr@google.com> | 2012-07-24 09:37:54 +0000 |
---|---|---|
committer | Timur Iskhodzhanov <timurrrr@google.com> | 2012-07-24 09:37:54 +0000 |
commit | f5e0b225b4d8027edab993ad4ac87510fcd6f991 (patch) | |
tree | 9041050804dd56d058925ddec4b14e638ea29f52 | |
parent | a5a4cba88796cb1b8365ed523e8b6fdce9e512bd (diff) |
Fix PR13207 (Mangling of templates with back references when using -cxx-abi microsoft)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@160667 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/AST/MicrosoftMangle.cpp | 65 | ||||
-rw-r--r-- | test/CodeGenCXX/mangle-ms-back-references-pr13207.cpp | 89 |
2 files changed, 120 insertions, 34 deletions
diff --git a/lib/AST/MicrosoftMangle.cpp b/lib/AST/MicrosoftMangle.cpp index d9fa057971..f9245b8c32 100644 --- a/lib/AST/MicrosoftMangle.cpp +++ b/lib/AST/MicrosoftMangle.cpp @@ -21,6 +21,8 @@ #include "clang/AST/ExprCXX.h" #include "clang/Basic/ABI.h" +#include <map> + using namespace clang; namespace { @@ -31,14 +33,18 @@ class MicrosoftCXXNameMangler { 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 @@ public: void mangleType(QualType T, SourceRange Range); private: + void disableBackReferences() { UseNameBackReferences = false; } void mangleUnqualifiedName(const NamedDecl *ND) { mangleUnqualifiedName(ND, ND->getDeclName()); } @@ -406,7 +413,42 @@ MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND, 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::mangleOperatorName(OverloadedOperatorKind OO, 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 @@ void MicrosoftCXXNameMangler::mangleTemplateInstantiationName( mangleUnscopedTemplateName(TD); mangleTemplateArgs(TemplateArgs); - + NameBackReferences.swap(TemplateContext); } @@ -863,7 +908,7 @@ void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals, 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(); diff --git a/test/CodeGenCXX/mangle-ms-back-references-pr13207.cpp b/test/CodeGenCXX/mangle-ms-back-references-pr13207.cpp index b5e1a0dc2d..9234e1c2b8 100644 --- a/test/CodeGenCXX/mangle-ms-back-references-pr13207.cpp +++ b/test/CodeGenCXX/mangle-ms-back-references-pr13207.cpp @@ -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@V?$B@D@@V1@V1@@@@Z" +void foo_abb(A<char, B<char>, B<char> >) {} +// CHECK: "\01?foo_abb@@YAXV?$A@DV?$B@D@@V1@@@@Z" +void foo_abc(A<char, B<char>, C<char> >) {} +// CHECK: "\01?foo_abc@@YAXV?$A@DV?$B@D@@V?$C@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@V?$B@D@N@@V12@V12@@N@@@Z" +void foo_abb(N::A<char, N::B<char>, N::B<char> >) {} +// CHECK: "\01?foo_abb@@YAXV?$A@DV?$B@D@N@@V12@@N@@@Z" +void foo_abc(N::A<char, N::B<char>, N::C<char> >) {} +// CHECK: "\01?foo_abc@@YAXV?$A@DV?$B@D@N@@V?$C@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@V?$Y@V?$Y@V?$Y@VX@NA@@@NB@@@NA@@@NB@@@NA@@@Z" + +void foo11(NA::Y<NA::X>, NB::Y<NA::X>) {} +// CHECK: "\01?foo11@@YAXV?$Y@VX@NA@@@NA@@V1NB@@@Z" + +void foo112(NA::Y<NA::X>, NB::Y<NB::X>) {} +// CHECK: "\01?foo112@@YAXV?$Y@VX@NA@@@NA@@V?$Y@VX@NB@@@NB@@@Z" + +void foo22(NA::Y<NB::Y<NA::X> >, NB::Y<NA::Y<NA::X> >) {} +// CHECK: "\01?foo22@@YAXV?$Y@V?$Y@VX@NA@@@NB@@@NA@@V?$Y@V?$Y@VX@NA@@@NA@@@NB@@@Z" namespace PR13207 { class A {}; @@ -37,23 +88,18 @@ void bar(J<A,B> x) {} void spam(K<A,B,C> x) {} // CHECK: "\01?spam@PR13207@@YAXV?$K@VA@PR13207@@VB@2@VC@2@@1@@Z" -// The following CURRENT line is here to improve the precision of the "scanning -// from here" reports of FileCheck. -// CURRENT: "\01?spam@PR13207@@YAXV?$K@VA@PR13207@@VB@2@VC@2@@1@@Z" - -// The tests below currently fail: void baz(K<char, F<char>, I<char> >) {} -// CURRENT: "\01?baz@PR13207@@YAXV?$K@DV?$F@D@PR13207@@V?$I@D@1@@1@@Z" -// CORRECT: "\01?baz@PR13207@@YAXV?$K@DV?$F@D@PR13207@@V?$I@D@2@@1@@Z" +// CHECK: "\01?baz@PR13207@@YAXV?$K@DV?$F@D@PR13207@@V?$I@D@2@@1@@Z" void qux(K<char, I<char>, I<char> >) {} -// CURRENT: "\01?qux@PR13207@@YAXV?$K@DV?$I@D@PR13207@@V?$I@D@1@@1@@Z" -// CORRECT: "\01?qux@PR13207@@YAXV?$K@DV?$I@D@PR13207@@V12@@1@@Z +// CHECK: "\01?qux@PR13207@@YAXV?$K@DV?$I@D@PR13207@@V12@@1@@Z" namespace NA { class X {}; template<class T> class Y {}; void foo(Y<X> x) {} // CHECK: "\01?foo@NA@PR13207@@YAXV?$Y@VX@NA@PR13207@@@12@@Z" +void foofoo(Y<Y<X> > x) {} +// CHECK: "\01?foofoo@NA@PR13207@@YAXV?$Y@V?$Y@VX@NA@PR13207@@@NA@PR13207@@@12@@Z" } namespace NB { @@ -68,22 +114,17 @@ void bar(NA::Y<X> x) {} void spam(NA::Y<NA::X> x) {} // CHECK: "\01?spam@NB@PR13207@@YAXV?$Y@VX@NA@PR13207@@@NA@2@@Z" -// The tests below currently fail: void foobar(NA::Y<Y<X> > a, Y<Y<X> >) {} -// CURRENT: "\01?foobar@NB@PR13207@@YAXV?$Y@V?$Y@VX@NB@PR13207@@@NB@PR13207@@@NA@2@V?$Y@V?$Y@VX@NB@PR13207@@@NB@PR13207@@@12@@Z" -// CORRECT: "\01?foobar@NB@PR13207@@YAXV?$Y@V?$Y@VX@NB@PR13207@@@NB@PR13207@@@NA@2@V312@@Z" +// CHECK: "\01?foobar@NB@PR13207@@YAXV?$Y@V?$Y@VX@NB@PR13207@@@NB@PR13207@@@NA@2@V312@@Z" void foobarspam(Y<X> a, NA::Y<Y<X> > b, Y<Y<X> >) {} -// CURRENT: "\01?foobarspam@NB@PR13207@@YAXV?$Y@VX@NB@PR13207@@@12@V?$Y@V?$Y@VX@NB@PR13207@@@NB@PR13207@@@NA@2@V?$Y@V?$Y@VX@NB@PR13207@@@NB@PR13207@@@12@@Z" -// CORRECT: "\01?foobarspam@NB@PR13207@@YAXV?$Y@VX@NB@PR13207@@@12@V?$Y@V?$Y@VX@NB@PR13207@@@NB@PR13207@@@NA@2@V412@@Z" +// CHECK: "\01?foobarspam@NB@PR13207@@YAXV?$Y@VX@NB@PR13207@@@12@V?$Y@V?$Y@VX@NB@PR13207@@@NB@PR13207@@@NA@2@V412@@Z" void foobarbaz(Y<X> a, NA::Y<Y<X> > b, Y<Y<X> >, Y<Y<X> > c) {} -// CURRENT: "\01?foobarbaz@NB@PR13207@@YAXV?$Y@VX@NB@PR13207@@@12@V?$Y@V?$Y@VX@NB@PR13207@@@NB@PR13207@@@NA@2@V?$Y@V?$Y@VX@NB@PR13207@@@NB@PR13207@@@12@2@Z" -// CORRECT: "\01?foobarbaz@NB@PR13207@@YAXV?$Y@VX@NB@PR13207@@@12@V?$Y@V?$Y@VX@NB@PR13207@@@NB@PR13207@@@NA@2@V412@2@Z" +// CHECK: "\01?foobarbaz@NB@PR13207@@YAXV?$Y@VX@NB@PR13207@@@12@V?$Y@V?$Y@VX@NB@PR13207@@@NB@PR13207@@@NA@2@V412@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@NB@PR13207@@YAXV?$Y@VX@NB@PR13207@@@12@V?$Y@V?$Y@VX@NB@PR13207@@@NB@PR13207@@@NA@2@V?$Y@V?$Y@VX@NB@PR13207@@@NB@PR13207@@@12@2V?$Y@V?$Y@V?$Y@VX@NB@PR13207@@@NB@PR13207@@@NB@PR13207@@@32@@Z" -// CORRECT: "\01?foobarbazqux@NB@PR13207@@YAXV?$Y@VX@NB@PR13207@@@12@V?$Y@V?$Y@VX@NB@PR13207@@@NB@PR13207@@@NA@2@V412@2V?$Y@V?$Y@V?$Y@VX@NB@PR13207@@@NB@PR13207@@@NB@PR13207@@@52@@Z" +// CHECK: "\01?foobarbazqux@NB@PR13207@@YAXV?$Y@VX@NB@PR13207@@@12@V?$Y@V?$Y@VX@NB@PR13207@@@NB@PR13207@@@NA@2@V412@2V?$Y@V?$Y@V?$Y@VX@NB@PR13207@@@NB@PR13207@@@NB@PR13207@@@52@@Z" } namespace NC { |