Commit 3cb32d8b authored by Erik de Castro Lopo's avatar Erik de Castro Lopo Committed by Ben Gamari

Add -Wcpp-undef warning flag

When enabled, this new warning flag passes `-Wundef` to the C
pre-processor which causes the pre-processor to warn on uses of
the `#if` directive on undefined identifiers.

It is not currently enabled in any of the standard warning groups.

Test Plan: Make sure the two tests pass on all major platforms.

Reviewers: hvr, carter, Phyx, bgamari, austin

Reviewed By: Phyx

Subscribers: thomie

Differential Revision: https://phabricator.haskell.org/D2626

GHC Trac Issues: #12752
parent a662f46c
......@@ -619,6 +619,7 @@ data WarningFlag =
| Opt_WarnMissingPatternSynonymSignatures -- since 8.0
| Opt_WarnUnrecognisedWarningFlags -- since 8.0
| Opt_WarnSimplifiableClassConstraints -- Since 8.2
| Opt_WarnCPPUndef -- Since 8.2
deriving (Eq, Show, Enum)
data Language = Haskell98 | Haskell2010
......@@ -3287,6 +3288,7 @@ wWarningFlagsDeps = [
"it has no effect",
depFlagSpec "auto-orphans" Opt_WarnAutoOrphans
"it has no effect",
flagSpec "cpp-undef" Opt_WarnCPPUndef,
flagSpec "deferred-type-errors" Opt_WarnDeferredTypeErrors,
flagSpec "deferred-out-of-scope-variables"
Opt_WarnDeferredOutOfScopeVariables,
......
......@@ -403,9 +403,8 @@ runCpp :: DynFlags -> [Option] -> IO ()
runCpp dflags args = do
let (p,args0) = pgm_P dflags
args1 = map Option (getOpts dflags opt_P)
args2 = if gopt Opt_WarnIsError dflags
then [Option "-Werror"]
else []
args2 = [Option "-Werror" | gopt Opt_WarnIsError dflags]
++ [Option "-Wundef" | wopt Opt_WarnCPPUndef dflags]
mb_env <- getGccEnv args2
runSomethingFiltered dflags id "C pre-processor" p
(args0 ++ args1 ++ args2 ++ args) mb_env
......
......@@ -56,6 +56,10 @@ Compiler
and the latter code has no restrictions about whether the data constructors
of ``T`` are in scope.
- Add warning flag :ghc-flag:`-Wcpp-undef` which passes ``-Wundef`` to the C
pre-processor causing the pre-processor to warn on uses of the ``#if``
directive on undefined identifiers.
GHCi
~~~~
......
......@@ -1007,6 +1007,12 @@ of ``-W(no-)*``.
be inlined before the rule has a chance to fire. See
:ref:`rules-inline`.
.. ghc-flag:: -Wcpp-undef
This flag passes ``-Wundef`` to the C pre-processor (if its being used)
which causes the pre-processor to warn on uses of the `#if` directive on
undefined identifiers.
If you're feeling really paranoid, the :ghc-flag:`-dcore-lint` option is a good choice.
It turns on heavyweight intra-pass sanity-checking within GHC. (It checks GHC's
sanity, not yours.)
......
{-# LANGUAGE CPP #-}
#if SHOULD_PASS
message :: String
message = "Hello!"
#endif
main :: IO ()
main = putStrLn message
......@@ -491,9 +491,11 @@ test('T12135',
run_command,
['$MAKE -s --no-print-directory T12135'])
test('T12192', normal, run_command, ['mkdir foo && (cd foo && {compiler} -v0 ../T12192)'])
test('T12192', normal, run_command, ['mkdir foo && (cd foo && {compiler} -v0 ../T12192)'])
test('T10923',
extra_clean(['T10923.o', 'T10923.hi']),
run_command,
['$MAKE -s --no-print-directory T10923'])
test('T12752pass', normal, compile, ['-DSHOULD_PASS=1 -Wcpp-undef'])
{-# LANGUAGE CPP #-}
-- This should fail to compile with "ghc -Wcpp-undef -Werror ...".
#if this_cpp_identifier_does_not_exist
message :: String
message = "This is wrong!"
#endif
main :: IO ()
main = putStrLn "Hello"
# --make -o without Main should be an error, not a warning.
test('T10895', normal, multimod_compile_fail, ['T10895.hs', '-v0 -o dummy'])
test('T12752', expect_fail, compile, ['-Wcpp-undef -Werror'])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment