Skip to content

Commit 914abce

Browse files
committed
swift-api-digester: refactor to a switch statement.
1 parent bc3e242 commit 914abce

File tree

1 file changed

+51
-24
lines changed

1 file changed

+51
-24
lines changed

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

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,8 @@ class SDKNode {
314314
bool operator==(const SDKNode &Other) const;
315315
bool operator!=(const SDKNode &Other) const { return !((*this) == Other); }
316316

317+
ArrayRef<NodeAnnotation>
318+
getAnnotations(std::vector<NodeAnnotation> &Scrach) const;
317319
bool isLeaf() const { return Children.empty(); }
318320
SDKNodeKind getKind() const { return SDKNodeKind(TheKind); }
319321
StringRef getName() const { return Name; }
@@ -364,10 +366,10 @@ class SDKNodeDecl : public SDKNode {
364366
bool isObjc() const { return Usr.startswith("c:"); }
365367
static bool classof(const SDKNode *N);
366368
DeclKind getDeclKind() const { return DKind; }
367-
void printFullyQualifiedName(llvm::raw_ostream &OS);
368-
StringRef getFullyQualifiedName();
369-
bool isSDKPrivate();
370-
bool isDeprecated();
369+
void printFullyQualifiedName(llvm::raw_ostream &OS) const;
370+
StringRef getFullyQualifiedName() const;
371+
bool isSDKPrivate() const;
372+
bool isDeprecated() const;
371373
bool isStatic() const { return IsStatic; };
372374
};
373375

@@ -431,9 +433,9 @@ unsigned SDKNode::getChildIndex(NodePtr Child) const {
431433
}
432434

433435
NodePtr SDKNode::getOnlyChild() const {
434-
assert(Children.size() == 1 && "more that one child.");
435-
return (*Children.begin()).get();
436-
}
436+
assert(Children.size() == 1 && "more that one child.");
437+
return (*Children.begin()).get();
438+
}
437439

