3 % (c) University of Glasgow, 2007
5 \section[Coverage]{@coverage@: the main function}
8 module Coverage (addCoverageTicksToBinds) where
30 import System.IO (FilePath)
31 import System.Directory ( createDirectoryIfMissing )
37 import Data.HashTable ( hashString )
41 %************************************************************************
43 %* The main function: addCoverageTicksToBinds
45 %************************************************************************
48 addCoverageTicksToBinds
51 -> ModLocation -- of the current module
52 -> [TyCon] -- type constructor in this module
54 -> IO (LHsBinds Id, HpcInfo, ModBreaks)
56 addCoverageTicksToBinds dflags mod mod_loc tyCons binds = do
59 case ml_hs_file mod_loc of
61 Nothing -> panic "can not find the original file during hpc trans"
63 if "boot" `isSuffixOf` orig_file then return (binds, emptyHpcInfo False, emptyModBreaks) else do
65 -- Now, we try look for a file generated from a .hsc file to a .hs file, by peeking ahead.
67 let top_pos = catMaybes $ foldrBag (\ (L pos _) rest -> srcSpanFileName_maybe pos : rest) [] binds
68 let orig_file2 = case top_pos of
70 | ".hsc" `isSuffixOf` unpackFS file_name -> unpackFS file_name
73 let mod_name = moduleNameString (moduleName mod)
76 = unTM (addTickLHsBinds binds)
78 { fileName = mkFastString orig_file2
80 , inScope = emptyVarSet
81 , blackList = listToFM [ (getSrcSpan (tyConName tyCon),())
89 let entries = reverse $ mixEntries st
91 -- write the mix entries for this module
92 hashNo <- if opt_Hpc then do
93 let hpc_dir = hpcDir dflags
95 let hpc_mod_dir = if modulePackageId mod == mainPackageId
97 else hpc_dir ++ "/" ++ packageIdString (modulePackageId mod)
99 let tabStop = 1 -- <tab> counts as a normal char in GHC's location ranges.
100 createDirectoryIfMissing True hpc_mod_dir
101 modTime <- getModificationTime orig_file2
102 let entries' = [ (hpcPos, box)
103 | (span,_,box) <- entries, hpcPos <- [mkHpcPos span] ]
104 when (length entries' /= tickBoxCount st) $ do
105 panic "the number of .mix entries are inconsistent"
106 let hashNo = mixHash orig_file2 modTime tabStop entries'
107 mixCreate hpc_mod_dir mod_name
108 $ Mix orig_file2 modTime (toHash hashNo) tabStop entries'
113 -- Todo: use proper src span type
114 breakArray <- newBreakArray $ length entries
116 let locsTicks = listArray (0,tickBoxCount st-1)
117 [ span | (span,_,_) <- entries ]
118 varsTicks = listArray (0,tickBoxCount st-1)
119 [ vars | (_,vars,_) <- entries ]
120 modBreaks = emptyModBreaks
121 { modBreaks_flags = breakArray
122 , modBreaks_locs = locsTicks
123 , modBreaks_vars = varsTicks
126 doIfSet_dyn dflags Opt_D_dump_hpc $ do
127 printDump (pprLHsBinds binds1)
129 return (binds1, HpcInfo (tickBoxCount st) hashNo, modBreaks)
134 liftL :: (Monad m) => (a -> m a) -> Located a -> m (Located a)
135 liftL f (L loc a) = do
139 addTickLHsBinds :: LHsBinds Id -> TM (LHsBinds Id)
140 addTickLHsBinds binds = mapBagM addTickLHsBind binds
142 addTickLHsBind :: LHsBind Id -> TM (LHsBind Id)
143 addTickLHsBind (L pos (AbsBinds abs_tvs abs_dicts abs_exports abs_binds)) = do
144 abs_binds' <- addTickLHsBinds abs_binds
145 return $ L pos $ AbsBinds abs_tvs abs_dicts abs_exports abs_binds'
146 addTickLHsBind (L pos (funBind@(FunBind { fun_id = (L _ id) }))) = do
147 let name = getOccString id
148 decl_path <- getPathEntry
150 (fvs, (MatchGroup matches' ty)) <-
153 addTickMatchGroup (fun_matches funBind)
155 blackListed <- isBlackListed pos
157 -- Todo: we don't want redundant ticks on simple pattern bindings
158 -- We don't want to generate code for blacklisted positions
159 if blackListed || (not opt_Hpc && isSimplePatBind funBind)
161 return $ L pos $ funBind { fun_matches = MatchGroup matches' ty
165 tick_no <- allocATickBox (if null decl_path
166 then TopLevelBox [name]
167 else LocalBox (decl_path ++ [name]))
170 return $ L pos $ funBind { fun_matches = MatchGroup matches' ty
174 -- a binding is a simple pattern binding if it is a funbind with zero patterns
175 isSimplePatBind :: HsBind a -> Bool
176 isSimplePatBind funBind = matchGroupArity (fun_matches funBind) == 0
178 -- TODO: Revisit this
179 addTickLHsBind (L pos (pat@(PatBind { pat_rhs = rhs }))) = do
181 rhs' <- addPathEntry name $ addTickGRHSs False rhs
183 decl_path <- getPathEntry
184 tick_me <- allocTickBox (if null decl_path
185 then TopLevelBox [name]
186 else LocalBox (name : decl_path))
188 return $ L pos $ pat { pat_rhs = rhs' }
190 -- Only internal stuff, not from source, uses VarBind, so we ignore it.
191 addTickLHsBind var_bind@(L _ (VarBind {})) = return var_bind
193 -- Add a tick to the expression no matter what it is. There is one exception:
194 -- for the debugger, if the expression is a 'let', then we don't want to add
195 -- a tick here because there will definititely be a tick on the body anyway.
196 addTickLHsExprAlways :: LHsExpr Id -> TM (LHsExpr Id)
197 addTickLHsExprAlways (L pos e0)
198 | not opt_Hpc, HsLet _ _ <- e0 = addTickLHsExprNever (L pos e0)
199 | otherwise = allocTickBox (ExpBox False) pos $ addTickHsExpr e0
201 addTickLHsExprNeverOrAlways :: LHsExpr Id -> TM (LHsExpr Id)
202 addTickLHsExprNeverOrAlways e
203 | opt_Hpc = addTickLHsExprNever e
204 | otherwise = addTickLHsExprAlways e
206 addTickLHsExprNeverOrMaybe :: LHsExpr Id -> TM (LHsExpr Id)
207 addTickLHsExprNeverOrMaybe e
208 | opt_Hpc = addTickLHsExprNever e
209 | otherwise = addTickLHsExpr e
211 -- version of addTick that does not actually add a tick,
212 -- because the scope of this tick is completely subsumed by
214 addTickLHsExprNever :: LHsExpr Id -> TM (LHsExpr Id)
215 addTickLHsExprNever (L pos e0) = do
216 e1 <- addTickHsExpr e0
219 -- selectively add ticks to interesting expressions
220 addTickLHsExpr :: LHsExpr Id -> TM (LHsExpr Id)
221 addTickLHsExpr (L pos e0) = do
222 if opt_Hpc || isGoodBreakExpr e0
224 allocTickBox (ExpBox False) pos $ addTickHsExpr e0
226 e1 <- addTickHsExpr e0
229 -- general heuristic: expressions which do not denote values are good break points
230 isGoodBreakExpr :: HsExpr Id -> Bool
231 isGoodBreakExpr (HsApp {}) = True
232 isGoodBreakExpr (OpApp {}) = True
233 isGoodBreakExpr (NegApp {}) = True
234 isGoodBreakExpr (HsCase {}) = True
235 isGoodBreakExpr (HsIf {}) = True
236 isGoodBreakExpr (RecordCon {}) = True
237 isGoodBreakExpr (RecordUpd {}) = True
238 isGoodBreakExpr (ArithSeq {}) = True
239 isGoodBreakExpr (PArrSeq {}) = True
240 isGoodBreakExpr _other = False
242 addTickLHsExprOptAlt :: Bool -> LHsExpr Id -> TM (LHsExpr Id)
243 addTickLHsExprOptAlt oneOfMany (L pos e0)
244 | not opt_Hpc = addTickLHsExpr (L pos e0)
246 allocTickBox (ExpBox oneOfMany) pos $
249 addBinTickLHsExpr :: (Bool -> BoxLabel) -> LHsExpr Id -> TM (LHsExpr Id)
250 addBinTickLHsExpr boxLabel (L pos e0) = do
251 e1 <- addTickHsExpr e0
252 allocBinTickBox boxLabel $ L pos e1
254 addTickHsExpr :: HsExpr Id -> TM (HsExpr Id)
255 addTickHsExpr e@(HsVar id) = do freeVar id; return e
256 addTickHsExpr e@(HsIPVar _) = return e
257 addTickHsExpr e@(HsOverLit _) = return e
258 addTickHsExpr e@(HsLit _) = return e
259 addTickHsExpr (HsLam matchgroup) =
260 liftM HsLam (addTickMatchGroup matchgroup)
261 addTickHsExpr (HsApp e1 e2) =
262 liftM2 HsApp (addTickLHsExprNever e1) (addTickLHsExpr e2)
263 addTickHsExpr (OpApp e1 e2 fix e3) =
266 (addTickLHsExprNever e2)
269 addTickHsExpr (NegApp e neg) =
272 (addTickSyntaxExpr hpcSrcSpan neg)
273 addTickHsExpr (HsPar e) = liftM HsPar (addTickLHsExprNeverOrMaybe e)
274 addTickHsExpr (SectionL e1 e2) =
278 addTickHsExpr (SectionR e1 e2) =
282 addTickHsExpr (HsCase e mgs) =
285 (addTickMatchGroup mgs)
286 addTickHsExpr (HsIf e1 e2 e3) =
288 (addBinTickLHsExpr (BinBox CondBinBox) e1)
289 (addTickLHsExprOptAlt True e2)
290 (addTickLHsExprOptAlt True e3)
291 addTickHsExpr (HsLet binds e) =
292 bindLocals (map unLoc $ collectLocalBinders binds) $
294 (addTickHsLocalBinds binds) -- to think about: !patterns.
295 (addTickLHsExprNeverOrAlways e)
296 addTickHsExpr (HsDo cxt stmts last_exp srcloc) = do
297 (stmts', last_exp') <- addTickLStmts' forQual stmts
298 (addTickLHsExpr last_exp)
299 return (HsDo cxt stmts' last_exp' srcloc)
301 forQual = case cxt of
302 ListComp -> Just $ BinBox QualBinBox
304 addTickHsExpr (ExplicitList ty es) =
307 (mapM (addTickLHsExpr) es)
308 addTickHsExpr (ExplicitPArr ty es) =
311 (mapM (addTickLHsExpr) es)
312 addTickHsExpr (ExplicitTuple es box) =
314 (mapM (addTickLHsExpr) es)
316 addTickHsExpr (RecordCon id ty rec_binds) =
320 (addTickHsRecordBinds rec_binds)
321 addTickHsExpr (RecordUpd e rec_binds cons tys1 tys2) =
324 (addTickHsRecordBinds rec_binds)
325 (return cons) (return tys1) (return tys2)
327 addTickHsExpr (ExprWithTySigOut e ty) =
328 liftM2 ExprWithTySigOut
329 (addTickLHsExprNever e) -- No need to tick the inner expression
330 -- for expressions with signatures
332 addTickHsExpr (ArithSeq ty arith_seq) =
335 (addTickArithSeqInfo arith_seq)
336 addTickHsExpr (HsTickPragma _ (L pos e0)) = do
337 e2 <- allocTickBox (ExpBox False) pos $
340 addTickHsExpr (PArrSeq ty arith_seq) =
343 (addTickArithSeqInfo arith_seq)
344 addTickHsExpr (HsSCC nm e) =
348 addTickHsExpr (HsCoreAnn nm e) =
352 addTickHsExpr e@(HsBracket {}) = return e
353 addTickHsExpr e@(HsBracketOut {}) = return e
354 addTickHsExpr e@(HsSpliceE {}) = return e
355 addTickHsExpr (HsProc pat cmdtop) =
358 (liftL (addTickHsCmdTop) cmdtop)
359 addTickHsExpr (HsWrap w e) =
362 (addTickHsExpr e) -- explicitly no tick on inside
363 addTickHsExpr (HsArrApp e1 e2 ty1 arr_ty lr) =
370 addTickHsExpr (HsArrForm e fix cmdtop) =
374 (mapM (liftL (addTickHsCmdTop)) cmdtop)
376 addTickHsExpr e@(HsType _) = return e
378 -- Others dhould never happen in expression content.
379 addTickHsExpr e = pprPanic "addTickHsExpr" (ppr e)
381 addTickMatchGroup :: MatchGroup Id -> TM (MatchGroup Id)
382 addTickMatchGroup (MatchGroup matches ty) = do
383 let isOneOfMany = matchesOneOfMany matches
384 matches' <- mapM (liftL (addTickMatch isOneOfMany)) matches
385 return $ MatchGroup matches' ty
387 addTickMatch :: Bool -> Match Id -> TM (Match Id)
388 addTickMatch isOneOfMany (Match pats opSig gRHSs) =
389 bindLocals (collectPatsBinders pats) $ do
390 gRHSs' <- addTickGRHSs isOneOfMany gRHSs
391 return $ Match pats opSig gRHSs'
393 addTickGRHSs :: Bool -> GRHSs Id -> TM (GRHSs Id)
394 addTickGRHSs isOneOfMany (GRHSs guarded local_binds) = do
395 bindLocals binders $ do
396 local_binds' <- addTickHsLocalBinds local_binds
397 guarded' <- mapM (liftL (addTickGRHS isOneOfMany)) guarded
398 return $ GRHSs guarded' local_binds'
400 binders = map unLoc (collectLocalBinders local_binds)
402 addTickGRHS :: Bool -> GRHS Id -> TM (GRHS Id)
403 addTickGRHS isOneOfMany (GRHS stmts expr) = do
404 (stmts',expr') <- addTickLStmts' (Just $ BinBox $ GuardBinBox) stmts
405 (if opt_Hpc then addTickLHsExprOptAlt isOneOfMany expr
406 else addTickLHsExprAlways expr)
407 return $ GRHS stmts' expr'
409 addTickLStmts :: (Maybe (Bool -> BoxLabel)) -> [LStmt Id] -> TM [LStmt Id]
410 addTickLStmts isGuard stmts = do
411 (stmts, _) <- addTickLStmts' isGuard stmts (return ())
414 addTickLStmts' :: (Maybe (Bool -> BoxLabel)) -> [LStmt Id] -> TM a
415 -> TM ([LStmt Id], a)
416 addTickLStmts' isGuard lstmts res
417 = bindLocals binders $ do
418 lstmts' <- mapM (liftL (addTickStmt isGuard)) lstmts
422 binders = map unLoc (collectLStmtsBinders lstmts)
424 addTickStmt :: (Maybe (Bool -> BoxLabel)) -> Stmt Id -> TM (Stmt Id)
425 addTickStmt _isGuard (BindStmt pat e bind fail) = do
428 (addTickLHsExprAlways e)
429 (addTickSyntaxExpr hpcSrcSpan bind)
430 (addTickSyntaxExpr hpcSrcSpan fail)
431 addTickStmt isGuard (ExprStmt e bind' ty) = do
434 (addTickSyntaxExpr hpcSrcSpan bind')
436 addTickStmt _isGuard (LetStmt binds) = do
438 (addTickHsLocalBinds binds)
439 addTickStmt isGuard (ParStmt pairs) = do
441 (mapM (addTickStmtAndBinders isGuard) pairs)
442 addTickStmt isGuard (TransformStmt (stmts, ids) usingExpr maybeByExpr) = do
444 (addTickStmtAndBinders isGuard (stmts, ids))
445 (addTickLHsExprAlways usingExpr)
446 (addTickMaybeByLHsExpr maybeByExpr)
447 addTickStmt isGuard (GroupStmt (stmts, binderMap) groupByClause) = do
449 (addTickStmtAndBinders isGuard (stmts, binderMap))
450 (case groupByClause of
451 GroupByNothing usingExpr -> addTickLHsExprAlways usingExpr >>= (return . GroupByNothing)
452 GroupBySomething eitherUsingExpr byExpr -> do
453 eitherUsingExpr' <- mapEitherM addTickLHsExprAlways (addTickSyntaxExpr hpcSrcSpan) eitherUsingExpr
454 byExpr' <- addTickLHsExprAlways byExpr
455 return $ GroupBySomething eitherUsingExpr' byExpr')
457 mapEitherM f g x = do
459 Left a -> f a >>= (return . Left)
460 Right b -> g b >>= (return . Right)
461 addTickStmt isGuard (RecStmt stmts ids1 ids2 tys dictbinds) = do
463 (addTickLStmts isGuard stmts)
467 (addTickDictBinds dictbinds)
469 addTick :: Maybe (Bool -> BoxLabel) -> LHsExpr Id -> TM (LHsExpr Id)
470 addTick isGuard e | Just fn <- isGuard = addBinTickLHsExpr fn e
471 | otherwise = addTickLHsExprAlways e
473 addTickStmtAndBinders :: Maybe (Bool -> BoxLabel) -> ([LStmt Id], a)
474 -> TM ([LStmt Id], a)
475 addTickStmtAndBinders isGuard (stmts, ids) =
477 (addTickLStmts isGuard stmts)
480 addTickMaybeByLHsExpr :: Maybe (LHsExpr Id) -> TM (Maybe (LHsExpr Id))
481 addTickMaybeByLHsExpr maybeByExpr =
483 Nothing -> return Nothing
484 Just byExpr -> addTickLHsExprAlways byExpr >>= (return . Just)
486 addTickHsLocalBinds :: HsLocalBinds Id -> TM (HsLocalBinds Id)
487 addTickHsLocalBinds (HsValBinds binds) =
489 (addTickHsValBinds binds)
490 addTickHsLocalBinds (HsIPBinds binds) =
492 (addTickHsIPBinds binds)
493 addTickHsLocalBinds (EmptyLocalBinds) = return EmptyLocalBinds
495 addTickHsValBinds :: HsValBindsLR Id a -> TM (HsValBindsLR Id b)
496 addTickHsValBinds (ValBindsOut binds sigs) =
498 (mapM (\ (rec,binds') ->
501 (addTickLHsBinds binds'))
504 addTickHsValBinds _ = panic "addTickHsValBinds"
506 addTickHsIPBinds :: HsIPBinds Id -> TM (HsIPBinds Id)
507 addTickHsIPBinds (IPBinds ipbinds dictbinds) =
509 (mapM (liftL (addTickIPBind)) ipbinds)
510 (addTickDictBinds dictbinds)
512 addTickIPBind :: IPBind Id -> TM (IPBind Id)
513 addTickIPBind (IPBind nm e) =
518 -- There is no location here, so we might need to use a context location??
519 addTickSyntaxExpr :: SrcSpan -> SyntaxExpr Id -> TM (SyntaxExpr Id)
520 addTickSyntaxExpr pos x = do
521 L _ x' <- addTickLHsExpr (L pos x)
523 -- we do not walk into patterns.
524 addTickLPat :: LPat Id -> TM (LPat Id)
525 addTickLPat pat = return pat
527 addTickHsCmdTop :: HsCmdTop Id -> TM (HsCmdTop Id)
528 addTickHsCmdTop (HsCmdTop cmd tys ty syntaxtable) =
535 addTickLHsCmd :: LHsCmd Id -> TM (LHsCmd Id)
536 addTickLHsCmd x = addTickLHsExpr x
538 addTickDictBinds :: DictBinds Id -> TM (DictBinds Id)
539 addTickDictBinds x = addTickLHsBinds x
541 addTickHsRecordBinds :: HsRecordBinds Id -> TM (HsRecordBinds Id)
542 addTickHsRecordBinds (HsRecFields fields dd)
543 = do { fields' <- mapM process fields
544 ; return (HsRecFields fields' dd) }
546 process (HsRecField ids expr doc)
547 = do { expr' <- addTickLHsExpr expr
548 ; return (HsRecField ids expr' doc) }
550 addTickArithSeqInfo :: ArithSeqInfo Id -> TM (ArithSeqInfo Id)
551 addTickArithSeqInfo (From e1) =
554 addTickArithSeqInfo (FromThen e1 e2) =
558 addTickArithSeqInfo (FromTo e1 e2) =
562 addTickArithSeqInfo (FromThenTo e1 e2 e3) =
570 data TickTransState = TT { tickBoxCount:: Int
571 , mixEntries :: [MixEntry_]
574 data TickTransEnv = TTE { fileName :: FastString
575 , declPath :: [String]
577 , blackList :: FiniteMap SrcSpan ()
582 type FreeVars = OccEnv Id
587 -- For breakpoints we want to collect the free variables of an
588 -- expression for pinning on the HsTick. We don't want to collect
589 -- *all* free variables though: in particular there's no point pinning
590 -- on free variables that are will otherwise be in scope at the GHCi
591 -- prompt, which means all top-level bindings. Unfortunately detecting
592 -- top-level bindings isn't easy (collectHsBindsBinders on the top-level
593 -- bindings doesn't do it), so we keep track of a set of "in-scope"
594 -- variables in addition to the free variables, and the former is used
595 -- to filter additions to the latter. This gives us complete control
596 -- over what free variables we track.
598 data TM a = TM { unTM :: TickTransEnv -> TickTransState -> (a,FreeVars,TickTransState) }
599 -- a combination of a state monad (TickTransState) and a writer
602 instance Monad TM where
603 return a = TM $ \ _env st -> (a,noFVs,st)
604 (TM m) >>= k = TM $ \ env st ->
607 case unTM (k r1) env st1 of
609 (r2, fv1 `plusOccEnv` fv2, st2)
611 -- getState :: TM TickTransState
612 -- getState = TM $ \ env st -> (st, noFVs, st)
614 -- setState :: (TickTransState -> TickTransState) -> TM ()
615 -- setState f = TM $ \ env st -> ((), noFVs, f st)
617 getEnv :: TM TickTransEnv
618 getEnv = TM $ \ env st -> (env, noFVs, st)
620 withEnv :: (TickTransEnv -> TickTransEnv) -> TM a -> TM a
621 withEnv f (TM m) = TM $ \ env st ->
623 (a, fvs, st') -> (a, fvs, st')
625 getFreeVars :: TM a -> TM (FreeVars, a)
627 = TM $ \ env st -> case m env st of (a, fv, st') -> ((fv,a), fv, st')
629 freeVar :: Id -> TM ()
630 freeVar id = TM $ \ env st ->
631 if id `elemVarSet` inScope env
632 then ((), unitOccEnv (nameOccName (idName id)) id, st)
635 addPathEntry :: String -> TM a -> TM a
636 addPathEntry nm = withEnv (\ env -> env { declPath = declPath env ++ [nm] })
638 getPathEntry :: TM [String]
639 getPathEntry = declPath `liftM` getEnv
641 getFileName :: TM FastString
642 getFileName = fileName `liftM` getEnv
644 sameFileName :: SrcSpan -> TM a -> TM a -> TM a
645 sameFileName pos out_of_scope in_scope = do
646 file_name <- getFileName
647 case srcSpanFileName_maybe pos of
649 | file_name == file_name2 -> in_scope
652 bindLocals :: [Id] -> TM a -> TM a
653 bindLocals new_ids (TM m)
655 case m env{ inScope = inScope env `extendVarSetList` new_ids } st of
656 (r, fv, st') -> (r, fv `delListFromOccEnv` occs, st')
657 where occs = [ nameOccName (idName id) | id <- new_ids ]
659 isBlackListed :: SrcSpan -> TM Bool
660 isBlackListed pos = TM $ \ env st ->
661 case lookupFM (blackList env) pos of
662 Nothing -> (False,noFVs,st)
663 Just () -> (True,noFVs,st)
665 -- the tick application inherits the source position of its
666 -- expression argument to support nested box allocations
667 allocTickBox :: BoxLabel -> SrcSpan -> TM (HsExpr Id) -> TM (LHsExpr Id)
668 allocTickBox boxLabel pos m | isGoodSrcSpan' pos =
670 (do e <- m; return (L pos e)) $ do
671 (fvs, e) <- getFreeVars m
673 let c = tickBoxCount st
676 me = (pos, map (nameOccName.idName) ids, boxLabel)
678 ( L pos (HsTick c ids (L pos e))
680 , st {tickBoxCount=c+1,mixEntries=me:mes}
682 allocTickBox _boxLabel pos m = do e <- m; return (L pos e)
684 -- the tick application inherits the source position of its
685 -- expression argument to support nested box allocations
686 allocATickBox :: BoxLabel -> SrcSpan -> FreeVars -> TM (Maybe (Int,[Id]))
687 allocATickBox boxLabel pos fvs | isGoodSrcSpan' pos =
689 (return Nothing) $ TM $ \ _env st ->
690 let me = (pos, map (nameOccName.idName) ids, boxLabel)
696 , st {tickBoxCount=c+1, mixEntries=me:mes}
698 allocATickBox _boxLabel _pos _fvs = return Nothing
700 allocBinTickBox :: (Bool -> BoxLabel) -> LHsExpr Id -> TM (LHsExpr Id)
701 allocBinTickBox boxLabel (L pos e) | isGoodSrcSpan' pos = TM $ \ _env st ->
702 let meT = (pos,[],boxLabel True)
703 meF = (pos,[],boxLabel False)
704 meE = (pos,[],ExpBox False)
709 then ( L pos $ HsTick c [] $ L pos $ HsBinTick (c+1) (c+2) (L pos e)
710 -- notice that F and T are reversed,
711 -- because we are building the list in
714 , st {tickBoxCount=c+3 , mixEntries=meF:meT:meE:mes}
717 ( L pos $ HsTick c [] $ L pos e
719 , st {tickBoxCount=c+1,mixEntries=meE:mes}
722 allocBinTickBox _boxLabel e = return e
724 isGoodSrcSpan' :: SrcSpan -> Bool
726 | not (isGoodSrcSpan pos) = False
727 | start == end = False
730 start = srcSpanStart pos
733 mkHpcPos :: SrcSpan -> HpcPos
735 | not (isGoodSrcSpan' pos) = panic "bad source span; expected such spans to be filtered out"
738 start = srcSpanStart pos
740 hpcPos = toHpcPos ( srcLocLine start
741 , srcLocCol start + 1
746 hpcSrcSpan :: SrcSpan
747 hpcSrcSpan = mkGeneralSrcSpan (fsLit "Haskell Program Coverage internals")
752 matchesOneOfMany :: [LMatch Id] -> Bool
753 matchesOneOfMany lmatches = sum (map matchCount lmatches) > 1
755 matchCount (L _ (Match _pats _ty (GRHSs grhss _binds))) = length grhss
760 type MixEntry_ = (SrcSpan, [OccName], BoxLabel)
762 -- For the hash value, we hash everything: the file name,
763 -- the timestamp of the original source file, the tab stop,
764 -- and the mix entries. We cheat, and hash the show'd string.
765 -- This hash only has to be hashed at Mix creation time,
766 -- and is for sanity checking only.
768 mixHash :: FilePath -> Integer -> Int -> [MixEntry] -> Int
769 mixHash file tm tabstop entries = fromIntegral $ hashString
770 (show $ Mix file tm 0 tabstop entries)