Skip to content

Commit a7104aa

Browse files
authored
Allow clients of Driver to pass DiagnosticsHandler (#1317)
Uses of `DiagnosticsEngine` are deprecated in SwiftPM, since it uses a different logging infrastructure based on `ObservabilityScope` type. Resolving that deprecation warning is possible if `Driver.init` is able to take a diagnostics handler directly.
1 parent fd78cd6 commit a7104aa

File tree

6 files changed

+48
-9
lines changed

6 files changed

+48
-9
lines changed

Sources/SwiftDriver/Driver/Driver.swift

+41-2
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ public struct Driver {
143143
}
144144
}
145145

146+
/// Specific implementation of a diagnostics output type that can be used when initializing a new `Driver`.
147+
public enum DiagnosticsOutput {
148+
case engine(DiagnosticsEngine)
149+
case handler(DiagnosticsEngine.DiagnosticsHandler)
150+
}
151+
146152
/// The set of environment variables that are visible to the driver and
147153
/// processes it launches. This is a hook for testing; in actual use
148154
/// it should be identical to the real environment.
@@ -452,13 +458,38 @@ public struct Driver {
452458
}
453459
}
454460

461+
@available(*, deprecated, renamed: "init(args:env:diagnosticsOutput:fileSystem:executor:integratedDriver:compilerExecutableDir:externalTargetModuleDetailsMap:interModuleDependencyOracle:)")
462+
public init(
463+
args: [String],
464+
env: [String: String] = ProcessEnv.vars,
465+
diagnosticsEngine: DiagnosticsEngine,
466+
fileSystem: FileSystem = localFileSystem,
467+
executor: DriverExecutor,
468+
integratedDriver: Bool = true,
469+
compilerExecutableDir: AbsolutePath? = nil,
470+
externalTargetModuleDetailsMap: ExternalTargetModuleDetailsMap? = nil,
471+
interModuleDependencyOracle: InterModuleDependencyOracle? = nil
472+
) throws {
473+
try self.init(
474+
args: args,
475+
env: env,
476+
diagnosticsOutput: .engine(diagnosticsEngine),
477+
fileSystem: fileSystem,
478+
executor: executor,
479+
integratedDriver: integratedDriver,
480+
compilerExecutableDir: compilerExecutableDir,
481+
externalTargetModuleDetailsMap: externalTargetModuleDetailsMap,
482+
interModuleDependencyOracle: interModuleDependencyOracle
483+
)
484+
}
485+
455486
/// Create the driver with the given arguments.
456487
///
457488
/// - Parameter args: The command-line arguments, including the "swift" or "swiftc"
458489
/// at the beginning.
459490
/// - Parameter env: The environment variables to use. This is a hook for testing;
460491
/// in production, you should use the default argument, which copies the current environment.
461-
/// - Parameter diagnosticsEngine: The diagnostic engine used by the driver to emit errors
492+
/// - Parameter diagnosticsOutput: The diagnostics output implementation used by the driver to emit errors
462493
/// and warnings.
463494
/// - Parameter fileSystem: The filesystem used by the driver to find resources/SDKs,
464495
/// expand response files, etc. By default this is the local filesystem.
@@ -476,7 +507,7 @@ public struct Driver {
476507
public init(
477508
args: [String],
478509
env: [String: String] = ProcessEnv.vars,
479-
diagnosticsEngine: DiagnosticsEngine = DiagnosticsEngine(handlers: [Driver.stderrDiagnosticsHandler]),
510+
diagnosticsOutput: DiagnosticsOutput = .engine(DiagnosticsEngine(handlers: [Driver.stderrDiagnosticsHandler])),
480511
fileSystem: FileSystem = localFileSystem,
481512
executor: DriverExecutor,
482513
integratedDriver: Bool = true,
@@ -488,7 +519,15 @@ public struct Driver {
488519
self.fileSystem = fileSystem
489520
self.integratedDriver = integratedDriver
490521

522+
let diagnosticsEngine: DiagnosticsEngine
523+
switch diagnosticsOutput {
524+
case .engine(let engine):
525+
diagnosticsEngine = engine
526+
case .handler(let handler):
527+
diagnosticsEngine = DiagnosticsEngine(handlers: [handler])
528+
}
491529
self.diagnosticEngine = diagnosticsEngine
530+
492531
self.executor = executor
493532
self.externalTargetModuleDetailsMap = externalTargetModuleDetailsMap
494533

Sources/SwiftDriver/ToolingInterface/ToolingUtil.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public func getSingleFrontendInvocationFromDriverArguments(argList: [String],
7878
fileSystem: localFileSystem,
7979
env: ProcessEnv.vars)
8080
var driver = try Driver(args: parsedOptions.commandLine,
81-
diagnosticsEngine: diagnosticsEngine,
81+
diagnosticsOutput: .engine(diagnosticsEngine),
8282
executor: executor)
8383
if diagnosticsEngine.hasErrors {
8484
return true

Sources/swift-build-sdk-interfaces/main.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ do {
156156
}
157157
let baselineABIDir = try getArgumentAsPath("-baseline-abi-dir")
158158
var driver = try Driver(args: args,
159-
diagnosticsEngine: diagnosticsEngine,
159+
diagnosticsOutput: .engine(diagnosticsEngine),
160160
executor: executor,
161161
compilerExecutableDir: swiftcPath.parentDirectory)
162162
let (jobs, danglingJobs) = try driver.generatePrebuiltModuleGenerationJobs(with: inputMap,

Sources/swift-driver/main.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ do {
9393
fileSystem: localFileSystem,
9494
env: ProcessEnv.vars)
9595
var driver = try Driver(args: arguments,
96-
diagnosticsEngine: diagnosticsEngine,
96+
diagnosticsOutput: .engine(diagnosticsEngine),
9797
executor: executor,
9898
integratedDriver: false)
9999

Tests/SwiftDriverTests/JobExecutorTests.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ final class JobExecutorTests: XCTestCase {
494494
"-driver-filelist-threshold", "0",
495495
"-o", outputPath.pathString] + getHostToolchainSdkArg(executor),
496496
env: ProcessEnv.vars,
497-
diagnosticsEngine: diags,
497+
diagnosticsOutput: .engine(diags),
498498
fileSystem: localFileSystem,
499499
executor: executor)
500500
let jobs = try driver.planBuild()
@@ -532,7 +532,7 @@ final class JobExecutorTests: XCTestCase {
532532
"-driver-filelist-threshold", "0",
533533
"-o", outputPath.pathString] + getHostToolchainSdkArg(executor),
534534
env: ProcessEnv.vars,
535-
diagnosticsEngine: diags,
535+
diagnosticsOutput: .engine(diags),
536536
fileSystem: localFileSystem,
537537
executor: executor)
538538
let jobs = try driver.planBuild()
@@ -570,7 +570,7 @@ final class JobExecutorTests: XCTestCase {
570570
"-Xfrontend", "-debug-crash-immediately",
571571
"-o", outputPath.pathString] + getHostToolchainSdkArg(executor),
572572
env: ProcessEnv.vars,
573-
diagnosticsEngine: diags,
573+
diagnosticsOutput: .engine(diags),
574574
fileSystem: localFileSystem,
575575
executor: executor)
576576
let jobs = try driver.planBuild()

Tests/TestUtilities/DriverExtensions.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ extension Driver {
3131
env: env)
3232
try self.init(args: args,
3333
env: env,
34-
diagnosticsEngine: diagnosticsEngine,
34+
diagnosticsOutput: .engine(diagnosticsEngine),
3535
fileSystem: fileSystem,
3636
executor: executor,
3737
integratedDriver: integratedDriver)

0 commit comments

Comments
 (0)