mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[Migrator] Handle function decl renames. rdar://31766131 (#9157)
This commit is contained in:
@@ -108,6 +108,7 @@ public:
|
||||
bool remove(SourceLoc TokenLoc);
|
||||
bool remove(SourceRange TokenRange);
|
||||
bool replace(SourceRange TokenRange, StringRef Text);
|
||||
bool replaceToken(SourceLoc TokenLoc, StringRef Text);
|
||||
bool replaceWithInner(SourceRange TokenRange, SourceRange TokenInnerRange);
|
||||
|
||||
/// Return the batched edits encountered so far.
|
||||
|
||||
@@ -148,3 +148,8 @@ bool EditorAdapter::replaceWithInner(SourceRange TokenRange,
|
||||
auto CharInnerRange = Lexer::getCharSourceRangeFromSourceRange(SwiftSrcMgr, TokenInnerRange);
|
||||
return replaceWithInner(CharRange, CharInnerRange);
|
||||
}
|
||||
|
||||
bool EditorAdapter::replaceToken(SourceLoc TokenLoc, StringRef Text) {
|
||||
return replace(Lexer::getTokenAtLocation(SwiftSrcMgr, TokenLoc).getRange(),
|
||||
Text);
|
||||
}
|
||||
|
||||
@@ -389,10 +389,41 @@ struct SyntacticMigratorPass::Implementation : public SourceEntityWalker {
|
||||
return true;
|
||||
}
|
||||
|
||||
void handleFuncDeclRename(AbstractFunctionDecl *AFD,
|
||||
CharSourceRange NameRange) {
|
||||
bool IgnoreBase = false;
|
||||
if (auto View = getFuncRename(AFD, IgnoreBase)) {
|
||||
if (!IgnoreBase)
|
||||
Editor.replace(NameRange, View.base());
|
||||
unsigned Index = 0;
|
||||
for (auto PL : AFD->getParameterLists()) {
|
||||
for (auto *PD : *PL) {
|
||||
if (Index == View.argSize())
|
||||
break;
|
||||
// Self parameter should not be updated.
|
||||
if (PD->isSelfParameter())
|
||||
continue;
|
||||
StringRef NewArg = View.args()[Index++];
|
||||
auto ArgLoc = PD->getArgumentNameLoc();
|
||||
|
||||
// If the argument name is not specified, add the argument name before
|
||||
// the paramter name.
|
||||
if (ArgLoc.isInvalid())
|
||||
Editor.insertBefore(PD->getNameLoc(),
|
||||
(llvm::Twine(NewArg) + " ").str());
|
||||
else
|
||||
// Otherwise, replace the argument name directly.
|
||||
Editor.replaceToken(ArgLoc, NewArg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool walkToDeclPre(Decl *D, CharSourceRange Range) override {
|
||||
if (D->isImplicit())
|
||||
return true;
|
||||
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(D)) {
|
||||
handleFuncDeclRename(AFD, Range);
|
||||
for (auto *Item: getRelatedDiffItems(AFD)) {
|
||||
if (auto *DiffItem = dyn_cast<CommonDiffItem>(Item)) {
|
||||
if (!DiffItem->isTypeChange())
|
||||
|
||||
@@ -346,5 +346,27 @@
|
||||
"RightUsr": "",
|
||||
"RightComment": "",
|
||||
"ModuleName": "Cities"
|
||||
},
|
||||
{
|
||||
"DiffItemKind": "CommonDiffItem",
|
||||
"NodeKind": "Function",
|
||||
"NodeAnnotation": "Rename",
|
||||
"ChildIndex": "0",
|
||||
"LeftUsr": "s:6CitiesAAC10mooloolabayAB1x_ABSg1ytF",
|
||||
"LeftComment": "",
|
||||
"RightUsr": "",
|
||||
"RightComment": "newMooloolaba(newX:newY:)",
|
||||
"ModuleName": "Cities"
|
||||
},
|
||||
{
|
||||
"DiffItemKind": "CommonDiffItem",
|
||||
"NodeKind": "Function",
|
||||
"NodeAnnotation": "Rename",
|
||||
"ChildIndex": "0",
|
||||
"LeftUsr": "s:6Cities04MoreA0P14setZooLocationySi1x_Si1ySi1ztF",
|
||||
"LeftComment": "",
|
||||
"RightUsr": "",
|
||||
"RightComment": "setZooLocationNew(newX:newY:newZ:)",
|
||||
"ModuleName": "Cities"
|
||||
}
|
||||
]
|
||||
|
||||
@@ -15,3 +15,7 @@ public protocol ExtraCities {
|
||||
func blibli(x: (String?, String) -> String!)
|
||||
func currimundi(x: (Int, (Int, Int))!)
|
||||
}
|
||||
|
||||
public protocol MoreCities {
|
||||
func setZooLocation(x: Int, y: Int, z: Int)
|
||||
}
|
||||
11
test/Migrator/rename-func-decl.swift
Normal file
11
test/Migrator/rename-func-decl.swift
Normal file
@@ -0,0 +1,11 @@
|
||||
// REQUIRES: objc_interop
|
||||
// RUN: rm -rf %t.mod && mkdir -p %t.mod
|
||||
// RUN: %target-swift-frontend -emit-module -o %t.mod/cities.swiftmodule %S/Inputs/cities.swift -module-name Cities -parse-as-library
|
||||
// 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
|
||||
// RUN: diff -u %S/rename-func-decl.swift.expected %t/rename-func-decl.swift.result
|
||||
|
||||
import Cities
|
||||
|
||||
class MyCities : MoreCities {
|
||||
func setZooLocation(x ix: Int, y iy: Int, z iz: Int) {}
|
||||
}
|
||||
11
test/Migrator/rename-func-decl.swift.expected
Normal file
11
test/Migrator/rename-func-decl.swift.expected
Normal file
@@ -0,0 +1,11 @@
|
||||
// REQUIRES: objc_interop
|
||||
// RUN: rm -rf %t.mod && mkdir -p %t.mod
|
||||
// RUN: %target-swift-frontend -emit-module -o %t.mod/cities.swiftmodule %S/Inputs/cities.swift -module-name Cities -parse-as-library
|
||||
// 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
|
||||
// RUN: diff -u %S/rename-func-decl.swift.expected %t/rename-func-decl.swift.result
|
||||
|
||||
import Cities
|
||||
|
||||
class MyCities : MoreCities {
|
||||
func setZooLocationNew(newX ix: Int, newY iy: Int, newZ iz: Int) {}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ import Cities
|
||||
class MyCities : Cities {
|
||||
override init?(x: Int?) { super.init(x: x) }
|
||||
override init?(y: Int) { super.init(y: y) }
|
||||
override func mooloolaba(x: Cities?, y: Cities) {}
|
||||
override func newMooloolaba(newX x: Cities?, newY y: Cities) {}
|
||||
override func toowoomba(x: [Cities?], y: [Cities?]) {}
|
||||
override func mareeba(x: [String? : Cities], y: [Int : Cities]) {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user