[project @ 1998-12-02 13:17:09 by simonm]
[ghc-hetmet.git] / ghc / compiler / main / ErrUtils.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1994-1998
3 %
4 \section[ErrsUtils]{Utilities for error reporting}
5
6 \begin{code}
7 module ErrUtils (
8         ErrMsg, WarnMsg, Message,
9         addShortErrLocLine, addShortWarnLocLine,
10         dontAddErrLoc,
11         pprBagOfErrors, pprBagOfWarnings,
12         ghcExit,
13         doIfSet, dumpIfSet
14     ) where
15
16 #include "HsVersions.h"
17
18 import Bag              ( Bag, bagToList )
19 import SrcLoc           ( SrcLoc )
20 import Outputable
21 \end{code}
22
23 \begin{code}
24 type ErrMsg   = SDoc
25 type WarnMsg = SDoc
26 type Message = SDoc
27
28 addShortErrLocLine, addShortWarnLocLine :: SrcLoc -> ErrMsg -> ErrMsg
29
30 addShortErrLocLine locn rest_of_err_msg
31   = hang (ppr locn <> colon)
32          4 rest_of_err_msg
33
34 addShortWarnLocLine locn rest_of_err_msg
35   = hang (ppr locn <> ptext SLIT(": Warning:"))
36          4 rest_of_err_msg
37
38 dontAddErrLoc :: String -> ErrMsg -> ErrMsg
39 dontAddErrLoc title rest_of_err_msg
40   = hang (hcat [text title, char ':'])
41          4 rest_of_err_msg
42
43 pprBagOfErrors :: Bag ErrMsg -> SDoc
44 pprBagOfErrors bag_of_errors
45   = vcat [space $$ p | p <- bagToList bag_of_errors]
46
47 pprBagOfWarnings :: Bag ErrMsg -> SDoc
48 pprBagOfWarnings bag_of_warns = pprBagOfErrors bag_of_warns
49 \end{code}
50
51 \begin{code}
52 ghcExit :: Int -> IO ()
53
54 ghcExit val
55   = if val /= 0
56     then error "Compilation had errors\n"
57     else return ()
58 \end{code}
59
60 \begin{code}
61 doIfSet :: Bool -> IO () -> IO ()
62 doIfSet flag action | flag      = action
63                     | otherwise = return ()
64 \end{code}
65
66 \begin{code}
67 dumpIfSet :: Bool -> String -> SDoc -> IO ()
68 dumpIfSet flag hdr doc
69   | not flag  = return ()
70   | otherwise = printDump dump
71   where
72     dump = vcat [text "", 
73                  line <+> text hdr <+> line,
74                  doc,
75                  text ""]
76     line = text (take 20 (repeat '='))
77 \end{code}