@@ -11,6 +11,7 @@ module GHC.Util (
1111 , getMessages
1212 , SDoc
1313 , Located
14+ , hseToGhcExtension
1415 -- Temporary : Export these so GHC doesn't consider them unused and
1516 -- tell weeder to ignore them.
1617 , isAtom , addParen , paren , isApp , isOpApp , isAnyApp , isDot , isSection , isDotApp
@@ -38,6 +39,8 @@ import "ghc-lib-parser" HeaderInfo
3839import Data.List
3940import System.FilePath
4041import Language.Preprocessor.Unlit
42+ import qualified Language.Haskell.Exts.Extension as HSE
43+ import qualified Data.Map.Strict as Map
4144
4245fakeSettings :: Settings
4346fakeSettings = Settings
@@ -83,13 +86,21 @@ baseDynFlags :: DynFlags
8386baseDynFlags = foldl' xopt_set
8487 (defaultDynFlags fakeSettings fakeLlvmConfig) enabledExtensions
8588
86- parsePragmasIntoDynFlags ::
87- DynFlags -> FilePath -> String -> IO (Either String DynFlags )
88- parsePragmasIntoDynFlags flags filepath str =
89+ -- | Adjust the input 'DynFlags' to take into account language
90+ -- extensions to explicitly enable/disable as well as language
91+ -- extensions enabled by pragma in the source.
92+ parsePragmasIntoDynFlags :: DynFlags
93+ -> ([Extension ], [Extension ])
94+ -> FilePath
95+ -> String
96+ -> IO (Either String DynFlags )
97+ parsePragmasIntoDynFlags flags (enable, disable) filepath str =
8998 catchErrors $ do
9099 let opts = getOptions flags (stringToStringBuffer str) filepath
91100 (flags, _, _) <- parseDynamicFilePragma flags opts
92- return $ Right flags
101+ let flags' = foldl' xopt_set flags enable
102+ let flags'' = foldl' xopt_unset flags' disable
103+ return $ Right flags''
93104 where
94105 catchErrors :: IO (Either String DynFlags ) -> IO (Either String DynFlags )
95106 catchErrors act = handleGhcException reportErr
@@ -195,3 +206,12 @@ isSection x = case x of
195206isDotApp :: HsExpr GhcPs -> Bool
196207isDotApp (OpApp _ _ (L _ op) _) = isDot op
197208isDotApp _ = False
209+
210+ -- | A mapping from 'HSE.KnownExtension' values to their
211+ -- 'GHC.LanguageExtensions.Type.Extension' equivalents.
212+ hseToGhcExtension :: Map. Map HSE. KnownExtension Extension
213+ hseToGhcExtension =
214+ let ghcExts = Map. fromList [(show x, x) | x <- [Cpp .. StarIsType ]]
215+ in
216+ Map. fromList [ (x, ext) | x <- [minBound .. maxBound ]
217+ , Just ext <- [Map. lookup (show x) ghcExts] ]
0 commit comments