Skip to content

Commit b302bd3

Browse files
committed
migrator: avoid inserting helper functions if a function with the same name exists in the source file. rdar://40108285
1 parent 1c0d598 commit b302bd3

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

lib/Migrator/APIDiffMigratorPass.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,15 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
309309
APIDiffMigratorPass(EditorAdapter &Editor, SourceFile *SF,
310310
const MigratorOptions &Opts):
311311
ASTMigratorPass(Editor, SF, Opts),
312-
FileEndLoc(SM.getRangeForBuffer(BufferID).getEnd()) {}
312+
FileEndLoc(SM.getRangeForBuffer(BufferID).getEnd()) {
313+
SmallVector<Decl*, 16> TopDecls;
314+
SF->getTopLevelDecls(TopDecls);
315+
for (auto *D: TopDecls) {
316+
if (auto *FD = dyn_cast<FuncDecl>(D)) {
317+
InsertedFunctions.insert(FD->getBaseName().getIdentifier().str());
318+
}
319+
}
320+
}
313321

314322
void run() {
315323
if (Opts.APIDigesterDataStorePaths.empty())
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+
}

0 commit comments

Comments
 (0)