ErrMsg module.

This commit is contained in:
Kazu Yamamoto 2011-08-24 16:50:26 +09:00
parent dbdcf9841e
commit 1790c5f327
5 changed files with 98 additions and 59 deletions

View File

@ -8,6 +8,7 @@ import Data.Attoparsec.Enumerator
import Data.Enumerator (run, ($$)) import Data.Enumerator (run, ($$))
import Data.Enumerator.Binary (enumFile) import Data.Enumerator.Binary (enumFile)
import Data.List import Data.List
import ErrMsg
import GHC import GHC
import System.Directory import System.Directory
import System.FilePath import System.FilePath

View File

@ -3,6 +3,8 @@ module Check (checkSyntax) where
import Cabal import Cabal
import Control.Applicative import Control.Applicative
import CoreMonad import CoreMonad
import ErrMsg
import Exception
import GHC import GHC
import Prelude hiding (catch) import Prelude hiding (catch)
import Types import Types
@ -15,15 +17,12 @@ checkSyntax opt file = unlines <$> check opt file
---------------------------------------------------------------- ----------------------------------------------------------------
check :: Options -> String -> IO [String] check :: Options -> String -> IO [String]
check opt fileName = withGHC $ do check opt fileName = withGHC $ checkIt `gcatch` handleErrMsg
(file,readLog) <- initializeGHC opt fileName options True
setTargetFile file
load LoadAllTargets -- `gcatch` handleParseError ref xxx
liftIO readLog
where where
options = ["-Wall","-fno-warn-unused-do-bind"] ++ map ("-i" ++) (checkIncludes opt) checkIt = do
{- (file,readLog) <- initializeGHC opt fileName options True
handleParseError ref e = do setTargetFile file
liftIO . writeIORef ref $ errBagToStrList . srcErrorMessages $ e load LoadAllTargets
return Succeeded liftIO readLog
-} options = ["-Wall","-fno-warn-unused-do-bind"]
++ map ("-i" ++) (checkIncludes opt)

85
ErrMsg.hs Normal file
View File

@ -0,0 +1,85 @@
{-# 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
appendLog ref _ src _ msg = modifyIORef ref (\ls -> ppMsg src msg : ls)
----------------------------------------------------------------
handleErrMsg :: SourceError -> Ghc [String]
handleErrMsg = return . errBagToStrList . srcErrorMessages
errBagToStrList :: Bag ErrMsg -> [String]
errBagToStrList = map ppErrMsg . reverse . bagToList
----------------------------------------------------------------
ppErrMsg :: ErrMsg -> String
ppErrMsg err = ppMsg spn msg ++ ext
where
spn = head (errMsgSpans err)
msg = errMsgShortDoc err
ext = showMsg (errMsgExtraInfo err)
ppMsg :: SrcSpan -> Message -> String
#if __GLASGOW_HASKELL__ >= 702
ppMsg (UnhelpfulSpan _) _ = undefined
ppMsg (RealSrcSpan src) msg
#else
ppMsg src msg
#endif
= file ++ ":" ++ line ++ ":" ++ col ++ ":" ++ cts ++ "\0"
where
file = takeFileName $ unpackFS (srcSpanFile src)
line = show (srcSpanStartLine src)
col = show (srcSpanStartCol src)
cts = showMsg msg
----------------------------------------------------------------
style :: PprStyle
style = mkUserStyle neverQualify AllTheWay
showMsg :: SDoc -> String
#if __GLASGOW_HASKELL__ >= 702
showMsg d = map toNull $ renderWithStyle d style
#else
showMsg d = map toNull . Pretty.showDocWith PageMode $ d style
#endif
where
toNull '\n' = '\0'
toNull x = x

View File

@ -1,20 +1,12 @@
{-# LANGUAGE CPP #-}
module Types where module Types where
import Control.Applicative
import Control.Monad import Control.Monad
import CoreMonad import CoreMonad
import Data.IORef
import DynFlags import DynFlags
import ErrUtils import ErrMsg
import Exception import Exception
import FastString
import GHC import GHC
import GHC.Paths (libdir) import GHC.Paths (libdir)
import Outputable
import System.FilePath
import Pretty
---------------------------------------------------------------- ----------------------------------------------------------------
@ -76,44 +68,6 @@ setPackageConfFlags
---------------------------------------------------------------- ----------------------------------------------------------------
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
appendLog ref _ src _ msg = modifyIORef ref (\ls -> ppMsg src msg : ls)
ppMsg :: SrcSpan -> Message -> String
#if __GLASGOW_HASKELL__ >= 702
ppMsg (UnhelpfulSpan _) _ = undefined
ppMsg (RealSrcSpan src) msg
#else
ppMsg src msg
#endif
= file ++ ":" ++ line ++ ":" ++ col ++ ":" ++ cts ++ "\0" -- xxx
where
file = takeFileName $ unpackFS (srcSpanFile src)
line = show (srcSpanStartLine src)
col = show (srcSpanStartCol src)
cts = showMsg msg
style :: PprStyle
style = mkUserStyle neverQualify AllTheWay
showMsg :: SDoc -> String
showMsg d = map toNull . Pretty.showDocWith PageMode $ d style
where
toNull '\n' = '\0'
toNull x = x
----------------------------------------------------------------
setTargetFile :: (GhcMonad m) => String -> m () setTargetFile :: (GhcMonad m) => String -> m ()
setTargetFile file = do setTargetFile file = do
target <- guessTarget file Nothing target <- guessTarget file Nothing

View File

@ -23,7 +23,7 @@ Data-Files: Makefile ghc.el ghc-func.el ghc-doc.el ghc-comp.el
ghc-flymake.el ghc-command.el ghc-info.el ghc-flymake.el ghc-command.el ghc-info.el
Executable ghc-mod Executable ghc-mod
Main-Is: GHCMod.hs Main-Is: GHCMod.hs
Other-Modules: List Browse Cabal Check Info Lang Lint Types Other-Modules: List Browse Cabal Check Info Lang Lint Types ErrMsg
if impl(ghc >= 6.12) if impl(ghc >= 6.12)
GHC-Options: -Wall -fno-warn-unused-do-bind GHC-Options: -Wall -fno-warn-unused-do-bind
else else