From f6fb2b9e1f94917601201f5d43c15e92e99b2a2f Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Wed, 28 Sep 2022 01:06:10 -0700 Subject: [PATCH] Port better to C23 bool+true+false keywords MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit C23 is adding the C++ keywords bool, true, and false; prefer them to 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 . The Gnulib stdbool module causes config.h to include stdbool.h on pre-C23 platforms. * src/emacs-module.h.in: Don’t include if C23 or later, or if it has already been included. --- admin/merge-gnulib | 4 +- doc/lispref/internals.texi | 4 +- lib-src/seccomp-filter.c | 1 - lib/gnulib.mk.in | 4 +- m4/c-bool.m4 | 51 ++++++++++++++++++++++ m4/gnulib-comp.m4 | 3 ++ src/conf_post.h | 6 +-- src/dynlib.h | 1 - src/emacs-module.c | 1 - src/emacs-module.h.in | 3 +- src/nsterm.m | 1 - src/systhread.h | 2 - test/src/emacs-module-resources/mod-test.c | 1 - 13 files changed, 64 insertions(+), 18 deletions(-) create mode 100644 m4/c-bool.m4 diff --git a/admin/merge-gnulib b/admin/merge-gnulib index 4dd6a4d222f..d3c5520ad0f 100755 --- a/admin/merge-gnulib +++ b/admin/merge-gnulib @@ -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 ' diff --git a/doc/lispref/internals.texi b/doc/lispref/internals.texi index 8d2089bad8b..ea1679f6934 100644 --- a/doc/lispref/internals.texi +++ b/doc/lispref/internals.texi @@ -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. diff --git a/lib-src/seccomp-filter.c b/lib-src/seccomp-filter.c index 9f0de7d64f8..4bd2816dfc5 100644 --- a/lib-src/seccomp-filter.c +++ b/lib-src/seccomp-filter.c @@ -39,7 +39,6 @@ variants of those files that can be used to sandbox Emacs before #include #include #include -#include #include #include #include diff --git a/lib/gnulib.mk.in b/lib/gnulib.mk.in index 7eca66a6093..04644bdabe3 100644 --- a/lib/gnulib.mk.in +++ b/lib/gnulib.mk.in @@ -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@ diff --git a/m4/c-bool.m4 b/m4/c-bool.m4 new file mode 100644 index 00000000000..bb109b77968 --- /dev/null +++ b/m4/c-bool.m4 @@ -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 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 +# else +# if defined __SUNPRO_C +# error " is not usable with this configuration. To make it usable, add -D_STDC_C99= to $CC." +# else +# error " 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]) +]) diff --git a/m4/gnulib-comp.m4 b/m4/gnulib-comp.m4 index 6a5f3006781..f1ac4991324 100644 --- a/m4/gnulib-comp.m4 +++ b/m4/gnulib-comp.m4 @@ -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 diff --git a/src/conf_post.h b/src/conf_post.h index 6ecebf36ab9..fb8d2e5d96e 100644 --- a/src/conf_post.h +++ b/src/conf_post.h @@ -30,14 +30,10 @@ along with GNU Emacs. If not, see . */ #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 - #if defined WINDOWSNT && !defined DEFER_MS_W32_H # include #endif diff --git a/src/dynlib.h b/src/dynlib.h index 03b8f983564..9a11c128981 100644 --- a/src/dynlib.h +++ b/src/dynlib.h @@ -21,7 +21,6 @@ along with GNU Emacs. If not, see . */ #define DYNLIB_H #include -#include typedef void *dynlib_handle_ptr; dynlib_handle_ptr dynlib_open (const char *path); diff --git a/src/emacs-module.c b/src/emacs-module.c index 1c392d65df8..fcdf103c19b 100644 --- a/src/emacs-module.c +++ b/src/emacs-module.c @@ -78,7 +78,6 @@ To add a new module function, proceed as follows: #include "emacs-module.h" #include -#include #include #include #include diff --git a/src/emacs-module.h.in b/src/emacs-module.h.in index 6642b55d932..bef89b059fc 100644 --- a/src/emacs-module.h.in +++ b/src/emacs-module.h.in @@ -30,7 +30,8 @@ information how to write modules and use this header file. #include #include -#ifndef __cplusplus +#if ((defined __STDC_VERSION__ ? __STDC_VERSION__ : 0) < 202311 \ + && !defined __bool_true_false_are_defined && !defined __cplusplus) #include #endif diff --git a/src/nsterm.m b/src/nsterm.m index 44979c7c04e..b68a6f8ec12 100644 --- a/src/nsterm.m +++ b/src/nsterm.m @@ -37,7 +37,6 @@ GNUstep port and post-20 update by Adrian Robert (arobert@cogsci.ucsd.edu) #include #include #include -#include #include #include diff --git a/src/systhread.h b/src/systhread.h index bf4e0306cdc..10d6237f275 100644 --- a/src/systhread.h +++ b/src/systhread.h @@ -19,8 +19,6 @@ along with GNU Emacs. If not, see . */ #ifndef SYSTHREAD_H #define SYSTHREAD_H -#include - #include #ifdef THREADS_ENABLED diff --git a/test/src/emacs-module-resources/mod-test.c b/test/src/emacs-module-resources/mod-test.c index 187af821c22..b47a0b7a39b 100644 --- a/test/src/emacs-module-resources/mod-test.c +++ b/test/src/emacs-module-resources/mod-test.c @@ -24,7 +24,6 @@ along with GNU Emacs. If not, see . */ #include #include -#include #include #include #include -- 2.39.2