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,
22 #include "HsVersions.h"
24 import Bag ( Bag, bagToList, isEmptyBag, emptyBag )
25 import SrcLoc ( SrcSpan )
26 import Util ( sortLt )
28 import qualified Pretty
29 import SrcLoc ( srcSpanStart )
30 import CmdLineOpts ( DynFlags(..), DynFlag(..), dopt,
33 import List ( replicate, sortBy )
34 import System ( ExitCode(..), exitWith )
35 import IO ( hPutStr, stderr, stdout )
38 -- -----------------------------------------------------------------------------
39 -- Basic error messages: just render a message with a source location.
43 mkLocMessage :: SrcSpan -> Message -> Message
45 | opt_ErrorSpans = hang (ppr locn <> colon) 4 msg
46 | otherwise = hang (ppr (srcSpanStart locn) <> colon) 4 msg
47 -- always print the location, even if it is unhelpful. Error messages
48 -- are supposed to be in a standard format, and one without a location
49 -- would look strange. Better to say explicitly "<no location info>".
51 printError :: SrcSpan -> Message -> IO ()
52 printError span msg = printErrs (mkLocMessage span msg $ defaultErrStyle)
55 -- -----------------------------------------------------------------------------
56 -- Collecting up messages for later ordering and printing.
58 data ErrMsg = ErrMsg {
59 errMsgSpans :: [SrcSpan],
60 errMsgContext :: PrintUnqualified,
61 errMsgShortDoc :: Message,
62 errMsgExtraInfo :: Message
64 -- The SrcSpan is used for sorting errors into line-number order
65 -- NB Pretty.Doc not SDoc: we deal with the printing style (in ptic
66 -- whether to qualify an External Name) at the error occurrence
70 -- A short (one-line) error message, with context to tell us whether
71 -- to qualify names in the message or not.
72 mkErrMsg :: SrcSpan -> PrintUnqualified -> Message -> ErrMsg
73 mkErrMsg locn print_unqual msg
74 = ErrMsg [locn] print_unqual msg empty
76 -- Variant that doesn't care about qualified/unqualified names
77 mkPlainErrMsg :: SrcSpan -> Message -> ErrMsg
78 mkPlainErrMsg locn msg
79 = ErrMsg [locn] alwaysQualify msg empty
81 -- A long (multi-line) error message, with context to tell us whether
82 -- to qualify names in the message or not.
83 mkLongErrMsg :: SrcSpan -> PrintUnqualified -> Message -> Message -> ErrMsg
84 mkLongErrMsg locn print_unqual msg extra
85 = ErrMsg [locn] print_unqual msg extra
87 -- A long (multi-line) error message, with context to tell us whether
88 -- to qualify names in the message or not.
89 mkLongMultiLocErrMsg :: [SrcSpan] -> PrintUnqualified -> Message -> Message -> ErrMsg
90 mkLongMultiLocErrMsg locns print_unqual msg extra
91 = ErrMsg locns print_unqual msg extra
93 mkWarnMsg :: SrcSpan -> PrintUnqualified -> Message -> WarnMsg
96 mkLongWarnMsg :: SrcSpan -> PrintUnqualified -> Message -> Message -> WarnMsg
97 mkLongWarnMsg = mkLongErrMsg
99 type Messages = (Bag WarnMsg, Bag ErrMsg)
101 emptyMessages :: Messages
102 emptyMessages = (emptyBag, emptyBag)
104 errorsFound :: DynFlags -> Messages -> Bool
105 -- The dyn-flags are used to see if the user has specified
106 -- -Werorr, which says that warnings should be fatal
107 errorsFound dflags (warns, errs)
108 | dopt Opt_WarnIsError dflags = not (isEmptyBag errs) || not (isEmptyBag warns)
109 | otherwise = not (isEmptyBag errs)
111 printErrorsAndWarnings :: Messages -> IO ()
112 printErrorsAndWarnings (warns, errs)
113 | no_errs && no_warns = return ()
114 | no_errs = printErrs (pprBagOfWarnings warns)
115 -- Don't print any warnings if there are errors
116 | otherwise = printErrs (pprBagOfErrors errs)
118 no_warns = isEmptyBag warns
119 no_errs = isEmptyBag errs
121 pprBagOfErrors :: Bag ErrMsg -> Pretty.Doc
122 pprBagOfErrors bag_of_errors
123 = Pretty.vcat [ let style = mkErrStyle unqual
124 doc = mkLocMessage s (d $$ e)
126 Pretty.text "" Pretty.$$ doc style
127 | ErrMsg { errMsgSpans = s:ss,
130 errMsgContext = unqual } <- sorted_errs ]
132 bag_ls = bagToList bag_of_errors
133 sorted_errs = sortLt occ'ed_before bag_ls
135 occ'ed_before err1 err2 =
136 LT == compare (head (errMsgSpans err1)) (head (errMsgSpans err2))
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 hPutStr stderr "\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 ()
163 | verbosity dflags >= 2 = hPutStr stderr ("*** "++what++":\n")
164 | otherwise = return ()
166 dumpIfSet :: Bool -> String -> SDoc -> IO ()
167 dumpIfSet flag hdr doc
168 | not flag = return ()
169 | otherwise = printDump (mkDumpDoc hdr doc)
171 dumpIfSet_core :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
172 dumpIfSet_core dflags flag hdr doc
174 || verbosity dflags >= 4
175 || dopt Opt_D_verbose_core2core dflags = printDump (mkDumpDoc hdr doc)
176 | otherwise = return ()
178 dumpIfSet_dyn :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
179 dumpIfSet_dyn dflags flag hdr doc
180 | dopt flag dflags || verbosity dflags >= 4
181 = if flag `elem` [Opt_D_dump_stix, Opt_D_dump_asm]
182 then printForC stdout (mkDumpDoc hdr doc)
183 else printDump (mkDumpDoc hdr doc)
187 dumpIfSet_dyn_or :: DynFlags -> [DynFlag] -> String -> SDoc -> IO ()
188 dumpIfSet_dyn_or dflags flags hdr doc
189 | or [dopt flag dflags | flag <- flags]
190 || verbosity dflags >= 4
191 = printDump (mkDumpDoc hdr doc)
192 | otherwise = return ()
196 line <+> text hdr <+> line,
200 line = text (replicate 20 '=')