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 List ( replicate )
31 import System ( ExitCode(..), exitWith )
32 import IO ( hPutStr, hPutStrLn, stderr, stdout )
36 type MsgWithLoc = (SrcLoc, SDoc)
38 type ErrMsg = MsgWithLoc
39 type WarnMsg = MsgWithLoc
42 addShortErrLocLine :: SrcLoc -> Message -> ErrMsg
43 addErrLocHdrLine :: SrcLoc -> Message -> Message -> ErrMsg
44 addWarnLocHdrLine :: SrcLoc -> Message -> Message -> ErrMsg
45 addShortWarnLocLine :: SrcLoc -> Message -> WarnMsg
47 addShortErrLocLine locn rest_of_err_msg
48 | isGoodSrcLoc locn = (locn, hang (ppr locn <> colon) 4
50 | otherwise = (locn, rest_of_err_msg)
52 addErrLocHdrLine locn hdr rest_of_err_msg
54 , hang (ppr locn <> colon<+> hdr)
58 addWarnLocHdrLine locn hdr rest_of_err_msg
60 , hang (ppr locn <> colon <+> ptext SLIT("Warning:") <+> hdr)
64 addShortWarnLocLine locn rest_of_err_msg
65 | isGoodSrcLoc locn = (locn, hang (ppr locn <> colon) 4
66 (ptext SLIT("Warning:") <+> rest_of_err_msg))
67 | otherwise = (locn, rest_of_err_msg)
69 dontAddErrLoc :: Message -> ErrMsg
70 dontAddErrLoc msg = (noSrcLoc, msg)
75 printError :: String -> IO ()
76 printError str = hPutStrLn stderr str
80 type Messages = (Bag WarnMsg, Bag ErrMsg)
82 errorsFound :: Messages -> Bool
83 errorsFound (warns, errs) = not (isEmptyBag errs)
85 warningsFound :: Messages -> Bool
86 warningsFound (warns, errs) = not (isEmptyBag warns)
88 printErrorsAndWarnings :: PrintUnqualified -> Messages -> IO ()
89 -- Don't print any warnings if there are errors
90 printErrorsAndWarnings unqual (warns, errs)
91 | no_errs && no_warns = return ()
92 | no_errs = printErrs unqual (pprBagOfWarnings warns)
93 | otherwise = printErrs unqual (pprBagOfErrors errs)
95 no_warns = isEmptyBag warns
96 no_errs = isEmptyBag errs
98 pprBagOfErrors :: Bag ErrMsg -> SDoc
99 pprBagOfErrors bag_of_errors
100 = vcat [text "" $$ p | (_,p) <- sorted_errs ]
102 bag_ls = bagToList bag_of_errors
103 sorted_errs = sortLt occ'ed_before bag_ls
105 occ'ed_before (a,_) (b,_) = LT == compare a b
107 pprBagOfWarnings :: Bag WarnMsg -> SDoc
108 pprBagOfWarnings bag_of_warns = pprBagOfErrors bag_of_warns
112 ghcExit :: Int -> IO ()
114 | val == 0 = exitWith ExitSuccess
115 | otherwise = do hPutStr stderr "\nCompilation had errors\n\n"
116 exitWith (ExitFailure val)
120 doIfSet :: Bool -> IO () -> IO ()
121 doIfSet flag action | flag = action
122 | otherwise = return ()
124 doIfSet_dyn :: DynFlags -> DynFlag -> IO () -> IO()
125 doIfSet_dyn dflags flag action | dopt flag dflags = action
126 | otherwise = return ()
130 showPass :: DynFlags -> String -> IO ()
132 | verbosity dflags >= 2 = hPutStr stderr ("*** "++what++":\n")
133 | otherwise = return ()
135 dumpIfSet :: Bool -> String -> SDoc -> IO ()
136 dumpIfSet flag hdr doc
137 | not flag = return ()
138 | otherwise = printDump (dump hdr doc)
140 dumpIfSet_core :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
141 dumpIfSet_core dflags flag hdr doc
143 || verbosity dflags >= 4
144 || dopt Opt_D_verbose_core2core dflags = printDump (dump hdr doc)
145 | otherwise = return ()
147 dumpIfSet_dyn :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
148 dumpIfSet_dyn dflags flag hdr doc
149 | dopt flag dflags || verbosity dflags >= 4
150 = if flag `elem` [Opt_D_dump_stix, Opt_D_dump_asm]
151 then printForC stdout (dump hdr doc)
152 else printDump (dump hdr doc)
156 dumpIfSet_dyn_or :: DynFlags -> [DynFlag] -> String -> SDoc -> IO ()
157 dumpIfSet_dyn_or dflags flags hdr doc
158 | or [dopt flag dflags | flag <- flags]
159 || verbosity dflags >= 4
160 = printDump (dump hdr doc)
161 | otherwise = return ()
165 line <+> text hdr <+> line,
169 line = text (replicate 20 '=')