JSON output of GHC Diagnostics

Ben Bellick benbellick at protonmail.com
Sun Jul 9 21:46:23 UTC 2023


Hey everyone,

I am working on a summer of Haskell project to implement JSON output of GHC's diagnostics. I have begun working through some of the implementation details but have stumbled across a few road blocks and I wanted to get some feedback on. Discussion surrounding the plan can be found [here](https://github.com/haskellfoundation/tech-proposals/pull/50).

In looking through the current implementation of the -ddump-json​ flag, I found a [note](https://gitlab.haskell.org/ghc/ghc/-/blob/master/compiler/GHC/Utils/Logger.hs#L392) which seems to indicate that a sensible (albeit invasive) approach is to alter the definition of [LogAction](https://gitlab.haskell.org/ghc/ghc/-/blob/master/compiler/GHC/Utils/Logger.hs#L174), reproduced below:

type

LogAction

=

LogFlags

->

MessageClass

->

SrcSpan

->

SDoc

->

IO

()

So instead of taking in an SDoc​, I could alter this signature to take a set of Diagnostic​s, though this may introduce other complications. In the first place, it seems log action is processing all of the diagnostics one at a time, though this particular JSON output should process all of them at once in order to place them all in a single JSON object. So, I am inclined to believe that this approach may not work.

Another approach could be the one originally tried and mentioned in the note linked above, which is to keep track of all diagnostics in an IORef​, and use this to produce a final JSON result at the end of GHC's execution. This approach seems invasive in a different way.

Does anyone familiar with the handling of diagnostics throughout the compiler have any suggestions on how best to implement such a feature?

You can find the work that I have done so far [here](https://gitlab.haskell.org/benbellick/ghc/-/compare/master...json-diag-dump). All I am really doing is checking if the new flag is set in [printMessages](https://gitlab.haskell.org/benbellick/ghc/-/compare/master...json-diag-dump#2c680115d7e70b54a18c48be02dce2d676eaf4f8_17_18). If it is, then I simply invoke the json​ function of the ToJson​ typeclass and use that to print the diagnostic.

It is currently a bit messy, but I wanted to just see if the path that I am taking seems fruitful or if there are any obvious hurdles that I am not seeing. Thanks so much!

Best,
Ben Bellick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-devs/attachments/20230709/13815b7e/attachment.html>


More information about the ghc-devs mailing list