Skip to content

Commit c393e30

Browse files
authored
Merge pull request #15 from sjavora/multithreading-tests-and-diagnostics
Multithreading tests and diagnostics
2 parents 4ae9835 + b651738 commit c393e30

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ extension Driver {
751751
}
752752

753753
extension Diagnostic.Message {
754-
public static func error_i_mode(_ driverKind: DriverKind) -> Diagnostic.Message {
754+
static func error_i_mode(_ driverKind: DriverKind) -> Diagnostic.Message {
755755
.error(
756756
"""
757757
the flag '-i' is no longer required and has been removed; \
@@ -775,17 +775,14 @@ extension Driver {
775775

776776
// Make sure we have a non-negative integer value.
777777
guard let numThreads = Int(numThreadsArg.asSingle), numThreads >= 0 else {
778-
diagnosticsEngine.emit(Diagnostic.Message.error_invalid_arg_value(arg: .numThreads, value: numThreadsArg.asSingle))
778+
diagnosticsEngine.emit(.error_invalid_arg_value(arg: .numThreads, value: numThreadsArg.asSingle))
779779
return 0
780780
}
781781

782-
#if false
783-
// FIXME: Check for batch mode.
784-
if false {
782+
if case .batchCompile = compilerMode {
785783
diagnosticsEngine.emit(.warning_cannot_multithread_batch_mode)
786784
return 0
787785
}
788-
#endif
789786

790787
return numThreads
791788
}

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,59 @@ final class SwiftDriverTests: XCTestCase {
144144

145145
func testPrimaryOutputKindsDiagnostics() throws {
146146
try assertDriverDiagnostics(args: "swift", "-i") {
147-
$1.expect(.error_i_mode(.interactive))
147+
$1.expect(.error("the flag '-i' is no longer required and has been removed; use 'swift input-filename'"))
148148
}
149149
}
150150

151+
func testMultithreading() throws {
152+
153+
XCTAssertEqual(try Driver(args: ["swiftc"]).numThreads, 0)
154+
155+
XCTAssertEqual(try Driver(args: ["swiftc", "-num-threads", "4"]).numThreads, 4)
156+
157+
XCTAssertEqual(try Driver(args: ["swiftc", "-num-threads", "0"]).numThreads, 0)
158+
159+
XCTAssertEqual(try Driver(args: ["swiftc", "-num-threads", "-1"]).numThreads, 0)
160+
161+
XCTAssertEqual(try Driver(args: ["swiftc", "-enable-batch-mode", "-num-threads", "4"]).numThreads, 0)
162+
163+
XCTAssertNil(try Driver(args: ["swiftc"]).numParallelJobs)
164+
165+
XCTAssertEqual(try Driver(args: ["swiftc", "-j", "4"]).numParallelJobs, 4)
166+
167+
XCTAssertNil(try Driver(args: ["swiftc", "-j", "0"]).numParallelJobs)
168+
169+
XCTAssertEqual(
170+
try Driver(
171+
args: ["swiftc", "-j", "4", "-target", "x86_64-apple-macosx10.15"],
172+
env: ["SWIFTC_MAXIMUM_DETERMINISM": "1"]
173+
).numParallelJobs,
174+
1
175+
)
176+
}
177+
178+
func testMultithreadingDiagnostics() throws {
179+
180+
try assertDriverDiagnostics(args: "swift", "-num-threads", "-1") {
181+
$1.expect(.error_invalid_arg_value(arg: .numThreads, value: "-1"))
182+
}
183+
184+
try assertDriverDiagnostics(args: "swiftc", "-enable-batch-mode", "-num-threads", "4") {
185+
$1.expect(.warning("ignoring -num-threads argument; cannot multithread batch mode"))
186+
}
187+
188+
try assertDriverDiagnostics(args: "swiftc", "-j", "0") {
189+
$1.expect(.error_invalid_arg_value(arg: .j, value: "0"))
190+
}
191+
192+
try assertDriverDiagnostics(
193+
args: "swiftc", "-j", "8", "-target", "x86_64-apple-macosx10.15",
194+
env: ["SWIFTC_MAXIMUM_DETERMINISM": "1"]
195+
) {
196+
$1.expect(.remark("SWIFTC_MAXIMUM_DETERMINISM overriding -j"))
197+
}
198+
}
199+
151200
func testDebugSettings() throws {
152201
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "-emit-module") { driver in
153202
XCTAssertNil(driver.debugInfoLevel)

0 commit comments

Comments
 (0)