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