2014-07-17 08:16:44 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
2013-03-16 02:50:45 +00:00
|
|
|
|
2014-04-28 12:47:08 +00:00
|
|
|
module Language.Haskell.GhcMod.Logger (
|
2014-04-28 12:41:29 +00:00
|
|
|
withLogger
|
2014-04-25 02:08:29 +00:00
|
|
|
, checkErrorPrefix
|
2011-08-24 07:50:26 +00:00
|
|
|
) where
|
|
|
|
|
2015-01-02 23:32:32 +00:00
|
|
|
import Bag (Bag, bagToList)
|
2014-07-15 08:20:35 +00:00
|
|
|
import Control.Applicative ((<$>))
|
2014-03-27 06:32:44 +00:00
|
|
|
import Data.IORef (IORef, newIORef, readIORef, writeIORef, modifyIORef)
|
2015-01-02 23:32:32 +00:00
|
|
|
import Data.List (isPrefixOf)
|
|
|
|
import Data.Maybe (fromMaybe)
|
|
|
|
import ErrUtils (ErrMsg, errMsgShortDoc, errMsgExtraInfo)
|
2014-04-28 04:52:28 +00:00
|
|
|
import Exception (ghandle)
|
2014-05-14 16:05:40 +00:00
|
|
|
import 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-07-12 01:30:06 +00:00
|
|
|
import Language.Haskell.GhcMod.DynFlags (withDynFlags, withCmdFlags)
|
2013-05-17 01:00:01 +00:00
|
|
|
import qualified Language.Haskell.GhcMod.Gap as Gap
|
2014-05-14 16:05:40 +00:00
|
|
|
import Language.Haskell.GhcMod.Convert (convert')
|
|
|
|
import Language.Haskell.GhcMod.Monad
|
2014-07-17 05:30:42 +00:00
|
|
|
import Language.Haskell.GhcMod.Types
|
2015-01-02 23:32:32 +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
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2014-03-18 03:38:04 +00:00
|
|
|
type Builder = [String] -> [String]
|
|
|
|
|
2014-07-15 08:20:35 +00:00
|
|
|
data Log = Log [String] Builder
|
|
|
|
|
|
|
|
newtype LogRef = LogRef (IORef Log)
|
|
|
|
|
|
|
|
emptyLog :: Log
|
|
|
|
emptyLog = Log [] id
|
2014-03-18 03:38:04 +00:00
|
|
|
|
|
|
|
newLogRef :: IO LogRef
|
2014-07-15 08:20:35 +00:00
|
|
|
newLogRef = LogRef <$> newIORef emptyLog
|
2014-03-18 03:38:04 +00:00
|
|
|
|
2014-07-12 09:16:16 +00:00
|
|
|
readAndClearLogRef :: IOish m => LogRef -> GhcModT m String
|
2014-05-14 16:05:40 +00:00
|
|
|
readAndClearLogRef (LogRef ref) = do
|
2014-07-15 08:20:35 +00:00
|
|
|
Log _ b <- liftIO $ readIORef ref
|
|
|
|
liftIO $ writeIORef ref emptyLog
|
2014-05-14 16:05:40 +00:00
|
|
|
convert' (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 ()
|
2014-07-15 08:20:35 +00:00
|
|
|
appendLogRef df (LogRef ref) _ sev src style msg = modifyIORef ref update
|
|
|
|
where
|
|
|
|
l = ppMsg src sev df style msg
|
|
|
|
update lg@(Log ls b)
|
|
|
|
| l `elem` ls = lg
|
|
|
|
| otherwise = Log (l:ls) (b . (l:))
|
2014-03-18 03:38:04 +00:00
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2014-04-28 12:51:39 +00:00
|
|
|
-- | Set the session flag (e.g. "-Wall" or "-w:") then
|
2014-05-14 16:05:40 +00:00
|
|
|
-- executes a body. Logged messages are returned as 'String'.
|
2014-04-28 12:51:39 +00:00
|
|
|
-- Right is success and Left is failure.
|
2014-07-12 09:16:16 +00:00
|
|
|
withLogger :: IOish m
|
|
|
|
=> (DynFlags -> DynFlags)
|
|
|
|
-> GhcModT m ()
|
|
|
|
-> GhcModT m (Either String String)
|
2014-05-14 16:05:40 +00:00
|
|
|
withLogger setDF body = ghandle sourceError $ do
|
2014-07-17 08:16:44 +00:00
|
|
|
logref <- liftIO newLogRef
|
2014-08-14 16:02:58 +00:00
|
|
|
wflags <- filter ("-fno-warn" `isPrefixOf`) . ghcUserOptions <$> options
|
2014-07-17 08:16:44 +00:00
|
|
|
withDynFlags (setLogger logref . setDF) $
|
2014-07-15 08:20:35 +00:00
|
|
|
withCmdFlags wflags $ do
|
|
|
|
body
|
|
|
|
Right <$> readAndClearLogRef logref
|
2014-04-28 03:52:09 +00:00
|
|
|
where
|
|
|
|
setLogger logref df = Gap.setLogAction df $ appendLogRef df logref
|
2014-05-14 16:05:40 +00:00
|
|
|
|
2011-08-24 07:50:26 +00:00
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2014-03-26 03:09:02 +00:00
|
|
|
-- | Converting 'SourceError' to 'String'.
|
2014-07-12 09:16:16 +00:00
|
|
|
sourceError :: IOish m => SourceError -> GhcModT m (Either String String)
|
2014-08-14 18:51:49 +00:00
|
|
|
sourceError err = errBagToStr (srcErrorMessages err)
|
|
|
|
|
|
|
|
errBagToStr :: IOish m => Bag ErrMsg -> GhcModT m (Either String String)
|
2014-08-15 07:32:28 +00:00
|
|
|
errBagToStr = errBagToStr' Left
|
|
|
|
|
|
|
|
errBagToStr' :: IOish m => (String -> a) -> Bag ErrMsg -> GhcModT m a
|
|
|
|
errBagToStr' f err = do
|
2014-05-14 16:05:40 +00:00
|
|
|
dflags <- G.getSessionDynFlags
|
2014-08-15 18:35:21 +00:00
|
|
|
style <- getStyle
|
2014-08-14 18:51:49 +00:00
|
|
|
ret <- convert' (errBagToStrList dflags style err)
|
2014-08-15 07:32:28 +00:00
|
|
|
return $ f ret
|
|
|
|
|
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
|
2014-08-15 07:32:28 +00:00
|
|
|
ppErrMsg dflag style err = ppMsg spn SevError dflag style msg ++ (if null ext then "" else "\n" ++ ext)
|
|
|
|
where
|
|
|
|
spn = Gap.errorMsgSpan err
|
|
|
|
msg = errMsgShortDoc err
|
|
|
|
ext = showPage dflag style (errMsgExtraInfo err)
|
|
|
|
|
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
|
2014-08-18 15:32:14 +00:00
|
|
|
prefix = ppMsgPrefix spn sev dflag style cts
|
2014-08-14 18:51:49 +00:00
|
|
|
|
2014-08-18 15:32:14 +00:00
|
|
|
ppMsgPrefix :: SrcSpan -> Severity-> DynFlags -> PprStyle -> String -> String
|
|
|
|
ppMsgPrefix spn sev dflag _style cts =
|
2014-08-14 18:51:49 +00:00
|
|
|
let defaultPrefix
|
|
|
|
| Gap.isDumpSplices dflag = ""
|
|
|
|
| otherwise = checkErrorPrefix
|
|
|
|
in fromMaybe defaultPrefix $ do
|
2013-03-16 02:50:45 +00:00
|
|
|
(line,col,_,_) <- Gap.getSrcSpan spn
|
|
|
|
file <- normalise <$> Gap.getSrcFile spn
|
|
|
|
let severityCaption = Gap.showSeverityCaption sev
|
2014-08-18 15:32:14 +00:00
|
|
|
pref0 | or (map (\x -> x `isPrefixOf` cts) warningAsErrorPrefixes)
|
|
|
|
= file ++ ":" ++ show line ++ ":" ++ show col ++ ":"
|
|
|
|
| otherwise = file ++ ":" ++ show line ++ ":" ++ show col ++ ":" ++ severityCaption
|
|
|
|
return pref0
|
2014-04-25 02:08:29 +00:00
|
|
|
|
|
|
|
checkErrorPrefix :: String
|
|
|
|
checkErrorPrefix = "Dummy:0:0:Error:"
|
2014-08-18 15:32:14 +00:00
|
|
|
|
|
|
|
warningAsErrorPrefixes :: [String]
|
|
|
|
warningAsErrorPrefixes = ["Couldn't match expected type"
|
|
|
|
, "Couldn't match type"
|
|
|
|
, "No instance for"]
|