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 )
42 import DYNAMIC ( TyCon, mkTyCon, Typeable(..), mkTyConApp )
45 -- -----------------------------------------------------------------------------
46 -- Basic error messages: just render a message with a source location.
50 mkLocMessage :: SrcSpan -> Message -> Message
52 | opt_ErrorSpans = hang (ppr locn <> colon) 4 msg
53 | otherwise = hang (ppr (srcSpanStart locn) <> colon) 4 msg
54 -- always print the location, even if it is unhelpful. Error messages
55 -- are supposed to be in a standard format, and one without a location
56 -- would look strange. Better to say explicitly "<no location info>".
58 printError :: SrcSpan -> Message -> IO ()
59 printError span msg = printErrs (mkLocMessage span msg $ defaultErrStyle)
62 -- -----------------------------------------------------------------------------
63 -- Collecting up messages for later ordering and printing.
65 data ErrMsg = ErrMsg {
66 errMsgSpans :: [SrcSpan],
67 errMsgContext :: PrintUnqualified,
68 errMsgShortDoc :: Message,
69 errMsgExtraInfo :: Message
71 -- The SrcSpan is used for sorting errors into line-number order
72 -- NB Pretty.Doc not SDoc: we deal with the printing style (in ptic
73 -- whether to qualify an External Name) at the error occurrence
75 -- So we can throw these things as exceptions
77 errMsgTc = mkTyCon "ErrMsg"
78 instance Typeable ErrMsg where
79 typeOf _ = mkTyConApp errMsgTc []
83 -- A short (one-line) error message, with context to tell us whether
84 -- to qualify names in the message or not.
85 mkErrMsg :: SrcSpan -> PrintUnqualified -> Message -> ErrMsg
86 mkErrMsg locn print_unqual msg
87 = ErrMsg [locn] print_unqual msg empty
89 -- Variant that doesn't care about qualified/unqualified names
90 mkPlainErrMsg :: SrcSpan -> Message -> ErrMsg
91 mkPlainErrMsg locn msg
92 = ErrMsg [locn] alwaysQualify msg empty
94 -- A long (multi-line) error message, with context to tell us whether
95 -- to qualify names in the message or not.
96 mkLongErrMsg :: SrcSpan -> PrintUnqualified -> Message -> Message -> ErrMsg
97 mkLongErrMsg locn print_unqual msg extra
98 = ErrMsg [locn] print_unqual msg extra
100 mkWarnMsg :: SrcSpan -> PrintUnqualified -> Message -> WarnMsg
103 type Messages = (Bag WarnMsg, Bag ErrMsg)
105 emptyMessages :: Messages
106 emptyMessages = (emptyBag, emptyBag)
108 errorsFound :: DynFlags -> Messages -> Bool
109 -- The dyn-flags are used to see if the user has specified
110 -- -Werorr, which says that warnings should be fatal
111 errorsFound dflags (warns, errs)
112 | dopt Opt_WarnIsError dflags = not (isEmptyBag errs) || not (isEmptyBag warns)
113 | otherwise = not (isEmptyBag errs)
115 printErrorsAndWarnings :: Messages -> IO ()
116 printErrorsAndWarnings (warns, errs)
117 | no_errs && no_warns = return ()
118 | no_errs = printErrs (pprBagOfWarnings warns)
119 -- Don't print any warnings if there are errors
120 | otherwise = printErrs (pprBagOfErrors errs)
122 no_warns = isEmptyBag warns
123 no_errs = isEmptyBag errs
125 pprBagOfErrors :: Bag ErrMsg -> Pretty.Doc
126 pprBagOfErrors bag_of_errors
127 = Pretty.vcat [ let style = mkErrStyle unqual
128 doc = mkLocMessage s (d $$ e)
130 Pretty.text "" Pretty.$$ doc style
131 | ErrMsg { errMsgSpans = s:ss,
134 errMsgContext = unqual } <- sorted_errs ]
136 bag_ls = bagToList bag_of_errors
137 sorted_errs = sortLe occ'ed_before bag_ls
139 occ'ed_before err1 err2 =
140 case compare (head (errMsgSpans err1)) (head (errMsgSpans err2)) of
145 pprBagOfWarnings :: Bag WarnMsg -> Pretty.Doc
146 pprBagOfWarnings bag_of_warns = pprBagOfErrors bag_of_warns
150 ghcExit :: Int -> IO ()
152 | val == 0 = exitWith ExitSuccess
153 | otherwise = do errorMsg "\nCompilation had errors\n\n"
154 exitWith (ExitFailure val)
158 doIfSet :: Bool -> IO () -> IO ()
159 doIfSet flag action | flag = action
160 | otherwise = return ()
162 doIfSet_dyn :: DynFlags -> DynFlag -> IO () -> IO()
163 doIfSet_dyn dflags flag action | dopt flag dflags = action
164 | otherwise = return ()
168 showPass :: DynFlags -> String -> IO ()
169 showPass dflags what = compilationPassMsg dflags ("*** "++what++":")
171 dumpIfSet :: Bool -> String -> SDoc -> IO ()
172 dumpIfSet flag hdr doc
173 | not flag = return ()
174 | otherwise = printDump (mkDumpDoc hdr doc)
176 dumpIfSet_core :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
177 dumpIfSet_core dflags flag hdr doc
179 || verbosity dflags >= 4
180 || dopt Opt_D_verbose_core2core dflags = printDump (mkDumpDoc hdr doc)
181 | otherwise = return ()
183 dumpIfSet_dyn :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
184 dumpIfSet_dyn dflags flag hdr doc
185 | dopt flag dflags || verbosity dflags >= 4
186 = printDump (mkDumpDoc hdr doc)
190 dumpIfSet_dyn_or :: DynFlags -> [DynFlag] -> String -> SDoc -> IO ()
191 dumpIfSet_dyn_or dflags flags hdr doc
192 | or [dopt flag dflags | flag <- flags]
193 || verbosity dflags >= 4
194 = printDump (mkDumpDoc hdr doc)
195 | otherwise = return ()
199 line <+> text hdr <+> line,
203 line = text (replicate 20 '=')
205 -- -----------------------------------------------------------------------------
206 -- Outputting messages from the compiler
208 -- We want all messages to go through one place, so that we can
209 -- redirect them if necessary. For example, when GHC is used as a
210 -- library we might want to catch all messages that GHC tries to
211 -- output and do something else with them.
213 ifVerbose :: DynFlags -> Int -> IO () -> IO ()
214 ifVerbose dflags val act
215 | verbosity dflags >= val = act
216 | otherwise = return ()
218 errorMsg :: String -> IO ()
221 compilationProgressMsg :: DynFlags -> String -> IO ()
222 compilationProgressMsg dflags msg
223 = ifVerbose dflags 1 (putMsg msg)
225 compilationPassMsg :: DynFlags -> String -> IO ()
226 compilationPassMsg dflags msg
227 = ifVerbose dflags 2 (putMsg msg)
229 debugTraceMsg :: DynFlags -> Int -> String -> IO ()
230 debugTraceMsg dflags val msg
231 = ifVerbose dflags val (putMsg msg)
233 GLOBAL_VAR(msgHandler, hPutStrLn stderr, (String -> IO ()))
235 setMsgHandler :: (String -> IO ()) -> IO ()
236 setMsgHandler handle_msg = writeIORef msgHandler handle_msg
238 putMsg :: String -> IO ()
239 putMsg msg = do h <- readIORef msgHandler; h msg