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,
17 dumpIfSet, dumpIfSet_core, dumpIfSet_dyn, dumpIfSet_dyn_or,
21 #include "HsVersions.h"
23 import Bag ( Bag, bagToList, isEmptyBag )
24 import SrcLoc ( SrcLoc, noSrcLoc, isGoodSrcLoc )
25 import Util ( sortLt )
27 import CmdLineOpts ( DynFlags(..), DynFlag(..), dopt )
29 import System ( ExitCode(..), exitWith )
30 import IO ( hPutStr, stderr )
34 type MsgWithLoc = (SrcLoc, SDoc)
36 type ErrMsg = MsgWithLoc
37 type WarnMsg = MsgWithLoc
40 addShortErrLocLine :: SrcLoc -> Message -> ErrMsg
41 addErrLocHdrLine :: SrcLoc -> Message -> Message -> ErrMsg
42 addWarnLocHdrLine :: SrcLoc -> Message -> Message -> ErrMsg
43 addShortWarnLocLine :: SrcLoc -> Message -> WarnMsg
45 addShortErrLocLine locn rest_of_err_msg
46 | isGoodSrcLoc locn = (locn, hang (ppr locn <> colon) 4
48 | otherwise = (locn, rest_of_err_msg)
50 addErrLocHdrLine locn hdr rest_of_err_msg
52 , hang (ppr locn <> colon<+> hdr)
56 addWarnLocHdrLine locn hdr rest_of_err_msg
58 , hang (ppr locn <> colon <+> ptext SLIT("Warning:") <+> hdr)
62 addShortWarnLocLine locn rest_of_err_msg
63 | isGoodSrcLoc locn = (locn, hang (ppr locn <> colon) 4
64 (ptext SLIT("Warning:") <+> rest_of_err_msg))
65 | otherwise = (locn, rest_of_err_msg)
67 dontAddErrLoc :: Message -> ErrMsg
68 dontAddErrLoc msg = (noSrcLoc, msg)
74 type Messages = (Bag WarnMsg, Bag ErrMsg)
76 errorsFound :: Messages -> Bool
77 errorsFound (warns, errs) = not (isEmptyBag errs)
79 warningsFound :: Messages -> Bool
80 warningsFound (warns, errs) = not (isEmptyBag warns)
82 printErrorsAndWarnings :: PrintUnqualified -> Messages -> IO ()
83 -- Don't print any warnings if there are errors
84 printErrorsAndWarnings unqual (warns, errs)
85 | no_errs && no_warns = return ()
86 | no_errs = printErrs unqual (pprBagOfWarnings warns)
87 | otherwise = printErrs unqual (pprBagOfErrors errs)
89 no_warns = isEmptyBag warns
90 no_errs = isEmptyBag errs
92 pprBagOfErrors :: Bag ErrMsg -> SDoc
93 pprBagOfErrors bag_of_errors
94 = vcat [text "" $$ p | (_,p) <- sorted_errs ]
96 bag_ls = bagToList bag_of_errors
97 sorted_errs = sortLt occ'ed_before bag_ls
99 occ'ed_before (a,_) (b,_) = LT == compare a b
101 pprBagOfWarnings :: Bag WarnMsg -> SDoc
102 pprBagOfWarnings bag_of_warns = pprBagOfErrors bag_of_warns
106 ghcExit :: Int -> IO ()
108 | val == 0 = exitWith ExitSuccess
109 | otherwise = do hPutStr stderr "\nCompilation had errors\n\n"
110 exitWith (ExitFailure val)
114 doIfSet :: Bool -> IO () -> IO ()
115 doIfSet flag action | flag = action
116 | otherwise = return ()
118 doIfSet_dyn :: DynFlags -> DynFlag -> IO () -> IO()
119 doIfSet_dyn dflags flag action | dopt flag dflags = action
120 | otherwise = return ()
124 showPass :: DynFlags -> String -> IO ()
126 | verbosity dflags >= 2 = hPutStr stderr ("*** "++what++":\n")
127 | otherwise = return ()
129 dumpIfSet :: Bool -> String -> SDoc -> IO ()
130 dumpIfSet flag hdr doc
131 | not flag = return ()
132 | otherwise = printDump (dump hdr doc)
134 dumpIfSet_core :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
135 dumpIfSet_core dflags flag hdr doc
137 || verbosity dflags >= 4
138 || dopt Opt_D_verbose_core2core dflags = printDump (dump hdr doc)
139 | otherwise = return ()
141 dumpIfSet_dyn :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
142 dumpIfSet_dyn dflags flag hdr doc
143 | dopt flag dflags || verbosity dflags >= 4 = printDump (dump hdr doc)
144 | otherwise = return ()
146 dumpIfSet_dyn_or :: DynFlags -> [DynFlag] -> String -> SDoc -> IO ()
147 dumpIfSet_dyn_or dflags flags hdr doc
148 | or [dopt flag dflags | flag <- flags]
149 || verbosity dflags >= 4
150 = printDump (dump hdr doc)
151 | otherwise = return ()
155 line <+> text hdr <+> line,
159 line = text (take 20 (repeat '='))