1
0
mirror of https://git.savannah.gnu.org/git/gnulib.git synced 2025-08-08 17:22:05 +03:00

alignasof: new module

This splits off support for the C23 keywords alignas and alignof,
from the now-deprecated stdalign module.  The latter now merely
provides C11 support.
* MODULES.html.sh, NEWS, doc/gnulib.texi:
* doc/posix-headers/stdalign.texi: Document the change.
* lib/stdalign.in.h: Remove most of the definitions (which are now
supplied by the alignasof module), leaving only
__alignas_is_defined and __alignof_is_defined.
* modules/alignasof, modules/alignasof-tests: New files.
* m4/stdalign.m4 (gl_ALIGNASOF): New macro, with
most of the contents of the old gl_STDALIGN_H.
Do not define __alignas_is_defined or __alignof_is_defined.
(gl_STDALIGN_H): Rely on gl_ALIGNASOF for most of the work.
* modules/alignalloc, modules/alignof, modules/argp:
* modules/crypto/md4-buffer, modules/crypto/md5-buffer:
* modules/crypto/sha1-buffer, modules/crypto/sha256-buffer:
* modules/crypto/sha512-buffer, modules/crypto/sm3-buffer:
* modules/fts, modules/rawmemchr, modules/relocatable-prog-wrapper:
* modules/stddef-tests, modules/sys_socket:
Depend on alignasof, not stdalign.
* modules/stdalign: Deprecate.  Depend on alignasof.
* modules/stdalign-tests: Move most contents to the new module
alignasof-tests, and depend on that.
This commit is contained in:
Paul Eggert
2023-01-15 11:48:43 -08:00
parent 8bec309111
commit c593e834e1
25 changed files with 198 additions and 179 deletions

View File

@@ -1,3 +1,30 @@
2023-01-15 Paul Eggert <eggert@cs.ucla.edu>
alignasof: new module
This splits off support for the C23 keywords alignas and alignof,
from the now-deprecated stdalign module. The latter now merely
provides C11 support.
* MODULES.html.sh, NEWS, doc/gnulib.texi:
* doc/posix-headers/stdalign.texi: Document the change.
* lib/stdalign.in.h: Remove most of the definitions (which are now
supplied by the alignasof module), leaving only
__alignas_is_defined and __alignof_is_defined.
* modules/alignasof, modules/alignasof-tests: New files.
* m4/stdalign.m4 (gl_ALIGNASOF): New macro, with
most of the contents of the old gl_STDALIGN_H.
Do not define __alignas_is_defined or __alignof_is_defined.
(gl_STDALIGN_H): Rely on gl_ALIGNASOF for most of the work.
* modules/alignalloc, modules/alignof, modules/argp:
* modules/crypto/md4-buffer, modules/crypto/md5-buffer:
* modules/crypto/sha1-buffer, modules/crypto/sha256-buffer:
* modules/crypto/sha512-buffer, modules/crypto/sm3-buffer:
* modules/fts, modules/rawmemchr, modules/relocatable-prog-wrapper:
* modules/stddef-tests, modules/sys_socket:
Depend on alignasof, not stdalign.
* modules/stdalign: Deprecate. Depend on alignasof.
* modules/stdalign-tests: Move most contents to the new module
alignasof-tests, and depend on that.
2023-01-15 Bruno Haible <bruno@clisp.org>
fpending: Fix compilation error with NDK ≥ r14b and Android API < 23.

View File

@@ -2383,6 +2383,7 @@ func_all_modules ()
func_echo "$element"
func_begin_table
func_module alignasof
func_module stdckdint
func_end_table

2
NEWS
View File

@@ -74,6 +74,8 @@ User visible incompatible changes
Date Modules Changes
2023-01-15 stdalign This module is deprecated. Use alignasof instead.
2023-01-07 timer_time Link with $(TIMER_TIME_LIB) instead of
$(LIB_TIMER_TIME).

View File

@@ -873,10 +873,10 @@ substituted by Gnulib.
@node alignof
@section @code{alignof} and @code{alignas}
Gnulib module: stdalign
Gnulib module: alignasof
The @code{stdalign} module arranges for @code{alignas}, @code{alignof}, and
@code{<stdalign.h>} to be more like standard C@. @xref{stdalign.h}.
The @code{alignasof} module arranges for @code{alignas} and @code{alignof}
to be more like standard C@.
Portability problems fixed by Gnulib:
@itemize
@@ -886,6 +886,7 @@ 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}.
@xref{stdalign.h}.
@end itemize
Portability problems not fixed by Gnulib:

