ghc-mod/src/GHCModi.hs

268 lines
9.0 KiB
Haskell
Raw Normal View History

2014-03-27 07:28:27 +00:00
{-# LANGUAGE CPP #-}
2014-04-07 02:06:17 +00:00
{-# LANGUAGE DeriveDataTypeable #-}
2014-03-25 02:14:25 +00:00
2014-03-27 01:34:43 +00:00
-- Commands:
-- check <file>
-- find <symbol>
2014-03-28 04:44:44 +00:00
-- lint [hlint options] <file>
-- the format of hlint options is [String] because they may contain
-- spaces and aslo <file> may contain spaces.
2014-03-27 01:34:43 +00:00
--
-- Session separators:
-- OK -- success
-- NG -- failure
2014-03-19 01:23:47 +00:00
module Main where
2014-03-27 07:28:27 +00:00
#ifndef MIN_VERSION_containers
2014-04-03 01:22:29 +00:00
#define MIN_VERSION_containers(x,y,z) 1
2014-03-27 07:28:27 +00:00
#endif
2014-03-25 03:28:39 +00:00
import Control.Applicative ((<$>))
2014-03-27 05:55:24 +00:00
import Control.Concurrent (forkIO, MVar, newEmptyMVar, putMVar, readMVar)
import Control.Exception (SomeException(..))
import qualified Control.Exception as E
2014-03-24 08:32:06 +00:00
import Control.Monad (when, void)
2014-03-27 06:08:07 +00:00
import CoreMonad (liftIO)
2014-03-27 05:55:24 +00:00
import Data.Function (on)
2014-03-25 03:28:39 +00:00
import Data.List (intercalate, groupBy, sort, find)
#if MIN_VERSION_containers(0,5,0)
2014-03-25 02:14:25 +00:00
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as M
#else
import Data.Map (Map)
import qualified Data.Map as M
#endif
2014-03-28 04:53:58 +00:00
import Data.Maybe (fromMaybe)
2014-03-25 02:14:25 +00:00
import Data.Set (Set)
import qualified Data.Set as S
2014-04-11 07:07:36 +00:00
import Data.Typeable (Typeable)
2014-04-07 02:06:17 +00:00
import Data.Version (showVersion)
2014-03-27 05:55:24 +00:00
import qualified Exception as GE
import GHC (Ghc, LoadHowMuch(LoadAllTargets), TargetId(TargetFile))
import qualified GHC as G
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-04-07 02:06:17 +00:00
import Paths_ghc_mod
import System.Console.GetOpt
2014-04-03 07:18:35 +00:00
import System.Directory (setCurrentDirectory)
2014-04-07 02:06:17 +00:00
import System.Environment (getArgs)
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-04-07 02:06:17 +00:00
argspec :: [OptDescr (Options -> Options)]
argspec = [ Option "b" ["boundary"]
(ReqArg (\s opts -> opts { lineSeparator = LineSeparator s }) "sep")
"specify line separator (default is Nul string)"
2014-04-18 08:28:12 +00:00
, Option "l" ["tolisp"]
(NoArg (\opts -> opts { outputStyle = LispStyle }))
"print as a list of Lisp"
2014-04-10 13:21:30 +00:00
, Option "g" []
(ReqArg (\s opts -> opts { ghcOpts = s : ghcOpts opts }) "flag") "specify a ghc flag"
2014-04-07 02:06:17 +00:00
]
usage :: String
usage = "ghc-modi version " ++ showVersion version ++ "\n"
++ "Usage:\n"
2014-04-18 08:28:12 +00:00
++ "\t ghc-modi [-l] [-b sep] [-g flag]\n"
2014-04-07 02:06:17 +00:00
++ "\t ghc-modi help\n"
parseArgs :: [OptDescr (Options -> Options)] -> [String] -> (Options, [String])
parseArgs spec argv
= case getOpt Permute spec argv of
(o,n,[] ) -> (foldr id defaultOptions o, n)
(_,_,errs) -> GE.throw (CmdArg errs)
----------------------------------------------------------------
data GHCModiError = CmdArg [String]
deriving (Show, Typeable)
instance GE.Exception GHCModiError
----------------------------------------------------------------
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-04-07 02:06:17 +00:00
main = handle [GE.Handler cmdHandler, GE.Handler someHandler] $
go =<< parseArgs argspec <$> getArgs
2014-03-19 01:23:47 +00:00
where
2014-04-07 02:06:17 +00:00
handle = flip GE.catches
LineSeparator lsc = lineSeparator defaultOptions
cmdHandler (CmdArg e) = do
putStr "ghc-modi:0:0:"
let x = intercalate lsc e
putStrLn x
putStr $ usageInfo usage argspec
putStrLn "NG"
someHandler (SomeException e) = do
putStr "ghc-modi:0:0:"
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-04-07 02:06:17 +00:00
go (_,"help":_) = do
putStr $ usageInfo usage argspec
putStrLn "NG"
go (opt,_) = do
cradle0 <- findCradle
let rootdir = cradleRootDir cradle0
cradle = cradle0 { cradleCurrentDir = rootdir }
ls = lineSeparator opt
setCurrentDirectory rootdir
mvar <- liftIO newEmptyMVar
mlibdir <- getSystemLibDir
void $ forkIO $ setupDB cradle mlibdir opt mvar
2014-04-11 07:07:36 +00:00
run cradle mlibdir opt $ loop opt S.empty ls mvar
2014-03-20 08:40:06 +00:00
2014-03-25 02:14:25 +00:00
----------------------------------------------------------------
run :: Cradle -> Maybe FilePath -> Options -> (Logger -> Ghc a) -> IO a
2014-03-27 05:55:24 +00:00
run cradle mlibdir opt body = G.runGhc mlibdir $ do
2014-03-25 02:14:25 +00:00
(readLog,_) <- initializeFlagsWithCradle opt cradle ["-Wall"] True
2014-03-27 05:55:24 +00:00
dflags <- G.getSessionDynFlags
G.defaultCleanupHandler dflags $ body readLog
2014-03-25 02:14:25 +00:00
----------------------------------------------------------------
setupDB :: Cradle -> Maybe FilePath -> Options -> MVar DB -> IO ()
setupDB cradle mlibdir opt mvar = E.handle handler $ do
2014-03-27 05:55:24 +00:00
sm <- run cradle mlibdir opt $ \_ -> G.getSessionDynFlags >>= browseAll
2014-03-25 02:14:25 +00:00
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)
2014-03-27 05:55:24 +00:00
handler (SomeException _) = return ()
2014-03-25 02:14:25 +00:00
----------------------------------------------------------------
2014-04-11 07:07:36 +00:00
loop :: Options -> Set FilePath -> LineSeparator -> MVar DB -> Logger -> Ghc ()
loop opt set ls mvar readLog = do
2014-03-28 04:53:58 +00:00
cmdArg <- liftIO getLine
2014-03-24 08:32:06 +00:00
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-27 01:34:43 +00:00
"lint" -> lintStx set ls arg
2014-04-11 07:07:36 +00:00
"info" -> showInfo set ls readLog arg
"type" -> showType opt set ls readLog arg
2014-03-24 08:32:06 +00:00
_ -> return ([], False, set)
let put = case outputStyle opt of
LispStyle -> putStr
PlainStyle -> putStrLn
liftIO $ put $ convert opt msgs
2014-03-24 08:32:06 +00:00
liftIO $ putStrLn $ if ok then "OK" else "NG"
liftIO $ hFlush stdout
2014-04-11 07:07:36 +00:00
when ok $ loop opt 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-27 05:55:24 +00:00
GE.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]
2014-03-27 05:55:24 +00:00
void $ G.load LoadAllTargets
2014-03-28 04:53:58 +00:00
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
2014-03-27 05:55:24 +00:00
mx <- find isMain <$> G.getModuleGraph
2014-03-25 03:28:39 +00:00
case mx of
Nothing -> return Nothing
Just x -> do
let mmainfile = G.ml_hs_file (G.ms_location x)
-- G.ms_hspp_file x is a temporary file with CPP.
-- this is a just fake.
mainfile = fromMaybe (G.ms_hspp_file x) mmainfile
2014-03-25 03:28:39 +00:00
if mainfile == file then
return Nothing
else do
let target = TargetFile mainfile Nothing
2014-03-27 05:55:24 +00:00
G.removeTarget target
2014-03-25 03:28:39 +00:00
return $ Just mainfile
2014-03-27 05:55:24 +00:00
isMain m = G.moduleNameString (G.moduleName (G.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
2014-03-28 04:53:58 +00:00
let ret = fromMaybe [] (M.lookup sym db)
2014-03-25 02:14:25 +00:00
return (ret, True, set)
2014-03-27 01:34:43 +00:00
lintStx :: Set FilePath -> LineSeparator -> FilePath
-> Ghc ([String], Bool, Set FilePath)
2014-03-28 05:41:01 +00:00
lintStx set (LineSeparator lsep) optFile = liftIO $ E.handle handler $ do
2014-03-27 01:34:43 +00:00
msgs <- map (intercalate lsep . lines) <$> lint hopts file
2014-03-28 04:51:47 +00:00
return (msgs, True, set)
2014-03-27 01:34:43 +00:00
where
2014-03-28 05:41:01 +00:00
(opt,file) = parseLintOptions optFile
2014-03-28 06:03:41 +00:00
hopts = if opt == "" then [] else read opt
2014-03-28 04:51:47 +00:00
-- let's continue the session
2014-03-28 06:03:41 +00:00
handler (SomeException e) = do
print e
return ([], True, set)
2014-03-28 05:41:01 +00:00
-- |
-- >>> parseLintOptions "[\"--ignore=Use camelCase\", \"--ignore=Eta reduce\"] file name"
-- (["--ignore=Use camelCase", "--ignore=Eta reduce"], "file name")
-- >>> parseLintOptions "file name"
-- ([], "file name")
parseLintOptions :: String -> (String, String)
parseLintOptions optFile = case brk (== ']') (dropWhile (/= '[') optFile) of
("","") -> ([], optFile)
2014-03-28 06:03:41 +00:00
(opt',file') -> (opt', dropWhile (== ' ') file')
2014-03-28 05:41:01 +00:00
where
brk _ [] = ([],[])
brk p (x:xs')
| p x = ([x],xs')
| otherwise = let (ys,zs) = brk p xs' in (x:ys,zs)
2014-04-11 07:07:36 +00:00
showInfo :: Set FilePath
-> LineSeparator
-> Logger
-> FilePath
-> Ghc ([String], Bool, Set FilePath)
showInfo set ls readLog fileArg = do
let [file, expr] = words fileArg
(_, _, set') <- checkStx set ls readLog file
msgs <- info file expr
_ <- liftIO readLog
return ([msgs], True, set')
showType :: Options
-> Set FilePath
-> LineSeparator
-> Logger
-> FilePath
-> Ghc ([String], Bool, Set FilePath)
showType opt set ls readLog fileArg = do
let [file, line, column] = words fileArg
(_, _, set') <- checkStx set ls readLog file
msgs <- typeOf opt file (read line) (read column)
_ <- liftIO readLog
return ([msgs], True, set')