2013-11-12 23:56:45 +00:00
|
|
|
{-# LANGUAGE BangPatterns, CPP #-}
|
2013-03-16 02:50:45 +00:00
|
|
|
|
2013-05-17 01:00:01 +00:00
|
|
|
module Language.Haskell.GhcMod.ErrMsg (
|
2011-08-24 07:50:26 +00:00
|
|
|
LogReader
|
2014-04-26 08:54:15 +00:00
|
|
|
, withLogger
|
2011-08-24 07:50:26 +00:00
|
|
|
, handleErrMsg
|
2014-04-25 02:08:29 +00:00
|
|
|
, checkErrorPrefix
|
2011-08-24 07:50:26 +00:00
|
|
|
) where
|
|
|
|
|
2014-03-27 06:32:44 +00:00
|
|
|
import Bag (Bag, bagToList)
|
|
|
|
import Control.Applicative ((<$>))
|
2014-04-26 08:54:15 +00:00
|
|
|
import CoreMonad (liftIO)
|
2014-03-27 06:32:44 +00:00
|
|
|
import Data.IORef (IORef, newIORef, readIORef, writeIORef, modifyIORef)
|
|
|
|
import Data.Maybe (fromMaybe)
|
|
|
|
import ErrUtils (ErrMsg, errMsgShortDoc, errMsgExtraInfo)
|
2014-03-27 11:54:18 +00:00
|
|
|
import GHC (Ghc, DynFlags, SrcSpan, Severity(SevError))
|
2014-03-27 06:32:44 +00:00
|
|
|
import qualified GHC as G
|
|
|
|
import HscTypes (SourceError, srcErrorMessages)
|
2014-04-03 00:49:23 +00:00
|
|
|
import Language.Haskell.GhcMod.Doc (showPage, getStyle)
|
2014-04-26 08:54:15 +00:00
|
|
|
import Language.Haskell.GhcMod.GHCApi (withDynFlags)
|
2013-05-17 01:00:01 +00:00
|
|
|
import qualified Language.Haskell.GhcMod.Gap as Gap
|
2014-04-21 06:58:36 +00:00
|
|
|
import Language.Haskell.GhcMod.Types (Options, convert)
|
2014-04-03 00:49:23 +00:00
|
|
|
import Outputable (PprStyle, SDoc)
|
2013-03-05 06:18:57 +00:00
|
|
|
import System.FilePath (normalise)
|
2011-08-24 07:50:26 +00:00
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2013-09-05 05:35:28 +00:00
|
|
|
-- | A means to read the log.
|
2014-04-21 05:04:58 +00:00
|
|
|
type LogReader = IO String
|
2011-08-24 07:50:26 +00:00
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2014-03-18 03:38:04 +00:00
|
|
|
type Builder = [String] -> [String]
|
|
|
|
|
|
|
|
newtype LogRef = LogRef (IORef Builder)
|
|
|
|
|
|
|
|
newLogRef :: IO LogRef
|
|
|
|
newLogRef = LogRef <$> newIORef id
|
|
|
|
|
2014-04-21 05:04:58 +00:00
|
|
|
readAndClearLogRef :: Options -> LogRef -> IO String
|
|
|
|
readAndClearLogRef opt (LogRef ref) = do
|
2014-03-18 03:38:04 +00:00
|
|
|
b <- readIORef ref
|
|
|
|
writeIORef ref id
|
2014-04-21 05:04:58 +00:00
|
|
|
return $! convert opt (b [])
|
2014-03-18 03:38:04 +00:00
|
|
|
|
2014-04-21 06:58:36 +00:00
|
|
|
appendLogRef :: DynFlags -> LogRef -> DynFlags -> Severity -> SrcSpan -> PprStyle -> SDoc -> IO ()
|
|
|
|
appendLogRef df (LogRef ref) _ sev src style msg = do
|
|
|
|
let !l = ppMsg src sev df style msg
|
2014-03-18 03:38:04 +00:00
|
|
|
modifyIORef ref (\b -> b . (l:))
|
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2014-04-28 03:52:09 +00:00
|
|
|
withLogger :: Options -> (DynFlags -> DynFlags) -> Ghc () -> Ghc String
|
|
|
|
withLogger opt setDF body = do
|
2014-04-26 08:54:15 +00:00
|
|
|
logref <- liftIO $ newLogRef
|
2014-04-28 03:52:09 +00:00
|
|
|
withDynFlags (setLogger logref . setDF) $ do
|
2014-04-26 08:54:15 +00:00
|
|
|
body
|
|
|
|
liftIO $ readAndClearLogRef opt logref
|
2014-04-28 03:52:09 +00:00
|
|
|
where
|
|
|
|
setLogger logref df = Gap.setLogAction df $ appendLogRef df logref
|
2011-08-24 07:50:26 +00:00
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2014-03-26 03:09:02 +00:00
|
|
|
-- | Converting 'SourceError' to 'String'.
|
2014-04-21 05:04:58 +00:00
|
|
|
handleErrMsg :: Options -> SourceError -> Ghc String
|
|
|
|
handleErrMsg opt err = do
|
2014-03-27 06:32:44 +00:00
|
|
|
dflag <- G.getSessionDynFlags
|
2014-04-03 00:49:23 +00:00
|
|
|
style <- getStyle
|
2014-04-21 06:58:36 +00:00
|
|
|
let ret = convert opt . errBagToStrList dflag style . srcErrorMessages $ err
|
2014-04-21 05:04:58 +00:00
|
|
|
return ret
|
2011-08-24 07:50:26 +00:00
|
|
|
|
2014-04-21 06:58:36 +00:00
|
|
|
errBagToStrList :: DynFlags -> PprStyle -> Bag ErrMsg -> [String]
|
|
|
|
errBagToStrList dflag style = map (ppErrMsg dflag style) . reverse . bagToList
|
2011-08-24 07:50:26 +00:00
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2014-04-21 06:58:36 +00:00
|
|
|
ppErrMsg :: DynFlags -> PprStyle -> ErrMsg -> String
|
|
|
|
ppErrMsg dflag style err = ppMsg spn SevError dflag style msg ++ ext
|
2011-08-24 07:50:26 +00:00
|
|
|
where
|
2013-11-19 03:28:59 +00:00
|
|
|
spn = Gap.errorMsgSpan err
|
2013-11-13 00:10:25 +00:00
|
|
|
msg = errMsgShortDoc err
|
2014-04-21 07:12:30 +00:00
|
|
|
ext = showPage dflag style (errMsgExtraInfo err)
|
2011-08-24 07:50:26 +00:00
|
|
|
|
2014-04-21 06:58:36 +00:00
|
|
|
ppMsg :: SrcSpan -> Severity-> DynFlags -> PprStyle -> SDoc -> String
|
|
|
|
ppMsg spn sev dflag style msg = prefix ++ cts
|
2011-08-24 07:50:26 +00:00
|
|
|
where
|
2014-04-21 07:12:30 +00:00
|
|
|
cts = showPage dflag style msg
|
2013-03-16 02:50:45 +00:00
|
|
|
defaultPrefix
|
2014-04-26 04:21:22 +00:00
|
|
|
| Gap.isDumpSplices dflag = ""
|
|
|
|
| otherwise = checkErrorPrefix
|
2013-03-16 02:50:45 +00:00
|
|
|
prefix = fromMaybe defaultPrefix $ do
|
|
|
|
(line,col,_,_) <- Gap.getSrcSpan spn
|
|
|
|
file <- normalise <$> Gap.getSrcFile spn
|
|
|
|
let severityCaption = Gap.showSeverityCaption sev
|
|
|
|
return $ file ++ ":" ++ show line ++ ":" ++ show col ++ ":" ++ severityCaption
|
2014-04-25 02:08:29 +00:00
|
|
|
|
|
|
|
checkErrorPrefix :: String
|
|
|
|
checkErrorPrefix = "Dummy:0:0:Error:"
|