-- Copyright (c) 2019 The DAML Authors. All rights reserved.
-- SPDX-License-Identifier: Apache-2.0
{-# LANGUAGE ExplicitNamespaces #-}

module Development.IDE.GHC.Warnings(withWarnings) where

import           Control.Concurrent.Strict
import           Data.List
import qualified Data.Text                         as T

import           Development.IDE.GHC.Compat
import           Development.IDE.GHC.Error
import           Development.IDE.Types.Diagnostics
import           Language.LSP.Types                (type (|?) (..))


-- | Take a GHC monadic action (e.g. @typecheckModule pm@ for some
-- parsed module 'pm@') and produce a "decorated" action that will
-- harvest any warnings encountered executing the action. The 'phase'
-- argument classifies the context (e.g. "Parser", "Typechecker").
--
--   The ModSummary function is required because of
--   https://github.com/ghc/ghc/blob/5f1d949ab9e09b8d95319633854b7959df06eb58/compiler/main/GHC.hs#L623-L640
--   which basically says that log_action is taken from the ModSummary when GHC feels like it.
--   The given argument lets you refresh a ModSummary log_action
withWarnings :: T.Text -> ((HscEnv -> HscEnv) -> IO a) -> IO ([(WarnReason, FileDiagnostic)], a)
withWarnings :: Text
-> ((HscEnv -> HscEnv) -> IO a)
-> IO ([(WarnReason, FileDiagnostic)], a)
withWarnings Text
diagSource (HscEnv -> HscEnv) -> IO a
action = do
  Var [[(WarnReason, FileDiagnostic)]]
warnings <- [[(WarnReason, FileDiagnostic)]]
-> IO (Var [[(WarnReason, FileDiagnostic)]])
forall a. a -> IO (Var a)
newVar []
  let newAction :: LogActionCompat
      newAction :: LogActionCompat
newAction DynFlags
dynFlags WarnReason
wr Severity
_ SrcSpan
loc PrintUnqualified
prUnqual SDoc
msg = do
        let wr_d :: [(WarnReason, FileDiagnostic)]
wr_d = (FileDiagnostic -> (WarnReason, FileDiagnostic))
-> [FileDiagnostic] -> [(WarnReason, FileDiagnostic)]
forall a b. (a -> b) -> [a] -> [b]
map ((WarnReason
wr,) (FileDiagnostic -> (WarnReason, FileDiagnostic))
-> (FileDiagnostic -> FileDiagnostic)
-> FileDiagnostic
-> (WarnReason, FileDiagnostic)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Diagnostic -> Diagnostic) -> FileDiagnostic -> FileDiagnostic
forall c d a b. (c -> d) -> (a, b, c) -> (a, b, d)
third3 (WarnReason -> Diagnostic -> Diagnostic
attachReason WarnReason
wr)) ([FileDiagnostic] -> [(WarnReason, FileDiagnostic)])
-> [FileDiagnostic] -> [(WarnReason, FileDiagnostic)]
forall a b. (a -> b) -> a -> b
$ Text -> DynFlags -> MsgEnvelope () -> [FileDiagnostic]
diagFromErrMsg Text
diagSource DynFlags
dynFlags (MsgEnvelope () -> [FileDiagnostic])
-> MsgEnvelope () -> [FileDiagnostic]
forall a b. (a -> b) -> a -> b
$ DynFlags -> SrcSpan -> PrintUnqualified -> SDoc -> MsgEnvelope ()
mkWarnMsg DynFlags
dynFlags SrcSpan
loc PrintUnqualified
prUnqual SDoc
msg
        Var [[(WarnReason, FileDiagnostic)]]
-> ([[(WarnReason, FileDiagnostic)]]
    -> IO [[(WarnReason, FileDiagnostic)]])
-> IO ()
forall a. Var a -> (a -> IO a) -> IO ()
modifyVar_ Var [[(WarnReason, FileDiagnostic)]]
warnings (([[(WarnReason, FileDiagnostic)]]
  -> IO [[(WarnReason, FileDiagnostic)]])
 -> IO ())
-> ([[(WarnReason, FileDiagnostic)]]
    -> IO [[(WarnReason, FileDiagnostic)]])
-> IO ()
forall a b. (a -> b) -> a -> b
$ [[(WarnReason, FileDiagnostic)]]
-> IO [[(WarnReason, FileDiagnostic)]]
forall (m :: * -> *) a. Monad m => a -> m a
return ([[(WarnReason, FileDiagnostic)]]
 -> IO [[(WarnReason, FileDiagnostic)]])
-> ([[(WarnReason, FileDiagnostic)]]
    -> [[(WarnReason, FileDiagnostic)]])
