Skip to content

Commit 273a717

Browse files
authored
Merge pull request #21416 from nkcsgexi/mig-5.0-che
[5.0] Cherry-pick migrator changes.
2 parents cb91bfa + 46a8c32 commit 273a717

File tree

12 files changed

+85
-14
lines changed

12 files changed

+85
-14
lines changed

include/swift/Migrator/Migrator.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ namespace migrator {
3131
bool updateCodeAndEmitRemapIfNeeded(CompilerInstance *Instance,
3232
const CompilerInvocation &Invocation);
3333

34+
/// Specify options when running syntactic migration pass.
35+
struct SyntacticPassOptions {
36+
bool RunOptionalTryMigration = false;
37+
};
38+
3439
struct Migrator {
3540
CompilerInstance *StartInstance;
3641
const CompilerInvocation &StartInvocation;
@@ -69,7 +74,7 @@ struct Migrator {
6974
/// Returns true if failed:
7075
/// - Setting up the Swift CompilerInstance failed.
7176
/// - performSema emitted fatal errors along the way.
72-
bool performSyntacticPasses();
77+
bool performSyntacticPasses(SyntacticPassOptions Opts);
7378

7479
/// Emit a replacement map from the very start state's output text to the
7580
/// final state's output text to the StartInvocation's output file.

lib/Frontend/CompilerInvocation.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,8 +1079,11 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
10791079
return false;
10801080
}
10811081

1082-
static std::string getScriptFileName(StringRef name) {
1083-
return (Twine(name) + "4" + ".json").str();
1082+
static std::string getScriptFileName(StringRef name, version::Version &ver) {
1083+
if (ver.isVersionAtLeast(4, 2))
1084+
return (Twine(name) + "42" + ".json").str();
1085+
else
1086+
return (Twine(name) + "4" + ".json").str();
10841087
}
10851088

10861089
static bool ParseMigratorArgs(MigratorOptions &Opts,
@@ -1124,20 +1127,20 @@ static bool ParseMigratorArgs(MigratorOptions &Opts,
11241127

11251128
bool Supported = true;
11261129
llvm::SmallString<128> dataPath(basePath);
1127-
1130+
auto &langVer = LangOpts.EffectiveLanguageVersion;
11281131
if (Triple.isMacOSX())
1129-
llvm::sys::path::append(dataPath, getScriptFileName("macos"));
1132+
llvm::sys::path::append(dataPath, getScriptFileName("macos", langVer));
11301133
else if (Triple.isiOS())
1131-
llvm::sys::path::append(dataPath, getScriptFileName("ios"));
1134+
llvm::sys::path::append(dataPath, getScriptFileName("ios", langVer));
11321135
else if (Triple.isTvOS())
1133-
llvm::sys::path::append(dataPath, getScriptFileName("tvos"));
1136+
llvm::sys::path::append(dataPath, getScriptFileName("tvos", langVer));
11341137
else if (Triple.isWatchOS())
1135-
llvm::sys::path::append(dataPath, getScriptFileName("watchos"));
1138+
llvm::sys::path::append(dataPath, getScriptFileName("watchos", langVer));
11361139
else
11371140
Supported = false;
11381141
if (Supported) {
11391142
llvm::SmallString<128> authoredDataPath(basePath);
1140-
llvm::sys::path::append(authoredDataPath, getScriptFileName("overlay"));
1143+
llvm::sys::path::append(authoredDataPath, getScriptFileName("overlay", langVer));
11411144
// Add authored list first to take higher priority.
11421145
Opts.APIDigesterDataStorePaths.push_back(authoredDataPath.str());
11431146
Opts.APIDigesterDataStorePaths.push_back(dataPath.str());

lib/Migrator/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ set(datafiles
44
tvos4.json
55
watchos4.json
66
overlay4.json
7+
macos42.json
8+
ios42.json
9+
tvos42.json
10+
watchos42.json
11+
overlay42.json
712
)
813
set(SWIFTLIB_DIR
914
"${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib/swift")

lib/Migrator/Migrator.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ bool migrator::updateCodeAndEmitRemapIfNeeded(
5959
// Phase 2: Syntactic Transformations
6060
// Don't run these passes if we're already in newest Swift version.
6161
if (EffectiveVersion != CurrentVersion) {
62-
auto FailedSyntacticPasses = M.performSyntacticPasses();
62+
SyntacticPassOptions Opts;
63+
64+
// Type of optional try changes since Swift 5.
65+
Opts.RunOptionalTryMigration = !EffectiveVersion.isVersionAtLeast(5);
66+
auto FailedSyntacticPasses = M.performSyntacticPasses(Opts);
6367
if (FailedSyntacticPasses) {
6468
return true;
6569
}
@@ -173,7 +177,7 @@ Migrator::performAFixItMigration(version::Version SwiftLanguageVersion) {
173177
return Instance;
174178
}
175179

176-
bool Migrator::performSyntacticPasses() {
180+
bool Migrator::performSyntacticPasses(SyntacticPassOptions Opts) {
177181
clang::FileSystemOptions ClangFileSystemOptions;
178182
clang::FileManager ClangFileManager { ClangFileSystemOptions };
179183

@@ -197,8 +201,10 @@ bool Migrator::performSyntacticPasses() {
197201

198202
runAPIDiffMigratorPass(Editor, StartInstance->getPrimarySourceFile(),
199203
getMigratorOptions());
200-
runOptionalTryMigratorPass(Editor, StartInstance->getPrimarySourceFile(),
201-
getMigratorOptions());
204+
if (Opts.RunOptionalTryMigration) {
205+
runOptionalTryMigratorPass(Editor, StartInstance->getPrimarySourceFile(),
206+
getMigratorOptions());
207+
}
202208

203209
Edits.commit(Editor.getEdits());
204210

lib/Migrator/ios42.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

lib/Migrator/macos42.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

lib/Migrator/overlay4.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1320,5 +1320,16 @@
13201320
"DiffItemKind": "SpecialCaseDiffItem",
13211321
"Usr": "s:5UIKit17UIApplicationMainys5Int32VAD_SpySpys4Int8VGGSgSSSgAJtF",
13221322
"SpecialCaseId": "UIApplicationMain"
1323-
}
1323+
},
1324+
{
1325+
"DiffItemKind": "CommonDiffItem",
1326+
"NodeKind": "Function",
1327+
"NodeAnnotation": "Rename",
1328+
"ChildIndex": "0",
1329+
"LeftUsr": "s:SlsSQ7ElementRpzrlE5index2of5IndexQzSgAB_tF",
1330+
"LeftComment": "index",
1331+
"RightUsr": "",
1332+
"RightComment": "firstIndex",
1333+
"ModuleName": "Swift"
1334+
},
13241335
]

lib/Migrator/overlay42.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[
2+
{
3+
"DiffItemKind": "CommonDiffItem",
4+
"NodeKind": "Function",
5+
"NodeAnnotation": "Rename",
6+
"ChildIndex": "0",
7+
"LeftUsr": "s:SlsSQ7ElementRpzrlE5index2of5IndexQzSgAB_tF",
8+
"LeftComment": "index",
9+
"RightUsr": "",
10+
"RightComment": "firstIndex",
11+
"ModuleName": "Swift"
12+
},
13+
]

lib/Migrator/tvos42.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

lib/Migrator/watchos42.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

test/Migrator/stdlib_rename.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// REQUIRES: objc_interop
2+
// RUN: %empty-directory(%t) && %target-swift-frontend -c -update-code -primary-file %s -F %S/mock-sdk -emit-migrated-file-path %t/stdlib_rename.swift.result -emit-remap-file-path %t/stdlib_rename.swift.remap -o /dev/null
3+
// RUN: diff -u %S/stdlib_rename.swift.expected %t/stdlib_rename.swift.result
4+
// RUN: %empty-directory(%t) && %target-swift-frontend -c -update-code -primary-file %s -F %S/mock-sdk -emit-migrated-file-path %t/stdlib_rename.swift.result -emit-remap-file-path %t/stdlib_rename.swift.remap -o /dev/null -swift-version 4.2
5+
// RUN: diff -u %S/stdlib_rename.swift.expected %t/stdlib_rename.swift.result
6+
7+
func test1(_ a: [String], s: String) {
8+
_ = a.index(of: s)
9+
}
10+
func test2(_ s: String, c: Character) {
11+
_ = s.index(of: c)
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// REQUIRES: objc_interop
2+
// RUN: %empty-directory(%t) && %target-swift-frontend -c -update-code -primary-file %s -F %S/mock-sdk -emit-migrated-file-path %t/stdlib_rename.swift.result -emit-remap-file-path %t/stdlib_rename.swift.remap -o /dev/null
3+
// RUN: diff -u %S/stdlib_rename.swift.expected %t/stdlib_rename.swift.result
4+
// RUN: %empty-directory(%t) && %target-swift-frontend -c -update-code -primary-file %s -F %S/mock-sdk -emit-migrated-file-path %t/stdlib_rename.swift.result -emit-remap-file-path %t/stdlib_rename.swift.remap -o /dev/null -swift-version 4.2
5+
// RUN: diff -u %S/stdlib_rename.swift.expected %t/stdlib_rename.swift.result
6+
7+
func test1(_ a: [String], s: String) {
8+
_ = a.firstIndex(of: s)
9+
}
10+
func test2(_ s: String, c: Character) {
11+
_ = s.firstIndex(of: c)
12+
}

0 commit comments

Comments
 (0)