add more checks

This commit is contained in:
Vincent Hanquez 2015-04-07 06:51:32 +01:00
parent 0e40a621da
commit 165974adc8

50
QA.hs
View File

@ -13,6 +13,18 @@ import Control.Exception
import System.Console.ANSI import System.Console.ANSI
allowedExtensions =
[ ScopedTypeVariables, BangPatterns, ForeignFunctionInterface, OverloadedStrings]
perModuleAllowedExtensions =
[ ("Crypto/Hash/Utils.hs", [MagicHash])
, ("Crypto/Internal/ByteArray.hs", [MagicHash, UnboxedTuples])
, ("Crypto/Internal/Memory.hs", [MagicHash, UnboxedTuples])
]
disallowedModules =
[ (ModuleName "System.IO.Unsafe", ModuleName "Crypto.Internal.Compat")
]
main = do main = do
modules <- findAllModules modules <- findAllModules
mapM_ qa modules mapM_ qa modules
@ -25,7 +37,7 @@ main = do
Just (_, exts) -> qaExts file content exts Just (_, exts) -> qaExts file content exts
qaExts file content exts = do qaExts file content exts = do
printInfo "extensions" (intercalate ", " $ map show exts) printInfo "extensions" (intercalate ", " $ map show (getEnabledExts exts))
let mode = defaultParseMode { parseFilename = file, extensions = exts } let mode = defaultParseMode { parseFilename = file, extensions = exts }
@ -35,8 +47,28 @@ main = do
let imports = getModulesImports mod let imports = getModulesImports mod
printInfo "modules" (intercalate ", " (map (prettyPrint . importModule) imports)) printInfo "modules" (intercalate ", " (map (prettyPrint . importModule) imports))
let useSystemIOUnsafe = elem (ModuleName "System.IO.Unsafe") (map importModule imports) -- check for allowed extensions
when useSystemIOUnsafe $ printWarningImport "Crypto.Internal.Compat" "System.IO.Unsafe" forM_ (getEnabledExts exts) $ \ext -> do
let allowed = elem ext allowedExtensions
allowed' = allowed || maybe False (\z -> elem ext z) (lookup file perModuleAllowedExtensions)
unless allowed' $ printWarningExtension ext
-- check for disallowed modules
forM_ (map importModule $ getModulesImports mod) $ \impMod ->
case lookup impMod disallowedModules of
Nothing -> return ()
Just newMod | file == moduleToFile impMod -> return ()
| otherwise -> printWarningImport impMod newMod
moduleToFile (ModuleName m) =
intercalate "/" (wordsWhen (== '.') m) ++ ".hs"
wordsWhen :: (Char -> Bool) -> String -> [String]
wordsWhen p s = case dropWhile p s of
"" -> []
s' -> w : wordsWhen p s'' where (w, s'') = break p s'
------------------------------------------------------------------------
printHeader s = printHeader s =
setSGR [SetColor Foreground Vivid Green] >> putStrLn s >> setSGR [] setSGR [SetColor Foreground Vivid Green] >> putStrLn s >> setSGR []
@ -45,11 +77,19 @@ main = do
printError s = printError s =
setSGR [SetColor Foreground Vivid Red] >> putStrLn s >> setSGR [] setSGR [SetColor Foreground Vivid Red] >> putStrLn s >> setSGR []
printWarningImport expected actual = printWarningImport (ModuleName expected) (ModuleName actual) =
setSGR [SetColor Foreground Vivid Yellow] >> putStrLn ("warning: use " ++ show expected ++ " instead of " ++ actual) >> setSGR [] setSGR [SetColor Foreground Vivid Yellow] >> putStrLn ("warning: use module " ++ expected ++ " instead of " ++ actual) >> setSGR []
printWarningExtension ext =
setSGR [SetColor Foreground Vivid Yellow] >> putStrLn ("warning: use extension " ++ show ext) >> setSGR []
getModulesImports (Module _ _ _ _ _ imports _) = imports getModulesImports (Module _ _ _ _ _ imports _) = imports
getEnabledExts = foldl doAcc []
where doAcc acc (EnableExtension e) = e : acc
doAcc acc _ = acc
------------------------------------------------------------------------
findAllModules :: IO [FilePath] findAllModules :: IO [FilePath]
findAllModules = dirTraverse "Crypto" fileCallback dirCallback [] findAllModules = dirTraverse "Crypto" fileCallback dirCallback []
where where