Skip to content

Commit 1cfa5ea

Browse files
committed
migrator: handle argument renames when a global function is hoisted to a static member function. rdar://40145590
1 parent 542fe37 commit 1cfa5ea

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
@@ -272,7 +272,8 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
272272
return results;
273273
}
274274

275-
DeclNameViewer getFuncRename(ValueDecl *VD, bool &IgnoreBase) {
275+
DeclNameViewer getFuncRename(ValueDecl *VD, llvm::SmallString<32> &Buffer,
276+
bool &IgnoreBase) {
276277
for (auto *Item: getRelatedDiffItems(VD)) {
277278
if (auto *CI = dyn_cast<CommonDiffItem>(Item)) {
278279
if (CI->isRename()) {
@@ -288,6 +289,13 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
288289
}
289290
}
290291
}
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+
}
291299
}
292300
return DeclNameViewer();
293301
}
@@ -419,7 +427,8 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
419427

420428
void handleFuncRename(ValueDecl *FD, Expr* FuncRefContainer, Expr *Arg) {
421429
bool IgnoreBase = false;
422-
if (auto View = getFuncRename(FD, IgnoreBase)) {
430+
llvm::SmallString<32> Buffer;
431+
if (auto View = getFuncRename(FD, Buffer, IgnoreBase)) {
423432
if (!IgnoreBase) {
424433
ReferenceCollector Walker(FD);
425434
Walker.walk(FuncRefContainer);
@@ -581,7 +590,8 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
581590
if (!Item)
582591
return false;
583592
if (Item->Subkind == TypeMemberDiffItemSubKind::SimpleReplacement ||
584-
Item->Subkind == TypeMemberDiffItemSubKind::QualifiedReplacement)
593+
Item->Subkind == TypeMemberDiffItemSubKind::QualifiedReplacement ||
594+
Item->Subkind == TypeMemberDiffItemSubKind::FuncRename)
585595
return false;
586596

587597
if (Item->Subkind == TypeMemberDiffItemSubKind::GlobalFuncToStaticProperty) {
@@ -630,6 +640,7 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
630640
}
631641

632642
switch (Item->Subkind) {
643+
case TypeMemberDiffItemSubKind::FuncRename:
633644
case TypeMemberDiffItemSubKind::GlobalFuncToStaticProperty:
634645
case TypeMemberDiffItemSubKind::SimpleReplacement:
635646
case TypeMemberDiffItemSubKind::QualifiedReplacement:
@@ -917,7 +928,8 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
917928
void handleFuncDeclRename(AbstractFunctionDecl *AFD,
918929
CharSourceRange NameRange) {
919930
bool IgnoreBase = false;
920-
if (auto View = getFuncRename(AFD, IgnoreBase)) {
931+
llvm::SmallString<32> Buffer;
932+
if (auto View = getFuncRename(AFD, Buffer, IgnoreBase)) {
921933
if (!IgnoreBase)
922934
Editor.replace(NameRange, View.base());
923935
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)