git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
C++: support 'brief format' via --brief-errors pcc switch
commit9fc0c769dcf7c6779986aadc4798eec04e60e75a authorAndy Arvanitis <andyarvanitis@gmail.com> dateMon 12 Dec 2016, 15:27 (10 years ago) parent8e3865b
1 file changed+61−5
pcc/Main.hs+61−5
pcc/Main.hs+61−5
@@ -14,6 +14,7 @@
1414
1515{-# LANGUAGE DataKinds #-}
1616{-# LANGUAGE FlexibleContexts #-}
17+{-# LANGUAGE PatternGuards #-}
1718{-# LANGUAGE RecordWildCards #-}
1819{-# LANGUAGE ScopedTypeVariables #-}
1920{-# LANGUAGE TupleSections #-}
@@ -28,6 +29,7 @@ import qualified Data.Aeson as A
2829import Data.Bool (bool)
2930import qualified Data.ByteString.Lazy as B
3031import qualified Data.ByteString.UTF8 as BU8
32+import Data.Char (isSpace)
3133import qualified Data.Map as M
3234import Data.Text (Text)
3335import Data.Version (showVersion)
@@ -45,6 +47,8 @@ import System.FilePath.Glob (glob)
4547import System.IO (hSetEncoding, hPutStrLn, stdout, stderr, utf8)
4648import System.IO.UTF8 (readUTF8FileT)
4749
50+import Text.Printf (printf)
51+
4852import Make
4953import Makefile
5054
@@ -55,12 +59,22 @@ data PCCMakeOptions = PCCMakeOptions
5559 , pccmOtherOpts :: OtherOptions
5660 , pccmUsePrefix :: Bool
5761 , pccmJSONErrors :: Bool
62+ , pccmBriefErrors :: Bool
5863 , pccmXcode :: Bool
5964 }
6065
61−-- | Argumnets: verbose, use JSON, warnings, errors
62−printWarningsAndErrors :: Bool -> Bool -> P.MultipleErrors -> Either P.MultipleErrors a -> IO ()
63−printWarningsAndErrors verbose False warnings errors = do
66+data ErrorTypes = StandardErrors | JSONErrors | BriefErrors
67+
68+data BriefType = Warning | Error | Note
69+
70+instance Show BriefType where
71+ show Warning = "warning"
72+ show Error = "error"
73+ show Note = "note"
74+
75+-- | Argumnets: verbose, ErrorTypes (e.g. JSON or Brief), warnings, errors
76+printWarningsAndErrors :: Bool -> ErrorTypes -> P.MultipleErrors -> Either P.MultipleErrors a -> IO ()
77+printWarningsAndErrors verbose StandardErrors warnings errors = do
6478 cc <- bool Nothing (Just P.defaultCodeColor) <$> ANSI.hSupportsANSI stderr
6579 let ppeOpts = P.defaultPPEOptions { P.ppeCodeColor = cc, P.ppeFull = verbose }
6680 when (P.nonEmpty warnings) $
@@ -70,11 +84,41 @@ printWarningsAndErrors verbose False warnings errors = do
7084 hPutStrLn stderr (P.prettyPrintMultipleErrors ppeOpts errs)
7185 exitFailure
7286 Right _ -> return ()
73−printWarningsAndErrors verbose True warnings errors = do
87+printWarningsAndErrors verbose JSONErrors warnings errors = do
7488 hPutStrLn stderr . BU8.toString . B.toStrict . A.encode $
7589 JSONResult (toJSONErrors verbose P.Warning warnings)
7690 (either (toJSONErrors verbose P.Error) (const []) errors)
7791 either (const exitFailure) (const (return ())) errors
92+printWarningsAndErrors verbose BriefErrors warnings errors = do
93+ mapM
94+ (hPutStrLn stderr)
95+ (briefFormat $ JSONResult (toJSONErrors verbose P.Warning warnings)
96+ (either (toJSONErrors verbose P.Error) (const []) errors))
97+ either (const exitFailure) (const (return ())) errors
98+
99+briefFormat :: JSONResult -> [String]
100+briefFormat (JSONResult warnings errors) = concat $
101+ (renderError Error <$> errors) <> (renderError Warning <$> warnings)
102+ where
103+ renderError :: BriefType -> JSONError -> [String]
104+ renderError t (JSONError pos msg _ lnk file _ suggest) =
105+ printf
106+ "%s:%d:%d: %s: %s"
107+ (concat file)
108+ (maybe 0 startLine pos)
109+ (maybe 0 startColumn pos)
110+ (show t)
111+ (dropWhile isSpace message) :
112+ notes
113+ where
114+ message
115+ | null lnk = msg
116+ | otherwise = printf "%sSee %s for more information\n" msg lnk
117+ notes
118+ | Error <- t,
119+ Just (ErrorSuggestion s@(_:_) r) <- suggest =
120+ renderError Note (JSONError (r <|> pos) s [] [] file Nothing Nothing)
121+ | otherwise = []
78122
79123compile :: PCCMakeOptions -> IO ()
80124compile PCCMakeOptions{..} = do
@@ -91,8 +135,13 @@ compile PCCMakeOptions{..} = do
91135 let filePathMap = M.fromList $ map (\(fp, P.Module _ _ mn _ _) -> (mn, Right fp)) ms
92136 let makeActions = buildMakeActions pccmOutputDir filePathMap pccmUsePrefix pccmOtherOpts
93137 P.make makeActions (map snd ms)
94− printWarningsAndErrors (P.optionsVerboseErrors pccmOpts) pccmJSONErrors makeWarnings makeErrors
138+ printWarningsAndErrors (P.optionsVerboseErrors pccmOpts) errorType makeWarnings makeErrors
95139 exitSuccess
140+ where
141+ errorType
142+ | pccmJSONErrors = JSONErrors
143+ | pccmBriefErrors = BriefErrors
144+ | otherwise = StandardErrors
96145
97146warnFileTypeNotFound :: String -> IO ()
98147warnFileTypeNotFound = hPutStrLn stderr . ("pcc: No files found using pattern: " ++)
@@ -159,6 +208,12 @@ jsonErrors :: Parser Bool
159208jsonErrors = switch $
160209 long "json-errors"
161210 <> help "Print errors to stderr as JSON"
211+
212+briefErrors :: Parser Bool
213+briefErrors = switch $
214+ long "brief-errors"
215+ <> help "Print errors to stderr in \"brief format\" (a de facto standard made popular by gcc)"
216+
162217sourceMaps :: Parser Bool
163218sourceMaps = switch $
164219 long "source-maps"
@@ -199,6 +254,7 @@ pccMakeOptions = PCCMakeOptions <$> many inputFile
199254 <*> otherOptions
200255 <*> (not <$> noPrefix)
201256 <*> jsonErrors
257+ <*> briefErrors
202258 <*> xcode
203259
204260main :: IO ()