2 % (c) The AQUA Project, Glasgow University, 1994-1998
4 \section[ErrsUtils]{Utilities for error reporting}
8 ErrMsg, WarnMsg, Message, Messages, errorsFound, warningsFound,
10 addShortErrLocLine, addShortWarnLocLine,
11 addErrLocHdrLine, addWarnLocHdrLine, dontAddErrLoc,
13 printErrorsAndWarnings, pprBagOfErrors, pprBagOfWarnings,
18 dumpIfSet, dumpIfSet_core, dumpIfSet_dyn, dumpIfSet_dyn_or,
22 #include "HsVersions.h"
24 import Bag ( Bag, bagToList, isEmptyBag )
25 import SrcLoc ( SrcLoc, noSrcLoc, isGoodSrcLoc )
26 import Util ( sortLt )
28 import CmdLineOpts ( DynFlags(..), DynFlag(..), dopt )
30 import System ( ExitCode(..), exitWith )
31 import IO ( hPutStr, hPutStrLn, stderr )
35 type MsgWithLoc = (SrcLoc, SDoc)
37 type ErrMsg = MsgWithLoc
38 type WarnMsg = MsgWithLoc
41 addShortErrLocLine :: SrcLoc -> Message -> ErrMsg
42 addErrLocHdrLine :: SrcLoc -> Message -> Message -> ErrMsg
43 addWarnLocHdrLine :: SrcLoc -> Message -> Message -> ErrMsg
44 addShortWarnLocLine :: SrcLoc -> Message -> WarnMsg
46 addShortErrLocLine locn rest_of_err_msg
47 | isGoodSrcLoc locn = (locn, hang (ppr locn <> colon) 4
49 | otherwise = (locn, rest_of_err_msg)
51 addErrLocHdrLine locn hdr rest_of_err_msg
53 , hang (ppr locn <> colon<+> hdr)
57 addWarnLocHdrLine locn hdr rest_of_err_msg
59 , hang (ppr locn <> colon <+> ptext SLIT("Warning:") <+> hdr)
63 addShortWarnLocLine locn rest_of_err_msg
64 | isGoodSrcLoc locn = (locn, hang (ppr locn <> colon) 4
65 (ptext SLIT("Warning:") <+> rest_of_err_msg))
66 | otherwise = (locn, rest_of_err_msg)
68 dontAddErrLoc :: Message -> ErrMsg
69 dontAddErrLoc msg = (noSrcLoc, msg)
74 printError :: String -> IO ()
75 printError str = hPutStrLn stderr str
79 type Messages = (Bag WarnMsg, Bag ErrMsg)
81 errorsFound :: Messages -> Bool
82 errorsFound (warns, errs) = not (isEmptyBag errs)
84 warningsFound :: Messages -> Bool
85 warningsFound (warns, errs) = not (isEmptyBag warns)
87 printErrorsAndWarnings :: PrintUnqualified -> Messages -> IO ()
88 -- Don't print any warnings if there are errors
89 printErrorsAndWarnings unqual (warns, errs)
90 | no_errs && no_warns = return ()
91 | no_errs = printErrs unqual (pprBagOfWarnings warns)
92 | otherwise = printErrs unqual (pprBagOfErrors errs)
94 no_warns = isEmptyBag warns
95 no_errs = isEmptyBag errs
97 pprBagOfErrors :: Bag ErrMsg -> SDoc
98 pprBagOfErrors bag_of_errors
99 = vcat [text "" $$ p | (_,p) <- sorted_errs ]
101 bag_ls = bagToList bag_of_errors
102 sorted_errs = sortLt occ'ed_before bag_ls
104 occ'ed_before (a,_) (b,_) = LT == compare a b
106 pprBagOfWarnings :: Bag WarnMsg -> SDoc
107 pprBagOfWarnings bag_of_warns = pprBagOfErrors bag_of_warns
111 ghcExit :: Int -> IO ()
113 | val == 0 = exitWith ExitSuccess
114 | otherwise = do hPutStr stderr "\nCompilation had errors\n\n"
115 exitWith (ExitFailure val)
119 doIfSet :: Bool -> IO () -> IO ()
120 doIfSet flag action | flag = action
121 | otherwise = return ()
123 doIfSet_dyn :: DynFlags -> DynFlag -> IO () -> IO()
124 doIfSet_dyn dflags flag action | dopt flag dflags = action
125 | otherwise = return ()
129 showPass :: DynFlags -> String -> IO ()
131 | verbosity dflags >= 2 = hPutStr stderr ("*** "++what++":\n")
132 | otherwise = return ()
134 dumpIfSet :: Bool -> String -> SDoc -> IO ()
135 dumpIfSet flag hdr doc
136 | not flag = return ()
137 | otherwise = printDump (dump hdr doc)
139 dumpIfSet_core :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
140 dumpIfSet_core dflags flag hdr doc
142 || verbosity dflags >= 4
143 || dopt Opt_D_verbose_core2core dflags = printDump (dump hdr doc)
144 | otherwise = return ()
146 dumpIfSet_dyn :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
147 dumpIfSet_dyn dflags flag hdr doc
148 | dopt flag dflags || verbosity dflags >= 4 = printDump (dump hdr doc)
149 | otherwise = return ()
151 dumpIfSet_dyn_or :: DynFlags -> [DynFlag] -> String -> SDoc -> IO ()
152 dumpIfSet_dyn_or dflags flags hdr doc
153 | or [dopt flag dflags | flag <- flags]
154 || verbosity dflags >= 4
155 = printDump (dump hdr doc)
156 | otherwise = return ()
160 line <+> text hdr <+> line,
164 line = text (take 20 (repeat '='))