on master help sign in
Add an option to tolerate CPP pragmas
c5b9fa1mrkkrp
app/Main.hs+5
@@ -132,6 +132,11 @@ configParser = Config
132 , short 'd'
132 , short 'd'
133 , help "Output information useful for debugging"
133 , help "Output information useful for debugging"
134 ]
134 ]
135+ <*> (switch . mconcat)
136+ [ long "tolerate-cpp"
137+ , short 'p'
138+ , help "Do not fail if CPP pragma is present"
139+ ]
135
140
136----------------------------------------------------------------------------
141----------------------------------------------------------------------------
137-- Helpers
142-- Helpers
nix/ormolize/ormolize.sh+1−4
@@ -5,9 +5,6 @@ set -euo pipefail
5# drop includes
5# drop includes
6sed -i '/^#include/d' "$1"
6sed -i '/^#include/d' "$1"
7
7
8−# drop CPP
9−sed -i '/^{-# LANGUAGE CPP/d' "$1"
10−
11# deal with CPP in a fairly straightforward way
8# deal with CPP in a fairly straightforward way
12cpphs "$1" --noline > "$1-nocpp" 2> /dev/null
9cpphs "$1" --noline > "$1-nocpp" 2> /dev/null
13
10
@@ -18,4 +15,4 @@ mv "$1-nocpp" "$1"
18cp "$1" "$1-original"
15cp "$1" "$1-original"
19
16
20# run ormolu
17# run ormolu
21−ormolu -m inplace "$1" || ormolu -u -m inplace "$1"
18+ormolu --tolerate-cpp --mode inplace "$1" || ormolu --tolerate-cpp --unsafe --mode inplace "$1"
src/Ormolu.hs+2−2
@@ -109,8 +109,8 @@ parseModule'
109 -> FilePath -- ^ File name to use in errors
109 -> FilePath -- ^ File name to use in errors
110 -> String -- ^ Actual input for the parser
110 -> String -- ^ Actual input for the parser
111 -> m ([GHC.Warn], ParseResult)
111 -> m ([GHC.Warn], ParseResult)
112−parseModule' Config {..} mkException path str = do
112+parseModule' cfg mkException path str = do
113− (ws, r) <- parseModule cfgDynOptions path str
113+ (ws, r) <- parseModule cfg path str
114 case r of
114 case r of
115 Left (spn, err) -> liftIO $ throwIO (mkException spn err)
115 Left (spn, err) -> liftIO $ throwIO (mkException spn err)
116 Right x -> return (ws, x)
116 Right x -> return (ws, x)
src/Ormolu/Config.hs+5
@@ -23,6 +23,10 @@ data Config = Config
23 -- ^ Do formatting faster but without automatic detection of defects
23 -- ^ Do formatting faster but without automatic detection of defects
24 , cfgDebug :: !Bool
24 , cfgDebug :: !Bool
25 -- ^ Output information useful for debugging
25 -- ^ Output information useful for debugging
26+ , cfgTolerateCpp :: !Bool
27+ -- ^ Do not fail if CPP pragma is present (still doesn't handle CPP but
28+ -- useful for formatting of files that enable the extension without
29+ -- actually containing CPP macros)
26 } deriving (Eq, Show)
30 } deriving (Eq, Show)
27
31
28-- | Default 'Config'.
32-- | Default 'Config'.
@@ -32,6 +36,7 @@ defaultConfig = Config
32 { cfgDynOptions = []
36 { cfgDynOptions = []
33 , cfgUnsafe = False
37 , cfgUnsafe = False
34 , cfgDebug = False
38 , cfgDebug = False
39+ , cfgTolerateCpp = False
35 }
40 }
36
41
37-- | A wrapper for dynamic options.
42-- | A wrapper for dynamic options.
src/Ormolu/Parser.hs+6−5
@@ -37,23 +37,24 @@ import qualified StringBuffer as GHC
37
37
38parseModule
38parseModule
39 :: MonadIO m
39 :: MonadIO m
40− => [DynOption] -- ^ Dynamic options that affect parsing
40+ => Config -- ^ Ormolu configuration
41 -> FilePath -- ^ File name (only for source location annotations)
41 -> FilePath -- ^ File name (only for source location annotations)
42 -> String -- ^ Input for parser
42 -> String -- ^ Input for parser
43 -> m ( [GHC.Warn]
43 -> m ( [GHC.Warn]
44 , Either (SrcSpan, String) ParseResult
44 , Either (SrcSpan, String) ParseResult
45 )
45 )
46−parseModule dynOpts path input' = liftIO $ do
46+parseModule Config {..} path input' = liftIO $ do
47 let (input, extraComments) = stripLinePragmas input'
47 let (input, extraComments) = stripLinePragmas input'
48 (ws, dynFlags) <- ghcWrapper $ do
48 (ws, dynFlags) <- ghcWrapper $ do
49 dynFlags0 <- initDynFlagsPure path input
49 dynFlags0 <- initDynFlagsPure path input
50− (dynFlags1, _, ws) <-
51− GHC.parseDynamicFilePragma dynFlags0 (dynOptionToLocatedStr <$> dynOpts)
50+ (dynFlags1, _, ws) <- GHC.parseDynamicFilePragma
51+ dynFlags0
52+ (dynOptionToLocatedStr <$> cfgDynOptions)
52 return (ws, dynFlags1)
53 return (ws, dynFlags1)
53 -- NOTE It's better to throw this outside of 'ghcWrapper' because
54 -- NOTE It's better to throw this outside of 'ghcWrapper' because
54 -- otherwise the exception will be wrapped as a GHC panic, which we don't
55 -- otherwise the exception will be wrapped as a GHC panic, which we don't
55 -- want.
56 -- want.
56− when (GHC.xopt Cpp dynFlags) $
57+ when (GHC.xopt Cpp dynFlags && not cfgTolerateCpp) $
57 throwIO (OrmoluCppEnabled path)
58 throwIO (OrmoluCppEnabled path)
58 let r = case runParser GHC.parseModule dynFlags path input of
59 let r = case runParser GHC.parseModule dynFlags path input of
59 GHC.PFailed _ ss m ->
60 GHC.PFailed _ ss m ->