bison-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

tests: fight G++ warnings about zero as null pointer constant


From: Akim Demaille
Subject: tests: fight G++ warnings about zero as null pointer constant
Date: Mon, 17 Sep 2018 18:40:13 +0200

commit 6d3e3199011507884e417027a72603461d28b7f6
Author: Akim Demaille <address@hidden>
Date:   Sat Sep 15 09:16:06 2018 +0200

    tests: fight G++ warnings about zero as null pointer constant
    
    In C++ pre C++11 it is standard practice to use 0 for the null pointer.
    But GCC pre 8 -std=c++98 with -Wzero-as-null-pointer-constant warns about
    this.
    
    So disable -Wzero-as-null-pointer-constant when compiling C++ pre 11.
    Let's do this in AT_DATA_SOURCE_PROLOGUE (which is pasted on top of
    all the test grammar files).  Unfortunately, that shifts all the
    locations in the expected error messages, which would be too noisy.
    Instead, let's introduce testsuite.h, which can vary in length, and
    include it in AT_DATA_SOURCE_PROLOGUE.
    
    * tests/testsuite.h: New.
    Disable -Wzero-as-null-pointer-constant's warning with GCC pre 8,
    C++ pre 11.
    * tests/local.at (AT_DATA_SOURCE_PROLOGUE): Use it.
    * tests/atlocal.in (CPPFLAGS): Find it.
    * tests/local.mk: Ship it.
    * data/c.m4 (YY_NULLPTR): Prefer ((void*)0) to 0 in C.

diff --git a/data/c.m4 b/data/c.m4
index a1c33e9d..3d812304 100644
--- a/data/c.m4
+++ b/data/c.m4
@@ -267,12 +267,19 @@ m4_define([b4_attribute_define],
 # --------------
 # Portability issues: define a YY_NULLPTR appropriate for the current
 # language (C, C++98, or C++11).
+#
+# In C++ pre C++11 it is standard practice to use 0 (not NULL) for the
+# null pointer.  In C, prefer ((void*)0) to avoid having to include stdlib.h.
 m4_define([b4_null_define],
 [# ifndef YY_NULLPTR
-#  if defined __cplusplus && 201103L <= __cplusplus
-#   define YY_NULLPTR nullptr
+#  if defined __cplusplus
+#   if 201103L <= __cplusplus
+#    define YY_NULLPTR nullptr
+#   else
+#    define YY_NULLPTR 0
+#   endif
 #  else
-#   define YY_NULLPTR 0
+#   define YY_NULLPTR ((void*)0)
 #  endif
 # endif[]dnl
 ])
diff --git a/tests/atlocal.in b/tests/atlocal.in
index 8638d1a0..6b24f1ca 100644
--- a/tests/atlocal.in
+++ b/tests/atlocal.in
@@ -16,8 +16,9 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-# We need 'config.h' (builddir/lib), and the gnulib headers (srcdir/lib).
-CPPFLAGS="-I$abs_top_srcdir/lib -I$abs_top_builddir/lib @CPPFLAGS@"
+# We need 'testsuite.h', (srcdir/test), 'config.h' (builddir/lib), and
+# the gnulib headers (srcdir/lib).
+CPPFLAGS="-I$abs_top_srcdir/tests -I$abs_top_srcdir/lib 
-I$abs_top_builddir/lib @CPPFLAGS@"
 
 # Don't just check if $POSIXLY_CORRECT is set, as Bash, when launched
 # as /bin/sh, sets the shell variable POSIXLY_CORRECT to y, but not
diff --git a/tests/local.at b/tests/local.at
index e1c8f353..ab66b00f 100644
--- a/tests/local.at
+++ b/tests/local.at
@@ -347,11 +347,11 @@ m4_define([AT_LANG_DISPATCH],
 # The prologue that should be included in any source code that is
 # meant to be compiled.  Keep atlocal.in sync (BISON_CXX_WORKS).
 m4_define([AT_DATA_SOURCE_PROLOGUE],
-[[#include <config.h>
-/* We don't need perfect functions for these tests. */
-#undef malloc
-#undef memcmp
-#undef realloc
+[[/* Load config.h, and adjust to the compiler.
+  We used to do it here, but each time we add a new line,
+  we have to adjust all the line numbers in error messages.
+  It's simpler to use a constant include to a varying file.  */
+#include <testsuite.h>
 ]])
 
 # AT_DATA_GRAMMAR_PROLOGUE
diff --git a/tests/local.mk b/tests/local.mk
index f480afa4..29106220 100644
--- a/tests/local.mk
+++ b/tests/local.mk
@@ -15,7 +15,7 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-EXTRA_DIST += $(TESTSUITE_AT) tests/testsuite
+EXTRA_DIST += $(TESTSUITE_AT) %D%/testsuite %D%/testsuite.h
 
 DISTCLEANFILES       += %D%/atconfig $(check_SCRIPTS)
 MAINTAINERCLEANFILES += $(TESTSUITE)
diff --git a/tests/testsuite.h b/tests/testsuite.h
new file mode 100644
index 00000000..e4c8298d
--- /dev/null
+++ b/tests/testsuite.h
@@ -0,0 +1,14 @@
+#include <config.h>
+/* We don't need perfect functions for these tests. */
+#undef malloc
+#undef memcmp
+#undef realloc
+
+/* In C++ pre C++11 it is standard practice to use 0 for the null
+   pointer.  But GCC -std=c++98 with -Wzero-as-null-pointer-constant
+   warns about this.  Warning introduced in GCC 4.7. */
+#if defined __cplusplus && __cplusplus < 201103L
+# if defined __GNUC__ && ! defined __clang__ && 407 <= __GNUC__ * 100 + 
__GNUC_MINOR__
+#  pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
+# endif
+#endif




reply via email to

[Prev in Thread] Current Thread [Next in Thread]