Simon Hengel pushed to branch wip/sol/remove-ddump-json at Glasgow Haskell Compiler / GHC
Commits:
-
611f3f3b
by Simon Hengel at 2025-08-10T10:12:54+07:00
7 changed files:
- compiler/GHC/Core/Opt/Monad.hs
- compiler/GHC/Driver/Errors.hs
- compiler/GHC/Driver/Make.hs
- compiler/GHC/Driver/Monad.hs
- compiler/GHC/Types/Error.hs
- compiler/GHC/Utils/Logger.hs
- ghc/GHCi/UI.hs
Changes:
... | ... | @@ -368,7 +368,7 @@ msg msg = do |
368 | 368 | name_ppr_ctx <- getNamePprCtx
|
369 | 369 | let m = case msg of
|
370 | 370 | MCDump doc -> MCDump (dump_sty doc)
|
371 | - MCDiagnostic span severity reason code doc -> UnsafeMCDiagnostic span severity reason code (err_sty doc)
|
|
371 | + UnsafeMCDiagnostic span severity reason code doc diagnostic -> UnsafeMCDiagnostic span severity reason code (err_sty doc) diagnostic
|
|
372 | 372 | MCOutput doc -> MCOutput (user_sty doc)
|
373 | 373 | MCFatal doc -> MCFatal (user_sty doc)
|
374 | 374 | MCInteractive doc -> MCInteractive (user_sty doc)
|
... | ... | @@ -47,18 +47,15 @@ printMessages logger msg_opts opts = mapM_ (printMessage logger msg_opts opts) . |
47 | 47 | |
48 | 48 | printMessage :: forall a. (Diagnostic a) => Logger -> DiagnosticOpts a -> DiagOpts -> MsgEnvelope a -> IO ()
|
49 | 49 | printMessage logger msg_opts opts message = do
|
50 | - decorated <- decorateDiagnostic logflags (messageClass doc) location
|
|
51 | - if log_diags_as_json then do
|
|
52 | - let
|
|
53 | - rendered :: String
|
|
54 | - rendered = renderWithContext (log_default_user_context logflags) decorated
|
|
55 | - |
|
56 | - jsonMessage :: JsonDoc
|
|
57 | - jsonMessage = jsonDiagnostic rendered message
|
|
58 | - |
|
59 | - logJsonMsg logger (messageClass decorated) jsonMessage
|
|
60 | - else do
|
|
61 | - logMsg logger (messageClass decorated)
|
|
50 | + decorated <- decorateDiagnostic logflags location severity reason code doc
|
|
51 | + let
|
|
52 | + rendered :: String
|
|
53 | + rendered = renderWithContext (log_default_user_context logflags) decorated
|
|
54 | + |
|
55 | + jsonMessage :: JsonDoc
|
|
56 | + jsonMessage = jsonDiagnostic rendered message
|
|
57 | + |
|
58 | + logMsg logger $ UnsafeMCDiagnostic location severity reason code decorated jsonMessage
|
|
62 | 59 | where
|
63 | 60 | logflags :: LogFlags
|
64 | 61 | logflags = logFlags logger
|
... | ... | @@ -66,9 +63,6 @@ printMessage logger msg_opts opts message = do |
66 | 63 | doc :: SDoc
|
67 | 64 | doc = updSDocContext (\_ -> ctx) (messageWithHints diagnostic)
|
68 | 65 | |
69 | - messageClass :: SDoc -> Message
|
|
70 | - messageClass = UnsafeMCDiagnostic location severity (errMsgReason message) (diagnosticCode diagnostic)
|
|
71 | - |
|
72 | 66 | style :: PprStyle
|
73 | 67 | style = mkErrStyle (errMsgContext message)
|
74 | 68 | |
... | ... | @@ -84,6 +78,12 @@ printMessage logger msg_opts opts message = do |
84 | 78 | severity :: Severity
|
85 | 79 | severity = errMsgSeverity message
|
86 | 80 | |
81 | + reason :: ResolvedDiagnosticReason
|
|
82 | + reason = errMsgReason message
|
|
83 | + |
|
84 | + code :: Maybe DiagnosticCode
|
|
85 | + code = diagnosticCode diagnostic
|
|
86 | + |
|
87 | 87 | messageWithHints :: a -> SDoc
|
88 | 88 | messageWithHints e =
|
89 | 89 | let main_msg = formatBulleted $ diagnosticMessage msg_opts e
|
... | ... | @@ -93,21 +93,18 @@ printMessage logger msg_opts opts message = do |
93 | 93 | hs -> main_msg $$ hang (text "Suggested fixes:") 2
|
94 | 94 | (formatBulleted $ mkDecorated . map ppr $ hs)
|
95 | 95 | |
96 | - log_diags_as_json :: Bool
|
|
97 | - log_diags_as_json = log_diagnostics_as_json (logFlags logger)
|
|
98 | - |
|
99 | -decorateDiagnostic :: LogFlags -> Message -> SrcSpan -> IO SDoc
|
|
100 | -decorateDiagnostic logflags msg srcSpan = addCaret
|
|
96 | +decorateDiagnostic :: LogFlags -> SrcSpan -> Severity -> ResolvedDiagnosticReason -> Maybe DiagnosticCode -> SDoc -> IO SDoc
|
|
97 | +decorateDiagnostic logflags span severity reason code doc = addCaret
|
|
101 | 98 | where
|
102 | 99 | -- Pretty print the warning flag, if any (#10752)
|
103 | 100 | message :: SDoc
|
104 | - message = mkLocMessageWarningGroups (log_show_warn_groups logflags) msg srcSpan
|
|
101 | + message = formatDiagnostic (log_show_warn_groups logflags) span severity reason code doc
|
|
105 | 102 | |
106 | 103 | addCaret :: IO SDoc
|
107 | 104 | addCaret = do
|
108 | 105 | caretDiagnostic <-
|
109 | 106 | if log_show_caret logflags
|
110 | - then getCaretDiagnostic msg srcSpan
|
|
107 | + then getCaretDiagnostic severity span
|
|
111 | 108 | else pure empty
|
112 | 109 | return $ getPprStyle $ \style ->
|
113 | 110 | withPprStyle (setStyleColoured True style)
|
... | ... | @@ -1445,9 +1445,9 @@ withDeferredDiagnostics f = do |
1445 | 1445 | let deferDiagnostics _dflags !msg = do
|
1446 | 1446 | let action = logMsg logger msg
|
1447 | 1447 | case msg of
|
1448 | - MCDiagnostic _ SevWarning _reason _code _
|
|
1448 | + MCDiagnostic _ SevWarning _reason _code
|
|
1449 | 1449 | -> atomicModifyIORef' warnings $ \(!i) -> (action: i, ())
|
1450 | - MCDiagnostic _ SevError _reason _code _
|
|
1450 | + MCDiagnostic _ SevError _reason _code
|
|
1451 | 1451 | -> atomicModifyIORef' errors $ \(!i) -> (action: i, ())
|
1452 | 1452 | MCFatal _
|
1453 | 1453 | -> atomicModifyIORef' fatals $ \(!i) -> (action: i, ())
|
... | ... | @@ -23,8 +23,6 @@ module GHC.Driver.Monad ( |
23 | 23 | modifyLogger,
|
24 | 24 | pushLogHookM,
|
25 | 25 | popLogHookM,
|
26 | - pushJsonLogHookM,
|
|
27 | - popJsonLogHookM,
|
|
28 | 26 | putLogMsgM,
|
29 | 27 | putMsgM,
|
30 | 28 | withTimingM,
|
... | ... | @@ -122,12 +120,6 @@ pushLogHookM = modifyLogger . pushLogHook |
122 | 120 | popLogHookM :: GhcMonad m => m ()
|
123 | 121 | popLogHookM = modifyLogger popLogHook
|
124 | 122 | |
125 | -pushJsonLogHookM :: GhcMonad m => (LogJsonAction -> LogJsonAction) -> m ()
|
|
126 | -pushJsonLogHookM = modifyLogger . pushJsonLogHook
|
|
127 | - |
|
128 | -popJsonLogHookM :: GhcMonad m => m ()
|
|
129 | -popJsonLogHookM = modifyLogger popJsonLogHook
|
|
130 | - |
|
131 | 123 | -- | Put a log message
|
132 | 124 | putMsgM :: GhcMonad m => SDoc -> m ()
|
133 | 125 | putMsgM doc = do
|
... | ... | @@ -70,7 +70,6 @@ module GHC.Types.Error |
70 | 70 | , mapDecoratedSDoc
|
71 | 71 | |
72 | 72 | , pprMessageBag
|
73 | - , mkLocMessageWarningGroups
|
|
74 | 73 | , formatLocMessage
|
75 | 74 | , formatFatalLocMessage
|
76 | 75 | , formatDiagnostic
|
... | ... | @@ -493,7 +492,7 @@ data Message |
493 | 492 | -- ^ Log messages intended for end users.
|
494 | 493 | -- No file\/line\/column stuff.
|
495 | 494 | |
496 | - | UnsafeMCDiagnostic SrcSpan Severity ResolvedDiagnosticReason (Maybe DiagnosticCode) SDoc
|
|
495 | + | UnsafeMCDiagnostic SrcSpan Severity ResolvedDiagnosticReason (Maybe DiagnosticCode) SDoc JsonDoc
|
|
497 | 496 | -- ^ Diagnostics from the compiler. This constructor is very powerful as
|
498 | 497 | -- it allows the construction of a 'Message' with a completely
|
499 | 498 | -- arbitrary permutation of 'Severity' and 'DiagnosticReason'. As such,
|
... | ... | @@ -509,8 +508,8 @@ data Message |
509 | 508 | -- should always have a 'DiagnosticCode'. See Note [Diagnostic codes].
|
510 | 509 | |
511 | 510 | {-# COMPLETE MCOutput, MCFatal, MCInteractive, MCDump, MCInfo, MCDiagnostic #-}
|
512 | -pattern MCDiagnostic :: SrcSpan -> Severity -> ResolvedDiagnosticReason -> Maybe DiagnosticCode -> SDoc -> Message
|
|
513 | -pattern MCDiagnostic span severity reason code doc <- UnsafeMCDiagnostic span severity reason code doc
|
|
511 | +pattern MCDiagnostic :: SrcSpan -> Severity -> ResolvedDiagnosticReason -> Maybe DiagnosticCode -> Message
|
|
512 | +pattern MCDiagnostic span severity reason code <- UnsafeMCDiagnostic span severity reason code _diagnostic _json
|
|
514 | 513 | |
515 | 514 | {-
|
516 | 515 | Note [Suppressing Messages]
|
... | ... | @@ -635,25 +634,9 @@ showMsgEnvelope err = |
635 | 634 | pprMessageBag :: Bag SDoc -> SDoc
|
636 | 635 | pprMessageBag msgs = vcat (punctuate blankLine (bagToList msgs))
|
637 | 636 | |
638 | --- | Make an error message with location info, specifying whether to show
|
|
639 | --- warning groups (if applicable).
|
|
640 | -mkLocMessageWarningGroups
|
|
641 | - :: Bool -- ^ Print warning groups (if applicable)?
|
|
642 | - -> Message -- ^ message
|
|
643 | - -> SrcSpan -- ^ location
|
|
644 | - -> SDoc
|
|
645 | -mkLocMessageWarningGroups show_warn_groups msg locn
|
|
646 | - = case msg of
|
|
647 | - MCDiagnostic span severity reason code doc -> formatDiagnostic show_warn_groups span severity reason code doc
|
|
648 | - MCFatal doc -> formatFatalLocMessage locn doc
|
|
649 | - MCOutput doc -> formatLocMessage locn doc
|
|
650 | - MCInteractive doc -> formatLocMessage locn doc
|
|
651 | - MCDump doc -> formatLocMessage locn doc
|
|
652 | - MCInfo doc -> formatLocMessage locn doc
|
|
653 | - |
|
654 | 637 | formatFatalLocMessage :: SrcSpan -> SDoc -> SDoc
|
655 | 638 | formatFatalLocMessage locn msg = sdocOption sdocColScheme $ \col_scheme ->
|
656 | - let msg_title = coloured (fatalColour col_scheme) $ text "fatal"
|
|
639 | + let msg_title = coloured (Col.sFatal col_scheme) $ text "fatal"
|
|
657 | 640 | in formatLocMessageWarningGroups locn msg_title empty empty msg
|
658 | 641 | |
659 | 642 | formatLocMessage :: SrcSpan -> SDoc -> SDoc
|
... | ... | @@ -770,23 +753,15 @@ formatLocMessageWarningGroups locn msg_title code_doc warning_flag_doc msg |
770 | 753 | in coloured (Col.sMessage col_scheme)
|
771 | 754 | $ hang (coloured (Col.sHeader col_scheme) header) 4 msg
|
772 | 755 | |
773 | -getMessageClassColour :: Message -> Col.Scheme -> Col.PprColour
|
|
774 | -getMessageClassColour (MCDiagnostic _span severity _reason _code _) = getSeverityColour severity
|
|
775 | -getMessageClassColour (MCFatal _) = fatalColour
|
|
776 | -getMessageClassColour _ = const mempty
|
|
777 | - |
|
778 | -fatalColour :: Col.Scheme -> Col.PprColour
|
|
779 | -fatalColour = Col.sFatal
|
|
780 | - |
|
781 | 756 | getSeverityColour :: Severity -> Col.Scheme -> Col.PprColour
|
782 | 757 | getSeverityColour severity = case severity of
|
783 | 758 | SevError -> Col.sError
|
784 | 759 | SevWarning -> Col.sWarning
|
785 | 760 | SevIgnore -> const mempty
|
786 | 761 | |
787 | -getCaretDiagnostic :: Message -> SrcSpan -> IO SDoc
|
|
762 | +getCaretDiagnostic :: Severity -> SrcSpan -> IO SDoc
|
|
788 | 763 | getCaretDiagnostic _ (UnhelpfulSpan _) = pure empty
|
789 | -getCaretDiagnostic msg (RealSrcSpan span _) =
|
|
764 | +getCaretDiagnostic severity (RealSrcSpan span _) =
|
|
790 | 765 | caretDiagnostic <$> getSrcLine (srcSpanFile span) row
|
791 | 766 | where
|
792 | 767 | getSrcLine fn i =
|
... | ... | @@ -819,7 +794,7 @@ getCaretDiagnostic msg (RealSrcSpan span _) = |
819 | 794 | caretDiagnostic Nothing = empty
|
820 | 795 | caretDiagnostic (Just srcLineWithNewline) =
|
821 | 796 | sdocOption sdocColScheme$ \col_scheme ->
|
822 | - let sevColour = getMessageClassColour msg col_scheme
|
|
797 | + let sevColour = getSeverityColour severity col_scheme
|
|
823 | 798 | marginColour = Col.sMargin col_scheme
|
824 | 799 | in
|
825 | 800 | coloured marginColour (text marginSpace) <>
|
... | ... | @@ -24,7 +24,6 @@ module GHC.Utils.Logger |
24 | 24 | -- * Logger setup
|
25 | 25 | , initLogger
|
26 | 26 | , LogAction
|
27 | - , LogJsonAction
|
|
28 | 27 | , DumpAction
|
29 | 28 | , TraceAction
|
30 | 29 | , DumpFormat (..)
|
... | ... | @@ -32,8 +31,6 @@ module GHC.Utils.Logger |
32 | 31 | -- ** Hooks
|
33 | 32 | , popLogHook
|
34 | 33 | , pushLogHook
|
35 | - , popJsonLogHook
|
|
36 | - , pushJsonLogHook
|
|
37 | 34 | , popDumpHook
|
38 | 35 | , pushDumpHook
|
39 | 36 | , popTraceHook
|
... | ... | @@ -55,11 +52,9 @@ module GHC.Utils.Logger |
55 | 52 | , putLogMsg
|
56 | 53 | , defaultLogAction
|
57 | 54 | , defaultLogActionWithHandles
|
58 | - , defaultLogJsonAction
|
|
59 | 55 | , defaultLogActionHPrintDoc
|
60 | 56 | , defaultLogActionHPutStrDoc
|
61 | 57 | , logMsg
|
62 | - , logJsonMsg
|
|
63 | 58 | , logDumpMsg
|
64 | 59 | |
65 | 60 | -- * Dumping
|
... | ... | @@ -86,8 +81,8 @@ import GHC.Types.Error |
86 | 81 | |
87 | 82 | import qualified GHC.Utils.Ppr as Pretty
|
88 | 83 | import GHC.Utils.Outputable
|
89 | -import GHC.Utils.Json
|
|
90 | 84 | import GHC.Utils.Panic
|
85 | +import GHC.Utils.Json (renderJSON)
|
|
91 | 86 | |
92 | 87 | import GHC.Data.EnumSet (EnumSet)
|
93 | 88 | import qualified GHC.Data.EnumSet as EnumSet
|
... | ... | @@ -181,11 +176,6 @@ type LogAction = LogFlags |
181 | 176 | -> Message
|
182 | 177 | -> IO ()
|
183 | 178 | |
184 | -type LogJsonAction = LogFlags
|
|
185 | - -> Message
|
|
186 | - -> JsonDoc
|
|
187 | - -> IO ()
|
|
188 | - |
|
189 | 179 | type DumpAction = LogFlags
|
190 | 180 | -> PprStyle
|
191 | 181 | -> DumpFlag
|
... | ... | @@ -223,9 +213,6 @@ data Logger = Logger |
223 | 213 | { log_hook :: [LogAction -> LogAction]
|
224 | 214 | -- ^ Log hooks stack
|
225 | 215 | |
226 | - , json_log_hook :: [LogJsonAction -> LogJsonAction]
|
|
227 | - -- ^ Json log hooks stack
|
|
228 | - |
|
229 | 216 | , dump_hook :: [DumpAction -> DumpAction]
|
230 | 217 | -- ^ Dump hooks stack
|
231 | 218 | |
... | ... | @@ -261,7 +248,6 @@ initLogger = do |
261 | 248 | dumps <- newMVar Map.empty
|
262 | 249 | return $ Logger
|
263 | 250 | { log_hook = []
|
264 | - , json_log_hook = []
|
|
265 | 251 | , dump_hook = []
|
266 | 252 | , trace_hook = []
|
267 | 253 | , generated_dumps = dumps
|
... | ... | @@ -273,10 +259,6 @@ initLogger = do |
273 | 259 | putLogMsg :: Logger -> LogAction
|
274 | 260 | putLogMsg logger = foldr ($) defaultLogAction (log_hook logger)
|
275 | 261 | |
276 | --- | Log a JsonDoc
|
|
277 | -putJsonLogMsg :: Logger -> LogJsonAction
|
|
278 | -putJsonLogMsg logger = foldr ($) defaultLogJsonAction (json_log_hook logger)
|
|
279 | - |
|
280 | 262 | -- | Dump something
|
281 | 263 | putDumpFile :: Logger -> DumpAction
|
282 | 264 | putDumpFile logger =
|
... | ... | @@ -301,15 +283,6 @@ popLogHook logger = case log_hook logger of |
301 | 283 | [] -> panic "popLogHook: empty hook stack"
|
302 | 284 | _:hs -> logger { log_hook = hs }
|
303 | 285 | |
304 | --- | Push a json log hook
|
|
305 | -pushJsonLogHook :: (LogJsonAction -> LogJsonAction) -> Logger -> Logger
|
|
306 | -pushJsonLogHook h logger = logger { json_log_hook = h:json_log_hook logger }
|
|
307 | - |
|
308 | -popJsonLogHook :: Logger -> Logger
|
|
309 | -popJsonLogHook logger = case json_log_hook logger of
|
|
310 | - [] -> panic "popJsonLogHook: empty hook stack"
|
|
311 | - _:hs -> logger { json_log_hook = hs}
|
|
312 | - |
|
313 | 286 | -- | Push a dump hook
|
314 | 287 | pushDumpHook :: (DumpAction -> DumpAction) -> Logger -> Logger
|
315 | 288 | pushDumpHook h logger = logger { dump_hook = h:dump_hook logger }
|
... | ... | @@ -353,22 +326,6 @@ makeThreadSafe logger = do |
353 | 326 | $ pushTraceHook trc
|
354 | 327 | $ logger
|
355 | 328 | |
356 | -defaultLogJsonAction :: LogJsonAction
|
|
357 | -defaultLogJsonAction logflags msg_class jsdoc =
|
|
358 | - case msg_class of
|
|
359 | - MCOutput _ -> printOut msg
|
|
360 | - MCDump _ -> printOut (msg $$ blankLine)
|
|
361 | - MCInteractive _ -> putStrSDoc msg
|
|
362 | - MCInfo _ -> printErrs msg
|
|
363 | - MCFatal _ -> printErrs msg
|
|
364 | - MCDiagnostic _ SevIgnore _ _ _ -> pure () -- suppress the message
|
|
365 | - MCDiagnostic _span _sev _rea _code _ -> printErrs msg
|
|
366 | - where
|
|
367 | - printOut = defaultLogActionHPrintDoc logflags False stdout
|
|
368 | - printErrs = defaultLogActionHPrintDoc logflags False stderr
|
|
369 | - putStrSDoc = defaultLogActionHPutStrDoc logflags False stdout
|
|
370 | - msg = renderJSON jsdoc
|
|
371 | - |
|
372 | 329 | -- | The default 'LogAction' prints to 'stdout' and 'stderr'.
|
373 | 330 | --
|
374 | 331 | -- To replicate the default log action behaviour with different @out@ and @err@
|
... | ... | @@ -386,8 +343,12 @@ defaultLogActionWithHandles out err logflags message |
386 | 343 | MCInteractive msg -> putStrSDoc msg
|
387 | 344 | MCInfo msg -> printErrs msg
|
388 | 345 | MCFatal msg -> printErrs msg
|
389 | - MCDiagnostic _ SevIgnore _ _ _ -> pure () -- suppress the message
|
|
390 | - MCDiagnostic _span _sev _rea _code msg -> printErrs msg
|
|
346 | + MCDiagnostic _ SevIgnore _ _ -> pure () -- suppress the message
|
|
347 | + UnsafeMCDiagnostic _span _severity _reason _code doc json -> do
|
|
348 | + if log_diagnostics_as_json logflags then do
|
|
349 | + printErrs (renderJSON json)
|
|
350 | + else do
|
|
351 | + printErrs doc
|
|
391 | 352 | where
|
392 | 353 | printOut = defaultLogActionHPrintDoc logflags False out
|
393 | 354 | printErrs = defaultLogActionHPrintDoc logflags False err
|
... | ... | @@ -534,9 +495,6 @@ defaultTraceAction logflags title doc x = |
534 | 495 | logMsg :: Logger -> Message -> IO ()
|
535 | 496 | logMsg logger = putLogMsg logger (logFlags logger)
|
536 | 497 | |
537 | -logJsonMsg :: Logger -> Message -> JsonDoc -> IO ()
|
|
538 | -logJsonMsg logger mc = putJsonLogMsg logger (logFlags logger) mc
|
|
539 | - |
|
540 | 498 | -- | Dump something
|
541 | 499 | logDumpFile :: Logger -> PprStyle -> DumpFlag -> String -> DumpFormat -> SDoc -> IO ()
|
542 | 500 | logDumpFile logger = putDumpFile logger (logFlags logger)
|
... | ... | @@ -836,7 +836,7 @@ ghciLogAction lastErrLocations old_log_action |
836 | 836 | dflags msg = do
|
837 | 837 | old_log_action dflags msg
|
838 | 838 | case msg of
|
839 | - MCDiagnostic srcSpan SevError _reason _code _ -> case srcSpan of
|
|
839 | + MCDiagnostic srcSpan SevError _reason _code -> case srcSpan of
|
|
840 | 840 | RealSrcSpan rsp _ -> modifyIORef lastErrLocations
|
841 | 841 | (++ [(srcLocFile (realSrcSpanStart rsp), srcLocLine (realSrcSpanStart rsp))])
|
842 | 842 | _ -> return ()
|