[project @ 2004-01-23 13:55:28 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         Message, mkLocMessage, printError,
9
10         ErrMsg, WarnMsg,
11         errMsgSpans, errMsgContext, errMsgShortDoc, errMsgExtraInfo,
12         Messages, errorsFound, emptyMessages,
13         mkErrMsg, mkWarnMsg, mkPlainErrMsg, mkLongErrMsg,
14         printErrorsAndWarnings, pprBagOfErrors, pprBagOfWarnings,
15
16         ghcExit,
17         doIfSet, doIfSet_dyn, 
18         dumpIfSet, dumpIfSet_core, dumpIfSet_dyn, dumpIfSet_dyn_or, mkDumpDoc,
19         showPass
20     ) where
21
22 #include "HsVersions.h"
23
24 import Bag              ( Bag, bagToList, isEmptyBag, emptyBag )
25 import SrcLoc           ( SrcSpan )
26 import Util             ( sortLt )
27 import Outputable
28 import qualified Pretty
29 import SrcLoc           ( srcSpanStart )
30 import CmdLineOpts      ( DynFlags(..), DynFlag(..), dopt,
31                           opt_ErrorSpans )
32
33 import List             ( replicate )
34 import System           ( ExitCode(..), exitWith )
35 import IO               ( hPutStr, stderr, stdout )
36
37
38 -- -----------------------------------------------------------------------------
39 -- Basic error messages: just render a message with a source location.
40
41 type Message = SDoc
42
43 mkLocMessage :: SrcSpan -> Message -> Message
44 mkLocMessage locn msg
45   | opt_ErrorSpans = hang (ppr locn <> colon) 4 msg
46   | otherwise      = hang (ppr (srcSpanStart locn) <> colon) 4 msg
47   -- always print the location, even if it is unhelpful.  Error messages
48   -- are supposed to be in a standard format, and one without a location
49   -- would look strange.  Better to say explicitly "<no location info>".
50
51 printError :: SrcSpan -> Message -> IO ()
52 printError span msg = printErrs (mkLocMessage span msg $ defaultErrStyle)
53
54
55 -- -----------------------------------------------------------------------------
56 -- Collecting up messages for later ordering and printing.
57
58 data ErrMsg = ErrMsg { 
59         errMsgSpans     :: [SrcSpan],
60         errMsgContext   :: PrintUnqualified,
61         errMsgShortDoc  :: Message,
62         errMsgExtraInfo :: Message
63         }
64         -- The SrcSpan is used for sorting errors into line-number order
65         -- NB  Pretty.Doc not SDoc: we deal with the printing style (in ptic 
66         -- whether to qualify an External Name) at the error occurrence
67
68 type WarnMsg = ErrMsg
69
70 -- A short (one-line) error message, with context to tell us whether
71 -- to qualify names in the message or not.
72 mkErrMsg :: SrcSpan -> PrintUnqualified -> Message -> ErrMsg
73 mkErrMsg locn print_unqual msg
74   = ErrMsg [locn] print_unqual msg empty
75
76 -- Variant that doesn't care about qualified/unqualified names
77 mkPlainErrMsg :: SrcSpan -> Message -> ErrMsg
78 mkPlainErrMsg locn msg
79   = ErrMsg [locn] alwaysQualify msg empty
80
81 -- A long (multi-line) error message, with context to tell us whether
82 -- to qualify names in the message or not.
83 mkLongErrMsg :: SrcSpan -> PrintUnqualified -> Message -> Message -> ErrMsg
84 mkLongErrMsg locn print_unqual msg extra 
85  = ErrMsg [locn] print_unqual msg extra
86
87 -- A long (multi-line) error message, with context to tell us whether
88 -- to qualify names in the message or not.
89 mkLongMultiLocErrMsg :: [SrcSpan] -> PrintUnqualified -> Message -> Message -> ErrMsg
90 mkLongMultiLocErrMsg locns print_unqual msg extra
91   = ErrMsg locns print_unqual msg extra
92
93 mkWarnMsg :: SrcSpan -> PrintUnqualified -> Message -> WarnMsg
94 mkWarnMsg = mkErrMsg
95
96 mkLongWarnMsg :: SrcSpan -> PrintUnqualified -> Message -> Message -> WarnMsg
97 mkLongWarnMsg = mkLongErrMsg
98
99 type Messages = (Bag WarnMsg, Bag ErrMsg)
100
101 emptyMessages :: Messages
102 emptyMessages = (emptyBag, emptyBag)
103
104 errorsFound :: DynFlags -> Messages -> Bool
105 -- The dyn-flags are used to see if the user has specified
106 -- -Werorr, which says that warnings should be fatal
107 errorsFound dflags (warns, errs) 
108   | dopt Opt_WarnIsError dflags = not (isEmptyBag errs) || not (isEmptyBag warns)
109   | otherwise                   = not (isEmptyBag errs)
110
111 printErrorsAndWarnings :: Messages -> IO ()
112 printErrorsAndWarnings (warns, errs)
113   | no_errs && no_warns  = return ()
114   | no_errs              = printErrs (pprBagOfWarnings warns)
115                             -- Don't print any warnings if there are errors
116   | otherwise            = printErrs (pprBagOfErrors   errs)
117   where
118     no_warns = isEmptyBag warns
119     no_errs  = isEmptyBag errs
120
121 pprBagOfErrors :: Bag ErrMsg -> Pretty.Doc
122 pprBagOfErrors bag_of_errors
123   = Pretty.vcat [ let style = mkErrStyle unqual in
124                   Pretty.text "" Pretty.$$ d style Pretty.$$ e style
125                 | ErrMsg { errMsgShortDoc = d,
126                            errMsgExtraInfo = e,
127                            errMsgContext = unqual } <- sorted_errs ]
128     where
129       bag_ls      = bagToList bag_of_errors
130       sorted_errs = sortLt occ'ed_before bag_ls
131
132       occ'ed_before err1 err2 = 
133          LT == compare (head (errMsgSpans err1)) (head (errMsgSpans err1))
134
135 pprBagOfWarnings :: Bag WarnMsg -> Pretty.Doc
136 pprBagOfWarnings bag_of_warns = pprBagOfErrors bag_of_warns
137 \end{code}
138
139 \begin{code}
140 ghcExit :: Int -> IO ()
141 ghcExit val
142   | val == 0  = exitWith ExitSuccess
143   | otherwise = do hPutStr stderr "\nCompilation had errors\n\n"
144                    exitWith (ExitFailure val)
145 \end{code}
146
147 \begin{code}
148 doIfSet :: Bool -> IO () -> IO ()
149 doIfSet flag action | flag      = action
150                     | otherwise = return ()
151
152 doIfSet_dyn :: DynFlags -> DynFlag -> IO () -> IO()
153 doIfSet_dyn dflags flag action | dopt flag dflags = action
154                                | otherwise        = return ()
155 \end{code}
156
157 \begin{code}
158 showPass :: DynFlags -> String -> IO ()
159 showPass dflags what
160   | verbosity dflags >= 2 = hPutStr stderr ("*** "++what++":\n")
161   | otherwise             = return ()
162
163 dumpIfSet :: Bool -> String -> SDoc -> IO ()
164 dumpIfSet flag hdr doc
165   | not flag   = return ()
166   | otherwise  = printDump (mkDumpDoc hdr doc)
167
168 dumpIfSet_core :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
169 dumpIfSet_core dflags flag hdr doc
170   | dopt flag dflags
171         || verbosity dflags >= 4
172         || dopt Opt_D_verbose_core2core dflags  = printDump (mkDumpDoc hdr doc)
173   | otherwise                                   = return ()
174
175 dumpIfSet_dyn :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
176 dumpIfSet_dyn dflags flag hdr doc
177   | dopt flag dflags || verbosity dflags >= 4 
178   = if   flag `elem` [Opt_D_dump_stix, Opt_D_dump_asm]
179     then printForC stdout (mkDumpDoc hdr doc)
180     else printDump (mkDumpDoc hdr doc)
181   | otherwise
182   = return ()
183
184 dumpIfSet_dyn_or :: DynFlags -> [DynFlag] -> String -> SDoc -> IO ()
185 dumpIfSet_dyn_or dflags flags hdr doc
186   | or [dopt flag dflags | flag <- flags]
187   || verbosity dflags >= 4 
188   = printDump (mkDumpDoc hdr doc)
189   | otherwise = return ()
190
191 mkDumpDoc hdr doc 
192    = vcat [text "", 
193            line <+> text hdr <+> line,
194            doc,
195            text ""]
196      where 
197         line = text (replicate 20 '=')
198 \end{code}