[project @ 2000-10-12 08:57:03 by sewardj]
[ghc-hetmet.git] / ghc / compiler / main / ErrUtils.lhs
index b461e4b..d6a64f3 100644 (file)
@@ -9,17 +9,21 @@ module ErrUtils (
        addShortErrLocLine, addShortWarnLocLine,
        addErrLocHdrLine,
        dontAddErrLoc,
-       pprBagOfErrors, pprBagOfWarnings,
+       printErrorsAndWarnings, pprBagOfErrors, pprBagOfWarnings,
        ghcExit,
        doIfSet, dumpIfSet
     ) where
 
 #include "HsVersions.h"
 
-import Bag             ( Bag, bagToList )
+import Bag             ( Bag, bagToList, isEmptyBag )
 import SrcLoc          ( SrcLoc, noSrcLoc )
 import Util            ( sortLt )
 import Outputable
+import CmdLineOpts     ( DynFlags )
+
+import System          ( ExitCode(..), exitWith )
+import IO              ( hPutStr, stderr )
 \end{code}
 
 \begin{code}
@@ -47,20 +51,29 @@ addErrLocHdrLine locn hdr rest_of_err_msg
 
 addShortWarnLocLine locn rest_of_err_msg
   = ( locn
-    , hang (ppr locn <> ptext SLIT(": Warning:")) 
-        4 rest_of_err_msg
+    , hang (ppr locn <> colon)
+        4 (ptext SLIT("Warning:") <+> rest_of_err_msg)
     )
 
 dontAddErrLoc :: String -> Message -> ErrMsg
 dontAddErrLoc title rest_of_err_msg
  | null title = (noSrcLoc, rest_of_err_msg)
  | otherwise  =
-    ( noSrcLoc, hang (hcat [text title, char ':'])
-                 4  rest_of_err_msg )
+    ( noSrcLoc, hang (text title <> colon) 4 rest_of_err_msg )
+
+printErrorsAndWarnings :: Bag ErrMsg -> Bag WarnMsg -> IO ()
+       -- Don't print any warnings if there are errors
+printErrorsAndWarnings errs warns
+  | no_errs && no_warns  = return ()
+  | no_errs             = printErrs (pprBagOfWarnings warns)
+  | otherwise           = printErrs (pprBagOfErrors   errs)
+  where
+    no_warns = isEmptyBag warns
+    no_errs  = isEmptyBag errs
 
 pprBagOfErrors :: Bag ErrMsg -> SDoc
 pprBagOfErrors bag_of_errors
-  = vcat [p $$ text "" | (_,p) <- sorted_errs ]
+  = vcat [text "" $$ p | (_,p) <- sorted_errs ]
     where
       bag_ls     = bagToList bag_of_errors
       sorted_errs = sortLt occ'ed_before bag_ls
@@ -73,11 +86,10 @@ pprBagOfWarnings bag_of_warns = pprBagOfErrors bag_of_warns
 
 \begin{code}
 ghcExit :: Int -> IO ()
-
 ghcExit val
-  = if val /= 0
-    then error "Compilation had errors\n"
-    else return ()
+  | val == 0  = exitWith ExitSuccess
+  | otherwise = do hPutStr stderr "\nCompilation had errors\n\n"
+                  exitWith (ExitFailure val)
 \end{code}
 
 \begin{code}
@@ -87,10 +99,10 @@ doIfSet flag action | flag      = action
 \end{code}
 
 \begin{code}
-dumpIfSet :: Bool -> String -> SDoc -> IO ()
-dumpIfSet flag hdr doc
-  | not flag  = return ()
-  | otherwise = printDump dump
+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,