Skip to content

Commit 50f9c94

Browse files
author
Nathan Hawes
committed
[migrator] Migrate UIApplicationMain
In Swift 4.2 the second parameter of UIApplicationMain exactly matches the type of CommandLine.unsafeArgv so it can be called as below: UIApplicationMain(CommandLine.argc, CommandLine.unsafeArgv, ...) This is how it was intended to be in Swift 4 as well, but the types had optionality differences, so callers instead had to do something like the below example from the Firefox-iOS project: let pointer = UnsafeMutableRawPointer(CommandLine.unsafeArgv).bindMemory( to: UnsafeMutablePointer<Int8>.self, capacity: Int(CommandLine.argc)) UIApplicationMain(CommandLine.argc, pointer, ...) This migration simply replaces the the second argument with CommandLine.unsafeArgv if the first argument is CommandLine.argc. There is an open issue for providing a deprecated version with the old type so we simply leave as is any callers that don't pass argc for the first argument. Resolves rdar://problem/40045693.
1 parent a5e0533 commit 50f9c94

10 files changed

+76
-2
lines changed

include/swift/IDE/DigesterEnums.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ SPECIAL_CASE_ID(StaticAbsToSwiftAbs)
172172
SPECIAL_CASE_ID(NSOpenGLGetVersion)
173173
SPECIAL_CASE_ID(ToIntMax)
174174
SPECIAL_CASE_ID(ToUIntMax)
175+
SPECIAL_CASE_ID(UIApplicationMain)
175176

176177
#undef SPECIAL_CASE_ID
177178
#undef DIFF_ITEM_KEY_KIND_INT

lib/Migrator/APIDiffMigratorPass.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,23 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
675675
}
676676
return false;
677677
}
678+
case SpecialCaseId::UIApplicationMain: {
679+
// If the first argument is CommandLine.argc, replace the second argument
680+
// with CommandLine.unsafeArgv
681+
CallArgInfo &FirstArg = AllArgs[0];
682+
// handle whitespace/line splits around the first arg when matching
683+
auto FirstArgSplit =
684+
SM.extractText(FirstArg.getEntireCharRange(SM)).rsplit('.');
685+
if (!FirstArgSplit.second.empty() &&
686+
FirstArgSplit.first.trim() == "CommandLine" &&
687+
FirstArgSplit.second.trim() == "argc") {
688+
CallArgInfo &SecondArg = AllArgs[1];
689+
Editor.replace(SecondArg.getEntireCharRange(SM),
690+
"CommandLine.unsafeArgv");
691+
return true;
692+
}
693+
return false;
694+
}
678695
}
679696
}
680697

lib/Migrator/overlay3.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,4 +1316,9 @@
13161316
"RightComment": "compactMap",
13171317
"ModuleName": "Swift"
13181318
},
1319+
{
1320+
"DiffItemKind": "SpecialCaseDiffItem",
1321+
"Usr": "c:@F@UIApplicationMain",
1322+
"SpecialCaseId": "UIApplicationMain"
1323+
}
13191324
]

lib/Migrator/overlay4.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,4 +1316,9 @@
13161316
"RightComment": "compactMap",
13171317
"ModuleName": "Swift"
13181318
},
1319+
{
1320+
"DiffItemKind": "SpecialCaseDiffItem",
1321+
"Usr": "c:@F@UIApplicationMain",
1322+
"SpecialCaseId": "UIApplicationMain"
1323+
}
13191324
]

test/Migrator/Inputs/MyAppKit.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
public class NSOpenGLOption {}
33
public func NSOpenGLGetOption(_ c : NSOpenGLOption, _ p :UnsafePointer<Int>) {}
44
public func NSOpenGLSetOption(_ c : NSOpenGLOption, _ p : Int) {}
5+
public func UIApplicationMain(_ a: Int32, _ b: UnsafeMutablePointer<UnsafeMutablePointer<Int8>>, _ c: String?, _ d: String?) -> Int32 { return 0 }

