-- | @futhark check@
module Futhark.CLI.Check (main) where

import Control.Monad
import Control.Monad.IO.Class
import Futhark.Compiler
import Futhark.Util.Options
import Futhark.Util.Pretty (pretty)
import Language.Futhark.Warnings
import System.IO

newtype CheckConfig = CheckConfig {CheckConfig -> Bool
checkWarn :: Bool}

newCheckConfig :: CheckConfig
newCheckConfig :: CheckConfig
newCheckConfig = Bool -> CheckConfig
CheckConfig Bool
True

options :: [FunOptDescr CheckConfig]
options :: [FunOptDescr CheckConfig]
options =
  [ [Char]
-> [[Char]]
-> ArgDescr (Either (IO ()) (CheckConfig -> CheckConfig))
-> [Char]
-> FunOptDescr CheckConfig
forall a. [Char] -> [[Char]] -> ArgDescr a -> [Char] -> OptDescr a
Option
      [Char]
"w"
      []
      (Either (IO ()) (CheckConfig -> CheckConfig)
-> ArgDescr (Either (IO ()) (CheckConfig -> CheckConfig))
forall a. a -> ArgDescr a
NoArg (Either (IO ()) (CheckConfig -> CheckConfig)
 -> ArgDescr (Either (IO ()) (CheckConfig -> CheckConfig)))
-> Either (IO ()) (CheckConfig -> CheckConfig)
-> ArgDescr (Either (IO ()) (CheckConfig -> CheckConfig))
forall a b. (a -> b) -> a -> b
$ (CheckConfig -> CheckConfig)
-> Either (IO ()) (CheckConfig -> CheckConfig)
forall a b. b -> Either a b
Right ((CheckConfig -> CheckConfig)
 -> Either (IO ()) (CheckConfig -> CheckConfig))
-> (CheckConfig -> CheckConfig)
-> Either (IO ()) (CheckConfig -> CheckConfig)
forall a b. (a -> b) -> a -> b
$ \CheckConfig
cfg -> CheckConfig
cfg {checkWarn :: Bool
checkWarn = Bool
False})
      [Char]
"Disable all warnings."
  ]

-- | Run @futhark check@.
main :: String -> [String] -> IO ()
main :: [Char] -> [[Char]] -> IO ()
main = CheckConfig
-> [FunOptDescr CheckConfig]
-> [Char]
-> ([[Char]] -> CheckConfig -> Maybe (IO ()))
-> [Char]
-> [[Char]]
-> IO ()
forall cfg.
cfg
-> [FunOptDescr cfg]
-> [Char]
-> ([[Char]] -> cfg -> Maybe (IO ()))
-> [Char]
-> [[Char]]
-> IO ()
mainWithOptions CheckConfig
newCheckConfig [FunOptDescr CheckConfig]
options [Char]
"program" (([[Char]] -> CheckConfig -> Maybe (IO ()))
 -> [Char] -> [[Char]] -> IO ())
-> ([[Char]] -> CheckConfig -> Maybe (IO ()))
-> [Char]
-> [[Char]]
-> IO ()
forall a b. (a -> b) -> a -> b
$ \[[Char]]
args CheckConfig
cfg ->
  case [[Char]]
args of
    [[Char]
file] -> IO () -> Maybe (IO ())
forall a. a -> Maybe a
Just (IO () -> Maybe (IO ())) -> IO () -> Maybe (IO ())
forall a b. (a -> b) -> a -> b
$ do
      (Warnings
warnings, Imports
_, VNameSource
_) <- [Char] -> IO (Warnings, Imports, VNameSource)
forall (m :: * -> *).
MonadIO m =>
[Char] -> m (Warnings, Imports, VNameSource)
readProgramOrDie [Char]
file
      Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (CheckConfig -> Bool
checkWarn CheckConfig
cfg Bool -> Bool -> Bool
&& Warnings -> Bool
anyWarnings Warnings
warnings) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
        IO () -> IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ Handle -> [Char] -> IO ()
hPutStrLn Handle
stderr ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$ Warnings -> [Char]
forall a. Pretty a => a -> [Char]
pretty Warnings
warnings
    [[Char]]
_ -> Maybe (IO ())
forall a. Maybe a
Nothing