Port better to C23 bool+true+false keywords

C23 is adding the C++ keywords bool, true, and false;
prefer them to <stdbool.h> if they are available.
* admin/merge-gnulib (GNULIB_MODULES):
Add stdbool, which emulates C23 on pre-C23 platforms.
(AVOIDED_MODULES): Remove stdbool; Gnulib has renamed
this module to stdbool-c99 and nobody uses it so it does
not need to be avoided.
* m4/c-bool.m4: New file, from Gnulib stdbool module.
* lib-src/seccomp-filter.c, src/conf_post.h, src/dynlib.h:
* src/emacs-module.c, src/nsterm.m, src/systhread.h:
* test/src/emacs-module-resources/mod-test.c:
Use the C23 style and use bool without including <stdbool.h>.
The Gnulib stdbool module causes config.h to include stdbool.h
on pre-C23 platforms.
* src/emacs-module.h.in:
Don’t include <stdbool.h> if C23 or later, or if
it has already been included.
This commit is contained in:
Paul Eggert 2022-09-28 01:06:10 -07:00
parent a9b29ee4a0
commit f6fb2b9e1f
13 changed files with 64 additions and 18 deletions

View file

@ -43,7 +43,7 @@ GNULIB_MODULES='
nanosleep nproc nstrftime
pathmax pipe2 pselect pthread_sigmask
qcopy-acl readlink readlinkat regex
sig2str sigdescr_np socklen stat-time std-gnu11 stdalign stddef stdio
sig2str sigdescr_np socklen stat-time std-gnu11 stdalign stdbool stddef stdio
stpcpy strnlen strtoimax symlink sys_stat sys_time
tempname time time_r time_rz timegm timer-time timespec-add timespec-sub
update-copyright unlocked-io utimensat
@ -54,7 +54,7 @@ AVOIDED_MODULES='
btowc chmod close crypto/af_alg dup fchdir fstat langinfo lock
mbrtowc mbsinit memchr mkdir msvc-inval msvc-nothrow nl_langinfo
openat-die opendir pthread-h raise
save-cwd select setenv sigprocmask stat stdarg stdbool
save-cwd select setenv sigprocmask stat stdarg
threadlib tzset unsetenv utime utime-h
wchar wcrtomb wctype-h
'

View file

@ -3004,8 +3004,8 @@ Using @code{bool} can make programs easier to read and a bit faster than
using @code{int}. Although it is also OK to use @code{int}, @code{0}
and @code{1}, this older style is gradually being phased out. When
using @code{bool}, respect the limitations of the replacement
implementation of @code{bool}, as documented in the source file
@file{lib/stdbool.in.h}. In particular, boolean bitfields should be of type
implementation of @code{bool}. In particular,
boolean bitfields should be of type
@code{bool_bf}, not @code{bool}, so that they work correctly even when
compiling Objective C with standard GCC.

View file

@ -39,7 +39,6 @@ variants of those files that can be used to sandbox Emacs before
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>

View file

@ -60,7 +60,6 @@
# --avoid=sigprocmask \
# --avoid=stat \
# --avoid=stdarg \
# --avoid=stdbool \
# --avoid=threadlib \
# --avoid=tzset \
# --avoid=unsetenv \
@ -147,6 +146,7 @@
# stat-time \
# std-gnu11 \
# stdalign \
# stdbool \
# stddef \
# stdio \
# stpcpy \
@ -953,6 +953,8 @@ MKDIR_P = @MKDIR_P@
MODULES_OBJ = @MODULES_OBJ@
MODULES_SECONDARY_SUFFIX = @MODULES_SECONDARY_SUFFIX@
MODULES_SUFFIX = @MODULES_SUFFIX@
NEXT_ASSERT_H = @NEXT_ASSERT_H@
NEXT_AS_FIRST_DIRECTIVE_ASSERT_H = @NEXT_AS_FIRST_DIRECTIVE_ASSERT_H@
NEXT_AS_FIRST_DIRECTIVE_DIRENT_H = @NEXT_AS_FIRST_DIRECTIVE_DIRENT_H@
NEXT_AS_FIRST_DIRECTIVE_ERRNO_H = @NEXT_AS_FIRST_DIRECTIVE_ERRNO_H@
NEXT_AS_FIRST_DIRECTIVE_FCNTL_H = @NEXT_AS_FIRST_DIRECTIVE_FCNTL_H@

51
m4/c-bool.m4 Normal file
View file

