Skip to content

Commit 186c6db

Browse files
authored
Merge pull request #5441 from nkcsgexi/digester-cleanup
2 parents 03029b6 + 63c3418 commit 186c6db

File tree

1 file changed

+71
-32
lines changed

1 file changed

+71
-32
lines changed

tools/swift-api-digester/swift-api-digester.cpp

Lines changed: 71 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,24 @@ struct NodeMatcher {
208208
virtual ~NodeMatcher() = default;
209209
};
210210

211-
#define KEY(NAME) static const char* Key_##NAME = #NAME;
211+
enum class KeyKind {
212+
#define KEY(NAME) KK_##NAME,
212213
#include "DigesterEnums.def"
214+
};
215+
216+
static KeyKind parseKeyKind(StringRef Content) {
217+
return llvm::StringSwitch<KeyKind>(Content)
218+
#define KEY(NAME) .Case(#NAME, KeyKind::KK_##NAME)
219+
#include "DigesterEnums.def"
220+
;
221+
}
222+
223+
static StringRef getKeyContent(KeyKind Kind) {
224+
switch (Kind) {
225+
#define KEY(NAME) case KeyKind::KK_##NAME: return InsertToBuffer(#NAME);
226+
#include "DigesterEnums.def"
227+
}
228+
}
213229

