bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
* [PATCH v2] Define alignof_slot using _Alignof when using C11 or newer
@ 2023-01-15  1:20 Khem Raj
  2023-01-15  1:51 ` Paul Eggert
  0 siblings, 1 reply; 6+ messages in thread
From: Khem Raj @ 2023-01-15  1:20 UTC (permalink / raw)
  To: bug-gnulib; +Cc: Khem Raj

WG14 N2350 made very clear that it is an UB having type definitions
within "offsetof" [1]. This patch enhances the implementation of macro
alignof_slot to use builtin "_Alignof" to avoid undefined behavior on
when using std=c11 or newer. Make exceptions for gcc < 4.9 and
clang < 8 where there is bug [3] in _Alignof implementation

clang 16+ has started to flag this [2]

Fixes build when using -std >= gnu11 and using clang16+

[1] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2350.htm
[2] https://reviews.llvm.org/D133574
[3] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52023

Signed-off-by: Khem Raj <raj.khem@gmail.com>
---
v1->v2:
- Exclude gcc < 4.9 and clang < 8 even though they may have C11 support

 ChangeLog     |  5 +++++
 lib/alignof.h | 10 +++++++++-
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index fb467a3c14..33c0a0bb8a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2023-01-15  Khem Raj  <raj.khem@gmail.com>
+
+	* lib/alignof.h (alignof_slot): Use _Alignof when using C11 or newer
+	standard barring GCC < 4.9 and clang < 8.
+
 2023-01-14  Bruno Haible  <bruno@clisp.org>
 
 	error, verror tests: Fix link error when the package uses libintl.
diff --git a/lib/alignof.h b/lib/alignof.h
index 505ad97aa4..962c8f31f4 100644
--- a/lib/alignof.h
+++ b/lib/alignof.h
@@ -28,8 +28,16 @@
 #if defined __cplusplus
   template <class type> struct alignof_helper { char __slot1; type __slot2; };
 # define alignof_slot(type) offsetof (alignof_helper<type>, __slot2)
-#else
+/* 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.  */
+#elif (!defined __STDC_VERSION__ || __STDC_VERSION__ < 201112 \
+       || (defined __GNUC__ && __GNUC__ < 4 + (__GNUC_MINOR__ < 9) \
+           && !defined __clang__) \
+       || (defined __clang__ && __clang_major__ < 8))
 # define alignof_slot(type) offsetof (struct { char __slot1; type __slot2; }, __slot2)
+#else
+# define alignof_slot(type) _Alignof(type)
 #endif
 
 /* alignof_type (TYPE)
-- 
2.39.0



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] Define alignof_slot using _Alignof when using C11 or newer
  2023-01-15  1:20 [PATCH v2] Define alignof_slot using _Alignof when using C11 or newer Khem Raj
@ 2023-01-15  1:51 ` Paul Eggert
  2023-01-15 19:52   ` Paul Eggert
  0 siblings, 1 reply; 6+ messages in thread
From: Paul Eggert @ 2023-01-15  1:51 UTC (permalink / raw)
  To: Khem Raj; +Cc: bug-gnulib

[-- Attachment #1: Type: text/plain, Size: 751 bytes --]

On 2023-01-14 17:20, Khem Raj wrote:
> +/* 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.  */
> +#elif (!defined __STDC_VERSION__ || __STDC_VERSION__ < 201112 \
> +       || (defined __GNUC__ && __GNUC__ < 4 + (__GNUC_MINOR__ < 9) \
> +           && !defined __clang__) \
> +       || (defined __clang__ && __clang_major__ < 8))

We already have two copies of that stuff elsewhere (in lib/stdalign.in.h 
and m4/stdalign.m4), for reasons that currently escape me. Let's not 
balloon the number of copies to three.

I installed the attached smaller patch which should do the trick for C.

Is there a similar problem with C++.

[-- Attachment #2: 0001-alignof-port-to-strict-C23.patch --]
[-- Type: text/x-patch, Size: 2215 bytes --]

From 2d404c7dd974cc65f894526f4a1b76bc1dcd8d82 Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Sat, 14 Jan 2023 17:48:18 -0800
Subject: [PATCH] alignof: port to strict C23

Problem reported by Khem Raj in:
https://lists.gnu.org/r/bug-gnulib/2023-01/msg00111.html
* lib/alignof.h (alignof_slot) [!__cplusplus]: Use C23-style alignof.
* modules/alignof (Depends-on): Add stdalign.
---
 ChangeLog       | 6 ++++++
 lib/alignof.h   | 4 ++--
 modules/alignof | 1 +
 3 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 0900c54c1d..fa684131db 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2023-01-14  Paul Eggert  <eggert@cs.ucla.edu>
 
+	alignof: port to strict C23
+	Problem reported by Khem Raj in:
+	https://lists.gnu.org/r/bug-gnulib/2023-01/msg00111.html
+	* lib/alignof.h (alignof_slot) [!__cplusplus]: Use C23-style alignof.
+	* modules/alignof (Depends-on): Add stdalign.
+
 	test-framework-sh: work around AIX 7.2 diff bugs
 	* tests/init.sh (compare_dev_null_): Run diff in the C locale.
 	AIX 7.2 diff messes up in UTF-8 locales; for example,
diff --git a/lib/alignof.h b/lib/alignof.h
index 505ad97aa4..2ded1c7884 100644
--- a/lib/alignof.h
+++ b/lib/alignof.h
@@ -22,14 +22,14 @@
 /* alignof_slot (TYPE)
    Determine the alignment of a structure slot (field) of a given type,
    at compile time.  Note that the result depends on the ABI.
-   This is the same as alignof (TYPE) and _Alignof (TYPE).
+   This is the same as alignof (TYPE).
    Note: The result cannot be used as a value for an 'enum' constant,
    due to bugs in HP-UX 10.20 cc and AIX 3.2.5 xlc.  */
 #if defined __cplusplus
   template <class type> struct alignof_helper { char __slot1; type __slot2; };
 # define alignof_slot(type) offsetof (alignof_helper<type>, __slot2)
 #else
-# define alignof_slot(type) offsetof (struct { char __slot1; type __slot2; }, __slot2)
+# define alignof_slot(type) alignof (type)
 #endif
 
 /* alignof_type (TYPE)
diff --git a/modules/alignof b/modules/alignof
index 4345eaca12..c78ab4a3c5 100644
--- a/modules/alignof
+++ b/modules/alignof
@@ -5,6 +5,7 @@ Files:
 lib/alignof.h
 
 Depends-on:
+stdalign
 
 configure.ac:
 
-- 
2.37.2


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] Define alignof_slot using _Alignof when using C11 or newer
  2023-01-15  1:51 ` Paul Eggert
