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, 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 addShortWarnLocLine :: SrcLoc -> Message -> WarnMsg
42 addShortErrLocLine locn rest_of_err_msg
43 | isGoodSrcLoc locn = (locn, hang (ppr locn <> colon) 4
45 | otherwise = (locn, rest_of_err_msg)
47 addErrLocHdrLine locn hdr rest_of_err_msg
49 , hang (ppr locn <> colon<+> hdr)
53 addShortWarnLocLine locn rest_of_err_msg
54 | isGoodSrcLoc locn = (locn, hang (ppr locn <> colon) 4
55 (ptext SLIT("Warning:") <+> rest_of_err_msg))
56 | otherwise = (locn, rest_of_err_msg)
58 dontAddErrLoc :: Message -> ErrMsg
59 dontAddErrLoc msg = (noSrcLoc, msg)
65 type Messages = (Bag WarnMsg, Bag ErrMsg)
67 errorsFound :: Messages -> Bool
68 errorsFound (warns, errs) = not (isEmptyBag errs)
70 warningsFound :: Messages -> Bool
71 warningsFound (warns, errs) = not (isEmptyBag warns)
73 printErrorsAndWarnings :: PrintUnqualified -> Messages -> IO ()
74 -- Don't print any warnings if there are errors
75 printErrorsAndWarnings unqual (warns, errs)
76 | no_errs && no_warns = return ()
77 | no_errs = printErrs unqual (pprBagOfWarnings warns)
78 | otherwise = printErrs unqual (pprBagOfErrors errs)
80 no_warns = isEmptyBag warns
81 no_errs = isEmptyBag errs
83 pprBagOfErrors :: Bag ErrMsg -> SDoc
84 pprBagOfErrors bag_of_errors
85 = vcat [text "" $$ p | (_,p) <- sorted_errs ]
87 bag_ls = bagToList bag_of_errors
88 sorted_errs = sortLt occ'ed_before bag_ls
90 occ'ed_before (a,_) (b,_) = LT == compare a b
92 pprBagOfWarnings :: Bag WarnMsg -> SDoc
93 pprBagOfWarnings bag_of_warns = pprBagOfErrors bag_of_warns
97 ghcExit :: Int -> IO ()
99 | val == 0 = exitWith ExitSuccess
100 | otherwise = do hPutStr stderr "\nCompilation had errors\n\n"
101 exitWith (ExitFailure val)
105 doIfSet :: Bool -> IO () -> IO ()
106 doIfSet flag action | flag = action
107 | otherwise = return ()
109 doIfSet_dyn :: DynFlags -> DynFlag -> IO () -> IO()
110 doIfSet_dyn dflags flag action | dopt flag dflags = action
111 | otherwise = return ()
115 showPass :: DynFlags -> String -> IO ()
117 | verbosity dflags >= 2 = hPutStr stderr ("*** "++what++":\n")
118 | otherwise = return ()
120 dumpIfSet :: Bool -> String -> SDoc -> IO ()
121 dumpIfSet flag hdr doc
122 | not flag = return ()
123 | otherwise = printDump (dump hdr doc)
125 dumpIfSet_core :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
126 dumpIfSet_core dflags flag hdr doc
128 || verbosity dflags >= 4
129 || dopt Opt_D_verbose_core2core dflags = printDump (dump hdr doc)
130 | otherwise = return ()
132 dumpIfSet_dyn :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
133 dumpIfSet_dyn dflags flag hdr doc
134 | dopt flag dflags || verbosity dflags >= 4 = printDump (dump hdr doc)
135 | otherwise = return ()
139 line <+> text hdr <+> line,
143 line = text (take 20 (repeat '='))