ghc-mod/ErrMsg.hs

83 lines
2.1 KiB
Haskell
Raw Normal View History

2011-08-24 07:50:26 +00:00
{-# LANGUAGE CPP #-}
module ErrMsg (
LogReader
, setLogger
, handleErrMsg
) where
import Bag
import Control.Applicative
import Data.IORef
import DynFlags
import ErrUtils
import FastString
import GHC
import HscTypes
import Outputable
import System.FilePath
#if __GLASGOW_HASKELL__ < 702
import Pretty
#endif
----------------------------------------------------------------
type LogReader = IO [String]
----------------------------------------------------------------
setLogger :: Bool -> DynFlags -> IO (DynFlags, LogReader)
setLogger False df = return (newdf, undefined)
where
newdf = df { log_action = \_ _ _ _ -> return () }
setLogger True df = do
ref <- newIORef [] :: IO (IORef [String])
let newdf = df { log_action = appendLog ref }
return (newdf, reverse <$> readIORef ref)
where
2012-02-08 07:57:24 +00:00
appendLog ref _ src stl msg = modifyIORef ref (\ls -> ppMsg src msg stl : ls)
2011-08-24 07:50:26 +00:00
----------------------------------------------------------------
handleErrMsg :: SourceError -> Ghc [String]
handleErrMsg = return . errBagToStrList . srcErrorMessages
errBagToStrList :: Bag ErrMsg -> [String]
errBagToStrList = map ppErrMsg . reverse . bagToList
----------------------------------------------------------------
ppErrMsg :: ErrMsg -> String
2012-02-08 07:57:24 +00:00
ppErrMsg err = ppMsg spn msg defaultUserStyle ++ ext
2011-08-24 07:50:26 +00:00
where
spn = head (errMsgSpans err)
msg = errMsgShortDoc err
2012-02-08 07:57:24 +00:00
ext = showMsg (errMsgExtraInfo err) defaultUserStyle
2011-08-24 07:50:26 +00:00
2012-02-08 07:57:24 +00:00
ppMsg :: SrcSpan -> Message -> PprStyle -> String
2011-08-24 07:50:26 +00:00
#if __GLASGOW_HASKELL__ >= 702
2012-02-08 07:57:24 +00:00
ppMsg (UnhelpfulSpan _) _ _ = undefined
ppMsg (RealSrcSpan src) msg _ = undefined
2011-08-24 07:50:26 +00:00
#else
2012-02-08 07:57:24 +00:00
ppMsg src msg stl
2011-08-24 07:50:26 +00:00
#endif
= file ++ ":" ++ line ++ ":" ++ col ++ ":" ++ cts ++ "\0"
where
file = takeFileName $ unpackFS (srcSpanFile src)
line = show (srcSpanStartLine src)
col = show (srcSpanStartCol src)
2012-02-08 07:57:24 +00:00
cts = showMsg msg stl
2011-08-24 07:50:26 +00:00
----------------------------------------------------------------
2012-02-08 07:57:24 +00:00
showMsg :: SDoc -> PprStyle -> String
2011-08-24 07:50:26 +00:00
#if __GLASGOW_HASKELL__ >= 702
2012-02-08 07:57:24 +00:00
showMsg d stl = map toNull $ renderWithStyle d stl
2011-08-24 07:50:26 +00:00
#else
2012-02-08 07:57:24 +00:00
showMsg d stl = map toNull . Pretty.showDocWith PageMode $ d stl
2011-08-24 07:50:26 +00:00
#endif
where
toNull '\n' = '\0'
toNull x = x