Skip to content

Commit

Permalink
Push CPP statements to compatibility module
Browse files Browse the repository at this point in the history
  • Loading branch information
fendor committed Dec 23, 2024
1 parent ada6ed6 commit 1398a0b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 32 deletions.
37 changes: 6 additions & 31 deletions ghcide/src/Development/IDE/Core/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1075,11 +1075,7 @@ parseHeader dflags filename contents = do
let loc = mkRealSrcLoc (Util.mkFastString filename) 1 1
case unP Compat.parseHeader (initParserState (initParserOpts dflags) contents loc) of
PFailedWithErrorMessages msgs ->
#if MIN_VERSION_ghc(9,5,0)
throwE $ diagFromErrMsgs sourceParser dflags $ msgs dflags
#else
throwE $ diagFromSDocErrMsgs sourceParser dflags $ msgs dflags
#endif
throwE $ diagFromGhcErrorMessages sourceParser dflags $ msgs dflags
POk pst rdr_module -> do
let (warns, errs) = renderMessages $ getPsMessages pst

Expand All @@ -1093,17 +1089,9 @@ parseHeader dflags filename contents = do
-- errors are those from which a parse tree just can't
-- be produced.
unless (null errs) $
#if MIN_VERSION_ghc(9,5,0)
throwE $ diagFromErrMsgs sourceParser dflags errs
#else
throwE $ diagFromSDocErrMsgs sourceParser dflags errs
#endif
throwE $ diagFromGhcErrorMessages sourceParser dflags errs

#if MIN_VERSION_ghc(9,5,0)
let warnings = diagFromErrMsgs sourceParser dflags warns
#else
let warnings = diagFromSDocErrMsgs sourceParser dflags warns
#endif
let warnings = diagFromGhcErrorMessages sourceParser dflags warns
return (warnings, rdr_module)

-- | Given a buffer, flags, and file path, produce a
Expand All @@ -1121,11 +1109,7 @@ parseFileContents env customPreprocessor filename ms = do
contents = fromJust $ ms_hspp_buf ms
case unP Compat.parseModule (initParserState (initParserOpts dflags) contents loc) of
PFailedWithErrorMessages msgs ->
#if MIN_VERSION_ghc(9,5,0)
throwE $ diagFromErrMsgs sourceParser dflags $ msgs dflags
#else
throwE $ diagFromSDocErrMsgs sourceParser dflags $ msgs dflags
#endif
throwE $ diagFromGhcErrorMessages sourceParser dflags $ msgs dflags
POk pst rdr_module ->
let
psMessages = getPsMessages pst
Expand Down Expand Up @@ -1159,12 +1143,7 @@ parseFileContents env customPreprocessor filename ms = do
-- errors are those from which a parse tree just can't
-- be produced.
unless (null errors) $
#if MIN_VERSION_ghc(9,5,0)
throwE $ diagFromErrMsgs sourceParser dflags errors
#else
throwE $ diagFromSDocErrMsgs sourceParser dflags errors
#endif

throwE $ diagFromGhcErrorMessages sourceParser dflags errors

-- To get the list of extra source files, we take the list
-- that the parser gave us,
Expand Down Expand Up @@ -1194,11 +1173,7 @@ parseFileContents env customPreprocessor filename ms = do
srcs2 <- liftIO $ filterM doesFileExist srcs1

let pm = ParsedModule ms parsed' srcs2
#if MIN_VERSION_ghc(9,5,0)
warnings = diagFromErrMsgs sourceParser dflags warns
#else
warnings = diagFromSDocErrMsgs sourceParser dflags warns
#endif
warnings = diagFromGhcErrorMessages sourceParser dflags warns
pure (warnings ++ preproc_warning_file_diagnostics, pm)

loadHieFile :: Compat.NameCacheUpdater -> FilePath -> IO GHC.HieFile
Expand Down
18 changes: 17 additions & 1 deletion ghcide/src/Development/IDE/GHC/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
module Development.IDE.GHC.Error
(
-- * Producing Diagnostic values
diagFromErrMsgs
diagFromGhcErrorMessages
, diagFromErrMsgs
, diagFromErrMsg
, diagFromSDocErrMsgs
, diagFromSDocErrMsg
Expand Down Expand Up @@ -72,6 +73,21 @@ diagFromErrMsg diagSource dflags origErr =
in
diagFromSDocWithOptionalOrigMsg diagSource dflags err

-- | Compatibility function for creating '[FileDiagnostic]' from
-- a 'Compat.Bag' of GHC error messages.
-- The function signature changes based on the GHC version.
-- While this is not desirable, it avoids more CPP statements in code
-- that implements actual logic.
#if MIN_VERSION_ghc(9,5,0)
diagFromGhcErrorMessages :: T.Text -> DynFlags -> Compat.Bag (MsgEnvelope GhcMessage) -> [FileDiagnostic]
diagFromGhcErrorMessages sourceParser dflags errs =
diagFromErrMsgs sourceParser dflags errs
#else
diagFromGhcErrorMessages :: T.Text -> DynFlags -> Compat.Bag (MsgEnvelope Compat.DecoratedSDoc) -> [FileDiagnostic]
diagFromGhcErrorMessages sourceParser dflags errs =
diagFromSDocErrMsgs sourceParser dflags errs
#endif

diagFromErrMsgs :: T.Text -> DynFlags -> Compat.Bag (MsgEnvelope GhcMessage) -> [FileDiagnostic]
diagFromErrMsgs diagSource dflags = concatMap (diagFromErrMsg diagSource dflags) . Compat.bagToList

Expand Down

0 comments on commit 1398a0b

Please sign in to comment.