]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/c-family/c-attribs.c
186df05eec58e5c6d81d0a8614b67ceb927582da
[thirdparty/gcc.git] / gcc / c-family / c-attribs.c
1 /* C-family attributes handling.
2 Copyright (C) 1992-2017 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "target.h"
24 #include "function.h"
25 #include "tree.h"
26 #include "memmodel.h"
27 #include "c-common.h"
28 #include "gimple-expr.h"
29 #include "tm_p.h"
30 #include "stringpool.h"
31 #include "cgraph.h"
32 #include "diagnostic.h"
33 #include "intl.h"
34 #include "stor-layout.h"
35 #include "calls.h"
36 #include "attribs.h"
37 #include "varasm.h"
38 #include "trans-mem.h"
39 #include "c-objc.h"
40 #include "common/common-target.h"
41 #include "langhooks.h"
42 #include "tree-inline.h"
43 #include "toplev.h"
44 #include "tree-iterator.h"
45 #include "opts.h"
46 #include "gimplify.h"
47
48 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
49 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
50 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
51 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
52 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
53 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
54 static tree handle_no_sanitize_attribute (tree *, tree, tree, int, bool *);
55 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
56 int, bool *);
57 static tree handle_no_sanitize_thread_attribute (tree *, tree, tree,
58 int, bool *);
59 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
60 int, bool *);
61 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
62 bool *);
63 static tree handle_asan_odr_indicator_attribute (tree *, tree, tree, int,
64 bool *);
65 static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
66 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
67 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
68 static tree handle_nocf_check_attribute (tree *, tree, tree, int, bool *);
69 static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
70 static tree handle_noipa_attribute (tree *, tree, tree, int, bool *);
71 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
72 static tree handle_always_inline_attribute (tree *, tree, tree, int,
73 bool *);
74 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
75 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
76 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
77 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
78 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
79 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
80 bool *);
81 static tree handle_no_reorder_attribute (tree *, tree, tree, int,
82 bool *);
83 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
84 static tree handle_transparent_union_attribute (tree *, tree, tree,
85 int, bool *);
86 static tree handle_scalar_storage_order_attribute (tree *, tree, tree,
87 int, bool *);
88 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
89 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
90 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
91 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
92 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
93 static tree handle_warn_if_not_aligned_attribute (tree *, tree, tree,
94 int, bool *);
95 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
96 static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
97 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
98 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
99 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
100 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
101 static tree handle_visibility_attribute (tree *, tree, tree, int,
102 bool *);
103 static tree handle_tls_model_attribute (tree *, tree, tree, int,
104 bool *);
105 static tree handle_no_instrument_function_attribute (tree *, tree,
106 tree, int, bool *);
107 static tree handle_no_profile_instrument_function_attribute (tree *, tree,
108 tree, int, bool *);
109 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
110 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
111 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
112 bool *);
113 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
114 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
115 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
116 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
117 static tree handle_deprecated_attribute (tree *, tree, tree, int,
118 bool *);
119 static tree handle_vector_size_attribute (tree *, tree, tree, int,
120 bool *);
121 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
122 static tree handle_nonstring_attribute (tree *, tree, tree, int, bool *);
123 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
124 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
125 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
126 bool *);
127 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
128 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
129 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
130 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
131 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
132 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
133 static tree handle_target_clones_attribute (tree *, tree, tree, int, bool *);
134 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
135 static tree ignore_attribute (tree *, tree, tree, int, bool *);
136 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
137 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
138 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
139 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
140 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
141 bool *);
142 static tree handle_simd_attribute (tree *, tree, tree, int, bool *);
143 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
144 bool *);
145 static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
146 static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
147 static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
148 static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
149 static tree handle_fallthrough_attribute (tree *, tree, tree, int, bool *);
150 static tree handle_patchable_function_entry_attribute (tree *, tree, tree,
151 int, bool *);
152
153 /* Helper to define attribute exclusions. */
154 #define ATTR_EXCL(name, function, type, variable) \
155 { name, function, type, variable }
156
157 /* Define attributes that are mutually exclusive with one another. */
158 static const struct attribute_spec::exclusions attr_aligned_exclusions[] =
159 {
160 /* Attribute name exclusion applies to:
161 function, type, variable */
162 ATTR_EXCL ("aligned", true, false, false),
163 ATTR_EXCL ("packed", true, false, false),
164 ATTR_EXCL (NULL, false, false, false)
165 };
166
167 static const struct attribute_spec::exclusions attr_cold_hot_exclusions[] =
168 {
169 ATTR_EXCL ("cold", true, true, true),
170 ATTR_EXCL ("hot", true, true, true),
171 ATTR_EXCL (NULL, false, false, false)
172 };
173
174 static const struct attribute_spec::exclusions attr_common_exclusions[] =
175 {
176 ATTR_EXCL ("common", true, true, true),
177 ATTR_EXCL ("nocommon", true, true, true),
178 ATTR_EXCL (NULL, false, false, false),
179 };
180
181 static const struct attribute_spec::exclusions attr_inline_exclusions[] =
182 {
183 ATTR_EXCL ("noinline", true, true, true),
184 ATTR_EXCL (NULL, false, false, false),
185 };
186
187 static const struct attribute_spec::exclusions attr_noinline_exclusions[] =
188 {
189 ATTR_EXCL ("always_inline", true, true, true),
190 ATTR_EXCL ("gnu_inline", true, true, true),
191 ATTR_EXCL (NULL, false, false, false),
192 };
193
194 static const struct attribute_spec::exclusions attr_noreturn_exclusions[] =
195 {
196 ATTR_EXCL ("alloc_align", true, true, true),
197 ATTR_EXCL ("alloc_size", true, true, true),
198 ATTR_EXCL ("const", true, true, true),
199 ATTR_EXCL ("malloc", true, true, true),
200 ATTR_EXCL ("pure", true, true, true),
201 ATTR_EXCL ("returns_twice", true, true, true),
202 ATTR_EXCL ("warn_unused_result", true, true, true),
203 ATTR_EXCL (NULL, false, false, false),
204 };
205
206 static const struct attribute_spec::exclusions
207 attr_warn_unused_result_exclusions[] =
208 {
209 ATTR_EXCL ("noreturn", true, true, true),
210 ATTR_EXCL ("warn_unused_result", true, true, true),
211 ATTR_EXCL (NULL, false, false, false),
212 };
213
214 static const struct attribute_spec::exclusions attr_returns_twice_exclusions[] =
215 {
216 ATTR_EXCL ("noreturn", true, true, true),
217 ATTR_EXCL (NULL, false, false, false),
218 };
219
220 /* Exclusions that apply to attribute alloc_align, alloc_size, and malloc. */
221 static const struct attribute_spec::exclusions attr_alloc_exclusions[] =
222 {
223 ATTR_EXCL ("const", true, true, true),
224 ATTR_EXCL ("noreturn", true, true, true),
225 ATTR_EXCL ("pure", true, true, true),
226 ATTR_EXCL (NULL, false, false, false),
227 };
228
229 static const struct attribute_spec::exclusions attr_const_pure_exclusions[] =
230 {
231 ATTR_EXCL ("const", true, true, true),
232 ATTR_EXCL ("alloc_align", true, true, true),
233 ATTR_EXCL ("alloc_size", true, true, true),
234 ATTR_EXCL ("malloc", true, true, true),
235 ATTR_EXCL ("noreturn", true, true, true),
236 ATTR_EXCL ("pure", true, true, true),
237 ATTR_EXCL (NULL, false, false, false)
238 };
239
240 /* Table of machine-independent attributes common to all C-like languages.
241
242 All attributes referencing arguments should be additionally processed
243 in chkp_copy_function_type_adding_bounds for correct instrumentation
244 by Pointer Bounds Checker.
245 Current list of processed common attributes: nonnull. */
246 const struct attribute_spec c_common_attribute_table[] =
247 {
248 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
249 affects_type_identity } */
250 { "packed", 0, 0, false, false, false,
251 handle_packed_attribute, false,
252 attr_aligned_exclusions },
253 { "nocommon", 0, 0, true, false, false,
254 handle_nocommon_attribute, false,
255 attr_common_exclusions },
256 { "common", 0, 0, true, false, false,
257 handle_common_attribute, false,
258 attr_common_exclusions },
259 /* FIXME: logically, noreturn attributes should be listed as
260 "false, true, true" and apply to function types. But implementing this
261 would require all the places in the compiler that use TREE_THIS_VOLATILE
262 on a decl to identify non-returning functions to be located and fixed
263 to check the function type instead. */
264 { "noreturn", 0, 0, true, false, false,
265 handle_noreturn_attribute, false,
266 attr_noreturn_exclusions },
267 { "volatile", 0, 0, true, false, false,
268 handle_noreturn_attribute, false, NULL },
269 { "stack_protect", 0, 0, true, false, false,
270 handle_stack_protect_attribute, false, NULL },
271 { "noinline", 0, 0, true, false, false,
272 handle_noinline_attribute, false,
273 attr_noinline_exclusions },
274 { "noclone", 0, 0, true, false, false,
275 handle_noclone_attribute, false, NULL },
276 { "no_icf", 0, 0, true, false, false,
277 handle_noicf_attribute, false, NULL },
278 { "noipa", 0, 0, true, false, false,
279 handle_noipa_attribute, false, NULL },
280 { "leaf", 0, 0, true, false, false,
281 handle_leaf_attribute, false, NULL },
282 { "always_inline", 0, 0, true, false, false,
283 handle_always_inline_attribute, false,
284 attr_inline_exclusions },
285 { "gnu_inline", 0, 0, true, false, false,
286 handle_gnu_inline_attribute, false,
287 attr_inline_exclusions },
288 { "artificial", 0, 0, true, false, false,
289 handle_artificial_attribute, false, NULL },
290 { "flatten", 0, 0, true, false, false,
291 handle_flatten_attribute, false, NULL },
292 { "used", 0, 0, true, false, false,
293 handle_used_attribute, false, NULL },
294 { "unused", 0, 0, false, false, false,
295 handle_unused_attribute, false, NULL },
296 { "externally_visible", 0, 0, true, false, false,
297 handle_externally_visible_attribute, false, NULL },
298 { "no_reorder", 0, 0, true, false, false,
299 handle_no_reorder_attribute, false, NULL },
300 /* The same comments as for noreturn attributes apply to const ones. */
301 { "const", 0, 0, true, false, false,
302 handle_const_attribute, false,
303 attr_const_pure_exclusions },
304 { "scalar_storage_order", 1, 1, false, false, false,
305 handle_scalar_storage_order_attribute, false, NULL },
306 { "transparent_union", 0, 0, false, false, false,
307 handle_transparent_union_attribute, false, NULL },
308 { "constructor", 0, 1, true, false, false,
309 handle_constructor_attribute, false, NULL },
310 { "destructor", 0, 1, true, false, false,
311 handle_destructor_attribute, false, NULL },
312 { "mode", 1, 1, false, true, false,
313 handle_mode_attribute, false, NULL },
314 { "section", 1, 1, true, false, false,
315 handle_section_attribute, false, NULL },
316 { "aligned", 0, 1, false, false, false,
317 handle_aligned_attribute, false,
318 attr_aligned_exclusions },
319 { "warn_if_not_aligned", 0, 1, false, false, false,
320 handle_warn_if_not_aligned_attribute,
321 false, NULL },
322 { "weak", 0, 0, true, false, false,
323 handle_weak_attribute, false, NULL },
324 { "noplt", 0, 0, true, false, false,
325 handle_noplt_attribute, false, NULL },
326 { "ifunc", 1, 1, true, false, false,
327 handle_ifunc_attribute, false, NULL },
328 { "alias", 1, 1, true, false, false,
329 handle_alias_attribute, false, NULL },
330 { "weakref", 0, 1, true, false, false,
331 handle_weakref_attribute, false, NULL },
332 { "no_instrument_function", 0, 0, true, false, false,
333 handle_no_instrument_function_attribute,
334 false, NULL },
335 { "no_profile_instrument_function", 0, 0, true, false, false,
336 handle_no_profile_instrument_function_attribute,
337 false, NULL },
338 { "malloc", 0, 0, true, false, false,
339 handle_malloc_attribute, false,
340 attr_alloc_exclusions },
341 { "returns_twice", 0, 0, true, false, false,
342 handle_returns_twice_attribute, false,
343 attr_returns_twice_exclusions },
344 { "no_stack_limit", 0, 0, true, false, false,
345 handle_no_limit_stack_attribute, false, NULL },
346 { "pure", 0, 0, true, false, false,
347 handle_pure_attribute, false,
348 attr_const_pure_exclusions },
349 { "transaction_callable", 0, 0, false, true, false,
350 handle_tm_attribute, false, NULL },
351 { "transaction_unsafe", 0, 0, false, true, false,
352 handle_tm_attribute, true, NULL },
353 { "transaction_safe", 0, 0, false, true, false,
354 handle_tm_attribute, true, NULL },
355 { "transaction_safe_dynamic", 0, 0, true, false, false,
356 handle_tm_attribute, false, NULL },
357 { "transaction_may_cancel_outer", 0, 0, false, true, false,
358 handle_tm_attribute, false, NULL },
359 /* ??? These two attributes didn't make the transition from the
360 Intel language document to the multi-vendor language document. */
361 { "transaction_pure", 0, 0, false, true, false,
362 handle_tm_attribute, false, NULL },
363 { "transaction_wrap", 1, 1, true, false, false,
364 handle_tm_wrap_attribute, false, NULL },
365 /* For internal use (marking of builtins) only. The name contains space
366 to prevent its usage in source code. */
367 { "no vops", 0, 0, true, false, false,
368 handle_novops_attribute, false, NULL },
369 { "deprecated", 0, 1, false, false, false,
370 handle_deprecated_attribute, false, NULL },
371 { "vector_size", 1, 1, false, true, false,
372 handle_vector_size_attribute, true, NULL },
373 { "visibility", 1, 1, false, false, false,
374 handle_visibility_attribute, false, NULL },
375 { "tls_model", 1, 1, true, false, false,
376 handle_tls_model_attribute, false, NULL },
377 { "nonnull", 0, -1, false, true, true,
378 handle_nonnull_attribute, false, NULL },
379 { "nonstring", 0, 0, true, false, false,
380 handle_nonstring_attribute, false, NULL },
381 { "nothrow", 0, 0, true, false, false,
382 handle_nothrow_attribute, false, NULL },
383 { "may_alias", 0, 0, false, true, false, NULL, false, NULL },
384 { "cleanup", 1, 1, true, false, false,
385 handle_cleanup_attribute, false, NULL },
386 { "warn_unused_result", 0, 0, false, true, true,
387 handle_warn_unused_result_attribute, false,
388 attr_warn_unused_result_exclusions },
389 { "sentinel", 0, 1, false, true, true,
390 handle_sentinel_attribute, false, NULL },
391 /* For internal use (marking of builtins) only. The name contains space
392 to prevent its usage in source code. */
393 { "type generic", 0, 0, false, true, true,
394 handle_type_generic_attribute, false, NULL },
395 { "alloc_size", 1, 2, false, true, true,
396 handle_alloc_size_attribute, false,
397 attr_alloc_exclusions },
398 { "cold", 0, 0, true, false, false,
399 handle_cold_attribute, false,
400 attr_cold_hot_exclusions },
401 { "hot", 0, 0, true, false, false,
402 handle_hot_attribute, false,
403 attr_cold_hot_exclusions },
404 { "no_address_safety_analysis",
405 0, 0, true, false, false,
406 handle_no_address_safety_analysis_attribute,
407 false, NULL },
408 { "no_sanitize", 1, 1, true, false, false,
409 handle_no_sanitize_attribute,
410 false, NULL },
411 { "no_sanitize_address", 0, 0, true, false, false,
412 handle_no_sanitize_address_attribute,
413 false, NULL },
414 { "no_sanitize_thread", 0, 0, true, false, false,
415 handle_no_sanitize_thread_attribute,
416 false, NULL },
417 { "no_sanitize_undefined", 0, 0, true, false, false,
418 handle_no_sanitize_undefined_attribute,
419 false, NULL },
420 { "asan odr indicator", 0, 0, true, false, false,
421 handle_asan_odr_indicator_attribute,
422 false, NULL },
423 { "warning", 1, 1, true, false, false,
424 handle_error_attribute, false, NULL },
425 { "error", 1, 1, true, false, false,
426 handle_error_attribute, false, NULL },
427 { "target", 1, -1, true, false, false,
428 handle_target_attribute, false, NULL },
429 { "target_clones", 1, -1, true, false, false,
430 handle_target_clones_attribute, false, NULL },
431 { "optimize", 1, -1, true, false, false,
432 handle_optimize_attribute, false, NULL },
433 /* For internal use only. The leading '*' both prevents its usage in
434 source code and signals that it may be overridden by machine tables. */
435 { "*tm regparm", 0, 0, false, true, true,
436 ignore_attribute, false, NULL },
437 { "no_split_stack", 0, 0, true, false, false,
438 handle_no_split_stack_attribute, false, NULL },
439 /* For internal use (marking of builtins and runtime functions) only.
440 The name contains space to prevent its usage in source code. */
441 { "fn spec", 1, 1, false, true, true,
442 handle_fnspec_attribute, false, NULL },
443 { "warn_unused", 0, 0, false, false, false,
444 handle_warn_unused_attribute, false, NULL },
445 { "returns_nonnull", 0, 0, false, true, true,
446 handle_returns_nonnull_attribute, false, NULL },
447 { "omp declare simd", 0, -1, true, false, false,
448 handle_omp_declare_simd_attribute, false, NULL },
449 { "cilk simd function", 0, -1, true, false, false,
450 handle_omp_declare_simd_attribute, false, NULL },
451 { "simd", 0, 1, true, false, false,
452 handle_simd_attribute, false, NULL },
453 { "omp declare target", 0, 0, true, false, false,
454 handle_omp_declare_target_attribute, false, NULL },
455 { "omp declare target link", 0, 0, true, false, false,
456 handle_omp_declare_target_attribute, false, NULL },
457 { "alloc_align", 1, 1, false, true, true,
458 handle_alloc_align_attribute, false,
459 attr_alloc_exclusions },
460 { "assume_aligned", 1, 2, false, true, true,
461 handle_assume_aligned_attribute, false, NULL },
462 { "designated_init", 0, 0, false, true, false,
463 handle_designated_init_attribute, false, NULL },
464 { "bnd_variable_size", 0, 0, true, false, false,
465 handle_bnd_variable_size_attribute, false, NULL },
466 { "bnd_legacy", 0, 0, true, false, false,
467 handle_bnd_legacy, false, NULL },
468 { "bnd_instrument", 0, 0, true, false, false,
469 handle_bnd_instrument, false, NULL },
470 { "fallthrough", 0, 0, false, false, false,
471 handle_fallthrough_attribute, false, NULL },
472 { "patchable_function_entry", 1, 2, true, false, false,
473 handle_patchable_function_entry_attribute,
474 false, NULL },
475 { "nocf_check", 0, 0, false, true, true,
476 handle_nocf_check_attribute, true, NULL },
477 { NULL, 0, 0, false, false, false, NULL, false, NULL }
478 };
479
480 /* Give the specifications for the format attributes, used by C and all
481 descendants.
482
483 All attributes referencing arguments should be additionally processed
484 in chkp_copy_function_type_adding_bounds for correct instrumentation
485 by Pointer Bounds Checker.
486 Current list of processed format attributes: format, format_arg. */
487 const struct attribute_spec c_common_format_attribute_table[] =
488 {
489 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
490 affects_type_identity } */
491 { "format", 3, 3, false, true, true,
492 handle_format_attribute, false, NULL },
493 { "format_arg", 1, 1, false, true, true,
494 handle_format_arg_attribute, false, NULL },
495 { NULL, 0, 0, false, false, false, NULL, false, NULL }
496 };
497
498 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
499 identifier as an argument, so the front end shouldn't look it up. */
500
501 bool
502 attribute_takes_identifier_p (const_tree attr_id)
503 {
504 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
505 if (spec == NULL)
506 /* Unknown attribute that we'll end up ignoring, return true so we
507 don't complain about an identifier argument. */
508 return true;
509 else if (!strcmp ("mode", spec->name)
510 || !strcmp ("format", spec->name)
511 || !strcmp ("cleanup", spec->name))
512 return true;
513 else
514 return targetm.attribute_takes_identifier_p (attr_id);
515 }
516
517 /* Attribute handlers common to C front ends. */
518
519 /* Handle a "packed" attribute; arguments as in
520 struct attribute_spec.handler. */
521
522 static tree
523 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
524 int flags, bool *no_add_attrs)
525 {
526 if (TYPE_P (*node))
527 {
528 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
529 *node = build_variant_type_copy (*node);
530 TYPE_PACKED (*node) = 1;
531 }
532 else if (TREE_CODE (*node) == FIELD_DECL)
533 {
534 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
535 /* Still pack bitfields. */
536 && ! DECL_C_BIT_FIELD (*node))
537 warning (OPT_Wattributes,
538 "%qE attribute ignored for field of type %qT",
539 name, TREE_TYPE (*node));
540 else
541 DECL_PACKED (*node) = 1;
542 }
543 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
544 used for DECL_REGISTER. It wouldn't mean anything anyway.
545 We can't set DECL_PACKED on the type of a TYPE_DECL, because
546 that changes what the typedef is typing. */
547 else
548 {
549 warning (OPT_Wattributes, "%qE attribute ignored", name);
550 *no_add_attrs = true;
551 }
552
553 return NULL_TREE;
554 }
555
556 /* Handle a "nocommon" attribute; arguments as in
557 struct attribute_spec.handler. */
558
559 static tree
560 handle_nocommon_attribute (tree *node, tree name,
561 tree ARG_UNUSED (args),
562 int ARG_UNUSED (flags), bool *no_add_attrs)
563 {
564 if (VAR_P (*node))
565 DECL_COMMON (*node) = 0;
566 else
567 {
568 warning (OPT_Wattributes, "%qE attribute ignored", name);
569 *no_add_attrs = true;
570 }
571
572 return NULL_TREE;
573 }
574
575 /* Handle a "common" attribute; arguments as in
576 struct attribute_spec.handler. */
577
578 static tree
579 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
580 int ARG_UNUSED (flags), bool *no_add_attrs)
581 {
582 if (VAR_P (*node))
583 DECL_COMMON (*node) = 1;
584 else
585 {
586 warning (OPT_Wattributes, "%qE attribute ignored", name);
587 *no_add_attrs = true;
588 }
589
590 return NULL_TREE;
591 }
592
593 /* Handle a "noreturn" attribute; arguments as in
594 struct attribute_spec.handler. */
595
596 static tree
597 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
598 int ARG_UNUSED (flags), bool *no_add_attrs)
599 {
600 tree type = TREE_TYPE (*node);
601
602 /* See FIXME comment in c_common_attribute_table. */
603 if (TREE_CODE (*node) == FUNCTION_DECL
604 || objc_method_decl (TREE_CODE (*node)))
605 TREE_THIS_VOLATILE (*node) = 1;
606 else if (TREE_CODE (type) == POINTER_TYPE
607 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
608 TREE_TYPE (*node)
609 = (build_qualified_type
610 (build_pointer_type
611 (build_type_variant (TREE_TYPE (type),
612 TYPE_READONLY (TREE_TYPE (type)), 1)),
613 TYPE_QUALS (type)));
614 else
615 {
616 warning (OPT_Wattributes, "%qE attribute ignored", name);
617 *no_add_attrs = true;
618 }
619
620 return NULL_TREE;
621 }
622
623 /* Handle a "hot" and attribute; arguments as in
624 struct attribute_spec.handler. */
625
626 static tree
627 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
628 int ARG_UNUSED (flags), bool *no_add_attrs)
629 {
630 if (TREE_CODE (*node) == FUNCTION_DECL
631 || TREE_CODE (*node) == LABEL_DECL)
632 {
633 /* Attribute hot processing is done later with lookup_attribute. */
634 }
635 else
636 {
637 warning (OPT_Wattributes, "%qE attribute ignored", name);
638 *no_add_attrs = true;
639 }
640
641 return NULL_TREE;
642 }
643
644 /* Handle a "cold" and attribute; arguments as in
645 struct attribute_spec.handler. */
646
647 static tree
648 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
649 int ARG_UNUSED (flags), bool *no_add_attrs)
650 {
651 if (TREE_CODE (*node) == FUNCTION_DECL
652 || TREE_CODE (*node) == LABEL_DECL)
653 {
654 /* Attribute cold processing is done later with lookup_attribute. */
655 }
656 else
657 {
658 warning (OPT_Wattributes, "%qE attribute ignored", name);
659 *no_add_attrs = true;
660 }
661
662 return NULL_TREE;
663 }
664
665 /* Add FLAGS for a function NODE to no_sanitize_flags in DECL_ATTRIBUTES. */
666
667 void
668 add_no_sanitize_value (tree node, unsigned int flags)
669 {
670 tree attr = lookup_attribute ("no_sanitize", DECL_ATTRIBUTES (node));
671 if (attr)
672 {
673 unsigned int old_value = tree_to_uhwi (TREE_VALUE (attr));
674 flags |= old_value;
675
676 if (flags == old_value)
677 return;
678
679 TREE_VALUE (attr) = build_int_cst (unsigned_type_node, flags);
680 }
681 else
682 DECL_ATTRIBUTES (node)
683 = tree_cons (get_identifier ("no_sanitize"),
684 build_int_cst (unsigned_type_node, flags),
685 DECL_ATTRIBUTES (node));
686 }
687
688 /* Handle a "no_sanitize" attribute; arguments as in
689 struct attribute_spec.handler. */
690
691 static tree
692 handle_no_sanitize_attribute (tree *node, tree name, tree args, int,
693 bool *no_add_attrs)
694 {
695 *no_add_attrs = true;
696 tree id = TREE_VALUE (args);
697 if (TREE_CODE (*node) != FUNCTION_DECL)
698 {
699 warning (OPT_Wattributes, "%qE attribute ignored", name);
700 return NULL_TREE;
701 }
702
703 if (TREE_CODE (id) != STRING_CST)
704 {
705 error ("no_sanitize argument not a string");
706 return NULL_TREE;
707 }
708
709 char *string = ASTRDUP (TREE_STRING_POINTER (id));
710 unsigned int flags = parse_no_sanitize_attribute (string);
711
712 add_no_sanitize_value (*node, flags);
713
714 return NULL_TREE;
715 }
716
717 /* Handle a "no_sanitize_address" attribute; arguments as in
718 struct attribute_spec.handler. */
719
720 static tree
721 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
722 bool *no_add_attrs)
723 {
724 *no_add_attrs = true;
725 if (TREE_CODE (*node) != FUNCTION_DECL)
726 warning (OPT_Wattributes, "%qE attribute ignored", name);
727 else
728 add_no_sanitize_value (*node, SANITIZE_ADDRESS);
729
730 return NULL_TREE;
731 }
732
733 /* Handle a "no_sanitize_thread" attribute; arguments as in
734 struct attribute_spec.handler. */
735
736 static tree
737 handle_no_sanitize_thread_attribute (tree *node, tree name, tree, int,
738 bool *no_add_attrs)
739 {
740 *no_add_attrs = true;
741 if (TREE_CODE (*node) != FUNCTION_DECL)
742 warning (OPT_Wattributes, "%qE attribute ignored", name);
743 else
744 add_no_sanitize_value (*node, SANITIZE_THREAD);
745
746 return NULL_TREE;
747 }
748
749
750 /* Handle a "no_address_safety_analysis" attribute; arguments as in
751 struct attribute_spec.handler. */
752
753 static tree
754 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
755 bool *no_add_attrs)
756 {
757 *no_add_attrs = true;
758 if (TREE_CODE (*node) != FUNCTION_DECL)
759 warning (OPT_Wattributes, "%qE attribute ignored", name);
760 else
761 add_no_sanitize_value (*node, SANITIZE_ADDRESS);
762
763 return NULL_TREE;
764 }
765
766 /* Handle a "no_sanitize_undefined" attribute; arguments as in
767 struct attribute_spec.handler. */
768
769 static tree
770 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
771 bool *no_add_attrs)
772 {
773 *no_add_attrs = true;
774 if (TREE_CODE (*node) != FUNCTION_DECL)
775 warning (OPT_Wattributes, "%qE attribute ignored", name);
776 else
777 add_no_sanitize_value (*node,
778 SANITIZE_UNDEFINED | SANITIZE_UNDEFINED_NONDEFAULT);
779
780 return NULL_TREE;
781 }
782
783 /* Handle an "asan odr indicator" attribute; arguments as in
784 struct attribute_spec.handler. */
785
786 static tree
787 handle_asan_odr_indicator_attribute (tree *, tree, tree, int, bool *)
788 {
789 return NULL_TREE;
790 }
791
792 /* Handle a "stack_protect" attribute; arguments as in
793 struct attribute_spec.handler. */
794
795 static tree
796 handle_stack_protect_attribute (tree *node, tree name, tree, int,
797 bool *no_add_attrs)
798 {
799 if (TREE_CODE (*node) != FUNCTION_DECL)
800 {
801 warning (OPT_Wattributes, "%qE attribute ignored", name);
802 *no_add_attrs = true;
803 }
804
805 return NULL_TREE;
806 }
807
808 /* Handle a "noipa" attribute; arguments as in
809 struct attribute_spec.handler. */
810
811 static tree
812 handle_noipa_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
813 {
814 if (TREE_CODE (*node) != FUNCTION_DECL)
815 {
816 warning (OPT_Wattributes, "%qE attribute ignored", name);
817 *no_add_attrs = true;
818 }
819
820 return NULL_TREE;
821 }
822
823 /* Handle a "noinline" attribute; arguments as in
824 struct attribute_spec.handler. */
825
826 static tree
827 handle_noinline_attribute (tree *node, tree name,
828 tree ARG_UNUSED (args),
829 int ARG_UNUSED (flags), bool *no_add_attrs)
830 {
831 if (TREE_CODE (*node) == FUNCTION_DECL)
832 {
833 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
834 {
835 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
836 "with attribute %qs", name, "always_inline");
837 *no_add_attrs = true;
838 }
839 else
840 DECL_UNINLINABLE (*node) = 1;
841 }
842 else
843 {
844 warning (OPT_Wattributes, "%qE attribute ignored", name);
845 *no_add_attrs = true;
846 }
847
848 return NULL_TREE;
849 }
850
851 /* Handle a "noclone" attribute; arguments as in
852 struct attribute_spec.handler. */
853
854 static tree
855 handle_noclone_attribute (tree *node, tree name,
856 tree ARG_UNUSED (args),
857 int ARG_UNUSED (flags), bool *no_add_attrs)
858 {
859 if (TREE_CODE (*node) != FUNCTION_DECL)
860 {
861 warning (OPT_Wattributes, "%qE attribute ignored", name);
862 *no_add_attrs = true;
863 }
864
865 return NULL_TREE;
866 }
867
868 /* Handle a "nocf_check" attribute; arguments as in
869 struct attribute_spec.handler. */
870
871 static tree
872 handle_nocf_check_attribute (tree *node, tree name,
873 tree ARG_UNUSED (args),
874 int ARG_UNUSED (flags), bool *no_add_attrs)
875 {
876 if (TREE_CODE (*node) != FUNCTION_TYPE
877 && TREE_CODE (*node) != METHOD_TYPE)
878 {
879 warning (OPT_Wattributes, "%qE attribute ignored", name);
880 *no_add_attrs = true;
881 }
882 else if (!(flag_cf_protection & CF_BRANCH))
883 {
884 warning (OPT_Wattributes, "%qE attribute ignored. Use "
885 "-fcf-protection option to enable it", name);
886 *no_add_attrs = true;
887 }
888
889 return NULL_TREE;
890 }
891
892 /* Handle a "no_icf" attribute; arguments as in
893 struct attribute_spec.handler. */
894
895 static tree
896 handle_noicf_attribute (tree *node, tree name,
897 tree ARG_UNUSED (args),
898 int ARG_UNUSED (flags), bool *no_add_attrs)
899 {
900 if (TREE_CODE (*node) != FUNCTION_DECL)
901 {
902 warning (OPT_Wattributes, "%qE attribute ignored", name);
903 *no_add_attrs = true;
904 }
905
906 return NULL_TREE;
907 }
908
909
910 /* Handle a "always_inline" attribute; arguments as in
911 struct attribute_spec.handler. */
912
913 static tree
914 handle_always_inline_attribute (tree *node, tree name,
915 tree ARG_UNUSED (args),
916 int ARG_UNUSED (flags),
917 bool *no_add_attrs)
918 {
919 if (TREE_CODE (*node) == FUNCTION_DECL)
920 {
921 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
922 {
923 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
924 "with %qs attribute", name, "noinline");
925 *no_add_attrs = true;
926 }
927 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
928 {
929 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
930 "with %qs attribute", name, "target_clones");
931 *no_add_attrs = true;
932 }
933 else
934 /* Set the attribute and mark it for disregarding inline
935 limits. */
936 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
937 }
938 else
939 {
940 warning (OPT_Wattributes, "%qE attribute ignored", name);
941 *no_add_attrs = true;
942 }
943
944 return NULL_TREE;
945 }
946
947 /* Handle a "gnu_inline" attribute; arguments as in
948 struct attribute_spec.handler. */
949
950 static tree
951 handle_gnu_inline_attribute (tree *node, tree name,
952 tree ARG_UNUSED (args),
953 int ARG_UNUSED (flags),
954 bool *no_add_attrs)
955 {
956 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
957 {
958 /* Do nothing else, just set the attribute. We'll get at
959 it later with lookup_attribute. */
960 }
961 else
962 {
963 warning (OPT_Wattributes, "%qE attribute ignored", name);
964 *no_add_attrs = true;
965 }
966
967 return NULL_TREE;
968 }
969
970 /* Handle a "leaf" attribute; arguments as in
971 struct attribute_spec.handler. */
972
973 static tree
974 handle_leaf_attribute (tree *node, tree name,
975 tree ARG_UNUSED (args),
976 int ARG_UNUSED (flags), bool *no_add_attrs)
977 {
978 if (TREE_CODE (*node) != FUNCTION_DECL)
979 {
980 warning (OPT_Wattributes, "%qE attribute ignored", name);
981 *no_add_attrs = true;
982 }
983 if (!TREE_PUBLIC (*node))
984 {
985 warning (OPT_Wattributes, "%qE attribute has no effect on unit local "
986 "functions", name);
987 *no_add_attrs = true;
988 }
989
990 return NULL_TREE;
991 }
992
993 /* Handle an "artificial" attribute; arguments as in
994 struct attribute_spec.handler. */
995
996 static tree
997 handle_artificial_attribute (tree *node, tree name,
998 tree ARG_UNUSED (args),
999 int ARG_UNUSED (flags),
1000 bool *no_add_attrs)
1001 {
1002 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
1003 {
1004 /* Do nothing else, just set the attribute. We'll get at
1005 it later with lookup_attribute. */
1006 }
1007 else
1008 {
1009 warning (OPT_Wattributes, "%qE attribute ignored", name);
1010 *no_add_attrs = true;
1011 }
1012
1013 return NULL_TREE;
1014 }
1015
1016 /* Handle a "flatten" attribute; arguments as in
1017 struct attribute_spec.handler. */
1018
1019 static tree
1020 handle_flatten_attribute (tree *node, tree name,
1021 tree args ATTRIBUTE_UNUSED,
1022 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
1023 {
1024 if (TREE_CODE (*node) == FUNCTION_DECL)
1025 /* Do nothing else, just set the attribute. We'll get at
1026 it later with lookup_attribute. */
1027 ;
1028 else
1029 {
1030 warning (OPT_Wattributes, "%qE attribute ignored", name);
1031 *no_add_attrs = true;
1032 }
1033
1034 return NULL_TREE;
1035 }
1036
1037 /* Handle a "warning" or "error" attribute; arguments as in
1038 struct attribute_spec.handler. */
1039
1040 static tree
1041 handle_error_attribute (tree *node, tree name, tree args,
1042 int ARG_UNUSED (flags), bool *no_add_attrs)
1043 {
1044 if (TREE_CODE (*node) == FUNCTION_DECL
1045 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
1046 /* Do nothing else, just set the attribute. We'll get at
1047 it later with lookup_attribute. */
1048 ;
1049 else
1050 {
1051 warning (OPT_Wattributes, "%qE attribute ignored", name);
1052 *no_add_attrs = true;
1053 }
1054
1055 return NULL_TREE;
1056 }
1057
1058 /* Handle a "used" attribute; arguments as in
1059 struct attribute_spec.handler. */
1060
1061 static tree
1062 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
1063 int ARG_UNUSED (flags), bool *no_add_attrs)
1064 {
1065 tree node = *pnode;
1066
1067 if (TREE_CODE (node) == FUNCTION_DECL
1068 || (VAR_P (node) && TREE_STATIC (node))
1069 || (TREE_CODE (node) == TYPE_DECL))
1070 {
1071 TREE_USED (node) = 1;
1072 DECL_PRESERVE_P (node) = 1;
1073 if (VAR_P (node))
1074 DECL_READ_P (node) = 1;
1075 }
1076 else
1077 {
1078 warning (OPT_Wattributes, "%qE attribute ignored", name);
1079 *no_add_attrs = true;
1080 }
1081
1082 return NULL_TREE;
1083 }
1084
1085 /* Handle a "unused" attribute; arguments as in
1086 struct attribute_spec.handler. */
1087
1088 tree
1089 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
1090 int flags, bool *no_add_attrs)
1091 {
1092 if (DECL_P (*node))
1093 {
1094 tree decl = *node;
1095
1096 if (TREE_CODE (decl) == PARM_DECL
1097 || VAR_OR_FUNCTION_DECL_P (decl)
1098 || TREE_CODE (decl) == LABEL_DECL
1099 || TREE_CODE (decl) == CONST_DECL
1100 || TREE_CODE (decl) == TYPE_DECL)
1101 {
1102 TREE_USED (decl) = 1;
1103 if (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
1104 DECL_READ_P (decl) = 1;
1105 }
1106 else
1107 {
1108 warning (OPT_Wattributes, "%qE attribute ignored", name);
1109 *no_add_attrs = true;
1110 }
1111 }
1112 else
1113 {
1114 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1115 *node = build_variant_type_copy (*node);
1116 TREE_USED (*node) = 1;
1117 }
1118
1119 return NULL_TREE;
1120 }
1121
1122 /* Handle a "externally_visible" attribute; arguments as in
1123 struct attribute_spec.handler. */
1124
1125 static tree
1126 handle_externally_visible_attribute (tree *pnode, tree name,
1127 tree ARG_UNUSED (args),
1128 int ARG_UNUSED (flags),
1129 bool *no_add_attrs)
1130 {
1131 tree node = *pnode;
1132
1133 if (VAR_OR_FUNCTION_DECL_P (node))
1134 {
1135 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
1136 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
1137 {
1138 warning (OPT_Wattributes,
1139 "%qE attribute have effect only on public objects", name);
1140 *no_add_attrs = true;
1141 }
1142 }
1143 else
1144 {
1145 warning (OPT_Wattributes, "%qE attribute ignored", name);
1146 *no_add_attrs = true;
1147 }
1148
1149 return NULL_TREE;
1150 }
1151
1152 /* Handle the "no_reorder" attribute. Arguments as in
1153 struct attribute_spec.handler. */
1154
1155 static tree
1156 handle_no_reorder_attribute (tree *pnode,
1157 tree name,
1158 tree,
1159 int,
1160 bool *no_add_attrs)
1161 {
1162 tree node = *pnode;
1163
1164 if (!VAR_OR_FUNCTION_DECL_P (node)
1165 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
1166 {
1167 warning (OPT_Wattributes,
1168 "%qE attribute only affects top level objects",
1169 name);
1170 *no_add_attrs = true;
1171 }
1172
1173 return NULL_TREE;
1174 }
1175
1176 /* Handle a "const" attribute; arguments as in
1177 struct attribute_spec.handler. */
1178
1179 static tree
1180 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
1181 int flags, bool *no_add_attrs)
1182 {
1183 tree type = TREE_TYPE (*node);
1184
1185 /* See FIXME comment on noreturn in c_common_attribute_table. */
1186 if (TREE_CODE (*node) == FUNCTION_DECL)
1187 TREE_READONLY (*node) = 1;
1188 else if (TREE_CODE (type) == POINTER_TYPE
1189 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
1190 TREE_TYPE (*node)
1191 = (build_qualified_type
1192 (build_pointer_type
1193 (build_type_variant (TREE_TYPE (type), 1,
1194 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
1195 TYPE_QUALS (type)));
1196 else
1197 {
1198 warning (OPT_Wattributes, "%qE attribute ignored", name);
1199 *no_add_attrs = true;
1200 }
1201
1202 /* void __builtin_unreachable(void) is const. Accept other such
1203 built-ins but warn on user-defined functions that return void. */
1204 if (!(flags & ATTR_FLAG_BUILT_IN)
1205 && TREE_CODE (*node) == FUNCTION_DECL
1206 && VOID_TYPE_P (TREE_TYPE (type)))
1207 warning (OPT_Wattributes, "%qE attribute on function "
1208 "returning %<void%>", name);
1209
1210 return NULL_TREE;
1211 }
1212
1213 /* Handle a "scalar_storage_order" attribute; arguments as in
1214 struct attribute_spec.handler. */
1215
1216 static tree
1217 handle_scalar_storage_order_attribute (tree *node, tree name, tree args,
1218 int flags, bool *no_add_attrs)
1219 {
1220 tree id = TREE_VALUE (args);
1221 tree type;
1222
1223 if (TREE_CODE (*node) == TYPE_DECL
1224 && ! (flags & ATTR_FLAG_CXX11))
1225 node = &TREE_TYPE (*node);
1226 type = *node;
1227
1228 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
1229 {
1230 error ("scalar_storage_order is not supported because endianness "
1231 "is not uniform");
1232 return NULL_TREE;
1233 }
1234
1235 if (RECORD_OR_UNION_TYPE_P (type) && !c_dialect_cxx ())
1236 {
1237 bool reverse = false;
1238
1239 if (TREE_CODE (id) == STRING_CST
1240 && strcmp (TREE_STRING_POINTER (id), "big-endian") == 0)
1241 reverse = !BYTES_BIG_ENDIAN;
1242 else if (TREE_CODE (id) == STRING_CST
1243 && strcmp (TREE_STRING_POINTER (id), "little-endian") == 0)
1244 reverse = BYTES_BIG_ENDIAN;
1245 else
1246 {
1247 error ("scalar_storage_order argument must be one of \"big-endian\""
1248 " or \"little-endian\"");
1249 return NULL_TREE;
1250 }
1251
1252 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1253 {
1254 if (reverse)
1255 /* A type variant isn't good enough, since we don't want a cast
1256 to such a type to be removed as a no-op. */
1257 *node = type = build_duplicate_type (type);
1258 }
1259
1260 TYPE_REVERSE_STORAGE_ORDER (type) = reverse;
1261 return NULL_TREE;
1262 }
1263
1264 warning (OPT_Wattributes, "%qE attribute ignored", name);
1265 *no_add_attrs = true;
1266 return NULL_TREE;
1267 }
1268
1269 /* Handle a "transparent_union" attribute; arguments as in
1270 struct attribute_spec.handler. */
1271
1272 static tree
1273 handle_transparent_union_attribute (tree *node, tree name,
1274 tree ARG_UNUSED (args), int flags,
1275 bool *no_add_attrs)
1276 {
1277 tree type;
1278
1279 *no_add_attrs = true;
1280
1281 if (TREE_CODE (*node) == TYPE_DECL
1282 && ! (flags & ATTR_FLAG_CXX11))
1283 node = &TREE_TYPE (*node);
1284 type = *node;
1285
1286 if (TREE_CODE (type) == UNION_TYPE)
1287 {
1288 /* Make sure that the first field will work for a transparent union.
1289 If the type isn't complete yet, leave the check to the code in
1290 finish_struct. */
1291 if (TYPE_SIZE (type))
1292 {
1293 tree first = first_field (type);
1294 if (first == NULL_TREE
1295 || DECL_ARTIFICIAL (first)
1296 || TYPE_MODE (type) != DECL_MODE (first))
1297 goto ignored;
1298 }
1299
1300 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1301 {
1302 /* If the type isn't complete yet, setting the flag
1303 on a variant wouldn't ever be checked. */
1304 if (!TYPE_SIZE (type))
1305 goto ignored;
1306
1307 /* build_duplicate_type doesn't work for C++. */
1308 if (c_dialect_cxx ())
1309 goto ignored;
1310
1311 /* A type variant isn't good enough, since we don't want a cast
1312 to such a type to be removed as a no-op. */
1313 *node = type = build_duplicate_type (type);
1314 }
1315
1316 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
1317 TYPE_TRANSPARENT_AGGR (t) = 1;
1318 return NULL_TREE;
1319 }
1320
1321 ignored:
1322 warning (OPT_Wattributes, "%qE attribute ignored", name);
1323 return NULL_TREE;
1324 }
1325
1326 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
1327 get the requested priority for a constructor or destructor,
1328 possibly issuing diagnostics for invalid or reserved
1329 priorities. */
1330
1331 static priority_type
1332 get_priority (tree args, bool is_destructor)
1333 {
1334 HOST_WIDE_INT pri;
1335 tree arg;
1336
1337 if (!args)
1338 return DEFAULT_INIT_PRIORITY;
1339
1340 if (!SUPPORTS_INIT_PRIORITY)
1341 {
1342 if (is_destructor)
1343 error ("destructor priorities are not supported");
1344 else
1345 error ("constructor priorities are not supported");
1346 return DEFAULT_INIT_PRIORITY;
1347 }
1348
1349 arg = TREE_VALUE (args);
1350 if (TREE_CODE (arg) == IDENTIFIER_NODE)
1351 goto invalid;
1352 if (arg == error_mark_node)
1353 return DEFAULT_INIT_PRIORITY;
1354 arg = default_conversion (arg);
1355 if (!tree_fits_shwi_p (arg)
1356 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
1357 goto invalid;
1358
1359 pri = tree_to_shwi (arg);
1360 if (pri < 0 || pri > MAX_INIT_PRIORITY)
1361 goto invalid;
1362
1363 if (pri <= MAX_RESERVED_INIT_PRIORITY)
1364 {
1365 if (is_destructor)
1366 warning (0,
1367 "destructor priorities from 0 to %d are reserved "
1368 "for the implementation",
1369 MAX_RESERVED_INIT_PRIORITY);
1370 else
1371 warning (0,
1372 "constructor priorities from 0 to %d are reserved "
1373 "for the implementation",
1374 MAX_RESERVED_INIT_PRIORITY);
1375 }
1376 return pri;
1377
1378 invalid:
1379 if (is_destructor)
1380 error ("destructor priorities must be integers from 0 to %d inclusive",
1381 MAX_INIT_PRIORITY);
1382 else
1383 error ("constructor priorities must be integers from 0 to %d inclusive",
1384 MAX_INIT_PRIORITY);
1385 return DEFAULT_INIT_PRIORITY;
1386 }
1387
1388 /* Handle a "constructor" attribute; arguments as in
1389 struct attribute_spec.handler. */
1390
1391 static tree
1392 handle_constructor_attribute (tree *node, tree name, tree args,
1393 int ARG_UNUSED (flags),
1394 bool *no_add_attrs)
1395 {
1396 tree decl = *node;
1397 tree type = TREE_TYPE (decl);
1398
1399 if (TREE_CODE (decl) == FUNCTION_DECL
1400 && TREE_CODE (type) == FUNCTION_TYPE
1401 && decl_function_context (decl) == 0)
1402 {
1403 priority_type priority;
1404 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1405 priority = get_priority (args, /*is_destructor=*/false);
1406 SET_DECL_INIT_PRIORITY (decl, priority);
1407 TREE_USED (decl) = 1;
1408 }
1409 else
1410 {
1411 warning (OPT_Wattributes, "%qE attribute ignored", name);
1412 *no_add_attrs = true;
1413 }
1414
1415 return NULL_TREE;
1416 }
1417
1418 /* Handle a "destructor" attribute; arguments as in
1419 struct attribute_spec.handler. */
1420
1421 static tree
1422 handle_destructor_attribute (tree *node, tree name, tree args,
1423 int ARG_UNUSED (flags),
1424 bool *no_add_attrs)
1425 {
1426 tree decl = *node;
1427 tree type = TREE_TYPE (decl);
1428
1429 if (TREE_CODE (decl) == FUNCTION_DECL
1430 && TREE_CODE (type) == FUNCTION_TYPE
1431 && decl_function_context (decl) == 0)
1432 {
1433 priority_type priority;
1434 DECL_STATIC_DESTRUCTOR (decl) = 1;
1435 priority = get_priority (args, /*is_destructor=*/true);
1436 SET_DECL_FINI_PRIORITY (decl, priority);
1437 TREE_USED (decl) = 1;
1438 }
1439 else
1440 {
1441 warning (OPT_Wattributes, "%qE attribute ignored", name);
1442 *no_add_attrs = true;
1443 }
1444
1445 return NULL_TREE;
1446 }
1447
1448 /* Nonzero if the mode is a valid vector mode for this architecture.
1449 This returns nonzero even if there is no hardware support for the
1450 vector mode, but we can emulate with narrower modes. */
1451
1452 static bool
1453 vector_mode_valid_p (machine_mode mode)
1454 {
1455 enum mode_class mclass = GET_MODE_CLASS (mode);
1456
1457 /* Doh! What's going on? */
1458 if (mclass != MODE_VECTOR_INT
1459 && mclass != MODE_VECTOR_FLOAT
1460 && mclass != MODE_VECTOR_FRACT
1461 && mclass != MODE_VECTOR_UFRACT
1462 && mclass != MODE_VECTOR_ACCUM
1463 && mclass != MODE_VECTOR_UACCUM)
1464 return false;
1465
1466 /* Hardware support. Woo hoo! */
1467 if (targetm.vector_mode_supported_p (mode))
1468 return true;
1469
1470 /* We should probably return 1 if requesting V4DI and we have no DI,
1471 but we have V2DI, but this is probably very unlikely. */
1472
1473 /* If we have support for the inner mode, we can safely emulate it.
1474 We may not have V2DI, but me can emulate with a pair of DIs. */
1475 return targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
1476 }
1477
1478
1479 /* Handle a "mode" attribute; arguments as in
1480 struct attribute_spec.handler. */
1481
1482 static tree
1483 handle_mode_attribute (tree *node, tree name, tree args,
1484 int ARG_UNUSED (flags), bool *no_add_attrs)
1485 {
1486 tree type = *node;
1487 tree ident = TREE_VALUE (args);
1488
1489 *no_add_attrs = true;
1490
1491 if (TREE_CODE (ident) != IDENTIFIER_NODE)
1492 warning (OPT_Wattributes, "%qE attribute ignored", name);
1493 else
1494 {
1495 int j;
1496 const char *p = IDENTIFIER_POINTER (ident);
1497 int len = strlen (p);
1498 machine_mode mode = VOIDmode;
1499 tree typefm;
1500 bool valid_mode;
1501
1502 if (len > 4 && p[0] == '_' && p[1] == '_'
1503 && p[len - 1] == '_' && p[len - 2] == '_')
1504 {
1505 char *newp = (char *) alloca (len - 1);
1506
1507 strcpy (newp, &p[2]);
1508 newp[len - 4] = '\0';
1509 p = newp;
1510 }
1511
1512 /* Change this type to have a type with the specified mode.
1513 First check for the special modes. */
1514 if (!strcmp (p, "byte"))
1515 mode = byte_mode;
1516 else if (!strcmp (p, "word"))
1517 mode = word_mode;
1518 else if (!strcmp (p, "pointer"))
1519 mode = ptr_mode;
1520 else if (!strcmp (p, "libgcc_cmp_return"))
1521 mode = targetm.libgcc_cmp_return_mode ();
1522 else if (!strcmp (p, "libgcc_shift_count"))
1523 mode = targetm.libgcc_shift_count_mode ();
1524 else if (!strcmp (p, "unwind_word"))
1525 mode = targetm.unwind_word_mode ();
1526 else
1527 for (j = 0; j < NUM_MACHINE_MODES; j++)
1528 if (!strcmp (p, GET_MODE_NAME (j)))
1529 {
1530 mode = (machine_mode) j;
1531 break;
1532 }
1533
1534 if (mode == VOIDmode)
1535 {
1536 error ("unknown machine mode %qE", ident);
1537 return NULL_TREE;
1538 }
1539
1540 valid_mode = false;
1541 switch (GET_MODE_CLASS (mode))
1542 {
1543 case MODE_INT:
1544 case MODE_PARTIAL_INT:
1545 case MODE_FLOAT:
1546 case MODE_DECIMAL_FLOAT:
1547 case MODE_FRACT:
1548 case MODE_UFRACT:
1549 case MODE_ACCUM:
1550 case MODE_UACCUM:
1551 valid_mode
1552 = targetm.scalar_mode_supported_p (as_a <scalar_mode> (mode));
1553 break;
1554
1555 case MODE_COMPLEX_INT:
1556 case MODE_COMPLEX_FLOAT:
1557 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
1558 break;
1559
1560 case MODE_VECTOR_INT:
1561 case MODE_VECTOR_FLOAT:
1562 case MODE_VECTOR_FRACT:
1563 case MODE_VECTOR_UFRACT:
1564 case MODE_VECTOR_ACCUM:
1565 case MODE_VECTOR_UACCUM:
1566 warning (OPT_Wattributes, "specifying vector types with "
1567 "__attribute__ ((mode)) is deprecated");
1568 warning (OPT_Wattributes,
1569 "use __attribute__ ((vector_size)) instead");
1570 valid_mode = vector_mode_valid_p (mode);
1571 break;
1572
1573 default:
1574 break;
1575 }
1576 if (!valid_mode)
1577 {
1578 error ("unable to emulate %qs", p);
1579 return NULL_TREE;
1580 }
1581
1582 if (POINTER_TYPE_P (type))
1583 {
1584 scalar_int_mode addr_mode;
1585 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
1586 tree (*fn)(tree, machine_mode, bool);
1587
1588 if (!is_a <scalar_int_mode> (mode, &addr_mode)
1589 || !targetm.addr_space.valid_pointer_mode (addr_mode, as))
1590 {
1591 error ("invalid pointer mode %qs", p);
1592 return NULL_TREE;
1593 }
1594
1595 if (TREE_CODE (type) == POINTER_TYPE)
1596 fn = build_pointer_type_for_mode;
1597 else
1598 fn = build_reference_type_for_mode;
1599 typefm = fn (TREE_TYPE (type), addr_mode, false);
1600 }
1601 else
1602 {
1603 /* For fixed-point modes, we need to test if the signness of type
1604 and the machine mode are consistent. */
1605 if (ALL_FIXED_POINT_MODE_P (mode)
1606 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
1607 {
1608 error ("signedness of type and machine mode %qs don%'t match", p);
1609 return NULL_TREE;
1610 }
1611 /* For fixed-point modes, we need to pass saturating info. */
1612 typefm = lang_hooks.types.type_for_mode (mode,
1613 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
1614 : TYPE_UNSIGNED (type));
1615 }
1616
1617 if (typefm == NULL_TREE)
1618 {
1619 error ("no data type for mode %qs", p);
1620 return NULL_TREE;
1621 }
1622 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1623 {
1624 /* For enumeral types, copy the precision from the integer
1625 type returned above. If not an INTEGER_TYPE, we can't use
1626 this mode for this type. */
1627 if (TREE_CODE (typefm) != INTEGER_TYPE)
1628 {
1629 error ("cannot use mode %qs for enumeral types", p);
1630 return NULL_TREE;
1631 }
1632
1633 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
1634 {
1635 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
1636 typefm = type;
1637 }
1638 else
1639 {
1640 /* We cannot build a type variant, as there's code that assumes
1641 that TYPE_MAIN_VARIANT has the same mode. This includes the
1642 debug generators. Instead, create a subrange type. This
1643 results in all of the enumeral values being emitted only once
1644 in the original, and the subtype gets them by reference. */
1645 if (TYPE_UNSIGNED (type))
1646 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
1647 else
1648 typefm = make_signed_type (TYPE_PRECISION (typefm));
1649 TREE_TYPE (typefm) = type;
1650 }
1651 }
1652 else if (VECTOR_MODE_P (mode)
1653 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
1654 : TREE_CODE (type) != TREE_CODE (typefm))
1655 {
1656 error ("mode %qs applied to inappropriate type", p);
1657 return NULL_TREE;
1658 }
1659
1660 *node = build_qualified_type (typefm, TYPE_QUALS (type));
1661 }
1662
1663 return NULL_TREE;
1664 }
1665
1666 /* Handle a "section" attribute; arguments as in
1667 struct attribute_spec.handler. */
1668
1669 static tree
1670 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
1671 int ARG_UNUSED (flags), bool *no_add_attrs)
1672 {
1673 tree decl = *node;
1674
1675 if (!targetm_common.have_named_sections)
1676 {
1677 error_at (DECL_SOURCE_LOCATION (*node),
1678 "section attributes are not supported for this target");
1679 goto fail;
1680 }
1681
1682 if (!VAR_OR_FUNCTION_DECL_P (decl))
1683 {
1684 error ("section attribute not allowed for %q+D", *node);
1685 goto fail;
1686 }
1687
1688 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
1689 {
1690 error ("section attribute argument not a string constant");
1691 goto fail;
1692 }
1693
1694 if (VAR_P (decl)
1695 && current_function_decl != NULL_TREE
1696 && !TREE_STATIC (decl))
1697 {
1698 error_at (DECL_SOURCE_LOCATION (decl),
1699 "section attribute cannot be specified for local variables");
1700 goto fail;
1701 }
1702
1703 /* The decl may have already been given a section attribute
1704 from a previous declaration. Ensure they match. */
1705 if (DECL_SECTION_NAME (decl) != NULL
1706 && strcmp (DECL_SECTION_NAME (decl),
1707 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
1708 {
1709 error ("section of %q+D conflicts with previous declaration", *node);
1710 goto fail;
1711 }
1712
1713 if (VAR_P (decl)
1714 && !targetm.have_tls && targetm.emutls.tmpl_section
1715 && DECL_THREAD_LOCAL_P (decl))
1716 {
1717 error ("section of %q+D cannot be overridden", *node);
1718 goto fail;
1719 }
1720
1721 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
1722 return NULL_TREE;
1723
1724 fail:
1725 *no_add_attrs = true;
1726 return NULL_TREE;
1727 }
1728
1729 /* If in c++-11, check if the c++-11 alignment constraint with respect
1730 to fundamental alignment (in [dcl.align]) are satisfied. If not in
1731 c++-11 mode, does nothing.
1732
1733 [dcl.align]2/ says:
1734
1735 [* if the constant expression evaluates to a fundamental alignment,
1736 the alignment requirement of the declared entity shall be the
1737 specified fundamental alignment.
1738
1739 * if the constant expression evaluates to an extended alignment
1740 and the implementation supports that alignment in the context
1741 of the declaration, the alignment of the declared entity shall
1742 be that alignment
1743
1744 * if the constant expression evaluates to an extended alignment
1745 and the implementation does not support that alignment in the
1746 context of the declaration, the program is ill-formed]. */
1747
1748 static bool
1749 check_cxx_fundamental_alignment_constraints (tree node,
1750 unsigned align_log,
1751 int flags)
1752 {
1753 bool alignment_too_large_p = false;
1754 unsigned requested_alignment = (1U << align_log) * BITS_PER_UNIT;
1755 unsigned max_align = 0;
1756
1757 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
1758 || (node == NULL_TREE || node == error_mark_node))
1759 return true;
1760
1761 if (cxx_fundamental_alignment_p (requested_alignment))
1762 return true;
1763
1764 if (VAR_P (node))
1765 {
1766 if (TREE_STATIC (node) || DECL_EXTERNAL (node))
1767 /* For file scope variables and static members, the target supports
1768 alignments that are at most MAX_OFILE_ALIGNMENT. */
1769 max_align = MAX_OFILE_ALIGNMENT;
1770 else
1771 /* For stack variables, the target supports at most
1772 MAX_STACK_ALIGNMENT. */
1773 max_align = MAX_STACK_ALIGNMENT;
1774 if (requested_alignment > max_align)
1775 alignment_too_large_p = true;
1776 }
1777 /* Let's be liberal for types and fields; don't limit their alignment any
1778 more than check_user_alignment already did. */
1779
1780 if (alignment_too_large_p)
1781 pedwarn (input_location, OPT_Wattributes,
1782 "requested alignment %d is larger than %d",
1783 requested_alignment / BITS_PER_UNIT, max_align / BITS_PER_UNIT);
1784
1785 return !alignment_too_large_p;
1786 }
1787
1788 /* Common codes shared by handle_warn_if_not_aligned_attribute and
1789 handle_aligned_attribute. */
1790
1791 static tree
1792 common_handle_aligned_attribute (tree *node, tree name, tree args, int flags,
1793 bool *no_add_attrs,
1794 bool warn_if_not_aligned_p)
1795 {
1796 tree decl = NULL_TREE;
1797 tree *type = NULL;
1798 bool is_type = false;
1799 tree align_expr;
1800
1801 /* The last (already pushed) declaration with all validated attributes
1802 merged in or the current about-to-be-pushed one if one hasn't been
1803 yet. */
1804 tree last_decl = node[1] ? node[1] : *node;
1805
1806 if (args)
1807 {
1808 align_expr = TREE_VALUE (args);
1809 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
1810 && TREE_CODE (align_expr) != FUNCTION_DECL)
1811 align_expr = default_conversion (align_expr);
1812 }
1813 else
1814 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
1815
1816 if (DECL_P (*node))
1817 {
1818 decl = *node;
1819 type = &TREE_TYPE (decl);
1820 is_type = TREE_CODE (*node) == TYPE_DECL;
1821 }
1822 else if (TYPE_P (*node))
1823 type = node, is_type = true;
1824
1825 /* Log2 of specified alignment. */
1826 int pow2align = check_user_alignment (align_expr, true);
1827
1828 /* The alignment in bits corresponding to the specified alignment. */
1829 unsigned bitalign = (1U << pow2align) * BITS_PER_UNIT;
1830
1831 /* The alignment of the current declaration and that of the last
1832 pushed declaration, determined on demand below. */
1833 unsigned curalign = 0;
1834 unsigned lastalign = 0;
1835
1836 if (pow2align == -1
1837 || !check_cxx_fundamental_alignment_constraints (*node, pow2align, flags))
1838 *no_add_attrs = true;
1839 else if (is_type)
1840 {
1841 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1842 /* OK, modify the type in place. */;
1843 /* If we have a TYPE_DECL, then copy the type, so that we
1844 don't accidentally modify a builtin type. See pushdecl. */
1845 else if (decl && TREE_TYPE (decl) != error_mark_node
1846 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
1847 {
1848 tree tt = TREE_TYPE (decl);
1849 *type = build_variant_type_copy (*type);
1850 DECL_ORIGINAL_TYPE (decl) = tt;
1851 TYPE_NAME (*type) = decl;
1852 TREE_USED (*type) = TREE_USED (decl);
1853 TREE_TYPE (decl) = *type;
1854 }
1855 else
1856 *type = build_variant_type_copy (*type);
1857
1858 if (warn_if_not_aligned_p)
1859 {
1860 SET_TYPE_WARN_IF_NOT_ALIGN (*type, bitalign);
1861 warn_if_not_aligned_p = false;
1862 }
1863 else
1864 {
1865 SET_TYPE_ALIGN (*type, bitalign);
1866 TYPE_USER_ALIGN (*type) = 1;
1867 }
1868 }
1869 else if (! VAR_OR_FUNCTION_DECL_P (decl)
1870 && TREE_CODE (decl) != FIELD_DECL)
1871 {
1872 error ("alignment may not be specified for %q+D", decl);
1873 *no_add_attrs = true;
1874 }
1875 else if (TREE_CODE (decl) == FUNCTION_DECL
1876 && ((curalign = DECL_ALIGN (decl)) > bitalign
1877 || ((lastalign = DECL_ALIGN (last_decl)) > bitalign)))
1878 {
1879 /* Either a prior attribute on the same declaration or one
1880 on a prior declaration of the same function specifies
1881 stricter alignment than this attribute. */
1882 bool note = lastalign != 0;
1883 if (lastalign)
1884 curalign = lastalign;
1885
1886 curalign /= BITS_PER_UNIT;
1887 bitalign /= BITS_PER_UNIT;
1888
1889 if (DECL_USER_ALIGN (decl) || DECL_USER_ALIGN (last_decl))
1890 warning (OPT_Wattributes,
1891 "ignoring attribute %<%E (%u)%> because it conflicts with "
1892 "attribute %<%E (%u)%>", name, bitalign, name, curalign);
1893 else
1894 error ("alignment for %q+D must be at least %d", decl, curalign);
1895
1896 if (note)
1897 inform (DECL_SOURCE_LOCATION (last_decl), "previous declaration here");
1898
1899 *no_add_attrs = true;
1900 }
1901 else if (DECL_USER_ALIGN (decl)
1902 && DECL_ALIGN (decl) > bitalign)
1903 /* C++-11 [dcl.align/4]:
1904
1905 When multiple alignment-specifiers are specified for an
1906 entity, the alignment requirement shall be set to the
1907 strictest specified alignment.
1908
1909 This formally comes from the c++11 specification but we are
1910 doing it for the GNU attribute syntax as well. */
1911 *no_add_attrs = true;
1912 else if (!warn_if_not_aligned_p
1913 && TREE_CODE (decl) == FUNCTION_DECL
1914 && DECL_ALIGN (decl) > bitalign)
1915 {
1916 /* Don't warn function alignment here if warn_if_not_aligned_p is
1917 true. It will be warned later. */
1918 if (DECL_USER_ALIGN (decl))
1919 error ("alignment for %q+D was previously specified as %d "
1920 "and may not be decreased", decl,
1921 DECL_ALIGN (decl) / BITS_PER_UNIT);
1922 else
1923 error ("alignment for %q+D must be at least %d", decl,
1924 DECL_ALIGN (decl) / BITS_PER_UNIT);
1925 *no_add_attrs = true;
1926 }
1927 else
1928 {
1929 if (warn_if_not_aligned_p)
1930 {
1931 if (TREE_CODE (decl) == FIELD_DECL && !DECL_C_BIT_FIELD (decl))
1932 {
1933 SET_DECL_WARN_IF_NOT_ALIGN (decl, bitalign);
1934 warn_if_not_aligned_p = false;
1935 }
1936 }
1937 else
1938 {
1939 SET_DECL_ALIGN (decl, bitalign);
1940 DECL_USER_ALIGN (decl) = 1;
1941 }
1942 }
1943
1944 if (warn_if_not_aligned_p)
1945 {
1946 error ("%<warn_if_not_aligned%> may not be specified for %q+D",
1947 decl);
1948 *no_add_attrs = true;
1949 }
1950
1951 return NULL_TREE;
1952 }
1953
1954 /* Handle a "aligned" attribute; arguments as in
1955 struct attribute_spec.handler. */
1956
1957 static tree
1958 handle_aligned_attribute (tree *node, tree name, tree args,
1959 int flags, bool *no_add_attrs)
1960 {
1961 return common_handle_aligned_attribute (node, name, args, flags,
1962 no_add_attrs, false);
1963 }
1964
1965 /* Handle a "warn_if_not_aligned" attribute; arguments as in
1966 struct attribute_spec.handler. */
1967
1968 static tree
1969 handle_warn_if_not_aligned_attribute (tree *node, tree name,
1970 tree args, int flags,
1971 bool *no_add_attrs)
1972 {
1973 return common_handle_aligned_attribute (node, name, args, flags,
1974 no_add_attrs, true);
1975 }
1976
1977 /* Handle a "weak" attribute; arguments as in
1978 struct attribute_spec.handler. */
1979
1980 static tree
1981 handle_weak_attribute (tree *node, tree name,
1982 tree ARG_UNUSED (args),
1983 int ARG_UNUSED (flags),
1984 bool * ARG_UNUSED (no_add_attrs))
1985 {
1986 if (TREE_CODE (*node) == FUNCTION_DECL
1987 && DECL_DECLARED_INLINE_P (*node))
1988 {
1989 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
1990 *no_add_attrs = true;
1991 }
1992 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
1993 {
1994 error ("indirect function %q+D cannot be declared weak", *node);
1995 *no_add_attrs = true;
1996 return NULL_TREE;
1997 }
1998 else if (VAR_OR_FUNCTION_DECL_P (*node))
1999 declare_weak (*node);
2000 else
2001 warning (OPT_Wattributes, "%qE attribute ignored", name);
2002
2003 return NULL_TREE;
2004 }
2005
2006 /* Handle a "noplt" attribute; arguments as in
2007 struct attribute_spec.handler. */
2008
2009 static tree
2010 handle_noplt_attribute (tree *node, tree name,
2011 tree ARG_UNUSED (args),
2012 int ARG_UNUSED (flags),
2013 bool * ARG_UNUSED (no_add_attrs))
2014 {
2015 if (TREE_CODE (*node) != FUNCTION_DECL)
2016 {
2017 warning (OPT_Wattributes,
2018 "%qE attribute is only applicable on functions", name);
2019 *no_add_attrs = true;
2020 return NULL_TREE;
2021 }
2022 return NULL_TREE;
2023 }
2024
2025 /* Handle an "alias" or "ifunc" attribute; arguments as in
2026 struct attribute_spec.handler, except that IS_ALIAS tells us
2027 whether this is an alias as opposed to ifunc attribute. */
2028
2029 static tree
2030 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
2031 bool *no_add_attrs)
2032 {
2033 tree decl = *node;
2034
2035 if (TREE_CODE (decl) != FUNCTION_DECL
2036 && (!is_alias || !VAR_P (decl)))
2037 {
2038 warning (OPT_Wattributes, "%qE attribute ignored", name);
2039 *no_add_attrs = true;
2040 }
2041 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
2042 || (TREE_CODE (decl) != FUNCTION_DECL
2043 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
2044 /* A static variable declaration is always a tentative definition,
2045 but the alias is a non-tentative definition which overrides. */
2046 || (TREE_CODE (decl) != FUNCTION_DECL
2047 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
2048 {
2049 error ("%q+D defined both normally and as %qE attribute", decl, name);
2050 *no_add_attrs = true;
2051 return NULL_TREE;
2052 }
2053 else if (!is_alias
2054 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2055 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
2056 {
2057 error ("weak %q+D cannot be defined %qE", decl, name);
2058 *no_add_attrs = true;
2059 return NULL_TREE;
2060 }
2061
2062 /* Note that the very first time we process a nested declaration,
2063 decl_function_context will not be set. Indeed, *would* never
2064 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
2065 we do below. After such frobbery, pushdecl would set the context.
2066 In any case, this is never what we want. */
2067 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
2068 {
2069 tree id;
2070
2071 id = TREE_VALUE (args);
2072 if (TREE_CODE (id) != STRING_CST)
2073 {
2074 error ("attribute %qE argument not a string", name);
2075 *no_add_attrs = true;
2076 return NULL_TREE;
2077 }
2078 id = get_identifier (TREE_STRING_POINTER (id));
2079 /* This counts as a use of the object pointed to. */
2080 TREE_USED (id) = 1;
2081
2082 if (TREE_CODE (decl) == FUNCTION_DECL)
2083 DECL_INITIAL (decl) = error_mark_node;
2084 else
2085 TREE_STATIC (decl) = 1;
2086
2087 if (!is_alias)
2088 {
2089 /* ifuncs are also aliases, so set that attribute too. */
2090 DECL_ATTRIBUTES (decl)
2091 = tree_cons (get_identifier ("alias"), args,
2092 DECL_ATTRIBUTES (decl));
2093 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("ifunc"),
2094 NULL, DECL_ATTRIBUTES (decl));
2095 }
2096 }
2097 else
2098 {
2099 warning (OPT_Wattributes, "%qE attribute ignored", name);
2100 *no_add_attrs = true;
2101 }
2102
2103 if (decl_in_symtab_p (*node))
2104 {
2105 struct symtab_node *n = symtab_node::get (decl);
2106 if (n && n->refuse_visibility_changes)
2107 {
2108 if (is_alias)
2109 error ("%+qD declared alias after being used", decl);
2110 else
2111 error ("%+qD declared ifunc after being used", decl);
2112 }
2113 }
2114
2115
2116 return NULL_TREE;
2117 }
2118
2119 /* Handle an "alias" or "ifunc" attribute; arguments as in
2120 struct attribute_spec.handler. */
2121
2122 static tree
2123 handle_ifunc_attribute (tree *node, tree name, tree args,
2124 int ARG_UNUSED (flags), bool *no_add_attrs)
2125 {
2126 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
2127 }
2128
2129 /* Handle an "alias" or "ifunc" attribute; arguments as in
2130 struct attribute_spec.handler. */
2131
2132 static tree
2133 handle_alias_attribute (tree *node, tree name, tree args,
2134 int ARG_UNUSED (flags), bool *no_add_attrs)
2135 {
2136 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
2137 }
2138
2139 /* Handle a "weakref" attribute; arguments as in struct
2140 attribute_spec.handler. */
2141
2142 static tree
2143 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
2144 int flags, bool *no_add_attrs)
2145 {
2146 tree attr = NULL_TREE;
2147
2148 /* We must ignore the attribute when it is associated with
2149 local-scoped decls, since attribute alias is ignored and many
2150 such symbols do not even have a DECL_WEAK field. */
2151 if (decl_function_context (*node)
2152 || current_function_decl
2153 || !VAR_OR_FUNCTION_DECL_P (*node))
2154 {
2155 warning (OPT_Wattributes, "%qE attribute ignored", name);
2156 *no_add_attrs = true;
2157 return NULL_TREE;
2158 }
2159
2160 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
2161 {
2162 error ("indirect function %q+D cannot be declared weakref", *node);
2163 *no_add_attrs = true;
2164 return NULL_TREE;
2165 }
2166
2167 /* The idea here is that `weakref("name")' mutates into `weakref,
2168 alias("name")', and weakref without arguments, in turn,
2169 implicitly adds weak. */
2170
2171 if (args)
2172 {
2173 attr = tree_cons (get_identifier ("alias"), args, attr);
2174 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
2175
2176 *no_add_attrs = true;
2177
2178 decl_attributes (node, attr, flags);
2179 }
2180 else
2181 {
2182 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
2183 error_at (DECL_SOURCE_LOCATION (*node),
2184 "weakref attribute must appear before alias attribute");
2185
2186 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
2187 and that isn't supported; and because it wants to add it to
2188 the list of weak decls, which isn't helpful. */
2189 DECL_WEAK (*node) = 1;
2190 }
2191
2192 if (decl_in_symtab_p (*node))
2193 {
2194 struct symtab_node *n = symtab_node::get (*node);
2195 if (n && n->refuse_visibility_changes)
2196 error ("%+qD declared weakref after being used", *node);
2197 }
2198
2199 return NULL_TREE;
2200 }
2201
2202 /* Handle an "visibility" attribute; arguments as in
2203 struct attribute_spec.handler. */
2204
2205 static tree
2206 handle_visibility_attribute (tree *node, tree name, tree args,
2207 int ARG_UNUSED (flags),
2208 bool *ARG_UNUSED (no_add_attrs))
2209 {
2210 tree decl = *node;
2211 tree id = TREE_VALUE (args);
2212 enum symbol_visibility vis;
2213
2214 if (TYPE_P (*node))
2215 {
2216 if (TREE_CODE (*node) == ENUMERAL_TYPE)
2217 /* OK */;
2218 else if (!RECORD_OR_UNION_TYPE_P (*node))
2219 {
2220 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
2221 name);
2222 return NULL_TREE;
2223 }
2224 else if (TYPE_FIELDS (*node))
2225 {
2226 error ("%qE attribute ignored because %qT is already defined",
2227 name, *node);
2228 return NULL_TREE;
2229 }
2230 }
2231 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
2232 {
2233 warning (OPT_Wattributes, "%qE attribute ignored", name);
2234 return NULL_TREE;
2235 }
2236
2237 if (TREE_CODE (id) != STRING_CST)
2238 {
2239 error ("visibility argument not a string");
2240 return NULL_TREE;
2241 }
2242
2243 /* If this is a type, set the visibility on the type decl. */
2244 if (TYPE_P (decl))
2245 {
2246 decl = TYPE_NAME (decl);
2247 if (!decl)
2248 return NULL_TREE;
2249 if (TREE_CODE (decl) == IDENTIFIER_NODE)
2250 {
2251 warning (OPT_Wattributes, "%qE attribute ignored on types",
2252 name);
2253 return NULL_TREE;
2254 }
2255 }
2256
2257 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
2258 vis = VISIBILITY_DEFAULT;
2259 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
2260 vis = VISIBILITY_INTERNAL;
2261 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
2262 vis = VISIBILITY_HIDDEN;
2263 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
2264 vis = VISIBILITY_PROTECTED;
2265 else
2266 {
2267 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
2268 vis = VISIBILITY_DEFAULT;
2269 }
2270
2271 if (DECL_VISIBILITY_SPECIFIED (decl)
2272 && vis != DECL_VISIBILITY (decl))
2273 {
2274 tree attributes = (TYPE_P (*node)
2275 ? TYPE_ATTRIBUTES (*node)
2276 : DECL_ATTRIBUTES (decl));
2277 if (lookup_attribute ("visibility", attributes))
2278 error ("%qD redeclared with different visibility", decl);
2279 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
2280 && lookup_attribute ("dllimport", attributes))
2281 error ("%qD was declared %qs which implies default visibility",
2282 decl, "dllimport");
2283 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
2284 && lookup_attribute ("dllexport", attributes))
2285 error ("%qD was declared %qs which implies default visibility",
2286 decl, "dllexport");
2287 }
2288
2289 DECL_VISIBILITY (decl) = vis;
2290 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2291
2292 /* Go ahead and attach the attribute to the node as well. This is needed
2293 so we can determine whether we have VISIBILITY_DEFAULT because the
2294 visibility was not specified, or because it was explicitly overridden
2295 from the containing scope. */
2296
2297 return NULL_TREE;
2298 }
2299
2300 /* Handle an "tls_model" attribute; arguments as in
2301 struct attribute_spec.handler. */
2302
2303 static tree
2304 handle_tls_model_attribute (tree *node, tree name, tree args,
2305 int ARG_UNUSED (flags), bool *no_add_attrs)
2306 {
2307 tree id;
2308 tree decl = *node;
2309 enum tls_model kind;
2310
2311 *no_add_attrs = true;
2312
2313 if (!VAR_P (decl) || !DECL_THREAD_LOCAL_P (decl))
2314 {
2315 warning (OPT_Wattributes, "%qE attribute ignored", name);
2316 return NULL_TREE;
2317 }
2318
2319 kind = DECL_TLS_MODEL (decl);
2320 id = TREE_VALUE (args);
2321 if (TREE_CODE (id) != STRING_CST)
2322 {
2323 error ("tls_model argument not a string");
2324 return NULL_TREE;
2325 }
2326
2327 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
2328 kind = TLS_MODEL_LOCAL_EXEC;
2329 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
2330 kind = TLS_MODEL_INITIAL_EXEC;
2331 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
2332 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
2333 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
2334 kind = TLS_MODEL_GLOBAL_DYNAMIC;
2335 else
2336 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
2337
2338 set_decl_tls_model (decl, kind);
2339 return NULL_TREE;
2340 }
2341
2342 /* Handle a "no_instrument_function" attribute; arguments as in
2343 struct attribute_spec.handler. */
2344
2345 static tree
2346 handle_no_instrument_function_attribute (tree *node, tree name,
2347 tree ARG_UNUSED (args),
2348 int ARG_UNUSED (flags),
2349 bool *no_add_attrs)
2350 {
2351 tree decl = *node;
2352
2353 if (TREE_CODE (decl) != FUNCTION_DECL)
2354 {
2355 error_at (DECL_SOURCE_LOCATION (decl),
2356 "%qE attribute applies only to functions", name);
2357 *no_add_attrs = true;
2358 }
2359 else
2360 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
2361
2362 return NULL_TREE;
2363 }
2364
2365 /* Handle a "no_profile_instrument_function" attribute; arguments as in
2366 struct attribute_spec.handler. */
2367
2368 static tree
2369 handle_no_profile_instrument_function_attribute (tree *node, tree name, tree,
2370 int, bool *no_add_attrs)
2371 {
2372 if (TREE_CODE (*node) != FUNCTION_DECL)
2373 {
2374 warning (OPT_Wattributes, "%qE attribute ignored", name);
2375 *no_add_attrs = true;
2376 }
2377
2378 return NULL_TREE;
2379 }
2380
2381 /* Handle a "malloc" attribute; arguments as in
2382 struct attribute_spec.handler. */
2383
2384 static tree
2385 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
2386 int ARG_UNUSED (flags), bool *no_add_attrs)
2387 {
2388 if (TREE_CODE (*node) == FUNCTION_DECL
2389 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
2390 DECL_IS_MALLOC (*node) = 1;
2391 else
2392 {
2393 warning (OPT_Wattributes, "%qE attribute ignored", name);
2394 *no_add_attrs = true;
2395 }
2396
2397 return NULL_TREE;
2398 }
2399
2400 /* Handle a "alloc_size" attribute; arguments as in
2401 struct attribute_spec.handler. */
2402
2403 static tree
2404 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
2405 int ARG_UNUSED (flags), bool *no_add_attrs)
2406 {
2407 unsigned arg_count = type_num_arguments (*node);
2408 for (; args; args = TREE_CHAIN (args))
2409 {
2410 tree position = TREE_VALUE (args);
2411 if (position && TREE_CODE (position) != IDENTIFIER_NODE
2412 && TREE_CODE (position) != FUNCTION_DECL)
2413 position = default_conversion (position);
2414
2415 if (!tree_fits_uhwi_p (position)
2416 || !arg_count
2417 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
2418 {
2419 warning (OPT_Wattributes,
2420 "alloc_size parameter outside range");
2421 *no_add_attrs = true;
2422 return NULL_TREE;
2423 }
2424 }
2425 return NULL_TREE;
2426 }
2427
2428 /* Handle a "alloc_align" attribute; arguments as in
2429 struct attribute_spec.handler. */
2430
2431 static tree
2432 handle_alloc_align_attribute (tree *node, tree, tree args, int,
2433 bool *no_add_attrs)
2434 {
2435 unsigned arg_count = type_num_arguments (*node);
2436 tree position = TREE_VALUE (args);
2437 if (position && TREE_CODE (position) != IDENTIFIER_NODE
2438 && TREE_CODE (position) != FUNCTION_DECL)
2439 position = default_conversion (position);
2440
2441 if (!tree_fits_uhwi_p (position)
2442 || !arg_count
2443 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
2444 {
2445 warning (OPT_Wattributes,
2446 "alloc_align parameter outside range");
2447 *no_add_attrs = true;
2448 return NULL_TREE;
2449 }
2450 return NULL_TREE;
2451 }
2452
2453 /* Handle a "assume_aligned" attribute; arguments as in
2454 struct attribute_spec.handler. */
2455
2456 static tree
2457 handle_assume_aligned_attribute (tree *, tree, tree args, int,
2458 bool *no_add_attrs)
2459 {
2460 for (; args; args = TREE_CHAIN (args))
2461 {
2462 tree position = TREE_VALUE (args);
2463 if (position && TREE_CODE (position) != IDENTIFIER_NODE
2464 && TREE_CODE (position) != FUNCTION_DECL)
2465 position = default_conversion (position);
2466
2467 if (TREE_CODE (position) != INTEGER_CST)
2468 {
2469 warning (OPT_Wattributes,
2470 "assume_aligned parameter not integer constant");
2471 *no_add_attrs = true;
2472 return NULL_TREE;
2473 }
2474 }
2475 return NULL_TREE;
2476 }
2477
2478 /* Handle a "fn spec" attribute; arguments as in
2479 struct attribute_spec.handler. */
2480
2481 static tree
2482 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
2483 tree args, int ARG_UNUSED (flags),
2484 bool *no_add_attrs ATTRIBUTE_UNUSED)
2485 {
2486 gcc_assert (args
2487 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
2488 && !TREE_CHAIN (args));
2489 return NULL_TREE;
2490 }
2491
2492 /* Handle a "bnd_variable_size" attribute; arguments as in
2493 struct attribute_spec.handler. */
2494
2495 static tree
2496 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
2497 int ARG_UNUSED (flags), bool *no_add_attrs)
2498 {
2499 if (TREE_CODE (*node) != FIELD_DECL)
2500 {
2501 warning (OPT_Wattributes, "%qE attribute ignored", name);
2502 *no_add_attrs = true;
2503 }
2504
2505 return NULL_TREE;
2506 }
2507
2508 /* Handle a "bnd_legacy" attribute; arguments as in
2509 struct attribute_spec.handler. */
2510
2511 static tree
2512 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
2513 int ARG_UNUSED (flags), bool *no_add_attrs)
2514 {
2515 if (TREE_CODE (*node) != FUNCTION_DECL)
2516 {
2517 warning (OPT_Wattributes, "%qE attribute ignored", name);
2518 *no_add_attrs = true;
2519 }
2520
2521 return NULL_TREE;
2522 }
2523
2524 /* Handle a "bnd_instrument" attribute; arguments as in
2525 struct attribute_spec.handler. */
2526
2527 static tree
2528 handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
2529 int ARG_UNUSED (flags), bool *no_add_attrs)
2530 {
2531 if (TREE_CODE (*node) != FUNCTION_DECL)
2532 {
2533 warning (OPT_Wattributes, "%qE attribute ignored", name);
2534 *no_add_attrs = true;
2535 }
2536
2537 return NULL_TREE;
2538 }
2539
2540 /* Handle a "warn_unused" attribute; arguments as in
2541 struct attribute_spec.handler. */
2542
2543 static tree
2544 handle_warn_unused_attribute (tree *node, tree name,
2545 tree args ATTRIBUTE_UNUSED,
2546 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2547 {
2548 if (TYPE_P (*node))
2549 /* Do nothing else, just set the attribute. We'll get at
2550 it later with lookup_attribute. */
2551 ;
2552 else
2553 {
2554 warning (OPT_Wattributes, "%qE attribute ignored", name);
2555 *no_add_attrs = true;
2556 }
2557
2558 return NULL_TREE;
2559 }
2560
2561 /* Handle an "omp declare simd" attribute; arguments as in
2562 struct attribute_spec.handler. */
2563
2564 static tree
2565 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
2566 {
2567 return NULL_TREE;
2568 }
2569
2570 /* Handle a "simd" attribute. */
2571
2572 static tree
2573 handle_simd_attribute (tree *node, tree name, tree args, int, bool *no_add_attrs)
2574 {
2575 if (TREE_CODE (*node) == FUNCTION_DECL)
2576 {
2577 tree t = get_identifier ("omp declare simd");
2578 tree attr = NULL_TREE;
2579 if (args)
2580 {
2581 tree id = TREE_VALUE (args);
2582
2583 if (TREE_CODE (id) != STRING_CST)
2584 {
2585 error ("attribute %qE argument not a string", name);
2586 *no_add_attrs = true;
2587 return NULL_TREE;
2588 }
2589
2590 if (strcmp (TREE_STRING_POINTER (id), "notinbranch") == 0)
2591 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
2592 OMP_CLAUSE_NOTINBRANCH);
2593 else if (strcmp (TREE_STRING_POINTER (id), "inbranch") == 0)
2594 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
2595 OMP_CLAUSE_INBRANCH);
2596 else
2597 {
2598 error ("only %<inbranch%> and %<notinbranch%> flags are "
2599 "allowed for %<__simd__%> attribute");
2600 *no_add_attrs = true;
2601 return NULL_TREE;
2602 }
2603 }
2604
2605 DECL_ATTRIBUTES (*node)
2606 = tree_cons (t, build_tree_list (NULL_TREE, attr),
2607 DECL_ATTRIBUTES (*node));
2608 }
2609 else
2610 {
2611 warning (OPT_Wattributes, "%qE attribute ignored", name);
2612 *no_add_attrs = true;
2613 }
2614
2615 return NULL_TREE;
2616 }
2617
2618 /* Handle an "omp declare target" attribute; arguments as in
2619 struct attribute_spec.handler. */
2620
2621 static tree
2622 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
2623 {
2624 return NULL_TREE;
2625 }
2626
2627 /* Handle a "returns_twice" attribute; arguments as in
2628 struct attribute_spec.handler. */
2629
2630 static tree
2631 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
2632 int ARG_UNUSED (flags), bool *no_add_attrs)
2633 {
2634 if (TREE_CODE (*node) == FUNCTION_DECL)
2635 DECL_IS_RETURNS_TWICE (*node) = 1;
2636 else
2637 {
2638 warning (OPT_Wattributes, "%qE attribute ignored", name);
2639 *no_add_attrs = true;
2640 }
2641
2642 return NULL_TREE;
2643 }
2644
2645 /* Handle a "no_limit_stack" attribute; arguments as in
2646 struct attribute_spec.handler. */
2647
2648 static tree
2649 handle_no_limit_stack_attribute (tree *node, tree name,
2650 tree ARG_UNUSED (args),
2651 int ARG_UNUSED (flags),
2652 bool *no_add_attrs)
2653 {
2654 tree decl = *node;
2655
2656 if (TREE_CODE (decl) != FUNCTION_DECL)
2657 {
2658 error_at (DECL_SOURCE_LOCATION (decl),
2659 "%qE attribute applies only to functions", name);
2660 *no_add_attrs = true;
2661 }
2662 else if (DECL_INITIAL (decl))
2663 {
2664 error_at (DECL_SOURCE_LOCATION (decl),
2665 "can%'t set %qE attribute after definition", name);
2666 *no_add_attrs = true;
2667 }
2668 else
2669 DECL_NO_LIMIT_STACK (decl) = 1;
2670
2671 return NULL_TREE;
2672 }
2673
2674 /* Handle a "pure" attribute; arguments as in
2675 struct attribute_spec.handler. */
2676
2677 static tree
2678 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
2679 int ARG_UNUSED (flags), bool *no_add_attrs)
2680 {
2681 if (TREE_CODE (*node) == FUNCTION_DECL)
2682 {
2683 tree type = TREE_TYPE (*node);
2684 if (VOID_TYPE_P (TREE_TYPE (type)))
2685 warning (OPT_Wattributes, "%qE attribute on function "
2686 "returning %<void%>", name);
2687
2688 DECL_PURE_P (*node) = 1;
2689 /* ??? TODO: Support types. */
2690 }
2691 else
2692 {
2693 warning (OPT_Wattributes, "%qE attribute ignored", name);
2694 *no_add_attrs = true;
2695 }
2696
2697 return NULL_TREE;
2698 }
2699
2700 /* Digest an attribute list destined for a transactional memory statement.
2701 ALLOWED is the set of attributes that are allowed for this statement;
2702 return the attribute we parsed. Multiple attributes are never allowed. */
2703
2704 int
2705 parse_tm_stmt_attr (tree attrs, int allowed)
2706 {
2707 tree a_seen = NULL;
2708 int m_seen = 0;
2709
2710 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
2711 {
2712 tree a = TREE_PURPOSE (attrs);
2713 int m = 0;
2714
2715 if (is_attribute_p ("outer", a))
2716 m = TM_STMT_ATTR_OUTER;
2717
2718 if ((m & allowed) == 0)
2719 {
2720 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
2721 continue;
2722 }
2723
2724 if (m_seen == 0)
2725 {
2726 a_seen = a;
2727 m_seen = m;
2728 }
2729 else if (m_seen == m)
2730 warning (OPT_Wattributes, "%qE attribute duplicated", a);
2731 else
2732 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
2733 }
2734
2735 return m_seen;
2736 }
2737
2738 /* Transform a TM attribute name into a maskable integer and back.
2739 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
2740 to how the lack of an attribute is treated. */
2741
2742 int
2743 tm_attr_to_mask (tree attr)
2744 {
2745 if (attr == NULL)
2746 return 0;
2747 if (is_attribute_p ("transaction_safe", attr))
2748 return TM_ATTR_SAFE;
2749 if (is_attribute_p ("transaction_callable", attr))
2750 return TM_ATTR_CALLABLE;
2751 if (is_attribute_p ("transaction_pure", attr))
2752 return TM_ATTR_PURE;
2753 if (is_attribute_p ("transaction_unsafe", attr))
2754 return TM_ATTR_IRREVOCABLE;
2755 if (is_attribute_p ("transaction_may_cancel_outer", attr))
2756 return TM_ATTR_MAY_CANCEL_OUTER;
2757 return 0;
2758 }
2759
2760 tree
2761 tm_mask_to_attr (int mask)
2762 {
2763 const char *str;
2764 switch (mask)
2765 {
2766 case TM_ATTR_SAFE:
2767 str = "transaction_safe";
2768 break;
2769 case TM_ATTR_CALLABLE:
2770 str = "transaction_callable";
2771 break;
2772 case TM_ATTR_PURE:
2773 str = "transaction_pure";
2774 break;
2775 case TM_ATTR_IRREVOCABLE:
2776 str = "transaction_unsafe";
2777 break;
2778 case TM_ATTR_MAY_CANCEL_OUTER:
2779 str = "transaction_may_cancel_outer";
2780 break;
2781 default:
2782 gcc_unreachable ();
2783 }
2784 return get_identifier (str);
2785 }
2786
2787 /* Return the first TM attribute seen in LIST. */
2788
2789 tree
2790 find_tm_attribute (tree list)
2791 {
2792 for (; list ; list = TREE_CHAIN (list))
2793 {
2794 tree name = TREE_PURPOSE (list);
2795 if (tm_attr_to_mask (name) != 0)
2796 return name;
2797 }
2798 return NULL_TREE;
2799 }
2800
2801 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
2802 Here we accept only function types, and verify that none of the other
2803 function TM attributes are also applied. */
2804 /* ??? We need to accept class types for C++, but not C. This greatly
2805 complicates this function, since we can no longer rely on the extra
2806 processing given by function_type_required. */
2807
2808 static tree
2809 handle_tm_attribute (tree *node, tree name, tree args,
2810 int flags, bool *no_add_attrs)
2811 {
2812 /* Only one path adds the attribute; others don't. */
2813 *no_add_attrs = true;
2814
2815 switch (TREE_CODE (*node))
2816 {
2817 case RECORD_TYPE:
2818 case UNION_TYPE:
2819 /* Only tm_callable and tm_safe apply to classes. */
2820 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
2821 goto ignored;
2822 /* FALLTHRU */
2823
2824 case FUNCTION_TYPE:
2825 case METHOD_TYPE:
2826 {
2827 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
2828 if (old_name == name)
2829 ;
2830 else if (old_name != NULL_TREE)
2831 error ("type was previously declared %qE", old_name);
2832 else
2833 *no_add_attrs = false;
2834 }
2835 break;
2836
2837 case FUNCTION_DECL:
2838 {
2839 /* transaction_safe_dynamic goes on the FUNCTION_DECL, but we also
2840 want to set transaction_safe on the type. */
2841 gcc_assert (is_attribute_p ("transaction_safe_dynamic", name));
2842 if (!TYPE_P (DECL_CONTEXT (*node)))
2843 error_at (DECL_SOURCE_LOCATION (*node),
2844 "%<transaction_safe_dynamic%> may only be specified for "
2845 "a virtual function");
2846 *no_add_attrs = false;
2847 decl_attributes (&TREE_TYPE (*node),
2848 build_tree_list (get_identifier ("transaction_safe"),
2849 NULL_TREE),
2850 0);
2851 break;
2852 }
2853
2854 case POINTER_TYPE:
2855 {
2856 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
2857 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
2858 {
2859 tree fn_tmp = TREE_TYPE (*node);
2860 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
2861 *node = build_pointer_type (fn_tmp);
2862 break;
2863 }
2864 }
2865 /* FALLTHRU */
2866
2867 default:
2868 /* If a function is next, pass it on to be tried next. */
2869 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
2870 return tree_cons (name, args, NULL);
2871
2872 ignored:
2873 warning (OPT_Wattributes, "%qE attribute ignored", name);
2874 break;
2875 }
2876
2877 return NULL_TREE;
2878 }
2879
2880 /* Handle the TM_WRAP attribute; arguments as in
2881 struct attribute_spec.handler. */
2882
2883 static tree
2884 handle_tm_wrap_attribute (tree *node, tree name, tree args,
2885 int ARG_UNUSED (flags), bool *no_add_attrs)
2886 {
2887 tree decl = *node;
2888
2889 /* We don't need the attribute even on success, since we
2890 record the entry in an external table. */
2891 *no_add_attrs = true;
2892
2893 if (TREE_CODE (decl) != FUNCTION_DECL)
2894 warning (OPT_Wattributes, "%qE attribute ignored", name);
2895 else
2896 {
2897 tree wrap_decl = TREE_VALUE (args);
2898 if (error_operand_p (wrap_decl))
2899 ;
2900 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
2901 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
2902 error ("%qE argument not an identifier", name);
2903 else
2904 {
2905 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
2906 wrap_decl = lookup_name (wrap_decl);
2907 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
2908 {
2909 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
2910 TREE_TYPE (wrap_decl)))
2911 record_tm_replacement (wrap_decl, decl);
2912 else
2913 error ("%qD is not compatible with %qD", wrap_decl, decl);
2914 }
2915 else
2916 error ("%qE argument is not a function", name);
2917 }
2918 }
2919
2920 return NULL_TREE;
2921 }
2922
2923 /* Ignore the given attribute. Used when this attribute may be usefully
2924 overridden by the target, but is not used generically. */
2925
2926 static tree
2927 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
2928 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
2929 bool *no_add_attrs)
2930 {
2931 *no_add_attrs = true;
2932 return NULL_TREE;
2933 }
2934
2935 /* Handle a "no vops" attribute; arguments as in
2936 struct attribute_spec.handler. */
2937
2938 static tree
2939 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
2940 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
2941 bool *ARG_UNUSED (no_add_attrs))
2942 {
2943 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
2944 DECL_IS_NOVOPS (*node) = 1;
2945 return NULL_TREE;
2946 }
2947
2948 /* Handle a "deprecated" attribute; arguments as in
2949 struct attribute_spec.handler. */
2950
2951 static tree
2952 handle_deprecated_attribute (tree *node, tree name,
2953 tree args, int flags,
2954 bool *no_add_attrs)
2955 {
2956 tree type = NULL_TREE;
2957 int warn = 0;
2958 tree what = NULL_TREE;
2959
2960 if (!args)
2961 *no_add_attrs = true;
2962 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
2963 {
2964 error ("deprecated message is not a string");
2965 *no_add_attrs = true;
2966 }
2967
2968 if (DECL_P (*node))
2969 {
2970 tree decl = *node;
2971 type = TREE_TYPE (decl);
2972
2973 if (TREE_CODE (decl) == TYPE_DECL
2974 || TREE_CODE (decl) == PARM_DECL
2975 || VAR_OR_FUNCTION_DECL_P (decl)
2976 || TREE_CODE (decl) == FIELD_DECL
2977 || TREE_CODE (decl) == CONST_DECL
2978 || objc_method_decl (TREE_CODE (decl)))
2979 TREE_DEPRECATED (decl) = 1;
2980 else
2981 warn = 1;
2982 }
2983 else if (TYPE_P (*node))
2984 {
2985 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
2986 *node = build_variant_type_copy (*node);
2987 TREE_DEPRECATED (*node) = 1;
2988 type = *node;
2989 }
2990 else
2991 warn = 1;
2992
2993 if (warn)
2994 {
2995 *no_add_attrs = true;
2996 if (type && TYPE_NAME (type))
2997 {
2998 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
2999 what = TYPE_NAME (*node);
3000 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
3001 && DECL_NAME (TYPE_NAME (type)))
3002 what = DECL_NAME (TYPE_NAME (type));
3003 }
3004 if (what)
3005 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
3006 else
3007 warning (OPT_Wattributes, "%qE attribute ignored", name);
3008 }
3009
3010 return NULL_TREE;
3011 }
3012
3013 /* Handle a "vector_size" attribute; arguments as in
3014 struct attribute_spec.handler. */
3015
3016 static tree
3017 handle_vector_size_attribute (tree *node, tree name, tree args,
3018 int ARG_UNUSED (flags),
3019 bool *no_add_attrs)
3020 {
3021 unsigned HOST_WIDE_INT vecsize, nunits;
3022 machine_mode orig_mode;
3023 tree type = *node, new_type, size;
3024
3025 *no_add_attrs = true;
3026
3027 size = TREE_VALUE (args);
3028 if (size && TREE_CODE (size) != IDENTIFIER_NODE
3029 && TREE_CODE (size) != FUNCTION_DECL)
3030 size = default_conversion (size);
3031
3032 if (!tree_fits_uhwi_p (size))
3033 {
3034 warning (OPT_Wattributes, "%qE attribute ignored", name);
3035 return NULL_TREE;
3036 }
3037
3038 /* Get the vector size (in bytes). */
3039 vecsize = tree_to_uhwi (size);
3040
3041 /* We need to provide for vector pointers, vector arrays, and
3042 functions returning vectors. For example:
3043
3044 __attribute__((vector_size(16))) short *foo;
3045
3046 In this case, the mode is SI, but the type being modified is
3047 HI, so we need to look further. */
3048
3049 while (POINTER_TYPE_P (type)
3050 || TREE_CODE (type) == FUNCTION_TYPE
3051 || TREE_CODE (type) == METHOD_TYPE
3052 || TREE_CODE (type) == ARRAY_TYPE
3053 || TREE_CODE (type) == OFFSET_TYPE)
3054 type = TREE_TYPE (type);
3055
3056 /* Get the mode of the type being modified. */
3057 orig_mode = TYPE_MODE (type);
3058
3059 if ((!INTEGRAL_TYPE_P (type)
3060 && !SCALAR_FLOAT_TYPE_P (type)
3061 && !FIXED_POINT_TYPE_P (type))
3062 || (!SCALAR_FLOAT_MODE_P (orig_mode)
3063 && GET_MODE_CLASS (orig_mode) != MODE_INT
3064 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
3065 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
3066 || TREE_CODE (type) == BOOLEAN_TYPE)
3067 {
3068 error ("invalid vector type for attribute %qE", name);
3069 return NULL_TREE;
3070 }
3071
3072 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
3073 {
3074 error ("vector size not an integral multiple of component size");
3075 return NULL;
3076 }
3077
3078 if (vecsize == 0)
3079 {
3080 error ("zero vector size");
3081 return NULL;
3082 }
3083
3084 /* Calculate how many units fit in the vector. */
3085 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
3086 if (nunits & (nunits - 1))
3087 {
3088 error ("number of components of the vector not a power of two");
3089 return NULL_TREE;
3090 }
3091
3092 new_type = build_vector_type (type, nunits);
3093
3094 /* Build back pointers if needed. */
3095 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
3096
3097 return NULL_TREE;
3098 }
3099
3100 /* Handle the "nonnull" attribute. */
3101
3102 static tree
3103 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
3104 tree args, int ARG_UNUSED (flags),
3105 bool *no_add_attrs)
3106 {
3107 tree type = *node;
3108 unsigned HOST_WIDE_INT attr_arg_num;
3109
3110 /* If no arguments are specified, all pointer arguments should be
3111 non-null. Verify a full prototype is given so that the arguments
3112 will have the correct types when we actually check them later.
3113 Avoid diagnosing type-generic built-ins since those have no
3114 prototype. */
3115 if (!args)
3116 {
3117 if (!prototype_p (type)
3118 && (!TYPE_ATTRIBUTES (type)
3119 || !lookup_attribute ("type generic", TYPE_ATTRIBUTES (type))))
3120 {
3121 error ("nonnull attribute without arguments on a non-prototype");
3122 *no_add_attrs = true;
3123 }
3124 return NULL_TREE;
3125 }
3126
3127 /* Argument list specified. Verify that each argument number references
3128 a pointer argument. */
3129 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
3130 {
3131 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
3132
3133 tree arg = TREE_VALUE (args);
3134 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
3135 && TREE_CODE (arg) != FUNCTION_DECL)
3136 TREE_VALUE (args) = arg = default_conversion (arg);
3137
3138 if (!get_nonnull_operand (arg, &arg_num))
3139 {
3140 error ("nonnull argument has invalid operand number (argument %lu)",
3141 (unsigned long) attr_arg_num);
3142 *no_add_attrs = true;
3143 return NULL_TREE;
3144 }
3145
3146 if (prototype_p (type))
3147 {
3148 function_args_iterator iter;
3149 tree argument;
3150
3151 function_args_iter_init (&iter, type);
3152 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
3153 {
3154 argument = function_args_iter_cond (&iter);
3155 if (argument == NULL_TREE || ck_num == arg_num)
3156 break;
3157 }
3158
3159 if (!argument
3160 || TREE_CODE (argument) == VOID_TYPE)
3161 {
3162 error ("nonnull argument with out-of-range operand number "
3163 "(argument %lu, operand %lu)",
3164 (unsigned long) attr_arg_num, (unsigned long) arg_num);
3165 *no_add_attrs = true;
3166 return NULL_TREE;
3167 }
3168
3169 if (TREE_CODE (argument) != POINTER_TYPE)
3170 {
3171 error ("nonnull argument references non-pointer operand "
3172 "(argument %lu, operand %lu)",
3173 (unsigned long) attr_arg_num, (unsigned long) arg_num);
3174 *no_add_attrs = true;
3175 return NULL_TREE;
3176 }
3177 }
3178 }
3179
3180 return NULL_TREE;
3181 }
3182
3183 /* Handle the "nonstring" variable attribute. */
3184
3185 static tree
3186 handle_nonstring_attribute (tree *node, tree name, tree ARG_UNUSED (args),
3187 int ARG_UNUSED (flags), bool *no_add_attrs)
3188 {
3189 gcc_assert (!args);
3190 tree_code code = TREE_CODE (*node);
3191
3192 if (VAR_P (*node)
3193 || code == FIELD_DECL
3194 || code == PARM_DECL)
3195 {
3196 tree type = TREE_TYPE (*node);
3197
3198 if (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
3199 {
3200 tree eltype = TREE_TYPE (type);
3201 if (eltype == char_type_node)
3202 return NULL_TREE;
3203 }
3204
3205 warning (OPT_Wattributes,
3206 "%qE attribute ignored on objects of type %qT",
3207 name, type);
3208 *no_add_attrs = true;
3209 return NULL_TREE;
3210 }
3211
3212 if (code == FUNCTION_DECL)
3213 warning (OPT_Wattributes,
3214 "%qE attribute does not apply to functions", name);
3215 else if (code == TYPE_DECL)
3216 warning (OPT_Wattributes,
3217 "%qE attribute does not apply to types", name);
3218 else
3219 warning (OPT_Wattributes, "%qE attribute ignored", name);
3220
3221 *no_add_attrs = true;
3222 return NULL_TREE;
3223 }
3224
3225 /* Handle a "nothrow" attribute; arguments as in
3226 struct attribute_spec.handler. */
3227
3228 static tree
3229 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
3230 int ARG_UNUSED (flags), bool *no_add_attrs)
3231 {
3232 if (TREE_CODE (*node) == FUNCTION_DECL)
3233 TREE_NOTHROW (*node) = 1;
3234 /* ??? TODO: Support types. */
3235 else
3236 {
3237 warning (OPT_Wattributes, "%qE attribute ignored", name);
3238 *no_add_attrs = true;
3239 }
3240
3241 return NULL_TREE;
3242 }
3243
3244 /* Handle a "cleanup" attribute; arguments as in
3245 struct attribute_spec.handler. */
3246
3247 static tree
3248 handle_cleanup_attribute (tree *node, tree name, tree args,
3249 int ARG_UNUSED (flags), bool *no_add_attrs)
3250 {
3251 tree decl = *node;
3252 tree cleanup_id, cleanup_decl;
3253
3254 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
3255 for global destructors in C++. This requires infrastructure that
3256 we don't have generically at the moment. It's also not a feature
3257 we'd be missing too much, since we do have attribute constructor. */
3258 if (!VAR_P (decl) || TREE_STATIC (decl))
3259 {
3260 warning (OPT_Wattributes, "%qE attribute ignored", name);
3261 *no_add_attrs = true;
3262 return NULL_TREE;
3263 }
3264
3265 /* Verify that the argument is a function in scope. */
3266 /* ??? We could support pointers to functions here as well, if
3267 that was considered desirable. */
3268 cleanup_id = TREE_VALUE (args);
3269 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
3270 {
3271 error ("cleanup argument not an identifier");
3272 *no_add_attrs = true;
3273 return NULL_TREE;
3274 }
3275 cleanup_decl = lookup_name (cleanup_id);
3276 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
3277 {
3278 error ("cleanup argument not a function");
3279 *no_add_attrs = true;
3280 return NULL_TREE;
3281 }
3282
3283 /* That the function has proper type is checked with the
3284 eventual call to build_function_call. */
3285
3286 return NULL_TREE;
3287 }
3288
3289 /* Handle a "warn_unused_result" attribute. No special handling. */
3290
3291 static tree
3292 handle_warn_unused_result_attribute (tree *node, tree name,
3293 tree ARG_UNUSED (args),
3294 int ARG_UNUSED (flags), bool *no_add_attrs)
3295 {
3296 /* Ignore the attribute for functions not returning any value. */
3297 if (VOID_TYPE_P (TREE_TYPE (*node)))
3298 {
3299 warning (OPT_Wattributes, "%qE attribute ignored", name);
3300 *no_add_attrs = true;
3301 }
3302
3303 return NULL_TREE;
3304 }
3305
3306 /* Handle a "sentinel" attribute. */
3307
3308 static tree
3309 handle_sentinel_attribute (tree *node, tree name, tree args,
3310 int ARG_UNUSED (flags), bool *no_add_attrs)
3311 {
3312 if (!prototype_p (*node))
3313 {
3314 warning (OPT_Wattributes,
3315 "%qE attribute requires prototypes with named arguments", name);
3316 *no_add_attrs = true;
3317 }
3318 else
3319 {
3320 if (!stdarg_p (*node))
3321 {
3322 warning (OPT_Wattributes,
3323 "%qE attribute only applies to variadic functions", name);
3324 *no_add_attrs = true;
3325 }
3326 }
3327
3328 if (args)
3329 {
3330 tree position = TREE_VALUE (args);
3331 if (position && TREE_CODE (position) != IDENTIFIER_NODE
3332 && TREE_CODE (position) != FUNCTION_DECL)
3333 position = default_conversion (position);
3334
3335 if (TREE_CODE (position) != INTEGER_CST
3336 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
3337 {
3338 warning (OPT_Wattributes,
3339 "requested position is not an integer constant");
3340 *no_add_attrs = true;
3341 }
3342 else
3343 {
3344 if (tree_int_cst_lt (position, integer_zero_node))
3345 {
3346 warning (OPT_Wattributes,
3347 "requested position is less than zero");
3348 *no_add_attrs = true;
3349 }
3350 }
3351 }
3352
3353 return NULL_TREE;
3354 }
3355
3356 /* Handle a "type_generic" attribute. */
3357
3358 static tree
3359 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
3360 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
3361 bool * ARG_UNUSED (no_add_attrs))
3362 {
3363 /* Ensure we have a function type. */
3364 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
3365
3366 /* Ensure we have a variadic function. */
3367 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
3368
3369 return NULL_TREE;
3370 }
3371
3372 /* Handle a "target" attribute. */
3373
3374 static tree
3375 handle_target_attribute (tree *node, tree name, tree args, int flags,
3376 bool *no_add_attrs)
3377 {
3378 /* Ensure we have a function type. */
3379 if (TREE_CODE (*node) != FUNCTION_DECL)
3380 {
3381 warning (OPT_Wattributes, "%qE attribute ignored", name);
3382 *no_add_attrs = true;
3383 }
3384 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
3385 {
3386 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
3387 "with %qs attribute", name, "target_clones");
3388 *no_add_attrs = true;
3389 }
3390 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
3391 flags))
3392 *no_add_attrs = true;
3393
3394 /* Check that there's no empty string in values of the attribute. */
3395 for (tree t = args; t != NULL_TREE; t = TREE_CHAIN (t))
3396 {
3397 tree value = TREE_VALUE (t);
3398 if (TREE_CODE (value) == STRING_CST
3399 && TREE_STRING_LENGTH (value) == 1
3400 && TREE_STRING_POINTER (value)[0] == '\0')
3401 {
3402 warning (OPT_Wattributes, "empty string in attribute %<target%>");
3403 *no_add_attrs = true;
3404 }
3405 }
3406
3407 return NULL_TREE;
3408 }
3409
3410 /* Handle a "target_clones" attribute. */
3411
3412 static tree
3413 handle_target_clones_attribute (tree *node, tree name, tree ARG_UNUSED (args),
3414 int ARG_UNUSED (flags), bool *no_add_attrs)
3415 {
3416 /* Ensure we have a function type. */
3417 if (TREE_CODE (*node) == FUNCTION_DECL)
3418 {
3419 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
3420 {
3421 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
3422 "with %qs attribute", name, "always_inline");
3423 *no_add_attrs = true;
3424 }
3425 else if (lookup_attribute ("target", DECL_ATTRIBUTES (*node)))
3426 {
3427 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
3428 "with %qs attribute", name, "target");
3429 *no_add_attrs = true;
3430 }
3431 else
3432 /* Do not inline functions with multiple clone targets. */
3433 DECL_UNINLINABLE (*node) = 1;
3434 }
3435 else
3436 {
3437 warning (OPT_Wattributes, "%qE attribute ignored", name);
3438 *no_add_attrs = true;
3439 }
3440 return NULL_TREE;
3441 }
3442
3443 /* For handling "optimize" attribute. arguments as in
3444 struct attribute_spec.handler. */
3445
3446 static tree
3447 handle_optimize_attribute (tree *node, tree name, tree args,
3448 int ARG_UNUSED (flags), bool *no_add_attrs)
3449 {
3450 /* Ensure we have a function type. */
3451 if (TREE_CODE (*node) != FUNCTION_DECL)
3452 {
3453 warning (OPT_Wattributes, "%qE attribute ignored", name);
3454 *no_add_attrs = true;
3455 }
3456 else
3457 {
3458 struct cl_optimization cur_opts;
3459 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
3460
3461 /* Save current options. */
3462 cl_optimization_save (&cur_opts, &global_options);
3463
3464 /* If we previously had some optimization options, use them as the
3465 default. */
3466 if (old_opts)
3467 cl_optimization_restore (&global_options,
3468 TREE_OPTIMIZATION (old_opts));
3469
3470 /* Parse options, and update the vector. */
3471 parse_optimize_options (args, true);
3472 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
3473 = build_optimization_node (&global_options);
3474
3475 /* Restore current options. */
3476 cl_optimization_restore (&global_options, &cur_opts);
3477 }
3478
3479 return NULL_TREE;
3480 }
3481
3482 /* Handle a "no_split_stack" attribute. */
3483
3484 static tree
3485 handle_no_split_stack_attribute (tree *node, tree name,
3486 tree ARG_UNUSED (args),
3487 int ARG_UNUSED (flags),
3488 bool *no_add_attrs)
3489 {
3490 tree decl = *node;
3491
3492 if (TREE_CODE (decl) != FUNCTION_DECL)
3493 {
3494 error_at (DECL_SOURCE_LOCATION (decl),
3495 "%qE attribute applies only to functions", name);
3496 *no_add_attrs = true;
3497 }
3498 else if (DECL_INITIAL (decl))
3499 {
3500 error_at (DECL_SOURCE_LOCATION (decl),
3501 "can%'t set %qE attribute after definition", name);
3502 *no_add_attrs = true;
3503 }
3504
3505 return NULL_TREE;
3506 }
3507
3508 /* Handle a "returns_nonnull" attribute; arguments as in
3509 struct attribute_spec.handler. */
3510
3511 static tree
3512 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
3513 bool *no_add_attrs)
3514 {
3515 // Even without a prototype we still have a return type we can check.
3516 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
3517 {
3518 error ("returns_nonnull attribute on a function not returning a pointer");
3519 *no_add_attrs = true;
3520 }
3521 return NULL_TREE;
3522 }
3523
3524 /* Handle a "designated_init" attribute; arguments as in
3525 struct attribute_spec.handler. */
3526
3527 static tree
3528 handle_designated_init_attribute (tree *node, tree name, tree, int,
3529 bool *no_add_attrs)
3530 {
3531 if (TREE_CODE (*node) != RECORD_TYPE)
3532 {
3533 error ("%qE attribute is only valid on %<struct%> type", name);
3534 *no_add_attrs = true;
3535 }
3536 return NULL_TREE;
3537 }
3538
3539
3540 /* Handle a "fallthrough" attribute; arguments as in struct
3541 attribute_spec.handler. */
3542
3543 static tree
3544 handle_fallthrough_attribute (tree *, tree name, tree, int,
3545 bool *no_add_attrs)
3546 {
3547 warning (OPT_Wattributes, "%qE attribute ignored", name);
3548 *no_add_attrs = true;
3549 return NULL_TREE;
3550 }
3551
3552 static tree
3553 handle_patchable_function_entry_attribute (tree *, tree, tree, int, bool *)
3554 {
3555 /* Nothing to be done here. */
3556 return NULL_TREE;
3557 }