]> Savannah Git Hosting - gnulib.git/commitdiff
stdalign: alignas and alignof are C23 keywords
authorPaul Eggert <eggert@cs.ucla.edu>
Tue, 13 Sep 2022 22:29:36 +0000 (17:29 -0500)
committerPaul Eggert <eggert@cs.ucla.edu>
Tue, 13 Sep 2022 22:52:21 +0000 (17:52 -0500)
* 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.

ChangeLog
doc/gnulib.texi
doc/posix-headers/stdalign.texi
m4/stdalign.m4
modules/stdalign
tests/test-stdalign.c

index 7c1b0afaddddaf0d9791c44bc855c57851064d75..e05f5812824fef1877745ce981360ed288ec43f0 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -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
index 1bef2a0975ad7f1e4326a370c058593832a92321..e1debc97425ee696de1151eadef76386656b1203 100644 (file)
@@ -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}
 
index 4ed83bec3e933771059496097ee0fb56138d7eea..be3cec80d16140033dac2164591a6260be01a70c 100644 (file)
@@ -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
index c3283760d526589ebd60fa35cec420e5d9fe96a6..9164c2f35703ffd27502d60fa16a630ffbd3f66a 100644 (file)
@@ -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])
 ])
index 24c79ec4936a5396ae7c3cefa004f2b5299c2fff..af038180fafc928553e315d36d018311012618a2 100644 (file)
@@ -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+
index ea07543802a461081bad17377d88fa403a9fd67a..a9355545636030347d69789725089cffc3901ac2 100644 (file)
@@ -18,8 +18,6 @@
 
 #include <config.h>
 
-#include <stdalign.h>
-
 #include <stddef.h>
 #include <stdint.h>