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 CmdLineOpts ( DynFlags(..), DynFlag(..), dopt, opt_ErrorSpans )
38 import System ( ExitCode(..), exitWith )
40 import IO ( hPutStrLn, stderr )
43 -- -----------------------------------------------------------------------------
44 -- Basic error messages: just render a message with a source location.
48 mkLocMessage :: SrcSpan -> Message -> Message
50 | opt_ErrorSpans = hang (ppr locn <> colon) 4 msg
51 | otherwise = hang (ppr (srcSpanStart locn) <> colon) 4 msg
52 -- always print the location, even if it is unhelpful. Error messages
53 -- are supposed to be in a standard format, and one without a location
54 -- would look strange. Better to say explicitly "<no location info>".
56 printError :: SrcSpan -> Message -> IO ()
57 printError span msg = printErrs (mkLocMessage span msg $ defaultErrStyle)
60 -- -----------------------------------------------------------------------------
61 -- Collecting up messages for later ordering and printing.
63 data ErrMsg = ErrMsg {
64 errMsgSpans :: [SrcSpan],
65 errMsgContext :: PrintUnqualified,
66 errMsgShortDoc :: Message,
67 errMsgExtraInfo :: Message
69 -- The SrcSpan is used for sorting errors into line-number order
70 -- NB Pretty.Doc not SDoc: we deal with the printing style (in ptic
71 -- whether to qualify an External Name) at the error occurrence
75 -- A short (one-line) error message, with context to tell us whether
76 -- to qualify names in the message or not.
77 mkErrMsg :: SrcSpan -> PrintUnqualified -> Message -> ErrMsg
78 mkErrMsg locn print_unqual msg
79 = ErrMsg [locn] print_unqual msg empty
81 -- Variant that doesn't care about qualified/unqualified names
82 mkPlainErrMsg :: SrcSpan -> Message -> ErrMsg
83 mkPlainErrMsg locn msg
84 = ErrMsg [locn] alwaysQualify msg empty
86 -- A long (multi-line) error message, with context to tell us whether
87 -- to qualify names in the message or not.
88 mkLongErrMsg :: SrcSpan -> PrintUnqualified -> Message -> Message -> ErrMsg
89 mkLongErrMsg locn print_unqual msg extra
90 = ErrMsg [locn] print_unqual msg extra
92 mkWarnMsg :: SrcSpan -> PrintUnqualified -> Message -> WarnMsg
95 type Messages = (Bag WarnMsg, Bag ErrMsg)
97 emptyMessages :: Messages
98 emptyMessages = (emptyBag, emptyBag)
100 errorsFound :: DynFlags -> Messages -> Bool
101 -- The dyn-flags are used to see if the user has specified
102 -- -Werorr, which says that warnings should be fatal
103 errorsFound dflags (warns, errs)
104 | dopt Opt_WarnIsError dflags = not (isEmptyBag errs) || not (isEmptyBag warns)
105 | otherwise = not (isEmptyBag errs)
107 printErrorsAndWarnings :: Messages -> IO ()
108 printErrorsAndWarnings (warns, errs)
109 | no_errs && no_warns = return ()
110 | no_errs = printErrs (pprBagOfWarnings warns)
111 -- Don't print any warnings if there are errors
112 | otherwise = printErrs (pprBagOfErrors errs)
114 no_warns = isEmptyBag warns
115 no_errs = isEmptyBag errs
117 pprBagOfErrors :: Bag ErrMsg -> Pretty.Doc
118 pprBagOfErrors bag_of_errors
119 = Pretty.vcat [ let style = mkErrStyle unqual
120 doc = mkLocMessage s (d $$ e)
122 Pretty.text "" Pretty.$$ doc style
123 | ErrMsg { errMsgSpans = s:ss,
126 errMsgContext = unqual } <- sorted_errs ]
128 bag_ls = bagToList bag_of_errors
129 sorted_errs = sortLe occ'ed_before bag_ls
131 occ'ed_before err1 err2 =
132 case compare (head (errMsgSpans err1)) (head (errMsgSpans err2)) of
137 pprBagOfWarnings :: Bag WarnMsg -> Pretty.Doc
138 pprBagOfWarnings bag_of_warns = pprBagOfErrors bag_of_warns
142 ghcExit :: Int -> IO ()
144 | val == 0 = exitWith ExitSuccess
145 | otherwise = do errorMsg "\nCompilation had errors\n\n"
146 exitWith (ExitFailure val)
150 doIfSet :: Bool -> IO () -> IO ()
151 doIfSet flag action | flag = action
152 | otherwise = return ()
154 doIfSet_dyn :: DynFlags -> DynFlag -> IO () -> IO()
155 doIfSet_dyn dflags flag action | dopt flag dflags = action
156 | otherwise = return ()
160 showPass :: DynFlags -> String -> IO ()
161 showPass dflags what = compilationPassMsg dflags ("*** "++what++":")
163 dumpIfSet :: Bool -> String -> SDoc -> IO ()
164 dumpIfSet flag hdr doc
165 | not flag = return ()
166 | otherwise = printDump (mkDumpDoc hdr doc)
168 dumpIfSet_core :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
169 dumpIfSet_core dflags flag hdr doc
171 || verbosity dflags >= 4
172 || dopt Opt_D_verbose_core2core dflags = printDump (mkDumpDoc hdr doc)
173 | otherwise = return ()
175 dumpIfSet_dyn :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
176 dumpIfSet_dyn dflags flag hdr doc
177 | dopt flag dflags || verbosity dflags >= 4
178 = printDump (mkDumpDoc hdr doc)
182 dumpIfSet_dyn_or :: DynFlags -> [DynFlag] -> String -> SDoc -> IO ()
183 dumpIfSet_dyn_or dflags flags hdr doc
184 | or [dopt flag dflags | flag <- flags]
185 || verbosity dflags >= 4
186 = printDump (mkDumpDoc hdr doc)
187 | otherwise = return ()
191 line <+> text hdr <+> line,
195 line = text (replicate 20 '=')
197 -- -----------------------------------------------------------------------------
198 -- Outputting messages from the compiler
200 -- We want all messages to go through one place, so that we can
201 -- redirect them if necessary. For example, when GHC is used as a
202 -- library we might want to catch all messages that GHC tries to
203 -- output and do something else with them.
205 ifVerbose :: DynFlags -> Int -> IO () -> IO ()
206 ifVerbose dflags val act
207 | verbosity dflags >= val = act
208 | otherwise = return ()
210 errorMsg :: String -> IO ()
213 compilationProgressMsg :: DynFlags -> String -> IO ()
214 compilationProgressMsg dflags msg
215 = ifVerbose dflags 1 (putMsg msg)
217 compilationPassMsg :: DynFlags -> String -> IO ()
218 compilationPassMsg dflags msg
219 = ifVerbose dflags 2 (putMsg msg)
221 debugTraceMsg :: DynFlags -> String -> IO ()
222 debugTraceMsg dflags msg
223 = ifVerbose dflags 2 (putMsg msg)
225 GLOBAL_VAR(msgHandler, hPutStrLn stderr, (String -> IO ()))
227 setMsgHandler :: (String -> IO ()) -> IO ()
228 setMsgHandler handle_msg = writeIORef msgHandler handle_msg
230 putMsg :: String -> IO ()
231 putMsg msg = do h <- readIORef msgHandler; h msg