Skip to content

Commit 6a478ca

Browse files
committed
demangling: Drop the support of "unmangled suffix"
Either the demangling completely succeeds or it fails. Don't demangle to something like: [...] with unmangled suffix "..." This avoids getting really stupid demangled names for symbols which are actually not swift symbols.
1 parent 9f33bc9 commit 6a478ca

File tree

4 files changed

+41
-35
lines changed

4 files changed

+41
-35
lines changed

include/swift/Demangling/Demangler.h

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -286,16 +286,11 @@ class CharVector : public Vector<char> {
286286
/// It de-mangles a string and it also owns the returned node-tree. This means
287287
/// The nodes of the tree only live as long as the Demangler itself.
288288
class Demangler : public NodeFactory {
289-
private:
289+
protected:
290290
StringRef Text;
291291
size_t Pos = 0;
292292

293-
struct NodeWithPos {
294-
NodePointer Node;
295-
size_t Pos;
296-
};
297-
298-
Vector<NodeWithPos> NodeStack;
293+
Vector<NodePointer> NodeStack;
299294
Vector<NodePointer> Substitutions;
300295
Vector<unsigned> PendingSubstitutions;
301296

@@ -334,18 +329,18 @@ class Demangler : public NodeFactory {
334329
}
335330

336331
void pushNode(NodePointer Nd) {
337-
NodeStack.push_back({ Nd, Pos }, *this);
332+
NodeStack.push_back(Nd, *this);
338333
}
339334

340335
NodePointer popNode() {
341-
return NodeStack.pop_back_val().Node;
336+
return NodeStack.pop_back_val();
342337
}
343338

344339
NodePointer popNode(Node::Kind kind) {
345340
if (NodeStack.empty())
346341
return nullptr;
347342

348-
Node::Kind NdKind = NodeStack.back().Node->getKind();
343+
Node::Kind NdKind = NodeStack.back()->getKind();
349344
if (NdKind != kind)
350345
return nullptr;
351346

@@ -356,7 +351,7 @@ class Demangler : public NodeFactory {
356351
if (NodeStack.empty())
357352
return nullptr;
358353

359-
Node::Kind NdKind = NodeStack.back().Node->getKind();
354+
Node::Kind NdKind = NodeStack.back()->getKind();
360355
if (!pred(NdKind))
361356
return nullptr;
362357

@@ -381,7 +376,7 @@ class Demangler : public NodeFactory {
381376
return createWithChild(kind, popNode(Node::Kind::Type));
382377
}
383378

384-
void parseAndPushNodes();
379+
bool parseAndPushNodes();
385380

386381
NodePointer changeKind(NodePointer Node, Node::Kind NewKind);
387382

lib/Demangling/Demangler.cpp

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,10 @@ NodePointer Demangler::demangleSymbol(StringRef MangledName) {
234234

235235
// If any other prefixes are accepted, please update Mangler::verify.
236236

237-
NodePointer topLevel = createNode(Node::Kind::Global);
238-
239-
parseAndPushNodes();
240-
241-
// Let a trailing '_' be part of the not demangled suffix.
242-
popNode(Node::Kind::FirstElementMarker);
237+
if (!parseAndPushNodes())
238+
return nullptr;
243239

244-
size_t EndPos = (NodeStack.empty() ? 0 : NodeStack.back().Pos);
240+
NodePointer topLevel = createNode(Node::Kind::Global);
245241

246242
NodePointer Parent = topLevel;
247243
while (NodePointer FuncAttr = popNode(isFunctionAttr)) {
@@ -250,8 +246,7 @@ NodePointer Demangler::demangleSymbol(StringRef MangledName) {
250246
FuncAttr->getKind() == Node::Kind::PartialApplyObjCForwarder)
251247
Parent = FuncAttr;
252248
}
253-
for (const NodeWithPos &NWP : NodeStack) {
254-
NodePointer Nd = NWP.Node;
249+
for (Node *Nd : NodeStack) {
255250
switch (Nd->getKind()) {
256251
case Node::Kind::Type:
257252
Parent->addChild(Nd->getFirstChild(), *this);
@@ -264,10 +259,6 @@ NodePointer Demangler::demangleSymbol(StringRef MangledName) {
264259
if (topLevel->getNumChildren() == 0)
265260
return nullptr;
266261

267-
if (EndPos < Text.size()) {
268-
topLevel->addChild(createNode(Node::Kind::Suffix, Text.substr(EndPos)), *this);
269-
}
270-
271262
return topLevel;
272263
}
273264

@@ -282,15 +273,16 @@ NodePointer Demangler::demangleType(StringRef MangledName) {
282273
return createNode(Node::Kind::Suffix, Text);
283274
}
284275

285-
void Demangler::parseAndPushNodes() {
276+
bool Demangler::parseAndPushNodes() {
286277
int Idx = 0;
287-
while (!Text.empty()) {
278+
while (Pos < Text.size()) {
288279
NodePointer Node = demangleOperator();
289280
if (!Node)
290-
break;
281+
return false;
291282
pushNode(Node);
292283
Idx++;
293284
}
285+
return true;
294286
}
295287

296288
NodePointer Demangler::addChild(NodePointer Parent, NodePointer Child) {

lib/Demangling/NodeDumper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ void swift::Demangle::Node::dump() {
6262

6363
void Demangler::dump() {
6464
for (unsigned Idx = 0; Idx < NodeStack.size(); ++Idx) {
65-
fprintf(stderr, "NodeStack[%u] at position %zd:\n", Idx, NodeStack[Idx].Pos);
66-
NodeStack[Idx].Node->dump();
65+
fprintf(stderr, "NodeStack[%u]:\n", Idx);
66+
NodeStack[Idx]->dump();
6767
fprintf(stderr, "\n");
6868
}
6969
fprintf(stderr, "Position = %zd:\n%.*s\n%*s\n", Pos,

lib/SILOptimizer/Utils/SpecializationMangler.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,37 @@ using namespace swift;
1919
using namespace Mangle;
2020

2121
void SpecializationMangler::beginMangling() {
22-
ASTMangler::beginMangling();
22+
ASTMangler::beginManglingWithoutPrefix();
2323
if (Serialized)
2424
ArgOpBuffer << 'q';
2525
ArgOpBuffer << char(uint8_t(Pass) + '0');
2626
}
2727

28+
namespace {
29+
30+
/// Utility class for demangling specialization attributes.
31+
class AttributeDemangler : public Demangle::Demangler {
32+
public:
33+
void demangleAndAddAsChildren(StringRef MangledSpecialization,
34+
NodePointer Parent) {
35+
init(MangledSpecialization);
36+
if (!parseAndPushNodes()) {
37+
llvm::errs() << "Can't demangle: " << MangledSpecialization << '\n';
38+
abort();
39+
}
40+
for (Node *Nd : NodeStack) {
41+
addChild(Parent, Nd);
42+
}
43+
}
44+
};
45+
46+
} // namespace
47+
2848
std::string SpecializationMangler::finalize() {
2949
StringRef MangledSpecialization(Storage.data(), Storage.size());
30-
Demangle::Demangler D;
31-
NodePointer TopLevel = D.demangleSymbol(MangledSpecialization);
50+
AttributeDemangler D;
51+
NodePointer TopLevel = D.createNode(Node::Kind::Global);
52+
D.demangleAndAddAsChildren(MangledSpecialization, TopLevel);
3253

3354
StringRef FuncName = Function->getName();
3455
NodePointer FuncTopLevel = nullptr;
@@ -41,8 +62,6 @@ std::string SpecializationMangler::finalize() {
4162
FuncTopLevel->addChild(D.createNode(Node::Kind::Identifier, FuncName), D);
4263
}
4364
for (NodePointer FuncChild : *FuncTopLevel) {
44-
assert(FuncChild->getKind() != Node::Kind::Suffix ||
45-
FuncChild->getText() == "merged");
4665
TopLevel->addChild(FuncChild, D);
4766
}
4867
std::string mangledName = Demangle::mangleNode(TopLevel);

0 commit comments

Comments
 (0)