Skip to content

Commit a8a18c0

Browse files
committed
swift-demangler: Add an option -strip-specialization to get the symbol name of the origin of a specialized function.
1 parent 218cc9b commit a8a18c0

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

include/swift/Demangling/Demangle.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ class Node {
221221
// Only to be used by the demangler parsers.
222222
void addChild(NodePointer Child, NodeFactory &Factory);
223223
// Only to be used by the demangler parsers.
224-
void removeChildAt(unsigned Pos, NodeFactory &factory);
224+
void removeChildAt(unsigned Pos);
225225

226226
// Reverses the order of children.
227227
void reverseChildren(size_t StartingAt = 0);

lib/Demangling/Demangler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ void Node::addChild(NodePointer Child, NodeFactory &Factory) {
355355
}
356356
}
357357

358-
void Node::removeChildAt(unsigned Pos, swift::Demangle::NodeFactory &factory) {
358+
void Node::removeChildAt(unsigned Pos) {
359359
switch (NodePayloadKind) {
360360
case PayloadKind::OneChild:
361361
assert(Pos == 0);
@@ -1255,7 +1255,7 @@ NodePointer Demangler::popFunctionParamLabels(NodePointer Type) {
12551255
auto Label = getChildIf(Param, Node::Kind::TupleElementName);
12561256

12571257
if (Label.first) {
1258-
Param->removeChildAt(Label.second, *this);
1258+
Param->removeChildAt(Label.second);
12591259
return createNodeWithAllocatedText(Node::Kind::Identifier,
12601260
Label.first->getText());
12611261
}

tools/swift-demangle/swift-demangle.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
#include <iostream>
3434

35+
using namespace swift::Demangle;
36+
3537
static llvm::cl::opt<bool>
3638
ExpandMode("expand",
3739
llvm::cl::desc("Expand mode (show node structure of the demangling)"));
@@ -44,6 +46,10 @@ static llvm::cl::opt<bool>
4446
TreeOnly("tree-only",
4547
llvm::cl::desc("Tree-only mode (do not show the demangled string)"));
4648

49+
static llvm::cl::opt<bool>
50+
StripSpecialization("strip-specialization",
51+
llvm::cl::desc("Remangle the origin of a specialized function"));
52+
4753
static llvm::cl::opt<bool>
4854
RemangleMode("test-remangle",
4955
llvm::cl::desc("Remangle test mode (show the remangled string)"));
@@ -82,6 +88,22 @@ static llvm::StringRef substrAfter(llvm::StringRef whole,
8288
return whole.substr((part.data() - whole.data()) + part.size());
8389
}
8490

91+
static void stripSpecialization(NodePointer Node) {
92+
if (Node->getKind() != Node::Kind::Global)
93+
return;
94+
switch (Node->getFirstChild()->getKind()) {
95+
case Node::Kind::FunctionSignatureSpecialization:
96+
case Node::Kind::GenericSpecialization:
97+
case Node::Kind::GenericSpecializationNotReAbstracted:
98+
case Node::Kind::GenericPartialSpecialization:
99+
case Node::Kind::GenericPartialSpecializationNotReAbstracted:
100+
Node->removeChildAt(0);
101+
break;
102+
default:
103+
break;
104+
}
105+
}
106+
85107
static void demangle(llvm::raw_ostream &os, llvm::StringRef name,
86108
swift::Demangle::Context &DCtx,
87109
const swift::Demangle::DemangleOptions &options) {
@@ -142,6 +164,12 @@ static void demangle(llvm::raw_ostream &os, llvm::StringRef name,
142164
llvm::outs() << remangled;
143165
return;
144166
}
167+
if (StripSpecialization) {
168+
stripSpecialization(pointer);
169+
std::string remangled = swift::Demangle::mangleNode(pointer);
170+
llvm::outs() << remangled;
171+
return;
172+
}
145173
std::string string = swift::Demangle::nodeToString(pointer, options);
146174
if (!CompactMode)
147175
llvm::outs() << name << " ---> ";

0 commit comments

Comments
 (0)