Daily bump.
[official-gcc.git] / gcc / testsuite / gcc.dg / attr-aligned.c
blobec545639f79d405fc49762aa5e2bc4bfe85f53ea
1 /* PR c/87795 - Excessive alignment permitted for functions and labels
2 { dg-do compile }
3 { dg-options "-Wno-pedantic" } */
5 /* The maximum alignment GCC can handle. */
6 #define ALIGN_MAX_HARD 0x10000000
8 /* Hardcode a few known values for testing the tight bounds. */
9 #if __hpux__ && __hppa__ && __LP64__
10 /* Maximum alignment for functions and objects with static storage
11 duration that's expected to be accepted. */
12 # define ALIGN_MAX_STATIC 0x1000
13 /* Excessive alignment for functions and objects with static storage
14 duration that's expected to trigger an error. */
15 #elif __MACH__
16 # define ALIGN_MAX_STATIC 0x8000
17 #elif pdp11
18 # define ALIGN_MAX_STATIC 2
19 /* Work around a pdp11 ICE (see PR target/87821). */
20 # define ALIGN_MAX_AUTO (ALIGN_MAX_HARD >> 14)
21 #elif __powerpc64__ || __x86_64__
22 /* Is this processor- or operating-system specific? */
23 # define ALIGN_MAX_STATIC ALIGN_MAX_HARD
24 #else
25 /* Guaranteed to be accepted regardless of the target. */
26 # define ALIGN_MAX_STATIC __BIGGEST_ALIGNMENT__
27 /* Guaranteed to be rejected regardless of the target. */
28 # define ALIGN_TOO_BIG_OFILE (ALIGN_MAX_HARD << 1)
29 #endif
31 /* Maximum alignment for auto objects that's expected to be accepted. */
32 #ifndef ALIGN_MAX_AUTO
33 # define ALIGN_MAX_AUTO ALIGN_MAX_HARD
34 #endif
36 #ifndef ALIGN_TOO_BIG_OFILE
37 # define ALIGN_TOO_BIG_OFILE (ALIGN_MAX_STATIC << 1)
38 #endif
41 #define ALIGN(N) __attribute__ ((aligned (N)))
44 /* Verify that types can be defined maximally overaligned using
45 attribute aligned. */
46 typedef ALIGN (ALIGN_MAX_HARD) char CharAlignedMaxHard;
47 typedef ALIGN (ALIGN_MAX_AUTO) char CharAlignedMaxAuto;
48 typedef ALIGN (ALIGN_MAX_STATIC) char CharAlignedMaxStatic;
50 #if ALIGN_TOO_BIG_OFILE < ALIGN_MAX_HARD
51 /* Also verify that an alignment greater than MAX_OFILE_ALIGNMENT
52 is accepted unless the constant is as large as GCC's maximum
53 supported alignment in any context. */
54 typedef ALIGN (ALIGN_TOO_BIG_OFILE) char CharAlignedTooBig;
55 #endif
57 CharAlignedMaxStatic t_max;
59 /* Verify that globals can be defined maximally overaligned using
60 attribute aligned. */
61 ALIGN (ALIGN_MAX_STATIC) static const char aligned_sc_max = 0;
62 ALIGN (ALIGN_MAX_STATIC) const char aligned_c_max = aligned_sc_max;
63 ALIGN (ALIGN_MAX_STATIC) char aligned_v_max;
64 ALIGN (ALIGN_MAX_STATIC) void aligned_f_max (void);
66 _Static_assert (__alignof__ (aligned_sc_max) == ALIGN_MAX_STATIC);
67 _Static_assert (__alignof__ (aligned_c_max) == ALIGN_MAX_STATIC);
68 _Static_assert (__alignof__ (aligned_v_max) == ALIGN_MAX_STATIC);
69 _Static_assert (__alignof__ (aligned_f_max) == ALIGN_MAX_STATIC);
72 /* Verify that globals can be defined maximally overaligned using
73 _Alignas. */
74 _Alignas (ALIGN_MAX_STATIC) static const char alignas_sc_max = 0;
75 _Alignas (ALIGN_MAX_STATIC) const char alignas_c_max = alignas_sc_max;
76 _Alignas (ALIGN_MAX_STATIC) char alignas_v_max;
78 _Static_assert (__alignof__ (alignas_sc_max) == ALIGN_MAX_STATIC);
79 _Static_assert (__alignof__ (alignas_c_max) == ALIGN_MAX_STATIC);
80 _Static_assert (__alignof__ (alignas_v_max) == ALIGN_MAX_STATIC);
83 /* Verify that auto and static local variables can be defined maximally
84 overaligned. */
86 int accept_local_attribute_aligned (void)
88 #if ALIGN_TOO_BIG_OFILE < ALIGN_MAX_HARD
89 /* Same as above. */
90 typedef ALIGN (ALIGN_TOO_BIG_OFILE) char LocalCharAlignedTooBig;
91 LocalCharAlignedTooBig aligned_lt_too_big = 0;
92 (void)&aligned_lt_too_big;
93 #endif
95 static CharAlignedMaxStatic aligned_st_max;
96 _Static_assert (_Alignof (aligned_st_max) == ALIGN_MAX_STATIC);
98 CharAlignedMaxAuto aligned_t_max;
99 _Static_assert (_Alignof (aligned_t_max) == ALIGN_MAX_AUTO);
101 ALIGN (ALIGN_MAX_STATIC) char aligned_s_max;
102 _Static_assert (_Alignof (aligned_s_max) == ALIGN_MAX_STATIC);
104 ALIGN (ALIGN_MAX_AUTO) char aligned_l_max;
105 _Static_assert (_Alignof (aligned_l_max) == ALIGN_MAX_AUTO);
107 return aligned_st_max++ + aligned_t_max++ + aligned_s_max++ + aligned_l_max++;
111 int accept_local_alignas (void)
113 _Alignas (ALIGN_MAX_STATIC) char alignas_s_max;
114 _Static_assert (_Alignof (alignas_s_max) == ALIGN_MAX_STATIC);
116 _Alignas (ALIGN_MAX_AUTO) char alignas_l_max;
117 _Static_assert (_Alignof (alignas_l_max) == ALIGN_MAX_AUTO);
119 return alignas_s_max++ + alignas_l_max++;
123 /* Verify that auto and static local variables are subject to the object
124 file alignment limit. The "object file" part may not be mentioned if
125 the object file maximum is the same as GCC's internal maximum. */
127 int reject_local_align (void)
129 /* Ironically, the errors below are on different lines for each
130 of the two declarations if the aligned attribute is on a line
131 of its own. */
132 ALIGN (ALIGN_TOO_BIG_OFILE) static char aligned_sl_max; /* { dg-error "requested alignment .\[0-9\]+. exceeds\( object file\)* maximum \[0-9\]+" } */
134 _Alignas (ALIGN_TOO_BIG_OFILE) static char alignas_sl_max; /* { dg-error "alignment" } */
136 return aligned_sl_max++ + alignas_sl_max++;
140 /* Verify that global variables are subject to the object file
141 alignment limit. */
143 ALIGN (ALIGN_TOO_BIG_OFILE) char a_max_x_2; /* { dg-error "requested alignment .\[0-9\]+. exceeds\( object file\)* maximum \[0-9\]+" } */
145 _Alignas (ALIGN_TOO_BIG_OFILE) char a_max_x_2; /* { dg-error "alignment" } */
147 ALIGN (ALIGN_TOO_BIG_OFILE) void f_max_x_2 (void); /* { dg-error "requested alignment .\[0-9\]+. exceeds\( object file\)* maximum \[0-9\]+" } */