2 % (c) The AQUA Project, Glasgow University, 1994-1998
4 \section[ErrsUtils]{Utilities for error reporting}
8 ErrMsg, WarnMsg, Message, Messages, errorsFound,
10 addShortErrLocLine, addShortWarnLocLine,
11 addErrLocHdrLine, dontAddErrLoc,
13 printErrorsAndWarnings, pprBagOfErrors, pprBagOfWarnings,
16 doIfSet, doIfSet_dyn, dumpIfSet, 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 printErrorsAndWarnings :: PrintUnqualified -> Messages -> IO ()
71 -- Don't print any warnings if there are errors
72 printErrorsAndWarnings unqual (warns, errs)
73 | no_errs && no_warns = return ()
74 | no_errs = printErrs unqual (pprBagOfWarnings warns)
75 | otherwise = printErrs unqual (pprBagOfErrors errs)
77 no_warns = isEmptyBag warns
78 no_errs = isEmptyBag errs
80 pprBagOfErrors :: Bag ErrMsg -> SDoc
81 pprBagOfErrors bag_of_errors
82 = vcat [text "" $$ p | (_,p) <- sorted_errs ]
84 bag_ls = bagToList bag_of_errors
85 sorted_errs = sortLt occ'ed_before bag_ls
87 occ'ed_before (a,_) (b,_) = LT == compare a b
89 pprBagOfWarnings :: Bag WarnMsg -> SDoc
90 pprBagOfWarnings bag_of_warns = pprBagOfErrors bag_of_warns
94 ghcExit :: Int -> IO ()
96 | val == 0 = exitWith ExitSuccess
97 | otherwise = do hPutStr stderr "\nCompilation had errors\n\n"
98 exitWith (ExitFailure val)
102 doIfSet :: Bool -> IO () -> IO ()
103 doIfSet flag action | flag = action
104 | otherwise = return ()
106 doIfSet_dyn :: DynFlags -> DynFlag -> IO () -> IO()
107 doIfSet_dyn dflags flag action | dopt flag dflags = action
108 | otherwise = return ()
112 showPass :: DynFlags -> String -> IO ()
114 | dopt Opt_D_show_passes dflags = hPutStr stderr ("*** "++what++":\n")
115 | otherwise = return ()
117 dumpIfSet :: Bool -> String -> SDoc -> IO ()
118 dumpIfSet flag hdr doc
119 | not flag = return ()
120 | otherwise = printDump (dump hdr doc)
122 dumpIfSet_dyn :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
123 dumpIfSet_dyn dflags flag hdr doc
124 | not (dopt flag dflags) = return ()
125 | otherwise = printDump (dump hdr doc)
129 line <+> text hdr <+> line,
133 line = text (take 20 (repeat '='))