2 % (c) The AQUA Project, Glasgow University, 1994-1998
4 \section[ErrsUtils]{Utilities for error reporting}
8 Message, mkLocMessage, printError,
11 errMsgSpans, errMsgContext, errMsgShortDoc, errMsgExtraInfo,
12 Messages, errorsFound, emptyMessages,
13 mkErrMsg, mkWarnMsg, mkPlainErrMsg, mkLongErrMsg,
14 printErrorsAndWarnings, pprBagOfErrors, pprBagOfWarnings,
18 dumpIfSet, dumpIfSet_core, dumpIfSet_dyn, dumpIfSet_dyn_or, mkDumpDoc,
21 -- * Messages during compilation
24 compilationProgressMsg,
29 #include "HsVersions.h"
31 import Bag ( Bag, bagToList, isEmptyBag, emptyBag )
32 import SrcLoc ( SrcSpan )
33 import Util ( sortLe, global )
35 import qualified Pretty
36 import SrcLoc ( srcSpanStart )
37 import DynFlags ( DynFlags(..), DynFlag(..), dopt )
38 import StaticFlags ( opt_ErrorSpans )
39 import System ( ExitCode(..), exitWith )
41 import IO ( hPutStrLn, stderr )
44 -- -----------------------------------------------------------------------------
45 -- Basic error messages: just render a message with a source location.
49 mkLocMessage :: SrcSpan -> Message -> Message
51 | opt_ErrorSpans = hang (ppr locn <> colon) 4 msg
52 | otherwise = hang (ppr (srcSpanStart locn) <> colon) 4 msg
53 -- always print the location, even if it is unhelpful. Error messages
54 -- are supposed to be in a standard format, and one without a location
55 -- would look strange. Better to say explicitly "<no location info>".
57 printError :: SrcSpan -> Message -> IO ()
58 printError span msg = printErrs (mkLocMessage span msg $ defaultErrStyle)
61 -- -----------------------------------------------------------------------------
62 -- Collecting up messages for later ordering and printing.
64 data ErrMsg = ErrMsg {
65 errMsgSpans :: [SrcSpan],
66 errMsgContext :: PrintUnqualified,
67 errMsgShortDoc :: Message,
68 errMsgExtraInfo :: Message
70 -- The SrcSpan is used for sorting errors into line-number order
71 -- NB Pretty.Doc not SDoc: we deal with the printing style (in ptic
72 -- whether to qualify an External Name) at the error occurrence
76 -- A short (one-line) error message, with context to tell us whether
77 -- to qualify names in the message or not.
78 mkErrMsg :: SrcSpan -> PrintUnqualified -> Message -> ErrMsg
79 mkErrMsg locn print_unqual msg
80 = ErrMsg [locn] print_unqual msg empty
82 -- Variant that doesn't care about qualified/unqualified names
83 mkPlainErrMsg :: SrcSpan -> Message -> ErrMsg
84 mkPlainErrMsg locn msg
85 = ErrMsg [locn] alwaysQualify msg empty
87 -- A long (multi-line) error message, with context to tell us whether
88 -- to qualify names in the message or not.
89 mkLongErrMsg :: SrcSpan -> PrintUnqualified -> Message -> Message -> ErrMsg
90 mkLongErrMsg locn print_unqual msg extra
91 = ErrMsg [locn] print_unqual msg extra
93 mkWarnMsg :: SrcSpan -> PrintUnqualified -> Message -> WarnMsg
96 type Messages = (Bag WarnMsg, Bag ErrMsg)
98 emptyMessages :: Messages
99 emptyMessages = (emptyBag, emptyBag)
101 errorsFound :: DynFlags -> Messages -> Bool
102 -- The dyn-flags are used to see if the user has specified
103 -- -Werorr, which says that warnings should be fatal
104 errorsFound dflags (warns, errs)
105 | dopt Opt_WarnIsError dflags = not (isEmptyBag errs) || not (isEmptyBag warns)
106 | otherwise = not (isEmptyBag errs)
108 printErrorsAndWarnings :: Messages -> IO ()
109 printErrorsAndWarnings (warns, errs)
110 | no_errs && no_warns = return ()
111 | no_errs = printErrs (pprBagOfWarnings warns)
112 -- Don't print any warnings if there are errors
113 | otherwise = printErrs (pprBagOfErrors errs)
115 no_warns = isEmptyBag warns
116 no_errs = isEmptyBag errs
118 pprBagOfErrors :: Bag ErrMsg -> Pretty.Doc
119 pprBagOfErrors bag_of_errors
120 = Pretty.vcat [ let style = mkErrStyle unqual
121 doc = mkLocMessage s (d $$ e)
123 Pretty.text "" Pretty.$$ doc style
124 | ErrMsg { errMsgSpans = s:ss,
127 errMsgContext = unqual } <- sorted_errs ]
129 bag_ls = bagToList bag_of_errors
130 sorted_errs = sortLe occ'ed_before bag_ls
132 occ'ed_before err1 err2 =
133 case compare (head (errMsgSpans err1)) (head (errMsgSpans err2)) of
138 pprBagOfWarnings :: Bag WarnMsg -> Pretty.Doc
139 pprBagOfWarnings bag_of_warns = pprBagOfErrors bag_of_warns
143 ghcExit :: Int -> IO ()
145 | val == 0 = exitWith ExitSuccess
146 | otherwise = do errorMsg "\nCompilation had errors\n\n"
147 exitWith (ExitFailure val)
151 doIfSet :: Bool -> IO () -> IO ()
152 doIfSet flag action | flag = action
153 | otherwise = return ()
155 doIfSet_dyn :: DynFlags -> DynFlag -> IO () -> IO()
156 doIfSet_dyn dflags flag action | dopt flag dflags = action
157 | otherwise = return ()
161 showPass :: DynFlags -> String -> IO ()
162 showPass dflags what = compilationPassMsg dflags ("*** "++what++":")
164 dumpIfSet :: Bool -> String -> SDoc -> IO ()
165 dumpIfSet flag hdr doc
166 | not flag = return ()
167 | otherwise = printDump (mkDumpDoc hdr doc)
169 dumpIfSet_core :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
170 dumpIfSet_core dflags flag hdr doc
172 || verbosity dflags >= 4
173 || dopt Opt_D_verbose_core2core dflags = printDump (mkDumpDoc hdr doc)
174 | otherwise = return ()
176 dumpIfSet_dyn :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
177 dumpIfSet_dyn dflags flag hdr doc
178 | dopt flag dflags || verbosity dflags >= 4
179 = printDump (mkDumpDoc hdr doc)
183 dumpIfSet_dyn_or :: DynFlags -> [DynFlag] -> String -> SDoc -> IO ()
184 dumpIfSet_dyn_or dflags flags hdr doc
185 | or [dopt flag dflags | flag <- flags]
186 || verbosity dflags >= 4
187 = printDump (mkDumpDoc hdr doc)
188 | otherwise = return ()
192 line <+> text hdr <+> line,
196 line = text (replicate 20 '=')
198 -- -----------------------------------------------------------------------------
199 -- Outputting messages from the compiler
201 -- We want all messages to go through one place, so that we can
202 -- redirect them if necessary. For example, when GHC is used as a
203 -- library we might want to catch all messages that GHC tries to
204 -- output and do something else with them.
206 ifVerbose :: DynFlags -> Int -> IO () -> IO ()
207 ifVerbose dflags val act
208 | verbosity dflags >= val = act
209 | otherwise = return ()
211 errorMsg :: String -> IO ()
214 compilationProgressMsg :: DynFlags -> String -> IO ()
215 compilationProgressMsg dflags msg
216 = ifVerbose dflags 1 (putMsg msg)
218 compilationPassMsg :: DynFlags -> String -> IO ()
219 compilationPassMsg dflags msg
220 = ifVerbose dflags 2 (putMsg msg)
222 debugTraceMsg :: DynFlags -> Int -> String -> IO ()
223 debugTraceMsg dflags val msg
224 = ifVerbose dflags val (putMsg msg)
226 GLOBAL_VAR(msgHandler, hPutStrLn stderr, (String -> IO ()))
228 setMsgHandler :: (String -> IO ()) -> IO ()
229 setMsgHandler handle_msg = writeIORef msgHandler handle_msg
231 putMsg :: String -> IO ()
232 putMsg msg = do h <- readIORef msgHandler; h msg