ghc-mod/Check.hs

73 lines
2.2 KiB
Haskell
Raw Normal View History

2010-03-11 10:03:17 +00:00
module Check (checkSyntax) where
2010-04-28 12:43:32 +00:00
import Bag
2011-05-24 07:00:47 +00:00
import Cabal
2010-03-11 10:03:17 +00:00
import Control.Applicative
2010-04-28 12:43:32 +00:00
import Data.IORef
import ErrUtils
2010-05-04 10:31:14 +00:00
import Exception
2010-04-28 12:43:32 +00:00
import FastString
import GHC
import HscTypes
import Outputable hiding (showSDoc)
2011-05-24 07:00:47 +00:00
import Prelude hiding (catch)
2010-04-28 12:43:32 +00:00
import Pretty
2011-05-26 05:43:53 +00:00
import System.FilePath
2010-04-30 09:36:31 +00:00
import Types
2010-03-11 10:03:17 +00:00
----------------------------------------------------------------
2010-03-11 13:39:07 +00:00
checkSyntax :: Options -> String -> IO String
checkSyntax opt file = unlines <$> check opt file
2010-04-28 12:43:32 +00:00
----------------------------------------------------------------
check :: Options -> String -> IO [String]
check opt fileName = withGHC $ do
file <- initializeGHC opt fileName options
2011-05-24 07:17:19 +00:00
setTargetFile file
2010-11-12 07:27:50 +00:00
ref <- newRef []
2010-05-04 10:31:14 +00:00
loadWithLogger (refLogger ref) LoadAllTargets `gcatch` handleParseError ref
clearWarnings
2010-11-12 07:27:50 +00:00
readRef ref
2010-03-11 10:03:17 +00:00
where
2011-08-02 18:18:07 +00:00
options = ["-Wall","-fno-warn-unused-do-bind"] ++ map ((++) "-i") (checkIncludes opt)
2010-05-04 10:31:14 +00:00
handleParseError ref e = do
liftIO . writeIORef ref $ errBagToStrList . srcErrorMessages $ e
return Succeeded
2010-11-12 07:27:50 +00:00
newRef = liftIO . newIORef
readRef = liftIO . readIORef
2010-04-30 09:09:26 +00:00
2010-04-28 12:43:32 +00:00
----------------------------------------------------------------
refLogger :: IORef [String] -> WarnErrLogger
2010-05-04 10:31:14 +00:00
refLogger ref Nothing =
(errBagToStrList <$> getWarnings) >>= liftIO . writeIORef ref
refLogger ref (Just e) =
liftIO . writeIORef ref $ errBagToStrList . srcErrorMessages $ e
errBagToStrList :: Bag ErrMsg -> [String]
errBagToStrList = map showErrMsg . reverse . bagToList
2010-04-28 12:43:32 +00:00
----------------------------------------------------------------
showErrMsg :: ErrMsg -> String
2010-06-14 05:27:35 +00:00
showErrMsg err = file ++ ":" ++ line ++ ":" ++ col ++ ":" ++ msg ++ "\0" ++ ext
2010-04-28 12:43:32 +00:00
where
spn = head (errMsgSpans err)
2011-05-26 05:43:53 +00:00
file = takeFileName $ unpackFS (srcSpanFile spn)
2010-04-28 12:43:32 +00:00
line = show (srcSpanStartLine spn)
col = show (srcSpanStartCol spn)
msg = showSDoc (errMsgShortDoc err)
2010-06-14 05:27:35 +00:00
ext = showSDoc (errMsgExtraInfo err)
2010-04-28 12:43:32 +00:00
style :: PprStyle
style = mkUserStyle neverQualify AllTheWay
showSDoc :: SDoc -> String
--showSDoc d = map toNull . Pretty.showDocWith ZigZagMode $ d style
showSDoc d = map toNull . Pretty.showDocWith PageMode $ d style
where
toNull '\n' = '\0'
toNull x = x