PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/utils/ghctags/Main.hs

https://github.com/crdueck/ghc
Haskell | 362 lines | 275 code | 49 blank | 38 comment | 14 complexity | e544526a903b2f7b2b0290247f9323bb MD5 | raw file
  1. {-# LANGUAGE PatternGuards, ScopedTypeVariables #-}
  2. module Main where
  3. import Prelude hiding ( mod, id, mapM )
  4. import GHC
  5. --import Packages
  6. import HscTypes ( isBootSummary )
  7. import Digraph ( flattenSCCs )
  8. import DriverPhases ( isHaskellSrcFilename )
  9. import HscTypes ( msHsFilePath )
  10. import Name ( getOccString )
  11. --import ErrUtils ( printBagOfErrors )
  12. import Panic ( panic )
  13. import DynFlags ( defaultFatalMessager, defaultFlushOut )
  14. import Bag
  15. import Exception
  16. import FastString
  17. import MonadUtils ( liftIO )
  18. import SrcLoc
  19. import Distribution.Simple.GHC ( componentGhcOptions )
  20. import Distribution.Simple.Configure ( getPersistBuildConfig )
  21. import Distribution.Simple.Compiler ( compilerVersion )
  22. import Distribution.Simple.Program.GHC ( renderGhcOptions )
  23. import Distribution.PackageDescription ( library, libBuildInfo )
  24. import Distribution.Simple.LocalBuildInfo
  25. import qualified Distribution.Verbosity as V
  26. import Control.Monad hiding (mapM)
  27. import System.Environment
  28. import System.Console.GetOpt
  29. import System.Exit
  30. import System.IO
  31. import Data.List as List hiding ( group )
  32. import Data.Traversable (mapM)
  33. import Data.Map ( Map )
  34. import qualified Data.Map as M
  35. --import UniqFM
  36. --import Debug.Trace
  37. -- search for definitions of things
  38. -- we do this by parsing the source and grabbing top-level definitions
  39. -- We generate both CTAGS and ETAGS format tags files
  40. -- The former is for use in most sensible editors, while EMACS uses ETAGS
  41. ----------------------------------
  42. ---- CENTRAL DATA TYPES ----------
  43. type FileName = String
  44. type ThingName = String -- name of a defined entity in a Haskell program
  45. -- A definition we have found (we know its containing module, name, and location)
  46. data FoundThing = FoundThing ModuleName ThingName RealSrcLoc
  47. -- Data we have obtained from a file (list of things we found)
  48. data FileData = FileData FileName [FoundThing] (Map Int String)
  49. --- invariant (not checked): every found thing has a source location in that file?
  50. ------------------------------
  51. -------- MAIN PROGRAM --------
  52. main :: IO ()
  53. main = do
  54. progName <- getProgName
  55. let usageString =
  56. "Usage: " ++ progName ++ " [OPTION...] [-- GHC OPTION... --] [files...]"
  57. args <- getArgs
  58. let (ghcArgs', ourArgs, unbalanced) = splitArgs args
  59. let (flags, filenames, errs) = getOpt Permute options ourArgs
  60. let (hsfiles, otherfiles) = List.partition isHaskellSrcFilename filenames
  61. let ghc_topdir = case [ d | FlagTopDir d <- flags ] of
  62. [] -> ""
  63. (x:_) -> x
  64. mapM_ (\n -> putStr $ "Warning: ignoring non-Haskellish file " ++ n ++ "\n")
  65. otherfiles
  66. if unbalanced || errs /= [] || elem FlagHelp flags || hsfiles == []
  67. then do
  68. putStr $ unlines errs
  69. putStr $ usageInfo usageString options
  70. exitWith (ExitFailure 1)
  71. else return ()
  72. ghcArgs <- case [ d | FlagUseCabalConfig d <- flags ] of
  73. [distPref] -> do
  74. cabalOpts <- flagsFromCabal distPref
  75. return (cabalOpts ++ ghcArgs')
  76. [] ->
  77. return ghcArgs'
  78. _ -> error "Too many --use-cabal-config flags"
  79. print ghcArgs
  80. let modes = getMode flags
  81. let openFileMode = if elem FlagAppend flags
  82. then AppendMode
  83. else WriteMode
  84. ctags_hdl <- if CTags `elem` modes
  85. then Just `liftM` openFile "tags" openFileMode
  86. else return Nothing
  87. etags_hdl <- if ETags `elem` modes
  88. then Just `liftM` openFile "TAGS" openFileMode
  89. else return Nothing
  90. GHC.defaultErrorHandler defaultFatalMessager defaultFlushOut $
  91. runGhc (Just ghc_topdir) $ do
  92. --liftIO $ print "starting up session"
  93. dflags <- getSessionDynFlags
  94. (pflags, unrec, warns) <- parseDynamicFlags dflags{ verbosity=1 }
  95. (map noLoc ghcArgs)
  96. unless (null unrec) $
  97. liftIO $ putStrLn $ "Unrecognised options:\n" ++ show (map unLoc unrec)
  98. liftIO $ mapM_ putStrLn (map unLoc warns)
  99. let dflags2 = pflags { hscTarget = HscNothing } -- don't generate anything
  100. -- liftIO $ print ("pkgDB", case (pkgDatabase dflags2) of Nothing -> 0
  101. -- Just m -> sizeUFM m)
  102. _ <- setSessionDynFlags dflags2
  103. --liftIO $ print (length pkgs)
  104. GHC.defaultCleanupHandler dflags2 $ do
  105. targetsAtOneGo hsfiles (ctags_hdl,etags_hdl)
  106. mapM_ (mapM (liftIO . hClose)) [ctags_hdl, etags_hdl]
  107. ----------------------------------------------
  108. ---------- ARGUMENT PROCESSING --------------
  109. data Flag
  110. = FlagETags
  111. | FlagCTags
  112. | FlagBoth
  113. | FlagAppend
  114. | FlagHelp
  115. | FlagTopDir FilePath
  116. | FlagUseCabalConfig FilePath
  117. | FlagFilesFromCabal
  118. deriving (Ord, Eq, Show)
  119. -- ^Represents options passed to the program
  120. data Mode = ETags | CTags deriving Eq
  121. getMode :: [Flag] -> [Mode]
  122. getMode fs = go (concatMap modeLike fs)
  123. where go [] = [ETags,CTags]
  124. go [x] = [x]
  125. go more = nub more
  126. modeLike FlagETags = [ETags]
  127. modeLike FlagCTags = [CTags]
  128. modeLike FlagBoth = [ETags,CTags]
  129. modeLike _ = []
  130. splitArgs :: [String] -> ([String], [String], Bool)
  131. -- ^Pull out arguments between -- for GHC
  132. splitArgs args0 = split [] [] False args0
  133. where split ghc' tags' unbal ("--" : args) = split tags' ghc' (not unbal) args
  134. split ghc' tags' unbal (arg : args) = split ghc' (arg:tags') unbal args
  135. split ghc' tags' unbal [] = (reverse ghc', reverse tags', unbal)
  136. options :: [OptDescr Flag]
  137. -- supports getopt
  138. options = [ Option "" ["topdir"]
  139. (ReqArg FlagTopDir "DIR") "root of GHC installation (optional)"
  140. , Option "c" ["ctags"]
  141. (NoArg FlagCTags) "generate CTAGS file (ctags)"
  142. , Option "e" ["etags"]
  143. (NoArg FlagETags) "generate ETAGS file (etags)"
  144. , Option "b" ["both"]
  145. (NoArg FlagBoth) ("generate both CTAGS and ETAGS")
  146. , Option "a" ["append"]
  147. (NoArg FlagAppend) ("append to existing CTAGS and/or ETAGS file(s)")
  148. , Option "" ["use-cabal-config"]
  149. (ReqArg FlagUseCabalConfig "DIR") "use local cabal configuration from dist dir"
  150. , Option "" ["files-from-cabal"]
  151. (NoArg FlagFilesFromCabal) "use files from cabal"
  152. , Option "h" ["help"] (NoArg FlagHelp) "This help"
  153. ]
  154. flagsFromCabal :: FilePath -> IO [String]
  155. flagsFromCabal distPref = do
  156. lbi <- getPersistBuildConfig distPref
  157. let pd = localPkgDescr lbi
  158. findLibraryConfig [] = Nothing
  159. findLibraryConfig ((CLibName, clbi, _) : _) = Just clbi
  160. findLibraryConfig (_ : xs) = findLibraryConfig xs
  161. mLibraryConfig = findLibraryConfig (componentsConfigs lbi)
  162. case (library pd, mLibraryConfig) of
  163. (Just lib, Just clbi) ->
  164. let bi = libBuildInfo lib
  165. odir = buildDir lbi
  166. opts = componentGhcOptions V.normal lbi bi clbi odir
  167. version = compilerVersion (compiler lbi)
  168. in return $ renderGhcOptions version opts
  169. _ -> error "no library"
  170. ----------------------------------------------------------------
  171. --- LOADING HASKELL SOURCE
  172. --- (these bits actually run the compiler and produce abstract syntax)
  173. safeLoad :: LoadHowMuch -> Ghc SuccessFlag
  174. -- like GHC.load, but does not stop process on exception
  175. safeLoad mode = do
  176. _dflags <- getSessionDynFlags
  177. ghandle (\(e :: SomeException) -> liftIO (print e) >> return Failed ) $
  178. handleSourceError (\e -> printException e >> return Failed) $
  179. load mode
  180. targetsAtOneGo :: [FileName] -> (Maybe Handle, Maybe Handle) -> Ghc ()
  181. -- load a list of targets
  182. targetsAtOneGo hsfiles handles = do
  183. targets <- mapM (\f -> guessTarget f Nothing) hsfiles
  184. setTargets targets
  185. modgraph <- depanal [] False
  186. let mods = flattenSCCs $ topSortModuleGraph False modgraph Nothing
  187. graphData mods handles
  188. fileTarget :: FileName -> Target
  189. fileTarget filename = Target (TargetFile filename Nothing) True Nothing
  190. ---------------------------------------------------------------
  191. ----- CRAWLING ABSTRACT SYNTAX TO SNAFFLE THE DEFINITIONS -----
  192. graphData :: ModuleGraph -> (Maybe Handle, Maybe Handle) -> Ghc ()
  193. graphData graph handles = do
  194. mapM_ foundthings graph
  195. where foundthings ms =
  196. let filename = msHsFilePath ms
  197. modname = moduleName $ ms_mod ms
  198. in handleSourceError (\e -> do
  199. printException e
  200. liftIO $ exitWith (ExitFailure 1)) $
  201. do liftIO $ putStrLn ("loading " ++ filename)
  202. mod <- loadModule =<< typecheckModule =<< parseModule ms
  203. case mod of
  204. _ | isBootSummary ms -> return ()
  205. _ | Just s <- renamedSource mod ->
  206. liftIO (writeTagsData handles =<< fileData filename modname s)
  207. _otherwise ->
  208. liftIO $ exitWith (ExitFailure 1)
  209. fileData :: FileName -> ModuleName -> RenamedSource -> IO FileData
  210. fileData filename modname (group, _imports, _lie, _doc) = do
  211. -- lie is related to type checking and so is irrelevant
  212. -- imports contains import declarations and no definitions
  213. -- doc and haddock seem haddock-related; let's hope to ignore them
  214. ls <- lines `fmap` readFile filename
  215. let line_map = M.fromAscList $ zip [1..] ls
  216. line_map' <- evaluate line_map
  217. return $ FileData filename (boundValues modname group) line_map'
  218. boundValues :: ModuleName -> HsGroup Name -> [FoundThing]
  219. -- ^Finds all the top-level definitions in a module
  220. boundValues mod group =
  221. let vals = case hs_valds group of
  222. ValBindsOut nest _sigs ->
  223. [ x | (_rec, binds) <- nest
  224. , bind <- bagToList binds
  225. , x <- boundThings mod bind ]
  226. _other -> error "boundValues"
  227. tys = [ n | ns <- map hsLTyClDeclBinders (concat (hs_tyclds group))
  228. , n <- map found ns ]
  229. fors = concat $ map forBound (hs_fords group)
  230. where forBound lford = case unLoc lford of
  231. ForeignImport n _ _ _ -> [found n]
  232. ForeignExport { } -> []
  233. in vals ++ tys ++ fors
  234. where found = foundOfLName mod
  235. startOfLocated :: Located a -> RealSrcLoc
  236. startOfLocated lHs = case getLoc lHs of
  237. RealSrcSpan l -> realSrcSpanStart l
  238. UnhelpfulSpan _ -> panic "startOfLocated UnhelpfulSpan"
  239. foundOfLName :: ModuleName -> Located Name -> FoundThing
  240. foundOfLName mod id = FoundThing mod (getOccString $ unLoc id) (startOfLocated id)
  241. boundThings :: ModuleName -> LHsBind Name -> [FoundThing]
  242. boundThings modname lbinding =
  243. case unLoc lbinding of
  244. FunBind { fun_id = id } -> [thing id]
  245. PatBind { pat_lhs = lhs } -> patThings lhs []
  246. VarBind { var_id = id } -> [FoundThing modname (getOccString id) (startOfLocated lbinding)]
  247. AbsBinds { } -> [] -- nothing interesting in a type abstraction
  248. where thing = foundOfLName modname
  249. patThings lpat tl =
  250. let loc = startOfLocated lpat
  251. lid id = FoundThing modname (getOccString id) loc
  252. in case unLoc lpat of
  253. WildPat _ -> tl
  254. VarPat name -> lid name : tl
  255. LazyPat p -> patThings p tl
  256. AsPat id p -> patThings p (thing id : tl)
  257. ParPat p -> patThings p tl
  258. BangPat p -> patThings p tl
  259. ListPat ps _ _ -> foldr patThings tl ps
  260. TuplePat ps _ _ -> foldr patThings tl ps
  261. PArrPat ps _ -> foldr patThings tl ps
  262. ConPatIn _ conargs -> conArgs conargs tl
  263. ConPatOut _ _ _ _ conargs _ -> conArgs conargs tl
  264. LitPat _ -> tl
  265. NPat _ _ _ -> tl -- form of literal pattern?
  266. NPlusKPat id _ _ _ -> thing id : tl
  267. SigPatIn p _ -> patThings p tl
  268. SigPatOut p _ -> patThings p tl
  269. _ -> error "boundThings"
  270. conArgs (PrefixCon ps) tl = foldr patThings tl ps
  271. conArgs (RecCon (HsRecFields { rec_flds = flds })) tl
  272. = foldr (\f tl' -> patThings (hsRecFieldArg f) tl') tl flds
  273. conArgs (InfixCon p1 p2) tl = patThings p1 $ patThings p2 tl
  274. -- stuff for dealing with ctags output format
  275. writeTagsData :: (Maybe Handle, Maybe Handle) -> FileData -> IO ()
  276. writeTagsData (mb_ctags_hdl, mb_etags_hdl) fd = do
  277. maybe (return ()) (\hdl -> writectagsfile hdl fd) mb_ctags_hdl
  278. maybe (return ()) (\hdl -> writeetagsfile hdl fd) mb_etags_hdl
  279. writectagsfile :: Handle -> FileData -> IO ()
  280. writectagsfile ctagsfile filedata = do
  281. let things = getfoundthings filedata
  282. mapM_ (\x -> hPutStrLn ctagsfile $ dumpthing False x) things
  283. mapM_ (\x -> hPutStrLn ctagsfile $ dumpthing True x) things
  284. getfoundthings :: FileData -> [FoundThing]
  285. getfoundthings (FileData _filename things _src_lines) = things
  286. dumpthing :: Bool -> FoundThing -> String
  287. dumpthing showmod (FoundThing modname name loc) =
  288. fullname ++ "\t" ++ filename ++ "\t" ++ (show line)
  289. where line = srcLocLine loc
  290. filename = unpackFS $ srcLocFile loc
  291. fullname = if showmod then moduleNameString modname ++ "." ++ name
  292. else name
  293. -- stuff for dealing with etags output format
  294. writeetagsfile :: Handle -> FileData -> IO ()
  295. writeetagsfile etagsfile = hPutStr etagsfile . e_dumpfiledata
  296. e_dumpfiledata :: FileData -> String
  297. e_dumpfiledata (FileData filename things line_map) =
  298. "\x0c\n" ++ filename ++ "," ++ (show thingslength) ++ "\n" ++ thingsdump
  299. where
  300. thingsdump = concat $ map (e_dumpthing line_map) things
  301. thingslength = length thingsdump
  302. e_dumpthing :: Map Int String -> FoundThing -> String
  303. e_dumpthing src_lines (FoundThing modname name loc) =
  304. tagline name ++ tagline (moduleNameString modname ++ "." ++ name)
  305. where tagline n = src_code ++ "\x7f"
  306. ++ n ++ "\x01"
  307. ++ (show line) ++ "," ++ (show $ column) ++ "\n"
  308. line = srcLocLine loc
  309. column = srcLocCol loc
  310. src_code = case M.lookup line src_lines of
  311. Just l -> take (column + length name) l
  312. Nothing -> --trace (show ("not found: ", moduleNameString modname, name, line, column))
  313. name