438440
void SDKNode::addChild(NodeUniquePtr Child) {
439441
Child->Parent = this;
@@ -468,6 +470,13 @@ StringRef SDKNode::getAnnotateComment(NodeAnnotation Anno) const {
468470
return AnnotateComments.find(Anno)->second;
469471
}
470472

473+
ArrayRef<NodeAnnotation> SDKNode::
474+
getAnnotations(std::vector<NodeAnnotation> &Scratch) const {
475+
for(auto Ann : Annotations)
476+
Scratch.push_back(Ann);
477+
return llvm::makeArrayRef(Scratch);
478+
}
479+
471480
bool SDKNode::isAnnotatedAs(NodeAnnotation Anno) const {
472481
return Annotations.find(Anno) != Annotations.end();;
473482
}
@@ -588,19 +597,19 @@ SDKNode* SDKNodeNil::getInstance() {
588597
return Instance.get();
589598
}
590599

591-
bool SDKNodeDecl::isDeprecated() {
600+
bool SDKNodeDecl::isDeprecated() const {
592601
return hasDeclAttribute(SDKDeclAttrKind::DAK_deprecated);
593602
}
594603

595-
bool SDKNodeDecl::isSDKPrivate() {
604+
bool SDKNodeDecl::isSDKPrivate() const {
596605
if (getName().startswith("__"))
597606
return true;
598607
if (auto *PD = dyn_cast<SDKNodeDecl>(getParent()))
599608
return PD->isSDKPrivate();
600609
return false;
601610
}
602611

603-
void SDKNodeDecl::printFullyQualifiedName(llvm::raw_ostream &OS) {
612+
void SDKNodeDecl::printFullyQualifiedName(llvm::raw_ostream &OS) const {
604613
std::vector<NodePtr> Parent;
605614
for (auto *P = getParent(); isa<SDKNodeDecl>(P); P = P->getParent())
606615
Parent.push_back(P);
@@ -609,7 +618,7 @@ void SDKNodeDecl::printFullyQualifiedName(llvm::raw_ostream &OS) {
609618
OS << getPrintedName();
610619
}
611620

612-
StringRef SDKNodeDecl::getFullyQualifiedName() {
621+
StringRef SDKNodeDecl::getFullyQualifiedName() const {
613622
llvm::SmallString<32> Buffer;
614623
llvm::raw_svector_ostream OS(Buffer);
615624
printFullyQualifiedName(OS);
@@ -1936,7 +1945,7 @@ class UpdatedNodesMap : public MatchedNodeListener {
19361945
MapImpl.push_back(std::make_pair(Left, Right));
19371946
}
19381947

1939-
NodePtr findUpdateCounterpart(NodePtr Node) const {
1948+
NodePtr findUpdateCounterpart(const SDKNode *Node) const {
19401949
assert(Node->isAnnotatedAs(NodeAnnotation::Updated) && "Not update operation.");
19411950
auto FoundPair = std::find_if(MapImpl.begin(), MapImpl.end(),
19421951
[&](std::pair<NodePtr, NodePtr> Pair) {
@@ -2688,10 +2697,11 @@ class DiffItemEmitter : public SDKNodeVisitor {
26882697
};
26892698

26902699
class DiagnosisEmitter : public SDKNodeVisitor {
2700+
void handle(const SDKNodeDecl *D, NodeAnnotation Anno);
26912701
void visitType(SDKNodeType *T);
26922702
void visitDecl(SDKNodeDecl *D);
26932703
void visit(NodePtr Node) override;
2694-
SDKNodeDecl *findAddedDecl(SDKNodeDecl *Node);
2704+
SDKNodeDecl *findAddedDecl(const SDKNodeDecl *Node);
26952705
static StringRef printName(StringRef Name);
26962706
static StringRef printDiagKeyword(StringRef Name);
26972707
static void collectAddedDecls(NodePtr Root, std::set<SDKNodeDecl*> &Results);
@@ -2805,7 +2815,7 @@ void DiagnosisEmitter::collectAddedDecls(NodePtr Root,
28052815
collectAddedDecls(C.get(), Results);
28062816
}
28072817

2808-
SDKNodeDecl *DiagnosisEmitter::findAddedDecl(SDKNodeDecl *Root) {
2818+
SDKNodeDecl *DiagnosisEmitter::findAddedDecl(const SDKNodeDecl *Root) {
28092819
for (auto *Added : AddedDecls) {
28102820
if (Root->getKind() == Added->getKind() &&
28112821
Root->getPrintedName() == Added->getPrintedName())
@@ -2904,11 +2914,10 @@ void DiagnosisEmitter::diagnosis(NodePtr LeftRoot, NodePtr RightRoot,
29042914
SDKNode::postorderVisit(LeftRoot, Emitter);
29052915
}
29062916

2907-
void DiagnosisEmitter::visitDecl(SDKNodeDecl *Node) {
2908-
if (Node->isSDKPrivate())
2909-
return;
2910-
if (Node->isAnnotatedAs(NodeAnnotation::Removed) &&
2911-
!Node->isAnnotatedAs(NodeAnnotation::Rename)) {
2917+
void DiagnosisEmitter::handle(const SDKNodeDecl *Node, NodeAnnotation Anno) {
2918+
assert(Node->isAnnotatedAs(Anno));
2919+
switch(Anno) {
2920+
case NodeAnnotation::Removed: {
29122921
if (auto *Added = findAddedDecl(Node)) {
29132922
MovedDecls.Diags.emplace_back(Node->getDeclKind(),
29142923
Added->getDeclKind(),
@@ -2919,29 +2928,34 @@ void DiagnosisEmitter::visitDecl(SDKNodeDecl *Node) {
29192928
Node->getFullyQualifiedName(),
29202929
Node->isDeprecated());
29212930
}
2931+
return;
29222932
}
2923-
if (Node->isAnnotatedAs(NodeAnnotation::Rename)) {
2933+
case NodeAnnotation::Rename: {
29242934
auto *Count = UpdateMap.findUpdateCounterpart(Node)->getAs<SDKNodeDecl>();
29252935
RenamedDecls.Diags.emplace_back(Node->getDeclKind(), Count->getDeclKind(),
29262936
Node->getFullyQualifiedName(),
29272937
Count->getFullyQualifiedName());
2938+
return;
29282939
}
2929-
if (Node->isAnnotatedAs(NodeAnnotation::NowMutating)) {
2940+
case NodeAnnotation::NowMutating: {
29302941
AttrChangedDecls.Diags.emplace_back(Node->getDeclKind(),
29312942
Node->getFullyQualifiedName(),
29322943
InsertToBuffer("mutating"));
2944+
return;
29332945
}
2934-
if (Node->isAnnotatedAs(NodeAnnotation::NowThrowing)) {
2946+
case NodeAnnotation::NowThrowing: {
29352947
AttrChangedDecls.Diags.emplace_back(Node->getDeclKind(),
29362948
Node->getFullyQualifiedName(),
29372949
InsertToBuffer("throwing"));
2950+
return;
29382951
}
2939-
if (Node->isAnnotatedAs(NodeAnnotation::StaticChange)) {
2952+
case NodeAnnotation::StaticChange: {
29402953
AttrChangedDecls.Diags.emplace_back(Node->getDeclKind(),
29412954
Node->getFullyQualifiedName(),
29422955
InsertToBuffer(Node->isStatic() ? "not static" : "static"));
2956+
return;
29432957
}
2944-
if (Node->isAnnotatedAs(NodeAnnotation::OwnershipChange)) {
2958+
case NodeAnnotation::OwnershipChange: {
29452959
auto getOwnershipDescription = [](swift::Ownership O) {
29462960
switch (O) {
29472961
case Ownership::Strong: return InsertToBuffer("strong");
@@ -2955,8 +2969,21 @@ void DiagnosisEmitter::visitDecl(SDKNodeDecl *Node) {
29552969
Node->getFullyQualifiedName(),
29562970
getOwnershipDescription(Node->getOwnership()),
29572971
getOwnershipDescription(Count->getOwnership()));
2972+
return;
2973+
}
2974+
default:
2975+
return;
29582976
}
29592977
}
2978+
2979+
void DiagnosisEmitter::visitDecl(SDKNodeDecl *Node) {
2980+
if (Node->isSDKPrivate())
2981+
return;
2982+
std::vector<NodeAnnotation> Scratch;
2983+
for (auto Anno : Node->getAnnotations(Scratch))
2984+
handle(Node, Anno);
2985+
}
2986+
29602987
void DiagnosisEmitter::visitType(SDKNodeType *Node) {
29612988
auto *Parent = Node->getParent()->getAs<SDKNodeDecl>();
29622989
if (!Parent || Parent->isSDKPrivate())

0 commit comments

Comments
 (0)