2 % (c) The AQUA Project, Glasgow University, 1994-1998
4 \section[ErrsUtils]{Utilities for error reporting}
8 Message, mkLocMessage, printError, pprMessageBag,
12 ErrorMessages, WarningMessages,
13 errMsgSpans, errMsgContext, errMsgShortDoc, errMsgExtraInfo,
14 Messages, errorsFound, emptyMessages,
15 mkErrMsg, mkPlainErrMsg, mkLongErrMsg, mkWarnMsg, mkPlainWarnMsg,
16 printBagOfErrors, printBagOfWarnings,
17 warnIsErrorMsg, mkLongWarnMsg,
21 dumpIfSet, dumpIfSet_dyn, dumpIfSet_dyn_or,
24 -- * Messages during compilation
28 compilationProgressMsg,
33 #include "HsVersions.h"
35 import Bag ( Bag, bagToList, isEmptyBag, emptyBag )
36 import Util ( sortLe )
39 import DynFlags ( DynFlags(..), DynFlag(..), dopt )
40 import StaticFlags ( opt_ErrorSpans )
42 import System.Exit ( ExitCode(..), exitWith )
46 -- -----------------------------------------------------------------------------
47 -- Basic error messages: just render a message with a source location.
51 pprMessageBag :: Bag Message -> SDoc
52 pprMessageBag msgs = vcat (punctuate blankLine (bagToList msgs))
61 mkLocMessage :: SrcSpan -> Message -> Message
63 | opt_ErrorSpans = hang (ppr locn <> colon) 4 msg
64 | otherwise = hang (ppr (srcSpanStart locn) <> colon) 4 msg
65 -- always print the location, even if it is unhelpful. Error messages
66 -- are supposed to be in a standard format, and one without a location
67 -- would look strange. Better to say explicitly "<no location info>".
69 printError :: SrcSpan -> Message -> IO ()
71 printErrs (mkLocMessage span msg) defaultErrStyle
74 -- -----------------------------------------------------------------------------
75 -- Collecting up messages for later ordering and printing.
77 data ErrMsg = ErrMsg {
78 errMsgSpans :: [SrcSpan],
79 errMsgContext :: PrintUnqualified,
80 errMsgShortDoc :: Message,
81 errMsgExtraInfo :: Message
83 -- The SrcSpan is used for sorting errors into line-number order
85 instance Show ErrMsg where
86 show em = showSDoc (errMsgShortDoc em)
90 -- A short (one-line) error message, with context to tell us whether
91 -- to qualify names in the message or not.
92 mkErrMsg :: SrcSpan -> PrintUnqualified -> Message -> ErrMsg
93 mkErrMsg locn print_unqual msg
94 = ErrMsg { errMsgSpans = [locn], errMsgContext = print_unqual
95 , errMsgShortDoc = msg, errMsgExtraInfo = empty }
97 -- Variant that doesn't care about qualified/unqualified names
98 mkPlainErrMsg :: SrcSpan -> Message -> ErrMsg
99 mkPlainErrMsg locn msg
100 = ErrMsg { errMsgSpans = [locn], errMsgContext = alwaysQualify
101 , errMsgShortDoc = msg, errMsgExtraInfo = empty }
103 -- A long (multi-line) error message, with context to tell us whether
104 -- to qualify names in the message or not.
105 mkLongErrMsg :: SrcSpan -> PrintUnqualified -> Message -> Message -> ErrMsg
106 mkLongErrMsg locn print_unqual msg extra
107 = ErrMsg { errMsgSpans = [locn], errMsgContext = print_unqual
108 , errMsgShortDoc = msg, errMsgExtraInfo = extra }
110 mkWarnMsg :: SrcSpan -> PrintUnqualified -> Message -> WarnMsg
113 mkLongWarnMsg :: SrcSpan -> PrintUnqualified -> Message -> Message -> ErrMsg
114 mkLongWarnMsg = mkLongErrMsg
116 -- Variant that doesn't care about qualified/unqualified names
117 mkPlainWarnMsg :: SrcSpan -> Message -> ErrMsg
118 mkPlainWarnMsg locn msg = mkWarnMsg locn alwaysQualify msg
120 type Messages = (Bag WarnMsg, Bag ErrMsg)
122 type WarningMessages = Bag WarnMsg
123 type ErrorMessages = Bag ErrMsg
125 emptyMessages :: Messages
126 emptyMessages = (emptyBag, emptyBag)
128 warnIsErrorMsg :: ErrMsg
129 warnIsErrorMsg = mkPlainErrMsg noSrcSpan (text "\nFailing due to -Werror.")
131 errorsFound :: DynFlags -> Messages -> Bool
132 errorsFound _dflags (_warns, errs) = not (isEmptyBag errs)
134 printBagOfErrors :: DynFlags -> Bag ErrMsg -> IO ()
135 printBagOfErrors dflags bag_of_errors =
136 printMsgBag dflags bag_of_errors SevError
138 printBagOfWarnings :: DynFlags -> Bag WarnMsg -> IO ()
139 printBagOfWarnings dflags bag_of_warns =
140 printMsgBag dflags bag_of_warns SevWarning
142 printMsgBag :: DynFlags -> Bag ErrMsg -> Severity -> IO ()
143 printMsgBag dflags bag sev
144 = sequence_ [ let style = mkErrStyle unqual
145 in log_action dflags sev s style (d $$ e)
146 | ErrMsg { errMsgSpans = s:_,
149 errMsgContext = unqual } <- sorted_errs ]
151 bag_ls = bagToList bag
152 sorted_errs = sortLe occ'ed_before bag_ls
154 occ'ed_before err1 err2 =
155 case compare (head (errMsgSpans err1)) (head (errMsgSpans err2)) of
160 ghcExit :: DynFlags -> Int -> IO ()
162 | val == 0 = exitWith ExitSuccess
163 | otherwise = do errorMsg dflags (text "\nCompilation had errors\n\n")
164 exitWith (ExitFailure val)
166 doIfSet :: Bool -> IO () -> IO ()
167 doIfSet flag action | flag = action
168 | otherwise = return ()
170 doIfSet_dyn :: DynFlags -> DynFlag -> IO () -> IO()
171 doIfSet_dyn dflags flag action | dopt flag dflags = action
172 | otherwise = return ()
174 -- -----------------------------------------------------------------------------
177 dumpIfSet :: Bool -> String -> SDoc -> IO ()
178 dumpIfSet flag hdr doc
179 | not flag = return ()
180 | otherwise = printDump (mkDumpDoc hdr doc)
182 dumpIfSet_dyn :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
183 dumpIfSet_dyn dflags flag hdr doc
184 | dopt flag dflags || verbosity dflags >= 4
185 = dumpSDoc dflags flag hdr doc
189 dumpIfSet_dyn_or :: DynFlags -> [DynFlag] -> String -> SDoc -> IO ()
190 dumpIfSet_dyn_or dflags flags hdr doc
191 | or [dopt flag dflags | flag <- flags]
192 || verbosity dflags >= 4
193 = printDump (mkDumpDoc hdr doc)
194 | otherwise = return ()
196 mkDumpDoc :: String -> SDoc -> SDoc
199 line <+> text hdr <+> line,
203 line = text (replicate 20 '=')
206 -- | Write out a dump.
207 -- If --dump-to-file is set then this goes to a file.
208 -- otherwise emit to stdout.
209 dumpSDoc :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
210 dumpSDoc dflags dflag hdr doc
211 = do let mFile = chooseDumpFile dflags dflag
213 -- write the dump to a file
214 -- don't add the header in this case, we can see what kind
215 -- of dump it is from the filename.
217 -> do handle <- openFile fileName AppendMode
218 hPrintDump handle doc
221 -- write the dump to stdout
223 -> do printDump (mkDumpDoc hdr doc)
226 -- | Choose where to put a dump file based on DynFlags
228 chooseDumpFile :: DynFlags -> DynFlag -> Maybe String
229 chooseDumpFile dflags dflag
231 -- dump file location is being forced
232 -- by the --ddump-file-prefix flag.
234 , Just prefix <- dumpPrefixForce dflags
235 = Just $ prefix ++ (beautifyDumpName dflag)
237 -- dump file location chosen by DriverPipeline.runPipeline
239 , Just prefix <- dumpPrefix dflags
240 = Just $ prefix ++ (beautifyDumpName dflag)
242 -- we haven't got a place to put a dump file.
246 where dumpToFile = dopt Opt_DumpToFile dflags
249 -- | Build a nice file name from name of a DynFlag constructor
250 beautifyDumpName :: DynFlag -> String
251 beautifyDumpName dflag
252 = let str = show dflag
253 cut = if isPrefixOf "Opt_D_" str
256 dash = map (\c -> case c of
263 -- -----------------------------------------------------------------------------
264 -- Outputting messages from the compiler
266 -- We want all messages to go through one place, so that we can
267 -- redirect them if necessary. For example, when GHC is used as a
268 -- library we might want to catch all messages that GHC tries to
269 -- output and do something else with them.
271 ifVerbose :: DynFlags -> Int -> IO () -> IO ()
272 ifVerbose dflags val act
273 | verbosity dflags >= val = act
274 | otherwise = return ()
276 putMsg :: DynFlags -> Message -> IO ()
277 putMsg dflags msg = log_action dflags SevInfo noSrcSpan defaultUserStyle msg
279 putMsgWith :: DynFlags -> PrintUnqualified -> Message -> IO ()
280 putMsgWith dflags print_unqual msg
281 = log_action dflags SevInfo noSrcSpan sty msg
283 sty = mkUserStyle print_unqual AllTheWay
285 errorMsg :: DynFlags -> Message -> IO ()
286 errorMsg dflags msg = log_action dflags SevError noSrcSpan defaultErrStyle msg
288 fatalErrorMsg :: DynFlags -> Message -> IO ()
289 fatalErrorMsg dflags msg = log_action dflags SevFatal noSrcSpan defaultErrStyle msg
291 compilationProgressMsg :: DynFlags -> String -> IO ()
292 compilationProgressMsg dflags msg
293 = ifVerbose dflags 1 (log_action dflags SevOutput noSrcSpan defaultUserStyle (text msg))
295 showPass :: DynFlags -> String -> IO ()
297 = ifVerbose dflags 2 (log_action dflags SevInfo noSrcSpan defaultUserStyle (text "***" <+> text what <> colon))
299 debugTraceMsg :: DynFlags -> Int -> Message -> IO ()
300 debugTraceMsg dflags val msg
301 = ifVerbose dflags val (log_action dflags SevInfo noSrcSpan defaultDumpStyle msg)