2 % (c) The AQUA Project, Glasgow University, 1994-1998
4 \section[ErrsUtils]{Utilities for error reporting}
8 -- The above warning supression flag is a temporary kludge.
9 -- While working on this module you are encouraged to remove it and fix
10 -- any warnings in the module. See
11 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
15 Message, mkLocMessage, printError,
19 errMsgSpans, errMsgContext, errMsgShortDoc, errMsgExtraInfo,
20 Messages, errorsFound, emptyMessages,
21 mkErrMsg, mkWarnMsg, mkPlainErrMsg, mkLongErrMsg,
22 printErrorsAndWarnings, printBagOfErrors, printBagOfWarnings,
26 dumpIfSet, dumpIfSet_core, dumpIfSet_dyn, dumpIfSet_dyn_or, mkDumpDoc, dumpSDoc,
28 -- * Messages during compilation
32 compilationProgressMsg,
37 #include "HsVersions.h"
39 import Module ( ModLocation(..))
40 import Bag ( Bag, bagToList, isEmptyBag, emptyBag )
41 import SrcLoc ( SrcSpan )
42 import Util ( sortLe )
44 import SrcLoc ( srcSpanStart, noSrcSpan )
45 import DynFlags ( DynFlags(..), DynFlag(..), dopt )
46 import StaticFlags ( opt_ErrorSpans )
48 import System.Exit ( ExitCode(..), exitWith )
53 -- -----------------------------------------------------------------------------
54 -- Basic error messages: just render a message with a source location.
64 mkLocMessage :: SrcSpan -> Message -> Message
66 | opt_ErrorSpans = hang (ppr locn <> colon) 4 msg
67 | otherwise = hang (ppr (srcSpanStart locn) <> colon) 4 msg
68 -- always print the location, even if it is unhelpful. Error messages
69 -- are supposed to be in a standard format, and one without a location
70 -- would look strange. Better to say explicitly "<no location info>".
72 printError :: SrcSpan -> Message -> IO ()
73 printError span msg = printErrs (mkLocMessage span msg $ defaultErrStyle)
76 -- -----------------------------------------------------------------------------
77 -- Collecting up messages for later ordering and printing.
79 data ErrMsg = ErrMsg {
80 errMsgSpans :: [SrcSpan],
81 errMsgContext :: PrintUnqualified,
82 errMsgShortDoc :: Message,
83 errMsgExtraInfo :: Message
85 -- The SrcSpan is used for sorting errors into line-number order
86 -- NB Pretty.Doc not SDoc: we deal with the printing style (in ptic
87 -- whether to qualify an External Name) at the error occurrence
89 -- So we can throw these things as exceptions
91 errMsgTc = mkTyCon "ErrMsg"
92 {-# NOINLINE errMsgTc #-}
93 instance Typeable ErrMsg where
94 #if __GLASGOW_HASKELL__ < 603
95 typeOf _ = mkAppTy errMsgTc []
97 typeOf _ = mkTyConApp errMsgTc []
100 type WarnMsg = ErrMsg
102 -- A short (one-line) error message, with context to tell us whether
103 -- to qualify names in the message or not.
104 mkErrMsg :: SrcSpan -> PrintUnqualified -> Message -> ErrMsg
105 mkErrMsg locn print_unqual msg
106 = ErrMsg [locn] print_unqual msg empty
108 -- Variant that doesn't care about qualified/unqualified names
109 mkPlainErrMsg :: SrcSpan -> Message -> ErrMsg
110 mkPlainErrMsg locn msg
111 = ErrMsg [locn] alwaysQualify msg empty
113 -- A long (multi-line) error message, with context to tell us whether
114 -- to qualify names in the message or not.
115 mkLongErrMsg :: SrcSpan -> PrintUnqualified -> Message -> Message -> ErrMsg
116 mkLongErrMsg locn print_unqual msg extra
117 = ErrMsg [locn] print_unqual msg extra
119 mkWarnMsg :: SrcSpan -> PrintUnqualified -> Message -> WarnMsg
122 type Messages = (Bag WarnMsg, Bag ErrMsg)
124 emptyMessages :: Messages
125 emptyMessages = (emptyBag, emptyBag)
127 errorsFound :: DynFlags -> Messages -> Bool
128 -- The dyn-flags are used to see if the user has specified
129 -- -Werorr, which says that warnings should be fatal
130 errorsFound dflags (warns, errs)
131 | dopt Opt_WarnIsError dflags = not (isEmptyBag errs) || not (isEmptyBag warns)
132 | otherwise = not (isEmptyBag errs)
134 printErrorsAndWarnings :: DynFlags -> Messages -> IO ()
135 printErrorsAndWarnings dflags (warns, errs)
136 | no_errs && no_warns = return ()
137 | no_errs = printBagOfWarnings dflags warns
138 -- Don't print any warnings if there are errors
139 | otherwise = printBagOfErrors dflags errs
141 no_warns = isEmptyBag warns
142 no_errs = isEmptyBag errs
144 printBagOfErrors :: DynFlags -> Bag ErrMsg -> IO ()
145 printBagOfErrors dflags bag_of_errors
146 = sequence_ [ let style = mkErrStyle unqual
147 in log_action dflags SevError s style (d $$ e)
148 | ErrMsg { errMsgSpans = s:ss,
151 errMsgContext = unqual } <- sorted_errs ]
153 bag_ls = bagToList bag_of_errors
154 sorted_errs = sortLe occ'ed_before bag_ls
156 occ'ed_before err1 err2 =
157 case compare (head (errMsgSpans err1)) (head (errMsgSpans err2)) of
162 printBagOfWarnings :: DynFlags -> Bag ErrMsg -> IO ()
163 printBagOfWarnings dflags bag_of_warns
164 = sequence_ [ let style = mkErrStyle unqual
165 in log_action dflags SevWarning s style (d $$ e)
166 | ErrMsg { errMsgSpans = s:ss,
169 errMsgContext = unqual } <- sorted_errs ]
171 bag_ls = bagToList bag_of_warns
172 sorted_errs = sortLe occ'ed_before bag_ls
174 occ'ed_before err1 err2 =
175 case compare (head (errMsgSpans err1)) (head (errMsgSpans err2)) of
182 ghcExit :: DynFlags -> Int -> IO ()
184 | val == 0 = exitWith ExitSuccess
185 | otherwise = do errorMsg dflags (text "\nCompilation had errors\n\n")
186 exitWith (ExitFailure val)
188 doIfSet :: Bool -> IO () -> IO ()
189 doIfSet flag action | flag = action
190 | otherwise = return ()
192 doIfSet_dyn :: DynFlags -> DynFlag -> IO () -> IO()
193 doIfSet_dyn dflags flag action | dopt flag dflags = action
194 | otherwise = return ()
196 -- -----------------------------------------------------------------------------
199 dumpIfSet :: Bool -> String -> SDoc -> IO ()
200 dumpIfSet flag hdr doc
201 | not flag = return ()
202 | otherwise = printDump (mkDumpDoc hdr doc)
204 dumpIfSet_core :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
205 dumpIfSet_core dflags flag hdr doc
207 || verbosity dflags >= 4
208 || dopt Opt_D_verbose_core2core dflags
209 = dumpSDoc dflags flag hdr doc
210 | otherwise = return ()
212 dumpIfSet_dyn :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
213 dumpIfSet_dyn dflags flag hdr doc
214 | dopt flag dflags || verbosity dflags >= 4
215 = dumpSDoc dflags flag hdr doc
219 dumpIfSet_dyn_or :: DynFlags -> [DynFlag] -> String -> SDoc -> IO ()
220 dumpIfSet_dyn_or dflags flags hdr doc
221 | or [dopt flag dflags | flag <- flags]
222 || verbosity dflags >= 4
223 = printDump (mkDumpDoc hdr doc)
224 | otherwise = return ()
228 line <+> text hdr <+> line,
232 line = text (replicate 20 '=')
235 -- | Write out a dump.
236 -- If --dump-to-file is set then this goes to a file.
237 -- otherwise emit to stdout.
238 dumpSDoc :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
239 dumpSDoc dflags dflag hdr doc
240 = do let mFile = chooseDumpFile dflags dflag
242 -- write the dump to a file
243 -- don't add the header in this case, we can see what kind
244 -- of dump it is from the filename.
246 -> do handle <- openFile fileName AppendMode
247 hPrintDump handle doc
250 -- write the dump to stdout
252 -> do printDump (mkDumpDoc hdr doc)
255 -- | Choose where to put a dump file based on DynFlags
257 chooseDumpFile :: DynFlags -> DynFlag -> Maybe String
258 chooseDumpFile dflags dflag
260 -- dump file location is being forced
261 -- by the --ddump-file-prefix flag.
263 , Just prefix <- dumpPrefixForce dflags
264 = Just $ prefix ++ (beautifyDumpName dflag)
266 -- dump file location chosen by DriverPipeline.runPipeline
268 , Just prefix <- dumpPrefix dflags
269 = Just $ prefix ++ (beautifyDumpName dflag)
271 -- we haven't got a place to put a dump file.
275 where dumpToFile = dopt Opt_DumpToFile dflags
278 -- | Build a nice file name from name of a DynFlag constructor
279 beautifyDumpName :: DynFlag -> String
280 beautifyDumpName dflag
281 = let str = show dflag
282 cut = if isPrefixOf "Opt_D_" str
285 dash = map (\c -> case c of
292 -- -----------------------------------------------------------------------------
293 -- Outputting messages from the compiler
295 -- We want all messages to go through one place, so that we can
296 -- redirect them if necessary. For example, when GHC is used as a
297 -- library we might want to catch all messages that GHC tries to
298 -- output and do something else with them.
300 ifVerbose :: DynFlags -> Int -> IO () -> IO ()
301 ifVerbose dflags val act
302 | verbosity dflags >= val = act
303 | otherwise = return ()
305 putMsg :: DynFlags -> Message -> IO ()
306 putMsg dflags msg = log_action dflags SevInfo noSrcSpan defaultUserStyle msg
308 errorMsg :: DynFlags -> Message -> IO ()
309 errorMsg dflags msg = log_action dflags SevError noSrcSpan defaultErrStyle msg
311 fatalErrorMsg :: DynFlags -> Message -> IO ()
312 fatalErrorMsg dflags msg = log_action dflags SevFatal noSrcSpan defaultErrStyle msg
314 compilationProgressMsg :: DynFlags -> String -> IO ()
315 compilationProgressMsg dflags msg
316 = ifVerbose dflags 1 (log_action dflags SevInfo noSrcSpan defaultUserStyle (text msg))
318 showPass :: DynFlags -> String -> IO ()
320 = ifVerbose dflags 2 (log_action dflags SevInfo noSrcSpan defaultUserStyle (text "***" <+> text what <> colon))
322 debugTraceMsg :: DynFlags -> Int -> Message -> IO ()
323 debugTraceMsg dflags val msg
324 = ifVerbose dflags val (log_action dflags SevInfo noSrcSpan defaultDumpStyle msg)