3 % (c) University of Glasgow, 2007
5 \section[Coverage]{@coverage@: the main function}
8 module Coverage (addCoverageTicksToBinds) where
30 import System.Directory ( createDirectoryIfMissing )
36 import Data.HashTable ( hashString )
38 import qualified Data.Map as Map
42 %************************************************************************
44 %* The main function: addCoverageTicksToBinds
46 %************************************************************************
49 addCoverageTicksToBinds
52 -> ModLocation -- of the current module
53 -> [TyCon] -- type constructor in this module
55 -> IO (LHsBinds Id, HpcInfo, ModBreaks)
57 addCoverageTicksToBinds dflags mod mod_loc tyCons binds =
58 case ml_hs_file mod_loc of
59 Nothing -> return (binds, emptyHpcInfo False, emptyModBreaks)
62 if "boot" `isSuffixOf` orig_file then return (binds, emptyHpcInfo False, emptyModBreaks) else do
64 -- Now, we try look for a file generated from a .hsc file to a .hs file, by peeking ahead.
66 let top_pos = catMaybes $ foldrBag (\ (L pos _) rest -> srcSpanFileName_maybe pos : rest) [] binds
67 let orig_file2 = case top_pos of
69 | ".hsc" `isSuffixOf` unpackFS file_name -> unpackFS file_name
72 let mod_name = moduleNameString (moduleName mod)
75 = unTM (addTickLHsBinds binds)
77 { fileName = mkFastString orig_file2
79 , inScope = emptyVarSet
80 , blackList = Map.fromList [ (getSrcSpan (tyConName tyCon),())
88 let entries = reverse $ mixEntries st
90 -- write the mix entries for this module
91 hashNo <- if opt_Hpc then do
92 let hpc_dir = hpcDir dflags
94 let hpc_mod_dir = if modulePackageId mod == mainPackageId
96 else hpc_dir ++ "/" ++ packageIdString (modulePackageId mod)
98 let tabStop = 1 -- <tab> counts as a normal char in GHC's location ranges.
99 createDirectoryIfMissing True hpc_mod_dir
100 modTime <- getModificationTime orig_file2
101 let entries' = [ (hpcPos, box)
102 | (span,_,_,box) <- entries, hpcPos <- [mkHpcPos span] ]
103 when (length entries' /= tickBoxCount st) $ do
104 panic "the number of .mix entries are inconsistent"
105 let hashNo = mixHash orig_file2 modTime tabStop entries'
106 mixCreate hpc_mod_dir mod_name
107 $ Mix orig_file2 modTime (toHash hashNo) tabStop entries'
112 -- Todo: use proper src span type
113 breakArray <- newBreakArray $ length entries
115 let locsTicks = listArray (0,tickBoxCount st-1)
116 [ span | (span,_,_,_) <- entries ]
117 varsTicks = listArray (0,tickBoxCount st-1)
118 [ vars | (_,_,vars,_) <- entries ]
119 declsTicks= listArray (0,tickBoxCount st-1)
120 [ decls | (_,decls,_,_) <- entries ]
121 modBreaks = emptyModBreaks
122 { modBreaks_flags = breakArray
123 , modBreaks_locs = locsTicks
124 , modBreaks_vars = varsTicks
125 , modBreaks_decls = declsTicks
128 doIfSet_dyn dflags Opt_D_dump_hpc $ do
129 printDump (pprLHsBinds binds1)
131 return (binds1, HpcInfo (tickBoxCount st) hashNo, modBreaks)
136 liftL :: (Monad m) => (a -> m a) -> Located a -> m (Located a)
137 liftL f (L loc a) = do
141 addTickLHsBinds :: LHsBinds Id -> TM (LHsBinds Id)
142 addTickLHsBinds binds = mapBagM addTickLHsBind binds
144 addTickLHsBind :: LHsBind Id -> TM (LHsBind Id)
145 addTickLHsBind (L pos bind@(AbsBinds { abs_binds = binds })) = do
146 binds' <- addTickLHsBinds binds
147 return $ L pos $ bind { abs_binds = binds' }
148 addTickLHsBind (L pos (funBind@(FunBind { fun_id = (L _ id) }))) = do
149 let name = getOccString id
150 decl_path <- getPathEntry
152 (fvs, (MatchGroup matches' ty)) <-
155 addTickMatchGroup (fun_matches funBind)
157 blackListed <- isBlackListed pos
159 -- Todo: we don't want redundant ticks on simple pattern bindings
160 -- We don't want to generate code for blacklisted positions
161 if blackListed || (not opt_Hpc && isSimplePatBind funBind)
163 return $ L pos $ funBind { fun_matches = MatchGroup matches' ty
167 tick_no <- allocATickBox (if null decl_path
168 then TopLevelBox [name]
169 else LocalBox (decl_path ++ [name]))
172 return $ L pos $ funBind { fun_matches = MatchGroup matches' ty
176 -- a binding is a simple pattern binding if it is a funbind with zero patterns
177 isSimplePatBind :: HsBind a -> Bool
178 isSimplePatBind funBind = matchGroupArity (fun_matches funBind) == 0
180 -- TODO: Revisit this
181 addTickLHsBind (L pos (pat@(PatBind { pat_rhs = rhs }))) = do
183 rhs' <- addPathEntry name $ addTickGRHSs False rhs
185 decl_path <- getPathEntry
186 tick_me <- allocTickBox (if null decl_path
187 then TopLevelBox [name]
188 else LocalBox (name : decl_path))
190 return $ L pos $ pat { pat_rhs = rhs' }
192 -- Only internal stuff, not from source, uses VarBind, so we ignore it.
193 addTickLHsBind var_bind@(L _ (VarBind {})) = return var_bind
195 -- Add a tick to the expression no matter what it is. There is one exception:
196 -- for the debugger, if the expression is a 'let', then we don't want to add
197 -- a tick here because there will definititely be a tick on the body anyway.
198 addTickLHsExprAlways :: LHsExpr Id -> TM (LHsExpr Id)
199 addTickLHsExprAlways (L pos e0)
200 | not opt_Hpc, HsLet _ _ <- e0 = addTickLHsExprNever (L pos e0)
201 | otherwise = allocTickBox (ExpBox False) pos $ addTickHsExpr e0
203 addTickLHsExprNeverOrAlways :: LHsExpr Id -> TM (LHsExpr Id)
204 addTickLHsExprNeverOrAlways e
205 | opt_Hpc = addTickLHsExprNever e
206 | otherwise = addTickLHsExprAlways e
208 addTickLHsExprNeverOrMaybe :: LHsExpr Id -> TM (LHsExpr Id)
209 addTickLHsExprNeverOrMaybe e
210 | opt_Hpc = addTickLHsExprNever e
211 | otherwise = addTickLHsExpr e
213 -- version of addTick that does not actually add a tick,
214 -- because the scope of this tick is completely subsumed by
216 addTickLHsExprNever :: LHsExpr Id -> TM (LHsExpr Id)
217 addTickLHsExprNever (L pos e0) = do
218 e1 <- addTickHsExpr e0
221 -- selectively add ticks to interesting expressions
222 addTickLHsExpr :: LHsExpr Id -> TM (LHsExpr Id)
223 addTickLHsExpr (L pos e0) = do
224 if opt_Hpc || isGoodBreakExpr e0
226 allocTickBox (ExpBox False) pos $ addTickHsExpr e0
228 e1 <- addTickHsExpr e0
231 -- general heuristic: expressions which do not denote values are good break points
232 isGoodBreakExpr :: HsExpr Id -> Bool
233 isGoodBreakExpr (HsApp {}) = True
234 isGoodBreakExpr (OpApp {}) = True
235 isGoodBreakExpr (NegApp {}) = True
236 isGoodBreakExpr (HsCase {}) = True
237 isGoodBreakExpr (HsIf {}) = True
238 isGoodBreakExpr (RecordCon {}) = True
239 isGoodBreakExpr (RecordUpd {}) = True
240 isGoodBreakExpr (ArithSeq {}) = True
241 isGoodBreakExpr (PArrSeq {}) = True
242 isGoodBreakExpr _other = False
244 addTickLHsExprOptAlt :: Bool -> LHsExpr Id -> TM (LHsExpr Id)
245 addTickLHsExprOptAlt oneOfMany (L pos e0)
246 | not opt_Hpc = addTickLHsExpr (L pos e0)
248 allocTickBox (ExpBox oneOfMany) pos $
251 addBinTickLHsExpr :: (Bool -> BoxLabel) -> LHsExpr Id -> TM (LHsExpr Id)
252 addBinTickLHsExpr boxLabel (L pos e0) =
253 allocBinTickBox boxLabel pos $
256 addTickHsExpr :: HsExpr Id -> TM (HsExpr Id)
257 addTickHsExpr e@(HsVar id) = do freeVar id; return e
258 addTickHsExpr e@(HsIPVar _) = return e
259 addTickHsExpr e@(HsOverLit _) = return e
260 addTickHsExpr e@(HsLit _) = return e
261 addTickHsExpr (HsLam matchgroup) =
262 liftM HsLam (addTickMatchGroup matchgroup)
263 addTickHsExpr (HsApp e1 e2) =
264 liftM2 HsApp (addTickLHsExprNever e1) (addTickLHsExpr e2)
265 addTickHsExpr (OpApp e1 e2 fix e3) =
268 (addTickLHsExprNever e2)
271 addTickHsExpr (NegApp e neg) =
274 (addTickSyntaxExpr hpcSrcSpan neg)
275 addTickHsExpr (HsPar e) = liftM HsPar (addTickLHsExprNeverOrMaybe e)
276 addTickHsExpr (SectionL e1 e2) =
280 addTickHsExpr (SectionR e1 e2) =
284 addTickHsExpr (ExplicitTuple es boxity) =
286 (mapM addTickTupArg es)
288 addTickHsExpr (HsCase e mgs) =
291 (addTickMatchGroup mgs)
292 addTickHsExpr (HsIf cnd e1 e2 e3) =
294 (addBinTickLHsExpr (BinBox CondBinBox) e1)
295 (addTickLHsExprOptAlt True e2)
296 (addTickLHsExprOptAlt True e3)
297 addTickHsExpr (HsLet binds e) =
298 bindLocals (collectLocalBinders binds) $
300 (addTickHsLocalBinds binds) -- to think about: !patterns.
301 (addTickLHsExprNeverOrAlways e)
302 addTickHsExpr (HsDo cxt stmts last_exp srcloc) = do
303 (stmts', last_exp') <- addTickLStmts' forQual stmts
304 (addTickLHsExpr last_exp)
305 return (HsDo cxt stmts' last_exp' srcloc)
307 forQual = case cxt of
308 ListComp -> Just $ BinBox QualBinBox
310 addTickHsExpr (ExplicitList ty es) =
313 (mapM (addTickLHsExpr) es)
314 addTickHsExpr (ExplicitPArr ty es) =
317 (mapM (addTickLHsExpr) es)
318 addTickHsExpr (RecordCon id ty rec_binds) =
322 (addTickHsRecordBinds rec_binds)
323 addTickHsExpr (RecordUpd e rec_binds cons tys1 tys2) =
326 (addTickHsRecordBinds rec_binds)
327 (return cons) (return tys1) (return tys2)
329 addTickHsExpr (ExprWithTySigOut e ty) =
330 liftM2 ExprWithTySigOut
331 (addTickLHsExprNever e) -- No need to tick the inner expression
332 -- for expressions with signatures
334 addTickHsExpr (ArithSeq ty arith_seq) =
337 (addTickArithSeqInfo arith_seq)
338 addTickHsExpr (HsTickPragma _ (L pos e0)) = do
339 e2 <- allocTickBox (ExpBox False) pos $
342 addTickHsExpr (PArrSeq ty arith_seq) =
345 (addTickArithSeqInfo arith_seq)
346 addTickHsExpr (HsSCC nm e) =
350 addTickHsExpr (HsCoreAnn nm e) =
354 addTickHsExpr e@(HsBracket {}) = return e
355 addTickHsExpr e@(HsBracketOut {}) = return e
356 addTickHsExpr e@(HsSpliceE {}) = return e
357 addTickHsExpr (HsProc pat cmdtop) =
360 (liftL (addTickHsCmdTop) cmdtop)
361 addTickHsExpr (HsWrap w e) =
364 (addTickHsExpr e) -- explicitly no tick on inside
365 addTickHsExpr (HsArrApp e1 e2 ty1 arr_ty lr) =
372 addTickHsExpr (HsArrForm e fix cmdtop) =
376 (mapM (liftL (addTickHsCmdTop)) cmdtop)
378 addTickHsExpr e@(HsType _) = return e
380 -- Others dhould never happen in expression content.
381 addTickHsExpr e = pprPanic "addTickHsExpr" (ppr e)
383 addTickTupArg :: HsTupArg Id -> TM (HsTupArg Id)
384 addTickTupArg (Present e) = do { e' <- addTickLHsExpr e; return (Present e') }
385 addTickTupArg (Missing ty) = return (Missing ty)
387 addTickMatchGroup :: MatchGroup Id -> TM (MatchGroup Id)
388 addTickMatchGroup (MatchGroup matches ty) = do
389 let isOneOfMany = matchesOneOfMany matches
390 matches' <- mapM (liftL (addTickMatch isOneOfMany)) matches
391 return $ MatchGroup matches' ty
393 addTickMatch :: Bool -> Match Id -> TM (Match Id)
394 addTickMatch isOneOfMany (Match pats opSig gRHSs) =
395 bindLocals (collectPatsBinders pats) $ do
396 gRHSs' <- addTickGRHSs isOneOfMany gRHSs
397 return $ Match pats opSig gRHSs'
399 addTickGRHSs :: Bool -> GRHSs Id -> TM (GRHSs Id)
400 addTickGRHSs isOneOfMany (GRHSs guarded local_binds) = do
401 bindLocals binders $ do
402 local_binds' <- addTickHsLocalBinds local_binds
403 guarded' <- mapM (liftL (addTickGRHS isOneOfMany)) guarded
404 return $ GRHSs guarded' local_binds'
406 binders = collectLocalBinders local_binds
408 addTickGRHS :: Bool -> GRHS Id -> TM (GRHS Id)
409 addTickGRHS isOneOfMany (GRHS stmts expr) = do
410 (stmts',expr') <- addTickLStmts' (Just $ BinBox $ GuardBinBox) stmts
411 (if opt_Hpc then addTickLHsExprOptAlt isOneOfMany expr
412 else addTickLHsExprAlways expr)
413 return $ GRHS stmts' expr'
415 addTickLStmts :: (Maybe (Bool -> BoxLabel)) -> [LStmt Id] -> TM [LStmt Id]
416 addTickLStmts isGuard stmts = do
417 (stmts, _) <- addTickLStmts' isGuard stmts (return ())
420 addTickLStmts' :: (Maybe (Bool -> BoxLabel)) -> [LStmt Id] -> TM a
421 -> TM ([LStmt Id], a)
422 addTickLStmts' isGuard lstmts res
423 = bindLocals binders $ do
424 lstmts' <- mapM (liftL (addTickStmt isGuard)) lstmts
428 binders = collectLStmtsBinders lstmts
430 addTickStmt :: (Maybe (Bool -> BoxLabel)) -> Stmt Id -> TM (Stmt Id)
431 addTickStmt _isGuard (BindStmt pat e bind fail) = do
434 (addTickLHsExprAlways e)
435 (addTickSyntaxExpr hpcSrcSpan bind)
436 (addTickSyntaxExpr hpcSrcSpan fail)
437 addTickStmt isGuard (ExprStmt e bind' ty) = do
440 (addTickSyntaxExpr hpcSrcSpan bind')
442 addTickStmt _isGuard (LetStmt binds) = do
444 (addTickHsLocalBinds binds)
445 addTickStmt isGuard (ParStmt pairs) = do
447 (mapM (addTickStmtAndBinders isGuard) pairs)
449 addTickStmt isGuard (TransformStmt stmts ids usingExpr maybeByExpr) = do
451 (addTickLStmts isGuard stmts)
453 (addTickLHsExprAlways usingExpr)
454 (addTickMaybeByLHsExpr maybeByExpr)
456 addTickStmt isGuard (GroupStmt stmts binderMap by using) = do
458 (addTickLStmts isGuard stmts)
460 (fmapMaybeM addTickLHsExprAlways by)
461 (fmapEitherM addTickLHsExprAlways (addTickSyntaxExpr hpcSrcSpan) using)
463 addTickStmt isGuard stmt@(RecStmt {})
464 = do { stmts' <- addTickLStmts isGuard (recS_stmts stmt)
465 ; ret' <- addTickSyntaxExpr hpcSrcSpan (recS_ret_fn stmt)
466 ; mfix' <- addTickSyntaxExpr hpcSrcSpan (recS_mfix_fn stmt)
467 ; bind' <- addTickSyntaxExpr hpcSrcSpan (recS_bind_fn stmt)
468 ; dicts' <- addTickEvBinds (recS_dicts stmt)
469 ; return (stmt { recS_stmts = stmts', recS_ret_fn = ret'
470 , recS_mfix_fn = mfix', recS_bind_fn = bind'
471 , recS_dicts = dicts' }) }
473 addTick :: Maybe (Bool -> BoxLabel) -> LHsExpr Id -> TM (LHsExpr Id)
474 addTick isGuard e | Just fn <- isGuard = addBinTickLHsExpr fn e
475 | otherwise = addTickLHsExprAlways e
477 addTickStmtAndBinders :: Maybe (Bool -> BoxLabel) -> ([LStmt Id], a)
478 -> TM ([LStmt Id], a)
479 addTickStmtAndBinders isGuard (stmts, ids) =
481 (addTickLStmts isGuard stmts)
484 addTickMaybeByLHsExpr :: Maybe (LHsExpr Id) -> TM (Maybe (LHsExpr Id))
485 addTickMaybeByLHsExpr maybeByExpr =
487 Nothing -> return Nothing
488 Just byExpr -> addTickLHsExprAlways byExpr >>= (return . Just)
490 addTickHsLocalBinds :: HsLocalBinds Id -> TM (HsLocalBinds Id)
491 addTickHsLocalBinds (HsValBinds binds) =
493 (addTickHsValBinds binds)
494 addTickHsLocalBinds (HsIPBinds binds) =
496 (addTickHsIPBinds binds)
497 addTickHsLocalBinds (EmptyLocalBinds) = return EmptyLocalBinds
499 addTickHsValBinds :: HsValBindsLR Id a -> TM (HsValBindsLR Id b)
500 addTickHsValBinds (ValBindsOut binds sigs) =
502 (mapM (\ (rec,binds') ->
505 (addTickLHsBinds binds'))
508 addTickHsValBinds _ = panic "addTickHsValBinds"
510 addTickHsIPBinds :: HsIPBinds Id -> TM (HsIPBinds Id)
511 addTickHsIPBinds (IPBinds ipbinds dictbinds) =
513 (mapM (liftL (addTickIPBind)) ipbinds)
516 addTickIPBind :: IPBind Id -> TM (IPBind Id)
517 addTickIPBind (IPBind nm e) =
522 -- There is no location here, so we might need to use a context location??
523 addTickSyntaxExpr :: SrcSpan -> SyntaxExpr Id -> TM (SyntaxExpr Id)
524 addTickSyntaxExpr pos x = do
525 L _ x' <- addTickLHsExpr (L pos x)
527 -- we do not walk into patterns.
528 addTickLPat :: LPat Id -> TM (LPat Id)
529 addTickLPat pat = return pat
531 addTickHsCmdTop :: HsCmdTop Id -> TM (HsCmdTop Id)
532 addTickHsCmdTop (HsCmdTop cmd tys ty syntaxtable) =
539 addTickLHsCmd :: LHsCmd Id -> TM (LHsCmd Id)
540 addTickLHsCmd x = addTickLHsExpr x
542 addTickEvBinds :: TcEvBinds -> TM TcEvBinds
543 addTickEvBinds x = return x -- No coverage testing for dictionary binding
545 addTickHsRecordBinds :: HsRecordBinds Id -> TM (HsRecordBinds Id)
546 addTickHsRecordBinds (HsRecFields fields dd)
547 = do { fields' <- mapM process fields
548 ; return (HsRecFields fields' dd) }
550 process (HsRecField ids expr doc)
551 = do { expr' <- addTickLHsExpr expr
552 ; return (HsRecField ids expr' doc) }
554 addTickArithSeqInfo :: ArithSeqInfo Id -> TM (ArithSeqInfo Id)
555 addTickArithSeqInfo (From e1) =
558 addTickArithSeqInfo (FromThen e1 e2) =
562 addTickArithSeqInfo (FromTo e1 e2) =
566 addTickArithSeqInfo (FromThenTo e1 e2 e3) =
574 data TickTransState = TT { tickBoxCount:: Int
575 , mixEntries :: [MixEntry_]
578 data TickTransEnv = TTE { fileName :: FastString
579 , declPath :: [String]
581 , blackList :: Map SrcSpan ()
586 type FreeVars = OccEnv Id
591 -- For breakpoints we want to collect the free variables of an
592 -- expression for pinning on the HsTick. We don't want to collect
593 -- *all* free variables though: in particular there's no point pinning
594 -- on free variables that are will otherwise be in scope at the GHCi
595 -- prompt, which means all top-level bindings. Unfortunately detecting
596 -- top-level bindings isn't easy (collectHsBindsBinders on the top-level
597 -- bindings doesn't do it), so we keep track of a set of "in-scope"
598 -- variables in addition to the free variables, and the former is used
599 -- to filter additions to the latter. This gives us complete control
600 -- over what free variables we track.
602 data TM a = TM { unTM :: TickTransEnv -> TickTransState -> (a,FreeVars,TickTransState) }
603 -- a combination of a state monad (TickTransState) and a writer
606 instance Monad TM where
607 return a = TM $ \ _env st -> (a,noFVs,st)
608 (TM m) >>= k = TM $ \ env st ->
611 case unTM (k r1) env st1 of
613 (r2, fv1 `plusOccEnv` fv2, st2)
615 -- getState :: TM TickTransState
616 -- getState = TM $ \ env st -> (st, noFVs, st)
618 -- setState :: (TickTransState -> TickTransState) -> TM ()
619 -- setState f = TM $ \ env st -> ((), noFVs, f st)
621 getEnv :: TM TickTransEnv
622 getEnv = TM $ \ env st -> (env, noFVs, st)
624 withEnv :: (TickTransEnv -> TickTransEnv) -> TM a -> TM a
625 withEnv f (TM m) = TM $ \ env st ->
627 (a, fvs, st') -> (a, fvs, st')
629 getFreeVars :: TM a -> TM (FreeVars, a)
631 = TM $ \ env st -> case m env st of (a, fv, st') -> ((fv,a), fv, st')
633 freeVar :: Id -> TM ()
634 freeVar id = TM $ \ env st ->
635 if id `elemVarSet` inScope env
636 then ((), unitOccEnv (nameOccName (idName id)) id, st)
639 addPathEntry :: String -> TM a -> TM a
640 addPathEntry nm = withEnv (\ env -> env { declPath = declPath env ++ [nm] })
642 getPathEntry :: TM [String]
643 getPathEntry = declPath `liftM` getEnv
645 getFileName :: TM FastString
646 getFileName = fileName `liftM` getEnv
648 sameFileName :: SrcSpan -> TM a -> TM a -> TM a
649 sameFileName pos out_of_scope in_scope = do
650 file_name <- getFileName
651 case srcSpanFileName_maybe pos of
653 | file_name == file_name2 -> in_scope
656 bindLocals :: [Id] -> TM a -> TM a
657 bindLocals new_ids (TM m)
659 case m env{ inScope = inScope env `extendVarSetList` new_ids } st of
660 (r, fv, st') -> (r, fv `delListFromOccEnv` occs, st')
661 where occs = [ nameOccName (idName id) | id <- new_ids ]
663 isBlackListed :: SrcSpan -> TM Bool
664 isBlackListed pos = TM $ \ env st ->
665 case Map.lookup pos (blackList env) of
666 Nothing -> (False,noFVs,st)
667 Just () -> (True,noFVs,st)
669 -- the tick application inherits the source position of its
670 -- expression argument to support nested box allocations
671 allocTickBox :: BoxLabel -> SrcSpan -> TM (HsExpr Id) -> TM (LHsExpr Id)
672 allocTickBox boxLabel pos m | isGoodSrcSpan' pos =
674 (do e <- m; return (L pos e)) $ do
675 (fvs, e) <- getFreeVars m
677 let c = tickBoxCount st
680 me = (pos, declPath env, map (nameOccName.idName) ids, boxLabel)
682 ( L pos (HsTick c ids (L pos e))
684 , st {tickBoxCount=c+1,mixEntries=me:mes}
686 allocTickBox _boxLabel pos m = do e <- m; return (L pos e)
688 -- the tick application inherits the source position of its
689 -- expression argument to support nested box allocations
690 allocATickBox :: BoxLabel -> SrcSpan -> FreeVars -> TM (Maybe (Int,[Id]))
691 allocATickBox boxLabel pos fvs | isGoodSrcSpan' pos =
693 (return Nothing) $ TM $ \ env st ->
695 | null (declPath env), TopLevelBox x <- boxLabel = x
696 | otherwise = declPath env
697 me = (pos, mydecl_path, map (nameOccName.idName) ids, boxLabel)
703 , st {tickBoxCount=c+1, mixEntries=me:mes}
705 allocATickBox _boxLabel _pos _fvs = return Nothing
707 allocBinTickBox :: (Bool -> BoxLabel) -> SrcSpan -> TM (HsExpr Id)
709 allocBinTickBox boxLabel pos m
710 | not opt_Hpc = allocTickBox (ExpBox False) pos m
711 | isGoodSrcSpan' pos =
715 let meT = (pos,declPath env, [],boxLabel True)
716 meF = (pos,declPath env, [],boxLabel False)
717 meE = (pos,declPath env, [],ExpBox False)
721 ( L pos $ HsTick c [] $ L pos $ HsBinTick (c+1) (c+2) (L pos e)
722 -- notice that F and T are reversed,
723 -- because we are building the list in
726 , st {tickBoxCount=c+3 , mixEntries=meF:meT:meE:mes}
728 allocBinTickBox _boxLabel pos m = do e <- m; return (L pos e)
730 isGoodSrcSpan' :: SrcSpan -> Bool
732 | not (isGoodSrcSpan pos) = False
733 | start == end = False
736 start = srcSpanStart pos
739 mkHpcPos :: SrcSpan -> HpcPos
741 | not (isGoodSrcSpan' pos) = panic "bad source span; expected such spans to be filtered out"
744 start = srcSpanStart pos
746 hpcPos = toHpcPos ( srcLocLine start
752 hpcSrcSpan :: SrcSpan
753 hpcSrcSpan = mkGeneralSrcSpan (fsLit "Haskell Program Coverage internals")
758 matchesOneOfMany :: [LMatch Id] -> Bool
759 matchesOneOfMany lmatches = sum (map matchCount lmatches) > 1
761 matchCount (L _ (Match _pats _ty (GRHSs grhss _binds))) = length grhss
766 type MixEntry_ = (SrcSpan, [String], [OccName], BoxLabel)
768 -- For the hash value, we hash everything: the file name,
769 -- the timestamp of the original source file, the tab stop,
770 -- and the mix entries. We cheat, and hash the show'd string.
771 -- This hash only has to be hashed at Mix creation time,
772 -- and is for sanity checking only.
774 mixHash :: FilePath -> Integer -> Int -> [MixEntry] -> Int
775 mixHash file tm tabstop entries = fromIntegral $ hashString
776 (show $ Mix file tm 0 tabstop entries)