| 1 | # Check for alignas and alignof that conform to C23.
 | 
|---|
| 2 | 
 | 
|---|
| 3 | dnl Copyright 2011-2022 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 alignas and alignof],
 | 
|---|
| 13 |     [gl_cv_header_working_stdalign_h],
 | 
|---|
| 14 |     [gl_save_CFLAGS=$CFLAGS
 | 
|---|
| 15 |      for gl_working in "yes, keywords" "yes, <stdalign.h> macros"; do
 | 
|---|
| 16 |       AS_CASE([$gl_working],
 | 
|---|
| 17 |         [*stdalign.h*], [CFLAGS="$gl_save_CFLAGS -DINCLUDE_STDALIGN_H"])
 | 
|---|
| 18 |       AC_COMPILE_IFELSE(
 | 
|---|
| 19 |        [AC_LANG_PROGRAM(
 | 
|---|
| 20 |           [[#include <stdint.h>
 | 
|---|
| 21 |             #ifdef INCLUDE_STDALIGN_H
 | 
|---|
| 22 |              #include <stdalign.h>
 | 
|---|
| 23 |             #endif
 | 
|---|
| 24 |             #include <stddef.h>
 | 
|---|
| 25 | 
 | 
|---|
| 26 |             /* Test that alignof yields a result consistent with offsetof.
 | 
|---|
| 27 |                This catches GCC bug 52023
 | 
|---|
| 28 |                <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52023>.  */
 | 
|---|
| 29 |             #ifdef __cplusplus
 | 
|---|
| 30 |                template <class t> struct alignof_helper { char a; t b; };
 | 
|---|
| 31 |             # define ao(type) offsetof (alignof_helper<type>, b)
 | 
|---|
| 32 |             #else
 | 
|---|
| 33 |             # define ao(type) offsetof (struct { char a; type b; }, b)
 | 
|---|
| 34 |             #endif
 | 
|---|
| 35 |             char test_double[ao (double) % _Alignof (double) == 0 ? 1 : -1];
 | 
|---|
| 36 |             char test_long[ao (long int) % _Alignof (long int) == 0 ? 1 : -1];
 | 
|---|
| 37 |             char test_alignof[alignof (double) == _Alignof (double) ? 1 : -1];
 | 
|---|
| 38 | 
 | 
|---|
| 39 |             /* Test alignas only on platforms where gnulib can help.  */
 | 
|---|
| 40 |             #if \
 | 
|---|
| 41 |                 ((defined __cplusplus && 201103 <= __cplusplus) \
 | 
|---|
| 42 |                  || (__TINYC__ && defined __attribute__) \
 | 
|---|
| 43 |                  || (defined __APPLE__ && defined __MACH__ \
 | 
|---|
| 44 |                      ? 4 < __GNUC__ + (1 <= __GNUC_MINOR__) \
 | 
|---|
| 45 |                      : __GNUC__) \
 | 
|---|
| 46 |                  || (__ia64 && (61200 <= __HP_cc || 61200 <= __HP_aCC)) \
 | 
|---|
| 47 |                  || __ICC || 0x590 <= __SUNPRO_C || 0x0600 <= __xlC__ \
 | 
|---|
| 48 |                  || 1300 <= _MSC_VER)
 | 
|---|
| 49 |               struct alignas_test { char c; char alignas (8) alignas_8; };
 | 
|---|
| 50 |               char test_alignas[offsetof (struct alignas_test, alignas_8) == 8
 | 
|---|
| 51 |                                 ? 1 : -1];
 | 
|---|
| 52 |             #endif
 | 
|---|
| 53 |           ]])],
 | 
|---|
| 54 |        [gl_cv_header_working_stdalign_h=$gl_working],
 | 
|---|
| 55 |        [gl_cv_header_working_stdalign_h=no])
 | 
|---|
| 56 | 
 | 
|---|
| 57 |       CFLAGS=$gl_save_CFLAGS
 | 
|---|
| 58 |       test "$gl_cv_header_working_stdalign_h" != no && break
 | 
|---|
| 59 |      done])
 | 
|---|
| 60 | 
 | 
|---|
| 61 |   GL_GENERATE_STDALIGN_H=false
 | 
|---|
| 62 |   AS_CASE([$gl_cv_header_working_stdalign_h],
 | 
|---|
| 63 |     [no],
 | 
|---|
| 64 |       [GL_GENERATE_STDALIGN_H=true],
 | 
|---|
| 65 |     [yes*keyword*],
 | 
|---|
| 66 |       [AC_DEFINE([HAVE_C_ALIGNASOF], [1],
 | 
|---|
| 67 |          [Define to 1 if the alignas and alignof keywords work.])])
 | 
|---|
| 68 | 
 | 
|---|
| 69 |   AC_CHECK_HEADERS_ONCE([stdalign.h])
 | 
|---|
| 70 | 
 | 