View File

@@ -1,15 +1,17 @@
@node stdalign.h
@section @file{stdalign.h}
POSIX specification:@* Not in POSIX yet, but we expect it will be.
ISO C11 (latest free draft
@url{http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf})
POSIX specification:@* Not in POSIX yet, but we expect it will be,
at least temporarily until it becomes obsolete due to its phasing
out starting in C23.
ISO C23 (latest free draft
@url{http://www.open-std.org/jtc1/sc22/wg14/www/docs/n3047.pdf})
sections 6.5.3.4, 6.7.5, 7.15.
C++11 (latest free draft
@url{http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf})
section 18.10.
Gnulib module: stdalign
Gnulib module: alignasof
Portability problems fixed by Gnulib:
@itemize
@@ -17,6 +19,10 @@ Portability problems fixed by Gnulib:
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.
On C23 and later platforms, @code{<stdalign.h>} has no effect and need
not be included. (Gnulib-using code should not include
@code{<stdalign.h>} without also employing Gnulib's now-deprecated
@code{stdalign} module.)
@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.
@@ -30,6 +36,15 @@ 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.
@item
In C11 and C17, @code{<stdalign.h>} defines the macros
@code{__alignas_is_defined} and
@code{__alignof_is_defined} to 1.
In C23, these macros are not defined.
This portability problem should not matter with code using Gnulib's
@code{alignasof} module, as such code should use @code{alignas} and
@code{alignof} without checking these two macros. (Gnulib's
now-deprecated @code{stdalign} module defines these two macros.)
@end itemize
Portability problems not fixed by Gnulib:
@@ -66,14 +81,6 @@ external variables that are declared close together with
@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.
@item
In C11 and C17, @code{<stdalign.h>} defines the macros
@code{__alignas_is_defined} and
@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;
they might be macros.
@end itemize

View File

@@ -17,117 +17,18 @@
/* Written by Paul Eggert and Bruno Haible. */
/* Define two obsolescent C11 macros, assuming alignas and alignof are
either keywords or alignasof-defined macros. */
#ifndef _GL_STDALIGN_H
#define _GL_STDALIGN_H
/* ISO C11 <stdalign.h> for platforms that lack it.
References:
ISO C11 (latest free draft
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf>)
sections 6.5.3.4, 6.7.5, 7.15.
C++11 (latest free draft
<http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf>)
section 18.10. */
/* alignof (TYPE), also known as _Alignof (TYPE), yields the alignment
requirement of a structure member (i.e., slot or field) that is of
type TYPE, as an integer constant expression.
This differs from GCC's and clang's __alignof__ operator, which can
yield a better-performing alignment for an object of that type. For
example, on x86 with GCC and on Linux/x86 with clang,
__alignof__ (double) and __alignof__ (long long) are 8, whereas
alignof (double) and alignof (long long) are 4 unless the option
'-malign-double' is used.
The result cannot be used as a value for an 'enum' constant, if you
want to be portable to HP-UX 10.20 cc and AIX 3.2.5 xlc. */
/* FreeBSD 9.1 <sys/cdefs.h>, included by <stddef.h> and lots of other
standard headers, defines conflicting implementations of _Alignas
and _Alignof that are no better than ours; override them. */
#undef _Alignas
#undef _Alignof
/* GCC releases before GCC 4.9 had a bug in _Alignof. See GCC bug 52023
<https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52023>.
clang versions < 8.0.0 have the same bug. */
#if (!defined __STDC_VERSION__ || __STDC_VERSION__ < 201112 \
|| (defined __GNUC__ && __GNUC__ < 4 + (__GNUC_MINOR__ < 9) \
&& !defined __clang__) \
|| (defined __clang__ && __clang_major__ < 8))
# ifdef __cplusplus
# if (201103 <= __cplusplus || defined _MSC_VER)
# define _Alignof(type) alignof (type)
# else
template <class __t> struct __alignof_helper { char __a; __t __b; };
# define _Alignof(type) offsetof (__alignof_helper<type>, __b)
# define _GL_STDALIGN_NEEDS_STDDEF 1
# endif
# else
# define _Alignof(type) offsetof (struct { char __a; type __b; }, __b)
# define _GL_STDALIGN_NEEDS_STDDEF 1
# endif
#endif
#if ! (defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER))
# define alignof _Alignof
#endif
#define __alignof_is_defined 1
/* alignas (A), also known as _Alignas (A), aligns a variable or type
to the alignment A, where A is an integer constant expression. For
example:
int alignas (8) foo;
struct s { int a; int alignas (8) bar; };
aligns the address of FOO and the offset of BAR to be multiples of 8.
A should be a power of two that is at least the type's alignment
and at most the implementation's alignment limit. This limit is
2**28 on typical GNUish hosts, and 2**13 on MSVC. To be portable
to MSVC through at least version 10.0, A should be an integer
constant, as MSVC does not support expressions such as 1 << 3.
To be portable to Sun C 5.11, do not align auto variables to
anything stricter than their default alignment.
The following C11 requirements are not supported here:
- If A is zero, alignas has no effect.
- alignas can be used multiple times; the strictest one wins.
- alignas (TYPE) is equivalent to alignas (alignof (TYPE)).
*/
#if !defined __STDC_VERSION__ || __STDC_VERSION__ < 201112
# if defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER)
# define _Alignas(a) alignas (a)
# elif (!defined __attribute__ \
&& ((defined __APPLE__ && defined __MACH__ \
? 4 < __GNUC__ + (1 <= __GNUC_MINOR__) \
: __GNUC__ && !defined __ibmxl__) \
|| (4 <= __clang_major__) \
|| (__ia64 && (61200 <= __HP_cc || 61200 <= __HP_aCC)) \
|| __ICC || 0x590 <= __SUNPRO_C || 0x0600 <= __xlC__))
# define _Alignas(a) __attribute__ ((__aligned__ (a)))
# elif 1300 <= _MSC_VER
# define _Alignas(a) __declspec (align (a))
# endif
#endif
#if ((defined _Alignas \
&& !(defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER))) \
|| (defined __STDC_VERSION__ && 201112 <= __STDC_VERSION__))
# define alignas _Alignas
#endif
#if (defined alignas \
|| (defined __STDC_VERSION__ && 202311 <= __STDC_VERSION__) \
|| (defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER)))
# define __alignas_is_defined 1
#endif
/* Include <stddef.h> if needed for offsetof. */
#if _GL_STDALIGN_NEEDS_STDDEF
# include <stddef.h>
#endif
#define __alignof_is_defined 1
#endif /* _GL_STDALIGN_H */

