Skip to content

Commit 343d84b

Browse files
committed
migrator: handle argument renames when a global function is hoisted to a static member function. rdar://40145590
1 parent 46b8ad3 commit 343d84b

File tree

7 files changed

+33
-7
lines changed

7 files changed

+33
-7
lines changed

include/swift/IDE/APIDigesterData.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ enum class TypeMemberDiffItemSubKind {
247247
HoistSelfOnly,
248248
HoistSelfAndRemoveParam,
249249
HoistSelfAndUseProperty,
250+
FuncRename,
250251
};
251252

252253
struct TypeMemberDiffItem: public APIDiffItem {

lib/IDE/APIDigesterData.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,10 @@ swift::ide::api::TypeMemberDiffItem::getSubKind() const {
148148
assert(OldName.argSize() == 0);
149149
assert(!removedIndex);
150150
return TypeMemberDiffItemSubKind::GlobalFuncToStaticProperty;
151-
} else if (oldTypeName.empty()){
151+
} else if (oldTypeName.empty()) {
152+
// we can handle this as a simple function rename.
152153
assert(NewName.argSize() == OldName.argSize());
153-
return TypeMemberDiffItemSubKind::SimpleReplacement;
154+
return TypeMemberDiffItemSubKind::FuncRename;
154155
} else {
155156
assert(NewName.argSize() == OldName.argSize());
156157
return TypeMemberDiffItemSubKind::QualifiedReplacement;

lib/Migrator/APIDiffMigratorPass.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
266266
return results;
267267
}
268268

269-
DeclNameViewer getFuncRename(ValueDecl *VD, bool &IgnoreBase) {
269+
DeclNameViewer getFuncRename(ValueDecl *VD, llvm::SmallString<32> &Buffer,
270+
bool &IgnoreBase) {
270271
for (auto *Item: getRelatedDiffItems(VD)) {
271272
if (auto *CI = dyn_cast<CommonDiffItem>(Item)) {
272273
if (CI->isRename()) {
@@ -282,6 +283,13 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
282283
}
283284
}
284285
}
286+
if (auto *MI = dyn_cast<TypeMemberDiffItem>(Item)) {
287+
if (MI->Subkind == TypeMemberDiffItemSubKind::FuncRename) {
288+
llvm::raw_svector_ostream OS(Buffer);
289+
OS << MI->newTypeName << "." << MI->newPrintedName;
290+
return DeclNameViewer(OS.str());
291+
}
292+
}
285293
}
286294
return DeclNameViewer();
287295
}
@@ -413,7 +421,8 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
413421

414422
void handleFuncRename(ValueDecl *FD, Expr* FuncRefContainer, Expr *Arg) {
415423
bool IgnoreBase = false;
416-
if (auto View = getFuncRename(FD, IgnoreBase)) {
424+
llvm::SmallString<32> Buffer;
425+
if (auto View = getFuncRename(FD, Buffer, IgnoreBase)) {
417426
if (!IgnoreBase) {
418427
ReferenceCollector Walker(FD);
419428
Walker.walk(FuncRefContainer);
@@ -575,7 +584,8 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
575584
if (!Item)
576585
return false;
577586
if (Item->Subkind == TypeMemberDiffItemSubKind::SimpleReplacement ||
578-
Item->Subkind == TypeMemberDiffItemSubKind::QualifiedReplacement)
587+
Item->Subkind == TypeMemberDiffItemSubKind::QualifiedReplacement ||
588+
Item->Subkind == TypeMemberDiffItemSubKind::FuncRename)
579589
return false;
580590

581591
if (Item->Subkind == TypeMemberDiffItemSubKind::GlobalFuncToStaticProperty) {
@@ -624,6 +634,7 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
624634
}
625635

626636
switch (Item->Subkind) {
637+
case TypeMemberDiffItemSubKind::FuncRename:
627638
case TypeMemberDiffItemSubKind::GlobalFuncToStaticProperty:
628639
case TypeMemberDiffItemSubKind::SimpleReplacement:
629640
case TypeMemberDiffItemSubKind::QualifiedReplacement:
@@ -904,7 +915,8 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
904915
void handleFuncDeclRename(AbstractFunctionDecl *AFD,
905916
CharSourceRange NameRange) {
906917
bool IgnoreBase = false;
907-
if (auto View = getFuncRename(AFD, IgnoreBase)) {
918+
llvm::SmallString<32> Buffer;
919+
if (auto View = getFuncRename(AFD, Buffer, IgnoreBase)) {
908920
if (!IgnoreBase)
909921
Editor.replace(NameRange, View.base());
910922
unsigned Index = 0;

test/Migrator/Inputs/API.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,5 +509,13 @@
509509
"RightUsr": "",
510510
"RightComment": "BarBase.Nested",
511511
"ModuleName": "bar"
512-
}
512+
},
513+
{
514+
"DiffItemKind": "TypeMemberDiffItem",
515+
"Usr": "c:@F@barGlobalHoistedFuncOldName",
516+
"OldPrintedName": "barGlobalHoistedFuncOldName(_:_:_:)",
517+
"OldTypeName": "",
518+
"NewPrintedName": "newName(is:at:for:)",
519+
"NewTypeName": "AwesomeWrapper"
520+
},
513521
]

test/Migrator/mock-sdk/Bar.framework/Headers/Bar.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ int barGlobalVariableOldEnumElement = 1;
1010

1111
int barGlobalFuncOldName(int a);
1212

13+
int barGlobalHoistedFuncOldName(int a, int b, int c);
14+
1315
@interface BarForwardDeclaredClass
1416
- (id _Nonnull)initWithOldLabel0:(int)frame;
1517
- (void) barInstanceFunc0;

test/Migrator/rename.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func foo(_ b: BarForwardDeclaredClass) {
1818
_ = barGlobalVariableOldEnumElement
1919
_ = PropertyUserInterface.methodPlus()
2020
let _: BarBaseNested
21+
_ = barGlobalHoistedFuncOldName(0, 1, 2)
2122
}
2223

2324
func foo1(_ b: BarForwardDeclaredClass) {

test/Migrator/rename.swift.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func foo(_ b: BarForwardDeclaredClass) {
1818
_ = NewEnum.enumElement
1919
_ = PropertyUserInterface.newMethodPlus()
2020
let _: BarBase.Nested
21+
_ = AwesomeWrapper.newName(is: 0, at: 1, for: 2)
2122
}
2223

2324
func foo1(_ b: BarForwardDeclaredClass) {

0 commit comments

Comments
 (0)