Skip to content

Commit 48cbefc

Browse files
Emit error diagnostic for invalid lto argument
1 parent 3338346 commit 48cbefc

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ public struct Driver {
379379
self.allSourcesFileList = nil
380380
}
381381

382-
self.lto = Self.ltoKind(&parsedOptions)
382+
self.lto = Self.ltoKind(&parsedOptions, diagnosticsEngine: diagnosticsEngine)
383383
// Figure out the primary outputs from the driver.
384384
(self.compilerOutputType, self.linkerOutputType) = Self.determinePrimaryOutputs(&parsedOptions, driverKind: driverKind, diagnosticsEngine: diagnosticEngine)
385385

@@ -594,11 +594,14 @@ extension Driver {
594594
}
595595

596596
extension Driver {
597-
private static func ltoKind(_ parsedOptions: inout ParsedOptions) -> LTOKind? {
598-
guard let arg = parsedOptions.getLastArgument(.lto)?.asSingle else {
597+
private static func ltoKind(_ parsedOptions: inout ParsedOptions,
598+
diagnosticsEngine: DiagnosticsEngine) -> LTOKind? {
599+
guard let arg = parsedOptions.getLastArgument(.lto)?.asSingle else { return nil }
600+
guard let kind = LTOKind(rawValue: arg) else {
601+
diagnosticsEngine.emit(.error_invalid_arg_value(arg: .lto, value: arg))
599602
return nil
600603
}
601-
return LTOKind(rawValue: arg)
604+
return kind
602605
}
603606
}
604607

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2469,6 +2469,18 @@ final class SwiftDriverTests: XCTestCase {
24692469
}
24702470
}
24712471

2472+
func testLTOOption() throws {
2473+
XCTAssertEqual(try Driver(args: ["swiftc"]).lto, nil)
2474+
2475+
XCTAssertEqual(try Driver(args: ["swiftc", "-lto=llvm-thin"]).lto, .llvmThin)
2476+
2477+
XCTAssertEqual(try Driver(args: ["swiftc", "-lto=llvm-full"]).lto, .llvmFull)
2478+
2479+
try assertDriverDiagnostics(args: ["swiftc", "-lto=nop"]) { driver, verify in
2480+
verify.expect(.error("invalid value 'nop' in '-lto='"))
2481+
}
2482+
}
2483+
24722484
func testScanDependenciesOption() throws {
24732485
do {
24742486
var driver = try Driver(args: ["swiftc", "-scan-dependencies", "foo.swift"])

0 commit comments

Comments
 (0)