]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/c-family/c-attribs.c
Update copyright years.
[thirdparty/gcc.git] / gcc / c-family / c-attribs.c
1 /* C-family attributes handling.
2 Copyright (C) 1992-2022 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #define INCLUDE_STRING
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "target.h"
25 #include "function.h"
26 #include "tree.h"
27 #include "memmodel.h"
28 #include "c-common.h"
29 #include "gimple-expr.h"
30 #include "tm_p.h"
31 #include "stringpool.h"
32 #include "cgraph.h"
33 #include "diagnostic.h"
34 #include "intl.h"
35 #include "stor-layout.h"
36 #include "calls.h"
37 #include "attribs.h"
38 #include "varasm.h"
39 #include "trans-mem.h"
40 #include "c-objc.h"
41 #include "common/common-target.h"
42 #include "langhooks.h"
43 #include "tree-inline.h"
44 #include "toplev.h"
45 #include "tree-iterator.h"
46 #include "opts.h"
47 #include "gimplify.h"
48 #include "tree-pretty-print.h"
49 #include "gcc-rich-location.h"
50
51 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
52 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
53 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
54 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
55 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
56 static tree handle_no_sanitize_attribute (tree *, tree, tree, int, bool *);
57 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
58 int, bool *);
59 static tree handle_no_sanitize_thread_attribute (tree *, tree, tree,
60 int, bool *);
61 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
62 int, bool *);
63 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
64 bool *);
65 static tree handle_no_sanitize_coverage_attribute (tree *, tree, tree, int,
66 bool *);
67 static tree handle_asan_odr_indicator_attribute (tree *, tree, tree, int,
68 bool *);
69 static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
70 static tree handle_no_stack_protector_function_attribute (tree *, tree,
71 tree, int, bool *);
72 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
73 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
74 static tree handle_nocf_check_attribute (tree *, tree, tree, int, bool *);
75 static tree handle_symver_attribute (tree *, tree, tree, int, bool *);
76 static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
77 static tree handle_noipa_attribute (tree *, tree, tree, int, bool *);
78 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
79 static tree handle_always_inline_attribute (tree *, tree, tree, int,
80 bool *);
81 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
82 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
83 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
84 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
85 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
86 static tree handle_uninitialized_attribute (tree *, tree, tree, int, bool *);
87 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
88 bool *);
89 static tree handle_no_reorder_attribute (tree *, tree, tree, int,
90 bool *);
91 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
92 static tree handle_transparent_union_attribute (tree *, tree, tree,
93 int, bool *);
94 static tree handle_scalar_storage_order_attribute (tree *, tree, tree,
95 int, bool *);
96 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
97 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
98 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
99 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
100 static tree handle_special_var_sec_attribute (tree *, tree, tree, int, bool *);
101 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
102 static tree handle_warn_if_not_aligned_attribute (tree *, tree, tree,
103 int, bool *);
104 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
105 static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
106 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
107 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
108 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
109 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
110 static tree handle_visibility_attribute (tree *, tree, tree, int,
111 bool *);
112 static tree handle_tls_model_attribute (tree *, tree, tree, int,
113 bool *);
114 static tree handle_no_instrument_function_attribute (tree *, tree,
115 tree, int, bool *);
116 static tree handle_no_profile_instrument_function_attribute (tree *, tree,
117 tree, int, bool *);
118 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
119 static tree handle_dealloc_attribute (tree *, tree, tree, int, bool *);
120 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
121 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
122 bool *);
123 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
124 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
125 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
126 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
127 static tree handle_unavailable_attribute (tree *, tree, tree, int,
128 bool *);
129 static tree handle_vector_size_attribute (tree *, tree, tree, int,
130 bool *) ATTRIBUTE_NONNULL(3);
131 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
132 static tree handle_nonstring_attribute (tree *, tree, tree, int, bool *);
133 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
134 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
135 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
136 bool *);
137 static tree handle_access_attribute (tree *, tree, tree, int, bool *);
138
139 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
140 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
141 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
142 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
143 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
144 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
145 static tree handle_target_clones_attribute (tree *, tree, tree, int, bool *);
146 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
147 static tree ignore_attribute (tree *, tree, tree, int, bool *);
148 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
149 static tree handle_zero_call_used_regs_attribute (tree *, tree, tree, int,
150 bool *);
151 static tree handle_argspec_attribute (tree *, tree, tree, int, bool *);
152 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
153 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
154 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
155 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
156 bool *);
157 static tree handle_omp_declare_variant_attribute (tree *, tree, tree, int,
158 bool *);
159 static tree handle_simd_attribute (tree *, tree, tree, int, bool *);
160 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
161 bool *);
162 static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
163 static tree handle_patchable_function_entry_attribute (tree *, tree, tree,
164 int, bool *);
165 static tree handle_copy_attribute (tree *, tree, tree, int, bool *);
166 static tree handle_nsobject_attribute (tree *, tree, tree, int, bool *);
167 static tree handle_objc_root_class_attribute (tree *, tree, tree, int, bool *);
168 static tree handle_objc_nullability_attribute (tree *, tree, tree, int, bool *);
169 static tree handle_signed_bool_precision_attribute (tree *, tree, tree, int,
170 bool *);
171 static tree handle_retain_attribute (tree *, tree, tree, int, bool *);
172
173 /* Helper to define attribute exclusions. */
174 #define ATTR_EXCL(name, function, type, variable) \
175 { name, function, type, variable }
176
177 /* Define attributes that are mutually exclusive with one another. */
178 static const struct attribute_spec::exclusions attr_aligned_exclusions[] =
179 {
180 /* Attribute name exclusion applies to:
181 function, type, variable */
182 ATTR_EXCL ("aligned", true, false, false),
183 ATTR_EXCL ("packed", true, false, false),
184 ATTR_EXCL (NULL, false, false, false)
185 };
186
187 extern const struct attribute_spec::exclusions attr_cold_hot_exclusions[] =
188 {
189 ATTR_EXCL ("cold", true, true, true),
190 ATTR_EXCL ("hot", true, true, true),
191 ATTR_EXCL (NULL, false, false, false)
192 };
193
194 static const struct attribute_spec::exclusions attr_common_exclusions[] =
195 {
196 ATTR_EXCL ("common", true, true, true),
197 ATTR_EXCL ("nocommon", true, true, true),
198 ATTR_EXCL (NULL, false, false, false),
199 };
200
201 static const struct attribute_spec::exclusions attr_inline_exclusions[] =
202 {
203 ATTR_EXCL ("noinline", true, true, true),
204 ATTR_EXCL (NULL, false, false, false),
205 };
206
207 static const struct attribute_spec::exclusions attr_noinline_exclusions[] =
208 {
209 ATTR_EXCL ("always_inline", true, true, true),
210 ATTR_EXCL ("gnu_inline", true, true, true),
211 ATTR_EXCL (NULL, false, false, false),
212 };
213
214 extern const struct attribute_spec::exclusions attr_noreturn_exclusions[] =
215 {
216 ATTR_EXCL ("alloc_align", true, true, true),
217 ATTR_EXCL ("alloc_size", true, true, true),
218 ATTR_EXCL ("const", true, true, true),
219 ATTR_EXCL ("malloc", true, true, true),
220 ATTR_EXCL ("pure", true, true, true),
221 ATTR_EXCL ("returns_twice", true, true, true),
222 ATTR_EXCL ("warn_unused_result", true, true, true),
223 ATTR_EXCL (NULL, false, false, false),
224 };
225
226 static const struct attribute_spec::exclusions
227 attr_warn_unused_result_exclusions[] =
228 {
229 ATTR_EXCL ("noreturn", true, true, true),
230 ATTR_EXCL ("warn_unused_result", true, true, true),
231 ATTR_EXCL (NULL, false, false, false),
232 };
233
234 static const struct attribute_spec::exclusions attr_returns_twice_exclusions[] =
235 {
236 ATTR_EXCL ("noreturn", true, true, true),
237 ATTR_EXCL (NULL, false, false, false),
238 };
239
240 /* Exclusions that apply to attribute alloc_align, alloc_size, and malloc. */
241 static const struct attribute_spec::exclusions attr_alloc_exclusions[] =
242 {
243 ATTR_EXCL ("const", true, true, true),
244 ATTR_EXCL ("noreturn", true, true, true),
245 ATTR_EXCL ("pure", true, true, true),
246 ATTR_EXCL (NULL, false, false, false),
247 };
248
249 static const struct attribute_spec::exclusions attr_const_pure_exclusions[] =
250 {
251 ATTR_EXCL ("const", true, true, true),
252 ATTR_EXCL ("alloc_align", true, true, true),
253 ATTR_EXCL ("alloc_size", true, true, true),
254 ATTR_EXCL ("malloc", true, true, true),
255 ATTR_EXCL ("noreturn", true, true, true),
256 ATTR_EXCL ("pure", true, true, true),
257 ATTR_EXCL (NULL, false, false, false)
258 };
259
260 /* Exclusions that apply to attributes that put declarations in specific
261 sections. */
262 static const struct attribute_spec::exclusions attr_section_exclusions[] =
263 {
264 ATTR_EXCL ("noinit", true, true, true),
265 ATTR_EXCL ("persistent", true, true, true),
266 ATTR_EXCL ("section", true, true, true),
267 ATTR_EXCL (NULL, false, false, false),
268 };
269
270 static const struct attribute_spec::exclusions attr_stack_protect_exclusions[] =
271 {
272 ATTR_EXCL ("stack_protect", true, false, false),
273 ATTR_EXCL ("no_stack_protector", true, false, false),
274 ATTR_EXCL (NULL, false, false, false),
275 };
276
277
278 /* Table of machine-independent attributes common to all C-like languages.
279
280 Current list of processed common attributes: nonnull. */
281 const struct attribute_spec c_common_attribute_table[] =
282 {
283 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
284 affects_type_identity, handler, exclude } */
285 { "signed_bool_precision", 1, 1, false, true, false, true,
286 handle_signed_bool_precision_attribute, NULL },
287 { "packed", 0, 0, false, false, false, false,
288 handle_packed_attribute,
289 attr_aligned_exclusions },
290 { "nocommon", 0, 0, true, false, false, false,
291 handle_nocommon_attribute,
292 attr_common_exclusions },
293 { "common", 0, 0, true, false, false, false,
294 handle_common_attribute,
295 attr_common_exclusions },
296 /* FIXME: logically, noreturn attributes should be listed as
297 "false, true, true" and apply to function types. But implementing this
298 would require all the places in the compiler that use TREE_THIS_VOLATILE
299 on a decl to identify non-returning functions to be located and fixed
300 to check the function type instead. */
301 { "noreturn", 0, 0, true, false, false, false,
302 handle_noreturn_attribute,
303 attr_noreturn_exclusions },
304 { "volatile", 0, 0, true, false, false, false,
305 handle_noreturn_attribute, NULL },
306 { "stack_protect", 0, 0, true, false, false, false,
307 handle_stack_protect_attribute,
308 attr_stack_protect_exclusions },
309 { "no_stack_protector", 0, 0, true, false, false, false,
310 handle_no_stack_protector_function_attribute,
311 attr_stack_protect_exclusions },
312 { "noinline", 0, 0, true, false, false, false,
313 handle_noinline_attribute,
314 attr_noinline_exclusions },
315 { "noclone", 0, 0, true, false, false, false,
316 handle_noclone_attribute, NULL },
317 { "no_icf", 0, 0, true, false, false, false,
318 handle_noicf_attribute, NULL },
319 { "noipa", 0, 0, true, false, false, false,
320 handle_noipa_attribute, NULL },
321 { "leaf", 0, 0, true, false, false, false,
322 handle_leaf_attribute, NULL },
323 { "always_inline", 0, 0, true, false, false, false,
324 handle_always_inline_attribute,
325 attr_inline_exclusions },
326 { "gnu_inline", 0, 0, true, false, false, false,
327 handle_gnu_inline_attribute,
328 attr_inline_exclusions },
329 { "artificial", 0, 0, true, false, false, false,
330 handle_artificial_attribute, NULL },
331 { "flatten", 0, 0, true, false, false, false,
332 handle_flatten_attribute, NULL },
333 { "used", 0, 0, true, false, false, false,
334 handle_used_attribute, NULL },
335 { "unused", 0, 0, false, false, false, false,
336 handle_unused_attribute, NULL },
337 { "uninitialized", 0, 0, true, false, false, false,
338 handle_uninitialized_attribute, NULL },
339 { "retain", 0, 0, true, false, false, false,
340 handle_retain_attribute, NULL },
341 { "externally_visible", 0, 0, true, false, false, false,
342 handle_externally_visible_attribute, NULL },
343 { "no_reorder", 0, 0, true, false, false, false,
344 handle_no_reorder_attribute, NULL },
345 /* The same comments as for noreturn attributes apply to const ones. */
346 { "const", 0, 0, true, false, false, false,
347 handle_const_attribute,
348 attr_const_pure_exclusions },
349 { "scalar_storage_order", 1, 1, false, false, false, false,
350 handle_scalar_storage_order_attribute, NULL },
351 { "transparent_union", 0, 0, false, false, false, false,
352 handle_transparent_union_attribute, NULL },
353 { "constructor", 0, 1, true, false, false, false,
354 handle_constructor_attribute, NULL },
355 { "destructor", 0, 1, true, false, false, false,
356 handle_destructor_attribute, NULL },
357 { "mode", 1, 1, false, true, false, false,
358 handle_mode_attribute, NULL },
359 { "section", 1, 1, true, false, false, false,
360 handle_section_attribute, attr_section_exclusions },
361 { "aligned", 0, 1, false, false, false, false,
362 handle_aligned_attribute,
363 attr_aligned_exclusions },
364 { "warn_if_not_aligned", 0, 1, false, false, false, false,
365 handle_warn_if_not_aligned_attribute, NULL },
366 { "weak", 0, 0, true, false, false, false,
367 handle_weak_attribute, NULL },
368 { "noplt", 0, 0, true, false, false, false,
369 handle_noplt_attribute, NULL },
370 { "ifunc", 1, 1, true, false, false, false,
371 handle_ifunc_attribute, NULL },
372 { "alias", 1, 1, true, false, false, false,
373 handle_alias_attribute, NULL },
374 { "weakref", 0, 1, true, false, false, false,
375 handle_weakref_attribute, NULL },
376 { "no_instrument_function", 0, 0, true, false, false, false,
377 handle_no_instrument_function_attribute,
378 NULL },
379 { "no_profile_instrument_function", 0, 0, true, false, false, false,
380 handle_no_profile_instrument_function_attribute,
381 NULL },
382 { "malloc", 0, 2, true, false, false, false,
383 handle_malloc_attribute, attr_alloc_exclusions },
384 { "returns_twice", 0, 0, true, false, false, false,
385 handle_returns_twice_attribute,
386 attr_returns_twice_exclusions },
387 { "no_stack_limit", 0, 0, true, false, false, false,
388 handle_no_limit_stack_attribute, NULL },
389 { "pure", 0, 0, true, false, false, false,
390 handle_pure_attribute,
391 attr_const_pure_exclusions },
392 { "transaction_callable", 0, 0, false, true, false, false,
393 handle_tm_attribute, NULL },
394 { "transaction_unsafe", 0, 0, false, true, false, true,
395 handle_tm_attribute, NULL },
396 { "transaction_safe", 0, 0, false, true, false, true,
397 handle_tm_attribute, NULL },
398 { "transaction_safe_dynamic", 0, 0, true, false, false, false,
399 handle_tm_attribute, NULL },
400 { "transaction_may_cancel_outer", 0, 0, false, true, false, false,
401 handle_tm_attribute, NULL },
402 /* ??? These two attributes didn't make the transition from the
403 Intel language document to the multi-vendor language document. */
404 { "transaction_pure", 0, 0, false, true, false, false,
405 handle_tm_attribute, NULL },
406 { "transaction_wrap", 1, 1, true, false, false, false,
407 handle_tm_wrap_attribute, NULL },
408 /* For internal use (marking of builtins) only. The name contains space
409 to prevent its usage in source code. */
410 { "no vops", 0, 0, true, false, false, false,
411 handle_novops_attribute, NULL },
412 { "deprecated", 0, 1, false, false, false, false,
413 handle_deprecated_attribute, NULL },
414 { "unavailable", 0, 1, false, false, false, false,
415 handle_unavailable_attribute, NULL },
416 { "vector_size", 1, 1, false, true, false, true,
417 handle_vector_size_attribute, NULL },
418 { "visibility", 1, 1, false, false, false, false,
419 handle_visibility_attribute, NULL },
420 { "tls_model", 1, 1, true, false, false, false,
421 handle_tls_model_attribute, NULL },
422 { "nonnull", 0, -1, false, true, true, false,
423 handle_nonnull_attribute, NULL },
424 { "nonstring", 0, 0, true, false, false, false,
425 handle_nonstring_attribute, NULL },
426 { "nothrow", 0, 0, true, false, false, false,
427 handle_nothrow_attribute, NULL },
428 { "may_alias", 0, 0, false, true, false, false, NULL, NULL },
429 { "cleanup", 1, 1, true, false, false, false,
430 handle_cleanup_attribute, NULL },
431 { "warn_unused_result", 0, 0, false, true, true, false,
432 handle_warn_unused_result_attribute,
433 attr_warn_unused_result_exclusions },
434 { "sentinel", 0, 1, false, true, true, false,
435 handle_sentinel_attribute, NULL },
436 /* For internal use (marking of builtins) only. The name contains space
437 to prevent its usage in source code. */
438 { "type generic", 0, 0, false, true, true, false,
439 handle_type_generic_attribute, NULL },
440 { "alloc_size", 1, 2, false, true, true, false,
441 handle_alloc_size_attribute,
442 attr_alloc_exclusions },
443 { "cold", 0, 0, true, false, false, false,
444 handle_cold_attribute,
445 attr_cold_hot_exclusions },
446 { "hot", 0, 0, true, false, false, false,
447 handle_hot_attribute,
448 attr_cold_hot_exclusions },
449 { "no_address_safety_analysis",
450 0, 0, true, false, false, false,
451 handle_no_address_safety_analysis_attribute,
452 NULL },
453 { "no_sanitize", 1, -1, true, false, false, false,
454 handle_no_sanitize_attribute, NULL },
455 { "no_sanitize_address", 0, 0, true, false, false, false,
456 handle_no_sanitize_address_attribute, NULL },
457 { "no_sanitize_thread", 0, 0, true, false, false, false,
458 handle_no_sanitize_thread_attribute, NULL },
459 { "no_sanitize_undefined", 0, 0, true, false, false, false,
460 handle_no_sanitize_undefined_attribute, NULL },
461 { "no_sanitize_coverage", 0, 0, true, false, false, false,
462 handle_no_sanitize_coverage_attribute, NULL },
463 { "asan odr indicator", 0, 0, true, false, false, false,
464 handle_asan_odr_indicator_attribute, NULL },
465 { "warning", 1, 1, true, false, false, false,
466 handle_error_attribute, NULL },
467 { "error", 1, 1, true, false, false, false,
468 handle_error_attribute, NULL },
469 { "target", 1, -1, true, false, false, false,
470 handle_target_attribute, NULL },
471 { "target_clones", 1, -1, true, false, false, false,
472 handle_target_clones_attribute, NULL },
473 { "optimize", 1, -1, true, false, false, false,
474 handle_optimize_attribute, NULL },
475 /* For internal use only. The leading '*' both prevents its usage in
476 source code and signals that it may be overridden by machine tables. */
477 { "*tm regparm", 0, 0, false, true, true, false,
478 ignore_attribute, NULL },
479 { "no_split_stack", 0, 0, true, false, false, false,
480 handle_no_split_stack_attribute, NULL },
481 { "zero_call_used_regs", 1, 1, true, false, false, false,
482 handle_zero_call_used_regs_attribute, NULL },
483 /* For internal use only (marking of function arguments).
484 The name contains a space to prevent its usage in source code. */
485 { "arg spec", 1, -1, true, false, false, false,
486 handle_argspec_attribute, NULL },
487 /* For internal use (marking of builtins and runtime functions) only.
488 The name contains space to prevent its usage in source code. */
489 { "fn spec", 1, 1, false, true, true, false,
490 handle_fnspec_attribute, NULL },
491 { "warn_unused", 0, 0, false, false, false, false,
492 handle_warn_unused_attribute, NULL },
493 { "returns_nonnull", 0, 0, false, true, true, false,
494 handle_returns_nonnull_attribute, NULL },
495 { "omp declare simd", 0, -1, true, false, false, false,
496 handle_omp_declare_simd_attribute, NULL },
497 { "omp declare variant base", 0, -1, true, false, false, false,
498 handle_omp_declare_variant_attribute, NULL },
499 { "omp declare variant variant", 0, -1, true, false, false, false,
500 handle_omp_declare_variant_attribute, NULL },
501 { "simd", 0, 1, true, false, false, false,
502 handle_simd_attribute, NULL },
503 { "omp declare target", 0, -1, true, false, false, false,
504 handle_omp_declare_target_attribute, NULL },
505 { "omp declare target link", 0, 0, true, false, false, false,
506 handle_omp_declare_target_attribute, NULL },
507 { "omp declare target implicit", 0, 0, true, false, false, false,
508 handle_omp_declare_target_attribute, NULL },
509 { "omp declare target host", 0, 0, true, false, false, false,
510 handle_omp_declare_target_attribute, NULL },
511 { "omp declare target nohost", 0, 0, true, false, false, false,
512 handle_omp_declare_target_attribute, NULL },
513 { "omp declare target block", 0, 0, true, false, false, false,
514 handle_omp_declare_target_attribute, NULL },
515 { "alloc_align", 1, 1, false, true, true, false,
516 handle_alloc_align_attribute,
517 attr_alloc_exclusions },
518 { "assume_aligned", 1, 2, false, true, true, false,
519 handle_assume_aligned_attribute, NULL },
520 { "designated_init", 0, 0, false, true, false, false,
521 handle_designated_init_attribute, NULL },
522 { "fallthrough", 0, 0, false, false, false, false,
523 handle_fallthrough_attribute, NULL },
524 { "patchable_function_entry", 1, 2, true, false, false, false,
525 handle_patchable_function_entry_attribute,
526 NULL },
527 { "nocf_check", 0, 0, false, true, true, true,
528 handle_nocf_check_attribute, NULL },
529 { "symver", 1, -1, true, false, false, false,
530 handle_symver_attribute, NULL},
531 { "copy", 1, 1, false, false, false, false,
532 handle_copy_attribute, NULL },
533 { "noinit", 0, 0, true, false, false, false,
534 handle_special_var_sec_attribute, attr_section_exclusions },
535 { "persistent", 0, 0, true, false, false, false,
536 handle_special_var_sec_attribute, attr_section_exclusions },
537 { "access", 1, 3, false, true, true, false,
538 handle_access_attribute, NULL },
539 /* Attributes used by Objective-C. */
540 { "NSObject", 0, 0, true, false, false, false,
541 handle_nsobject_attribute, NULL },
542 { "objc_root_class", 0, 0, true, false, false, false,
543 handle_objc_root_class_attribute, NULL },
544 { "objc_nullability", 1, 1, true, false, false, false,
545 handle_objc_nullability_attribute, NULL },
546 { "*dealloc", 1, 2, true, false, false, false,
547 handle_dealloc_attribute, NULL },
548 { NULL, 0, 0, false, false, false, false, NULL, NULL }
549 };
550
551 /* Give the specifications for the format attributes, used by C and all
552 descendants.
553
554 Current list of processed format attributes: format, format_arg. */
555 const struct attribute_spec c_common_format_attribute_table[] =
556 {
557 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
558 affects_type_identity, handler, exclude } */
559 { "format", 3, 3, false, true, true, false,
560 handle_format_attribute, NULL },
561 { "format_arg", 1, 1, false, true, true, false,
562 handle_format_arg_attribute, NULL },
563 { NULL, 0, 0, false, false, false, false, NULL, NULL }
564 };
565
566 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
567 identifier as an argument, so the front end shouldn't look it up. */
568
569 bool
570 attribute_takes_identifier_p (const_tree attr_id)
571 {
572 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
573 if (spec == NULL)
574 /* Unknown attribute that we'll end up ignoring, return true so we
575 don't complain about an identifier argument. */
576 return true;
577 else if (!strcmp ("mode", spec->name)
578 || !strcmp ("format", spec->name)
579 || !strcmp ("cleanup", spec->name)
580 || !strcmp ("access", spec->name))
581 return true;
582 else
583 return targetm.attribute_takes_identifier_p (attr_id);
584 }
585
586 /* Verify that argument value POS at position ARGNO to attribute NAME
587 applied to function TYPE refers to a function parameter at position
588 POS and the expected type CODE. Treat CODE == INTEGER_TYPE as
589 matching all C integral types except bool. If successful, return
590 POS after default conversions, if any. Otherwise, issue appropriate
591 warnings and return null. A non-zero 1-based ARGNO should be passed
592 in by callers only for attributes with more than one argument. */
593
594 tree
595 positional_argument (const_tree fntype, const_tree atname, tree pos,
596 tree_code code, int argno /* = 0 */,
597 int flags /* = posargflags () */)
598 {
599 if (pos && TREE_CODE (pos) != IDENTIFIER_NODE
600 && TREE_CODE (pos) != FUNCTION_DECL)
601 pos = default_conversion (pos);
602
603 tree postype = TREE_TYPE (pos);
604 if (pos == error_mark_node || !postype)
605 {
606 /* Only mention the positional argument number when it's non-zero. */
607 if (argno < 1)
608 warning (OPT_Wattributes,
609 "%qE attribute argument is invalid", atname);
610 else
611 warning (OPT_Wattributes,
612 "%qE attribute argument %i is invalid", atname, argno);
613
614 return NULL_TREE;
615 }
616
617 if (!INTEGRAL_TYPE_P (postype))
618 {
619 /* Handle this case specially to avoid mentioning the value
620 of pointer constants in diagnostics. Only mention
621 the positional argument number when it's non-zero. */
622 if (argno < 1)
623 warning (OPT_Wattributes,
624 "%qE attribute argument has type %qT",
625 atname, postype);
626 else
627 warning (OPT_Wattributes,
628 "%qE attribute argument %i has type %qT",
629 atname, argno, postype);
630
631 return NULL_TREE;
632 }
633
634 if (TREE_CODE (pos) != INTEGER_CST)
635 {
636 /* Only mention the argument number when it's non-zero. */
637 if (argno < 1)
638 warning (OPT_Wattributes,
639 "%qE attribute argument value %qE is not an integer "
640 "constant",
641 atname, pos);
642 else
643 warning (OPT_Wattributes,
644 "%qE attribute argument %i value %qE is not an integer "
645 "constant",
646 atname, argno, pos);
647
648 return NULL_TREE;
649 }
650
651 /* Argument positions are 1-based. */
652 if (integer_zerop (pos))
653 {
654 if (flags & POSARG_ZERO)
655 /* Zero is explicitly allowed. */
656 return pos;
657
658 if (argno < 1)
659 warning (OPT_Wattributes,
660 "%qE attribute argument value %qE does not refer to "
661 "a function parameter",
662 atname, pos);
663 else
664 warning (OPT_Wattributes,
665 "%qE attribute argument %i value %qE does not refer to "
666 "a function parameter",
667 atname, argno, pos);
668
669 return NULL_TREE;
670 }
671
672 if (!prototype_p (fntype))
673 return pos;
674
675 /* Verify that the argument position does not exceed the number
676 of formal arguments to the function. When POSARG_ELLIPSIS
677 is set, ARGNO may be beyond the last argument of a vararg
678 function. */
679 unsigned nargs = type_num_arguments (fntype);
680 if (!nargs
681 || !tree_fits_uhwi_p (pos)
682 || ((flags & POSARG_ELLIPSIS) == 0
683 && !IN_RANGE (tree_to_uhwi (pos), 1, nargs)))
684 {
685
686 if (argno < 1)
687 warning (OPT_Wattributes,
688 "%qE attribute argument value %qE exceeds the number "
689 "of function parameters %u",
690 atname, pos, nargs);
691 else
692 warning (OPT_Wattributes,
693 "%qE attribute argument %i value %qE exceeds the number "
694 "of function parameters %u",
695 atname, argno, pos, nargs);
696 return NULL_TREE;
697 }
698
699 /* Verify that the type of the referenced formal argument matches
700 the expected type. */
701 unsigned HOST_WIDE_INT ipos = tree_to_uhwi (pos);
702
703 /* Zero was handled above. */
704 gcc_assert (ipos != 0);
705
706 if (tree argtype = type_argument_type (fntype, ipos))
707 {
708 if (argtype == error_mark_node)
709 return NULL_TREE;
710
711 if (flags & POSARG_ELLIPSIS)
712 {
713 if (argno < 1)
714 error ("%qE attribute argument value %qE does not refer to "
715 "a variable argument list",
716 atname, pos);
717 else
718 error ("%qE attribute argument %i value %qE does not refer to "
719 "a variable argument list",
720 atname, argno, pos);
721 return NULL_TREE;
722 }
723
724 /* Where the expected code is STRING_CST accept any pointer
725 expected by attribute format (this includes possibly qualified
726 char pointers and, for targets like Darwin, also pointers to
727 struct CFString). */
728 bool type_match;
729 if (code == STRING_CST)
730 type_match = valid_format_string_type_p (argtype);
731 else if (code == INTEGER_TYPE)
732 /* For integers, accept enums, wide characters and other types
733 that match INTEGRAL_TYPE_P except for bool. */
734 type_match = (INTEGRAL_TYPE_P (argtype)
735 && TREE_CODE (argtype) != BOOLEAN_TYPE);
736 else
737 type_match = TREE_CODE (argtype) == code;
738
739 if (!type_match)
740 {
741 if (code == STRING_CST)
742 {
743 /* Reject invalid format strings with an error. */
744 if (argno < 1)
745 error ("%qE attribute argument value %qE refers to "
746 "parameter type %qT",
747 atname, pos, argtype);
748 else
749 error ("%qE attribute argument %i value %qE refers to "
750 "parameter type %qT",
751 atname, argno, pos, argtype);
752
753 return NULL_TREE;
754 }
755
756 if (argno < 1)
757 warning (OPT_Wattributes,
758 "%qE attribute argument value %qE refers to "
759 "parameter type %qT",
760 atname, pos, argtype);
761 else
762 warning (OPT_Wattributes,
763 "%qE attribute argument %i value %qE refers to "
764 "parameter type %qT",
765 atname, argno, pos, argtype);
766 return NULL_TREE;
767 }
768 }
769 else if (!(flags & POSARG_ELLIPSIS))
770 {
771 if (argno < 1)
772 warning (OPT_Wattributes,
773 "%qE attribute argument value %qE refers to "
774 "a variadic function parameter of unknown type",
775 atname, pos);
776 else
777 warning (OPT_Wattributes,
778 "%qE attribute argument %i value %qE refers to "
779 "a variadic function parameter of unknown type",
780 atname, argno, pos);
781 return NULL_TREE;
782 }
783
784 return pos;
785 }
786
787 /* Return the first of DECL or TYPE attributes installed in NODE if it's
788 a DECL, or TYPE attributes if it's a TYPE, or null otherwise. */
789
790 static tree
791 decl_or_type_attrs (tree node)
792 {
793 if (DECL_P (node))
794 {
795 if (tree attrs = DECL_ATTRIBUTES (node))
796 return attrs;
797
798 tree type = TREE_TYPE (node);
799 return TYPE_ATTRIBUTES (type);
800 }
801
802 if (TYPE_P (node))
803 return TYPE_ATTRIBUTES (node);
804
805 return NULL_TREE;
806 }
807
808 /* Given a pair of NODEs for arbitrary DECLs or TYPEs, validate one or
809 two integral or string attribute arguments NEWARGS to be applied to
810 NODE[0] for the absence of conflicts with the same attribute arguments
811 already applied to NODE[1]. Issue a warning for conflicts and return
812 false. Otherwise, when no conflicts are found, return true. */
813
814 static bool
815 validate_attr_args (tree node[2], tree name, tree newargs[2])
816 {
817 /* First validate the arguments against those already applied to
818 the same declaration (or type). */
819 tree self[2] = { node[0], node[0] };
820 if (node[0] != node[1] && !validate_attr_args (self, name, newargs))
821 return false;
822
823 if (!node[1])
824 return true;
825
826 /* Extract the same attribute from the previous declaration or type. */
827 tree prevattr = decl_or_type_attrs (node[1]);
828 const char* const namestr = IDENTIFIER_POINTER (name);
829 prevattr = lookup_attribute (namestr, prevattr);
830 if (!prevattr)
831 return true;
832
833 /* Extract one or both attribute arguments. */
834 tree prevargs[2];
835 prevargs[0] = TREE_VALUE (TREE_VALUE (prevattr));
836 prevargs[1] = TREE_CHAIN (TREE_VALUE (prevattr));
837 if (prevargs[1])
838 prevargs[1] = TREE_VALUE (prevargs[1]);
839
840 /* Both arguments must be equal or, for the second pair, neither must
841 be provided to succeed. */
842 bool arg1eq, arg2eq;
843 if (TREE_CODE (newargs[0]) == INTEGER_CST)
844 {
845 arg1eq = tree_int_cst_equal (newargs[0], prevargs[0]);
846 if (newargs[1] && prevargs[1])
847 arg2eq = tree_int_cst_equal (newargs[1], prevargs[1]);
848 else
849 arg2eq = newargs[1] == prevargs[1];
850 }
851 else if (TREE_CODE (newargs[0]) == STRING_CST)
852 {
853 const char *s0 = TREE_STRING_POINTER (newargs[0]);
854 const char *s1 = TREE_STRING_POINTER (prevargs[0]);
855 arg1eq = strcmp (s0, s1) == 0;
856 if (newargs[1] && prevargs[1])
857 {
858 s0 = TREE_STRING_POINTER (newargs[1]);
859 s1 = TREE_STRING_POINTER (prevargs[1]);
860 arg2eq = strcmp (s0, s1) == 0;
861 }
862 else
863 arg2eq = newargs[1] == prevargs[1];
864 }
865 else
866 gcc_unreachable ();
867
868 if (arg1eq && arg2eq)
869 return true;
870
871 /* If the two locations are different print a note pointing to
872 the previous one. */
873 const location_t curloc = input_location;
874 const location_t prevloc =
875 DECL_P (node[1]) ? DECL_SOURCE_LOCATION (node[1]) : curloc;
876
877 /* Format the attribute specification for convenience. */
878 char newspec[80], prevspec[80];
879 if (newargs[1])
880 snprintf (newspec, sizeof newspec, "%s (%s, %s)", namestr,
881 print_generic_expr_to_str (newargs[0]),
882 print_generic_expr_to_str (newargs[1]));
883 else
884 snprintf (newspec, sizeof newspec, "%s (%s)", namestr,
885 print_generic_expr_to_str (newargs[0]));
886
887 if (prevargs[1])
888 snprintf (prevspec, sizeof prevspec, "%s (%s, %s)", namestr,
889 print_generic_expr_to_str (prevargs[0]),
890 print_generic_expr_to_str (prevargs[1]));
891 else
892 snprintf (prevspec, sizeof prevspec, "%s (%s)", namestr,
893 print_generic_expr_to_str (prevargs[0]));
894
895 if (warning_at (curloc, OPT_Wattributes,
896 "ignoring attribute %qs because it conflicts "
897 "with previous %qs",
898 newspec, prevspec)
899 && curloc != prevloc)
900 inform (prevloc, "previous declaration here");
901
902 return false;
903 }
904
905 /* Convenience wrapper for validate_attr_args to validate a single
906 attribute argument. Used by handlers for attributes that take
907 just a single argument. */
908
909 static bool
910 validate_attr_arg (tree node[2], tree name, tree newarg)
911 {
912 tree argarray[2] = { newarg, NULL_TREE };
913 return validate_attr_args (node, name, argarray);
914 }
915
916 /* Attribute handlers common to C front ends. */
917
918 /* Handle a "signed_bool_precision" attribute; arguments as in
919 struct attribute_spec.handler. */
920
921 static tree
922 handle_signed_bool_precision_attribute (tree *node, tree name, tree args,
923 int, bool *no_add_attrs)
924 {
925 *no_add_attrs = true;
926 if (!flag_gimple)
927 {
928 warning (OPT_Wattributes, "%qE attribute ignored", name);
929 return NULL_TREE;
930 }
931
932 if (!TYPE_P (*node) || TREE_CODE (*node) != BOOLEAN_TYPE)
933 {
934 warning (OPT_Wattributes, "%qE attribute only supported on "
935 "boolean types", name);
936 return NULL_TREE;
937 }
938
939 unsigned HOST_WIDE_INT prec = HOST_WIDE_INT_M1U;
940 if (tree_fits_uhwi_p (TREE_VALUE (args)))
941 prec = tree_to_uhwi (TREE_VALUE (args));
942 if (prec > MAX_FIXED_MODE_SIZE)
943 {
944 warning (OPT_Wattributes, "%qE attribute with unsupported boolean "
945 "precision", name);
946 return NULL_TREE;
947 }
948
949 tree new_type = build_nonstandard_boolean_type (prec);
950 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
951
952 return NULL_TREE;
953 }
954
955 /* Handle a "packed" attribute; arguments as in
956 struct attribute_spec.handler. */
957
958 static tree
959 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
960 int flags, bool *no_add_attrs)
961 {
962 if (TYPE_P (*node))
963 {
964 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
965 {
966 warning (OPT_Wattributes,
967 "%qE attribute ignored for type %qT", name, *node);
968 *no_add_attrs = true;
969 }
970 else
971 TYPE_PACKED (*node) = 1;
972 }
973 else if (TREE_CODE (*node) == FIELD_DECL)
974 {
975 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
976 /* Still pack bitfields. */
977 && ! DECL_C_BIT_FIELD (*node))
978 warning (OPT_Wattributes,
979 "%qE attribute ignored for field of type %qT",
980 name, TREE_TYPE (*node));
981 else
982 DECL_PACKED (*node) = 1;
983 }
984 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
985 used for DECL_REGISTER. It wouldn't mean anything anyway.
986 We can't set DECL_PACKED on the type of a TYPE_DECL, because
987 that changes what the typedef is typing. */
988 else
989 {
990 warning (OPT_Wattributes, "%qE attribute ignored", name);
991 *no_add_attrs = true;
992 }
993
994 return NULL_TREE;
995 }
996
997 /* Handle a "nocommon" attribute; arguments as in
998 struct attribute_spec.handler. */
999
1000 static tree
1001 handle_nocommon_attribute (tree *node, tree name,
1002 tree ARG_UNUSED (args),
1003 int ARG_UNUSED (flags), bool *no_add_attrs)
1004 {
1005 if (VAR_P (*node))
1006 DECL_COMMON (*node) = 0;
1007 else
1008 {
1009 warning (OPT_Wattributes, "%qE attribute ignored", name);
1010 *no_add_attrs = true;
1011 }
1012
1013 return NULL_TREE;
1014 }
1015
1016 /* Handle a "common" attribute; arguments as in
1017 struct attribute_spec.handler. */
1018
1019 static tree
1020 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
1021 int ARG_UNUSED (flags), bool *no_add_attrs)
1022 {
1023 if (VAR_P (*node))
1024 DECL_COMMON (*node) = 1;
1025 else
1026 {
1027 warning (OPT_Wattributes, "%qE attribute ignored", name);
1028 *no_add_attrs = true;
1029 }
1030
1031 return NULL_TREE;
1032 }
1033
1034 /* Handle a "noreturn" attribute; arguments as in
1035 struct attribute_spec.handler. */
1036
1037 tree
1038 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
1039 int ARG_UNUSED (flags), bool *no_add_attrs)
1040 {
1041 tree type = TREE_TYPE (*node);
1042
1043 /* See FIXME comment in c_common_attribute_table. */
1044 if (TREE_CODE (*node) == FUNCTION_DECL
1045 || objc_method_decl (TREE_CODE (*node)))
1046 TREE_THIS_VOLATILE (*node) = 1;
1047 else if (TREE_CODE (type) == POINTER_TYPE
1048 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
1049 TREE_TYPE (*node)
1050 = (build_qualified_type
1051 (build_pointer_type
1052 (build_type_variant (TREE_TYPE (type),
1053 TYPE_READONLY (TREE_TYPE (type)), 1)),
1054 TYPE_QUALS (type)));
1055 else
1056 {
1057 warning (OPT_Wattributes, "%qE attribute ignored", name);
1058 *no_add_attrs = true;
1059 }
1060
1061 return NULL_TREE;
1062 }
1063
1064 /* Handle a "hot" and attribute; arguments as in
1065 struct attribute_spec.handler. */
1066
1067 static tree
1068 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
1069 int ARG_UNUSED (flags), bool *no_add_attrs)
1070 {
1071 if (TREE_CODE (*node) == FUNCTION_DECL
1072 || TREE_CODE (*node) == LABEL_DECL)
1073 {
1074 /* Attribute hot processing is done later with lookup_attribute. */
1075 }
1076 else
1077 {
1078 warning (OPT_Wattributes, "%qE attribute ignored", name);
1079 *no_add_attrs = true;
1080 }
1081
1082 return NULL_TREE;
1083 }
1084
1085 /* Handle a "cold" and attribute; arguments as in
1086 struct attribute_spec.handler. */
1087
1088 static tree
1089 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
1090 int ARG_UNUSED (flags), bool *no_add_attrs)
1091 {
1092 if (TREE_CODE (*node) == FUNCTION_DECL
1093 || TREE_CODE (*node) == LABEL_DECL)
1094 {
1095 /* Attribute cold processing is done later with lookup_attribute. */
1096 }
1097 else
1098 {
1099 warning (OPT_Wattributes, "%qE attribute ignored", name);
1100 *no_add_attrs = true;
1101 }
1102
1103 return NULL_TREE;
1104 }
1105
1106 /* Add FLAGS for a function NODE to no_sanitize_flags in DECL_ATTRIBUTES. */
1107
1108 void
1109 add_no_sanitize_value (tree node, unsigned int flags)
1110 {
1111 tree attr = lookup_attribute ("no_sanitize", DECL_ATTRIBUTES (node));
1112 if (attr)
1113 {
1114 unsigned int old_value = tree_to_uhwi (TREE_VALUE (attr));
1115 flags |= old_value;
1116
1117 if (flags == old_value)
1118 return;
1119
1120 TREE_VALUE (attr) = build_int_cst (unsigned_type_node, flags);
1121 }
1122 else
1123 DECL_ATTRIBUTES (node)
1124 = tree_cons (get_identifier ("no_sanitize"),
1125 build_int_cst (unsigned_type_node, flags),
1126 DECL_ATTRIBUTES (node));
1127 }
1128
1129 /* Handle a "no_sanitize" attribute; arguments as in
1130 struct attribute_spec.handler. */
1131
1132 static tree
1133 handle_no_sanitize_attribute (tree *node, tree name, tree args, int,
1134 bool *no_add_attrs)
1135 {
1136 unsigned int flags = 0;
1137 *no_add_attrs = true;
1138 if (TREE_CODE (*node) != FUNCTION_DECL)
1139 {
1140 warning (OPT_Wattributes, "%qE attribute ignored", name);
1141 return NULL_TREE;
1142 }
1143
1144 for (; args; args = TREE_CHAIN (args))
1145 {
1146 tree id = TREE_VALUE (args);
1147 if (TREE_CODE (id) != STRING_CST)
1148 {
1149 error ("%qE argument not a string", name);
1150 return NULL_TREE;
1151 }
1152
1153 char *string = ASTRDUP (TREE_STRING_POINTER (id));
1154 flags |= parse_no_sanitize_attribute (string);
1155 }
1156
1157 add_no_sanitize_value (*node, flags);
1158
1159 return NULL_TREE;
1160 }
1161
1162 /* Handle a "no_sanitize_address" attribute; arguments as in
1163 struct attribute_spec.handler. */
1164
1165 static tree
1166 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
1167 bool *no_add_attrs)
1168 {
1169 *no_add_attrs = true;
1170 if (TREE_CODE (*node) != FUNCTION_DECL)
1171 warning (OPT_Wattributes, "%qE attribute ignored", name);
1172 else
1173 add_no_sanitize_value (*node, SANITIZE_ADDRESS);
1174
1175 return NULL_TREE;
1176 }
1177
1178 /* Handle a "no_sanitize_thread" attribute; arguments as in
1179 struct attribute_spec.handler. */
1180
1181 static tree
1182 handle_no_sanitize_thread_attribute (tree *node, tree name, tree, int,
1183 bool *no_add_attrs)
1184 {
1185 *no_add_attrs = true;
1186 if (TREE_CODE (*node) != FUNCTION_DECL)
1187 warning (OPT_Wattributes, "%qE attribute ignored", name);
1188 else
1189 add_no_sanitize_value (*node, SANITIZE_THREAD);
1190
1191 return NULL_TREE;
1192 }
1193
1194
1195 /* Handle a "no_address_safety_analysis" attribute; arguments as in
1196 struct attribute_spec.handler. */
1197
1198 static tree
1199 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
1200 bool *no_add_attrs)
1201 {
1202 *no_add_attrs = true;
1203 if (TREE_CODE (*node) != FUNCTION_DECL)
1204 warning (OPT_Wattributes, "%qE attribute ignored", name);
1205 else
1206 add_no_sanitize_value (*node, SANITIZE_ADDRESS);
1207
1208 return NULL_TREE;
1209 }
1210
1211 /* Handle a "no_sanitize_undefined" attribute; arguments as in
1212 struct attribute_spec.handler. */
1213
1214 static tree
1215 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
1216 bool *no_add_attrs)
1217 {
1218 *no_add_attrs = true;
1219 if (TREE_CODE (*node) != FUNCTION_DECL)
1220 warning (OPT_Wattributes, "%qE attribute ignored", name);
1221 else
1222 add_no_sanitize_value (*node,
1223 SANITIZE_UNDEFINED | SANITIZE_UNDEFINED_NONDEFAULT);
1224
1225 return NULL_TREE;
1226 }
1227
1228 /* Handle a "no_sanitize_coverage" attribute; arguments as in
1229 struct attribute_spec.handler. */
1230
1231 static tree
1232 handle_no_sanitize_coverage_attribute (tree *node, tree name, tree, int,
1233 bool *no_add_attrs)
1234 {
1235 if (TREE_CODE (*node) != FUNCTION_DECL)
1236 {
1237 warning (OPT_Wattributes, "%qE attribute ignored", name);
1238 *no_add_attrs = true;
1239 }
1240
1241 return NULL_TREE;
1242 }
1243
1244 /* Handle an "asan odr indicator" attribute; arguments as in
1245 struct attribute_spec.handler. */
1246
1247 static tree
1248 handle_asan_odr_indicator_attribute (tree *, tree, tree, int, bool *)
1249 {
1250 return NULL_TREE;
1251 }
1252
1253 /* Handle a "stack_protect" attribute; arguments as in
1254 struct attribute_spec.handler. */
1255
1256 static tree
1257 handle_stack_protect_attribute (tree *node, tree name, tree, int,
1258 bool *no_add_attrs)
1259 {
1260 if (TREE_CODE (*node) != FUNCTION_DECL)
1261 {
1262 warning (OPT_Wattributes, "%qE attribute ignored", name);
1263 *no_add_attrs = true;
1264 }
1265
1266 return NULL_TREE;
1267 }
1268
1269 /* Handle a "no_stack_protector" attribute; arguments as in
1270 struct attribute_spec.handler. */
1271
1272 static tree
1273 handle_no_stack_protector_function_attribute (tree *node, tree name, tree,
1274 int, bool *no_add_attrs)
1275 {
1276 if (TREE_CODE (*node) != FUNCTION_DECL)
1277 {
1278 warning (OPT_Wattributes, "%qE attribute ignored", name);
1279 *no_add_attrs = true;
1280 }
1281
1282 return NULL_TREE;
1283 }
1284
1285 /* Handle a "noipa" attribute; arguments as in
1286 struct attribute_spec.handler. */
1287
1288 static tree
1289 handle_noipa_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
1290 {
1291 if (TREE_CODE (*node) != FUNCTION_DECL)
1292 {
1293 warning (OPT_Wattributes, "%qE attribute ignored", name);
1294 *no_add_attrs = true;
1295 }
1296
1297 return NULL_TREE;
1298 }
1299
1300 /* Handle a "noinline" attribute; arguments as in
1301 struct attribute_spec.handler. */
1302
1303 static tree
1304 handle_noinline_attribute (tree *node, tree name,
1305 tree ARG_UNUSED (args),
1306 int ARG_UNUSED (flags), bool *no_add_attrs)
1307 {
1308 if (TREE_CODE (*node) == FUNCTION_DECL)
1309 {
1310 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
1311 {
1312 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
1313 "with attribute %qs", name, "always_inline");
1314 *no_add_attrs = true;
1315 }
1316 else
1317 DECL_UNINLINABLE (*node) = 1;
1318 }
1319 else
1320 {
1321 warning (OPT_Wattributes, "%qE attribute ignored", name);
1322 *no_add_attrs = true;
1323 }
1324
1325 return NULL_TREE;
1326 }
1327
1328 /* Handle a "noclone" attribute; arguments as in
1329 struct attribute_spec.handler. */
1330
1331 static tree
1332 handle_noclone_attribute (tree *node, tree name,
1333 tree ARG_UNUSED (args),
1334 int ARG_UNUSED (flags), bool *no_add_attrs)
1335 {
1336 if (TREE_CODE (*node) != FUNCTION_DECL)
1337 {
1338 warning (OPT_Wattributes, "%qE attribute ignored", name);
1339 *no_add_attrs = true;
1340 }
1341
1342 return NULL_TREE;
1343 }
1344
1345 /* Handle a "nocf_check" attribute; arguments as in
1346 struct attribute_spec.handler. */
1347
1348 static tree
1349 handle_nocf_check_attribute (tree *node, tree name,
1350 tree ARG_UNUSED (args),
1351 int ARG_UNUSED (flags), bool *no_add_attrs)
1352 {
1353 if (TREE_CODE (*node) != FUNCTION_TYPE
1354 && TREE_CODE (*node) != METHOD_TYPE)
1355 {
1356 warning (OPT_Wattributes, "%qE attribute ignored", name);
1357 *no_add_attrs = true;
1358 }
1359 else if (!(flag_cf_protection & CF_BRANCH))
1360 {
1361 warning (OPT_Wattributes, "%qE attribute ignored. Use "
1362 "%<-fcf-protection%> option to enable it",
1363 name);
1364 *no_add_attrs = true;
1365 }
1366
1367 return NULL_TREE;
1368 }
1369
1370 /* Handle a "no_icf" attribute; arguments as in
1371 struct attribute_spec.handler. */
1372
1373 static tree
1374 handle_noicf_attribute (tree *node, tree name,
1375 tree ARG_UNUSED (args),
1376 int ARG_UNUSED (flags), bool *no_add_attrs)
1377 {
1378 if (TREE_CODE (*node) != FUNCTION_DECL)
1379 {
1380 warning (OPT_Wattributes, "%qE attribute ignored", name);
1381 *no_add_attrs = true;
1382 }
1383
1384 return NULL_TREE;
1385 }
1386
1387
1388 /* Handle a "always_inline" attribute; arguments as in
1389 struct attribute_spec.handler. */
1390
1391 static tree
1392 handle_always_inline_attribute (tree *node, tree name,
1393 tree ARG_UNUSED (args),
1394 int ARG_UNUSED (flags),
1395 bool *no_add_attrs)
1396 {
1397 if (TREE_CODE (*node) == FUNCTION_DECL)
1398 {
1399 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
1400 {
1401 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
1402 "with %qs attribute", name, "noinline");
1403 *no_add_attrs = true;
1404 }
1405 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
1406 {
1407 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
1408 "with %qs attribute", name, "target_clones");
1409 *no_add_attrs = true;
1410 }
1411 else
1412 /* Set the attribute and mark it for disregarding inline
1413 limits. */
1414 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
1415 }
1416 else
1417 {
1418 warning (OPT_Wattributes, "%qE attribute ignored", name);
1419 *no_add_attrs = true;
1420 }
1421
1422 return NULL_TREE;
1423 }
1424
1425 /* Handle a "gnu_inline" attribute; arguments as in
1426 struct attribute_spec.handler. */
1427
1428 static tree
1429 handle_gnu_inline_attribute (tree *node, tree name,
1430 tree ARG_UNUSED (args),
1431 int ARG_UNUSED (flags),
1432 bool *no_add_attrs)
1433 {
1434 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
1435 {
1436 /* Do nothing else, just set the attribute. We'll get at
1437 it later with lookup_attribute. */
1438 }
1439 else
1440 {
1441 warning (OPT_Wattributes, "%qE attribute ignored", name);
1442 *no_add_attrs = true;
1443 }
1444
1445 return NULL_TREE;
1446 }
1447
1448 /* Handle a "leaf" attribute; arguments as in
1449 struct attribute_spec.handler. */
1450
1451 static tree
1452 handle_leaf_attribute (tree *node, tree name,
1453 tree ARG_UNUSED (args),
1454 int ARG_UNUSED (flags), bool *no_add_attrs)
1455 {
1456 if (TREE_CODE (*node) != FUNCTION_DECL)
1457 {
1458 warning (OPT_Wattributes, "%qE attribute ignored", name);
1459 *no_add_attrs = true;
1460 }
1461 if (!TREE_PUBLIC (*node))
1462 {
1463 warning (OPT_Wattributes, "%qE attribute has no effect on unit local "
1464 "functions", name);
1465 *no_add_attrs = true;
1466 }
1467
1468 return NULL_TREE;
1469 }
1470
1471 /* Handle an "artificial" attribute; arguments as in
1472 struct attribute_spec.handler. */
1473
1474 static tree
1475 handle_artificial_attribute (tree *node, tree name,
1476 tree ARG_UNUSED (args),
1477 int ARG_UNUSED (flags),
1478 bool *no_add_attrs)
1479 {
1480 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
1481 {
1482 /* Do nothing else, just set the attribute. We'll get at
1483 it later with lookup_attribute. */
1484 }
1485 else
1486 {
1487 warning (OPT_Wattributes, "%qE attribute ignored", name);
1488 *no_add_attrs = true;
1489 }
1490
1491 return NULL_TREE;
1492 }
1493
1494 /* Handle a "flatten" attribute; arguments as in
1495 struct attribute_spec.handler. */
1496
1497 static tree
1498 handle_flatten_attribute (tree *node, tree name,
1499 tree args ATTRIBUTE_UNUSED,
1500 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
1501 {
1502 if (TREE_CODE (*node) == FUNCTION_DECL)
1503 /* Do nothing else, just set the attribute. We'll get at
1504 it later with lookup_attribute. */
1505 ;
1506 else
1507 {
1508 warning (OPT_Wattributes, "%qE attribute ignored", name);
1509 *no_add_attrs = true;
1510 }
1511
1512 return NULL_TREE;
1513 }
1514
1515 /* Handle a "warning" or "error" attribute; arguments as in
1516 struct attribute_spec.handler. */
1517
1518 static tree
1519 handle_error_attribute (tree *node, tree name, tree args,
1520 int ARG_UNUSED (flags), bool *no_add_attrs)
1521 {
1522 if (TREE_CODE (*node) == FUNCTION_DECL
1523 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
1524 /* Do nothing else, just set the attribute. We'll get at
1525 it later with lookup_attribute. */
1526 ;
1527 else
1528 {
1529 warning (OPT_Wattributes, "%qE attribute ignored", name);
1530 *no_add_attrs = true;
1531 }
1532
1533 return NULL_TREE;
1534 }
1535
1536 /* Handle a "used" attribute; arguments as in
1537 struct attribute_spec.handler. */
1538
1539 static tree
1540 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
1541 int ARG_UNUSED (flags), bool *no_add_attrs)
1542 {
1543 tree node = *pnode;
1544
1545 if (TREE_CODE (node) == FUNCTION_DECL
1546 || (VAR_P (node) && TREE_STATIC (node))
1547 || (TREE_CODE (node) == TYPE_DECL))
1548 {
1549 TREE_USED (node) = 1;
1550 DECL_PRESERVE_P (node) = 1;
1551 if (VAR_P (node))
1552 DECL_READ_P (node) = 1;
1553 }
1554 else
1555 {
1556 warning (OPT_Wattributes, "%qE attribute ignored", name);
1557 *no_add_attrs = true;
1558 }
1559
1560 return NULL_TREE;
1561 }
1562
1563 /* Handle a "unused" attribute; arguments as in
1564 struct attribute_spec.handler. */
1565
1566 tree
1567 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
1568 int flags, bool *no_add_attrs)
1569 {
1570 if (DECL_P (*node))
1571 {
1572 tree decl = *node;
1573
1574 if (TREE_CODE (decl) == PARM_DECL
1575 || VAR_OR_FUNCTION_DECL_P (decl)
1576 || TREE_CODE (decl) == LABEL_DECL
1577 || TREE_CODE (decl) == CONST_DECL
1578 || TREE_CODE (decl) == FIELD_DECL
1579 || TREE_CODE (decl) == TYPE_DECL)
1580 {
1581 TREE_USED (decl) = 1;
1582 if (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
1583 DECL_READ_P (decl) = 1;
1584 }
1585 else
1586 {
1587 warning (OPT_Wattributes, "%qE attribute ignored", name);
1588 *no_add_attrs = true;
1589 }
1590 }
1591 else
1592 {
1593 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1594 *node = build_variant_type_copy (*node);
1595 TREE_USED (*node) = 1;
1596 }
1597
1598 return NULL_TREE;
1599 }
1600
1601 /* Handle a "retain" attribute; arguments as in
1602 struct attribute_spec.handler. */
1603
1604 static tree
1605 handle_retain_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
1606 int ARG_UNUSED (flags), bool *no_add_attrs)
1607 {
1608 tree node = *pnode;
1609
1610 if (SUPPORTS_SHF_GNU_RETAIN
1611 && (TREE_CODE (node) == FUNCTION_DECL
1612 || (VAR_P (node) && TREE_STATIC (node))))
1613 ;
1614 else
1615 {
1616 warning (OPT_Wattributes, "%qE attribute ignored", name);
1617 *no_add_attrs = true;
1618 }
1619
1620 return NULL_TREE;
1621 }
1622
1623 /* Handle an "uninitialized" attribute; arguments as in
1624 struct attribute_spec.handler. */
1625
1626 static tree
1627 handle_uninitialized_attribute (tree *node, tree name, tree ARG_UNUSED (args),
1628 int ARG_UNUSED (flags), bool *no_add_attrs)
1629 {
1630 tree decl = *node;
1631 if (!VAR_P (decl))
1632 {
1633 warning (OPT_Wattributes, "%qE attribute ignored because %qD "
1634 "is not a variable", name, decl);
1635 *no_add_attrs = true;
1636 }
1637 else if (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
1638 {
1639 warning (OPT_Wattributes, "%qE attribute ignored because %qD "
1640 "is not a local variable", name, decl);
1641 *no_add_attrs = true;
1642 }
1643
1644 return NULL_TREE;
1645 }
1646
1647 /* Handle a "externally_visible" attribute; arguments as in
1648 struct attribute_spec.handler. */
1649
1650 static tree
1651 handle_externally_visible_attribute (tree *pnode, tree name,
1652 tree ARG_UNUSED (args),
1653 int ARG_UNUSED (flags),
1654 bool *no_add_attrs)
1655 {
1656 tree node = *pnode;
1657
1658 if (VAR_OR_FUNCTION_DECL_P (node))
1659 {
1660 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
1661 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
1662 {
1663 warning (OPT_Wattributes,
1664 "%qE attribute have effect only on public objects", name);
1665 *no_add_attrs = true;
1666 }
1667 }
1668 else
1669 {
1670 warning (OPT_Wattributes, "%qE attribute ignored", name);
1671 *no_add_attrs = true;
1672 }
1673
1674 return NULL_TREE;
1675 }
1676
1677 /* Handle the "no_reorder" attribute. Arguments as in
1678 struct attribute_spec.handler. */
1679
1680 static tree
1681 handle_no_reorder_attribute (tree *pnode,
1682 tree name,
1683 tree,
1684 int,
1685 bool *no_add_attrs)
1686 {
1687 tree node = *pnode;
1688
1689 if (!VAR_OR_FUNCTION_DECL_P (node)
1690 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
1691 {
1692 warning (OPT_Wattributes,
1693 "%qE attribute only affects top level objects",
1694 name);
1695 *no_add_attrs = true;
1696 }
1697
1698 return NULL_TREE;
1699 }
1700
1701 /* Handle a "const" attribute; arguments as in
1702 struct attribute_spec.handler. */
1703
1704 static tree
1705 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
1706 int flags, bool *no_add_attrs)
1707 {
1708 tree type = TREE_TYPE (*node);
1709
1710 /* See FIXME comment on noreturn in c_common_attribute_table. */
1711 if (TREE_CODE (*node) == FUNCTION_DECL)
1712 TREE_READONLY (*node) = 1;
1713 else if (TREE_CODE (type) == POINTER_TYPE
1714 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
1715 TREE_TYPE (*node)
1716 = (build_qualified_type
1717 (build_pointer_type
1718 (build_type_variant (TREE_TYPE (type), 1,
1719 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
1720 TYPE_QUALS (type)));
1721 else
1722 {
1723 warning (OPT_Wattributes, "%qE attribute ignored", name);
1724 *no_add_attrs = true;
1725 }
1726
1727 /* void __builtin_unreachable(void) is const. Accept other such
1728 built-ins but warn on user-defined functions that return void. */
1729 if (!(flags & ATTR_FLAG_BUILT_IN)
1730 && TREE_CODE (*node) == FUNCTION_DECL
1731 && VOID_TYPE_P (TREE_TYPE (type)))
1732 warning (OPT_Wattributes, "%qE attribute on function "
1733 "returning %<void%>", name);
1734
1735 return NULL_TREE;
1736 }
1737
1738 /* Handle a "scalar_storage_order" attribute; arguments as in
1739 struct attribute_spec.handler. */
1740
1741 static tree
1742 handle_scalar_storage_order_attribute (tree *node, tree name, tree args,
1743 int flags, bool *no_add_attrs)
1744 {
1745 tree id = TREE_VALUE (args);
1746 tree type;
1747
1748 if (TREE_CODE (*node) == TYPE_DECL
1749 && ! (flags & ATTR_FLAG_CXX11))
1750 node = &TREE_TYPE (*node);
1751 type = *node;
1752
1753 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
1754 {
1755 error ("%qE attribute is not supported because endianness is not uniform",
1756 name);
1757 return NULL_TREE;
1758 }
1759
1760 if (RECORD_OR_UNION_TYPE_P (type) && !c_dialect_cxx ())
1761 {
1762 bool reverse = false;
1763
1764 if (TREE_CODE (id) == STRING_CST
1765 && strcmp (TREE_STRING_POINTER (id), "big-endian") == 0)
1766 reverse = !BYTES_BIG_ENDIAN;
1767 else if (TREE_CODE (id) == STRING_CST
1768 && strcmp (TREE_STRING_POINTER (id), "little-endian") == 0)
1769 reverse = BYTES_BIG_ENDIAN;
1770 else
1771 {
1772 error ("attribute %qE argument must be one of %qs or %qs",
1773 name, "big-endian", "little-endian");
1774 return NULL_TREE;
1775 }
1776
1777 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1778 {
1779 if (reverse)
1780 /* A type variant isn't good enough, since we don't want a cast
1781 to such a type to be removed as a no-op. */
1782 *node = type = build_duplicate_type (type);
1783 }
1784
1785 TYPE_REVERSE_STORAGE_ORDER (type) = reverse;
1786 return NULL_TREE;
1787 }
1788
1789 warning (OPT_Wattributes, "%qE attribute ignored", name);
1790 *no_add_attrs = true;
1791 return NULL_TREE;
1792 }
1793
1794 /* Handle a "transparent_union" attribute; arguments as in
1795 struct attribute_spec.handler. */
1796
1797 static tree
1798 handle_transparent_union_attribute (tree *node, tree name,
1799 tree ARG_UNUSED (args), int flags,
1800 bool *no_add_attrs)
1801 {
1802 tree type;
1803
1804 *no_add_attrs = true;
1805
1806 if (TREE_CODE (*node) == TYPE_DECL
1807 && ! (flags & ATTR_FLAG_CXX11))
1808 node = &TREE_TYPE (*node);
1809 type = *node;
1810
1811 if (TREE_CODE (type) == UNION_TYPE)
1812 {
1813 /* Make sure that the first field will work for a transparent union.
1814 If the type isn't complete yet, leave the check to the code in
1815 finish_struct. */
1816 if (TYPE_SIZE (type))
1817 {
1818 tree first = first_field (type);
1819 if (first == NULL_TREE
1820 || DECL_ARTIFICIAL (first)
1821 || TYPE_MODE (type) != DECL_MODE (first))
1822 goto ignored;
1823 }
1824
1825 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1826 {
1827 /* If the type isn't complete yet, setting the flag
1828 on a variant wouldn't ever be checked. */
1829 if (!TYPE_SIZE (type))
1830 goto ignored;
1831
1832 /* build_duplicate_type doesn't work for C++. */
1833 if (c_dialect_cxx ())
1834 goto ignored;
1835
1836 /* A type variant isn't good enough, since we don't want a cast
1837 to such a type to be removed as a no-op. */
1838 *node = type = build_duplicate_type (type);
1839 }
1840
1841 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
1842 TYPE_TRANSPARENT_AGGR (t) = 1;
1843 return NULL_TREE;
1844 }
1845
1846 ignored:
1847 warning (OPT_Wattributes, "%qE attribute ignored", name);
1848 return NULL_TREE;
1849 }
1850
1851 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
1852 get the requested priority for a constructor or destructor,
1853 possibly issuing diagnostics for invalid or reserved
1854 priorities. */
1855
1856 static priority_type
1857 get_priority (tree args, bool is_destructor)
1858 {
1859 HOST_WIDE_INT pri;
1860 tree arg;
1861
1862 if (!args)
1863 return DEFAULT_INIT_PRIORITY;
1864
1865 if (!SUPPORTS_INIT_PRIORITY)
1866 {
1867 if (is_destructor)
1868 error ("destructor priorities are not supported");
1869 else
1870 error ("constructor priorities are not supported");
1871 return DEFAULT_INIT_PRIORITY;
1872 }
1873
1874 arg = TREE_VALUE (args);
1875 if (TREE_CODE (arg) == IDENTIFIER_NODE)
1876 goto invalid;
1877 if (arg == error_mark_node)
1878 return DEFAULT_INIT_PRIORITY;
1879 arg = default_conversion (arg);
1880 if (!tree_fits_shwi_p (arg)
1881 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
1882 goto invalid;
1883
1884 pri = tree_to_shwi (arg);
1885 if (pri < 0 || pri > MAX_INIT_PRIORITY)
1886 goto invalid;
1887
1888 if (pri <= MAX_RESERVED_INIT_PRIORITY)
1889 {
1890 if (is_destructor)
1891 warning (OPT_Wprio_ctor_dtor,
1892 "destructor priorities from 0 to %d are reserved "
1893 "for the implementation",
1894 MAX_RESERVED_INIT_PRIORITY);
1895 else
1896 warning (OPT_Wprio_ctor_dtor,
1897 "constructor priorities from 0 to %d are reserved "
1898 "for the implementation",
1899 MAX_RESERVED_INIT_PRIORITY);
1900 }
1901 return pri;
1902
1903 invalid:
1904 if (is_destructor)
1905 error ("destructor priorities must be integers from 0 to %d inclusive",
1906 MAX_INIT_PRIORITY);
1907 else
1908 error ("constructor priorities must be integers from 0 to %d inclusive",
1909 MAX_INIT_PRIORITY);
1910 return DEFAULT_INIT_PRIORITY;
1911 }
1912
1913 /* Handle a "constructor" attribute; arguments as in
1914 struct attribute_spec.handler. */
1915
1916 static tree
1917 handle_constructor_attribute (tree *node, tree name, tree args,
1918 int ARG_UNUSED (flags),
1919 bool *no_add_attrs)
1920 {
1921 tree decl = *node;
1922 tree type = TREE_TYPE (decl);
1923
1924 if (TREE_CODE (decl) == FUNCTION_DECL
1925 && TREE_CODE (type) == FUNCTION_TYPE
1926 && decl_function_context (decl) == 0)
1927 {
1928 priority_type priority;
1929 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1930 priority = get_priority (args, /*is_destructor=*/false);
1931 SET_DECL_INIT_PRIORITY (decl, priority);
1932 TREE_USED (decl) = 1;
1933 }
1934 else
1935 {
1936 warning (OPT_Wattributes, "%qE attribute ignored", name);
1937 *no_add_attrs = true;
1938 }
1939
1940 return NULL_TREE;
1941 }
1942
1943 /* Handle a "destructor" attribute; arguments as in
1944 struct attribute_spec.handler. */
1945
1946 static tree
1947 handle_destructor_attribute (tree *node, tree name, tree args,
1948 int ARG_UNUSED (flags),
1949 bool *no_add_attrs)
1950 {
1951 tree decl = *node;
1952 tree type = TREE_TYPE (decl);
1953
1954 if (TREE_CODE (decl) == FUNCTION_DECL
1955 && TREE_CODE (type) == FUNCTION_TYPE
1956 && decl_function_context (decl) == 0)
1957 {
1958 priority_type priority;
1959 DECL_STATIC_DESTRUCTOR (decl) = 1;
1960 priority = get_priority (args, /*is_destructor=*/true);
1961 SET_DECL_FINI_PRIORITY (decl, priority);
1962 TREE_USED (decl) = 1;
1963 }
1964 else
1965 {
1966 warning (OPT_Wattributes, "%qE attribute ignored", name);
1967 *no_add_attrs = true;
1968 }
1969
1970 return NULL_TREE;
1971 }
1972
1973 /* Nonzero if the mode is a valid vector mode for this architecture.
1974 This returns nonzero even if there is no hardware support for the
1975 vector mode, but we can emulate with narrower modes. */
1976
1977 static bool
1978 vector_mode_valid_p (machine_mode mode)
1979 {
1980 enum mode_class mclass = GET_MODE_CLASS (mode);
1981
1982 /* Doh! What's going on? */
1983 if (mclass != MODE_VECTOR_INT
1984 && mclass != MODE_VECTOR_FLOAT
1985 && mclass != MODE_VECTOR_FRACT
1986 && mclass != MODE_VECTOR_UFRACT
1987 && mclass != MODE_VECTOR_ACCUM
1988 && mclass != MODE_VECTOR_UACCUM)
1989 return false;
1990
1991 /* Hardware support. Woo hoo! */
1992 if (targetm.vector_mode_supported_p (mode))
1993 return true;
1994
1995 /* We should probably return 1 if requesting V4DI and we have no DI,
1996 but we have V2DI, but this is probably very unlikely. */
1997
1998 /* If we have support for the inner mode, we can safely emulate it.
1999 We may not have V2DI, but me can emulate with a pair of DIs. */
2000 return targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
2001 }
2002
2003
2004 /* Handle a "mode" attribute; arguments as in
2005 struct attribute_spec.handler. */
2006
2007 static tree
2008 handle_mode_attribute (tree *node, tree name, tree args,
2009 int ARG_UNUSED (flags), bool *no_add_attrs)
2010 {
2011 tree type = *node;
2012 tree ident = TREE_VALUE (args);
2013
2014 *no_add_attrs = true;
2015
2016 if (TREE_CODE (ident) != IDENTIFIER_NODE)
2017 warning (OPT_Wattributes, "%qE attribute ignored", name);
2018 else
2019 {
2020 int j;
2021 const char *p = IDENTIFIER_POINTER (ident);
2022 int len = strlen (p);
2023 machine_mode mode = VOIDmode;
2024 tree typefm;
2025 bool valid_mode;
2026
2027 if (len > 4 && p[0] == '_' && p[1] == '_'
2028 && p[len - 1] == '_' && p[len - 2] == '_')
2029 {
2030 char *newp = (char *) alloca (len - 1);
2031
2032 strcpy (newp, &p[2]);
2033 newp[len - 4] = '\0';
2034 p = newp;
2035 }
2036
2037 /* Change this type to have a type with the specified mode.
2038 First check for the special modes. */
2039 if (!strcmp (p, "byte"))
2040 mode = byte_mode;
2041 else if (!strcmp (p, "word"))
2042 mode = word_mode;
2043 else if (!strcmp (p, "pointer"))
2044 mode = ptr_mode;
2045 else if (!strcmp (p, "libgcc_cmp_return"))
2046 mode = targetm.libgcc_cmp_return_mode ();
2047 else if (!strcmp (p, "libgcc_shift_count"))
2048 mode = targetm.libgcc_shift_count_mode ();
2049 else if (!strcmp (p, "unwind_word"))
2050 mode = targetm.unwind_word_mode ();
2051 else
2052 for (j = 0; j < NUM_MACHINE_MODES; j++)
2053 if (!strcmp (p, GET_MODE_NAME (j)))
2054 {
2055 mode = (machine_mode) j;
2056 break;
2057 }
2058
2059 if (mode == VOIDmode)
2060 {
2061 error ("unknown machine mode %qE", ident);
2062 return NULL_TREE;
2063 }
2064
2065 /* Allow the target a chance to translate MODE into something supported.
2066 See PR86324. */
2067 mode = targetm.translate_mode_attribute (mode);
2068
2069 valid_mode = false;
2070 switch (GET_MODE_CLASS (mode))
2071 {
2072 case MODE_INT:
2073 case MODE_PARTIAL_INT:
2074 case MODE_FLOAT:
2075 case MODE_DECIMAL_FLOAT:
2076 case MODE_FRACT:
2077 case MODE_UFRACT:
2078 case MODE_ACCUM:
2079 case MODE_UACCUM:
2080 valid_mode
2081 = targetm.scalar_mode_supported_p (as_a <scalar_mode> (mode));
2082 break;
2083
2084 case MODE_COMPLEX_INT:
2085 case MODE_COMPLEX_FLOAT:
2086 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
2087 break;
2088
2089 case MODE_VECTOR_INT:
2090 case MODE_VECTOR_FLOAT:
2091 case MODE_VECTOR_FRACT:
2092 case MODE_VECTOR_UFRACT:
2093 case MODE_VECTOR_ACCUM:
2094 case MODE_VECTOR_UACCUM:
2095 warning (OPT_Wattributes, "specifying vector types with "
2096 "%<__attribute__ ((mode))%> is deprecated");
2097 inform (input_location,
2098 "use %<__attribute__ ((vector_size))%> instead");
2099 valid_mode = vector_mode_valid_p (mode);
2100 break;
2101
2102 default:
2103 break;
2104 }
2105 if (!valid_mode)
2106 {
2107 error ("unable to emulate %qs", p);
2108 return NULL_TREE;
2109 }
2110
2111 if (POINTER_TYPE_P (type))
2112 {
2113 scalar_int_mode addr_mode;
2114 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
2115 tree (*fn)(tree, machine_mode, bool);
2116
2117 if (!is_a <scalar_int_mode> (mode, &addr_mode)
2118 || !targetm.addr_space.valid_pointer_mode (addr_mode, as))
2119 {
2120 error ("invalid pointer mode %qs", p);
2121 return NULL_TREE;
2122 }
2123
2124 if (TREE_CODE (type) == POINTER_TYPE)
2125 fn = build_pointer_type_for_mode;
2126 else
2127 fn = build_reference_type_for_mode;
2128 typefm = fn (TREE_TYPE (type), addr_mode, false);
2129 }
2130 else
2131 {
2132 /* For fixed-point modes, we need to test if the signness of type
2133 and the machine mode are consistent. */
2134 if (ALL_FIXED_POINT_MODE_P (mode)
2135 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
2136 {
2137 error ("signedness of type and machine mode %qs don%'t match", p);
2138 return NULL_TREE;
2139 }
2140 /* For fixed-point modes, we need to pass saturating info. */
2141 typefm = lang_hooks.types.type_for_mode (mode,
2142 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
2143 : TYPE_UNSIGNED (type));
2144 }
2145
2146 if (typefm == NULL_TREE)
2147 {
2148 error ("no data type for mode %qs", p);
2149 return NULL_TREE;
2150 }
2151 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2152 {
2153 /* For enumeral types, copy the precision from the integer
2154 type returned above. If not an INTEGER_TYPE, we can't use
2155 this mode for this type. */
2156 if (TREE_CODE (typefm) != INTEGER_TYPE)
2157 {
2158 error ("cannot use mode %qs for enumerated types", p);
2159 return NULL_TREE;
2160 }
2161
2162 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
2163 {
2164 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
2165 typefm = type;
2166 }
2167 else
2168 {
2169 /* We cannot build a type variant, as there's code that assumes
2170 that TYPE_MAIN_VARIANT has the same mode. This includes the
2171 debug generators. Instead, create a subrange type. This
2172 results in all of the enumeral values being emitted only once
2173 in the original, and the subtype gets them by reference. */
2174 if (TYPE_UNSIGNED (type))
2175 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
2176 else
2177 typefm = make_signed_type (TYPE_PRECISION (typefm));
2178 TREE_TYPE (typefm) = type;
2179 }
2180 *no_add_attrs = false;
2181 }
2182 else if (VECTOR_MODE_P (mode)
2183 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
2184 : TREE_CODE (type) != TREE_CODE (typefm))
2185 {
2186 error ("mode %qs applied to inappropriate type", p);
2187 return NULL_TREE;
2188 }
2189
2190 *node = build_qualified_type (typefm, TYPE_QUALS (type));
2191 }
2192
2193 return NULL_TREE;
2194 }
2195
2196 /* Handle a "section" attribute; arguments as in
2197 struct attribute_spec.handler. */
2198
2199 static tree
2200 handle_section_attribute (tree *node, tree name, tree args,
2201 int flags, bool *no_add_attrs)
2202 {
2203 tree decl = *node;
2204 tree res = NULL_TREE;
2205 tree argval = TREE_VALUE (args);
2206 const char* new_section_name;
2207
2208 if (!targetm_common.have_named_sections)
2209 {
2210 error_at (DECL_SOURCE_LOCATION (*node),
2211 "section attributes are not supported for this target");
2212 goto fail;
2213 }
2214
2215 if (!VAR_OR_FUNCTION_DECL_P (decl))
2216 {
2217 error ("section attribute not allowed for %q+D", *node);
2218 goto fail;
2219 }
2220
2221 if (TREE_CODE (argval) != STRING_CST)
2222 {
2223 error ("section attribute argument not a string constant");
2224 goto fail;
2225 }
2226
2227 if (VAR_P (decl)
2228 && current_function_decl != NULL_TREE
2229 && !TREE_STATIC (decl))
2230 {
2231 error_at (DECL_SOURCE_LOCATION (decl),
2232 "section attribute cannot be specified for local variables");
2233 goto fail;
2234 }
2235
2236 new_section_name = TREE_STRING_POINTER (argval);
2237
2238 /* The decl may have already been given a section attribute
2239 from a previous declaration. Ensure they match. */
2240 if (const char* const old_section_name = DECL_SECTION_NAME (decl))
2241 if (strcmp (old_section_name, new_section_name) != 0)
2242 {
2243 error ("section of %q+D conflicts with previous declaration",
2244 *node);
2245 goto fail;
2246 }
2247
2248 if (VAR_P (decl)
2249 && !targetm.have_tls && targetm.emutls.tmpl_section
2250 && DECL_THREAD_LOCAL_P (decl))
2251 {
2252 error ("section of %q+D cannot be overridden", *node);
2253 goto fail;
2254 }
2255
2256 if (!validate_attr_arg (node, name, argval))
2257 goto fail;
2258
2259 res = targetm.handle_generic_attribute (node, name, args, flags,
2260 no_add_attrs);
2261
2262 /* If the back end confirms the attribute can be added then continue onto
2263 final processing. */
2264 if (!(*no_add_attrs))
2265 {
2266 set_decl_section_name (decl, new_section_name);
2267 return res;
2268 }
2269
2270 fail:
2271 *no_add_attrs = true;
2272 return res;
2273 }
2274
2275 /* Common codes shared by handle_warn_if_not_aligned_attribute and
2276 handle_aligned_attribute. */
2277
2278 static tree
2279 common_handle_aligned_attribute (tree *node, tree name, tree args, int flags,
2280 bool *no_add_attrs,
2281 bool warn_if_not_aligned_p)
2282 {
2283 tree decl = NULL_TREE;
2284 tree *type = NULL;
2285 bool is_type = false;
2286 tree align_expr;
2287
2288 /* The last (already pushed) declaration with all validated attributes
2289 merged in or the current about-to-be-pushed one if one hasn't been
2290 yet. */
2291 tree last_decl = node[1] ? node[1] : *node;
2292
2293 if (args)
2294 {
2295 align_expr = TREE_VALUE (args);
2296 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
2297 && TREE_CODE (align_expr) != FUNCTION_DECL)
2298 align_expr = default_conversion (align_expr);
2299 }
2300 else
2301 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
2302
2303 if (DECL_P (*node))
2304 {
2305 decl = *node;
2306 type = &TREE_TYPE (decl);
2307 is_type = TREE_CODE (*node) == TYPE_DECL;
2308 }
2309 else if (TYPE_P (*node))
2310 type = node, is_type = true;
2311
2312 /* True to consider invalid alignments greater than MAX_OFILE_ALIGNMENT. */
2313 bool objfile = (TREE_CODE (*node) == FUNCTION_DECL
2314 || (VAR_P (*node) && TREE_STATIC (*node)));
2315 /* Log2 of specified alignment. */
2316 int pow2align = check_user_alignment (align_expr, objfile,
2317 /* warn_zero = */ true);
2318 if (pow2align == -1)
2319 {
2320 *no_add_attrs = true;
2321 return NULL_TREE;
2322 }
2323
2324 /* The alignment in bits corresponding to the specified alignment. */
2325 unsigned bitalign = (1U << pow2align) * BITS_PER_UNIT;
2326
2327 /* The alignment of the current declaration and that of the last
2328 pushed declaration, determined on demand below. */
2329 unsigned curalign = 0;
2330 unsigned lastalign = 0;
2331
2332 /* True when SET_DECL_ALIGN() should be called for the decl when
2333 *NO_ADD_ATTRS is false. */
2334 bool set_align = true;
2335 if (is_type)
2336 {
2337 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
2338 /* OK, modify the type in place. */;
2339 /* If we have a TYPE_DECL, then copy the type, so that we
2340 don't accidentally modify a builtin type. See pushdecl. */
2341 else if (decl && TREE_TYPE (decl) != error_mark_node
2342 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
2343 {
2344 tree tt = TREE_TYPE (decl);
2345 *type = build_variant_type_copy (*type);
2346 DECL_ORIGINAL_TYPE (decl) = tt;
2347 TYPE_NAME (*type) = decl;
2348 TREE_USED (*type) = TREE_USED (decl);
2349 TREE_TYPE (decl) = *type;
2350 }
2351 else
2352 *type = build_variant_type_copy (*type);
2353
2354 if (warn_if_not_aligned_p)
2355 {
2356 SET_TYPE_WARN_IF_NOT_ALIGN (*type, bitalign);
2357 warn_if_not_aligned_p = false;
2358 }
2359 else
2360 {
2361 SET_TYPE_ALIGN (*type, bitalign);
2362 TYPE_USER_ALIGN (*type) = 1;
2363 }
2364 }
2365 else if (! VAR_OR_FUNCTION_DECL_P (decl)
2366 && TREE_CODE (decl) != FIELD_DECL)
2367 {
2368 error ("alignment may not be specified for %q+D", decl);
2369 *no_add_attrs = true;
2370 }
2371 else if (TREE_CODE (decl) == FUNCTION_DECL
2372 && (((curalign = DECL_ALIGN (decl)) > bitalign)
2373 | ((lastalign = DECL_ALIGN (last_decl)) > bitalign)))
2374 {
2375 /* Either a prior attribute on the same declaration or one
2376 on a prior declaration of the same function specifies
2377 stricter alignment than this attribute. */
2378 bool note = (lastalign > curalign
2379 || (lastalign == curalign
2380 && (DECL_USER_ALIGN (last_decl)
2381 > DECL_USER_ALIGN (decl))));
2382 if (note)
2383 curalign = lastalign;
2384
2385 curalign /= BITS_PER_UNIT;
2386 unsigned newalign = bitalign / BITS_PER_UNIT;
2387
2388 auto_diagnostic_group d;
2389 if ((DECL_USER_ALIGN (decl)
2390 || DECL_USER_ALIGN (last_decl)))
2391 {
2392 if (warning (OPT_Wattributes,
2393 "ignoring attribute %<%E (%u)%> because it conflicts "
2394 "with attribute %<%E (%u)%>",
2395 name, newalign, name, curalign)
2396 && note)
2397 inform (DECL_SOURCE_LOCATION (last_decl),
2398 "previous declaration here");
2399 /* Only reject attempts to relax/override an alignment
2400 explicitly specified previously and accept declarations
2401 that appear to relax the implicit function alignment for
2402 the target. Both increasing and increasing the alignment
2403 set by -falign-functions setting is permitted. */
2404 *no_add_attrs = true;
2405 }
2406 else if (!warn_if_not_aligned_p)
2407 {
2408 /* Do not fail for attribute warn_if_not_aligned. Otherwise,
2409 silently avoid applying the alignment to the declaration
2410 because it's implicitly satisfied by the target. Apply
2411 the attribute nevertheless so it can be retrieved by
2412 __builtin_has_attribute. */
2413 set_align = false;
2414 }
2415 }
2416 else if (DECL_USER_ALIGN (decl)
2417 && DECL_ALIGN (decl) > bitalign)
2418 /* C++-11 [dcl.align/4]:
2419
2420 When multiple alignment-specifiers are specified for an
2421 entity, the alignment requirement shall be set to the
2422 strictest specified alignment.
2423
2424 This formally comes from the c++11 specification but we are
2425 doing it for the GNU attribute syntax as well. */
2426 *no_add_attrs = true;
2427 else if (warn_if_not_aligned_p
2428 && TREE_CODE (decl) == FIELD_DECL
2429 && !DECL_C_BIT_FIELD (decl))
2430 {
2431 SET_DECL_WARN_IF_NOT_ALIGN (decl, bitalign);
2432 warn_if_not_aligned_p = false;
2433 set_align = false;
2434 }
2435
2436 if (warn_if_not_aligned_p)
2437 {
2438 error ("%<warn_if_not_aligned%> may not be specified for %q+D",
2439 decl);
2440 *no_add_attrs = true;
2441 }
2442 else if (!is_type && !*no_add_attrs && set_align)
2443 {
2444 SET_DECL_ALIGN (decl, bitalign);
2445 DECL_USER_ALIGN (decl) = 1;
2446 }
2447
2448 return NULL_TREE;
2449 }
2450
2451 /* Handle a "aligned" attribute; arguments as in
2452 struct attribute_spec.handler. */
2453
2454 static tree
2455 handle_aligned_attribute (tree *node, tree name, tree args,
2456 int flags, bool *no_add_attrs)
2457 {
2458 return common_handle_aligned_attribute (node, name, args, flags,
2459 no_add_attrs, false);
2460 }
2461
2462 /* Handle a "warn_if_not_aligned" attribute; arguments as in
2463 struct attribute_spec.handler. */
2464
2465 static tree
2466 handle_warn_if_not_aligned_attribute (tree *node, tree name,
2467 tree args, int flags,
2468 bool *no_add_attrs)
2469 {
2470 return common_handle_aligned_attribute (node, name, args, flags,
2471 no_add_attrs, true);
2472 }
2473
2474 /* Handle a "weak" attribute; arguments as in
2475 struct attribute_spec.handler. */
2476
2477 static tree
2478 handle_weak_attribute (tree *node, tree name,
2479 tree ARG_UNUSED (args),
2480 int ARG_UNUSED (flags),
2481 bool * ARG_UNUSED (no_add_attrs))
2482 {
2483 if (TREE_CODE (*node) == FUNCTION_DECL
2484 && DECL_DECLARED_INLINE_P (*node))
2485 {
2486 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
2487 *no_add_attrs = true;
2488 }
2489 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
2490 {
2491 error ("indirect function %q+D cannot be declared weak", *node);
2492 *no_add_attrs = true;
2493 return NULL_TREE;
2494 }
2495 else if (VAR_OR_FUNCTION_DECL_P (*node))
2496 declare_weak (*node);
2497 else
2498 warning (OPT_Wattributes, "%qE attribute ignored", name);
2499
2500 return NULL_TREE;
2501 }
2502
2503 /* Handle a "noinit" or "persistent" attribute; arguments as in
2504 struct attribute_spec.handler.
2505 This generic handler is used for "special variable sections" that allow the
2506 section name to be set using a dedicated attribute. Additional validation
2507 is performed for the specific properties of the section corresponding to the
2508 attribute.
2509 The ".noinit" section *is not* loaded by the program loader, and is not
2510 initialized by the runtime startup code.
2511 The ".persistent" section *is* loaded by the program loader, but is not
2512 initialized by the runtime startup code. */
2513 static tree
2514 handle_special_var_sec_attribute (tree *node, tree name, tree args,
2515 int flags, bool *no_add_attrs)
2516 {
2517 tree decl = *node;
2518 tree res = NULL_TREE;
2519
2520 /* First perform generic validation common to "noinit" and "persistent"
2521 attributes. */
2522 if (!targetm_common.have_named_sections)
2523 {
2524 error_at (DECL_SOURCE_LOCATION (decl),
2525 "section attributes are not supported for this target");
2526 goto fail;
2527 }
2528
2529 if (!VAR_P (decl))
2530 {
2531 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
2532 "ignoring %qE attribute not set on a variable",
2533 name);
2534 goto fail;
2535 }
2536
2537 if (VAR_P (decl)
2538 && current_function_decl != NULL_TREE
2539 && !TREE_STATIC (decl))
2540 {
2541 error_at (DECL_SOURCE_LOCATION (decl),
2542 "%qE attribute cannot be specified for local variables",
2543 name);
2544 goto fail;
2545 }
2546
2547 if (VAR_P (decl)
2548 && !targetm.have_tls && targetm.emutls.tmpl_section
2549 && DECL_THREAD_LOCAL_P (decl))
2550 {
2551 error ("section of %q+D cannot be overridden", decl);
2552 goto fail;
2553 }
2554
2555 if (!targetm.have_switchable_bss_sections)
2556 {
2557 error ("%qE attribute is specific to ELF targets", name);
2558 goto fail;
2559 }
2560
2561 if (TREE_READONLY (decl))
2562 {
2563 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
2564 "ignoring %qE attribute set on const variable",
2565 name);
2566 goto fail;
2567 }
2568
2569 /* Now validate noinit/persistent individually. */
2570 if (strcmp (IDENTIFIER_POINTER (name), "noinit") == 0)
2571 {
2572 if (DECL_INITIAL (decl))
2573 {
2574 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
2575 "ignoring %qE attribute set on initialized variable",
2576 name);
2577 goto fail;
2578 }
2579 /* If this var is thought to be common, then change this. "noinit"
2580 variables must be placed in an explicit ".noinit" section. */
2581 DECL_COMMON (decl) = 0;
2582 }
2583 else if (strcmp (IDENTIFIER_POINTER (name), "persistent") == 0)
2584 {
2585 if (DECL_COMMON (decl) || DECL_INITIAL (decl) == NULL_TREE)
2586 {
2587 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
2588 "ignoring %qE attribute set on uninitialized variable",
2589 name);
2590 goto fail;
2591 }
2592 }
2593 else
2594 gcc_unreachable ();
2595
2596 res = targetm.handle_generic_attribute (node, name, args, flags,
2597 no_add_attrs);
2598
2599 /* If the back end confirms the attribute can be added then continue onto
2600 final processing. */
2601 if (!(*no_add_attrs))
2602 return res;
2603
2604 fail:
2605 *no_add_attrs = true;
2606 return res;
2607 }
2608
2609 /* Handle a "noplt" attribute; arguments as in
2610 struct attribute_spec.handler. */
2611
2612 static tree
2613 handle_noplt_attribute (tree *node, tree name,
2614 tree ARG_UNUSED (args),
2615 int ARG_UNUSED (flags),
2616 bool * ARG_UNUSED (no_add_attrs))
2617 {
2618 if (TREE_CODE (*node) != FUNCTION_DECL)
2619 {
2620 warning (OPT_Wattributes,
2621 "%qE attribute is only applicable on functions", name);
2622 *no_add_attrs = true;
2623 return NULL_TREE;
2624 }
2625 return NULL_TREE;
2626 }
2627
2628 /* Handle a "symver" attribute. */
2629
2630 static tree
2631 handle_symver_attribute (tree *node, tree ARG_UNUSED (name), tree args,
2632 int ARG_UNUSED (flags), bool *no_add_attrs)
2633 {
2634 tree symver;
2635 const char *symver_str;
2636
2637 if (TREE_CODE (*node) != FUNCTION_DECL && TREE_CODE (*node) != VAR_DECL)
2638 {
2639 warning (OPT_Wattributes,
2640 "%<symver%> attribute only applies to functions and variables");
2641 *no_add_attrs = true;
2642 return NULL_TREE;
2643 }
2644
2645 if (!decl_in_symtab_p (*node))
2646 {
2647 warning (OPT_Wattributes,
2648 "%<symver%> attribute is only applicable to symbols");
2649 *no_add_attrs = true;
2650 return NULL_TREE;
2651 }
2652
2653 for (; args; args = TREE_CHAIN (args))
2654 {
2655 symver = TREE_VALUE (args);
2656 if (TREE_CODE (symver) != STRING_CST)
2657 {
2658 error ("%<symver%> attribute argument not a string constant");
2659 *no_add_attrs = true;
2660 return NULL_TREE;
2661 }
2662
2663 symver_str = TREE_STRING_POINTER (symver);
2664
2665 int ats = 0;
2666 for (int n = 0; (int)n < TREE_STRING_LENGTH (symver); n++)
2667 if (symver_str[n] == '@')
2668 ats++;
2669
2670 if (ats != 1 && ats != 2)
2671 {
2672 error ("symver attribute argument must have format %<name@nodename%>");
2673 error ("%<symver%> attribute argument %qs must contain one or two "
2674 "%<@%>", symver_str);
2675 *no_add_attrs = true;
2676 return NULL_TREE;
2677 }
2678 }
2679
2680 return NULL_TREE;
2681 }
2682
2683
2684 /* Handle an "alias" or "ifunc" attribute; arguments as in
2685 struct attribute_spec.handler, except that IS_ALIAS tells us
2686 whether this is an alias as opposed to ifunc attribute. */
2687
2688 static tree
2689 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
2690 bool *no_add_attrs)
2691 {
2692 tree decl = *node;
2693
2694 if (TREE_CODE (decl) != FUNCTION_DECL
2695 && (!is_alias || !VAR_P (decl)))
2696 {
2697 warning (OPT_Wattributes, "%qE attribute ignored", name);
2698 *no_add_attrs = true;
2699 }
2700 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
2701 || (TREE_CODE (decl) != FUNCTION_DECL
2702 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
2703 /* A static variable declaration is always a tentative definition,
2704 but the alias is a non-tentative definition which overrides. */
2705 || (TREE_CODE (decl) != FUNCTION_DECL
2706 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
2707 {
2708 error ("%q+D defined both normally and as %qE attribute", decl, name);
2709 *no_add_attrs = true;
2710 return NULL_TREE;
2711 }
2712 else if (!is_alias
2713 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2714 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
2715 {
2716 error ("weak %q+D cannot be defined %qE", decl, name);
2717 *no_add_attrs = true;
2718 return NULL_TREE;
2719 }
2720
2721 /* Note that the very first time we process a nested declaration,
2722 decl_function_context will not be set. Indeed, *would* never
2723 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
2724 we do below. After such frobbery, pushdecl would set the context.
2725 In any case, this is never what we want. */
2726 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
2727 {
2728 tree id;
2729
2730 id = TREE_VALUE (args);
2731 if (TREE_CODE (id) != STRING_CST)
2732 {
2733 error ("attribute %qE argument not a string", name);
2734 *no_add_attrs = true;
2735 return NULL_TREE;
2736 }
2737 id = get_identifier (TREE_STRING_POINTER (id));
2738 /* This counts as a use of the object pointed to. */
2739 TREE_USED (id) = 1;
2740
2741 if (TREE_CODE (decl) == FUNCTION_DECL)
2742 DECL_INITIAL (decl) = error_mark_node;
2743 else
2744 TREE_STATIC (decl) = 1;
2745
2746 if (!is_alias)
2747 {
2748 /* ifuncs are also aliases, so set that attribute too. */
2749 DECL_ATTRIBUTES (decl)
2750 = tree_cons (get_identifier ("alias"), args,
2751 DECL_ATTRIBUTES (decl));
2752 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("ifunc"),
2753 NULL, DECL_ATTRIBUTES (decl));
2754 }
2755 }
2756 else
2757 {
2758 warning (OPT_Wattributes, "%qE attribute ignored", name);
2759 *no_add_attrs = true;
2760 }
2761
2762 if (decl_in_symtab_p (*node))
2763 {
2764 struct symtab_node *n = symtab_node::get (decl);
2765 if (n && n->refuse_visibility_changes)
2766 error ("%+qD declared %qs after being used",
2767 decl, is_alias ? "alias" : "ifunc");
2768 }
2769
2770
2771 return NULL_TREE;
2772 }
2773
2774 /* Handle an "alias" or "ifunc" attribute; arguments as in
2775 struct attribute_spec.handler. */
2776
2777 static tree
2778 handle_ifunc_attribute (tree *node, tree name, tree args,
2779 int ARG_UNUSED (flags), bool *no_add_attrs)
2780 {
2781 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
2782 }
2783
2784 /* Handle an "alias" or "ifunc" attribute; arguments as in
2785 struct attribute_spec.handler. */
2786
2787 static tree
2788 handle_alias_attribute (tree *node, tree name, tree args,
2789 int ARG_UNUSED (flags), bool *no_add_attrs)
2790 {
2791 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
2792 }
2793
2794 /* Handle the "copy" attribute NAME by copying the set of attributes
2795 from the symbol referenced by ARGS to the declaration of *NODE. */
2796
2797 static tree
2798 handle_copy_attribute (tree *node, tree name, tree args,
2799 int flags, bool *no_add_attrs)
2800 {
2801 /* Do not apply the copy attribute itself. It serves no purpose
2802 other than to copy other attributes. */
2803 *no_add_attrs = true;
2804
2805 tree decl = *node;
2806
2807 tree ref = TREE_VALUE (args);
2808 if (ref == error_mark_node)
2809 return NULL_TREE;
2810
2811 if (TREE_CODE (ref) == STRING_CST)
2812 {
2813 /* Explicitly handle this case since using a string literal
2814 as an argument is a likely mistake. */
2815 error_at (DECL_SOURCE_LOCATION (decl),
2816 "%qE attribute argument cannot be a string",
2817 name);
2818 return NULL_TREE;
2819 }
2820
2821 if (CONSTANT_CLASS_P (ref)
2822 && (INTEGRAL_TYPE_P (TREE_TYPE (ref))
2823 || FLOAT_TYPE_P (TREE_TYPE (ref))))
2824 {
2825 /* Similar to the string case, since some function attributes
2826 accept literal numbers as arguments (e.g., alloc_size or
2827 nonnull) using one here is a likely mistake. */
2828 error_at (DECL_SOURCE_LOCATION (decl),
2829 "%qE attribute argument cannot be a constant arithmetic "
2830 "expression",
2831 name);
2832 return NULL_TREE;
2833 }
2834
2835 if (ref == node[1])
2836 {
2837 /* Another possible mistake (but indirect self-references aren't
2838 and diagnosed and shouldn't be). */
2839 if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
2840 "%qE attribute ignored on a redeclaration "
2841 "of the referenced symbol",
2842 name))
2843 inform (DECL_SOURCE_LOCATION (node[1]),
2844 "previous declaration here");
2845 return NULL_TREE;
2846 }
2847
2848 /* Consider address-of expressions in the attribute argument
2849 as requests to copy from the referenced entity. */
2850 if (TREE_CODE (ref) == ADDR_EXPR)
2851 ref = TREE_OPERAND (ref, 0);
2852
2853 do
2854 {
2855 /* Drill down into references to find the referenced decl. */
2856 tree_code refcode = TREE_CODE (ref);
2857 if (refcode == ARRAY_REF
2858 || refcode == INDIRECT_REF)
2859 ref = TREE_OPERAND (ref, 0);
2860 else if (refcode == COMPONENT_REF)
2861 ref = TREE_OPERAND (ref, 1);
2862 else
2863 break;
2864 } while (!DECL_P (ref));
2865
2866 /* For object pointer expressions, consider those to be requests
2867 to copy from their type, such as in:
2868 struct __attribute__ (copy ((struct T *)0)) U { ... };
2869 which copies type attributes from struct T to the declaration
2870 of struct U. */
2871 if ((CONSTANT_CLASS_P (ref) || EXPR_P (ref))
2872 && POINTER_TYPE_P (TREE_TYPE (ref))
2873 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (ref)))
2874 ref = TREE_TYPE (ref);
2875
2876 tree reftype = TYPE_P (ref) ? ref : TREE_TYPE (ref);
2877
2878 if (DECL_P (decl))
2879 {
2880 if ((VAR_P (decl)
2881 && (TREE_CODE (ref) == FUNCTION_DECL
2882 || (EXPR_P (ref)
2883 && POINTER_TYPE_P (reftype)
2884 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (reftype)))))
2885 || (TREE_CODE (decl) == FUNCTION_DECL
2886 && (VAR_P (ref)
2887 || (EXPR_P (ref)
2888 && !FUNC_OR_METHOD_TYPE_P (reftype)
2889 && (!POINTER_TYPE_P (reftype)
2890 || !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (reftype)))))))
2891 {
2892 /* It makes no sense to try to copy function attributes
2893 to a variable, or variable attributes to a function. */
2894 if (warning (OPT_Wattributes,
2895 "%qE attribute ignored on a declaration of "
2896 "a different kind than referenced symbol",
2897 name)
2898 && DECL_P (ref))
2899 inform (DECL_SOURCE_LOCATION (ref),
2900 "symbol %qD referenced by %qD declared here", ref, decl);
2901 return NULL_TREE;
2902 }
2903
2904 tree attrs = NULL_TREE;
2905 if (DECL_P (ref))
2906 attrs = DECL_ATTRIBUTES (ref);
2907 else if (TYPE_P (ref))
2908 attrs = TYPE_ATTRIBUTES (ref);
2909
2910 /* Copy decl attributes from REF to DECL. */
2911 for (tree at = attrs; at; at = TREE_CHAIN (at))
2912 {
2913 /* Avoid copying attributes that affect a symbol linkage,
2914 inlining, or visibility since those in all likelihood
2915 only apply to the target.
2916 FIXME: make it possible to specify which attributes to
2917 copy or not to copy in the copy attribute itself. */
2918 tree atname = get_attribute_name (at);
2919 if (is_attribute_p ("alias", atname)
2920 || is_attribute_p ("always_inline", atname)
2921 || is_attribute_p ("gnu_inline", atname)
2922 || is_attribute_p ("ifunc", atname)
2923 || is_attribute_p ("noinline", atname)
2924 || is_attribute_p ("visibility", atname)
2925 || is_attribute_p ("weak", atname)
2926 || is_attribute_p ("weakref", atname)
2927 || is_attribute_p ("target_clones", atname))
2928 continue;
2929
2930 /* Attribute leaf only applies to extern functions.
2931 Avoid copying it to static ones. */
2932 if (!TREE_PUBLIC (decl)
2933 && is_attribute_p ("leaf", atname))
2934 continue;
2935
2936 tree atargs = TREE_VALUE (at);
2937 /* Create a copy of just the one attribute ar AT, including
2938 its argumentsm and add it to DECL. */
2939 tree attr = tree_cons (atname, copy_list (atargs), NULL_TREE);
2940 decl_attributes (node, attr, flags, EXPR_P (ref) ? NULL_TREE : ref);
2941 }
2942
2943 /* Proceed to copy type attributes below. */
2944 }
2945 else if (!TYPE_P (decl))
2946 {
2947 error_at (DECL_SOURCE_LOCATION (decl),
2948 "%qE attribute must apply to a declaration",
2949 name);
2950 return NULL_TREE;
2951 }
2952
2953 /* A function declared with attribute nothrow has the attribute
2954 attached to it, but a C++ throw() function does not. */
2955 if (TREE_NOTHROW (ref))
2956 TREE_NOTHROW (decl) = true;
2957
2958 /* Similarly, a function declared with attribute noreturn has it
2959 attached on to it, but a C11 _Noreturn function does not. */
2960 if (DECL_P (ref)
2961 && TREE_THIS_VOLATILE (ref)
2962 && FUNC_OR_METHOD_TYPE_P (reftype))
2963 TREE_THIS_VOLATILE (decl) = true;
2964
2965 if (POINTER_TYPE_P (reftype))
2966 reftype = TREE_TYPE (reftype);
2967
2968 if (!TYPE_P (reftype))
2969 return NULL_TREE;
2970
2971 tree attrs = TYPE_ATTRIBUTES (reftype);
2972
2973 /* Copy type attributes from REF to DECL. Pass in REF if it's a DECL
2974 or a type but not if it's an expression. Set ATTR_FLAG_INTERNAL
2975 since the attributes' arguments may be in their internal form. */
2976 for (tree at = attrs; at; at = TREE_CHAIN (at))
2977 decl_attributes (node, at, flags | ATTR_FLAG_INTERNAL,
2978 EXPR_P (ref) ? NULL_TREE : ref);
2979
2980 return NULL_TREE;
2981 }
2982
2983 /* Handle a "weakref" attribute; arguments as in struct
2984 attribute_spec.handler. */
2985
2986 static tree
2987 handle_weakref_attribute (tree *node, tree name, tree args,
2988 int flags, bool *no_add_attrs)
2989 {
2990 tree attr = NULL_TREE;
2991
2992 /* We must ignore the attribute when it is associated with
2993 local-scoped decls, since attribute alias is ignored and many
2994 such symbols do not even have a DECL_WEAK field. */
2995 if (decl_function_context (*node)
2996 || current_function_decl
2997 || !VAR_OR_FUNCTION_DECL_P (*node))
2998 {
2999 warning (OPT_Wattributes, "%qE attribute ignored", name);
3000 *no_add_attrs = true;
3001 return NULL_TREE;
3002 }
3003
3004 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
3005 {
3006 error ("indirect function %q+D cannot be declared %qE",
3007 *node, name);
3008 *no_add_attrs = true;
3009 return NULL_TREE;
3010 }
3011
3012 /* The idea here is that `weakref("name")' mutates into `weakref,
3013 alias("name")', and weakref without arguments, in turn,
3014 implicitly adds weak. */
3015
3016 if (args)
3017 {
3018 attr = tree_cons (get_identifier ("alias"), args, attr);
3019 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
3020
3021 *no_add_attrs = true;
3022
3023 decl_attributes (node, attr, flags);
3024 }
3025 else
3026 {
3027 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
3028 error_at (DECL_SOURCE_LOCATION (*node),
3029 "%qE attribute must appear before %qs attribute",
3030 name, "alias");
3031
3032 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
3033 and that isn't supported; and because it wants to add it to
3034 the list of weak decls, which isn't helpful. */
3035 DECL_WEAK (*node) = 1;
3036 }
3037
3038 if (decl_in_symtab_p (*node))
3039 {
3040 struct symtab_node *n = symtab_node::get (*node);
3041 if (n && n->refuse_visibility_changes)
3042 error ("%+qD declared %qE after being used", *node, name);
3043 }
3044
3045 return NULL_TREE;
3046 }
3047
3048 /* Handle an "visibility" attribute; arguments as in
3049 struct attribute_spec.handler. */
3050
3051 static tree
3052 handle_visibility_attribute (tree *node, tree name, tree args,
3053 int ARG_UNUSED (flags),
3054 bool *ARG_UNUSED (no_add_attrs))
3055 {
3056 tree decl = *node;
3057 tree id = TREE_VALUE (args);
3058 enum symbol_visibility vis;
3059
3060 if (TYPE_P (*node))
3061 {
3062 if (TREE_CODE (*node) == ENUMERAL_TYPE)
3063 /* OK */;
3064 else if (!RECORD_OR_UNION_TYPE_P (*node))
3065 {
3066 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
3067 name);
3068 return NULL_TREE;
3069 }
3070 else if (TYPE_FIELDS (*node))
3071 {
3072 error ("%qE attribute ignored because %qT is already defined",
3073 name, *node);
3074 return NULL_TREE;
3075 }
3076 }
3077 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
3078 {
3079 warning (OPT_Wattributes, "%qE attribute ignored", name);
3080 return NULL_TREE;
3081 }
3082
3083 if (TREE_CODE (id) != STRING_CST)
3084 {
3085 error ("visibility argument not a string");
3086 return NULL_TREE;
3087 }
3088
3089 /* If this is a type, set the visibility on the type decl. */
3090 if (TYPE_P (decl))
3091 {
3092 decl = TYPE_NAME (decl);
3093 if (!decl)
3094 return NULL_TREE;
3095 if (TREE_CODE (decl) == IDENTIFIER_NODE)
3096 {
3097 warning (OPT_Wattributes, "%qE attribute ignored on types",
3098 name);
3099 return NULL_TREE;
3100 }
3101 }
3102
3103 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
3104 vis = VISIBILITY_DEFAULT;
3105 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
3106 vis = VISIBILITY_INTERNAL;
3107 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
3108 vis = VISIBILITY_HIDDEN;
3109 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
3110 vis = VISIBILITY_PROTECTED;
3111 else
3112 {
3113 error ("attribute %qE argument must be one of %qs, %qs, %qs, or %qs",
3114 name, "default", "hidden", "protected", "internal");
3115 vis = VISIBILITY_DEFAULT;
3116 }
3117
3118 if (DECL_VISIBILITY_SPECIFIED (decl)
3119 && vis != DECL_VISIBILITY (decl))
3120 {
3121 tree attributes = (TYPE_P (*node)
3122 ? TYPE_ATTRIBUTES (*node)
3123 : DECL_ATTRIBUTES (decl));
3124 if (lookup_attribute ("visibility", attributes))
3125 error ("%qD redeclared with different visibility", decl);
3126 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
3127 && lookup_attribute ("dllimport", attributes))
3128 error ("%qD was declared %qs which implies default visibility",
3129 decl, "dllimport");
3130 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
3131 && lookup_attribute ("dllexport", attributes))
3132 error ("%qD was declared %qs which implies default visibility",
3133 decl, "dllexport");
3134 }
3135
3136 DECL_VISIBILITY (decl) = vis;
3137 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3138
3139 /* Go ahead and attach the attribute to the node as well. This is needed
3140 so we can determine whether we have VISIBILITY_DEFAULT because the
3141 visibility was not specified, or because it was explicitly overridden
3142 from the containing scope. */
3143
3144 return NULL_TREE;
3145 }
3146
3147 /* Handle an "tls_model" attribute; arguments as in
3148 struct attribute_spec.handler. */
3149
3150 static tree
3151 handle_tls_model_attribute (tree *node, tree name, tree args,
3152 int ARG_UNUSED (flags),
3153 bool *ARG_UNUSED (no_add_attrs))
3154 {
3155 tree id;
3156 tree decl = *node;
3157 enum tls_model kind;
3158
3159 if (!VAR_P (decl))
3160 {
3161 warning (OPT_Wattributes, "%qE attribute ignored because %qD "
3162 "is not a variable",
3163 name, decl);
3164 return NULL_TREE;
3165 }
3166
3167 if (!DECL_THREAD_LOCAL_P (decl))
3168 {
3169 warning (OPT_Wattributes, "%qE attribute ignored because %qD does "
3170 "not have thread storage duration", name, decl);
3171 return NULL_TREE;
3172 }
3173
3174 kind = DECL_TLS_MODEL (decl);
3175 id = TREE_VALUE (args);
3176 if (TREE_CODE (id) != STRING_CST)
3177 {
3178 error ("%qE argument not a string", name);
3179 return NULL_TREE;
3180 }
3181
3182 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
3183 kind = TLS_MODEL_LOCAL_EXEC;
3184 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
3185 kind = TLS_MODEL_INITIAL_EXEC;
3186 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
3187 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
3188 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
3189 kind = TLS_MODEL_GLOBAL_DYNAMIC;
3190 else
3191 error ("%qE argument must be one of %qs, %qs, %qs, or %qs",
3192 name,
3193 "local-exec", "initial-exec", "local-dynamic", "global-dynamic");
3194
3195 set_decl_tls_model (decl, kind);
3196 return NULL_TREE;
3197 }
3198
3199 /* Handle a "no_instrument_function" attribute; arguments as in
3200 struct attribute_spec.handler. */
3201
3202 static tree
3203 handle_no_instrument_function_attribute (tree *node, tree name,
3204 tree ARG_UNUSED (args),
3205 int ARG_UNUSED (flags),
3206 bool *no_add_attrs)
3207 {
3208 tree decl = *node;
3209
3210 if (TREE_CODE (decl) != FUNCTION_DECL)
3211 {
3212 error_at (DECL_SOURCE_LOCATION (decl),
3213 "%qE attribute applies only to functions", name);
3214 *no_add_attrs = true;
3215 }
3216 else
3217 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
3218
3219 return NULL_TREE;
3220 }
3221
3222 /* Handle a "no_profile_instrument_function" attribute; arguments as in
3223 struct attribute_spec.handler. */
3224
3225 static tree
3226 handle_no_profile_instrument_function_attribute (tree *node, tree name, tree,
3227 int, bool *no_add_attrs)
3228 {
3229 if (TREE_CODE (*node) != FUNCTION_DECL)
3230 {
3231 warning (OPT_Wattributes, "%qE attribute ignored", name);
3232 *no_add_attrs = true;
3233 }
3234
3235 return NULL_TREE;
3236 }
3237
3238 /* If ALLOC_DECL and DEALLOC_DECL are a pair of user-defined functions,
3239 if they are declared inline issue warnings and return null. Otherwise
3240 create attribute noinline, install it in ALLOC_DECL, and return it.
3241 Otherwise return null. */
3242
3243 static tree
3244 maybe_add_noinline (tree name, tree alloc_decl, tree dealloc_decl,
3245 bool *no_add_attrs)
3246 {
3247 if (fndecl_built_in_p (alloc_decl) || fndecl_built_in_p (dealloc_decl))
3248 return NULL_TREE;
3249
3250 /* When inlining (or optimization) is enabled and the allocator and
3251 deallocator are not built-in functions, ignore the attribute on
3252 functions declared inline since it could lead to false positives
3253 when inlining one or the other call would wind up calling
3254 a mismatched allocator or deallocator. */
3255 if ((optimize && DECL_DECLARED_INLINE_P (alloc_decl))
3256 || lookup_attribute ("always_inline", DECL_ATTRIBUTES (alloc_decl)))
3257 {
3258 warning (OPT_Wattributes,
3259 "%<%E (%E)%> attribute ignored on functions "
3260 "declared %qs", name, DECL_NAME (dealloc_decl), "inline");
3261 *no_add_attrs = true;
3262 return NULL_TREE;
3263 }
3264
3265 if ((optimize && DECL_DECLARED_INLINE_P (dealloc_decl))
3266 || lookup_attribute ("always_inline", DECL_ATTRIBUTES (dealloc_decl)))
3267 {
3268 warning (OPT_Wattributes,
3269 "%<%E (%E)%> attribute ignored with deallocation "
3270 "functions declared %qs",
3271 name, DECL_NAME (dealloc_decl), "inline");
3272 inform (DECL_SOURCE_LOCATION (dealloc_decl),
3273 "deallocation function declared here" );
3274 *no_add_attrs = true;
3275 return NULL_TREE;
3276 }
3277
3278 /* Disable inlining for non-standard deallocators to avoid false
3279 positives due to mismatches between the inlined implementation
3280 of one and not the other pair of functions. */
3281 tree attr = tree_cons (get_identifier ("noinline"), NULL_TREE, NULL_TREE);
3282 decl_attributes (&alloc_decl, attr, 0);
3283 return attr;
3284 }
3285
3286 /* Handle the "malloc" attribute. */
3287
3288 static tree
3289 handle_malloc_attribute (tree *node, tree name, tree args, int flags,
3290 bool *no_add_attrs)
3291 {
3292 if (flags & ATTR_FLAG_INTERNAL)
3293 /* Recursive call. */
3294 return NULL_TREE;
3295
3296 tree fndecl = *node;
3297
3298 if (TREE_CODE (*node) != FUNCTION_DECL)
3299 {
3300 warning (OPT_Wattributes, "%qE attribute ignored; valid only "
3301 "for functions",
3302 name);
3303 *no_add_attrs = true;
3304 return NULL_TREE;
3305 }
3306
3307 tree rettype = TREE_TYPE (TREE_TYPE (*node));
3308 if (!POINTER_TYPE_P (rettype))
3309 {
3310 warning (OPT_Wattributes, "%qE attribute ignored on functions "
3311 "returning %qT; valid only for pointer return types",
3312 name, rettype);
3313 *no_add_attrs = true;
3314 return NULL_TREE;
3315 }
3316
3317 if (!args)
3318 {
3319 /* Only the form of the attribute with no arguments declares
3320 a function malloc-like. */
3321 DECL_IS_MALLOC (*node) = 1;
3322 return NULL_TREE;
3323 }
3324
3325 tree dealloc = TREE_VALUE (args);
3326 if (error_operand_p (dealloc))
3327 {
3328 /* If the argument is in error it will have already been diagnosed.
3329 Avoid issuing redundant errors here. */
3330 *no_add_attrs = true;
3331 return NULL_TREE;
3332 }
3333
3334 STRIP_NOPS (dealloc);
3335 if (TREE_CODE (dealloc) == ADDR_EXPR)
3336 {
3337 /* In C++ the argument may be wrapped in a cast to disambiguate
3338 one of a number of overloads (such as operator delete). To
3339 make things interesting, the cast looks different between
3340 different C++ versions. Strip it and install the attribute
3341 with the disambiguated function. */
3342 dealloc = TREE_OPERAND (dealloc, 0);
3343
3344 *no_add_attrs = true;
3345 tree attr = tree_cons (NULL_TREE, dealloc, TREE_CHAIN (args));
3346 attr = build_tree_list (name, attr);
3347 return decl_attributes (node, attr, 0);
3348 }
3349
3350 if (TREE_CODE (dealloc) != FUNCTION_DECL)
3351 {
3352 if (TREE_CODE (dealloc) == OVERLOAD)
3353 {
3354 /* Handle specially the common case of specifying one of a number
3355 of overloads, such as operator delete. */
3356 error ("%qE attribute argument 1 is ambiguous", name);
3357 inform (input_location,
3358 "use a cast to the expected type to disambiguate");
3359 *no_add_attrs = true;
3360 return NULL_TREE;
3361 }
3362
3363 error ("%qE attribute argument 1 does not name a function", name);
3364 if (DECL_P (dealloc))
3365 inform (DECL_SOURCE_LOCATION (dealloc),
3366 "argument references a symbol declared here");
3367 *no_add_attrs = true;
3368 return NULL_TREE;
3369 }
3370
3371 /* Mentioning the deallocation function qualifies as its use. */
3372 TREE_USED (dealloc) = 1;
3373
3374 tree fntype = TREE_TYPE (dealloc);
3375 tree argpos = TREE_CHAIN (args) ? TREE_VALUE (TREE_CHAIN (args)) : NULL_TREE;
3376 if (!argpos)
3377 {
3378 tree argtypes = TYPE_ARG_TYPES (fntype);
3379 if (!argtypes)
3380 {
3381 /* Reject functions without a prototype. */
3382 error ("%qE attribute argument 1 must take a pointer "
3383 "type as its first argument", name);
3384 inform (DECL_SOURCE_LOCATION (dealloc),
3385 "referenced symbol declared here");
3386 *no_add_attrs = true;
3387 return NULL_TREE;
3388 }
3389
3390 tree argtype = TREE_VALUE (argtypes);
3391 if (TREE_CODE (argtype) != POINTER_TYPE)
3392 {
3393 /* Reject functions that don't take a pointer as their first
3394 argument. */
3395 error ("%qE attribute argument 1 must take a pointer type "
3396 "as its first argument; have %qT", name, argtype);
3397 inform (DECL_SOURCE_LOCATION (dealloc),
3398 "referenced symbol declared here");
3399 *no_add_attrs = true;
3400 return NULL_TREE;
3401 }
3402
3403 /* Disable inlining for non-standard deallocators to avoid false
3404 positives (or warn if either function is explicitly inline). */
3405 tree at_noinline =
3406 maybe_add_noinline (name, fndecl, dealloc, no_add_attrs);
3407 if (*no_add_attrs)
3408 return NULL_TREE;
3409
3410 /* Add attribute *dealloc to the deallocator function associating
3411 it with this one. Ideally, the attribute would reference
3412 the DECL of the deallocator but since that changes for each
3413 redeclaration, use DECL_NAME instead. (DECL_ASSEMBLER_NAME
3414 need not be set set this point and setting it here is too early. */
3415 tree attrs = build_tree_list (NULL_TREE, DECL_NAME (fndecl));
3416 attrs = tree_cons (get_identifier ("*dealloc"), attrs, at_noinline);
3417 decl_attributes (&dealloc, attrs, 0);
3418 return NULL_TREE;
3419 }
3420
3421 /* Validate the positional argument. */
3422 argpos = positional_argument (fntype, name, argpos, POINTER_TYPE);
3423 if (!argpos)
3424 {
3425 *no_add_attrs = true;
3426 return NULL_TREE;
3427 }
3428
3429 /* As above, disable inlining for non-standard deallocators to avoid
3430 false positives (or warn). */
3431 tree at_noinline =
3432 maybe_add_noinline (name, fndecl, dealloc, no_add_attrs);
3433 if (*no_add_attrs)
3434 return NULL_TREE;
3435
3436 /* It's valid to declare the same function with multiple instances
3437 of attribute malloc, each naming the same or different deallocator
3438 functions, and each referencing either the same or a different
3439 positional argument. */
3440 tree attrs = tree_cons (NULL_TREE, argpos, NULL_TREE);
3441 attrs = tree_cons (NULL_TREE, DECL_NAME (fndecl), attrs);
3442 attrs = tree_cons (get_identifier ("*dealloc"), attrs, at_noinline);
3443 decl_attributes (&dealloc, attrs, 0);
3444 return NULL_TREE;
3445 }
3446
3447 /* Handle the internal "*dealloc" attribute added for functions declared
3448 with the one- and two-argument forms of attribute malloc. Add it
3449 to *NODE unless it's already there with the same arguments. */
3450
3451 static tree
3452 handle_dealloc_attribute (tree *node, tree name, tree args, int,
3453 bool *no_add_attrs)
3454 {
3455 tree fndecl = *node;
3456
3457 tree attrs = DECL_ATTRIBUTES (fndecl);
3458 if (!attrs)
3459 return NULL_TREE;
3460
3461 tree arg = TREE_VALUE (args);
3462 args = TREE_CHAIN (args);
3463 tree arg_pos = args ? TREE_VALUE (args) : integer_zero_node;
3464
3465 gcc_checking_assert ((DECL_P (arg)
3466 && fndecl_built_in_p (arg, BUILT_IN_NORMAL))
3467 || TREE_CODE (arg) == IDENTIFIER_NODE);
3468
3469 const char* const namestr = IDENTIFIER_POINTER (name);
3470 for (tree at = attrs; (at = lookup_attribute (namestr, at));
3471 at = TREE_CHAIN (at))
3472 {
3473 tree alloc = TREE_VALUE (at);
3474 if (!alloc)
3475 continue;
3476
3477 tree pos = TREE_CHAIN (alloc);
3478 alloc = TREE_VALUE (alloc);
3479 pos = pos ? TREE_VALUE (pos) : integer_zero_node;
3480 gcc_checking_assert ((DECL_P (alloc)
3481 && fndecl_built_in_p (alloc, BUILT_IN_NORMAL))
3482 || TREE_CODE (alloc) == IDENTIFIER_NODE);
3483
3484 if (alloc == arg && tree_int_cst_equal (pos, arg_pos))
3485 {
3486 /* The function already has the attribute either without any
3487 arguments or with the same arguments as the attribute that's
3488 being added. Return without adding another copy. */
3489 *no_add_attrs = true;
3490 return NULL_TREE;
3491 }
3492 }
3493
3494 return NULL_TREE;
3495 }
3496
3497 /* Handle the "alloc_size (argpos1 [, argpos2])" function type attribute.
3498 *NODE is the type of the function the attribute is being applied to. */
3499
3500 static tree
3501 handle_alloc_size_attribute (tree *node, tree name, tree args,
3502 int ARG_UNUSED (flags), bool *no_add_attrs)
3503 {
3504 tree fntype = *node;
3505 tree rettype = TREE_TYPE (fntype);
3506 if (!POINTER_TYPE_P (rettype))
3507 {
3508 warning (OPT_Wattributes,
3509 "%qE attribute ignored on a function returning %qT",
3510 name, rettype);
3511 *no_add_attrs = true;
3512 return NULL_TREE;
3513 }
3514
3515 tree newargs[2] = { NULL_TREE, NULL_TREE };
3516 for (int i = 1; args; ++i)
3517 {
3518 tree pos = TREE_VALUE (args);
3519 /* NEXT is null when the attribute includes just one argument.
3520 That's used to tell positional_argument to avoid mentioning
3521 the argument number in diagnostics (since there's just one
3522 mentioning it is unnecessary and coule be confusing). */
3523 tree next = TREE_CHAIN (args);
3524 if (tree val = positional_argument (fntype, name, pos, INTEGER_TYPE,
3525 next || i > 1 ? i : 0))
3526 {
3527 TREE_VALUE (args) = val;
3528 newargs[i - 1] = val;
3529 }
3530 else
3531 {
3532 *no_add_attrs = true;
3533 return NULL_TREE;
3534 }
3535
3536 args = next;
3537 }
3538
3539 if (!validate_attr_args (node, name, newargs))
3540 *no_add_attrs = true;
3541
3542 return NULL_TREE;
3543 }
3544
3545
3546 /* Handle an "alloc_align (argpos)" attribute. */
3547
3548 static tree
3549 handle_alloc_align_attribute (tree *node, tree name, tree args, int,
3550 bool *no_add_attrs)
3551 {
3552 tree fntype = *node;
3553 tree rettype = TREE_TYPE (fntype);
3554 if (!POINTER_TYPE_P (rettype))
3555 {
3556 warning (OPT_Wattributes,
3557 "%qE attribute ignored on a function returning %qT",
3558 name, rettype);
3559 *no_add_attrs = true;
3560 return NULL_TREE;
3561 }
3562
3563 if (tree val = positional_argument (*node, name, TREE_VALUE (args),
3564 INTEGER_TYPE))
3565 if (validate_attr_arg (node, name, val))
3566 return NULL_TREE;
3567
3568 *no_add_attrs = true;
3569 return NULL_TREE;
3570 }
3571
3572 /* Handle a "assume_aligned" attribute; arguments as in
3573 struct attribute_spec.handler. */
3574
3575 static tree
3576 handle_assume_aligned_attribute (tree *node, tree name, tree args, int,
3577 bool *no_add_attrs)
3578 {
3579 tree decl = *node;
3580 tree rettype = TREE_TYPE (decl);
3581 if (TREE_CODE (rettype) != POINTER_TYPE)
3582 {
3583 warning (OPT_Wattributes,
3584 "%qE attribute ignored on a function returning %qT",
3585 name, rettype);
3586 *no_add_attrs = true;
3587 return NULL_TREE;
3588 }
3589
3590 /* The alignment specified by the first argument. */
3591 tree align = NULL_TREE;
3592
3593 for (; args; args = TREE_CHAIN (args))
3594 {
3595 tree val = TREE_VALUE (args);
3596 if (val && TREE_CODE (val) != IDENTIFIER_NODE
3597 && TREE_CODE (val) != FUNCTION_DECL)
3598 val = default_conversion (val);
3599
3600 if (!tree_fits_shwi_p (val))
3601 {
3602 warning (OPT_Wattributes,
3603 "%qE attribute argument %E is not an integer constant",
3604 name, val);
3605 *no_add_attrs = true;
3606 return NULL_TREE;
3607 }
3608 else if (tree_int_cst_sgn (val) < 0)
3609 {
3610 warning (OPT_Wattributes,
3611 "%qE attribute argument %E is not positive", name, val);
3612 *no_add_attrs = true;
3613 return NULL_TREE;
3614 }
3615
3616 if (!align)
3617 {
3618 /* Validate and save the alignment. */
3619 if (!integer_pow2p (val))
3620 {
3621 warning (OPT_Wattributes,
3622 "%qE attribute argument %E is not a power of 2",
3623 name, val);
3624 *no_add_attrs = true;
3625 return NULL_TREE;
3626 }
3627
3628 align = val;
3629 }
3630 else if (tree_int_cst_le (align, val))
3631 {
3632 /* The misalignment specified by the second argument
3633 must be non-negative and less than the alignment. */
3634 warning (OPT_Wattributes,
3635 "%qE attribute argument %E is not in the range [0, %wu]",
3636 name, val, tree_to_uhwi (align) - 1);
3637 *no_add_attrs = true;
3638 return NULL_TREE;
3639 }
3640 }
3641 return NULL_TREE;
3642 }
3643
3644 /* Handle the internal-only "arg spec" attribute. */
3645
3646 static tree
3647 handle_argspec_attribute (tree *, tree, tree args, int, bool *)
3648 {
3649 /* Verify the attribute has one or two arguments and their kind. */
3650 gcc_assert (args && TREE_CODE (TREE_VALUE (args)) == STRING_CST);
3651 for (tree next = TREE_CHAIN (args); next; next = TREE_CHAIN (next))
3652 {
3653 tree val = TREE_VALUE (next);
3654 gcc_assert (DECL_P (val) || EXPR_P (val));
3655 }
3656 return NULL_TREE;
3657 }
3658
3659 /* Handle the internal-only "fn spec" attribute. */
3660
3661 static tree
3662 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
3663 tree args, int ARG_UNUSED (flags),
3664 bool *no_add_attrs ATTRIBUTE_UNUSED)
3665 {
3666 gcc_assert (args
3667 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
3668 && !TREE_CHAIN (args));
3669 return NULL_TREE;
3670 }
3671
3672 /* Handle a "warn_unused" attribute; arguments as in
3673 struct attribute_spec.handler. */
3674
3675 static tree
3676 handle_warn_unused_attribute (tree *node, tree name,
3677 tree args ATTRIBUTE_UNUSED,
3678 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
3679 {
3680 if (TYPE_P (*node))
3681 /* Do nothing else, just set the attribute. We'll get at
3682 it later with lookup_attribute. */
3683 ;
3684 else
3685 {
3686 warning (OPT_Wattributes, "%qE attribute ignored", name);
3687 *no_add_attrs = true;
3688 }
3689
3690 return NULL_TREE;
3691 }
3692
3693 /* Handle an "omp declare simd" attribute; arguments as in
3694 struct attribute_spec.handler. */
3695
3696 static tree
3697 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
3698 {
3699 return NULL_TREE;
3700 }
3701
3702 /* Handle an "omp declare variant {base,variant}" attribute; arguments as in
3703 struct attribute_spec.handler. */
3704
3705 static tree
3706 handle_omp_declare_variant_attribute (tree *, tree, tree, int, bool *)
3707 {
3708 return NULL_TREE;
3709 }
3710
3711 /* Handle a "simd" attribute. */
3712
3713 static tree
3714 handle_simd_attribute (tree *node, tree name, tree args, int, bool *no_add_attrs)
3715 {
3716 if (TREE_CODE (*node) == FUNCTION_DECL)
3717 {
3718 tree t = get_identifier ("omp declare simd");
3719 tree attr = NULL_TREE;
3720 if (args)
3721 {
3722 tree id = TREE_VALUE (args);
3723
3724 if (TREE_CODE (id) != STRING_CST)
3725 {
3726 error ("attribute %qE argument not a string", name);
3727 *no_add_attrs = true;
3728 return NULL_TREE;
3729 }
3730
3731 if (strcmp (TREE_STRING_POINTER (id), "notinbranch") == 0)
3732 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
3733 OMP_CLAUSE_NOTINBRANCH);
3734 else if (strcmp (TREE_STRING_POINTER (id), "inbranch") == 0)
3735 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
3736 OMP_CLAUSE_INBRANCH);
3737 else
3738 {
3739 error ("only %<inbranch%> and %<notinbranch%> flags are "
3740 "allowed for %<__simd__%> attribute");
3741 *no_add_attrs = true;
3742 return NULL_TREE;
3743 }
3744 }
3745
3746 DECL_ATTRIBUTES (*node)
3747 = tree_cons (t, build_tree_list (NULL_TREE, attr),
3748 DECL_ATTRIBUTES (*node));
3749 }
3750 else
3751 {
3752 warning (OPT_Wattributes, "%qE attribute ignored", name);
3753 *no_add_attrs = true;
3754 }
3755
3756 return NULL_TREE;
3757 }
3758
3759 /* Handle an "omp declare target" attribute; arguments as in
3760 struct attribute_spec.handler. */
3761
3762 static tree
3763 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
3764 {
3765 return NULL_TREE;
3766 }
3767
3768 /* Handle a "returns_twice" attribute; arguments as in
3769 struct attribute_spec.handler. */
3770
3771 static tree
3772 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
3773 int ARG_UNUSED (flags), bool *no_add_attrs)
3774 {
3775 if (TREE_CODE (*node) == FUNCTION_DECL)
3776 DECL_IS_RETURNS_TWICE (*node) = 1;
3777 else
3778 {
3779 warning (OPT_Wattributes, "%qE attribute ignored", name);
3780 *no_add_attrs = true;
3781 }
3782
3783 return NULL_TREE;
3784 }
3785
3786 /* Handle a "no_limit_stack" attribute; arguments as in
3787 struct attribute_spec.handler. */
3788
3789 static tree
3790 handle_no_limit_stack_attribute (tree *node, tree name,
3791 tree ARG_UNUSED (args),
3792 int ARG_UNUSED (flags),
3793 bool *no_add_attrs)
3794 {
3795 tree decl = *node;
3796
3797 if (TREE_CODE (decl) != FUNCTION_DECL)
3798 {
3799 error_at (DECL_SOURCE_LOCATION (decl),
3800 "%qE attribute applies only to functions", name);
3801 *no_add_attrs = true;
3802 }
3803 else if (DECL_INITIAL (decl))
3804 {
3805 error_at (DECL_SOURCE_LOCATION (decl),
3806 "cannot set %qE attribute after definition", name);
3807 *no_add_attrs = true;
3808 }
3809 else
3810 DECL_NO_LIMIT_STACK (decl) = 1;
3811
3812 return NULL_TREE;
3813 }
3814
3815 /* Handle a "pure" attribute; arguments as in
3816 struct attribute_spec.handler. */
3817
3818 static tree
3819 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
3820 int ARG_UNUSED (flags), bool *no_add_attrs)
3821 {
3822 if (TREE_CODE (*node) == FUNCTION_DECL)
3823 {
3824 tree type = TREE_TYPE (*node);
3825 if (VOID_TYPE_P (TREE_TYPE (type)))
3826 warning (OPT_Wattributes, "%qE attribute on function "
3827 "returning %<void%>", name);
3828
3829 DECL_PURE_P (*node) = 1;
3830 /* ??? TODO: Support types. */
3831 }
3832 else
3833 {
3834 warning (OPT_Wattributes, "%qE attribute ignored", name);
3835 *no_add_attrs = true;
3836 }
3837
3838 return NULL_TREE;
3839 }
3840
3841 /* Digest an attribute list destined for a transactional memory statement.
3842 ALLOWED is the set of attributes that are allowed for this statement;
3843 return the attribute we parsed. Multiple attributes are never allowed. */
3844
3845 int
3846 parse_tm_stmt_attr (tree attrs, int allowed)
3847 {
3848 tree a_seen = NULL;
3849 int m_seen = 0;
3850
3851 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
3852 {
3853 tree a = get_attribute_name (attrs);
3854 tree ns = get_attribute_namespace (attrs);
3855 int m = 0;
3856
3857 if (is_attribute_p ("outer", a)
3858 && (ns == NULL_TREE || strcmp (IDENTIFIER_POINTER (ns), "gnu") == 0))
3859 m = TM_STMT_ATTR_OUTER;
3860
3861 if ((m & allowed) == 0)
3862 {
3863 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
3864 continue;
3865 }
3866
3867 if (m_seen == 0)
3868 {
3869 a_seen = a;
3870 m_seen = m;
3871 }
3872 else if (m_seen == m)
3873 warning (OPT_Wattributes, "%qE attribute duplicated", a);
3874 else
3875 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
3876 }
3877
3878 return m_seen;
3879 }
3880
3881 /* Transform a TM attribute name into a maskable integer and back.
3882 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
3883 to how the lack of an attribute is treated. */
3884
3885 int
3886 tm_attr_to_mask (tree attr)
3887 {
3888 if (attr == NULL)
3889 return 0;
3890 if (is_attribute_p ("transaction_safe", attr))
3891 return TM_ATTR_SAFE;
3892 if (is_attribute_p ("transaction_callable", attr))
3893 return TM_ATTR_CALLABLE;
3894 if (is_attribute_p ("transaction_pure", attr))
3895 return TM_ATTR_PURE;
3896 if (is_attribute_p ("transaction_unsafe", attr))
3897 return TM_ATTR_IRREVOCABLE;
3898 if (is_attribute_p ("transaction_may_cancel_outer", attr))
3899 return TM_ATTR_MAY_CANCEL_OUTER;
3900 return 0;
3901 }
3902
3903 tree
3904 tm_mask_to_attr (int mask)
3905 {
3906 const char *str;
3907 switch (mask)
3908 {
3909 case TM_ATTR_SAFE:
3910 str = "transaction_safe";
3911 break;
3912 case TM_ATTR_CALLABLE:
3913 str = "transaction_callable";
3914 break;
3915 case TM_ATTR_PURE:
3916 str = "transaction_pure";
3917 break;
3918 case TM_ATTR_IRREVOCABLE:
3919 str = "transaction_unsafe";
3920 break;
3921 case TM_ATTR_MAY_CANCEL_OUTER:
3922 str = "transaction_may_cancel_outer";
3923 break;
3924 default:
3925 gcc_unreachable ();
3926 }
3927 return get_identifier (str);
3928 }
3929
3930 /* Return the first TM attribute seen in LIST. */
3931
3932 tree
3933 find_tm_attribute (tree list)
3934 {
3935 for (; list ; list = TREE_CHAIN (list))
3936 {
3937 tree name = get_attribute_name (list);
3938 if (tm_attr_to_mask (name) != 0)
3939 return name;
3940 }
3941 return NULL_TREE;
3942 }
3943
3944 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
3945 Here we accept only function types, and verify that none of the other
3946 function TM attributes are also applied. */
3947 /* ??? We need to accept class types for C++, but not C. This greatly
3948 complicates this function, since we can no longer rely on the extra
3949 processing given by function_type_required. */
3950
3951 static tree
3952 handle_tm_attribute (tree *node, tree name, tree args,
3953 int flags, bool *no_add_attrs)
3954 {
3955 /* Only one path adds the attribute; others don't. */
3956 *no_add_attrs = true;
3957
3958 switch (TREE_CODE (*node))
3959 {
3960 case RECORD_TYPE:
3961 case UNION_TYPE:
3962 /* Only tm_callable and tm_safe apply to classes. */
3963 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
3964 goto ignored;
3965 /* FALLTHRU */
3966
3967 case FUNCTION_TYPE:
3968 case METHOD_TYPE:
3969 {
3970 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
3971 if (old_name == name)
3972 ;
3973 else if (old_name != NULL_TREE)
3974 error ("type was previously declared %qE", old_name);
3975 else
3976 *no_add_attrs = false;
3977 }
3978 break;
3979
3980 case FUNCTION_DECL:
3981 {
3982 /* transaction_safe_dynamic goes on the FUNCTION_DECL, but we also
3983 want to set transaction_safe on the type. */
3984 gcc_assert (is_attribute_p ("transaction_safe_dynamic", name));
3985 if (!TYPE_P (DECL_CONTEXT (*node)))
3986 error_at (DECL_SOURCE_LOCATION (*node),
3987 "%<transaction_safe_dynamic%> may only be specified for "
3988 "a virtual function");
3989 *no_add_attrs = false;
3990 decl_attributes (&TREE_TYPE (*node),
3991 build_tree_list (get_identifier ("transaction_safe"),
3992 NULL_TREE),
3993 0);
3994 break;
3995 }
3996
3997 case POINTER_TYPE:
3998 {
3999 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
4000 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
4001 {
4002 tree fn_tmp = TREE_TYPE (*node);
4003 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
4004 *node = build_pointer_type (fn_tmp);
4005 break;
4006 }
4007 }
4008 /* FALLTHRU */
4009
4010 default:
4011 /* If a function is next, pass it on to be tried next. */
4012 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
4013 return tree_cons (name, args, NULL);
4014
4015 ignored:
4016 warning (OPT_Wattributes, "%qE attribute ignored", name);
4017 break;
4018 }
4019
4020 return NULL_TREE;
4021 }
4022
4023 /* Handle the TM_WRAP attribute; arguments as in
4024 struct attribute_spec.handler. */
4025
4026 static tree
4027 handle_tm_wrap_attribute (tree *node, tree name, tree args,
4028 int ARG_UNUSED (flags), bool *no_add_attrs)
4029 {
4030 tree decl = *node;
4031
4032 /* We don't need the attribute even on success, since we
4033 record the entry in an external table. */
4034 *no_add_attrs = true;
4035
4036 if (TREE_CODE (decl) != FUNCTION_DECL)
4037 warning (OPT_Wattributes, "%qE attribute ignored", name);
4038 else
4039 {
4040 tree wrap_decl = TREE_VALUE (args);
4041 if (error_operand_p (wrap_decl))
4042 ;
4043 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
4044 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
4045 error ("%qE argument not an identifier", name);
4046 else
4047 {
4048 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
4049 wrap_decl = lookup_name (wrap_decl);
4050 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
4051 {
4052 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
4053 TREE_TYPE (wrap_decl)))
4054 record_tm_replacement (wrap_decl, decl);
4055 else
4056 error ("%qD is not compatible with %qD", wrap_decl, decl);
4057 }
4058 else
4059 error ("%qE argument is not a function", name);
4060 }
4061 }
4062
4063 return NULL_TREE;
4064 }
4065
4066 /* Ignore the given attribute. Used when this attribute may be usefully
4067 overridden by the target, but is not used generically. */
4068
4069 static tree
4070 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
4071 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
4072 bool *no_add_attrs)
4073 {
4074 *no_add_attrs = true;
4075 return NULL_TREE;
4076 }
4077
4078 /* Handle a "no vops" attribute; arguments as in
4079 struct attribute_spec.handler. */
4080
4081 static tree
4082 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
4083 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
4084 bool *ARG_UNUSED (no_add_attrs))
4085 {
4086 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
4087 DECL_IS_NOVOPS (*node) = 1;
4088 return NULL_TREE;
4089 }
4090
4091 /* Handle a "deprecated" attribute; arguments as in
4092 struct attribute_spec.handler. */
4093
4094 tree
4095 handle_deprecated_attribute (tree *node, tree name,
4096 tree args, int flags,
4097 bool *no_add_attrs)
4098 {
4099 tree type = NULL_TREE;
4100 int warn = 0;
4101 tree what = NULL_TREE;
4102
4103 if (!args)
4104 *no_add_attrs = true;
4105 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
4106 {
4107 error ("deprecated message is not a string");
4108 *no_add_attrs = true;
4109 }
4110
4111 if (DECL_P (*node))
4112 {
4113 tree decl = *node;
4114 type = TREE_TYPE (decl);
4115
4116 if (TREE_CODE (decl) == TYPE_DECL
4117 || TREE_CODE (decl) == PARM_DECL
4118 || VAR_OR_FUNCTION_DECL_P (decl)
4119 || TREE_CODE (decl) == FIELD_DECL
4120 || TREE_CODE (decl) == CONST_DECL
4121 || objc_method_decl (TREE_CODE (decl)))
4122 TREE_DEPRECATED (decl) = 1;
4123 else
4124 warn = 1;
4125 }
4126 else if (TYPE_P (*node))
4127 {
4128 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
4129 *node = build_variant_type_copy (*node);
4130 TREE_DEPRECATED (*node) = 1;
4131 type = *node;
4132 }
4133 else
4134 warn = 1;
4135
4136 if (warn)
4137 {
4138 *no_add_attrs = true;
4139 if (type && TYPE_NAME (type))
4140 {
4141 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
4142 what = TYPE_NAME (*node);
4143 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
4144 && DECL_NAME (TYPE_NAME (type)))
4145 what = DECL_NAME (TYPE_NAME (type));
4146 }
4147 if (what)
4148 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
4149 else
4150 warning (OPT_Wattributes, "%qE attribute ignored", name);
4151 }
4152
4153 return NULL_TREE;
4154 }
4155
4156 /* Handle a "unavailable" attribute; arguments as in
4157 struct attribute_spec.handler. */
4158
4159 static tree
4160 handle_unavailable_attribute (tree *node, tree name,
4161 tree args, int flags,
4162 bool *no_add_attrs)
4163 {
4164 tree type = NULL_TREE;
4165 int warn = 0;
4166 tree what = NULL_TREE;
4167
4168 if (!args)
4169 *no_add_attrs = true;
4170 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
4171 {
4172 error ("the message attached to %<unavailable%> is not a string");
4173 *no_add_attrs = true;
4174 }
4175
4176 if (DECL_P (*node))
4177 {
4178 tree decl = *node;
4179 type = TREE_TYPE (decl);
4180
4181 if (TREE_CODE (decl) == TYPE_DECL
4182 || TREE_CODE (decl) == PARM_DECL
4183 || VAR_OR_FUNCTION_DECL_P (decl)
4184 || TREE_CODE (decl) == FIELD_DECL
4185 || TREE_CODE (decl) == CONST_DECL
4186 || objc_method_decl (TREE_CODE (decl)))
4187 TREE_UNAVAILABLE (decl) = 1;
4188 else
4189 warn = 1;
4190 }
4191 else if (TYPE_P (*node))
4192 {
4193 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
4194 *node = build_variant_type_copy (*node);
4195 TREE_UNAVAILABLE (*node) = 1;
4196 type = *node;
4197 }
4198 else
4199 warn = 1;
4200
4201 if (warn)
4202 {
4203 *no_add_attrs = true;
4204 if (type && TYPE_NAME (type))
4205 {
4206 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
4207 what = TYPE_NAME (*node);
4208 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
4209 && DECL_NAME (TYPE_NAME (type)))
4210 what = DECL_NAME (TYPE_NAME (type));
4211 }
4212 if (what)
4213 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
4214 else
4215 warning (OPT_Wattributes, "%qE attribute ignored", name);
4216 }
4217
4218 return NULL_TREE;
4219 }
4220
4221 /* Return the "base" type from TYPE that is suitable to apply attribute
4222 vector_size to by stripping arrays, function types, etc. */
4223 static tree
4224 type_for_vector_size (tree type)
4225 {
4226 /* We need to provide for vector pointers, vector arrays, and
4227 functions returning vectors. For example:
4228
4229 __attribute__((vector_size(16))) short *foo;
4230
4231 In this case, the mode is SI, but the type being modified is
4232 HI, so we need to look further. */
4233
4234 while (POINTER_TYPE_P (type)
4235 || TREE_CODE (type) == FUNCTION_TYPE
4236 || TREE_CODE (type) == METHOD_TYPE
4237 || TREE_CODE (type) == ARRAY_TYPE
4238 || TREE_CODE (type) == OFFSET_TYPE)
4239 type = TREE_TYPE (type);
4240
4241 return type;
4242 }
4243
4244 /* Given TYPE, return the base type to which the vector_size attribute
4245 ATNAME with ARGS, when non-null, can be applied, if one exists.
4246 On success and when both ARGS and PTRNUNITS are non-null, set
4247 *PTRNUNINTS to the number of vector units. When PTRNUNITS is not
4248 null, issue a warning when the attribute argument is not constant
4249 and an error if there is no such type. Otherwise issue a warning
4250 in the latter case and return null. */
4251
4252 static tree
4253 type_valid_for_vector_size (tree type, tree atname, tree args,
4254 unsigned HOST_WIDE_INT *ptrnunits)
4255 {
4256 bool error_p = ptrnunits != NULL;
4257
4258 /* Get the mode of the type being modified. */
4259 machine_mode orig_mode = TYPE_MODE (type);
4260
4261 if ((!INTEGRAL_TYPE_P (type)
4262 && !SCALAR_FLOAT_TYPE_P (type)
4263 && !FIXED_POINT_TYPE_P (type))
4264 || (!SCALAR_FLOAT_MODE_P (orig_mode)
4265 && GET_MODE_CLASS (orig_mode) != MODE_INT
4266 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
4267 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
4268 || TREE_CODE (type) == BOOLEAN_TYPE)
4269 {
4270 if (error_p)
4271 error ("invalid vector type for attribute %qE", atname);
4272 else
4273 warning (OPT_Wattributes, "invalid vector type for attribute %qE",
4274 atname);
4275 return NULL_TREE;
4276 }
4277
4278 /* When no argument has been provided this is just a request to validate
4279 the type above. Return TYPE to indicate success. */
4280 if (!args)
4281 return type;
4282
4283 tree size = TREE_VALUE (args);
4284 /* Erroneous arguments have already been diagnosed. */
4285 if (size == error_mark_node)
4286 return NULL_TREE;
4287
4288 if (size && TREE_CODE (size) != IDENTIFIER_NODE
4289 && TREE_CODE (size) != FUNCTION_DECL)
4290 size = default_conversion (size);
4291
4292 if (TREE_CODE (size) != INTEGER_CST)
4293 {
4294 if (error_p)
4295 error ("%qE attribute argument value %qE is not an integer constant",
4296 atname, size);
4297 else
4298 warning (OPT_Wattributes,
4299 "%qE attribute argument value %qE is not an integer constant",
4300 atname, size);
4301 return NULL_TREE;
4302 }
4303
4304 if (!TYPE_UNSIGNED (TREE_TYPE (size))
4305 && tree_int_cst_sgn (size) < 0)
4306 {
4307 if (error_p)
4308 error ("%qE attribute argument value %qE is negative",
4309 atname, size);
4310 else
4311 warning (OPT_Wattributes,
4312 "%qE attribute argument value %qE is negative",
4313 atname, size);
4314 return NULL_TREE;
4315 }
4316
4317 /* The attribute argument value is constrained by the maximum bit
4318 alignment representable in unsigned int on the host. */
4319 unsigned HOST_WIDE_INT vecsize;
4320 unsigned HOST_WIDE_INT maxsize = tree_to_uhwi (max_object_size ());
4321 if (!tree_fits_uhwi_p (size)
4322 || (vecsize = tree_to_uhwi (size)) > maxsize)
4323 {
4324 if (error_p)
4325 error ("%qE attribute argument value %qE exceeds %wu",
4326 atname, size, maxsize);
4327 else
4328 warning (OPT_Wattributes,
4329 "%qE attribute argument value %qE exceeds %wu",
4330 atname, size, maxsize);
4331 return NULL_TREE;
4332 }
4333
4334 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
4335 {
4336 if (error_p)
4337 error ("vector size not an integral multiple of component size");
4338 return NULL_TREE;
4339 }
4340
4341 if (vecsize == 0)
4342 {
4343 error ("zero vector size");
4344 return NULL;
4345 }
4346
4347 /* Calculate how many units fit in the vector. */
4348 unsigned HOST_WIDE_INT nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
4349 if (nunits & (nunits - 1))
4350 {
4351 if (error_p)
4352 error ("number of vector components %wu not a power of two", nunits);
4353 else
4354 warning (OPT_Wattributes,
4355 "number of vector components %wu not a power of two", nunits);
4356 return NULL_TREE;
4357 }
4358
4359 if (nunits >= (unsigned HOST_WIDE_INT)INT_MAX)
4360 {
4361 if (error_p)
4362 error ("number of vector components %wu exceeds %d",
4363 nunits, INT_MAX - 1);
4364 else
4365 warning (OPT_Wattributes,
4366 "number of vector components %wu exceeds %d",
4367 nunits, INT_MAX - 1);
4368 return NULL_TREE;
4369 }
4370
4371 if (ptrnunits)
4372 *ptrnunits = nunits;
4373
4374 return type;
4375 }
4376
4377 /* Handle a "vector_size" attribute; arguments as in
4378 struct attribute_spec.handler. */
4379
4380 static tree
4381 handle_vector_size_attribute (tree *node, tree name, tree args,
4382 int ARG_UNUSED (flags),
4383 bool *no_add_attrs)
4384 {
4385 *no_add_attrs = true;
4386
4387 /* Determine the "base" type to apply the attribute to. */
4388 tree type = type_for_vector_size (*node);
4389
4390 /* Get the vector size (in bytes) and let the function compute
4391 the number of vector units. */
4392 unsigned HOST_WIDE_INT nunits;
4393 type = type_valid_for_vector_size (type, name, args, &nunits);
4394 if (!type)
4395 return NULL_TREE;
4396
4397 gcc_checking_assert (args != NULL);
4398
4399 tree new_type = build_vector_type (type, nunits);
4400
4401 /* Build back pointers if needed. */
4402 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
4403
4404 return NULL_TREE;
4405 }
4406
4407 /* Handle the "nonnull" attribute. */
4408
4409 static tree
4410 handle_nonnull_attribute (tree *node, tree name,
4411 tree args, int ARG_UNUSED (flags),
4412 bool *no_add_attrs)
4413 {
4414 tree type = *node;
4415
4416 /* If no arguments are specified, all pointer arguments should be
4417 non-null. Verify a full prototype is given so that the arguments
4418 will have the correct types when we actually check them later.
4419 Avoid diagnosing type-generic built-ins since those have no
4420 prototype. */
4421 if (!args)
4422 {
4423 if (!prototype_p (type)
4424 && (!TYPE_ATTRIBUTES (type)
4425 || !lookup_attribute ("type generic", TYPE_ATTRIBUTES (type))))
4426 {
4427 error ("%qE attribute without arguments on a non-prototype",
4428 name);
4429 *no_add_attrs = true;
4430 }
4431 return NULL_TREE;
4432 }
4433
4434 for (int i = 1; args; ++i)
4435 {
4436 tree pos = TREE_VALUE (args);
4437 /* NEXT is null when the attribute includes just one argument.
4438 That's used to tell positional_argument to avoid mentioning
4439 the argument number in diagnostics (since there's just one
4440 mentioning it is unnecessary and coule be confusing). */
4441 tree next = TREE_CHAIN (args);
4442 if (tree val = positional_argument (type, name, pos, POINTER_TYPE,
4443 next || i > 1 ? i : 0))
4444 TREE_VALUE (args) = val;
4445 else
4446 {
4447 *no_add_attrs = true;
4448 break;
4449 }
4450 args = next;
4451 }
4452
4453 return NULL_TREE;
4454 }
4455
4456 /* Handle the "nonstring" variable attribute. */
4457
4458 static tree
4459 handle_nonstring_attribute (tree *node, tree name, tree ARG_UNUSED (args),
4460 int ARG_UNUSED (flags), bool *no_add_attrs)
4461 {
4462 gcc_assert (!args);
4463 tree_code code = TREE_CODE (*node);
4464
4465 if (VAR_P (*node)
4466 || code == FIELD_DECL
4467 || code == PARM_DECL)
4468 {
4469 tree type = TREE_TYPE (*node);
4470
4471 if (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
4472 {
4473 /* Accept the attribute on arrays and pointers to all three
4474 narrow character types. */
4475 tree eltype = TREE_TYPE (type);
4476 eltype = TYPE_MAIN_VARIANT (eltype);
4477 if (eltype == char_type_node
4478 || eltype == signed_char_type_node
4479 || eltype == unsigned_char_type_node)
4480 return NULL_TREE;
4481 }
4482
4483 warning (OPT_Wattributes,
4484 "%qE attribute ignored on objects of type %qT",
4485 name, type);
4486 *no_add_attrs = true;
4487 return NULL_TREE;
4488 }
4489
4490 if (code == FUNCTION_DECL)
4491 warning (OPT_Wattributes,
4492 "%qE attribute does not apply to functions", name);
4493 else if (code == TYPE_DECL)
4494 warning (OPT_Wattributes,
4495 "%qE attribute does not apply to types", name);
4496 else
4497 warning (OPT_Wattributes, "%qE attribute ignored", name);
4498
4499 *no_add_attrs = true;
4500 return NULL_TREE;
4501 }
4502
4503 /* Given a function type FUNCTYPE, returns the type of the parameter
4504 ARGNO or null if ARGNO exceeds the number of parameters. On failure
4505 set *NARGS to the number of function parameters. */
4506
4507 static tree
4508 get_argument_type (tree functype, unsigned argno, unsigned *nargs)
4509 {
4510 function_args_iterator iter;
4511 function_args_iter_init (&iter, functype);
4512
4513 unsigned count = 0;
4514
4515 for ( ; iter.next; ++count, function_args_iter_next (&iter))
4516 {
4517 if (count + 1 == argno)
4518 {
4519 tree argtype = function_args_iter_cond (&iter);
4520 if (VOID_TYPE_P (argtype))
4521 break;
4522 if (argtype != error_mark_node)
4523 return argtype;
4524 }
4525 }
4526
4527 *nargs = count;
4528 return NULL_TREE;
4529 }
4530
4531 /* Given a function FNDECL return the function argument at the zero-
4532 based position ARGNO or null if it can't be found. */
4533
4534 static tree
4535 get_argument (tree fndecl, unsigned argno)
4536 {
4537 if (!DECL_P (fndecl))
4538 return NULL_TREE;
4539
4540 unsigned i = 0;
4541 for (tree arg = DECL_ARGUMENTS (fndecl); arg; arg = TREE_CHAIN (arg))
4542 if (i++ == argno)
4543 return arg;
4544
4545 return NULL_TREE;
4546 }
4547
4548 /* Attempt to append attribute access specification ATTRSPEC, optionally
4549 described by the human-readable string ATTRSTR, for type T, to one in
4550 ATTRS. VBLIST is an optional list of bounds of variable length array
4551 parameters described by ATTRSTR.
4552 Issue warning for conflicts and return null if any are found.
4553 Return the concatenated access string on success. */
4554
4555 static tree
4556 append_access_attr (tree node[3], tree attrs, const char *attrstr,
4557 const char *attrspec, tree vblist = NULL_TREE)
4558 {
4559 tree argstr = build_string (strlen (attrspec) + 1, attrspec);
4560 tree ataccess = tree_cons (NULL_TREE, argstr, vblist);
4561 ataccess = tree_cons (get_identifier ("access"), ataccess, NULL_TREE);
4562
4563 /* The access specification being applied. This may be an implicit
4564 access spec synthesized for array (or VLA) parameters even for
4565 a declaration with an explicit access spec already applied, if
4566 this call corresponds to the first declaration of the function. */
4567 rdwr_map new_idxs;
4568 init_attr_rdwr_indices (&new_idxs, ataccess);
4569
4570 /* The current access specification alrady applied. */
4571 rdwr_map cur_idxs;
4572 init_attr_rdwr_indices (&cur_idxs, attrs);
4573
4574 std::string spec;
4575 for (auto it = new_idxs.begin (); it != new_idxs.end (); ++it)
4576 {
4577 const auto &newaxsref = *it;
4578
4579 /* The map has two equal entries for each pointer argument that
4580 has an associated size argument. Process just the entry for
4581 the former. */
4582 if ((unsigned)newaxsref.first != newaxsref.second.ptrarg)
4583 continue;
4584
4585 const attr_access* const cura = cur_idxs.get (newaxsref.first);
4586 if (!cura)
4587 {
4588 /* The new attribute needs to be added. */
4589 tree str = newaxsref.second.to_internal_string ();
4590 spec += TREE_STRING_POINTER (str);
4591 continue;
4592 }
4593
4594 /* The new access spec refers to an array/pointer argument for
4595 which an access spec already exists. Check and diagnose any
4596 conflicts. If no conflicts are found, merge the two. */
4597 const attr_access* const newa = &newaxsref.second;
4598
4599 if (!attrstr)
4600 {
4601 tree str = NULL_TREE;
4602 if (newa->mode != access_deferred)
4603 str = newa->to_external_string ();
4604 else if (cura->mode != access_deferred)
4605 str = cura->to_external_string ();
4606 if (str)
4607 attrstr = TREE_STRING_POINTER (str);
4608 }
4609
4610 location_t curloc = input_location;
4611 if (node[2] && DECL_P (node[2]))
4612 curloc = DECL_SOURCE_LOCATION (node[2]);
4613
4614 location_t prevloc = UNKNOWN_LOCATION;
4615 if (node[1] && DECL_P (node[1]))
4616 prevloc = DECL_SOURCE_LOCATION (node[1]);
4617
4618 if (newa->mode != cura->mode
4619 && newa->mode != access_deferred
4620 && cura->mode != access_deferred
4621 && newa->internal_p == cura->internal_p)
4622 {
4623 /* Mismatch in access mode. */
4624 auto_diagnostic_group d;
4625 if (warning_at (curloc, OPT_Wattributes,
4626 "attribute %qs mismatch with mode %qs",
4627 attrstr, cura->mode_names[cura->mode])
4628 && prevloc != UNKNOWN_LOCATION)
4629 inform (prevloc, "previous declaration here");
4630 continue;
4631 }
4632
4633 /* Set if PTRARG refers to a VLA with an unspecified bound (T[*]).
4634 Be prepared for either CURA or NEWA to refer to it, depending
4635 on which happens to come first in the declaration. */
4636 const bool cur_vla_ub = (cura->internal_p
4637 && cura->sizarg == UINT_MAX
4638 && cura->minsize == HOST_WIDE_INT_M1U);
4639 const bool new_vla_ub = (newa->internal_p
4640 && newa->sizarg == UINT_MAX
4641 && newa->minsize == HOST_WIDE_INT_M1U);
4642
4643 if (newa->sizarg != cura->sizarg
4644 && attrstr
4645 && (!(cur_vla_ub ^ new_vla_ub)
4646 || (!cura->internal_p && !newa->internal_p)))
4647 {
4648 /* Avoid diagnosing redeclarations of functions with no explicit
4649 attribute access that add one. */
4650 if (newa->mode == access_deferred
4651 && cura->mode != access_deferred
4652 && newa->sizarg == UINT_MAX
4653 && cura->sizarg != UINT_MAX)
4654 continue;
4655
4656 if (cura->mode == access_deferred
4657 && newa->mode != access_deferred
4658 && cura->sizarg == UINT_MAX
4659 && newa->sizarg != UINT_MAX)
4660 continue;
4661
4662 /* The two specs designate different size arguments. It's okay
4663 for the explicit spec to specify a size where none is provided
4664 by the implicit (VLA) one, as in:
4665 __attribute__ ((access (read_write, 1, 2)))
4666 void f (int*, int);
4667 but not for two explicit access attributes to do that. */
4668 bool warned = false;
4669
4670 auto_diagnostic_group d;
4671
4672 if (newa->sizarg == UINT_MAX)
4673 /* Mismatch in the presence of the size argument. */
4674 warned = warning_at (curloc, OPT_Wattributes,
4675 "attribute %qs missing positional argument 2 "
4676 "provided in previous designation by argument "
4677 "%u", attrstr, cura->sizarg + 1);
4678 else if (cura->sizarg == UINT_MAX)
4679 /* Mismatch in the presence of the size argument. */
4680 warned = warning_at (curloc, OPT_Wattributes,
4681 "attribute %qs positional argument 2 "
4682 "missing in previous designation",
4683 attrstr);
4684 else if (newa->internal_p || cura->internal_p)
4685 /* Mismatch in the value of the size argument and a VLA bound. */
4686 warned = warning_at (curloc, OPT_Wattributes,
4687 "attribute %qs positional argument 2 "
4688 "conflicts with previous designation "
4689 "by argument %u",
4690 attrstr, cura->sizarg + 1);
4691 else
4692 /* Mismatch in the value of the size argument between two
4693 explicit access attributes. */
4694 warned = warning_at (curloc, OPT_Wattributes,
4695 "attribute %qs mismatched positional argument "
4696 "values %i and %i",
4697 attrstr, newa->sizarg + 1, cura->sizarg + 1);
4698
4699 if (warned)
4700 {
4701 /* If the previous declaration is a function (as opposed
4702 to a typedef of one), find the location of the array
4703 or pointer argument that uses the conflicting VLA bound
4704 and point to it in the note. */
4705 const attr_access* const pa = cura->size ? cura : newa;
4706 tree size = pa->size ? TREE_VALUE (pa->size) : NULL_TREE;
4707 if (size && DECL_P (size))
4708 {
4709 location_t argloc = UNKNOWN_LOCATION;
4710 if (tree arg = get_argument (node[2], pa->ptrarg))
4711 argloc = DECL_SOURCE_LOCATION (arg);
4712
4713 gcc_rich_location richloc (DECL_SOURCE_LOCATION (size));
4714 if (argloc != UNKNOWN_LOCATION)
4715 richloc.add_range (argloc);
4716
4717 inform (&richloc, "designating the bound of variable "
4718 "length array argument %u",
4719 pa->ptrarg + 1);
4720 }
4721 else if (prevloc != UNKNOWN_LOCATION)
4722 inform (prevloc, "previous declaration here");
4723 }
4724
4725 continue;
4726 }
4727
4728 if (newa->internal_p == cura->internal_p)
4729 continue;
4730
4731 /* Merge the CURA and NEWA. */
4732 attr_access merged = newaxsref.second;
4733
4734 /* VLA seen in a declaration takes precedence. */
4735 if (cura->minsize == HOST_WIDE_INT_M1U)
4736 merged.minsize = HOST_WIDE_INT_M1U;
4737
4738 /* Use the explicitly specified size positional argument. */
4739 if (cura->sizarg != UINT_MAX)
4740 merged.sizarg = cura->sizarg;
4741
4742 /* Use the explicitly specified mode. */
4743 if (merged.mode == access_deferred)
4744 merged.mode = cura->mode;
4745
4746 tree str = merged.to_internal_string ();
4747 spec += TREE_STRING_POINTER (str);
4748 }
4749
4750 if (!spec.length ())
4751 return NULL_TREE;
4752
4753 return build_string (spec.length (), spec.c_str ());
4754 }
4755
4756 /* Convenience wrapper for the above. */
4757
4758 tree
4759 append_access_attr (tree node[3], tree attrs, const char *attrstr,
4760 char code, HOST_WIDE_INT idxs[2])
4761 {
4762 char attrspec[80];
4763 int n = sprintf (attrspec, "%c%u", code, (unsigned) idxs[0] - 1);
4764 if (idxs[1])
4765 n += sprintf (attrspec + n, ",%u", (unsigned) idxs[1] - 1);
4766
4767 return append_access_attr (node, attrs, attrstr, attrspec);
4768 }
4769
4770 /* Handle the access attribute for function type NODE[0], with the function
4771 DECL optionally in NODE[1]. The handler is called both in response to
4772 an explict attribute access on a declaration with a mode and one or two
4773 positional arguments, and for internally synthesized access specifications
4774 with a string argument optionally followd by a DECL or expression
4775 representing a VLA bound. To speed up parsing, the handler transforms
4776 the attribute and its arguments into a string. */
4777
4778 static tree
4779 handle_access_attribute (tree node[3], tree name, tree args, int flags,
4780 bool *no_add_attrs)
4781 {
4782 tree attrs = TYPE_ATTRIBUTES (*node);
4783 tree type = *node;
4784 if (POINTER_TYPE_P (type))
4785 {
4786 tree ptype = TREE_TYPE (type);
4787 if (FUNC_OR_METHOD_TYPE_P (ptype))
4788 type = ptype;
4789 }
4790
4791 *no_add_attrs = true;
4792
4793 /* Verify a full prototype is provided so that the argument types
4794 can be validated. Avoid diagnosing type-generic built-ins since
4795 those have no prototype. */
4796 if (!args
4797 && !prototype_p (type)
4798 && (!attrs || !lookup_attribute ("type generic", attrs)))
4799 {
4800 error ("attribute %qE without arguments on a non-prototype", name);
4801 return NULL_TREE;
4802 }
4803
4804 tree access_mode = TREE_VALUE (args);
4805 if (TREE_CODE (access_mode) == STRING_CST)
4806 {
4807 const char* const str = TREE_STRING_POINTER (access_mode);
4808 if (*str == '+')
4809 {
4810 /* This is a request to merge an internal specification for
4811 a function declaration involving arrays but no explicit
4812 attribute access. */
4813 tree vblist = TREE_CHAIN (args);
4814 tree axstr = append_access_attr (node, attrs, NULL, str + 1,
4815 vblist);
4816 if (!axstr)
4817 return NULL_TREE;
4818
4819 /* Replace any existing access attribute specification with
4820 the concatenation above. */
4821 tree axsat = tree_cons (NULL_TREE, axstr, vblist);
4822 axsat = tree_cons (name, axsat, NULL_TREE);
4823
4824 /* Recursively call self to "replace" the documented/external
4825 form of the attribute with the condensend internal form. */
4826 decl_attributes (node, axsat, flags | ATTR_FLAG_INTERNAL);
4827 return NULL_TREE;
4828 }
4829
4830 if (flags & ATTR_FLAG_INTERNAL)
4831 {
4832 /* This is a recursive call to handle the condensed internal
4833 form of the attribute (see below). Since all validation
4834 has been done simply return here, accepting the attribute
4835 as is. */
4836 *no_add_attrs = false;
4837 return NULL_TREE;
4838 }
4839 }
4840
4841 /* Set to true when the access mode has the form of a function call
4842 as in 'attribute (read_only (1, 2))'. That's an easy mistake to
4843 make and so worth a special diagnostic. */
4844 bool funcall = false;
4845 if (TREE_CODE (access_mode) == CALL_EXPR)
4846 {
4847 access_mode = CALL_EXPR_FN (access_mode);
4848 if (TREE_CODE (access_mode) != ADDR_EXPR)
4849 {
4850 error ("attribute %qE invalid mode", name);
4851 return NULL_TREE;
4852 }
4853 access_mode = TREE_OPERAND (access_mode, 0);
4854 access_mode = DECL_NAME (access_mode);
4855 funcall = true;
4856 }
4857 else if (TREE_CODE (access_mode) != IDENTIFIER_NODE)
4858 {
4859 error ("attribute %qE mode %qE is not an identifier; expected one of "
4860 "%qs, %qs, %qs, or %qs", name, access_mode,
4861 "read_only", "read_write", "write_only", "none");
4862 return NULL_TREE;
4863 }
4864
4865 const char* const access_str = IDENTIFIER_POINTER (access_mode);
4866 const char *ps = access_str;
4867 if (ps[0] == '_' && ps[1] == '_')
4868 {
4869 size_t len = strlen (ps);
4870 if (ps[len - 1] == '_' && ps[len - 2] == '_')
4871 ps += 2;
4872 }
4873
4874 int imode;
4875
4876 {
4877 const int nmodes =
4878 sizeof attr_access::mode_names / sizeof *attr_access::mode_names;
4879
4880 for (imode = 0; imode != nmodes; ++imode)
4881 if (!strncmp (ps, attr_access::mode_names[imode],
4882 strlen (attr_access::mode_names[imode])))
4883 break;
4884
4885 if (imode == nmodes)
4886 {
4887 error ("attribute %qE invalid mode %qs; expected one of "
4888 "%qs, %qs, %qs, or %qs", name, access_str,
4889 "read_only", "read_write", "write_only", "none");
4890 return NULL_TREE;
4891 }
4892 }
4893
4894 const ::access_mode mode = static_cast<::access_mode>(imode);
4895
4896 if (funcall)
4897 {
4898 error ("attribute %qE unexpected %<(%> after mode %qs; expected "
4899 "a positional argument or %<)%>",
4900 name, access_str);
4901 return NULL_TREE;
4902 }
4903
4904 args = TREE_CHAIN (args);
4905 if (!args)
4906 {
4907 /* The first positional argument is required. It may be worth
4908 dropping the requirement at some point and having read_only
4909 apply to all const-qualified pointers and read_write or
4910 write_only to the rest. */
4911 error ("attribute %<%E(%s)%> missing an argument",
4912 name, access_str);
4913 return NULL_TREE;
4914 }
4915
4916 /* One or more positional arguments have been specified. Validate
4917 them. */
4918 tree idxnodes[2] = { NULL_TREE, NULL_TREE };
4919 tree argtypes[2] = { NULL_TREE, NULL_TREE };
4920 /* 1-based attribute positional arguments or zero if not specified.
4921 Invalid negative or excessive values are also stored but used
4922 only in diagnostics. */
4923 HOST_WIDE_INT idxs[2] = { 0, 0 };
4924
4925 /* Number of function formal arguments (used in diagnostics). */
4926 unsigned nfuncargs = 0;
4927 /* Number of (optional) attribute positional arguments. */
4928 unsigned nattrargs = 0;
4929
4930 for (unsigned i = 0; i != 2; ++i, args = TREE_CHAIN (args), ++nattrargs)
4931 {
4932 if (!args)
4933 break;
4934
4935 idxnodes[i] = TREE_VALUE (args);
4936
4937 if (TREE_CODE (idxnodes[i]) != IDENTIFIER_NODE
4938 && TREE_CODE (idxnodes[i]) != FUNCTION_DECL)
4939 idxnodes[i] = default_conversion (idxnodes[i]);
4940
4941 if (tree_fits_shwi_p (idxnodes[i]))
4942 {
4943 idxs[i] = tree_to_shwi (idxnodes[i]);
4944 argtypes[i] = get_argument_type (type, idxs[i], &nfuncargs);
4945 }
4946 }
4947
4948 if ((nattrargs == 1 && !idxs[0])
4949 || (nattrargs == 2 && (!idxs[0] || !idxs[1])))
4950 {
4951 if (idxnodes[1])
4952 error ("attribute %<%E(%s, %E, %E)%> invalid positional argument %i",
4953 name, access_str, idxnodes[0], idxnodes[1], idxs[0] ? 2 : 1);
4954 else
4955 error ("attribute %<%E(%s, %E)%> invalid positional argument %i",
4956 name, access_str, idxnodes[0], idxs[0] ? 2 : 1);
4957 return NULL_TREE;
4958 }
4959
4960 /* Format the attribute specification to include in diagnostics. */
4961 char attrstr[80];
4962 if (idxnodes[1])
4963 snprintf (attrstr, sizeof attrstr, "%s(%s, %lli, %lli)",
4964 IDENTIFIER_POINTER (name), access_str,
4965 (long long) idxs[0], (long long) idxs[1]);
4966 else if (idxnodes[0])
4967 snprintf (attrstr, sizeof attrstr, "%s(%s, %lli)",
4968 IDENTIFIER_POINTER (name), access_str,
4969 (long long) idxs[0]);
4970 else
4971 snprintf (attrstr, sizeof attrstr, "%s(%s)",
4972 IDENTIFIER_POINTER (name), access_str);
4973
4974 /* Verify the positional argument values are in range. */
4975 if (!argtypes[0] || (idxnodes[1] && !argtypes[1]))
4976 {
4977 if (idxnodes[0])
4978 {
4979 if (idxs[0] < 0 || idxs[1] < 0)
4980 error ("attribute %qs positional argument %i invalid value %wi",
4981 attrstr, idxs[0] < 0 ? 1 : 2,
4982 idxs[0] < 0 ? idxs[0] : idxs[1]);
4983 else
4984 error ("attribute %qs positional argument %i value %wi exceeds "
4985 "number of function arguments %u",
4986 attrstr, idxs[0] ? 1 : 2,
4987 idxs[0] ? idxs[0] : idxs[1],
4988 nfuncargs);
4989 }
4990 else
4991 error ("attribute %qs invalid positional argument", attrstr);
4992
4993 return NULL_TREE;
4994 }
4995
4996 if (!POINTER_TYPE_P (argtypes[0]))
4997 {
4998 /* The first argument must have a pointer or reference type. */
4999 error ("attribute %qs positional argument 1 references "
5000 "non-pointer argument type %qT",
5001 attrstr, argtypes[0]);
5002 return NULL_TREE;
5003 }
5004
5005 {
5006 /* Pointers to functions are not allowed. */
5007 tree ptrtype = TREE_TYPE (argtypes[0]);
5008 if (FUNC_OR_METHOD_TYPE_P (ptrtype))
5009 {
5010 error ("attribute %qs positional argument 1 references "
5011 "argument of function type %qT",
5012 attrstr, ptrtype);
5013 return NULL_TREE;
5014 }
5015 }
5016
5017 if (mode == access_read_write || mode == access_write_only)
5018 {
5019 /* Read_write and write_only modes must reference non-const
5020 arguments. */
5021 if (TYPE_READONLY (TREE_TYPE (argtypes[0])))
5022 {
5023 error ("attribute %qs positional argument 1 references "
5024 "%qs-qualified argument type %qT",
5025 attrstr, "const", argtypes[0]);
5026 return NULL_TREE;
5027 }
5028 }
5029 else if (!TYPE_READONLY (TREE_TYPE (argtypes[0])))
5030 {
5031 /* A read_only mode should ideally reference const-qualified
5032 arguments but it's not diagnosed error if one doesn't.
5033 This makes it possible to annotate legacy, const-incorrect
5034 APIs. It might be worth a diagnostic along the lines of
5035 -Wsuggest-const. */
5036 ;
5037 }
5038
5039 if (argtypes[1] && !INTEGRAL_TYPE_P (argtypes[1]))
5040 {
5041 error ("attribute %qs positional argument 2 references "
5042 "non-integer argument type %qT",
5043 attrstr, argtypes[1]);
5044 return NULL_TREE;
5045 }
5046
5047 /* Verify that the new attribute doesn't conflict with any existing
5048 attributes specified on previous declarations of the same type
5049 and if not, concatenate the two. */
5050 const char code = attr_access::mode_chars[mode];
5051 tree new_attrs = append_access_attr (node, attrs, attrstr, code, idxs);
5052 if (!new_attrs)
5053 return NULL_TREE;
5054
5055 /* Replace any existing access attribute specification with
5056 the concatenation above. */
5057 new_attrs = tree_cons (NULL_TREE, new_attrs, NULL_TREE);
5058 new_attrs = tree_cons (name, new_attrs, NULL_TREE);
5059
5060 if (node[1])
5061 {
5062 /* Repeat for the previously declared type. */
5063 attrs = TYPE_ATTRIBUTES (TREE_TYPE (node[1]));
5064 new_attrs = append_access_attr (node, attrs, attrstr, code, idxs);
5065 if (!new_attrs)
5066 return NULL_TREE;
5067
5068 new_attrs = tree_cons (NULL_TREE, new_attrs, NULL_TREE);
5069 new_attrs = tree_cons (name, new_attrs, NULL_TREE);
5070 }
5071
5072 /* Recursively call self to "replace" the documented/external form
5073 of the attribute with the condensed internal form. */
5074 decl_attributes (node, new_attrs, flags | ATTR_FLAG_INTERNAL);
5075 return NULL_TREE;
5076 }
5077
5078 /* Extract attribute "arg spec" from each FNDECL argument that has it,
5079 build a single attribute access corresponding to all the arguments,
5080 and return the result. SKIP_VOIDPTR set to ignore void* parameters
5081 (used for user-defined functions for which, unlike in for built-ins,
5082 void* cannot be relied on to determine anything about the access
5083 through it or whether it even takes place).
5084
5085 For example, the parameters in the declaration:
5086
5087 void f (int x, int y, char [x][1][y][3], char [y][2][y][5]);
5088
5089 result in the following attribute access:
5090
5091 value: "+^2[*],$0$1^3[*],$1$1"
5092 list: < <0, x> <1, y> >
5093
5094 where the list has a single value which itself is is a list each
5095 of whose <node>s corresponds to one VLA bound for each of the two
5096 parameters. */
5097
5098 tree
5099 build_attr_access_from_parms (tree parms, bool skip_voidptr)
5100 {
5101 /* Maps each named integral argument DECL seen so far to its position
5102 in the argument list; used to associate VLA sizes with arguments. */
5103 hash_map<tree, unsigned> arg2pos;
5104
5105 /* The string representation of the access specification for all
5106 arguments. */
5107 std::string spec;
5108 unsigned argpos = 0;
5109
5110 /* A TREE_LIST of VLA bounds. */
5111 tree vblist = NULL_TREE;
5112
5113 for (tree arg = parms; arg; arg = TREE_CHAIN (arg), ++argpos)
5114 {
5115 if (!DECL_P (arg))
5116 continue;
5117
5118 tree argtype = TREE_TYPE (arg);
5119 if (DECL_NAME (arg) && INTEGRAL_TYPE_P (argtype))
5120 arg2pos.put (arg, argpos);
5121
5122 tree argspec = DECL_ATTRIBUTES (arg);
5123 if (!argspec)
5124 continue;
5125
5126 if (POINTER_TYPE_P (argtype))
5127 {
5128 /* void* arguments in user-defined functions could point to
5129 anything; skip them. */
5130 tree reftype = TREE_TYPE (argtype);
5131 if (skip_voidptr && VOID_TYPE_P (reftype))
5132 continue;
5133 }
5134
5135 /* Each parameter should have at most one "arg spec" attribute. */
5136 argspec = lookup_attribute ("arg spec", argspec);
5137 if (!argspec)
5138 continue;
5139
5140 /* Attribute arg spec should have one or two arguments. */
5141 argspec = TREE_VALUE (argspec);
5142
5143 /* The attribute arg spec string. */
5144 tree str = TREE_VALUE (argspec);
5145 const char *s = TREE_STRING_POINTER (str);
5146
5147 /* Create the attribute access string from the arg spec string,
5148 optionally followed by position of the VLA bound argument if
5149 it is one. */
5150 {
5151 size_t specend = spec.length ();
5152 if (!specend)
5153 {
5154 spec = '+';
5155 specend = 1;
5156 }
5157
5158 /* Format the access string in place. */
5159 int len = snprintf (NULL, 0, "%c%u%s",
5160 attr_access::mode_chars[access_deferred],
5161 argpos, s);
5162 spec.resize (specend + len + 1);
5163 sprintf (&spec[specend], "%c%u%s",
5164 attr_access::mode_chars[access_deferred],
5165 argpos, s);
5166 /* Trim the trailing NUL. */
5167 spec.resize (specend + len);
5168 }
5169
5170 /* The (optional) list of expressions denoting the VLA bounds
5171 N in ARGTYPE <arg>[Ni]...[Nj]...[Nk]. */
5172 tree argvbs = TREE_CHAIN (argspec);
5173 if (argvbs)
5174 {
5175 spec += ',';
5176 /* Add ARGVBS to the list. Their presence is indicated by
5177 appending a comma followed by the dollar sign and, when
5178 it corresponds to a function parameter, the position of
5179 each bound Ni, so it can be distinguished from
5180 an unspecified bound (as in T[*]). The list is in reverse
5181 order of arguments and needs to be reversed to access in
5182 order. */
5183 vblist = tree_cons (NULL_TREE, argvbs, vblist);
5184
5185 unsigned nelts = 0;
5186 for (tree vb = argvbs; vb; vb = TREE_CHAIN (vb), ++nelts)
5187 {
5188 tree bound = TREE_VALUE (vb);
5189 if (const unsigned *psizpos = arg2pos.get (bound))
5190 {
5191 /* BOUND previously seen in the parameter list. */
5192 TREE_PURPOSE (vb) = size_int (*psizpos);
5193 /* Format the position string in place. */
5194 int len = snprintf (NULL, 0, "$%u", *psizpos);
5195 size_t specend = spec.length ();
5196 spec.resize (specend + len + 1);
5197 sprintf (&spec[specend], "$%u", *psizpos);
5198 /* Trim the trailing NUL. */
5199 spec.resize (specend + len);
5200 }
5201 else
5202 {
5203 /* BOUND doesn't name a parameter (it could be a global
5204 variable or an expression such as a function call). */
5205 spec += '$';
5206 }
5207 }
5208 }
5209 }
5210
5211 if (!spec.length ())
5212 return NULL_TREE;
5213
5214 /* Attribute access takes a two or three arguments. Wrap VBLIST in
5215 another list in case it has more nodes than would otherwise fit. */
5216 vblist = build_tree_list (NULL_TREE, vblist);
5217
5218 /* Build a single attribute access with the string describing all
5219 array arguments and an optional list of any non-parameter VLA
5220 bounds in order. */
5221 tree str = build_string (spec.length (), spec.c_str ());
5222 tree attrargs = tree_cons (NULL_TREE, str, vblist);
5223 tree name = get_identifier ("access");
5224 return build_tree_list (name, attrargs);
5225 }
5226
5227 /* Handle a "nothrow" attribute; arguments as in
5228 struct attribute_spec.handler. */
5229
5230 static tree
5231 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5232 int ARG_UNUSED (flags), bool *no_add_attrs)
5233 {
5234 if (TREE_CODE (*node) == FUNCTION_DECL)
5235 TREE_NOTHROW (*node) = 1;
5236 /* ??? TODO: Support types. */
5237 else
5238 {
5239 warning (OPT_Wattributes, "%qE attribute ignored", name);
5240 *no_add_attrs = true;
5241 }
5242
5243 return NULL_TREE;
5244 }
5245
5246 /* Handle a "cleanup" attribute; arguments as in
5247 struct attribute_spec.handler. */
5248
5249 static tree
5250 handle_cleanup_attribute (tree *node, tree name, tree args,
5251 int ARG_UNUSED (flags), bool *no_add_attrs)
5252 {
5253 tree decl = *node;
5254 tree cleanup_id, cleanup_decl;
5255
5256 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
5257 for global destructors in C++. This requires infrastructure that
5258 we don't have generically at the moment. It's also not a feature
5259 we'd be missing too much, since we do have attribute constructor. */
5260 if (!VAR_P (decl) || TREE_STATIC (decl))
5261 {
5262 warning (OPT_Wattributes, "%qE attribute ignored", name);
5263 *no_add_attrs = true;
5264 return NULL_TREE;
5265 }
5266
5267 /* Verify that the argument is a function in scope. */
5268 /* ??? We could support pointers to functions here as well, if
5269 that was considered desirable. */
5270 cleanup_id = TREE_VALUE (args);
5271 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
5272 {
5273 error ("cleanup argument not an identifier");
5274 *no_add_attrs = true;
5275 return NULL_TREE;
5276 }
5277 cleanup_decl = lookup_name (cleanup_id);
5278 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
5279 {
5280 error ("cleanup argument not a function");
5281 *no_add_attrs = true;
5282 return NULL_TREE;
5283 }
5284
5285 /* That the function has proper type is checked with the
5286 eventual call to build_function_call. */
5287
5288 return NULL_TREE;
5289 }
5290
5291 /* Handle a "warn_unused_result" attribute. No special handling. */
5292
5293 static tree
5294 handle_warn_unused_result_attribute (tree *node, tree name,
5295 tree ARG_UNUSED (args),
5296 int ARG_UNUSED (flags), bool *no_add_attrs)
5297 {
5298 /* Ignore the attribute for functions not returning any value. */
5299 if (VOID_TYPE_P (TREE_TYPE (*node)))
5300 {
5301 warning (OPT_Wattributes, "%qE attribute ignored", name);
5302 *no_add_attrs = true;
5303 }
5304
5305 return NULL_TREE;
5306 }
5307
5308 /* Handle a "sentinel" attribute. */
5309
5310 static tree
5311 handle_sentinel_attribute (tree *node, tree name, tree args,
5312 int ARG_UNUSED (flags), bool *no_add_attrs)
5313 {
5314 if (!prototype_p (*node))
5315 {
5316 warning (OPT_Wattributes,
5317 "%qE attribute requires prototypes with named arguments", name);
5318 *no_add_attrs = true;
5319 }
5320 else
5321 {
5322 if (!stdarg_p (*node))
5323 {
5324 warning (OPT_Wattributes,
5325 "%qE attribute only applies to variadic functions", name);
5326 *no_add_attrs = true;
5327 }
5328 }
5329
5330 if (args)
5331 {
5332 tree position = TREE_VALUE (args);
5333 if (position && TREE_CODE (position) != IDENTIFIER_NODE
5334 && TREE_CODE (position) != FUNCTION_DECL)
5335 position = default_conversion (position);
5336
5337 if (TREE_CODE (position) != INTEGER_CST
5338 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
5339 {
5340 warning (OPT_Wattributes,
5341 "requested position is not an integer constant");
5342 *no_add_attrs = true;
5343 }
5344 else
5345 {
5346 if (tree_int_cst_lt (position, integer_zero_node))
5347 {
5348 warning (OPT_Wattributes,
5349 "requested position is less than zero");
5350 *no_add_attrs = true;
5351 }
5352 }
5353 }
5354
5355 return NULL_TREE;
5356 }
5357
5358 /* Handle a "type_generic" attribute. */
5359
5360 static tree
5361 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
5362 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
5363 bool * ARG_UNUSED (no_add_attrs))
5364 {
5365 /* Ensure we have a function type. */
5366 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
5367
5368 /* Ensure we have a variadic function. */
5369 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
5370
5371 return NULL_TREE;
5372 }
5373
5374 /* Handle a "target" attribute. */
5375
5376 static tree
5377 handle_target_attribute (tree *node, tree name, tree args, int flags,
5378 bool *no_add_attrs)
5379 {
5380 /* Ensure we have a function type. */
5381 if (TREE_CODE (*node) != FUNCTION_DECL)
5382 {
5383 warning (OPT_Wattributes, "%qE attribute ignored", name);
5384 *no_add_attrs = true;
5385 }
5386 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
5387 {
5388 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
5389 "with %qs attribute", name, "target_clones");
5390 *no_add_attrs = true;
5391 }
5392 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
5393 flags))
5394 *no_add_attrs = true;
5395
5396 /* Check that there's no empty string in values of the attribute. */
5397 for (tree t = args; t != NULL_TREE; t = TREE_CHAIN (t))
5398 {
5399 tree value = TREE_VALUE (t);
5400 if (TREE_CODE (value) == STRING_CST
5401 && TREE_STRING_LENGTH (value) == 1
5402 && TREE_STRING_POINTER (value)[0] == '\0')
5403 {
5404 warning (OPT_Wattributes, "empty string in attribute %<target%>");
5405 *no_add_attrs = true;
5406 }
5407 }
5408
5409 return NULL_TREE;
5410 }
5411
5412 /* Handle a "target_clones" attribute. */
5413
5414 static tree
5415 handle_target_clones_attribute (tree *node, tree name, tree ARG_UNUSED (args),
5416 int ARG_UNUSED (flags), bool *no_add_attrs)
5417 {
5418 /* Ensure we have a function type. */
5419 if (TREE_CODE (*node) == FUNCTION_DECL)
5420 {
5421 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
5422 {
5423 error ("%qE attribute argument not a string constant", name);
5424 *no_add_attrs = true;
5425 }
5426 else if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
5427 {
5428 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
5429 "with %qs attribute", name, "always_inline");
5430 *no_add_attrs = true;
5431 }
5432 else if (lookup_attribute ("target", DECL_ATTRIBUTES (*node)))
5433 {
5434 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
5435 "with %qs attribute", name, "target");
5436 *no_add_attrs = true;
5437 }
5438 else
5439 /* Do not inline functions with multiple clone targets. */
5440 DECL_UNINLINABLE (*node) = 1;
5441 }
5442 else
5443 {
5444 warning (OPT_Wattributes, "%qE attribute ignored", name);
5445 *no_add_attrs = true;
5446 }
5447 return NULL_TREE;
5448 }
5449
5450 /* For handling "optimize" attribute. arguments as in
5451 struct attribute_spec.handler. */
5452
5453 static tree
5454 handle_optimize_attribute (tree *node, tree name, tree args,
5455 int ARG_UNUSED (flags), bool *no_add_attrs)
5456 {
5457 /* Ensure we have a function type. */
5458 if (TREE_CODE (*node) != FUNCTION_DECL)
5459 {
5460 warning (OPT_Wattributes, "%qE attribute ignored", name);
5461 *no_add_attrs = true;
5462 }
5463 else
5464 {
5465 struct cl_optimization cur_opts;
5466 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
5467
5468 /* Save current options. */
5469 cl_optimization_save (&cur_opts, &global_options, &global_options_set);
5470 tree prev_target_node = build_target_option_node (&global_options,
5471 &global_options_set);
5472
5473 /* If we previously had some optimization options, use them as the
5474 default. */
5475 gcc_options *saved_global_options = NULL;
5476
5477 /* When #pragma GCC optimize pragma is used, it modifies global_options
5478 without calling targetm.override_options_after_change. That can leave
5479 target flags inconsistent for comparison. */
5480 if (flag_checking && optimization_current_node == optimization_default_node)
5481 {
5482 saved_global_options = XNEW (gcc_options);
5483 *saved_global_options = global_options;
5484 }
5485
5486 if (old_opts)
5487 cl_optimization_restore (&global_options, &global_options_set,
5488 TREE_OPTIMIZATION (old_opts));
5489
5490 /* Parse options, and update the vector. */
5491 parse_optimize_options (args, true);
5492 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
5493 = build_optimization_node (&global_options, &global_options_set);
5494 tree target_node = build_target_option_node (&global_options,
5495 &global_options_set);
5496 if (prev_target_node != target_node)
5497 DECL_FUNCTION_SPECIFIC_TARGET (*node) = target_node;
5498
5499 /* Restore current options. */
5500 cl_optimization_restore (&global_options, &global_options_set,
5501 &cur_opts);
5502 cl_target_option_restore (&global_options, &global_options_set,
5503 TREE_TARGET_OPTION (prev_target_node));
5504
5505 if (saved_global_options != NULL)
5506 {
5507 cl_optimization_compare (saved_global_options, &global_options);
5508 free (saved_global_options);
5509 }
5510 }
5511
5512 return NULL_TREE;
5513 }
5514
5515 /* Handle a "no_split_stack" attribute. */
5516
5517 static tree
5518 handle_no_split_stack_attribute (tree *node, tree name,
5519 tree ARG_UNUSED (args),
5520 int ARG_UNUSED (flags),
5521 bool *no_add_attrs)
5522 {
5523 tree decl = *node;
5524
5525 if (TREE_CODE (decl) != FUNCTION_DECL)
5526 {
5527 error_at (DECL_SOURCE_LOCATION (decl),
5528 "%qE attribute applies only to functions", name);
5529 *no_add_attrs = true;
5530 }
5531 else if (DECL_INITIAL (decl))
5532 {
5533 error_at (DECL_SOURCE_LOCATION (decl),
5534 "cannot set %qE attribute after definition", name);
5535 *no_add_attrs = true;
5536 }
5537
5538 return NULL_TREE;
5539 }
5540
5541 /* Handle a "zero_call_used_regs" attribute; arguments as in
5542 struct attribute_spec.handler. */
5543
5544 static tree
5545 handle_zero_call_used_regs_attribute (tree *node, tree name, tree args,
5546 int ARG_UNUSED (flags),
5547 bool *no_add_attrs)
5548 {
5549 tree decl = *node;
5550 tree id = TREE_VALUE (args);
5551
5552 if (TREE_CODE (decl) != FUNCTION_DECL)
5553 {
5554 error_at (DECL_SOURCE_LOCATION (decl),
5555 "%qE attribute applies only to functions", name);
5556 *no_add_attrs = true;
5557 return NULL_TREE;
5558 }
5559
5560 if (TREE_CODE (id) != STRING_CST)
5561 {
5562 error_at (DECL_SOURCE_LOCATION (decl),
5563 "%qE argument not a string", name);
5564 *no_add_attrs = true;
5565 return NULL_TREE;
5566 }
5567
5568 bool found = false;
5569 for (unsigned int i = 0; zero_call_used_regs_opts[i].name != NULL; ++i)
5570 if (strcmp (TREE_STRING_POINTER (id),
5571 zero_call_used_regs_opts[i].name) == 0)
5572 {
5573 found = true;
5574 break;
5575 }
5576
5577 if (!found)
5578 {
5579 error_at (DECL_SOURCE_LOCATION (decl),
5580 "unrecognized %qE attribute argument %qs",
5581 name, TREE_STRING_POINTER (id));
5582 *no_add_attrs = true;
5583 }
5584
5585 return NULL_TREE;
5586 }
5587
5588 /* Handle a "returns_nonnull" attribute; arguments as in
5589 struct attribute_spec.handler. */
5590
5591 static tree
5592 handle_returns_nonnull_attribute (tree *node, tree name, tree, int,
5593 bool *no_add_attrs)
5594 {
5595 // Even without a prototype we still have a return type we can check.
5596 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
5597 {
5598 error ("%qE attribute on a function not returning a pointer", name);
5599 *no_add_attrs = true;
5600 }
5601 return NULL_TREE;
5602 }
5603
5604 /* Handle a "designated_init" attribute; arguments as in
5605 struct attribute_spec.handler. */
5606
5607 static tree
5608 handle_designated_init_attribute (tree *node, tree name, tree, int,
5609 bool *no_add_attrs)
5610 {
5611 if (TREE_CODE (*node) != RECORD_TYPE)
5612 {
5613 error ("%qE attribute is only valid on %<struct%> type", name);
5614 *no_add_attrs = true;
5615 }
5616 return NULL_TREE;
5617 }
5618
5619
5620 /* Handle a "fallthrough" attribute; arguments as in struct
5621 attribute_spec.handler. */
5622
5623 tree
5624 handle_fallthrough_attribute (tree *, tree name, tree, int,
5625 bool *no_add_attrs)
5626 {
5627 pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored", name);
5628 *no_add_attrs = true;
5629 return NULL_TREE;
5630 }
5631
5632 /* Handle a "patchable_function_entry" attributes; arguments as in
5633 struct attribute_spec.handler. */
5634
5635 static tree
5636 handle_patchable_function_entry_attribute (tree *, tree name, tree args,
5637 int, bool *no_add_attrs)
5638 {
5639 for (; args; args = TREE_CHAIN (args))
5640 {
5641 tree val = TREE_VALUE (args);
5642 if (val && TREE_CODE (val) != IDENTIFIER_NODE
5643 && TREE_CODE (val) != FUNCTION_DECL)
5644 val = default_conversion (val);
5645
5646 if (!tree_fits_uhwi_p (val))
5647 {
5648 warning (OPT_Wattributes,
5649 "%qE attribute argument %qE is not an integer constant",
5650 name, val);
5651 *no_add_attrs = true;
5652 return NULL_TREE;
5653 }
5654
5655 if (tree_to_uhwi (val) > USHRT_MAX)
5656 {
5657 warning (OPT_Wattributes,
5658 "%qE attribute argument %qE exceeds %u",
5659 name, val, USHRT_MAX);
5660 *no_add_attrs = true;
5661 return NULL_TREE;
5662 }
5663 }
5664 return NULL_TREE;
5665 }
5666
5667 /* Handle a "NSObject" attributes; arguments as in
5668 struct attribute_spec.handler. */
5669
5670 static tree
5671 handle_nsobject_attribute (tree *node, tree name, tree args,
5672 int /*flags*/, bool *no_add_attrs)
5673 {
5674 *no_add_attrs = true;
5675
5676 /* This attribute only applies to typedefs (or field decls for properties),
5677 we drop it otherwise - but warn about this if enabled. */
5678 if (TREE_CODE (*node) != TYPE_DECL && TREE_CODE (*node) != FIELD_DECL)
5679 {
5680 warning (OPT_WNSObject_attribute, "%qE attribute may be put on a"
5681 " typedef only; attribute is ignored", name);
5682 return NULL_TREE;
5683 }
5684
5685 /* The original implementation only allowed pointers to records, however
5686 recent implementations also allow void *. */
5687 tree type = TREE_TYPE (*node);
5688 if (!type || !POINTER_TYPE_P (type)
5689 || (TREE_CODE (TREE_TYPE (type)) != RECORD_TYPE
5690 && !VOID_TYPE_P (TREE_TYPE (type))))
5691 {
5692 error ("%qE attribute is for pointer types only", name);
5693 return NULL_TREE;
5694 }
5695
5696 tree t = tree_cons (name, args, TYPE_ATTRIBUTES (type));
5697 TREE_TYPE (*node) = build_type_attribute_variant (type, t);
5698
5699 return NULL_TREE;
5700 }
5701
5702 /* Handle a "objc_root_class" attributes; arguments as in
5703 struct attribute_spec.handler. */
5704
5705 static tree
5706 handle_objc_root_class_attribute (tree */*node*/, tree name, tree /*args*/,
5707 int /*flags*/, bool *no_add_attrs)
5708 {
5709 /* This has no meaning outside Objective-C. */
5710 if (!c_dialect_objc())
5711 warning (OPT_Wattributes, "%qE is only applicable to Objective-C"
5712 " class interfaces, attribute ignored", name);
5713
5714 *no_add_attrs = true;
5715 return NULL_TREE;
5716 }
5717
5718 /* Handle an "objc_nullability" attribute; arguments as in
5719 struct attribute_spec.handler. */
5720
5721 static tree
5722 handle_objc_nullability_attribute (tree *node, tree name, tree args,
5723 int /*flags*/,
5724 bool *no_add_attrs)
5725 {
5726 *no_add_attrs = true;
5727
5728 tree type = TREE_TYPE (*node);
5729 if (TREE_CODE (*node) == FUNCTION_DECL)
5730 type = TREE_TYPE (type);
5731
5732 if (type && !POINTER_TYPE_P (type))
5733 {
5734 error ("%qE cannot be applied to non-pointer type %qT", name, type);
5735 return NULL_TREE;
5736 }
5737
5738 /* We accept objc_nullability() with a single argument.
5739 string: "unspecified", "nullable", "nonnull" or "resettable"
5740 integer: 0 and 3 where the values have the same meaning as
5741 the strings. */
5742 tree val = TREE_VALUE (args);
5743 if (TREE_CODE (val) == INTEGER_CST)
5744 {
5745 val = default_conversion (val);
5746 if (!tree_fits_uhwi_p (val) || tree_to_uhwi (val) > 3)
5747 error ("%qE attribute argument %qE is not an integer constant"
5748 " between 0 and 3", name, val);
5749 else
5750 *no_add_attrs = false; /* OK */
5751 }
5752 else if (TREE_CODE (val) == STRING_CST
5753 && (strcmp (TREE_STRING_POINTER (val), "nullable") == 0
5754 || strcmp (TREE_STRING_POINTER (val), "nonnull") == 0
5755 || strcmp (TREE_STRING_POINTER (val), "unspecified") == 0
5756 || strcmp (TREE_STRING_POINTER (val), "resettable") == 0))
5757 *no_add_attrs = false; /* OK */
5758 else if (val != error_mark_node)
5759 error ("%qE attribute argument %qE is not recognised", name, val);
5760
5761 return NULL_TREE;
5762 }
5763
5764 /* Attempt to partially validate a single attribute ATTR as if
5765 it were to be applied to an entity OPER. */
5766
5767 static bool
5768 validate_attribute (location_t atloc, tree oper, tree attr)
5769 {
5770 /* Determine whether the name of the attribute is valid
5771 and fail with an error if not. */
5772 tree atname = get_attribute_name (attr);
5773 if (!lookup_attribute_spec (atname))
5774 {
5775 if (atloc != UNKNOWN_LOCATION)
5776 error_at (atloc, "unknown attribute %qE", atname);
5777 return false;
5778 }
5779
5780 tree args = TREE_VALUE (attr);
5781 if (!args)
5782 return true;
5783
5784 /* FIXME: Do some validation. */
5785 const char *atstr = IDENTIFIER_POINTER (atname);
5786 if (!strcmp (atstr, "format"))
5787 return true;
5788
5789 /* Only when attribute arguments have been provided try to validate
5790 the whole thing. decl_attributes doesn't return an indication of
5791 success or failure so proceed regardless. */
5792 const char tmpname[] = "__builtin_has_attribute_tmp.";
5793 tree tmpid = get_identifier (tmpname);
5794 tree tmpdecl;
5795 if (!strcmp (atstr, "vector_size"))
5796 {
5797 tree type = TYPE_P (oper) ? oper : TREE_TYPE (oper);
5798 /* Check for function type here since type_for_vector_size
5799 strips it while looking for a function's return type. */
5800 if (FUNC_OR_METHOD_TYPE_P (type))
5801 {
5802 warning_at (atloc, OPT_Wattributes,
5803 "invalid operand type %qT for %qs", type, atstr);
5804 return false;
5805 }
5806
5807 type = type_for_vector_size (type);
5808 if (VECTOR_TYPE_P (type))
5809 type = TREE_TYPE (type);
5810 /* Avoid trying to apply attribute vector_size to OPER since
5811 it's overly restrictive. Simply make sure it has the right
5812 type. */
5813 return type_valid_for_vector_size (type, atname, args, NULL);
5814 }
5815
5816 if (TYPE_P (oper))
5817 tmpdecl = build_decl (atloc, TYPE_DECL, tmpid, oper);
5818 else if (DECL_P (oper))
5819 tmpdecl = build_decl (atloc, TREE_CODE (oper), tmpid, TREE_TYPE (oper));
5820 else if (EXPR_P (oper))
5821 tmpdecl = build_decl (atloc, TYPE_DECL, tmpid, TREE_TYPE (oper));
5822 else
5823 return false;
5824
5825 /* Temporarily clear CURRENT_FUNCTION_DECL to make decl_attributes
5826 believe the DECL declared above is at file scope. (See bug 87526.) */
5827 tree save_curfunc = current_function_decl;
5828 current_function_decl = NULL_TREE;
5829 if (DECL_P (tmpdecl))
5830 {
5831 if (DECL_P (oper))
5832 /* An alias cannot be a definition so declare the symbol extern. */
5833 DECL_EXTERNAL (tmpdecl) = true;
5834 /* Attribute visibility only applies to symbols visible from other
5835 translation units so make it "public." */
5836 TREE_PUBLIC (tmpdecl) = TREE_PUBLIC (oper);
5837 }
5838 decl_attributes (&tmpdecl, attr, 0);
5839 current_function_decl = save_curfunc;
5840
5841 /* FIXME: Change decl_attributes to indicate success or failure (and
5842 parameterize it to avoid failing with errors). */
5843 return true;
5844 }
5845
5846 /* Return true if the DECL, EXPR, or TYPE t has been declared with
5847 attribute ATTR. For DECL, consider also its type. For EXPR,
5848 consider just its type. */
5849
5850 bool
5851 has_attribute (location_t atloc, tree t, tree attr, tree (*convert)(tree))
5852 {
5853 if (!attr || !t || t == error_mark_node)
5854 return false;
5855
5856 if (!validate_attribute (atloc, t, attr))
5857 return false;
5858
5859 tree type = NULL_TREE;
5860 tree expr = NULL_TREE;
5861 if (TYPE_P (t))
5862 type = t;
5863 else
5864 {
5865 do
5866 {
5867 /* Determine the array element/member declaration from
5868 a COMPONENT_REF and an INDIRECT_REF involving a refeence. */
5869 STRIP_NOPS (t);
5870 tree_code code = TREE_CODE (t);
5871 if (code == INDIRECT_REF)
5872 {
5873 tree op0 = TREE_OPERAND (t, 0);
5874 if (TREE_CODE (TREE_TYPE (op0)) == REFERENCE_TYPE)
5875 t = op0;
5876 else
5877 break;
5878 }
5879 else if (code == COMPONENT_REF)
5880 t = TREE_OPERAND (t, 1);
5881 else
5882 break;
5883 } while (true);
5884 expr = t;
5885 }
5886
5887 /* Set to true when an attribute is found in the referenced entity
5888 that matches the specified attribute. */
5889 bool found_match = false;
5890
5891 tree atname = get_attribute_name (attr);
5892 const char *namestr = IDENTIFIER_POINTER (atname);
5893
5894 /* Iterate once for a type and twice for a function or variable
5895 declaration: once for the DECL and the second time for its
5896 TYPE. */
5897 for (bool done = false; !found_match && !done; )
5898 {
5899 tree atlist;
5900 if (type)
5901 {
5902 if (type == error_mark_node)
5903 {
5904 /* This could be a label. FIXME: add support for labels. */
5905 warning_at (atloc, OPT_Wattributes,
5906 (TYPE_P (t)
5907 ? G_("%qs attribute not supported for %qT "
5908 "in %<__builtin_has_attribute%>")
5909 : G_("%qs attribute not supported for %qE "
5910 "in %<__builtin_has_attribute%>")),
5911 namestr, t);
5912 return false;
5913 }
5914
5915 /* Clear EXPR to prevent considering it again below. */
5916 atlist = TYPE_ATTRIBUTES (type);
5917 expr = NULL_TREE;
5918 done = true;
5919 }
5920 else if (DECL_P (expr))
5921 {
5922 /* Set TYPE to the DECL's type to process it on the next
5923 iteration. */
5924 atlist = DECL_ATTRIBUTES (expr);
5925 type = TREE_TYPE (expr);
5926 }
5927 else
5928 {
5929 type = TREE_TYPE (expr);
5930 atlist = TYPE_ATTRIBUTES (type);
5931 done = true;
5932 }
5933
5934 /* True when an attribute with the sought name (though not necessarily
5935 with the sought attributes) has been found on the attribute chain. */
5936 bool found_attr = false;
5937
5938 /* When clear, the first mismatched attribute argument results
5939 in failure. Otherwise, the first matched attribute argument
5940 results in success. */
5941 bool attr_nonnull = !strcmp ("nonnull", namestr);
5942 bool ignore_mismatches = attr_nonnull;
5943
5944 /* Iterate over the instances of the sought attribute on the DECL or
5945 TYPE (there may be multiple instances with different arguments). */
5946 for (; (atlist = lookup_attribute (namestr, atlist));
5947 found_attr = true, atlist = TREE_CHAIN (atlist))
5948 {
5949 /* If there are no arguments to match the result is true except
5950 for nonnull where the attribute with no arguments must match. */
5951 if (!TREE_VALUE (attr))
5952 return attr_nonnull ? !TREE_VALUE (atlist) : true;
5953
5954 /* Attribute nonnull with no arguments subsumes all values of
5955 the attribute. FIXME: This is overly broad since it only
5956 applies to pointer arguments, but querying non-pointer
5957 arguments is diagnosed. */
5958 if (!TREE_VALUE (atlist) && attr_nonnull)
5959 return true;
5960
5961 /* Iterate over the DECL or TYPE attribute argument's values. */
5962 for (tree val = TREE_VALUE (atlist); val; val = TREE_CHAIN (val))
5963 {
5964 /* Iterate over the arguments in the sought attribute comparing
5965 their values to those specified for the DECL or TYPE. */
5966 for (tree arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
5967 {
5968 tree v1 = TREE_VALUE (val);
5969 tree v2 = TREE_VALUE (arg);
5970 if (v1 == v2)
5971 return true;
5972
5973 if (!v1 || !v2)
5974 break;
5975
5976 if (TREE_CODE (v1) == IDENTIFIER_NODE
5977 || TREE_CODE (v2) == IDENTIFIER_NODE)
5978 /* Two identifiers are the same if their values are
5979 equal (that's handled above). Otherwise ther are
5980 either not the same or oneis not an identifier. */
5981 return false;
5982
5983 /* Convert to make them equality-comparable. */
5984 v1 = convert (v1);
5985 v2 = convert (v2);
5986
5987 /* A positive value indicates equality, negative means
5988 "don't know." */
5989 if (simple_cst_equal (v1, v2) == 1)
5990 return true;
5991
5992 if (!ignore_mismatches)
5993 break;
5994 }
5995 }
5996 }
5997
5998 if (!found_attr)
5999 {
6000 /* Some attributes are encoded directly in the tree node. */
6001 if (!strcmp ("aligned", namestr))
6002 {
6003 if (tree arg = TREE_VALUE (attr))
6004 {
6005 arg = convert (TREE_VALUE (arg));
6006 if (!tree_fits_uhwi_p (arg))
6007 /* Invalid argument. */;
6008 else if (expr && DECL_P (expr)
6009 && DECL_USER_ALIGN (expr))
6010 found_match = DECL_ALIGN_UNIT (expr) == tree_to_uhwi (arg);
6011 else if (type && TYPE_USER_ALIGN (type))
6012 found_match = TYPE_ALIGN_UNIT (type) == tree_to_uhwi (arg);
6013 }
6014 else if (expr && DECL_P (expr))
6015 found_match = DECL_USER_ALIGN (expr);
6016 else if (type)
6017 found_match = TYPE_USER_ALIGN (type);
6018 }
6019 else if (!strcmp ("const", namestr))
6020 {
6021 if (expr && DECL_P (expr))
6022 found_match = TREE_READONLY (expr);
6023 }
6024 else if (!strcmp ("noreturn", namestr))
6025 {
6026 /* C11 _Noreturn sets the volatile bit without attaching
6027 an attribute to the decl. */
6028 if (expr
6029 && DECL_P (expr)
6030 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (expr)))
6031 found_match = TREE_THIS_VOLATILE (expr);
6032 }
6033 else if (!strcmp ("pure", namestr))
6034 {
6035 if (expr && DECL_P (expr))
6036 found_match = DECL_PURE_P (expr);
6037 }
6038 else if (!strcmp ("deprecated", namestr))
6039 {
6040 found_match = TREE_DEPRECATED (expr ? expr : type);
6041 if (found_match)
6042 return true;
6043 }
6044 else if (!strcmp ("vector_size", namestr))
6045 {
6046 if (!type || !VECTOR_TYPE_P (type))
6047 return false;
6048
6049 if (tree arg = TREE_VALUE (attr))
6050 {
6051 /* Compare the vector size argument for equality. */
6052 arg = convert (TREE_VALUE (arg));
6053 return tree_int_cst_equal (arg, TYPE_SIZE_UNIT (type)) == 1;
6054 }
6055 else
6056 return true;
6057 }
6058 else if (!strcmp ("warn_if_not_aligned", namestr))
6059 {
6060 if (tree arg = TREE_VALUE (attr))
6061 {
6062 arg = convert (TREE_VALUE (arg));
6063 if (expr && DECL_P (expr))
6064 found_match = (DECL_WARN_IF_NOT_ALIGN (expr)
6065 == tree_to_uhwi (arg) * BITS_PER_UNIT);
6066 else if (type)
6067 found_match = (TYPE_WARN_IF_NOT_ALIGN (type)
6068 == tree_to_uhwi (arg) * BITS_PER_UNIT);
6069 }
6070 else if (expr && DECL_P (expr))
6071 found_match = DECL_WARN_IF_NOT_ALIGN (expr);
6072 else if (type)
6073 found_match = TYPE_WARN_IF_NOT_ALIGN (type);
6074 }
6075 else if (!strcmp ("transparent_union", namestr))
6076 {
6077 if (type)
6078 found_match = TYPE_TRANSPARENT_AGGR (type) != 0;
6079 }
6080 else if (!strcmp ("mode", namestr))
6081 {
6082 /* Finally issue a warning for attributes that cannot
6083 be supported in this context. Attribute mode is not
6084 added to a symbol and cannot be determined from it. */
6085 warning_at (atloc, OPT_Wattributes,
6086 "%qs attribute not supported in "
6087 "%<__builtin_has_attribute%>", namestr);
6088 break;
6089 }
6090 }
6091 }
6092 return found_match;
6093 }