diff options
author | Max Winkler <max.enrico.winkler@gmail.com> | 2024-07-04 10:17:32 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-04 10:17:32 -0700 |
commit | d1dc4169838381688a74f245cdaedbe9fce13848 (patch) | |
tree | d162c82bc14d163133be58dca6a20e006b796579 | |
parent | 4f77677c3b383da99e46fdb29e7b8de63eb72447 (diff) | |
download | llvm-d1dc4169838381688a74f245cdaedbe9fce13848.zip llvm-d1dc4169838381688a74f245cdaedbe9fce13848.tar.gz llvm-d1dc4169838381688a74f245cdaedbe9fce13848.tar.bz2 |
Fix MSVC 1920+ auto NTTP mangling for pointers to members (#97007)
Fixes https://github.com/llvm/llvm-project/issues/70899.
This is a continuation of
https://github.com/llvm/llvm-project/pull/92477 for pointers to member
data and pointers to member functions.
The mangled name must be prefixed with `$M <mangled-type>` for the
deduced type of the nttp parameter.
-rw-r--r-- | clang/docs/ReleaseNotes.rst | 7 | ||||
-rw-r--r-- | clang/lib/AST/MicrosoftMangle.cpp | 73 | ||||
-rw-r--r-- | clang/test/CodeGenCXX/mangle-ms-auto-templates-memptrs.cpp | 71 | ||||
-rw-r--r-- | clang/test/CodeGenCXX/mangle-ms-auto-templates-nullptr.cpp | 24 | ||||
-rw-r--r-- | llvm/lib/Demangle/MicrosoftDemangle.cpp | 7 | ||||
-rw-r--r-- | llvm/test/Demangle/ms-auto-templates.test | 42 |
6 files changed, 204 insertions, 20 deletions
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index f149684..36cf615a 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -106,6 +106,13 @@ ABI Changes in This Version earlier versions of Clang unless such code is built with the compiler option `-fms-compatibility-version=19.14` to imitate the MSVC 1914 mangling behavior. +- Fixed Microsoft name mangling for auto non-type template arguments of pointer + to member type for MSVC 1920+. This change resolves incompatibilities with code + compiled by MSVC 1920+ but will introduce incompatibilities with code compiled by + earlier versions of Clang unless such code is built with the compiler option + `-fms-compatibility-version=19.14` to imitate the MSVC 1914 mangling behavior. + (GH#70899). + AST Dumping Potentially Breaking Changes ---------------------------------------- diff --git a/clang/lib/AST/MicrosoftMangle.cpp b/clang/lib/AST/MicrosoftMangle.cpp index 7f1e9ab..fac14ce 100644 --- a/clang/lib/AST/MicrosoftMangle.cpp +++ b/clang/lib/AST/MicrosoftMangle.cpp @@ -368,11 +368,15 @@ public: void mangleFunctionEncoding(GlobalDecl GD, bool ShouldMangle); void mangleVariableEncoding(const VarDecl *VD); void mangleMemberDataPointer(const CXXRecordDecl *RD, const ValueDecl *VD, + const NonTypeTemplateParmDecl *PD, + QualType TemplateArgType, StringRef Prefix = "$"); void mangleMemberDataPointerInClassNTTP(const CXXRecordDecl *, const ValueDecl *); void mangleMemberFunctionPointer(const CXXRecordDecl *RD, const CXXMethodDecl *MD, + const NonTypeTemplateParmDecl *PD, + QualType TemplateArgType, StringRef Prefix = "$"); void mangleFunctionPointer(const FunctionDecl *FD, const NonTypeTemplateParmDecl *PD, @@ -673,12 +677,17 @@ void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) { } } -void MicrosoftCXXNameMangler::mangleMemberDataPointer(const CXXRecordDecl *RD, - const ValueDecl *VD, - StringRef Prefix) { +void MicrosoftCXXNameMangler::mangleMemberDataPointer( + const CXXRecordDecl *RD, const ValueDecl *VD, + const NonTypeTemplateParmDecl *PD, QualType TemplateArgType, + StringRef Prefix) { // <member-data-pointer> ::= <integer-literal> // ::= $F <number> <number> // ::= $G <number> <number> <number> + // + // <auto-nttp> ::= $ M <type> <integer-literal> + // <auto-nttp> ::= $ M <type> F <name> <number> + // <auto-nttp> ::= $ M <type> G <name> <number> <number> int64_t FieldOffset; int64_t VBTableOffset; @@ -707,7 +716,18 @@ void MicrosoftCXXNameMangler::mangleMemberDataPointer(const CXXRecordDecl *RD, case MSInheritanceModel::Unspecified: Code = 'G'; break; } - Out << Prefix << Code; + Out << Prefix; + + if (VD && + getASTContext().getLangOpts().isCompatibleWithMSVC( + LangOptions::MSVC2019) && + PD && PD->getType()->getTypeClass() == Type::Auto && + !TemplateArgType.isNull()) { + Out << "M"; + mangleType(TemplateArgType, SourceRange(), QMM_Drop); + } + + Out << Code; mangleNumber(FieldOffset); @@ -728,7 +748,7 @@ void MicrosoftCXXNameMangler::mangleMemberDataPointerInClassNTTP( // ::= 8 <postfix> @ <unqualified-name> @ if (IM != MSInheritanceModel::Single && IM != MSInheritanceModel::Multiple) - return mangleMemberDataPointer(RD, VD, ""); + return mangleMemberDataPointer(RD, VD, nullptr, QualType(), ""); if (!VD) { Out << 'N'; @@ -742,14 +762,19 @@ void MicrosoftCXXNameMangler::mangleMemberDataPointerInClassNTTP( Out << '@'; } -void -MicrosoftCXXNameMangler::mangleMemberFunctionPointer(const CXXRecordDecl *RD, - const CXXMethodDecl *MD, - StringRef Prefix) { +void MicrosoftCXXNameMangler::mangleMemberFunctionPointer( + const CXXRecordDecl *RD, const CXXMethodDecl *MD, + const NonTypeTemplateParmDecl *PD, QualType TemplateArgType, + StringRef Prefix) { // <member-function-pointer> ::= $1? <name> // ::= $H? <name> <number> // ::= $I? <name> <number> <number> // ::= $J? <name> <number> <number> <number> + // + // <auto-nttp> ::= $ M <type> 1? <name> + // <auto-nttp> ::= $ M <type> H? <name> <number> + // <auto-nttp> ::= $ M <type> I? <name> <number> <number> + // <auto-nttp> ::= $ M <type> J? <name> <number> <number> <number> MSInheritanceModel IM = RD->getMSInheritanceModel(); @@ -767,7 +792,17 @@ MicrosoftCXXNameMangler::mangleMemberFunctionPointer(const CXXRecordDecl *RD, uint64_t VBTableOffset = 0; uint64_t VBPtrOffset = 0; if (MD) { - Out << Prefix << Code << '?'; + Out << Prefix; + + if (getASTContext().getLangOpts().isCompatibleWithMSVC( + LangOptions::MSVC2019) && + PD && PD->getType()->getTypeClass() == Type::Auto && + !TemplateArgType.isNull()) { + Out << "M"; + mangleType(TemplateArgType, SourceRange(), QMM_Drop); + } + + Out << Code << '?'; if (MD->isVirtual()) { MicrosoftVTableContext *VTContext = cast<MicrosoftVTableContext>(getASTContext().getVTableContext()); @@ -859,7 +894,7 @@ void MicrosoftCXXNameMangler::mangleMemberFunctionPointerInClassNTTP( if (!MD) { if (RD->getMSInheritanceModel() != MSInheritanceModel::Single) - return mangleMemberFunctionPointer(RD, MD, ""); + return mangleMemberFunctionPointer(RD, MD, nullptr, QualType(), ""); Out << 'N'; return; @@ -1732,12 +1767,15 @@ void MicrosoftCXXNameMangler::mangleTemplateArg(const TemplateDecl *TD, if (isa<FieldDecl>(ND) || isa<IndirectFieldDecl>(ND)) { mangleMemberDataPointer(cast<CXXRecordDecl>(ND->getDeclContext()) ->getMostRecentNonInjectedDecl(), - cast<ValueDecl>(ND)); + cast<ValueDecl>(ND), + cast<NonTypeTemplateParmDecl>(Parm), + TA.getParamTypeForDecl()); } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); if (MD && MD->isInstance()) { mangleMemberFunctionPointer( - MD->getParent()->getMostRecentNonInjectedDecl(), MD); + MD->getParent()->getMostRecentNonInjectedDecl(), MD, + cast<NonTypeTemplateParmDecl>(Parm), TA.getParamTypeForDecl()); } else { mangleFunctionPointer(FD, cast<NonTypeTemplateParmDecl>(Parm), TA.getParamTypeForDecl()); @@ -1767,12 +1805,12 @@ void MicrosoftCXXNameMangler::mangleTemplateArg(const TemplateDecl *TD, const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); if (MPT->isMemberFunctionPointerType() && !isa<FunctionTemplateDecl>(TD)) { - mangleMemberFunctionPointer(RD, nullptr); + mangleMemberFunctionPointer(RD, nullptr, nullptr, QualType()); return; } if (MPT->isMemberDataPointer()) { if (!isa<FunctionTemplateDecl>(TD)) { - mangleMemberDataPointer(RD, nullptr); + mangleMemberDataPointer(RD, nullptr, nullptr, QualType()); return; } // nullptr data pointers are always represented with a single field @@ -1979,9 +2017,10 @@ void MicrosoftCXXNameMangler::mangleTemplateArgValue(QualType T, cast_or_null<CXXMethodDecl>(D)); } else { if (T->isMemberDataPointerType()) - mangleMemberDataPointer(RD, D, ""); + mangleMemberDataPointer(RD, D, nullptr, QualType(), ""); else - mangleMemberFunctionPointer(RD, cast_or_null<CXXMethodDecl>(D), ""); + mangleMemberFunctionPointer(RD, cast_or_null<CXXMethodDecl>(D), nullptr, + QualType(), ""); } return; } diff --git a/clang/test/CodeGenCXX/mangle-ms-auto-templates-memptrs.cpp b/clang/test/CodeGenCXX/mangle-ms-auto-templates-memptrs.cpp new file mode 100644 index 0000000..360ebde --- /dev/null +++ b/clang/test/CodeGenCXX/mangle-ms-auto-templates-memptrs.cpp @@ -0,0 +1,71 @@ +// RUN: %clang_cc1 -std=c++17 -fms-compatibility-version=19.20 -emit-llvm %s -o - -fms-extensions -fdelayed-template-parsing -triple=x86_64-pc-windows-msvc | FileCheck --check-prefix=AFTER %s +// RUN: %clang_cc1 -std=c++17 -fms-compatibility-version=19.14 -emit-llvm %s -o - -fms-extensions -fdelayed-template-parsing -triple=x86_64-pc-windows-msvc | FileCheck --check-prefix=BEFORE %s + +template <auto a> +class AutoParmTemplate { +public: + AutoParmTemplate() {} +}; + +template <auto a> +auto AutoFunc() { + return a; +} + +struct A {}; +struct B {}; + +struct S { int a; void f(); virtual void g(); }; +struct M : A, B { int a; void f(); virtual void g(); }; +struct V : virtual A { int a; void f(); virtual void g(); }; + +void template_mangling() { + + AutoParmTemplate<&S::f> auto_method_single_inheritance; + // AFTER: call {{.*}} @"??0?$AutoParmTemplate@$MP8S@@EAAXXZ1?f@1@QEAAXXZ@@QEAA@XZ" + // BEFORE: call {{.*}} @"??0?$AutoParmTemplate@$1?f@S@@QEAAXXZ@@QEAA@XZ" + + AutoParmTemplate<&M::f> auto_method_multiple_inheritance; + // AFTER: call {{.*}} @"??0?$AutoParmTemplate@$MP8M@@EAAXXZH?f@1@QEAAXXZA@@@QEAA@XZ" + // BEFORE: call {{.*}} @"??0?$AutoParmTemplate@$H?f@M@@QEAAXXZA@@@QEAA@XZ" + + AutoParmTemplate<&V::f> auto_method_virtual_inheritance; + // AFTER: call {{.*}} @"??0?$AutoParmTemplate@$MP8V@@EAAXXZI?f@1@QEAAXXZA@A@@@QEAA@XZ" + // BEFORE: call {{.*}} @"??0?$AutoParmTemplate@$I?f@V@@QEAAXXZA@A@@@QEAA@XZ" + + AutoFunc<&S::f>(); + // AFTER: call {{.*}} @"??$AutoFunc@$MP8S@@EAAXXZ1?f@1@QEAAXXZ@@YA?A?<auto>@@XZ" + // BEFORE: call {{.*}} @"??$AutoFunc@$1?f@S@@QEAAXXZ@@YA?A?<auto>@@XZ" + + AutoFunc<&M::f>(); + // AFTER: call {{.*}} @"??$AutoFunc@$MP8M@@EAAXXZH?f@1@QEAAXXZA@@@YA?A?<auto>@@XZ" + // BEFORE: call {{.*}} @"??$AutoFunc@$H?f@M@@QEAAXXZA@@@YA?A?<auto>@@XZ" + + AutoFunc<&V::f>(); + // AFTER: call {{.*}} @"??$AutoFunc@$MP8V@@EAAXXZI?f@1@QEAAXXZA@A@@@YA?A?<auto>@@XZ" + // BEFORE: call {{.*}} @"??$AutoFunc@$I?f@V@@QEAAXXZA@A@@@YA?A?<auto>@@XZ" + + AutoParmTemplate<&S::a> auto_data_single_inheritance; + // AFTER: call {{.*}} @"??0?$AutoParmTemplate@$MPEQS@@H07@@QEAA@XZ" + // BEFORE: call {{.*}} @"??0?$AutoParmTemplate@$07@@QEAA@XZ" + + AutoParmTemplate<&M::a> auto_data_multiple_inheritance; + // AFTER: call {{.*}} @"??0?$AutoParmTemplate@$MPEQM@@H0M@@@QEAA@XZ" + // BEFORE: call {{.*}} @"??0?$AutoParmTemplate@$0M@@@QEAA@XZ" + + AutoParmTemplate<&V::a> auto_data_virtual_inheritance; + // AFTER: call {{.*}} @"??0?$AutoParmTemplate@$MPEQV@@HFBA@A@@@QEAA@XZ" + // BEFORE: call {{.*}} @"??0?$AutoParmTemplate@$FBA@A@@@QEAA@XZ" + + AutoFunc<&S::a>(); + // AFTER: call {{.*}} @"??$AutoFunc@$MPEQS@@H07@@YA?A?<auto>@@XZ" + // BEFORE: call {{.*}} @"??$AutoFunc@$07@@YA?A?<auto>@@XZ" + + AutoFunc<&M::a>(); + // AFTER: call {{.*}} @"??$AutoFunc@$MPEQM@@H0M@@@YA?A?<auto>@@XZ" + // BEFORE: call {{.*}} @"??$AutoFunc@$0M@@@YA?A?<auto>@@XZ" + + AutoFunc<&V::a>(); + // AFTER: call {{.*}} @"??$AutoFunc@$MPEQV@@HFBA@A@@@YA?A?<auto>@@XZ" + // BEFORE: call {{.*}} @"??$AutoFunc@$FBA@A@@@YA?A?<auto>@@XZ" +} diff --git a/clang/test/CodeGenCXX/mangle-ms-auto-templates-nullptr.cpp b/clang/test/CodeGenCXX/mangle-ms-auto-templates-nullptr.cpp new file mode 100644 index 0000000..8f98c1e --- /dev/null +++ b/clang/test/CodeGenCXX/mangle-ms-auto-templates-nullptr.cpp @@ -0,0 +1,24 @@ +// RUN: %clang_cc1 -std=c++17 -fms-compatibility-version=19.20 -emit-llvm %s -o - -fms-extensions -fdelayed-template-parsing -triple=x86_64-pc-windows-msvc | FileCheck --check-prefix=AFTER %s +// RUN: %clang_cc1 -std=c++17 -fms-compatibility-version=19.14 -emit-llvm %s -o - -fms-extensions -fdelayed-template-parsing -triple=x86_64-pc-windows-msvc | FileCheck --check-prefix=BEFORE %s + +template <auto a> +class AutoParmTemplate { +public: + AutoParmTemplate() {} +}; + +template <auto a> +auto AutoFunc() { + return a; +} + +void template_mangling() { + + AutoParmTemplate<nullptr> auto_nullptr; + // AFTER: call {{.*}} @"??0?$AutoParmTemplate@$M$$T0A@@@QEAA@XZ" + // BEFORE: call {{.*}} @"??0?$AutoParmTemplate@$0A@@@QEAA@XZ" + + AutoFunc<nullptr>(); + // AFTER: call {{.*}} @"??$AutoFunc@$M$$T0A@@@YA?A?<auto>@@XZ" + // BEFORE: call {{.*}} @"??$AutoFunc@$0A@@@YA?A?<auto>@@XZ" +} diff --git a/llvm/lib/Demangle/MicrosoftDemangle.cpp b/llvm/lib/Demangle/MicrosoftDemangle.cpp index e18de9a..c5835e8 100644 --- a/llvm/lib/Demangle/MicrosoftDemangle.cpp +++ b/llvm/lib/Demangle/MicrosoftDemangle.cpp @@ -2343,12 +2343,13 @@ Demangler::demangleTemplateParameterList(std::string_view &MangledName) { TP.N = TPRN = Arena.alloc<TemplateParameterReferenceNode>(); TPRN->Symbol = parse(MangledName); TPRN->Affinity = PointerAffinity::Reference; - } else if (llvm::itanium_demangle::starts_with(MangledName, "$F") || - llvm::itanium_demangle::starts_with(MangledName, "$G")) { + } else if (startsWith(MangledName, "$F", "F", !IsAutoNTTP) || + startsWith(MangledName, "$G", "G", !IsAutoNTTP)) { TP.N = TPRN = Arena.alloc<TemplateParameterReferenceNode>(); // Data member pointer. - MangledName.remove_prefix(1); + if (!IsAutoNTTP) + MangledName.remove_prefix(1); // Remove leading '$' char InheritanceSpecifier = MangledName.front(); MangledName.remove_prefix(1); diff --git a/llvm/test/Demangle/ms-auto-templates.test b/llvm/test/Demangle/ms-auto-templates.test index a90ffb6..414885d 100644 --- a/llvm/test/Demangle/ms-auto-templates.test +++ b/llvm/test/Demangle/ms-auto-templates.test @@ -55,3 +55,45 @@ ??0?$AutoNTTPClass@$MH0A@$M_N0A@$MD0GB@@@QEAA@XZ ; CHECK: public: __cdecl AutoNTTPClass<0, 0, 97>::AutoNTTPClass<0, 0, 97>(void) + +??0?$AutoNTTPClass@$M$$T0A@@@QEAA@XZ +; CHECK: public: __cdecl AutoNTTPClass<0>::AutoNTTPClass<0>(void) + +??0?$AutoNTTPClass@$0A@@@QEAA@XZ +; CHECK: public: __cdecl AutoNTTPClass<0>::AutoNTTPClass<0>(void) + +??0?$AutoNTTPClass@$MP8S@@EAAXXZ1?f@1@QEAAXXZ@@QEAA@XZ +; CHECK: public: __cdecl AutoNTTPClass<&public: void __cdecl S::f(void)>::AutoNTTPClass<&public: void __cdecl S::f(void)>(void) + +??0?$AutoNTTPClass@$1?f@S@@QEAAXXZ@@QEAA@XZ +; CHECK: public: __cdecl AutoNTTPClass<&public: void __cdecl S::f(void)>::AutoNTTPClass<&public: void __cdecl S::f(void)>(void) + +??0?$AutoNTTPClass@$MP8M@@EAAXXZH?f@1@QEAAXXZA@@@QEAA@XZ +; CHECK: public: __cdecl AutoNTTPClass<{public: void __cdecl M::f(void), 0}>::AutoNTTPClass<{public: void __cdecl M::f(void), 0}>(void) + +??0?$AutoNTTPClass@$H?f@M@@QEAAXXZA@@@QEAA@XZ +; CHECK: public: __cdecl AutoNTTPClass<{public: void __cdecl M::f(void), 0}>::AutoNTTPClass<{public: void __cdecl M::f(void), 0}>(void) + +??0?$AutoNTTPClass@$MP8V@@EAAXXZI?f@1@QEAAXXZA@A@@@QEAA@XZ +; CHECK: public: __cdecl AutoNTTPClass<{public: void __cdecl V::f(void), 0, 0}>::AutoNTTPClass<{public: void __cdecl V::f(void), 0, 0}>(void) + +??0?$AutoNTTPClass@$I?f@V@@QEAAXXZA@A@@@QEAA@XZ +; CHECK: public: __cdecl AutoNTTPClass<{public: void __cdecl V::f(void), 0, 0}>::AutoNTTPClass<{public: void __cdecl V::f(void), 0, 0}>(void) + +??0?$AutoNTTPClass@$MPEQS@@H07@@QEAA@XZ +; CHECK: public: __cdecl AutoNTTPClass<8>::AutoNTTPClass<8>(void) + +??0?$AutoNTTPClass@$07@@QEAA@XZ +; CHECK: public: __cdecl AutoNTTPClass<8>::AutoNTTPClass<8>(void) + +??0?$AutoNTTPClass@$MPEQM@@H0M@@@QEAA@XZ +; CHECK: public: __cdecl AutoNTTPClass<12>::AutoNTTPClass<12>(void) + +??0?$AutoNTTPClass@$0M@@@QEAA@XZ +; CHECK: public: __cdecl AutoNTTPClass<12>::AutoNTTPClass<12>(void) + +??0?$AutoNTTPClass@$MPEQV@@HFBA@A@@@QEAA@XZ +; CHECK: public: __cdecl AutoNTTPClass<{16, 0}>::AutoNTTPClass<{16, 0}>(void) + +??0?$AutoNTTPClass@$FBA@A@@@QEAA@XZ +; CHECK: public: __cdecl AutoNTTPClass<{16, 0}>::AutoNTTPClass<{16, 0}>(void) |