[project @ 2001-03-13 12:50:29 by simonmar]
[ghc-hetmet.git] / ghc / compiler / main / ErrUtils.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1994-1998
3 %
4 \section[ErrsUtils]{Utilities for error reporting}
5
6 \begin{code}
7 module ErrUtils (
8         ErrMsg, WarnMsg, Message, Messages, errorsFound, warningsFound,
9
10         addShortErrLocLine, addShortWarnLocLine,
11         addErrLocHdrLine, addWarnLocHdrLine, dontAddErrLoc,
12
13         printErrorsAndWarnings, pprBagOfErrors, pprBagOfWarnings,
14
15         printError,
16         ghcExit,
17         doIfSet, doIfSet_dyn, 
18         dumpIfSet, dumpIfSet_core, dumpIfSet_dyn, dumpIfSet_dyn_or, 
19         showPass
20     ) where
21
22 #include "HsVersions.h"
23
24 import Bag              ( Bag, bagToList, isEmptyBag )
25 import SrcLoc           ( SrcLoc, noSrcLoc, isGoodSrcLoc )
26 import Util             ( sortLt )
27 import Outputable
28 import CmdLineOpts      ( DynFlags(..), DynFlag(..), dopt )
29
30 import System           ( ExitCode(..), exitWith )
31 import IO               ( hPutStr, hPutStrLn, stderr )
32 \end{code}
33
34 \begin{code}
35 type MsgWithLoc = (SrcLoc, SDoc)
36
37 type ErrMsg  = MsgWithLoc
38 type WarnMsg = MsgWithLoc
39 type Message = SDoc
40
41 addShortErrLocLine  :: SrcLoc -> Message -> ErrMsg
42 addErrLocHdrLine    :: SrcLoc -> Message -> Message -> ErrMsg
43 addWarnLocHdrLine    :: SrcLoc -> Message -> Message -> ErrMsg
44 addShortWarnLocLine :: SrcLoc -> Message -> WarnMsg
45
46 addShortErrLocLine locn rest_of_err_msg
47   | isGoodSrcLoc locn = (locn, hang (ppr locn <> colon) 4 
48                                     rest_of_err_msg)
49   | otherwise         = (locn, rest_of_err_msg)
50
51 addErrLocHdrLine locn hdr rest_of_err_msg
52   = ( locn
53     , hang (ppr locn <> colon<+> hdr) 
54          4 rest_of_err_msg
55     )
56
57 addWarnLocHdrLine locn hdr rest_of_err_msg
58   = ( locn
59     , hang (ppr locn <> colon <+> ptext SLIT("Warning:") <+> hdr) 
60          4 (rest_of_err_msg)
61     )
62
63 addShortWarnLocLine locn rest_of_err_msg
64   | isGoodSrcLoc locn = (locn, hang (ppr locn <> colon) 4 
65                                     (ptext SLIT("Warning:") <+> rest_of_err_msg))
66   | otherwise         = (locn, rest_of_err_msg)
67
68 dontAddErrLoc :: Message -> ErrMsg
69 dontAddErrLoc msg = (noSrcLoc, msg)
70
71 \end{code}
72
73 \begin{code}
74 printError :: String -> IO ()
75 printError str = hPutStrLn stderr str
76 \end{code}
77
78 \begin{code}
79 type Messages = (Bag WarnMsg, Bag ErrMsg)
80
81 errorsFound :: Messages -> Bool
82 errorsFound (warns, errs) = not (isEmptyBag errs)
83
84 warningsFound :: Messages -> Bool
85 warningsFound (warns, errs) = not (isEmptyBag warns)
86
87 printErrorsAndWarnings :: PrintUnqualified -> Messages -> IO ()
88         -- Don't print any warnings if there are errors
89 printErrorsAndWarnings unqual (warns, errs)
90   | no_errs && no_warns  = return ()
91   | no_errs              = printErrs unqual (pprBagOfWarnings warns)
92   | otherwise            = printErrs unqual (pprBagOfErrors   errs)
93   where
94     no_warns = isEmptyBag warns
95     no_errs  = isEmptyBag errs
96
97 pprBagOfErrors :: Bag ErrMsg -> SDoc
98 pprBagOfErrors bag_of_errors
99   = vcat [text "" $$ p | (_,p) <- sorted_errs ]
100     where
101       bag_ls      = bagToList bag_of_errors
102       sorted_errs = sortLt occ'ed_before bag_ls
103
104       occ'ed_before (a,_) (b,_) = LT == compare a b
105
106 pprBagOfWarnings :: Bag WarnMsg -> SDoc
107 pprBagOfWarnings bag_of_warns = pprBagOfErrors bag_of_warns
108 \end{code}
109
110 \begin{code}
111 ghcExit :: Int -> IO ()
112 ghcExit val
113   | val == 0  = exitWith ExitSuccess
114   | otherwise = do hPutStr stderr "\nCompilation had errors\n\n"
115                    exitWith (ExitFailure val)
116 \end{code}
117
118 \begin{code}
119 doIfSet :: Bool -> IO () -> IO ()
120 doIfSet flag action | flag      = action
121                     | otherwise = return ()
122
123 doIfSet_dyn :: DynFlags -> DynFlag -> IO () -> IO()
124 doIfSet_dyn dflags flag action | dopt flag dflags = action
125                                | otherwise        = return ()
126 \end{code}
127
128 \begin{code}
129 showPass :: DynFlags -> String -> IO ()
130 showPass dflags what
131   | verbosity dflags >= 2 = hPutStr stderr ("*** "++what++":\n")
132   | otherwise             = return ()
133
134 dumpIfSet :: Bool -> String -> SDoc -> IO ()
135 dumpIfSet flag hdr doc
136   | not flag   = return ()
137   | otherwise  = printDump (dump hdr doc)
138
139 dumpIfSet_core :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
140 dumpIfSet_core dflags flag hdr doc
141   | dopt flag dflags
142         || verbosity dflags >= 4
143         || dopt Opt_D_verbose_core2core dflags  = printDump (dump hdr doc)
144   | otherwise                                   = return ()
145
146 dumpIfSet_dyn :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
147 dumpIfSet_dyn dflags flag hdr doc
148   | dopt flag dflags || verbosity dflags >= 4 = printDump (dump hdr doc)
149   | otherwise                                 = return ()
150
151 dumpIfSet_dyn_or :: DynFlags -> [DynFlag] -> String -> SDoc -> IO ()
152 dumpIfSet_dyn_or dflags flags hdr doc
153   | or [dopt flag dflags | flag <- flags]
154   || verbosity dflags >= 4 
155   = printDump (dump hdr doc)
156   | otherwise = return ()
157
158 dump hdr doc 
159    = vcat [text "", 
160            line <+> text hdr <+> line,
161            doc,
162            text ""]
163      where 
164         line = text (take 20 (repeat '='))
165 \end{code}