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 )
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 in
124 Pretty.text "" Pretty.$$ d style Pretty.$$ e style
125 | ErrMsg { errMsgShortDoc = d,
127 errMsgContext = unqual } <- sorted_errs ]
129 bag_ls = bagToList bag_of_errors
130 sorted_errs = sortLt occ'ed_before bag_ls
132 occ'ed_before err1 err2 =
133 LT == compare (head (errMsgSpans err1)) (head (errMsgSpans err1))
135 pprBagOfWarnings :: Bag WarnMsg -> Pretty.Doc
136 pprBagOfWarnings bag_of_warns = pprBagOfErrors bag_of_warns
140 ghcExit :: Int -> IO ()
142 | val == 0 = exitWith ExitSuccess
143 | otherwise = do hPutStr stderr "\nCompilation had errors\n\n"
144 exitWith (ExitFailure val)
148 doIfSet :: Bool -> IO () -> IO ()
149 doIfSet flag action | flag = action
150 | otherwise = return ()
152 doIfSet_dyn :: DynFlags -> DynFlag -> IO () -> IO()
153 doIfSet_dyn dflags flag action | dopt flag dflags = action
154 | otherwise = return ()
158 showPass :: DynFlags -> String -> IO ()
160 | verbosity dflags >= 2 = hPutStr stderr ("*** "++what++":\n")
161 | otherwise = return ()
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 = if flag `elem` [Opt_D_dump_stix, Opt_D_dump_asm]
179 then printForC stdout (mkDumpDoc hdr doc)
180 else printDump (mkDumpDoc hdr doc)
184 dumpIfSet_dyn_or :: DynFlags -> [DynFlag] -> String -> SDoc -> IO ()
185 dumpIfSet_dyn_or dflags flags hdr doc
186 | or [dopt flag dflags | flag <- flags]
187 || verbosity dflags >= 4
188 = printDump (mkDumpDoc hdr doc)
189 | otherwise = return ()
193 line <+> text hdr <+> line,
197 line = text (replicate 20 '=')