1 {-# OPTIONS_GHC -XCPP -XPatternGuards -XScopedTypeVariables -Wall #-}
4 import Prelude hiding ( mod, id, mapM )
5 import GHC hiding (flags)
7 import HscTypes ( isBootSummary )
9 import Digraph ( flattenSCCs )
10 import DriverPhases ( isHaskellSrcFilename )
11 import HscTypes ( msHsFilePath )
12 import Name ( getOccString )
13 --import ErrUtils ( printBagOfErrors )
14 import DynFlags ( defaultDynFlags )
17 import Exception -- ( ghandle )
19 import MonadUtils ( liftIO )
21 -- Every GHC comes with Cabal anyways, so this is not a bad new dependency
22 import Distribution.Simple.GHC ( ghcOptions )
23 import Distribution.Simple.Configure ( getPersistBuildConfig )
24 import Distribution.PackageDescription ( library, libBuildInfo )
25 import Distribution.Simple.LocalBuildInfo ( localPkgDescr, buildDir )
27 import Control.Monad hiding (mapM)
28 import System.Environment
29 import System.Console.GetOpt
33 import Data.List as List hiding ( group )
35 import Data.Traversable (mapM)
36 import Data.Map ( Map )
37 import qualified Data.Map as M
42 -- search for definitions of things
43 -- we do this by parsing the source and grabbing top-level definitions
45 -- We generate both CTAGS and ETAGS format tags files
46 -- The former is for use in most sensible editors, while EMACS uses ETAGS
48 ----------------------------------
49 ---- CENTRAL DATA TYPES ----------
51 type FileName = String
52 type ThingName = String -- name of a defined entity in a Haskell program
54 -- A definition we have found (we know its containing module, name, and location)
55 data FoundThing = FoundThing ModuleName ThingName SrcLoc
57 -- Data we have obtained from a file (list of things we found)
58 data FileData = FileData FileName [FoundThing] (Map Int String)
59 --- invariant (not checked): every found thing has a source location in that file?
62 ------------------------------
63 -------- MAIN PROGRAM --------
67 progName <- getProgName
69 "Usage: " ++ progName ++ " [OPTION...] [-- GHC OPTION... --] [files...]"
71 let (ghcArgs', ourArgs, unbalanced) = splitArgs args
72 let (flags, filenames, errs) = getOpt Permute options ourArgs
73 let (hsfiles, otherfiles) = List.partition isHaskellSrcFilename filenames
75 let ghc_topdir = case [ d | FlagTopDir d <- flags ] of
78 mapM_ (\n -> putStr $ "Warning: ignoring non-Haskellish file " ++ n ++ "\n")
80 if unbalanced || errs /= [] || elem FlagHelp flags || hsfiles == []
83 putStr $ usageInfo usageString options
84 exitWith (ExitFailure 1)
87 ghcArgs <- case [ d | FlagUseCabalConfig d <- flags ] of
89 cabalOpts <- flagsFromCabal distPref
90 return (ghcArgs' ++ cabalOpts)
93 _ -> error "Too many --use-cabal-config flags"
96 let modes = getMode flags
97 let openFileMode = if elem FlagAppend flags
100 ctags_hdl <- if CTags `elem` modes
101 then Just `liftM` openFile "tags" openFileMode
103 etags_hdl <- if ETags `elem` modes
104 then Just `liftM` openFile "TAGS" openFileMode
107 GHC.defaultErrorHandler defaultDynFlags $
108 runGhc (Just ghc_topdir) $ do
109 --liftIO $ print "starting up session"
110 dflags <- getSessionDynFlags
111 (pflags, unrec, warns) <- parseDynamicFlags dflags{ verbosity=1 }
113 unless (null unrec) $
114 liftIO $ putStrLn $ "Unrecognised options:\n" ++ show (map unLoc unrec)
115 liftIO $ mapM_ putStrLn (map unLoc warns)
116 let dflags2 = pflags { hscTarget = HscNothing } -- don't generate anything
117 -- liftIO $ print ("pkgDB", case (pkgDatabase dflags2) of Nothing -> 0
118 -- Just m -> sizeUFM m)
119 setSessionDynFlags dflags2
120 --liftIO $ print (length pkgs)
122 GHC.defaultCleanupHandler dflags2 $ do
124 targetsAtOneGo hsfiles (ctags_hdl,etags_hdl)
125 mapM_ (mapM (liftIO . hClose)) [ctags_hdl, etags_hdl]
127 ----------------------------------------------
128 ---------- ARGUMENT PROCESSING --------------
136 | FlagTopDir FilePath
137 | FlagUseCabalConfig FilePath
139 deriving (Ord, Eq, Show)
140 -- ^Represents options passed to the program
142 data Mode = ETags | CTags deriving Eq
144 getMode :: [Flag] -> [Mode]
145 getMode fs = go (concatMap modeLike fs)
146 where go [] = [ETags,CTags]
150 modeLike FlagETags = [ETags]
151 modeLike FlagCTags = [CTags]
152 modeLike FlagBoth = [ETags,CTags]
155 splitArgs :: [String] -> ([String], [String], Bool)
156 -- ^Pull out arguments between -- for GHC
157 splitArgs args0 = split [] [] False args0
158 where split ghc' tags' unbal ("--" : args) = split tags' ghc' (not unbal) args
159 split ghc' tags' unbal (arg : args) = split ghc' (arg:tags') unbal args
160 split ghc' tags' unbal [] = (reverse ghc', reverse tags', unbal)
162 options :: [OptDescr Flag]
164 options = [ Option "" ["topdir"]
165 (ReqArg FlagTopDir "DIR") "root of GHC installation (optional)"
166 , Option "c" ["ctags"]
167 (NoArg FlagCTags) "generate CTAGS file (ctags)"
168 , Option "e" ["etags"]
169 (NoArg FlagETags) "generate ETAGS file (etags)"
170 , Option "b" ["both"]
171 (NoArg FlagBoth) ("generate both CTAGS and ETAGS")
172 , Option "a" ["append"]
173 (NoArg FlagAppend) ("append to existing CTAGS and/or ETAGS file(s)")
174 , Option "" ["use-cabal-config"]
175 (ReqArg FlagUseCabalConfig "DIR") "use local cabal configuration from dist dir"
176 , Option "" ["files-from-cabal"]
177 (NoArg FlagFilesFromCabal) "use files from cabal"
178 , Option "h" ["help"] (NoArg FlagHelp) "This help"
181 flagsFromCabal :: FilePath -> IO [String]
182 flagsFromCabal distPref = do
183 lbi <- getPersistBuildConfig distPref
184 let pd = localPkgDescr lbi
186 Nothing -> error "no library"
188 let bi = libBuildInfo lib
190 opts = ghcOptions lbi bi odir
193 ----------------------------------------------------------------
194 --- LOADING HASKELL SOURCE
195 --- (these bits actually run the compiler and produce abstract syntax)
197 safeLoad :: LoadHowMuch -> Ghc SuccessFlag
198 -- like GHC.load, but does not stop process on exception
200 _dflags <- getSessionDynFlags
201 ghandle (\(e :: SomeException) -> liftIO (print e) >> return Failed ) $
202 handleSourceError (\e -> printExceptionAndWarnings e >> return Failed) $
206 targetsAtOneGo :: [FileName] -> (Maybe Handle, Maybe Handle) -> Ghc ()
207 -- load a list of targets
208 targetsAtOneGo hsfiles handles = do
209 targets <- mapM (\f -> guessTarget f Nothing) hsfiles
211 modgraph <- depanal [] False
212 let mods = flattenSCCs $ topSortModuleGraph False modgraph Nothing
213 graphData mods handles
215 fileTarget :: FileName -> Target
216 fileTarget filename = Target (TargetFile filename Nothing) True Nothing
218 ---------------------------------------------------------------
219 ----- CRAWLING ABSTRACT SYNTAX TO SNAFFLE THE DEFINITIONS -----
221 graphData :: ModuleGraph -> (Maybe Handle, Maybe Handle) -> Ghc ()
222 graphData graph handles = do
223 mapM_ foundthings graph
224 where foundthings ms =
225 let filename = msHsFilePath ms
226 modname = moduleName $ ms_mod ms
227 in handleSourceError (\e -> do
228 printExceptionAndWarnings e
229 liftIO $ exitWith (ExitFailure 1)) $
230 do liftIO $ putStrLn ("loading " ++ filename)
231 mod <- loadModule =<< typecheckModule =<< parseModule ms
233 _ | isBootSummary ms -> return ()
234 _ | Just s <- renamedSource mod ->
235 liftIO (writeTagsData handles =<< fileData filename modname s)
237 liftIO $ exitWith (ExitFailure 1)
239 fileData :: FileName -> ModuleName -> RenamedSource -> IO FileData
240 fileData filename modname (group, _imports, _lie, _doc, _haddock) = do
241 -- lie is related to type checking and so is irrelevant
242 -- imports contains import declarations and no definitions
243 -- doc and haddock seem haddock-related; let's hope to ignore them
244 ls <- lines `fmap` readFile filename
245 let line_map = M.fromAscList $ zip [1..] ls
247 return $ FileData filename (boundValues modname group) line_map
249 boundValues :: ModuleName -> HsGroup Name -> [FoundThing]
250 -- ^Finds all the top-level definitions in a module
251 boundValues mod group =
252 let vals = case hs_valds group of
253 ValBindsOut nest _sigs ->
254 [ x | (_rec, binds) <- nest
255 , bind <- bagToList binds
256 , x <- boundThings mod bind ]
257 _other -> error "boundValues"
258 tys = [ n | ns <- map (tyClDeclNames . unLoc) (hs_tyclds group)
259 , n <- map found ns ]
260 fors = concat $ map forBound (hs_fords group)
261 where forBound lford = case unLoc lford of
262 ForeignImport n _ _ -> [found n]
263 ForeignExport { } -> []
264 in vals ++ tys ++ fors
265 where dataNames tycon cons = found tycon : map conName cons
266 conName td = found $ con_name $ unLoc td
267 found = foundOfLName mod
269 startOfLocated :: Located a -> SrcLoc
270 startOfLocated lHs = srcSpanStart $ getLoc lHs
272 foundOfLName :: ModuleName -> Located Name -> FoundThing
273 foundOfLName mod id = FoundThing mod (getOccString $ unLoc id) (startOfLocated id)
275 boundThings :: ModuleName -> LHsBind Name -> [FoundThing]
276 boundThings modname lbinding =
277 case unLoc lbinding of
278 FunBind { fun_id = id } -> [thing id]
279 PatBind { pat_lhs = lhs } -> patThings lhs []
280 VarBind { var_id = id } -> [FoundThing modname (getOccString id) (startOfLocated lbinding)]
281 AbsBinds { } -> [] -- nothing interesting in a type abstraction
282 where thing = foundOfLName modname
284 let loc = startOfLocated lpat
285 lid id = FoundThing modname (getOccString id) loc
286 in case unLoc lpat of
288 VarPat name -> lid name : tl
289 VarPatOut name _ -> lid name : tl -- XXX need help here
290 LazyPat p -> patThings p tl
291 AsPat id p -> patThings p (thing id : tl)
292 ParPat p -> patThings p tl
293 BangPat p -> patThings p tl
294 ListPat ps _ -> foldr patThings tl ps
295 TuplePat ps _ _ -> foldr patThings tl ps
296 PArrPat ps _ -> foldr patThings tl ps
297 ConPatIn _ conargs -> conArgs conargs tl
298 ConPatOut _ _ _ _ conargs _ -> conArgs conargs tl
300 #if __GLASGOW_HASKELL__ > 608
301 NPat _ _ _ -> tl -- form of literal pattern?
303 NPat _ _ _ _ -> tl -- form of literal pattern?
305 NPlusKPat id _ _ _ -> thing id : tl
306 TypePat _ -> tl -- XXX need help here
307 SigPatIn p _ -> patThings p tl
308 SigPatOut p _ -> patThings p tl
309 _ -> error "boundThings"
310 conArgs (PrefixCon ps) tl = foldr patThings tl ps
311 conArgs (RecCon (HsRecFields { rec_flds = flds })) tl
312 = foldr (\f tl' -> patThings (hsRecFieldArg f) tl') tl flds
313 conArgs (InfixCon p1 p2) tl = patThings p1 $ patThings p2 tl
316 -- stuff for dealing with ctags output format
318 writeTagsData :: (Maybe Handle, Maybe Handle) -> FileData -> IO ()
319 writeTagsData (mb_ctags_hdl, mb_etags_hdl) fd = do
320 maybe (return ()) (\hdl -> writectagsfile hdl fd) mb_ctags_hdl
321 maybe (return ()) (\hdl -> writeetagsfile hdl fd) mb_etags_hdl
323 writectagsfile :: Handle -> FileData -> IO ()
324 writectagsfile ctagsfile filedata = do
325 let things = getfoundthings filedata
326 mapM_ (\x -> hPutStrLn ctagsfile $ dumpthing False x) things
327 mapM_ (\x -> hPutStrLn ctagsfile $ dumpthing True x) things
329 getfoundthings :: FileData -> [FoundThing]
330 getfoundthings (FileData _filename things _src_lines) = things
332 dumpthing :: Bool -> FoundThing -> String
333 dumpthing showmod (FoundThing modname name loc) =
334 fullname ++ "\t" ++ filename ++ "\t" ++ (show line)
335 where line = srcLocLine loc
336 filename = unpackFS $ srcLocFile loc
337 fullname = if showmod then moduleNameString modname ++ "." ++ name
340 -- stuff for dealing with etags output format
342 writeetagsfile :: Handle -> FileData -> IO ()
343 writeetagsfile etagsfile = hPutStr etagsfile . e_dumpfiledata
345 e_dumpfiledata :: FileData -> String
346 e_dumpfiledata (FileData filename things line_map) =
347 "\x0c\n" ++ filename ++ "," ++ (show thingslength) ++ "\n" ++ thingsdump
349 thingsdump = concat $ map (e_dumpthing line_map) things
350 thingslength = length thingsdump
352 e_dumpthing :: Map Int String -> FoundThing -> String
353 e_dumpthing src_lines (FoundThing modname name loc) =
354 tagline name ++ tagline (moduleNameString modname ++ "." ++ name)
355 where tagline n = src_code ++ "\x7f"
357 ++ (show line) ++ "," ++ (show $ column) ++ "\n"
358 line = srcLocLine loc
359 column = srcLocCol loc
360 src_code = case M.lookup line src_lines of
361 Just l -> take (column + length name) l
362 Nothing -> --trace (show ("not found: ", moduleNameString modname, name, line, column))