On FreeBSD 12.2/sparc64, in a testdir of gnulib, I see a compilation error
in most or all C++ compilation units. Such as this one:

In file included from /usr/include/stddef.h:41,
                 from ../gllib/stddef.h:80,
                 from ../config.h:8257,
                 from ../../gltests/test-limits-h-c++.cc:20:
/usr/include/sys/_types.h:107: error: expected `)' before '(' token
/usr/include/sys/_types.h:107: error: requested alignment is not a constant
/usr/include/sys/_types.h:109: error: expected `)' before '(' token
/usr/include/sys/_types.h:109: error: requested alignment is not a constant

The cause is that, after preprocessing, part of /usr/include/sys/_types.h
produces:

  typedef struct {
   long long __max_align1 __attribute__((__aligned__(offsetof 
(__alignof_helper<long long>, __b))));

   long double __max_align2 __attribute__((__aligned__(offsetof 
(__alignof_helper<long double>, __b))));

  } __max_align_t;

and this is invalid syntax, because 'offsetof' is not a defined macro at this
point.

We had a similar fix in C mode earlier:

    2023-01-24  Bruno Haible  <[email protected]>

        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.

The same thing needs to be done in C++ mode.


2023-08-21  Bruno Haible  <[email protected]>

        alignasof, stdalign: Fix a compilation error in C++ mode on FreeBSD 12.
        * 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.

diff --git a/m4/stdalign.m4 b/m4/stdalign.m4
index 6a39ffe756..5880efb2eb 100644
--- a/m4/stdalign.m4
+++ b/m4/stdalign.m4
@@ -112,7 +112,11 @@ AC_DEFUN([gl_ALIGNASOF]
 #     define _Alignof(type) alignof (type)
 #    else
       template <class __t> struct __alignof_helper { char __a; __t __b; };
-#     define _Alignof(type) offsetof (__alignof_helper<type>, __b)
+#     if (defined __GNUC__ && 4 <= __GNUC__) || defined __clang__
+#      define _Alignof(type) __builtin_offsetof (__alignof_helper<type>, __b)
+#     else
+#      define _Alignof(type) offsetof (__alignof_helper<type>, __b)
+#     endif
 #     define _GL_STDALIGN_NEEDS_STDDEF 1
 #    endif
 #   else




Reply via email to