@@ -394,7 +394,7 @@ class SDKNodeDecl : public SDKNode {
394
394
395
395
class SDKNodeRoot :public SDKNode {
396
396
// / This keeps track of all decl descendants with USRs.
397
- llvm::StringMap<SDKNodeDecl*> DescendantDeclTable;
397
+ llvm::StringMap<llvm::SmallSetVector< SDKNodeDecl*, 2 > > DescendantDeclTable;
398
398
399
399
public:
400
400
SDKNodeRoot (SDKNodeInitInfo Info) : SDKNode(Info, SDKNodeKind::Root) {}
@@ -403,13 +403,11 @@ class SDKNodeRoot :public SDKNode {
403
403
void registerDescendant (SDKNode *D) {
404
404
if (auto DD = dyn_cast<SDKNodeDecl>(D)) {
405
405
assert (!DD->getUsr ().empty ());
406
- DescendantDeclTable[DD->getUsr ()] = DD ;
406
+ DescendantDeclTable[DD->getUsr ()]. insert (DD) ;
407
407
}
408
408
}
409
- Optional<SDKNodeDecl*> getDescendantByUsr (StringRef Usr) {
410
- if (DescendantDeclTable.count (Usr))
411
- return DescendantDeclTable[Usr];
412
- return None;
409
+ ArrayRef<SDKNodeDecl*> getDescendantsByUsr (StringRef Usr) {
410
+ return DescendantDeclTable[Usr].getArrayRef ();
413
411
}
414
412
};
415
413
@@ -729,8 +727,9 @@ class SDKNodeTypeDecl : public SDKNodeDecl {
729
727
Optional<SDKNodeTypeDecl*> getSuperclass () const {
730
728
if (SuperclassUsr.empty ())
731
729
return None;
732
- if (auto SC = getRootNode ()->getDescendantByUsr (SuperclassUsr)) {
733
- return (*SC)->getAs <SDKNodeTypeDecl>();
730
+ auto Descendants = getRootNode ()->getDescendantsByUsr (SuperclassUsr);
731
+ if (!Descendants.empty ()) {
732
+ return Descendants.front ()->getAs <SDKNodeTypeDecl>();
734
733
}
735
734
return None;
736
735
}
@@ -2197,43 +2196,13 @@ class PrunePass : public MatchedNodeListener, public SDKTreeDiffPass {
2197
2196
}
2198
2197
};
2199
2198
2200
- // For a given SDK node tree, this will build up a mapping from USR to node
2201
- using USRToNodeMap = llvm::StringMap<NodePtr, llvm::BumpPtrAllocator>;
2202
-
2203
- // Class to build up mappings from USR to SDKNode
2204
- class MapUSRToNode : public SDKNodeVisitor {
2205
- friend class SDKNode ; // for visit()
2206
- USRToNodeMap usrMap;
2207
-
2208
- void visit (NodePtr ptr) override {
2209
- if (auto D = dyn_cast<SDKNodeDecl>(ptr)) {
2210
- usrMap[D->getUsr ()] = ptr;
2211
- }
2212
- }
2213
-
2214
- public:
2215
- MapUSRToNode () = default ;
2216
-
2217
- const USRToNodeMap &getMap () const { return usrMap; }
2218
-
2219
- void map (NodePtr ptr) {
2220
- SDKNode::preorderVisit (ptr, *this );
2221
- }
2222
-
2223
- void dump (llvm::raw_ostream &) const ;
2224
- void dump () const { dump (llvm::errs ()); }
2225
-
2226
- private:
2227
- MapUSRToNode (MapUSRToNode &) = delete ;
2228
- MapUSRToNode &operator =(MapUSRToNode &) = delete ;
2229
- };
2230
2199
2231
2200
// Class to build up a diff of structurally different nodes, based on the given
2232
2201
// USR map for the left (original) side of the diff, based on parent types.
2233
2202
class TypeMemberDiffFinder : public SDKNodeVisitor {
2234
2203
friend class SDKNode ; // for visit()
2235
2204
2236
- const USRToNodeMap & diffAgainst;
2205
+ SDKNodeRoot * diffAgainst;
2237
2206
2238
2207
// Vector of {givenNodePtr, diffAgainstPtr}
2239
2208
NodePairVector TypeMemberDiffs;
@@ -2245,10 +2214,22 @@ class TypeMemberDiffFinder : public SDKNodeVisitor {
2245
2214
return ;
2246
2215
auto usr = declNode->getUsr ();
2247
2216
auto &usrName = usr;
2248
- if (!diffAgainst.count (usrName))
2217
+
2218
+ // If we can find no nodes in the other tree with the same usr, abort.
2219
+ auto candidates = diffAgainst->getDescendantsByUsr (usrName);
2220
+ if (candidates.empty ())
2249
2221
return ;
2250
2222
2251
- auto diffNode = diffAgainst.lookup (usrName);
2223
+ // If any of the candidates has the same kind and name with the node, we
2224
+ // shouldn't continue.
2225
+ for (auto Can : candidates) {
2226
+ if (Can->getKind () == declNode->getKind () &&
2227
+ Can->getAs <SDKNodeDecl>()->getFullyQualifiedName () ==
2228
+ declNode->getFullyQualifiedName ())
2229
+ return ;
2230
+ }
2231
+
2232
+ auto diffNode = candidates.front ();
2252
2233
assert (node && diffNode && " nullptr visited?" );
2253
2234
auto nodeParent = node->getParent ();
2254
2235
auto diffParent = diffNode->getParent ();
@@ -2261,9 +2242,7 @@ class TypeMemberDiffFinder : public SDKNodeVisitor {
2261
2242
// Move from a member variable to another member variable
2262
2243
if (nodeParent->getKind () == SDKNodeKind::TypeDecl &&
2263
2244
diffParent->getKind () == SDKNodeKind::TypeDecl &&
2264
- declNode->isStatic () &&
2265
- nodeParent->getAs <SDKNodeDecl>()->getFullyQualifiedName () !=
2266
- diffParent->getAs <SDKNodeDecl>()->getFullyQualifiedName ())
2245
+ declNode->isStatic ())
2267
2246
TypeMemberDiffs.insert ({diffNode, node});
2268
2247
// Move from a getter/setter function to a property
2269
2248
else if (node->getKind () == SDKNodeKind::Getter &&
@@ -2280,8 +2259,8 @@ class TypeMemberDiffFinder : public SDKNodeVisitor {
2280
2259
}
2281
2260
2282
2261
public:
2283
- TypeMemberDiffFinder (const USRToNodeMap &rightUSRMap)
2284
- : diffAgainst(rightUSRMap ) {}
2262
+ TypeMemberDiffFinder (SDKNodeRoot *diffAgainst):
2263
+ diffAgainst (diffAgainst ) {}
2285
2264
2286
2265
void findDiffsFor (NodePtr ptr) { SDKNode::preorderVisit (ptr, *this ); }
2287
2266
@@ -2446,19 +2425,6 @@ static void printNode(llvm::raw_ostream &os, NodePtr node) {
2446
2425
os << " }" ;
2447
2426
}
2448
2427
2449
- void MapUSRToNode::dump (llvm::raw_ostream &os) const {
2450
- for (auto &elt : usrMap) {
2451
- auto &node = elt.getValue ();
2452
- os << elt.getKey () << " ==> " ;
2453
- printNode (os, node);
2454
- if (node->getParent ()) {
2455
- os << " parent: " ;
2456
- printNode (os, node->getParent ());
2457
- }
2458
- os << " \n " ;
2459
- }
2460
- }
2461
-
2462
2428
void TypeMemberDiffFinder::dump (llvm::raw_ostream &os) const {
2463
2429
for (auto pair : getDiffs ()) {
2464
2430
os << " - " ;
@@ -3102,12 +3068,7 @@ static Optional<uint8_t> findSelfIndex(SDKNode* Node) {
3102
3068
// / Find cases where a diff is due to a change to being a type member
3103
3069
static void findTypeMemberDiffs (NodePtr leftSDKRoot, NodePtr rightSDKRoot,
3104
3070
TypeMemberDiffVector &out) {
3105
- // Mapping from USR to SDKNode
3106
- MapUSRToNode leftMapper;
3107
- leftMapper.map (leftSDKRoot);
3108
- auto &leftMap = leftMapper.getMap ();
3109
-
3110
- TypeMemberDiffFinder diffFinder (leftMap);
3071
+ TypeMemberDiffFinder diffFinder (cast<SDKNodeRoot>(leftSDKRoot));
3111
3072
diffFinder.findDiffsFor (rightSDKRoot);
3112
3073
RenameDetectorForMemberDiff Detector;
3113
3074
for (auto pair : diffFinder.getDiffs ()) {
0 commit comments