@@ -20,7 +20,7 @@ import DynFlags
2020import qualified HeaderInfo as Hdr
2121import Development.IDE.Types.Diagnostics
2222import Development.IDE.GHC.Error
23−import SysTools (Option (..), runUnlit)
23+import SysTools (Option (..), runUnlit, runPp)
2424import Control.Monad.Trans.Except
2525import qualified GHC.LanguageExtensions as LangExt
2626import Data.Maybe
@@ -43,10 +43,19 @@ preprocessor filename mbContents = do
4343
4444 -- Perform cpp
4545 dflags <- ExceptT $ parsePragmasIntoDynFlags filename contents
46− if not $ xopt LangExt.Cpp dflags then
46+ (isOnDisk, contents, dflags) <-
47+ if not $ xopt LangExt.Cpp dflags then
48+ return (isOnDisk, contents, dflags)
49+ else do
50+ contents <- liftIO $ runCpp dflags filename $ if isOnDisk then Nothing else Just contents
51+ dflags <- ExceptT $ parsePragmasIntoDynFlags filename contents
52+ return (False, contents, dflags)
53+
54+ -- Perform preprocessor
55+ if not $ gopt Opt_Pp dflags then
4756 return (contents, dflags)
4857 else do
49− contents <- liftIO $ runCpp dflags filename $ if isOnDisk then Nothing else Just contents
58+ contents <- liftIO $ runPreprocessor dflags filename $ if isOnDisk then Nothing else Just contents
5059 dflags <- ExceptT $ parsePragmasIntoDynFlags filename contents
5160 return (contents, dflags)
5261
@@ -132,3 +141,18 @@ runCpp dflags filename contents = withTempDir $ \dir -> do
132141 = "# " <> num <> " \"" <> map (\x -> if isPathSeparator x then '/' else x) filename <> "\""
133142 | otherwise = x
134143 stringToStringBuffer . unlines . map tweak . lines <$> readFileUTF8' out
144+
145+
146+-- | Run a preprocessor on a file
147+runPreprocessor :: DynFlags -> FilePath -> Maybe SB.StringBuffer -> IO SB.StringBuffer
148+runPreprocessor dflags filename contents = withTempDir $ \dir -> do
149+ let out = dir </> takeFileName filename <.> "out"
150+ inp <- case contents of
151+ Nothing -> return filename
152+ Just contents -> do
153+ let inp = dir </> takeFileName filename <.> "hs"
154+ withBinaryFile inp WriteMode $ \h ->
155+ hPutStringBuffer h contents
156+ return inp
157+ runPp dflags [SysTools.Option filename, SysTools.Option inp, SysTools.FileOption "" out]
158+ SB.hGetStringBuffer out