2010-03-11 10:03:17 +00:00
|
|
|
module Check (checkSyntax) where
|
|
|
|
|
2010-04-28 12:43:32 +00:00
|
|
|
import Bag
|
2010-03-11 10:03:17 +00:00
|
|
|
import Control.Applicative
|
2010-04-28 12:43:32 +00:00
|
|
|
import Data.IORef
|
|
|
|
import DynFlags
|
|
|
|
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)
|
|
|
|
import Pretty
|
2010-04-30 09:36:31 +00:00
|
|
|
import Types
|
2010-05-04 10:31:14 +00:00
|
|
|
import Prelude hiding (catch)
|
2010-03-11 10:03:17 +00:00
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2010-03-11 13:39:07 +00:00
|
|
|
checkSyntax :: Options -> String -> IO String
|
2010-04-29 03:39:48 +00:00
|
|
|
checkSyntax _ file = unlines <$> check file
|
2010-04-28 12:43:32 +00:00
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2010-04-29 03:39:48 +00:00
|
|
|
check :: String -> IO [String]
|
2010-04-30 09:36:31 +00:00
|
|
|
check fileName = withGHC $ do
|
2010-04-28 12:43:32 +00:00
|
|
|
ref <- liftIO $ newIORef []
|
|
|
|
initSession
|
|
|
|
setTargetFile fileName
|
2010-05-04 10:31:14 +00:00
|
|
|
loadWithLogger (refLogger ref) LoadAllTargets `gcatch` handleParseError ref
|
|
|
|
clearWarnings
|
2010-04-28 12:43:32 +00:00
|
|
|
liftIO $ readIORef ref
|
2010-03-11 10:03:17 +00:00
|
|
|
where
|
2010-04-30 06:27:17 +00:00
|
|
|
-- I don't know why, but parseDynamicFlags must be used.
|
2010-04-28 12:43:32 +00:00
|
|
|
initSession = do
|
|
|
|
dflags <- getSessionDynFlags
|
|
|
|
(dflags',_,_) <- parseDynamicFlags dflags cmdOptions
|
2010-04-30 06:09:24 +00:00
|
|
|
setSessionDynFlags $ setFlags dflags'
|
2010-04-28 12:43:32 +00:00
|
|
|
setTargetFile file = do
|
|
|
|
target <- guessTarget file Nothing
|
|
|
|
setTargets [target]
|
2010-05-04 10:31:14 +00:00
|
|
|
handleParseError ref e = do
|
|
|
|
liftIO . writeIORef ref $ errBagToStrList . srcErrorMessages $ e
|
|
|
|
return Succeeded
|
2010-04-28 12:43:32 +00:00
|
|
|
|
2010-04-30 09:09:26 +00:00
|
|
|
-- I don't know why, but parseDynamicFlags must be used.
|
|
|
|
cmdOptions :: [Located String]
|
|
|
|
cmdOptions = map noLoc ["-Wall","-fno-warn-unused-do-bind"]
|
|
|
|
|
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
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2010-04-30 06:09:24 +00:00
|
|
|
setFlags :: DynFlags -> DynFlags
|
|
|
|
setFlags d = d {
|
2010-04-30 06:27:17 +00:00
|
|
|
importPaths = importPaths d ++ importDirs
|
|
|
|
, packageFlags = ghcPackage : packageFlags d
|
2010-04-29 03:30:23 +00:00
|
|
|
, ghcLink = NoLink
|
2010-04-30 06:11:41 +00:00
|
|
|
-- GHC.desugarModule does not produces the pattern warnings, why?
|
|
|
|
-- , hscTarget = HscNothing
|
|
|
|
, hscTarget = HscInterpreted
|
2010-04-29 03:30:23 +00:00
|
|
|
}
|
2010-04-29 03:39:48 +00:00
|
|
|
|
2010-04-30 06:27:17 +00:00
|
|
|
importDirs :: [String]
|
|
|
|
importDirs = ["..","../..","../../..","../../../../.."]
|
|
|
|
|
|
|
|
ghcPackage :: PackageFlag
|
|
|
|
ghcPackage = ExposePackage "ghc"
|
|
|
|
|
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)
|
|
|
|
file = unpackFS (srcSpanFile spn)
|
|
|
|
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
|
2010-06-25 12:45:32 +00:00
|
|
|
showSDoc d = map toNull . Pretty.showDocWith ZigZagMode $ d style
|
|
|
|
where
|
|
|
|
toNull '\n' = '\0'
|
|
|
|
toNull x = x
|