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

source code of gcc/attribs.cc