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