Add "hackAroundIncludes" option (default false)

- Makes brittany work in stdin/stdout mode when
  CPP usage is limited to #includes.
pull/35/head
Lennart Spitzner 2017-05-22 15:49:18 +02:00
parent 3957272c10
commit fd9427754e
3 changed files with 20 additions and 5 deletions

View File

@ -115,6 +115,12 @@ mainCmdParser helpDesc = do
& _ppconf_CPPMode & _ppconf_CPPMode
& runIdentity & runIdentity
& Semigroup.getLast & Semigroup.getLast
-- the flag will do the following: insert a marker string
-- ("-- BRITTANY_INCLUDE_HACK ") right before any lines starting with
-- "#include" before processing (parsing) input; and remove that marker
-- string from the transformation output.
let hackAroundIncludes =
config & _conf_preprocessor & _ppconf_hackAroundIncludes & runIdentity & Semigroup.getLast
let cppCheckFunc dynFlags = if GHC.xopt GHC.Cpp dynFlags let cppCheckFunc dynFlags = if GHC.xopt GHC.Cpp dynFlags
then case cppMode of then case cppMode of
CPPModeAbort -> do CPPModeAbort -> do
@ -130,8 +136,11 @@ mainCmdParser helpDesc = do
return $ Right True return $ Right True
else return $ Right False else return $ Right False
parseResult <- case inputPathM of parseResult <- case inputPathM of
Nothing -> parseModuleFromString ghcOptions "stdin" cppCheckFunc Nothing -> do
=<< System.IO.hGetContents System.IO.stdin let hackF s = if "#include" `isPrefixOf` s then "-- BRITTANY_INCLUDE_HACK " ++ s else s
let hackTransform = if hackAroundIncludes then List.unlines . fmap hackF . List.lines else id
inputString <- System.IO.hGetContents System.IO.stdin
parseModuleFromString ghcOptions "stdin" cppCheckFunc (hackTransform inputString)
Just p -> parseModule ghcOptions p cppCheckFunc Just p -> parseModule ghcOptions p cppCheckFunc
case parseResult of case parseResult of
Left left -> do Left left -> do
@ -150,9 +159,12 @@ mainCmdParser helpDesc = do
-- decl <- someDecls -- decl <- someDecls
-- ExactPrint.exactPrint decl anns -- ExactPrint.exactPrint decl anns
let omitCheck = config & _conf_errorHandling .> _econf_omit_output_valid_check .> confUnpack let omitCheck = config & _conf_errorHandling .> _econf_omit_output_valid_check .> confUnpack
(errsWarns, outLText) <- if hasCPP || omitCheck (errsWarns, outLText) <- do
then return $ pPrintModule config anns parsedSource (ews, outRaw) <- if hasCPP || omitCheck
else pPrintModuleAndCheck config anns parsedSource then return $ pPrintModule config anns parsedSource
else pPrintModuleAndCheck config anns parsedSource
let hackF s = fromMaybe s $ TextL.stripPrefix (TextL.pack "-- BRITTANY_INCLUDE_HACK ") s
pure $ if hackAroundIncludes then (ews, TextL.unlines $ fmap hackF $ TextL.lines outRaw) else (ews, outRaw)
let customErrOrder LayoutWarning{} = 0 :: Int let customErrOrder LayoutWarning{} = 0 :: Int
customErrOrder LayoutErrorOutputCheck{} = 1 customErrOrder LayoutErrorOutputCheck{} = 1
customErrOrder LayoutErrorUnusedComment{} = 2 customErrOrder LayoutErrorUnusedComment{} = 2

View File

@ -97,6 +97,7 @@ configParser = do
} }
, _conf_preprocessor = PreProcessorConfig , _conf_preprocessor = PreProcessorConfig
{ _ppconf_CPPMode = mempty { _ppconf_CPPMode = mempty
, _ppconf_hackAroundIncludes = mempty
} }
, _conf_forward = ForwardOptions , _conf_forward = ForwardOptions
{ _options_ghc = [ optionsGhc & List.unwords & CmdArgs.splitArgs | not $ null optionsGhc ] { _options_ghc = [ optionsGhc & List.unwords & CmdArgs.splitArgs | not $ null optionsGhc ]

View File

@ -99,6 +99,7 @@ data CErrorHandlingConfig f = ErrorHandlingConfig
data CPreProcessorConfig f = PreProcessorConfig data CPreProcessorConfig f = PreProcessorConfig
{ _ppconf_CPPMode :: f (Semigroup.Last CPPMode) { _ppconf_CPPMode :: f (Semigroup.Last CPPMode)
, _ppconf_hackAroundIncludes :: f (Semigroup.Last Bool)
} }
deriving (Generic) deriving (Generic)
@ -334,6 +335,7 @@ staticDefaultConfig = Config
} }
, _conf_preprocessor = PreProcessorConfig , _conf_preprocessor = PreProcessorConfig
{ _ppconf_CPPMode = coerce CPPModeAbort { _ppconf_CPPMode = coerce CPPModeAbort
, _ppconf_hackAroundIncludes = coerce False
} }
, _conf_forward = ForwardOptions , _conf_forward = ForwardOptions
{ _options_ghc = Identity [] { _options_ghc = Identity []