5 import System.Console.GetOpt
6 import Data.Maybe ( fromMaybe )
7 import qualified HpcSet as Set
14 { outputFile :: String
15 , includeMods :: Set.Set String
16 , excludeMods :: Set.Set String
26 , altHighlight :: Bool
28 , combineFun :: CombineFun -- tick-wise combine
29 , postFun :: PostFun --
30 , mergeModule :: MergeFun -- module-wise merge
35 , includeMods = Set.empty
36 , excludeMods = Set.empty
46 , altHighlight = False
50 , mergeModule = INTERSECTION
54 -- We do this after reading flags, because the defaults
55 -- depends on if specific flags we used.
57 default_final_flags flags = flags
58 { srcDirs = if null (srcDirs flags)
63 type FlagOptSeq = [OptDescr (Flags -> Flags)] -> [OptDescr (Flags -> Flags)]
65 noArg :: String -> String -> (Flags -> Flags) -> FlagOptSeq
66 noArg flag detail fn = (:) $ Option [] [flag] (NoArg $ fn) detail
68 anArg :: String -> String -> String -> (String -> Flags -> Flags) -> FlagOptSeq
69 anArg flag detail argtype fn = (:) $ Option [] [flag] (ReqArg fn argtype) detail
71 infoArg :: String -> FlagOptSeq
72 infoArg info = (:) $ Option [] [] (NoArg $ id) info
74 excludeOpt = anArg "exclude" "exclude MODULE and/or PACKAGE" "[PACKAGE:][MODULE]"
75 $ \ a f -> f { excludeMods = a `Set.insert` excludeMods f }
77 includeOpt = anArg "include" "include MODULE and/or PACKAGE" "[PACKAGE:][MODULE]"
78 $ \ a f -> f { includeMods = a `Set.insert` includeMods f }
80 hpcDirOpt = anArg "hpcdir" "sub-directory that contains .mix files" "DIR"
81 (\ a f -> f { hpcDir = a })
82 . infoArg "default .hpc [rarely used]"
84 srcDirOpt = anArg "srcdir" "path to source directory of .hs files" "DIR"
85 (\ a f -> f { srcDirs = srcDirs f ++ [a] })
86 . infoArg "multi-use of srcdir possible"
88 destDirOpt = anArg "destdir" "path to write output to" "DIR"
89 $ \ a f -> f { destDir = a }
92 outputOpt = anArg "output" "output FILE" "FILE" $ \ a f -> f { outputFile = a }
95 perModuleOpt = noArg "per-module" "show module level detail" $ \ f -> f { perModule = True }
96 decListOpt = noArg "decl-list" "show unused decls" $ \ f -> f { decList = True }
97 xmlOutputOpt = noArg "xml-output" "show output in XML" $ \ f -> f { xmlOutput = True }
98 funTotalsOpt = noArg "fun-entry-count" "show top-level function entry counts"
99 $ \ f -> f { funTotals = True }
101 = noArg "highlight-covered" "highlight covered code, rather that code gaps"
102 $ \ f -> f { altHighlight = True }
104 combineFunOpt = anArg "function"
105 "combine .tix files with join function, default = ADD" "FUNCTION"
106 $ \ a f -> case reads (map toUpper a) of
107 [(c,"")] -> f { combineFun = c }
108 _ -> error $ "no such combine function : " ++ a
109 combineFunOptInfo = infoArg
110 $ "FUNCTION = " ++ foldr1 (\ a b -> a ++ " | " ++ b) (map fst foldFuns)
112 mapFunOpt = anArg "function"
113 "apply function to .tix files, default = ID" "FUNCTION"
114 $ \ a f -> case reads (map toUpper a) of
115 [(c,"")] -> f { postFun = c }
116 _ -> error $ "no such combine function : " ++ a
117 mapFunOptInfo = infoArg
118 $ "FUNCTION = " ++ foldr1 (\ a b -> a ++ " | " ++ b) (map fst postFuns)
120 unionModuleOpt = noArg "union"
121 "use the union of the module namespace (default is intersection)"
122 $ \ f -> f { mergeModule = UNION }
125 -------------------------------------------------------------------------------
127 readMixWithFlags :: Flags -> Either String TixModule -> IO Mix
128 readMixWithFlags flags mod = readMix [ dir ++ "/" ++ hpcDir flags
129 | dir <- srcDirs flags
132 -------------------------------------------------------------------------------
134 command_usage plugin =
136 "Usage: hpc " ++ (name plugin) ++ " " ++
138 "\n" ++ summary plugin ++ "\n" ++
139 if null (options plugin [])
141 else usageInfo "\n\nOptions:\n" (options plugin [])
143 hpcError :: Plugin -> String -> IO a
144 hpcError plugin msg = do
145 putStrLn $ "Error: " ++ msg
149 -------------------------------------------------------------------------------
151 data Plugin = Plugin { name :: String
153 , options :: FlagOptSeq
155 , implementation :: Flags -> [String] -> IO ()
156 , init_flags :: Flags
157 , final_flags :: Flags -> Flags
160 ------------------------------------------------------------------------------
162 -- filterModules takes a list of candidate modules,
164 -- * excludes the excluded modules
165 -- * includes the rest if there are no explicity included modules
166 -- * otherwise, accepts just the included modules.
168 allowModule :: Flags -> String -> Bool
169 allowModule flags full_mod
170 | full_mod' `Set.member` excludeMods flags = False
171 | pkg_name `Set.member` excludeMods flags = False
172 | mod_name `Set.member` excludeMods flags = False
173 | Set.null (includeMods flags) = True
174 | full_mod' `Set.member` includeMods flags = True
175 | pkg_name `Set.member` includeMods flags = True
176 | mod_name `Set.member` includeMods flags = True
179 full_mod' = pkg_name ++ mod_name
180 -- pkg name always ends with '/', main
181 (pkg_name,mod_name) =
182 case span (/= '/') full_mod of
183 (p,'/':m) -> (p ++ ":",m)
185 _ -> error "impossible case in allowModule"
187 filterTix :: Flags -> Tix -> Tix
188 filterTix flags (Tix tixs) =
189 Tix $ filter (allowModule flags . tixModuleName) tixs
193 ------------------------------------------------------------------------------
194 -- HpcCombine specifics
196 data CombineFun = ADD | DIFF | SUB
197 deriving (Eq,Show, Read, Enum)
199 theCombineFun :: CombineFun -> Integer -> Integer -> Integer
200 theCombineFun fn = case fn of
201 ADD -> \ l r -> l + r
202 SUB -> \ l r -> max 0 (l - r)
203 DIFF -> \ g b -> if g > 0 then 0 else min 1 b
205 foldFuns :: [ (String,CombineFun) ]
206 foldFuns = [ (show comb,comb)
207 | comb <- [ADD .. SUB]
210 data PostFun = ID | INV | ZERO
211 deriving (Eq,Show, Read, Enum)
213 thePostFun :: PostFun -> Integer -> Integer
217 thePostFun ZERO x = 0
219 postFuns = [ (show pos,pos)
220 | pos <- [ID .. ZERO]
224 data MergeFun = INTERSECTION | UNION
225 deriving (Eq,Show, Read, Enum)
227 theMergeFun :: (Ord a) => MergeFun -> Set.Set a -> Set.Set a -> Set.Set a
228 theMergeFun INTERSECTION = Set.intersection
229 theMergeFun UNION = Set.union
231 mergeFuns = [ (show pos,pos)
232 | pos <- [INTERSECTION,UNION]