2014-03-25 02:14:25 +00:00
|
|
|
{-# LANGUAGE BangPatterns #-}
|
|
|
|
|
2014-03-19 01:23:47 +00:00
|
|
|
module Main where
|
|
|
|
|
2014-03-25 03:28:39 +00:00
|
|
|
import Control.Applicative ((<$>))
|
2014-03-25 02:14:25 +00:00
|
|
|
import Control.Concurrent
|
2014-03-25 02:34:58 +00:00
|
|
|
import qualified Control.Exception as E (handle, SomeException(..))
|
2014-03-24 08:32:06 +00:00
|
|
|
import Control.Monad (when, void)
|
2014-03-25 02:14:25 +00:00
|
|
|
import Data.Function
|
2014-03-25 03:28:39 +00:00
|
|
|
import Data.List (intercalate, groupBy, sort, find)
|
2014-03-25 02:14:25 +00:00
|
|
|
import Data.Map.Strict (Map)
|
|
|
|
import qualified Data.Map.Strict as M
|
|
|
|
import Data.Set (Set)
|
|
|
|
import qualified Data.Set as S
|
|
|
|
import qualified Exception as G (ghandle)
|
2014-03-19 01:23:47 +00:00
|
|
|
import GHC
|
2014-03-25 02:14:25 +00:00
|
|
|
import GhcMonad
|
2014-03-25 02:34:58 +00:00
|
|
|
import HscTypes (SourceError)
|
2014-03-19 01:23:47 +00:00
|
|
|
import Language.Haskell.GhcMod
|
|
|
|
import Language.Haskell.GhcMod.Internal
|
2014-03-24 08:32:06 +00:00
|
|
|
import System.IO (hFlush,stdout)
|
2014-03-19 01:23:47 +00:00
|
|
|
|
2014-03-25 02:14:25 +00:00
|
|
|
----------------------------------------------------------------
|
|
|
|
|
|
|
|
type DB = Map String [String]
|
|
|
|
type Logger = IO [String]
|
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2014-03-25 02:34:58 +00:00
|
|
|
-- Running two GHC monad threads disables the handling of
|
|
|
|
-- C-c since installSignalHandlers is called twice, sigh.
|
|
|
|
|
2014-03-19 01:23:47 +00:00
|
|
|
main :: IO ()
|
2014-03-25 02:14:25 +00:00
|
|
|
main = E.handle handler $ do
|
2014-03-19 01:23:47 +00:00
|
|
|
cradle <- findCradle
|
2014-03-25 02:14:25 +00:00
|
|
|
mvar <- liftIO newEmptyMVar
|
|
|
|
mlibdir <- getSystemLibDir
|
|
|
|
void $ forkIO $ setupDB cradle mlibdir opt mvar
|
|
|
|
run cradle mlibdir opt $ loop S.empty ls mvar
|
2014-03-19 01:23:47 +00:00
|
|
|
where
|
|
|
|
opt = defaultOptions
|
|
|
|
ls = lineSeparator opt
|
2014-03-25 02:14:25 +00:00
|
|
|
LineSeparator lsc = ls
|
|
|
|
handler (E.SomeException e) = do
|
2014-03-20 08:40:06 +00:00
|
|
|
putStr "ghc-modi:0:0:Error:"
|
2014-03-25 02:14:25 +00:00
|
|
|
let x = intercalate lsc $ lines $ show e
|
2014-03-20 08:40:06 +00:00
|
|
|
putStrLn x
|
|
|
|
putStrLn "NG"
|
|
|
|
|
2014-03-25 02:14:25 +00:00
|
|
|
----------------------------------------------------------------
|
|
|
|
|
|
|
|
run :: Cradle -> Maybe FilePath -> Options -> (Logger -> Ghc a) -> IO a
|
|
|
|
run cradle mlibdir opt body = runGhc mlibdir $ do
|
|
|
|
(readLog,_) <- initializeFlagsWithCradle opt cradle ["-Wall"] True
|
|
|
|
dflags <- getSessionDynFlags
|
|
|
|
defaultCleanupHandler dflags $ body readLog
|
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
|
|
|
setupDB :: Cradle -> Maybe FilePath -> Options -> MVar DB -> IO ()
|
|
|
|
setupDB cradle mlibdir opt mvar = do
|
|
|
|
sm <- run cradle mlibdir opt $ \_ -> getSessionDynFlags >>= browseAll
|
|
|
|
let sms = map tieup $ groupBy ((==) `on` fst) $ sort sm
|
|
|
|
m = M.fromList sms
|
|
|
|
putMVar mvar m
|
|
|
|
where
|
|
|
|
tieup x = (head (map fst x), map snd x)
|
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
|
|
|
loop :: Set FilePath -> LineSeparator -> MVar DB -> Logger -> Ghc ()
|
|
|
|
loop set ls mvar readLog = do
|
2014-03-24 08:32:06 +00:00
|
|
|
cmdArg <- liftIO $ getLine
|
|
|
|
let (cmd,arg') = break (== ' ') cmdArg
|
|
|
|
arg = dropWhile (== ' ') arg'
|
|
|
|
(msgs,ok,set') <- case cmd of
|
2014-03-25 02:14:25 +00:00
|
|
|
"check" -> checkStx set ls readLog arg
|
|
|
|
"find" -> findSym set mvar arg
|
2014-03-24 08:32:06 +00:00
|
|
|
_ -> return ([], False, set)
|
|
|
|
mapM_ (liftIO . putStrLn) msgs
|
|
|
|
liftIO $ putStrLn $ if ok then "OK" else "NG"
|
|
|
|
liftIO $ hFlush stdout
|
2014-03-25 02:14:25 +00:00
|
|
|
when ok $ loop set' ls mvar readLog
|
2014-03-24 08:32:06 +00:00
|
|
|
|
2014-03-25 02:14:25 +00:00
|
|
|
----------------------------------------------------------------
|
|
|
|
|
|
|
|
checkStx :: Set FilePath
|
2014-03-24 08:32:06 +00:00
|
|
|
-> LineSeparator
|
2014-03-25 02:14:25 +00:00
|
|
|
-> Logger
|
2014-03-24 08:32:06 +00:00
|
|
|
-> FilePath
|
|
|
|
-> Ghc ([String], Bool, Set FilePath)
|
2014-03-25 02:14:25 +00:00
|
|
|
checkStx set ls readLog file = do
|
2014-03-19 01:23:47 +00:00
|
|
|
let add = not $ S.member file set
|
2014-03-25 02:14:25 +00:00
|
|
|
G.ghandle handler $ do
|
2014-03-25 03:28:39 +00:00
|
|
|
mdel <- removeMainTarget
|
2014-03-19 01:23:47 +00:00
|
|
|
when add $ addTargetFiles [file]
|
|
|
|
void $ load LoadAllTargets
|
|
|
|
msgs <- liftIO $ readLog
|
2014-03-25 03:28:39 +00:00
|
|
|
let set1 = if add then S.insert file set else set
|
|
|
|
set2 = case mdel of
|
|
|
|
Nothing -> set1
|
|
|
|
Just delfl -> S.delete delfl set1
|
|
|
|
return (msgs, True, set2)
|
2014-03-19 01:23:47 +00:00
|
|
|
where
|
2014-03-25 02:14:25 +00:00
|
|
|
handler :: SourceError -> Ghc ([String], Bool, Set FilePath)
|
2014-03-19 01:23:47 +00:00
|
|
|
handler err = do
|
|
|
|
errmsgs <- handleErrMsg ls err
|
2014-03-24 08:32:06 +00:00
|
|
|
return (errmsgs, False, set)
|
2014-03-25 03:28:39 +00:00
|
|
|
removeMainTarget = do
|
|
|
|
mx <- find isMain <$> getModuleGraph
|
|
|
|
case mx of
|
|
|
|
Nothing -> return Nothing
|
|
|
|
Just x -> do
|
|
|
|
let mainfile = ms_hspp_file x
|
|
|
|
if mainfile == file then
|
|
|
|
return Nothing
|
|
|
|
else do
|
|
|
|
let target = TargetFile mainfile Nothing
|
|
|
|
removeTarget target
|
|
|
|
return $ Just mainfile
|
|
|
|
isMain m = moduleNameString (moduleName (ms_mod m)) == "Main"
|
2014-03-25 02:14:25 +00:00
|
|
|
|
|
|
|
findSym :: Set FilePath -> MVar DB -> String
|
|
|
|
-> Ghc ([String], Bool, Set FilePath)
|
|
|
|
findSym set mvar sym = do
|
|
|
|
db <- liftIO $ readMVar mvar
|
|
|
|
let ret = case M.lookup sym db of
|
|
|
|
Nothing -> []
|
|
|
|
Just xs -> xs
|
|
|
|
return (ret, True, set)
|