@ -0,0 +1,51 @@
# Check for bool that conforms to C2023.
dnl Copyright 2022 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_BOOL],
[
AC_CACHE_CHECK([for bool, true, false], [gl_cv_c_bool],
[AC_COMPILE_IFELSE(
[AC_LANG_SOURCE([[
#if true == false
#error "true == false"
#endif
extern bool b;
bool b = true == false;]])],
[gl_cv_c_bool=yes],
[gl_cv_c_bool=no])])
if test "$gl_cv_c_bool" = yes; then
AC_DEFINE([HAVE_C_BOOL], [1],
[Define to 1 if bool, true and false work as per C2023.])
fi
AC_CHECK_HEADERS_ONCE([stdbool.h])
dnl The "zz" puts this toward config.h's end, to avoid potential
dnl collisions with other definitions.
dnl If 'bool', 'true' and 'false' do not work, arrange for them to work.
dnl In C, this means including <stdbool.h> if it is not already included.
dnl However, if the preprocessor mistakenly treats 'true' as 0,
dnl define it to a bool expression equal to 1; this is needed in
dnl Sun C++ 5.11 (Oracle Solaris Studio 12.2, 2010) and older.
AH_VERBATIM([zzbool],
[#ifndef HAVE_C_BOOL
# if !defined __cplusplus && !defined __bool_true_false_are_defined
# if HAVE_STDBOOL_H
# include <stdbool.h>
# else
# if defined __SUNPRO_C
# error "<stdbool.h> is not usable with this configuration. To make it usable, add -D_STDC_C99= to $CC."
# else
# error "<stdbool.h> does not exist on this platform. Use gnulib module 'stdbool-c99' instead of gnulib module 'stdbool'."
# endif
# endif
# endif
# if !true
# define true (!false)
# endif
#endif])
])

View file

@ -172,6 +172,7 @@ AC_DEFUN([gl_EARLY],
# Code from module stat-time:
# Code from module std-gnu11:
# Code from module stdalign:
# Code from module stdbool:
# Code from module stdckdint:
# Code from module stddef:
# Code from module stdint:
@ -490,6 +491,7 @@ AC_DEFUN([gl_INIT],
gl_STDALIGN_H
gl_CONDITIONAL_HEADER([stdalign.h])
AC_PROG_MKDIR_P
gl_C_BOOL
gl_STDDEF_H
gl_STDDEF_H_REQUIRE_DEFAULTS
gl_CONDITIONAL_HEADER([stddef.h])
@ -1428,6 +1430,7 @@ AC_DEFUN([gl_FILE_LIST], [
m4/assert_h.m4
m4/builtin-expect.m4
m4/byteswap.m4
m4/c-bool.m4
m4/canonicalize.m4
m4/clock_time.m4
m4/copy-file-range.m4

View file

@ -30,14 +30,10 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
#endif
/* To help make dependencies clearer elsewhere, this file typically
does not #include other files. The exceptions are stdbool.h
because it is unlikely to interfere with configuration and bool is
such a core part of the C language, and ms-w32.h (DOS_NT
does not #include other files. The exception is ms-w32.h (DOS_NT
only) because it historically was included here and changing that
would take some work. */
#include <stdbool.h>
#if defined WINDOWSNT && !defined DEFER_MS_W32_H
# include <ms-w32.h>
#endif

View file

@ -21,7 +21,6 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
#define DYNLIB_H
#include <attribute.h>
#include <stdbool.h>
typedef void *dynlib_handle_ptr;
dynlib_handle_ptr dynlib_open (const char *path);

View file

@ -78,7 +78,6 @@ To add a new module function, proceed as follows:
#include "emacs-module.h"
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>

View file

@ -30,7 +30,8 @@ information how to write modules and use this header file.
#include <stdint.h>
#include <time.h>
#ifndef __cplusplus
#if ((defined __STDC_VERSION__ ? __STDC_VERSION__ : 0) < 202311 \
&& !defined __bool_true_false_are_defined && !defined __cplusplus)
#include <stdbool.h>
#endif

View file

@ -37,7 +37,6 @@ Updated by Christian Limpach (chris@nice.ch)
#include <time.h>
#include <signal.h>
#include <unistd.h>
#include <stdbool.h>
#include <c-ctype.h>
#include <c-strcase.h>

View file

@ -19,8 +19,6 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
#ifndef SYSTHREAD_H
#define SYSTHREAD_H
#include <stdbool.h>
#include <attribute.h>
#ifdef THREADS_ENABLED

View file

@ -24,7 +24,6 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>