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 #include "HsVersions.h"
33 import Bag ( Bag, bagToList, isEmptyBag, emptyBag )
34 import SrcLoc ( SrcSpan )
35 import Util ( sortLe )
37 import SrcLoc ( srcSpanStart, noSrcSpan )
38 import DynFlags ( DynFlags(..), DynFlag(..), dopt )
39 import StaticFlags ( opt_ErrorSpans )
42 import System.Exit ( ExitCode(..), exitWith )
47 -- -----------------------------------------------------------------------------
48 -- Basic error messages: just render a message with a source location.
58 mkLocMessage :: SrcSpan -> Message -> Message
60 | opt_ErrorSpans = hang (ppr locn <> colon) 4 msg
61 | otherwise = hang (ppr (srcSpanStart locn) <> colon) 4 msg
62 -- always print the location, even if it is unhelpful. Error messages
63 -- are supposed to be in a standard format, and one without a location
64 -- would look strange. Better to say explicitly "<no location info>".
66 printError :: SrcSpan -> Message -> IO ()
67 printError span msg = printErrs (mkLocMessage span msg $ defaultErrStyle)
70 -- -----------------------------------------------------------------------------
71 -- Collecting up messages for later ordering and printing.
73 data ErrMsg = ErrMsg {
74 errMsgSpans :: [SrcSpan],
75 errMsgContext :: PrintUnqualified,
76 errMsgShortDoc :: Message,
77 errMsgExtraInfo :: Message
79 -- The SrcSpan is used for sorting errors into line-number order
80 -- NB Pretty.Doc not SDoc: we deal with the printing style (in ptic
81 -- whether to qualify an External Name) at the error occurrence
83 -- So we can throw these things as exceptions
85 errMsgTc = mkTyCon "ErrMsg"
86 {-# NOINLINE errMsgTc #-}
87 instance Typeable ErrMsg where
88 #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ < 603
89 typeOf _ = mkAppTy errMsgTc []
91 typeOf _ = mkTyConApp errMsgTc []
96 -- A short (one-line) error message, with context to tell us whether
97 -- to qualify names in the message or not.
98 mkErrMsg :: SrcSpan -> PrintUnqualified -> Message -> ErrMsg
99 mkErrMsg locn print_unqual msg
100 = ErrMsg [locn] print_unqual msg empty
102 -- Variant that doesn't care about qualified/unqualified names
103 mkPlainErrMsg :: SrcSpan -> Message -> ErrMsg
104 mkPlainErrMsg locn msg
105 = ErrMsg [locn] alwaysQualify msg empty
107 -- A long (multi-line) error message, with context to tell us whether
108 -- to qualify names in the message or not.
109 mkLongErrMsg :: SrcSpan -> PrintUnqualified -> Message -> Message -> ErrMsg
110 mkLongErrMsg locn print_unqual msg extra
111 = ErrMsg [locn] print_unqual msg extra
113 mkWarnMsg :: SrcSpan -> PrintUnqualified -> Message -> WarnMsg
116 type Messages = (Bag WarnMsg, Bag ErrMsg)
118 emptyMessages :: Messages
119 emptyMessages = (emptyBag, emptyBag)
121 errorsFound :: DynFlags -> Messages -> Bool
122 -- The dyn-flags are used to see if the user has specified
123 -- -Werorr, which says that warnings should be fatal
124 errorsFound dflags (warns, errs)
125 | dopt Opt_WarnIsError dflags = not (isEmptyBag errs) || not (isEmptyBag warns)
126 | otherwise = not (isEmptyBag errs)
128 printErrorsAndWarnings :: DynFlags -> Messages -> IO ()
129 printErrorsAndWarnings dflags (warns, errs)
130 | no_errs && no_warns = return ()
131 | no_errs = do printBagOfWarnings dflags warns
132 when (dopt Opt_WarnIsError dflags) $
134 text "\nFailing due to -Werror.\n"
135 -- Don't print any warnings if there are errors
136 | otherwise = printBagOfErrors dflags errs
138 no_warns = isEmptyBag warns
139 no_errs = isEmptyBag errs
141 printBagOfErrors :: DynFlags -> Bag ErrMsg -> IO ()
142 printBagOfErrors dflags bag_of_errors
143 = sequence_ [ let style = mkErrStyle unqual
144 in log_action dflags SevError s style (d $$ e)
145 | ErrMsg { errMsgSpans = s:_,
148 errMsgContext = unqual } <- sorted_errs ]
150 bag_ls = bagToList bag_of_errors
151 sorted_errs = sortLe occ'ed_before bag_ls
153 occ'ed_before err1 err2 =
154 case compare (head (errMsgSpans err1)) (head (errMsgSpans err2)) of
159 printBagOfWarnings :: DynFlags -> Bag ErrMsg -> IO ()
160 printBagOfWarnings dflags bag_of_warns
161 = sequence_ [ let style = mkErrStyle unqual
162 in log_action dflags SevWarning s style (d $$ e)
163 | ErrMsg { errMsgSpans = s:_,
166 errMsgContext = unqual } <- sorted_errs ]
168 bag_ls = bagToList bag_of_warns
169 sorted_errs = sortLe occ'ed_before bag_ls
171 occ'ed_before err1 err2 =
172 case compare (head (errMsgSpans err1)) (head (errMsgSpans err2)) of
179 ghcExit :: DynFlags -> Int -> IO ()
181 | val == 0 = exitWith ExitSuccess
182 | otherwise = do errorMsg dflags (text "\nCompilation had errors\n\n")
183 exitWith (ExitFailure val)
185 doIfSet :: Bool -> IO () -> IO ()
186 doIfSet flag action | flag = action
187 | otherwise = return ()
189 doIfSet_dyn :: DynFlags -> DynFlag -> IO () -> IO()
190 doIfSet_dyn dflags flag action | dopt flag dflags = action
191 | otherwise = return ()
193 -- -----------------------------------------------------------------------------
196 dumpIfSet :: Bool -> String -> SDoc -> IO ()
197 dumpIfSet flag hdr doc
198 | not flag = return ()
199 | otherwise = printDump (mkDumpDoc hdr doc)
201 dumpIf_core :: Bool -> DynFlags -> DynFlag -> String -> SDoc -> IO ()
202 dumpIf_core cond dflags dflag hdr doc
204 || verbosity dflags >= 4
205 || dopt Opt_D_verbose_core2core dflags
206 = dumpSDoc dflags dflag hdr doc
208 | otherwise = return ()
210 dumpIfSet_core :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
211 dumpIfSet_core dflags flag hdr doc
212 = dumpIf_core (dopt flag dflags) dflags flag hdr doc
214 dumpIfSet_dyn :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
215 dumpIfSet_dyn dflags flag hdr doc
216 | dopt flag dflags || verbosity dflags >= 4
217 = dumpSDoc dflags flag hdr doc
221 dumpIfSet_dyn_or :: DynFlags -> [DynFlag] -> String -> SDoc -> IO ()
222 dumpIfSet_dyn_or dflags flags hdr doc
223 | or [dopt flag dflags | flag <- flags]
224 || verbosity dflags >= 4
225 = printDump (mkDumpDoc hdr doc)
226 | otherwise = return ()
228 mkDumpDoc :: String -> SDoc -> SDoc
231 line <+> text hdr <+> line,
235 line = text (replicate 20 '=')
238 -- | Write out a dump.
239 -- If --dump-to-file is set then this goes to a file.
240 -- otherwise emit to stdout.
241 dumpSDoc :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
242 dumpSDoc dflags dflag hdr doc
243 = do let mFile = chooseDumpFile dflags dflag
245 -- write the dump to a file
246 -- don't add the header in this case, we can see what kind
247 -- of dump it is from the filename.
249 -> do handle <- openFile fileName AppendMode
250 hPrintDump handle doc
253 -- write the dump to stdout
255 -> do printDump (mkDumpDoc hdr doc)
258 -- | Choose where to put a dump file based on DynFlags
260 chooseDumpFile :: DynFlags -> DynFlag -> Maybe String
261 chooseDumpFile dflags dflag
263 -- dump file location is being forced
264 -- by the --ddump-file-prefix flag.
266 , Just prefix <- dumpPrefixForce dflags
267 = Just $ prefix ++ (beautifyDumpName dflag)
269 -- dump file location chosen by DriverPipeline.runPipeline
271 , Just prefix <- dumpPrefix dflags
272 = Just $ prefix ++ (beautifyDumpName dflag)
274 -- we haven't got a place to put a dump file.
278 where dumpToFile = dopt Opt_DumpToFile dflags
281 -- | Build a nice file name from name of a DynFlag constructor
282 beautifyDumpName :: DynFlag -> String
283 beautifyDumpName dflag
284 = let str = show dflag
285 cut = if isPrefixOf "Opt_D_" str
288 dash = map (\c -> case c of
295 -- -----------------------------------------------------------------------------
296 -- Outputting messages from the compiler
298 -- We want all messages to go through one place, so that we can
299 -- redirect them if necessary. For example, when GHC is used as a
300 -- library we might want to catch all messages that GHC tries to
301 -- output and do something else with them.
303 ifVerbose :: DynFlags -> Int -> IO () -> IO ()
304 ifVerbose dflags val act
305 | verbosity dflags >= val = act
306 | otherwise = return ()
308 putMsg :: DynFlags -> Message -> IO ()
309 putMsg dflags msg = log_action dflags SevInfo noSrcSpan defaultUserStyle msg
311 errorMsg :: DynFlags -> Message -> IO ()
312 errorMsg dflags msg = log_action dflags SevError noSrcSpan defaultErrStyle msg
314 fatalErrorMsg :: DynFlags -> Message -> IO ()
315 fatalErrorMsg dflags msg = log_action dflags SevFatal noSrcSpan defaultErrStyle msg
317 compilationProgressMsg :: DynFlags -> String -> IO ()
318 compilationProgressMsg dflags msg
319 = ifVerbose dflags 1 (log_action dflags SevInfo noSrcSpan defaultUserStyle (text msg))
321 showPass :: DynFlags -> String -> IO ()
323 = ifVerbose dflags 2 (log_action dflags SevInfo noSrcSpan defaultUserStyle (text "***" <+> text what <> colon))
325 debugTraceMsg :: DynFlags -> Int -> Message -> IO ()
326 debugTraceMsg dflags val msg
327 = ifVerbose dflags val (log_action dflags SevInfo noSrcSpan defaultDumpStyle msg)