@@ -14,6 +14,7 @@
15{-# LANGUAGE DataKinds #-}
15{-# LANGUAGE DataKinds #-}
16{-# LANGUAGE FlexibleContexts #-}
16{-# LANGUAGE FlexibleContexts #-}
17+{-# LANGUAGE PatternGuards #-}
17{-# LANGUAGE RecordWildCards #-}
18{-# LANGUAGE RecordWildCards #-}
18{-# LANGUAGE ScopedTypeVariables #-}
19{-# LANGUAGE ScopedTypeVariables #-}
19{-# LANGUAGE TupleSections #-}
20{-# LANGUAGE TupleSections #-}
@@ -28,6 +29,7 @@ import qualified Data.Aeson as A
28import Data.Bool (bool)
29import Data.Bool (bool)
29import qualified Data.ByteString.Lazy as B
30import qualified Data.ByteString.Lazy as B
30import qualified Data.ByteString.UTF8 as BU8
31import qualified Data.ByteString.UTF8 as BU8
32+import Data.Char (isSpace)
31import qualified Data.Map as M
33import qualified Data.Map as M
32import Data.Text (Text)
34import Data.Text (Text)
33import Data.Version (showVersion)
35import Data.Version (showVersion)
@@ -45,6 +47,8 @@ import System.FilePath.Glob (glob)
45import System.IO (hSetEncoding, hPutStrLn, stdout, stderr, utf8)
47import System.IO (hSetEncoding, hPutStrLn, stdout, stderr, utf8)
46import System.IO.UTF8 (readUTF8FileT)
48import System.IO.UTF8 (readUTF8FileT)
50+import Text.Printf (printf)
48import Make
52import Make
49import Makefile
53import Makefile
@@ -55,12 +59,22 @@ data PCCMakeOptions = PCCMakeOptions
55 , pccmOtherOpts :: OtherOptions
59 , pccmOtherOpts :: OtherOptions
56 , pccmUsePrefix :: Bool
60 , pccmUsePrefix :: Bool
57 , pccmJSONErrors :: Bool
61 , pccmJSONErrors :: Bool
62+ , pccmBriefErrors :: Bool
58 , pccmXcode :: Bool
63 , pccmXcode :: Bool
66+data ErrorTypes = StandardErrors | JSONErrors | BriefErrors
68+data BriefType = Warning | Error | Note
70+instance Show BriefType where
71+ show Warning = "warning"
61−-- | Argumnets: verbose, use JSON, warnings, errors
75+-- | Argumnets: verbose, ErrorTypes (e.g. JSON or Brief), warnings, errors
62−printWarningsAndErrors :: Bool -> Bool -> P.MultipleErrors -> Either P.MultipleErrors a -> IO ()
76+printWarningsAndErrors :: Bool -> ErrorTypes -> P.MultipleErrors -> Either P.MultipleErrors a -> IO ()
63−printWarningsAndErrors verbose False warnings errors = do
77+printWarningsAndErrors verbose StandardErrors warnings errors = do
64 cc <- bool Nothing (Just P.defaultCodeColor) <$> ANSI.hSupportsANSI stderr
78 cc <- bool Nothing (Just P.defaultCodeColor) <$> ANSI.hSupportsANSI stderr
65 let ppeOpts = P.defaultPPEOptions { P.ppeCodeColor = cc, P.ppeFull = verbose }
79 let ppeOpts = P.defaultPPEOptions { P.ppeCodeColor = cc, P.ppeFull = verbose }
66 when (P.nonEmpty warnings) $
80 when (P.nonEmpty warnings) $
@@ -70,11 +84,41 @@ printWarningsAndErrors verbose False warnings errors = do
70 hPutStrLn stderr (P.prettyPrintMultipleErrors ppeOpts errs)
84 hPutStrLn stderr (P.prettyPrintMultipleErrors ppeOpts errs)
71 exitFailure
85 exitFailure
72 Right _ -> return ()
86 Right _ -> return ()
73−printWarningsAndErrors verbose True warnings errors = do
87+printWarningsAndErrors verbose JSONErrors warnings errors = do
74 hPutStrLn stderr . BU8.toString . B.toStrict . A.encode $
88 hPutStrLn stderr . BU8.toString . B.toStrict . A.encode $
75 JSONResult (toJSONErrors verbose P.Warning warnings)
89 JSONResult (toJSONErrors verbose P.Warning warnings)
76 (either (toJSONErrors verbose P.Error) (const []) errors)
90 (either (toJSONErrors verbose P.Error) (const []) errors)
77 either (const exitFailure) (const (return ())) errors
91 either (const exitFailure) (const (return ())) errors
92+printWarningsAndErrors verbose BriefErrors warnings errors = do
95+ (briefFormat $ JSONResult (toJSONErrors verbose P.Warning warnings)
96+ (either (toJSONErrors verbose P.Error) (const []) errors))
97+ either (const exitFailure) (const (return ())) errors
99+briefFormat :: JSONResult -> [String]
100+briefFormat (JSONResult warnings errors) = concat $
101+ (renderError Error <$> errors) <> (renderError Warning <$> warnings)
103+ renderError :: BriefType -> JSONError -> [String]
104+ renderError t (JSONError pos msg _ lnk file _ suggest) =
108+ (maybe 0 startLine pos)
109+ (maybe 0 startColumn pos)
111+ (dropWhile isSpace message) :
116+ | otherwise = printf "%sSee %s for more information\n" msg lnk
119+ Just (ErrorSuggestion s@(_:_) r) <- suggest =
120+ renderError Note (JSONError (r <|> pos) s [] [] file Nothing Nothing)
79compile :: PCCMakeOptions -> IO ()
123compile :: PCCMakeOptions -> IO ()
80compile PCCMakeOptions{..} = do
124compile PCCMakeOptions{..} = do
@@ -91,8 +135,13 @@ compile PCCMakeOptions{..} = do
91 let filePathMap = M.fromList $ map (\(fp, P.Module _ _ mn _ _) -> (mn, Right fp)) ms
135 let filePathMap = M.fromList $ map (\(fp, P.Module _ _ mn _ _) -> (mn, Right fp)) ms
92 let makeActions = buildMakeActions pccmOutputDir filePathMap pccmUsePrefix pccmOtherOpts
136 let makeActions = buildMakeActions pccmOutputDir filePathMap pccmUsePrefix pccmOtherOpts
93 P.make makeActions (map snd ms)
137 P.make makeActions (map snd ms)
94− printWarningsAndErrors (P.optionsVerboseErrors pccmOpts) pccmJSONErrors makeWarnings makeErrors
138+ printWarningsAndErrors (P.optionsVerboseErrors pccmOpts) errorType makeWarnings makeErrors
95 exitSuccess
139 exitSuccess
142+ | pccmJSONErrors = JSONErrors
143+ | pccmBriefErrors = BriefErrors
144+ | otherwise = StandardErrors
97warnFileTypeNotFound :: String -> IO ()
146warnFileTypeNotFound :: String -> IO ()
98warnFileTypeNotFound = hPutStrLn stderr . ("pcc: No files found using pattern: " ++)
147warnFileTypeNotFound = hPutStrLn stderr . ("pcc: No files found using pattern: " ++)
@@ -159,6 +208,12 @@ jsonErrors :: Parser Bool
159jsonErrors = switch $
208jsonErrors = switch $
160 long "json-errors"
209 long "json-errors"
161 <> help "Print errors to stderr as JSON"
210 <> help "Print errors to stderr as JSON"
212+briefErrors :: Parser Bool
213+briefErrors = switch $
215+ <> help "Print errors to stderr in \"brief format\" (a de facto standard made popular by gcc)"
162sourceMaps :: Parser Bool
217sourceMaps :: Parser Bool
163sourceMaps = switch $
218sourceMaps = switch $
164 long "source-maps"
219 long "source-maps"
@@ -199,6 +254,7 @@ pccMakeOptions = PCCMakeOptions <$> many inputFile
199 <*> otherOptions
254 <*> otherOptions
200 <*> (not <$> noPrefix)
255 <*> (not <$> noPrefix)
201 <*> jsonErrors
256 <*> jsonErrors
202 <*> xcode
258 <*> xcode
204main :: IO ()
260main :: IO ()