@@ -277,6 +277,11 @@ enum class KnownTypeKind: uint8_t {
277
277
Unknown,
278
278
};
279
279
280
+ enum class KnownProtocolKind : uint8_t {
281
+ #define KNOWN_PROTOCOL (NAME ) NAME,
282
+ #include " swift/IDE/DigesterEnums.def"
283
+ };
284
+
280
285
enum class SDKDeclAttrKind : uint8_t {
281
286
#define DECL_ATTR (Name ) DAK_##Name,
282
287
#include " swift/IDE/DigesterEnums.def"
@@ -378,11 +383,11 @@ class SDKNode {
378
383
void removeChild (ChildIt CI) { Children.erase (CI); }
379
384
ChildIt getChildBegin () { return Children.begin (); }
380
385
void annotate (NodeAnnotation Anno) { Annotations.insert (Anno); }
386
+ void annotate (NodeAnnotation Anno, StringRef Comment);
381
387
NodePtr getParent () const { return Parent; };
382
388
unsigned getChildrenCount () const { return Children.size (); }
383
389
NodePtr childAt (unsigned I) const ;
384
390
void removeChild (NodePtr C);
385
- void addAnnotateComment (NodeAnnotation Anno, StringRef Comment);
386
391
StringRef getAnnotateComment (NodeAnnotation Anno) const ;
387
392
bool isAnnotatedAs (NodeAnnotation Anno) const ;
388
393
void addChild (SDKNode *Child);
@@ -598,8 +603,9 @@ void SDKNode::removeChild(NodePtr C) {
598
603
Children.erase (std::find (Children.begin (), Children.end (), C));
599
604
}
600
605
601
- void SDKNode::addAnnotateComment (NodeAnnotation Anno, StringRef Comment) {
602
- assert (isAnnotatedAs (Anno) && " Cannot find annotation" );
606
+ void SDKNode::annotate (NodeAnnotation Anno, StringRef Comment) {
607
+ assert (!isAnnotatedAs (Anno) && " already annotated" );
608
+ annotate (Anno);
603
609
AnnotateComments[Anno] = Comment;
604
610
}
605
611
@@ -831,6 +837,18 @@ class SDKNodeTypeDecl : public SDKNodeDecl {
831
837
}
832
838
return None;
833
839
}
840
+
841
+ bool isConformingTo (KnownProtocolKind Kind) const {
842
+ StringRef Usr;
843
+ switch (Kind) {
844
+ #define KNOWN_PROTOCOL (NAME ) \
845
+ case KnownProtocolKind::NAME: \
846
+ return std::find (ConformingProtocols.begin (), \
847
+ ConformingProtocols.end (), \
848
+ #NAME) != ConformingProtocols.end ();
849
+ #include " swift/IDE/DigesterEnums.def"
850
+ }
851
+ }
834
852
};
835
853
836
854
class SDKNodeTypeAlias : public SDKNodeDecl {
@@ -1957,8 +1975,7 @@ class RemovedAddedNodeMatcher : public NodeMatcher, public MatchedNodeListener {
1957
1975
} else {
1958
1976
return false ;
1959
1977
}
1960
- R->annotate (NodeAnnotation::PropertyName);
1961
- R->addAnnotateComment (NodeAnnotation::PropertyName, A->getPrintedName ());
1978
+ R->annotate (NodeAnnotation::PropertyName, A->getPrintedName ());
1962
1979
foundMatch (R, A);
1963
1980
return true ;
1964
1981
}
@@ -1995,11 +2012,10 @@ class RemovedAddedNodeMatcher : public NodeMatcher, public MatchedNodeListener {
1995
2012
if (auto VC = dyn_cast<SDKNodeVar>(Child)) {
1996
2013
auto LastPartOfA = getLastPartOfUsr (VC);
1997
2014
if (LastPartOfA && LastPartOfR.getValue () == LastPartOfA.getValue ()) {
1998
- R->annotate (NodeAnnotation::ModernizeEnum);
1999
2015
std::string FullName = (llvm::Twine (A->getName ()) + " ." +
2000
2016
Child->getName ()).str ();
2001
- R->addAnnotateComment (NodeAnnotation::ModernizeEnum,
2002
- R->getSDKContext ().buffer (FullName));
2017
+ R->annotate (NodeAnnotation::ModernizeEnum,
2018
+ R->getSDKContext ().buffer (FullName));
2003
2019
foundMatch (R, A);
2004
2020
return true ;
2005
2021
}
@@ -2304,10 +2320,8 @@ static void detectRename(NodePtr L, NodePtr R) {
2304
2320
assert (L->getKind () == R->getKind ());
2305
2321
if (isa<SDKNodeDecl>(L) && L->getPrintedName () != R->getPrintedName ()) {
2306
2322
L->annotate (NodeAnnotation::Rename);
2307
- L->annotate (NodeAnnotation::RenameOldName);
2308
- L->addAnnotateComment (NodeAnnotation::RenameOldName, L->getPrintedName ());
2309
- L->annotate (NodeAnnotation::RenameNewName);
2310
- L->addAnnotateComment (NodeAnnotation::RenameNewName, R->getPrintedName ());
2323
+ L->annotate (NodeAnnotation::RenameOldName, L->getPrintedName ());
2324
+ L->annotate (NodeAnnotation::RenameNewName, R->getPrintedName ());
2311
2325
}
2312
2326
}
2313
2327
@@ -2477,12 +2491,10 @@ class TypeMemberDiffFinder : public SDKNodeVisitor {
2477
2491
diffNode->getKind () == SDKNodeKind::Function &&
2478
2492
node->isNameValid ()) {
2479
2493
diffNode->annotate (NodeAnnotation::Rename);
2480
- diffNode->annotate (NodeAnnotation::RenameOldName);
2481
- diffNode->addAnnotateComment (NodeAnnotation::RenameOldName,
2482
- diffNode->getPrintedName ());
2483
- diffNode->annotate (NodeAnnotation::RenameNewName);
2484
- diffNode->addAnnotateComment (NodeAnnotation::RenameNewName,
2485
- node->getParent ()->getPrintedName ());
2494
+ diffNode->annotate (NodeAnnotation::RenameOldName,
2495
+ diffNode->getPrintedName ());
2496
+ diffNode->annotate (NodeAnnotation::RenameNewName,
2497
+ node->getParent ()->getPrintedName ());
2486
2498
}
2487
2499
}
2488
2500
@@ -2587,17 +2599,45 @@ class ChangeRefinementPass : public SDKTreeDiffPass, public SDKNodeVisitor {
2587
2599
(Node->getName () != Counter->getName ()||
2588
2600
Node->getChildrenCount () != Counter->getChildrenCount ())) {
2589
2601
Node->annotate (NodeAnnotation::TypeRewritten);
2590
- Node->annotate (NodeAnnotation::TypeRewrittenLeft);
2591
- Node->annotate (NodeAnnotation::TypeRewrittenRight);
2592
- Node->addAnnotateComment (NodeAnnotation::TypeRewrittenLeft,
2593
- Node->getPrintedName ());
2594
- Node->addAnnotateComment (NodeAnnotation::TypeRewrittenRight,
2595
- Counter->getPrintedName ());
2602
+ Node->annotate (NodeAnnotation::TypeRewrittenLeft, Node->getPrintedName ());
2603
+ Node->annotate (NodeAnnotation::TypeRewrittenRight,
2604
+ Counter->getPrintedName ());
2596
2605
return true ;
2597
2606
}
2598
2607
return false ;
2599
2608
}
2600
2609
2610
+ bool detectDictionaryKeyChange (SDKNodeType *L, SDKNodeType *R) {
2611
+ if (!IsVisitingLeft)
2612
+ return false ;
2613
+ if (L->getTypeKind () != KnownTypeKind::Dictionary ||
2614
+ R->getTypeKind () != KnownTypeKind::Dictionary)
2615
+ return false ;
2616
+ auto *Left = dyn_cast<SDKNodeTypeNominal>(L);
2617
+ auto *Right = dyn_cast<SDKNodeTypeNominal>(R);
2618
+ assert (Left && Right);
2619
+ assert (Left->getChildrenCount () == 2 );
2620
+ assert (Right->getChildrenCount () == 2 );
2621
+ auto * LKey = dyn_cast<SDKNodeTypeNominal>(*Left->getChildBegin ());
2622
+ auto * RKey = dyn_cast<SDKNodeTypeNominal>(*Right->getChildBegin ());
2623
+ if (!LKey || !RKey)
2624
+ return false ;
2625
+ if (LKey->getTypeKind () != KnownTypeKind::String)
2626
+ return false ;
2627
+ auto Results = RKey->getRootNode ()->getDescendantsByUsr (RKey->getUsr ());
2628
+ if (Results.empty ())
2629
+ return false ;
2630
+ if (auto DT = dyn_cast<SDKNodeTypeDecl>(Results.front ())) {
2631
+ if (DT->isConformingTo (KnownProtocolKind::RawRepresentable)) {
2632
+ L->annotate (NodeAnnotation::DictionaryKeyUpdate);
2633
+ L->annotate (NodeAnnotation::TypeRewrittenRight,
2634
+ DT->getFullyQualifiedName ());
2635
+ return true ;
2636
+ }
2637
+ }
2638
+ return false ;
2639
+ }
2640
+
2601
2641
bool isUnhandledCase (SDKNodeType *Node) {
2602
2642
auto Counter = UpdateMap.findUpdateCounterpart (Node)->getAs <SDKNodeType>();
2603
2643
return Node->getTypeKind () == KnownTypeKind::Void ||
@@ -2628,6 +2668,7 @@ class ChangeRefinementPass : public SDKTreeDiffPass, public SDKNodeVisitor {
2628
2668
detectOptionalUpdate (Node, Counter)||
2629
2669
detectWrapImplicitOptional (Node, Counter)||
2630
2670
detectUnmanagedUpdate (Node, Counter)||
2671
+ detectDictionaryKeyChange (Node, Counter) ||
2631
2672
detectTypeRewritten (Node, Counter);
2632
2673
(void ) Result;
2633
2674
return ;
@@ -2739,6 +2780,7 @@ class DiffItemEmitter : public SDKNodeVisitor {
2739
2780
2740
2781
static StringRef getRightComment (NodePtr Node, NodeAnnotation Anno) {
2741
2782
switch (Anno) {
2783
+ case NodeAnnotation::DictionaryKeyUpdate:
2742
2784
case NodeAnnotation::TypeRewritten:
2743
2785
return Node->getAnnotateComment (NodeAnnotation::TypeRewrittenRight);
2744
2786
case NodeAnnotation::ModernizeEnum:
@@ -2795,7 +2837,8 @@ class DiffItemEmitter : public SDKNodeVisitor {
2795
2837
NodeAnnotation::GetterToProperty,
2796
2838
NodeAnnotation::ModernizeEnum,
2797
2839
NodeAnnotation::Rename,
2798
- NodeAnnotation::NowThrowing
2840
+ NodeAnnotation::NowThrowing,
2841
+ NodeAnnotation::DictionaryKeyUpdate,
2799
2842
});
2800
2843
}
2801
2844
0 commit comments