View File

@@ -5,9 +5,11 @@ 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.
dnl Written by Paul Eggert and Bruno Haible.
# Prepare for substituting <stdalign.h> if it is not supported.
AC_DEFUN([gl_STDALIGN_H],
AC_DEFUN([gl_ALIGNASOF],
[
AC_CACHE_CHECK([for alignas and alignof],
[gl_cv_header_working_stdalign_h],
@@ -58,16 +60,11 @@ AC_DEFUN([gl_STDALIGN_H],
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.])])
AC_CHECK_HEADERS_ONCE([stdalign.h])
dnl The "zz" puts this toward config.h's end, to avoid potential
dnl collisions with other definitions.
AH_VERBATIM([zzalignas],
@@ -75,11 +72,33 @@ AC_DEFUN([gl_STDALIGN_H],
# if HAVE_STDALIGN_H
# include <stdalign.h>
# else
/* Substitute. Keep consistent with gnulib/lib/stdalign.in.h. */
# ifndef _GL_STDALIGN_H
# define _GL_STDALIGN_H
# undef _Alignas
# undef _Alignof
/* ISO C23 alignas and alignof for platforms that lack it.
References:
ISO C23 (latest free draft
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n3047.pdf>)
sections 6.5.3.4, 6.7.5, 7.15.
C++11 (latest free draft
<http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf>)
section 18.10. */
/* alignof (TYPE), also known as _Alignof (TYPE), yields the alignment
requirement of a structure member (i.e., slot or field) that is of
type TYPE, as an integer constant expression.
This differs from GCC's and clang's __alignof__ operator, which can
yield a better-performing alignment for an object of that type. For
example, on x86 with GCC and on Linux/x86 with clang,
__alignof__ (double) and __alignof__ (long long) are 8, whereas
alignof (double) and alignof (long long) are 4 unless the option
'-malign-double' is used.
The result cannot be used as a value for an 'enum' constant, if you
want to be portable to HP-UX 10.20 cc and AIX 3.2.5 xlc. */
/* GCC releases before GCC 4.9 had a bug in _Alignof. See GCC bug 52023
<https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52023>.
clang versions < 8.0.0 have the same bug. */
# if (!defined __STDC_VERSION__ || __STDC_VERSION__ < 201112 \
|| (defined __GNUC__ && __GNUC__ < 4 + (__GNUC_MINOR__ < 9) \
&& !defined __clang__) \
@@ -100,35 +119,65 @@ AC_DEFUN([gl_STDALIGN_H],
# if ! (defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER))
# define alignof _Alignof
# endif
# define __alignof_is_defined 1
# if !defined __STDC_VERSION__ || __STDC_VERSION__ < 201112
# if defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER)
# define _Alignas(a) alignas (a)
# elif (!defined __attribute__ \
&& ((defined __APPLE__ && defined __MACH__ \
? 4 < __GNUC__ + (1 <= __GNUC_MINOR__) \
: __GNUC__ && !defined __ibmxl__) \
|| (4 <= __clang_major__) \
|| (__ia64 && (61200 <= __HP_cc || 61200 <= __HP_aCC)) \
|| __ICC || 0x590 <= __SUNPRO_C || 0x0600 <= __xlC__))
# define _Alignas(a) __attribute__ ((__aligned__ (a)))
# elif 1300 <= _MSC_VER
# define _Alignas(a) __declspec (align (a))
# endif
/* alignas (A), also known as _Alignas (A), aligns a variable or type
to the alignment A, where A is an integer constant expression. For
example:
int alignas (8) foo;
struct s { int a; int alignas (8) bar; };
aligns the address of FOO and the offset of BAR to be multiples of 8.
A should be a power of two that is at least the type's alignment
and at most the implementation's alignment limit. This limit is
2**28 on typical GNUish hosts, and 2**13 on MSVC. To be portable
to MSVC through at least version 10.0, A should be an integer
constant, as MSVC does not support expressions such as 1 << 3.
To be portable to Sun C 5.11, do not align auto variables to
anything stricter than their default alignment.
The following C23 requirements are not supported here:
- If A is zero, alignas has no effect.
- alignas can be used multiple times; the strictest one wins.
- alignas (TYPE) is equivalent to alignas (alignof (TYPE)).
*/
# if !defined __STDC_VERSION__ || __STDC_VERSION__ < 201112
# if defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER)
# define _Alignas(a) alignas (a)
# elif (!defined __attribute__ \
&& ((defined __APPLE__ && defined __MACH__ \
? 4 < __GNUC__ + (1 <= __GNUC_MINOR__) \
: __GNUC__ && !defined __ibmxl__) \
|| (4 <= __clang_major__) \
|| (__ia64 && (61200 <= __HP_cc || 61200 <= __HP_aCC)) \
|| __ICC || 0x590 <= __SUNPRO_C || 0x0600 <= __xlC__))
# define _Alignas(a) __attribute__ ((__aligned__ (a)))
# elif 1300 <= _MSC_VER
# define _Alignas(a) __declspec (align (a))
# endif
# if ((defined _Alignas \
&& !(defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER))) \
|| (defined __STDC_VERSION__ && 201112 <= __STDC_VERSION__))
# define alignas _Alignas
# endif
# if (defined alignas \
|| (defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER)))
# define __alignas_is_defined 1
# endif
# if _GL_STDALIGN_NEEDS_STDDEF
# include <stddef.h>
# endif
# endif /* _GL_STDALIGN_H */
# endif
# if ((defined _Alignas \
&& !(defined __cplusplus \
&& (201103 <= __cplusplus || defined _MSC_VER))) \
|| (defined __STDC_VERSION__ && 201112 <= __STDC_VERSION__))
# define alignas _Alignas
# endif
# if _GL_STDALIGN_NEEDS_STDDEF
# include <stddef.h>
# endif
# endif
#endif])
])
AC_DEFUN([gl_STDALIGN_H],
[
AC_REQUIRE([gl_ALIGNASOF])
GL_GENERATE_STDALIGN_H=false
AS_IF([test "$gl_cv_header_working_stdalign_h" = no],
[GL_GENERATE_STDALIGN_H=true])
AC_CHECK_HEADERS_ONCE([stdalign.h])
])

