-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 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 locn) (char ':'))
- 4 (rest_of_err_msg sty)
-
-addShortWarnLocLine locn rest_of_err_msg sty
- = hang ((<>) (ppr PprForUser 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)
+type MsgWithLoc = (SrcLoc, SDoc)
+
+type ErrMsg = MsgWithLoc
+type WarnMsg = MsgWithLoc
+type Message = SDoc
+
+addShortErrLocLine :: SrcLoc -> Message -> ErrMsg
+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
+ )
+
+addErrLocHdrLine locn hdr rest_of_err_msg
+ = ( locn
+ , hang (ppr locn <> colon<+> hdr)
+ 4 rest_of_err_msg
+ )
+
+addShortWarnLocLine locn rest_of_err_msg
+ = ( locn
+ , 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 (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 [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 WarnMsg -> SDoc
+pprBagOfWarnings bag_of_warns = pprBagOfErrors bag_of_warns