Debian Codegraph @

m4/nullptr.m4

raw
1 # nullptr.m4
2 # serial 2
3 dnl Copyright 2023-2025 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
7 dnl This file is offered as-is, without any warranty.
8
9 # Check for nullptr that conforms to C23 and C++11.
10
11 AC_DEFUN([gl_NULLPTR],
12 [
13 m4_provide_if([AC_PROG_CC],
14 [AC_LANG_PUSH([C])
15 AC_CACHE_CHECK([for C nullptr], [gl_cv_c_nullptr],
16 [AC_COMPILE_IFELSE(
17 [AC_LANG_SOURCE([[int *p = nullptr;]])],
18 [gl_cv_c_nullptr=yes
19 # Work around <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114780>.
20 gl_saved_CFLAGS=$CFLAGS
21 CFLAGS="$CFLAGS -Wall -Werror"
22 AC_COMPILE_IFELSE(
23 [AC_LANG_PROGRAM(
24 [[void f (char const *, ...) __attribute__ ((sentinel));]],
25 [[f ("", nullptr);]])],
26 [],
27 [AC_COMPILE_IFELSE(
28 [AC_LANG_PROGRAM(
29 [[void f (char const *, ...) __attribute__ ((sentinel));]],
30 [[f ("", (void *) 0);]])],
31 [gl_cv_c_nullptr='not as a sentinel'])])
32 CFLAGS=$gl_saved_CFLAGS],
33 [gl_cv_c_nullptr=no])])
34 gl_c_nullptr=$gl_cv_c_nullptr
35 AC_LANG_POP([C])],
36 [gl_c_nullptr=no])
37 if test "$gl_c_nullptr" = yes; then
38 AC_DEFINE([HAVE_C_NULLPTR], [1],
39 [Define to 1 if C nullptr is known to work.])
40 fi
41
42 m4_provide_if([AC_PROG_CXX],
43 [AC_LANG_PUSH([C++])
44 AC_CACHE_CHECK([for C++ nullptr], [gl_cv_cxx_nullptr],
45 [AC_COMPILE_IFELSE(
46 [AC_LANG_SOURCE([[int *p = nullptr;]])],
47 [gl_cv_cxx_nullptr=yes],
48 [AC_COMPILE_IFELSE(
49 [AC_LANG_SOURCE([[#include <stddef.h>
50 int *p = nullptr;]])],
51 [gl_cv_cxx_nullptr="yes, but it is a <stddef.h> macro"],
52 [gl_cv_cxx_nullptr=no])])])
53 AS_CASE([$gl_cv_cxx_nullptr],
54 [yes], [gl_have_cxx_nullptr=1],
55 [yes*], [gl_have_cxx_nullptr="(-1)"],
56 [gl_have_cxx_nullptr=0])
57 AC_DEFINE_UNQUOTED([HAVE_CXX_NULLPTR], [$gl_have_cxx_nullptr],
58 [Define to 1 if C++ nullptr works, 0 if not,
59 (-1) if it is a <stddef.h> macro.])
60 AC_LANG_POP([C++])])
61 ])
62
63 AH_VERBATIM([zznullptr],
64 [#if defined __cplusplus && HAVE_CXX_NULLPTR < 0
65 # include <stddef.h>
66 # undef/**/nullptr
67 #endif
68 #ifndef nullptr
69 # if !defined __cplusplus && !defined HAVE_C_NULLPTR
70 # define nullptr ((void *) 0)
71 # elif defined __cplusplus && HAVE_CXX_NULLPTR <= 0
72 # if 3 <= __GNUG__
73 # define nullptr __null
74 # else
75 # define nullptr 0L
76 # endif
77 # endif
78 #endif])
79 ])