test/Migrator/Inputs/SpecialCaseAPI.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,10 @@
1313
"DiffItemKind": "SpecialCaseDiffItem",
1414
"Usr": "s:7MySwift0A6DoubleV3absyS2dFZ",
1515
"SpecialCaseId": "StaticAbsToSwiftAbs"
16+
},
17+
{
18+
"DiffItemKind": "SpecialCaseDiffItem",
19+
"Usr": "s:8MyAppKit17UIApplicationMainys5Int32VAD_SpySpys4Int8VGGSSSgAItF",
20+
"SpecialCaseId": "UIApplicationMain"
1621
}
1722
]

test/Migrator/api-special-cases.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@
88
import MyAppKit
99
import MySwift
1010

11-
func foo(_ Opt: NSOpenGLOption) {
11+
func foo(_ Opt: NSOpenGLOption, _ pointer: UnsafeMutablePointer<UnsafeMutablePointer<Int8>>) {
1212
var Value = 1
1313
NSOpenGLSetOption(Opt, 1)
1414
NSOpenGLGetOption(Opt, &Value)
15+
UIApplicationMain(CommandLine.argc, pointer, "", "")
16+
UIApplicationMain(
17+
CommandLine.argc, pointer, "", "")
18+
UIApplicationMain( CommandLine .
19+
argc, pointer, "", "")
20+
UIApplicationMain(10, pointer, "", "")
1521
}
1622

1723
do {

test/Migrator/api-special-cases.swift.expected

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@
88
import MyAppKit
99
import MySwift
1010

11-
func foo(_ Opt: NSOpenGLOption) {
11+
func foo(_ Opt: NSOpenGLOption, _ pointer: UnsafeMutablePointer<UnsafeMutablePointer<Int8>>) {
1212
var Value = 1
1313
Opt.globalValue = 1
1414
Value = Opt.globalValue
15+
UIApplicationMain(CommandLine.argc, CommandLine.unsafeArgv, "", "")
16+
UIApplicationMain(
17+
CommandLine.argc, CommandLine.unsafeArgv, "", "")
18+
UIApplicationMain( CommandLine .
19+
argc, CommandLine.unsafeArgv, "", "")
20+
UIApplicationMain(10, pointer, "", "")
1521
}
1622

1723
do {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %empty-directory(%t) && %target-swift-frontend -c -update-code -primary-file %s -emit-migrated-file-path %t/ui-application-main.swift.result -emit-remap-file-path %t/ui-application-main.swift.remap -o /dev/null -swift-version 3
2+
// RUN: diff -u %S/ui-application-main.swift.expected %t/ui-application-main.swift.result
3+
// RUN: %empty-directory(%t) && %target-swift-frontend -c -update-code -primary-file %s -emit-migrated-file-path %t/ui-application-main.swift.result -emit-remap-file-path %t/ui-application-main.swift.remap -o /dev/null -swift-version 4
4+
// RUN: diff -u %S/ui-application-main.swift.expected %t/ui-application-main.swift.result
5+
6+
// REQUIRES: OS=ios
7+
// REQUIRES: objc_interop
8+
9+
import UIKit
10+
11+
func foo(pointer: UnsafeMutablePointer<UnsafeMutablePointer<Int8>>) {
12+
UIApplicationMain(CommandLine.argc, pointer, "", "")
13+
UIApplicationMain(2, pointer, "", "")
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %empty-directory(%t) && %target-swift-frontend -c -update-code -primary-file %s -emit-migrated-file-path %t/ui-application-main.swift.result -emit-remap-file-path %t/ui-application-main.swift.remap -o /dev/null -swift-version 3
2+
// RUN: diff -u %S/ui-application-main.swift.expected %t/ui-application-main.swift.result
3+
// RUN: %empty-directory(%t) && %target-swift-frontend -c -update-code -primary-file %s -emit-migrated-file-path %t/ui-application-main.swift.result -emit-remap-file-path %t/ui-application-main.swift.remap -o /dev/null -swift-version 4
4+
// RUN: diff -u %S/ui-application-main.swift.expected %t/ui-application-main.swift.result
5+
6+
// REQUIRES: OS=ios
7+
// REQUIRES: objc_interop
8+
9+
import UIKit
10+
11+
func foo(pointer: UnsafeMutablePointer<UnsafeMutablePointer<Int8>>) {
12+
UIApplicationMain(CommandLine.argc, CommandLine.unsafeArgv, "", "")
13+
UIApplicationMain(2, pointer, "", "")
14+
}

0 commit comments

Comments
 (0)