[llvm] [MsDemangle] Read entire chain of target names in special tables (PR #155630)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 27 07:28:57 PDT 2025
https://github.com/Nerixyz created https://github.com/llvm/llvm-project/pull/155630
When there's a deep inheritance hierarchy of multiple C++ classes (see below), then the mangled name of a VFTable can include multiple key nodes in the target name.
For example, in the following code, MSVC will generate mangled names for the VFTables that have up to three key classes in the context.
<details><summary>Code</summary>
```cpp
class Base1 {
virtual void a() {};
};
class Base2 {
virtual void b() {}
};
class Ind1 : public Base1 {};
class Ind2 : public Base1 {};
class A : public Ind1, public Ind2 {};
class Ind3 : public A {};
class Ind4 : public A {};
class B : public Ind3, public Ind4 {};
class Ind5 : public B {};
class Ind6 : public B {};
class C : public Ind5, public Ind6 {};
int main() { auto i = new C; }
```
</details>
This will include `??_7C@@6BInd1@@Ind4@@Ind5@@@` (and every other combination). Microsoft's undname will demangle this to "const C::\`vftable'{for \`Ind1's \`Ind4's \`Ind5'}". Previously, LLVM would demangle this to "const C::\`vftable'{for \`Ind1'}".
With this PR, the output of LLVM's undname will be identical to Microsoft's version. This changes `SpecialTableSymbolNode::TargetName` to a node array which contains each key from the name. Unlike namespaces, these keys are not in reverse order - they are in the same order as in the mangled name.
>From 99582cddc79f98bf499a87b4c8683e57438e6a90 Mon Sep 17 00:00:00 2001
From: Nerixyz <nerixdev at outlook.de>
Date: Wed, 27 Aug 2025 15:58:02 +0200
Subject: [PATCH] [MsDemangle] Read entire chain of target names in special
table
---
.../llvm/Demangle/MicrosoftDemangleNodes.h | 2 +-
llvm/lib/Demangle/MicrosoftDemangle.cpp | 50 +++++++++++++------
llvm/lib/Demangle/MicrosoftDemangleNodes.cpp | 4 +-
llvm/test/Demangle/ms-operators.test | 15 ++++++
4 files changed, 54 insertions(+), 17 deletions(-)
diff --git a/llvm/include/llvm/Demangle/MicrosoftDemangleNodes.h b/llvm/include/llvm/Demangle/MicrosoftDemangleNodes.h
index 155cfe8dd3a98..711aa70a4a8d3 100644
--- a/llvm/include/llvm/Demangle/MicrosoftDemangleNodes.h
+++ b/llvm/include/llvm/Demangle/MicrosoftDemangleNodes.h
@@ -708,7 +708,7 @@ struct DEMANGLE_ABI SpecialTableSymbolNode : public SymbolNode {
return N->kind() == NodeKind::SpecialTableSymbol;
}
- QualifiedNameNode *TargetName = nullptr;
+ NodeArrayNode *TargetNames = nullptr;
Qualifiers Quals = Qualifiers::Q_None;
};
diff --git a/llvm/lib/Demangle/MicrosoftDemangle.cpp b/llvm/lib/Demangle/MicrosoftDemangle.cpp
index b22928be3be50..0aefe6e077c24 100644
--- a/llvm/lib/Demangle/MicrosoftDemangle.cpp
+++ b/llvm/lib/Demangle/MicrosoftDemangle.cpp
@@ -277,6 +277,18 @@ demanglePointerCVQualifiers(std::string_view &MangledName) {
DEMANGLE_UNREACHABLE;
}
+static NodeArrayNode *nodeListToNodeArray(ArenaAllocator &Arena, NodeList *Head,
+ size_t Count) {
+ NodeArrayNode *N = Arena.alloc<NodeArrayNode>();
+ N->Count = Count;
+ N->Nodes = Arena.allocArray<Node *>(Count);
+ for (size_t I = 0; I < Count; ++I) {
+ N->Nodes[I] = Head->N;
+ Head = Head->Next;
+ }
+ return N;
+}
+
std::string_view Demangler::copyString(std::string_view Borrowed) {
char *Stable = Arena.allocUnalignedBuffer(Borrowed.size());
// This is not a micro-optimization, it avoids UB, should Borrowed be an null
@@ -323,8 +335,30 @@ Demangler::demangleSpecialTableSymbolNode(std::string_view &MangledName,
}
std::tie(STSN->Quals, IsMember) = demangleQualifiers(MangledName);
- if (!consumeFront(MangledName, '@'))
- STSN->TargetName = demangleFullyQualifiedTypeName(MangledName);
+
+ NodeList *TargetCurrent = nullptr;
+ NodeList *TargetHead = nullptr;
+ size_t Count = 0;
+ while (!consumeFront(MangledName, '@')) {
+ ++Count;
+
+ NodeList *Next = Arena.alloc<NodeList>();
+ if (TargetCurrent)
+ TargetCurrent->Next = Next;
+ else
+ TargetHead = Next;
+
+ TargetCurrent = Next;
+ QualifiedNameNode *QN = demangleFullyQualifiedTypeName(MangledName);
+ if (Error)
+ return nullptr;
+ assert(QN);
+ TargetCurrent->N = QN;
+ }
+
+ if (Count > 0)
+ STSN->TargetNames = nodeListToNodeArray(Arena, TargetHead, Count);
+
return STSN;
}
@@ -1605,18 +1639,6 @@ Demangler::demangleNameScopePiece(std::string_view &MangledName) {
return demangleSimpleName(MangledName, /*Memorize=*/true);
}
-static NodeArrayNode *nodeListToNodeArray(ArenaAllocator &Arena, NodeList *Head,
- size_t Count) {
- NodeArrayNode *N = Arena.alloc<NodeArrayNode>();
- N->Count = Count;
- N->Nodes = Arena.allocArray<Node *>(Count);
- for (size_t I = 0; I < Count; ++I) {
- N->Nodes[I] = Head->N;
- Head = Head->Next;
- }
- return N;
-}
-
QualifiedNameNode *
Demangler::demangleNameScopeChain(std::string_view &MangledName,
IdentifierNode *UnqualifiedName) {
diff --git a/llvm/lib/Demangle/MicrosoftDemangleNodes.cpp b/llvm/lib/Demangle/MicrosoftDemangleNodes.cpp
index 61e4961c714bc..17c6aab500049 100644
--- a/llvm/lib/Demangle/MicrosoftDemangleNodes.cpp
+++ b/llvm/lib/Demangle/MicrosoftDemangleNodes.cpp
@@ -662,9 +662,9 @@ void VcallThunkIdentifierNode::output(OutputBuffer &OB,
void SpecialTableSymbolNode::output(OutputBuffer &OB, OutputFlags Flags) const {
outputQualifiers(OB, Quals, false, true);
Name->output(OB, Flags);
- if (TargetName) {
+ if (TargetNames) {
OB << "{for `";
- TargetName->output(OB, Flags);
+ TargetNames->output(OB, Flags, "'s `");
OB << "'}";
}
}
diff --git a/llvm/test/Demangle/ms-operators.test b/llvm/test/Demangle/ms-operators.test
index b940488786631..cafa1ae3c0663 100644
--- a/llvm/test/Demangle/ms-operators.test
+++ b/llvm/test/Demangle/ms-operators.test
@@ -143,9 +143,24 @@
??_7A at B@@6BC at D@@@
; CHECK: const B::A::`vftable'{for `D::C'}
+??_7A at B@@6BC at D@@E at F@@@
+; CHECK: const B::A::`vftable'{for `D::C's `F::E'}
+
+??_7A at B@@6BC at D@@E at F@@G at H@@@
+; CHECK: const B::A::`vftable'{for `D::C's `F::E's `H::G'}
+
??_8Middle2@@7B@
; CHECK: const Middle2::`vbtable'
+??_7A@@6BB@@@
+; CHECK: const A::`vftable'{for `B'}
+
+??_7A@@6BB@@C@@@
+; CHECK: const A::`vftable'{for `B's `C'}
+
+??_7A@@6BB@@C@@D@@@
+; CHECK: const A::`vftable'{for `B's `C's `D'}
+
??_9Base@@$B7AA
; CHECK: [thunk]: __cdecl Base::`vcall'{8, {flat}}
More information about the llvm-commits
mailing list