Skip to content

Commit bc95428

Browse files
committed
migrator: make sure we can handle the composite change of rename and string representable update. radr://40076924
1 parent 29b3394 commit bc95428

File tree

5 files changed

+43
-11
lines changed

5 files changed

+43
-11
lines changed

lib/Migrator/APIDiffMigratorPass.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,6 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
347347
Editor.replace(Range, RepText);
348348
return true;
349349
}
350-
if (updateStringRepresentableDeclRef(Item, Range)) {
351-
return true;
352-
}
353350
}
354351
return true;
355352
}
@@ -683,20 +680,30 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
683680
auto *RD = getReferencedDecl(Reference);
684681
if (!RD)
685682
return false;
683+
std::string Func;
684+
std::string Rename;
686685
for (auto *Item: getRelatedDiffItems(RD)) {
687-
if (auto *CI = dyn_cast<CommonDiffItem>(Item)) {
686+
if (isSimpleReplacement(Item, Rename)) {
687+
} else if (auto *CI = dyn_cast<CommonDiffItem>(Item)) {
688688
if (CI->isStringRepresentableChange() &&
689689
CI->NodeKind == SDKNodeKind::DeclVar) {
690690
SmallString<256> Buffer;
691-
auto Func = insertHelperFunction(CI->DiffKind, CI->RightComment,
692-
Buffer, FromString);
693-
Editor.insert(WrapperTarget->getStartLoc(), (Twine(Func) + "(").str());
694-
Editor.insertAfterToken(WrapperTarget->getEndLoc(), ")");
695-
return true;
691+
Func = insertHelperFunction(CI->DiffKind, CI->RightComment, Buffer,
692+
FromString);
696693
}
697694
}
698695
}
699-
return false;
696+
if (Func.empty())
697+
return false;
698+
699+
Editor.insert(WrapperTarget->getStartLoc(), (Twine(Func) + "(").str());
700+
Editor.insertAfterToken(WrapperTarget->getEndLoc(), ")");
701+
if (!Rename.empty()) {
702+
auto Range = CharSourceRange(SM, Reference->getStartLoc(),
703+
Lexer::getLocForEndOfToken(SM, Reference->getEndLoc()));
704+
Editor.replace(Range, Rename);
705+
}
706+
return true;
700707
}
701708

702709
bool handleAssignDestMigration(Expr *E) {
@@ -827,7 +834,9 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
827834
bool walkToExprPre(Expr *E) override {
828835
if (handleQualifiedReplacement(E))
829836
return false;
830-
if (handleAssignDestMigration(E) || handleAttributeReference(E))
837+
if (handleAssignDestMigration(E))
838+
return false;
839+
if (handleAttributeReference(E))
831840
return false;
832841
if (auto *CE = dyn_cast<CallExpr>(E)) {
833842
auto Fn = CE->getFn();

test/Migrator/Inputs/Cities.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,5 @@ public class ToplevelType {
5959
public init() {}
6060
public init(recordName: String) {}
6161
}
62+
63+
public var GlobalAttribute: String = ""

test/Migrator/Inputs/string-representable.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,23 @@
197197
"RightComment": "SimpleAttribute",
198198
"ModuleName": "Cities"
199199
},
200+
{
201+
"DiffItemKind": "CommonDiffItem",
202+
"NodeKind": "Var",
203+
"NodeAnnotation": "SimpleStringRepresentableUpdate",
204+
"ChildIndex": "0",
205+
"LeftUsr": "s:6Cities15GlobalAttributeSSvp",
206+
"LeftComment": "",
207+
"RightUsr": "",
208+
"RightComment": "NewAttribute",
209+
"ModuleName": "Cities"
210+
},
211+
{
212+
"DiffItemKind": "TypeMemberDiffItem",
213+
"Usr": "s:6Cities15GlobalAttributeSSvp",
214+
"OldPrintedName": "GlobalAttribute",
215+
"OldTypeName": "",
216+
"NewPrintedName": "NewAttribute",
217+
"NewTypeName": "AttributeWrapper"
218+
},
200219
]

test/Migrator/string-representable.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ func foo(_ c: Container) -> String {
3434
c.adding(attributes: c.attrDict)
3535
_ = Container(optionalAttrArray: c.attrArr)
3636
c.adding(optionalAttributes: c.optionalAttrDict)
37+
_ = GlobalAttribute
3738
return c.Value
3839
}

test/Migrator/string-representable.swift.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func foo(_ c: Container) -> String {
3434
c.adding(attributes: convertToSimpleAttributeDictionary(convertFromSimpleAttributeDictionary(c.attrDict)))
3535
_ = Container(optionalAttrArray: convertToOptionalSimpleAttributeArray(convertFromSimpleAttributeArray(c.attrArr)))
3636
c.adding(optionalAttributes: convertToOptionalSimpleAttributeDictionary(convertFromOptionalSimpleAttributeDictionary(c.optionalAttrDict)))
37+
_ = convertFromNewAttribute(AttributeWrapper.NewAttribute)
3738
return convertFromNewAttribute(c.Value)
3839
}
3940

0 commit comments

Comments
 (0)