ed8be63675fd79932cd1d31e0cb74fe4a81efc7e
[ghc.git] / utils / hpc / HpcUtils.hs
1 module HpcUtils where
2
3 import Trace.Hpc.Util
4 import qualified HpcMap as Map
5 import HpcFlags
6
7 -- turns \n into ' '
8 -- | grab's the text behind a HpcPos;
9 grabHpcPos :: Map.Map Int String -> HpcPos -> String
10 grabHpcPos hsMap span =
11 case lns of
12 [ln] -> (take ((c2 - c1) + 1) $ drop (c1 - 1) ln)
13 _ -> let lns1 = drop (c1 -1) (head lns) : tail lns
14 lns2 = init lns1 ++ [take (c2 + 1) (last lns1) ]
15 in foldl1 (\ xs ys -> xs ++ "\n" ++ ys) lns2
16 where (l1,c1,l2,c2) = fromHpcPos span
17 lns = map (\ n -> case Map.lookup n hsMap of
18 Just ln -> ln
19 Nothing -> error $ "bad line number : " ++ show n
20 ) [l1..l2]
21
22
23 readFileFromPath :: (String -> IO String) -> String -> [String] -> IO String
24 readFileFromPath err filename@('/':_) _ = readFile filename
25 readFileFromPath err filename path0 = readTheFile path0
26 where
27 readTheFile [] = err $ "could not find " ++ show filename
28 ++ " in path " ++ show path0
29 readTheFile (dir:dirs) =
30 catch (do str <- readFile (dir ++ "/" ++ filename)
31 return str)
32 (\ _ -> readTheFile dirs)