1 /* C-family attributes handling.
2 Copyright (C) 1992-2020 Free Software Foundation, Inc.
4 This file is part of GCC.
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
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
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/>. */
22 #include "coretypes.h"
28 #include "gimple-expr.h"
30 #include "stringpool.h"
32 #include "diagnostic.h"
34 #include "stor-layout.h"
38 #include "trans-mem.h"
40 #include "common/common-target.h"
41 #include "langhooks.h"
42 #include "tree-inline.h"
44 #include "tree-iterator.h"
47 #include "tree-pretty-print.h"
49 static tree
handle_packed_attribute (tree
*, tree
, tree
, int, bool *);
50 static tree
handle_nocommon_attribute (tree
*, tree
, tree
, int, bool *);
51 static tree
handle_common_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
,
57 static tree
handle_no_sanitize_thread_attribute (tree
*, tree
, tree
,
59 static tree
handle_no_address_safety_analysis_attribute (tree
*, tree
, tree
,
61 static tree
handle_no_sanitize_undefined_attribute (tree
*, tree
, tree
, int,
63 static tree
handle_asan_odr_indicator_attribute (tree
*, tree
, tree
, int,
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_symver_attribute (tree
*, tree
, tree
, int, bool *);
70 static tree
handle_noicf_attribute (tree
*, tree
, tree
, int, bool *);
71 static tree
handle_noipa_attribute (tree
*, tree
, tree
, int, bool *);
72 static tree
handle_leaf_attribute (tree
*, tree
, tree
, int, bool *);
73 static tree
handle_always_inline_attribute (tree
*, tree
, tree
, int,
75 static tree
handle_gnu_inline_attribute (tree
*, tree
, tree
, int, bool *);
76 static tree
handle_artificial_attribute (tree
*, tree
, tree
, int, bool *);
77 static tree
handle_flatten_attribute (tree
*, tree
, tree
, int, bool *);
78 static tree
handle_error_attribute (tree
*, tree
, tree
, int, bool *);
79 static tree
handle_used_attribute (tree
*, tree
, tree
, int, bool *);
80 static tree
handle_externally_visible_attribute (tree
*, tree
, tree
, int,
82 static tree
handle_no_reorder_attribute (tree
*, tree
, tree
, int,
84 static tree
handle_const_attribute (tree
*, tree
, tree
, int, bool *);
85 static tree
handle_transparent_union_attribute (tree
*, tree
, tree
,
87 static tree
handle_scalar_storage_order_attribute (tree
*, tree
, tree
,
89 static tree
handle_constructor_attribute (tree
*, tree
, tree
, int, bool *);
90 static tree
handle_destructor_attribute (tree
*, tree
, tree
, int, bool *);
91 static tree
handle_mode_attribute (tree
*, tree
, tree
, int, bool *);
92 static tree
handle_section_attribute (tree
*, tree
, tree
, int, bool *);
93 static tree
handle_aligned_attribute (tree
*, tree
, tree
, int, bool *);
94 static tree
handle_warn_if_not_aligned_attribute (tree
*, tree
, tree
,
96 static tree
handle_noinit_attribute (tree
*, tree
, tree
, int, bool *);
97 static tree
handle_weak_attribute (tree
*, tree
, tree
, int, bool *) ;
98 static tree
handle_noplt_attribute (tree
*, tree
, tree
, int, bool *) ;
99 static tree
handle_alias_ifunc_attribute (bool, tree
*, tree
, tree
, bool *);
100 static tree
handle_ifunc_attribute (tree
*, tree
, tree
, int, bool *);
101 static tree
handle_alias_attribute (tree
*, tree
, tree
, int, bool *);
102 static tree
handle_weakref_attribute (tree
*, tree
, tree
, int, bool *) ;
103 static tree
handle_visibility_attribute (tree
*, tree
, tree
, int,
105 static tree
handle_tls_model_attribute (tree
*, tree
, tree
, int,
107 static tree
handle_no_instrument_function_attribute (tree
*, tree
,
109 static tree
handle_no_profile_instrument_function_attribute (tree
*, tree
,
111 static tree
handle_malloc_attribute (tree
*, tree
, tree
, int, bool *);
112 static tree
handle_returns_twice_attribute (tree
*, tree
, tree
, int, bool *);
113 static tree
handle_no_limit_stack_attribute (tree
*, tree
, tree
, int,
115 static tree
handle_pure_attribute (tree
*, tree
, tree
, int, bool *);
116 static tree
handle_tm_attribute (tree
*, tree
, tree
, int, bool *);
117 static tree
handle_tm_wrap_attribute (tree
*, tree
, tree
, int, bool *);
118 static tree
handle_novops_attribute (tree
*, tree
, tree
, int, bool *);
119 static tree
handle_vector_size_attribute (tree
*, tree
, tree
, int,
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,
127 static tree
handle_access_attribute (tree
*, tree
, tree
, int, bool *);
129 static tree
handle_sentinel_attribute (tree
*, tree
, tree
, int, bool *);
130 static tree
handle_type_generic_attribute (tree
*, tree
, tree
, int, bool *);
131 static tree
handle_alloc_size_attribute (tree
*, tree
, tree
, int, bool *);
132 static tree
handle_alloc_align_attribute (tree
*, tree
, tree
, int, bool *);
133 static tree
handle_assume_aligned_attribute (tree
*, tree
, tree
, int, bool *);
134 static tree
handle_target_attribute (tree
*, tree
, tree
, int, bool *);
135 static tree
handle_target_clones_attribute (tree
*, tree
, tree
, int, bool *);
136 static tree
handle_optimize_attribute (tree
*, tree
, tree
, int, bool *);
137 static tree
ignore_attribute (tree
*, tree
, tree
, int, bool *);
138 static tree
handle_no_split_stack_attribute (tree
*, tree
, tree
, int, bool *);
139 static tree
handle_fnspec_attribute (tree
*, tree
, tree
, int, bool *);
140 static tree
handle_warn_unused_attribute (tree
*, tree
, tree
, int, bool *);
141 static tree
handle_returns_nonnull_attribute (tree
*, tree
, tree
, int, bool *);
142 static tree
handle_omp_declare_simd_attribute (tree
*, tree
, tree
, int,
144 static tree
handle_omp_declare_variant_attribute (tree
*, tree
, tree
, int,
146 static tree
handle_simd_attribute (tree
*, tree
, tree
, int, bool *);
147 static tree
handle_omp_declare_target_attribute (tree
*, tree
, tree
, int,
149 static tree
handle_designated_init_attribute (tree
*, tree
, tree
, int, bool *);
150 static tree
handle_patchable_function_entry_attribute (tree
*, tree
, tree
,
152 static tree
handle_copy_attribute (tree
*, tree
, tree
, int, bool *);
154 /* Helper to define attribute exclusions. */
155 #define ATTR_EXCL(name, function, type, variable) \
156 { name, function, type, variable }
158 /* Define attributes that are mutually exclusive with one another. */
159 static const struct attribute_spec::exclusions attr_aligned_exclusions
[] =
161 /* Attribute name exclusion applies to:
162 function, type, variable */
163 ATTR_EXCL ("aligned", true, false, false),
164 ATTR_EXCL ("packed", true, false, false),
165 ATTR_EXCL (NULL
, false, false, false)
168 extern const struct attribute_spec::exclusions attr_cold_hot_exclusions
[] =
170 ATTR_EXCL ("cold", true, true, true),
171 ATTR_EXCL ("hot", true, true, true),
172 ATTR_EXCL (NULL
, false, false, false)
175 static const struct attribute_spec::exclusions attr_common_exclusions
[] =
177 ATTR_EXCL ("common", true, true, true),
178 ATTR_EXCL ("nocommon", true, true, true),
179 ATTR_EXCL (NULL
, false, false, false),
182 static const struct attribute_spec::exclusions attr_inline_exclusions
[] =
184 ATTR_EXCL ("noinline", true, true, true),
185 ATTR_EXCL (NULL
, false, false, false),
188 static const struct attribute_spec::exclusions attr_noinline_exclusions
[] =
190 ATTR_EXCL ("always_inline", true, true, true),
191 ATTR_EXCL ("gnu_inline", true, true, true),
192 ATTR_EXCL (NULL
, false, false, false),
195 extern const struct attribute_spec::exclusions attr_noreturn_exclusions
[] =
197 ATTR_EXCL ("alloc_align", true, true, true),
198 ATTR_EXCL ("alloc_size", true, true, true),
199 ATTR_EXCL ("const", true, true, true),
200 ATTR_EXCL ("malloc", true, true, true),
201 ATTR_EXCL ("pure", true, true, true),
202 ATTR_EXCL ("returns_twice", true, true, true),
203 ATTR_EXCL ("warn_unused_result", true, true, true),
204 ATTR_EXCL (NULL
, false, false, false),
207 static const struct attribute_spec::exclusions
208 attr_warn_unused_result_exclusions
[] =
210 ATTR_EXCL ("noreturn", true, true, true),
211 ATTR_EXCL ("warn_unused_result", true, true, true),
212 ATTR_EXCL (NULL
, false, false, false),
215 static const struct attribute_spec::exclusions attr_returns_twice_exclusions
[] =
217 ATTR_EXCL ("noreturn", true, true, true),
218 ATTR_EXCL (NULL
, false, false, false),
221 /* Exclusions that apply to attribute alloc_align, alloc_size, and malloc. */
222 static const struct attribute_spec::exclusions attr_alloc_exclusions
[] =
224 ATTR_EXCL ("const", true, true, true),
225 ATTR_EXCL ("noreturn", true, true, true),
226 ATTR_EXCL ("pure", true, true, true),
227 ATTR_EXCL (NULL
, false, false, false),
230 static const struct attribute_spec::exclusions attr_const_pure_exclusions
[] =
232 ATTR_EXCL ("const", true, true, true),
233 ATTR_EXCL ("alloc_align", true, true, true),
234 ATTR_EXCL ("alloc_size", true, true, true),
235 ATTR_EXCL ("malloc", true, true, true),
236 ATTR_EXCL ("noreturn", true, true, true),
237 ATTR_EXCL ("pure", true, true, true),
238 ATTR_EXCL (NULL
, false, false, false)
241 static const struct attribute_spec::exclusions attr_noinit_exclusions
[] =
243 ATTR_EXCL ("noinit", true, true, true),
244 ATTR_EXCL ("section", true, true, true),
245 ATTR_EXCL (NULL
, false, false, false),
248 /* Table of machine-independent attributes common to all C-like languages.
250 Current list of processed common attributes: nonnull. */
251 const struct attribute_spec c_common_attribute_table
[] =
253 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
254 affects_type_identity, handler, exclude } */
255 { "packed", 0, 0, false, false, false, false,
256 handle_packed_attribute
,
257 attr_aligned_exclusions
},
258 { "nocommon", 0, 0, true, false, false, false,
259 handle_nocommon_attribute
,
260 attr_common_exclusions
},
261 { "common", 0, 0, true, false, false, false,
262 handle_common_attribute
,
263 attr_common_exclusions
},
264 /* FIXME: logically, noreturn attributes should be listed as
265 "false, true, true" and apply to function types. But implementing this
266 would require all the places in the compiler that use TREE_THIS_VOLATILE
267 on a decl to identify non-returning functions to be located and fixed
268 to check the function type instead. */
269 { "noreturn", 0, 0, true, false, false, false,
270 handle_noreturn_attribute
,
271 attr_noreturn_exclusions
},
272 { "volatile", 0, 0, true, false, false, false,
273 handle_noreturn_attribute
, NULL
},
274 { "stack_protect", 0, 0, true, false, false, false,
275 handle_stack_protect_attribute
, NULL
},
276 { "noinline", 0, 0, true, false, false, false,
277 handle_noinline_attribute
,
278 attr_noinline_exclusions
},
279 { "noclone", 0, 0, true, false, false, false,
280 handle_noclone_attribute
, NULL
},
281 { "no_icf", 0, 0, true, false, false, false,
282 handle_noicf_attribute
, NULL
},
283 { "noipa", 0, 0, true, false, false, false,
284 handle_noipa_attribute
, NULL
},
285 { "leaf", 0, 0, true, false, false, false,
286 handle_leaf_attribute
, NULL
},
287 { "always_inline", 0, 0, true, false, false, false,
288 handle_always_inline_attribute
,
289 attr_inline_exclusions
},
290 { "gnu_inline", 0, 0, true, false, false, false,
291 handle_gnu_inline_attribute
,
292 attr_inline_exclusions
},
293 { "artificial", 0, 0, true, false, false, false,
294 handle_artificial_attribute
, NULL
},
295 { "flatten", 0, 0, true, false, false, false,
296 handle_flatten_attribute
, NULL
},
297 { "used", 0, 0, true, false, false, false,
298 handle_used_attribute
, NULL
},
299 { "unused", 0, 0, false, false, false, false,
300 handle_unused_attribute
, NULL
},
301 { "externally_visible", 0, 0, true, false, false, false,
302 handle_externally_visible_attribute
, NULL
},
303 { "no_reorder", 0, 0, true, false, false, false,
304 handle_no_reorder_attribute
, NULL
},
305 /* The same comments as for noreturn attributes apply to const ones. */
306 { "const", 0, 0, true, false, false, false,
307 handle_const_attribute
,
308 attr_const_pure_exclusions
},
309 { "scalar_storage_order", 1, 1, false, false, false, false,
310 handle_scalar_storage_order_attribute
, NULL
},
311 { "transparent_union", 0, 0, false, false, false, false,
312 handle_transparent_union_attribute
, NULL
},
313 { "constructor", 0, 1, true, false, false, false,
314 handle_constructor_attribute
, NULL
},
315 { "destructor", 0, 1, true, false, false, false,
316 handle_destructor_attribute
, NULL
},
317 { "mode", 1, 1, false, true, false, false,
318 handle_mode_attribute
, NULL
},
319 { "section", 1, 1, true, false, false, false,
320 handle_section_attribute
, attr_noinit_exclusions
},
321 { "aligned", 0, 1, false, false, false, false,
322 handle_aligned_attribute
,
323 attr_aligned_exclusions
},
324 { "warn_if_not_aligned", 0, 1, false, false, false, false,
325 handle_warn_if_not_aligned_attribute
, NULL
},
326 { "weak", 0, 0, true, false, false, false,
327 handle_weak_attribute
, NULL
},
328 { "noplt", 0, 0, true, false, false, false,
329 handle_noplt_attribute
, NULL
},
330 { "ifunc", 1, 1, true, false, false, false,
331 handle_ifunc_attribute
, NULL
},
332 { "alias", 1, 1, true, false, false, false,
333 handle_alias_attribute
, NULL
},
334 { "weakref", 0, 1, true, false, false, false,
335 handle_weakref_attribute
, NULL
},
336 { "no_instrument_function", 0, 0, true, false, false, false,
337 handle_no_instrument_function_attribute
,
339 { "no_profile_instrument_function", 0, 0, true, false, false, false,
340 handle_no_profile_instrument_function_attribute
,
342 { "malloc", 0, 0, true, false, false, false,
343 handle_malloc_attribute
, attr_alloc_exclusions
},
344 { "returns_twice", 0, 0, true, false, false, false,
345 handle_returns_twice_attribute
,
346 attr_returns_twice_exclusions
},
347 { "no_stack_limit", 0, 0, true, false, false, false,
348 handle_no_limit_stack_attribute
, NULL
},
349 { "pure", 0, 0, true, false, false, false,
350 handle_pure_attribute
,
351 attr_const_pure_exclusions
},
352 { "transaction_callable", 0, 0, false, true, false, false,
353 handle_tm_attribute
, NULL
},
354 { "transaction_unsafe", 0, 0, false, true, false, true,
355 handle_tm_attribute
, NULL
},
356 { "transaction_safe", 0, 0, false, true, false, true,
357 handle_tm_attribute
, NULL
},
358 { "transaction_safe_dynamic", 0, 0, true, false, false, false,
359 handle_tm_attribute
, NULL
},
360 { "transaction_may_cancel_outer", 0, 0, false, true, false, false,
361 handle_tm_attribute
, NULL
},
362 /* ??? These two attributes didn't make the transition from the
363 Intel language document to the multi-vendor language document. */
364 { "transaction_pure", 0, 0, false, true, false, false,
365 handle_tm_attribute
, NULL
},
366 { "transaction_wrap", 1, 1, true, false, false, false,
367 handle_tm_wrap_attribute
, NULL
},
368 /* For internal use (marking of builtins) only. The name contains space
369 to prevent its usage in source code. */
370 { "no vops", 0, 0, true, false, false, false,
371 handle_novops_attribute
, NULL
},
372 { "deprecated", 0, 1, false, false, false, false,
373 handle_deprecated_attribute
, NULL
},
374 { "vector_size", 1, 1, false, true, false, true,
375 handle_vector_size_attribute
, NULL
},
376 { "visibility", 1, 1, false, false, false, false,
377 handle_visibility_attribute
, NULL
},
378 { "tls_model", 1, 1, true, false, false, false,
379 handle_tls_model_attribute
, NULL
},
380 { "nonnull", 0, -1, false, true, true, false,
381 handle_nonnull_attribute
, NULL
},
382 { "nonstring", 0, 0, true, false, false, false,
383 handle_nonstring_attribute
, NULL
},
384 { "nothrow", 0, 0, true, false, false, false,
385 handle_nothrow_attribute
, NULL
},
386 { "may_alias", 0, 0, false, true, false, false, NULL
, NULL
},
387 { "cleanup", 1, 1, true, false, false, false,
388 handle_cleanup_attribute
, NULL
},
389 { "warn_unused_result", 0, 0, false, true, true, false,
390 handle_warn_unused_result_attribute
,
391 attr_warn_unused_result_exclusions
},
392 { "sentinel", 0, 1, false, true, true, false,
393 handle_sentinel_attribute
, NULL
},
394 /* For internal use (marking of builtins) only. The name contains space
395 to prevent its usage in source code. */
396 { "type generic", 0, 0, false, true, true, false,
397 handle_type_generic_attribute
, NULL
},
398 { "alloc_size", 1, 2, false, true, true, false,
399 handle_alloc_size_attribute
,
400 attr_alloc_exclusions
},
401 { "cold", 0, 0, true, false, false, false,
402 handle_cold_attribute
,
403 attr_cold_hot_exclusions
},
404 { "hot", 0, 0, true, false, false, false,
405 handle_hot_attribute
,
406 attr_cold_hot_exclusions
},
407 { "no_address_safety_analysis",
408 0, 0, true, false, false, false,
409 handle_no_address_safety_analysis_attribute
,
411 { "no_sanitize", 1, -1, true, false, false, false,
412 handle_no_sanitize_attribute
, NULL
},
413 { "no_sanitize_address", 0, 0, true, false, false, false,
414 handle_no_sanitize_address_attribute
, NULL
},
415 { "no_sanitize_thread", 0, 0, true, false, false, false,
416 handle_no_sanitize_thread_attribute
, NULL
},
417 { "no_sanitize_undefined", 0, 0, true, false, false, false,
418 handle_no_sanitize_undefined_attribute
, NULL
},
419 { "asan odr indicator", 0, 0, true, false, false, false,
420 handle_asan_odr_indicator_attribute
, NULL
},
421 { "warning", 1, 1, true, false, false, false,
422 handle_error_attribute
, NULL
},
423 { "error", 1, 1, true, false, false, false,
424 handle_error_attribute
, NULL
},
425 { "target", 1, -1, true, false, false, false,
426 handle_target_attribute
, NULL
},
427 { "target_clones", 1, -1, true, false, false, false,
428 handle_target_clones_attribute
, NULL
},
429 { "optimize", 1, -1, true, false, false, false,
430 handle_optimize_attribute
, NULL
},
431 /* For internal use only. The leading '*' both prevents its usage in
432 source code and signals that it may be overridden by machine tables. */
433 { "*tm regparm", 0, 0, false, true, true, false,
434 ignore_attribute
, NULL
},
435 { "no_split_stack", 0, 0, true, false, false, false,
436 handle_no_split_stack_attribute
, NULL
},
437 /* For internal use (marking of builtins and runtime functions) only.
438 The name contains space to prevent its usage in source code. */
439 { "fn spec", 1, 1, false, true, true, false,
440 handle_fnspec_attribute
, NULL
},
441 { "warn_unused", 0, 0, false, false, false, false,
442 handle_warn_unused_attribute
, NULL
},
443 { "returns_nonnull", 0, 0, false, true, true, false,
444 handle_returns_nonnull_attribute
, NULL
},
445 { "omp declare simd", 0, -1, true, false, false, false,
446 handle_omp_declare_simd_attribute
, NULL
},
447 { "omp declare variant base", 0, -1, true, false, false, false,
448 handle_omp_declare_variant_attribute
, NULL
},
449 { "omp declare variant variant", 0, -1, true, false, false, false,
450 handle_omp_declare_variant_attribute
, NULL
},
451 { "simd", 0, 1, true, false, false, false,
452 handle_simd_attribute
, NULL
},
453 { "omp declare target", 0, -1, true, false, false, false,
454 handle_omp_declare_target_attribute
, NULL
},
455 { "omp declare target link", 0, 0, true, false, false, false,
456 handle_omp_declare_target_attribute
, NULL
},
457 { "omp declare target implicit", 0, 0, true, false, false, false,
458 handle_omp_declare_target_attribute
, NULL
},
459 { "omp declare target host", 0, 0, true, false, false, false,
460 handle_omp_declare_target_attribute
, NULL
},
461 { "omp declare target nohost", 0, 0, true, false, false, false,
462 handle_omp_declare_target_attribute
, NULL
},
463 { "omp declare target block", 0, 0, true, false, false, false,
464 handle_omp_declare_target_attribute
, NULL
},
465 { "alloc_align", 1, 1, false, true, true, false,
466 handle_alloc_align_attribute
,
467 attr_alloc_exclusions
},
468 { "assume_aligned", 1, 2, false, true, true, false,
469 handle_assume_aligned_attribute
, NULL
},
470 { "designated_init", 0, 0, false, true, false, false,
471 handle_designated_init_attribute
, NULL
},
472 { "fallthrough", 0, 0, false, false, false, false,
473 handle_fallthrough_attribute
, NULL
},
474 { "patchable_function_entry", 1, 2, true, false, false, false,
475 handle_patchable_function_entry_attribute
,
477 { "nocf_check", 0, 0, false, true, true, true,
478 handle_nocf_check_attribute
, NULL
},
479 { "symver", 1, -1, true, false, false, false,
480 handle_symver_attribute
, NULL
},
481 { "copy", 1, 1, false, false, false, false,
482 handle_copy_attribute
, NULL
},
483 { "noinit", 0, 0, true, false, false, false,
484 handle_noinit_attribute
, attr_noinit_exclusions
},
485 { "access", 1, 3, false, true, true, false,
486 handle_access_attribute
, NULL
},
487 { NULL
, 0, 0, false, false, false, false, NULL
, NULL
}
490 /* Give the specifications for the format attributes, used by C and all
493 Current list of processed format attributes: format, format_arg. */
494 const struct attribute_spec c_common_format_attribute_table
[] =
496 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
497 affects_type_identity, handler, exclude } */
498 { "format", 3, 3, false, true, true, false,
499 handle_format_attribute
, NULL
},
500 { "format_arg", 1, 1, false, true, true, false,
501 handle_format_arg_attribute
, NULL
},
502 { NULL
, 0, 0, false, false, false, false, NULL
, NULL
}
505 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
506 identifier as an argument, so the front end shouldn't look it up. */
509 attribute_takes_identifier_p (const_tree attr_id
)
511 const struct attribute_spec
*spec
= lookup_attribute_spec (attr_id
);
513 /* Unknown attribute that we'll end up ignoring, return true so we
514 don't complain about an identifier argument. */
516 else if (!strcmp ("mode", spec
->name
)
517 || !strcmp ("format", spec
->name
)
518 || !strcmp ("cleanup", spec
->name
)
519 || !strcmp ("access", spec
->name
))
522 return targetm
.attribute_takes_identifier_p (attr_id
);
525 /* Verify that argument value POS at position ARGNO to attribute NAME
526 applied to function TYPE refers to a function parameter at position
527 POS and the expected type CODE. Treat CODE == INTEGER_TYPE as
528 matching all C integral types except bool. If successful, return
529 POS after default conversions, if any. Otherwise, issue appropriate
530 warnings and return null. A non-zero 1-based ARGNO should be passed
531 in by callers only for attributes with more than one argument. */
534 positional_argument (const_tree fntype
, const_tree atname
, tree pos
,
535 tree_code code
, int argno
/* = 0 */,
536 int flags
/* = posargflags () */)
538 if (pos
&& TREE_CODE (pos
) != IDENTIFIER_NODE
539 && TREE_CODE (pos
) != FUNCTION_DECL
)
540 pos
= default_conversion (pos
);
542 tree postype
= TREE_TYPE (pos
);
543 if (pos
== error_mark_node
|| !postype
)
545 /* Only mention the positional argument number when it's non-zero. */
547 warning (OPT_Wattributes
,
548 "%qE attribute argument is invalid", atname
);
550 warning (OPT_Wattributes
,
551 "%qE attribute argument %i is invalid", atname
, argno
);
556 if (!INTEGRAL_TYPE_P (postype
))
558 /* Handle this case specially to avoid mentioning the value
559 of pointer constants in diagnostics. Only mention
560 the positional argument number when it's non-zero. */
562 warning (OPT_Wattributes
,
563 "%qE attribute argument has type %qT",
566 warning (OPT_Wattributes
,
567 "%qE attribute argument %i has type %qT",
568 atname
, argno
, postype
);
573 if (TREE_CODE (pos
) != INTEGER_CST
)
575 /* Only mention the argument number when it's non-zero. */
577 warning (OPT_Wattributes
,
578 "%qE attribute argument value %qE is not an integer "
582 warning (OPT_Wattributes
,
583 "%qE attribute argument %i value %qE is not an integer "
590 /* Argument positions are 1-based. */
591 if (integer_zerop (pos
))
593 if (flags
& POSARG_ZERO
)
594 /* Zero is explicitly allowed. */
598 warning (OPT_Wattributes
,
599 "%qE attribute argument value %qE does not refer to "
600 "a function parameter",
603 warning (OPT_Wattributes
,
604 "%qE attribute argument %i value %qE does not refer to "
605 "a function parameter",
611 if (!prototype_p (fntype
))
614 /* Verify that the argument position does not exceed the number
615 of formal arguments to the function. When POSARG_ELLIPSIS
616 is set, ARGNO may be beyond the last argument of a vararg
618 unsigned nargs
= type_num_arguments (fntype
);
620 || !tree_fits_uhwi_p (pos
)
621 || ((flags
& POSARG_ELLIPSIS
) == 0
622 && !IN_RANGE (tree_to_uhwi (pos
), 1, nargs
)))
626 warning (OPT_Wattributes
,
627 "%qE attribute argument value %qE exceeds the number "
628 "of function parameters %u",
631 warning (OPT_Wattributes
,
632 "%qE attribute argument %i value %qE exceeds the number "
633 "of function parameters %u",
634 atname
, argno
, pos
, nargs
);
638 /* Verify that the type of the referenced formal argument matches
639 the expected type. */
640 unsigned HOST_WIDE_INT ipos
= tree_to_uhwi (pos
);
642 /* Zero was handled above. */
643 gcc_assert (ipos
!= 0);
645 if (tree argtype
= type_argument_type (fntype
, ipos
))
647 if (flags
& POSARG_ELLIPSIS
)
650 error ("%qE attribute argument value %qE does not refer to "
651 "a variable argument list",
654 error ("%qE attribute argument %i value %qE does not refer to "
655 "a variable argument list",
660 /* Where the expected code is STRING_CST accept any pointer
661 expected by attribute format (this includes possibly qualified
662 char pointers and, for targets like Darwin, also pointers to
665 if (code
== STRING_CST
)
666 type_match
= valid_format_string_type_p (argtype
);
667 else if (code
== INTEGER_TYPE
)
668 /* For integers, accept enums, wide characters and other types
669 that match INTEGRAL_TYPE_P except for bool. */
670 type_match
= (INTEGRAL_TYPE_P (argtype
)
671 && TREE_CODE (argtype
) != BOOLEAN_TYPE
);
673 type_match
= TREE_CODE (argtype
) == code
;
677 if (code
== STRING_CST
)
679 /* Reject invalid format strings with an error. */
681 error ("%qE attribute argument value %qE refers to "
682 "parameter type %qT",
683 atname
, pos
, argtype
);
685 error ("%qE attribute argument %i value %qE refers to "
686 "parameter type %qT",
687 atname
, argno
, pos
, argtype
);
693 warning (OPT_Wattributes
,
694 "%qE attribute argument value %qE refers to "
695 "parameter type %qT",
696 atname
, pos
, argtype
);
698 warning (OPT_Wattributes
,
699 "%qE attribute argument %i value %qE refers to "
700 "parameter type %qT",
701 atname
, argno
, pos
, argtype
);
705 else if (!(flags
& POSARG_ELLIPSIS
))
708 warning (OPT_Wattributes
,
709 "%qE attribute argument value %qE refers to "
710 "a variadic function parameter of unknown type",
713 warning (OPT_Wattributes
,
714 "%qE attribute argument %i value %qE refers to "
715 "a variadic function parameter of unknown type",
724 /* Attribute handlers common to C front ends. */
726 /* Handle a "packed" attribute; arguments as in
727 struct attribute_spec.handler. */
730 handle_packed_attribute (tree
*node
, tree name
, tree
ARG_UNUSED (args
),
731 int flags
, bool *no_add_attrs
)
735 if (!(flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
))
737 warning (OPT_Wattributes
,
738 "%qE attribute ignored for type %qT", name
, *node
);
739 *no_add_attrs
= true;
742 TYPE_PACKED (*node
) = 1;
744 else if (TREE_CODE (*node
) == FIELD_DECL
)
746 if (TYPE_ALIGN (TREE_TYPE (*node
)) <= BITS_PER_UNIT
747 /* Still pack bitfields. */
748 && ! DECL_C_BIT_FIELD (*node
))
749 warning (OPT_Wattributes
,
750 "%qE attribute ignored for field of type %qT",
751 name
, TREE_TYPE (*node
));
753 DECL_PACKED (*node
) = 1;
755 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
756 used for DECL_REGISTER. It wouldn't mean anything anyway.
757 We can't set DECL_PACKED on the type of a TYPE_DECL, because
758 that changes what the typedef is typing. */
761 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
762 *no_add_attrs
= true;
768 /* Handle a "nocommon" attribute; arguments as in
769 struct attribute_spec.handler. */
772 handle_nocommon_attribute (tree
*node
, tree name
,
773 tree
ARG_UNUSED (args
),
774 int ARG_UNUSED (flags
), bool *no_add_attrs
)
777 DECL_COMMON (*node
) = 0;
780 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
781 *no_add_attrs
= true;
787 /* Handle a "common" attribute; arguments as in
788 struct attribute_spec.handler. */
791 handle_common_attribute (tree
*node
, tree name
, tree
ARG_UNUSED (args
),
792 int ARG_UNUSED (flags
), bool *no_add_attrs
)
795 DECL_COMMON (*node
) = 1;
798 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
799 *no_add_attrs
= true;
805 /* Handle a "noreturn" attribute; arguments as in
806 struct attribute_spec.handler. */
809 handle_noreturn_attribute (tree
*node
, tree name
, tree
ARG_UNUSED (args
),
810 int ARG_UNUSED (flags
), bool *no_add_attrs
)
812 tree type
= TREE_TYPE (*node
);
814 /* See FIXME comment in c_common_attribute_table. */
815 if (TREE_CODE (*node
) == FUNCTION_DECL
816 || objc_method_decl (TREE_CODE (*node
)))
817 TREE_THIS_VOLATILE (*node
) = 1;
818 else if (TREE_CODE (type
) == POINTER_TYPE
819 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
821 = (build_qualified_type
823 (build_type_variant (TREE_TYPE (type
),
824 TYPE_READONLY (TREE_TYPE (type
)), 1)),
828 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
829 *no_add_attrs
= true;
835 /* Handle a "hot" and attribute; arguments as in
836 struct attribute_spec.handler. */
839 handle_hot_attribute (tree
*node
, tree name
, tree
ARG_UNUSED (args
),
840 int ARG_UNUSED (flags
), bool *no_add_attrs
)
842 if (TREE_CODE (*node
) == FUNCTION_DECL
843 || TREE_CODE (*node
) == LABEL_DECL
)
845 /* Attribute hot processing is done later with lookup_attribute. */
849 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
850 *no_add_attrs
= true;
856 /* Handle a "cold" and attribute; arguments as in
857 struct attribute_spec.handler. */
860 handle_cold_attribute (tree
*node
, tree name
, tree
ARG_UNUSED (args
),
861 int ARG_UNUSED (flags
), bool *no_add_attrs
)
863 if (TREE_CODE (*node
) == FUNCTION_DECL
864 || TREE_CODE (*node
) == LABEL_DECL
)
866 /* Attribute cold processing is done later with lookup_attribute. */
870 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
871 *no_add_attrs
= true;
877 /* Add FLAGS for a function NODE to no_sanitize_flags in DECL_ATTRIBUTES. */
880 add_no_sanitize_value (tree node
, unsigned int flags
)
882 tree attr
= lookup_attribute ("no_sanitize", DECL_ATTRIBUTES (node
));
885 unsigned int old_value
= tree_to_uhwi (TREE_VALUE (attr
));
888 if (flags
== old_value
)
891 TREE_VALUE (attr
) = build_int_cst (unsigned_type_node
, flags
);
894 DECL_ATTRIBUTES (node
)
895 = tree_cons (get_identifier ("no_sanitize"),
896 build_int_cst (unsigned_type_node
, flags
),
897 DECL_ATTRIBUTES (node
));
900 /* Handle a "no_sanitize" attribute; arguments as in
901 struct attribute_spec.handler. */
904 handle_no_sanitize_attribute (tree
*node
, tree name
, tree args
, int,
907 unsigned int flags
= 0;
908 *no_add_attrs
= true;
909 if (TREE_CODE (*node
) != FUNCTION_DECL
)
911 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
915 for (; args
; args
= TREE_CHAIN (args
))
917 tree id
= TREE_VALUE (args
);
918 if (TREE_CODE (id
) != STRING_CST
)
920 error ("%qE argument not a string", name
);
924 char *string
= ASTRDUP (TREE_STRING_POINTER (id
));
925 flags
|= parse_no_sanitize_attribute (string
);
928 add_no_sanitize_value (*node
, flags
);
933 /* Handle a "no_sanitize_address" attribute; arguments as in
934 struct attribute_spec.handler. */
937 handle_no_sanitize_address_attribute (tree
*node
, tree name
, tree
, int,
940 *no_add_attrs
= true;
941 if (TREE_CODE (*node
) != FUNCTION_DECL
)
942 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
944 add_no_sanitize_value (*node
, SANITIZE_ADDRESS
);
949 /* Handle a "no_sanitize_thread" attribute; arguments as in
950 struct attribute_spec.handler. */
953 handle_no_sanitize_thread_attribute (tree
*node
, tree name
, tree
, int,
956 *no_add_attrs
= true;
957 if (TREE_CODE (*node
) != FUNCTION_DECL
)
958 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
960 add_no_sanitize_value (*node
, SANITIZE_THREAD
);
966 /* Handle a "no_address_safety_analysis" attribute; arguments as in
967 struct attribute_spec.handler. */
970 handle_no_address_safety_analysis_attribute (tree
*node
, tree name
, tree
, int,
973 *no_add_attrs
= true;
974 if (TREE_CODE (*node
) != FUNCTION_DECL
)
975 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
977 add_no_sanitize_value (*node
, SANITIZE_ADDRESS
);
982 /* Handle a "no_sanitize_undefined" attribute; arguments as in
983 struct attribute_spec.handler. */
986 handle_no_sanitize_undefined_attribute (tree
*node
, tree name
, tree
, int,
989 *no_add_attrs
= true;
990 if (TREE_CODE (*node
) != FUNCTION_DECL
)
991 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
993 add_no_sanitize_value (*node
,
994 SANITIZE_UNDEFINED
| SANITIZE_UNDEFINED_NONDEFAULT
);
999 /* Handle an "asan odr indicator" attribute; arguments as in
1000 struct attribute_spec.handler. */
1003 handle_asan_odr_indicator_attribute (tree
*, tree
, tree
, int, bool *)
1008 /* Handle a "stack_protect" attribute; arguments as in
1009 struct attribute_spec.handler. */
1012 handle_stack_protect_attribute (tree
*node
, tree name
, tree
, int,
1015 if (TREE_CODE (*node
) != FUNCTION_DECL
)
1017 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
1018 *no_add_attrs
= true;
1024 /* Handle a "noipa" attribute; arguments as in
1025 struct attribute_spec.handler. */
1028 handle_noipa_attribute (tree
*node
, tree name
, tree
, int, bool *no_add_attrs
)
1030 if (TREE_CODE (*node
) != FUNCTION_DECL
)
1032 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
1033 *no_add_attrs
= true;
1039 /* Handle a "noinline" attribute; arguments as in
1040 struct attribute_spec.handler. */
1043 handle_noinline_attribute (tree
*node
, tree name
,
1044 tree
ARG_UNUSED (args
),
1045 int ARG_UNUSED (flags
), bool *no_add_attrs
)
1047 if (TREE_CODE (*node
) == FUNCTION_DECL
)
1049 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node
)))
1051 warning (OPT_Wattributes
, "%qE attribute ignored due to conflict "
1052 "with attribute %qs", name
, "always_inline");
1053 *no_add_attrs
= true;
1056 DECL_UNINLINABLE (*node
) = 1;
1060 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
1061 *no_add_attrs
= true;
1067 /* Handle a "noclone" attribute; arguments as in
1068 struct attribute_spec.handler. */
1071 handle_noclone_attribute (tree
*node
, tree name
,
1072 tree
ARG_UNUSED (args
),
1073 int ARG_UNUSED (flags
), bool *no_add_attrs
)
1075 if (TREE_CODE (*node
) != FUNCTION_DECL
)
1077 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
1078 *no_add_attrs
= true;
1084 /* Handle a "nocf_check" attribute; arguments as in
1085 struct attribute_spec.handler. */
1088 handle_nocf_check_attribute (tree
*node
, tree name
,
1089 tree
ARG_UNUSED (args
),
1090 int ARG_UNUSED (flags
), bool *no_add_attrs
)
1092 if (TREE_CODE (*node
) != FUNCTION_TYPE
1093 && TREE_CODE (*node
) != METHOD_TYPE
)
1095 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
1096 *no_add_attrs
= true;
1098 else if (!(flag_cf_protection
& CF_BRANCH
))
1100 warning (OPT_Wattributes
, "%qE attribute ignored. Use "
1101 "%<-fcf-protection%> option to enable it",
1103 *no_add_attrs
= true;
1109 /* Handle a "no_icf" attribute; arguments as in
1110 struct attribute_spec.handler. */
1113 handle_noicf_attribute (tree
*node
, tree name
,
1114 tree
ARG_UNUSED (args
),
1115 int ARG_UNUSED (flags
), bool *no_add_attrs
)
1117 if (TREE_CODE (*node
) != FUNCTION_DECL
)
1119 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
1120 *no_add_attrs
= true;
1127 /* Handle a "always_inline" attribute; arguments as in
1128 struct attribute_spec.handler. */
1131 handle_always_inline_attribute (tree
*node
, tree name
,
1132 tree
ARG_UNUSED (args
),
1133 int ARG_UNUSED (flags
),
1136 if (TREE_CODE (*node
) == FUNCTION_DECL
)
1138 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node
)))
1140 warning (OPT_Wattributes
, "%qE attribute ignored due to conflict "
1141 "with %qs attribute", name
, "noinline");
1142 *no_add_attrs
= true;
1144 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node
)))
1146 warning (OPT_Wattributes
, "%qE attribute ignored due to conflict "
1147 "with %qs attribute", name
, "target_clones");
1148 *no_add_attrs
= true;
1151 /* Set the attribute and mark it for disregarding inline
1153 DECL_DISREGARD_INLINE_LIMITS (*node
) = 1;
1157 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
1158 *no_add_attrs
= true;
1164 /* Handle a "gnu_inline" attribute; arguments as in
1165 struct attribute_spec.handler. */
1168 handle_gnu_inline_attribute (tree
*node
, tree name
,
1169 tree
ARG_UNUSED (args
),
1170 int ARG_UNUSED (flags
),
1173 if (TREE_CODE (*node
) == FUNCTION_DECL
&& DECL_DECLARED_INLINE_P (*node
))
1175 /* Do nothing else, just set the attribute. We'll get at
1176 it later with lookup_attribute. */
1180 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
1181 *no_add_attrs
= true;
1187 /* Handle a "leaf" attribute; arguments as in
1188 struct attribute_spec.handler. */
1191 handle_leaf_attribute (tree
*node
, tree name
,
1192 tree
ARG_UNUSED (args
),
1193 int ARG_UNUSED (flags
), bool *no_add_attrs
)
1195 if (TREE_CODE (*node
) != FUNCTION_DECL
)
1197 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
1198 *no_add_attrs
= true;
1200 if (!TREE_PUBLIC (*node
))
1202 warning (OPT_Wattributes
, "%qE attribute has no effect on unit local "
1204 *no_add_attrs
= true;
1210 /* Handle an "artificial" attribute; arguments as in
1211 struct attribute_spec.handler. */
1214 handle_artificial_attribute (tree
*node
, tree name
,
1215 tree
ARG_UNUSED (args
),
1216 int ARG_UNUSED (flags
),
1219 if (TREE_CODE (*node
) == FUNCTION_DECL
&& DECL_DECLARED_INLINE_P (*node
))
1221 /* Do nothing else, just set the attribute. We'll get at
1222 it later with lookup_attribute. */
1226 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
1227 *no_add_attrs
= true;
1233 /* Handle a "flatten" attribute; arguments as in
1234 struct attribute_spec.handler. */
1237 handle_flatten_attribute (tree
*node
, tree name
,
1238 tree args ATTRIBUTE_UNUSED
,
1239 int flags ATTRIBUTE_UNUSED
, bool *no_add_attrs
)
1241 if (TREE_CODE (*node
) == FUNCTION_DECL
)
1242 /* Do nothing else, just set the attribute. We'll get at
1243 it later with lookup_attribute. */
1247 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
1248 *no_add_attrs
= true;
1254 /* Handle a "warning" or "error" attribute; arguments as in
1255 struct attribute_spec.handler. */
1258 handle_error_attribute (tree
*node
, tree name
, tree args
,
1259 int ARG_UNUSED (flags
), bool *no_add_attrs
)
1261 if (TREE_CODE (*node
) == FUNCTION_DECL
1262 && TREE_CODE (TREE_VALUE (args
)) == STRING_CST
)
1263 /* Do nothing else, just set the attribute. We'll get at
1264 it later with lookup_attribute. */
1268 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
1269 *no_add_attrs
= true;
1275 /* Handle a "used" attribute; arguments as in
1276 struct attribute_spec.handler. */
1279 handle_used_attribute (tree
*pnode
, tree name
, tree
ARG_UNUSED (args
),
1280 int ARG_UNUSED (flags
), bool *no_add_attrs
)
1284 if (TREE_CODE (node
) == FUNCTION_DECL
1285 || (VAR_P (node
) && TREE_STATIC (node
))
1286 || (TREE_CODE (node
) == TYPE_DECL
))
1288 TREE_USED (node
) = 1;
1289 DECL_PRESERVE_P (node
) = 1;
1291 DECL_READ_P (node
) = 1;
1295 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
1296 *no_add_attrs
= true;
1302 /* Handle a "unused" attribute; arguments as in
1303 struct attribute_spec.handler. */
1306 handle_unused_attribute (tree
*node
, tree name
, tree
ARG_UNUSED (args
),
1307 int flags
, bool *no_add_attrs
)
1313 if (TREE_CODE (decl
) == PARM_DECL
1314 || VAR_OR_FUNCTION_DECL_P (decl
)
1315 || TREE_CODE (decl
) == LABEL_DECL
1316 || TREE_CODE (decl
) == CONST_DECL
1317 || TREE_CODE (decl
) == TYPE_DECL
)
1319 TREE_USED (decl
) = 1;
1320 if (VAR_P (decl
) || TREE_CODE (decl
) == PARM_DECL
)
1321 DECL_READ_P (decl
) = 1;
1325 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
1326 *no_add_attrs
= true;
1331 if (!(flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
))
1332 *node
= build_variant_type_copy (*node
);
1333 TREE_USED (*node
) = 1;
1339 /* Handle a "externally_visible" attribute; arguments as in
1340 struct attribute_spec.handler. */
1343 handle_externally_visible_attribute (tree
*pnode
, tree name
,
1344 tree
ARG_UNUSED (args
),
1345 int ARG_UNUSED (flags
),
1350 if (VAR_OR_FUNCTION_DECL_P (node
))
1352 if ((!TREE_STATIC (node
) && TREE_CODE (node
) != FUNCTION_DECL
1353 && !DECL_EXTERNAL (node
)) || !TREE_PUBLIC (node
))
1355 warning (OPT_Wattributes
,
1356 "%qE attribute have effect only on public objects", name
);
1357 *no_add_attrs
= true;
1362 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
1363 *no_add_attrs
= true;
1369 /* Handle the "no_reorder" attribute. Arguments as in
1370 struct attribute_spec.handler. */
1373 handle_no_reorder_attribute (tree
*pnode
,
1381 if (!VAR_OR_FUNCTION_DECL_P (node
)
1382 && !(TREE_STATIC (node
) || DECL_EXTERNAL (node
)))
1384 warning (OPT_Wattributes
,
1385 "%qE attribute only affects top level objects",
1387 *no_add_attrs
= true;
1393 /* Handle a "const" attribute; arguments as in
1394 struct attribute_spec.handler. */
1397 handle_const_attribute (tree
*node
, tree name
, tree
ARG_UNUSED (args
),
1398 int flags
, bool *no_add_attrs
)
1400 tree type
= TREE_TYPE (*node
);
1402 /* See FIXME comment on noreturn in c_common_attribute_table. */
1403 if (TREE_CODE (*node
) == FUNCTION_DECL
)
1404 TREE_READONLY (*node
) = 1;
1405 else if (TREE_CODE (type
) == POINTER_TYPE
1406 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
1408 = (build_qualified_type
1410 (build_type_variant (TREE_TYPE (type
), 1,
1411 TREE_THIS_VOLATILE (TREE_TYPE (type
)))),
1412 TYPE_QUALS (type
)));
1415 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
1416 *no_add_attrs
= true;
1419 /* void __builtin_unreachable(void) is const. Accept other such
1420 built-ins but warn on user-defined functions that return void. */
1421 if (!(flags
& ATTR_FLAG_BUILT_IN
)
1422 && TREE_CODE (*node
) == FUNCTION_DECL
1423 && VOID_TYPE_P (TREE_TYPE (type
)))
1424 warning (OPT_Wattributes
, "%qE attribute on function "
1425 "returning %<void%>", name
);
1430 /* Handle a "scalar_storage_order" attribute; arguments as in
1431 struct attribute_spec.handler. */
1434 handle_scalar_storage_order_attribute (tree
*node
, tree name
, tree args
,
1435 int flags
, bool *no_add_attrs
)
1437 tree id
= TREE_VALUE (args
);
1440 if (TREE_CODE (*node
) == TYPE_DECL
1441 && ! (flags
& ATTR_FLAG_CXX11
))
1442 node
= &TREE_TYPE (*node
);
1445 if (BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
)
1447 error ("%qE attribute is not supported because endianness is not uniform",
1452 if (RECORD_OR_UNION_TYPE_P (type
) && !c_dialect_cxx ())
1454 bool reverse
= false;
1456 if (TREE_CODE (id
) == STRING_CST
1457 && strcmp (TREE_STRING_POINTER (id
), "big-endian") == 0)
1458 reverse
= !BYTES_BIG_ENDIAN
;
1459 else if (TREE_CODE (id
) == STRING_CST
1460 && strcmp (TREE_STRING_POINTER (id
), "little-endian") == 0)
1461 reverse
= BYTES_BIG_ENDIAN
;
1464 error ("attribute %qE argument must be one of %qs or %qs",
1465 name
, "big-endian", "little-endian");
1469 if (!(flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
))
1472 /* A type variant isn't good enough, since we don't want a cast
1473 to such a type to be removed as a no-op. */
1474 *node
= type
= build_duplicate_type (type
);
1477 TYPE_REVERSE_STORAGE_ORDER (type
) = reverse
;
1481 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
1482 *no_add_attrs
= true;
1486 /* Handle a "transparent_union" attribute; arguments as in
1487 struct attribute_spec.handler. */
1490 handle_transparent_union_attribute (tree
*node
, tree name
,
1491 tree
ARG_UNUSED (args
), int flags
,
1496 *no_add_attrs
= true;
1498 if (TREE_CODE (*node
) == TYPE_DECL
1499 && ! (flags
& ATTR_FLAG_CXX11
))
1500 node
= &TREE_TYPE (*node
);
1503 if (TREE_CODE (type
) == UNION_TYPE
)
1505 /* Make sure that the first field will work for a transparent union.
1506 If the type isn't complete yet, leave the check to the code in
1508 if (TYPE_SIZE (type
))
1510 tree first
= first_field (type
);
1511 if (first
== NULL_TREE
1512 || DECL_ARTIFICIAL (first
)
1513 || TYPE_MODE (type
) != DECL_MODE (first
))
1517 if (!(flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
))
1519 /* If the type isn't complete yet, setting the flag
1520 on a variant wouldn't ever be checked. */
1521 if (!TYPE_SIZE (type
))
1524 /* build_duplicate_type doesn't work for C++. */
1525 if (c_dialect_cxx ())
1528 /* A type variant isn't good enough, since we don't want a cast
1529 to such a type to be removed as a no-op. */
1530 *node
= type
= build_duplicate_type (type
);
1533 for (tree t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
1534 TYPE_TRANSPARENT_AGGR (t
) = 1;
1539 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
1543 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
1544 get the requested priority for a constructor or destructor,
1545 possibly issuing diagnostics for invalid or reserved
1548 static priority_type
1549 get_priority (tree args
, bool is_destructor
)
1555 return DEFAULT_INIT_PRIORITY
;
1557 if (!SUPPORTS_INIT_PRIORITY
)
1560 error ("destructor priorities are not supported");
1562 error ("constructor priorities are not supported");
1563 return DEFAULT_INIT_PRIORITY
;
1566 arg
= TREE_VALUE (args
);
1567 if (TREE_CODE (arg
) == IDENTIFIER_NODE
)
1569 if (arg
== error_mark_node
)
1570 return DEFAULT_INIT_PRIORITY
;
1571 arg
= default_conversion (arg
);
1572 if (!tree_fits_shwi_p (arg
)
1573 || !INTEGRAL_TYPE_P (TREE_TYPE (arg
)))
1576 pri
= tree_to_shwi (arg
);
1577 if (pri
< 0 || pri
> MAX_INIT_PRIORITY
)
1580 if (pri
<= MAX_RESERVED_INIT_PRIORITY
)
1583 warning (OPT_Wprio_ctor_dtor
,
1584 "destructor priorities from 0 to %d are reserved "
1585 "for the implementation",
1586 MAX_RESERVED_INIT_PRIORITY
);
1588 warning (OPT_Wprio_ctor_dtor
,
1589 "constructor priorities from 0 to %d are reserved "
1590 "for the implementation",
1591 MAX_RESERVED_INIT_PRIORITY
);
1597 error ("destructor priorities must be integers from 0 to %d inclusive",
1600 error ("constructor priorities must be integers from 0 to %d inclusive",
1602 return DEFAULT_INIT_PRIORITY
;
1605 /* Handle a "constructor" attribute; arguments as in
1606 struct attribute_spec.handler. */
1609 handle_constructor_attribute (tree
*node
, tree name
, tree args
,
1610 int ARG_UNUSED (flags
),
1614 tree type
= TREE_TYPE (decl
);
1616 if (TREE_CODE (decl
) == FUNCTION_DECL
1617 && TREE_CODE (type
) == FUNCTION_TYPE
1618 && decl_function_context (decl
) == 0)
1620 priority_type priority
;
1621 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
1622 priority
= get_priority (args
, /*is_destructor=*/false);
1623 SET_DECL_INIT_PRIORITY (decl
, priority
);
1624 TREE_USED (decl
) = 1;
1628 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
1629 *no_add_attrs
= true;
1635 /* Handle a "destructor" attribute; arguments as in
1636 struct attribute_spec.handler. */
1639 handle_destructor_attribute (tree
*node
, tree name
, tree args
,
1640 int ARG_UNUSED (flags
),
1644 tree type
= TREE_TYPE (decl
);
1646 if (TREE_CODE (decl
) == FUNCTION_DECL
1647 && TREE_CODE (type
) == FUNCTION_TYPE
1648 && decl_function_context (decl
) == 0)
1650 priority_type priority
;
1651 DECL_STATIC_DESTRUCTOR (decl
) = 1;
1652 priority
= get_priority (args
, /*is_destructor=*/true);
1653 SET_DECL_FINI_PRIORITY (decl
, priority
);
1654 TREE_USED (decl
) = 1;
1658 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
1659 *no_add_attrs
= true;
1665 /* Nonzero if the mode is a valid vector mode for this architecture.
1666 This returns nonzero even if there is no hardware support for the
1667 vector mode, but we can emulate with narrower modes. */
1670 vector_mode_valid_p (machine_mode mode
)
1672 enum mode_class mclass
= GET_MODE_CLASS (mode
);
1674 /* Doh! What's going on? */
1675 if (mclass
!= MODE_VECTOR_INT
1676 && mclass
!= MODE_VECTOR_FLOAT
1677 && mclass
!= MODE_VECTOR_FRACT
1678 && mclass
!= MODE_VECTOR_UFRACT
1679 && mclass
!= MODE_VECTOR_ACCUM
1680 && mclass
!= MODE_VECTOR_UACCUM
)
1683 /* Hardware support. Woo hoo! */
1684 if (targetm
.vector_mode_supported_p (mode
))
1687 /* We should probably return 1 if requesting V4DI and we have no DI,
1688 but we have V2DI, but this is probably very unlikely. */
1690 /* If we have support for the inner mode, we can safely emulate it.
1691 We may not have V2DI, but me can emulate with a pair of DIs. */
1692 return targetm
.scalar_mode_supported_p (GET_MODE_INNER (mode
));
1696 /* Handle a "mode" attribute; arguments as in
1697 struct attribute_spec.handler. */
1700 handle_mode_attribute (tree
*node
, tree name
, tree args
,
1701 int ARG_UNUSED (flags
), bool *no_add_attrs
)
1704 tree ident
= TREE_VALUE (args
);
1706 *no_add_attrs
= true;
1708 if (TREE_CODE (ident
) != IDENTIFIER_NODE
)
1709 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
1713 const char *p
= IDENTIFIER_POINTER (ident
);
1714 int len
= strlen (p
);
1715 machine_mode mode
= VOIDmode
;
1719 if (len
> 4 && p
[0] == '_' && p
[1] == '_'
1720 && p
[len
- 1] == '_' && p
[len
- 2] == '_')
1722 char *newp
= (char *) alloca (len
- 1);
1724 strcpy (newp
, &p
[2]);
1725 newp
[len
- 4] = '\0';
1729 /* Change this type to have a type with the specified mode.
1730 First check for the special modes. */
1731 if (!strcmp (p
, "byte"))
1733 else if (!strcmp (p
, "word"))
1735 else if (!strcmp (p
, "pointer"))
1737 else if (!strcmp (p
, "libgcc_cmp_return"))
1738 mode
= targetm
.libgcc_cmp_return_mode ();
1739 else if (!strcmp (p
, "libgcc_shift_count"))
1740 mode
= targetm
.libgcc_shift_count_mode ();
1741 else if (!strcmp (p
, "unwind_word"))
1742 mode
= targetm
.unwind_word_mode ();
1744 for (j
= 0; j
< NUM_MACHINE_MODES
; j
++)
1745 if (!strcmp (p
, GET_MODE_NAME (j
)))
1747 mode
= (machine_mode
) j
;
1751 if (mode
== VOIDmode
)
1753 error ("unknown machine mode %qE", ident
);
1757 /* Allow the target a chance to translate MODE into something supported.
1759 mode
= targetm
.translate_mode_attribute (mode
);
1762 switch (GET_MODE_CLASS (mode
))
1765 case MODE_PARTIAL_INT
:
1767 case MODE_DECIMAL_FLOAT
:
1773 = targetm
.scalar_mode_supported_p (as_a
<scalar_mode
> (mode
));
1776 case MODE_COMPLEX_INT
:
1777 case MODE_COMPLEX_FLOAT
:
1778 valid_mode
= targetm
.scalar_mode_supported_p (GET_MODE_INNER (mode
));
1781 case MODE_VECTOR_INT
:
1782 case MODE_VECTOR_FLOAT
:
1783 case MODE_VECTOR_FRACT
:
1784 case MODE_VECTOR_UFRACT
:
1785 case MODE_VECTOR_ACCUM
:
1786 case MODE_VECTOR_UACCUM
:
1787 warning (OPT_Wattributes
, "specifying vector types with "
1788 "%<__attribute__ ((mode))%> is deprecated");
1789 inform (input_location
,
1790 "use %<__attribute__ ((vector_size))%> instead");
1791 valid_mode
= vector_mode_valid_p (mode
);
1799 error ("unable to emulate %qs", p
);
1803 if (POINTER_TYPE_P (type
))
1805 scalar_int_mode addr_mode
;
1806 addr_space_t as
= TYPE_ADDR_SPACE (TREE_TYPE (type
));
1807 tree (*fn
)(tree
, machine_mode
, bool);
1809 if (!is_a
<scalar_int_mode
> (mode
, &addr_mode
)
1810 || !targetm
.addr_space
.valid_pointer_mode (addr_mode
, as
))
1812 error ("invalid pointer mode %qs", p
);
1816 if (TREE_CODE (type
) == POINTER_TYPE
)
1817 fn
= build_pointer_type_for_mode
;
1819 fn
= build_reference_type_for_mode
;
1820 typefm
= fn (TREE_TYPE (type
), addr_mode
, false);
1824 /* For fixed-point modes, we need to test if the signness of type
1825 and the machine mode are consistent. */
1826 if (ALL_FIXED_POINT_MODE_P (mode
)
1827 && TYPE_UNSIGNED (type
) != UNSIGNED_FIXED_POINT_MODE_P (mode
))
1829 error ("signedness of type and machine mode %qs don%'t match", p
);
1832 /* For fixed-point modes, we need to pass saturating info. */
1833 typefm
= lang_hooks
.types
.type_for_mode (mode
,
1834 ALL_FIXED_POINT_MODE_P (mode
) ? TYPE_SATURATING (type
)
1835 : TYPE_UNSIGNED (type
));
1838 if (typefm
== NULL_TREE
)
1840 error ("no data type for mode %qs", p
);
1843 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
1845 /* For enumeral types, copy the precision from the integer
1846 type returned above. If not an INTEGER_TYPE, we can't use
1847 this mode for this type. */
1848 if (TREE_CODE (typefm
) != INTEGER_TYPE
)
1850 error ("cannot use mode %qs for enumerated types", p
);
1854 if (flags
& ATTR_FLAG_TYPE_IN_PLACE
)
1856 TYPE_PRECISION (type
) = TYPE_PRECISION (typefm
);
1861 /* We cannot build a type variant, as there's code that assumes
1862 that TYPE_MAIN_VARIANT has the same mode. This includes the
1863 debug generators. Instead, create a subrange type. This
1864 results in all of the enumeral values being emitted only once
1865 in the original, and the subtype gets them by reference. */
1866 if (TYPE_UNSIGNED (type
))
1867 typefm
= make_unsigned_type (TYPE_PRECISION (typefm
));
1869 typefm
= make_signed_type (TYPE_PRECISION (typefm
));
1870 TREE_TYPE (typefm
) = type
;
1872 *no_add_attrs
= false;
1874 else if (VECTOR_MODE_P (mode
)
1875 ? TREE_CODE (type
) != TREE_CODE (TREE_TYPE (typefm
))
1876 : TREE_CODE (type
) != TREE_CODE (typefm
))
1878 error ("mode %qs applied to inappropriate type", p
);
1882 *node
= build_qualified_type (typefm
, TYPE_QUALS (type
));
1888 /* Handle a "section" attribute; arguments as in
1889 struct attribute_spec.handler. */
1892 handle_section_attribute (tree
*node
, tree
ARG_UNUSED (name
), tree args
,
1893 int ARG_UNUSED (flags
), bool *no_add_attrs
)
1896 tree res
= NULL_TREE
;
1898 if (!targetm_common
.have_named_sections
)
1900 error_at (DECL_SOURCE_LOCATION (*node
),
1901 "section attributes are not supported for this target");
1905 if (!VAR_OR_FUNCTION_DECL_P (decl
))
1907 error ("section attribute not allowed for %q+D", *node
);
1911 if (TREE_CODE (TREE_VALUE (args
)) != STRING_CST
)
1913 error ("section attribute argument not a string constant");
1918 && current_function_decl
!= NULL_TREE
1919 && !TREE_STATIC (decl
))
1921 error_at (DECL_SOURCE_LOCATION (decl
),
1922 "section attribute cannot be specified for local variables");
1926 /* The decl may have already been given a section attribute
1927 from a previous declaration. Ensure they match. */
1928 if (DECL_SECTION_NAME (decl
) != NULL
1929 && strcmp (DECL_SECTION_NAME (decl
),
1930 TREE_STRING_POINTER (TREE_VALUE (args
))) != 0)
1932 error ("section of %q+D conflicts with previous declaration", *node
);
1937 && !targetm
.have_tls
&& targetm
.emutls
.tmpl_section
1938 && DECL_THREAD_LOCAL_P (decl
))
1940 error ("section of %q+D cannot be overridden", *node
);
1944 res
= targetm
.handle_generic_attribute (node
, name
, args
, flags
,
1947 /* If the back end confirms the attribute can be added then continue onto
1948 final processing. */
1949 if (!(*no_add_attrs
))
1951 set_decl_section_name (decl
, TREE_STRING_POINTER (TREE_VALUE (args
)));
1956 *no_add_attrs
= true;
1960 /* If in c++-11, check if the c++-11 alignment constraint with respect
1961 to fundamental alignment (in [dcl.align]) are satisfied. If not in
1962 c++-11 mode, does nothing.
1966 [* if the constant expression evaluates to a fundamental alignment,
1967 the alignment requirement of the declared entity shall be the
1968 specified fundamental alignment.
1970 * if the constant expression evaluates to an extended alignment
1971 and the implementation supports that alignment in the context
1972 of the declaration, the alignment of the declared entity shall
1975 * if the constant expression evaluates to an extended alignment
1976 and the implementation does not support that alignment in the
1977 context of the declaration, the program is ill-formed]. */
1980 check_cxx_fundamental_alignment_constraints (tree node
,
1984 bool alignment_too_large_p
= false;
1985 unsigned requested_alignment
= (1U << align_log
) * BITS_PER_UNIT
;
1986 unsigned max_align
= 0;
1988 if ((!(flags
& ATTR_FLAG_CXX11
) && !warn_cxx_compat
)
1989 || (node
== NULL_TREE
|| node
== error_mark_node
))
1992 if (cxx_fundamental_alignment_p (requested_alignment
))
1997 if (TREE_STATIC (node
) || DECL_EXTERNAL (node
))
1998 /* For file scope variables and static members, the target supports
1999 alignments that are at most MAX_OFILE_ALIGNMENT. */
2000 max_align
= MAX_OFILE_ALIGNMENT
;
2002 /* For stack variables, the target supports at most
2003 MAX_STACK_ALIGNMENT. */
2004 max_align
= MAX_STACK_ALIGNMENT
;
2005 if (requested_alignment
> max_align
)
2006 alignment_too_large_p
= true;
2008 /* Let's be liberal for types and fields; don't limit their alignment any
2009 more than check_user_alignment already did. */
2011 if (alignment_too_large_p
)
2012 pedwarn (input_location
, OPT_Wattributes
,
2013 "requested alignment %d is larger than %d",
2014 requested_alignment
/ BITS_PER_UNIT
, max_align
/ BITS_PER_UNIT
);
2016 return !alignment_too_large_p
;
2019 /* Common codes shared by handle_warn_if_not_aligned_attribute and
2020 handle_aligned_attribute. */
2023 common_handle_aligned_attribute (tree
*node
, tree name
, tree args
, int flags
,
2025 bool warn_if_not_aligned_p
)
2027 tree decl
= NULL_TREE
;
2029 bool is_type
= false;
2032 /* The last (already pushed) declaration with all validated attributes
2033 merged in or the current about-to-be-pushed one if one hasn't been
2035 tree last_decl
= node
[1] ? node
[1] : *node
;
2039 align_expr
= TREE_VALUE (args
);
2040 if (align_expr
&& TREE_CODE (align_expr
) != IDENTIFIER_NODE
2041 && TREE_CODE (align_expr
) != FUNCTION_DECL
)
2042 align_expr
= default_conversion (align_expr
);
2045 align_expr
= size_int (ATTRIBUTE_ALIGNED_VALUE
/ BITS_PER_UNIT
);
2050 type
= &TREE_TYPE (decl
);
2051 is_type
= TREE_CODE (*node
) == TYPE_DECL
;
2053 else if (TYPE_P (*node
))
2054 type
= node
, is_type
= true;
2056 /* True to consider invalid alignments greater than MAX_OFILE_ALIGNMENT. */
2057 bool objfile
= (TREE_CODE (*node
) == FUNCTION_DECL
2058 || (VAR_P (*node
) && TREE_STATIC (*node
)));
2059 /* Log2 of specified alignment. */
2060 int pow2align
= check_user_alignment (align_expr
, objfile
,
2061 /* warn_zero = */ true);
2063 || !check_cxx_fundamental_alignment_constraints (*node
, pow2align
, flags
))
2065 *no_add_attrs
= true;
2069 /* The alignment in bits corresponding to the specified alignment. */
2070 unsigned bitalign
= (1U << pow2align
) * BITS_PER_UNIT
;
2072 /* The alignment of the current declaration and that of the last
2073 pushed declaration, determined on demand below. */
2074 unsigned curalign
= 0;
2075 unsigned lastalign
= 0;
2077 /* True when SET_DECL_ALIGN() should be called for the decl when
2078 *NO_ADD_ATTRS is false. */
2079 bool set_align
= true;
2082 if ((flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
))
2083 /* OK, modify the type in place. */;
2084 /* If we have a TYPE_DECL, then copy the type, so that we
2085 don't accidentally modify a builtin type. See pushdecl. */
2086 else if (decl
&& TREE_TYPE (decl
) != error_mark_node
2087 && DECL_ORIGINAL_TYPE (decl
) == NULL_TREE
)
2089 tree tt
= TREE_TYPE (decl
);
2090 *type
= build_variant_type_copy (*type
);
2091 DECL_ORIGINAL_TYPE (decl
) = tt
;
2092 TYPE_NAME (*type
) = decl
;
2093 TREE_USED (*type
) = TREE_USED (decl
);
2094 TREE_TYPE (decl
) = *type
;
2097 *type
= build_variant_type_copy (*type
);
2099 if (warn_if_not_aligned_p
)
2101 SET_TYPE_WARN_IF_NOT_ALIGN (*type
, bitalign
);
2102 warn_if_not_aligned_p
= false;
2106 SET_TYPE_ALIGN (*type
, bitalign
);
2107 TYPE_USER_ALIGN (*type
) = 1;
2110 else if (! VAR_OR_FUNCTION_DECL_P (decl
)
2111 && TREE_CODE (decl
) != FIELD_DECL
)
2113 error ("alignment may not be specified for %q+D", decl
);
2114 *no_add_attrs
= true;
2116 else if (TREE_CODE (decl
) == FUNCTION_DECL
2117 && ((curalign
= DECL_ALIGN (decl
)) > bitalign
2118 || ((lastalign
= DECL_ALIGN (last_decl
)) > bitalign
)))
2120 /* Either a prior attribute on the same declaration or one
2121 on a prior declaration of the same function specifies
2122 stricter alignment than this attribute. */
2123 bool note
= lastalign
!= 0;
2125 curalign
= lastalign
;
2127 curalign
/= BITS_PER_UNIT
;
2128 unsigned newalign
= bitalign
/ BITS_PER_UNIT
;
2130 auto_diagnostic_group d
;
2131 if ((DECL_USER_ALIGN (decl
)
2132 || DECL_USER_ALIGN (last_decl
)))
2134 if (warning (OPT_Wattributes
,
2135 "ignoring attribute %<%E (%u)%> because it conflicts "
2136 "with attribute %<%E (%u)%>",
2137 name
, newalign
, name
, curalign
)
2139 inform (DECL_SOURCE_LOCATION (last_decl
),
2140 "previous declaration here");
2141 /* Only reject attempts to relax/override an alignment
2142 explicitly specified previously and accept declarations
2143 that appear to relax the implicit function alignment for
2144 the target. Both increasing and increasing the alignment
2145 set by -falign-functions setting is permitted. */
2146 *no_add_attrs
= true;
2148 else if (!warn_if_not_aligned_p
)
2150 /* Do not fail for attribute warn_if_not_aligned. Otherwise,
2151 silently avoid applying the alignment to the declaration
2152 because it's implicitly satisfied by the target. Apply
2153 the attribute nevertheless so it can be retrieved by
2154 __builtin_has_attribute. */
2158 else if (DECL_USER_ALIGN (decl
)
2159 && DECL_ALIGN (decl
) > bitalign
)
2160 /* C++-11 [dcl.align/4]:
2162 When multiple alignment-specifiers are specified for an
2163 entity, the alignment requirement shall be set to the
2164 strictest specified alignment.
2166 This formally comes from the c++11 specification but we are
2167 doing it for the GNU attribute syntax as well. */
2168 *no_add_attrs
= true;
2169 else if (!warn_if_not_aligned_p
2170 && TREE_CODE (decl
) == FUNCTION_DECL
2171 && DECL_ALIGN (decl
) > bitalign
)
2173 /* Don't warn for function alignment here if warn_if_not_aligned_p
2174 is true. It will be warned about later. */
2175 if (DECL_USER_ALIGN (decl
))
2177 /* Only reject attempts to relax/override an alignment
2178 explicitly specified previously and accept declarations
2179 that appear to relax the implicit function alignment for
2180 the target. Both increasing and increasing the alignment
2181 set by -falign-functions setting is permitted. */
2182 error ("alignment for %q+D was previously specified as %d "
2183 "and may not be decreased", decl
,
2184 DECL_ALIGN (decl
) / BITS_PER_UNIT
);
2185 *no_add_attrs
= true;
2188 else if (warn_if_not_aligned_p
2189 && TREE_CODE (decl
) == FIELD_DECL
2190 && !DECL_C_BIT_FIELD (decl
))
2192 SET_DECL_WARN_IF_NOT_ALIGN (decl
, bitalign
);
2193 warn_if_not_aligned_p
= false;
2197 if (warn_if_not_aligned_p
)
2199 error ("%<warn_if_not_aligned%> may not be specified for %q+D",
2201 *no_add_attrs
= true;
2203 else if (!is_type
&& !*no_add_attrs
&& set_align
)
2205 SET_DECL_ALIGN (decl
, bitalign
);
2206 DECL_USER_ALIGN (decl
) = 1;
2212 /* Handle a "aligned" attribute; arguments as in
2213 struct attribute_spec.handler. */
2216 handle_aligned_attribute (tree
*node
, tree name
, tree args
,
2217 int flags
, bool *no_add_attrs
)
2219 return common_handle_aligned_attribute (node
, name
, args
, flags
,
2220 no_add_attrs
, false);
2223 /* Handle a "warn_if_not_aligned" attribute; arguments as in
2224 struct attribute_spec.handler. */
2227 handle_warn_if_not_aligned_attribute (tree
*node
, tree name
,
2228 tree args
, int flags
,
2231 return common_handle_aligned_attribute (node
, name
, args
, flags
,
2232 no_add_attrs
, true);
2235 /* Handle a "weak" attribute; arguments as in
2236 struct attribute_spec.handler. */
2239 handle_weak_attribute (tree
*node
, tree name
,
2240 tree
ARG_UNUSED (args
),
2241 int ARG_UNUSED (flags
),
2242 bool * ARG_UNUSED (no_add_attrs
))
2244 if (TREE_CODE (*node
) == FUNCTION_DECL
2245 && DECL_DECLARED_INLINE_P (*node
))
2247 warning (OPT_Wattributes
, "inline function %q+D declared weak", *node
);
2248 *no_add_attrs
= true;
2250 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node
)))
2252 error ("indirect function %q+D cannot be declared weak", *node
);
2253 *no_add_attrs
= true;
2256 else if (VAR_OR_FUNCTION_DECL_P (*node
))
2257 declare_weak (*node
);
2259 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
2264 /* Handle a "noinit" attribute; arguments as in struct
2265 attribute_spec.handler. Check whether the attribute is allowed
2266 here and add the attribute to the variable decl tree or otherwise
2267 issue a diagnostic. This function checks NODE is of the expected
2268 type and issues diagnostics otherwise using NAME. If it is not of
2269 the expected type *NO_ADD_ATTRS will be set to true. */
2272 handle_noinit_attribute (tree
* node
,
2275 int flags ATTRIBUTE_UNUSED
,
2278 const char *message
= NULL
;
2279 tree res
= NULL_TREE
;
2281 gcc_assert (DECL_P (*node
));
2282 gcc_assert (args
== NULL
);
2284 if (TREE_CODE (*node
) != VAR_DECL
)
2285 message
= G_("%qE attribute only applies to variables");
2287 /* Check that it's possible for the variable to have a section. */
2288 else if ((TREE_STATIC (*node
) || DECL_EXTERNAL (*node
) || in_lto_p
)
2289 && DECL_SECTION_NAME (*node
))
2290 message
= G_("%qE attribute cannot be applied to variables "
2291 "with specific sections");
2293 else if (!targetm
.have_switchable_bss_sections
)
2294 message
= G_("%qE attribute is specific to ELF targets");
2298 warning (OPT_Wattributes
, message
, name
);
2299 *no_add_attrs
= true;
2303 res
= targetm
.handle_generic_attribute (node
, name
, args
, flags
,
2305 /* If the back end confirms the attribute can be added then continue onto
2306 final processing. */
2307 if (!(*no_add_attrs
))
2309 /* If this var is thought to be common, then change this. Common
2310 variables are assigned to sections before the backend has a
2311 chance to process them. Do this only if the attribute is
2313 if (DECL_COMMON (*node
))
2314 DECL_COMMON (*node
) = 0;
2322 /* Handle a "noplt" attribute; arguments as in
2323 struct attribute_spec.handler. */
2326 handle_noplt_attribute (tree
*node
, tree name
,
2327 tree
ARG_UNUSED (args
),
2328 int ARG_UNUSED (flags
),
2329 bool * ARG_UNUSED (no_add_attrs
))
2331 if (TREE_CODE (*node
) != FUNCTION_DECL
)
2333 warning (OPT_Wattributes
,
2334 "%qE attribute is only applicable on functions", name
);
2335 *no_add_attrs
= true;
2341 /* Handle a "symver" attribute. */
2344 handle_symver_attribute (tree
*node
, tree
ARG_UNUSED (name
), tree args
,
2345 int ARG_UNUSED (flags
), bool *no_add_attrs
)
2348 const char *symver_str
;
2350 if (TREE_CODE (*node
) != FUNCTION_DECL
&& TREE_CODE (*node
) != VAR_DECL
)
2352 warning (OPT_Wattributes
,
2353 "%<symver%> attribute only applies to functions and variables");
2354 *no_add_attrs
= true;
2358 if (!decl_in_symtab_p (*node
))
2360 warning (OPT_Wattributes
,
2361 "%<symver%> attribute is only applicable to symbols");
2362 *no_add_attrs
= true;
2366 for (; args
; args
= TREE_CHAIN (args
))
2368 symver
= TREE_VALUE (args
);
2369 if (TREE_CODE (symver
) != STRING_CST
)
2371 error ("%<symver%> attribute argument not a string constant");
2372 *no_add_attrs
= true;
2376 symver_str
= TREE_STRING_POINTER (symver
);
2379 for (int n
= 0; (int)n
< TREE_STRING_LENGTH (symver
); n
++)
2380 if (symver_str
[n
] == '@')
2383 if (ats
!= 1 && ats
!= 2)
2385 error ("symver attribute argument must have format %<name@nodename%>");
2386 error ("%<symver%> attribute argument %qs must contain one or two "
2387 "%<@%>", symver_str
);
2388 *no_add_attrs
= true;
2397 /* Handle an "alias" or "ifunc" attribute; arguments as in
2398 struct attribute_spec.handler, except that IS_ALIAS tells us
2399 whether this is an alias as opposed to ifunc attribute. */
2402 handle_alias_ifunc_attribute (bool is_alias
, tree
*node
, tree name
, tree args
,
2407 if (TREE_CODE (decl
) != FUNCTION_DECL
2408 && (!is_alias
|| !VAR_P (decl
)))
2410 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
2411 *no_add_attrs
= true;
2413 else if ((TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_INITIAL (decl
))
2414 || (TREE_CODE (decl
) != FUNCTION_DECL
2415 && TREE_PUBLIC (decl
) && !DECL_EXTERNAL (decl
))
2416 /* A static variable declaration is always a tentative definition,
2417 but the alias is a non-tentative definition which overrides. */
2418 || (TREE_CODE (decl
) != FUNCTION_DECL
2419 && ! TREE_PUBLIC (decl
) && DECL_INITIAL (decl
)))
2421 error ("%q+D defined both normally and as %qE attribute", decl
, name
);
2422 *no_add_attrs
= true;
2426 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl
))
2427 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
))))
2429 error ("weak %q+D cannot be defined %qE", decl
, name
);
2430 *no_add_attrs
= true;
2434 /* Note that the very first time we process a nested declaration,
2435 decl_function_context will not be set. Indeed, *would* never
2436 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
2437 we do below. After such frobbery, pushdecl would set the context.
2438 In any case, this is never what we want. */
2439 else if (decl_function_context (decl
) == 0 && current_function_decl
== NULL
)
2443 id
= TREE_VALUE (args
);
2444 if (TREE_CODE (id
) != STRING_CST
)
2446 error ("attribute %qE argument not a string", name
);
2447 *no_add_attrs
= true;
2450 id
= get_identifier (TREE_STRING_POINTER (id
));
2451 /* This counts as a use of the object pointed to. */
2454 if (TREE_CODE (decl
) == FUNCTION_DECL
)
2455 DECL_INITIAL (decl
) = error_mark_node
;
2457 TREE_STATIC (decl
) = 1;
2461 /* ifuncs are also aliases, so set that attribute too. */
2462 DECL_ATTRIBUTES (decl
)
2463 = tree_cons (get_identifier ("alias"), args
,
2464 DECL_ATTRIBUTES (decl
));
2465 DECL_ATTRIBUTES (decl
) = tree_cons (get_identifier ("ifunc"),
2466 NULL
, DECL_ATTRIBUTES (decl
));
2471 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
2472 *no_add_attrs
= true;
2475 if (decl_in_symtab_p (*node
))
2477 struct symtab_node
*n
= symtab_node::get (decl
);
2478 if (n
&& n
->refuse_visibility_changes
)
2479 error ("%+qD declared %qs after being used",
2480 decl
, is_alias
? "alias" : "ifunc");
2487 /* Handle an "alias" or "ifunc" attribute; arguments as in
2488 struct attribute_spec.handler. */
2491 handle_ifunc_attribute (tree
*node
, tree name
, tree args
,
2492 int ARG_UNUSED (flags
), bool *no_add_attrs
)
2494 return handle_alias_ifunc_attribute (false, node
, name
, args
, no_add_attrs
);
2497 /* Handle an "alias" or "ifunc" attribute; arguments as in
2498 struct attribute_spec.handler. */
2501 handle_alias_attribute (tree
*node
, tree name
, tree args
,
2502 int ARG_UNUSED (flags
), bool *no_add_attrs
)
2504 return handle_alias_ifunc_attribute (true, node
, name
, args
, no_add_attrs
);
2507 /* Handle the "copy" attribute NAME by copying the set of attributes
2508 from the symbol referenced by ARGS to the declaration of *NODE. */
2511 handle_copy_attribute (tree
*node
, tree name
, tree args
,
2512 int flags
, bool *no_add_attrs
)
2514 /* Do not apply the copy attribute itself. It serves no purpose
2515 other than to copy other attributes. */
2516 *no_add_attrs
= true;
2520 tree ref
= TREE_VALUE (args
);
2521 if (ref
== error_mark_node
)
2524 if (TREE_CODE (ref
) == STRING_CST
)
2526 /* Explicitly handle this case since using a string literal
2527 as an argument is a likely mistake. */
2528 error_at (DECL_SOURCE_LOCATION (decl
),
2529 "%qE attribute argument cannot be a string",
2534 if (CONSTANT_CLASS_P (ref
)
2535 && (INTEGRAL_TYPE_P (TREE_TYPE (ref
))
2536 || FLOAT_TYPE_P (TREE_TYPE (ref
))))
2538 /* Similar to the string case, since some function attributes
2539 accept literal numbers as arguments (e.g., alloc_size or
2540 nonnull) using one here is a likely mistake. */
2541 error_at (DECL_SOURCE_LOCATION (decl
),
2542 "%qE attribute argument cannot be a constant arithmetic "
2550 /* Another possible mistake (but indirect self-references aren't
2551 and diagnosed and shouldn't be). */
2552 if (warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wattributes
,
2553 "%qE attribute ignored on a redeclaration "
2554 "of the referenced symbol",
2556 inform (DECL_SOURCE_LOCATION (node
[1]),
2557 "previous declaration here");
2561 /* Consider address-of expressions in the attribute argument
2562 as requests to copy from the referenced entity. */
2563 if (TREE_CODE (ref
) == ADDR_EXPR
)
2564 ref
= TREE_OPERAND (ref
, 0);
2568 /* Drill down into references to find the referenced decl. */
2569 tree_code refcode
= TREE_CODE (ref
);
2570 if (refcode
== ARRAY_REF
2571 || refcode
== INDIRECT_REF
)
2572 ref
= TREE_OPERAND (ref
, 0);
2573 else if (refcode
== COMPONENT_REF
)
2574 ref
= TREE_OPERAND (ref
, 1);
2577 } while (!DECL_P (ref
));
2579 /* For object pointer expressions, consider those to be requests
2580 to copy from their type, such as in:
2581 struct __attribute__ (copy ((struct T *)0)) U { ... };
2582 which copies type attributes from struct T to the declaration
2584 if ((CONSTANT_CLASS_P (ref
) || EXPR_P (ref
))
2585 && POINTER_TYPE_P (TREE_TYPE (ref
))
2586 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (ref
)))
2587 ref
= TREE_TYPE (ref
);
2592 && (TREE_CODE (ref
) == FUNCTION_DECL
2594 && POINTER_TYPE_P (TREE_TYPE (ref
))
2595 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (ref
))))))
2596 || (TREE_CODE (decl
) == FUNCTION_DECL
2599 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (ref
))))))
2601 /* It makes no sense to try to copy function attributes
2602 to a variable, or variable attributes to a function. */
2603 if (warning (OPT_Wattributes
,
2604 "%qE attribute ignored on a declaration of "
2605 "a different kind than referenced symbol",
2608 inform (DECL_SOURCE_LOCATION (ref
),
2609 "symbol %qD referenced by %qD declared here", ref
, decl
);
2613 tree attrs
= NULL_TREE
;
2615 attrs
= DECL_ATTRIBUTES (ref
);
2616 else if (TYPE_P (ref
))
2617 attrs
= TYPE_ATTRIBUTES (ref
);
2619 /* Copy decl attributes from REF to DECL. */
2620 for (tree at
= attrs
; at
; at
= TREE_CHAIN (at
))
2622 /* Avoid copying attributes that affect a symbol linkage,
2623 inlining, or visibility since those in all likelihood
2624 only apply to the target.
2625 FIXME: make it possible to specify which attributes to
2626 copy or not to copy in the copy attribute itself. */
2627 tree atname
= get_attribute_name (at
);
2628 if (is_attribute_p ("alias", atname
)
2629 || is_attribute_p ("always_inline", atname
)
2630 || is_attribute_p ("gnu_inline", atname
)
2631 || is_attribute_p ("ifunc", atname
)
2632 || is_attribute_p ("noinline", atname
)
2633 || is_attribute_p ("visibility", atname
)
2634 || is_attribute_p ("weak", atname
)
2635 || is_attribute_p ("weakref", atname
)
2636 || is_attribute_p ("target_clones", atname
))
2639 /* Attribute leaf only applies to extern functions.
2640 Avoid copying it to static ones. */
2641 if (!TREE_PUBLIC (decl
)
2642 && is_attribute_p ("leaf", atname
))
2645 tree atargs
= TREE_VALUE (at
);
2646 /* Create a copy of just the one attribute ar AT, including
2647 its argumentsm and add it to DECL. */
2648 tree attr
= tree_cons (atname
, copy_list (atargs
), NULL_TREE
);
2649 decl_attributes (node
, attr
, flags
, ref
);
2652 /* Proceed to copy type attributes below. */
2654 else if (!TYPE_P (decl
))
2656 error_at (DECL_SOURCE_LOCATION (decl
),
2657 "%qE attribute must apply to a declaration",
2662 /* A function declared with attribute nothrow has the attribute
2663 attached to it, but a C++ throw() function does not. */
2664 if (TREE_NOTHROW (ref
))
2665 TREE_NOTHROW (decl
) = true;
2667 /* Similarly, a function declared with attribute noreturn has it
2668 attached on to it, but a C11 _Noreturn function does not. */
2671 && TREE_THIS_VOLATILE (ref
)
2672 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (reftype
)))
2673 TREE_THIS_VOLATILE (decl
) = true;
2675 if (DECL_P (ref
) || EXPR_P (ref
))
2676 reftype
= TREE_TYPE (ref
);
2678 if (POINTER_TYPE_P (reftype
))
2679 reftype
= TREE_TYPE (reftype
);
2681 if (!TYPE_P (reftype
))
2684 tree attrs
= TYPE_ATTRIBUTES (reftype
);
2686 /* Copy type attributes from REF to DECL. */
2687 for (tree at
= attrs
; at
; at
= TREE_CHAIN (at
))
2688 decl_attributes (node
, at
, flags
, ref
);
2693 /* Handle a "weakref" attribute; arguments as in struct
2694 attribute_spec.handler. */
2697 handle_weakref_attribute (tree
*node
, tree name
, tree args
,
2698 int flags
, bool *no_add_attrs
)
2700 tree attr
= NULL_TREE
;
2702 /* We must ignore the attribute when it is associated with
2703 local-scoped decls, since attribute alias is ignored and many
2704 such symbols do not even have a DECL_WEAK field. */
2705 if (decl_function_context (*node
)
2706 || current_function_decl
2707 || !VAR_OR_FUNCTION_DECL_P (*node
))
2709 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
2710 *no_add_attrs
= true;
2714 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node
)))
2716 error ("indirect function %q+D cannot be declared %qE",
2718 *no_add_attrs
= true;
2722 /* The idea here is that `weakref("name")' mutates into `weakref,
2723 alias("name")', and weakref without arguments, in turn,
2724 implicitly adds weak. */
2728 attr
= tree_cons (get_identifier ("alias"), args
, attr
);
2729 attr
= tree_cons (get_identifier ("weakref"), NULL_TREE
, attr
);
2731 *no_add_attrs
= true;
2733 decl_attributes (node
, attr
, flags
);
2737 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node
)))
2738 error_at (DECL_SOURCE_LOCATION (*node
),
2739 "%qE attribute must appear before %qs attribute",
2742 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
2743 and that isn't supported; and because it wants to add it to
2744 the list of weak decls, which isn't helpful. */
2745 DECL_WEAK (*node
) = 1;
2748 if (decl_in_symtab_p (*node
))
2750 struct symtab_node
*n
= symtab_node::get (*node
);
2751 if (n
&& n
->refuse_visibility_changes
)
2752 error ("%+qD declared %qE after being used", *node
, name
);
2758 /* Handle an "visibility" attribute; arguments as in
2759 struct attribute_spec.handler. */
2762 handle_visibility_attribute (tree
*node
, tree name
, tree args
,
2763 int ARG_UNUSED (flags
),
2764 bool *ARG_UNUSED (no_add_attrs
))
2767 tree id
= TREE_VALUE (args
);
2768 enum symbol_visibility vis
;
2772 if (TREE_CODE (*node
) == ENUMERAL_TYPE
)
2774 else if (!RECORD_OR_UNION_TYPE_P (*node
))
2776 warning (OPT_Wattributes
, "%qE attribute ignored on non-class types",
2780 else if (TYPE_FIELDS (*node
))
2782 error ("%qE attribute ignored because %qT is already defined",
2787 else if (decl_function_context (decl
) != 0 || !TREE_PUBLIC (decl
))
2789 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
2793 if (TREE_CODE (id
) != STRING_CST
)
2795 error ("visibility argument not a string");
2799 /* If this is a type, set the visibility on the type decl. */
2802 decl
= TYPE_NAME (decl
);
2805 if (TREE_CODE (decl
) == IDENTIFIER_NODE
)
2807 warning (OPT_Wattributes
, "%qE attribute ignored on types",
2813 if (strcmp (TREE_STRING_POINTER (id
), "default") == 0)
2814 vis
= VISIBILITY_DEFAULT
;
2815 else if (strcmp (TREE_STRING_POINTER (id
), "internal") == 0)
2816 vis
= VISIBILITY_INTERNAL
;
2817 else if (strcmp (TREE_STRING_POINTER (id
), "hidden") == 0)
2818 vis
= VISIBILITY_HIDDEN
;
2819 else if (strcmp (TREE_STRING_POINTER (id
), "protected") == 0)
2820 vis
= VISIBILITY_PROTECTED
;
2823 error ("attribute %qE argument must be one of %qs, %qs, %qs, or %qs",
2824 name
, "default", "hidden", "protected", "internal");
2825 vis
= VISIBILITY_DEFAULT
;
2828 if (DECL_VISIBILITY_SPECIFIED (decl
)
2829 && vis
!= DECL_VISIBILITY (decl
))
2831 tree attributes
= (TYPE_P (*node
)
2832 ? TYPE_ATTRIBUTES (*node
)
2833 : DECL_ATTRIBUTES (decl
));
2834 if (lookup_attribute ("visibility", attributes
))
2835 error ("%qD redeclared with different visibility", decl
);
2836 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
2837 && lookup_attribute ("dllimport", attributes
))
2838 error ("%qD was declared %qs which implies default visibility",
2840 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
2841 && lookup_attribute ("dllexport", attributes
))
2842 error ("%qD was declared %qs which implies default visibility",
2846 DECL_VISIBILITY (decl
) = vis
;
2847 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
2849 /* Go ahead and attach the attribute to the node as well. This is needed
2850 so we can determine whether we have VISIBILITY_DEFAULT because the
2851 visibility was not specified, or because it was explicitly overridden
2852 from the containing scope. */
2857 /* Handle an "tls_model" attribute; arguments as in
2858 struct attribute_spec.handler. */
2861 handle_tls_model_attribute (tree
*node
, tree name
, tree args
,
2862 int ARG_UNUSED (flags
),
2863 bool *ARG_UNUSED (no_add_attrs
))
2867 enum tls_model kind
;
2871 warning (OPT_Wattributes
, "%qE attribute ignored because %qD "
2872 "is not a variable",
2877 if (!DECL_THREAD_LOCAL_P (decl
))
2879 warning (OPT_Wattributes
, "%qE attribute ignored because %qD does "
2880 "not have thread storage duration", name
, decl
);
2884 kind
= DECL_TLS_MODEL (decl
);
2885 id
= TREE_VALUE (args
);
2886 if (TREE_CODE (id
) != STRING_CST
)
2888 error ("%qE argument not a string", name
);
2892 if (!strcmp (TREE_STRING_POINTER (id
), "local-exec"))
2893 kind
= TLS_MODEL_LOCAL_EXEC
;
2894 else if (!strcmp (TREE_STRING_POINTER (id
), "initial-exec"))
2895 kind
= TLS_MODEL_INITIAL_EXEC
;
2896 else if (!strcmp (TREE_STRING_POINTER (id
), "local-dynamic"))
2897 kind
= optimize
? TLS_MODEL_LOCAL_DYNAMIC
: TLS_MODEL_GLOBAL_DYNAMIC
;
2898 else if (!strcmp (TREE_STRING_POINTER (id
), "global-dynamic"))
2899 kind
= TLS_MODEL_GLOBAL_DYNAMIC
;
2901 error ("%qE argument must be one of %qs, %qs, %qs, or %qs",
2903 "local-exec", "initial-exec", "local-dynamic", "global-dynamic");
2905 set_decl_tls_model (decl
, kind
);
2909 /* Handle a "no_instrument_function" attribute; arguments as in
2910 struct attribute_spec.handler. */
2913 handle_no_instrument_function_attribute (tree
*node
, tree name
,
2914 tree
ARG_UNUSED (args
),
2915 int ARG_UNUSED (flags
),
2920 if (TREE_CODE (decl
) != FUNCTION_DECL
)
2922 error_at (DECL_SOURCE_LOCATION (decl
),
2923 "%qE attribute applies only to functions", name
);
2924 *no_add_attrs
= true;
2927 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl
) = 1;
2932 /* Handle a "no_profile_instrument_function" attribute; arguments as in
2933 struct attribute_spec.handler. */
2936 handle_no_profile_instrument_function_attribute (tree
*node
, tree name
, tree
,
2937 int, bool *no_add_attrs
)
2939 if (TREE_CODE (*node
) != FUNCTION_DECL
)
2941 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
2942 *no_add_attrs
= true;
2948 /* Handle a "malloc" attribute; arguments as in
2949 struct attribute_spec.handler. */
2952 handle_malloc_attribute (tree
*node
, tree name
, tree
ARG_UNUSED (args
),
2953 int ARG_UNUSED (flags
), bool *no_add_attrs
)
2955 if (TREE_CODE (*node
) == FUNCTION_DECL
2956 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node
))))
2957 DECL_IS_MALLOC (*node
) = 1;
2960 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
2961 *no_add_attrs
= true;
2967 /* Handle a "alloc_size" attribute; arguments as in
2968 struct attribute_spec.handler. */
2971 handle_alloc_size_attribute (tree
*node
, tree name
, tree args
,
2972 int ARG_UNUSED (flags
), bool *no_add_attrs
)
2975 tree rettype
= TREE_TYPE (decl
);
2976 if (!POINTER_TYPE_P (rettype
))
2978 warning (OPT_Wattributes
,
2979 "%qE attribute ignored on a function returning %qT",
2981 *no_add_attrs
= true;
2985 for (int i
= 1; args
; ++i
)
2987 tree pos
= TREE_VALUE (args
);
2988 /* NEXT is null when the attribute includes just one argument.
2989 That's used to tell positional_argument to avoid mentioning
2990 the argument number in diagnostics (since there's just one
2991 mentioning it is unnecessary and coule be confusing). */
2992 tree next
= TREE_CHAIN (args
);
2993 if (tree val
= positional_argument (decl
, name
, pos
, INTEGER_TYPE
,
2994 next
|| i
> 1 ? i
: 0))
2995 TREE_VALUE (args
) = val
;
2998 *no_add_attrs
= true;
3008 /* Handle a "alloc_align" attribute; arguments as in
3009 struct attribute_spec.handler. */
3012 handle_alloc_align_attribute (tree
*node
, tree name
, tree args
, int,
3016 tree rettype
= TREE_TYPE (decl
);
3017 if (!POINTER_TYPE_P (rettype
))
3019 warning (OPT_Wattributes
,
3020 "%qE attribute ignored on a function returning %qT",
3022 *no_add_attrs
= true;
3026 if (!positional_argument (*node
, name
, TREE_VALUE (args
), INTEGER_TYPE
))
3027 *no_add_attrs
= true;
3032 /* Handle a "assume_aligned" attribute; arguments as in
3033 struct attribute_spec.handler. */
3036 handle_assume_aligned_attribute (tree
*node
, tree name
, tree args
, int,
3040 tree rettype
= TREE_TYPE (decl
);
3041 if (TREE_CODE (rettype
) != POINTER_TYPE
)
3043 warning (OPT_Wattributes
,
3044 "%qE attribute ignored on a function returning %qT",
3046 *no_add_attrs
= true;
3050 /* The alignment specified by the first argument. */
3051 tree align
= NULL_TREE
;
3053 for (; args
; args
= TREE_CHAIN (args
))
3055 tree val
= TREE_VALUE (args
);
3056 if (val
&& TREE_CODE (val
) != IDENTIFIER_NODE
3057 && TREE_CODE (val
) != FUNCTION_DECL
)
3058 val
= default_conversion (val
);
3060 if (!tree_fits_shwi_p (val
))
3062 warning (OPT_Wattributes
,
3063 "%qE attribute %E is not an integer constant",
3065 *no_add_attrs
= true;
3071 /* Validate and save the alignment. */
3072 if (!integer_pow2p (val
))
3074 warning (OPT_Wattributes
,
3075 "%qE attribute argument %E is not a power of 2",
3077 *no_add_attrs
= true;
3083 else if (tree_int_cst_sgn (val
) < 0 || tree_int_cst_le (align
, val
))
3085 /* The misalignment specified by the second argument
3086 must be non-negative and less than the alignment. */
3087 warning (OPT_Wattributes
,
3088 "%qE attribute argument %E is not in the range [0, %wu]",
3089 name
, val
, tree_to_uhwi (align
) - 1);
3090 *no_add_attrs
= true;
3097 /* Handle a "fn spec" attribute; arguments as in
3098 struct attribute_spec.handler. */
3101 handle_fnspec_attribute (tree
*node ATTRIBUTE_UNUSED
, tree
ARG_UNUSED (name
),
3102 tree args
, int ARG_UNUSED (flags
),
3103 bool *no_add_attrs ATTRIBUTE_UNUSED
)
3106 && TREE_CODE (TREE_VALUE (args
)) == STRING_CST
3107 && !TREE_CHAIN (args
));
3111 /* Handle a "warn_unused" attribute; arguments as in
3112 struct attribute_spec.handler. */
3115 handle_warn_unused_attribute (tree
*node
, tree name
,
3116 tree args ATTRIBUTE_UNUSED
,
3117 int flags ATTRIBUTE_UNUSED
, bool *no_add_attrs
)
3120 /* Do nothing else, just set the attribute. We'll get at
3121 it later with lookup_attribute. */
3125 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
3126 *no_add_attrs
= true;
3132 /* Handle an "omp declare simd" attribute; arguments as in
3133 struct attribute_spec.handler. */
3136 handle_omp_declare_simd_attribute (tree
*, tree
, tree
, int, bool *)
3141 /* Handle an "omp declare variant {base,variant}" attribute; arguments as in
3142 struct attribute_spec.handler. */
3145 handle_omp_declare_variant_attribute (tree
*, tree
, tree
, int, bool *)
3150 /* Handle a "simd" attribute. */
3153 handle_simd_attribute (tree
*node
, tree name
, tree args
, int, bool *no_add_attrs
)
3155 if (TREE_CODE (*node
) == FUNCTION_DECL
)
3157 tree t
= get_identifier ("omp declare simd");
3158 tree attr
= NULL_TREE
;
3161 tree id
= TREE_VALUE (args
);
3163 if (TREE_CODE (id
) != STRING_CST
)
3165 error ("attribute %qE argument not a string", name
);
3166 *no_add_attrs
= true;
3170 if (strcmp (TREE_STRING_POINTER (id
), "notinbranch") == 0)
3171 attr
= build_omp_clause (DECL_SOURCE_LOCATION (*node
),
3172 OMP_CLAUSE_NOTINBRANCH
);
3173 else if (strcmp (TREE_STRING_POINTER (id
), "inbranch") == 0)
3174 attr
= build_omp_clause (DECL_SOURCE_LOCATION (*node
),
3175 OMP_CLAUSE_INBRANCH
);
3178 error ("only %<inbranch%> and %<notinbranch%> flags are "
3179 "allowed for %<__simd__%> attribute");
3180 *no_add_attrs
= true;
3185 DECL_ATTRIBUTES (*node
)
3186 = tree_cons (t
, build_tree_list (NULL_TREE
, attr
),
3187 DECL_ATTRIBUTES (*node
));
3191 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
3192 *no_add_attrs
= true;
3198 /* Handle an "omp declare target" attribute; arguments as in
3199 struct attribute_spec.handler. */
3202 handle_omp_declare_target_attribute (tree
*, tree
, tree
, int, bool *)
3207 /* Handle a "returns_twice" attribute; arguments as in
3208 struct attribute_spec.handler. */
3211 handle_returns_twice_attribute (tree
*node
, tree name
, tree
ARG_UNUSED (args
),
3212 int ARG_UNUSED (flags
), bool *no_add_attrs
)
3214 if (TREE_CODE (*node
) == FUNCTION_DECL
)
3215 DECL_IS_RETURNS_TWICE (*node
) = 1;
3218 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
3219 *no_add_attrs
= true;
3225 /* Handle a "no_limit_stack" attribute; arguments as in
3226 struct attribute_spec.handler. */
3229 handle_no_limit_stack_attribute (tree
*node
, tree name
,
3230 tree
ARG_UNUSED (args
),
3231 int ARG_UNUSED (flags
),
3236 if (TREE_CODE (decl
) != FUNCTION_DECL
)
3238 error_at (DECL_SOURCE_LOCATION (decl
),
3239 "%qE attribute applies only to functions", name
);
3240 *no_add_attrs
= true;
3242 else if (DECL_INITIAL (decl
))
3244 error_at (DECL_SOURCE_LOCATION (decl
),
3245 "cannot set %qE attribute after definition", name
);
3246 *no_add_attrs
= true;
3249 DECL_NO_LIMIT_STACK (decl
) = 1;
3254 /* Handle a "pure" attribute; arguments as in
3255 struct attribute_spec.handler. */
3258 handle_pure_attribute (tree
*node
, tree name
, tree
ARG_UNUSED (args
),
3259 int ARG_UNUSED (flags
), bool *no_add_attrs
)
3261 if (TREE_CODE (*node
) == FUNCTION_DECL
)
3263 tree type
= TREE_TYPE (*node
);
3264 if (VOID_TYPE_P (TREE_TYPE (type
)))
3265 warning (OPT_Wattributes
, "%qE attribute on function "
3266 "returning %<void%>", name
);
3268 DECL_PURE_P (*node
) = 1;
3269 /* ??? TODO: Support types. */
3273 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
3274 *no_add_attrs
= true;
3280 /* Digest an attribute list destined for a transactional memory statement.
3281 ALLOWED is the set of attributes that are allowed for this statement;
3282 return the attribute we parsed. Multiple attributes are never allowed. */
3285 parse_tm_stmt_attr (tree attrs
, int allowed
)
3290 for ( ; attrs
; attrs
= TREE_CHAIN (attrs
))
3292 tree a
= get_attribute_name (attrs
);
3293 tree ns
= get_attribute_namespace (attrs
);
3296 if (is_attribute_p ("outer", a
)
3297 && (ns
== NULL_TREE
|| strcmp (IDENTIFIER_POINTER (ns
), "gnu") == 0))
3298 m
= TM_STMT_ATTR_OUTER
;
3300 if ((m
& allowed
) == 0)
3302 warning (OPT_Wattributes
, "%qE attribute directive ignored", a
);
3311 else if (m_seen
== m
)
3312 warning (OPT_Wattributes
, "%qE attribute duplicated", a
);
3314 warning (OPT_Wattributes
, "%qE attribute follows %qE", a
, a_seen
);
3320 /* Transform a TM attribute name into a maskable integer and back.
3321 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
3322 to how the lack of an attribute is treated. */
3325 tm_attr_to_mask (tree attr
)
3329 if (is_attribute_p ("transaction_safe", attr
))
3330 return TM_ATTR_SAFE
;
3331 if (is_attribute_p ("transaction_callable", attr
))
3332 return TM_ATTR_CALLABLE
;
3333 if (is_attribute_p ("transaction_pure", attr
))
3334 return TM_ATTR_PURE
;
3335 if (is_attribute_p ("transaction_unsafe", attr
))
3336 return TM_ATTR_IRREVOCABLE
;
3337 if (is_attribute_p ("transaction_may_cancel_outer", attr
))
3338 return TM_ATTR_MAY_CANCEL_OUTER
;
3343 tm_mask_to_attr (int mask
)
3349 str
= "transaction_safe";
3351 case TM_ATTR_CALLABLE
:
3352 str
= "transaction_callable";
3355 str
= "transaction_pure";
3357 case TM_ATTR_IRREVOCABLE
:
3358 str
= "transaction_unsafe";
3360 case TM_ATTR_MAY_CANCEL_OUTER
:
3361 str
= "transaction_may_cancel_outer";
3366 return get_identifier (str
);
3369 /* Return the first TM attribute seen in LIST. */
3372 find_tm_attribute (tree list
)
3374 for (; list
; list
= TREE_CHAIN (list
))
3376 tree name
= TREE_PURPOSE (list
);
3377 if (tm_attr_to_mask (name
) != 0)
3383 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
3384 Here we accept only function types, and verify that none of the other
3385 function TM attributes are also applied. */
3386 /* ??? We need to accept class types for C++, but not C. This greatly
3387 complicates this function, since we can no longer rely on the extra
3388 processing given by function_type_required. */
3391 handle_tm_attribute (tree
*node
, tree name
, tree args
,
3392 int flags
, bool *no_add_attrs
)
3394 /* Only one path adds the attribute; others don't. */
3395 *no_add_attrs
= true;
3397 switch (TREE_CODE (*node
))
3401 /* Only tm_callable and tm_safe apply to classes. */
3402 if (tm_attr_to_mask (name
) & ~(TM_ATTR_SAFE
| TM_ATTR_CALLABLE
))
3409 tree old_name
= find_tm_attribute (TYPE_ATTRIBUTES (*node
));
3410 if (old_name
== name
)
3412 else if (old_name
!= NULL_TREE
)
3413 error ("type was previously declared %qE", old_name
);
3415 *no_add_attrs
= false;
3421 /* transaction_safe_dynamic goes on the FUNCTION_DECL, but we also
3422 want to set transaction_safe on the type. */
3423 gcc_assert (is_attribute_p ("transaction_safe_dynamic", name
));
3424 if (!TYPE_P (DECL_CONTEXT (*node
)))
3425 error_at (DECL_SOURCE_LOCATION (*node
),
3426 "%<transaction_safe_dynamic%> may only be specified for "
3427 "a virtual function");
3428 *no_add_attrs
= false;
3429 decl_attributes (&TREE_TYPE (*node
),
3430 build_tree_list (get_identifier ("transaction_safe"),
3438 enum tree_code subcode
= TREE_CODE (TREE_TYPE (*node
));
3439 if (subcode
== FUNCTION_TYPE
|| subcode
== METHOD_TYPE
)
3441 tree fn_tmp
= TREE_TYPE (*node
);
3442 decl_attributes (&fn_tmp
, tree_cons (name
, args
, NULL
), 0);
3443 *node
= build_pointer_type (fn_tmp
);
3450 /* If a function is next, pass it on to be tried next. */
3451 if (flags
& (int) ATTR_FLAG_FUNCTION_NEXT
)
3452 return tree_cons (name
, args
, NULL
);
3455 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
3462 /* Handle the TM_WRAP attribute; arguments as in
3463 struct attribute_spec.handler. */
3466 handle_tm_wrap_attribute (tree
*node
, tree name
, tree args
,
3467 int ARG_UNUSED (flags
), bool *no_add_attrs
)
3471 /* We don't need the attribute even on success, since we
3472 record the entry in an external table. */
3473 *no_add_attrs
= true;
3475 if (TREE_CODE (decl
) != FUNCTION_DECL
)
3476 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
3479 tree wrap_decl
= TREE_VALUE (args
);
3480 if (error_operand_p (wrap_decl
))
3482 else if (TREE_CODE (wrap_decl
) != IDENTIFIER_NODE
3483 && !VAR_OR_FUNCTION_DECL_P (wrap_decl
))
3484 error ("%qE argument not an identifier", name
);
3487 if (TREE_CODE (wrap_decl
) == IDENTIFIER_NODE
)
3488 wrap_decl
= lookup_name (wrap_decl
);
3489 if (wrap_decl
&& TREE_CODE (wrap_decl
) == FUNCTION_DECL
)
3491 if (lang_hooks
.types_compatible_p (TREE_TYPE (decl
),
3492 TREE_TYPE (wrap_decl
)))
3493 record_tm_replacement (wrap_decl
, decl
);
3495 error ("%qD is not compatible with %qD", wrap_decl
, decl
);
3498 error ("%qE argument is not a function", name
);
3505 /* Ignore the given attribute. Used when this attribute may be usefully
3506 overridden by the target, but is not used generically. */
3509 ignore_attribute (tree
* ARG_UNUSED (node
), tree
ARG_UNUSED (name
),
3510 tree
ARG_UNUSED (args
), int ARG_UNUSED (flags
),
3513 *no_add_attrs
= true;
3517 /* Handle a "no vops" attribute; arguments as in
3518 struct attribute_spec.handler. */
3521 handle_novops_attribute (tree
*node
, tree
ARG_UNUSED (name
),
3522 tree
ARG_UNUSED (args
), int ARG_UNUSED (flags
),
3523 bool *ARG_UNUSED (no_add_attrs
))
3525 gcc_assert (TREE_CODE (*node
) == FUNCTION_DECL
);
3526 DECL_IS_NOVOPS (*node
) = 1;
3530 /* Handle a "deprecated" attribute; arguments as in
3531 struct attribute_spec.handler. */
3534 handle_deprecated_attribute (tree
*node
, tree name
,
3535 tree args
, int flags
,
3538 tree type
= NULL_TREE
;
3540 tree what
= NULL_TREE
;
3543 *no_add_attrs
= true;
3544 else if (TREE_CODE (TREE_VALUE (args
)) != STRING_CST
)
3546 error ("deprecated message is not a string");
3547 *no_add_attrs
= true;
3553 type
= TREE_TYPE (decl
);
3555 if (TREE_CODE (decl
) == TYPE_DECL
3556 || TREE_CODE (decl
) == PARM_DECL
3557 || VAR_OR_FUNCTION_DECL_P (decl
)
3558 || TREE_CODE (decl
) == FIELD_DECL
3559 || TREE_CODE (decl
) == CONST_DECL
3560 || objc_method_decl (TREE_CODE (decl
)))
3561 TREE_DEPRECATED (decl
) = 1;
3565 else if (TYPE_P (*node
))
3567 if (!(flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
))
3568 *node
= build_variant_type_copy (*node
);
3569 TREE_DEPRECATED (*node
) = 1;
3577 *no_add_attrs
= true;
3578 if (type
&& TYPE_NAME (type
))
3580 if (TREE_CODE (TYPE_NAME (type
)) == IDENTIFIER_NODE
)
3581 what
= TYPE_NAME (*node
);
3582 else if (TREE_CODE (TYPE_NAME (type
)) == TYPE_DECL
3583 && DECL_NAME (TYPE_NAME (type
)))
3584 what
= DECL_NAME (TYPE_NAME (type
));
3587 warning (OPT_Wattributes
, "%qE attribute ignored for %qE", name
, what
);
3589 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
3595 /* Return the "base" type from TYPE that is suitable to apply attribute
3596 vector_size to by stripping arrays, function types, etc. */
3598 type_for_vector_size (tree type
)
3600 /* We need to provide for vector pointers, vector arrays, and
3601 functions returning vectors. For example:
3603 __attribute__((vector_size(16))) short *foo;
3605 In this case, the mode is SI, but the type being modified is
3606 HI, so we need to look further. */
3608 while (POINTER_TYPE_P (type
)
3609 || TREE_CODE (type
) == FUNCTION_TYPE
3610 || TREE_CODE (type
) == METHOD_TYPE
3611 || TREE_CODE (type
) == ARRAY_TYPE
3612 || TREE_CODE (type
) == OFFSET_TYPE
)
3613 type
= TREE_TYPE (type
);
3618 /* Given TYPE, return the base type to which the vector_size attribute
3619 ATNAME with ARGS, when non-null, can be applied, if one exists.
3620 On success and when both ARGS and PTRNUNITS are non-null, set
3621 *PTRNUNINTS to the number of vector units. When PTRNUNITS is not
3622 null, issue a warning when the attribute argument is not constant
3623 and an error if there is no such type. Otherwise issue a warning
3624 in the latter case and return null. */
3627 type_valid_for_vector_size (tree type
, tree atname
, tree args
,
3628 unsigned HOST_WIDE_INT
*ptrnunits
)
3630 bool error_p
= ptrnunits
!= NULL
;
3632 /* Get the mode of the type being modified. */
3633 machine_mode orig_mode
= TYPE_MODE (type
);
3635 if ((!INTEGRAL_TYPE_P (type
)
3636 && !SCALAR_FLOAT_TYPE_P (type
)
3637 && !FIXED_POINT_TYPE_P (type
))
3638 || (!SCALAR_FLOAT_MODE_P (orig_mode
)
3639 && GET_MODE_CLASS (orig_mode
) != MODE_INT
3640 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode
))
3641 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type
))
3642 || TREE_CODE (type
) == BOOLEAN_TYPE
)
3645 error ("invalid vector type for attribute %qE", atname
);
3647 warning (OPT_Wattributes
, "invalid vector type for attribute %qE",
3652 /* When no argument has been provided this is just a request to validate
3653 the type above. Return TYPE to indicate success. */
3657 tree size
= TREE_VALUE (args
);
3658 /* Erroneous arguments have already been diagnosed. */
3659 if (size
== error_mark_node
)
3662 if (size
&& TREE_CODE (size
) != IDENTIFIER_NODE
3663 && TREE_CODE (size
) != FUNCTION_DECL
)
3664 size
= default_conversion (size
);
3666 if (TREE_CODE (size
) != INTEGER_CST
)
3669 error ("%qE attribute argument value %qE is not an integer constant",
3672 warning (OPT_Wattributes
,
3673 "%qE attribute argument value %qE is not an integer constant",
3678 if (!TYPE_UNSIGNED (TREE_TYPE (size
))
3679 && tree_int_cst_sgn (size
) < 0)
3682 error ("%qE attribute argument value %qE is negative",
3685 warning (OPT_Wattributes
,
3686 "%qE attribute argument value %qE is negative",
3691 /* The attribute argument value is constrained by the maximum bit
3692 alignment representable in unsigned int on the host. */
3693 unsigned HOST_WIDE_INT vecsize
;
3694 unsigned HOST_WIDE_INT maxsize
= tree_to_uhwi (max_object_size ());
3695 if (!tree_fits_uhwi_p (size
)
3696 || (vecsize
= tree_to_uhwi (size
)) > maxsize
)
3699 error ("%qE attribute argument value %qE exceeds %wu",
3700 atname
, size
, maxsize
);
3702 warning (OPT_Wattributes
,
3703 "%qE attribute argument value %qE exceeds %wu",
3704 atname
, size
, maxsize
);
3708 if (vecsize
% tree_to_uhwi (TYPE_SIZE_UNIT (type
)))
3711 error ("vector size not an integral multiple of component size");
3717 error ("zero vector size");
3721 /* Calculate how many units fit in the vector. */
3722 unsigned HOST_WIDE_INT nunits
= vecsize
/ tree_to_uhwi (TYPE_SIZE_UNIT (type
));
3723 if (nunits
& (nunits
- 1))
3726 error ("number of components of the vector not a power of two");
3728 warning (OPT_Wattributes
,
3729 "number of components of the vector not a power of two");
3734 *ptrnunits
= nunits
;
3739 /* Handle a "vector_size" attribute; arguments as in
3740 struct attribute_spec.handler. */
3743 handle_vector_size_attribute (tree
*node
, tree name
, tree args
,
3744 int ARG_UNUSED (flags
),
3747 *no_add_attrs
= true;
3749 /* Determine the "base" type to apply the attribute to. */
3750 tree type
= type_for_vector_size (*node
);
3752 /* Get the vector size (in bytes) and let the function compute
3753 the number of vector units. */
3754 unsigned HOST_WIDE_INT nunits
;
3755 type
= type_valid_for_vector_size (type
, name
, args
, &nunits
);
3759 tree new_type
= build_vector_type (type
, nunits
);
3761 /* Build back pointers if needed. */
3762 *node
= lang_hooks
.types
.reconstruct_complex_type (*node
, new_type
);
3767 /* Handle the "nonnull" attribute. */
3770 handle_nonnull_attribute (tree
*node
, tree name
,
3771 tree args
, int ARG_UNUSED (flags
),
3776 /* If no arguments are specified, all pointer arguments should be
3777 non-null. Verify a full prototype is given so that the arguments
3778 will have the correct types when we actually check them later.
3779 Avoid diagnosing type-generic built-ins since those have no
3783 if (!prototype_p (type
)
3784 && (!TYPE_ATTRIBUTES (type
)
3785 || !lookup_attribute ("type generic", TYPE_ATTRIBUTES (type
))))
3787 error ("%qE attribute without arguments on a non-prototype",
3789 *no_add_attrs
= true;
3794 for (int i
= 1; args
; ++i
)
3796 tree pos
= TREE_VALUE (args
);
3797 /* NEXT is null when the attribute includes just one argument.
3798 That's used to tell positional_argument to avoid mentioning
3799 the argument number in diagnostics (since there's just one
3800 mentioning it is unnecessary and coule be confusing). */
3801 tree next
= TREE_CHAIN (args
);
3802 if (tree val
= positional_argument (type
, name
, pos
, POINTER_TYPE
,
3803 next
|| i
> 1 ? i
: 0))
3804 TREE_VALUE (args
) = val
;
3807 *no_add_attrs
= true;
3816 /* Handle the "nonstring" variable attribute. */
3819 handle_nonstring_attribute (tree
*node
, tree name
, tree
ARG_UNUSED (args
),
3820 int ARG_UNUSED (flags
), bool *no_add_attrs
)
3823 tree_code code
= TREE_CODE (*node
);
3826 || code
== FIELD_DECL
3827 || code
== PARM_DECL
)
3829 tree type
= TREE_TYPE (*node
);
3831 if (POINTER_TYPE_P (type
) || TREE_CODE (type
) == ARRAY_TYPE
)
3833 /* Accept the attribute on arrays and pointers to all three
3834 narrow character types. */
3835 tree eltype
= TREE_TYPE (type
);
3836 eltype
= TYPE_MAIN_VARIANT (eltype
);
3837 if (eltype
== char_type_node
3838 || eltype
== signed_char_type_node
3839 || eltype
== unsigned_char_type_node
)
3843 warning (OPT_Wattributes
,
3844 "%qE attribute ignored on objects of type %qT",
3846 *no_add_attrs
= true;
3850 if (code
== FUNCTION_DECL
)
3851 warning (OPT_Wattributes
,
3852 "%qE attribute does not apply to functions", name
);
3853 else if (code
== TYPE_DECL
)
3854 warning (OPT_Wattributes
,
3855 "%qE attribute does not apply to types", name
);
3857 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
3859 *no_add_attrs
= true;
3863 /* Given a function type FUNCTYPE, returns the type of the parameter
3864 ARGNO or null if ARGNO exceeds the number of parameters. On failure
3865 set *NARGS to the number of function parameters. */
3868 get_argument_type (tree functype
, unsigned argno
, unsigned *nargs
)
3870 function_args_iterator iter
;
3871 function_args_iter_init (&iter
, functype
);
3875 for ( ; iter
.next
; ++count
, function_args_iter_next (&iter
))
3877 if (count
+ 1 == argno
)
3879 tree argtype
= function_args_iter_cond (&iter
);
3880 if (VOID_TYPE_P (argtype
))
3890 /* Appends ATTRSTR to the access string in ATTRS if one is there
3891 or creates a new one and returns the concatenated access string. */
3894 append_access_attrs (tree t
, tree attrs
, const char *attrstr
,
3895 char code
, HOST_WIDE_INT idxs
[2])
3898 int n1
= sprintf (attrspec
, "%c%u", code
, (unsigned) idxs
[0] - 1);
3901 n2
= sprintf (attrspec
+ n1
+ 1, "%u", (unsigned) idxs
[1] - 1);
3903 size_t newlen
= n1
+ n2
+ !!n2
;
3904 char *newspec
= attrspec
;
3906 if (tree acs
= lookup_attribute ("access", attrs
))
3908 acs
= TREE_VALUE (acs
);
3909 gcc_assert (TREE_CODE (acs
) == STRING_CST
);
3911 /* Check to make sure ATTRSPEC doesn't conflict with another
3912 access attribute specified in ATTRS by searching the access
3913 string in ATTRS for the position string formatted above into
3914 ATTRSPEC, and if it's found, that the two match. */
3916 const char *posstr
= attrspec
+ 1;
3917 const char *str
= TREE_STRING_POINTER (acs
);
3918 const char *pos
= str
;
3919 for ( ; ; pos
+= n1
)
3921 pos
= strstr (pos
, posstr
);
3925 if (ISDIGIT (pos
[-1]) || ISDIGIT (pos
[n1
-1]))
3928 /* Found a matching positional argument. */
3929 if (*attrspec
!= pos
[-1])
3931 /* Mismatch in access mode. */
3932 auto_diagnostic_group d
;
3933 if (warning (OPT_Wattributes
,
3934 "attribute %qs mismatch with mode %qs",
3938 : (pos
[-1] == 'w' ? "write_only" : "read_write")))
3940 inform (DECL_SOURCE_LOCATION (t
),
3941 "previous declaration here");
3945 if ((n2
&& pos
[n1
- 1] != ','))
3947 /* Mismatch in the presence of the size argument. */
3948 auto_diagnostic_group d
;
3949 if (warning (OPT_Wattributes
,
3950 "attribute %qs positional argument 2 conflicts "
3951 "with previous designation",
3954 inform (DECL_SOURCE_LOCATION (t
),
3955 "previous declaration here");
3959 if (!n2
&& pos
[n1
- 1] == ',')
3961 /* Mismatch in the presence of the size argument. */
3962 auto_diagnostic_group d
;
3963 if (warning (OPT_Wattributes
,
3964 "attribute %qs missing positional argument 2 "
3965 "provided in previous designation",
3968 inform (DECL_SOURCE_LOCATION (t
),
3969 "previous declaration here");
3973 if (n2
&& strncmp (attrstr
+ n1
+ 1, pos
+ n1
, n2
))
3975 /* Mismatch in the value of the size argument. */
3976 auto_diagnostic_group d
;
3977 if (warning (OPT_Wattributes
,
3978 "attribute %qs mismatch positional argument "
3980 attrstr
, atoi (attrstr
+ n1
+ 1), atoi (pos
+ n1
))
3982 inform (DECL_SOURCE_LOCATION (t
),
3983 "previous declaration here");
3987 /* Avoid adding the same attribute specification. */
3991 /* Connect the two substrings formatted above into a single one. */
3995 size_t len
= strlen (str
);
3996 newspec
= XNEWVEC (char, newlen
+ len
+ 1);
3997 strcpy (newspec
, str
);
3998 strcpy (newspec
+ len
, attrspec
);
4002 /* Connect the two substrings formatted above into a single one. */
4005 tree ret
= build_string (newlen
+ 1, newspec
);
4006 if (newspec
!= attrspec
)
4007 XDELETEVEC (newspec
);
4011 /* Handle the access attribute (read_only, write_only, and read_write). */
4014 handle_access_attribute (tree
*node
, tree name
, tree args
,
4015 int ARG_UNUSED (flags
), bool *no_add_attrs
)
4018 tree attrs
= TYPE_ATTRIBUTES (type
);
4020 *no_add_attrs
= true;
4022 /* Verify a full prototype is provided so that the argument types
4023 can be validated. Avoid diagnosing type-generic built-ins since
4024 those have no prototype. */
4026 && !prototype_p (type
)
4027 && (!attrs
|| !lookup_attribute ("type generic", attrs
)))
4029 error ("attribute %qE without arguments on a non-prototype", name
);
4033 /* Set to true when the access mode has the form of a function call
4034 as in 'attribute (read_only (1, 2))'. That's an easy mistake to
4035 make and so worth a special diagnostic. */
4036 bool funcall
= false;
4037 tree access_mode
= TREE_VALUE (args
);
4038 if (TREE_CODE (access_mode
) == CALL_EXPR
)
4040 access_mode
= CALL_EXPR_FN (access_mode
);
4041 if (TREE_CODE (access_mode
) != ADDR_EXPR
)
4043 error ("attribute %qE invalid mode", name
);
4046 access_mode
= TREE_OPERAND (access_mode
, 0);
4047 access_mode
= DECL_NAME (access_mode
);
4051 const char* const access_str
= IDENTIFIER_POINTER (access_mode
);
4052 const char *ps
= access_str
;
4053 if (ps
[0] == '_' && ps
[1] == '_')
4055 size_t len
= strlen (ps
);
4056 if (ps
[len
- 1] == '_' && ps
[len
- 2] == '_')
4060 const bool read_only
= strncmp (ps
, "read_only", 9) == 0;
4061 const bool write_only
= strncmp (ps
, "write_only", 9) == 0;
4062 if (!read_only
&& !write_only
&& strncmp (ps
, "read_write", 9))
4064 error ("attribute %qE invalid mode %qs; expected one of "
4065 "%qs, %qs, or %qs", name
, access_str
,
4066 "read_only", "read_write", "write_only");
4072 error ("attribute %qE unexpected %<(%> after mode %qs; expected "
4073 "a positional argument or %<)%>",
4078 args
= TREE_CHAIN (args
);
4081 /* The first positional argument is required. It may be worth
4082 dropping the requirement at some point and having read_only
4083 apply to all const-qualified pointers and read_write or
4084 write_only to the rest. */
4085 error ("attribute %<%E(%s)%> missing an argument",
4090 /* One or more positional arguments have been specified. Validate
4092 tree idxnodes
[2] = { NULL_TREE
, NULL_TREE
};
4093 tree argtypes
[2] = { NULL_TREE
, NULL_TREE
};
4094 /* 1-based attribute positional arguments or zero if not specified.
4095 Invalid negative or excessive values are also stored but used
4096 only in diagnostics. */
4097 HOST_WIDE_INT idxs
[2] = { 0, 0 };
4099 /* Number of function formal arguments (used in diagnostics). */
4100 unsigned nfuncargs
= 0;
4101 /* Number of (optional) attribute positional arguments. */
4102 unsigned nattrargs
= 0;
4104 for (unsigned i
= 0; i
!= 2; ++i
, args
= TREE_CHAIN (args
), ++nattrargs
)
4109 idxnodes
[i
] = TREE_VALUE (args
);
4111 if (TREE_CODE (idxnodes
[i
]) != IDENTIFIER_NODE
4112 && TREE_CODE (idxnodes
[i
]) != FUNCTION_DECL
)
4113 idxnodes
[i
] = default_conversion (idxnodes
[i
]);
4115 if (tree_fits_shwi_p (idxnodes
[i
]))
4117 idxs
[i
] = tree_to_shwi (idxnodes
[i
]);
4118 argtypes
[i
] = get_argument_type (type
, idxs
[i
], &nfuncargs
);
4122 if ((nattrargs
== 1 && !idxs
[0])
4123 || (nattrargs
== 2 && (!idxs
[0] || !idxs
[1])))
4126 error ("attribute %<%E(%s, %E, %E)%> invalid positional argument %i",
4127 name
, access_str
, idxnodes
[0], idxnodes
[1], idxs
[0] ? 2 : 1);
4129 error ("attribute %<%E(%s, %E)%> invalid positional argument %i",
4130 name
, access_str
, idxnodes
[0], idxs
[0] ? 2 : 1);
4134 /* Format the attribute specification to include in diagnostics. */
4137 snprintf (attrstr
, sizeof attrstr
, "%s(%s, %lli, %lli)",
4138 IDENTIFIER_POINTER (name
), access_str
,
4139 (long long) idxs
[0], (long long) idxs
[1]);
4140 else if (idxnodes
[0])
4141 snprintf (attrstr
, sizeof attrstr
, "%s(%s, %lli)",
4142 IDENTIFIER_POINTER (name
), access_str
,
4143 (long long) idxs
[0]);
4145 snprintf (attrstr
, sizeof attrstr
, "%s(%s)",
4146 IDENTIFIER_POINTER (name
), access_str
);
4148 /* Verify the positional argument values are in range. */
4149 if (!argtypes
[0] || (idxnodes
[1] && !argtypes
[1]))
4153 if (idxs
[0] < 0 || idxs
[1] < 0)
4154 error ("attribute %qs positional argument %i invalid value %wi",
4155 attrstr
, idxs
[0] < 0 ? 1 : 2,
4156 idxs
[0] < 0 ? idxs
[0] : idxs
[1]);
4158 error ("attribute %qs positional argument %i value %wi exceeds "
4159 "number of function arguments %u",
4160 attrstr
, idxs
[0] ? 1 : 2,
4161 idxs
[0] ? idxs
[0] : idxs
[1],
4165 error ("attribute %qs invalid positional argument", attrstr
);
4170 if (!POINTER_TYPE_P (argtypes
[0]))
4172 /* The first argument must have a pointer or reference type. */
4173 error ("attribute %qs positional argument 1 references "
4174 "non-pointer argument type %qT",
4175 attrstr
, argtypes
[0]);
4180 /* Pointers to functions are not allowed. */
4181 tree ptrtype
= TREE_TYPE (argtypes
[0]);
4182 if (FUNC_OR_METHOD_TYPE_P (ptrtype
))
4184 error ("attribute %qs positional argument 1 references "
4185 "argument of function type %qT",
4193 /* A read_write and write_only modes must reference non-const
4195 if (TYPE_READONLY (TREE_TYPE (argtypes
[0])))
4197 error ("attribute %qs positional argument 1 references "
4198 "%qs-qualified argument type %qT",
4199 attrstr
, "const", argtypes
[0]);
4203 else if (!TYPE_READONLY (TREE_TYPE (argtypes
[0])))
4205 /* A read_only mode should ideally reference const-qualified
4206 arguments but it's not diagnosed error if one doesn't.
4207 This makes it possible to annotate legacy, const-incorrect
4208 APIs. It might be worth a diagnostic along the lines of
4213 if (argtypes
[1] && !INTEGRAL_TYPE_P (argtypes
[1]))
4215 error ("attribute %qs positional argument 2 references "
4216 "non-integer argument type %qT",
4217 attrstr
, argtypes
[1]);
4221 /* Verify that the new attribute doesn't conflict with any existing
4222 attributes specified on previous declarations of the same type
4223 and if not, concatenate the two. */
4224 const char code
= read_only
? 'r' : write_only
? 'w' : 'x';
4225 tree new_attrs
= append_access_attrs (node
[0], attrs
, attrstr
, code
, idxs
);
4229 /* Replace any existing access attribute specification with
4230 the concatenation above. */
4231 attrs
= remove_attribute (IDENTIFIER_POINTER (name
), attrs
);
4232 new_attrs
= tree_cons (name
, new_attrs
, attrs
);
4236 /* Repeat for the previously declared type. */
4237 attrs
= TYPE_ATTRIBUTES (TREE_TYPE (node
[1]));
4239 = append_access_attrs (node
[1], attrs
, attrstr
, code
, idxs
);
4243 attrs
= remove_attribute (IDENTIFIER_POINTER (name
), attrs
);
4244 new_attrs
= tree_cons (name
, new_attrs
, attrs
);
4245 TYPE_ATTRIBUTES (TREE_TYPE (node
[1])) = new_attrs
;
4248 TYPE_ATTRIBUTES (*node
) = new_attrs
;
4252 /* Handle a "nothrow" attribute; arguments as in
4253 struct attribute_spec.handler. */
4256 handle_nothrow_attribute (tree
*node
, tree name
, tree
ARG_UNUSED (args
),
4257 int ARG_UNUSED (flags
), bool *no_add_attrs
)
4259 if (TREE_CODE (*node
) == FUNCTION_DECL
)
4260 TREE_NOTHROW (*node
) = 1;
4261 /* ??? TODO: Support types. */
4264 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
4265 *no_add_attrs
= true;
4271 /* Handle a "cleanup" attribute; arguments as in
4272 struct attribute_spec.handler. */
4275 handle_cleanup_attribute (tree
*node
, tree name
, tree args
,
4276 int ARG_UNUSED (flags
), bool *no_add_attrs
)
4279 tree cleanup_id
, cleanup_decl
;
4281 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
4282 for global destructors in C++. This requires infrastructure that
4283 we don't have generically at the moment. It's also not a feature
4284 we'd be missing too much, since we do have attribute constructor. */
4285 if (!VAR_P (decl
) || TREE_STATIC (decl
))
4287 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
4288 *no_add_attrs
= true;
4292 /* Verify that the argument is a function in scope. */
4293 /* ??? We could support pointers to functions here as well, if
4294 that was considered desirable. */
4295 cleanup_id
= TREE_VALUE (args
);
4296 if (TREE_CODE (cleanup_id
) != IDENTIFIER_NODE
)
4298 error ("cleanup argument not an identifier");
4299 *no_add_attrs
= true;
4302 cleanup_decl
= lookup_name (cleanup_id
);
4303 if (!cleanup_decl
|| TREE_CODE (cleanup_decl
) != FUNCTION_DECL
)
4305 error ("cleanup argument not a function");
4306 *no_add_attrs
= true;
4310 /* That the function has proper type is checked with the
4311 eventual call to build_function_call. */
4316 /* Handle a "warn_unused_result" attribute. No special handling. */
4319 handle_warn_unused_result_attribute (tree
*node
, tree name
,
4320 tree
ARG_UNUSED (args
),
4321 int ARG_UNUSED (flags
), bool *no_add_attrs
)
4323 /* Ignore the attribute for functions not returning any value. */
4324 if (VOID_TYPE_P (TREE_TYPE (*node
)))
4326 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
4327 *no_add_attrs
= true;
4333 /* Handle a "sentinel" attribute. */
4336 handle_sentinel_attribute (tree
*node
, tree name
, tree args
,
4337 int ARG_UNUSED (flags
), bool *no_add_attrs
)
4339 if (!prototype_p (*node
))
4341 warning (OPT_Wattributes
,
4342 "%qE attribute requires prototypes with named arguments", name
);
4343 *no_add_attrs
= true;
4347 if (!stdarg_p (*node
))
4349 warning (OPT_Wattributes
,
4350 "%qE attribute only applies to variadic functions", name
);
4351 *no_add_attrs
= true;
4357 tree position
= TREE_VALUE (args
);
4358 if (position
&& TREE_CODE (position
) != IDENTIFIER_NODE
4359 && TREE_CODE (position
) != FUNCTION_DECL
)
4360 position
= default_conversion (position
);
4362 if (TREE_CODE (position
) != INTEGER_CST
4363 || !INTEGRAL_TYPE_P (TREE_TYPE (position
)))
4365 warning (OPT_Wattributes
,
4366 "requested position is not an integer constant");
4367 *no_add_attrs
= true;
4371 if (tree_int_cst_lt (position
, integer_zero_node
))
4373 warning (OPT_Wattributes
,
4374 "requested position is less than zero");
4375 *no_add_attrs
= true;
4383 /* Handle a "type_generic" attribute. */
4386 handle_type_generic_attribute (tree
*node
, tree
ARG_UNUSED (name
),
4387 tree
ARG_UNUSED (args
), int ARG_UNUSED (flags
),
4388 bool * ARG_UNUSED (no_add_attrs
))
4390 /* Ensure we have a function type. */
4391 gcc_assert (TREE_CODE (*node
) == FUNCTION_TYPE
);
4393 /* Ensure we have a variadic function. */
4394 gcc_assert (!prototype_p (*node
) || stdarg_p (*node
));
4399 /* Handle a "target" attribute. */
4402 handle_target_attribute (tree
*node
, tree name
, tree args
, int flags
,
4405 /* Ensure we have a function type. */
4406 if (TREE_CODE (*node
) != FUNCTION_DECL
)
4408 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
4409 *no_add_attrs
= true;
4411 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node
)))
4413 warning (OPT_Wattributes
, "%qE attribute ignored due to conflict "
4414 "with %qs attribute", name
, "target_clones");
4415 *no_add_attrs
= true;
4417 else if (! targetm
.target_option
.valid_attribute_p (*node
, name
, args
,
4419 *no_add_attrs
= true;
4421 /* Check that there's no empty string in values of the attribute. */
4422 for (tree t
= args
; t
!= NULL_TREE
; t
= TREE_CHAIN (t
))
4424 tree value
= TREE_VALUE (t
);
4425 if (TREE_CODE (value
) == STRING_CST
4426 && TREE_STRING_LENGTH (value
) == 1
4427 && TREE_STRING_POINTER (value
)[0] == '\0')
4429 warning (OPT_Wattributes
, "empty string in attribute %<target%>");
4430 *no_add_attrs
= true;
4437 /* Handle a "target_clones" attribute. */
4440 handle_target_clones_attribute (tree
*node
, tree name
, tree
ARG_UNUSED (args
),
4441 int ARG_UNUSED (flags
), bool *no_add_attrs
)
4443 /* Ensure we have a function type. */
4444 if (TREE_CODE (*node
) == FUNCTION_DECL
)
4446 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node
)))
4448 warning (OPT_Wattributes
, "%qE attribute ignored due to conflict "
4449 "with %qs attribute", name
, "always_inline");
4450 *no_add_attrs
= true;
4452 else if (lookup_attribute ("target", DECL_ATTRIBUTES (*node
)))
4454 warning (OPT_Wattributes
, "%qE attribute ignored due to conflict "
4455 "with %qs attribute", name
, "target");
4456 *no_add_attrs
= true;
4459 /* Do not inline functions with multiple clone targets. */
4460 DECL_UNINLINABLE (*node
) = 1;
4464 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
4465 *no_add_attrs
= true;
4470 /* For handling "optimize" attribute. arguments as in
4471 struct attribute_spec.handler. */
4474 handle_optimize_attribute (tree
*node
, tree name
, tree args
,
4475 int ARG_UNUSED (flags
), bool *no_add_attrs
)
4477 /* Ensure we have a function type. */
4478 if (TREE_CODE (*node
) != FUNCTION_DECL
)
4480 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
4481 *no_add_attrs
= true;
4485 struct cl_optimization cur_opts
;
4486 tree old_opts
= DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node
);
4488 /* Save current options. */
4489 cl_optimization_save (&cur_opts
, &global_options
);
4491 /* If we previously had some optimization options, use them as the
4494 cl_optimization_restore (&global_options
,
4495 TREE_OPTIMIZATION (old_opts
));
4497 /* Parse options, and update the vector. */
4498 parse_optimize_options (args
, true);
4499 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node
)
4500 = build_optimization_node (&global_options
);
4502 /* Restore current options. */
4503 cl_optimization_restore (&global_options
, &cur_opts
);
4509 /* Handle a "no_split_stack" attribute. */
4512 handle_no_split_stack_attribute (tree
*node
, tree name
,
4513 tree
ARG_UNUSED (args
),
4514 int ARG_UNUSED (flags
),
4519 if (TREE_CODE (decl
) != FUNCTION_DECL
)
4521 error_at (DECL_SOURCE_LOCATION (decl
),
4522 "%qE attribute applies only to functions", name
);
4523 *no_add_attrs
= true;
4525 else if (DECL_INITIAL (decl
))
4527 error_at (DECL_SOURCE_LOCATION (decl
),
4528 "cannot set %qE attribute after definition", name
);
4529 *no_add_attrs
= true;
4535 /* Handle a "returns_nonnull" attribute; arguments as in
4536 struct attribute_spec.handler. */
4539 handle_returns_nonnull_attribute (tree
*node
, tree name
, tree
, int,
4542 // Even without a prototype we still have a return type we can check.
4543 if (TREE_CODE (TREE_TYPE (*node
)) != POINTER_TYPE
)
4545 error ("%qE attribute on a function not returning a pointer", name
);
4546 *no_add_attrs
= true;
4551 /* Handle a "designated_init" attribute; arguments as in
4552 struct attribute_spec.handler. */
4555 handle_designated_init_attribute (tree
*node
, tree name
, tree
, int,
4558 if (TREE_CODE (*node
) != RECORD_TYPE
)
4560 error ("%qE attribute is only valid on %<struct%> type", name
);
4561 *no_add_attrs
= true;
4567 /* Handle a "fallthrough" attribute; arguments as in struct
4568 attribute_spec.handler. */
4571 handle_fallthrough_attribute (tree
*, tree name
, tree
, int,
4574 pedwarn (input_location
, OPT_Wattributes
, "%qE attribute ignored", name
);
4575 *no_add_attrs
= true;
4579 /* Handle a "patchable_function_entry" attributes; arguments as in
4580 struct attribute_spec.handler. */
4583 handle_patchable_function_entry_attribute (tree
*, tree name
, tree args
,
4584 int, bool *no_add_attrs
)
4586 for (; args
; args
= TREE_CHAIN (args
))
4588 tree val
= TREE_VALUE (args
);
4589 if (val
&& TREE_CODE (val
) != IDENTIFIER_NODE
4590 && TREE_CODE (val
) != FUNCTION_DECL
)
4591 val
= default_conversion (val
);
4593 if (!tree_fits_uhwi_p (val
))
4595 warning (OPT_Wattributes
,
4596 "%qE attribute argument %qE is not an integer constant",
4598 *no_add_attrs
= true;
4605 /* Attempt to partially validate a single attribute ATTR as if
4606 it were to be applied to an entity OPER. */
4609 validate_attribute (location_t atloc
, tree oper
, tree attr
)
4611 /* Determine whether the name of the attribute is valid
4612 and fail with an error if not. */
4613 tree atname
= get_attribute_name (attr
);
4614 if (!lookup_attribute_spec (atname
))
4616 if (atloc
!= UNKNOWN_LOCATION
)
4617 error_at (atloc
, "unknown attribute %qE", atname
);
4621 tree args
= TREE_VALUE (attr
);
4625 /* FIXME: Do some validation. */
4626 const char *atstr
= IDENTIFIER_POINTER (atname
);
4627 if (!strcmp (atstr
, "format"))
4630 /* Only when attribute arguments have been provided try to validate
4631 the whole thing. decl_attributes doesn't return an indication of
4632 success or failure so proceed regardless. */
4633 const char tmpname
[] = "__builtin_has_attribute_tmp.";
4634 tree tmpid
= get_identifier (tmpname
);
4636 if (!strcmp (atstr
, "vector_size"))
4638 tree type
= TYPE_P (oper
) ? oper
: TREE_TYPE (oper
);
4639 /* Check for function type here since type_for_vector_size
4640 strips it while looking for a function's return type. */
4641 if (FUNC_OR_METHOD_TYPE_P (type
))
4643 warning_at (atloc
, OPT_Wattributes
,
4644 "invalid operand type %qT for %qs", type
, atstr
);
4648 type
= type_for_vector_size (type
);
4649 if (VECTOR_TYPE_P (type
))
4650 type
= TREE_TYPE (type
);
4651 /* Avoid trying to apply attribute vector_size to OPER since
4652 it's overly restrictive. Simply make sure it has the right
4654 return type_valid_for_vector_size (type
, atname
, args
, NULL
);
4658 tmpdecl
= build_decl (atloc
, TYPE_DECL
, tmpid
, oper
);
4659 else if (DECL_P (oper
))
4660 tmpdecl
= build_decl (atloc
, TREE_CODE (oper
), tmpid
, TREE_TYPE (oper
));
4661 else if (EXPR_P (oper
))
4662 tmpdecl
= build_decl (atloc
, TYPE_DECL
, tmpid
, TREE_TYPE (oper
));
4666 /* Temporarily clear CURRENT_FUNCTION_DECL to make decl_attributes
4667 believe the DECL declared above is at file scope. (See bug 87526.) */
4668 tree save_curfunc
= current_function_decl
;
4669 current_function_decl
= NULL_TREE
;
4670 if (DECL_P (tmpdecl
))
4673 /* An alias cannot be a definition so declare the symbol extern. */
4674 DECL_EXTERNAL (tmpdecl
) = true;
4675 /* Attribute visibility only applies to symbols visible from other
4676 translation units so make it "public." */
4677 TREE_PUBLIC (tmpdecl
) = TREE_PUBLIC (oper
);
4679 decl_attributes (&tmpdecl
, attr
, 0);
4680 current_function_decl
= save_curfunc
;
4682 /* FIXME: Change decl_attributes to indicate success or failure (and
4683 parameterize it to avoid failing with errors). */
4687 /* Return true if the DECL, EXPR, or TYPE t has been declared with
4688 attribute ATTR. For DECL, consider also its type. For EXPR,
4689 consider just its type. */
4692 has_attribute (location_t atloc
, tree t
, tree attr
, tree (*convert
)(tree
))
4694 if (!attr
|| !t
|| t
== error_mark_node
)
4697 if (!validate_attribute (atloc
, t
, attr
))
4700 tree type
= NULL_TREE
;
4701 tree expr
= NULL_TREE
;
4708 /* Determine the array element/member declaration from
4709 a COMPONENT_REF and an INDIRECT_REF involving a refeence. */
4711 tree_code code
= TREE_CODE (t
);
4712 if (code
== INDIRECT_REF
)
4714 tree op0
= TREE_OPERAND (t
, 0);
4715 if (TREE_CODE (TREE_TYPE (op0
)) == REFERENCE_TYPE
)
4720 else if (code
== COMPONENT_REF
)
4721 t
= TREE_OPERAND (t
, 1);
4728 /* Set to true when an attribute is found in the referenced entity
4729 that matches the specified attribute. */
4730 bool found_match
= false;
4732 tree atname
= get_attribute_name (attr
);
4733 const char *namestr
= IDENTIFIER_POINTER (atname
);
4735 /* Iterate once for a type and twice for a function or variable
4736 declaration: once for the DECL and the second time for its
4738 for (bool done
= false; !found_match
&& !done
; )
4743 if (type
== error_mark_node
)
4745 /* This could be a label. FIXME: add support for labels. */
4746 warning_at (atloc
, OPT_Wattributes
,
4748 ? G_("%qs attribute not supported for %qT "
4749 "in %<__builtin_has_attribute%>")
4750 : G_("%qs attribute not supported for %qE "
4751 "in %<__builtin_has_attribute%>")),
4756 /* Clear EXPR to prevent considering it again below. */
4757 atlist
= TYPE_ATTRIBUTES (type
);
4761 else if (DECL_P (expr
))
4763 /* Set TYPE to the DECL's type to process it on the next
4765 atlist
= DECL_ATTRIBUTES (expr
);
4766 type
= TREE_TYPE (expr
);
4770 type
= TREE_TYPE (expr
);
4771 atlist
= TYPE_ATTRIBUTES (type
);
4775 /* True when an attribute with the sought name (though not necessarily
4776 with the sought attributes) has been found on the attribute chain. */
4777 bool found_attr
= false;
4779 /* When clear, the first mismatched attribute argument results
4780 in failure. Otherwise, the first matched attribute argument
4781 results in success. */
4782 bool attr_nonnull
= !strcmp ("nonnull", namestr
);
4783 bool ignore_mismatches
= attr_nonnull
;
4785 /* Iterate over the instances of the sought attribute on the DECL or
4786 TYPE (there may be multiple instances with different arguments). */
4787 for (; (atlist
= lookup_attribute (namestr
, atlist
));
4788 found_attr
= true, atlist
= TREE_CHAIN (atlist
))
4790 /* If there are no arguments to match the result is true except
4791 for nonnull where the attribute with no arguments must match. */
4792 if (!TREE_VALUE (attr
))
4793 return attr_nonnull
? !TREE_VALUE (atlist
) : true;
4795 /* Attribute nonnull with no arguments subsumes all values of
4796 the attribute. FIXME: This is overly broad since it only
4797 applies to pointer arguments, but querying non-pointer
4798 arguments is diagnosed. */
4799 if (!TREE_VALUE (atlist
) && attr_nonnull
)
4802 /* Iterate over the DECL or TYPE attribute argument's values. */
4803 for (tree val
= TREE_VALUE (atlist
); val
; val
= TREE_CHAIN (val
))
4805 /* Iterate over the arguments in the sought attribute comparing
4806 their values to those specified for the DECL or TYPE. */
4807 for (tree arg
= TREE_VALUE (attr
); arg
; arg
= TREE_CHAIN (arg
))
4809 tree v1
= TREE_VALUE (val
);
4810 tree v2
= TREE_VALUE (arg
);
4817 if (TREE_CODE (v1
) == IDENTIFIER_NODE
4818 || TREE_CODE (v2
) == IDENTIFIER_NODE
)
4819 /* Two identifiers are the same if their values are
4820 equal (that's handled above). Otherwise ther are
4821 either not the same or oneis not an identifier. */
4824 /* Convert to make them equality-comparable. */
4828 /* A positive value indicates equality, negative means
4830 if (simple_cst_equal (v1
, v2
) == 1)
4833 if (!ignore_mismatches
)
4841 /* Some attributes are encoded directly in the tree node. */
4842 if (!strcmp ("aligned", namestr
))
4844 if (tree arg
= TREE_VALUE (attr
))
4846 arg
= convert (TREE_VALUE (arg
));
4847 if (!tree_fits_uhwi_p (arg
))
4848 /* Invalid argument. */;
4849 else if (expr
&& DECL_P (expr
)
4850 && DECL_USER_ALIGN (expr
))
4851 found_match
= DECL_ALIGN_UNIT (expr
) == tree_to_uhwi (arg
);
4852 else if (type
&& TYPE_USER_ALIGN (type
))
4853 found_match
= TYPE_ALIGN_UNIT (type
) == tree_to_uhwi (arg
);
4855 else if (expr
&& DECL_P (expr
))
4856 found_match
= DECL_USER_ALIGN (expr
);
4858 found_match
= TYPE_USER_ALIGN (type
);
4860 else if (!strcmp ("const", namestr
))
4862 if (expr
&& DECL_P (expr
))
4863 found_match
= TREE_READONLY (expr
);
4865 else if (!strcmp ("noreturn", namestr
))
4867 /* C11 _Noreturn sets the volatile bit without attaching
4868 an attribute to the decl. */
4871 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (expr
)))
4872 found_match
= TREE_THIS_VOLATILE (expr
);
4874 else if (!strcmp ("pure", namestr
))
4876 if (expr
&& DECL_P (expr
))
4877 found_match
= DECL_PURE_P (expr
);
4879 else if (!strcmp ("deprecated", namestr
))
4881 found_match
= TREE_DEPRECATED (expr
? expr
: type
);
4885 else if (!strcmp ("vector_size", namestr
))
4887 if (!type
|| !VECTOR_TYPE_P (type
))
4890 if (tree arg
= TREE_VALUE (attr
))
4892 /* Compare the vector size argument for equality. */
4893 arg
= convert (TREE_VALUE (arg
));
4894 return tree_int_cst_equal (arg
, TYPE_SIZE_UNIT (type
)) == 1;
4899 else if (!strcmp ("warn_if_not_aligned", namestr
))
4901 if (tree arg
= TREE_VALUE (attr
))
4903 arg
= convert (TREE_VALUE (arg
));
4904 if (expr
&& DECL_P (expr
))
4905 found_match
= (DECL_WARN_IF_NOT_ALIGN (expr
)
4906 == tree_to_uhwi (arg
) * BITS_PER_UNIT
);
4908 found_match
= (TYPE_WARN_IF_NOT_ALIGN (type
)
4909 == tree_to_uhwi (arg
) * BITS_PER_UNIT
);
4911 else if (expr
&& DECL_P (expr
))
4912 found_match
= DECL_WARN_IF_NOT_ALIGN (expr
);
4914 found_match
= TYPE_WARN_IF_NOT_ALIGN (type
);
4916 else if (!strcmp ("transparent_union", namestr
))
4919 found_match
= TYPE_TRANSPARENT_AGGR (type
) != 0;
4921 else if (!strcmp ("mode", namestr
))
4923 /* Finally issue a warning for attributes that cannot
4924 be supported in this context. Attribute mode is not
4925 added to a symbol and cannot be determined from it. */
4926 warning_at (atloc
, OPT_Wattributes
,
4927 "%qs attribute not supported in "
4928 "%<__builtin_has_attribute%>", namestr
);