[project @ 2000-08-09 15:19:58 by rrt]
[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         addErrLocHdrLine,
11         dontAddErrLoc,
12         printErrorsAndWarnings, pprBagOfErrors, pprBagOfWarnings,
13         ghcExit,
14         doIfSet, dumpIfSet
15     ) where
16
17 #include "HsVersions.h"
18
19 import Bag              ( Bag, bagToList, isEmptyBag )
20 import SrcLoc           ( SrcLoc, noSrcLoc )
21 import Util             ( sortLt )
22 import Outputable
23 import IO               ( hPutStr, stderr )
24 \end{code}
25
26 \begin{code}
27 type MsgWithLoc = (SrcLoc, SDoc)
28
29 type ErrMsg  = MsgWithLoc
30 type WarnMsg = MsgWithLoc
31 type Message = SDoc
32
33 addShortErrLocLine  :: SrcLoc -> Message -> ErrMsg
34 addErrLocHdrLine    :: SrcLoc -> Message -> Message -> ErrMsg
35 addShortWarnLocLine :: SrcLoc -> Message -> WarnMsg
36
37 addShortErrLocLine locn rest_of_err_msg
38   = ( locn
39     , hang (ppr locn <> colon) 
40          4 rest_of_err_msg
41     )
42
43 addErrLocHdrLine locn hdr rest_of_err_msg
44   = ( locn
45     , hang (ppr locn <> colon<+> hdr) 
46          4 rest_of_err_msg
47     )
48
49 addShortWarnLocLine locn rest_of_err_msg
50   = ( locn
51     , hang (ppr locn <> colon)
52          4 (ptext SLIT("Warning:") <+> rest_of_err_msg)
53     )
54
55 dontAddErrLoc :: String -> Message -> ErrMsg
56 dontAddErrLoc title rest_of_err_msg
57  | null title = (noSrcLoc, rest_of_err_msg)
58  | otherwise  =
59     ( noSrcLoc, hang (text title <> colon) 4 rest_of_err_msg )
60
61 printErrorsAndWarnings :: Bag ErrMsg -> Bag WarnMsg -> IO ()
62         -- Don't print any warnings if there are errors
63 printErrorsAndWarnings errs warns
64   | no_errs && no_warns  = return ()
65   | no_errs              = printErrs (pprBagOfWarnings warns)
66   | otherwise            = printErrs (pprBagOfErrors   errs)
67   where
68     no_warns = isEmptyBag warns
69     no_errs  = isEmptyBag errs
70
71 pprBagOfErrors :: Bag ErrMsg -> SDoc
72 pprBagOfErrors bag_of_errors
73   = vcat [text "" $$ p | (_,p) <- sorted_errs ]
74     where
75       bag_ls      = bagToList bag_of_errors
76       sorted_errs = sortLt occ'ed_before bag_ls
77
78       occ'ed_before (a,_) (b,_) = LT == compare a b
79
80 pprBagOfWarnings :: Bag WarnMsg -> SDoc
81 pprBagOfWarnings bag_of_warns = pprBagOfErrors bag_of_warns
82 \end{code}
83
84 \begin{code}
85 ghcExit :: Int -> IO ()
86
87 ghcExit val
88   = if val /= 0
89     then hPutStr stderr "Compilation had errors\n"
90     else return ()
91 \end{code}
92
93 \begin{code}
94 doIfSet :: Bool -> IO () -> IO ()
95 doIfSet flag action | flag      = action
96                     | otherwise = return ()
97 \end{code}
98
99 \begin{code}
100 dumpIfSet :: Bool -> String -> SDoc -> IO ()
101 dumpIfSet flag hdr doc
102   | not flag  = return ()
103   | otherwise = printDump dump
104   where
105     dump = vcat [text "", 
106                  line <+> text hdr <+> line,
107                  doc,
108                  text ""]
109     line = text (take 20 (repeat '='))
110 \end{code}