Line data Source code
1 : /* Functions dealing with attribute handling, used by most front ends.
2 : Copyright (C) 1992-2026 Free Software Foundation, Inc.
3 :
4 : This file is part of GCC.
5 :
6 : GCC is free software; you can redistribute it and/or modify it under
7 : the terms of the GNU General Public License as published by the Free
8 : Software Foundation; either version 3, or (at your option) any later
9 : version.
10 :
11 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : for more details.
15 :
16 : You should have received a copy of the GNU General Public License
17 : along with GCC; see the file COPYING3. If not see
18 : <http://www.gnu.org/licenses/>. */
19 :
20 : #define INCLUDE_STRING
21 : #include "config.h"
22 : #include "system.h"
23 : #include "coretypes.h"
24 : #include "target.h"
25 : #include "tree.h"
26 : #include "stringpool.h"
27 : #include "diagnostic-core.h"
28 : #include "attribs.h"
29 : #include "fold-const.h"
30 : #include "ipa-strub.h"
31 : #include "stor-layout.h"
32 : #include "langhooks.h"
33 : #include "plugin.h"
34 : #include "selftest.h"
35 : #include "hash-set.h"
36 : #include "diagnostic.h"
37 : #include "pretty-print.h"
38 : #include "pretty-print-markup.h"
39 : #include "tree-pretty-print.h"
40 : #include "intl.h"
41 : #include "gcc-urlifier.h"
42 : #include "cgraph.h"
43 :
44 : /* Table of the tables of attributes (common, language, format, machine)
45 : searched. */
46 : static array_slice<const scoped_attribute_specs *const> attribute_tables[2];
47 :
48 : /* Substring representation. */
49 :
50 : struct substring
51 : {
52 : const char *str;
53 : int length;
54 : };
55 :
56 : /* Simple hash function to avoid need to scan whole string. */
57 :
58 : static inline hashval_t
59 13427913452 : substring_hash (const char *str, int l)
60 : {
61 13427913452 : return str[0] + str[l - 1] * 256 + l * 65536;
62 : }
63 :
64 : /* Used for attribute_hash. */
65 :
66 : struct attribute_hasher : nofree_ptr_hash <attribute_spec>
67 : {
68 : typedef substring *compare_type;
69 : static inline hashval_t hash (const attribute_spec *);
70 : static inline bool equal (const attribute_spec *, const substring *);
71 : };
72 :
73 : inline hashval_t
74 11364438038 : attribute_hasher::hash (const attribute_spec *spec)
75 : {
76 11364438038 : const int l = strlen (spec->name);
77 11364438038 : return substring_hash (spec->name, l);
78 : }
79 :
80 : inline bool
81 13910110321 : attribute_hasher::equal (const attribute_spec *spec, const substring *str)
82 : {
83 13910110321 : return (strncmp (spec->name, str->str, str->length) == 0
84 13910110321 : && !spec->name[str->length]);
85 : }
86 :
87 : /* Scoped attribute name representation. */
88 :
89 : struct scoped_attributes
90 : {
91 : const char *ns;
92 : vec<attribute_spec> attributes;
93 : hash_table<attribute_hasher> *attribute_hash;
94 : /* True if we should not warn about unknown attributes in this NS. */
95 : bool ignored_p;
96 : };
97 :
98 : /* The table of scope attributes. */
99 : static vec<scoped_attributes> attributes_table;
100 :
101 : static scoped_attributes* find_attribute_namespace (const char*);
102 : static void register_scoped_attribute (const struct attribute_spec *,
103 : scoped_attributes *);
104 : static const struct attribute_spec *lookup_scoped_attribute_spec (const_tree,
105 : const_tree);
106 :
107 : static bool attributes_initialized = false;
108 :
109 : /* Do not use directly; go through get_gnu_namespace instead. */
110 : static GTY(()) tree gnu_namespace_cache;
111 :
112 : /* Return the IDENTIFIER_NODE for the gnu namespace. */
113 :
114 : static tree
115 2846002220 : get_gnu_namespace ()
116 : {
117 2846002220 : if (!gnu_namespace_cache)
118 292859 : gnu_namespace_cache = get_identifier ("gnu");
119 2846002220 : return gnu_namespace_cache;
120 : }
121 :
122 : /* Insert SPECS into its namespace. IGNORED_P is true iff all unknown
123 : attributes in this namespace should be ignored for the purposes of
124 : -Wattributes. The function returns the namespace into which the
125 : attributes have been registered. */
126 :
127 : scoped_attributes *
128 1541088 : register_scoped_attributes (const scoped_attribute_specs &specs,
129 : bool ignored_p /*=false*/)
130 : {
131 1541088 : scoped_attributes *result = NULL;
132 :
133 : /* See if we already have attributes in the namespace NS. */
134 1541088 : result = find_attribute_namespace (specs.ns);
135 :
136 1541088 : if (result == NULL)
137 : {
138 : /* We don't have any namespace NS yet. Create one. */
139 824004 : scoped_attributes sa;
140 :
141 824004 : if (attributes_table.is_empty ())
142 292988 : attributes_table.create (64);
143 :
144 824004 : memset (&sa, 0, sizeof (sa));
145 824004 : sa.ns = specs.ns;
146 824004 : sa.attributes.create (64);
147 824004 : sa.ignored_p = ignored_p;
148 824004 : result = attributes_table.safe_push (sa);
149 824004 : result->attribute_hash = new hash_table<attribute_hasher> (200);
150 : }
151 : else
152 717084 : result->ignored_p |= ignored_p;
153 :
154 : /* Really add the attributes to their namespace now. */
155 43555478 : for (const attribute_spec &attribute : specs.attributes)
156 : {
157 42014390 : result->attributes.safe_push (attribute);
158 42014390 : register_scoped_attribute (&attribute, result);
159 : }
160 :
161 1541088 : gcc_assert (result != NULL);
162 :
163 1541088 : return result;
164 : }
165 :
166 : /* Return the namespace which name is NS, NULL if none exist. */
167 :
168 : static scoped_attributes*
169 2023272201 : find_attribute_namespace (const char* ns)
170 : {
171 7917124573 : for (scoped_attributes &iter : attributes_table)
172 3870602875 : if (ns == iter.ns
173 3842688380 : || (iter.ns != NULL
174 2000314983 : && ns != NULL
175 2000314983 : && !strcmp (iter.ns, ns)))
176 : return &iter;
177 : return NULL;
178 : }
179 :
180 : /* Make some sanity checks on the attribute tables. */
181 :
182 : static void
183 292969 : check_attribute_tables (void)
184 : {
185 292969 : hash_set<pair_hash<nofree_string_hash, nofree_string_hash>> names;
186 :
187 1171876 : for (auto scoped_array : attribute_tables)
188 2126756 : for (auto scoped_attributes : scoped_array)
189 43551627 : for (const attribute_spec &attribute : scoped_attributes->attributes)
190 : {
191 : /* The name must not begin and end with __. */
192 42010809 : const char *name = attribute.name;
193 42010809 : int len = strlen (name);
194 :
195 42010809 : gcc_assert (!(name[0] == '_' && name[1] == '_'
196 : && name[len - 1] == '_' && name[len - 2] == '_'));
197 :
198 : /* The minimum and maximum lengths must be consistent. */
199 42010809 : gcc_assert (attribute.min_length >= 0);
200 :
201 42010809 : gcc_assert (attribute.max_length == -1
202 : || attribute.max_length >= attribute.min_length);
203 :
204 : /* An attribute cannot require both a DECL and a TYPE. */
205 42010809 : gcc_assert (!attribute.decl_required
206 : || !attribute.type_required);
207 :
208 : /* If an attribute requires a function type, in particular
209 : it requires a type. */
210 42010809 : gcc_assert (!attribute.function_type_required
211 : || attribute.type_required);
212 :
213 : /* Check that no name occurs more than once. Names that
214 : begin with '*' are exempt, and may be overridden. */
215 42010809 : const char *ns = scoped_attributes->ns;
216 43828642 : if (name[0] != '*' && names.add ({ ns ? ns : "", name }))
217 0 : gcc_unreachable ();
218 : }
219 292969 : }
220 :
221 : /* Used to stash pointers to allocated memory so that we can free them at
222 : the end of parsing of all TUs. */
223 : static vec<attribute_spec *> ignored_attributes_table;
224 :
225 : /* Parse arguments V of -Wno-attributes=.
226 : Currently we accept:
227 : vendor::attr
228 : vendor::
229 : This functions also registers the parsed attributes so that we don't
230 : warn that we don't recognize them. */
231 :
232 : void
233 293032 : handle_ignored_attributes_option (vec<char *> *v)
234 : {
235 293032 : if (v == nullptr)
236 : return;
237 :
238 330 : for (auto opt : v)
239 : {
240 184 : char *cln = strstr (opt, "::");
241 : /* We don't accept '::attr'. */
242 184 : if (cln == nullptr || cln == opt)
243 : {
244 0 : auto_diagnostic_group d;
245 0 : error ("wrong argument to ignored attributes");
246 0 : inform (input_location, "valid format is %<ns::attr%> or %<ns::%>");
247 0 : continue;
248 0 : }
249 184 : const char *vendor_start = opt;
250 184 : ptrdiff_t vendor_len = cln - opt;
251 184 : const char *attr_start = cln + 2;
252 : /* This could really use rawmemchr :(. */
253 184 : ptrdiff_t attr_len = strchr (attr_start, '\0') - attr_start;
254 : /* Verify that they look valid. */
255 501 : auto valid_p = [](const char *const s, ptrdiff_t len) {
256 317 : bool ok = false;
257 :
258 1596 : for (int i = 0; i < len; ++i)
259 1287 : if (ISALNUM (s[i]))
260 : ok = true;
261 182 : else if (s[i] != '_')
262 : return false;
263 :
264 : return ok;
265 : };
266 184 : if (!valid_p (vendor_start, vendor_len))
267 : {
268 12 : error ("wrong argument to ignored attributes");
269 12 : continue;
270 : }
271 172 : canonicalize_attr_name (vendor_start, vendor_len);
272 : /* We perform all this hijinks so that we don't have to copy OPT. */
273 172 : tree vendor_id = get_identifier_with_length (vendor_start, vendor_len);
274 172 : array_slice<const attribute_spec> attrs;
275 : /* In the "vendor::" case, we should ignore *any* attribute coming
276 : from this attribute namespace. */
277 172 : if (attr_len > 0)
278 : {
279 133 : if (!valid_p (attr_start, attr_len))
280 : {
281 8 : error ("wrong argument to ignored attributes");
282 24 : continue;
283 : }
284 125 : canonicalize_attr_name (attr_start, attr_len);
285 125 : tree attr_id = get_identifier_with_length (attr_start, attr_len);
286 125 : const char *attr = IDENTIFIER_POINTER (attr_id);
287 : /* If we've already seen this vendor::attr, ignore it. Attempting to
288 : register it twice would lead to a crash. */
289 125 : if (lookup_scoped_attribute_spec (vendor_id, attr_id))
290 16 : continue;
291 : /* Create a table with extra attributes which we will register.
292 : We can't free it here, so squirrel away the pointers. */
293 109 : attribute_spec *table = new attribute_spec {
294 : attr, 0, -2, false, false, false, false, nullptr, nullptr
295 109 : };
296 109 : ignored_attributes_table.safe_push (table);
297 109 : attrs = { table, 1 };
298 : }
299 148 : const scoped_attribute_specs scoped_specs = {
300 148 : IDENTIFIER_POINTER (vendor_id), { attrs }
301 148 : };
302 148 : register_scoped_attributes (scoped_specs, attrs.empty ());
303 : }
304 : }
305 :
306 : /* Free data we might have allocated when adding extra attributes. */
307 :
308 : void
309 285029 : free_attr_data ()
310 : {
311 285204 : for (auto x : ignored_attributes_table)
312 109 : delete x;
313 285029 : ignored_attributes_table.release ();
314 285029 : }
315 :
316 : /* Initialize attribute tables, and make some sanity checks if checking is
317 : enabled. */
318 :
319 : void
320 1155562 : init_attributes (void)
321 : {
322 1155562 : if (attributes_initialized)
323 : return;
324 :
325 292988 : attribute_tables[0] = lang_hooks.attribute_table;
326 292988 : attribute_tables[1] = targetm.attribute_table;
327 :
328 292988 : if (flag_checking)
329 292969 : check_attribute_tables ();
330 :
331 878964 : for (auto scoped_array : attribute_tables)
332 2126916 : for (auto scoped_attributes : scoped_array)
333 1540940 : register_scoped_attributes (*scoped_attributes);
334 :
335 292988 : vec<char *> *ignored = (vec<char *> *) flag_ignored_attributes;
336 292988 : handle_ignored_attributes_option (ignored);
337 :
338 292988 : invoke_plugin_callbacks (PLUGIN_ATTRIBUTES, NULL);
339 292988 : attributes_initialized = true;
340 : }
341 :
342 : /* Insert a single ATTR into the attribute table. */
343 :
344 : void
345 1 : register_attribute (const struct attribute_spec *attr)
346 : {
347 1 : register_scoped_attribute (attr, find_attribute_namespace ("gnu"));
348 1 : }
349 :
350 : /* Insert a single attribute ATTR into a namespace of attributes. */
351 :
352 : static void
353 42014391 : register_scoped_attribute (const struct attribute_spec *attr,
354 : scoped_attributes *name_space)
355 : {
356 42014391 : struct substring str;
357 42014391 : attribute_spec **slot;
358 :
359 42014391 : gcc_assert (attr != NULL && name_space != NULL);
360 :
361 42014391 : gcc_assert (name_space->attribute_hash);
362 :
363 42014391 : str.str = attr->name;
364 42014391 : str.length = strlen (str.str);
365 :
366 : /* Attribute names in the table must be in the form 'text' and not
367 : in the form '__text__'. */
368 42014391 : gcc_checking_assert (!canonicalize_attr_name (str.str, str.length));
369 :
370 42014391 : slot = name_space->attribute_hash
371 42014391 : ->find_slot_with_hash (&str, substring_hash (str.str, str.length),
372 : INSERT);
373 42014391 : gcc_assert (!*slot || attr->name[0] == '*');
374 42014391 : *slot = const_cast<struct attribute_spec *> (attr);
375 42014391 : }
376 :
377 : /* Return the spec for the scoped attribute with namespace NS and
378 : name NAME. */
379 :
380 : static const struct attribute_spec *
381 2021730357 : lookup_scoped_attribute_spec (const_tree ns, const_tree name)
382 : {
383 2021730357 : struct substring attr;
384 2021730357 : scoped_attributes *attrs;
385 :
386 4016264233 : const char *ns_str = (ns != NULL_TREE) ? IDENTIFIER_POINTER (ns) : NULL;
387 :
388 2021730357 : attrs = find_attribute_namespace (ns_str);
389 :
390 2021730357 : if (attrs == NULL)
391 : return NULL;
392 :
393 2021461023 : attr.str = IDENTIFIER_POINTER (name);
394 2021461023 : attr.length = IDENTIFIER_LENGTH (name);
395 2021461023 : return attrs->attribute_hash->find_with_hash (&attr,
396 : substring_hash (attr.str,
397 2021461023 : attr.length));
398 : }
399 :
400 : /* Return the spec for the attribute named NAME. If NAME is a TREE_LIST,
401 : it also specifies the attribute namespace. */
402 :
403 : const struct attribute_spec *
404 383371428 : lookup_attribute_spec (const_tree name)
405 : {
406 383371428 : tree ns;
407 383371428 : if (TREE_CODE (name) == TREE_LIST)
408 : {
409 22447244 : ns = TREE_PURPOSE (name);
410 22447244 : name = TREE_VALUE (name);
411 : }
412 : else
413 360924184 : ns = get_gnu_namespace ();
414 383371428 : return lookup_scoped_attribute_spec (ns, name);
415 : }
416 :
417 :
418 : /* Return the namespace of the attribute ATTR. This accessor works on
419 : GNU and C++11 (scoped) attributes. On GNU attributes,
420 : it returns an identifier tree for the string "gnu".
421 :
422 : Please read the comments of cxx11_attribute_p to understand the
423 : format of attributes. */
424 :
425 : tree
426 2531216565 : get_attribute_namespace (const_tree attr)
427 : {
428 2531216565 : if (cxx11_attribute_p (attr))
429 46138529 : return TREE_PURPOSE (TREE_PURPOSE (attr));
430 2485078036 : return get_gnu_namespace ();
431 : }
432 :
433 : /* Check LAST_DECL and NODE of the same symbol for attributes that are
434 : recorded in SPEC to be mutually exclusive with ATTRNAME, diagnose
435 : them, and return true if any have been found. NODE can be a DECL
436 : or a TYPE. */
437 :
438 : static bool
439 360563076 : diag_attr_exclusions (tree last_decl, tree node, tree attrname,
440 : const attribute_spec *spec)
441 : {
442 360563076 : const attribute_spec::exclusions *excl = spec->exclude;
443 :
444 360563076 : tree_code code = TREE_CODE (node);
445 :
446 360563076 : if ((code == FUNCTION_DECL && !excl->function
447 0 : && (!excl->type || !spec->affects_type_identity))
448 360563076 : || (code == VAR_DECL && !excl->variable
449 : && (!excl->type || !spec->affects_type_identity))
450 360525774 : || (((code == TYPE_DECL || RECORD_OR_UNION_TYPE_P (node)) && !excl->type)))
451 : return false;
452 :
453 : /* True if an attribute that's mutually exclusive with ATTRNAME
454 : has been found. */
455 360246896 : bool found = false;
456 :
457 360246896 : if (last_decl && last_decl != node && TREE_TYPE (last_decl) != node)
458 : {
459 : /* Check both the last DECL and its type for conflicts with
460 : the attribute being added to the current decl or type. */
461 1946690 : found |= diag_attr_exclusions (last_decl, last_decl, attrname, spec);
462 1946690 : tree decl_type = TREE_TYPE (last_decl);
463 1946690 : found |= diag_attr_exclusions (last_decl, decl_type, attrname, spec);
464 : }
465 :
466 : /* NODE is either the current DECL to which the attribute is being
467 : applied or its TYPE. For the former, consider the attributes on
468 : both the DECL and its type. */
469 360246896 : tree attrs[2];
470 :
471 360246896 : if (DECL_P (node))
472 : {
473 352904455 : attrs[0] = DECL_ATTRIBUTES (node);
474 352904455 : if (TREE_TYPE (node))
475 352904452 : attrs[1] = TYPE_ATTRIBUTES (TREE_TYPE (node));
476 : else
477 : /* TREE_TYPE can be NULL e.g. while processing attributes on
478 : enumerators. */
479 3 : attrs[1] = NULL_TREE;
480 : }
481 : else
482 : {
483 7342441 : attrs[0] = TYPE_ATTRIBUTES (node);
484 7342441 : attrs[1] = NULL_TREE;
485 : }
486 :
487 : /* Iterate over the mutually exclusive attribute names and verify
488 : that the symbol doesn't contain it. */
489 1080740688 : for (unsigned i = 0; i != ARRAY_SIZE (attrs); ++i)
490 : {
491 720493792 : if (!attrs[i])
492 631686756 : continue;
493 :
494 276961654 : for ( ; excl->name; ++excl)
495 : {
496 : /* Avoid checking the attribute against itself. */
497 188154618 : if (is_attribute_p (excl->name, attrname))
498 188154203 : continue;
499 :
500 174078907 : if (!lookup_attribute (excl->name, attrs[i]))
501 174077310 : continue;
502 :
503 : /* An exclusion may apply either to a function declaration,
504 : type declaration, or a field/variable declaration, or
505 : any subset of the three. */
506 1597 : if (TREE_CODE (node) == FUNCTION_DECL
507 353 : && !excl->function)
508 0 : continue;
509 :
510 1597 : if (TREE_CODE (node) == TYPE_DECL
511 0 : && !excl->type)
512 0 : continue;
513 :
514 1597 : if ((TREE_CODE (node) == FIELD_DECL
515 1597 : || VAR_P (node))
516 1203 : && !excl->variable)
517 1182 : continue;
518 :
519 415 : found = true;
520 :
521 : /* Print a note? */
522 415 : bool note = last_decl != NULL_TREE;
523 415 : auto_diagnostic_group d;
524 415 : if (TREE_CODE (node) == FUNCTION_DECL
525 415 : && fndecl_built_in_p (node))
526 0 : note &= warning (OPT_Wattributes,
527 : "ignoring attribute %qE in declaration of "
528 : "a built-in function %qD because it conflicts "
529 : "with attribute %qs",
530 0 : attrname, node, excl->name);
531 : else
532 415 : note &= warning (OPT_Wattributes,
533 : "ignoring attribute %qE because "
534 : "it conflicts with attribute %qs",
535 415 : attrname, excl->name);
536 :
537 415 : if (note)
538 180 : inform (DECL_SOURCE_LOCATION (last_decl),
539 : "previous declaration here");
540 415 : }
541 : }
542 :
543 : return found;
544 : }
545 :
546 : /* Return true iff we should not complain about unknown attributes
547 : coming from the attribute namespace NS. This is the case for
548 : the -Wno-attributes=ns:: command-line option. */
549 :
550 : static bool
551 806 : attr_namespace_ignored_p (tree ns)
552 : {
553 806 : if (ns == NULL_TREE)
554 : return false;
555 755 : scoped_attributes *r = find_attribute_namespace (IDENTIFIER_POINTER (ns));
556 755 : return r && r->ignored_p;
557 : }
558 :
559 : /* Return true if the attribute ATTR should not be warned about. */
560 :
561 : bool
562 1635231264 : attribute_ignored_p (tree attr)
563 : {
564 1635231264 : if (!cxx11_attribute_p (attr))
565 : return false;
566 19732450 : if (tree ns = get_attribute_namespace (attr))
567 : {
568 4446420 : const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (attr));
569 4446420 : if (as == NULL && attr_namespace_ignored_p (ns))
570 : return true;
571 4446353 : if (as && as->max_length == -2)
572 221 : return true;
573 : }
574 : return false;
575 : }
576 :
577 : /* Like above, but takes an attribute_spec AS, which must be nonnull. */
578 :
579 : bool
580 94570 : attribute_ignored_p (const attribute_spec *const as)
581 : {
582 94570 : return as->max_length == -2;
583 : }
584 :
585 : /* Return true if the ATTRS chain contains at least one attribute which
586 : is not ignored. */
587 :
588 : bool
589 1509 : any_nonignored_attribute_p (tree attrs)
590 : {
591 1581 : for (tree attr = attrs; attr; attr = TREE_CHAIN (attr))
592 1554 : if (!attribute_ignored_p (attr))
593 : return true;
594 :
595 : return false;
596 : }
597 :
598 : /* See whether LIST contains at least one instance of attribute ATTR
599 : (possibly with different arguments). Return the first such attribute
600 : if so, otherwise return null. */
601 :
602 : static tree
603 1640336637 : find_same_attribute (const_tree attr, tree list)
604 : {
605 1640336637 : if (list == NULL_TREE)
606 : return NULL_TREE;
607 847532932 : tree ns = get_attribute_namespace (attr);
608 847532932 : tree name = get_attribute_name (attr);
609 1695065864 : return private_lookup_attribute (ns ? IDENTIFIER_POINTER (ns) : nullptr,
610 847532932 : IDENTIFIER_POINTER (name),
611 847354850 : ns ? IDENTIFIER_LENGTH (ns) : 0,
612 1695065864 : IDENTIFIER_LENGTH (name), list);
613 : }
614 :
615 : /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
616 : which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
617 : it should be modified in place; if a TYPE, a copy should be created
618 : unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
619 : information, in the form of a bitwise OR of flags in enum attribute_flags
620 : from tree.h. Depending on these flags, some attributes may be
621 : returned to be applied at a later stage (for example, to apply
622 : a decl attribute to the declaration rather than to its type). */
623 :
624 : tree
625 1937441847 : decl_attributes (tree *node, tree attributes, int flags,
626 : tree last_decl /* = NULL_TREE */)
627 : {
628 1937441847 : tree returned_attrs = NULL_TREE;
629 :
630 1937441847 : if (TREE_TYPE (*node) == error_mark_node || attributes == error_mark_node)
631 : return NULL_TREE;
632 :
633 1937441507 : if (!attributes_initialized)
634 292988 : init_attributes ();
635 :
636 1937441507 : auto_urlify_attributes sentinel;
637 :
638 : /* If this is a function and the user used #pragma GCC optimize, add the
639 : options to the attribute((optimize(...))) list. */
640 1937441507 : if (TREE_CODE (*node) == FUNCTION_DECL && current_optimize_pragma)
641 : {
642 342862 : tree cur_attr = lookup_attribute ("optimize", attributes);
643 342862 : tree opts = copy_list (current_optimize_pragma);
644 :
645 342862 : if (! cur_attr)
646 342859 : attributes
647 342859 : = tree_cons (get_identifier ("optimize"), opts, attributes);
648 : else
649 3 : TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
650 : }
651 :
652 1937441507 : if (TREE_CODE (*node) == FUNCTION_DECL
653 1172024603 : && (optimization_current_node != optimization_default_node
654 1171967623 : || target_option_current_node != target_option_default_node)
655 1962979538 : && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node))
656 : {
657 25490200 : DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) = optimization_current_node;
658 : /* Don't set DECL_FUNCTION_SPECIFIC_TARGET for targets that don't
659 : support #pragma GCC target or target attribute. */
660 25490200 : if (target_option_default_node)
661 : {
662 25490200 : tree cur_tree
663 25490200 : = build_target_option_node (&global_options, &global_options_set);
664 25490200 : tree old_tree = DECL_FUNCTION_SPECIFIC_TARGET (*node);
665 25490200 : if (!old_tree)
666 25490200 : old_tree = target_option_default_node;
667 : /* The changes on optimization options can cause the changes in
668 : target options, update it accordingly if it's changed. */
669 25490200 : if (old_tree != cur_tree)
670 25460821 : DECL_FUNCTION_SPECIFIC_TARGET (*node) = cur_tree;
671 : }
672 : }
673 :
674 : /* If this is a function and the user used #pragma GCC target, add the
675 : options to the attribute((target(...))) list. */
676 1937441507 : if (TREE_CODE (*node) == FUNCTION_DECL
677 1172024603 : && current_target_pragma
678 1962378058 : && targetm.target_option.valid_attribute_p (*node,
679 : get_identifier ("target"),
680 : current_target_pragma, 0))
681 : {
682 24936551 : tree cur_attr = lookup_attribute ("target", attributes);
683 24936551 : tree opts = copy_list (current_target_pragma);
684 :
685 24936551 : if (! cur_attr)
686 24936550 : attributes = tree_cons (get_identifier ("target"), opts, attributes);
687 : else
688 1 : TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
689 : }
690 :
691 : /* A "naked" function attribute implies "noinline" and "noclone" for
692 : those targets that support it. */
693 1937441507 : if (TREE_CODE (*node) == FUNCTION_DECL
694 1172024603 : && attributes
695 581254515 : && lookup_attribute ("naked", attributes) != NULL
696 79 : && lookup_attribute_spec (get_identifier ("naked"))
697 1937441586 : && lookup_attribute ("noipa", attributes) == NULL)
698 79 : attributes = tree_cons (get_identifier ("noipa"), NULL, attributes);
699 :
700 : /* A "noipa" function attribute implies "noinline", "noclone" and "no_icf"
701 : for those targets that support it. */
702 1937441507 : if (TREE_CODE (*node) == FUNCTION_DECL
703 1172024603 : && attributes
704 581254515 : && lookup_attribute ("noipa", attributes) != NULL
705 1937462562 : && lookup_attribute_spec (get_identifier ("noipa")))
706 : {
707 21055 : if (lookup_attribute ("noinline", attributes) == NULL)
708 20150 : attributes = tree_cons (get_identifier ("noinline"), NULL, attributes);
709 :
710 21055 : if (lookup_attribute ("noclone", attributes) == NULL)
711 20715 : attributes = tree_cons (get_identifier ("noclone"), NULL, attributes);
712 :
713 21055 : if (lookup_attribute ("no_icf", attributes) == NULL)
714 21045 : attributes = tree_cons (get_identifier ("no_icf"), NULL, attributes);
715 : }
716 :
717 1937441507 : targetm.insert_attributes (*node, &attributes);
718 :
719 : /* Note that attributes on the same declaration are not necessarily
720 : in the same order as in the source. */
721 3575800310 : for (tree attr = attributes; attr; attr = TREE_CHAIN (attr))
722 : {
723 1638358804 : tree ns = get_attribute_namespace (attr);
724 1638358804 : tree name = get_attribute_name (attr);
725 1638358804 : tree args = TREE_VALUE (attr);
726 1638358804 : tree *anode = node;
727 1638358804 : const struct attribute_spec *spec
728 1638358804 : = lookup_scoped_attribute_spec (ns, name);
729 1638358804 : int fn_ptr_quals = 0;
730 1638358804 : tree fn_ptr_tmp = NULL_TREE;
731 1638358804 : const bool cxx11_attr_p = cxx11_attribute_p (attr);
732 :
733 1638358804 : if (spec == NULL)
734 : {
735 3109277 : if (!(flags & (int) ATTR_FLAG_BUILT_IN)
736 3109277 : && !attr_namespace_ignored_p (ns))
737 : {
738 583 : if (ns == NULL_TREE || !cxx11_attr_p)
739 206 : warning (OPT_Wattributes, "%qE attribute directive ignored",
740 : name);
741 186 : else if ((flag_openmp || flag_openmp_simd)
742 191 : && is_attribute_p ("omp", ns)
743 191 : && is_attribute_p ("directive", name)
744 568 : && (VAR_P (*node)
745 20 : || TREE_CODE (*node) == FUNCTION_DECL))
746 3129506 : continue;
747 : else
748 194 : warning (OPT_Wattributes,
749 : "%<%E::%E%> scoped attribute directive ignored",
750 : ns, name);
751 : }
752 3109093 : continue;
753 : }
754 : else
755 : {
756 1635249527 : int nargs = list_length (args);
757 1635249527 : if (nargs < spec->min_length
758 1635249493 : || (spec->max_length >= 0
759 1557853494 : && nargs > spec->max_length))
760 : {
761 88 : auto_diagnostic_group d;
762 88 : error ("wrong number of arguments specified for %qE attribute",
763 : name);
764 88 : if (spec->max_length < 0)
765 0 : inform (input_location, "expected %i or more, found %i",
766 0 : spec->min_length, nargs);
767 88 : else if (spec->min_length == spec->max_length)
768 45 : inform (input_location, "expected %i, found %i",
769 : spec->min_length, nargs);
770 : else
771 43 : inform (input_location, "expected between %i and %i, found %i",
772 : spec->min_length, spec->max_length, nargs);
773 88 : continue;
774 88 : }
775 : }
776 1635249439 : gcc_assert (is_attribute_p (spec->name, name));
777 :
778 1635249439 : if (spec->decl_required && !DECL_P (*anode))
779 : {
780 18981 : if (flags & ((int) ATTR_FLAG_DECL_NEXT
781 : | (int) ATTR_FLAG_FUNCTION_NEXT
782 : | (int) ATTR_FLAG_ARRAY_NEXT))
783 : {
784 : /* Pass on this attribute to be tried again. */
785 18743 : tree attr = tree_cons (name, args, NULL_TREE);
786 18743 : returned_attrs = chainon (returned_attrs, attr);
787 18743 : continue;
788 18743 : }
789 : else
790 : {
791 238 : warning (OPT_Wattributes, "%qE attribute does not apply to types",
792 : name);
793 238 : continue;
794 : }
795 : }
796 :
797 : /* If we require a type, but were passed a decl, set up to make a
798 : new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE
799 : would have applied if we'd been passed a type, but we cannot modify
800 : the decl's type in place here. */
801 1635230458 : if (spec->type_required && DECL_P (*anode))
802 : {
803 246895133 : anode = &TREE_TYPE (*anode);
804 246895133 : flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
805 : }
806 :
807 1635230458 : if (spec->function_type_required
808 245869995 : && !FUNC_OR_METHOD_TYPE_P (*anode))
809 : {
810 833 : if (TREE_CODE (*anode) == POINTER_TYPE
811 833 : && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (*anode)))
812 : {
813 : /* OK, this is a bit convoluted. We can't just make a copy
814 : of the pointer type and modify its TREE_TYPE, because if
815 : we change the attributes of the target type the pointer
816 : type needs to have a different TYPE_MAIN_VARIANT. So we
817 : pull out the target type now, frob it as appropriate, and
818 : rebuild the pointer type later.
819 :
820 : This would all be simpler if attributes were part of the
821 : declarator, grumble grumble. */
822 298 : fn_ptr_tmp = TREE_TYPE (*anode);
823 298 : fn_ptr_quals = TYPE_QUALS (*anode);
824 298 : anode = &fn_ptr_tmp;
825 298 : flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
826 : }
827 535 : else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
828 : {
829 : /* Pass on this attribute to be tried again. */
830 461 : tree attr = tree_cons (name, args, NULL_TREE);
831 461 : returned_attrs = chainon (returned_attrs, attr);
832 461 : continue;
833 461 : }
834 :
835 446 : if (TREE_CODE (*anode) != FUNCTION_TYPE
836 372 : && TREE_CODE (*anode) != METHOD_TYPE)
837 : {
838 74 : warning (OPT_Wattributes,
839 : "%qE attribute only applies to function types",
840 : name);
841 74 : continue;
842 : }
843 : }
844 :
845 1635229962 : if (TYPE_P (*anode)
846 248641433 : && (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
847 1635782417 : && COMPLETE_TYPE_P (*anode))
848 : {
849 39 : warning (OPT_Wattributes, "type attributes ignored after type is already defined");
850 39 : continue;
851 : }
852 :
853 1635229884 : bool no_add_attrs = false;
854 :
855 : /* Check for exclusions with other attributes on the current
856 : declaration as well as the last declaration of the same
857 : symbol already processed (if one exists). Detect and
858 : reject incompatible attributes. */
859 1635229884 : bool built_in = flags & ATTR_FLAG_BUILT_IN;
860 1635229884 : if (spec->exclude
861 352736959 : && (flag_checking || !built_in)
862 1987947252 : && !error_operand_p (last_decl))
863 : {
864 : /* Always check attributes on user-defined functions.
865 : Check them on built-ins only when -fchecking is set.
866 : Ignore __builtin_unreachable -- it's both const and
867 : noreturn. */
868 :
869 352717367 : if (!built_in
870 237085930 : || !DECL_P (*anode)
871 232162916 : || DECL_BUILT_IN_CLASS (*anode) != BUILT_IN_NORMAL
872 584880283 : || (DECL_FUNCTION_CODE (*anode) != BUILT_IN_UNREACHABLE
873 231462641 : && DECL_FUNCTION_CODE (*anode) != BUILT_IN_UNREACHABLE_TRAP
874 230762381 : && (DECL_FUNCTION_CODE (*anode)
875 : != BUILT_IN_UBSAN_HANDLE_BUILTIN_UNREACHABLE)))
876 : {
877 351274691 : bool no_add = diag_attr_exclusions (last_decl, *anode, name, spec);
878 351274691 : if (!no_add && anode != node)
879 5395005 : no_add = diag_attr_exclusions (last_decl, *node, name, spec);
880 351274691 : no_add_attrs |= no_add;
881 : }
882 : }
883 :
884 1635230471 : if (no_add_attrs
885 : /* Don't add attributes registered just for -Wno-attributes=foo::bar
886 : purposes. */
887 1635229884 : || attribute_ignored_p (attr))
888 587 : continue;
889 :
890 1635229297 : if (spec->handler != NULL)
891 : {
892 1634967996 : int cxx11_flag = (cxx11_attr_p ? ATTR_FLAG_CXX11 : 0);
893 :
894 : /* Pass in an array of the current declaration followed
895 : by the last pushed/merged declaration if one exists.
896 : For calls that modify the type attributes of a DECL
897 : and for which *ANODE is *NODE's type, also pass in
898 : the DECL as the third element to use in diagnostics.
899 : If the handler changes CUR_AND_LAST_DECL[0] replace
900 : *ANODE with its value. */
901 1634967996 : tree cur_and_last_decl[3] = { *anode, last_decl };
902 1634967996 : if (anode != node && DECL_P (*node))
903 246643945 : cur_and_last_decl[2] = *node;
904 :
905 1634967996 : tree ret = (spec->handler) (cur_and_last_decl, name, args,
906 : flags|cxx11_flag, &no_add_attrs);
907 :
908 : /* Fix up typedefs clobbered by attribute handlers. */
909 1634967996 : if (TREE_CODE (*node) == TYPE_DECL
910 853971 : && anode == &TREE_TYPE (*node)
911 575272 : && DECL_ORIGINAL_TYPE (*node)
912 2277 : && TYPE_NAME (*anode) == *node
913 1634968000 : && TYPE_NAME (cur_and_last_decl[0]) != *node)
914 : {
915 4 : tree t = cur_and_last_decl[0];
916 4 : DECL_ORIGINAL_TYPE (*node) = t;
917 4 : tree tt = build_variant_type_copy (t);
918 4 : cur_and_last_decl[0] = tt;
919 4 : TREE_TYPE (*node) = tt;
920 4 : TYPE_NAME (tt) = *node;
921 : }
922 :
923 1634967996 : if (*anode != cur_and_last_decl[0])
924 : {
925 : /* Even if !spec->function_type_required, allow the attribute
926 : handler to request the attribute to be applied to the function
927 : type, rather than to the function pointer type, by setting
928 : cur_and_last_decl[0] to the function type. */
929 1754015 : if (!fn_ptr_tmp
930 1753997 : && POINTER_TYPE_P (*anode)
931 11229 : && TREE_TYPE (*anode) == cur_and_last_decl[0]
932 1754149 : && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (*anode)))
933 : {
934 134 : fn_ptr_tmp = TREE_TYPE (*anode);
935 134 : fn_ptr_quals = TYPE_QUALS (*anode);
936 134 : anode = &fn_ptr_tmp;
937 : }
938 1754015 : *anode = cur_and_last_decl[0];
939 : }
940 :
941 1634967996 : if (ret == error_mark_node)
942 : {
943 302 : warning (OPT_Wattributes, "%qE attribute ignored", name);
944 302 : no_add_attrs = true;
945 : }
946 : else
947 1634967694 : returned_attrs = chainon (ret, returned_attrs);
948 : }
949 :
950 : /* Layout the decl in case anything changed. */
951 1635229297 : if (spec->type_required && DECL_P (*node)
952 : && (VAR_P (*node)
953 : || TREE_CODE (*node) == PARM_DECL
954 : || TREE_CODE (*node) == RESULT_DECL))
955 3449 : relayout_decl (*node);
956 :
957 1635229297 : if (!no_add_attrs)
958 : {
959 1631455665 : tree old_attrs;
960 1631455665 : tree a;
961 :
962 1631455665 : if (DECL_P (*anode))
963 1386103167 : old_attrs = DECL_ATTRIBUTES (*anode);
964 : else
965 245352498 : old_attrs = TYPE_ATTRIBUTES (*anode);
966 :
967 1631455665 : for (a = find_same_attribute (attr, old_attrs);
968 1638516043 : a != NULL_TREE;
969 7060378 : a = find_same_attribute (attr, TREE_CHAIN (a)))
970 13093072 : if (attribute_value_equal (a, attr))
971 : break;
972 :
973 1631455665 : if (a == NULL_TREE)
974 : {
975 : /* This attribute isn't already in the list. */
976 1625422971 : tree r;
977 : /* Preserve the C++11 form. */
978 1625422971 : if (cxx11_attr_p)
979 19401796 : r = tree_cons (build_tree_list (ns, name), args, old_attrs);
980 : else
981 1606021175 : r = tree_cons (name, args, old_attrs);
982 :
983 1625422971 : if (DECL_P (*anode))
984 1380070793 : DECL_ATTRIBUTES (*anode) = r;
985 245352178 : else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
986 : {
987 261862 : TYPE_ATTRIBUTES (*anode) = r;
988 : /* If this is the main variant, also push the attributes
989 : out to the other variants. */
990 261862 : if (*anode == TYPE_MAIN_VARIANT (*anode))
991 : {
992 644051 : for (tree variant = *anode; variant;
993 382189 : variant = TYPE_NEXT_VARIANT (variant))
994 : {
995 382189 : if (TYPE_ATTRIBUTES (variant) == old_attrs)
996 240654 : TYPE_ATTRIBUTES (variant)
997 120327 : = TYPE_ATTRIBUTES (*anode);
998 261862 : else if (!find_same_attribute
999 261862 : (attr, TYPE_ATTRIBUTES (variant)))
1000 0 : TYPE_ATTRIBUTES (variant) = tree_cons
1001 0 : (name, args, TYPE_ATTRIBUTES (variant));
1002 : }
1003 : }
1004 : }
1005 : else
1006 245090316 : *anode = build_type_attribute_variant (*anode, r);
1007 : }
1008 : }
1009 :
1010 1635229297 : if (fn_ptr_tmp)
1011 : {
1012 : /* Rebuild the function pointer type and put it in the
1013 : appropriate place. */
1014 432 : fn_ptr_tmp = build_pointer_type (fn_ptr_tmp);
1015 432 : if (fn_ptr_quals)
1016 22 : fn_ptr_tmp = build_qualified_type (fn_ptr_tmp, fn_ptr_quals);
1017 432 : if (DECL_P (*node))
1018 413 : TREE_TYPE (*node) = fn_ptr_tmp;
1019 : else
1020 : {
1021 19 : gcc_assert (TREE_CODE (*node) == POINTER_TYPE);
1022 19 : *node = fn_ptr_tmp;
1023 : }
1024 : }
1025 : }
1026 :
1027 1937441506 : return returned_attrs;
1028 1937441506 : }
1029 :
1030 : /* Return TRUE iff ATTR has been parsed by the front-end as a C++-11
1031 : attribute.
1032 :
1033 : When G++ parses a C++11 attribute, it is represented as
1034 : a TREE_LIST which TREE_PURPOSE is itself a TREE_LIST. TREE_PURPOSE
1035 : (TREE_PURPOSE (ATTR)) is the namespace of the attribute, and the
1036 : TREE_VALUE (TREE_PURPOSE (ATTR)) is its non-qualified name. Please
1037 : use get_attribute_namespace and get_attribute_name to retrieve the
1038 : namespace and name of the attribute, as these accessors work with
1039 : GNU attributes as well. */
1040 :
1041 : bool
1042 35669993743 : cxx11_attribute_p (const_tree attr)
1043 : {
1044 35669993743 : if (attr == NULL_TREE
1045 35666520878 : || TREE_CODE (attr) != TREE_LIST)
1046 : return false;
1047 :
1048 35666520828 : return (TREE_CODE (TREE_PURPOSE (attr)) == TREE_LIST);
1049 : }
1050 :
1051 : /* Return the name of the attribute ATTR. This accessor works on GNU
1052 : and C++11 (scoped) attributes.
1053 :
1054 : Please read the comments of cxx11_attribute_p to understand the
1055 : format of attributes. */
1056 :
1057 : tree
1058 29838445008 : get_attribute_name (const_tree attr)
1059 : {
1060 29838445008 : if (cxx11_attribute_p (attr))
1061 1448192230 : return TREE_VALUE (TREE_PURPOSE (attr));
1062 28390252778 : return TREE_PURPOSE (attr);
1063 : }
1064 :
1065 : /* Subroutine of set_method_tm_attributes. Apply TM attribute ATTR
1066 : to the method FNDECL. */
1067 :
1068 : void
1069 3684 : apply_tm_attr (tree fndecl, tree attr)
1070 : {
1071 3684 : decl_attributes (&TREE_TYPE (fndecl), tree_cons (attr, NULL, NULL), 0);
1072 3684 : }
1073 :
1074 : /* Makes a function attribute of the form NAME(ARG_NAME) and chains
1075 : it to CHAIN. */
1076 :
1077 : tree
1078 456 : make_attribute (string_slice name, string_slice arg_name, tree chain)
1079 : {
1080 456 : tree attr_name = get_identifier_with_length (name.begin (), name.size ());
1081 456 : tree attr_arg_name = build_string (arg_name.size (), arg_name.begin ());
1082 456 : tree attr_args = tree_cons (NULL_TREE, attr_arg_name, NULL_TREE);
1083 456 : tree attr = tree_cons (attr_name, attr_args, chain);
1084 456 : return attr;
1085 : }
1086 :
1087 : /* Default implementation of TARGET_OPTION_FUNCTIONS_B_RESOLVABLE_FROM_A.
1088 : Used to check very basically if DECL_B is callable from DECL_A.
1089 : For now this checks if the version strings are the same. */
1090 :
1091 : bool
1092 393 : functions_b_resolvable_from_a (tree decl_a, tree decl_b,
1093 : tree base ATTRIBUTE_UNUSED)
1094 : {
1095 393 : const char *attr_name = TARGET_HAS_FMV_TARGET_ATTRIBUTE
1096 : ? "target"
1097 : : "target_version";
1098 :
1099 393 : tree attr_a = lookup_attribute (attr_name, DECL_ATTRIBUTES (decl_a));
1100 393 : tree attr_b = lookup_attribute (attr_name, DECL_ATTRIBUTES (decl_b));
1101 :
1102 393 : gcc_assert (attr_b);
1103 393 : if (!attr_a)
1104 : return false;
1105 :
1106 19 : return attribute_value_equal (attr_a, attr_b);
1107 : }
1108 :
1109 : /* Comparator function to be used in qsort routine to sort attribute
1110 : specification strings to "target". */
1111 :
1112 : static int
1113 7788 : attr_strcmp (const void *v1, const void *v2)
1114 : {
1115 7788 : const char *c1 = *(char *const*)v1;
1116 7788 : const char *c2 = *(char *const*)v2;
1117 7788 : return strcmp (c1, c2);
1118 : }
1119 :
1120 : /* ARGLIST is the argument to target attribute. This function tokenizes
1121 : the TARGET_CLONES_ATTR_SEPARATOR separated arguments, sorts them and
1122 : returns a string which is a unique identifier for the
1123 : TARGET_CLONES_ATTR_SEPARATOR separated arguments. It also replaces
1124 : non-identifier characters "=,-" with "_". */
1125 :
1126 : char *
1127 38133 : sorted_attr_string (tree arglist)
1128 : {
1129 38133 : tree arg;
1130 38133 : size_t str_len_sum = 0;
1131 38133 : char **args = NULL;
1132 38133 : char *attr_str, *ret_str;
1133 38133 : char *attr = NULL;
1134 38133 : unsigned int argnum = 1;
1135 38133 : unsigned int i;
1136 38133 : static const char separator_str[] = { TARGET_CLONES_ATTR_SEPARATOR, 0 };
1137 :
1138 76284 : for (arg = arglist; arg; arg = TREE_CHAIN (arg))
1139 : {
1140 38151 : const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
1141 38151 : size_t len = strlen (str);
1142 38151 : str_len_sum += len + 1;
1143 38151 : if (arg != arglist)
1144 18 : argnum++;
1145 371569 : for (i = 0; i < strlen (str); i++)
1146 333418 : if (str[i] == TARGET_CLONES_ATTR_SEPARATOR)
1147 1929 : argnum++;
1148 : }
1149 :
1150 38133 : attr_str = XNEWVEC (char, str_len_sum);
1151 38133 : str_len_sum = 0;
1152 114417 : for (arg = arglist; arg; arg = TREE_CHAIN (arg))
1153 : {
1154 38151 : const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
1155 38151 : size_t len = strlen (str);
1156 38151 : memcpy (attr_str + str_len_sum, str, len);
1157 76302 : attr_str[str_len_sum + len]
1158 38151 : = TREE_CHAIN (arg) ? TARGET_CLONES_ATTR_SEPARATOR : '\0';
1159 38151 : str_len_sum += len + 1;
1160 : }
1161 :
1162 : /* Replace "=,-" with "_". */
1163 371569 : for (i = 0; i < strlen (attr_str); i++)
1164 333436 : if (attr_str[i] == '=' || attr_str[i]== '-')
1165 19434 : attr_str[i] = '_';
1166 :
1167 38133 : if (argnum == 1)
1168 : return attr_str;
1169 :
1170 1947 : args = XNEWVEC (char *, argnum);
1171 :
1172 1947 : i = 0;
1173 1947 : attr = strtok (attr_str, separator_str);
1174 7788 : while (attr != NULL)
1175 : {
1176 3894 : args[i] = attr;
1177 3894 : i++;
1178 3894 : attr = strtok (NULL, separator_str);
1179 : }
1180 :
1181 1947 : qsort (args, argnum, sizeof (char *), attr_strcmp);
1182 :
1183 1947 : ret_str = XNEWVEC (char, str_len_sum);
1184 1947 : str_len_sum = 0;
1185 7788 : for (i = 0; i < argnum; i++)
1186 : {
1187 3894 : size_t len = strlen (args[i]);
1188 3894 : memcpy (ret_str + str_len_sum, args[i], len);
1189 3894 : ret_str[str_len_sum + len] = i < argnum - 1 ? '_' : '\0';
1190 3894 : str_len_sum += len + 1;
1191 : }
1192 :
1193 1947 : XDELETEVEC (args);
1194 1947 : XDELETEVEC (attr_str);
1195 1947 : return ret_str;
1196 : }
1197 :
1198 : /* Make a dispatcher declaration for the multi-versioned function DECL.
1199 : Calls to DECL function will be replaced with calls to the dispatcher
1200 : by the front-end. Return the decl created. */
1201 :
1202 : tree
1203 206 : make_dispatcher_decl (const tree decl)
1204 : {
1205 206 : tree fn_type = TREE_TYPE (decl);
1206 206 : tree func_type = build_function_type (TREE_TYPE (fn_type),
1207 206 : TYPE_ARG_TYPES (fn_type));
1208 206 : tree func_decl = build_fn_decl (IDENTIFIER_POINTER (DECL_NAME (decl)),
1209 : func_type);
1210 :
1211 206 : TREE_USED (func_decl) = 1;
1212 206 : DECL_CONTEXT (func_decl) = NULL_TREE;
1213 206 : DECL_INITIAL (func_decl) = error_mark_node;
1214 206 : DECL_ARTIFICIAL (func_decl) = 1;
1215 : /* Mark this func as external, the resolver will flip it again if
1216 : it gets generated. */
1217 206 : DECL_EXTERNAL (func_decl) = 1;
1218 : /* This will be of type IFUNCs have to be externally visible. */
1219 206 : TREE_PUBLIC (func_decl) = 1;
1220 206 : TREE_NOTHROW (func_decl) = TREE_NOTHROW (decl);
1221 :
1222 : /* Set the decl name to avoid graph_node re-mangling it. */
1223 206 : SET_DECL_ASSEMBLER_NAME (func_decl, DECL_ASSEMBLER_NAME (decl));
1224 :
1225 206 : cgraph_node *node = cgraph_node::get (decl);
1226 206 : gcc_assert (node);
1227 206 : cgraph_function_version_info *node_v = node->function_version ();
1228 206 : gcc_assert (node_v);
1229 :
1230 : /* Set flags on the cgraph_node for the new decl. */
1231 206 : cgraph_node *func_node = cgraph_node::get_create (func_decl);
1232 206 : func_node->dispatcher_function = true;
1233 : /* For targets with TARGET_HAS_FMV_TARGET_ATTRIBUTE, the resolver is created
1234 : unconditionally if any versioned nodes are present.
1235 : For !TARGET_HAS_FMV_TARGET_ATTRIBUTE, the dispatcher is only defined when
1236 : the default node is defined. */
1237 206 : func_node->definition = node->definition || TARGET_HAS_FMV_TARGET_ATTRIBUTE;
1238 :
1239 206 : cgraph_function_version_info *func_v
1240 206 : = func_node->insert_new_function_version ();
1241 206 : func_v->next = node_v;
1242 206 : func_v->assembler_name = node_v->assembler_name;
1243 :
1244 : /* If the default node is from a target_clone, mark the dispatcher as from
1245 : target_clone. */
1246 206 : func_node->is_target_clone = node->is_target_clone;
1247 :
1248 : /* Get the assembler name by mangling with the base assembler name. */
1249 206 : tree id = targetm.mangle_decl_assembler_name
1250 206 : (func_decl, func_v->assembler_name);
1251 206 : symtab->change_decl_assembler_name (func_decl, id);
1252 :
1253 206 : return func_decl;
1254 : }
1255 :
1256 : /* Returns true if DECL a multiversioned default.
1257 : With the target attribute semantics, returns true if the function is marked
1258 : as default with the target version.
1259 : With the target_version attribute semantics, returns true if the function
1260 : is either not annotated, annotated as default, or is a target_clone
1261 : containing the default declaration. */
1262 :
1263 : bool
1264 9803 : is_function_default_version (const tree decl)
1265 : {
1266 9803 : tree attr;
1267 9803 : if (TREE_CODE (decl) != FUNCTION_DECL)
1268 : return false;
1269 9803 : if (TARGET_HAS_FMV_TARGET_ATTRIBUTE)
1270 : {
1271 9803 : if (!DECL_FUNCTION_VERSIONED (decl))
1272 : return false;
1273 8726 : attr = lookup_attribute ("target", DECL_ATTRIBUTES (decl));
1274 8726 : gcc_assert (attr);
1275 : }
1276 : else
1277 : {
1278 : if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (decl)))
1279 : {
1280 : int num_defaults = 0;
1281 : get_clone_versions (decl, &num_defaults);
1282 : return num_defaults > 0;
1283 : }
1284 :
1285 : attr = lookup_attribute ("target_version", DECL_ATTRIBUTES (decl));
1286 : if (!attr)
1287 : return true;
1288 : }
1289 8726 : attr = TREE_VALUE (TREE_VALUE (attr));
1290 8726 : return (TREE_CODE (attr) == STRING_CST
1291 8726 : && strcmp (TREE_STRING_POINTER (attr), "default") == 0);
1292 : }
1293 :
1294 : /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
1295 : is ATTRIBUTE. */
1296 :
1297 : tree
1298 52794035 : build_decl_attribute_variant (tree ddecl, tree attribute)
1299 : {
1300 52794035 : DECL_ATTRIBUTES (ddecl) = attribute;
1301 52794035 : return ddecl;
1302 : }
1303 :
1304 : /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
1305 : is ATTRIBUTE and its qualifiers are QUALS.
1306 :
1307 : Record such modified types already made so we don't make duplicates. */
1308 :
1309 : tree
1310 400742440 : build_type_attribute_qual_variant (tree otype, tree attribute, int quals)
1311 : {
1312 400742440 : tree ttype = otype;
1313 400742440 : if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
1314 : {
1315 257056017 : tree ntype;
1316 :
1317 : /* Building a distinct copy of a tagged type is inappropriate; it
1318 : causes breakage in code that expects there to be a one-to-one
1319 : relationship between a struct and its fields.
1320 : build_duplicate_type is another solution (as used in
1321 : handle_transparent_union_attribute), but that doesn't play well
1322 : with the stronger C++ type identity model. */
1323 257056017 : if (RECORD_OR_UNION_TYPE_P (ttype)
1324 257056014 : || TREE_CODE (ttype) == ENUMERAL_TYPE)
1325 : {
1326 3 : warning (OPT_Wattributes,
1327 : "ignoring attributes applied to %qT after definition",
1328 3 : TYPE_MAIN_VARIANT (ttype));
1329 3 : return build_qualified_type (ttype, quals);
1330 : }
1331 :
1332 257056014 : ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
1333 257056014 : if (lang_hooks.types.copy_lang_qualifiers
1334 257056014 : && otype != TYPE_MAIN_VARIANT (otype))
1335 9120680 : ttype = (lang_hooks.types.copy_lang_qualifiers
1336 9120680 : (ttype, TYPE_MAIN_VARIANT (otype)));
1337 :
1338 257056014 : tree dtype = ntype = build_distinct_type_copy (ttype);
1339 :
1340 257056014 : TYPE_ATTRIBUTES (ntype) = attribute;
1341 : /* If the target-dependent attributes make NTYPE different from
1342 : its canonical type, we will need to use structural equality
1343 : checks for this type.
1344 :
1345 : We shouldn't get here for stripping attributes from a type;
1346 : the no-attribute type might not need structural comparison. But
1347 : we can if was discarded from type_hash_table. */
1348 257056014 : if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
1349 257056014 : || !comp_type_attributes (ntype, ttype))
1350 4972079 : SET_TYPE_STRUCTURAL_EQUALITY (ntype);
1351 :
1352 257056014 : hashval_t hash = type_hash_canon_hash (ntype);
1353 257056014 : ntype = type_hash_canon (hash, ntype);
1354 :
1355 257056014 : if (ntype != dtype)
1356 : /* This variant was already in the hash table, don't mess with
1357 : TYPE_CANONICAL. */;
1358 61998210 : else if (TYPE_CANONICAL (ntype) == ntype)
1359 59711799 : TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
1360 :
1361 257056014 : ttype = build_qualified_type (ntype, quals);
1362 257056014 : if (lang_hooks.types.copy_lang_qualifiers
1363 257056014 : && otype != TYPE_MAIN_VARIANT (otype))
1364 9120680 : ttype = lang_hooks.types.copy_lang_qualifiers (ttype, otype);
1365 : }
1366 143686423 : else if (TYPE_QUALS (ttype) != quals)
1367 927373 : ttype = build_qualified_type (ttype, quals);
1368 :
1369 : return ttype;
1370 : }
1371 :
1372 : /* Compare two identifier nodes representing attributes.
1373 : Return true if they are the same, false otherwise. */
1374 :
1375 : static bool
1376 76229704 : cmp_attrib_identifiers (const_tree attr1, const_tree attr2)
1377 : {
1378 : /* Make sure we're dealing with IDENTIFIER_NODEs. */
1379 76229704 : gcc_checking_assert (TREE_CODE (attr1) == IDENTIFIER_NODE
1380 : && TREE_CODE (attr2) == IDENTIFIER_NODE);
1381 :
1382 : /* Identifiers can be compared directly for equality. */
1383 76229704 : if (attr1 == attr2)
1384 : return true;
1385 :
1386 115635382 : return cmp_attribs (IDENTIFIER_POINTER (attr1), IDENTIFIER_LENGTH (attr1),
1387 39405678 : IDENTIFIER_POINTER (attr2), IDENTIFIER_LENGTH (attr2));
1388 : }
1389 :
1390 : /* Compare two constructor-element-type constants. Return 1 if the lists
1391 : are known to be equal; otherwise return 0. */
1392 :
1393 : bool
1394 27075693 : simple_cst_list_equal (const_tree l1, const_tree l2)
1395 : {
1396 41335935 : while (l1 != NULL_TREE && l2 != NULL_TREE)
1397 : {
1398 30061276 : if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
1399 : return false;
1400 :
1401 14260242 : l1 = TREE_CHAIN (l1);
1402 14260242 : l2 = TREE_CHAIN (l2);
1403 : }
1404 :
1405 11274659 : return l1 == l2;
1406 : }
1407 :
1408 : /* Check if "omp declare simd" attribute arguments, CLAUSES1 and CLAUSES2, are
1409 : the same. */
1410 :
1411 : static bool
1412 0 : omp_declare_simd_clauses_equal (tree clauses1, tree clauses2)
1413 : {
1414 0 : tree cl1, cl2;
1415 0 : for (cl1 = clauses1, cl2 = clauses2;
1416 0 : cl1 && cl2;
1417 0 : cl1 = OMP_CLAUSE_CHAIN (cl1), cl2 = OMP_CLAUSE_CHAIN (cl2))
1418 : {
1419 0 : if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_CODE (cl2))
1420 : return false;
1421 0 : if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_SIMDLEN)
1422 : {
1423 0 : if (simple_cst_equal (OMP_CLAUSE_DECL (cl1),
1424 0 : OMP_CLAUSE_DECL (cl2)) != 1)
1425 : return false;
1426 : }
1427 0 : switch (OMP_CLAUSE_CODE (cl1))
1428 : {
1429 0 : case OMP_CLAUSE_ALIGNED:
1430 0 : if (simple_cst_equal (OMP_CLAUSE_ALIGNED_ALIGNMENT (cl1),
1431 0 : OMP_CLAUSE_ALIGNED_ALIGNMENT (cl2)) != 1)
1432 : return false;
1433 : break;
1434 0 : case OMP_CLAUSE_LINEAR:
1435 0 : if (simple_cst_equal (OMP_CLAUSE_LINEAR_STEP (cl1),
1436 0 : OMP_CLAUSE_LINEAR_STEP (cl2)) != 1)
1437 : return false;
1438 : break;
1439 0 : case OMP_CLAUSE_SIMDLEN:
1440 0 : if (simple_cst_equal (OMP_CLAUSE_SIMDLEN_EXPR (cl1),
1441 0 : OMP_CLAUSE_SIMDLEN_EXPR (cl2)) != 1)
1442 : return false;
1443 0 : if (OMP_CLAUSE_SIMDLEN_TYPE (cl1) != OMP_CLAUSE_SIMDLEN_TYPE (cl2))
1444 : return false;
1445 0 : if ((OMP_CLAUSE_SIMDLEN_DIVISOR (cl1) == NULL_TREE)
1446 0 : != (OMP_CLAUSE_SIMDLEN_DIVISOR (cl2) == NULL_TREE)
1447 0 : || (OMP_CLAUSE_SIMDLEN_DIVISOR (cl1)
1448 0 : && simple_cst_equal (OMP_CLAUSE_SIMDLEN_DIVISOR (cl1),
1449 0 : OMP_CLAUSE_SIMDLEN_DIVISOR (cl2)) != 1))
1450 : return false;
1451 : default:
1452 : break;
1453 : }
1454 : }
1455 : return true;
1456 : }
1457 :
1458 :
1459 : /* Compare two attributes for their value identity. Return true if the
1460 : attribute values are known to be equal; otherwise return false. */
1461 :
1462 : bool
1463 49473299 : attribute_value_equal (const_tree attr1, const_tree attr2)
1464 : {
1465 49473299 : if (TREE_VALUE (attr1) == TREE_VALUE (attr2))
1466 : return true;
1467 :
1468 33676874 : if (TREE_VALUE (attr1) != NULL_TREE
1469 31391137 : && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST
1470 31391122 : && TREE_VALUE (attr2) != NULL_TREE
1471 63475088 : && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST)
1472 : {
1473 29798214 : if (ATTR_UNIQUE_VALUE_P (TREE_VALUE (attr1))
1474 29798214 : || ATTR_UNIQUE_VALUE_P (TREE_VALUE (attr2)))
1475 : return false;
1476 :
1477 : /* Handle attribute format. */
1478 29797252 : if (is_attribute_p ("format", get_attribute_name (attr1)))
1479 : {
1480 3177499 : attr1 = TREE_VALUE (attr1);
1481 3177499 : attr2 = TREE_VALUE (attr2);
1482 : /* Compare the archetypes (printf/scanf/strftime/...). */
1483 3177499 : if (!cmp_attrib_identifiers (TREE_VALUE (attr1), TREE_VALUE (attr2)))
1484 : return false;
1485 : /* Archetypes are the same. Compare the rest. */
1486 449393 : return (simple_cst_list_equal (TREE_CHAIN (attr1),
1487 898786 : TREE_CHAIN (attr2)) == 1);
1488 : }
1489 53239506 : return (simple_cst_list_equal (TREE_VALUE (attr1),
1490 53239506 : TREE_VALUE (attr2)) == 1);
1491 : }
1492 :
1493 3878660 : if (TREE_VALUE (attr1)
1494 1592923 : && TREE_CODE (TREE_VALUE (attr1)) == OMP_CLAUSE
1495 0 : && TREE_VALUE (attr2)
1496 3878660 : && TREE_CODE (TREE_VALUE (attr2)) == OMP_CLAUSE)
1497 0 : return omp_declare_simd_clauses_equal (TREE_VALUE (attr1),
1498 0 : TREE_VALUE (attr2));
1499 :
1500 3878660 : return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1);
1501 : }
1502 :
1503 : /* Return 0 if the attributes for two types are incompatible, 1 if they
1504 : are compatible, and 2 if they are nearly compatible (which causes a
1505 : warning to be generated). */
1506 : int
1507 1177415924 : comp_type_attributes (const_tree type1, const_tree type2)
1508 : {
1509 1177415924 : const_tree a1 = TYPE_ATTRIBUTES (type1);
1510 1177415924 : const_tree a2 = TYPE_ATTRIBUTES (type2);
1511 1177415924 : const_tree a;
1512 :
1513 1177415924 : if (a1 == a2)
1514 : return 1;
1515 575153467 : for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
1516 : {
1517 301873984 : const struct attribute_spec *as;
1518 301873984 : const_tree attr;
1519 :
1520 301873984 : as = lookup_attribute_spec (TREE_PURPOSE (a));
1521 301873984 : if (!as || as->affects_type_identity == false)
1522 300323297 : continue;
1523 :
1524 1550687 : attr = find_same_attribute (a, const_cast<tree> (a2));
1525 1550687 : if (!attr || !attribute_value_equal (a, attr))
1526 : break;
1527 : }
1528 274827784 : if (!a)
1529 : {
1530 311441838 : for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
1531 : {
1532 38168014 : const struct attribute_spec *as;
1533 :
1534 38168014 : as = lookup_attribute_spec (TREE_PURPOSE (a));
1535 38168014 : if (!as || as->affects_type_identity == false)
1536 38159969 : continue;
1537 :
1538 8045 : if (!find_same_attribute (a, const_cast<tree> (a1)))
1539 : break;
1540 : /* We don't need to compare trees again, as we did this
1541 : already in first loop. */
1542 : }
1543 : /* All types - affecting identity - are equal, so
1544 : there is no need to call target hook for comparison. */
1545 273279483 : if (!a)
1546 : return 1;
1547 : }
1548 1553960 : if (lookup_attribute ("transaction_safe", const_cast<tree> (a)))
1549 : return 0;
1550 1549930 : if ((lookup_attribute ("nocf_check", TYPE_ATTRIBUTES (type1)) != NULL)
1551 1549930 : ^ (lookup_attribute ("nocf_check", TYPE_ATTRIBUTES (type2)) != NULL))
1552 : return 0;
1553 1549759 : int strub_ret = strub_comptypes (const_cast<tree> (type1),
1554 : const_cast<tree> (type2));
1555 1549759 : if (strub_ret == 0)
1556 : return strub_ret;
1557 : /* As some type combinations - like default calling-convention - might
1558 : be compatible, we have to call the target hook to get the final result. */
1559 1547314 : int target_ret = targetm.comp_type_attributes (type1, type2);
1560 1547314 : if (target_ret == 0)
1561 : return target_ret;
1562 752458 : if (strub_ret == 2 || target_ret == 2)
1563 : return 2;
1564 750548 : if (strub_ret == 1 && target_ret == 1)
1565 : return 1;
1566 0 : gcc_unreachable ();
1567 : }
1568 :
1569 : /* PREDICATE acts as a function of type:
1570 :
1571 : (const_tree attr, const attribute_spec *as) -> bool
1572 :
1573 : where ATTR is an attribute and AS is its possibly-null specification.
1574 : Return a list of every attribute in attribute list ATTRS for which
1575 : PREDICATE is true. Return ATTRS itself if PREDICATE returns true
1576 : for every attribute. */
1577 :
1578 : template<typename Predicate>
1579 : tree
1580 438007 : remove_attributes_matching (tree attrs, Predicate predicate)
1581 : {
1582 438007 : tree new_attrs = NULL_TREE;
1583 438007 : tree *ptr = &new_attrs;
1584 438007 : const_tree start = attrs;
1585 830869 : for (const_tree attr = attrs; attr; attr = TREE_CHAIN (attr))
1586 : {
1587 392862 : const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (attr));
1588 : const_tree end;
1589 392862 : if (!predicate (attr, as))
1590 378148 : end = attr;
1591 14714 : else if (start == attrs)
1592 14714 : continue;
1593 : else
1594 0 : end = TREE_CHAIN (attr);
1595 :
1596 378148 : for (; start != end; start = TREE_CHAIN (start))
1597 : {
1598 0 : *ptr = tree_cons (TREE_PURPOSE (start),
1599 0 : TREE_VALUE (start), NULL_TREE);
1600 0 : TREE_CHAIN (*ptr) = NULL_TREE;
1601 0 : ptr = &TREE_CHAIN (*ptr);
1602 : }
1603 378148 : start = TREE_CHAIN (attr);
1604 : }
1605 438007 : gcc_assert (!start || start == attrs);
1606 438007 : return start ? attrs : new_attrs;
1607 : }
1608 :
1609 : /* If VALUE is true, return the subset of ATTRS that affect type identity,
1610 : otherwise return the subset of ATTRS that don't affect type identity. */
1611 :
1612 : tree
1613 378146 : affects_type_identity_attributes (tree attrs, bool value)
1614 : {
1615 756294 : auto predicate = [value](const_tree, const attribute_spec *as) -> bool
1616 : {
1617 378148 : return bool (as && as->affects_type_identity) == value;
1618 378146 : };
1619 378146 : return remove_attributes_matching (attrs, predicate);
1620 : }
1621 :
1622 : /* Remove attributes that affect type identity from ATTRS unless the
1623 : same attributes occur in OK_ATTRS. */
1624 :
1625 : tree
1626 59861 : restrict_type_identity_attributes_to (tree attrs, tree ok_attrs)
1627 : {
1628 74575 : auto predicate = [ok_attrs](const_tree attr,
1629 : const attribute_spec *as) -> bool
1630 : {
1631 14714 : if (!as || !as->affects_type_identity)
1632 : return true;
1633 :
1634 0 : for (tree ok_attr = lookup_attribute (as->name, ok_attrs);
1635 0 : ok_attr;
1636 0 : ok_attr = lookup_attribute (as->name, TREE_CHAIN (ok_attr)))
1637 0 : if (simple_cst_equal (TREE_VALUE (ok_attr), TREE_VALUE (attr)) == 1)
1638 : return true;
1639 :
1640 : return false;
1641 59861 : };
1642 59861 : return remove_attributes_matching (attrs, predicate);
1643 : }
1644 :
1645 : /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
1646 : is ATTRIBUTE.
1647 :
1648 : Record such modified types already made so we don't make duplicates. */
1649 :
1650 : tree
1651 392178461 : build_type_attribute_variant (tree ttype, tree attribute)
1652 : {
1653 784356922 : return build_type_attribute_qual_variant (ttype, attribute,
1654 392178461 : TYPE_QUALS (ttype));
1655 : }
1656 :
1657 : /* A variant of lookup_attribute() that can be used with an identifier
1658 : as the first argument, and where the identifier can be either
1659 : 'text' or '__text__'.
1660 :
1661 : Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
1662 : return a pointer to the attribute's list element if the attribute
1663 : is part of the list, or NULL_TREE if not found. If the attribute
1664 : appears more than once, this only returns the first occurrence; the
1665 : TREE_CHAIN of the return value should be passed back in if further
1666 : occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
1667 : can be in the form 'text' or '__text__'. */
1668 : static tree
1669 304843757 : lookup_ident_attribute (tree attr_identifier, tree list)
1670 : {
1671 304843757 : gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE);
1672 :
1673 341521329 : while (list)
1674 : {
1675 73052205 : gcc_checking_assert (TREE_CODE (get_attribute_name (list))
1676 : == IDENTIFIER_NODE);
1677 :
1678 73052205 : if (cmp_attrib_identifiers (attr_identifier,
1679 73052205 : get_attribute_name (list)))
1680 : /* Found it. */
1681 : break;
1682 36677572 : list = TREE_CHAIN (list);
1683 : }
1684 :
1685 304843757 : return list;
1686 : }
1687 :
1688 : /* Remove any instances of attribute ATTR_NAME in LIST and return the
1689 : modified list. */
1690 :
1691 : tree
1692 123279575 : remove_attribute (const char *attr_name, tree list)
1693 : {
1694 123279575 : tree *p;
1695 123279575 : gcc_checking_assert (attr_name[0] != '_');
1696 :
1697 159714584 : for (p = &list; *p;)
1698 : {
1699 36435009 : tree l = *p;
1700 :
1701 36435009 : tree attr = get_attribute_name (l);
1702 36435009 : if (is_attribute_p (attr_name, attr))
1703 3215 : *p = TREE_CHAIN (l);
1704 : else
1705 36431794 : p = &TREE_CHAIN (l);
1706 : }
1707 :
1708 123279575 : return list;
1709 : }
1710 :
1711 : /* Similarly but also match namespace on the removed attributes.
1712 : ATTR_NS "" stands for NULL or "gnu" namespace. */
1713 :
1714 : tree
1715 488948 : remove_attribute (const char *attr_ns, const char *attr_name, tree list)
1716 : {
1717 488948 : tree *p;
1718 488948 : gcc_checking_assert (attr_name[0] != '_');
1719 488948 : gcc_checking_assert (attr_ns == NULL || attr_ns[0] != '_');
1720 :
1721 502471 : for (p = &list; *p;)
1722 : {
1723 13523 : tree l = *p;
1724 :
1725 13523 : tree attr = get_attribute_name (l);
1726 13523 : if (is_attribute_p (attr_name, attr)
1727 13523 : && is_attribute_namespace_p (attr_ns, l))
1728 : {
1729 12804 : *p = TREE_CHAIN (l);
1730 12804 : continue;
1731 : }
1732 719 : p = &TREE_CHAIN (l);
1733 : }
1734 :
1735 488948 : return list;
1736 : }
1737 :
1738 : /* Return an attribute list that is the union of a1 and a2. */
1739 :
1740 : tree
1741 206501488 : merge_attributes (tree a1, tree a2)
1742 : {
1743 206501488 : tree attributes;
1744 :
1745 : /* Either one unset? Take the set one. */
1746 :
1747 206501488 : if ((attributes = a1) == 0)
1748 : attributes = a2;
1749 :
1750 : /* One that completely contains the other? Take it. */
1751 :
1752 12345706 : else if (a2 != 0 && ! attribute_list_contained (a1, a2))
1753 : {
1754 1649538 : if (attribute_list_contained (a2, a1))
1755 : attributes = a2;
1756 : else
1757 : {
1758 : /* Pick the longest list, and hang on the other list,
1759 : unless both lists contain ATTR_UNIQUE_VALUE_P values.
1760 : In that case a1 list needs to go after the a2 list
1761 : because attributes from a single declaration are stored
1762 : in reverse order of their declarations. */
1763 : bool a1_unique_value_p = false, a2_unique_value_p = false;
1764 : tree aa1 = a1, aa2 = a2;
1765 2850689 : for (; aa1 && aa2; aa1 = TREE_CHAIN (aa1), aa2 = TREE_CHAIN (aa2))
1766 : {
1767 1597407 : if (!a1_unique_value_p
1768 1597385 : && TREE_VALUE (aa1)
1769 778119 : && TREE_CODE (TREE_VALUE (aa1)) == TREE_LIST
1770 2375526 : && ATTR_UNIQUE_VALUE_P (TREE_VALUE (aa1)))
1771 : a1_unique_value_p = true;
1772 1597407 : if (!a2_unique_value_p
1773 1597385 : && TREE_VALUE (aa2)
1774 829410 : && TREE_CODE (TREE_VALUE (aa2)) == TREE_LIST
1775 2426801 : && ATTR_UNIQUE_VALUE_P (TREE_VALUE (aa2)))
1776 : a2_unique_value_p = true;
1777 : }
1778 :
1779 1253282 : if (aa2 && (!a1_unique_value_p || !a2_unique_value_p))
1780 : {
1781 1253282 : attributes = a2;
1782 1253282 : a2 = a1;
1783 : }
1784 :
1785 1253282 : tree a3 = NULL_TREE, *pa = &a3;
1786 2850699 : for (; a2 != 0; a2 = TREE_CHAIN (a2))
1787 : {
1788 1597417 : tree a;
1789 1597417 : for (a = lookup_ident_attribute (get_attribute_name (a2),
1790 : attributes);
1791 2157361 : a != NULL_TREE && !attribute_value_equal (a, a2);
1792 559944 : a = lookup_ident_attribute (get_attribute_name (a2),
1793 559944 : TREE_CHAIN (a)))
1794 : ;
1795 1597417 : if (a == NULL_TREE)
1796 : {
1797 1253904 : a1 = copy_node (a2);
1798 1253904 : *pa = a1;
1799 1253904 : pa = &TREE_CHAIN (a1);
1800 : }
1801 : }
1802 1253282 : if (a3)
1803 : {
1804 1253282 : *pa = attributes;
1805 1253282 : attributes = a3;
1806 : }
1807 : }
1808 : }
1809 206501488 : return attributes;
1810 : }
1811 :
1812 : /* Given types T1 and T2, merge their attributes and return
1813 : the result. */
1814 :
1815 : tree
1816 148167425 : merge_type_attributes (tree t1, tree t2)
1817 : {
1818 148167425 : return merge_attributes (TYPE_ATTRIBUTES (t1),
1819 148167425 : TYPE_ATTRIBUTES (t2));
1820 : }
1821 :
1822 : /* Given decls OLDDECL and NEWDECL, merge their attributes and return
1823 : the result. */
1824 :
1825 : tree
1826 58257293 : merge_decl_attributes (tree olddecl, tree newdecl)
1827 : {
1828 58257293 : return merge_attributes (DECL_ATTRIBUTES (olddecl),
1829 58257293 : DECL_ATTRIBUTES (newdecl));
1830 : }
1831 :
1832 : /* Duplicate all attributes with name NAME in ATTR list to *ATTRS if
1833 : they are missing there. */
1834 :
1835 : void
1836 8177262 : duplicate_one_attribute (tree *attrs, tree attr, const char *name)
1837 : {
1838 8177262 : attr = lookup_attribute (name, attr);
1839 8177262 : if (!attr)
1840 : return;
1841 9074 : tree a = lookup_attribute (name, *attrs);
1842 27222 : while (attr)
1843 : {
1844 : tree a2;
1845 9074 : for (a2 = a; a2; a2 = lookup_attribute (name, TREE_CHAIN (a2)))
1846 0 : if (attribute_value_equal (attr, a2))
1847 : break;
1848 9074 : if (!a2)
1849 : {
1850 9074 : a2 = copy_node (attr);
1851 9074 : TREE_CHAIN (a2) = *attrs;
1852 9074 : *attrs = a2;
1853 : }
1854 9074 : attr = lookup_attribute (name, TREE_CHAIN (attr));
1855 : }
1856 : }
1857 :
1858 : /* Duplicate all attributes from user DECL to the corresponding
1859 : builtin that should be propagated. */
1860 :
1861 : void
1862 8177221 : copy_attributes_to_builtin (tree decl)
1863 : {
1864 8177221 : tree b = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
1865 8177221 : if (b)
1866 8177221 : duplicate_one_attribute (&DECL_ATTRIBUTES (b),
1867 8177221 : DECL_ATTRIBUTES (decl), "omp declare simd");
1868 8177221 : }
1869 :
1870 : #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
1871 :
1872 : /* Specialization of merge_decl_attributes for various Windows targets.
1873 :
1874 : This handles the following situation:
1875 :
1876 : __declspec (dllimport) int foo;
1877 : int foo;
1878 :
1879 : The second instance of `foo' nullifies the dllimport. */
1880 :
1881 : tree
1882 : merge_dllimport_decl_attributes (tree old, tree new_tree)
1883 : {
1884 : tree a;
1885 : int delete_dllimport_p = 1;
1886 :
1887 : /* What we need to do here is remove from `old' dllimport if it doesn't
1888 : appear in `new'. dllimport behaves like extern: if a declaration is
1889 : marked dllimport and a definition appears later, then the object
1890 : is not dllimport'd. We also remove a `new' dllimport if the old list
1891 : contains dllexport: dllexport always overrides dllimport, regardless
1892 : of the order of declaration. */
1893 : if (!VAR_OR_FUNCTION_DECL_P (new_tree))
1894 : delete_dllimport_p = 0;
1895 : else if (DECL_DLLIMPORT_P (new_tree)
1896 : && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
1897 : {
1898 : DECL_DLLIMPORT_P (new_tree) = 0;
1899 : warning (OPT_Wattributes, "%q+D already declared with dllexport "
1900 : "attribute: dllimport ignored", new_tree);
1901 : }
1902 : else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
1903 : {
1904 : /* Warn about overriding a symbol that has already been used, e.g.:
1905 : extern int __attribute__ ((dllimport)) foo;
1906 : int* bar () {return &foo;}
1907 : int foo;
1908 : */
1909 : if (TREE_USED (old))
1910 : {
1911 : warning (0, "%q+D redeclared without dllimport attribute "
1912 : "after being referenced with dll linkage", new_tree);
1913 : /* If we have used a variable's address with dllimport linkage,
1914 : keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
1915 : decl may already have had TREE_CONSTANT computed.
1916 : We still remove the attribute so that assembler code refers
1917 : to '&foo rather than '_imp__foo'. */
1918 : if (VAR_P (old) && TREE_ADDRESSABLE (old))
1919 : DECL_DLLIMPORT_P (new_tree) = 1;
1920 : }
1921 :
1922 : /* Let an inline definition silently override the external reference,
1923 : but otherwise warn about attribute inconsistency. */
1924 : else if (VAR_P (new_tree) || !DECL_DECLARED_INLINE_P (new_tree))
1925 : warning (OPT_Wattributes, "%q+D redeclared without dllimport "
1926 : "attribute: previous dllimport ignored", new_tree);
1927 : }
1928 : else
1929 : delete_dllimport_p = 0;
1930 :
1931 : a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
1932 :
1933 : if (delete_dllimport_p)
1934 : a = remove_attribute ("dllimport", a);
1935 :
1936 : return a;
1937 : }
1938 :
1939 : /* Handle a "dllimport" or "dllexport" attribute; arguments as in
1940 : struct attribute_spec.handler. */
1941 :
1942 : tree
1943 : handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
1944 : bool *no_add_attrs)
1945 : {
1946 : tree node = *pnode;
1947 : bool is_dllimport;
1948 :
1949 : /* These attributes may apply to structure and union types being created,
1950 : but otherwise should pass to the declaration involved. */
1951 : if (!DECL_P (node))
1952 : {
1953 : if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
1954 : | (int) ATTR_FLAG_ARRAY_NEXT))
1955 : {
1956 : *no_add_attrs = true;
1957 : return tree_cons (name, args, NULL_TREE);
1958 : }
1959 : if (TREE_CODE (node) == RECORD_TYPE
1960 : || TREE_CODE (node) == UNION_TYPE)
1961 : {
1962 : node = TYPE_NAME (node);
1963 : if (!node)
1964 : return NULL_TREE;
1965 : }
1966 : else
1967 : {
1968 : warning (OPT_Wattributes, "%qE attribute ignored",
1969 : name);
1970 : *no_add_attrs = true;
1971 : return NULL_TREE;
1972 : }
1973 : }
1974 :
1975 : if (!VAR_OR_FUNCTION_DECL_P (node) && TREE_CODE (node) != TYPE_DECL)
1976 : {
1977 : *no_add_attrs = true;
1978 : warning (OPT_Wattributes, "%qE attribute ignored",
1979 : name);
1980 : return NULL_TREE;
1981 : }
1982 :
1983 : if (TREE_CODE (node) == TYPE_DECL
1984 : && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
1985 : && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
1986 : {
1987 : *no_add_attrs = true;
1988 : warning (OPT_Wattributes, "%qE attribute ignored",
1989 : name);
1990 : return NULL_TREE;
1991 : }
1992 :
1993 : is_dllimport = is_attribute_p ("dllimport", name);
1994 :
1995 : /* Report error on dllimport ambiguities seen now before they cause
1996 : any damage. */
1997 : if (is_dllimport)
1998 : {
1999 : /* Honor any target-specific overrides. */
2000 : if (!targetm.valid_dllimport_attribute_p (node))
2001 : *no_add_attrs = true;
2002 :
2003 : else if (TREE_CODE (node) == FUNCTION_DECL
2004 : && DECL_DECLARED_INLINE_P (node))
2005 : {
2006 : warning (OPT_Wattributes, "inline function %q+D declared as "
2007 : "dllimport: attribute ignored", node);
2008 : *no_add_attrs = true;
2009 : }
2010 : /* Like MS, treat definition of dllimported variables and
2011 : non-inlined functions on declaration as syntax errors. */
2012 : else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
2013 : {
2014 : error ("function %q+D definition is marked dllimport", node);
2015 : *no_add_attrs = true;
2016 : }
2017 :
2018 : else if (VAR_P (node))
2019 : {
2020 : if (DECL_INITIAL (node))
2021 : {
2022 : error ("variable %q+D definition is marked dllimport", node);
2023 : *no_add_attrs = true;
2024 : }
2025 : #if TARGET_WIN32_TLS
2026 : else if (DECL_THREAD_LOCAL_P (node))
2027 : {
2028 : error ("thread-local variable %q+D declared as dllimport", node);
2029 : *no_add_attrs = true;
2030 : }
2031 : #endif
2032 :
2033 : /* `extern' needn't be specified with dllimport.
2034 : Specify `extern' now and hope for the best. Sigh. */
2035 : DECL_EXTERNAL (node) = 1;
2036 : /* Also, implicitly give dllimport'd variables declared within
2037 : a function global scope, unless declared static. */
2038 : if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
2039 : TREE_PUBLIC (node) = 1;
2040 : /* Clear TREE_STATIC because DECL_EXTERNAL is set, unless
2041 : it is a C++ static data member. */
2042 : if (DECL_CONTEXT (node) == NULL_TREE
2043 : || !RECORD_OR_UNION_TYPE_P (DECL_CONTEXT (node)))
2044 : TREE_STATIC (node) = 0;
2045 : }
2046 :
2047 : if (*no_add_attrs == false)
2048 : DECL_DLLIMPORT_P (node) = 1;
2049 : }
2050 : else if (TREE_CODE (node) == FUNCTION_DECL
2051 : && DECL_DECLARED_INLINE_P (node)
2052 : && flag_keep_inline_dllexport)
2053 : /* An exported function, even if inline, must be emitted. */
2054 : DECL_EXTERNAL (node) = 0;
2055 : #if TARGET_WIN32_TLS
2056 : else if (VAR_P (node) && DECL_THREAD_LOCAL_P (node))
2057 : {
2058 : error ("thread-local variable %q+D declared as dllexport", node);
2059 : *no_add_attrs = true;
2060 : }
2061 : #endif
2062 :
2063 : /* Report error if symbol is not accessible at global scope. */
2064 : if (!TREE_PUBLIC (node) && VAR_OR_FUNCTION_DECL_P (node))
2065 : {
2066 : error ("external linkage required for symbol %q+D because of "
2067 : "%qE attribute", node, name);
2068 : *no_add_attrs = true;
2069 : }
2070 :
2071 : /* A dllexport'd entity must have default visibility so that other
2072 : program units (shared libraries or the main executable) can see
2073 : it. A dllimport'd entity must have default visibility so that
2074 : the linker knows that undefined references within this program
2075 : unit can be resolved by the dynamic linker. */
2076 : if (!*no_add_attrs)
2077 : {
2078 : if (DECL_VISIBILITY_SPECIFIED (node)
2079 : && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
2080 : error ("%qE implies default visibility, but %qD has already "
2081 : "been declared with a different visibility",
2082 : name, node);
2083 : DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
2084 : DECL_VISIBILITY_SPECIFIED (node) = 1;
2085 : }
2086 :
2087 : return NULL_TREE;
2088 : }
2089 :
2090 : #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
2091 :
2092 : /* Given two lists of attributes, return true if list l2 is
2093 : equivalent to l1. */
2094 :
2095 : int
2096 5768203175 : attribute_list_equal (const_tree l1, const_tree l2)
2097 : {
2098 5768203175 : if (l1 == l2)
2099 : return 1;
2100 :
2101 454062030 : return attribute_list_contained (l1, l2)
2102 718374430 : && attribute_list_contained (l2, l1);
2103 : }
2104 :
2105 : /* Given two lists of attributes, return true if list L2 is
2106 : completely contained within L1. */
2107 : /* ??? This would be faster if attribute names were stored in a canonicalized
2108 : form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
2109 : must be used to show these elements are equivalent (which they are). */
2110 : /* ??? It's not clear that attributes with arguments will always be handled
2111 : correctly. */
2112 :
2113 : int
2114 652400761 : attribute_list_contained (const_tree l1, const_tree l2)
2115 : {
2116 652400761 : const_tree t1, t2;
2117 :
2118 : /* First check the obvious, maybe the lists are identical. */
2119 652400761 : if (l1 == l2)
2120 : return 1;
2121 :
2122 : /* Maybe the lists are similar. */
2123 : for (t1 = l1, t2 = l2;
2124 1052653650 : t1 != 0 && t2 != 0
2125 450933227 : && get_attribute_name (t1) == get_attribute_name (t2)
2126 1477644310 : && TREE_VALUE (t1) == TREE_VALUE (t2);
2127 400283994 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
2128 : ;
2129 :
2130 : /* Maybe the lists are equal. */
2131 652369656 : if (t1 == 0 && t2 == 0)
2132 : return 1;
2133 :
2134 303050094 : for (; t2 != 0; t2 = TREE_CHAIN (t2))
2135 : {
2136 287630050 : const_tree attr;
2137 : /* This CONST_CAST is okay because lookup_attribute does not
2138 : modify its argument and the return value is assigned to a
2139 : const_tree. */
2140 287630050 : for (attr = lookup_ident_attribute (get_attribute_name (t2),
2141 : const_cast<tree> (l1));
2142 302686396 : attr != NULL_TREE && !attribute_value_equal (t2, attr);
2143 15056346 : attr = lookup_ident_attribute (get_attribute_name (t2),
2144 15056346 : TREE_CHAIN (attr)))
2145 : ;
2146 :
2147 287630050 : if (attr == NULL_TREE)
2148 : return 0;
2149 : }
2150 :
2151 : return 1;
2152 : }
2153 :
2154 : /* The backbone of lookup_attribute(). ATTR_LEN is the string length
2155 : of ATTR_NAME, and LIST is not NULL_TREE.
2156 :
2157 : The function is called from lookup_attribute in order to optimize
2158 : for size. */
2159 :
2160 : tree
2161 13128389842 : private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
2162 : {
2163 35591485846 : while (list)
2164 : {
2165 24114590403 : tree attr = get_attribute_name (list);
2166 24114590403 : size_t ident_len = IDENTIFIER_LENGTH (attr);
2167 24114590403 : if (cmp_attribs (attr_name, attr_len, IDENTIFIER_POINTER (attr),
2168 : ident_len))
2169 : break;
2170 22463096004 : list = TREE_CHAIN (list);
2171 : }
2172 :
2173 13128389842 : return list;
2174 : }
2175 :
2176 : /* Similarly but with also attribute namespace. */
2177 :
2178 : tree
2179 922132062 : private_lookup_attribute (const char *attr_ns, const char *attr_name,
2180 : size_t attr_ns_len, size_t attr_len, tree list)
2181 : {
2182 2295953422 : while (list)
2183 : {
2184 1387294044 : tree attr = get_attribute_name (list);
2185 1387294044 : size_t ident_len = IDENTIFIER_LENGTH (attr);
2186 1387294044 : if (cmp_attribs (attr_name, attr_len, IDENTIFIER_POINTER (attr),
2187 : ident_len))
2188 : {
2189 13472778 : tree ns = get_attribute_namespace (list);
2190 13472778 : if (ns == NULL_TREE)
2191 : {
2192 14126 : if (attr_ns_len == 0)
2193 : break;
2194 : }
2195 13458652 : else if (attr_ns)
2196 : {
2197 13458644 : ident_len = IDENTIFIER_LENGTH (ns);
2198 13458644 : if (attr_ns_len == 0)
2199 : {
2200 2296049602 : if (cmp_attribs ("gnu", strlen ("gnu"),
2201 96180 : IDENTIFIER_POINTER (ns), ident_len))
2202 : break;
2203 : }
2204 2309315886 : else if (cmp_attribs (attr_ns, attr_ns_len,
2205 13362464 : IDENTIFIER_POINTER (ns), ident_len))
2206 : break;
2207 : }
2208 : }
2209 1373821360 : list = TREE_CHAIN (list);
2210 : }
2211 :
2212 922132062 : return list;
2213 : }
2214 :
2215 : /* Return true if the function decl or type NODE has been declared
2216 : with attribute ANAME among attributes ATTRS. */
2217 :
2218 : static bool
2219 1312485 : has_attribute (tree node, tree attrs, const char *aname)
2220 : {
2221 1312485 : if (!strcmp (aname, "const"))
2222 : {
2223 8866 : if (DECL_P (node) && TREE_READONLY (node))
2224 : return true;
2225 : }
2226 1303619 : else if (!strcmp (aname, "malloc"))
2227 : {
2228 269896 : if (DECL_P (node) && DECL_IS_MALLOC (node))
2229 : return true;
2230 : }
2231 1123716 : else if (!strcmp (aname, "noreturn"))
2232 : {
2233 8866 : if (DECL_P (node) && TREE_THIS_VOLATILE (node))
2234 : return true;
2235 : }
2236 1114850 : else if (!strcmp (aname, "nothrow"))
2237 : {
2238 8866 : if (TREE_NOTHROW (node))
2239 : return true;
2240 : }
2241 1105984 : else if (!strcmp (aname, "pure"))
2242 : {
2243 13300 : if (DECL_P (node) && DECL_PURE_P (node))
2244 : return true;
2245 : }
2246 :
2247 1311590 : return lookup_attribute (aname, attrs);
2248 : }
2249 :
2250 : /* Return the number of mismatched function or type attributes between
2251 : the "template" function declaration TMPL and DECL. The word "template"
2252 : doesn't necessarily refer to a C++ template but rather a declaration
2253 : whose attributes should be matched by those on DECL. For a non-zero
2254 : return value append the names of the mismatcheed attributes to OUTATTRS.
2255 : ATTRLIST is a list of additional attributes that SPEC should be
2256 : taken to ultimately be declared with. */
2257 :
2258 : unsigned
2259 1626658 : decls_mismatched_attributes (tree tmpl, tree decl, tree attrlist,
2260 : const char* const blacklist[],
2261 : auto_vec<const char *> &outattrs)
2262 : {
2263 1626658 : if (TREE_CODE (tmpl) != FUNCTION_DECL)
2264 : return 0;
2265 :
2266 : /* Avoid warning if either declaration or its type is deprecated. */
2267 673124 : if (TREE_DEPRECATED (tmpl)
2268 673124 : || TREE_DEPRECATED (decl))
2269 : return 0;
2270 :
2271 673124 : const tree tmpls[] = { tmpl, TREE_TYPE (tmpl) };
2272 673124 : const tree decls[] = { decl, TREE_TYPE (decl) };
2273 :
2274 673124 : if (TREE_DEPRECATED (tmpls[1])
2275 673124 : || TREE_DEPRECATED (decls[1])
2276 673124 : || TREE_DEPRECATED (TREE_TYPE (tmpls[1]))
2277 1346248 : || TREE_DEPRECATED (TREE_TYPE (decls[1])))
2278 : return 0;
2279 :
2280 673115 : tree tmpl_attrs[] = { DECL_ATTRIBUTES (tmpl), TYPE_ATTRIBUTES (tmpls[1]) };
2281 673115 : tree decl_attrs[] = { DECL_ATTRIBUTES (decl), TYPE_ATTRIBUTES (decls[1]) };
2282 :
2283 673115 : if (!decl_attrs[0])
2284 668097 : decl_attrs[0] = attrlist;
2285 5018 : else if (!decl_attrs[1])
2286 4983 : decl_attrs[1] = attrlist;
2287 :
2288 : /* Avoid warning if the template has no attributes. */
2289 673115 : if (!tmpl_attrs[0] && !tmpl_attrs[1])
2290 : return 0;
2291 :
2292 : /* Avoid warning if either declaration contains an attribute on
2293 : the white list below. */
2294 89971 : const char* const whitelist[] = {
2295 : "error", "warning"
2296 : };
2297 :
2298 269871 : for (unsigned i = 0; i != 2; ++i)
2299 539733 : for (unsigned j = 0; j != ARRAY_SIZE (whitelist); ++j)
2300 359833 : if (lookup_attribute (whitelist[j], tmpl_attrs[i])
2301 359833 : || lookup_attribute (whitelist[j], decl_attrs[i]))
2302 : return 0;
2303 :
2304 : /* Put together a list of the black-listed attributes that the template
2305 : is declared with and the declaration is not, in case it's not apparent
2306 : from the most recent declaration of the template. */
2307 : unsigned nattrs = 0;
2308 :
2309 746198 : for (unsigned i = 0; blacklist[i]; ++i)
2310 : {
2311 : /* Attribute leaf only applies to extern functions. Avoid mentioning
2312 : it when it's missing from a static declaration. */
2313 656248 : if (!TREE_PUBLIC (decl)
2314 667 : && !strcmp ("leaf", blacklist[i]))
2315 47 : continue;
2316 :
2317 1967560 : for (unsigned j = 0; j != 2; ++j)
2318 : {
2319 1311909 : if (!has_attribute (tmpls[j], tmpl_attrs[j], blacklist[i]))
2320 1311359 : continue;
2321 :
2322 550 : bool found = false;
2323 550 : unsigned kmax = 1 + !!decl_attrs[1];
2324 633 : for (unsigned k = 0; k != kmax; ++k)
2325 : {
2326 576 : if (has_attribute (decls[k], decl_attrs[k], blacklist[i]))
2327 : {
2328 : found = true;
2329 : break;
2330 : }
2331 : }
2332 :
2333 550 : if (!found)
2334 : {
2335 57 : outattrs.safe_push (blacklist[i]);
2336 57 : ++nattrs;
2337 : }
2338 :
2339 : break;
2340 : }
2341 : }
2342 :
2343 : return nattrs;
2344 : }
2345 :
2346 : /* Issue a warning for the declaration ALIAS for TARGET where ALIAS
2347 : specifies either attributes that are incompatible with those of
2348 : TARGET, or attributes that are missing and that declaring ALIAS
2349 : with would benefit. */
2350 :
2351 : void
2352 5143 : maybe_diag_alias_attributes (tree alias, tree target)
2353 : {
2354 : /* Do not expect attributes to match between aliases and ifunc
2355 : resolvers. There is no obvious correspondence between them. */
2356 5143 : if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (alias)))
2357 121 : return;
2358 :
2359 5026 : const char* const blacklist[] = {
2360 : "alloc_align", "alloc_size", "cold", "const", "hot", "leaf", "malloc",
2361 : "nonnull", "noreturn", "nothrow", "pure", "returns_nonnull",
2362 : "returns_twice", NULL
2363 : };
2364 :
2365 5026 : if (warn_attribute_alias > 1)
2366 : {
2367 : /* With -Wattribute-alias=2 detect alias declarations that are more
2368 : restrictive than their targets first. Those indicate potential
2369 : codegen bugs. */
2370 4 : auto_vec<const char *> mismatches;
2371 4 : if (unsigned n = decls_mismatched_attributes (alias, target, NULL_TREE,
2372 : blacklist, mismatches))
2373 : {
2374 4 : auto_diagnostic_group d;
2375 4 : pp_markup::comma_separated_quoted_strings e (mismatches);
2376 4 : if (warning_n (DECL_SOURCE_LOCATION (alias),
2377 4 : OPT_Wattribute_alias_, n,
2378 : "%qD specifies more restrictive attribute than "
2379 : "its target %qD: %e",
2380 : "%qD specifies more restrictive attributes than "
2381 : "its target %qD: %e",
2382 : alias, target, &e))
2383 3 : inform (DECL_SOURCE_LOCATION (target),
2384 : "%qD target declared here", alias);
2385 4 : return;
2386 4 : }
2387 4 : }
2388 :
2389 : /* Detect alias declarations that are less restrictive than their
2390 : targets. Those suggest potential optimization opportunities
2391 : (solved by adding the missing attribute(s) to the alias). */
2392 5022 : auto_vec<const char *> mismatches;
2393 5022 : if (unsigned n = decls_mismatched_attributes (target, alias, NULL_TREE,
2394 : blacklist, mismatches))
2395 : {
2396 19 : auto_diagnostic_group d;
2397 19 : pp_markup::comma_separated_quoted_strings e (mismatches);
2398 19 : if (warning_n (DECL_SOURCE_LOCATION (alias),
2399 19 : OPT_Wmissing_attributes, n,
2400 : "%qD specifies less restrictive attribute than "
2401 : "its target %qD: %e",
2402 : "%qD specifies less restrictive attributes than "
2403 : "its target %qD: %e",
2404 : alias, target, &e))
2405 10 : inform (DECL_SOURCE_LOCATION (target),
2406 : "%qD target declared here", alias);
2407 19 : }
2408 5022 : }
2409 :
2410 : /* Initialize a mapping RWM for a call to a function declared with
2411 : attribute access in ATTRS. Each attribute positional operand
2412 : inserts one entry into the mapping with the operand number as
2413 : the key. */
2414 :
2415 : void
2416 8337480 : init_attr_rdwr_indices (rdwr_map *rwm, tree attrs)
2417 : {
2418 8337480 : if (!attrs)
2419 : return;
2420 :
2421 1351351 : for (tree access = attrs;
2422 7040065 : (access = lookup_attribute ("access", access));
2423 1351351 : access = TREE_CHAIN (access))
2424 : {
2425 : /* The TREE_VALUE of an attribute is a TREE_LIST whose TREE_VALUE
2426 : is the attribute argument's value. */
2427 1351351 : tree mode = TREE_VALUE (access);
2428 1351351 : if (!mode)
2429 : return;
2430 :
2431 : /* The (optional) list of VLA bounds. */
2432 1351351 : tree vblist = TREE_CHAIN (mode);
2433 1351351 : mode = TREE_VALUE (mode);
2434 1351351 : if (TREE_CODE (mode) != STRING_CST)
2435 0 : continue;
2436 1351351 : gcc_assert (TREE_CODE (mode) == STRING_CST);
2437 :
2438 1351351 : if (vblist)
2439 515660 : vblist = nreverse (copy_list (TREE_VALUE (vblist)));
2440 :
2441 2754751 : for (const char *m = TREE_STRING_POINTER (mode); *m; )
2442 : {
2443 1403400 : attr_access acc = { };
2444 :
2445 : /* Skip the internal-only plus sign. */
2446 1403400 : if (*m == '+')
2447 39133 : ++m;
2448 :
2449 1403400 : acc.str = m;
2450 1403400 : acc.mode = acc.from_mode_char (*m);
2451 1403400 : acc.sizarg = UINT_MAX;
2452 :
2453 1403400 : const char *end;
2454 1403400 : acc.ptrarg = strtoul (++m, const_cast<char**>(&end), 10);
2455 1403400 : m = end;
2456 :
2457 1403400 : if (*m == '[')
2458 : {
2459 : /* Forms containing the square bracket are internal-only
2460 : (not specified by an attribute declaration), and used
2461 : for various forms of array and VLA parameters. */
2462 567709 : acc.internal_p = true;
2463 :
2464 : /* Search to the closing bracket and look at the preceding
2465 : code: it determines the form of the most significant
2466 : bound of the array. Others prior to it encode the form
2467 : of interior VLA bounds. They're not of interest here. */
2468 567709 : end = strchr (m, ']');
2469 567709 : const char *p = end;
2470 567709 : gcc_assert (p);
2471 :
2472 1223558 : while (ISDIGIT (p[-1]))
2473 655849 : --p;
2474 :
2475 567709 : if (ISDIGIT (*p))
2476 : {
2477 : /* A digit denotes a constant bound (as in T[3]). */
2478 488043 : acc.static_p = p[-1] == 's';
2479 488043 : acc.minsize = strtoull (p, NULL, 10);
2480 : }
2481 79666 : else if (' ' == p[-1])
2482 : {
2483 : /* A space denotes an ordinary array of unspecified bound
2484 : (as in T[]). */
2485 : acc.minsize = 0;
2486 : }
2487 2725 : else if ('*' == p[-1] || '$' == p[-1])
2488 : {
2489 : /* An asterisk denotes a VLA. When the closing bracket
2490 : is followed by a comma and a dollar sign its bound is
2491 : on the list. Otherwise it's a VLA with an unspecified
2492 : bound. */
2493 2725 : acc.static_p = p[-2] == 's';
2494 2725 : acc.minsize = HOST_WIDE_INT_M1U;
2495 : }
2496 :
2497 567709 : m = end + 1;
2498 : }
2499 :
2500 1403400 : if (*m == ',')
2501 : {
2502 689885 : ++m;
2503 710925 : do
2504 : {
2505 710925 : if (*m == '$')
2506 : {
2507 24176 : ++m;
2508 24176 : if (!acc.size && vblist)
2509 : {
2510 : /* Extract the list of VLA bounds for the current
2511 : parameter, store it in ACC.SIZE, and advance
2512 : to the list of bounds for the next VLA parameter.
2513 : */
2514 3136 : acc.size = TREE_VALUE (vblist);
2515 3136 : vblist = TREE_CHAIN (vblist);
2516 : }
2517 : }
2518 :
2519 710925 : if (ISDIGIT (*m))
2520 : {
2521 : /* Extract the positional argument. It's absent
2522 : for VLAs whose bound doesn't name a function
2523 : parameter. */
2524 687766 : unsigned pos = strtoul (m, const_cast<char**>(&end), 10);
2525 687766 : if (acc.sizarg == UINT_MAX)
2526 687653 : acc.sizarg = pos;
2527 687766 : m = end;
2528 : }
2529 : }
2530 710925 : while (*m == '$');
2531 : }
2532 :
2533 1403400 : acc.end = m;
2534 :
2535 1403400 : bool existing;
2536 1403400 : auto &ref = rwm->get_or_insert (acc.ptrarg, &existing);
2537 1403400 : if (existing)
2538 : {
2539 : /* Merge the new spec with the existing. */
2540 222 : if (acc.minsize == HOST_WIDE_INT_M1U)
2541 12 : ref.minsize = HOST_WIDE_INT_M1U;
2542 :
2543 222 : if (acc.sizarg != UINT_MAX)
2544 60 : ref.sizarg = acc.sizarg;
2545 :
2546 222 : if (acc.mode)
2547 186 : ref.mode = acc.mode;
2548 : }
2549 : else
2550 1403178 : ref = acc;
2551 :
2552 : /* Unconditionally add an entry for the required pointer
2553 : operand of the attribute, and one for the optional size
2554 : operand when it's specified. */
2555 1403400 : if (acc.sizarg != UINT_MAX)
2556 687653 : rwm->put (acc.sizarg, acc);
2557 : }
2558 : }
2559 : }
2560 :
2561 : /* Return the access specification for a function parameter PARM
2562 : or null if the current function has no such specification. */
2563 :
2564 : attr_access *
2565 796843 : get_parm_access (rdwr_map &rdwr_idx, tree parm,
2566 : tree fndecl /* = current_function_decl */)
2567 : {
2568 796843 : tree fntype = TREE_TYPE (fndecl);
2569 796843 : init_attr_rdwr_indices (&rdwr_idx, TYPE_ATTRIBUTES (fntype));
2570 :
2571 796843 : if (rdwr_idx.is_empty ())
2572 : return NULL;
2573 :
2574 3563 : unsigned argpos = 0;
2575 3563 : tree fnargs = DECL_ARGUMENTS (fndecl);
2576 8371 : for (tree arg = fnargs; arg; arg = TREE_CHAIN (arg), ++argpos)
2577 8357 : if (arg == parm)
2578 3549 : return rdwr_idx.get (argpos);
2579 :
2580 : return NULL;
2581 : }
2582 :
2583 : /* Return the internal representation as STRING_CST. Internal positional
2584 : arguments are zero-based. */
2585 :
2586 : tree
2587 1198208 : attr_access::to_internal_string () const
2588 : {
2589 1198208 : return build_string (end - str, str);
2590 : }
2591 :
2592 : /* Return the human-readable representation of the external attribute
2593 : specification (as it might appear in the source code) as STRING_CST.
2594 : External positional arguments are one-based. */
2595 :
2596 : tree
2597 2489 : attr_access::to_external_string () const
2598 : {
2599 2489 : char buf[80];
2600 2489 : gcc_assert (mode != access_deferred);
2601 2489 : int len = snprintf (buf, sizeof buf, "access (%s, %u",
2602 2489 : mode_names[mode], ptrarg + 1);
2603 2489 : if (sizarg != UINT_MAX)
2604 2179 : len += snprintf (buf + len, sizeof buf - len, ", %u", sizarg + 1);
2605 2489 : strcpy (buf + len, ")");
2606 2489 : return build_string (len + 2, buf);
2607 : }
2608 :
2609 : /* Return the number of specified VLA bounds and set *nunspec to
2610 : the number of unspecified ones (those designated by [*]). */
2611 :
2612 : unsigned
2613 8446 : attr_access::vla_bounds (unsigned *nunspec) const
2614 : {
2615 8446 : unsigned nbounds = 0;
2616 8446 : *nunspec = 0;
2617 : /* STR points to the beginning of the specified string for the current
2618 : argument that may be followed by the string for the next argument. */
2619 54390 : for (const char* p = strchr (str, ']'); p && *p != '['; --p)
2620 : {
2621 45944 : if (*p == '*')
2622 91 : ++*nunspec;
2623 45853 : else if (*p == '$')
2624 9054 : ++nbounds;
2625 : }
2626 8446 : return nbounds;
2627 : }
2628 :
2629 : /* Reset front end-specific attribute access data from ATTRS.
2630 : Called from the free_lang_data pass. */
2631 :
2632 : /* static */ void
2633 334626 : attr_access::free_lang_data (tree attrs)
2634 : {
2635 384460 : for (tree acs = attrs; (acs = lookup_attribute ("access", acs));
2636 49834 : acs = TREE_CHAIN (acs))
2637 : {
2638 49834 : tree vblist = TREE_VALUE (acs);
2639 49834 : vblist = TREE_CHAIN (vblist);
2640 49834 : if (!vblist)
2641 26 : continue;
2642 :
2643 50083 : for (vblist = TREE_VALUE (vblist); vblist; vblist = TREE_CHAIN (vblist))
2644 : {
2645 275 : tree *pvbnd = &TREE_VALUE (vblist);
2646 275 : if (!*pvbnd || DECL_P (*pvbnd))
2647 0 : continue;
2648 :
2649 : /* VLA bounds that are expressions as opposed to DECLs are
2650 : only used in the front end. Reset them to keep front end
2651 : trees leaking into the middle end (see pr97172) and to
2652 : free up memory. */
2653 275 : *pvbnd = NULL_TREE;
2654 : }
2655 : }
2656 :
2657 415505 : for (tree argspec = attrs; (argspec = lookup_attribute ("arg spec", argspec));
2658 80879 : argspec = TREE_CHAIN (argspec))
2659 : {
2660 : /* Same as above. */
2661 80879 : tree *pvblist = &TREE_VALUE (argspec);
2662 80879 : *pvblist = NULL_TREE;
2663 : }
2664 334626 : }
2665 :
2666 : /* Defined in attr_access. */
2667 : constexpr char attr_access::mode_chars[];
2668 : constexpr char attr_access::mode_names[][11];
2669 :
2670 : /* Format an array, including a VLA, pointed to by TYPE and used as
2671 : a function parameter as a human-readable string. ACC describes
2672 : an access to the parameter and is used to determine the outermost
2673 : form of the array including its bound which is otherwise obviated
2674 : by its decay to pointer. Return the formatted string. */
2675 :
2676 : std::string
2677 8972 : attr_access::array_as_string (tree type) const
2678 : {
2679 8972 : std::string typstr;
2680 :
2681 8972 : if (type == error_mark_node)
2682 0 : return std::string ();
2683 :
2684 8972 : if (this->str)
2685 : {
2686 : /* For array parameters (but not pointers) create a temporary array
2687 : type that corresponds to the form of the parameter including its
2688 : qualifiers even though they apply to the pointer, not the array
2689 : type. */
2690 8588 : const bool vla_p = minsize == HOST_WIDE_INT_M1U;
2691 8588 : tree eltype = TREE_TYPE (type);
2692 8588 : tree index_type = NULL_TREE;
2693 :
2694 8588 : if (minsize == HOST_WIDE_INT_M1U)
2695 : {
2696 : /* Determine if this is a VLA (an array whose most significant
2697 : bound is nonconstant and whose access string has "$]" in it)
2698 : extract the bound expression from SIZE. */
2699 741 : const char *p = end;
2700 9610 : for ( ; p != str && *p-- != ']'; );
2701 741 : if (*p == '$')
2702 : /* SIZE may have been cleared. Use it with care. */
2703 670 : index_type = build_index_type (size ? TREE_VALUE (size) : size);
2704 : }
2705 7847 : else if (minsize)
2706 7228 : index_type = build_index_type (size_int (minsize - 1));
2707 :
2708 8588 : tree arat = NULL_TREE;
2709 8588 : if (static_p || vla_p)
2710 : {
2711 780 : tree flag = static_p ? integer_one_node : NULL_TREE;
2712 : /* Hack: there's no language-independent way to encode
2713 : the "static" specifier or the "*" notation in an array type.
2714 : Add a "fake" attribute to have the pretty-printer add "static"
2715 : or "*". The "[static N]" notation is only valid in the most
2716 : significant bound but [*] can be used for any bound. Because
2717 : [*] is represented the same as [0] this hack only works for
2718 : the most significant bound like static and the others are
2719 : rendered as [0]. */
2720 780 : arat = build_tree_list (get_identifier ("array "), flag);
2721 : }
2722 :
2723 8588 : const int quals = TYPE_QUALS (type);
2724 8588 : type = build_array_type (eltype, index_type);
2725 8588 : type = build_type_attribute_qual_variant (type, arat, quals);
2726 : }
2727 :
2728 : /* Format the type using the current pretty printer. The generic tree
2729 : printer does a terrible job. */
2730 8972 : std::unique_ptr<pretty_printer> pp (global_dc->clone_printer ());
2731 8972 : pp_printf (pp.get (), "%qT", type);
2732 8972 : typstr = pp_formatted_text (pp.get ());
2733 :
2734 8972 : return typstr;
2735 8972 : }
2736 :
2737 : #if CHECKING_P
2738 :
2739 : namespace selftest
2740 : {
2741 :
2742 : /* Self-test to verify that each attribute exclusion is symmetric,
2743 : meaning that if attribute A is encoded as incompatible with
2744 : attribute B then the opposite relationship is also encoded.
2745 : This test also detects most cases of misspelled attribute names
2746 : in exclusions. */
2747 :
2748 : static void
2749 4 : test_attribute_exclusions ()
2750 : {
2751 4 : using excl_hash_traits = pair_hash<nofree_string_hash, nofree_string_hash>;
2752 :
2753 : /* Iterate over the array of attribute tables first (with TI0 as
2754 : the index) and over the array of attribute_spec in each table
2755 : (with SI0 as the index). */
2756 4 : hash_set<excl_hash_traits> excl_set;
2757 :
2758 16 : for (auto scoped_array : attribute_tables)
2759 29 : for (auto scoped_attributes : scoped_array)
2760 608 : for (const attribute_spec &attribute : scoped_attributes->attributes)
2761 : {
2762 587 : const attribute_spec::exclusions *excl = attribute.exclude;
2763 :
2764 : /* Skip each attribute that doesn't define exclusions. */
2765 587 : if (!excl)
2766 496 : continue;
2767 :
2768 : /* Skip standard (non-GNU) attributes, since currently the
2769 : exclusions are implicitly for GNU attributes only.
2770 : Also, C++ likely and unlikely get rewritten to gnu::hot
2771 : and gnu::cold, so symmetry isn't necessary there. */
2772 91 : if (!scoped_attributes->ns)
2773 3 : continue;
2774 :
2775 88 : const char *attr_name = attribute.name;
2776 :
2777 : /* Iterate over the set of exclusions for every attribute
2778 : (with EI0 as the index) adding the exclusions defined
2779 : for each to the set. */
2780 320 : for (size_t ei0 = 0; excl[ei0].name; ++ei0)
2781 : {
2782 232 : const char *excl_name = excl[ei0].name;
2783 :
2784 232 : if (!strcmp (attr_name, excl_name))
2785 47 : continue;
2786 :
2787 185 : excl_set.add ({ attr_name, excl_name });
2788 : }
2789 : }
2790 :
2791 : /* Traverse the set of mutually exclusive pairs of attributes
2792 : and verify that they are symmetric. */
2793 192 : for (auto excl_pair : excl_set)
2794 184 : if (!excl_set.contains ({ excl_pair.second, excl_pair.first }))
2795 : {
2796 : /* An exclusion for an attribute has been found that
2797 : doesn't have a corresponding exclusion in the opposite
2798 : direction. */
2799 0 : char desc[120];
2800 0 : sprintf (desc, "'%s' attribute exclusion '%s' must be symmetric",
2801 : excl_pair.first, excl_pair.second);
2802 0 : fail (SELFTEST_LOCATION, desc);
2803 : }
2804 4 : }
2805 :
2806 : void
2807 4 : attribs_cc_tests ()
2808 : {
2809 4 : test_attribute_exclusions ();
2810 4 : }
2811 :
2812 : } /* namespace selftest */
2813 :
2814 : #endif /* CHECKING_P */
2815 :
2816 : #include "gt-attribs.h"
|