@@ -247,6 +247,12 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
247
247
248
248
APIDiffItemStore DiffStore;
249
249
250
+ bool isNilExpr (Expr *E) {
251
+ auto Range = E->getSourceRange ();
252
+ return Range.isValid () && Lexer::getCharSourceRangeFromSourceRange (
253
+ SF->getASTContext ().SourceMgr , Range).str () == " nil" ;
254
+ }
255
+
250
256
std::vector<APIDiffItem*> getRelatedDiffItems (ValueDecl *VD) {
251
257
std::vector<APIDiffItem*> results;
252
258
auto addDiffItems = [&](ValueDecl *VD) {
@@ -266,7 +272,8 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
266
272
return results;
267
273
}
268
274
269
- DeclNameViewer getFuncRename (ValueDecl *VD, bool &IgnoreBase) {
275
+ DeclNameViewer getFuncRename (ValueDecl *VD, llvm::SmallString<32 > &Buffer,
276
+ bool &IgnoreBase) {
270
277
for (auto *Item: getRelatedDiffItems (VD)) {
271
278
if (auto *CI = dyn_cast<CommonDiffItem>(Item)) {
272
279
if (CI->isRename ()) {
@@ -282,6 +289,13 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
282
289
}
283
290
}
284
291
}
292
+ if (auto *MI = dyn_cast<TypeMemberDiffItem>(Item)) {
293
+ if (MI->Subkind == TypeMemberDiffItemSubKind::FuncRename) {
294
+ llvm::raw_svector_ostream OS (Buffer);
295
+ OS << MI->newTypeName << " ." << MI->newPrintedName ;
296
+ return DeclNameViewer (OS.str ());
297
+ }
298
+ }
285
299
}
286
300
return DeclNameViewer ();
287
301
}
@@ -413,7 +427,8 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
413
427
414
428
void handleFuncRename (ValueDecl *FD, Expr* FuncRefContainer, Expr *Arg) {
415
429
bool IgnoreBase = false ;
416
- if (auto View = getFuncRename (FD, IgnoreBase)) {
430
+ llvm::SmallString<32 > Buffer;
431
+ if (auto View = getFuncRename (FD, Buffer, IgnoreBase)) {
417
432
if (!IgnoreBase) {
418
433
ReferenceCollector Walker (FD);
419
434
Walker.walk (FuncRefContainer);
@@ -575,7 +590,8 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
575
590
if (!Item)
576
591
return false ;
577
592
if (Item->Subkind == TypeMemberDiffItemSubKind::SimpleReplacement ||
578
- Item->Subkind == TypeMemberDiffItemSubKind::QualifiedReplacement)
593
+ Item->Subkind == TypeMemberDiffItemSubKind::QualifiedReplacement ||
594
+ Item->Subkind == TypeMemberDiffItemSubKind::FuncRename)
579
595
return false ;
580
596
581
597
if (Item->Subkind == TypeMemberDiffItemSubKind::GlobalFuncToStaticProperty) {
@@ -624,6 +640,7 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
624
640
}
625
641
626
642
switch (Item->Subkind ) {
643
+ case TypeMemberDiffItemSubKind::FuncRename:
627
644
case TypeMemberDiffItemSubKind::GlobalFuncToStaticProperty:
628
645
case TypeMemberDiffItemSubKind::SimpleReplacement:
629
646
case TypeMemberDiffItemSubKind::QualifiedReplacement:
@@ -696,24 +713,27 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
696
713
auto *RD = getReferencedDecl (Reference);
697
714
if (!RD)
698
715
return false ;
699
- std::string Func;
700
716
std::string Rename;
717
+ Optional<NodeAnnotation> Kind;
718
+ StringRef RightComment;
701
719
for (auto *Item: getRelatedDiffItems (RD)) {
702
720
if (isSimpleReplacement (Item, Rename)) {
703
721
} else if (auto *CI = dyn_cast<CommonDiffItem>(Item)) {
704
722
if (CI->isStringRepresentableChange () &&
705
723
CI->NodeKind == SDKNodeKind::DeclVar) {
706
- SmallString<256 > Buffer;
707
- Func = insertHelperFunction (CI->DiffKind , CI->RightComment , Buffer,
708
- FromString);
724
+ Kind = CI->DiffKind ;
725
+ RightComment = CI->RightComment ;
709
726
}
710
727
}
711
728
}
712
- if (Func. empty ())
729
+ if (!Kind. hasValue ())
713
730
return false ;
714
-
715
- Editor.insert (WrapperTarget->getStartLoc (), (Twine (Func) + " (" ).str ());
716
- Editor.insertAfterToken (WrapperTarget->getEndLoc (), " )" );
731
+ if (Kind && !isNilExpr (WrapperTarget)) {
732
+ SmallString<256 > Buffer;
733
+ auto Func = insertHelperFunction (*Kind, RightComment, Buffer, FromString);
734
+ Editor.insert (WrapperTarget->getStartLoc (), (Twine (Func) + " (" ).str ());
735
+ Editor.insertAfterToken (WrapperTarget->getEndLoc (), " )" );
736
+ }
717
737
if (!Rename.empty ()) {
718
738
replaceExpr (Reference, Rename);
719
739
}
@@ -838,21 +858,25 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
838
858
}
839
859
if (NewAttributeType.empty ())
840
860
return ;
841
- SmallString<256 > Buffer;
842
- auto FuncName = insertHelperFunction (Kind, NewAttributeType, Buffer,
843
- /* FromString*/ ArgIdx);
861
+ Expr *WrapTarget = Call;
862
+ bool FromString = false ;
844
863
if (ArgIdx) {
845
864
ArgIdx --;
865
+ FromString = true ;
846
866
auto AllArgs = getCallArgInfo (SM, Arg, LabelRangeEndAt::LabelNameOnly);
847
867
if (AllArgs.size () <= ArgIdx)
848
868
return ;
849
- auto Exp = AllArgs[ArgIdx].ArgExp ;
850
- Editor.insert (Exp->getStartLoc (), (Twine (FuncName) + " (" ).str ());
851
- Editor.insertAfterToken (Exp->getEndLoc (), " )" );
852
- } else {
853
- Editor.insert (Call->getStartLoc (), (Twine (FuncName) + " (" ).str ());
854
- Editor.insertAfterToken (Call->getEndLoc (), " )" );
869
+ WrapTarget = AllArgs[ArgIdx].ArgExp ;
870
+ // Avoid wrapping nil literal.
871
+ if (isNilExpr (WrapTarget))
872
+ return ;
855
873
}
874
+ assert (WrapTarget);
875
+ SmallString<256 > Buffer;
876
+ auto FuncName = insertHelperFunction (Kind, NewAttributeType, Buffer,
877
+ FromString);
878
+ Editor.insert (WrapTarget->getStartLoc (), (Twine (FuncName) + " (" ).str ());
879
+ Editor.insertAfterToken (WrapTarget->getEndLoc (), " )" );
856
880
}
857
881
858
882
bool walkToExprPre (Expr *E) override {
@@ -904,7 +928,8 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
904
928
void handleFuncDeclRename (AbstractFunctionDecl *AFD,
905
929
CharSourceRange NameRange) {
906
930
bool IgnoreBase = false ;
907
- if (auto View = getFuncRename (AFD, IgnoreBase)) {
931
+ llvm::SmallString<32 > Buffer;
932
+ if (auto View = getFuncRename (AFD, Buffer, IgnoreBase)) {
908
933
if (!IgnoreBase)
909
934
Editor.replace (NameRange, View.base ());
910
935
unsigned Index = 0 ;
0 commit comments