@ 2023-01-15 19:52   ` Paul Eggert
  2023-01-15 19:56     ` Paul Eggert
                       ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Paul Eggert @ 2023-01-15 19:52 UTC (permalink / raw)
  To: Khem Raj; +Cc: bug-gnulib

[-- Attachment #1: Type: text/plain, Size: 521 bytes --]

On 2023-01-14 17:51, Paul Eggert wrote:

> We already have two copies of that stuff elsewhere (in lib/stdalign.in.h 
> and m4/stdalign.m4), for reasons that currently escape me.

To try to address that in an upward-compatible way, I installed the 
attached. This shrinks the number of copies of that code down to one. 
The basic idea is to prefer C23 semantics in apps and the rest of Gnulib 
by using the new alignasof module, whereas the existing stdalign module 
is now for C11/C17 compatibility and is now deprecated.

[-- Attachment #2: 0001-alignasof-new-module.patch --]
[-- Type: text/x-patch, Size: 26039 bytes --]

From c593e834e1c17daf5c151ec2bdadbccc65b9efd4 Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Sun, 15 Jan 2023 11:48:43 -0800
Subject: [PATCH] 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.
---
 ChangeLog                        |  27 +++++++
 MODULES.html.sh                  |   1 +
 NEWS                             |   2 +
 doc/gnulib.texi                  |   7 +-
 doc/posix-headers/stdalign.texi  |  31 +++++---
 lib/stdalign.in.h                | 109 ++------------------------
 m4/stdalign.m4                   | 127 +++++++++++++++++++++----------
 modules/alignalloc               |   2 +-
 modules/alignasof                |  18 +++++
 modules/alignasof-tests          |  13 ++++
 modules/alignof                  |   2 +-
 modules/argp                     |   2 +-
 modules/crypto/md4-buffer        |   2 +-
 modules/crypto/md5-buffer        |   2 +-
 modules/crypto/sha1-buffer       |   2 +-
 modules/crypto/sha256-buffer     |   2 +-
 modules/crypto/sha512-buffer     |   2 +-
 modules/crypto/sm3-buffer        |   2 +-
 modules/fts                      |   2 +-
 modules/rawmemchr                |   2 +-
 modules/relocatable-prog-wrapper |   2 +-
 modules/stdalign                 |   7 +-
 modules/stdalign-tests           |   7 +-
 modules/stddef-tests             |   2 +-
 modules/sys_socket               |   2 +-
 25 files changed, 198 insertions(+), 179 deletions(-)
 create mode 100644 modules/alignasof
 create mode 100644 modules/alignasof-tests

diff --git a/ChangeLog b/ChangeLog
index 0dedc0c7d6..4755f19360 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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.
diff --git a/MODULES.html.sh b/MODULES.html.sh
index 185e390440..937e9c02d8 100755
--- a/MODULES.html.sh
+++ b/MODULES.html.sh
@@ -2383,6 +2383,7 @@ func_all_modules ()
   func_echo "$element"
 
   func_begin_table
+  func_module alignasof
   func_module stdckdint
   func_end_table
 
diff --git a/NEWS b/NEWS
index f960dd978f..b54781a6f8 100644
--- a/NEWS
+++ b/NEWS
@@ -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).
 
diff --git a/doc/gnulib.texi b/doc/gnulib.texi
index 0f645206bf..7388fb0b45 100644
--- a/doc/gnulib.texi
+++ b/doc/gnulib.texi
@@ -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:
diff --git a/doc/posix-headers/stdalign.texi b/doc/posix-headers/stdalign.texi
index be3cec80d1..7f75a15540 100644
--- a/doc/posix-headers/stdalign.texi
+++ b/doc/posix-headers/stdalign.texi
@@ -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
diff --git a/lib/stdalign.in.h b/lib/stdalign.in.h
index 17357810c7..6523546f16 100644
--- a/lib/stdalign.in.h
+++ b/lib/stdalign.in.h
@@ -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 */
diff --git a/m4/stdalign.m4 b/m4/stdalign.m4
index b1438eeace..0bb9281f5e 100644
--- a/m4/stdalign.m4
+++ b/m4/stdalign.m4
@@ -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
-#   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>
+
+/* 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
-#  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])
+])
diff --git a/modules/alignalloc b/modules/alignalloc
index 0a34b4bcf4..400ab29182 100644
--- a/modules/alignalloc
+++ b/modules/alignalloc
@@ -7,12 +7,12 @@ lib/alignalloc.c
 m4/alignalloc.m4
 
 Depends-on:
+alignasof
 assert-h
 extensions
 extern-inline
 idx
 posix_memalign
-stdalign
 stdckdint
 stdint
 
diff --git a/modules/alignasof b/modules/alignasof
new file mode 100644
index 0000000000..c5795a02d3
--- /dev/null
+++ b/modules/alignasof
@@ -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
diff --git a/modules/alignasof-tests b/modules/alignasof-tests
new file mode 100644
index 0000000000..95602c2fef
--- /dev/null
+++ b/modules/alignasof-tests
@@ -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
diff --git a/modules/alignof b/modules/alignof
index c78ab4a3c5..cc1ab96903 100644
--- a/modules/alignof
+++ b/modules/alignof
@@ -5,7 +5,7 @@ Files:
 lib/alignof.h
 
 Depends-on:
-stdalign
+alignasof
 
 configure.ac:
 
diff --git a/modules/argp b/modules/argp
index 74519a4b86..88cc78c3cb 100644
--- a/modules/argp
+++ b/modules/argp
@@ -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
diff --git a/modules/crypto/md4-buffer b/modules/crypto/md4-buffer
index f5a5f160ed..73f11e35ee 100644
--- a/modules/crypto/md4-buffer
+++ b/modules/crypto/md4-buffer
@@ -7,8 +7,8 @@ lib/md4.c
 m4/md4.m4
 
 Depends-on:
+alignasof
 byteswap
-stdalign
 stdint
 
 configure.ac:
diff --git a/modules/crypto/md5-buffer b/modules/crypto/md5-buffer
index cd51cab2de..8290130b48 100644
--- a/modules/crypto/md5-buffer
+++ b/modules/crypto/md5-buffer
@@ -9,10 +9,10 @@ m4/gl-openssl.m4
 m4/md5.m4
 
 Depends-on:
+alignasof
 byteswap
 c99
 extern-inline
-stdalign
 stdint
 
 configure.ac:
diff --git a/modules/crypto/sha1-buffer b/modules/crypto/sha1-buffer
index a934d4e06b..ca6f477a66 100644
--- a/modules/crypto/sha1-buffer
+++ b/modules/crypto/sha1-buffer
@@ -9,10 +9,10 @@ m4/gl-openssl.m4
 m4/sha1.m4
 
 Depends-on:
+alignasof
 byteswap
 c99
 extern-inline
-stdalign
 stdint
 
 configure.ac:
diff --git a/modules/crypto/sha256-buffer b/modules/crypto/sha256-buffer
index 85529a342e..9e5c029af4 100644
--- a/modules/crypto/sha256-buffer
+++ b/modules/crypto/sha256-buffer
@@ -9,10 +9,10 @@ m4/gl-openssl.m4
 m4/sha256.m4
 
 Depends-on:
+alignasof
 byteswap
 c99
 extern-inline
-stdalign
 stdint
 
 configure.ac:
diff --git a/modules/crypto/sha512-buffer b/modules/crypto/sha512-buffer
index 416a0b5202..2a35e2ef20 100644
--- a/modules/crypto/sha512-buffer
+++ b/modules/crypto/sha512-buffer
@@ -9,10 +9,10 @@ m4/gl-openssl.m4
 m4/sha512.m4
 
 Depends-on:
+alignasof
 byteswap
 c99
 extern-inline
-stdalign
 stdint
 u64
 
diff --git a/modules/crypto/sm3-buffer b/modules/crypto/sm3-buffer
index 3df7d88259..5f2f2f6242 100644
--- a/modules/crypto/sm3-buffer
+++ b/modules/crypto/sm3-buffer
@@ -7,10 +7,10 @@ lib/sm3.c
 m4/sm3.m4
 
 Depends-on:
+alignasof
 byteswap
 c99
 extern-inline
-stdalign
 stdint
 
 configure.ac:
diff --git a/modules/fts b/modules/fts
index e5443adb2f..18b10fac6e 100644
--- a/modules/fts
+++ b/modules/fts
@@ -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
 
diff --git a/modules/rawmemchr b/modules/rawmemchr
index 03da3a27b7..5f3eeaf2d0 100644
--- a/modules/rawmemchr
+++ b/modules/rawmemchr
@@ -7,9 +7,9 @@ lib/rawmemchr.valgrind
 m4/rawmemchr.m4
 
 Depends-on:
+alignasof
 assert-h
 extensions
-stdalign
 stdint
 string
 
diff --git a/modules/relocatable-prog-wrapper b/modules/relocatable-prog-wrapper
index 052499c1ad..c885ce2759 100644
--- a/modules/relocatable-prog-wrapper
+++ b/modules/relocatable-prog-wrapper
@@ -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
diff --git a/modules/stdalign b/modules/stdalign
index af038180fa..c5c29d3088 100644
--- a/modules/stdalign
+++ b/modules/stdalign
@@ -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
diff --git a/modules/stdalign-tests b/modules/stdalign-tests
index e4665ed5fc..2a0d486d77 100644
--- a/modules/stdalign-tests
+++ b/modules/stdalign-tests
@@ -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
diff --git a/modules/stddef-tests b/modules/stddef-tests
index 644eb0d7aa..f81ca4785e 100644
--- a/modules/stddef-tests
+++ b/modules/stddef-tests
@@ -2,8 +2,8 @@ Files:
 tests/test-stddef.c
 
 Depends-on:
+alignasof
 assert-h
-stdalign
 stddef-c++-tests
 
 configure.ac:
diff --git a/modules/sys_socket b/modules/sys_socket
index 44b2792669..ebb5d7d4a6 100644
--- a/modules/sys_socket
+++ b/modules/sys_socket
@@ -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:
-- 
2.37.2


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] Define alignof_slot using _Alignof when using C11 or newer
  2023-01-15 19:52   ` Paul Eggert
@ 2023-01-15 19:56     ` Paul Eggert
  2023-01-15 23:05     ` Khem Raj
  2023-01-16 17:01     ` Bruno Haible
  2 siblings, 0 replies; 6+ messages in thread
From: Paul Eggert @ 2023-01-15 19:56 UTC (permalink / raw)
  To: Khem Raj; +Cc: bug-gnulib

[-- Attachment #1: Type: text/plain, Size: 95 bytes --]

Oh, that patch had a misspelling "alignasf"; fixed by installing the 
attached further patch.


[-- Attachment #2: 0001-sys_socket-fix-typo.patch --]
[-- Type: text/x-patch, Size: 567 bytes --]

From 784f8c776223ba3234c7673664f1fd2a9629bcda Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Sun, 15 Jan 2023 11:53:40 -0800
Subject: [PATCH] sys_socket: fix typo

Fix typo in previous change.
---
 modules/sys_socket | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/modules/sys_socket b/modules/sys_socket
index ebb5d7d4a6..57ea690270 100644
--- a/modules/sys_socket
+++ b/modules/sys_socket
@@ -8,7 +8,7 @@ m4/sys_socket_h.m4
 m4/sockpfaf.m4
 
 Depends-on:
-alignasf
+alignasof
 errno
 extern-inline
 gen-header
-- 
2.37.2


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] Define alignof_slot using _Alignof when using C11 or newer
  2023-01-15 19:52   ` Paul Eggert
  2023-01-15 19:56     ` Paul Eggert
@ 2023-01-15 23:05     ` Khem Raj
  2023-01-16 17:01     ` Bruno Haible
  2 siblings, 0 replies; 6+ messages in thread
From: Khem Raj @ 2023-01-15 23:05 UTC (permalink / raw)
  To: Paul Eggert; +Cc: bug-gnulib

[-- Attachment #1: Type: text/plain, Size: 780 bytes --]

On Sun, Jan 15, 2023 at 11:52 AM Paul Eggert <eggert@cs.ucla.edu> wrote:

> On 2023-01-14 17:51, Paul Eggert wrote:
>
> > We already have two copies of that stuff elsewhere (in lib/stdalign.in.h
> > and m4/stdalign.m4), for reasons that currently escape me.
>
> To try to address that in an upward-compatible way, I installed the
> attached. This shrinks the number of copies of that code down to one.
> The basic idea is to prefer C23 semantics in apps and the rest of Gnulib
> by using the new alignasof module, whereas the existing stdalign module
> is now for C11/C17 compatibility and is now deprecated.


Your patch fixed the issue I did not try regenerating the gnulib in the
concerned project but copied the change manually to test. AFAICT c++ looks
to be not affected

>

[-- Attachment #2: Type: text/html, Size: 1253 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] Define alignof_slot using _Alignof when using C11 or newer
  2023-01-15 19:52   ` Paul Eggert
  2023-01-15 19:56     ` Paul Eggert
  2023-01-15 23:05     ` Khem Raj
@ 2023-01-16 17:01     ` Bruno Haible
  2 siblings, 0 replies; 6+ messages in thread
From: Bruno Haible @ 2023-01-16 17:01 UTC (permalink / raw)
  To: bug-gnulib, Paul Eggert; +Cc: Khem Raj

Paul Eggert wrote:
> The basic idea is to prefer C23 semantics in apps and the rest of Gnulib 
> by using the new alignasof module, whereas the existing stdalign module 
> is now for C11/C17 compatibility and is now deprecated.

On Android (and probably some other platforms) I now see a compilation error:

terminal-gcc -mandroid -D__ANDROID__ -std=gnu99 -DHAVE_CONFIG_H -I. -I../../gltests -I..  -DGNULIB_STRICT_CHECKING=1 -DIN_GNULIB_TESTS=1 -I. -I../../gltests -I.. -I../../gltests/.. -I../gllib -I../../gltests/../gllib -I/data/data/com.spartacusrex.spartacuside/files/local/include -Wall  -Wno-error -O2 -c -o test-stddef.o ../../gltests/test-stddef.c
../../gltests/test-stddef.c:23:22: error: stdalign.h: No such file or directory
make[4]: *** [test-stddef.o] Error 1

This patch fixes it. It could already have been done on 2022-09-13, but
is needed now that the 'alignasof' module is used instead of the old,
deprecated 'stdalign' module.


2023-01-16  Bruno Haible  <bruno@clisp.org>

	stddef tests: Fix compilation error (regression 2023-01-15).
	* tests/test-stddef.c: Don't include <stdalign.h>.

diff --git a/tests/test-stddef.c b/tests/test-stddef.c
index 25ad00e490..1d674b3cea 100644
--- a/tests/test-stddef.c
+++ b/tests/test-stddef.c
@@ -20,7 +20,6 @@
 
 #include <stddef.h>
 #include <limits.h>
-#include <stdalign.h>
 
 /* Check that appropriate types are defined.  */
 wchar_t a = 'c';





^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-01-16 17:02 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-15  1:20 [PATCH v2] Define alignof_slot using _Alignof when using C11 or newer Khem Raj
2023-01-15  1:51 ` Paul Eggert
2023-01-15 19:52   ` Paul Eggert
2023-01-15 19:56     ` Paul Eggert
2023-01-15 23:05     ` Khem Raj
2023-01-16 17:01     ` Bruno Haible

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).