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