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