On 2/6/23 18:30, Paul Eggert wrote:
1. Since the nullptr issue affects C++ so much (something I wasn't aware of until now), I'm inclined to rename the module from c-nullptr to nullptr as was hinted earlier. It's not just C so the "c-" is a bit misleading.

2. The issues you mentioned about C++ compilers possibly not supporting nullptr seem to be serious enough that it seems that we should test the C++ compiler at configure time, as we already test the C compiler. That way we wouldn't have to worry whether our __cplusplus-related #ifdefs are OK.

I installed the attached to try to address these issues. The third patch fixes some problems I ran into when using the first two on diffutils and groff. I pushed the diffutils change onto Savannah (but not groff of course). Comments welcome, as quite possibly there are further opportunities for improvement.
From 96d3aaacc5aaa0e325eff344dd7ae0e0fbf4b418 Mon Sep 17 00:00:00 2001
From: Paul Eggert <egg...@cs.ucla.edu>
Date: Tue, 7 Feb 2023 14:30:54 -0800
Subject: [PATCH 1/3] nullptr: rename from c-nullptr

* NEWS, doc/gnulib.texi: Mention this.
* m4/nullptr.m4: Rename from m4/c-nullptr.m4.
(gl_NULLPTR): Rename from gl_C_NULLPTR.
* modules/nullptr: Rename from modules/nullptr.
* modules/nullptr-c++-tests: Rename from modules/c-nullptr-c++-tests.
* modules/nullptr-tests: Rename from modules/c-nullptr-tests.
All uses changed.
---
 ChangeLog                                          | 11 +++++++++++
 NEWS                                               |  2 ++
 doc/gnulib.texi                                    | 11 ++++++-----
 m4/{c-nullptr.m4 => nullptr.m4}                    |  2 +-
 modules/{c-nullptr => nullptr}                     |  4 ++--
 modules/{c-nullptr-c++-tests => nullptr-c++-tests} |  0
 modules/{c-nullptr-tests => nullptr-tests}         |  2 +-
 7 files changed, 23 insertions(+), 9 deletions(-)
 rename m4/{c-nullptr.m4 => nullptr.m4} (98%)
 rename modules/{c-nullptr => nullptr} (82%)
 rename modules/{c-nullptr-c++-tests => nullptr-c++-tests} (100%)
 rename modules/{c-nullptr-tests => nullptr-tests} (87%)

diff --git a/ChangeLog b/ChangeLog
index b6eec570be..defcef16e3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2023-02-07  Paul Eggert  <egg...@cs.ucla.edu>
+
+	nullptr: rename from c-nullptr
+	* NEWS, doc/gnulib.texi: Mention this.
+	* m4/nullptr.m4: Rename from m4/c-nullptr.m4.
+	(gl_NULLPTR): Rename from gl_C_NULLPTR.
+	* modules/nullptr: Rename from modules/nullptr.
+	* modules/nullptr-c++-tests: Rename from modules/c-nullptr-c++-tests.
+	* modules/nullptr-tests: Rename from modules/c-nullptr-tests.
+	All uses changed.
+
 2023-02-07  Bruno Haible  <br...@clisp.org>
 
 	Fix a copyright header - module license mismatch.
diff --git a/NEWS b/NEWS
index f3b508f950..0ec377915c 100644
--- a/NEWS
+++ b/NEWS
@@ -74,6 +74,8 @@ User visible incompatible changes
 
 Date        Modules         Changes
 
+2023-02-07  c-nullptr       Rename this module to nullptr.
+
 2023-01-21  getprogname     The include file is changed from "getprogname.h"
                             to <stdlib.h>.
 
diff --git a/doc/gnulib.texi b/doc/gnulib.texi
index b3f011818c..6fe2678834 100644
--- a/doc/gnulib.texi
+++ b/doc/gnulib.texi
@@ -920,13 +920,13 @@ On pre-C23 platforms, the keyword substitutes assume C99 or later.
 @node nullptr
 @section @code{nullptr}
 
-Gnulib module: c-nullptr
+Gnulib module: nullptr
 
 @cindex null pointer
-The @code{c-nullptr} module arranges for @code{nullptr} to act
-like standard C@.
+The @code{nullptr} module arranges for @code{nullptr} to act
+like standard C and C++.
 
-The C @code{nullptr} keyword yields a null pointer.  It differs from
+The @code{nullptr} keyword yields a null pointer.  It differs from
 the @code{NULL} macro, in that @code{NULL} might be an integer whereas
 @code{nullptr} is of a special @code{nullptr_t} type with only one
 value, namely @code{nullptr} itself.  Using @code{nullptr} can help
@@ -939,7 +939,8 @@ Portability problems fixed by Gnulib:
 @itemize
 @item
 Some platforms lack @code{nullptr}:
-GCC 12, Clang 15, and other pre-2023 C compilers.
+For C: GCC 12, Clang 15, and other pre-2023 C compilers.
+For C++: pre-2011 C++ compilers.
 @end itemize
 
 Portability problems not fixed by Gnulib:
diff --git a/m4/c-nullptr.m4 b/m4/nullptr.m4
similarity index 98%
rename from m4/c-nullptr.m4
rename to m4/nullptr.m4
index 960eeff18d..e1218ccd49 100644
--- a/m4/c-nullptr.m4
+++ b/m4/nullptr.m4
@@ -5,7 +5,7 @@ dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
 dnl with or without modifications, as long as this notice is preserved.
 
-AC_DEFUN([gl_C_NULLPTR],
+AC_DEFUN([gl_NULLPTR],
 [
   AC_CACHE_CHECK([for nullptr], [gl_cv_c_nullptr],
     [AC_COMPILE_IFELSE(
diff --git a/modules/c-nullptr b/modules/nullptr
similarity index 82%
rename from modules/c-nullptr
rename to modules/nullptr
index bc5fa43200..41330854b9 100644
--- a/modules/c-nullptr
+++ b/modules/nullptr
@@ -2,12 +2,12 @@ Description:
 A nullptr that is like C23.
 
 Files:
-m4/c-nullptr.m4
+m4/nullptr.m4
 
 Depends-on:
 
 configure.ac:
-gl_C_NULLPTR
+gl_NULLPTR
 
 Makefile.am:
 
diff --git a/modules/c-nullptr-c++-tests b/modules/nullptr-c++-tests
similarity index 100%
rename from modules/c-nullptr-c++-tests
rename to modules/nullptr-c++-tests
diff --git a/modules/c-nullptr-tests b/modules/nullptr-tests
similarity index 87%
rename from modules/c-nullptr-tests
rename to modules/nullptr-tests
index 40e28d200b..21a7caf00d 100644
--- a/modules/c-nullptr-tests
+++ b/modules/nullptr-tests
@@ -3,7 +3,7 @@ tests/test-nullptr.c
 tests/macros.h
 
 Depends-on:
-c-nullptr-c++-tests
+nullptr-c++-tests
 
 configure.ac:
 
-- 
2.39.1

From 7a5061a0527b9fcaeedf48e8f5056dd80e77aa98 Mon Sep 17 00:00:00 2001
From: Paul Eggert <egg...@cs.ucla.edu>
Date: Tue, 7 Feb 2023 15:11:32 -0800
Subject: [PATCH 2/3] nullptr: test for C++ nullptr at configure-time

* m4/nullptr.m4 (gl_NULLPTR): Test for C++ support for nullptr
at configure-time, as we already do for C support.
This should be more reliable than maintaining #ifdefs by hand.
---
 ChangeLog     |  5 +++++
 m4/nullptr.m4 | 48 ++++++++++++++++++++++++------------------------
 2 files changed, 29 insertions(+), 24 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index defcef16e3..624afb7e95 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2023-02-07  Paul Eggert  <egg...@cs.ucla.edu>
 
+	nullptr: test for C++ nullptr at configure-time
+	* m4/nullptr.m4 (gl_NULLPTR): Test for C++ support for nullptr
+	at configure-time, as we already do for C support.
+	This should be more reliable than maintaining #ifdefs by hand.
+
 	nullptr: rename from c-nullptr
 	* NEWS, doc/gnulib.texi: Mention this.
 	* m4/nullptr.m4: Rename from m4/c-nullptr.m4.
diff --git a/m4/nullptr.m4 b/m4/nullptr.m4
index e1218ccd49..dda7646906 100644
--- a/m4/nullptr.m4
+++ b/m4/nullptr.m4
@@ -1,4 +1,4 @@
-# Check for nullptr that conforms to C23.
+# Check for nullptr that conforms to C23 and C++11.
 
 dnl Copyright 2023 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
@@ -7,35 +7,35 @@ dnl with or without modifications, as long as this notice is preserved.
 
 AC_DEFUN([gl_NULLPTR],
 [
-  AC_CACHE_CHECK([for nullptr], [gl_cv_c_nullptr],
-    [AC_COMPILE_IFELSE(
-       [AC_LANG_SOURCE([[int *p = nullptr;]])],
-       [gl_cv_c_nullptr=yes],
-       [gl_cv_c_nullptr=no])])
+  AS_IF([test ${CC+set}],
+    [AC_CACHE_CHECK([for C nullptr], [gl_cv_c_nullptr],
+       [AC_COMPILE_IFELSE(
+          [AC_LANG_SOURCE([[int *p = nullptr;]])],
+          [gl_cv_c_nullptr=yes],
+          [gl_cv_c_nullptr=no])])])
   if test "$gl_cv_c_nullptr" = yes; then
-    AC_DEFINE([HAVE_C_NULLPTR], [1], [Define to 1 if nullptr works.])
+    AC_DEFINE([HAVE_C_NULLPTR], [1], [Define to 1 if C nullptr works.])
+  fi
+  AS_IF([test ${CXX+set}],
+    [AC_CACHE_CHECK([for C++ nullptr], [gl_cv_cxx_nullptr],
+       [AC_COMPILE_IFELSE(
+          [AC_LANG_SOURCE([[int *p = nullptr;]])],
+          [gl_cv_cxx_nullptr=yes],
+          [gl_cv_cxx_nullptr=no])])])
+  if test "$gl_cv_cxx_nullptr" = yes; then
+    AC_DEFINE([HAVE_CXX_NULLPTR], [1], [Define to 1 if C++ nullptr works.])
   fi
 ])
 
   AH_VERBATIM([nullptr],
 [#ifndef nullptr /* keep config.h idempotent */
-# ifdef __cplusplus
-/* For the C++ compiler the result of the configure test is irrelevant.
-   We know that at least g++ and clang with option -std=c++11 or higher, as well
-   as MSVC 14 or newer, already have nullptr.  */
-#  if !(((defined __GNUC__ || defined __clang__) && __cplusplus >= 201103L) \
-        || (defined _MSC_VER && 1900 <= _MSC_VER))
-/* Define nullptr as a macro, the best we can.  */
-#   if 3 <= __GNUG__
-#    define nullptr __null
-#   else
-#    define nullptr 0L
-#   endif
-#  endif
-# else
-/* For the C compiler, use the result of the configure test.  */
-#  ifndef HAVE_C_NULLPTR
-#   define nullptr ((void *) 0)
+# if !defined __cplusplus && !defined HAVE_C_NULLPTR
+#  define nullptr ((void *) 0)
+# elif defined __cplusplus && !defined HAVE_CXX_NULLPTR
+#  if 3 <= __GNUG__
+#   define nullptr __null
+#  else
+#   define nullptr 0L
 #  endif
 # endif
 #endif])
-- 
2.39.1

From 97f54920a3c2bd9a536ef8c1cb7667584bd84eaf Mon Sep 17 00:00:00 2001
From: Paul Eggert <egg...@cs.ucla.edu>
Date: Tue, 7 Feb 2023 20:37:09 -0800
Subject: [PATCH 3/3] nullptr: test for compilers at autoconf-time

* m4/nullptr.m4 (gl_NULLPTR): Test for C and C++ compiler usage at
autoconf-time.  This should work better than testing at
configure-time.  Also, push and pop languages so we check the
proper compiler.
---
 ChangeLog     |  6 ++++++
 m4/nullptr.m4 | 28 +++++++++++++++++++---------
 2 files changed, 25 insertions(+), 9 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 624afb7e95..09317e6b89 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2023-02-07  Paul Eggert  <egg...@cs.ucla.edu>
 
+	nullptr: test for compilers at autoconf-time
+	* m4/nullptr.m4 (gl_NULLPTR): Test for C and C++ compiler usage at
+	autoconf-time.  This should work better than testing at
+	configure-time.  Also, push and pop languages so we check the
+	proper compiler.
+
 	nullptr: test for C++ nullptr at configure-time
 	* m4/nullptr.m4 (gl_NULLPTR): Test for C++ support for nullptr
 	at configure-time, as we already do for C support.
diff --git a/m4/nullptr.m4 b/m4/nullptr.m4
index dda7646906..a0b438be94 100644
--- a/m4/nullptr.m4
+++ b/m4/nullptr.m4
@@ -7,22 +7,32 @@ dnl with or without modifications, as long as this notice is preserved.
 
 AC_DEFUN([gl_NULLPTR],
 [
-  AS_IF([test ${CC+set}],
-    [AC_CACHE_CHECK([for C nullptr], [gl_cv_c_nullptr],
+  m4_provide_if([AC_PROG_CC],
+    [AC_LANG_PUSH([C])
+     AC_CACHE_CHECK([for C nullptr], [gl_cv_c_nullptr],
        [AC_COMPILE_IFELSE(
           [AC_LANG_SOURCE([[int *p = nullptr;]])],
           [gl_cv_c_nullptr=yes],
-          [gl_cv_c_nullptr=no])])])
-  if test "$gl_cv_c_nullptr" = yes; then
-    AC_DEFINE([HAVE_C_NULLPTR], [1], [Define to 1 if C nullptr works.])
+          [gl_cv_c_nullptr=no])])
+      gl_c_nullptr=$gl_cv_c_nullptr
+      AC_LANG_POP([C])],
+     [gl_c_nullptr=no])
+  if test "$gl_c_nullptr" = yes; then
+    AC_DEFINE([HAVE_C_NULLPTR], [1],
+      [Define to 1 if C nullptr is known to work.])
   fi
-  AS_IF([test ${CXX+set}],
-    [AC_CACHE_CHECK([for C++ nullptr], [gl_cv_cxx_nullptr],
+
+  m4_provide_if([AC_PROG_CXX],
+    [AC_LANG_PUSH([C++])
+     AC_CACHE_CHECK([for C++ nullptr], [gl_cv_cxx_nullptr],
        [AC_COMPILE_IFELSE(
           [AC_LANG_SOURCE([[int *p = nullptr;]])],
           [gl_cv_cxx_nullptr=yes],
-          [gl_cv_cxx_nullptr=no])])])
-  if test "$gl_cv_cxx_nullptr" = yes; then
+          [gl_cv_cxx_nullptr=no])])
+      gl_cxx_nullptr=$gl_cv_cxx_nullptr
+      AC_LANG_POP([C++])],
+     [gl_cxx_nullptr=no])
+  if test "$gl_cxx_nullptr" = yes; then
     AC_DEFINE([HAVE_CXX_NULLPTR], [1], [Define to 1 if C++ nullptr works.])
   fi
 ])
-- 
2.39.1

Reply via email to