Skip to content

Commit 697ba85

Browse files
committed
swift-api-digester: add a new enum class describing why two SDKNodes are matched, NFC.
1 parent 37a7dce commit 697ba85

File tree

1 file changed

+65
-32
lines changed

1 file changed

+65
-32
lines changed

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

Lines changed: 65 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -226,27 +226,52 @@ class SDKNodeVisitor {
226226
virtual ~SDKNodeVisitor() = default;
227227
};
228228

229+
enum class NodeMatchReason: uint8_t {
230+
231+
// Two nodes are matched because they're both roots.
232+
Root,
233+
234+
// The first node is missing.
235+
Added,
236+
237+
// The second node is missing.
238+
Removed,
239+
240+
// The nodes are considered a pair becuase they have same/similar name.
241+
Name,
242+
243+
// The nodes are matched because they're in the same order, e.g. ith child of
244+
// a type declaration.
245+
Sequential,
246+
247+
// The first node is a function and it chanaged to a propery as the second
248+
// node.
249+
FuncToProperty,
250+
251+
// The first node is a global variable and the second node is an enum element.
252+
ModernizeEnum,
253+
};
254+
229255
// During the matching phase, any matched node will be reported using this API.
230256
// For update Node left = {Node before change} Right = {Node after change};
231257
// For added Node left = {NilNode} Right = {Node after change};
232258
// For removed Node left = {Node before change} Right = {NilNode}
233259
struct MatchedNodeListener {
234-
virtual void foundMatch(NodePtr Left, NodePtr Right) = 0;
235-
virtual void foundRemoveAddMatch(NodePtr Removed, NodePtr Added) {}
260+
virtual void foundMatch(NodePtr Left, NodePtr Right, NodeMatchReason Reason) = 0;
236261
virtual ~MatchedNodeListener() = default;
237262
};
238263

239264
using NodePairVector = llvm::MapVector<NodePtr, NodePtr>;
240265

