graphql-engine/server/src-lib/GHC/AssertNF/CPP.hs
Robert d900a65399 server: Reduce and localize CPP usage
Some of our use of CPP causes trouble for ormolu, compare https://github.com/tweag/ormolu/issues/774.
Specifically, for understandable reasons, it can't deal well with `#ifdef` use that is not at the top-level.

This PR removes the problematic usage in ways that I hope are also a net non-loss regardless of helping
out ormolu (or other tooling).

- The default value for enabled APIs moves to the top level, next to the command line help, so
  they'll stay in sync more easily.
- All the CPP around using `assertNFHere` is moved to one module.

https://github.com/hasura/graphql-engine-mono/pull/2361

GitOrigin-RevId: ed6e039e6d8960322fd8d1312df762ad197c29b1
2021-09-16 18:52:37 +00:00

44 lines
1.2 KiB
Haskell

{-# LANGUAGE CPP #-}
-- | GHC.AssertNF.CPP localizes our use of CPP around calls
-- to 'assertNFHere', primarily to give tooling (e.g. ormolu)
-- an easier time.
module GHC.AssertNF.CPP
( assertNFHere
, disableAssertNF
, GHC.AssertNF.assertNFNamed
)
where
import qualified GHC.AssertNF
import Hasura.Prelude
import Language.Haskell.TH
#ifndef PROFILING
import Text.Printf (printf)
#endif
assertNFHere :: Q Exp
#ifdef PROFILING
assertNFHere = [| const (return ()) |]
#else
-- This is a copy of 'GHC.AssertNF.assertNFHere'. We can't easily
-- use the original because that relies on an import of "GHC.AssertNF".
-- Instead, we rewrite it to use the re-exported 'assertNFNamed'.
assertNFHere = do
locStr <- formatLoc <$> location
return $ AppE (VarE (mkName "GHC.AssertNF.CPP.assertNFNamed"))
(LitE (StringL locStr))
where formatLoc :: Loc -> String
formatLoc loc = let file = loc_filename loc
(line, col) = loc_start loc
in printf "parameter at %s:%d:%d" file line col
#endif
disableAssertNF :: IO ()
#ifdef PROFILING
disableAssertNF = return ()
#else
disableAssertNF = GHC.AssertNF.disableAssertNF
#endif