Skip to content

Commit 5b7d10b

Browse files
authored
Merge pull request #16513 from nkcsgexi/cherry-migrator-05-10-4-30
[4.2-04-30] cherry-pick migrator changes
2 parents 22f6eb1 + 3b2b5ad commit 5b7d10b

File tree

5 files changed

+75
-5
lines changed

5 files changed

+75
-5
lines changed

lib/Migrator/APIDiffMigratorPass.cpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ class ChildIndexFinder : public TypeReprVisitor<ChildIndexFinder, FoundResult> {
229229
};
230230

231231
static ValueDecl* getReferencedDecl(Expr *E) {
232+
// Get the syntactic expression out of an implicit expression.
233+
if (auto *ICE = dyn_cast<ImplicitConversionExpr>(E))
234+
E = ICE->getSyntacticSubExpr();
232235
if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
233236
return DRE->getDecl();
234237
} else if (auto *MRE = dyn_cast<MemberRefExpr>(E)) {
@@ -309,7 +312,15 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
309312
APIDiffMigratorPass(EditorAdapter &Editor, SourceFile *SF,
310313
const MigratorOptions &Opts):
311314
ASTMigratorPass(Editor, SF, Opts),
312-
FileEndLoc(SM.getRangeForBuffer(BufferID).getEnd()) {}
315+
FileEndLoc(SM.getRangeForBuffer(BufferID).getEnd()) {
316+
SmallVector<Decl*, 16> TopDecls;
317+
SF->getTopLevelDecls(TopDecls);
318+
for (auto *D: TopDecls) {
319+
if (auto *FD = dyn_cast<FuncDecl>(D)) {
320+
InsertedFunctions.insert(FD->getBaseName().getIdentifier().str());
321+
}
322+
}
323+
}
313324

314325
void run() {
315326
if (Opts.APIDigesterDataStorePaths.empty())
@@ -675,6 +686,11 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
675686
}
676687
}
677688

689+
void replaceExpr(Expr* E, StringRef Text) {
690+
Editor.replace(CharSourceRange(SM, E->getStartLoc(),
691+
Lexer::getLocForEndOfToken(SM, E->getEndLoc())), Text);
692+
}
693+
678694
bool wrapAttributeReference(Expr* Reference, Expr* WrapperTarget,
679695
bool FromString) {
680696
auto *RD = getReferencedDecl(Reference);
@@ -699,18 +715,26 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
699715
Editor.insert(WrapperTarget->getStartLoc(), (Twine(Func) + "(").str());
700716
Editor.insertAfterToken(WrapperTarget->getEndLoc(), ")");
701717
if (!Rename.empty()) {
702-
auto Range = CharSourceRange(SM, Reference->getStartLoc(),
703-
Lexer::getLocForEndOfToken(SM, Reference->getEndLoc()));
704-
Editor.replace(Range, Rename);
718+
replaceExpr(Reference, Rename);
705719
}
706720
return true;
707721
}
708722

709723
bool handleAssignDestMigration(Expr *E) {
724+
Editor.disableCache();
725+
SWIFT_DEFER { Editor.enableCache(); };
710726
auto *ASE = dyn_cast<AssignExpr>(E);
711727
if (!ASE || !ASE->getDest() || !ASE->getSrc())
712728
return false;
713-
return wrapAttributeReference(ASE->getDest(), ASE->getSrc(), true);
729+
auto *Dest = ASE->getDest();
730+
auto Src = ASE->getSrc();
731+
if (wrapAttributeReference(Dest, Src, true)) {
732+
// We should handle the assignment source here since we won't visit
733+
// the children with present changes.
734+
handleAttributeReference(Src);
735+
return true;
736+
}
737+
return false;
714738
}
715739

716740
bool handleAttributeReference(Expr *E) {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// REQUIRES: objc_interop
2+
// RUN: %empty-directory(%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: %empty-directory(%t) && %target-swift-frontend -c -update-code -primary-file %s -I %t.mod -api-diff-data-file %S/Inputs/string-representable.json -emit-migrated-file-path %t/avoid_insert_existing_functions.swift.result -disable-migrator-fixits -o /dev/null
5+
// RUN: diff -u %S/avoid_insert_existing_functions.swift.expected %t/avoid_insert_existing_functions.swift.result
6+
7+
import Cities
8+
9+
typealias NewAttribute = String
10+
11+
func foo(_ c: Container) -> String {
12+
c.Value = ""
13+
return c.Value
14+
}
15+
16+
fileprivate func convertToNewAttribute(_ input: String) -> NewAttribute {
17+
return ""
18+
}
19+
20+
fileprivate func convertFromNewAttribute(_ input: NewAttribute) -> String {
21+
return ""
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// REQUIRES: objc_interop
2+
// RUN: %empty-directory(%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: %empty-directory(%t) && %target-swift-frontend -c -update-code -primary-file %s -I %t.mod -api-diff-data-file %S/Inputs/string-representable.json -emit-migrated-file-path %t/avoid_insert_existing_functions.swift.result -disable-migrator-fixits -o /dev/null
5+
// RUN: diff -u %S/avoid_insert_existing_functions.swift.expected %t/avoid_insert_existing_functions.swift.result
6+
7+
import Cities
8+
9+
typealias NewAttribute = String
10+
11+
func foo(_ c: Container) -> String {
12+
c.Value = convertToNewAttribute("")
13+
return convertFromNewAttribute(c.Value)
14+
}
15+
16+
fileprivate func convertToNewAttribute(_ input: String) -> NewAttribute {
17+
return ""
18+
}
19+
20+
fileprivate func convertFromNewAttribute(_ input: NewAttribute) -> String {
21+
return ""
22+
}

test/Migrator/string-representable.swift

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

test/Migrator/string-representable.swift.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func foo(_ c: Container) -> String {
3535
_ = Container(optionalAttrArray: convertToOptionalSimpleAttributeArray(convertFromSimpleAttributeArray(c.attrArr)))
3636
c.adding(optionalAttributes: convertToOptionalSimpleAttributeDictionary(convertFromOptionalSimpleAttributeDictionary(c.optionalAttrDict)))
3737
_ = convertFromNewAttribute(AttributeWrapper.NewAttribute)
38+
c.Value = convertToNewAttribute(convertFromNewAttribute(AttributeWrapper.NewAttribute))
3839
return convertFromNewAttribute(c.Value)
3940
}
4041

0 commit comments

Comments
 (0)