Skip to content

Commit dc96f87

Browse files
committed
Compute the source hash before the preprocessor runs because this is
how GHC does it. This reduces spurious rebuilds of modules using CPP on GHC 9.4
1 parent ff28990 commit dc96f87

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

ghcide/src/Development/IDE/Core/Compile.hs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,8 @@ getModSummaryFromImports
10901090
-> Maybe Util.StringBuffer
10911091
-> ExceptT [FileDiagnostic] IO ModSummaryResult
10921092
getModSummaryFromImports env fp modTime contents = do
1093-
(contents, opts, env) <- preprocessor env fp contents
1093+
1094+
(contents, opts, env, src_hash) <- preprocessor env fp contents
10941095

10951096
let dflags = hsc_dflags env
10961097

@@ -1141,9 +1142,6 @@ getModSummaryFromImports env fp modTime contents = do
11411142
liftIO $ evaluate $ rnf srcImports
11421143
liftIO $ evaluate $ rnf textualImports
11431144

1144-
#if MIN_VERSION_ghc (9,3,0)
1145-
!src_hash <- liftIO $ fingerprintFromStringBuffer contents
1146-
#endif
11471145

11481146
modLoc <- liftIO $ if mod == mAIN_NAME
11491147
-- specially in tests it's common to have lots of nameless modules

ghcide/src/Development/IDE/Core/Preprocessor.hs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Development.IDE.Core.Preprocessor
88

99
import Development.IDE.GHC.Compat
1010
import qualified Development.IDE.GHC.Compat.Util as Util
11+
import qualified Development.IDE.GHC.Util as Util
1112
import Development.IDE.GHC.CPP
1213
import Development.IDE.GHC.Orphans ()
1314

@@ -36,7 +37,7 @@ import GHC.Utils.Outputable (renderWithContext)
3637

3738
-- | Given a file and some contents, apply any necessary preprocessors,
3839
-- e.g. unlit/cpp. Return the resulting buffer and the DynFlags it implies.
39-
preprocessor :: HscEnv -> FilePath -> Maybe Util.StringBuffer -> ExceptT [FileDiagnostic] IO (Util.StringBuffer, [String], HscEnv)
40+
preprocessor :: HscEnv -> FilePath -> Maybe Util.StringBuffer -> ExceptT [FileDiagnostic] IO (Util.StringBuffer, [String], HscEnv, Util.Fingerprint)
4041
preprocessor env filename mbContents = do
4142
-- Perform unlit
4243
(isOnDisk, contents) <-
@@ -48,6 +49,10 @@ preprocessor env filename mbContents = do
4849
let isOnDisk = isNothing mbContents
4950
return (isOnDisk, contents)
5051

52+
-- Compute the source hash before the preprocessor because this is
53+
-- how GHC does it.
54+
!src_hash <- liftIO $ Util.fingerprintFromStringBuffer contents
55+
5156
-- Perform cpp
5257
(opts, env) <- ExceptT $ parsePragmasIntoHscEnv env filename contents
5358
let dflags = hsc_dflags env
@@ -73,11 +78,11 @@ preprocessor env filename mbContents = do
7378

7479
-- Perform preprocessor
7580
if not $ gopt Opt_Pp dflags then
76-
return (contents, opts, env)
81+
return (contents, opts, env, src_hash)
7782
else do
7883
contents <- liftIO $ runPreprocessor env filename $ if isOnDisk then Nothing else Just contents
7984
(opts, env) <- ExceptT $ parsePragmasIntoHscEnv env filename contents
80-
return (contents, opts, env)
85+
return (contents, opts, env, src_hash)
8186
where
8287
logAction :: IORef [CPPLog] -> LogActionCompat
8388
logAction cppLogs dflags _reason severity srcSpan _style msg = do

0 commit comments

Comments
 (0)