Debian Codegraph @

gcc/testsuite/gcc.dg/attr-aligned.c

raw
1 /* PR c/87795 - Excessive alignment permitted for functions and labels
2 { dg-do compile }
3 { dg-options "-Wno-pedantic" } */
4
5 /* The maximum alignment GCC can handle. */
6 #define ALIGN_MAX_HARD 0x10000000
7
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 __APPLE__
16 # if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1070
17 # define ALIGN_MAX_STATIC 0x8000
18 # else
19 # define ALIGN_MAX_STATIC ALIGN_MAX_HARD
20 # endif
21 #elif __CRIS__
22 /* __BIGGEST_ALIGNMENT__ doesn't cover functions (16 bits for CRIS). */
23 # define ALIGN_MAX_STATIC 512
24 # define ALIGN_TOO_BIG_OFILE (ALIGN_MAX_HARD << 1)
25 #elif pdp11
26 # define ALIGN_MAX_STATIC 2
27 /* Work around a pdp11 ICE (see PR target/87821). */
28 # define ALIGN_MAX_AUTO (ALIGN_MAX_HARD >> 14)
29 #elif __WIN32__ || __CYGWIN__
30 # define ALIGN_MAX_STATIC 8192
31 # define ALIGN_MAX_AUTO 8192
32 #elif __powerpc64__ || __x86_64__
33 /* Is this processor- or operating-system specific? */
34 # define ALIGN_MAX_STATIC ALIGN_MAX_HARD
35 #else
36 /* Guaranteed to be accepted regardless of the target for objects.
37 This might not be true for alignment of functions though, so
38 may need to be set to a target-specific value above. */
39 # define ALIGN_MAX_STATIC __BIGGEST_ALIGNMENT__
40 /* Guaranteed to be rejected regardless of the target. */
41 # define ALIGN_TOO_BIG_OFILE (ALIGN_MAX_HARD << 1)
42 #endif
43
44 /* Maximum alignment for auto objects that's expected to be accepted. */
45 #ifndef ALIGN_MAX_AUTO
46 # define ALIGN_MAX_AUTO ALIGN_MAX_HARD
47 #endif
48
49 #ifndef ALIGN_TOO_BIG_OFILE
50 # define ALIGN_TOO_BIG_OFILE (ALIGN_MAX_STATIC << 1)
51 #endif
52
53
54 #define ALIGN(N) __attribute__ ((aligned (N)))
55
56
57 /* Verify that types can be defined maximally overaligned using
58 attribute aligned. */
59 typedef ALIGN (ALIGN_MAX_HARD) char CharAlignedMaxHard;
60 typedef ALIGN (ALIGN_MAX_AUTO) char CharAlignedMaxAuto;
61 typedef ALIGN (ALIGN_MAX_STATIC) char CharAlignedMaxStatic;
62
63 #if ALIGN_TOO_BIG_OFILE < ALIGN_MAX_HARD
64 /* Also verify that an alignment greater than MAX_OFILE_ALIGNMENT
65 is accepted unless the constant is as large as GCC's maximum
66 supported alignment in any context. */
67 typedef ALIGN (ALIGN_TOO_BIG_OFILE) char CharAlignedTooBig;
68 #endif
69
70 CharAlignedMaxStatic t_max;
71
72 /* Verify that globals can be defined maximally overaligned using
73 attribute aligned. */
74 ALIGN (ALIGN_MAX_STATIC) static const char aligned_sc_max = 0;
75 ALIGN (ALIGN_MAX_STATIC) const char aligned_c_max = aligned_sc_max;
76 ALIGN (ALIGN_MAX_STATIC) char aligned_v_max;
77 ALIGN (ALIGN_MAX_STATIC) void aligned_f_max (void);
78
79 _Static_assert (__alignof__ (aligned_sc_max) == ALIGN_MAX_STATIC);
80 _Static_assert (__alignof__ (aligned_c_max) == ALIGN_MAX_STATIC);
81 _Static_assert (__alignof__ (aligned_v_max) == ALIGN_MAX_STATIC);
82 _Static_assert (__alignof__ (aligned_f_max) == ALIGN_MAX_STATIC);
83
84
85 /* Verify that globals can be defined maximally overaligned using
86 _Alignas. */
87 _Alignas (ALIGN_MAX_STATIC) static const char alignas_sc_max = 0;
88 _Alignas (ALIGN_MAX_STATIC) const char alignas_c_max = alignas_sc_max;
89 _Alignas (ALIGN_MAX_STATIC) char alignas_v_max;
90
91 _Static_assert (__alignof__ (alignas_sc_max) == ALIGN_MAX_STATIC);
92 _Static_assert (__alignof__ (alignas_c_max) == ALIGN_MAX_STATIC);
93 _Static_assert (__alignof__ (alignas_v_max) == ALIGN_MAX_STATIC);
94
95
96 /* Verify that auto and static local variables can be defined maximally
97 overaligned. */
98
99 int accept_local_attribute_aligned (void)
100 {
101 #if ALIGN_TOO_BIG_OFILE < ALIGN_MAX_HARD
102 /* Same as above. */
103 typedef ALIGN (ALIGN_TOO_BIG_OFILE) char LocalCharAlignedTooBig;
104 LocalCharAlignedTooBig aligned_lt_too_big = 0;
105 (void)&aligned_lt_too_big;
106 #endif
107
108 static CharAlignedMaxStatic aligned_st_max;
109 _Static_assert (_Alignof (aligned_st_max) == ALIGN_MAX_STATIC);
110
111 CharAlignedMaxAuto aligned_t_max;
112 _Static_assert (_Alignof (aligned_t_max) == ALIGN_MAX_AUTO);
113
114 ALIGN (ALIGN_MAX_STATIC) char aligned_s_max;
115 _Static_assert (_Alignof (aligned_s_max) == ALIGN_MAX_STATIC);
116
117 ALIGN (ALIGN_MAX_AUTO) char aligned_l_max;
118 _Static_assert (_Alignof (aligned_l_max) == ALIGN_MAX_AUTO);
119
120 return aligned_st_max++ + aligned_t_max++ + aligned_s_max++ + aligned_l_max++;
121 }
122
123
124 int accept_local_alignas (void)
125 {
126 _Alignas (ALIGN_MAX_STATIC) char alignas_s_max;
127 _Static_assert (_Alignof (alignas_s_max) == ALIGN_MAX_STATIC);
128
129 _Alignas (ALIGN_MAX_AUTO) char alignas_l_max;
130 _Static_assert (_Alignof (alignas_l_max) == ALIGN_MAX_AUTO);
131
132 return alignas_s_max++ + alignas_l_max++;
133 }
134
135
136 /* Verify that auto and static local variables are subject to the object
137 file alignment limit. The "object file" part may not be mentioned if
138 the object file maximum is the same as GCC's internal maximum. */
139
140 int reject_local_align (void)
141 {
142 /* Ironically, the errors below are on different lines for each
143 of the two declarations if the aligned attribute is on a line
144 of its own. */
145 ALIGN (ALIGN_TOO_BIG_OFILE) static char aligned_sl_max; /* { dg-error "requested alignment .\[0-9\]+. exceeds\( object file\)* maximum \[0-9\]+" } */
146
147 _Alignas (ALIGN_TOO_BIG_OFILE) static char alignas_sl_max; /* { dg-error "alignment" } */
148
149 return aligned_sl_max++ + alignas_sl_max++;
150 }
151
152
153 /* Verify that global variables are subject to the object file
154 alignment limit. */
155
156 ALIGN (ALIGN_TOO_BIG_OFILE) char a_max_x_2; /* { dg-error "requested alignment .\[0-9\]+. exceeds\( object file\)* maximum \[0-9\]+" } */
157
158 _Alignas (ALIGN_TOO_BIG_OFILE) char a_max_x_2; /* { dg-error "alignment" } */
159
160 ALIGN (ALIGN_TOO_BIG_OFILE) void f_max_x_2 (void); /* { dg-error "requested alignment .\[0-9\]+. exceeds\( object file\)* maximum \[0-9\]+" } */