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,
16 doIfSet, doIfSet_dyn, dumpIfSet, dumpIfSet_core, dumpIfSet_dyn, showPass
19 #include "HsVersions.h"
21 import Bag ( Bag, bagToList, isEmptyBag )
22 import SrcLoc ( SrcLoc, noSrcLoc, isGoodSrcLoc )
23 import Util ( sortLt )
25 import CmdLineOpts ( DynFlags(..), DynFlag(..), dopt )
27 import System ( ExitCode(..), exitWith )
28 import IO ( hPutStr, stderr )
32 type MsgWithLoc = (SrcLoc, SDoc)
34 type ErrMsg = MsgWithLoc
35 type WarnMsg = MsgWithLoc
38 addShortErrLocLine :: SrcLoc -> Message -> ErrMsg
39 addErrLocHdrLine :: SrcLoc -> Message -> Message -> ErrMsg
40 addWarnLocHdrLine :: SrcLoc -> Message -> Message -> ErrMsg
41 addShortWarnLocLine :: SrcLoc -> Message -> WarnMsg
43 addShortErrLocLine locn rest_of_err_msg
44 | isGoodSrcLoc locn = (locn, hang (ppr locn <> colon) 4
46 | otherwise = (locn, rest_of_err_msg)
48 addErrLocHdrLine locn hdr rest_of_err_msg
50 , hang (ppr locn <> colon<+> hdr)
54 addWarnLocHdrLine locn hdr rest_of_err_msg
56 , hang (ppr locn <> colon <+> ptext SLIT("Warning:") <+> hdr)
60 addShortWarnLocLine locn rest_of_err_msg
61 | isGoodSrcLoc locn = (locn, hang (ppr locn <> colon) 4
62 (ptext SLIT("Warning:") <+> rest_of_err_msg))
63 | otherwise = (locn, rest_of_err_msg)
65 dontAddErrLoc :: Message -> ErrMsg
66 dontAddErrLoc msg = (noSrcLoc, msg)
72 type Messages = (Bag WarnMsg, Bag ErrMsg)
74 errorsFound :: Messages -> Bool
75 errorsFound (warns, errs) = not (isEmptyBag errs)
77 warningsFound :: Messages -> Bool
78 warningsFound (warns, errs) = not (isEmptyBag warns)
80 printErrorsAndWarnings :: PrintUnqualified -> Messages -> IO ()
81 -- Don't print any warnings if there are errors
82 printErrorsAndWarnings unqual (warns, errs)
83 | no_errs && no_warns = return ()
84 | no_errs = printErrs unqual (pprBagOfWarnings warns)
85 | otherwise = printErrs unqual (pprBagOfErrors errs)
87 no_warns = isEmptyBag warns
88 no_errs = isEmptyBag errs
90 pprBagOfErrors :: Bag ErrMsg -> SDoc
91 pprBagOfErrors bag_of_errors
92 = vcat [text "" $$ p | (_,p) <- sorted_errs ]
94 bag_ls = bagToList bag_of_errors
95 sorted_errs = sortLt occ'ed_before bag_ls
97 occ'ed_before (a,_) (b,_) = LT == compare a b
99 pprBagOfWarnings :: Bag WarnMsg -> SDoc
100 pprBagOfWarnings bag_of_warns = pprBagOfErrors bag_of_warns
104 ghcExit :: Int -> IO ()
106 | val == 0 = exitWith ExitSuccess
107 | otherwise = do hPutStr stderr "\nCompilation had errors\n\n"
108 exitWith (ExitFailure val)
112 doIfSet :: Bool -> IO () -> IO ()
113 doIfSet flag action | flag = action
114 | otherwise = return ()
116 doIfSet_dyn :: DynFlags -> DynFlag -> IO () -> IO()
117 doIfSet_dyn dflags flag action | dopt flag dflags = action
118 | otherwise = return ()
122 showPass :: DynFlags -> String -> IO ()
124 | verbosity dflags >= 2 = hPutStr stderr ("*** "++what++":\n")
125 | otherwise = return ()
127 dumpIfSet :: Bool -> String -> SDoc -> IO ()
128 dumpIfSet flag hdr doc
129 | not flag = return ()
130 | otherwise = printDump (dump hdr doc)
132 dumpIfSet_core :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
133 dumpIfSet_core dflags flag hdr doc
135 || verbosity dflags >= 4
136 || dopt Opt_D_verbose_core2core dflags = printDump (dump hdr doc)
137 | otherwise = return ()
139 dumpIfSet_dyn :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
140 dumpIfSet_dyn dflags flag hdr doc
141 | dopt flag dflags || verbosity dflags >= 4 = printDump (dump hdr doc)
142 | otherwise = return ()
146 line <+> text hdr <+> line,
150 line = text (take 20 (repeat '='))