Skip to content

Commit 4e0fef3

Browse files
committed
Erase lines affected by CPP before attempting to parse pragmas and imports
1 parent 0fbbb11 commit 4e0fef3

File tree

6 files changed

+35
-2
lines changed

6 files changed

+35
-2
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
component being formatted it will still be taken into account correctly.
1515
[Issue 1037](https://github.com/tweag/ormolu/issues/1037).
1616

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

1923
* Inference of operator fixity information is now more precise and takes
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{-# LANGUAGE CPP #-}
2+
3+
module Bug where
4+
5+
#ifdef flag
6+
constant :: Int
7+
constant = 1312
8+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{-# LANGUAGE CPP #-}
2+
3+
module Bug where
4+
5+
#ifdef flag
6+
constant :: Int
7+
constant = 1312
8+
#endif

src/Ormolu/Parser.hs

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import Ormolu.Imports (normalizeImports)
5454
import Ormolu.Parser.CommentStream
5555
import Ormolu.Parser.Result
5656
import Ormolu.Processing.Common
57+
import Ormolu.Processing.Cpp (eraseCppLines)
5758
import Ormolu.Processing.Preprocess
5859
import Ormolu.Utils (incSpanLine, showOutputable, textToStringBuffer)
5960

@@ -81,7 +82,7 @@ parseModule config@Config {..} packageFixityMap path rawInput = liftIO $ do
8182
GHC.Opt_Haddock
8283
(setDefaultExts baseDynFlags)
8384
extraOpts = dynOptionToLocatedStr <$> cfgDynOptions
84-
rawInputStringBuffer = textToStringBuffer rawInput
85+
rawInputStringBuffer = textToStringBuffer (eraseCppLines rawInput)
8586
beginningLoc =
8687
mkSrcSpan
8788
(mkSrcLoc (GHC.mkFastString path) 1 1)

src/Ormolu/Processing/Cpp.hs

+12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
-- | Support for CPP.
44
module Ormolu.Processing.Cpp
55
( cppLines,
6+
eraseCppLines,
67
)
78
where
89

@@ -60,3 +61,14 @@ cppLines input = IntSet.fromAscList $ go Outside (T.lines input `zip` [1 ..])
6061
inConditional = case state of
6162
InConditional {} -> True
6263
_ -> False
64+
65+
-- | Replace all lines affected by CPP with blank lines.
66+
eraseCppLines :: Text -> Text
67+
eraseCppLines input =
68+
T.unlines . fmap eraseCpp $ T.lines input `zip` [1 ..]
69+
where
70+
linesToErase = cppLines input
71+
eraseCpp (x, i) =
72+
if i `IntSet.member` linesToErase
73+
then "\n"
74+
else x

src/Ormolu/Processing/Preprocess.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ linesNotToFormat cppEnabled region@RegionDeltas {..} input =
104104
totalLines = length (T.lines input)
105105
regionLines = linesInRegion region input
106106
(magicDisabled, lineUpdates) = magicDisabledLines regionLines
107-
otherDisabled = (mconcat allLines) regionLines
107+
otherDisabled = mconcat allLines regionLines
108108
where
109109
allLines = [shebangLines, linePragmaLines] <> [cppLines | cppEnabled]
110110

0 commit comments

Comments
 (0)