-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for GHC JSON diagnostics
- Loading branch information
Showing
22 changed files
with
425 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,7 @@ dependencies: | |
- yaml | ||
- casing | ||
- temporary | ||
- ghc | ||
|
||
ghc-options: -Wall -threaded | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
{-# LANGUAGE DeriveAnyClass #-} | ||
module GHC.Diagnostic where | ||
|
||
import Prelude hiding (span) | ||
import Imports hiding (empty) | ||
import GHC.Generics | ||
import Data.Aeson (ToJSON(..), FromJSON(..), decode) | ||
import Data.ByteString.Lazy (fromStrict) | ||
|
||
import GHC.Types.SrcLoc | ||
import GHC.Types.Error hiding (Severity, Diagnostic) | ||
import GHC.Utils.Error hiding (Severity, Diagnostic) | ||
import qualified GHC.Utils.Error as GHC | ||
import GHC.Utils.Outputable | ||
|
||
data Diagnostic = Diagnostic { | ||
version :: String | ||
, ghcVersion :: String | ||
, span :: Maybe Span | ||
, severity :: Severity | ||
, code :: Maybe Int | ||
, message :: [String] | ||
, hints :: [String] | ||
} deriving (Eq, Show, Generic, ToJSON, FromJSON) | ||
|
||
data Span = Span { | ||
file :: FilePath | ||
, start :: Location | ||
, end :: Location | ||
} deriving (Eq, Show, Generic, ToJSON, FromJSON) | ||
|
||
data Location = Location { | ||
line :: Int | ||
, column :: Int | ||
} deriving (Eq, Show, Generic, ToJSON, FromJSON) | ||
|
||
data Severity = Warning | Error | ||
deriving (Eq, Show, Generic, ToJSON, FromJSON) | ||
|
||
parse :: ByteString -> Maybe Diagnostic | ||
parse = decode . fromStrict | ||
|
||
toSrcSpan :: Maybe Span -> SrcSpan | ||
toSrcSpan = \ case | ||
Nothing -> UnhelpfulSpan UnhelpfulNoLocationInfo | ||
Just span -> mkSrcSpan (loc span.start) (loc span.end) | ||
where | ||
loc l = mkSrcLoc (fromString span.file) l.line l.column | ||
|
||
format :: Diagnostic -> ByteString | ||
format = diagnosticToSDoc >>> showSDocUnsafe >>> encodeUtf8 | ||
|
||
|
||
diagnosticToSDoc :: Diagnostic -> SDoc | ||
diagnosticToSDoc diagnostic = result $+$ blankLine $+$ blankLine | ||
where | ||
result :: SDoc | ||
result = mkLocMessageWarningGroups printWarningGroups msg_class span formatted | ||
|
||
printWarningGroups :: Bool | ||
-- printWarningGroups = True | ||
printWarningGroups = False | ||
|
||
formatted :: SDoc | ||
-- formatted = updSDocContext (\_ -> ctx) messageWithHints | ||
formatted = messageWithHints | ||
|
||
msg_class :: MessageClass | ||
msg_class = MCDiagnostic severity reason code | ||
|
||
reason :: ResolvedDiagnosticReason | ||
-- reason = undefined -- msg.errMsgReason | ||
reason = ResolvedDiagnosticReason ErrorWithoutFlag | ||
|
||
-- style :: PprStyle | ||
-- style = defaultErrStyle | ||
|
||
-- opts :: DiagOpts | ||
-- opts = emptyDiagOpts -- FIXME | ||
|
||
-- ctx :: SDocContext | ||
-- ctx = (diag_ppr_ctx opts) { sdocStyle = style } | ||
|
||
span :: SrcSpan | ||
span = toSrcSpan diagnostic.span | ||
|
||
severity :: GHC.Severity | ||
severity = case diagnostic.severity of | ||
Warning -> SevWarning | ||
Error -> SevError | ||
|
||
|
||
code :: Maybe DiagnosticCode | ||
code = DiagnosticCode "GHC" . fromIntegral <$> diagnostic.code | ||
|
||
hints :: [SDoc] | ||
hints = map verbose diagnostic.hints | ||
|
||
message :: SDoc | ||
message = bulleted $ map verbose diagnostic.message | ||
|
||
messageWithHints :: SDoc | ||
messageWithHints = case hints of | ||
[] -> message | ||
[h] -> message $$ hang (text "Suggested fix:") 2 h | ||
hs -> message $$ hang (text "Suggested fixes:") 2 (bulleted hs) | ||
|
||
bulleted :: [SDoc] -> SDoc | ||
bulleted = formatBulleted . mkDecorated | ||
|
||
verbose :: String -> SDoc | ||
verbose = foldr ($+$) empty . map text . lines | ||
|
||
-- FIXME: check performance impact |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.