[project @ 2000-11-15 14:37:08 by simonpj]
[ghc-hetmet.git] / ghc / compiler / main / ErrUtils.lhs
index d6a64f3..8267c93 100644 (file)
@@ -5,22 +5,24 @@
 
 \begin{code}
 module ErrUtils (
-       ErrMsg, WarnMsg, Message,
+       ErrMsg, WarnMsg, Message, Messages, errorsFound, warningsFound,
+
        addShortErrLocLine, addShortWarnLocLine,
-       addErrLocHdrLine,
-       dontAddErrLoc,
+       addErrLocHdrLine, dontAddErrLoc,
+
        printErrorsAndWarnings, pprBagOfErrors, pprBagOfWarnings,
+
        ghcExit,
-       doIfSet, dumpIfSet
+       doIfSet, doIfSet_dyn, dumpIfSet, dumpIfSet_dyn, showPass
     ) where
 
 #include "HsVersions.h"
 
 import Bag             ( Bag, bagToList, isEmptyBag )
-import SrcLoc          ( SrcLoc, noSrcLoc )
+import SrcLoc          ( SrcLoc, noSrcLoc, isGoodSrcLoc )
 import Util            ( sortLt )
 import Outputable
-import CmdLineOpts     ( DynFlags )
+import CmdLineOpts     ( DynFlags, DynFlag(..), dopt )
 
 import System          ( ExitCode(..), exitWith )
 import IO              ( hPutStr, stderr )
@@ -38,10 +40,9 @@ addErrLocHdrLine    :: SrcLoc -> Message -> Message -> ErrMsg
 addShortWarnLocLine :: SrcLoc -> Message -> WarnMsg
 
 addShortErrLocLine locn rest_of_err_msg
-  = ( locn
-    , hang (ppr locn <> colon) 
-         4 rest_of_err_msg
-    )
+  | isGoodSrcLoc locn = (locn, hang (ppr locn <> colon) 4 
+                                   rest_of_err_msg)
+  | otherwise        = (locn, rest_of_err_msg)
 
 addErrLocHdrLine locn hdr rest_of_err_msg
   = ( locn
@@ -50,23 +51,31 @@ addErrLocHdrLine locn hdr rest_of_err_msg
     )
 
 addShortWarnLocLine locn rest_of_err_msg
-  = ( locn
-    , hang (ppr locn <> colon)
-        4 (ptext SLIT("Warning:") <+> rest_of_err_msg)
-    )
+  | isGoodSrcLoc locn = (locn, hang (ppr locn <> colon) 4 
+                                   (ptext SLIT("Warning:") <+> rest_of_err_msg))
+  | otherwise        = (locn, rest_of_err_msg)
 
-dontAddErrLoc :: String -> Message -> ErrMsg
-dontAddErrLoc title rest_of_err_msg
- | null title = (noSrcLoc, rest_of_err_msg)
- | otherwise  =
-    ( noSrcLoc, hang (text title <> colon) 4 rest_of_err_msg )
+dontAddErrLoc :: Message -> ErrMsg
+dontAddErrLoc msg = (noSrcLoc, msg)
 
-printErrorsAndWarnings :: Bag ErrMsg -> Bag WarnMsg -> IO ()
+\end{code}
+
+
+\begin{code}
+type Messages = (Bag WarnMsg, Bag ErrMsg)
+
+errorsFound :: Messages -> Bool
+errorsFound (warns, errs) = not (isEmptyBag errs)
+
+warningsFound :: Messages -> Bool
+warningsFound (warns, errs) = not (isEmptyBag warns)
+
+printErrorsAndWarnings :: PrintUnqualified -> Messages -> IO ()
        -- Don't print any warnings if there are errors
-printErrorsAndWarnings errs warns
+printErrorsAndWarnings unqual (warns, errs)
   | no_errs && no_warns  = return ()
-  | no_errs             = printErrs (pprBagOfWarnings warns)
-  | otherwise           = printErrs (pprBagOfErrors   errs)
+  | no_errs             = printErrs unqual (pprBagOfWarnings warns)
+  | otherwise           = printErrs unqual (pprBagOfErrors   errs)
   where
     no_warns = isEmptyBag warns
     no_errs  = isEmptyBag errs
@@ -96,17 +105,33 @@ ghcExit val
 doIfSet :: Bool -> IO () -> IO ()
 doIfSet flag action | flag      = action
                    | otherwise = return ()
+
+doIfSet_dyn :: DynFlags -> DynFlag -> IO () -> IO()
+doIfSet_dyn dflags flag action | dopt flag dflags = action
+                              | otherwise        = return ()
 \end{code}
 
 \begin{code}
-dumpIfSet :: DynFlags -> (DynFlags -> Bool) -> String -> SDoc -> IO ()
-dumpIfSet dflags flag hdr doc
-  | not (flag dflags)  = return ()
-  | otherwise          = printDump dump
-  where
-    dump = vcat [text "", 
-                line <+> text hdr <+> line,
-                doc,
-                text ""]
-    line = text (take 20 (repeat '='))
+showPass :: DynFlags -> String -> IO ()
+showPass dflags what
+  | dopt Opt_D_show_passes dflags = hPutStr stderr ("*** "++what++":\n")
+  | otherwise                    = return ()
+
+dumpIfSet :: Bool -> String -> SDoc -> IO ()
+dumpIfSet flag hdr doc
+  | not flag   = return ()
+  | otherwise  = printDump (dump hdr doc)
+
+dumpIfSet_dyn :: DynFlags -> DynFlag -> String -> SDoc -> IO ()
+dumpIfSet_dyn dflags flag hdr doc
+  | not (dopt flag dflags)  = return ()
+  | otherwise               = printDump (dump hdr doc)
+
+dump hdr doc 
+   = vcat [text "", 
+          line <+> text hdr <+> line,
+          doc,
+          text ""]
+     where 
+        line = text (take 20 (repeat '='))
 \end{code}