ghc-mod/Language/Haskell/GhcMod/ErrMsg.hs

99 lines
3.1 KiB
Haskell
Raw Normal View History

2013-11-12 23:56:45 +00:00
{-# LANGUAGE BangPatterns, CPP #-}
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
import Bag
import Control.Applicative
import Data.IORef
2012-02-14 07:09:53 +00:00
import Data.Maybe
2011-08-24 07:50:26 +00:00
import DynFlags
import ErrUtils
import GHC
import HscTypes
2013-07-14 08:07:30 +00:00
import Language.Haskell.GhcMod.Doc (showUnqualifiedPage)
2013-09-03 05:40:51 +00:00
import Language.Haskell.GhcMod.Types (LineSeparator(..))
2013-05-17 01:00:01 +00:00
import qualified Language.Haskell.GhcMod.Gap as Gap
2011-08-24 07:50:26 +00:00
import Outputable
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 []
appendLogRef :: DynFlags -> LineSeparator -> LogRef -> a -> Severity -> SrcSpan -> b -> SDoc -> IO ()
appendLogRef df ls (LogRef ref) _ sev src _ msg = do
let !l = ppMsg src sev df ls msg
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
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
----------------------------------------------------------------
2013-09-03 05:40:51 +00:00
handleErrMsg :: LineSeparator -> SourceError -> Ghc [String]
handleErrMsg ls err = do
2013-03-13 01:54:50 +00:00
dflag <- getSessionDynFlags
2013-09-03 05:40:51 +00:00
return . errBagToStrList dflag ls . srcErrorMessages $ err
2011-08-24 07:50:26 +00:00
2013-09-03 05:40:51 +00:00
errBagToStrList :: DynFlags -> LineSeparator -> Bag ErrMsg -> [String]
errBagToStrList dflag ls = map (ppErrMsg dflag ls) . reverse . bagToList
2011-08-24 07:50:26 +00:00
----------------------------------------------------------------
2013-09-03 05:40:51 +00:00
ppErrMsg :: DynFlags -> LineSeparator -> ErrMsg -> String
ppErrMsg dflag ls err = ppMsg spn SevError dflag ls 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
ext = showMsg dflag ls (errMsgExtraInfo err)
2011-08-24 07:50:26 +00:00
2013-09-03 05:40:51 +00:00
ppMsg :: SrcSpan -> Severity-> DynFlags -> LineSeparator -> SDoc -> String
2014-03-26 02:54:34 +00:00
ppMsg spn sev dflag ls msg = prefix ++ cts
2011-08-24 07:50:26 +00:00
where
2013-09-03 05:40:51 +00:00
cts = showMsg dflag ls msg
defaultPrefix
| dopt Opt_D_dump_splices dflag = ""
2014-03-19 01:23:32 +00:00
| otherwise = "Dummy:0:0:Error:"
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
----------------------------------------------------------------
2013-09-03 05:40:51 +00:00
showMsg :: DynFlags -> LineSeparator -> SDoc -> String
showMsg dflag (LineSeparator lsep) sdoc = replaceNull $ showUnqualifiedPage dflag sdoc
where
replaceNull [] = []
replaceNull ('\n':xs) = lsep ++ replaceNull xs
replaceNull (x:xs) = x : replaceNull xs