241266
// This map keeps track of updated nodes; thus we can conveniently find out what
242267
// is the counterpart of a node before or after being updated.
243-
class UpdatedNodesMap : public MatchedNodeListener {
268+
class UpdatedNodesMap {
244269
NodePairVector MapImpl;
245270
UpdatedNodesMap(const UpdatedNodesMap& that) = delete;
246271
public:
247272
UpdatedNodesMap() = default;
248273
NodePtr findUpdateCounterpart(const SDKNode *Node) const;
249-
void foundMatch(NodePtr Left, NodePtr Right) override {
274+
void insert(NodePtr Left, NodePtr Right) {
250275
assert(Left && Right && "Not update operation.");
251276
MapImpl.insert({Left, Right});
252277
}
@@ -2029,9 +2054,10 @@ class SequentialNodeMatcher : public NodeMatcher {
20292054
if (L && R && *L == *R)
20302055
continue;
20312056
if (!L || !R)
2032-
Listener.foundRemoveAddMatch(L, R);
2057+
Listener.foundMatch(L, R,
2058+
L ? NodeMatchReason::Removed : NodeMatchReason::Added);
20332059
else
2034-
Listener.foundMatch(L, R);
2060+
Listener.foundMatch(L, R, NodeMatchReason::Sequential);
20352061
}
20362062
}
20372063
};
@@ -2045,6 +2071,7 @@ class BestMatchMatcher : public NodeMatcher {
20452071
NodeVector &Right;
20462072
llvm::function_ref<bool(NodePtr, NodePtr)> CanMatch;
20472073
llvm::function_ref<bool(NodeMatch, NodeMatch)> IsFirstMatchBetter;
2074+
NodeMatchReason Reason;
20482075
MatchedNodeListener &Listener;
20492076
llvm::SmallPtrSet<NodePtr, 16> MatchedRight;
20502077

@@ -2068,16 +2095,17 @@ class BestMatchMatcher : public NodeMatcher {
20682095
BestMatchMatcher(NodeVector &Left, NodeVector &Right,
20692096
llvm::function_ref<bool(NodePtr, NodePtr)> CanMatch,
20702097
llvm::function_ref<bool(NodeMatch, NodeMatch)> IsFirstMatchBetter,
2098+
NodeMatchReason Reason,
20712099
MatchedNodeListener &Listener) : Left(Left), Right(Right),
20722100
CanMatch(CanMatch),
2073-
IsFirstMatchBetter(IsFirstMatchBetter),
2101+
IsFirstMatchBetter(IsFirstMatchBetter), Reason(Reason),
20742102
Listener(Listener){}
20752103

20762104
void match() override {
20772105
for (auto L : Left) {
20782106
if (auto Best = findBestMatch(L, Right)) {
20792107
MatchedRight.insert(Best.getValue());
2080-
Listener.foundMatch(L, Best.getValue());
2108+
Listener.foundMatch(L, Best.getValue(), Reason);
20812109
}
20822110
}
20832111
}
@@ -2096,9 +2124,9 @@ class RemovedAddedNodeMatcher : public NodeMatcher, public MatchedNodeListener {
20962124
if (contains(Matched, A))
20972125
continue;
20982126
if (Left)
2099-
Listener.foundRemoveAddMatch(A, nullptr);
2127+
Listener.foundMatch(A, nullptr, NodeMatchReason::Removed);
21002128
else
2101-
Listener.foundRemoveAddMatch(nullptr, A);
2129+
Listener.foundMatch(nullptr, A, NodeMatchReason::Added);
21022130
}
21032131
}
21042132

@@ -2117,7 +2145,7 @@ class RemovedAddedNodeMatcher : public NodeMatcher, public MatchedNodeListener {
21172145
return false;
21182146
}
21192147
R->annotate(NodeAnnotation::PropertyName, A->getPrintedName());
2120-
foundMatch(R, A);
2148+
foundMatch(R, A, NodeMatchReason::FuncToProperty);
21212149
return true;
21222150
}
21232151
}
@@ -2157,7 +2185,7 @@ class RemovedAddedNodeMatcher : public NodeMatcher, public MatchedNodeListener {
21572185
Child->getName()).str();
21582186
R->annotate(NodeAnnotation::ModernizeEnum,
21592187
R->getSDKContext().buffer(FullName));
2160-
foundMatch(R, A);
2188+
foundMatch(R, A, NodeMatchReason::ModernizeEnum);
21612189
return true;
21622190
}
21632191
}
@@ -2171,7 +2199,7 @@ class RemovedAddedNodeMatcher : public NodeMatcher, public MatchedNodeListener {
21712199
auto LastR = getLastPartOfUsr(R);
21722200
auto LastA = getLastPartOfUsr(A);
21732201
if (LastR && LastA && LastR.getValue() == LastA.getValue()) {
2174-
foundMatch(R, A);
2202+
foundMatch(R, A, NodeMatchReason::Name);
21752203
return true;
21762204
}
21772205
return false;
@@ -2249,8 +2277,8 @@ class RemovedAddedNodeMatcher : public NodeMatcher, public MatchedNodeListener {
22492277
#undef DIST
22502278
}
22512279

2252-
void foundMatch(NodePtr R, NodePtr A) override {
2253-
Listener.foundRemoveAddMatch(R, A);
2280+
void foundMatch(NodePtr R, NodePtr A, NodeMatchReason Reason) override {
2281+
Listener.foundMatch(R, A, Reason);
22542282
RemovedMatched.push_back(R);
22552283
AddedMatched.push_back(A);
22562284
}
@@ -2277,7 +2305,6 @@ class RemovedAddedNodeMatcher : public NodeMatcher, public MatchedNodeListener {
22772305
NodeVector RenameLeft;
22782306
NodeVector RenameRight;
22792307

2280-
22812308
for (auto Remain : Removed) {
22822309
if (!contains(RemovedMatched, Remain))
22832310
RenameLeft.push_back(Remain);
@@ -2289,7 +2316,7 @@ class RemovedAddedNodeMatcher : public NodeMatcher, public MatchedNodeListener {
22892316
}
22902317

22912318
BestMatchMatcher RenameMatcher(RenameLeft, RenameRight, isRename,
2292-
isBetterMatch, *this);
2319+
isBetterMatch, NodeMatchReason::Name, *this);
22932320
RenameMatcher.match();
22942321
// Rename detection ends.
22952322

@@ -2393,7 +2420,7 @@ void SameNameNodeMatcher::match() {
23932420
// match kind list.
23942421
if (auto Match = findBestNameMatch(Candidates,
23952422
getNameMatchKindPriority(LN->getKind()))) {
2396-
Listener.foundMatch(LN, Match);
2423+
Listener.foundMatch(LN, Match, NodeMatchReason::Name);
23972424
MatchedRight.push_back(Match);
23982425
} else {
23992426
Removed.push_back(LN);
@@ -2416,7 +2443,7 @@ class SequentialRecursiveMatcher : public NodeMatcher {
24162443
MatchedNodeListener &Listener;
24172444

24182445
void matchInternal(NodePtr L, NodePtr R) {
2419-
Listener.foundMatch(L, R);
2446+
Listener.foundMatch(L, R, NodeMatchReason::Sequential);
24202447
if (!L || !R)
24212448
return;
24222449
for (unsigned I = 0; I < std::max(L->getChildrenCount(),
@@ -2525,25 +2552,31 @@ class PrunePass : public MatchedNodeListener, public SDKTreeDiffPass {
25252552
public:
25262553
PrunePass(UpdatedNodesMap &UpdateMap) : UpdateMap(UpdateMap) {}
25272554

2528-
void foundRemoveAddMatch(NodePtr Left, NodePtr Right) override {
2529-
if (!Left)
2555+
void foundMatch(NodePtr Left, NodePtr Right, NodeMatchReason Reason) override {
2556+
switch (Reason) {
2557+
case NodeMatchReason::Added:
2558+
assert(!Left);
25302559
Right->annotate(NodeAnnotation::Added);
2531-
else if (!Right) {
2560+
return;
2561+
case NodeMatchReason::Removed:
2562+
assert(!Right);
25322563
Left->annotate(NodeAnnotation::Removed);
2533-
} else if (Right->getKind() == Left->getKind()) {
2534-
foundMatch(Left, Right);
2535-
} else {
2564+
return;
2565+
case NodeMatchReason::FuncToProperty:
2566+
case NodeMatchReason::ModernizeEnum:
25362567
Left->annotate(NodeAnnotation::Removed);
25372568
Right->annotate(NodeAnnotation::Added);
2569+
return;
2570+
case NodeMatchReason::Root:
2571+
case NodeMatchReason::Name:
2572+
case NodeMatchReason::Sequential:
2573+
break;
25382574
}
2539-
}
2540-
2541-
void foundMatch(NodePtr Left, NodePtr Right) override {
25422575
assert(Left && Right);
25432576
Left->annotate(NodeAnnotation::Updated);
25442577
Right->annotate(NodeAnnotation::Updated);
25452578
// Push the updated node to the map for future reference.
2546-
UpdateMap.foundMatch(Left, Right);
2579+
UpdateMap.insert(Left, Right);
25472580

25482581
if (Left->getKind() != Right->getKind()) {
25492582
assert(isa<SDKNodeType>(Left) && isa<SDKNodeType>(Right) &&
@@ -2589,14 +2622,14 @@ class PrunePass : public MatchedNodeListener, public SDKTreeDiffPass {
25892622
auto LC = Left->getChildren()[0];
25902623
auto RC = Right->getChildren()[0];
25912624
if (!(*LC == *RC))
2592-
foundMatch(LC, RC);
2625+
foundMatch(LC, RC, NodeMatchReason::Sequential);
25932626
break;
25942627
}
25952628
}
25962629
}
25972630

25982631
void pass(NodePtr Left, NodePtr Right) override {
2599-
foundMatch(Left, Right);
2632+
foundMatch(Left, Right, NodeMatchReason::Root);
26002633
}
26012634
};
26022635

@@ -3762,7 +3795,7 @@ class RenameDetectorForMemberDiff : public MatchedNodeListener {
37623795
InterfaceTypeChangeDetector RightDetector;
37633796
public:
37643797
RenameDetectorForMemberDiff(): LeftDetector(true), RightDetector(false) {}
3765-
void foundMatch(NodePtr Left, NodePtr Right) override {
3798+
void foundMatch(NodePtr Left, NodePtr Right, NodeMatchReason Reason) override {
37663799
detectRename(Left, Right);
37673800
LeftDetector.detect(Left, Right);
37683801
RightDetector.detect(Right, Left);

0 commit comments

Comments
 (0)