X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;ds=sidebyside;f=ghc%2Fcompiler%2Fmain%2FErrUtils.lhs;h=b6d9bade5a5894d328baf518c8a8c1f8d1e79f31;hb=98dbad1214b2c49919802e4e51559604fcb21f9e;hp=dcf2934f7a9094882e0483033fc5c9700fbcad55;hpb=438596897ebbe25a07e1c82085cfbc5bdb00f09e;p=ghc-hetmet.git diff --git a/ghc/compiler/main/ErrUtils.lhs b/ghc/compiler/main/ErrUtils.lhs index dcf2934..b6d9bad 100644 --- a/ghc/compiler/main/ErrUtils.lhs +++ b/ghc/compiler/main/ErrUtils.lhs @@ -7,71 +7,117 @@ module ErrUtils ( ErrMsg, WarnMsg, Message, addShortErrLocLine, addShortWarnLocLine, + addErrLocHdrLine, dontAddErrLoc, - pprBagOfErrors, pprBagOfWarnings, + printErrorsAndWarnings, pprBagOfErrors, pprBagOfWarnings, ghcExit, - doIfSet, dumpIfSet + doIfSet, doIfSet_dyn, dumpIfSet, dumpIfSet_dyn ) where #include "HsVersions.h" -import Bag ( Bag, bagToList ) -import SrcLoc ( SrcLoc ) +import Bag ( Bag, bagToList, isEmptyBag ) +import SrcLoc ( SrcLoc, noSrcLoc ) +import Util ( sortLt ) import Outputable +import CmdLineOpts ( DynFlags, DynFlag, dopt ) + +import System ( ExitCode(..), exitWith ) +import IO ( hPutStr, stderr ) \end{code} \begin{code} -type ErrMsg = SDoc -type WarnMsg = SDoc +type MsgWithLoc = (SrcLoc, SDoc) + +type ErrMsg = MsgWithLoc +type WarnMsg = MsgWithLoc type Message = SDoc -addShortErrLocLine, addShortWarnLocLine :: SrcLoc -> ErrMsg -> ErrMsg +addShortErrLocLine :: SrcLoc -> Message -> ErrMsg +addErrLocHdrLine :: SrcLoc -> Message -> Message -> ErrMsg +addShortWarnLocLine :: SrcLoc -> Message -> WarnMsg addShortErrLocLine locn rest_of_err_msg - = hang (ppr locn <> colon) - 4 rest_of_err_msg + = ( locn + , hang (ppr locn <> colon) + 4 rest_of_err_msg + ) + +addErrLocHdrLine locn hdr rest_of_err_msg + = ( locn + , hang (ppr locn <> colon<+> hdr) + 4 rest_of_err_msg + ) addShortWarnLocLine locn rest_of_err_msg - = hang (ppr locn <> ptext SLIT(": Warning:")) - 4 rest_of_err_msg + = ( locn + , hang (ppr locn <> colon) + 4 (ptext SLIT("Warning:") <+> rest_of_err_msg) + ) -dontAddErrLoc :: String -> ErrMsg -> ErrMsg +dontAddErrLoc :: String -> Message -> ErrMsg dontAddErrLoc title rest_of_err_msg - = hang (hcat [text title, char ':']) - 4 rest_of_err_msg + | null title = (noSrcLoc, rest_of_err_msg) + | otherwise = + ( noSrcLoc, hang (text title <> colon) 4 rest_of_err_msg ) + +printErrorsAndWarnings :: (Bag WarnMsg, Bag ErrMsg) -> IO () + -- Don't print any warnings if there are errors +printErrorsAndWarnings (warns, errs) + | 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 [space $$ p | p <- bagToList bag_of_errors] + = vcat [text "" $$ p | (_,p) <- sorted_errs ] + where + bag_ls = bagToList bag_of_errors + sorted_errs = sortLt occ'ed_before bag_ls + + occ'ed_before (a,_) (b,_) = LT == compare a b -pprBagOfWarnings :: Bag ErrMsg -> SDoc +pprBagOfWarnings :: Bag WarnMsg -> SDoc pprBagOfWarnings bag_of_warns = pprBagOfErrors bag_of_warns \end{code} \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} 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 :: Bool -> String -> SDoc -> IO () dumpIfSet flag hdr doc - | not flag = return () - | otherwise = printDump dump - where - dump = vcat [text "", - line <+> text hdr <+> line, - doc, - text ""] - line = text (take 20 (repeat '=')) + | 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}