on master help sign in
Add an option to tolerate CPP pragmas
commitc5b9fa11683cbf0a57d6f12ff9da057806a56baf authormrkkrp <mark.karpov@tweag.io> dateSun 14 Jul 2019, 13:05 (7 years ago) parent62cbd42
5 files changed+19−11
app/Main.hs+5 nix/ormolize/ormolize.sh+1−4 src/Ormolu.hs+2−2 src/Ormolu/Config.hs+5 src/Ormolu/Parser.hs+6−5
app/Main.hs+5
@@ -132,6 +132,11 @@ configParser = Config
132132 , short 'd'
133133 , help "Output information useful for debugging"
134134 ]
135+ <*> (switch . mconcat)
136+ [ long "tolerate-cpp"
137+ , short 'p'
138+ , help "Do not fail if CPP pragma is present"
139+ ]
135140
136141----------------------------------------------------------------------------
137142-- Helpers
nix/ormolize/ormolize.sh+1−4
@@ -5,9 +5,6 @@ set -euo pipefail
55# drop includes
66sed -i '/^#include/d' "$1"
77
8−# drop CPP
9−sed -i '/^{-# LANGUAGE CPP/d' "$1"
10−
118# deal with CPP in a fairly straightforward way
129cpphs "$1" --noline > "$1-nocpp" 2> /dev/null
1310
@@ -18,4 +15,4 @@ mv "$1-nocpp" "$1"
1815cp "$1" "$1-original"
1916
2017# 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'
109109 -> FilePath -- ^ File name to use in errors
110110 -> String -- ^ Actual input for the parser
111111 -> m ([GHC.Warn], ParseResult)
112−parseModule' Config {..} mkException path str = do
113− (ws, r) <- parseModule cfgDynOptions path str
112+parseModule' cfg mkException path str = do
113+ (ws, r) <- parseModule cfg path str
114114 case r of
115115 Left (spn, err) -> liftIO $ throwIO (mkException spn err)
116116 Right x -> return (ws, x)
src/Ormolu/Config.hs+5
@@ -23,6 +23,10 @@ data Config = Config
2323 -- ^ Do formatting faster but without automatic detection of defects
2424 , cfgDebug :: !Bool
2525 -- ^ 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)
2630 } deriving (Eq, Show)
2731
2832-- | Default 'Config'.
@@ -32,6 +36,7 @@ defaultConfig = Config
3236 { cfgDynOptions = []
3337 , cfgUnsafe = False
3438 , cfgDebug = False
39+ , cfgTolerateCpp = False
3540 }
3641
3742-- | A wrapper for dynamic options.
src/Ormolu/Parser.hs+6−5
@@ -37,23 +37,24 @@ import qualified StringBuffer as GHC
3737
3838parseModule
3939 :: MonadIO m
40− => [DynOption] -- ^ Dynamic options that affect parsing
40+ => Config -- ^ Ormolu configuration
4141 -> FilePath -- ^ File name (only for source location annotations)
4242 -> String -- ^ Input for parser
4343 -> m ( [GHC.Warn]
4444 , Either (SrcSpan, String) ParseResult
4545 )
46−parseModule dynOpts path input' = liftIO $ do
46+parseModule Config {..} path input' = liftIO $ do
4747 let (input, extraComments) = stripLinePragmas input'
4848 (ws, dynFlags) <- ghcWrapper $ do
4949 dynFlags0 <- initDynFlagsPure path input
50− (dynFlags1, _, ws) <-
51− GHC.parseDynamicFilePragma dynFlags0 (dynOptionToLocatedStr <$> dynOpts)
50+ (dynFlags1, _, ws) <- GHC.parseDynamicFilePragma
51+ dynFlags0
52+ (dynOptionToLocatedStr <$> cfgDynOptions)
5253 return (ws, dynFlags1)
5354 -- NOTE It's better to throw this outside of 'ghcWrapper' because
5455 -- otherwise the exception will be wrapped as a GHC panic, which we don't
5556 -- want.
56− when (GHC.xopt Cpp dynFlags) $
57+ when (GHC.xopt Cpp dynFlags && not cfgTolerateCpp) $
5758 throwIO (OrmoluCppEnabled path)
5859 let r = case runParser GHC.parseModule dynFlags path input of
5960 GHC.PFailed _ ss m ->