[commit: ghc] master: Add -Wcpp-undef warning flag (3cb32d8)

git at git.haskell.org git at git.haskell.org
Sat Oct 22 20:24:26 UTC 2016


Repository : ssh://git@git.haskell.org/ghc

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/3cb32d8b0b51c548ab424139c66cce6b37a2ab1b/ghc

>---------------------------------------------------------------

commit 3cb32d8b0b51c548ab424139c66cce6b37a2ab1b
Author: Erik de Castro Lopo <erikd at mega-nerd.com>
Date:   Sat Oct 22 15:38:41 2016 -0400

    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


>---------------------------------------------------------------

3cb32d8b0b51c548ab424139c66cce6b37a2ab1b
 compiler/main/DynFlags.hs                    |  2 ++
 compiler/main/SysTools.hs                    |  5 ++---
 docs/users_guide/8.2.1-notes.rst             |  4 ++++
 docs/users_guide/using-warnings.rst          |  6 ++++++
 testsuite/tests/driver/T12752pass.hs         |  9 +++++++++
 testsuite/tests/driver/all.T                 |  2 ++
 testsuite/tests/driver/should_fail/T12752.hs | 10 ++++++++++
 testsuite/tests/driver/should_fail/all.T     |  2 ++
 8 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/compiler/main/DynFlags.hs b/compiler/main/DynFlags.hs
index 7978c03..cd8dc41 100644
--- a/compiler/main/DynFlags.hs
+++ b/compiler/main/DynFlags.hs
@@ -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,
diff --git a/compiler/main/SysTools.hs b/compiler/main/SysTools.hs
index dd98883..d5fd0c5 100644
--- a/compiler/main/SysTools.hs
+++ b/compiler/main/SysTools.hs
@@ -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
diff --git a/docs/users_guide/8.2.1-notes.rst b/docs/users_guide/8.2.1-notes.rst
index c176a08..8988630 100644
--- a/docs/users_guide/8.2.1-notes.rst
+++ b/docs/users_guide/8.2.1-notes.rst
@@ -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
 ~~~~
 
diff --git a/docs/users_guide/using-warnings.rst b/docs/users_guide/using-warnings.rst
index c07058a..c9216b9 100644
--- a/docs/users_guide/using-warnings.rst
+++ b/docs/users_guide/using-warnings.rst
@@ -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.)
diff --git a/testsuite/tests/driver/T12752pass.hs b/testsuite/tests/driver/T12752pass.hs
new file mode 100644
index 0000000..5826d48
--- /dev/null
+++ b/testsuite/tests/driver/T12752pass.hs
@@ -0,0 +1,9 @@
+{-# LANGUAGE CPP #-}
+
+#if SHOULD_PASS
+message :: String
+message = "Hello!"
+#endif
+
+main :: IO ()
+main = putStrLn message
diff --git a/testsuite/tests/driver/all.T b/testsuite/tests/driver/all.T
index c6283df..8cd5c2f 100644
--- a/testsuite/tests/driver/all.T
+++ b/testsuite/tests/driver/all.T
@@ -497,3 +497,5 @@ 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'])
diff --git a/testsuite/tests/driver/should_fail/T12752.hs b/testsuite/tests/driver/should_fail/T12752.hs
new file mode 100644
index 0000000..3560d00
--- /dev/null
+++ b/testsuite/tests/driver/should_fail/T12752.hs
@@ -0,0 +1,10 @@
+{-# 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"
diff --git a/testsuite/tests/driver/should_fail/all.T b/testsuite/tests/driver/should_fail/all.T
index f068d65..3d0708b 100644
--- a/testsuite/tests/driver/should_fail/all.T
+++ b/testsuite/tests/driver/should_fail/all.T
@@ -1,2 +1,4 @@
 # --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'])



More information about the ghc-commits mailing list