2 % (c) The AQUA Project, Glasgow University, 1994-1998
4 \section[ErrsUtils]{Utilities for error reporting}
8 Message, mkLocMessage, printError,
12 errMsgSpans, errMsgContext, errMsgShortDoc, errMsgExtraInfo,
13 Messages, errorsFound, emptyMessages,
14 mkErrMsg, mkWarnMsg, mkPlainErrMsg, mkLongErrMsg,
15 printErrorsAndWarnings, printBagOfErrors, printBagOfWarnings,
19 dumpIfSet, dumpIf_core, dumpIfSet_core, dumpIfSet_dyn, dumpIfSet_dyn_or,
22 -- * Messages during compilation
26 compilationProgressMsg,
31 -- XXX This define is a bit of a hack, and should be done more nicely
32 #define FAST_STRING_NOT_NEEDED 1
33 #include "HsVersions.h"
35 import Bag ( Bag, bagToList, isEmptyBag, emptyBag )
36 import SrcLoc ( SrcSpan )
37 import Util ( sortLe )
39 import SrcLoc ( srcSpanStart, noSrcSpan )
40 import DynFlags ( DynFlags(..), DynFlag(..), dopt )
41 import StaticFlags ( opt_ErrorSpans )
44 import System.Exit ( ExitCode(..), exitWith )
49 -- -----------------------------------------------------------------------------
50 -- Basic error messages: just render a message with a source location.
60 mkLocMessage :: SrcSpan -> Message -> Message
62 | opt_ErrorSpans = hang (ppr locn <> colon) 4 msg
63 | otherwise = hang (ppr (srcSpanStart locn) <> colon) 4 msg
64 -- always print the location, even if it is unhelpful. Error messages
65 -- are supposed to be in a standard format, and one without a location
66 -- would look strange. Better to say explicitly "<no location info>".
68 printError :: SrcSpan -> Message -> IO ()
69 printError span msg = printErrs (mkLocMessage span msg $ defaultErrStyle)
72 -- -----------------------------------------------------------------------------
73 -- Collecting up messages for later ordering and printing.
75 data ErrMsg = ErrMsg {
76 errMsgSpans :: [SrcSpan],
77 errMsgContext :: PrintUnqualified,
78 errMsgShortDoc :: Message,
79 errMsgExtraInfo :: Message
81 -- The SrcSpan is used for sorting errors into line-number order
82 -- NB Pretty.Doc not SDoc: we deal with the printing style (in ptic
83 -- whether to qualify an External Name) at the error occurrence
85 -- So we can throw these things as exceptions
87 errMsgTc = mkTyCon "ErrMsg"
88 {-# NOINLINE errMsgTc #-}
89 instance Typeable ErrMsg where
90 #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ < 603
91 typeOf _ = mkAppTy errMsgTc []
93 typeOf _ = mkTyConApp errMsgTc []
98 -- A short (one-line) error message, with context to tell us whether
99 -- to qualify names in the message or not.
100 mkErrMsg :: SrcSpan -> PrintUnqualified -> Message -> ErrMsg
101 mkErrMsg locn print_unqual msg
102 = ErrMsg [locn] print_unqual msg empty
104 -- Variant that doesn't care about qualified/unqualified names
105 mkPlainErrMsg :: SrcSpan -> Message -> ErrMsg
106 mkPlainErrMsg locn msg
107 = ErrMsg [locn] alwaysQualify msg empty
109 -- A long (multi-line) error message, with context to tell us whether
110 -- to qualify names in the message or not.
111 mkLongErrMsg :: SrcSpan -> PrintUnqualified -> Message -> Message -> ErrMsg
112 mkLongErrMsg locn print_unqual msg extra
113 = ErrMsg [locn] print_unqual msg extra
115 mkWarnMsg :: SrcSpan -> PrintUnqualified -> Message -> WarnMsg
118 type Messages = (Bag WarnMsg, Bag ErrMsg)
120 emptyMessages :: Messages
121 emptyMessages = (emptyBag, emptyBag)
123 errorsFound :: DynFlags -> Messages -> Bool
124 -- The dyn-flags are used to see if the user has specified
125 -- -Werorr, which says that warnings should be fatal
126 errorsFound dflags (warns, errs)
127 | dopt Opt_WarnIsError dflags = not (isEmptyBag errs) || not (isEmptyBag warns)
128 | otherwise = not (isEmptyBag errs)
130 printErrorsAndWarnings :: DynFlags -> Messages -> IO ()
131 printErrorsAndWarnings dflags (warns, errs)
132 | no_errs && no_warns = return ()
133 | no_errs = do printBagOfWarnings dflags warns
134 when (dopt Opt_WarnIsError dflags) $
136 text "\nFailing due to -Werror.\n"
137 -- Don't print any warnings if there are errors
138 | otherwise = printBagOfErrors dflags errs
140 no_warns = isEmptyBag warns
141 no_errs = isEmptyBag errs
143 printBagOfErrors :: DynFlags -> Bag ErrMsg -> IO ()
144 printBagOfErrors dflags bag_of_errors
145 = sequence_ [ let style = mkErrStyle unqual
146 in log_action dflags SevError s style (d $$ e)
147 | ErrMsg { errMsgSpans = s:_,
150 errMsgContext = unqual } <- sorted_errs ]
152 bag_ls = bagToList bag_of_errors
153 sorted_errs = sortLe occ'ed_before bag_ls
155 occ'ed_before err1 err2 =
156 case compare (head (errMsgSpans err1)) (head (errMsgSpans err2)) of
161 printBagOfWarnings :: DynFlags -> Bag ErrMsg -> IO ()
162 printBagOfWarnings dflags bag_of_warns
163 = sequence_ [ let style = mkErrStyle unqual
164 in log_action dflags SevWarning s style (d $$ e)
165 | ErrMsg { errMsgSpans = s:_,
168 errMsgContext = unqual } <- sorted_errs ]
170 bag_ls = bagToList bag_of_warns
171 sorted_errs = sortLe occ'ed_before bag_ls
173 occ'ed_before err1 err2 =
174 case compare (head (errMsgSpans err1)) (head (errMsgSpans err2)) of
181 ghcExit :: DynFlags -> Int -> IO ()
183 | val == 0 = exitWith ExitSuccess
184 | otherwise = do errorMsg dflags (text "\nCompilation had errors\n\n")
185 exitWith (ExitFailure val)
187 doIfSet :: Bool -> IO () -> IO ()
188 doIfSet flag action | flag = action
189 | otherwise = return ()
191 doIfSet_dyn :: DynFlags -> DynFlag -> IO () -> IO()
192 doIfSet_dyn dflags flag action | dopt flag dflags = action
193 | otherwise = return ()
195 -- -----------------------------------------------------------------------------
198 dumpIfSet :: Bool -> String -> SDoc -> IO ()
199 dumpIfSet flag hdr doc
200 | not flag = return ()
201 | otherwise = printDump (mkDumpDoc hdr doc)
203 dumpIf_core :: Bool -> DynFlags -> DynFlag -> String -> SDoc -> IO ()
204 dumpIf_core cond dflags dflag hdr doc
206 || verbosity dflags >= 4
207 || dopt Opt_D_verbose_core2core dflags
208 = dumpSDoc dflags dflag hdr doc
210 | otherwise = return ()
212 dumpIfSet_core :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
213 dumpIfSet_core dflags flag hdr doc
214 = dumpIf_core (dopt flag dflags) dflags flag hdr doc
216 dumpIfSet_dyn :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
217 dumpIfSet_dyn dflags flag hdr doc
218 | dopt flag dflags || verbosity dflags >= 4
219 = dumpSDoc dflags flag hdr doc
223 dumpIfSet_dyn_or :: DynFlags -> [DynFlag] -> String -> SDoc -> IO ()
224 dumpIfSet_dyn_or dflags flags hdr doc
225 | or [dopt flag dflags | flag <- flags]
226 || verbosity dflags >= 4
227 = printDump (mkDumpDoc hdr doc)
228 | otherwise = return ()
230 mkDumpDoc :: String -> SDoc -> SDoc
233 line <+> text hdr <+> line,
237 line = text (replicate 20 '=')
240 -- | Write out a dump.
241 -- If --dump-to-file is set then this goes to a file.
242 -- otherwise emit to stdout.
243 dumpSDoc :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
244 dumpSDoc dflags dflag hdr doc
245 = do let mFile = chooseDumpFile dflags dflag
247 -- write the dump to a file
248 -- don't add the header in this case, we can see what kind
249 -- of dump it is from the filename.
251 -> do handle <- openFile fileName AppendMode
252 hPrintDump handle doc
255 -- write the dump to stdout
257 -> do printDump (mkDumpDoc hdr doc)
260 -- | Choose where to put a dump file based on DynFlags
262 chooseDumpFile :: DynFlags -> DynFlag -> Maybe String
263 chooseDumpFile dflags dflag
265 -- dump file location is being forced
266 -- by the --ddump-file-prefix flag.
268 , Just prefix <- dumpPrefixForce dflags
269 = Just $ prefix ++ (beautifyDumpName dflag)
271 -- dump file location chosen by DriverPipeline.runPipeline
273 , Just prefix <- dumpPrefix dflags
274 = Just $ prefix ++ (beautifyDumpName dflag)
276 -- we haven't got a place to put a dump file.
280 where dumpToFile = dopt Opt_DumpToFile dflags
283 -- | Build a nice file name from name of a DynFlag constructor
284 beautifyDumpName :: DynFlag -> String
285 beautifyDumpName dflag
286 = let str = show dflag
287 cut = if isPrefixOf "Opt_D_" str
290 dash = map (\c -> case c of
297 -- -----------------------------------------------------------------------------
298 -- Outputting messages from the compiler
300 -- We want all messages to go through one place, so that we can
301 -- redirect them if necessary. For example, when GHC is used as a
302 -- library we might want to catch all messages that GHC tries to
303 -- output and do something else with them.
305 ifVerbose :: DynFlags -> Int -> IO () -> IO ()
306 ifVerbose dflags val act
307 | verbosity dflags >= val = act
308 | otherwise = return ()
310 putMsg :: DynFlags -> Message -> IO ()
311 putMsg dflags msg = log_action dflags SevInfo noSrcSpan defaultUserStyle msg
313 errorMsg :: DynFlags -> Message -> IO ()
314 errorMsg dflags msg = log_action dflags SevError noSrcSpan defaultErrStyle msg
316 fatalErrorMsg :: DynFlags -> Message -> IO ()
317 fatalErrorMsg dflags msg = log_action dflags SevFatal noSrcSpan defaultErrStyle msg
319 compilationProgressMsg :: DynFlags -> String -> IO ()
320 compilationProgressMsg dflags msg
321 = ifVerbose dflags 1 (log_action dflags SevInfo noSrcSpan defaultUserStyle (text msg))
323 showPass :: DynFlags -> String -> IO ()
325 = ifVerbose dflags 2 (log_action dflags SevInfo noSrcSpan defaultUserStyle (text "***" <+> text what <> colon))
327 debugTraceMsg :: DynFlags -> Int -> Message -> IO ()
328 debugTraceMsg dflags val msg
329 = ifVerbose dflags val (log_action dflags SevInfo noSrcSpan defaultDumpStyle msg)