summaryrefslogtreecommitdiffstats
path: root/src/gl/tests/test-stdalign.c
blob: 38812b67c8caf9140e864c29d067f48a11b22682 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* Test of <stdalign.h>.
   Copyright 2009-2021 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

/* Written by Paul Eggert, inspired by Bruno Haible's test-alignof.c.  */

#include <config.h>

#include <stdalign.h>

#include <stddef.h>
#include <stdint.h>

#include "verify.h"

#include "macros.h"

typedef long double longdouble;
typedef struct { char a[1]; } struct1;
typedef struct { char a[2]; } struct2;
typedef struct { char a[3]; } struct3;
typedef struct { char a[4]; } struct4;

verify (__alignof_is_defined == 1);
#ifndef alignof
# error "alignof is not a macro"
#endif

#if __alignas_is_defined
verify (__alignas_is_defined == 1);
# ifndef alignas
#  error "alignas is not a macro"
# endif
/* mingw can go up only to 8.  8 is all that GNU Emacs needs, so let's
   limit the test to 8 for now.  */
# define TEST_ALIGNMENT 8
#else
# define _Alignas(alignment)
# define alignas(alignment)
# define TEST_ALIGNMENT 1
#endif

#define CHECK_STATIC(type) \
  typedef struct { char slot1; type slot2; } type##_helper; \
  verify (alignof (type) == offsetof (type##_helper, slot2)); \
  verify (_Alignof (type) == alignof (type)); \
  const int type##_alignment = alignof (type); \
  type alignas (TEST_ALIGNMENT) static_##type##_alignas; \
  type _Alignas (TEST_ALIGNMENT) static_##type##_Alignas

#define CHECK_ALIGNED(var) ASSERT ((uintptr_t) &(var) % TEST_ALIGNMENT == 0)

CHECK_STATIC (char);
CHECK_STATIC (short);
CHECK_STATIC (int);
CHECK_STATIC (long);
#ifdef INT64_MAX
CHECK_STATIC (int64_t);
#endif
CHECK_STATIC (float);
CHECK_STATIC (double);
/* CHECK_STATIC (longdouble); */
CHECK_STATIC (struct1);
CHECK_STATIC (struct2);
CHECK_STATIC (struct3);
CHECK_STATIC (struct4);

int
main ()
{
#if defined __SUNPRO_C && __SUNPRO_C < 0x5150
  /* Avoid a test failure due to Sun Studio Developer Bug Report #2125432.  */
  fputs ("Skipping test: known Sun C compiler bug\n", stderr);
  return 77;
#elif defined __HP_cc && __ia64
  /* Avoid a test failure due to HP-UX Itanium cc bug; see:
     https://lists.gnu.org/r/bug-gnulib/2017-03/msg00078.html  */
  fputs ("Skipping test: known HP-UX Itanium cc compiler bug\n", stderr);
  return 77;
#elif defined __clang__ && defined __ibmxl__
  /* Avoid a test failure with IBM xlc 16.1.  It ignores alignas (8),
     _Alignas (8), and __attribute__ ((__aligned__ (8))).  */
  fputs ("Skipping test: known AIX XL C compiler deficiency\n", stderr);
  return 77;
#else
  CHECK_ALIGNED (static_char_alignas);
  CHECK_ALIGNED (static_char_Alignas);
  CHECK_ALIGNED (static_short_alignas);
  CHECK_ALIGNED (static_short_Alignas);
  CHECK_ALIGNED (static_int_alignas);
  CHECK_ALIGNED (static_int_Alignas);
  CHECK_ALIGNED (static_long_alignas);
  CHECK_ALIGNED (static_long_Alignas);
# ifdef INT64_MAX
  CHECK_ALIGNED (static_int64_t_alignas);
  CHECK_ALIGNED (static_int64_t_Alignas);
# endif
  CHECK_ALIGNED (static_float_alignas);
  CHECK_ALIGNED (static_float_Alignas);
  CHECK_ALIGNED (static_double_alignas);
  CHECK_ALIGNED (static_double_Alignas);
  /* CHECK_ALIGNED (static_longdouble_alignas); */
  /* CHECK_ALIGNED (static_longdouble_Alignas); */
  CHECK_ALIGNED (static_struct1_alignas);
  CHECK_ALIGNED (static_struct1_Alignas);
  CHECK_ALIGNED (static_struct2_alignas);
  CHECK_ALIGNED (static_struct2_Alignas);
  CHECK_ALIGNED (static_struct3_alignas);
  CHECK_ALIGNED (static_struct3_Alignas);
  CHECK_ALIGNED (static_struct4_alignas);
  CHECK_ALIGNED (static_struct4_Alignas);
  return 0;
#endif
}