bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Bruno Haible <bruno@clisp.org>
To: bug-gnulib@gnu.org
Subject: alignasof: Ensure a correct _Alignof with older versions of GCC, clang
Date: Sat, 28 Jan 2023 03:28:10 +0100	[thread overview]
Message-ID: <12256368.OGSbbv5N1J@nimes> (raw)

On FreeBSD 11.0/x86 and FreeBSD 12.0/x86 I now see these compilation errors:

../../gltests/test-alignasof.c:56:1: error: static_assert failed "static assertion failed"
CHECK_STATIC (int64_t);
^~~~~~~~~~~~~~~~~~~~~~
../../gltests/test-alignasof.c:45:3: note: expanded from macro 'CHECK_STATIC'
  static_assert (alignof (type) == offsetof (type##_helper, slot2)); \
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/assert.h:73:23: note: expanded from macro 'static_assert'
#define static_assert   _Static_assert
                        ^
../gllib/assert.h:256:7: note: expanded from macro '_Static_assert'
      _GL_STATIC_ASSERT (__VA_ARGS__, "static assertion failed", -)
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../gllib/assert.h:241:48: note: expanded from macro '_GL_STATIC_ASSERT'
# define _GL_STATIC_ASSERT(R, DIAGNOSTIC, ...) _Static_assert (R, DIAGNOSTIC)
                                               ^               ~
../../gltests/test-alignasof.c:59:1: error: static_assert failed "static assertion failed"
CHECK_STATIC (double);
^~~~~~~~~~~~~~~~~~~~~
../../gltests/test-alignasof.c:45:3: note: expanded from macro 'CHECK_STATIC'
  static_assert (alignof (type) == offsetof (type##_helper, slot2)); \
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/assert.h:73:23: note: expanded from macro 'static_assert'
#define static_assert   _Static_assert
                        ^
../gllib/assert.h:256:7: note: expanded from macro '_Static_assert'
      _GL_STATIC_ASSERT (__VA_ARGS__, "static assertion failed", -)
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../gllib/assert.h:241:48: note: expanded from macro '_GL_STATIC_ASSERT'
# define _GL_STATIC_ASSERT(R, DIAGNOSTIC, ...) _Static_assert (R, DIAGNOSTIC)
                                               ^               ~
2 errors generated.


and similarly in test-alignof.c. The reason is that

  * On these platforms, clang version 3.8.0 and 6.0.1 is used, respectively.

  * These old GCC and clang versions have a bug in _Alignof, that we work
    around since 2015-10-18.

  * But since 2023-01-15, the workaround exists only in one place: in config.h.
    No longer in gnulib's generated <stdalign.h>.

  * There, in config.h, it is not applied if the system has a <stdalign.h> file.

This patch fixes it.


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

	alignasof: Ensure a correct _Alignof with older versions of GCC, clang.
	* m4/stdalign.m4 (gl_ALIGNASOF): Apply the workaround for wrong _Alignof
	in GCC < 4.9 and clang < 8.0.0 also when the system has <stdalign.h>.

diff --git a/ChangeLog b/ChangeLog
index 34514a6d9a..8b26652f22 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2023-01-27  Bruno Haible  <bruno@clisp.org>
+
+	alignasof: Ensure a correct _Alignof (regression 2023-01-15).
+	* m4/stdalign.m4 (gl_ALIGNASOF): Apply the workaround for wrong _Alignof
+	in GCC < 4.9 and clang < 8.0.0 also when the system has <stdalign.h>.
+
 2023-01-27  Bruno Haible  <bruno@clisp.org>
 
 	stdalign: Ensure alignof is defined on FreeBSD (regression 2023-01-15).
diff --git a/m4/stdalign.m4 b/m4/stdalign.m4
index 3e83a2ca58..bfb08027b2 100644
--- a/m4/stdalign.m4
+++ b/m4/stdalign.m4
@@ -71,7 +71,8 @@ AC_DEFUN([gl_ALIGNASOF],
 [#if !defined HAVE_C_ALIGNASOF && __cplusplus < 201103 && !defined alignof
 # if HAVE_STDALIGN_H
 #  include <stdalign.h>
-# else
+# endif
+
 /* ISO C23 alignas and alignof for platforms that lack it.
 
    References:
@@ -99,30 +100,32 @@ AC_DEFUN([gl_ALIGNASOF],
 /* 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
+#  if (!defined __STDC_VERSION__ || __STDC_VERSION__ < 201112 \
+       || (defined __GNUC__ && __GNUC__ < 4 + (__GNUC_MINOR__ < 9) \
+           && !defined __clang__) \
+       || (defined __clang__ && __clang_major__ < 8))
+#   undef/**/_Alignof
+#   ifdef __cplusplus
+#    if (201103 <= __cplusplus || defined _MSC_VER)
+#     define _Alignof(type) alignof (type)
 #    else
-#     if (defined __GNUC__ && 4 <= __GNUC__) || defined __clang__
-#      define _Alignof(type) __builtin_offsetof (struct { char __a; type __b; }, __b)
-#     else
-#      define _Alignof(type) offsetof (struct { char __a; type __b; }, __b)
-#      define _GL_STDALIGN_NEEDS_STDDEF 1
-#     endif
+      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
+#    if (defined __GNUC__ && 4 <= __GNUC__) || defined __clang__
+#     define _Alignof(type) __builtin_offsetof (struct { char __a; type __b; }, __b)
+#    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
+#  endif
+#  if ! (defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER))
+#   undef/**/alignof
+#   define alignof _Alignof
+#  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
@@ -148,6 +151,7 @@ AC_DEFUN([gl_ALIGNASOF],
      - alignas (TYPE) is equivalent to alignas (alignof (TYPE)).
 
    */
+# if !HAVE_STDALIGN_H
 #  if !defined __STDC_VERSION__ || __STDC_VERSION__ < 201112
 #   if defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER)
 #    define _Alignas(a) alignas (a)
@@ -169,9 +173,10 @@ AC_DEFUN([gl_ALIGNASOF],
        || (defined __STDC_VERSION__ && 201112 <= __STDC_VERSION__))
 #   define alignas _Alignas
 #  endif
-#  if _GL_STDALIGN_NEEDS_STDDEF
-#   include <stddef.h>
-#  endif
+# endif
+
+# if _GL_STDALIGN_NEEDS_STDDEF
+#  include <stddef.h>
 # endif
 #endif])
 ])





                 reply	other threads:[~2023-01-28  2:28 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=12256368.OGSbbv5N1J@nimes \
    --to=bruno@clisp.org \
    --cc=bug-gnulib@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).