214230
// The node kind appearing in the tree that describes the content of the SDK
215231
enum class SDKNodeKind {
@@ -751,36 +767,49 @@ NodeUniquePtr SDKNode::constructSDKNode(llvm::yaml::MappingNode *Node) {
751767
NodeOwnedVector Children;
752768

753769
for (auto Pair : *Node) {
754-
auto Key = GetScalarString(Pair.getKey());
755-
if (Key == Key_kind) {
770+
switch(parseKeyKind(GetScalarString(Pair.getKey()))) {
771+
case KeyKind::KK_kind:
756772
Kind = llvm::StringSwitch<SDKNodeKind>(GetScalarString(Pair.getValue()))
757773
#define NODE_KIND(NAME) .Case(#NAME, SDKNodeKind::NAME)
758774
#include "DigesterEnums.def"
759775
;
760-
} else if (Key == Key_name) {
776+
break;
777+
case KeyKind::KK_name:
761778
Info.Name = GetScalarString(Pair.getValue());
762-
} else if (Key == Key_selfIndex) {
779+
break;
780+
case KeyKind::KK_selfIndex:
763781
Info.SelfIndex = std::stoi(cast<llvm::yaml::ScalarNode>(Pair.getValue())->
764782
getRawValue());
765-
} else if (Key == Key_usr) {
783+
break;
784+
case KeyKind::KK_usr:
766785
Info.USR = GetScalarString(Pair.getValue());
767-
} else if (Key == Key_location) {
786+
break;
787+
788+
case KeyKind::KK_location:
768789
Info.Location = GetScalarString(Pair.getValue());
769-
} else if (Key == Key_children) {
790+
break;
791+
case KeyKind::KK_children:
770792
for (auto &Mapping : *cast<llvm::yaml::SequenceNode>(Pair.getValue())) {
771793
Children.push_back(constructSDKNode(cast<llvm::yaml::MappingNode>(&Mapping)));
772794
}
773-
} else if (Key == Key_printedName) {
795+
break;
796+
case KeyKind::KK_printedName:
774797
Info.PrintedName = GetScalarString(Pair.getValue());
775-
} else if (Key == Key_moduleName) {
798+
break;
799+
case KeyKind::KK_moduleName:
776800
Info.ModuleName = GetScalarString(Pair.getValue());
777-
} else if (Key == Key_throwing) {
801+
break;
802+
case KeyKind::KK_throwing:
778803
Info.IsThrowing = true;
779-
} else if (Key == Key_mutating) {
804+
break;
805+
case KeyKind::KK_mutating:
780806
Info.IsMutating = true;
781-
} else if (Key == Key_static) {
807+
break;
808+
case KeyKind::KK_static:
782809
Info.IsStatic = true;
783-
} else if (Key == Key_typeAttributes) {
810+
break;
811+
812+
case KeyKind::KK_typeAttributes: {
784813
auto *Seq = cast<llvm::yaml::SequenceNode>(Pair.getValue());
785814
for (auto It = Seq->begin(); It != Seq->end(); ++ It) {
786815
Info.TypeAttrs.push_back(
@@ -789,7 +818,9 @@ NodeUniquePtr SDKNode::constructSDKNode(llvm::yaml::MappingNode *Node) {
789818
#include "swift/AST/Attr.def"
790819
.Case("Count", TypeAttrKind::TAK_Count));
791820
}
792-
} else if (Key == Key_declAttributes) {
821+
break;
822+
}
823+
case KeyKind::KK_declAttributes: {
793824
auto *Seq = cast<llvm::yaml::SequenceNode>(Pair.getValue());
794825
for (auto It = Seq->begin(); It != Seq->end(); ++ It) {
795826
Info.DeclAttrs.push_back(
@@ -798,13 +829,14 @@ NodeUniquePtr SDKNode::constructSDKNode(llvm::yaml::MappingNode *Node) {
798829
#include "DigesterEnums.def"
799830
);
800831
}
801-
} else if (Key == Key_declKind) {
832+
break;
833+
}
834+
case KeyKind::KK_declKind:
802835
Info.DKind = llvm::StringSwitch<DeclKind>(GetScalarString(Pair.getValue()))
803836
#define DECL(X, PARENT) .Case(#X, DeclKind::X)
804837
#include "swift/AST/DeclNodes.def"
805838
;
806-
} else {
807-
llvm_unreachable("Cannot parse key.");
839+
break;
808840
}
809841
};
810842
NodeUniquePtr Result = Info.createSDKNode(Kind);
@@ -1315,44 +1347,51 @@ namespace swift {
13151347
auto Name = value->getName();
13161348
auto PrintedName = value->getPrintedName();
13171349

1318-
out.mapRequired(Key_kind, Kind);
1319-
out.mapRequired(Key_name, Name);
1320-
out.mapRequired(Key_printedName, PrintedName);
1350+
out.mapRequired(getKeyContent(KeyKind::KK_kind).data(), Kind);
1351+
out.mapRequired(getKeyContent(KeyKind::KK_name).data(), Name);
1352+
out.mapRequired(getKeyContent(KeyKind::KK_printedName).data(),
1353+
PrintedName);
13211354

13221355
if (auto D = dyn_cast<SDKNodeDecl>(value.get())) {
13231356
DeclKind DK = D->getDeclKind();
13241357
StringRef Usr = D->getUsr();
13251358
StringRef Location = D->getLocation();
13261359
StringRef ModuleName = D->getModuleName();
13271360

1328-
out.mapRequired(Key_declKind, DK);
1329-
out.mapRequired(Key_usr, Usr);
1330-
out.mapRequired(Key_location, Location);
1331-
out.mapRequired(Key_moduleName, ModuleName);
1361+
out.mapRequired(getKeyContent(KeyKind::KK_declKind).data(), DK);
1362+
out.mapRequired(getKeyContent(KeyKind::KK_usr).data(), Usr);
1363+
out.mapRequired(getKeyContent(KeyKind::KK_location).data(), Location);
1364+
out.mapRequired(getKeyContent(KeyKind::KK_moduleName).data(),
1365+
ModuleName);
13321366
if (auto isStatic = D->isStatic())
1333-
out.mapRequired(Key_static, isStatic);
1367+
out.mapRequired(getKeyContent(KeyKind::KK_static).data(), isStatic);
13341368

13351369
if (auto F = dyn_cast<SDKNodeAbstractFunc>(value.get())) {
13361370
if (bool isThrowing = F->isThrowing())
1337-
out.mapRequired(Key_throwing, isThrowing);
1371+
out.mapRequired(getKeyContent(KeyKind::KK_throwing).data(),
1372+
isThrowing);
13381373
if (bool isMutating = F->isMutating())
1339-
out.mapRequired(Key_mutating, isMutating);
1374+
out.mapRequired(getKeyContent(KeyKind::KK_mutating).data(),
1375+
isMutating);
13401376
if (F->hasSelfIndex()) {
13411377
auto Index = F->getSelfIndex();
1342-
out.mapRequired(Key_selfIndex, Index);
1378+
out.mapRequired(getKeyContent(KeyKind::KK_selfIndex).data(),
1379+
Index);
13431380
}
13441381
}
13451382
auto Attributes = D->getDeclAttributes();
13461383
if (!Attributes.empty())
1347-
out.mapRequired(Key_declAttributes, Attributes);
1384+
out.mapRequired(getKeyContent(KeyKind::KK_declAttributes).data(),
1385+
Attributes);
13481386
} else if (auto T = dyn_cast<SDKNodeType>(value.get())) {
13491387
auto Attributes = T->getTypeAttributes();
13501388
if (!Attributes.empty())
1351-
out.mapRequired(Key_typeAttributes, Attributes);
1389+
out.mapRequired(getKeyContent(KeyKind::KK_typeAttributes).data(),
1390+
Attributes);
13521391
}
13531392
if (!value->isLeaf()) {
13541393
ArrayRef<NodeUniquePtr> Children = value->getChildren();
1355-
out.mapRequired(Key_children, Children);
1394+
out.mapRequired(getKeyContent(KeyKind::KK_children).data(), Children);
13561395
}
13571396
}
13581397
};

0 commit comments

Comments
 (0)