diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2022-09-13 17:29:36 -0500 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2022-09-13 17:52:21 -0500 |
commit | 0804812cc2c659802fb8c070ab4f64957eb4e26c (patch) | |
tree | 88cc017b1969d3291d424e497e6df23dde66f389 | |
parent | 28465e160f242e39e6008e6de674e7c2360cf7df (diff) | |
download | gnulib-0804812cc2c659802fb8c070ab4f64957eb4e26c.tar.gz |
stdalign: alignas and alignof are C23 keywords
* m4/stdalign.m4 (gl_STDALIGN_H): Check also whether
alignas and alignof are keywords and so don’t need stdalign.h,
and define HAVE_C_ALIGNASOF if so.
If not, arrange for config.h to #define alignas and alignof
by including <stdalign.h>.
* modules/stdalign: No need to include <stdalign.h>.
* tests/test-stdalign.c: Do not include stdalign.h.
-rw-r--r-- | ChangeLog | 9 | ||||
-rw-r--r-- | doc/gnulib.texi | 25 | ||||
-rw-r--r-- | doc/posix-headers/stdalign.texi | 35 | ||||
-rw-r--r-- | m4/stdalign.m4 | 43 | ||||
-rw-r--r-- | modules/stdalign | 3 | ||||
-rw-r--r-- | tests/test-stdalign.c | 2 |
6 files changed, 87 insertions, 30 deletions
@@ -7,6 +7,15 @@ 2022-09-13 Paul Eggert <eggert@cs.ucla.edu> + stdalign: alignas and alignof are C23 keywords + * m4/stdalign.m4 (gl_STDALIGN_H): Check also whether + alignas and alignof are keywords and so don’t need stdalign.h, + and define HAVE_C_ALIGNASOF if so. + If not, arrange for config.h to #define alignas and alignof + by including <stdalign.h>. + * modules/stdalign: No need to include <stdalign.h>. + * tests/test-stdalign.c: Do not include stdalign.h. + assert-h: prefer to ‘verify’ Where it’s easy, prefer ‘static_assert’ to ‘verify’, as this simplifies the source by removing the need to diff --git a/doc/gnulib.texi b/doc/gnulib.texi index 1bef2a0975..e1debc9742 100644 --- a/doc/gnulib.texi +++ b/doc/gnulib.texi @@ -865,10 +865,35 @@ This chapter describes which keywords specified by ISO C are substituted by Gnulib. @menu +* alignof:: @code{alignas} and @code{alignof} * bool:: @code{bool}, @code{false}, and @code{true} * static_assert:: @code{static_assert} @end menu +@node alignof +@section @code{alignof} and @code{alignas} + +Gnulib module: stdalign + +The @code{stdalign} module arranges for @code{alignas}, @code{alignof}, and +@code{<stdalign.h>} to be more like standard C@. @xref{stdalign.h}. + +Portability problems fixed by Gnulib: +@itemize +@item +Pre-C11 platforms lack @code{alignas} and @code{alignof}. + +@item +On pre-C23 platforms, @code{<stdalign.h>} must be included before +using @code{alignas} or @code{alignof}. +@end itemize + +Portability problems not fixed by Gnulib: +@itemize +@item +On pre-C23 platforms, @code{alignas} and @code{alignof} are macros. +@end itemize + @node bool @section @code{bool} diff --git a/doc/posix-headers/stdalign.texi b/doc/posix-headers/stdalign.texi index 4ed83bec3e..be3cec80d1 100644 --- a/doc/posix-headers/stdalign.texi +++ b/doc/posix-headers/stdalign.texi @@ -14,50 +14,57 @@ Gnulib module: stdalign Portability problems fixed by Gnulib: @itemize @item +On older C platforms @code{<stdalign.h>} must be included before using +@code{alignas} or @code{alignof}. For example, GCC versions before 13 do not +support these keywords, which were standardized by C23. +@item This header file is missing on many platforms: FreeBSD 6.4, NetBSD 7.1, OpenBSD 6.7, Minix 3.3.0, AIX 7.1, HP-UX 11.31, IRIX 6.5, Solaris 11.3, mingw, MSVC 14, Android 9.0. @item -Clang 3.0's @code{<stdalign.h>} does not define @code{alignof}/@code{_Alignof}. +Clang 3.0's @code{<stdalign.h>} does not define @code{alignof}. @item -The @code{alignof} and @code{_Alignof} macros return too large values for +The @code{alignof} macro returns too large values for the types @code{double} and @code{long long} in GCC 4.7.0. +@item +Older C platforms might not support the obsolescent +@code{_Alignas} and @code{_Alignof} keywords or macros. +This portability problem should not matter with code using this module, +as such code should use @code{alignas} and @code{alignof} instead. @end itemize Portability problems not fixed by Gnulib: @itemize @item -In ISO C11, the operand of @code{alignof}/@code{_Alignof} must be a +In C11 and later, the operand of @code{alignof} must be a parenthesized type. Recent versions of GCC support an extension in which the operand can also be a unary expression, as with @code{sizeof}. The Gnulib substitute does not support this extension. @item On most pre-C11 platforms, the operand of -@code{alignof}/@code{_Alignof} cannot be a structure type containing a +@code{alignof} cannot be a structure type containing a flexible array member. @item -@code{_Alignas} and @code{alignas} are not always supported. +The @code{alignas} keyword or macro is not always supported. Supported compilers include any compiler supporting C11 or later, which includes GCC, IBM C, Sun C 5.9 and later, and MSVC 7.0 and later. @item Some compilers do not support alignment via -@code{alignas}/@code{_Alignas} of @code{auto} variables (i.e., +@code{alignas} of @code{auto} variables (i.e., variables on the stack). They diagnose and ignore the alignment: Sun C 5.11. @item -Some linkers do not support operands of @code{_Alignas}/@code{alignas} +Some linkers do not support operands of @code{alignas} that are greater than 8: mingw. @item -Some compilers require the operand of @code{_Alignas}/@code{alignas} +Some compilers require the operand of @code{alignas} to be a single integer constant, not an expression: MSVC 7.0 through at least 10.0. @item -The Sun C 5.13 compiler sometimes mishandles the alignment of multiple +The Sun C 5.13 (2014) compiler sometimes mishandles the alignment of multiple external variables that are declared close together with -@code{_Alignas}/@code{alignas}. The Sun Studio Developer Bug -Report Review Team assigned the internal review ID 2125432 (dated -2011-11-01) to this issue. The bug appears to be fixed in Sun C 5.15, -also known as Oracle Developer Studio 12.6. +@code{alignas}. The bug is fixed in Sun C 5.15, also known as Oracle +Developer Studio 12.6 (2017). @item @code{<stdalign.h>} must be #included before @code{_Alignas} and @code{_Alignof} can be used. @@ -67,6 +74,6 @@ In C11 and C17, @code{<stdalign.h>} defines the macros @code{__alignof_is_defined} to 1. In C23, these macros are not defined. @item -You cannot assume that @code{_Alignas} and @code{_Alignof} are reserved words; +You cannot assume that @code{alignas} and @code{alignof} are reserved words; they might be macros. @end itemize diff --git a/m4/stdalign.m4 b/m4/stdalign.m4 index c3283760d5..9164c2f357 100644 --- a/m4/stdalign.m4 +++ b/m4/stdalign.m4 @@ -1,4 +1,4 @@ -# Check for stdalign.h that conforms to C11. +# Check for alignas and alignof that conform to C23. dnl Copyright 2011-2022 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation @@ -9,12 +9,18 @@ dnl with or without modifications, as long as this notice is preserved. AC_DEFUN([gl_STDALIGN_H], [ - AC_CACHE_CHECK([for working stdalign.h], + AC_CACHE_CHECK([for alignas and alignof], [gl_cv_header_working_stdalign_h], - [AC_COMPILE_IFELSE( + [gl_save_CFLAGS=$CFLAGS + for gl_working in "yes, keywords" "yes, <stdalign.h> macros"; do + AS_CASE([$gl_working], + [*stdalign.h*], [CFLAGS="$gl_save_CFLAGS -DINCLUDE_STDALIGN_H"]) + AC_COMPILE_IFELSE( [AC_LANG_PROGRAM( [[#include <stdint.h> - #include <stdalign.h> + #ifdef INCLUDE_STDALIGN_H + #include <stdalign.h> + #endif #include <stddef.h> /* Test that alignof yields a result consistent with offsetof. @@ -45,12 +51,25 @@ AC_DEFUN([gl_STDALIGN_H], ? 1 : -1]; #endif ]])], - [gl_cv_header_working_stdalign_h=yes], - [gl_cv_header_working_stdalign_h=no])]) - - if test $gl_cv_header_working_stdalign_h = yes; then - GL_GENERATE_STDALIGN_H=false - else - GL_GENERATE_STDALIGN_H=true - fi + [gl_cv_header_working_stdalign_h=$gl_working], + [gl_cv_header_working_stdalign_h=no]) + + CFLAGS=$gl_save_CFLAGS + test "$gl_cv_header_working_stdalign_h" != no && break + done]) + + GL_GENERATE_STDALIGN_H=false + AS_CASE([$gl_cv_header_working_stdalign_h], + [no], + [GL_GENERATE_STDALIGN_H=true], + [yes*keyword*], + [AC_DEFINE([HAVE_C_ALIGNASOF], [1], + [Define to 1 if the alignas and alignof keywords work.])]) + + dnl The "zz" puts this toward config.h's end, to avoid potential + dnl collisions with other definitions. + AH_VERBATIM([zzalignas], +[#if !defined HAVE_C_ALIGNASOF && __cplusplus < 201103 && !defined alignof + #include <stdalign.h> +#endif]) ]) diff --git a/modules/stdalign b/modules/stdalign index 24c79ec493..af038180fa 100644 --- a/modules/stdalign +++ b/modules/stdalign @@ -1,5 +1,5 @@ Description: -A <stdalign.h> that nearly conforms to ISO C11 and C++11. +alignas and alignof that are like C23 and C++11. Files: lib/stdalign.in.h @@ -30,7 +30,6 @@ endif MOSTLYCLEANFILES += stdalign.h stdalign.h-t Include: -<stdalign.h> License: LGPLv2+ diff --git a/tests/test-stdalign.c b/tests/test-stdalign.c index ea07543802..a935554563 100644 --- a/tests/test-stdalign.c +++ b/tests/test-stdalign.c @@ -18,8 +18,6 @@ #include <config.h> -#include <stdalign.h> - #include <stddef.h> #include <stdint.h> |