@@ -722,13 +722,15 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
722
722
return false ;
723
723
std::string Rename;
724
724
Optional<NodeAnnotation> Kind;
725
+ StringRef LeftComment;
725
726
StringRef RightComment;
726
727
for (auto *Item: getRelatedDiffItems (RD)) {
727
728
if (isSimpleReplacement (Item, Rename)) {
728
729
} else if (auto *CI = dyn_cast<CommonDiffItem>(Item)) {
729
730
if (CI->isStringRepresentableChange () &&
730
731
CI->NodeKind == SDKNodeKind::DeclVar) {
731
732
Kind = CI->DiffKind ;
733
+ LeftComment = CI->LeftComment ;
732
734
RightComment = CI->RightComment ;
733
735
}
734
736
}
@@ -737,7 +739,8 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
737
739
return false ;
738
740
if (Kind && !isNilExpr (WrapperTarget)) {
739
741
SmallString<256 > Buffer;
740
- auto Func = insertHelperFunction (*Kind, RightComment, Buffer, FromString);
742
+ auto Func = insertHelperFunction (*Kind, LeftComment, RightComment, Buffer,
743
+ FromString);
741
744
Editor.insert (WrapperTarget->getStartLoc (), (Twine (Func) + " (" ).str ());
742
745
Editor.insertAfterToken (WrapperTarget->getEndLoc (), " )" );
743
746
}
@@ -768,7 +771,8 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
768
771
return wrapAttributeReference (E, E, false );
769
772
}
770
773
771
- StringRef insertHelperFunction (NodeAnnotation Anno, StringRef NewType,
774
+ StringRef insertHelperFunction (NodeAnnotation Anno, StringRef RawType,
775
+ StringRef NewType,
772
776
SmallString<256 > &Buffer, bool FromString) {
773
777
llvm::raw_svector_ostream OS (Buffer);
774
778
OS << " \n " ;
@@ -780,13 +784,13 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
780
784
StringRef guard = " \t guard let input = input else { return nil }\n " ;
781
785
switch (Anno) {
782
786
case NodeAnnotation::OptionalArrayMemberUpdate:
783
- Segs = {" Optional" , " Array" , " [String ]?" };
787
+ Segs = {" Optional" , " Array" , ( Twine ( " [ " ) + RawType + " ]?" ). str () };
784
788
Segs.push_back ((Twine (" [" ) + NewType +" ]?" ).str ());
785
789
Segs.push_back ((Twine (guard) + " \t return input.map { key in " + NewType +" (key) }" ).str ());
786
790
Segs.push_back ((Twine (guard) + " \t return input.map { key in key.rawValue }" ).str ());
787
791
break ;
788
792
case NodeAnnotation::OptionalDictionaryKeyUpdate:
789
- Segs = {" Optional" , " Dictionary" , " [String : Any]?" };
793
+ Segs = {" Optional" , " Dictionary" , ( Twine ( " [ " ) + RawType + " : Any]?" ). str () };
790
794
Segs.push_back ((Twine (" [" ) + NewType +" : Any]?" ).str ());
791
795
Segs.push_back ((Twine (guard) +
792
796
" \t return Dictionary(uniqueKeysWithValues: input.map"
@@ -796,27 +800,27 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
796
800
" {key, value in (key.rawValue, value)})" ).str ());
797
801
break ;
798
802
case NodeAnnotation::ArrayMemberUpdate:
799
- Segs = {" " , " Array" , " [String] " };
803
+ Segs = {" " , " Array" , ( Twine ( " [ " ) + RawType + " ] " ). str () };
800
804
Segs.push_back ((Twine (" [" ) + NewType +" ]" ).str ());
801
805
Segs.push_back ((Twine (" \t return input.map { key in " ) + NewType +" (key) }" ).str ());
802
806
Segs.push_back (" \t return input.map { key in key.rawValue }" );
803
807
break ;
804
808
case NodeAnnotation::DictionaryKeyUpdate:
805
- Segs = {" " , " Dictionary" , " [String : Any]" };
809
+ Segs = {" " , " Dictionary" , ( Twine ( " [ " ) + RawType + " : Any]" ). str () };
806
810
Segs.push_back ((Twine (" [" ) + NewType +" : Any]" ).str ());
807
811
Segs.push_back ((Twine (" \t return Dictionary(uniqueKeysWithValues: input.map"
808
812
" { key, value in (" ) + NewType + " (rawValue: key), value)})" ).str ());
809
813
Segs.push_back (" \t return Dictionary(uniqueKeysWithValues: input.map"
810
814
" {key, value in (key.rawValue, value)})" );
811
815
break ;
812
816
case NodeAnnotation::SimpleStringRepresentableUpdate:
813
- Segs = {" " , " " , " String " };
817
+ Segs = {" " , " " , RawType };
814
818
Segs.push_back (NewType);
815
819
Segs.push_back ((Twine (" \t return " ) + NewType + " (rawValue: input)" ).str ());
816
820
Segs.push_back (" \t return input.rawValue" );
817
821
break ;
818
822
case NodeAnnotation::SimpleOptionalStringRepresentableUpdate:
819
- Segs = {" Optional" , " " , " String? " };
823
+ Segs = {" Optional" , " " , ( Twine (RawType) + " ? " ). str () };
820
824
Segs.push_back ((Twine (NewType) +" ?" ).str ());
821
825
Segs.push_back ((Twine (guard) + " \t return " + NewType + " (rawValue: input)" ).str ());
822
826
Segs.push_back ((Twine (guard) + " \t return input.rawValue" ).str ());
@@ -850,12 +854,14 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
850
854
Editor.disableCache ();
851
855
SWIFT_DEFER { Editor.enableCache (); };
852
856
NodeAnnotation Kind;
857
+ StringRef RawType;
853
858
StringRef NewAttributeType;
854
859
uint8_t ArgIdx;
855
860
for (auto Item: getRelatedDiffItems (FD)) {
856
861
if (auto *CI = dyn_cast<CommonDiffItem>(Item)) {
857
862
if (CI->isStringRepresentableChange ()) {
858
863
Kind = CI->DiffKind ;
864
+ RawType = CI->LeftComment ;
859
865
NewAttributeType = CI->RightComment ;
860
866
assert (CI->getChildIndices ().size () == 1 );
861
867
ArgIdx = CI->getChildIndices ().front ();
@@ -880,7 +886,7 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
880
886
}
881
887
assert (WrapTarget);
882
888
SmallString<256 > Buffer;
883
- auto FuncName = insertHelperFunction (Kind, NewAttributeType, Buffer,
889
+ auto FuncName = insertHelperFunction (Kind, RawType, NewAttributeType, Buffer,
884
890
FromString);
885
891
Editor.insert (WrapTarget->getStartLoc (), (Twine (FuncName) + " (" ).str ());
886
892
Editor.insertAfterToken (WrapTarget->getEndLoc (), " )" );
0 commit comments