-> [[(WarnReason, FileDiagnostic)]]
-> IO [[(WarnReason, FileDiagnostic)]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([(WarnReason, FileDiagnostic)]
wr_d[(WarnReason, FileDiagnostic)]
-> [[(WarnReason, FileDiagnostic)]]
-> [[(WarnReason, FileDiagnostic)]]
forall a. a -> [a] -> [a]
:)
      newLogger :: HscEnv -> Logger
newLogger HscEnv
env = (LogAction -> LogAction) -> Logger -> Logger
pushLogHook (LogAction -> LogAction -> LogAction
forall a b. a -> b -> a
const (LogActionCompat -> LogAction
logActionCompat LogActionCompat
newAction)) (HscEnv -> Logger
hsc_logger HscEnv
env)
  a
res <- (HscEnv -> HscEnv) -> IO a
action ((HscEnv -> HscEnv) -> IO a) -> (HscEnv -> HscEnv) -> IO a
forall a b. (a -> b) -> a -> b
$ \HscEnv
env -> Logger -> HscEnv -> HscEnv
putLogHook (HscEnv -> Logger
newLogger HscEnv
env) HscEnv
env
  [[(WarnReason, FileDiagnostic)]]
warns <- Var [[(WarnReason, FileDiagnostic)]]
-> IO [[(WarnReason, FileDiagnostic)]]
forall a. Var a -> IO a
readVar Var [[(WarnReason, FileDiagnostic)]]
warnings
  ([(WarnReason, FileDiagnostic)], a)
-> IO ([(WarnReason, FileDiagnostic)], a)
forall (m :: * -> *) a. Monad m => a -> m a
return ([(WarnReason, FileDiagnostic)] -> [(WarnReason, FileDiagnostic)]
forall a. [a] -> [a]
reverse ([(WarnReason, FileDiagnostic)] -> [(WarnReason, FileDiagnostic)])
-> [(WarnReason, FileDiagnostic)] -> [(WarnReason, FileDiagnostic)]
forall a b. (a -> b) -> a -> b
$ [[(WarnReason, FileDiagnostic)]] -> [(WarnReason, FileDiagnostic)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[(WarnReason, FileDiagnostic)]]
warns, a
res)
  where
    third3 :: (c -> d) -> (a, b, c) -> (a, b, d)
    third3 :: (c -> d) -> (a, b, c) -> (a, b, d)
third3 c -> d
f (a
a, b
b, c
c) = (a
a, b
b, c -> d
f c
c)

attachReason :: WarnReason -> Diagnostic -> Diagnostic
attachReason :: WarnReason -> Diagnostic -> Diagnostic
attachReason WarnReason
wr Diagnostic
d = Diagnostic
d{$sel:_code:Diagnostic :: Maybe (Int32 |? Text)
_code = Text -> Int32 |? Text
forall a b. b -> a |? b
InR (Text -> Int32 |? Text) -> Maybe Text -> Maybe (Int32 |? Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> WarnReason -> Maybe Text
showReason WarnReason
wr}
 where
  showReason :: WarnReason -> Maybe Text
showReason = \case
    WarnReason
NoReason       -> Maybe Text
forall a. Maybe a
Nothing
    Reason WarningFlag
flag    -> WarningFlag -> Maybe Text
showFlag WarningFlag
flag
    ErrReason Maybe WarningFlag
flag -> WarningFlag -> Maybe Text
showFlag (WarningFlag -> Maybe Text) -> Maybe WarningFlag -> Maybe Text
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Maybe WarningFlag
flag

showFlag :: WarningFlag -> Maybe T.Text
showFlag :: WarningFlag -> Maybe Text
showFlag WarningFlag
flag = (Text
"-W" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>) (Text -> Text)
-> (FlagSpec WarningFlag -> Text) -> FlagSpec WarningFlag -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack (String -> Text)
-> (FlagSpec WarningFlag -> String) -> FlagSpec WarningFlag -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FlagSpec WarningFlag -> String
forall flag. FlagSpec flag -> String
flagSpecName (FlagSpec WarningFlag -> Text)
-> Maybe (FlagSpec WarningFlag) -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (FlagSpec WarningFlag -> Bool)
-> [FlagSpec WarningFlag] -> Maybe (FlagSpec WarningFlag)
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find ((WarningFlag -> WarningFlag -> Bool
forall a. Eq a => a -> a -> Bool
== WarningFlag
flag) (WarningFlag -> Bool)
-> (FlagSpec WarningFlag -> WarningFlag)
-> FlagSpec WarningFlag
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FlagSpec WarningFlag -> WarningFlag
forall flag. FlagSpec flag -> flag
flagSpecFlag) [FlagSpec WarningFlag]
wWarningFlags