-\begin{code}
-type Error = PprStyle -> Doc
-type Warning = PprStyle -> Doc
-type Message = PprStyle -> Doc
-
-addErrLoc :: SrcLoc -> String -> Error -> Error
-addErrLoc locn title rest_of_err_msg sty
- = hang (hcat [ppr (PprForUser opt_PprUserLength) locn,
- if null title then empty else text (": " ++ title),
- char ':'])
- 4 (rest_of_err_msg sty)
-
-addShortErrLocLine, addShortWarnLocLine :: SrcLoc -> Error -> Error
-
-addShortErrLocLine locn rest_of_err_msg sty
- = hang ((<>) (ppr (PprForUser opt_PprUserLength) locn) (char ':'))
- 4 (rest_of_err_msg sty)
-
-addShortWarnLocLine locn rest_of_err_msg sty
- = hang ((<>) (ppr (PprForUser opt_PprUserLength) locn) (ptext SLIT(":warning:")))
- 4 (rest_of_err_msg sty)
-
-dontAddErrLoc :: String -> Error -> Error
-dontAddErrLoc title rest_of_err_msg sty
- = hang (hcat [text title, char ':'])
- 4 (rest_of_err_msg sty)
-
-pprBagOfErrors :: PprStyle -> Bag Error -> Doc
-pprBagOfErrors sty bag_of_errors
- = let pretties = map ( \ e -> e sty ) (bagToList bag_of_errors)
- in
- vcat (map (\ p -> ($$) space p) pretties)
+-- -----------------------------------------------------------------------------
+-- Basic error messages: just render a message with a source location.
+
+type Message = SDoc
+
+data Severity
+ = SevInfo
+ | SevWarning
+ | SevError
+ | SevFatal
+
+mkLocMessage :: SrcSpan -> Message -> Message
+mkLocMessage locn msg
+ | opt_ErrorSpans = hang (ppr locn <> colon) 4 msg
+ | otherwise = hang (ppr (srcSpanStart locn) <> colon) 4 msg
+ -- always print the location, even if it is unhelpful. Error messages
+ -- are supposed to be in a standard format, and one without a location
+ -- would look strange. Better to say explicitly "<no location info>".
+
+printError :: SrcSpan -> Message -> IO ()
+printError span msg = printErrs (mkLocMessage span msg $ defaultErrStyle)
+
+
+-- -----------------------------------------------------------------------------
+-- Collecting up messages for later ordering and printing.
+
+data ErrMsg = ErrMsg {
+ errMsgSpans :: [SrcSpan],
+ errMsgContext :: PrintUnqualified,
+ errMsgShortDoc :: Message,
+ errMsgExtraInfo :: Message
+ }
+ -- The SrcSpan is used for sorting errors into line-number order
+ -- NB Pretty.Doc not SDoc: we deal with the printing style (in ptic
+ -- whether to qualify an External Name) at the error occurrence
+
+-- So we can throw these things as exceptions
+errMsgTc :: TyCon
+errMsgTc = mkTyCon "ErrMsg"
+{-# NOINLINE errMsgTc #-}
+instance Typeable ErrMsg where
+#if __GLASGOW_HASKELL__ < 603
+ typeOf _ = mkAppTy errMsgTc []
+#else
+ typeOf _ = mkTyConApp errMsgTc []
+#endif
+
+type WarnMsg = ErrMsg
+
+-- A short (one-line) error message, with context to tell us whether
+-- to qualify names in the message or not.
+mkErrMsg :: SrcSpan -> PrintUnqualified -> Message -> ErrMsg
+mkErrMsg locn print_unqual msg
+ = ErrMsg [locn] print_unqual msg empty
+
+-- Variant that doesn't care about qualified/unqualified names
+mkPlainErrMsg :: SrcSpan -> Message -> ErrMsg
+mkPlainErrMsg locn msg
+ = ErrMsg [locn] alwaysQualify msg empty
+
+-- A long (multi-line) error message, with context to tell us whether
+-- to qualify names in the message or not.
+mkLongErrMsg :: SrcSpan -> PrintUnqualified -> Message -> Message -> ErrMsg
+mkLongErrMsg locn print_unqual msg extra
+ = ErrMsg [locn] print_unqual msg extra
+
+mkWarnMsg :: SrcSpan -> PrintUnqualified -> Message -> WarnMsg
+mkWarnMsg = mkErrMsg
+
+type Messages = (Bag WarnMsg, Bag ErrMsg)
+
+emptyMessages :: Messages
+emptyMessages = (emptyBag, emptyBag)
+
+errorsFound :: DynFlags -> Messages -> Bool
+-- The dyn-flags are used to see if the user has specified
+-- -Werorr, which says that warnings should be fatal
+errorsFound dflags (warns, errs)
+ | dopt Opt_WarnIsError dflags = not (isEmptyBag errs) || not (isEmptyBag warns)
+ | otherwise = not (isEmptyBag errs)
+
+printErrorsAndWarnings :: DynFlags -> Messages -> IO ()
+printErrorsAndWarnings dflags (warns, errs)
+ | no_errs && no_warns = return ()
+ | no_errs = printBagOfWarnings dflags warns
+ -- Don't print any warnings if there are errors
+ | otherwise = printBagOfErrors dflags errs
+ where
+ no_warns = isEmptyBag warns
+ no_errs = isEmptyBag errs
+
+printBagOfErrors :: DynFlags -> Bag ErrMsg -> IO ()
+printBagOfErrors dflags bag_of_errors
+ = sequence_ [ let style = mkErrStyle unqual
+ in log_action dflags SevError s style (d $$ e)
+ | ErrMsg { errMsgSpans = s:ss,
+ errMsgShortDoc = d,
+ errMsgExtraInfo = e,
+ errMsgContext = unqual } <- sorted_errs ]
+ where
+ bag_ls = bagToList bag_of_errors
+ sorted_errs = sortLe occ'ed_before bag_ls
+
+ occ'ed_before err1 err2 =
+ case compare (head (errMsgSpans err1)) (head (errMsgSpans err2)) of
+ LT -> True
+ EQ -> True
+ GT -> False
+
+printBagOfWarnings :: DynFlags -> Bag ErrMsg -> IO ()
+printBagOfWarnings dflags bag_of_warns
+ = sequence_ [ let style = mkErrStyle unqual
+ in log_action dflags SevWarning s style (d $$ e)
+ | ErrMsg { errMsgSpans = s:ss,
+ errMsgShortDoc = d,
+ errMsgExtraInfo = e,
+ errMsgContext = unqual } <- sorted_errs ]
+ where
+ bag_ls = bagToList bag_of_warns
+ sorted_errs = sortLe occ'ed_before bag_ls
+
+ occ'ed_before err1 err2 =
+ case compare (head (errMsgSpans err1)) (head (errMsgSpans err2)) of
+ LT -> True
+ EQ -> True
+ GT -> False