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