View File

@@ -7,12 +7,12 @@ lib/alignalloc.c
m4/alignalloc.m4
Depends-on:
alignasof
assert-h
extensions
extern-inline
idx
posix_memalign
stdalign
stdckdint
stdint

18
modules/alignasof Normal file
View File

@@ -0,0 +1,18 @@
Description:
alignas and alignof that are like C23 and C++11.
Files:
m4/stdalign.m4
configure.ac:
gl_ALIGNASOF
Makefile.am:
Include:
License:
LGPLv2+
Maintainer:
all

13
modules/alignasof-tests Normal file
View File

@@ -0,0 +1,13 @@
Files:
tests/test-stdalign.c
tests/macros.h
Depends-on:
assert-h
stdint
configure.ac:
Makefile.am:
TESTS += test-stdalign
check_PROGRAMS += test-stdalign

View File

@@ -5,7 +5,7 @@ Files:
lib/alignof.h
Depends-on:
stdalign
alignasof
configure.ac:

View File

@@ -18,6 +18,7 @@ lib/argp-xinl.c
m4/argp.m4
Depends-on:
alignasof
alloca
basename-lgpl
extern-inline
@@ -31,7 +32,6 @@ strcase
extensions
vsnprintf
sleep
stdalign
stdbool
stdio
strerror

