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
|
|
|
|
, setLogger
|
|
|
|
, handleErrMsg
|
|
|
|
) where
|
|
|
|
|
2014-03-27 06:32:44 +00:00
|
|
|
import Bag (Bag, bagToList)
|
|
|
|
import Control.Applicative ((<$>))
|
|
|
|
import Data.IORef (IORef, newIORef, readIORef, writeIORef, modifyIORef)
|
|
|
|
import Data.Maybe (fromMaybe)
|
2014-03-27 11:54:18 +00:00
|
|
|
import DynFlags (dopt)
|
2014-03-27 06:32:44 +00:00
|
|
|
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)
|
2013-05-17 01:00:01 +00:00
|
|
|
import qualified Language.Haskell.GhcMod.Gap as Gap
|
2014-03-27 06:32:44 +00:00
|
|
|
import Language.Haskell.GhcMod.Types (LineSeparator(..))
|
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.
|
2011-08-24 07:50:26 +00:00
|
|
|
type LogReader = IO [String]
|
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2014-03-18 03:38:04 +00:00
|
|
|
type Builder = [String] -> [String]
|
|
|
|
|
|
|
|
newtype LogRef = LogRef (IORef Builder)
|
|
|
|
|
|
|
|
newLogRef :: IO LogRef
|
|
|
|
newLogRef = LogRef <$> newIORef id
|
|
|
|
|
|
|
|
readAndClearLogRef :: LogRef -> IO [String]
|
|
|
|
readAndClearLogRef (LogRef ref) = do
|
|
|
|
b <- readIORef ref
|
|
|
|
writeIORef ref id
|
|
|
|
return $! b []
|
|
|
|
|
2014-04-03 00:49:23 +00:00
|
|
|
appendLogRef :: DynFlags -> LineSeparator -> LogRef -> DynFlags -> Severity -> SrcSpan -> PprStyle -> SDoc -> IO ()
|
|
|
|
appendLogRef df ls (LogRef ref) _ sev src style msg = do
|
|
|
|
let !l = ppMsg src sev df ls style msg
|
2014-03-18 03:38:04 +00:00
|
|
|
modifyIORef ref (\b -> b . (l:))
|
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2013-09-03 05:40:51 +00:00
|
|
|
setLogger :: Bool -> DynFlags -> LineSeparator -> IO (DynFlags, LogReader)
|
|
|
|
setLogger False df _ = return (newdf, undefined)
|
2011-08-24 07:50:26 +00:00
|
|
|
where
|
2012-10-16 10:27:35 +00:00
|
|
|
newdf = Gap.setLogAction df $ \_ _ _ _ _ -> return ()
|
2013-09-03 05:40:51 +00:00
|
|
|
setLogger True df ls = do
|
2014-03-18 03:38:04 +00:00
|
|
|
logref <- newLogRef
|
|
|
|
let newdf = Gap.setLogAction df $ appendLogRef df ls logref
|
|
|
|
return (newdf, readAndClearLogRef logref)
|
2011-08-24 07:50:26 +00:00
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2014-03-26 03:09:02 +00:00
|
|
|
-- | Converting 'SourceError' to 'String'.
|
2013-09-03 05:40:51 +00:00
|
|
|
handleErrMsg :: LineSeparator -> SourceError -> Ghc [String]
|
|
|
|
handleErrMsg ls err = do
|
2014-03-27 06:32:44 +00:00
|
|
|
dflag <- G.getSessionDynFlags
|
2014-04-03 00:49:23 +00:00
|
|
|
style <- getStyle
|
|
|
|
return . errBagToStrList dflag ls style . srcErrorMessages $ err
|
2011-08-24 07:50:26 +00:00
|
|
|
|
2014-04-03 00:49:23 +00:00
|
|
|
errBagToStrList :: DynFlags -> LineSeparator -> PprStyle -> Bag ErrMsg -> [String]
|
|
|
|
errBagToStrList dflag ls style = map (ppErrMsg dflag ls style) . reverse . bagToList
|
2011-08-24 07:50:26 +00:00
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2014-04-03 00:49:23 +00:00
|
|
|
ppErrMsg :: DynFlags -> LineSeparator -> PprStyle -> ErrMsg -> String
|
|
|
|
ppErrMsg dflag ls style err = ppMsg spn SevError dflag ls 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-03 00:49:23 +00:00
|
|
|
ext = showMsg dflag ls style (errMsgExtraInfo err)
|
2011-08-24 07:50:26 +00:00
|
|
|
|
2014-04-03 00:49:23 +00:00
|
|
|
ppMsg :: SrcSpan -> Severity-> DynFlags -> LineSeparator -> PprStyle -> SDoc -> String
|
|
|
|
ppMsg spn sev dflag ls style msg = prefix ++ cts
|
2011-08-24 07:50:26 +00:00
|
|
|
where
|
2014-04-03 00:49:23 +00:00
|
|
|
cts = showMsg dflag ls style msg
|
2013-03-16 02:50:45 +00:00
|
|
|
defaultPrefix
|
2014-03-27 11:54:18 +00:00
|
|
|
| dopt Gap.dumpSplicesFlag dflag = ""
|
|
|
|
| otherwise = "Dummy:0:0:Error:"
|
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
|
2011-08-24 07:50:26 +00:00
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2014-04-03 00:49:23 +00:00
|
|
|
showMsg :: DynFlags -> LineSeparator -> PprStyle -> SDoc -> String
|
|
|
|
showMsg dflag (LineSeparator lsep) style sdoc = replaceNull $ showPage dflag style sdoc
|
2013-09-03 05:40:51 +00:00
|
|
|
where
|
|
|
|
replaceNull [] = []
|
|
|
|
replaceNull ('\n':xs) = lsep ++ replaceNull xs
|
|
|
|
replaceNull (x:xs) = x : replaceNull xs
|