Skip to content

Commit 1478159

Browse files
authored
[Migrator] Handle getter/setter to property access migration. rdar://31766311 (#9083)
1 parent 37ac1a0 commit 1478159

File tree

5 files changed

+82
-1
lines changed

5 files changed

+82
-1
lines changed

lib/Migrator/SyntacticMigratorPass.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,38 @@ struct SyntacticMigratorPass::Implementation : public SourceEntityWalker {
152152
}
153153
}
154154

155+
void handleFunctionCallToPropertyChange(ValueDecl *FD, Expr* FuncRefContainer,
156+
Expr *Arg) {
157+
for(auto *Item :getRelatedDiffItems(FD)) {
158+
if (auto *CD = dyn_cast<CommonDiffItem>(Item)) {
159+
switch (CD->DiffKind) {
160+
case NodeAnnotation::GetterToProperty: {
161+
// Remove "()"
162+
Editor.remove(Lexer::getCharSourceRangeFromSourceRange(SM,
163+
Arg->getSourceRange()));
164+
return;
165+
}
166+
case NodeAnnotation::SetterToProperty: {
167+
ReferenceCollector Walker(FD);
168+
Walker.walk(FuncRefContainer);
169+
auto ReplaceRange = CharSourceRange(SM, Walker.Result.getStart(),
170+
Arg->getStartLoc().getAdvancedLoc(1));
171+
172+
// Replace "x.getY(" with "x.Y =".
173+
Editor.replace(ReplaceRange, (llvm::Twine(Walker.Result.str().
174+
substr(3)) + " = ").str());
175+
// Remove ")"
176+
Editor.remove(CharSourceRange(SM, Arg->getEndLoc(), Arg->getEndLoc().
177+
getAdvancedLoc(1)));
178+
return;
179+
}
180+
default:
181+
break;
182+
}
183+
}
184+
}
185+
}
186+
155187
bool walkToExprPre(Expr *E) override {
156188
if (auto *CE = dyn_cast<CallExpr>(E)) {
157189
auto Fn = CE->getFn();
@@ -164,8 +196,10 @@ struct SyntacticMigratorPass::Implementation : public SourceEntityWalker {
164196
}
165197
case ExprKind::DotSyntaxCall: {
166198
auto DSC = cast<DotSyntaxCallExpr>(Fn);
167-
if (auto FD = DSC->getFn()->getReferencedDecl().getDecl())
199+
if (auto FD = DSC->getFn()->getReferencedDecl().getDecl()) {
168200
handleFuncRename(FD, DSC->getFn(), Args);
201+
handleFunctionCallToPropertyChange(FD, DSC->getFn(), Args);
202+
}
169203
break;
170204
}
171205
case ExprKind::ConstructorRefCall: {

test/Migrator/API.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,27 @@
6060
"RightUsr": "",
6161
"RightComment": "NewEnum.enumElement",
6262
"ModuleName": "bar"
63+
},
64+
{
65+
"DiffItemKind": "CommonDiffItem",
66+
"NodeKind": "Function",
67+
"NodeAnnotation": "GetterToProperty",
68+
"ChildIndex": "0",
69+
"LeftUsr": "c:objc(cs)PropertyUserInterface(im)field",
70+
"LeftComment": "",
71+
"RightUsr": "",
72+
"RightComment": "",
73+
"ModuleName": "bar"
74+
},
75+
{
76+
"DiffItemKind": "CommonDiffItem",
77+
"NodeKind": "Function",
78+
"NodeAnnotation": "SetterToProperty",
79+
"ChildIndex": "0",
80+
"LeftUsr": "c:objc(cs)PropertyUserInterface(im)setField:",
81+
"LeftComment": "",
82+
"RightUsr": "",
83+
"RightComment": "",
84+
"ModuleName": "bar"
6385
}
6486
]

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ enum BarForwardDeclaredEnum {
2020
BarForwardDeclaredEnumValue = 42
2121
};
2222

23+
@interface PropertyUserInterface
24+
- (int) field;
25+
- (void) setField:(int)info;
26+
@end
27+
2328
#define BAR_MACRO_1 0
2429

2530
typedef struct {

test/Migrator/property.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// REQUIRES: objc_interop
2+
// RUN: rm -rf %t && mkdir -p %t && %target-swift-frontend -c -update-code -primary-file %s -F %S/mock-sdk -api-diff-data-file %S/API.json -emit-migrated-file-path %t/property.swift.result -disable-migrator-fixits
3+
// RUN: diff -u %S/property.swift.expected %t/property.swift.result
4+
5+
import Bar
6+
7+
func foo(_ a : PropertyUserInterface) {
8+
a.setField(1)
9+
_ = a.field()
10+
}

test/Migrator/property.swift.expected

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// REQUIRES: objc_interop
2+
// RUN: rm -rf %t && mkdir -p %t && %target-swift-frontend -c -update-code -primary-file %s -F %S/mock-sdk -api-diff-data-file %S/API.json -emit-migrated-file-path %t/property.swift.result -disable-migrator-fixits
3+
// RUN: diff -u %S/property.swift.expected %t/property.swift.result
4+
5+
import Bar
6+
7+
func foo(_ a : PropertyUserInterface) {
8+
a.Field = 1
9+
_ = a.field
10+
}

0 commit comments

Comments
 (0)