c-nullptr: new module
authorPaul Eggert <eggert@cs.ucla.edu>
Sun, 5 Feb 2023 17:47:12 +0000 (09:47 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Sun, 5 Feb 2023 20:13:26 +0000 (12:13 -0800)
* doc/gnulib.texi (nullptr): New section.
* doc/posix-headers/stddef.texi: Document lack of nullptr_t.
* m4/c-nullptr.m4, modules/c-nullptr: New files.

ChangeLog
doc/gnulib.texi
doc/posix-headers/stddef.texi
m4/c-nullptr.m4 [new file with mode: 0644]
modules/c-nullptr [new file with mode: 0644]

index 3cc97a8c34f359727ede0ecf623640215861a394..51a731f115b9b9e1719fd75848504580d89e8ed5 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2023-02-05  Paul Eggert  <eggert@cs.ucla.edu>
+
+       c-nullptr: new module
+       * doc/gnulib.texi (nullptr): New section.
+       * doc/posix-headers/stddef.texi: Document lack of nullptr_t.
+       * m4/c-nullptr.m4, modules/c-nullptr: New files.
+
 2023-02-05  Bruno Haible  <bruno@clisp.org>
 
        Update build-aux/po/Makefile.in.in.
index e9a4ace67fdf77a03a4ff5977548cc481822b6b0..b3f011818c60c28be407ade45c715335388cf108 100644 (file)
@@ -867,6 +867,7 @@ substituted by Gnulib.
 @menu
 * alignof::        @code{alignas} and @code{alignof}
 * bool::           @code{bool}, @code{false}, and @code{true}
+* nullptr::        @code{nullptr}
 * static_assert::  @code{static_assert}
 @end menu
 
@@ -916,6 +917,47 @@ On pre-C23 platforms, the keyword substitutes are macros.
 On pre-C23 platforms, the keyword substitutes assume C99 or later.
 @end itemize
 
+@node nullptr
+@section @code{nullptr}
+
+Gnulib module: c-nullptr
+
+@cindex null pointer
+The @code{c-nullptr} module arranges for @code{nullptr} to act
+like standard C@.
+
+The C @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
+some compilers emit more sensible warnings, can avoid the need to cast
+a null pointer passed to a function prototyped with an ellipsis, and
+removes the need to include @code{<stddef.h>} merely to define
+@code{NULL}.
+
+Portability problems fixed by Gnulib:
+@itemize
+@item
+Some platforms lack @code{nullptr}:
+GCC 12, Clang 15, and other pre-2023 C compilers.
+@end itemize
+
+Portability problems not fixed by Gnulib:
+@itemize
+@item
+On older platforms, @code{nullptr} is a macro instead of a keyword.
+
+@item
+On older platforms, @code{nullptr} does not have the type @code{nullptr_t}.
+In C, it has type @code{void *}; in C++ it has an integer type.
+
+@item
+On older platforms Gnulib cannot easily emulate @code{nullptr_t}, so
+null pointer type checking is more error prone, and @code{_Generic}
+expressions cannot reliably distinguish @code{nullptr}'s type from
+integer or @code{void *} types.
+@end itemize
+
 @node static_assert
 @section @code{static_assert}
 
index fe7ed26c9bc8fd1d2ab2fc379924ab4b93e724f8..e240f93363c90e7f7770aadf5d1c9c51b479540e 100644 (file)
@@ -35,6 +35,12 @@ it does not fulfil the expectations of other system header files.
 
 Portability problems not fixed by Gnulib:
 @itemize
+@item
+@cindex null pointer type
+Some platforms fail to provide @code{nullptr_t},
+which Gnulib cannot usefully emulate:
+GCC 12, Clang 15, and other pre-2023 C compilers.
+
 @item
 Some platforms provide an @code{offsetof} macro that cannot be used in
 arbitrary expressions:
diff --git a/m4/c-nullptr.m4 b/m4/c-nullptr.m4
new file mode 100644 (file)
index 0000000..af79854
--- /dev/null
@@ -0,0 +1,30 @@
+# Check for nullptr that conforms to C23.
+
+dnl Copyright 2023 Free Software Foundation, Inc.
+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_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])])
+  if test "$gl_cv_c_nullptr" = yes; then
+    AC_DEFINE([HAVE_C_NULLPTR], [1], [Define to 1 if nullptr works.])
+  fi
+])
+
+  AH_VERBATIM([nullptr],
+[#ifndef HAVE_C_NULLPTR
+# ifndef __cplusplus
+#  define nullptr ((void *) 0)
+# elif 3 <= __GNUG__
+#  define nullptr __null
+# else
+#  define nullptr 0L
+# endif
+#endif])
+])
diff --git a/modules/c-nullptr b/modules/c-nullptr
new file mode 100644 (file)
index 0000000..bc5fa43
--- /dev/null
@@ -0,0 +1,20 @@
+Description:
+A nullptr that is like C23.
+
+Files:
+m4/c-nullptr.m4
+
+Depends-on:
+
+configure.ac:
+gl_C_NULLPTR
+
+Makefile.am:
+
+Include:
+
+License:
+LGPLv2+
+
+Maintainer:
+all