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)
|
2013-07-14 08:07:30 +00:00
|
|
|
import Language.Haskell.GhcMod.Doc (showUnqualifiedPage)
|
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(..))
|
|
|
|
import Outputable (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 []
|
|
|
|
|
|
|
|
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
|
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
|
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
|
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
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
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
|