[project @ 2001-10-25 02:13:10 by sof]
[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, Messages, errorsFound, warningsFound,
9
10         addShortErrLocLine, addShortWarnLocLine,
11         addErrLocHdrLine, addWarnLocHdrLine, dontAddErrLoc,
12
13         printErrorsAndWarnings, pprBagOfErrors, pprBagOfWarnings,
14
15         printError,
16         ghcExit,
17         doIfSet, doIfSet_dyn, 
18         dumpIfSet, dumpIfSet_core, dumpIfSet_dyn, dumpIfSet_dyn_or, 
19         showPass
20     ) where
21
22 #include "HsVersions.h"
23
24 import Bag              ( Bag, bagToList, isEmptyBag )
25 import SrcLoc           ( SrcLoc, noSrcLoc, isGoodSrcLoc )
26 import Util             ( sortLt )
27 import Outputable
28 import CmdLineOpts      ( DynFlags(..), DynFlag(..), dopt )
29
30 import List             ( replicate )
31 import System           ( ExitCode(..), exitWith )
32 import IO               ( hPutStr, hPutStrLn, stderr )
33 \end{code}
34
35 \begin{code}
36 type MsgWithLoc = (SrcLoc, SDoc)
37
38 type ErrMsg  = MsgWithLoc
39 type WarnMsg = MsgWithLoc
40 type Message = SDoc
41
42 addShortErrLocLine  :: SrcLoc -> Message -> ErrMsg
43 addErrLocHdrLine    :: SrcLoc -> Message -> Message -> ErrMsg
44 addWarnLocHdrLine    :: SrcLoc -> Message -> Message -> ErrMsg
45 addShortWarnLocLine :: SrcLoc -> Message -> WarnMsg
46
47 addShortErrLocLine locn rest_of_err_msg
48   | isGoodSrcLoc locn = (locn, hang (ppr locn <> colon) 4 
49                                     rest_of_err_msg)
50   | otherwise         = (locn, rest_of_err_msg)
51
52 addErrLocHdrLine locn hdr rest_of_err_msg
53   = ( locn
54     , hang (ppr locn <> colon<+> hdr) 
55          4 rest_of_err_msg
56     )
57
58 addWarnLocHdrLine locn hdr rest_of_err_msg
59   = ( locn
60     , hang (ppr locn <> colon <+> ptext SLIT("Warning:") <+> hdr) 
61          4 (rest_of_err_msg)
62     )
63
64 addShortWarnLocLine locn rest_of_err_msg
65   | isGoodSrcLoc locn = (locn, hang (ppr locn <> colon) 4 
66                                     (ptext SLIT("Warning:") <+> rest_of_err_msg))
67   | otherwise         = (locn, rest_of_err_msg)
68
69 dontAddErrLoc :: Message -> ErrMsg
70 dontAddErrLoc msg = (noSrcLoc, msg)
71
72 \end{code}
73
74 \begin{code}
75 printError :: String -> IO ()
76 printError str = hPutStrLn stderr str
77 \end{code}
78
79 \begin{code}
80 type Messages = (Bag WarnMsg, Bag ErrMsg)
81
82 errorsFound :: Messages -> Bool
83 errorsFound (warns, errs) = not (isEmptyBag errs)
84
85 warningsFound :: Messages -> Bool
86 warningsFound (warns, errs) = not (isEmptyBag warns)
87
88 printErrorsAndWarnings :: PrintUnqualified -> Messages -> IO ()
89         -- Don't print any warnings if there are errors
90 printErrorsAndWarnings unqual (warns, errs)
91   | no_errs && no_warns  = return ()
92   | no_errs              = printErrs unqual (pprBagOfWarnings warns)
93   | otherwise            = printErrs unqual (pprBagOfErrors   errs)
94   where
95     no_warns = isEmptyBag warns
96     no_errs  = isEmptyBag errs
97
98 pprBagOfErrors :: Bag ErrMsg -> SDoc
99 pprBagOfErrors bag_of_errors
100   = vcat [text "" $$ p | (_,p) <- sorted_errs ]
101     where
102       bag_ls      = bagToList bag_of_errors
103       sorted_errs = sortLt occ'ed_before bag_ls
104
105       occ'ed_before (a,_) (b,_) = LT == compare a b
106
107 pprBagOfWarnings :: Bag WarnMsg -> SDoc
108 pprBagOfWarnings bag_of_warns = pprBagOfErrors bag_of_warns
109 \end{code}
110
111 \begin{code}
112 ghcExit :: Int -> IO ()
113 ghcExit val
114   | val == 0  = exitWith ExitSuccess
115   | otherwise = do hPutStr stderr "\nCompilation had errors\n\n"
116                    exitWith (ExitFailure val)
117 \end{code}
118
119 \begin{code}
120 doIfSet :: Bool -> IO () -> IO ()
121 doIfSet flag action | flag      = action
122                     | otherwise = return ()
123
124 doIfSet_dyn :: DynFlags -> DynFlag -> IO () -> IO()
125 doIfSet_dyn dflags flag action | dopt flag dflags = action
126                                | otherwise        = return ()
127 \end{code}
128
129 \begin{code}
130 showPass :: DynFlags -> String -> IO ()
131 showPass dflags what
132   | verbosity dflags >= 2 = hPutStr stderr ("*** "++what++":\n")
133   | otherwise             = return ()
134
135 dumpIfSet :: Bool -> String -> SDoc -> IO ()
136 dumpIfSet flag hdr doc
137   | not flag   = return ()
138   | otherwise  = printDump (dump hdr doc)
139
140 dumpIfSet_core :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
141 dumpIfSet_core dflags flag hdr doc
142   | dopt flag dflags
143         || verbosity dflags >= 4
144         || dopt Opt_D_verbose_core2core dflags  = printDump (dump hdr doc)
145   | otherwise                                   = return ()
146
147 dumpIfSet_dyn :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
148 dumpIfSet_dyn dflags flag hdr doc
149   | dopt flag dflags || verbosity dflags >= 4 = printDump (dump hdr doc)
150   | otherwise                                 = return ()
151
152 dumpIfSet_dyn_or :: DynFlags -> [DynFlag] -> String -> SDoc -> IO ()
153 dumpIfSet_dyn_or dflags flags hdr doc
154   | or [dopt flag dflags | flag <- flags]
155   || verbosity dflags >= 4 
156   = printDump (dump hdr doc)
157   | otherwise = return ()
158
159 dump hdr doc 
160    = vcat [text "", 
161            line <+> text hdr <+> line,
162            doc,
163            text ""]
164      where 
165         line = text (replicate 20 '=')
166 \end{code}