stdalign: check that alignof and offsetof are consistent
[gnulib.git] / m4 / stdalign.m4
1 # Check for stdalign.h that conforms to C11.
2
3 dnl Copyright 2011-2012 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
7
8 # Prepare for substituting <stdalign.h> if it is not supported.
9
10 AC_DEFUN([gl_STDALIGN_H],
11 [
12   AC_CACHE_CHECK([for working stdalign.h],
13     [gl_cv_header_working_stdalign_h],
14     [AC_COMPILE_IFELSE(
15        [AC_LANG_PROGRAM(
16           [[#include <stdalign.h>
17             #include <stddef.h>
18
19             /* Test that alignof yields a result consistent with offsetof.
20                This catches GCC bug 51316
21                <http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51316>.  */
22             #ifdef __cplusplus
23                template <class t> struct alignof_helper { char a; t b; };
24             # define ao(type) offsetof (alignof_helper<type>, b)
25             #else
26             # define ao(type) offsetof (struct { char a; type b; }, b)
27             #endif
28             char test1[_Alignof (double) == ao (double) ? 1 : -1];
29             char test2[alignof (long int) == ao (long int) ? 1 : -1];
30
31             /* Test _Alignas only on platforms where gnulib can help.  */
32             #if \
33                 (__GNUC__ || __IBMC__ || __IBMCPP__ \
34                  || 0x5110 <= __SUNPRO_C || 1300 <= _MSC_VER)
35               int alignas (8) alignas_int = 1;
36               char test3[8 <= _Alignof (alignas_int) ? 1 : -1];
37             #endif
38           ]])],
39        [gl_cv_header_working_stdalign_h=yes],
40        [gl_cv_header_working_stdalign_h=no])])
41
42   if test $gl_cv_header_working_stdalign_h = yes; then
43     STDALIGN_H=''
44   else
45     STDALIGN_H='stdalign.h'
46   fi
47
48   AC_SUBST([STDALIGN_H])
49   AM_CONDITIONAL([GL_GENERATE_STDALIGN_H], [test -n "$STDALIGN_H"])
50 ])