Skip to content

Commit 39c550f

Browse files
authored
[Migrator] Handle function decl renames. rdar://31766131 (swiftlang#9157)
1 parent 89d50e3 commit 39c550f

File tree

8 files changed

+86
-1
lines changed

8 files changed

+86
-1
lines changed

include/swift/Migrator/EditorAdapter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ class EditorAdapter {
108108
bool remove(SourceLoc TokenLoc);
109109
bool remove(SourceRange TokenRange);
110110
bool replace(SourceRange TokenRange, StringRef Text);
111+
bool replaceToken(SourceLoc TokenLoc, StringRef Text);
111112
bool replaceWithInner(SourceRange TokenRange, SourceRange TokenInnerRange);
112113

113114
/// Return the batched edits encountered so far.

lib/Migrator/EditorAdapter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,8 @@ bool EditorAdapter::replaceWithInner(SourceRange TokenRange,
148148
auto CharInnerRange = Lexer::getCharSourceRangeFromSourceRange(SwiftSrcMgr, TokenInnerRange);
149149
return replaceWithInner(CharRange, CharInnerRange);
150150
}
151+
152+
bool EditorAdapter::replaceToken(SourceLoc TokenLoc, StringRef Text) {
153+
return replace(Lexer::getTokenAtLocation(SwiftSrcMgr, TokenLoc).getRange(),
154+
Text);
155+
}

lib/Migrator/SyntacticMigratorPass.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,10 +389,41 @@ struct SyntacticMigratorPass::Implementation : public SourceEntityWalker {
389389
return true;
390390
}
391391

392+
void handleFuncDeclRename(AbstractFunctionDecl *AFD,
393+
CharSourceRange NameRange) {
394+
bool IgnoreBase = false;
395+
if (auto View = getFuncRename(AFD, IgnoreBase)) {
396+
if (!IgnoreBase)
397+
Editor.replace(NameRange, View.base());
398+
unsigned Index = 0;
399+
for (auto PL : AFD->getParameterLists()) {
400+
for (auto *PD : *PL) {
401+
if (Index == View.argSize())
402+
break;
403+
// Self parameter should not be updated.
404+
if (PD->isSelfParameter())
405+
continue;
406+
StringRef NewArg = View.args()[Index++];
407+
auto ArgLoc = PD->getArgumentNameLoc();
408+
409+
// If the argument name is not specified, add the argument name before
410+
// the paramter name.
411+
if (ArgLoc.isInvalid())
412+
Editor.insertBefore(PD->getNameLoc(),
413+
(llvm::Twine(NewArg) + " ").str());
414+
else
415+
// Otherwise, replace the argument name directly.
416+
Editor.replaceToken(ArgLoc, NewArg);
417+
}
418+
}
419+
}
420+
}
421+
392422
bool walkToDeclPre(Decl *D, CharSourceRange Range) override {
393423
if (D->isImplicit())
394424
return true;
395425
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(D)) {
426+
handleFuncDeclRename(AFD, Range);
396427
for (auto *Item: getRelatedDiffItems(AFD)) {
397428
if (auto *DiffItem = dyn_cast<CommonDiffItem>(Item)) {
398429
if (!DiffItem->isTypeChange())

test/Migrator/API.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,5 +346,27 @@
346346
"RightUsr": "",
347347
"RightComment": "",
348348
"ModuleName": "Cities"
349+
},
350+
{
351+
"DiffItemKind": "CommonDiffItem",
352+
"NodeKind": "Function",
353+
"NodeAnnotation": "Rename",
354+
"ChildIndex": "0",
355+
"LeftUsr": "s:6CitiesAAC10mooloolabayAB1x_ABSg1ytF",
356+
"LeftComment": "",
357+
"RightUsr": "",
358+
"RightComment": "newMooloolaba(newX:newY:)",
359+
"ModuleName": "Cities"
360+
},
361+
{
362+
"DiffItemKind": "CommonDiffItem",
363+
"NodeKind": "Function",
364+
"NodeAnnotation": "Rename",
365+
"ChildIndex": "0",
366+
"LeftUsr": "s:6Cities04MoreA0P14setZooLocationySi1x_Si1ySi1ztF",
367+
"LeftComment": "",
368+
"RightUsr": "",
369+
"RightComment": "setZooLocationNew(newX:newY:newZ:)",
370+
"ModuleName": "Cities"
349371
}
350372
]

test/Migrator/Inputs/cities.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ public protocol ExtraCities {
1515
func blibli(x: (String?, String) -> String!)
1616
func currimundi(x: (Int, (Int, Int))!)
1717
}
18+
19+
public protocol MoreCities {
20+
func setZooLocation(x: Int, y: Int, z: Int)
21+
}

test/Migrator/rename-func-decl.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// REQUIRES: objc_interop
2+
// RUN: rm -rf %t.mod && mkdir -p %t.mod
3+
// RUN: %target-swift-frontend -emit-module -o %t.mod/cities.swiftmodule %S/Inputs/cities.swift -module-name Cities -parse-as-library
4+
// RUN: rm -rf %t && mkdir -p %t && %target-swift-frontend -c -update-code -disable-migrator-fixits -primary-file %s -I %t.mod -api-diff-data-file %S/API.json -emit-migrated-file-path %t/rename-func-decl.swift.result -o %t/rename-func-decl.swift.remap -o /dev/null
5+
// RUN: diff -u %S/rename-func-decl.swift.expected %t/rename-func-decl.swift.result
6+
7+
import Cities
8+
9+
class MyCities : MoreCities {
10+
func setZooLocation(x ix: Int, y iy: Int, z iz: Int) {}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// REQUIRES: objc_interop
2+
// RUN: rm -rf %t.mod && mkdir -p %t.mod
3+
// RUN: %target-swift-frontend -emit-module -o %t.mod/cities.swiftmodule %S/Inputs/cities.swift -module-name Cities -parse-as-library
4+
// RUN: rm -rf %t && mkdir -p %t && %target-swift-frontend -c -update-code -disable-migrator-fixits -primary-file %s -I %t.mod -api-diff-data-file %S/API.json -emit-migrated-file-path %t/rename-func-decl.swift.result -o %t/rename-func-decl.swift.remap -o /dev/null
5+
// RUN: diff -u %S/rename-func-decl.swift.expected %t/rename-func-decl.swift.result
6+
7+
import Cities
8+
9+
class MyCities : MoreCities {
10+
func setZooLocationNew(newX ix: Int, newY iy: Int, newZ iz: Int) {}
11+
}

test/Migrator/wrap_optional.swift.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Cities
99
class MyCities : Cities {
1010
override init?(x: Int?) { super.init(x: x) }
1111
override init?(y: Int) { super.init(y: y) }
12-
override func mooloolaba(x: Cities?, y: Cities) {}
12+
override func newMooloolaba(newX x: Cities?, newY y: Cities) {}
1313
override func toowoomba(x: [Cities?], y: [Cities?]) {}
1414
override func mareeba(x: [String? : Cities], y: [Int : Cities]) {}
1515
}

0 commit comments

Comments
 (0)