Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions lib/Migrator/APIDiffMigratorPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ class ChildIndexFinder : public TypeReprVisitor<ChildIndexFinder, FoundResult> {
};

static ValueDecl* getReferencedDecl(Expr *E) {
// Get the syntactic expression out of an implicit expression.
if (auto *ICE = dyn_cast<ImplicitConversionExpr>(E))
E = ICE->getSyntacticSubExpr();
if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
return DRE->getDecl();
} else if (auto *MRE = dyn_cast<MemberRefExpr>(E)) {
Expand Down Expand Up @@ -309,7 +312,15 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
APIDiffMigratorPass(EditorAdapter &Editor, SourceFile *SF,
const MigratorOptions &Opts):
ASTMigratorPass(Editor, SF, Opts),
FileEndLoc(SM.getRangeForBuffer(BufferID).getEnd()) {}
FileEndLoc(SM.getRangeForBuffer(BufferID).getEnd()) {
SmallVector<Decl*, 16> TopDecls;
SF->getTopLevelDecls(TopDecls);
for (auto *D: TopDecls) {
if (auto *FD = dyn_cast<FuncDecl>(D)) {
InsertedFunctions.insert(FD->getBaseName().getIdentifier().str());
}
}
}

void run() {
if (Opts.APIDigesterDataStorePaths.empty())
Expand Down Expand Up @@ -675,6 +686,11 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
}
}

void replaceExpr(Expr* E, StringRef Text) {
Editor.replace(CharSourceRange(SM, E->getStartLoc(),
Lexer::getLocForEndOfToken(SM, E->getEndLoc())), Text);
}

bool wrapAttributeReference(Expr* Reference, Expr* WrapperTarget,
bool FromString) {
auto *RD = getReferencedDecl(Reference);
Expand All @@ -699,18 +715,26 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
Editor.insert(WrapperTarget->getStartLoc(), (Twine(Func) + "(").str());
Editor.insertAfterToken(WrapperTarget->getEndLoc(), ")");
if (!Rename.empty()) {
auto Range = CharSourceRange(SM, Reference->getStartLoc(),
Lexer::getLocForEndOfToken(SM, Reference->getEndLoc()));
Editor.replace(Range, Rename);
replaceExpr(Reference, Rename);
}
return true;
}

bool handleAssignDestMigration(Expr *E) {
Editor.disableCache();
SWIFT_DEFER { Editor.enableCache(); };
auto *ASE = dyn_cast<AssignExpr>(E);
if (!ASE || !ASE->getDest() || !ASE->getSrc())
return false;
return wrapAttributeReference(ASE->getDest(), ASE->getSrc(), true);
auto *Dest = ASE->getDest();
auto Src = ASE->getSrc();
if (wrapAttributeReference(Dest, Src, true)) {
// We should handle the assignment source here since we won't visit
// the children with present changes.
handleAttributeReference(Src);
return true;
}
return false;
}

bool handleAttributeReference(Expr *E) {
Expand Down
22 changes: 22 additions & 0 deletions test/Migrator/avoid_insert_existing_functions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// REQUIRES: objc_interop
// RUN: %empty-directory(%t.mod)
// RUN: %target-swift-frontend -emit-module -o %t.mod/Cities.swiftmodule %S/Inputs/Cities.swift -module-name Cities -parse-as-library
// 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
// RUN: diff -u %S/avoid_insert_existing_functions.swift.expected %t/avoid_insert_existing_functions.swift.result

import Cities

typealias NewAttribute = String

func foo(_ c: Container) -> String {
c.Value = ""
return c.Value
}

fileprivate func convertToNewAttribute(_ input: String) -> NewAttribute {
return ""
}

fileprivate func convertFromNewAttribute(_ input: NewAttribute) -> String {
return ""
}
22 changes: 22 additions & 0 deletions test/Migrator/avoid_insert_existing_functions.swift.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// REQUIRES: objc_interop
// RUN: %empty-directory(%t.mod)
// RUN: %target-swift-frontend -emit-module -o %t.mod/Cities.swiftmodule %S/Inputs/Cities.swift -module-name Cities -parse-as-library
// 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
// RUN: diff -u %S/avoid_insert_existing_functions.swift.expected %t/avoid_insert_existing_functions.swift.result

import Cities

typealias NewAttribute = String

func foo(_ c: Container) -> String {
c.Value = convertToNewAttribute("")
return convertFromNewAttribute(c.Value)
}

fileprivate func convertToNewAttribute(_ input: String) -> NewAttribute {
return ""
}

fileprivate func convertFromNewAttribute(_ input: NewAttribute) -> String {
return ""
}
1 change: 1 addition & 0 deletions test/Migrator/string-representable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ func foo(_ c: Container) -> String {
_ = Container(optionalAttrArray: c.attrArr)
c.adding(optionalAttributes: c.optionalAttrDict)
_ = GlobalAttribute
c.Value = GlobalAttribute
return c.Value
}
1 change: 1 addition & 0 deletions test/Migrator/string-representable.swift.expected
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func foo(_ c: Container) -> String {
_ = Container(optionalAttrArray: convertToOptionalSimpleAttributeArray(convertFromSimpleAttributeArray(c.attrArr)))
c.adding(optionalAttributes: convertToOptionalSimpleAttributeDictionary(convertFromOptionalSimpleAttributeDictionary(c.optionalAttrDict)))
_ = convertFromNewAttribute(AttributeWrapper.NewAttribute)
c.Value = convertToNewAttribute(convertFromNewAttribute(AttributeWrapper.NewAttribute))
return convertFromNewAttribute(c.Value)
}

Expand Down