|---|
| 71 |   dnl The "zz" puts this toward config.h's end, to avoid potential
 | 
|---|
| 72 |   dnl collisions with other definitions.
 | 
|---|
| 73 |   AH_VERBATIM([zzalignas],
 | 
|---|
| 74 | [#if !defined HAVE_C_ALIGNASOF && __cplusplus < 201103 && !defined alignof
 | 
|---|
| 75 | # if HAVE_STDALIGN_H
 | 
|---|
| 76 | #  include <stdalign.h>
 | 
|---|
| 77 | # else
 | 
|---|
| 78 |    /* Substitute.  Keep consistent with gnulib/lib/stdalign.in.h.  */
 | 
|---|
| 79 | #  ifndef _GL_STDALIGN_H
 | 
|---|
| 80 | #   define _GL_STDALIGN_H
 | 
|---|
| 81 | #   undef _Alignas
 | 
|---|
| 82 | #   undef _Alignof
 | 
|---|
| 83 | #   if (!defined __STDC_VERSION__ || __STDC_VERSION__ < 201112 \
 | 
|---|
| 84 |         || (defined __GNUC__ && __GNUC__ < 4 + (__GNUC_MINOR__ < 9) \
 | 
|---|
| 85 |             && !defined __clang__) \
 | 
|---|
| 86 |         || (defined __clang__ && __clang_major__ < 8))
 | 
|---|
| 87 | #    ifdef __cplusplus
 | 
|---|
| 88 | #     if (201103 <= __cplusplus || defined _MSC_VER)
 | 
|---|
| 89 | #      define _Alignof(type) alignof (type)
 | 
|---|
| 90 | #     else
 | 
|---|
| 91 |        template <class __t> struct __alignof_helper { char __a; __t __b; };
 | 
|---|
| 92 | #      define _Alignof(type) offsetof (__alignof_helper<type>, __b)
 | 
|---|
| 93 | #      define _GL_STDALIGN_NEEDS_STDDEF 1
 | 
|---|
| 94 | #     endif
 | 
|---|
| 95 | #    else
 | 
|---|
| 96 | #     define _Alignof(type) offsetof (struct { char __a; type __b; }, __b)
 | 
|---|
| 97 | #     define _GL_STDALIGN_NEEDS_STDDEF 1
 | 
|---|
| 98 | #    endif
 | 
|---|
| 99 | #   endif
 | 
|---|
| 100 | #   if ! (defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER))
 | 
|---|
| 101 | #    define alignof _Alignof
 | 
|---|
| 102 | #   endif
 | 
|---|
| 103 | #   define __alignof_is_defined 1
 | 
|---|
| 104 | #   if !defined __STDC_VERSION__ || __STDC_VERSION__ < 201112
 | 
|---|
| 105 | #    if defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER)
 | 
|---|
| 106 | #     define _Alignas(a) alignas (a)
 | 
|---|
| 107 | #    elif (!defined __attribute__ \
 | 
|---|
| 108 |            && ((defined __APPLE__ && defined __MACH__ \
 | 
|---|
| 109 |                 ? 4 < __GNUC__ + (1 <= __GNUC_MINOR__) \
 | 
|---|
| 110 |                 : __GNUC__ && !defined __ibmxl__) \
 | 
|---|
| 111 |                || (4 <= __clang_major__) \
 | 
|---|
| 112 |                || (__ia64 && (61200 <= __HP_cc || 61200 <= __HP_aCC)) \
 | 
|---|
| 113 |                || __ICC || 0x590 <= __SUNPRO_C || 0x0600 <= __xlC__))
 | 
|---|
| 114 | #     define _Alignas(a) __attribute__ ((__aligned__ (a)))
 | 
|---|
| 115 | #    elif 1300 <= _MSC_VER
 | 
|---|
| 116 | #     define _Alignas(a) __declspec (align (a))
 | 
|---|
| 117 | #    endif
 | 
|---|
| 118 | #   endif
 | 
|---|
| 119 | #   if ((defined _Alignas \
 | 
|---|
| 120 |          && !(defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER))) \
 | 
|---|
| 121 |         || (defined __STDC_VERSION__ && 201112 <= __STDC_VERSION__))
 | 
|---|
| 122 | #    define alignas _Alignas
 | 
|---|
| 123 | #   endif
 | 
|---|
| 124 | #   if (defined alignas \
 | 
|---|
| 125 |         || (defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER)))
 | 
|---|
| 126 | #    define __alignas_is_defined 1
 | 
|---|
| 127 | #   endif
 | 
|---|
| 128 | #   if _GL_STDALIGN_NEEDS_STDDEF
 | 
|---|
| 129 | #    include <stddef.h>
 | 
|---|
| 130 | #   endif
 | 
|---|
| 131 | #  endif /* _GL_STDALIGN_H */
 | 
|---|
| 132 | # endif
 | 
|---|
| 133 | #endif])
 | 
|---|
| 134 | ])
 | 
|---|