Skip to content

Support -serialize-diagnostics=<format> in swift-driver - #2232

Draft
aviralg wants to merge 1 commit into
swiftlang:mainfrom
aviralg:aviralg/sarif-diagnostics
Draft

aviralg wants to merge 1 commit into
swiftlang:mainfrom
aviralg:aviralg/sarif-diagnostics

Conversation

@aviralg

@aviralg aviralg commented Oct 12, 2026

Copy link
Copy Markdown

This change updates swift-driver to match the C++ driver:

  • Recognize -serialize-diagnostics=<format> wherever -serialize-diagnostics was recognized, so the format flag enables serialization by itself. A format other than dia or sarif is rejected.

  • Choose the output file type from the requested format. This covers compile and PCH jobs, and also the emit-module and dependency scan jobs, which only swift-driver plans. Their output file map entries are sarif-diagnostics, emit-module-sarif-diagnostics and dependency-scan-sarif-diagnostics.

  • Pass the format alongside the path it belongs to, rather than to every frontend job. The flag enables serialization on its own, so a job with no diagnostics output would otherwise start writing a log the driver knows nothing about.

  • Keep the SARIF log of a failed compile job, just as the .dia file is kept, when the job's other outputs are removed.

@aviralg
aviralg force-pushed the aviralg/sarif-diagnostics branch 4 times, most recently from 179454c to 53d1c77 Compare October 12, 2026 18:54
Comment thread Sources/SwiftDriver/Driver/Driver.swift Outdated
let serializedDiagnosticsFilePath: VirtualPath.Handle?

/// The file type diagnostics are serialized to.
let serializedDiagnosticsType: FileType

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think I'd prefer "format" to "type" in these.

public static let serializeDependencyScanCache: Option = Option("-serialize-dependency-scan-cache", .flag, attributes: [.frontend, .noDriver], helpText: "After performing a dependency scan, serialize the scanner's internal state.")
public static let serializeDiagnosticsPathEQ: Option = Option("-serialize-diagnostics-path=", .joined, alias: Option.serializeDiagnosticsPath, attributes: [.frontend, .noBatch, .argumentIsPath, .supplementaryOutput, .cacheInvariant])
public static let serializeDiagnosticsPath: Option = Option("-serialize-diagnostics-path", .separate, attributes: [.frontend, .noBatch, .argumentIsPath, .supplementaryOutput, .cacheInvariant], metaVar: "<path>", helpText: "Emit a serialized diagnostics file to <path>")
public static let serializeDiagnosticsEQ: Option = Option("-serialize-diagnostics=", .joined, attributes: [.frontend, .noInteractive, .supplementaryOutput, .cacheInvariant], metaVar: "<format>", helpText: "Serialize diagnostics in a specific format: 'dia', a binary format (the default), or 'sarif'")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A reminder that these need to be generated with:
https://github.com/swiftlang/swift-driver#rebuilding-optionsswift
In case it wasn't already.

@aviralg
aviralg force-pushed the aviralg/sarif-diagnostics branch from 53d1c77 to 805ffdc Compare October 15, 2026 20:42
This change updates swift-driver to match the C++ driver:

- Recognize `-serialize-diagnostics=<format>` wherever `-serialize-diagnostics`
was recognized, so the format flag enables serialization by itself.

- Choose the output file type from the requested format. This covers compile and
PCH jobs, and also the emit-module and dependency scan jobs, which only
swift-driver plans. Their output file map entries are `sarif-diagnostics`,
`emit-module-sarif-diagnostics` and `dependency-scan-sarif-diagnostics`.

- Pass the format alongside the path it belongs to, rather than to every
frontend job. The flag enables serialization on its own, so a job with no
diagnostics output would otherwise start writing a log the driver knows nothing
about.

- Keep the SARIF log of a failed compile job, just as the `.dia` file is kept,
when the job's other outputs are removed.

The arguments are read as the frontend reads them: the bare
`-serialize-diagnostics` means `=dia`, the last of the two forms wins, and an
unknown format is an error wherever it appears.
@aviralg
aviralg force-pushed the aviralg/sarif-diagnostics branch from 805ffdc to 5440f5b Compare October 21, 2026 18:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants