Skip to content

Commit 206eb47

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 206eb47

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-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: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import GHC.Utils.Outputable (renderWithContext)
3636

3737
-- | Given a file and some contents, apply any necessary preprocessors,
3838
-- 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)
39+
preprocessor :: HscEnv -> FilePath -> Maybe Util.StringBuffer -> ExceptT [FileDiagnostic] IO (Util.StringBuffer, [String], HscEnv, Util.Fingerprint)
4040
preprocessor env filename mbContents = do
4141
-- Perform unlit
4242
(isOnDisk, contents) <-
@@ -48,6 +48,10 @@ preprocessor env filename mbContents = do
4848
let isOnDisk = isNothing mbContents
4949
return (isOnDisk, contents)
5050

51+
-- Compute the source hash before the preprocessor because this is
52+
-- how GHC does it.
53+
!src_hash <- liftIO $ Util.fingerprintFromStringBuffer contents
54+
5155
-- Perform cpp
5256
(opts, env) <- ExceptT $ parsePragmasIntoHscEnv env filename contents
5357
let dflags = hsc_dflags env
@@ -73,11 +77,11 @@ preprocessor env filename mbContents = do
7377

7478
-- Perform preprocessor
7579
if not $ gopt Opt_Pp dflags then
76-
return (contents, opts, env)
80+
return (contents, opts, env, src_hash)
7781
else do
7882
contents <- liftIO $ runPreprocessor env filename $ if isOnDisk then Nothing else Just contents
7983
(opts, env) <- ExceptT $ parsePragmasIntoHscEnv env filename contents
80-
return (contents, opts, env)
84+
return (contents, opts, env, src_hash)
8185
where
8286
logAction :: IORef [CPPLog] -> LogActionCompat
8387
logAction cppLogs dflags _reason severity srcSpan _style msg = do

0 commit comments

Comments
 (0)