bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
* alignasof, stdalign: Fix a compilation error on FreeBSD 12.0
@ 2023-01-24 13:02 Bruno Haible
  2023-01-24 13:34 ` Bruno Haible
  0 siblings, 1 reply; 2+ messages in thread
From: Bruno Haible @ 2023-01-24 13:02 UTC (permalink / raw)
  To: bug-gnulib

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

On FreeBSD 12.0/i386 I get a compilation error when building a testdir
for module 'rawmemchr'.

On the master branch, the first error is:

In file included from ../../gllib/rawmemchr.c:17:
In file included from ../config.h:1018:
In file included from /usr/include/stddef.h:41:
/usr/include/sys/_types.h:106:35: error: expected expression
        long long __max_align1 __aligned(_Alignof(long long));
                                         ^
../config.h:964:39: note: expanded from macro '_Alignof'
#     define _Alignof(type) offsetof (struct { char __a; type __b; }, __b)
                                      ^

On the stable-202301 branch, the first error is:

In file included from ../../gllib/rawmemchr.c:17:
In file included from ../config.h:925:
In file included from ./stdalign.h:131:
In file included from /usr/include/stddef.h:41:
/usr/include/sys/_types.h:106:35: error: expected expression
        long long __max_align1 __aligned(_Alignof(long long));
                                         ^
./stdalign.h:70:36: note: expanded from macro '_Alignof'
#  define _Alignof(type) offsetof (struct { char __a; type __b; }, __b)
                                   ^

What happens is that
  - config.h includes <stdalign.h>, per the patch that I submitted in
    <https://lists.gnu.org/archive/html/bug-gnulib/2022-09/msg00144.html>.
  - This <stdalign.h> is the gnulib-generated one.
    (In <https://lists.gnu.org/archive/html/bug-gnulib/2022-09/msg00144.html>
    the purpose of my patch was to avoid including the gnulib-generated
    stdalign.h, but it did not achieve that goal since on this platform
    HAVE_STDALIGN_H is 1, following these configure findings:
      checking for stdalign.h... yes
      checking for alignas and alignof... no
    . Anyway.)
  - At the end of <stdalign.h>, <stddef.h> gets included.
  - <stddef.h> includes <sys/_types.h>, which attempts to define max_align_t.
    For this it uses _Alignof(long long), which expands to
    offsetof (struct { char __a; long long __b; }, __b)
  - But at this point offsetof is not yet defined (since <stddef.h> is still
    being included).

The following patch fixes it. One in master, a different one for the
stable-202301 branch. (On the stable-202207 branch there is no problem.)


[-- Attachment #2: 0001-master-alignasof-stdalign-Fix-a-compilation-error-on-FreeBS.patch --]
[-- Type: text/x-patch, Size: 1800 bytes --]

From b323b5297757c5c904e6aff63454c777703361ce Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Tue, 24 Jan 2023 13:44:07 +0100
Subject: [PATCH] alignasof, stdalign: Fix a compilation error on FreeBSD 12.0.

* m4/stdalign.m4 (gl_ALIGNASOF): In C mode, prefer __builtin_offsetof
over offsetof when possible, since __builtin_offsetof works also when
<stddef.h> has not been fully included yet.
---
 ChangeLog      | 7 +++++++
 m4/stdalign.m4 | 8 ++++++--
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 759310346b..f999f105a5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2023-01-24  Bruno Haible  <bruno@clisp.org>
+
+	alignasof, stdalign: Fix a compilation error on FreeBSD 12.0.
+	* m4/stdalign.m4 (gl_ALIGNASOF): In C mode, prefer __builtin_offsetof
+	over offsetof when possible, since __builtin_offsetof works also when
+	<stddef.h> has not been fully included yet.
+
 2023-01-22  Bruno Haible  <bruno@clisp.org>
 
 	doc: Update list of target platforms.
diff --git a/m4/stdalign.m4 b/m4/stdalign.m4
index 0bb9281f5e..6a0ab2c102 100644
--- a/m4/stdalign.m4
+++ b/m4/stdalign.m4
@@ -112,8 +112,12 @@ AC_DEFUN([gl_ALIGNASOF],
 #      define _GL_STDALIGN_NEEDS_STDDEF 1
 #     endif
 #    else
-#     define _Alignof(type) offsetof (struct { char __a; type __b; }, __b)
-#     define _GL_STDALIGN_NEEDS_STDDEF 1
+#     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
 #   endif
 #   if ! (defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER))
-- 
2.34.1


[-- Attachment #3: 0001-stable-stdalign-Fix-a-compilation-error-on-FreeBSD-12.0.patch --]
[-- Type: text/x-patch, Size: 2604 bytes --]

From 1f4fe409ea4379c3e537507822151e135be758d8 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Tue, 24 Jan 2023 13:36:11 +0100
Subject: [PATCH] stdalign: Fix a compilation error on FreeBSD 12.0.

* lib/stdalign.in.h (_Alignof): In C mode, prefer __builtin_offsetof
over offsetof when possible, since __builtin_offsetof works also when
<stddef.h> has not been fully included yet.
* m4/stdalign.m4 (gl_STDALIGN_H): Likewise.
---
 ChangeLog         | 8 ++++++++
 lib/stdalign.in.h | 8 ++++++--
 m4/stdalign.m4    | 8 ++++++--
 3 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index ca8306d1e4..bd7fd9ab5d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2023-01-24  Bruno Haible  <bruno@clisp.org>
+
+	stdalign: Fix a compilation error on FreeBSD 12.0.
+	* lib/stdalign.in.h (_Alignof): In C mode, prefer __builtin_offsetof
+	over offsetof when possible, since __builtin_offsetof works also when
+	<stddef.h> has not been fully included yet.
+	* m4/stdalign.m4 (gl_STDALIGN_H): Likewise.
+
 2023-01-22  Paul Eggert  <eggert@cs.ucla.edu>
 
 	sigsegv, vma-iter: port to Solaris 10
diff --git a/lib/stdalign.in.h b/lib/stdalign.in.h
index 17357810c7..7f9dbb466e 100644
--- a/lib/stdalign.in.h
+++ b/lib/stdalign.in.h
@@ -66,8 +66,12 @@
 #   define _GL_STDALIGN_NEEDS_STDDEF 1
 #  endif
 # else
-#  define _Alignof(type) offsetof (struct { char __a; type __b; }, __b)
-#  define _GL_STDALIGN_NEEDS_STDDEF 1
+#  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
 #endif
 #if ! (defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER))
diff --git a/m4/stdalign.m4 b/m4/stdalign.m4
index b1438eeace..dc2971753d 100644
--- a/m4/stdalign.m4
+++ b/m4/stdalign.m4
@@ -93,8 +93,12 @@ AC_DEFUN([gl_STDALIGN_H],
 #      define _GL_STDALIGN_NEEDS_STDDEF 1
 #     endif
 #    else
-#     define _Alignof(type) offsetof (struct { char __a; type __b; }, __b)
-#     define _GL_STDALIGN_NEEDS_STDDEF 1
+#     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
 #   endif
 #   if ! (defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER))
-- 
2.34.1


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

end of thread, other threads:[~2023-01-24 13:34 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-24 13:02 alignasof, stdalign: Fix a compilation error on FreeBSD 12.0 Bruno Haible
2023-01-24 13:34 ` 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).