1 ---------------------------------------------------------
2 -- The main program for the hpc-markup tool, part of HPC.
3 -- Andy Gill and Colin Runciman, June 2006
4 ---------------------------------------------------------
6 module HpcMarkup (markup_plugin) where
15 import System.Environment
16 import System.Directory
18 import Data.Maybe(fromJust)
21 import qualified HpcSet as Set
23 ------------------------------------------------------------------------------
34 markup_plugin = Plugin { name = "markup"
35 , usage = "[OPTION] .. <TIX_FILE> [<MODULE> [<MODULE> ..]]"
36 , options = markup_options
37 , summary = "Markup Haskell source with program coverage"
38 , implementation = markup_main
39 , init_flags = default_flags
40 , final_flags = default_final_flags
43 ------------------------------------------------------------------------------
45 markup_main :: Flags -> [String] -> IO ()
46 markup_main flags (prog:modNames) = do
48 { includeMods = Set.fromList modNames
52 { funTotals = theFunTotals
53 , altHighlight = invertOutput
57 mtix <- readTix (getTixFileName prog)
58 Tix tixs <- case mtix of
59 Nothing -> hpcError markup_plugin $ "unable to find tix file for: " ++ prog
62 #if __GLASGOW_HASKELL__ >= 604
63 -- create the dest_dir if needed
64 createDirectoryIfMissing True dest_dir
68 sequence [ genHtmlFromMod dest_dir hpcflags1 tix theFunTotals invertOutput
70 , allowModule hpcflags1 (tixModuleName tix)
73 let index_name = "hpc_index"
74 index_fun = "hpc_index_fun"
75 index_alt = "hpc_index_alt"
76 index_exp = "hpc_index_exp"
78 let writeSummary name cmp = do
79 let mods' = sortBy cmp mods
84 putStrLn $ "Writing: " ++ (name ++ ".html")
85 writeFile (dest_dir ++ "/" ++ name ++ ".html") $
87 "<style type=\"text/css\">" ++
88 "table.bar { background-color: #f25913; }\n" ++
89 "td.bar { background-color: #60de51; }\n" ++
90 "td.invbar { background-color: #f25913; }\n" ++
91 "table.dashboard { border-collapse: collapse ; border: solid 1px black }\n" ++
92 ".dashboard td { border: solid 1px black }\n" ++
93 ".dashboard th { border: solid 1px black }\n" ++
95 "<table class=\"dashboard\" width=\"100%\" border=1>\n" ++
97 "<th rowspan=2><a href=\"" ++ index_name ++ ".html\">module</a></th>" ++
98 "<th colspan=3><a href=\"" ++ index_fun ++ ".html\">Top Level Definitions</a></th>" ++
99 "<th colspan=3><a href=\"" ++ index_alt ++ ".html\">Alternatives</a></th>" ++
100 "<th colspan=3><a href=\"" ++ index_exp ++ ".html\">Expressions</a></th>" ++
104 "<th colspan=2>covered / total</th>" ++
106 "<th colspan=2>covered / total</th>" ++
108 "<th colspan=2>covered / total</th>" ++
110 concat [ showModuleSummary (modName,fileName,summary)
111 | (modName,fileName,summary) <- mods'
114 showTotalSummary (mconcat
116 | (_,_,summary) <- mods'
118 ++ "</table></html>\n"
120 writeSummary index_name $ \ (n1,_,_) (n2,_,_) -> compare n1 n2
122 writeSummary index_fun $ \ (_,_,s1) (_,_,s2) ->
123 compare (percent (topFunTicked s2) (topFunTotal s2))
124 (percent (topFunTicked s1) (topFunTotal s1))
126 writeSummary index_alt $ \ (_,_,s1) (_,_,s2) ->
127 compare (percent (altTicked s2) (altTotal s2))
128 (percent (altTicked s1) (altTotal s1))
130 writeSummary index_exp $ \ (_,_,s1) (_,_,s2) ->
131 compare (percent (expTicked s2) (expTotal s2))
132 (percent (expTicked s1) (expTotal s1))
135 markup_main flags [] = hpcError markup_plugin $ "no .tix file or executable name specified"
143 -> IO (String, [Char], ModuleSummary)
144 genHtmlFromMod dest_dir flags tix theFunTotals invertOutput = do
145 let theHsPath = srcDirs flags
146 let modName0 = tixModuleName tix
148 (Mix origFile _ mixHash tabStop mix') <- readMixWithFlags flags (Right tix)
150 let arr_tix :: Array Int Integer
151 arr_tix = listArray (0,length (tixModuleTixs tix) - 1)
154 let tickedWith :: Int -> Integer
155 tickedWith n = arr_tix ! n
157 isTicked n = tickedWith n /= 0
159 let info = [ (pos,theMarkup)
160 | (gid,(pos,boxLabel)) <- zip [0 ..] mix'
161 , let binBox = case (isTicked gid,isTicked (gid+1)) of
163 (True,False) -> [TickedOnlyTrue]
164 (False,True) -> [TickedOnlyFalse]
166 , let tickBox = if isTicked gid
169 , theMarkup <- case boxLabel of
172 -> TopLevelDecl theFunTotals (tickedWith gid) : tickBox
173 LocalBox {} -> tickBox
174 BinBox _ True -> binBox
179 let summary = foldr (.) id
183 -> st { expTicked = ticked (expTicked st)
184 , expTotal = succ (expTotal st)
187 -> st { expTicked = ticked (expTicked st)
188 , expTotal = succ (expTotal st)
189 , altTicked = ticked (altTicked st)
190 , altTotal = succ (altTotal st)
193 st { topFunTicked = ticked (topFunTicked st)
194 , topFunTotal = succ (topFunTotal st)
197 | (gid,(_pos,boxLabel)) <- zip [0 ..] mix'
198 , let ticked = if isTicked gid
203 -- add prefix to modName argument
204 content <- readFileFromPath (hpcError markup_plugin) origFile theHsPath
206 let content' = markup tabStop info content
207 let show' = reverse . take 5 . (++ " ") . reverse . show
208 let addLine n xs = "<span class=\"lineno\">" ++ show' n ++ " </span>" ++ xs
209 let addLines = unlines . map (uncurry addLine) . zip [1 :: Int ..] . lines
210 let fileName = modName0 ++ ".hs.html"
211 putStrLn $ "Writing: " ++ fileName
212 writeFile (dest_dir ++ "/" ++ fileName) $
213 unlines [ "<html><style type=\"text/css\">",
214 "span.lineno { color: white; background: #aaaaaa; border-right: solid white 12px }",
216 then "span.nottickedoff { color: #404040; background: white; font-style: oblique }"
217 else "span.nottickedoff { background: " ++ yellow ++ "}",
219 then "span.istickedoff { color: black; background: #d0c0ff; font-style: normal; }"
220 else "span.istickedoff { background: white }",
221 "span.tickonlyfalse { margin: -1px; border: 1px solid " ++ red ++ "; background: " ++ red ++ " }",
222 "span.tickonlytrue { margin: -1px; border: 1px solid " ++ green ++ "; background: " ++ green ++ " }",
223 "span.funcount { font-size: small; color: orange; z-index: 2; position: absolute; right: 20 }",
225 then "span.decl { font-weight: bold; background: #d0c0ff }"
226 else "span.decl { font-weight: bold }",
227 "span.spaces { background: white }",
229 "<pre>"] ++ addLines content' ++ "\n</pre>\n</html>\n";
231 summary `seq` return (modName0,fileName,summary)
233 data Loc = Loc !Int !Int
234 deriving (Eq,Ord,Show)
242 Bool -- display entry totals
246 markup :: Int -- ^tabStop
247 -> [(HpcPos,Markup)] -- random list of tick location pairs
248 -> String -- text to mark up
250 markup tabStop mix str = addMarkup tabStop str (Loc 1 1) [] sortedTickLocs
252 tickLocs = [ (Loc ln1 c1,Loc ln2 c2,mark)
254 , let (ln1,c1,ln2,c2) = fromHpcPos pos
256 sortedTickLocs = sortBy (\(locA1,locZ1,_) (locA2,locZ2,_) ->
257 (locA1,locZ2) `compare` (locA2,locZ1)) tickLocs
259 addMarkup :: Int -- tabStop
260 -> String -- text to mark up
261 -> Loc -- current location
262 -> [(Loc,Markup)] -- stack of open ticks, with closing location
263 -> [(Loc,Loc,Markup)] -- sorted list of tick location pairs
266 -- check the pre-condition.
267 --addMarkup tabStop cs loc os ticks
268 -- | not (isSorted (map fst os)) = error $ "addMarkup: bad closing ordering: " ++ show os
270 --addMarkup tabStop cs loc os@(_:_) ticks
271 -- | trace (show (loc,os,take 10 ticks)) False = undefined
273 -- close all open ticks, if we have reached the end
274 addMarkup _ [] _loc os [] =
275 concatMap (const closeTick) os
276 addMarkup tabStop cs loc ((o,_):os) ticks | loc > o =
277 closeTick ++ addMarkup tabStop cs loc os ticks
279 --addMarkup tabStop cs loc os ((t1,t2,tik@(TopLevelDecl {})):ticks) | loc == t1 =
280 -- openTick tik ++ closeTick ++ addMarkup tabStop cs loc os ticks
282 addMarkup tabStop cs loc os ((t1,t2,tik0):ticks) | loc == t1 =
285 | not (allowNesting tik0 tik')
286 -> addMarkup tabStop cs loc os ticks -- already marked or bool within marked bool
287 _ -> openTick tik0 ++ addMarkup tabStop cs loc (addTo (t2,tik0) os) ticks
290 addTo (t,tik) [] = [(t,tik)]
291 addTo (t,tik) ((t',tik'):xs) | t <= t' = (t,tik):(t',tik'):xs
292 | t > t' = (t',tik):(t',tik'):xs
294 addMarkup tabStop0 cs loc os ((t1,_t2,_tik):ticks) | loc > t1 =
295 -- throw away this tick, because it is from a previous place ??
296 addMarkup tabStop0 cs loc os ticks
298 addMarkup tabStop0 ('\n':cs) loc@(Loc ln col) os@((Loc ln2 col2,_):_) ticks
299 | ln == ln2 && col < col2
300 = addMarkup tabStop0 (' ':'\n':cs) loc os ticks
301 addMarkup tabStop0 (c0:cs) loc@(Loc _ p) os ticks =
302 if c0=='\n' && os/=[] then
303 concatMap (const closeTick) (downToTopLevel os) ++
304 c0 : "<span class=\"spaces\">" ++ expand 1 w ++ "</span>" ++
305 concatMap (openTick.snd) (reverse (downToTopLevel os)) ++
306 addMarkup tabStop0 cs' loc' os ticks
307 else if c0=='\t' then
308 expand p "\t" ++ addMarkup tabStop0 cs (incBy c0 loc) os ticks
310 escape c0 ++ addMarkup tabStop0 cs (incBy c0 loc) os ticks
312 (w,cs') = span (`elem` " \t") cs
313 loc' = foldl (flip incBy) loc (c0:w)
316 escape '"' = """
320 expand :: Int -> String -> String
322 expand c ('\t':s) = replicate (c' - c) ' ' ++ expand c' s
324 c' = tabStopAfter 8 c
325 expand c (' ':s) = ' ' : expand (c+1) s
326 expand _ _ = error "bad character in string for expansion"
328 incBy :: Char -> Loc -> Loc
329 incBy '\n' (Loc ln _c) = Loc (succ ln) 1
330 incBy '\t' (Loc ln c) = Loc ln (tabStopAfter tabStop0 c)
331 incBy _ (Loc ln c) = Loc ln (succ c)
333 tabStopAfter :: Int -> Int -> Int
334 tabStopAfter tabStop c = fromJust (find (>c) [1,(tabStop + 1)..])
337 addMarkup tabStop cs loc os ticks = "ERROR: " ++ show (take 10 cs,tabStop,loc,take 10 os,take 10 ticks)
339 openTick :: Markup -> String
340 openTick NotTicked = "<span class=\"nottickedoff\">"
341 openTick IsTicked = "<span class=\"istickedoff\">"
342 openTick TickedOnlyTrue = "<span class=\"tickonlytrue\">"
343 openTick TickedOnlyFalse = "<span class=\"tickonlyfalse\">"
344 openTick (TopLevelDecl False _) = openTopDecl
345 openTick (TopLevelDecl True 0)
346 = "<span class=\"funcount\">-- never entered</span>" ++
348 openTick (TopLevelDecl True 1)
349 = "<span class=\"funcount\">-- entered once</span>" ++
351 openTick (TopLevelDecl True n0)
352 = "<span class=\"funcount\">-- entered " ++ showBigNum n0 ++ " times</span>" ++ openTopDecl
353 where showBigNum n | n <= 9999 = show n
354 | otherwise = showBigNum' (n `div` 1000) ++ "," ++ showWith (n `mod` 1000)
355 showBigNum' n | n <= 999 = show n
356 | otherwise = showBigNum' (n `div` 1000) ++ "," ++ showWith (n `mod` 1000)
357 showWith n = take 3 $ reverse $ ("000" ++) $ reverse $ show n
360 closeTick = "</span>"
362 openTopDecl :: String
363 openTopDecl = "<span class=\"decl\">"
365 downToTopLevel :: [(Loc,Markup)] -> [(Loc,Markup)]
366 downToTopLevel ((_,TopLevelDecl {}):_) = []
367 downToTopLevel (o : os) = o : downToTopLevel os
368 downToTopLevel [] = []
371 -- build in logic for nesting bin boxes
373 allowNesting :: Markup -- innermost
374 -> Markup -- outermost
376 allowNesting n m | n == m = False -- no need to double nest
377 allowNesting IsTicked TickedOnlyFalse = False
378 allowNesting IsTicked TickedOnlyTrue = False
379 allowNesting _ _ = True
381 ------------------------------------------------------------------------------
383 data ModuleSummary = ModuleSummary
386 , topFunTicked :: !Int
387 , topFunTotal :: !Int
394 showModuleSummary :: (String, String, ModuleSummary) -> String
395 showModuleSummary (modName,fileName,summary) =
397 "<td> <tt>module <a href=\"" ++ fileName ++ "\">"
398 ++ modName ++ "</a></tt></td>\n" ++
399 showSummary (topFunTicked summary) (topFunTotal summary) ++
400 showSummary (altTicked summary) (altTotal summary) ++
401 showSummary (expTicked summary) (expTotal summary) ++
404 showTotalSummary :: ModuleSummary -> String
405 showTotalSummary summary =
406 "<tr style=\"background: #e0e0e0\">\n" ++
407 "<th align=left> Program Coverage Total</tt></th>\n" ++
408 showSummary (topFunTicked summary) (topFunTotal summary) ++
409 showSummary (altTicked summary) (altTotal summary) ++
410 showSummary (expTicked summary) (expTotal summary) ++
413 showSummary :: (Integral t) => t -> t -> String
414 showSummary ticked total =
415 "<td align=\"right\">" ++ showP (percent ticked total) ++ "</td>" ++
416 "<td>" ++ show ticked ++ "/" ++ show total ++ "</td>" ++
418 (case percent ticked total of
420 Just w -> bar w "bar"
423 showP Nothing = "- "
424 showP (Just x) = show x ++ "%"
425 bar 0 inner = bar 100 "invbar"
426 bar w inner = "<table cellpadding=0 cellspacing=0 width=\"100\" class=\"bar\">" ++
427 "<tr><td><table cellpadding=0 cellspacing=0 width=\"" ++ show w ++ "%\">" ++
428 "<tr><td height=12 class=" ++ show inner ++ "></td></tr>" ++
429 "</table></td></tr></table>"
431 percent :: (Integral a) => a -> a -> Maybe a
432 percent ticked total = if total == 0 then Nothing else Just (ticked * 100 `div` total)
435 instance Monoid ModuleSummary where
436 mempty = ModuleSummary
444 mappend (ModuleSummary eTik1 eTot1 tTik1 tTot1 aTik1 aTot1)
445 (ModuleSummary eTik2 eTot2 tTik2 tTot2 aTik2 aTot2)
446 = ModuleSummary (eTik1 + eTik2) (eTot1 + eTot2) (tTik1 + tTik2) (tTot1 + tTot2) (aTik1 + aTik2) (aTot1 + aTot2)
449 ------------------------------------------------------------------------------
450 -- global color pallete
452 red,green,yellow :: String