9e43b3fba34b5feb2422e0bc0f608a35da3fe51e
[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         Message, mkLocMessage, printError,
9
10         ErrMsg, WarnMsg,
11         errMsgSpans, errMsgContext, errMsgShortDoc, errMsgExtraInfo,
12         Messages, errorsFound, emptyMessages,
13         mkErrMsg, mkWarnMsg, mkPlainErrMsg, mkLongErrMsg,
14         printErrorsAndWarnings, pprBagOfErrors, pprBagOfWarnings,
15
16         ghcExit,
17         doIfSet, doIfSet_dyn, 
18         dumpIfSet, dumpIfSet_core, dumpIfSet_dyn, dumpIfSet_dyn_or, mkDumpDoc,
19         showPass,
20
21         --  * Messages during compilation
22         setMsgHandler,
23         putMsg,
24         compilationProgressMsg,
25         debugTraceMsg,
26         errorMsg,
27     ) where
28
29 #include "HsVersions.h"
30
31 import Bag              ( Bag, bagToList, isEmptyBag, emptyBag )
32 import SrcLoc           ( SrcSpan )
33 import Util             ( sortLe, global )
34 import Outputable
35 import qualified Pretty
36 import SrcLoc           ( srcSpanStart )
37 import DynFlags         ( DynFlags(..), DynFlag(..), dopt )
38 import StaticFlags      ( opt_ErrorSpans )
39 import System           ( ExitCode(..), exitWith )
40 import DATA_IOREF
41 import IO               ( hPutStrLn, stderr )
42
43
44 -- -----------------------------------------------------------------------------
45 -- Basic error messages: just render a message with a source location.
46
47 type Message = SDoc
48
49 mkLocMessage :: SrcSpan -> Message -> Message
50 mkLocMessage locn msg
51   | opt_ErrorSpans = hang (ppr locn <> colon) 4 msg
52   | otherwise      = hang (ppr (srcSpanStart locn) <> colon) 4 msg
53   -- always print the location, even if it is unhelpful.  Error messages
54   -- are supposed to be in a standard format, and one without a location
55   -- would look strange.  Better to say explicitly "<no location info>".
56
57 printError :: SrcSpan -> Message -> IO ()
58 printError span msg = printErrs (mkLocMessage span msg $ defaultErrStyle)
59
60
61 -- -----------------------------------------------------------------------------
62 -- Collecting up messages for later ordering and printing.
63
64 data ErrMsg = ErrMsg { 
65         errMsgSpans     :: [SrcSpan],
66         errMsgContext   :: PrintUnqualified,
67         errMsgShortDoc  :: Message,
68         errMsgExtraInfo :: Message
69         }
70         -- The SrcSpan is used for sorting errors into line-number order
71         -- NB  Pretty.Doc not SDoc: we deal with the printing style (in ptic 
72         -- whether to qualify an External Name) at the error occurrence
73
74 type WarnMsg = ErrMsg
75
76 -- A short (one-line) error message, with context to tell us whether
77 -- to qualify names in the message or not.
78 mkErrMsg :: SrcSpan -> PrintUnqualified -> Message -> ErrMsg
79 mkErrMsg locn print_unqual msg
80   = ErrMsg [locn] print_unqual msg empty
81
82 -- Variant that doesn't care about qualified/unqualified names
83 mkPlainErrMsg :: SrcSpan -> Message -> ErrMsg
84 mkPlainErrMsg locn msg
85   = ErrMsg [locn] alwaysQualify msg empty
86
87 -- A long (multi-line) error message, with context to tell us whether
88 -- to qualify names in the message or not.
89 mkLongErrMsg :: SrcSpan -> PrintUnqualified -> Message -> Message -> ErrMsg
90 mkLongErrMsg locn print_unqual msg extra 
91  = ErrMsg [locn] print_unqual msg extra
92
93 mkWarnMsg :: SrcSpan -> PrintUnqualified -> Message -> WarnMsg
94 mkWarnMsg = mkErrMsg
95
96 type Messages = (Bag WarnMsg, Bag ErrMsg)
97
98 emptyMessages :: Messages
99 emptyMessages = (emptyBag, emptyBag)
100
101 errorsFound :: DynFlags -> Messages -> Bool
102 -- The dyn-flags are used to see if the user has specified
103 -- -Werorr, which says that warnings should be fatal
104 errorsFound dflags (warns, errs) 
105   | dopt Opt_WarnIsError dflags = not (isEmptyBag errs) || not (isEmptyBag warns)
106   | otherwise                   = not (isEmptyBag errs)
107
108 printErrorsAndWarnings :: Messages -> IO ()
109 printErrorsAndWarnings (warns, errs)
110   | no_errs && no_warns  = return ()
111   | no_errs              = printErrs (pprBagOfWarnings warns)
112                             -- Don't print any warnings if there are errors
113   | otherwise            = printErrs (pprBagOfErrors   errs)
114   where
115     no_warns = isEmptyBag warns
116     no_errs  = isEmptyBag errs
117
118 pprBagOfErrors :: Bag ErrMsg -> Pretty.Doc
119 pprBagOfErrors bag_of_errors
120   = Pretty.vcat [ let style = mkErrStyle unqual
121                       doc = mkLocMessage s (d $$ e)
122                   in
123                   Pretty.text "" Pretty.$$ doc style
124                 | ErrMsg { errMsgSpans = s:ss,
125                            errMsgShortDoc = d,
126                            errMsgExtraInfo = e,
127                            errMsgContext = unqual } <- sorted_errs ]
128     where
129       bag_ls      = bagToList bag_of_errors
130       sorted_errs = sortLe occ'ed_before bag_ls
131
132       occ'ed_before err1 err2 = 
133          case compare (head (errMsgSpans err1)) (head (errMsgSpans err2)) of
134                 LT -> True
135                 EQ -> True
136                 GT -> False
137
138 pprBagOfWarnings :: Bag WarnMsg -> Pretty.Doc
139 pprBagOfWarnings bag_of_warns = pprBagOfErrors bag_of_warns
140 \end{code}
141
142 \begin{code}
143 ghcExit :: Int -> IO ()
144 ghcExit val
145   | val == 0  = exitWith ExitSuccess
146   | otherwise = do errorMsg "\nCompilation had errors\n\n"
147                    exitWith (ExitFailure val)
148 \end{code}
149
150 \begin{code}
151 doIfSet :: Bool -> IO () -> IO ()
152 doIfSet flag action | flag      = action
153                     | otherwise = return ()
154
155 doIfSet_dyn :: DynFlags -> DynFlag -> IO () -> IO()
156 doIfSet_dyn dflags flag action | dopt flag dflags = action
157                                | otherwise        = return ()
158 \end{code}
159
160 \begin{code}
161 showPass :: DynFlags -> String -> IO ()
162 showPass dflags what = compilationPassMsg dflags ("*** "++what++":")
163
164 dumpIfSet :: Bool -> String -> SDoc -> IO ()
165 dumpIfSet flag hdr doc
166   | not flag   = return ()
167   | otherwise  = printDump (mkDumpDoc hdr doc)
168
169 dumpIfSet_core :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
170 dumpIfSet_core dflags flag hdr doc
171   | dopt flag dflags
172         || verbosity dflags >= 4
173         || dopt Opt_D_verbose_core2core dflags  = printDump (mkDumpDoc hdr doc)
174   | otherwise                                   = return ()
175
176 dumpIfSet_dyn :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
177 dumpIfSet_dyn dflags flag hdr doc
178   | dopt flag dflags || verbosity dflags >= 4 
179   = printDump (mkDumpDoc hdr doc)
180   | otherwise
181   = return ()
182
183 dumpIfSet_dyn_or :: DynFlags -> [DynFlag] -> String -> SDoc -> IO ()
184 dumpIfSet_dyn_or dflags flags hdr doc
185   | or [dopt flag dflags | flag <- flags]
186   || verbosity dflags >= 4 
187   = printDump (mkDumpDoc hdr doc)
188   | otherwise = return ()
189
190 mkDumpDoc hdr doc 
191    = vcat [text "", 
192            line <+> text hdr <+> line,
193            doc,
194            text ""]
195      where 
196         line = text (replicate 20 '=')
197
198 -- -----------------------------------------------------------------------------
199 -- Outputting messages from the compiler
200
201 -- We want all messages to go through one place, so that we can
202 -- redirect them if necessary.  For example, when GHC is used as a
203 -- library we might want to catch all messages that GHC tries to
204 -- output and do something else with them.
205
206 ifVerbose :: DynFlags -> Int -> IO () -> IO ()
207 ifVerbose dflags val act
208   | verbosity dflags >= val = act
209   | otherwise               = return ()
210
211 errorMsg :: String -> IO ()
212 errorMsg = putMsg
213
214 compilationProgressMsg :: DynFlags -> String -> IO ()
215 compilationProgressMsg dflags msg
216   = ifVerbose dflags 1 (putMsg msg)
217
218 compilationPassMsg :: DynFlags -> String -> IO ()
219 compilationPassMsg dflags msg
220   = ifVerbose dflags 2 (putMsg msg)
221
222 debugTraceMsg :: DynFlags -> String -> IO ()
223 debugTraceMsg dflags msg
224   = ifVerbose dflags 2 (putMsg msg)
225
226 GLOBAL_VAR(msgHandler, hPutStrLn stderr, (String -> IO ()))
227
228 setMsgHandler :: (String -> IO ()) -> IO ()
229 setMsgHandler handle_msg = writeIORef msgHandler handle_msg
230
231 putMsg :: String -> IO ()
232 putMsg msg = do h <- readIORef msgHandler; h msg
233 \end{code}