2 % (c) The AQUA Project, Glasgow University, 1994-1998
4 \section[ErrsUtils]{Utilities for error reporting}
8 Message, mkLocMessage, printError,
11 errMsgSpans, errMsgContext, errMsgShortDoc, errMsgExtraInfo,
12 Messages, errorsFound, emptyMessages,
13 mkErrMsg, mkWarnMsg, mkPlainErrMsg, mkLongErrMsg,
14 printErrorsAndWarnings, pprBagOfErrors, pprBagOfWarnings,
18 dumpIfSet, dumpIfSet_core, dumpIfSet_dyn, dumpIfSet_dyn_or, mkDumpDoc,
21 -- * Messages during compilation
24 compilationProgressMsg,
29 #include "HsVersions.h"
31 import Bag ( Bag, bagToList, isEmptyBag, emptyBag )
32 import SrcLoc ( SrcSpan )
33 import Util ( sortLe, global )
35 import qualified Pretty
36 import SrcLoc ( srcSpanStart )
37 import CmdLineOpts ( DynFlags(..), DynFlag(..), dopt,
40 import List ( replicate, sortBy )
41 import System ( ExitCode(..), exitWith )
43 import IO ( hPutStrLn, stderr, stdout )
46 -- -----------------------------------------------------------------------------
47 -- Basic error messages: just render a message with a source location.
51 mkLocMessage :: SrcSpan -> Message -> Message
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>".
59 printError :: SrcSpan -> Message -> IO ()
60 printError span msg = printErrs (mkLocMessage span msg $ defaultErrStyle)
63 -- -----------------------------------------------------------------------------
64 -- Collecting up messages for later ordering and printing.
66 data ErrMsg = ErrMsg {
67 errMsgSpans :: [SrcSpan],
68 errMsgContext :: PrintUnqualified,
69 errMsgShortDoc :: Message,
70 errMsgExtraInfo :: Message
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
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
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
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
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
101 mkWarnMsg :: SrcSpan -> PrintUnqualified -> Message -> WarnMsg
104 mkLongWarnMsg :: SrcSpan -> PrintUnqualified -> Message -> Message -> WarnMsg
105 mkLongWarnMsg = mkLongErrMsg
107 type Messages = (Bag WarnMsg, Bag ErrMsg)
109 emptyMessages :: Messages
110 emptyMessages = (emptyBag, emptyBag)
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)
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)
126 no_warns = isEmptyBag warns
127 no_errs = isEmptyBag errs
129 pprBagOfErrors :: Bag ErrMsg -> Pretty.Doc
130 pprBagOfErrors bag_of_errors
131 = Pretty.vcat [ let style = mkErrStyle unqual
132 doc = mkLocMessage s (d $$ e)
134 Pretty.text "" Pretty.$$ doc style
135 | ErrMsg { errMsgSpans = s:ss,
138 errMsgContext = unqual } <- sorted_errs ]
140 bag_ls = bagToList bag_of_errors
141 sorted_errs = sortLe occ'ed_before bag_ls
143 occ'ed_before err1 err2 =
144 case compare (head (errMsgSpans err1)) (head (errMsgSpans err2)) of
149 pprBagOfWarnings :: Bag WarnMsg -> Pretty.Doc
150 pprBagOfWarnings bag_of_warns = pprBagOfErrors bag_of_warns
154 ghcExit :: Int -> IO ()
156 | val == 0 = exitWith ExitSuccess
157 | otherwise = do errorMsg "\nCompilation had errors\n\n"
158 exitWith (ExitFailure val)
162 doIfSet :: Bool -> IO () -> IO ()
163 doIfSet flag action | flag = action
164 | otherwise = return ()
166 doIfSet_dyn :: DynFlags -> DynFlag -> IO () -> IO()
167 doIfSet_dyn dflags flag action | dopt flag dflags = action
168 | otherwise = return ()
172 showPass :: DynFlags -> String -> IO ()
173 showPass dflags what = compilationPassMsg dflags ("*** "++what++":")
175 dumpIfSet :: Bool -> String -> SDoc -> IO ()
176 dumpIfSet flag hdr doc
177 | not flag = return ()
178 | otherwise = printDump (mkDumpDoc hdr doc)
180 dumpIfSet_core :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
181 dumpIfSet_core dflags flag hdr doc
183 || verbosity dflags >= 4
184 || dopt Opt_D_verbose_core2core dflags = printDump (mkDumpDoc hdr doc)
185 | otherwise = return ()
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)
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 ()
203 line <+> text hdr <+> line,
207 line = text (replicate 20 '=')
209 -- -----------------------------------------------------------------------------
210 -- Outputting messages from the compiler
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.
217 ifVerbose :: DynFlags -> Int -> IO () -> IO ()
218 ifVerbose dflags val act
219 | verbosity dflags >= val = act
220 | otherwise = return ()
222 errorMsg :: String -> IO ()
225 compilationProgressMsg :: DynFlags -> String -> IO ()
226 compilationProgressMsg dflags msg
227 = ifVerbose dflags 1 (putMsg msg)
229 compilationPassMsg :: DynFlags -> String -> IO ()
230 compilationPassMsg dflags msg
231 = ifVerbose dflags 2 (putMsg msg)
233 debugTraceMsg :: DynFlags -> String -> IO ()
234 debugTraceMsg dflags msg
235 = ifVerbose dflags 2 (putMsg msg)
237 GLOBAL_VAR(msgHandler, hPutStrLn stderr, (String -> IO ()))
239 setMsgHandler :: (String -> IO ()) -> IO ()
240 setMsgHandler handle_msg = writeIORef msgHandler handle_msg
242 putMsg :: String -> IO ()
243 putMsg msg = do h <- readIORef msgHandler; h msg