Skip to content

Refactor globals/ErrorSinkCompiler so sarif / other sinks can extend it cleanly#23176

Open
dkorpel wants to merge 2 commits into
dlang:masterfrom
dkorpel:sarif-refactor
Open

Refactor globals/ErrorSinkCompiler so sarif / other sinks can extend it cleanly#23176
dkorpel wants to merge 2 commits into
dlang:masterfrom
dkorpel:sarif-refactor

Conversation

@dkorpel
Copy link
Copy Markdown
Contributor

@dkorpel dkorpel commented May 26, 2026

SARIF output has been hacked in with global variables and if (global.params.v.messageStyle == MessageStyle.sarif) conditions. Meanwhile there's a WIP ErrorSink refactor that is supposed to cleanly separate custom presentation from errors.d, so here's a refactor to clean up the tangle.

Put the limiting / gating logic in ErrorSinkCompiler,
and make ErrorSinkSarif extend from that, separating it from dmd.errors.
Move enum ErrorKind from errors.d to errorsink.d so error sinks can use it without importing errors.d.

  • Remove import dmd.console, dmd.errors from dmd.sarif
  • Remove reliance on global.params.v and errors.d
  • Remove redundant __gshared Diagnostic[] collection
  • Remove redundant struct SarifReport indirection

private extern(C++) void vreportDiagnostic logic is moved into ErrorSinkCompiler. It's private and has no header so I assume it's not used by LDC or GDC. The global gagging/error count, and warning/deprecation as error logic is intact.

This PR is quite big so I can split it up if it's too hard to review or too risky.

@dkorpel dkorpel added the Severity:Refactoring No semantic changes to code label May 26, 2026
@dlang-bot
Copy link
Copy Markdown
Contributor

Thanks for your pull request and interest in making D better, @dkorpel! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + dmd#23176"

@rikkimax
Copy link
Copy Markdown
Contributor

Feel free to ping me once this is closer to being ready.

I don't trust what I'm seeing atm to approve it.

@dkorpel
Copy link
Copy Markdown
Contributor Author

dkorpel commented May 26, 2026

Yeah I'm still figuring out some kinks, there's some funny interactions with stuff like:

-dip25 -de
// Deprecation: `-dip25` no longer has any effect
// (compilation continues)

-de -dip25
// Deprecation: `-dip25` no longer has any effect
// (treated as error)

That is, the error sink can be modified by -v flags after it has been used already to print an error/deprecation for command line parsing.

Copy link
Copy Markdown
Contributor

@thewilsonator thewilsonator left a comment

Choose a reason for hiding this comment

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

this would be much easier to review if split into
global.params.v.foo -> global.errorSink.foo (foo = {errorLimit,useWarnings, useDeprecated,showGaggedErrors}) as this changes lots of files and makes the changes to errors, errorsink and sarif much harder to see.

Also the PR title makes this sound like a refactoring of sarif, but it is just as much a refactoring of errors/errorsink. If it is possible to split those changes apart that would be much nicer, failing that please update the PR title and commit messages accordingly.

Comment thread compiler/src/dmd/errors.d
dkorpel and others added 2 commits May 27, 2026 12:49
Restructure the diagnostic pipeline so the SARIF output format is a
subclass of the regular compiler error sink instead of a separate
procedural path:

- Move the gating logic (gag handling, error limit, warning and
  deprecation modes) into `ErrorSinkCompiler` and expose a single
  virtual `emit` hook for output customisation.
- Introduce `ErrorSinkSarif` in `dmd.sarif` that extends
  `ErrorSinkCompiler` and overrides `emit`/`plugSink` to build the
  SARIF JSON document; `dmd.errors` no longer imports `dmd.sarif`.
- Drop the global `Diagnostic[]` collection and the `SarifReport`
  indirection in favour of the sink's own `OutBuffer`.
- Move `ErrorKind` from `dmd.errors` to `dmd.errorsink` so the SARIF
  sink does not need to import `dmd.errors`.
- Type `global.errorSink` as `ErrorSinkCompiler` so the gating-state
  fields can be reached without a cast.
- In `main`, swap to `ErrorSinkSarif` once the CLI confirms
  `-verror-style=sarif`.

Behaviour is unchanged; gating still reads its inputs from
`global.params` for now. A follow-up commit migrates those fields onto
`ErrorSinkCompiler`.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
…ErrorSinkCompiler

These four settings used to live on `Param`/`Verbose`. The error sink
now owns them directly, which removes the need for the gating code in
`dmd.errors` to reach across to `global.params`.

Mechanical migration:

- `global.params.useDeprecated`      -> `global.errorSink.useDeprecated`
- `global.params.useWarnings`        -> `global.errorSink.useWarnings`
- `global.params.v.showGaggedErrors` -> `global.errorSink.showGaggedErrors`
- The CLI handlers in `dmd.mars` write straight into the sink.
- `Param.useDeprecated`, `Param.useWarnings`, and
  `Verbose.showGaggedErrors` are removed; `frontend.h` is regenerated.
- `errorLimit` keeps its CLI staging field on `Verbose` (parsed by
  `-verrors=N`) and is mirrored into the sink so it is in effect when
  the very next argument is processed.

No behaviour change.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@dkorpel dkorpel changed the title Make sarif use the ErrorSink interface Refactor globals/ErrorSinkCompiler so sarif / other sinks can extend it cleanly May 27, 2026
@dkorpel dkorpel marked this pull request as ready for review May 27, 2026 11:08
@dkorpel dkorpel requested a review from ibuclaw as a code owner May 27, 2026 11:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Severity:Refactoring No semantic changes to code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants