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, dumpIfSet_core, dumpIfSet_dyn, dumpIfSet_dyn_or, mkDumpDoc, dumpSDoc,
21 -- * Messages during compilation
25 compilationProgressMsg,
30 #include "HsVersions.h"
32 import Module ( ModLocation(..))
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 )
41 import System.Exit ( ExitCode(..), exitWith )
46 -- -----------------------------------------------------------------------------
47 -- Basic error messages: just render a message with a source location.
57 mkLocMessage :: SrcSpan -> Message -> Message
59 | opt_ErrorSpans = hang (ppr locn <> colon) 4 msg
60 | otherwise = hang (ppr (srcSpanStart locn) <> colon) 4 msg
61 -- always print the location, even if it is unhelpful. Error messages
62 -- are supposed to be in a standard format, and one without a location
63 -- would look strange. Better to say explicitly "<no location info>".
65 printError :: SrcSpan -> Message -> IO ()
66 printError span msg = printErrs (mkLocMessage span msg $ defaultErrStyle)
69 -- -----------------------------------------------------------------------------
70 -- Collecting up messages for later ordering and printing.
72 data ErrMsg = ErrMsg {
73 errMsgSpans :: [SrcSpan],
74 errMsgContext :: PrintUnqualified,
75 errMsgShortDoc :: Message,
76 errMsgExtraInfo :: Message
78 -- The SrcSpan is used for sorting errors into line-number order
79 -- NB Pretty.Doc not SDoc: we deal with the printing style (in ptic
80 -- whether to qualify an External Name) at the error occurrence
82 -- So we can throw these things as exceptions
84 errMsgTc = mkTyCon "ErrMsg"
85 {-# NOINLINE errMsgTc #-}
86 instance Typeable ErrMsg where
87 #if __GLASGOW_HASKELL__ < 603
88 typeOf _ = mkAppTy errMsgTc []
90 typeOf _ = mkTyConApp errMsgTc []
95 -- A short (one-line) error message, with context to tell us whether
96 -- to qualify names in the message or not.
97 mkErrMsg :: SrcSpan -> PrintUnqualified -> Message -> ErrMsg
98 mkErrMsg locn print_unqual msg
99 = ErrMsg [locn] print_unqual msg empty
101 -- Variant that doesn't care about qualified/unqualified names
102 mkPlainErrMsg :: SrcSpan -> Message -> ErrMsg
103 mkPlainErrMsg locn msg
104 = ErrMsg [locn] alwaysQualify msg empty
106 -- A long (multi-line) error message, with context to tell us whether
107 -- to qualify names in the message or not.
108 mkLongErrMsg :: SrcSpan -> PrintUnqualified -> Message -> Message -> ErrMsg
109 mkLongErrMsg locn print_unqual msg extra
110 = ErrMsg [locn] print_unqual msg extra
112 mkWarnMsg :: SrcSpan -> PrintUnqualified -> Message -> WarnMsg
115 type Messages = (Bag WarnMsg, Bag ErrMsg)
117 emptyMessages :: Messages
118 emptyMessages = (emptyBag, emptyBag)
120 errorsFound :: DynFlags -> Messages -> Bool
121 -- The dyn-flags are used to see if the user has specified
122 -- -Werorr, which says that warnings should be fatal
123 errorsFound dflags (warns, errs)
124 | dopt Opt_WarnIsError dflags = not (isEmptyBag errs) || not (isEmptyBag warns)
125 | otherwise = not (isEmptyBag errs)
127 printErrorsAndWarnings :: DynFlags -> Messages -> IO ()
128 printErrorsAndWarnings dflags (warns, errs)
129 | no_errs && no_warns = return ()
130 | no_errs = printBagOfWarnings dflags warns
131 -- Don't print any warnings if there are errors
132 | otherwise = printBagOfErrors dflags errs
134 no_warns = isEmptyBag warns
135 no_errs = isEmptyBag errs
137 printBagOfErrors :: DynFlags -> Bag ErrMsg -> IO ()
138 printBagOfErrors dflags bag_of_errors
139 = sequence_ [ let style = mkErrStyle unqual
140 in log_action dflags SevError s style (d $$ e)
141 | ErrMsg { errMsgSpans = s:ss,
144 errMsgContext = unqual } <- sorted_errs ]
146 bag_ls = bagToList bag_of_errors
147 sorted_errs = sortLe occ'ed_before bag_ls
149 occ'ed_before err1 err2 =
150 case compare (head (errMsgSpans err1)) (head (errMsgSpans err2)) of
155 printBagOfWarnings :: DynFlags -> Bag ErrMsg -> IO ()
156 printBagOfWarnings dflags bag_of_warns
157 = sequence_ [ let style = mkErrStyle unqual
158 in log_action dflags SevWarning s style (d $$ e)
159 | ErrMsg { errMsgSpans = s:ss,
162 errMsgContext = unqual } <- sorted_errs ]
164 bag_ls = bagToList bag_of_warns
165 sorted_errs = sortLe occ'ed_before bag_ls
167 occ'ed_before err1 err2 =
168 case compare (head (errMsgSpans err1)) (head (errMsgSpans err2)) of
175 ghcExit :: DynFlags -> Int -> IO ()
177 | val == 0 = exitWith ExitSuccess
178 | otherwise = do errorMsg dflags (text "\nCompilation had errors\n\n")
179 exitWith (ExitFailure val)
181 doIfSet :: Bool -> IO () -> IO ()
182 doIfSet flag action | flag = action
183 | otherwise = return ()
185 doIfSet_dyn :: DynFlags -> DynFlag -> IO () -> IO()
186 doIfSet_dyn dflags flag action | dopt flag dflags = action
187 | otherwise = return ()
189 -- -----------------------------------------------------------------------------
192 dumpIfSet :: Bool -> String -> SDoc -> IO ()
193 dumpIfSet flag hdr doc
194 | not flag = return ()
195 | otherwise = printDump (mkDumpDoc hdr doc)
197 dumpIfSet_core :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
198 dumpIfSet_core dflags flag hdr doc
200 || verbosity dflags >= 4
201 || dopt Opt_D_verbose_core2core dflags
202 = dumpSDoc dflags flag hdr doc
203 | otherwise = return ()
205 dumpIfSet_dyn :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
206 dumpIfSet_dyn dflags flag hdr doc
207 | dopt flag dflags || verbosity dflags >= 4
208 = dumpSDoc dflags flag hdr doc
212 dumpIfSet_dyn_or :: DynFlags -> [DynFlag] -> String -> SDoc -> IO ()
213 dumpIfSet_dyn_or dflags flags hdr doc
214 | or [dopt flag dflags | flag <- flags]
215 || verbosity dflags >= 4
216 = printDump (mkDumpDoc hdr doc)
217 | otherwise = return ()
221 line <+> text hdr <+> line,
225 line = text (replicate 20 '=')
228 -- | Write out a dump.
229 -- If --dump-to-file is set then this goes to a file.
230 -- otherwise emit to stdout.
231 dumpSDoc :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
232 dumpSDoc dflags dflag hdr doc
233 = do let mFile = chooseDumpFile dflags dflag
235 -- write the dump to a file
236 -- don't add the header in this case, we can see what kind
237 -- of dump it is from the filename.
239 -> do handle <- openFile fileName AppendMode
240 hPrintDump handle doc
243 -- write the dump to stdout
245 -> do printDump (mkDumpDoc hdr doc)
248 -- | Choose where to put a dump file based on DynFlags
250 chooseDumpFile :: DynFlags -> DynFlag -> Maybe String
251 chooseDumpFile dflags dflag
253 -- dump file location is being forced
254 -- by the --ddump-file-prefix flag.
256 , Just prefix <- dumpPrefixForce dflags
257 = Just $ prefix ++ (beautifyDumpName dflag)
259 -- dump file location chosen by DriverPipeline.runPipeline
261 , Just prefix <- dumpPrefix dflags
262 = Just $ prefix ++ (beautifyDumpName dflag)
264 -- we haven't got a place to put a dump file.
268 where dumpToFile = dopt Opt_DumpToFile dflags
271 -- | Build a nice file name from name of a DynFlag constructor
272 beautifyDumpName :: DynFlag -> String
273 beautifyDumpName dflag
274 = let str = show dflag
275 cut = if isPrefixOf "Opt_D_" str
278 dash = map (\c -> case c of
285 -- -----------------------------------------------------------------------------
286 -- Outputting messages from the compiler
288 -- We want all messages to go through one place, so that we can
289 -- redirect them if necessary. For example, when GHC is used as a
290 -- library we might want to catch all messages that GHC tries to
291 -- output and do something else with them.
293 ifVerbose :: DynFlags -> Int -> IO () -> IO ()
294 ifVerbose dflags val act
295 | verbosity dflags >= val = act
296 | otherwise = return ()
298 putMsg :: DynFlags -> Message -> IO ()
299 putMsg dflags msg = log_action dflags SevInfo noSrcSpan defaultUserStyle msg
301 errorMsg :: DynFlags -> Message -> IO ()
302 errorMsg dflags msg = log_action dflags SevError noSrcSpan defaultErrStyle msg
304 fatalErrorMsg :: DynFlags -> Message -> IO ()
305 fatalErrorMsg dflags msg = log_action dflags SevFatal noSrcSpan defaultErrStyle msg
307 compilationProgressMsg :: DynFlags -> String -> IO ()
308 compilationProgressMsg dflags msg
309 = ifVerbose dflags 1 (log_action dflags SevInfo noSrcSpan defaultUserStyle (text msg))
311 showPass :: DynFlags -> String -> IO ()
313 = ifVerbose dflags 2 (log_action dflags SevInfo noSrcSpan defaultUserStyle (text "***" <+> text what <> colon))
315 debugTraceMsg :: DynFlags -> Int -> Message -> IO ()
316 debugTraceMsg dflags val msg
317 = ifVerbose dflags val (log_action dflags SevInfo noSrcSpan defaultDumpStyle msg)