Skip to content

Commit 227b438

Browse files
committed
[Demangling] Added DEMANGLER_ASSERT to replace assert() calls.
This returns an error code if we're in the runtime, rather than assert()ing. rdar://79725187
1 parent cc869b9 commit 227b438

File tree

6 files changed

+132
-91
lines changed

6 files changed

+132
-91
lines changed

include/swift/Demangling/Demangle.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ enum class OperatorKind {
516516
struct SWIFT_NODISCARD ManglingError {
517517
enum Code {
518518
Success = 0,
519+
AssertionFailed,
519520
Uninitialized,
520521
TooComplex,
521522
BadNodeKind,

lib/Demangling/DemanglerAssert.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===--- DemanglerAssert.h - Assertions for de/re-mangling ----------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This file implements a macro, DEMANGLE_ASSERT(), which will assert in the
14+
// compiler, but in the runtime will return a ManglingError on failure.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
18+
#ifndef SWIFT_DEMANGLING_ASSERT_H
19+
#define SWIFT_DEMANGLING_ASSERT_H
20+
21+
#if SWIFT_RUNTIME
22+
#define DEMANGLER_ASSERT(expr, node) \
23+
do { \
24+
if (!(expr)) \
25+
return ManglingError(ManglingError::AssertionFailed, node); \
26+
} while (0)
27+
#else
28+
#define DEMANGLER_ASSERT(expr, node) assert(expr)
29+
#endif
30+
31+
#endif // SWIFT_DEMANGLING_ASSERT_H

lib/Demangling/OldRemangler.cpp

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,13 @@
2222
#include "swift/AST/Ownership.h"
2323
#include "swift/Strings.h"
2424
#include "RemanglerBase.h"
25+
#include "DemanglerAssert.h"
2526
#include <cstdio>
2627
#include <cstdlib>
2728

2829
using namespace swift;
2930
using namespace Demangle;
3031

31-
#define RETURN_IF_ERROR(x) \
32-
do { \
33-
ManglingError err = (x); \
34-
if (!err.isSuccess()) \
35-
return err; \
36-
} while (0)
37-
3832
namespace {
3933
class Remangler : public RemanglerBase {
4034
static const unsigned MaxDepth = 1024;
@@ -122,7 +116,7 @@ namespace {
122116
return mangle(*node->begin(), depth);
123117
}
124118
ManglingError mangleChildNode(Node *node, unsigned index, unsigned depth) {
125-
assert(index < node->getNumChildren());
119+
DEMANGLER_ASSERT(index < node->getNumChildren(), node);
126120
return mangle(node->begin()[index], depth);
127121
}
128122

@@ -547,7 +541,7 @@ Remangler::mangleDependentProtocolConformanceAssociated(Node *node,
547541

548542
ManglingError Remangler::mangleProtocolConformance(Node *node, unsigned depth) {
549543
// type, protocol name, context
550-
assert(node->getNumChildren() == 3);
544+
DEMANGLER_ASSERT(node->getNumChildren() == 3, node);
551545
RETURN_IF_ERROR(mangleChildNode(node, 0, depth + 1));
552546
RETURN_IF_ERROR(mangleProtocolWithoutPrefix(node->begin()[1], depth + 1));
553547
return mangleChildNode(node, 2, depth + 1);
@@ -909,7 +903,7 @@ ManglingError
909903
Remangler::mangleAssociatedTypeWitnessTableAccessor(Node *node,
910904
unsigned depth) {
911905
Buffer << "WT";
912-
assert(node->getNumChildren() == 3);
906+
DEMANGLER_ASSERT(node->getNumChildren() == 3, node);
913907
RETURN_IF_ERROR(mangleChildNode(node, 0, depth + 1)); // protocol conformance
914908
RETURN_IF_ERROR(mangleChildNode(node, 1, depth + 1)); // type
915909
return mangleProtocolWithoutPrefix(node->begin()[2], depth + 1); // type
@@ -1010,7 +1004,7 @@ ManglingError Remangler::mangleVariable(Node *node, EntityContext &ctx,
10101004

10111005
ManglingError Remangler::mangleSubscript(Node *node, EntityContext &ctx,
10121006
unsigned depth) {
1013-
assert(node->getNumChildren() >= 2);
1007+
DEMANGLER_ASSERT(node->getNumChildren() >= 2, node);
10141008
Buffer << 'i';
10151009
RETURN_IF_ERROR(mangleEntityContext(node->begin()[0], ctx, depth + 1));
10161010
if (node->getLastChild()->getKind() == Node::Kind::PrivateDeclName)
@@ -1056,7 +1050,7 @@ ManglingError Remangler::mangleAccessor(Node *storageNode,
10561050

10571051
case Demangle::Node::Kind::Subscript: {
10581052
auto NumChildren = storageNode->getNumChildren();
1059-
assert(NumChildren <= 4);
1053+
DEMANGLER_ASSERT(NumChildren <= 4, storageNode);
10601054

10611055
auto PrivateName = storageNode->getChild(NumChildren - 1);
10621056
if (PrivateName->getKind() == Node::Kind::PrivateDeclName)
@@ -1238,7 +1232,7 @@ ManglingError Remangler::mangleSimpleEntity(Node *node, char basicKind,
12381232
StringRef entityKind,
12391233
EntityContext &ctx,
12401234
unsigned depth) {
1241-
assert(node->getNumChildren() == 1);
1235+
DEMANGLER_ASSERT(node->getNumChildren() == 1, node);
12421236
Buffer << basicKind;
12431237
RETURN_IF_ERROR(mangleEntityContext(node->begin()[0], ctx, depth + 1));
12441238
Buffer << entityKind;
@@ -1249,7 +1243,7 @@ ManglingError
12491243
Remangler::mangleNamedEntity(Node *node, char basicKind, StringRef entityKind,
12501244
EntityContext &ctx, unsigned depth,
12511245
StringRef artificialPrivateDiscriminator) {
1252-
assert(node->getNumChildren() == 2);
1246+
DEMANGLER_ASSERT(node->getNumChildren() == 2, node);
12531247
if (basicKind != '\0') Buffer << basicKind;
12541248
RETURN_IF_ERROR(mangleEntityContext(node->begin()[0], ctx, depth + 1));
12551249
Buffer << entityKind;
@@ -1275,7 +1269,8 @@ Remangler::mangleNamedEntity(Node *node, char basicKind, StringRef entityKind,
12751269
ManglingError Remangler::mangleTypedEntity(Node *node, char basicKind,
12761270
StringRef entityKind,
12771271
EntityContext &ctx, unsigned depth) {
1278-
assert(node->getNumChildren() == 2 || node->getNumChildren() == 3);
1272+
DEMANGLER_ASSERT(node->getNumChildren() == 2 || node->getNumChildren() == 3,
1273+
node);
12791274
Buffer << basicKind;
12801275
RETURN_IF_ERROR(mangleEntityContext(node->begin()[0], ctx, depth + 1));
12811276
Buffer << entityKind;
@@ -1295,7 +1290,8 @@ ManglingError Remangler::mangleNamedAndTypedEntity(Node *node, char basicKind,
12951290
StringRef entityKind,
12961291
EntityContext &ctx,
12971292
unsigned depth) {
1298-
assert(node->getNumChildren() == 3 || node->getNumChildren() == 4);
1293+
DEMANGLER_ASSERT(node->getNumChildren() == 3 || node->getNumChildren() == 4,
1294+
node);
12991295
Buffer << basicKind;
13001296
RETURN_IF_ERROR(mangleEntityContext(node->begin()[0], ctx, depth + 1));
13011297
Buffer << entityKind;
@@ -1349,8 +1345,8 @@ ManglingError Remangler::mangleEntityContext(Node *node, EntityContext &ctx,
13491345

13501346
ManglingError Remangler::mangleEntityType(Node *node, EntityContext &ctx,
13511347
unsigned depth) {
1352-
assert(node->getKind() == Node::Kind::Type);
1353-
assert(node->getNumChildren() == 1);
1348+
DEMANGLER_ASSERT(node->getKind() == Node::Kind::Type, node);
1349+
DEMANGLER_ASSERT(node->getNumChildren() == 1, node);
13541350
node = node->begin()[0];
13551351

13561352
// Expand certain kinds of type within the entity context.
@@ -1362,13 +1358,13 @@ ManglingError Remangler::mangleEntityType(Node *node, EntityContext &ctx,
13621358
node->getKind() == Node::Kind::NoEscapeFunctionType)
13631359
? 'F'
13641360
: 'f');
1365-
assert(node->getNumChildren() >= 2);
1361+
DEMANGLER_ASSERT(node->getNumChildren() >= 2, node);
13661362
unsigned inputIndex = node->getNumChildren() - 2;
13671363
for (unsigned i = 0; i <= inputIndex; ++i)
13681364
RETURN_IF_ERROR(mangle(node->begin()[i], depth + 1));
13691365
auto returnType = node->begin()[inputIndex+1];
1370-
assert(returnType->getKind() == Node::Kind::ReturnType);
1371-
assert(returnType->getNumChildren() == 1);
1366+
DEMANGLER_ASSERT(returnType->getKind() == Node::Kind::ReturnType, returnType);
1367+
DEMANGLER_ASSERT(returnType->getNumChildren() == 1, returnType);
13721368
return mangleEntityType(returnType->begin()[0], ctx, depth + 1);
13731369
}
13741370
default:
@@ -1604,23 +1600,23 @@ ManglingError Remangler::mangleClangType(Node *node, unsigned depth) {
16041600
}
16051601

16061602
ManglingError Remangler::mangleImplParameter(Node *node, unsigned depth) {
1607-
assert(node->getNumChildren() == 2);
1603+
DEMANGLER_ASSERT(node->getNumChildren() == 2, node);
16081604
return mangleChildNodes(node, depth + 1); // impl convention, type
16091605
}
16101606

16111607
ManglingError Remangler::mangleImplErrorResult(Node *node, unsigned depth) {
1612-
assert(node->getNumChildren() == 2);
1608+
DEMANGLER_ASSERT(node->getNumChildren() == 2, node);
16131609
Buffer << 'z';
16141610
return mangleChildNodes(node, depth + 1); // impl convention, type
16151611
}
16161612

16171613
ManglingError Remangler::mangleImplResult(Node *node, unsigned depth) {
1618-
assert(node->getNumChildren() == 2);
1614+
DEMANGLER_ASSERT(node->getNumChildren() == 2, node);
16191615
return mangleChildNodes(node, depth + 1); // impl convention, type
16201616
}
16211617

16221618
ManglingError Remangler::mangleImplYield(Node *node, unsigned depth) {
1623-
assert(node->getNumChildren() == 2);
1619+
DEMANGLER_ASSERT(node->getNumChildren() == 2, node);
16241620
Buffer << 'Y';
16251621
return mangleChildNodes(node, depth + 1); // impl convention, type
16261622
}
@@ -1650,7 +1646,7 @@ ManglingError Remangler::mangleImplInvocationSubstitutions(Node *node,
16501646
}
16511647

16521648
ManglingError Remangler::mangleImplConvention(Node *node, unsigned depth) {
1653-
assert(node->getKind() == Node::Kind::ImplConvention);
1649+
DEMANGLER_ASSERT(node->getKind() == Node::Kind::ImplConvention, node);
16541650
StringRef text = node->getText();
16551651
if (text == "@autoreleased") {
16561652
Buffer << 'a';
@@ -1679,7 +1675,8 @@ ManglingError Remangler::mangleImplConvention(Node *node, unsigned depth) {
16791675
ManglingError
16801676
Remangler::mangleImplParameterResultDifferentiability(Node *node,
16811677
unsigned depth) {
1682-
assert(node->getKind() == Node::Kind::ImplDifferentiabilityKind);
1678+
DEMANGLER_ASSERT(node->getKind() == Node::Kind::ImplDifferentiabilityKind,
1679+
node);
16831680
StringRef text = node->getText();
16841681
// Empty string represents default differentiability.
16851682
if (text.empty())
@@ -1711,7 +1708,7 @@ ManglingError Remangler::mangleMetatype(Node *node, unsigned depth) {
17111708
Buffer << 'M';
17121709
return mangleSingleChildNode(node, depth + 1); // type
17131710
} else {
1714-
assert(node->getNumChildren() == 2);
1711+
DEMANGLER_ASSERT(node->getNumChildren() == 2, node);
17151712
Buffer << "XM";
17161713
return mangleChildNodes(node, depth + 1); // metatype representation, type
17171714
}
@@ -1722,7 +1719,7 @@ ManglingError Remangler::mangleExistentialMetatype(Node *node, unsigned depth) {
17221719
Buffer << "PM";
17231720
return mangleSingleChildNode(node, depth + 1); // type
17241721
} else {
1725-
assert(node->getNumChildren() == 2);
1722+
DEMANGLER_ASSERT(node->getNumChildren() == 2, node);
17261723
Buffer << "XPM";
17271724
return mangleChildNodes(node, depth + 1); // metatype representation, type
17281725
}
@@ -1752,10 +1749,10 @@ ManglingError Remangler::mangleProtocolList(Node *node, unsigned depth) {
17521749
ManglingError
17531750
Remangler::mangleProtocolListWithoutPrefix(Node *node, unsigned depth,
17541751
Node *additionalProto) {
1755-
assert(node->getKind() == Node::Kind::ProtocolList);
1756-
assert(node->getNumChildren() == 1);
1752+
DEMANGLER_ASSERT(node->getKind() == Node::Kind::ProtocolList, node);
1753+
DEMANGLER_ASSERT(node->getNumChildren() == 1, node);
17571754
auto typeList = node->begin()[0];
1758-
assert(typeList->getKind() == Node::Kind::TypeList);
1755+
DEMANGLER_ASSERT(typeList->getKind() == Node::Kind::TypeList, typeList);
17591756
for (auto &child : *typeList) {
17601757
RETURN_IF_ERROR(mangleProtocolWithoutPrefix(child, depth + 1));
17611758
}
@@ -1932,7 +1929,7 @@ ManglingError Remangler::mangleConstrainedType(Node *node, unsigned depth) {
19321929

19331930
ManglingError Remangler::mangleAssociatedType(Node *node, unsigned depth) {
19341931
if (node->hasChildren()) {
1935-
assert(node->getNumChildren() == 1);
1932+
DEMANGLER_ASSERT(node->getNumChildren() == 1, node);
19361933
return mangleProtocolListWithoutPrefix(*node->begin(), depth + 1);
19371934
} else {
19381935
Buffer << '_';
@@ -1946,7 +1943,8 @@ ManglingError Remangler::mangleDeclContext(Node *node, unsigned depth) {
19461943

19471944
ManglingError Remangler::mangleExtension(Node *node, EntityContext &ctx,
19481945
unsigned depth) {
1949-
assert(node->getNumChildren() == 2 || node->getNumChildren() == 3);
1946+
DEMANGLER_ASSERT(node->getNumChildren() == 2 || node->getNumChildren() == 3,
1947+
node);
19501948
if (node->getNumChildren() == 3) {
19511949
Buffer << 'e';
19521950
} else {
@@ -2006,10 +2004,10 @@ ManglingError Remangler::mangleDependentMemberType(Node *node, unsigned depth) {
20062004
base = base->getFirstChild()->getFirstChild();
20072005
} while (base->getKind() == Node::Kind::DependentMemberType);
20082006

2009-
assert(base->getKind() == Node::Kind::DependentGenericParamType
2010-
&& "dependent members not based on a generic param are non-canonical"
2011-
" and shouldn't need remangling");
2012-
assert(members.size() >= 1);
2007+
DEMANGLER_ASSERT(base->getKind() == Node::Kind::DependentGenericParamType
2008+
&& "dependent members not based on a generic param are "
2009+
"non-canonical and shouldn't need remangling", base);
2010+
DEMANGLER_ASSERT(members.size() >= 1, node);
20132011
if (members.size() == 1) {
20142012
Buffer << 'w';
20152013
RETURN_IF_ERROR(mangleDependentGenericParamIndex(base, depth + 1));
@@ -2098,11 +2096,11 @@ ManglingError Remangler::mangleProtocolWithoutPrefix(Node *node,
20982096
return ManglingError::Success;
20992097

21002098
if (node->getKind() == Node::Kind::Type) {
2101-
assert(node->getNumChildren() == 1);
2099+
DEMANGLER_ASSERT(node->getNumChildren() == 1, node);
21022100
node = node->begin()[0];
21032101
}
21042102

2105-
assert(node->getKind() == Node::Kind::Protocol);
2103+
DEMANGLER_ASSERT(node->getKind() == Node::Kind::Protocol, node);
21062104
EntityContext ctx;
21072105
return mangleNominalType(node, '\0', ctx, depth + 1);
21082106
}
@@ -2125,7 +2123,7 @@ ManglingError Remangler::mangleGenericArgs(Node *node, EntityContext &ctx,
21252123
case Node::Kind::BoundGenericEnum:
21262124
case Node::Kind::BoundGenericClass: {
21272125
NodePointer unboundType = node->getChild(0);
2128-
assert(unboundType->getKind() == Node::Kind::Type);
2126+
DEMANGLER_ASSERT(unboundType->getKind() == Node::Kind::Type, unboundType);
21292127
NodePointer nominalType = unboundType->getChild(0);
21302128
NodePointer parentOrModule = nominalType->getChild(0);
21312129
RETURN_IF_ERROR(mangleGenericArgs(parentOrModule, ctx, depth + 1));
@@ -2490,26 +2488,28 @@ ManglingError Remangler::mangleVTableThunk(Node *node, unsigned depth) {
24902488

24912489
ManglingError Remangler::mangleSILBoxTypeWithLayout(Node *node,
24922490
unsigned depth) {
2493-
assert(node->getKind() == Node::Kind::SILBoxTypeWithLayout);
2494-
assert(node->getNumChildren() == 1 || node->getNumChildren() == 3);
2491+
DEMANGLER_ASSERT(node->getKind() == Node::Kind::SILBoxTypeWithLayout, node);
2492+
DEMANGLER_ASSERT(node->getNumChildren() == 1 || node->getNumChildren() == 3,
2493+
node);
24952494
Buffer << "XB";
24962495
auto layout = node->getChild(0);
2497-
assert(layout->getKind() == Node::Kind::SILBoxLayout);
2496+
DEMANGLER_ASSERT(layout->getKind() == Node::Kind::SILBoxLayout, layout);
24982497
NodePointer genericArgs = nullptr;
24992498
if (node->getNumChildren() == 3) {
25002499
NodePointer signature = node->getChild(1);
2501-
assert(signature->getKind() == Node::Kind::DependentGenericSignature);
2500+
DEMANGLER_ASSERT(signature->getKind() == Node::Kind::DependentGenericSignature,
2501+
signature);
25022502
genericArgs = node->getChild(2);
2503-
assert(genericArgs->getKind() == Node::Kind::TypeList);
2504-
2503+
DEMANGLER_ASSERT(genericArgs->getKind() == Node::Kind::TypeList, genericArgs);
2504+
25052505
Buffer << 'G';
25062506
RETURN_IF_ERROR(mangleDependentGenericSignature(signature, depth + 1));
25072507
}
25082508
RETURN_IF_ERROR(mangleSILBoxLayout(layout, depth + 1));
25092509
if (genericArgs) {
25102510
for (unsigned i = 0; i < genericArgs->getNumChildren(); ++i) {
25112511
auto type = genericArgs->getChild(i);
2512-
assert(genericArgs->getKind() == Node::Kind::Type);
2512+
DEMANGLER_ASSERT(genericArgs->getKind() == Node::Kind::Type, genericArgs);
25132513
RETURN_IF_ERROR(mangleType(type, depth + 1));
25142514
}
25152515
Buffer << '_';
@@ -2519,10 +2519,11 @@ ManglingError Remangler::mangleSILBoxTypeWithLayout(Node *node,
25192519
}
25202520

25212521
ManglingError Remangler::mangleSILBoxLayout(Node *node, unsigned depth) {
2522-
assert(node->getKind() == Node::Kind::SILBoxLayout);
2522+
DEMANGLER_ASSERT(node->getKind() == Node::Kind::SILBoxLayout, node);
25232523
for (unsigned i = 0; i < node->getNumChildren(); ++i) {
2524-
assert(node->getKind() == Node::Kind::SILBoxImmutableField
2525-
|| node->getKind() == Node::Kind::SILBoxMutableField);
2524+
DEMANGLER_ASSERT(node->getKind() == Node::Kind::SILBoxImmutableField
2525+
|| node->getKind() == Node::Kind::SILBoxMutableField,
2526+
node);
25262527
RETURN_IF_ERROR(mangle(node->getChild(i), depth + 1));
25272528
}
25282529
Buffer << '_';
@@ -2532,16 +2533,16 @@ ManglingError Remangler::mangleSILBoxLayout(Node *node, unsigned depth) {
25322533

25332534
ManglingError Remangler::mangleSILBoxMutableField(Node *node, unsigned depth) {
25342535
Buffer << 'm';
2535-
assert(node->getNumChildren() == 1
2536-
&& node->getChild(0)->getKind() == Node::Kind::Type);
2536+
DEMANGLER_ASSERT(node->getNumChildren() == 1
2537+
&& node->getChild(0)->getKind() == Node::Kind::Type, node);
25372538
return mangleType(node->getChild(0), depth + 1);
25382539
}
25392540

25402541
ManglingError Remangler::mangleSILBoxImmutableField(Node *node,
25412542
unsigned depth) {
25422543
Buffer << 'i';
2543-
assert(node->getNumChildren() == 1
2544-
&& node->getChild(0)->getKind() == Node::Kind::Type);
2544+
DEMANGLER_ASSERT(node->getNumChildren() == 1
2545+
&& node->getChild(0)->getKind() == Node::Kind::Type, node);
25452546
return mangleType(node->getChild(0), depth + 1);
25462547
}
25472548

@@ -2730,7 +2731,7 @@ Demangle::mangleNodeOld(NodePointer node, NodeFactory &Factory) {
27302731
ManglingErrorOr<const char *>
27312732
Demangle::mangleNodeAsObjcCString(NodePointer node,
27322733
NodeFactory &Factory) {
2733-
assert(node);
2734+
DEMANGLER_ASSERT(node, node);
27342735

27352736
Remangler remangler(Factory);
27362737
remangler.append("_Tt");

0 commit comments

Comments
 (0)