Skip to content

Commit

Permalink
[Demangler] Fix OldRemangler to cope with single argument functions.
Browse files Browse the repository at this point in the history
The Demangler can sometimes output ArgumentTuples containing a single argument
without placing that argument inside a Tuple node.  OldRemangler failed to take
account of this and either crashed or failed with an assertion failure depending
on whether assertions were enabled or not.

rdar://63678072
  • Loading branch information
al45tair committed Jun 19, 2021
1 parent 528764c commit d70e93b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
34 changes: 19 additions & 15 deletions lib/Demangling/OldRemangler.cpp
Expand Up @@ -164,27 +164,31 @@ static NodePointer applyParamLabels(NodePointer LabelList, NodePointer OrigType,
auto ParamsType = Factory.createNode(Node::Kind::ArgumentTuple);
auto Tuple = Factory.createNode(Node::Kind::Tuple);

auto OrigTuple = ArgTuple->getFirstChild()->getFirstChild();
assert(OrigTuple->getKind() == Node::Kind::Tuple);

for (unsigned i = 0, n = OrigTuple->getNumChildren(); i != n; ++i) {
const auto Label = LabelList->getChild(i);
auto processParameter = [&](NodePointer Label, NodePointer Param) {
if (Label->getKind() == Node::Kind::FirstElementMarker) {
Tuple->addChild(OrigTuple->getChild(i), Factory);
continue;
Tuple->addChild(Param, Factory);
return;
}

auto OrigElt = OrigTuple->getChild(i);
auto NewElt = Factory.createNode(Node::Kind::TupleElement);

NewElt->addChild(Factory.createNodeWithAllocatedText(
auto NewParam = Factory.createNode(Node::Kind::TupleElement);
NewParam->addChild(Factory.createNodeWithAllocatedText(
Node::Kind::TupleElementName, Label->getText()),
Factory);
Factory);

for (auto &Child : *OrigElt)
NewElt->addChild(Child, Factory);
for (auto &Child : *Param)
NewParam->addChild(Child, Factory);

Tuple->addChild(NewParam, Factory);
};

Tuple->addChild(NewElt, Factory);
auto OrigTuple = ArgTuple->getFirstChild()->getFirstChild();

if (OrigTuple->getKind() != Node::Kind::Tuple) {
processParameter(LabelList->getChild(0), OrigTuple);
} else {
for (unsigned i = 0, n = OrigTuple->getNumChildren(); i != n; ++i) {
processParameter(LabelList->getChild(i), OrigTuple->getChild(i));
}
}

auto Type = Factory.createNode(Node::Kind::Type);
Expand Down
4 changes: 3 additions & 1 deletion test/Demangle/remangle.swift
Expand Up @@ -9,6 +9,8 @@ RUN: diff %t.input %t.output
// CHECK: Swift.(Mystruct in _7B40D7ED6632C2BEA2CA3BFFD57E3435)
RUN: swift-demangle -remangle-objc-rt '$ss8Mystruct33_7B40D7ED6632C2BEA2CA3BFFD57E3435LLV' | %FileCheck %s

// CHECK-OLD: r in _.?(Swift.UnsafeRawPointer) -> Swift.UnsafeRawPointer
RUN: swift-demangle -remangle-objc-rt '$s1_1?1PSVSVF1rP' | %FileCheck -check-prefix CHECK-OLD %s

// CHECK-GENERICEXT: Swift._ContiguousArrayStorage<(extension in Swift):Swift.FlattenSequence<StdlibCollectionUnittest.MinimalBidirectionalCollection<StdlibCollectionUnittest.MinimalBidirectionalCollection<Swift.Int>>>.Index>
RUN: swift-demangle -remangle-objc-rt '$ss23_ContiguousArrayStorageCys15FlattenSequenceVsE5IndexVy24StdlibCollectionUnittest020MinimalBidirectionalH0VyAIySiGG_GGD' | %FileCheck -check-prefix CHECK-GENERICEXT %s

0 comments on commit d70e93b

Please sign in to comment.