View File

@@ -7,8 +7,8 @@ lib/md4.c
m4/md4.m4
Depends-on:
alignasof
byteswap
stdalign
stdint
configure.ac:

View File

@@ -9,10 +9,10 @@ m4/gl-openssl.m4
m4/md5.m4
Depends-on:
alignasof
byteswap
c99
extern-inline
stdalign
stdint
configure.ac:

View File

@@ -9,10 +9,10 @@ m4/gl-openssl.m4
m4/sha1.m4
Depends-on:
alignasof
byteswap
c99
extern-inline
stdalign
stdint
configure.ac:

View File

@@ -9,10 +9,10 @@ m4/gl-openssl.m4
m4/sha256.m4
Depends-on:
alignasof
byteswap
c99
extern-inline
stdalign
stdint
configure.ac:

View File

@@ -9,10 +9,10 @@ m4/gl-openssl.m4
m4/sha512.m4
Depends-on:
alignasof
byteswap
c99
extern-inline
stdalign
stdint
u64

View File

@@ -7,10 +7,10 @@ lib/sm3.c
m4/sm3.m4
Depends-on:
alignasof
byteswap
c99
extern-inline
stdalign
stdint
configure.ac:

View File

@@ -8,6 +8,7 @@ lib/fts-cycle.c
m4/fts.m4
Depends-on:
alignasof
attribute
c99
closedir
@@ -28,7 +29,6 @@ openat-h
opendir
opendirat
readdir
stdalign
stdbool
stddef

View File

@@ -7,9 +7,9 @@ lib/rawmemchr.valgrind
m4/rawmemchr.m4
Depends-on:
alignasof
assert-h
extensions
stdalign
stdint
string

View File

@@ -50,6 +50,7 @@ m4/relocatable-lib.m4
m4/setenv.m4
Depends-on:
alignasof
assert-h
c99
double-slash-root
@@ -64,7 +65,6 @@ largefile
libc-config
pathmax
ssize_t
stdalign
stdbool
stddef
stdckdint

View File

@@ -1,11 +1,16 @@
Description:
alignas and alignof that are like C23 and C++11.
Notice:
The stdalign module is deprecated.
New code should use the alignasof module, and not include <stdalign.h>.
Files:
lib/stdalign.in.h
m4/stdalign.m4
Depends-on:
alignasof
gen-header
configure.ac:
@@ -35,4 +40,4 @@ License:
LGPLv2+
Maintainer:
Paul Eggert
all

View File

@@ -1,14 +1,9 @@
Files:
tests/test-stdalign.c
tests/macros.h
Depends-on:
assert-h
stdint
alignasof-tests
stdalign-c++-tests
configure.ac:
Makefile.am:
TESTS += test-stdalign
check_PROGRAMS += test-stdalign

View File

@@ -2,8 +2,8 @@ Files:
tests/test-stddef.c
Depends-on:
alignasof
assert-h
stdalign
stddef-c++-tests
configure.ac:

View File

@@ -8,6 +8,7 @@ m4/sys_socket_h.m4
m4/sockpfaf.m4
Depends-on:
alignasf
errno
extern-inline
gen-header
@@ -17,7 +18,6 @@ snippet/c++defs
snippet/warn-on-use
socklen
ssize_t
stdalign
sys_uio
configure.ac: