@@ -20,7 +20,7 @@ import DynFlags
20
20
import qualified HeaderInfo as Hdr
21
21
import Development.IDE.Types.Diagnostics
22
22
import Development.IDE.GHC.Error
23
- import SysTools (Option (.. ), runUnlit )
23
+ import SysTools (Option (.. ), runUnlit , runPp )
24
24
import Control.Monad.Trans.Except
25
25
import qualified GHC.LanguageExtensions as LangExt
26
26
import Data.Maybe
@@ -43,10 +43,19 @@ preprocessor filename mbContents = do
43
43
44
44
-- Perform cpp
45
45
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
47
56
return (contents, dflags)
48
57
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
50
59
dflags <- ExceptT $ parsePragmasIntoDynFlags filename contents
51
60
return (contents, dflags)
52
61
@@ -132,3 +141,18 @@ runCpp dflags filename contents = withTempDir $ \dir -> do
132
141
= " # " <> num <> " \" " <> map (\ x -> if isPathSeparator x then ' /' else x) filename <> " \" "
133
142
| otherwise = x
134
143
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
0 commit comments