2016-05-22 00:55:06 +00:00
|
|
|
{-# LANGUAGE CPP, RankNTypes #-}
|
2016-02-04 18:54:55 +00:00
|
|
|
|
2017-05-28 02:22:56 +00:00
|
|
|
module GhcMod.Logger (
|
2014-04-28 12:41:29 +00:00
|
|
|
withLogger
|
2015-03-03 20:12:43 +00:00
|
|
|
, withLogger'
|
2014-04-25 02:08:29 +00:00
|
|
|
, checkErrorPrefix
|
2015-04-02 23:15:12 +00:00
|
|
|
, errsToStr
|
2015-06-19 15:15:14 +00:00
|
|
|
, errBagToStrList
|
2011-08-24 07:50:26 +00:00
|
|
|
) where
|
|
|
|
|
2015-03-03 20:12:43 +00:00
|
|
|
import Control.Arrow
|
2015-08-03 01:09:56 +00:00
|
|
|
import Control.Applicative
|
2015-08-18 05:41:08 +00:00
|
|
|
import Data.Ord
|
|
|
|
import Data.List
|
|
|
|
import Data.Maybe
|
|
|
|
import Data.Function
|
2016-05-22 00:55:06 +00:00
|
|
|
import Control.Monad.Reader (Reader, ask, runReader)
|
2015-03-03 20:12:43 +00:00
|
|
|
import Data.IORef (IORef, newIORef, readIORef, writeIORef, modifyIORef)
|
2015-07-04 14:49:48 +00:00
|
|
|
import System.FilePath (normalise)
|
2015-03-03 20:12:43 +00:00
|
|
|
|
2015-08-18 05:41:08 +00:00
|
|
|
import ErrUtils
|
|
|
|
import GHC
|
2015-03-03 20:12:43 +00:00
|
|
|
import HscTypes
|
|
|
|
import Outputable
|
2014-03-27 06:32:44 +00:00
|
|
|
import qualified GHC as G
|
2015-04-11 14:40:27 +00:00
|
|
|
import Bag
|
2016-05-22 00:55:06 +00:00
|
|
|
import SrcLoc
|
|
|
|
import FastString
|
2011-08-24 07:50:26 +00:00
|
|
|
|
2017-05-28 02:22:56 +00:00
|
|
|
import GhcMod.Convert
|
|
|
|
import GhcMod.Doc (showPage)
|
|
|
|
import GhcMod.DynFlags (withDynFlags)
|
|
|
|
import GhcMod.Monad.Types
|
|
|
|
import GhcMod.Error
|
|
|
|
import GhcMod.Pretty
|
|
|
|
import GhcMod.Utils (mkRevRedirMapFunc)
|
|
|
|
import qualified GhcMod.Gap as Gap
|
2015-08-03 01:09:56 +00:00
|
|
|
import Prelude
|
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)
|
|
|
|
|
2015-08-11 15:57:17 +00:00
|
|
|
data GmPprEnv = GmPprEnv { gpeDynFlags :: DynFlags
|
|
|
|
, gpeMapFile :: FilePath -> FilePath
|
2015-07-02 10:17:49 +00:00
|
|
|
}
|
2015-06-16 10:49:53 +00:00
|
|
|
|
2015-07-02 10:17:49 +00:00
|
|
|
type GmPprEnvM a = Reader GmPprEnv a
|
2015-06-16 10:49:53 +00:00
|
|
|
|
2014-07-15 08:20:35 +00:00
|
|
|
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
|
|
|
|
2015-03-03 20:12:43 +00:00
|
|
|
readAndClearLogRef :: LogRef -> IO [String]
|
2014-05-14 16:05:40 +00:00
|
|
|
readAndClearLogRef (LogRef ref) = do
|
2015-03-03 20:12:43 +00:00
|
|
|
Log _ b <- readIORef ref
|
|
|
|
writeIORef ref emptyLog
|
|
|
|
return $ b []
|
2014-03-18 03:38:04 +00:00
|
|
|
|
2016-05-22 00:55:06 +00:00
|
|
|
appendLogRef :: (FilePath -> FilePath) -> DynFlags -> LogRef -> Gap.GmLogAction
|
|
|
|
appendLogRef map_file df (LogRef ref) _reason _df sev src st msg = do
|
2015-08-18 05:41:08 +00:00
|
|
|
modifyIORef ref update
|
2014-07-15 08:20:35 +00:00
|
|
|
where
|
2016-05-22 00:55:06 +00:00
|
|
|
-- TODO: get rid of ppMsg and just do more or less what ghc's
|
|
|
|
-- defaultLogAction does
|
|
|
|
l = ppMsg map_file df st src sev msg
|
2015-08-18 05:41:08 +00:00
|
|
|
|
2014-07-15 08:20:35 +00:00
|
|
|
update lg@(Log ls b)
|
|
|
|
| l `elem` ls = lg
|
|
|
|
| otherwise = Log (l:ls) (b . (l:))
|
2014-03-18 03:38:04 +00:00
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2015-08-18 05:41:08 +00:00
|
|
|
-- | Logged messages are returned as 'String'.
|
2014-04-28 12:51:39 +00:00
|
|
|
-- Right is success and Left is failure.
|
2015-09-01 08:27:12 +00:00
|
|
|
withLogger :: (GmGhc m, GmEnv m, GmOut m, GmState m)
|
2014-07-12 09:16:16 +00:00
|
|
|
=> (DynFlags -> DynFlags)
|
2015-03-03 20:12:43 +00:00
|
|
|
-> m a
|
|
|
|
-> m (Either String (String, a))
|
|
|
|
withLogger f action = do
|
|
|
|
env <- G.getSession
|
2015-09-01 08:27:12 +00:00
|
|
|
oopts <- outputOpts
|
2015-08-31 05:33:36 +00:00
|
|
|
let conv = convert oopts
|
2015-03-03 20:12:43 +00:00
|
|
|
eres <- withLogger' env $ \setDf ->
|
|
|
|
withDynFlags (f . setDf) action
|
|
|
|
return $ either (Left . conv) (Right . first conv) eres
|
|
|
|
|
2015-06-16 10:49:53 +00:00
|
|
|
withLogger' :: (IOish m, GmState m, GmEnv m)
|
2015-03-03 20:12:43 +00:00
|
|
|
=> HscEnv -> ((DynFlags -> DynFlags) -> m a) -> m (Either [String] ([String], a))
|
|
|
|
withLogger' env action = do
|
|
|
|
logref <- liftIO $ newLogRef
|
|
|
|
|
2015-07-04 14:49:48 +00:00
|
|
|
rfm <- mkRevRedirMapFunc
|
2015-03-03 20:12:43 +00:00
|
|
|
|
2015-08-18 05:41:08 +00:00
|
|
|
let setLogger df = Gap.setLogAction df $ appendLogRef rfm df logref
|
2015-06-16 10:49:53 +00:00
|
|
|
handlers = [
|
2015-08-18 05:41:08 +00:00
|
|
|
GHandler $ \ex -> return $ Left $ runReader (sourceError ex) gpe,
|
2017-05-12 13:28:08 +00:00
|
|
|
GHandler $ \ex -> return $ Left [renderGm $ ghcExceptionDoc ex]
|
2015-08-18 05:41:08 +00:00
|
|
|
]
|
|
|
|
gpe = GmPprEnv {
|
|
|
|
gpeDynFlags = hsc_dflags env
|
|
|
|
, gpeMapFile = rfm
|
|
|
|
}
|
2015-06-16 10:49:53 +00:00
|
|
|
|
|
|
|
a <- gcatches (Right <$> action setLogger) handlers
|
2015-03-03 20:12:43 +00:00
|
|
|
ls <- liftIO $ readAndClearLogRef logref
|
|
|
|
|
2015-06-16 10:49:53 +00:00
|
|
|
return ((,) ls <$> a)
|
2014-05-14 16:05:40 +00:00
|
|
|
|
2015-08-18 05:41:08 +00:00
|
|
|
errBagToStrList :: (IOish m, GmState m, GmEnv m) => HscEnv -> Bag ErrMsg -> m [String]
|
|
|
|
errBagToStrList env errs = do
|
2015-08-16 20:20:00 +00:00
|
|
|
rfm <- mkRevRedirMapFunc
|
|
|
|
return $ runReader
|
2015-08-18 05:41:08 +00:00
|
|
|
(errsToStr (sortMsgBag errs))
|
|
|
|
GmPprEnv{ gpeDynFlags = hsc_dflags env, gpeMapFile = rfm }
|
2015-06-19 15:15:14 +00:00
|
|
|
|
2011-08-24 07:50:26 +00:00
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2014-03-26 03:09:02 +00:00
|
|
|
-- | Converting 'SourceError' to 'String'.
|
2015-07-02 10:17:49 +00:00
|
|
|
sourceError :: SourceError -> GmPprEnvM [String]
|
2015-08-18 05:41:08 +00:00
|
|
|
sourceError = errsToStr . sortMsgBag . srcErrorMessages
|
2014-08-15 07:32:28 +00:00
|
|
|
|
2015-07-02 10:17:49 +00:00
|
|
|
errsToStr :: [ErrMsg] -> GmPprEnvM [String]
|
2015-06-16 10:49:53 +00:00
|
|
|
errsToStr = mapM ppErrMsg
|
2011-08-24 07:50:26 +00:00
|
|
|
|
2015-08-18 05:41:08 +00:00
|
|
|
sortMsgBag :: Bag ErrMsg -> [ErrMsg]
|
|
|
|
sortMsgBag bag = sortBy (compare `on` Gap.errorMsgSpan) $ bagToList bag
|
|
|
|
|
2011-08-24 07:50:26 +00:00
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2015-07-02 10:17:49 +00:00
|
|
|
ppErrMsg :: ErrMsg -> GmPprEnvM String
|
2015-06-16 10:49:53 +00:00
|
|
|
ppErrMsg err = do
|
2016-05-22 00:55:06 +00:00
|
|
|
GmPprEnv {..} <- ask
|
2015-08-18 05:41:08 +00:00
|
|
|
let unqual = errMsgContext err
|
2016-05-22 00:55:06 +00:00
|
|
|
st = Gap.mkErrStyle' gpeDynFlags unqual
|
|
|
|
err' = Gap.setErrorMsgSpan err $ mapSrcSpanFile gpeMapFile (Gap.errorMsgSpan err)
|
|
|
|
return $ showPage gpeDynFlags st $ pprLocErrMsg err'
|
|
|
|
|
|
|
|
mapSrcSpanFile :: (FilePath -> FilePath) -> SrcSpan -> SrcSpan
|
|
|
|
mapSrcSpanFile map_file (RealSrcSpan s) =
|
|
|
|
RealSrcSpan $ mapRealSrcSpanFile map_file s
|
|
|
|
mapSrcSpanFile _ (UnhelpfulSpan s) =
|
|
|
|
UnhelpfulSpan s
|
|
|
|
|
|
|
|
mapRealSrcSpanFile :: (FilePath -> FilePath) -> RealSrcSpan -> RealSrcSpan
|
|
|
|
mapRealSrcSpanFile map_file s = let
|
|
|
|
start = mapRealSrcLocFile map_file $ realSrcSpanStart s
|
|
|
|
end = mapRealSrcLocFile map_file $ realSrcSpanEnd s
|
|
|
|
in
|
|
|
|
mkRealSrcSpan start end
|
|
|
|
|
|
|
|
mapRealSrcLocFile :: (FilePath -> FilePath) -> RealSrcLoc -> RealSrcLoc
|
|
|
|
mapRealSrcLocFile map_file l = let
|
|
|
|
file = mkFastString $ map_file $ unpackFS $ srcLocFile l
|
|
|
|
line = srcLocLine l
|
|
|
|
col = srcLocCol l
|
|
|
|
in
|
|
|
|
mkRealSrcLoc file line col
|
|
|
|
|
|
|
|
ppMsg :: (FilePath -> FilePath) -> DynFlags -> PprStyle -> SrcSpan -> Severity -> SDoc -> String
|
|
|
|
ppMsg map_file df st spn sev msg = let
|
|
|
|
cts = showPage df st msg
|
|
|
|
in
|
|
|
|
ppMsgPrefix map_file df spn sev cts ++ cts
|
|
|
|
|
|
|
|
ppMsgPrefix :: (FilePath -> FilePath) -> DynFlags -> SrcSpan -> Severity -> String -> String
|
|
|
|
ppMsgPrefix map_file df spn sev cts =
|
|
|
|
let
|
|
|
|
defaultPrefix = if Gap.isDumpSplices df then "" else checkErrorPrefix
|
|
|
|
in
|
|
|
|
fromMaybe defaultPrefix $ do
|
|
|
|
(line,col,_,_) <- Gap.getSrcSpan spn
|
|
|
|
file <- map_file <$> normalise <$> Gap.getSrcFile spn
|
|
|
|
return $ file ++ ":" ++ show line ++ ":" ++ show col ++ ":" ++
|
|
|
|
if or (map (\x -> x `isPrefixOf` cts) warningAsErrorPrefixes)
|
|
|
|
then ""
|
|
|
|
else Gap.showSeverityCaption sev
|
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]
|
2016-02-14 07:41:11 +00:00
|
|
|
warningAsErrorPrefixes = [ "Couldn't match expected type"
|
2014-08-18 15:32:14 +00:00
|
|
|
, "Couldn't match type"
|
|
|
|
, "No instance for"]
|