Skip to content

Commit

Permalink
Erase lines affected by CPP before attempting to parse pragmas and im…
Browse files Browse the repository at this point in the history
…ports
  • Loading branch information
mrkkrp committed Jun 7, 2023
1 parent 0fbbb11 commit 4e0fef3
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
component being formatted it will still be taken into account correctly.
[Issue 1037](https://github.com/tweag/ormolu/issues/1037).

* Ormolu no longer fails when CPP directly follows the import section (a
regression introduced in 0.7.0.0). [Issue
1040](https://github.com/tweag/ormolu/issues/1040).

## Ormolu 0.7.0.0

* Inference of operator fixity information is now more precise and takes
Expand Down
8 changes: 8 additions & 0 deletions data/examples/other/cpp/cpp-and-imports-out.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{-# LANGUAGE CPP #-}

module Bug where

#ifdef flag
constant :: Int
constant = 1312
#endif
8 changes: 8 additions & 0 deletions data/examples/other/cpp/cpp-and-imports.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{-# LANGUAGE CPP #-}

module Bug where

#ifdef flag
constant :: Int
constant = 1312
#endif
3 changes: 2 additions & 1 deletion src/Ormolu/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import Ormolu.Imports (normalizeImports)
import Ormolu.Parser.CommentStream
import Ormolu.Parser.Result
import Ormolu.Processing.Common
import Ormolu.Processing.Cpp (eraseCppLines)
import Ormolu.Processing.Preprocess
import Ormolu.Utils (incSpanLine, showOutputable, textToStringBuffer)

Expand Down Expand Up @@ -81,7 +82,7 @@ parseModule config@Config {..} packageFixityMap path rawInput = liftIO $ do
GHC.Opt_Haddock
(setDefaultExts baseDynFlags)
extraOpts = dynOptionToLocatedStr <$> cfgDynOptions
rawInputStringBuffer = textToStringBuffer rawInput
rawInputStringBuffer = textToStringBuffer (eraseCppLines rawInput)
beginningLoc =
mkSrcSpan
(mkSrcLoc (GHC.mkFastString path) 1 1)
Expand Down
12 changes: 12 additions & 0 deletions src/Ormolu/Processing/Cpp.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
-- | Support for CPP.
module Ormolu.Processing.Cpp
( cppLines,
eraseCppLines,
)
where

Expand Down Expand Up @@ -60,3 +61,14 @@ cppLines input = IntSet.fromAscList $ go Outside (T.lines input `zip` [1 ..])
inConditional = case state of
InConditional {} -> True
_ -> False

-- | Replace all lines affected by CPP with blank lines.
eraseCppLines :: Text -> Text
eraseCppLines input =
T.unlines . fmap eraseCpp $ T.lines input `zip` [1 ..]
where
linesToErase = cppLines input
eraseCpp (x, i) =
if i `IntSet.member` linesToErase
then "\n"
else x
2 changes: 1 addition & 1 deletion src/Ormolu/Processing/Preprocess.hs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ linesNotToFormat cppEnabled region@RegionDeltas {..} input =
totalLines = length (T.lines input)
regionLines = linesInRegion region input
(magicDisabled, lineUpdates) = magicDisabledLines regionLines
otherDisabled = (mconcat allLines) regionLines
otherDisabled = mconcat allLines regionLines
where
allLines = [shebangLines, linePragmaLines] <> [cppLines | cppEnabled]

Expand Down

0 comments on commit 4e0fef3

Please sign in to comment.