Skip to content

Allow clients of Driver to pass DiagnosticsHandler #1317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions Sources/SwiftDriver/Driver/Driver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ public struct Driver {
}
}

/// Specific implementation of a diagnostics output type that can be used when initializing a new `Driver`.
public enum DiagnosticsOutput {
case engine(DiagnosticsEngine)
case handler(DiagnosticsEngine.DiagnosticsHandler)
}

/// The set of environment variables that are visible to the driver and
/// processes it launches. This is a hook for testing; in actual use
/// it should be identical to the real environment.
Expand Down Expand Up @@ -452,13 +458,38 @@ public struct Driver {
}
}

@available(*, deprecated, renamed: "init(args:env:diagnosticsOutput:fileSystem:executor:integratedDriver:compilerExecutableDir:externalTargetModuleDetailsMap:interModuleDependencyOracle:)")
public init(
args: [String],
env: [String: String] = ProcessEnv.vars,
diagnosticsEngine: DiagnosticsEngine,
fileSystem: FileSystem = localFileSystem,
executor: DriverExecutor,
integratedDriver: Bool = true,
compilerExecutableDir: AbsolutePath? = nil,
externalTargetModuleDetailsMap: ExternalTargetModuleDetailsMap? = nil,
interModuleDependencyOracle: InterModuleDependencyOracle? = nil
) throws {
try self.init(
args: args,
env: env,
diagnosticsOutput: .engine(diagnosticsEngine),
fileSystem: fileSystem,
executor: executor,
integratedDriver: integratedDriver,
compilerExecutableDir: compilerExecutableDir,
externalTargetModuleDetailsMap: externalTargetModuleDetailsMap,
interModuleDependencyOracle: interModuleDependencyOracle
)
}

/// Create the driver with the given arguments.
///
/// - Parameter args: The command-line arguments, including the "swift" or "swiftc"
/// at the beginning.
/// - Parameter env: The environment variables to use. This is a hook for testing;
/// in production, you should use the default argument, which copies the current environment.
/// - Parameter diagnosticsEngine: The diagnostic engine used by the driver to emit errors
/// - Parameter diagnosticsOutput: The diagnostics output implementation used by the driver to emit errors
/// and warnings.
/// - Parameter fileSystem: The filesystem used by the driver to find resources/SDKs,
/// expand response files, etc. By default this is the local filesystem.
Expand All @@ -476,7 +507,7 @@ public struct Driver {
public init(
args: [String],
env: [String: String] = ProcessEnv.vars,
diagnosticsEngine: DiagnosticsEngine = DiagnosticsEngine(handlers: [Driver.stderrDiagnosticsHandler]),
diagnosticsOutput: DiagnosticsOutput = .engine(DiagnosticsEngine(handlers: [Driver.stderrDiagnosticsHandler])),
fileSystem: FileSystem = localFileSystem,
executor: DriverExecutor,
integratedDriver: Bool = true,
Expand All @@ -488,7 +519,15 @@ public struct Driver {
self.fileSystem = fileSystem
self.integratedDriver = integratedDriver

let diagnosticsEngine: DiagnosticsEngine
switch diagnosticsOutput {
case .engine(let engine):
diagnosticsEngine = engine
case .handler(let handler):
diagnosticsEngine = DiagnosticsEngine(handlers: [handler])
}
self.diagnosticEngine = diagnosticsEngine

self.executor = executor
self.externalTargetModuleDetailsMap = externalTargetModuleDetailsMap

Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftDriver/ToolingInterface/ToolingUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public func getSingleFrontendInvocationFromDriverArguments(argList: [String],
fileSystem: localFileSystem,
env: ProcessEnv.vars)
var driver = try Driver(args: parsedOptions.commandLine,
diagnosticsEngine: diagnosticsEngine,
diagnosticsOutput: .engine(diagnosticsEngine),
executor: executor)
if diagnosticsEngine.hasErrors {
return true
Expand Down
2 changes: 1 addition & 1 deletion Sources/swift-build-sdk-interfaces/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ do {
}
let baselineABIDir = try getArgumentAsPath("-baseline-abi-dir")
var driver = try Driver(args: args,
diagnosticsEngine: diagnosticsEngine,
diagnosticsOutput: .engine(diagnosticsEngine),
executor: executor,
compilerExecutableDir: swiftcPath.parentDirectory)
let (jobs, danglingJobs) = try driver.generatePrebuiltModuleGenerationJobs(with: inputMap,
Expand Down
2 changes: 1 addition & 1 deletion Sources/swift-driver/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ do {
fileSystem: localFileSystem,
env: ProcessEnv.vars)
var driver = try Driver(args: arguments,
diagnosticsEngine: diagnosticsEngine,
diagnosticsOutput: .engine(diagnosticsEngine),
executor: executor,
integratedDriver: false)

Expand Down
6 changes: 3 additions & 3 deletions Tests/SwiftDriverTests/JobExecutorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ final class JobExecutorTests: XCTestCase {
"-driver-filelist-threshold", "0",
"-o", outputPath.pathString] + getHostToolchainSdkArg(executor),
env: ProcessEnv.vars,
diagnosticsEngine: diags,
diagnosticsOutput: .engine(diags),
fileSystem: localFileSystem,
executor: executor)
let jobs = try driver.planBuild()
Expand Down Expand Up @@ -532,7 +532,7 @@ final class JobExecutorTests: XCTestCase {
"-driver-filelist-threshold", "0",
"-o", outputPath.pathString] + getHostToolchainSdkArg(executor),
env: ProcessEnv.vars,
diagnosticsEngine: diags,
diagnosticsOutput: .engine(diags),
fileSystem: localFileSystem,
executor: executor)
let jobs = try driver.planBuild()
Expand Down Expand Up @@ -570,7 +570,7 @@ final class JobExecutorTests: XCTestCase {
"-Xfrontend", "-debug-crash-immediately",
"-o", outputPath.pathString] + getHostToolchainSdkArg(executor),
env: ProcessEnv.vars,
diagnosticsEngine: diags,
diagnosticsOutput: .engine(diags),
fileSystem: localFileSystem,
executor: executor)
let jobs = try driver.planBuild()
Expand Down
2 changes: 1 addition & 1 deletion Tests/TestUtilities/DriverExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extension Driver {
env: env)
try self.init(args: args,
env: env,
diagnosticsEngine: diagnosticsEngine,
diagnosticsOutput: .engine(diagnosticsEngine),
fileSystem: fileSystem,
executor: executor,
integratedDriver: integratedDriver)
Expand Down