@@ -226,27 +226,52 @@ class SDKNodeVisitor {
226
226
virtual ~SDKNodeVisitor () = default ;
227
227
};
228
228
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
+
229
255
// During the matching phase, any matched node will be reported using this API.
230
256
// For update Node left = {Node before change} Right = {Node after change};
231
257
// For added Node left = {NilNode} Right = {Node after change};
232
258
// For removed Node left = {Node before change} Right = {NilNode}
233
259
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;
236
261
virtual ~MatchedNodeListener () = default ;
237
262
};
238
263
239
264
using NodePairVector = llvm::MapVector<NodePtr, NodePtr>;
240
265
241
266
// This map keeps track of updated nodes; thus we can conveniently find out what
242
267
// is the counterpart of a node before or after being updated.
243
- class UpdatedNodesMap : public MatchedNodeListener {
268
+ class UpdatedNodesMap {
244
269
NodePairVector MapImpl;
245
270
UpdatedNodesMap (const UpdatedNodesMap& that) = delete ;
246
271
public:
247
272
UpdatedNodesMap () = default ;
248
273
NodePtr findUpdateCounterpart (const SDKNode *Node) const ;
249
- void foundMatch (NodePtr Left, NodePtr Right) override {
274
+ void insert (NodePtr Left, NodePtr Right) {
250
275
assert (Left && Right && " Not update operation." );
251
276
MapImpl.insert ({Left, Right});
252
277
}
@@ -2029,9 +2054,10 @@ class SequentialNodeMatcher : public NodeMatcher {
2029
2054
if (L && R && *L == *R)
2030
2055
continue ;
2031
2056
if (!L || !R)
2032
- Listener.foundRemoveAddMatch (L, R);
2057
+ Listener.foundMatch (L, R,
2058
+ L ? NodeMatchReason::Removed : NodeMatchReason::Added);
2033
2059
else
2034
- Listener.foundMatch (L, R);
2060
+ Listener.foundMatch (L, R, NodeMatchReason::Sequential );
2035
2061
}
2036
2062
}
2037
2063
};
@@ -2045,6 +2071,7 @@ class BestMatchMatcher : public NodeMatcher {
2045
2071
NodeVector &Right;
2046
2072
llvm::function_ref<bool (NodePtr, NodePtr)> CanMatch;
2047
2073
llvm::function_ref<bool (NodeMatch, NodeMatch)> IsFirstMatchBetter;
2074
+ NodeMatchReason Reason;
2048
2075
MatchedNodeListener &Listener;
2049
2076
llvm::SmallPtrSet<NodePtr, 16 > MatchedRight;
2050
2077
@@ -2068,16 +2095,17 @@ class BestMatchMatcher : public NodeMatcher {
2068
2095
BestMatchMatcher (NodeVector &Left, NodeVector &Right,
2069
2096
llvm::function_ref<bool (NodePtr, NodePtr)> CanMatch,
2070
2097
llvm::function_ref<bool (NodeMatch, NodeMatch)> IsFirstMatchBetter,
2098
+ NodeMatchReason Reason,
2071
2099
MatchedNodeListener &Listener) : Left(Left), Right(Right),
2072
2100
CanMatch (CanMatch),
2073
- IsFirstMatchBetter(IsFirstMatchBetter),
2101
+ IsFirstMatchBetter(IsFirstMatchBetter), Reason(Reason),
2074
2102
Listener(Listener){}
2075
2103
2076
2104
void match () override {
2077
2105
for (auto L : Left) {
2078
2106
if (auto Best = findBestMatch (L, Right)) {
2079
2107
MatchedRight.insert (Best.getValue ());
2080
- Listener.foundMatch (L, Best.getValue ());
2108
+ Listener.foundMatch (L, Best.getValue (), Reason );
2081
2109
}
2082
2110
}
2083
2111
}
@@ -2096,9 +2124,9 @@ class RemovedAddedNodeMatcher : public NodeMatcher, public MatchedNodeListener {
2096
2124
if (contains (Matched, A))
2097
2125
continue ;
2098
2126
if (Left)
2099
- Listener.foundRemoveAddMatch (A, nullptr );
2127
+ Listener.foundMatch (A, nullptr , NodeMatchReason::Removed );
2100
2128
else
2101
- Listener.foundRemoveAddMatch (nullptr , A);
2129
+ Listener.foundMatch (nullptr , A, NodeMatchReason::Added );
2102
2130
}
2103
2131
}
2104
2132
@@ -2117,7 +2145,7 @@ class RemovedAddedNodeMatcher : public NodeMatcher, public MatchedNodeListener {
2117
2145
return false ;
2118
2146
}
2119
2147
R->annotate (NodeAnnotation::PropertyName, A->getPrintedName ());
2120
- foundMatch (R, A);
2148
+ foundMatch (R, A, NodeMatchReason::FuncToProperty );
2121
2149
return true ;
2122
2150
}
2123
2151
}
@@ -2157,7 +2185,7 @@ class RemovedAddedNodeMatcher : public NodeMatcher, public MatchedNodeListener {
2157
2185
Child->getName ()).str ();
2158
2186
R->annotate (NodeAnnotation::ModernizeEnum,
2159
2187
R->getSDKContext ().buffer (FullName));
2160
- foundMatch (R, A);
2188
+ foundMatch (R, A, NodeMatchReason::ModernizeEnum );
2161
2189
return true ;
2162
2190
}
2163
2191
}
@@ -2171,7 +2199,7 @@ class RemovedAddedNodeMatcher : public NodeMatcher, public MatchedNodeListener {
2171
2199
auto LastR = getLastPartOfUsr (R);
2172
2200
auto LastA = getLastPartOfUsr (A);
2173
2201
if (LastR && LastA && LastR.getValue () == LastA.getValue ()) {
2174
- foundMatch (R, A);
2202
+ foundMatch (R, A, NodeMatchReason::Name );
2175
2203
return true ;
2176
2204
}
2177
2205
return false ;
@@ -2249,8 +2277,8 @@ class RemovedAddedNodeMatcher : public NodeMatcher, public MatchedNodeListener {
2249
2277
#undef DIST
2250
2278
}
2251
2279
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 );
2254
2282
RemovedMatched.push_back (R);
2255
2283
AddedMatched.push_back (A);
2256
2284
}
@@ -2277,7 +2305,6 @@ class RemovedAddedNodeMatcher : public NodeMatcher, public MatchedNodeListener {
2277
2305
NodeVector RenameLeft;
2278
2306
NodeVector RenameRight;
2279
2307
2280
-
2281
2308
for (auto Remain : Removed) {
2282
2309
if (!contains (RemovedMatched, Remain))
2283
2310
RenameLeft.push_back (Remain);
@@ -2289,7 +2316,7 @@ class RemovedAddedNodeMatcher : public NodeMatcher, public MatchedNodeListener {
2289
2316
}
2290
2317
2291
2318
BestMatchMatcher RenameMatcher (RenameLeft, RenameRight, isRename,
2292
- isBetterMatch, *this );
2319
+ isBetterMatch, NodeMatchReason::Name, *this );
2293
2320
RenameMatcher.match ();
2294
2321
// Rename detection ends.
2295
2322
@@ -2393,7 +2420,7 @@ void SameNameNodeMatcher::match() {
2393
2420
// match kind list.
2394
2421
if (auto Match = findBestNameMatch (Candidates,
2395
2422
getNameMatchKindPriority (LN->getKind ()))) {
2396
- Listener.foundMatch (LN, Match);
2423
+ Listener.foundMatch (LN, Match, NodeMatchReason::Name );
2397
2424
MatchedRight.push_back (Match);
2398
2425
} else {
2399
2426
Removed.push_back (LN);
@@ -2416,7 +2443,7 @@ class SequentialRecursiveMatcher : public NodeMatcher {
2416
2443
MatchedNodeListener &Listener;
2417
2444
2418
2445
void matchInternal (NodePtr L, NodePtr R) {
2419
- Listener.foundMatch (L, R);
2446
+ Listener.foundMatch (L, R, NodeMatchReason::Sequential );
2420
2447
if (!L || !R)
2421
2448
return ;
2422
2449
for (unsigned I = 0 ; I < std::max (L->getChildrenCount (),
@@ -2525,25 +2552,31 @@ class PrunePass : public MatchedNodeListener, public SDKTreeDiffPass {
2525
2552
public:
2526
2553
PrunePass (UpdatedNodesMap &UpdateMap) : UpdateMap(UpdateMap) {}
2527
2554
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);
2530
2559
Right->annotate (NodeAnnotation::Added);
2531
- else if (!Right) {
2560
+ return ;
2561
+ case NodeMatchReason::Removed:
2562
+ assert (!Right);
2532
2563
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:
2536
2567
Left->annotate (NodeAnnotation::Removed);
2537
2568
Right->annotate (NodeAnnotation::Added);
2569
+ return ;
2570
+ case NodeMatchReason::Root:
2571
+ case NodeMatchReason::Name:
2572
+ case NodeMatchReason::Sequential:
2573
+ break ;
2538
2574
}
2539
- }
2540
-
2541
- void foundMatch (NodePtr Left, NodePtr Right) override {
2542
2575
assert (Left && Right);
2543
2576
Left->annotate (NodeAnnotation::Updated);
2544
2577
Right->annotate (NodeAnnotation::Updated);
2545
2578
// Push the updated node to the map for future reference.
2546
- UpdateMap.foundMatch (Left, Right);
2579
+ UpdateMap.insert (Left, Right);
2547
2580
2548
2581
if (Left->getKind () != Right->getKind ()) {
2549
2582
assert (isa<SDKNodeType>(Left) && isa<SDKNodeType>(Right) &&
@@ -2589,14 +2622,14 @@ class PrunePass : public MatchedNodeListener, public SDKTreeDiffPass {
2589
2622
auto LC = Left->getChildren ()[0 ];
2590
2623
auto RC = Right->getChildren ()[0 ];
2591
2624
if (!(*LC == *RC))
2592
- foundMatch (LC, RC);
2625
+ foundMatch (LC, RC, NodeMatchReason::Sequential );
2593
2626
break ;
2594
2627
}
2595
2628
}
2596
2629
}
2597
2630
2598
2631
void pass (NodePtr Left, NodePtr Right) override {
2599
- foundMatch (Left, Right);
2632
+ foundMatch (Left, Right, NodeMatchReason::Root );
2600
2633
}
2601
2634
};
2602
2635
@@ -3762,7 +3795,7 @@ class RenameDetectorForMemberDiff : public MatchedNodeListener {
3762
3795
InterfaceTypeChangeDetector RightDetector;
3763
3796
public:
3764
3797
RenameDetectorForMemberDiff (): LeftDetector(true ), RightDetector(false ) {}
3765
- void foundMatch (NodePtr Left, NodePtr Right) override {
3798
+ void foundMatch (NodePtr Left, NodePtr Right, NodeMatchReason Reason ) override {
3766
3799
detectRename (Left, Right);
3767
3800
LeftDetector.detect (Left, Right);
3768
3801
RightDetector.detect (Right, Left);
0 commit comments