<div style="font-family: Arial, sans-serif; font-size: 14px;">Hey everyone, </div><div style="font-family: Arial, sans-serif; font-size: 14px;"><br></div><div style="font-family: Arial, sans-serif; font-size: 14px;">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 <a title="here" href="https://github.com/haskellfoundation/tech-proposals/pull/50" rel="noreferrer nofollow noopener" target="_blank">here</a>.</div><div style="font-family: Arial, sans-serif; font-size: 14px;"><br></div><div style="font-family: Arial, sans-serif; font-size: 14px;">In looking through the current implementation of the <code>-ddump-json</code>​ flag, I found a <a title="note" href="https://gitlab.haskell.org/ghc/ghc/-/blob/master/compiler/GHC/Utils/Logger.hs#L392" rel="noreferrer nofollow noopener" target="_blank">note</a>  which seems to indicate that a sensible (albeit invasive) approach is to alter the definition of <code><a title="LogAction" href="https://gitlab.haskell.org/ghc/ghc/-/blob/master/compiler/GHC/Utils/Logger.hs#L174" rel="noreferrer nofollow noopener" target="_blank">LogAction</a><span style="font-family: Arial, sans-serif;">, reproduced below:</span></code></div><div style="font-family: Arial, sans-serif; font-size: 14px;"><pre lang="haskell"><span data-testid="content" lang="haskell"><span><span>type</span><span> </span><span>LogAction</span><span> = </span><span>LogFlags</span></span><span></span></span>
<span data-testid="content" lang="haskell"><span>              </span><span>-></span><span> </span><span>MessageClass</span><span></span></span>
<span data-testid="content" lang="haskell"><span>              </span><span>-></span><span> </span><span>SrcSpan</span><span></span></span>
<span data-testid="content" lang="haskell"><span>              </span><span>-></span><span> </span><span>SDoc</span><span></span></span>
<span data-testid="content" lang="haskell"><span>              </span><span>-></span><span> </span><span>IO</span><span> ()</span></span>
</pre>So instead of taking in an <code>SDoc</code>​, I could alter this signature to take a set of <code>Diagnostic</code>​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. </div><div style="font-family: Arial, sans-serif; font-size: 14px;"><br></div><div style="font-family: Arial, sans-serif; font-size: 14px;">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 <code>IORef</code>​, and use this to produce a final JSON result at the end of GHC's execution. This approach seems invasive in a different way.</div><div style="font-family: Arial, sans-serif; font-size: 14px;"><br>Does anyone familiar with the handling of diagnostics throughout the compiler have any suggestions on how best to implement such a feature? </div><div style="font-family: Arial, sans-serif; font-size: 14px;"><br></div><div style="font-family: Arial, sans-serif; font-size: 14px;">You can find the work that I have done so far <a title="here" href="https://gitlab.haskell.org/benbellick/ghc/-/compare/master...json-diag-dump" rel="noreferrer nofollow noopener" target="_blank">here</a>. All I am really doing is checking if the new flag is set in <code><a href="https://gitlab.haskell.org/benbellick/ghc/-/compare/master...json-diag-dump#2c680115d7e70b54a18c48be02dce2d676eaf4f8_17_18" title="printMessages">printMessages</a></code>. If it is, then I simply invoke the <code>json</code>​ function of the <code>ToJson</code>​ typeclass and use that to print the diagnostic.</div><div style="font-family: Arial, sans-serif; font-size: 14px;"><br></div><div style="font-family: Arial, sans-serif; font-size: 14px;">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!</div><div style="font-family: Arial, sans-serif; font-size: 14px;"><br></div>
<div style="font-family: Arial, sans-serif; font-size: 14px;" class="protonmail_signature_block">
    <div class="protonmail_signature_block-user">
        <div>Best, <br></div><div>Ben Bellick</div>
    </div>

            <div class="protonmail_signature_block-proton protonmail_signature_block-empty">

            </div>
</div>