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