Skip to content

GHC 9.4: Compute the source hash before the preprocessor #3415

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 4 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions ghcide/src/Development/IDE/Core/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,8 @@ getModSummaryFromImports
-> Maybe Util.StringBuffer
-> ExceptT [FileDiagnostic] IO ModSummaryResult
getModSummaryFromImports env fp modTime contents = do
(contents, opts, env) <- preprocessor env fp contents

(contents, opts, env, src_hash) <- preprocessor env fp contents

let dflags = hsc_dflags env

Expand Down Expand Up @@ -1141,9 +1142,6 @@ getModSummaryFromImports env fp modTime contents = do
liftIO $ evaluate $ rnf srcImports
liftIO $ evaluate $ rnf textualImports

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

modLoc <- liftIO $ if mod == mAIN_NAME
-- specially in tests it's common to have lots of nameless modules
Expand Down
11 changes: 8 additions & 3 deletions ghcide/src/Development/IDE/Core/Preprocessor.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Development.IDE.Core.Preprocessor

import Development.IDE.GHC.Compat
import qualified Development.IDE.GHC.Compat.Util as Util
import qualified Development.IDE.GHC.Util as Util
import Development.IDE.GHC.CPP
import Development.IDE.GHC.Orphans ()

Expand Down Expand Up @@ -36,7 +37,7 @@ import GHC.Utils.Outputable (renderWithContext)

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

-- Compute the source hash before the preprocessor because this is
-- how GHC does it.
!src_hash <- liftIO $ Util.fingerprintFromStringBuffer contents

-- Perform cpp
(opts, env) <- ExceptT $ parsePragmasIntoHscEnv env filename contents
let dflags = hsc_dflags env
Expand All @@ -73,11 +78,11 @@ preprocessor env filename mbContents = do

-- Perform preprocessor
if not $ gopt Opt_Pp dflags then
return (contents, opts, env)
return (contents, opts, env, src_hash)
else do
contents <- liftIO $ runPreprocessor env filename $ if isOnDisk then Nothing else Just contents
(opts, env) <- ExceptT $ parsePragmasIntoHscEnv env filename contents
return (contents, opts, env)
return (contents, opts, env, src_hash)
where
logAction :: IORef [CPPLog] -> LogActionCompat
logAction cppLogs dflags _reason severity srcSpan _style msg = do
Expand Down