1 /* Functions dealing with attribute handling, used by most front ends.
2 Copyright (C) 1992-2024 Free Software Foundation, Inc.
4 This file is part of GCC.
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
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
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/>. */
20 #define INCLUDE_STRING
23 #include "coretypes.h"
26 #include "stringpool.h"
27 #include "diagnostic-core.h"
29 #include "fold-const.h"
30 #include "ipa-strub.h"
31 #include "stor-layout.h"
32 #include "langhooks.h"
36 #include "diagnostic.h"
37 #include "pretty-print.h"
38 #include "pretty-print-markup.h"
39 #include "tree-pretty-print.h"
42 /* Table of the tables of attributes (common, language, format, machine)
44 static array_slice
<const scoped_attribute_specs
*const> attribute_tables
[2];
46 /* Substring representation. */
54 /* Simple hash function to avoid need to scan whole string. */
56 static inline hashval_t
57 substring_hash (const char *str
, int l
)
59 return str
[0] + str
[l
- 1] * 256 + l
* 65536;
62 /* Used for attribute_hash. */
64 struct attribute_hasher
: nofree_ptr_hash
<attribute_spec
>
66 typedef substring
*compare_type
;
67 static inline hashval_t
hash (const attribute_spec
*);
68 static inline bool equal (const attribute_spec
*, const substring
*);
72 attribute_hasher::hash (const attribute_spec
*spec
)
74 const int l
= strlen (spec
->name
);
75 return substring_hash (spec
->name
, l
);
79 attribute_hasher::equal (const attribute_spec
*spec
, const substring
*str
)
81 return (strncmp (spec
->name
, str
->str
, str
->length
) == 0
82 && !spec
->name
[str
->length
]);
85 /* Scoped attribute name representation. */
87 struct scoped_attributes
90 vec
<attribute_spec
> attributes
;
91 hash_table
<attribute_hasher
> *attribute_hash
;
92 /* True if we should not warn about unknown attributes in this NS. */
96 /* The table of scope attributes. */
97 static vec
<scoped_attributes
> attributes_table
;
99 static scoped_attributes
* find_attribute_namespace (const char*);
100 static void register_scoped_attribute (const struct attribute_spec
*,
101 scoped_attributes
*);
102 static const struct attribute_spec
*lookup_scoped_attribute_spec (const_tree
,
105 static bool attributes_initialized
= false;
107 /* Do not use directly; go through get_gnu_namespace instead. */
108 static GTY(()) tree gnu_namespace_cache
;
110 /* Return the IDENTIFIER_NODE for the gnu namespace. */
115 if (!gnu_namespace_cache
)
116 gnu_namespace_cache
= get_identifier ("gnu");
117 return gnu_namespace_cache
;
120 /* Insert SPECS into its namespace. IGNORED_P is true iff all unknown
121 attributes in this namespace should be ignored for the purposes of
122 -Wattributes. The function returns the namespace into which the
123 attributes have been registered. */
126 register_scoped_attributes (const scoped_attribute_specs
&specs
,
127 bool ignored_p
/*=false*/)
129 scoped_attributes
*result
= NULL
;
131 /* See if we already have attributes in the namespace NS. */
132 result
= find_attribute_namespace (specs
.ns
);
136 /* We don't have any namespace NS yet. Create one. */
137 scoped_attributes sa
;
139 if (attributes_table
.is_empty ())
140 attributes_table
.create (64);
142 memset (&sa
, 0, sizeof (sa
));
144 sa
.attributes
.create (64);
145 sa
.ignored_p
= ignored_p
;
146 result
= attributes_table
.safe_push (sa
);
147 result
->attribute_hash
= new hash_table
<attribute_hasher
> (200);
150 result
->ignored_p
|= ignored_p
;
152 /* Really add the attributes to their namespace now. */
153 for (const attribute_spec
&attribute
: specs
.attributes
)
155 result
->attributes
.safe_push (attribute
);
156 register_scoped_attribute (&attribute
, result
);
159 gcc_assert (result
!= NULL
);
164 /* Return the namespace which name is NS, NULL if none exist. */
166 static scoped_attributes
*
167 find_attribute_namespace (const char* ns
)
169 for (scoped_attributes
&iter
: attributes_table
)
173 && !strcmp (iter
.ns
, ns
)))
178 /* Make some sanity checks on the attribute tables. */
181 check_attribute_tables (void)
183 hash_set
<pair_hash
<nofree_string_hash
, nofree_string_hash
>> names
;
185 for (auto scoped_array
: attribute_tables
)
186 for (auto scoped_attributes
: scoped_array
)
187 for (const attribute_spec
&attribute
: scoped_attributes
->attributes
)
189 /* The name must not begin and end with __. */
190 const char *name
= attribute
.name
;
191 int len
= strlen (name
);
193 gcc_assert (!(name
[0] == '_' && name
[1] == '_'
194 && name
[len
- 1] == '_' && name
[len
- 2] == '_'));
196 /* The minimum and maximum lengths must be consistent. */
197 gcc_assert (attribute
.min_length
>= 0);
199 gcc_assert (attribute
.max_length
== -1
200 || attribute
.max_length
>= attribute
.min_length
);
202 /* An attribute cannot require both a DECL and a TYPE. */
203 gcc_assert (!attribute
.decl_required
204 || !attribute
.type_required
);
206 /* If an attribute requires a function type, in particular
207 it requires a type. */
208 gcc_assert (!attribute
.function_type_required
209 || attribute
.type_required
);
211 /* Check that no name occurs more than once. Names that
212 begin with '*' are exempt, and may be overridden. */
213 const char *ns
= scoped_attributes
->ns
;
214 if (name
[0] != '*' && names
.add ({ ns
? ns
: "", name
}))
219 /* Used to stash pointers to allocated memory so that we can free them at
220 the end of parsing of all TUs. */
221 static vec
<attribute_spec
*> ignored_attributes_table
;
223 /* Parse arguments V of -Wno-attributes=.
227 This functions also registers the parsed attributes so that we don't
228 warn that we don't recognize them. */
231 handle_ignored_attributes_option (vec
<char *> *v
)
238 char *cln
= strstr (opt
, "::");
239 /* We don't accept '::attr'. */
240 if (cln
== nullptr || cln
== opt
)
242 auto_diagnostic_group d
;
243 error ("wrong argument to ignored attributes");
244 inform (input_location
, "valid format is %<ns::attr%> or %<ns::%>");
247 const char *vendor_start
= opt
;
248 ptrdiff_t vendor_len
= cln
- opt
;
249 const char *attr_start
= cln
+ 2;
250 /* This could really use rawmemchr :(. */
251 ptrdiff_t attr_len
= strchr (attr_start
, '\0') - attr_start
;
252 /* Verify that they look valid. */
253 auto valid_p
= [](const char *const s
, ptrdiff_t len
) {
256 for (int i
= 0; i
< len
; ++i
)
259 else if (s
[i
] != '_')
264 if (!valid_p (vendor_start
, vendor_len
))
266 error ("wrong argument to ignored attributes");
269 canonicalize_attr_name (vendor_start
, vendor_len
);
270 /* We perform all this hijinks so that we don't have to copy OPT. */
271 tree vendor_id
= get_identifier_with_length (vendor_start
, vendor_len
);
272 array_slice
<const attribute_spec
> attrs
;
273 /* In the "vendor::" case, we should ignore *any* attribute coming
274 from this attribute namespace. */
277 if (!valid_p (attr_start
, attr_len
))
279 error ("wrong argument to ignored attributes");
282 canonicalize_attr_name (attr_start
, attr_len
);
283 tree attr_id
= get_identifier_with_length (attr_start
, attr_len
);
284 const char *attr
= IDENTIFIER_POINTER (attr_id
);
285 /* If we've already seen this vendor::attr, ignore it. Attempting to
286 register it twice would lead to a crash. */
287 if (lookup_scoped_attribute_spec (vendor_id
, attr_id
))
289 /* Create a table with extra attributes which we will register.
290 We can't free it here, so squirrel away the pointers. */
291 attribute_spec
*table
= new attribute_spec
{
292 attr
, 0, -2, false, false, false, false, nullptr, nullptr
294 ignored_attributes_table
.safe_push (table
);
295 attrs
= { table
, 1 };
297 const scoped_attribute_specs scoped_specs
= {
298 IDENTIFIER_POINTER (vendor_id
), { attrs
}
300 register_scoped_attributes (scoped_specs
, attrs
.empty ());
304 /* Free data we might have allocated when adding extra attributes. */
309 for (auto x
: ignored_attributes_table
)
311 ignored_attributes_table
.release ();
314 /* Initialize attribute tables, and make some sanity checks if checking is
318 init_attributes (void)
320 if (attributes_initialized
)
323 attribute_tables
[0] = lang_hooks
.attribute_table
;
324 attribute_tables
[1] = targetm
.attribute_table
;
327 check_attribute_tables ();
329 for (auto scoped_array
: attribute_tables
)
330 for (auto scoped_attributes
: scoped_array
)
331 register_scoped_attributes (*scoped_attributes
);
333 vec
<char *> *ignored
= (vec
<char *> *) flag_ignored_attributes
;
334 handle_ignored_attributes_option (ignored
);
336 invoke_plugin_callbacks (PLUGIN_ATTRIBUTES
, NULL
);
337 attributes_initialized
= true;
340 /* Insert a single ATTR into the attribute table. */
343 register_attribute (const struct attribute_spec
*attr
)
345 register_scoped_attribute (attr
, find_attribute_namespace ("gnu"));
348 /* Insert a single attribute ATTR into a namespace of attributes. */
351 register_scoped_attribute (const struct attribute_spec
*attr
,
352 scoped_attributes
*name_space
)
354 struct substring str
;
355 attribute_spec
**slot
;
357 gcc_assert (attr
!= NULL
&& name_space
!= NULL
);
359 gcc_assert (name_space
->attribute_hash
);
361 str
.str
= attr
->name
;
362 str
.length
= strlen (str
.str
);
364 /* Attribute names in the table must be in the form 'text' and not
365 in the form '__text__'. */
366 gcc_checking_assert (!canonicalize_attr_name (str
.str
, str
.length
));
368 slot
= name_space
->attribute_hash
369 ->find_slot_with_hash (&str
, substring_hash (str
.str
, str
.length
),
371 gcc_assert (!*slot
|| attr
->name
[0] == '*');
372 *slot
= CONST_CAST (struct attribute_spec
*, attr
);
375 /* Return the spec for the scoped attribute with namespace NS and
378 static const struct attribute_spec
*
379 lookup_scoped_attribute_spec (const_tree ns
, const_tree name
)
381 struct substring attr
;
382 scoped_attributes
*attrs
;
384 const char *ns_str
= (ns
!= NULL_TREE
) ? IDENTIFIER_POINTER (ns
): NULL
;
386 attrs
= find_attribute_namespace (ns_str
);
391 attr
.str
= IDENTIFIER_POINTER (name
);
392 attr
.length
= IDENTIFIER_LENGTH (name
);
393 return attrs
->attribute_hash
->find_with_hash (&attr
,
394 substring_hash (attr
.str
,
398 /* Return the spec for the attribute named NAME. If NAME is a TREE_LIST,
399 it also specifies the attribute namespace. */
401 const struct attribute_spec
*
402 lookup_attribute_spec (const_tree name
)
405 if (TREE_CODE (name
) == TREE_LIST
)
407 ns
= TREE_PURPOSE (name
);
408 name
= TREE_VALUE (name
);
411 ns
= get_gnu_namespace ();
412 return lookup_scoped_attribute_spec (ns
, name
);
416 /* Return the namespace of the attribute ATTR. This accessor works on
417 GNU and C++11 (scoped) attributes. On GNU attributes,
418 it returns an identifier tree for the string "gnu".
420 Please read the comments of cxx11_attribute_p to understand the
421 format of attributes. */
424 get_attribute_namespace (const_tree attr
)
426 if (cxx11_attribute_p (attr
))
427 return TREE_PURPOSE (TREE_PURPOSE (attr
));
428 return get_gnu_namespace ();
431 /* Check LAST_DECL and NODE of the same symbol for attributes that are
432 recorded in SPEC to be mutually exclusive with ATTRNAME, diagnose
433 them, and return true if any have been found. NODE can be a DECL
437 diag_attr_exclusions (tree last_decl
, tree node
, tree attrname
,
438 const attribute_spec
*spec
)
440 const attribute_spec::exclusions
*excl
= spec
->exclude
;
442 tree_code code
= TREE_CODE (node
);
444 if ((code
== FUNCTION_DECL
&& !excl
->function
445 && (!excl
->type
|| !spec
->affects_type_identity
))
446 || (code
== VAR_DECL
&& !excl
->variable
447 && (!excl
->type
|| !spec
->affects_type_identity
))
448 || (((code
== TYPE_DECL
|| RECORD_OR_UNION_TYPE_P (node
)) && !excl
->type
)))
451 /* True if an attribute that's mutually exclusive with ATTRNAME
455 if (last_decl
&& last_decl
!= node
&& TREE_TYPE (last_decl
) != node
)
457 /* Check both the last DECL and its type for conflicts with
458 the attribute being added to the current decl or type. */
459 found
|= diag_attr_exclusions (last_decl
, last_decl
, attrname
, spec
);
460 tree decl_type
= TREE_TYPE (last_decl
);
461 found
|= diag_attr_exclusions (last_decl
, decl_type
, attrname
, spec
);
464 /* NODE is either the current DECL to which the attribute is being
465 applied or its TYPE. For the former, consider the attributes on
466 both the DECL and its type. */
471 attrs
[0] = DECL_ATTRIBUTES (node
);
472 if (TREE_TYPE (node
))
473 attrs
[1] = TYPE_ATTRIBUTES (TREE_TYPE (node
));
475 /* TREE_TYPE can be NULL e.g. while processing attributes on
477 attrs
[1] = NULL_TREE
;
481 attrs
[0] = TYPE_ATTRIBUTES (node
);
482 attrs
[1] = NULL_TREE
;
485 /* Iterate over the mutually exclusive attribute names and verify
486 that the symbol doesn't contain it. */
487 for (unsigned i
= 0; i
!= ARRAY_SIZE (attrs
); ++i
)
492 for ( ; excl
->name
; ++excl
)
494 /* Avoid checking the attribute against itself. */
495 if (is_attribute_p (excl
->name
, attrname
))
498 if (!lookup_attribute (excl
->name
, attrs
[i
]))
501 /* An exclusion may apply either to a function declaration,
502 type declaration, or a field/variable declaration, or
503 any subset of the three. */
504 if (TREE_CODE (node
) == FUNCTION_DECL
508 if (TREE_CODE (node
) == TYPE_DECL
512 if ((TREE_CODE (node
) == FIELD_DECL
520 bool note
= last_decl
!= NULL_TREE
;
521 auto_diagnostic_group d
;
522 if (TREE_CODE (node
) == FUNCTION_DECL
523 && fndecl_built_in_p (node
))
524 note
&= warning (OPT_Wattributes
,
525 "ignoring attribute %qE in declaration of "
526 "a built-in function %qD because it conflicts "
527 "with attribute %qs",
528 attrname
, node
, excl
->name
);
530 note
&= warning (OPT_Wattributes
,
531 "ignoring attribute %qE because "
532 "it conflicts with attribute %qs",
533 attrname
, excl
->name
);
536 inform (DECL_SOURCE_LOCATION (last_decl
),
537 "previous declaration here");
544 /* Return true iff we should not complain about unknown attributes
545 coming from the attribute namespace NS. This is the case for
546 the -Wno-attributes=ns:: command-line option. */
549 attr_namespace_ignored_p (tree ns
)
553 scoped_attributes
*r
= find_attribute_namespace (IDENTIFIER_POINTER (ns
));
554 return r
&& r
->ignored_p
;
557 /* Return true if the attribute ATTR should not be warned about. */
560 attribute_ignored_p (tree attr
)
562 if (!cxx11_attribute_p (attr
))
564 if (tree ns
= get_attribute_namespace (attr
))
566 const attribute_spec
*as
= lookup_attribute_spec (TREE_PURPOSE (attr
));
567 if (as
== NULL
&& attr_namespace_ignored_p (ns
))
569 if (as
&& as
->max_length
== -2)
575 /* Like above, but takes an attribute_spec AS, which must be nonnull. */
578 attribute_ignored_p (const attribute_spec
*const as
)
580 return as
->max_length
== -2;
583 /* Return true if the ATTRS chain contains at least one attribute which
587 any_nonignored_attribute_p (tree attrs
)
589 for (tree attr
= attrs
; attr
; attr
= TREE_CHAIN (attr
))
590 if (!attribute_ignored_p (attr
))
596 /* See whether LIST contains at least one instance of attribute ATTR
597 (possibly with different arguments). Return the first such attribute
598 if so, otherwise return null. */
601 find_same_attribute (const_tree attr
, tree list
)
603 if (list
== NULL_TREE
)
605 tree ns
= get_attribute_namespace (attr
);
606 tree name
= get_attribute_name (attr
);
607 return private_lookup_attribute (ns
? IDENTIFIER_POINTER (ns
) : nullptr,
608 IDENTIFIER_POINTER (name
),
609 ns
? IDENTIFIER_LENGTH (ns
) : 0,
610 IDENTIFIER_LENGTH (name
), list
);
613 /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
614 which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
615 it should be modified in place; if a TYPE, a copy should be created
616 unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
617 information, in the form of a bitwise OR of flags in enum attribute_flags
618 from tree.h. Depending on these flags, some attributes may be
619 returned to be applied at a later stage (for example, to apply
620 a decl attribute to the declaration rather than to its type). */
623 decl_attributes (tree
*node
, tree attributes
, int flags
,
624 tree last_decl
/* = NULL_TREE */)
626 tree returned_attrs
= NULL_TREE
;
628 if (TREE_TYPE (*node
) == error_mark_node
|| attributes
== error_mark_node
)
631 if (!attributes_initialized
)
634 /* If this is a function and the user used #pragma GCC optimize, add the
635 options to the attribute((optimize(...))) list. */
636 if (TREE_CODE (*node
) == FUNCTION_DECL
&& current_optimize_pragma
)
638 tree cur_attr
= lookup_attribute ("optimize", attributes
);
639 tree opts
= copy_list (current_optimize_pragma
);
643 = tree_cons (get_identifier ("optimize"), opts
, attributes
);
645 TREE_VALUE (cur_attr
) = chainon (opts
, TREE_VALUE (cur_attr
));
648 if (TREE_CODE (*node
) == FUNCTION_DECL
649 && (optimization_current_node
!= optimization_default_node
650 || target_option_current_node
!= target_option_default_node
)
651 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node
))
653 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node
) = optimization_current_node
;
654 /* Don't set DECL_FUNCTION_SPECIFIC_TARGET for targets that don't
655 support #pragma GCC target or target attribute. */
656 if (target_option_default_node
)
659 = build_target_option_node (&global_options
, &global_options_set
);
660 tree old_tree
= DECL_FUNCTION_SPECIFIC_TARGET (*node
);
662 old_tree
= target_option_default_node
;
663 /* The changes on optimization options can cause the changes in
664 target options, update it accordingly if it's changed. */
665 if (old_tree
!= cur_tree
)
666 DECL_FUNCTION_SPECIFIC_TARGET (*node
) = cur_tree
;
670 /* If this is a function and the user used #pragma GCC target, add the
671 options to the attribute((target(...))) list. */
672 if (TREE_CODE (*node
) == FUNCTION_DECL
673 && current_target_pragma
674 && targetm
.target_option
.valid_attribute_p (*node
,
675 get_identifier ("target"),
676 current_target_pragma
, 0))
678 tree cur_attr
= lookup_attribute ("target", attributes
);
679 tree opts
= copy_list (current_target_pragma
);
682 attributes
= tree_cons (get_identifier ("target"), opts
, attributes
);
684 TREE_VALUE (cur_attr
) = chainon (opts
, TREE_VALUE (cur_attr
));
687 /* A "naked" function attribute implies "noinline" and "noclone" for
688 those targets that support it. */
689 if (TREE_CODE (*node
) == FUNCTION_DECL
691 && lookup_attribute ("naked", attributes
) != NULL
692 && lookup_attribute_spec (get_identifier ("naked"))
693 && lookup_attribute ("noipa", attributes
) == NULL
)
694 attributes
= tree_cons (get_identifier ("noipa"), NULL
, attributes
);
696 /* A "noipa" function attribute implies "noinline", "noclone" and "no_icf"
697 for those targets that support it. */
698 if (TREE_CODE (*node
) == FUNCTION_DECL
700 && lookup_attribute ("noipa", attributes
) != NULL
701 && lookup_attribute_spec (get_identifier ("noipa")))
703 if (lookup_attribute ("noinline", attributes
) == NULL
)
704 attributes
= tree_cons (get_identifier ("noinline"), NULL
, attributes
);
706 if (lookup_attribute ("noclone", attributes
) == NULL
)
707 attributes
= tree_cons (get_identifier ("noclone"), NULL
, attributes
);
709 if (lookup_attribute ("no_icf", attributes
) == NULL
)
710 attributes
= tree_cons (get_identifier ("no_icf"), NULL
, attributes
);
713 targetm
.insert_attributes (*node
, &attributes
);
715 /* Note that attributes on the same declaration are not necessarily
716 in the same order as in the source. */
717 for (tree attr
= attributes
; attr
; attr
= TREE_CHAIN (attr
))
719 tree ns
= get_attribute_namespace (attr
);
720 tree name
= get_attribute_name (attr
);
721 tree args
= TREE_VALUE (attr
);
723 const struct attribute_spec
*spec
724 = lookup_scoped_attribute_spec (ns
, name
);
725 int fn_ptr_quals
= 0;
726 tree fn_ptr_tmp
= NULL_TREE
;
727 const bool cxx11_attr_p
= cxx11_attribute_p (attr
);
731 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
)
732 && !attr_namespace_ignored_p (ns
))
734 if (ns
== NULL_TREE
|| !cxx11_attr_p
)
735 warning (OPT_Wattributes
, "%qE attribute directive ignored",
737 else if ((flag_openmp
|| flag_openmp_simd
)
738 && is_attribute_p ("omp", ns
)
739 && is_attribute_p ("directive", name
)
741 || TREE_CODE (*node
) == FUNCTION_DECL
))
744 warning (OPT_Wattributes
,
745 "%<%E::%E%> scoped attribute directive ignored",
752 int nargs
= list_length (args
);
753 if (nargs
< spec
->min_length
754 || (spec
->max_length
>= 0
755 && nargs
> spec
->max_length
))
757 auto_diagnostic_group d
;
758 error ("wrong number of arguments specified for %qE attribute",
760 if (spec
->max_length
< 0)
761 inform (input_location
, "expected %i or more, found %i",
762 spec
->min_length
, nargs
);
763 else if (spec
->min_length
== spec
->max_length
)
764 inform (input_location
, "expected %i, found %i",
765 spec
->min_length
, nargs
);
767 inform (input_location
, "expected between %i and %i, found %i",
768 spec
->min_length
, spec
->max_length
, nargs
);
772 gcc_assert (is_attribute_p (spec
->name
, name
));
774 if (spec
->decl_required
&& !DECL_P (*anode
))
776 if (flags
& ((int) ATTR_FLAG_DECL_NEXT
777 | (int) ATTR_FLAG_FUNCTION_NEXT
778 | (int) ATTR_FLAG_ARRAY_NEXT
))
780 /* Pass on this attribute to be tried again. */
781 tree attr
= tree_cons (name
, args
, NULL_TREE
);
782 returned_attrs
= chainon (returned_attrs
, attr
);
787 warning (OPT_Wattributes
, "%qE attribute does not apply to types",
793 /* If we require a type, but were passed a decl, set up to make a
794 new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE
795 would have applied if we'd been passed a type, but we cannot modify
796 the decl's type in place here. */
797 if (spec
->type_required
&& DECL_P (*anode
))
799 anode
= &TREE_TYPE (*anode
);
800 flags
&= ~(int) ATTR_FLAG_TYPE_IN_PLACE
;
803 if (spec
->function_type_required
804 && !FUNC_OR_METHOD_TYPE_P (*anode
))
806 if (TREE_CODE (*anode
) == POINTER_TYPE
807 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (*anode
)))
809 /* OK, this is a bit convoluted. We can't just make a copy
810 of the pointer type and modify its TREE_TYPE, because if
811 we change the attributes of the target type the pointer
812 type needs to have a different TYPE_MAIN_VARIANT. So we
813 pull out the target type now, frob it as appropriate, and
814 rebuild the pointer type later.
816 This would all be simpler if attributes were part of the
817 declarator, grumble grumble. */
818 fn_ptr_tmp
= TREE_TYPE (*anode
);
819 fn_ptr_quals
= TYPE_QUALS (*anode
);
821 flags
&= ~(int) ATTR_FLAG_TYPE_IN_PLACE
;
823 else if (flags
& (int) ATTR_FLAG_FUNCTION_NEXT
)
825 /* Pass on this attribute to be tried again. */
826 tree attr
= tree_cons (name
, args
, NULL_TREE
);
827 returned_attrs
= chainon (returned_attrs
, attr
);
831 if (TREE_CODE (*anode
) != FUNCTION_TYPE
832 && TREE_CODE (*anode
) != METHOD_TYPE
)
834 warning (OPT_Wattributes
,
835 "%qE attribute only applies to function types",
842 && (flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
)
843 && COMPLETE_TYPE_P (*anode
))
845 warning (OPT_Wattributes
, "type attributes ignored after type is already defined");
849 bool no_add_attrs
= false;
851 /* Check for exclusions with other attributes on the current
852 declation as well as the last declaration of the same
853 symbol already processed (if one exists). Detect and
854 reject incompatible attributes. */
855 bool built_in
= flags
& ATTR_FLAG_BUILT_IN
;
857 && (flag_checking
|| !built_in
)
858 && !error_operand_p (last_decl
))
860 /* Always check attributes on user-defined functions.
861 Check them on built-ins only when -fchecking is set.
862 Ignore __builtin_unreachable -- it's both const and
867 || DECL_BUILT_IN_CLASS (*anode
) != BUILT_IN_NORMAL
868 || (DECL_FUNCTION_CODE (*anode
) != BUILT_IN_UNREACHABLE
869 && DECL_FUNCTION_CODE (*anode
) != BUILT_IN_UNREACHABLE_TRAP
870 && (DECL_FUNCTION_CODE (*anode
)
871 != BUILT_IN_UBSAN_HANDLE_BUILTIN_UNREACHABLE
)))
873 bool no_add
= diag_attr_exclusions (last_decl
, *anode
, name
, spec
);
874 if (!no_add
&& anode
!= node
)
875 no_add
= diag_attr_exclusions (last_decl
, *node
, name
, spec
);
876 no_add_attrs
|= no_add
;
881 /* Don't add attributes registered just for -Wno-attributes=foo::bar
883 || attribute_ignored_p (attr
))
886 if (spec
->handler
!= NULL
)
888 int cxx11_flag
= (cxx11_attr_p
? ATTR_FLAG_CXX11
: 0);
890 /* Pass in an array of the current declaration followed
891 by the last pushed/merged declaration if one exists.
892 For calls that modify the type attributes of a DECL
893 and for which *ANODE is *NODE's type, also pass in
894 the DECL as the third element to use in diagnostics.
895 If the handler changes CUR_AND_LAST_DECL[0] replace
896 *ANODE with its value. */
897 tree cur_and_last_decl
[3] = { *anode
, last_decl
};
898 if (anode
!= node
&& DECL_P (*node
))
899 cur_and_last_decl
[2] = *node
;
901 tree ret
= (spec
->handler
) (cur_and_last_decl
, name
, args
,
902 flags
|cxx11_flag
, &no_add_attrs
);
904 /* Fix up typedefs clobbered by attribute handlers. */
905 if (TREE_CODE (*node
) == TYPE_DECL
906 && anode
== &TREE_TYPE (*node
)
907 && DECL_ORIGINAL_TYPE (*node
)
908 && TYPE_NAME (*anode
) == *node
909 && TYPE_NAME (cur_and_last_decl
[0]) != *node
)
911 tree t
= cur_and_last_decl
[0];
912 DECL_ORIGINAL_TYPE (*node
) = t
;
913 tree tt
= build_variant_type_copy (t
);
914 cur_and_last_decl
[0] = tt
;
915 TREE_TYPE (*node
) = tt
;
916 TYPE_NAME (tt
) = *node
;
919 if (*anode
!= cur_and_last_decl
[0])
921 /* Even if !spec->function_type_required, allow the attribute
922 handler to request the attribute to be applied to the function
923 type, rather than to the function pointer type, by setting
924 cur_and_last_decl[0] to the function type. */
926 && POINTER_TYPE_P (*anode
)
927 && TREE_TYPE (*anode
) == cur_and_last_decl
[0]
928 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (*anode
)))
930 fn_ptr_tmp
= TREE_TYPE (*anode
);
931 fn_ptr_quals
= TYPE_QUALS (*anode
);
934 *anode
= cur_and_last_decl
[0];
937 if (ret
== error_mark_node
)
939 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
943 returned_attrs
= chainon (ret
, returned_attrs
);
946 /* Layout the decl in case anything changed. */
947 if (spec
->type_required
&& DECL_P (*node
)
949 || TREE_CODE (*node
) == PARM_DECL
950 || TREE_CODE (*node
) == RESULT_DECL
))
951 relayout_decl (*node
);
959 old_attrs
= DECL_ATTRIBUTES (*anode
);
961 old_attrs
= TYPE_ATTRIBUTES (*anode
);
963 for (a
= find_same_attribute (attr
, old_attrs
);
965 a
= find_same_attribute (attr
, TREE_CHAIN (a
)))
967 if (simple_cst_equal (TREE_VALUE (a
), args
) == 1)
973 /* This attribute isn't already in the list. */
975 /* Preserve the C++11 form. */
977 r
= tree_cons (build_tree_list (ns
, name
), args
, old_attrs
);
979 r
= tree_cons (name
, args
, old_attrs
);
982 DECL_ATTRIBUTES (*anode
) = r
;
983 else if (flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
)
985 TYPE_ATTRIBUTES (*anode
) = r
;
986 /* If this is the main variant, also push the attributes
987 out to the other variants. */
988 if (*anode
== TYPE_MAIN_VARIANT (*anode
))
990 for (tree variant
= *anode
; variant
;
991 variant
= TYPE_NEXT_VARIANT (variant
))
993 if (TYPE_ATTRIBUTES (variant
) == old_attrs
)
994 TYPE_ATTRIBUTES (variant
)
995 = TYPE_ATTRIBUTES (*anode
);
996 else if (!find_same_attribute
997 (attr
, TYPE_ATTRIBUTES (variant
)))
998 TYPE_ATTRIBUTES (variant
) = tree_cons
999 (name
, args
, TYPE_ATTRIBUTES (variant
));
1004 *anode
= build_type_attribute_variant (*anode
, r
);
1010 /* Rebuild the function pointer type and put it in the
1011 appropriate place. */
1012 fn_ptr_tmp
= build_pointer_type (fn_ptr_tmp
);
1014 fn_ptr_tmp
= build_qualified_type (fn_ptr_tmp
, fn_ptr_quals
);
1016 TREE_TYPE (*node
) = fn_ptr_tmp
;
1019 gcc_assert (TREE_CODE (*node
) == POINTER_TYPE
);
1025 return returned_attrs
;
1028 /* Return TRUE iff ATTR has been parsed by the front-end as a C++-11
1031 When G++ parses a C++11 attribute, it is represented as
1032 a TREE_LIST which TREE_PURPOSE is itself a TREE_LIST. TREE_PURPOSE
1033 (TREE_PURPOSE (ATTR)) is the namespace of the attribute, and the
1034 TREE_VALUE (TREE_PURPOSE (ATTR)) is its non-qualified name. Please
1035 use get_attribute_namespace and get_attribute_name to retrieve the
1036 namespace and name of the attribute, as these accessors work with
1037 GNU attributes as well. */
1040 cxx11_attribute_p (const_tree attr
)
1042 if (attr
== NULL_TREE
1043 || TREE_CODE (attr
) != TREE_LIST
)
1046 return (TREE_CODE (TREE_PURPOSE (attr
)) == TREE_LIST
);
1049 /* Return the name of the attribute ATTR. This accessor works on GNU
1050 and C++11 (scoped) attributes.
1052 Please read the comments of cxx11_attribute_p to understand the
1053 format of attributes. */
1056 get_attribute_name (const_tree attr
)
1058 if (cxx11_attribute_p (attr
))
1059 return TREE_VALUE (TREE_PURPOSE (attr
));
1060 return TREE_PURPOSE (attr
);
1063 /* Subroutine of set_method_tm_attributes. Apply TM attribute ATTR
1064 to the method FNDECL. */
1067 apply_tm_attr (tree fndecl
, tree attr
)
1069 decl_attributes (&TREE_TYPE (fndecl
), tree_cons (attr
, NULL
, NULL
), 0);
1072 /* Makes a function attribute of the form NAME(ARG_NAME) and chains
1076 make_attribute (const char *name
, const char *arg_name
, tree chain
)
1083 attr_name
= get_identifier (name
);
1084 attr_arg_name
= build_string (strlen (arg_name
), arg_name
);
1085 attr_args
= tree_cons (NULL_TREE
, attr_arg_name
, NULL_TREE
);
1086 attr
= tree_cons (attr_name
, attr_args
, chain
);
1091 /* Common functions used for target clone support. */
1093 /* Comparator function to be used in qsort routine to sort attribute
1094 specification strings to "target". */
1097 attr_strcmp (const void *v1
, const void *v2
)
1099 const char *c1
= *(char *const*)v1
;
1100 const char *c2
= *(char *const*)v2
;
1101 return strcmp (c1
, c2
);
1104 /* ARGLIST is the argument to target attribute. This function tokenizes
1105 the comma separated arguments, sorts them and returns a string which
1106 is a unique identifier for the comma separated arguments. It also
1107 replaces non-identifier characters "=,-" with "_". */
1110 sorted_attr_string (tree arglist
)
1113 size_t str_len_sum
= 0;
1115 char *attr_str
, *ret_str
;
1117 unsigned int argnum
= 1;
1120 for (arg
= arglist
; arg
; arg
= TREE_CHAIN (arg
))
1122 const char *str
= TREE_STRING_POINTER (TREE_VALUE (arg
));
1123 size_t len
= strlen (str
);
1124 str_len_sum
+= len
+ 1;
1127 for (i
= 0; i
< strlen (str
); i
++)
1132 attr_str
= XNEWVEC (char, str_len_sum
);
1134 for (arg
= arglist
; arg
; arg
= TREE_CHAIN (arg
))
1136 const char *str
= TREE_STRING_POINTER (TREE_VALUE (arg
));
1137 size_t len
= strlen (str
);
1138 memcpy (attr_str
+ str_len_sum
, str
, len
);
1139 attr_str
[str_len_sum
+ len
] = TREE_CHAIN (arg
) ? ',' : '\0';
1140 str_len_sum
+= len
+ 1;
1143 /* Replace "=,-" with "_". */
1144 for (i
= 0; i
< strlen (attr_str
); i
++)
1145 if (attr_str
[i
] == '=' || attr_str
[i
]== '-')
1151 args
= XNEWVEC (char *, argnum
);
1154 attr
= strtok (attr_str
, ",");
1155 while (attr
!= NULL
)
1159 attr
= strtok (NULL
, ",");
1162 qsort (args
, argnum
, sizeof (char *), attr_strcmp
);
1164 ret_str
= XNEWVEC (char, str_len_sum
);
1166 for (i
= 0; i
< argnum
; i
++)
1168 size_t len
= strlen (args
[i
]);
1169 memcpy (ret_str
+ str_len_sum
, args
[i
], len
);
1170 ret_str
[str_len_sum
+ len
] = i
< argnum
- 1 ? '_' : '\0';
1171 str_len_sum
+= len
+ 1;
1175 XDELETEVEC (attr_str
);
1180 /* This function returns true if FN1 and FN2 are versions of the same function,
1181 that is, the target strings of the function decls are different. This assumes
1182 that FN1 and FN2 have the same signature. */
1185 common_function_versions (tree fn1
, tree fn2
)
1188 char *target1
, *target2
;
1191 if (TREE_CODE (fn1
) != FUNCTION_DECL
1192 || TREE_CODE (fn2
) != FUNCTION_DECL
)
1195 attr1
= lookup_attribute ("target", DECL_ATTRIBUTES (fn1
));
1196 attr2
= lookup_attribute ("target", DECL_ATTRIBUTES (fn2
));
1198 /* At least one function decl should have the target attribute specified. */
1199 if (attr1
== NULL_TREE
&& attr2
== NULL_TREE
)
1202 /* Diagnose missing target attribute if one of the decls is already
1204 if (attr1
== NULL_TREE
|| attr2
== NULL_TREE
)
1206 if (DECL_FUNCTION_VERSIONED (fn1
) || DECL_FUNCTION_VERSIONED (fn2
))
1208 if (attr2
!= NULL_TREE
)
1210 std::swap (fn1
, fn2
);
1213 auto_diagnostic_group d
;
1214 error_at (DECL_SOURCE_LOCATION (fn2
),
1215 "missing %<target%> attribute for multi-versioned %qD",
1217 inform (DECL_SOURCE_LOCATION (fn1
),
1218 "previous declaration of %qD", fn1
);
1219 /* Prevent diagnosing of the same error multiple times. */
1220 DECL_ATTRIBUTES (fn2
)
1221 = tree_cons (get_identifier ("target"),
1222 copy_node (TREE_VALUE (attr1
)),
1223 DECL_ATTRIBUTES (fn2
));
1228 target1
= sorted_attr_string (TREE_VALUE (attr1
));
1229 target2
= sorted_attr_string (TREE_VALUE (attr2
));
1231 /* The sorted target strings must be different for fn1 and fn2
1233 if (strcmp (target1
, target2
) == 0)
1238 XDELETEVEC (target1
);
1239 XDELETEVEC (target2
);
1244 /* Make a dispatcher declaration for the multi-versioned function DECL.
1245 Calls to DECL function will be replaced with calls to the dispatcher
1246 by the front-end. Return the decl created. */
1249 make_dispatcher_decl (const tree decl
)
1253 tree fn_type
, func_type
;
1255 func_name
= xstrdup (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
)));
1257 fn_type
= TREE_TYPE (decl
);
1258 func_type
= build_function_type (TREE_TYPE (fn_type
),
1259 TYPE_ARG_TYPES (fn_type
));
1261 func_decl
= build_fn_decl (func_name
, func_type
);
1262 XDELETEVEC (func_name
);
1263 TREE_USED (func_decl
) = 1;
1264 DECL_CONTEXT (func_decl
) = NULL_TREE
;
1265 DECL_INITIAL (func_decl
) = error_mark_node
;
1266 DECL_ARTIFICIAL (func_decl
) = 1;
1267 /* Mark this func as external, the resolver will flip it again if
1268 it gets generated. */
1269 DECL_EXTERNAL (func_decl
) = 1;
1270 /* This will be of type IFUNCs have to be externally visible. */
1271 TREE_PUBLIC (func_decl
) = 1;
1276 /* Returns true if DECL is multi-versioned using the target attribute, and this
1277 is the default version. This function can only be used for targets that do
1278 not support the "target_version" attribute. */
1281 is_function_default_version (const tree decl
)
1283 if (TREE_CODE (decl
) != FUNCTION_DECL
1284 || !DECL_FUNCTION_VERSIONED (decl
))
1286 tree attr
= lookup_attribute ("target", DECL_ATTRIBUTES (decl
));
1288 attr
= TREE_VALUE (TREE_VALUE (attr
));
1289 return (TREE_CODE (attr
) == STRING_CST
1290 && strcmp (TREE_STRING_POINTER (attr
), "default") == 0);
1293 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
1297 build_decl_attribute_variant (tree ddecl
, tree attribute
)
1299 DECL_ATTRIBUTES (ddecl
) = attribute
;
1303 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
1304 is ATTRIBUTE and its qualifiers are QUALS.
1306 Record such modified types already made so we don't make duplicates. */
1309 build_type_attribute_qual_variant (tree otype
, tree attribute
, int quals
)
1312 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype
), attribute
))
1316 /* Building a distinct copy of a tagged type is inappropriate; it
1317 causes breakage in code that expects there to be a one-to-one
1318 relationship between a struct and its fields.
1319 build_duplicate_type is another solution (as used in
1320 handle_transparent_union_attribute), but that doesn't play well
1321 with the stronger C++ type identity model. */
1322 if (RECORD_OR_UNION_TYPE_P (ttype
)
1323 || TREE_CODE (ttype
) == ENUMERAL_TYPE
)
1325 warning (OPT_Wattributes
,
1326 "ignoring attributes applied to %qT after definition",
1327 TYPE_MAIN_VARIANT (ttype
));
1328 return build_qualified_type (ttype
, quals
);
1331 ttype
= build_qualified_type (ttype
, TYPE_UNQUALIFIED
);
1332 if (lang_hooks
.types
.copy_lang_qualifiers
1333 && otype
!= TYPE_MAIN_VARIANT (otype
))
1334 ttype
= (lang_hooks
.types
.copy_lang_qualifiers
1335 (ttype
, TYPE_MAIN_VARIANT (otype
)));
1337 tree dtype
= ntype
= build_distinct_type_copy (ttype
);
1339 TYPE_ATTRIBUTES (ntype
) = attribute
;
1340 /* If the target-dependent attributes make NTYPE different from
1341 its canonical type, we will need to use structural equality
1342 checks for this type.
1344 We shouldn't get here for stripping attributes from a type;
1345 the no-attribute type might not need structural comparison. But
1346 we can if was discarded from type_hash_table. */
1347 if (TYPE_STRUCTURAL_EQUALITY_P (ttype
)
1348 || !comp_type_attributes (ntype
, ttype
))
1349 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
1351 hashval_t hash
= type_hash_canon_hash (ntype
);
1352 ntype
= type_hash_canon (hash
, ntype
);
1355 /* This variant was already in the hash table, don't mess with
1357 else if (TYPE_CANONICAL (ntype
) == ntype
)
1358 TYPE_CANONICAL (ntype
) = TYPE_CANONICAL (ttype
);
1360 ttype
= build_qualified_type (ntype
, quals
);
1361 if (lang_hooks
.types
.copy_lang_qualifiers
1362 && otype
!= TYPE_MAIN_VARIANT (otype
))
1363 ttype
= lang_hooks
.types
.copy_lang_qualifiers (ttype
, otype
);
1365 else if (TYPE_QUALS (ttype
) != quals
)
1366 ttype
= build_qualified_type (ttype
, quals
);
1371 /* Compare two identifier nodes representing attributes.
1372 Return true if they are the same, false otherwise. */
1375 cmp_attrib_identifiers (const_tree attr1
, const_tree attr2
)
1377 /* Make sure we're dealing with IDENTIFIER_NODEs. */
1378 gcc_checking_assert (TREE_CODE (attr1
) == IDENTIFIER_NODE
1379 && TREE_CODE (attr2
) == IDENTIFIER_NODE
);
1381 /* Identifiers can be compared directly for equality. */
1385 return cmp_attribs (IDENTIFIER_POINTER (attr1
), IDENTIFIER_LENGTH (attr1
),
1386 IDENTIFIER_POINTER (attr2
), IDENTIFIER_LENGTH (attr2
));
1389 /* Compare two constructor-element-type constants. Return 1 if the lists
1390 are known to be equal; otherwise return 0. */
1393 simple_cst_list_equal (const_tree l1
, const_tree l2
)
1395 while (l1
!= NULL_TREE
&& l2
!= NULL_TREE
)
1397 if (simple_cst_equal (TREE_VALUE (l1
), TREE_VALUE (l2
)) != 1)
1400 l1
= TREE_CHAIN (l1
);
1401 l2
= TREE_CHAIN (l2
);
1407 /* Check if "omp declare simd" attribute arguments, CLAUSES1 and CLAUSES2, are
1411 omp_declare_simd_clauses_equal (tree clauses1
, tree clauses2
)
1414 for (cl1
= clauses1
, cl2
= clauses2
;
1416 cl1
= OMP_CLAUSE_CHAIN (cl1
), cl2
= OMP_CLAUSE_CHAIN (cl2
))
1418 if (OMP_CLAUSE_CODE (cl1
) != OMP_CLAUSE_CODE (cl2
))
1420 if (OMP_CLAUSE_CODE (cl1
) != OMP_CLAUSE_SIMDLEN
)
1422 if (simple_cst_equal (OMP_CLAUSE_DECL (cl1
),
1423 OMP_CLAUSE_DECL (cl2
)) != 1)
1426 switch (OMP_CLAUSE_CODE (cl1
))
1428 case OMP_CLAUSE_ALIGNED
:
1429 if (simple_cst_equal (OMP_CLAUSE_ALIGNED_ALIGNMENT (cl1
),
1430 OMP_CLAUSE_ALIGNED_ALIGNMENT (cl2
)) != 1)
1433 case OMP_CLAUSE_LINEAR
:
1434 if (simple_cst_equal (OMP_CLAUSE_LINEAR_STEP (cl1
),
1435 OMP_CLAUSE_LINEAR_STEP (cl2
)) != 1)
1438 case OMP_CLAUSE_SIMDLEN
:
1439 if (simple_cst_equal (OMP_CLAUSE_SIMDLEN_EXPR (cl1
),
1440 OMP_CLAUSE_SIMDLEN_EXPR (cl2
)) != 1)
1450 /* Compare two attributes for their value identity. Return true if the
1451 attribute values are known to be equal; otherwise return false. */
1454 attribute_value_equal (const_tree attr1
, const_tree attr2
)
1456 if (TREE_VALUE (attr1
) == TREE_VALUE (attr2
))
1459 if (TREE_VALUE (attr1
) != NULL_TREE
1460 && TREE_CODE (TREE_VALUE (attr1
)) == TREE_LIST
1461 && TREE_VALUE (attr2
) != NULL_TREE
1462 && TREE_CODE (TREE_VALUE (attr2
)) == TREE_LIST
)
1464 /* Handle attribute format. */
1465 if (is_attribute_p ("format", get_attribute_name (attr1
)))
1467 attr1
= TREE_VALUE (attr1
);
1468 attr2
= TREE_VALUE (attr2
);
1469 /* Compare the archetypes (printf/scanf/strftime/...). */
1470 if (!cmp_attrib_identifiers (TREE_VALUE (attr1
), TREE_VALUE (attr2
)))
1472 /* Archetypes are the same. Compare the rest. */
1473 return (simple_cst_list_equal (TREE_CHAIN (attr1
),
1474 TREE_CHAIN (attr2
)) == 1);
1476 return (simple_cst_list_equal (TREE_VALUE (attr1
),
1477 TREE_VALUE (attr2
)) == 1);
1480 if (TREE_VALUE (attr1
)
1481 && TREE_CODE (TREE_VALUE (attr1
)) == OMP_CLAUSE
1482 && TREE_VALUE (attr2
)
1483 && TREE_CODE (TREE_VALUE (attr2
)) == OMP_CLAUSE
)
1484 return omp_declare_simd_clauses_equal (TREE_VALUE (attr1
),
1485 TREE_VALUE (attr2
));
1487 return (simple_cst_equal (TREE_VALUE (attr1
), TREE_VALUE (attr2
)) == 1);
1490 /* Return 0 if the attributes for two types are incompatible, 1 if they
1491 are compatible, and 2 if they are nearly compatible (which causes a
1492 warning to be generated). */
1494 comp_type_attributes (const_tree type1
, const_tree type2
)
1496 const_tree a1
= TYPE_ATTRIBUTES (type1
);
1497 const_tree a2
= TYPE_ATTRIBUTES (type2
);
1502 for (a
= a1
; a
!= NULL_TREE
; a
= TREE_CHAIN (a
))
1504 const struct attribute_spec
*as
;
1507 as
= lookup_attribute_spec (TREE_PURPOSE (a
));
1508 if (!as
|| as
->affects_type_identity
== false)
1511 attr
= find_same_attribute (a
, CONST_CAST_TREE (a2
));
1512 if (!attr
|| !attribute_value_equal (a
, attr
))
1517 for (a
= a2
; a
!= NULL_TREE
; a
= TREE_CHAIN (a
))
1519 const struct attribute_spec
*as
;
1521 as
= lookup_attribute_spec (TREE_PURPOSE (a
));
1522 if (!as
|| as
->affects_type_identity
== false)
1525 if (!find_same_attribute (a
, CONST_CAST_TREE (a1
)))
1527 /* We don't need to compare trees again, as we did this
1528 already in first loop. */
1530 /* All types - affecting identity - are equal, so
1531 there is no need to call target hook for comparison. */
1535 if (lookup_attribute ("transaction_safe", CONST_CAST_TREE (a
)))
1537 if ((lookup_attribute ("nocf_check", TYPE_ATTRIBUTES (type1
)) != NULL
)
1538 ^ (lookup_attribute ("nocf_check", TYPE_ATTRIBUTES (type2
)) != NULL
))
1540 int strub_ret
= strub_comptypes (CONST_CAST_TREE (type1
),
1541 CONST_CAST_TREE (type2
));
1544 /* As some type combinations - like default calling-convention - might
1545 be compatible, we have to call the target hook to get the final result. */
1546 int target_ret
= targetm
.comp_type_attributes (type1
, type2
);
1547 if (target_ret
== 0)
1549 if (strub_ret
== 2 || target_ret
== 2)
1551 if (strub_ret
== 1 && target_ret
== 1)
1556 /* PREDICATE acts as a function of type:
1558 (const_tree attr, const attribute_spec *as) -> bool
1560 where ATTR is an attribute and AS is its possibly-null specification.
1561 Return a list of every attribute in attribute list ATTRS for which
1562 PREDICATE is true. Return ATTRS itself if PREDICATE returns true
1563 for every attribute. */
1565 template<typename Predicate
>
1567 remove_attributes_matching (tree attrs
, Predicate predicate
)
1569 tree new_attrs
= NULL_TREE
;
1570 tree
*ptr
= &new_attrs
;
1571 const_tree start
= attrs
;
1572 for (const_tree attr
= attrs
; attr
; attr
= TREE_CHAIN (attr
))
1574 const attribute_spec
*as
= lookup_attribute_spec (TREE_PURPOSE (attr
));
1576 if (!predicate (attr
, as
))
1578 else if (start
== attrs
)
1581 end
= TREE_CHAIN (attr
);
1583 for (; start
!= end
; start
= TREE_CHAIN (start
))
1585 *ptr
= tree_cons (TREE_PURPOSE (start
),
1586 TREE_VALUE (start
), NULL_TREE
);
1587 TREE_CHAIN (*ptr
) = NULL_TREE
;
1588 ptr
= &TREE_CHAIN (*ptr
);
1590 start
= TREE_CHAIN (attr
);
1592 gcc_assert (!start
|| start
== attrs
);
1593 return start
? attrs
: new_attrs
;
1596 /* If VALUE is true, return the subset of ATTRS that affect type identity,
1597 otherwise return the subset of ATTRS that don't affect type identity. */
1600 affects_type_identity_attributes (tree attrs
, bool value
)
1602 auto predicate
= [value
](const_tree
, const attribute_spec
*as
) -> bool
1604 return bool (as
&& as
->affects_type_identity
) == value
;
1606 return remove_attributes_matching (attrs
, predicate
);
1609 /* Remove attributes that affect type identity from ATTRS unless the
1610 same attributes occur in OK_ATTRS. */
1613 restrict_type_identity_attributes_to (tree attrs
, tree ok_attrs
)
1615 auto predicate
= [ok_attrs
](const_tree attr
,
1616 const attribute_spec
*as
) -> bool
1618 if (!as
|| !as
->affects_type_identity
)
1621 for (tree ok_attr
= lookup_attribute (as
->name
, ok_attrs
);
1623 ok_attr
= lookup_attribute (as
->name
, TREE_CHAIN (ok_attr
)))
1624 if (simple_cst_equal (TREE_VALUE (ok_attr
), TREE_VALUE (attr
)) == 1)
1629 return remove_attributes_matching (attrs
, predicate
);
1632 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
1635 Record such modified types already made so we don't make duplicates. */
1638 build_type_attribute_variant (tree ttype
, tree attribute
)
1640 return build_type_attribute_qual_variant (ttype
, attribute
,
1641 TYPE_QUALS (ttype
));
1644 /* A variant of lookup_attribute() that can be used with an identifier
1645 as the first argument, and where the identifier can be either
1646 'text' or '__text__'.
1648 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
1649 return a pointer to the attribute's list element if the attribute
1650 is part of the list, or NULL_TREE if not found. If the attribute
1651 appears more than once, this only returns the first occurrence; the
1652 TREE_CHAIN of the return value should be passed back in if further
1653 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
1654 can be in the form 'text' or '__text__'. */
1656 lookup_ident_attribute (tree attr_identifier
, tree list
)
1658 gcc_checking_assert (TREE_CODE (attr_identifier
) == IDENTIFIER_NODE
);
1662 gcc_checking_assert (TREE_CODE (get_attribute_name (list
))
1663 == IDENTIFIER_NODE
);
1665 if (cmp_attrib_identifiers (attr_identifier
,
1666 get_attribute_name (list
)))
1669 list
= TREE_CHAIN (list
);
1675 /* Remove any instances of attribute ATTR_NAME in LIST and return the
1679 remove_attribute (const char *attr_name
, tree list
)
1682 gcc_checking_assert (attr_name
[0] != '_');
1684 for (p
= &list
; *p
;)
1688 tree attr
= get_attribute_name (l
);
1689 if (is_attribute_p (attr_name
, attr
))
1690 *p
= TREE_CHAIN (l
);
1692 p
= &TREE_CHAIN (l
);
1698 /* Similarly but also match namespace on the removed attributes.
1699 ATTR_NS "" stands for NULL or "gnu" namespace. */
1702 remove_attribute (const char *attr_ns
, const char *attr_name
, tree list
)
1705 gcc_checking_assert (attr_name
[0] != '_');
1706 gcc_checking_assert (attr_ns
== NULL
|| attr_ns
[0] != '_');
1708 for (p
= &list
; *p
;)
1712 tree attr
= get_attribute_name (l
);
1713 if (is_attribute_p (attr_name
, attr
)
1714 && is_attribute_namespace_p (attr_ns
, l
))
1716 *p
= TREE_CHAIN (l
);
1719 p
= &TREE_CHAIN (l
);
1725 /* Return an attribute list that is the union of a1 and a2. */
1728 merge_attributes (tree a1
, tree a2
)
1732 /* Either one unset? Take the set one. */
1734 if ((attributes
= a1
) == 0)
1737 /* One that completely contains the other? Take it. */
1739 else if (a2
!= 0 && ! attribute_list_contained (a1
, a2
))
1741 if (attribute_list_contained (a2
, a1
))
1745 /* Pick the longest list, and hang on the other list. */
1747 if (list_length (a1
) < list_length (a2
))
1748 attributes
= a2
, a2
= a1
;
1750 for (; a2
!= 0; a2
= TREE_CHAIN (a2
))
1753 for (a
= lookup_ident_attribute (get_attribute_name (a2
),
1755 a
!= NULL_TREE
&& !attribute_value_equal (a
, a2
);
1756 a
= lookup_ident_attribute (get_attribute_name (a2
),
1761 a1
= copy_node (a2
);
1762 TREE_CHAIN (a1
) = attributes
;
1771 /* Given types T1 and T2, merge their attributes and return
1775 merge_type_attributes (tree t1
, tree t2
)
1777 return merge_attributes (TYPE_ATTRIBUTES (t1
),
1778 TYPE_ATTRIBUTES (t2
));
1781 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
1785 merge_decl_attributes (tree olddecl
, tree newdecl
)
1787 return merge_attributes (DECL_ATTRIBUTES (olddecl
),
1788 DECL_ATTRIBUTES (newdecl
));
1791 /* Duplicate all attributes with name NAME in ATTR list to *ATTRS if
1792 they are missing there. */
1795 duplicate_one_attribute (tree
*attrs
, tree attr
, const char *name
)
1797 attr
= lookup_attribute (name
, attr
);
1800 tree a
= lookup_attribute (name
, *attrs
);
1804 for (a2
= a
; a2
; a2
= lookup_attribute (name
, TREE_CHAIN (a2
)))
1805 if (attribute_value_equal (attr
, a2
))
1809 a2
= copy_node (attr
);
1810 TREE_CHAIN (a2
) = *attrs
;
1813 attr
= lookup_attribute (name
, TREE_CHAIN (attr
));
1817 /* Duplicate all attributes from user DECL to the corresponding
1818 builtin that should be propagated. */
1821 copy_attributes_to_builtin (tree decl
)
1823 tree b
= builtin_decl_explicit (DECL_FUNCTION_CODE (decl
));
1825 duplicate_one_attribute (&DECL_ATTRIBUTES (b
),
1826 DECL_ATTRIBUTES (decl
), "omp declare simd");
1829 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
1831 /* Specialization of merge_decl_attributes for various Windows targets.
1833 This handles the following situation:
1835 __declspec (dllimport) int foo;
1838 The second instance of `foo' nullifies the dllimport. */
1841 merge_dllimport_decl_attributes (tree old
, tree new_tree
)
1844 int delete_dllimport_p
= 1;
1846 /* What we need to do here is remove from `old' dllimport if it doesn't
1847 appear in `new'. dllimport behaves like extern: if a declaration is
1848 marked dllimport and a definition appears later, then the object
1849 is not dllimport'd. We also remove a `new' dllimport if the old list
1850 contains dllexport: dllexport always overrides dllimport, regardless
1851 of the order of declaration. */
1852 if (!VAR_OR_FUNCTION_DECL_P (new_tree
))
1853 delete_dllimport_p
= 0;
1854 else if (DECL_DLLIMPORT_P (new_tree
)
1855 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old
)))
1857 DECL_DLLIMPORT_P (new_tree
) = 0;
1858 warning (OPT_Wattributes
, "%q+D already declared with dllexport "
1859 "attribute: dllimport ignored", new_tree
);
1861 else if (DECL_DLLIMPORT_P (old
) && !DECL_DLLIMPORT_P (new_tree
))
1863 /* Warn about overriding a symbol that has already been used, e.g.:
1864 extern int __attribute__ ((dllimport)) foo;
1865 int* bar () {return &foo;}
1868 if (TREE_USED (old
))
1870 warning (0, "%q+D redeclared without dllimport attribute "
1871 "after being referenced with dll linkage", new_tree
);
1872 /* If we have used a variable's address with dllimport linkage,
1873 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
1874 decl may already have had TREE_CONSTANT computed.
1875 We still remove the attribute so that assembler code refers
1876 to '&foo rather than '_imp__foo'. */
1877 if (VAR_P (old
) && TREE_ADDRESSABLE (old
))
1878 DECL_DLLIMPORT_P (new_tree
) = 1;
1881 /* Let an inline definition silently override the external reference,
1882 but otherwise warn about attribute inconsistency. */
1883 else if (VAR_P (new_tree
) || !DECL_DECLARED_INLINE_P (new_tree
))
1884 warning (OPT_Wattributes
, "%q+D redeclared without dllimport "
1885 "attribute: previous dllimport ignored", new_tree
);
1888 delete_dllimport_p
= 0;
1890 a
= merge_attributes (DECL_ATTRIBUTES (old
), DECL_ATTRIBUTES (new_tree
));
1892 if (delete_dllimport_p
)
1893 a
= remove_attribute ("dllimport", a
);
1898 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
1899 struct attribute_spec.handler. */
1902 handle_dll_attribute (tree
* pnode
, tree name
, tree args
, int flags
,
1908 /* These attributes may apply to structure and union types being created,
1909 but otherwise should pass to the declaration involved. */
1912 if (flags
& ((int) ATTR_FLAG_DECL_NEXT
| (int) ATTR_FLAG_FUNCTION_NEXT
1913 | (int) ATTR_FLAG_ARRAY_NEXT
))
1915 *no_add_attrs
= true;
1916 return tree_cons (name
, args
, NULL_TREE
);
1918 if (TREE_CODE (node
) == RECORD_TYPE
1919 || TREE_CODE (node
) == UNION_TYPE
)
1921 node
= TYPE_NAME (node
);
1927 warning (OPT_Wattributes
, "%qE attribute ignored",
1929 *no_add_attrs
= true;
1934 if (!VAR_OR_FUNCTION_DECL_P (node
) && TREE_CODE (node
) != TYPE_DECL
)
1936 *no_add_attrs
= true;
1937 warning (OPT_Wattributes
, "%qE attribute ignored",
1942 if (TREE_CODE (node
) == TYPE_DECL
1943 && TREE_CODE (TREE_TYPE (node
)) != RECORD_TYPE
1944 && TREE_CODE (TREE_TYPE (node
)) != UNION_TYPE
)
1946 *no_add_attrs
= true;
1947 warning (OPT_Wattributes
, "%qE attribute ignored",
1952 is_dllimport
= is_attribute_p ("dllimport", name
);
1954 /* Report error on dllimport ambiguities seen now before they cause
1958 /* Honor any target-specific overrides. */
1959 if (!targetm
.valid_dllimport_attribute_p (node
))
1960 *no_add_attrs
= true;
1962 else if (TREE_CODE (node
) == FUNCTION_DECL
1963 && DECL_DECLARED_INLINE_P (node
))
1965 warning (OPT_Wattributes
, "inline function %q+D declared as "
1966 "dllimport: attribute ignored", node
);
1967 *no_add_attrs
= true;
1969 /* Like MS, treat definition of dllimported variables and
1970 non-inlined functions on declaration as syntax errors. */
1971 else if (TREE_CODE (node
) == FUNCTION_DECL
&& DECL_INITIAL (node
))
1973 error ("function %q+D definition is marked dllimport", node
);
1974 *no_add_attrs
= true;
1977 else if (VAR_P (node
))
1979 if (DECL_INITIAL (node
))
1981 error ("variable %q+D definition is marked dllimport",
1983 *no_add_attrs
= true;
1986 /* `extern' needn't be specified with dllimport.
1987 Specify `extern' now and hope for the best. Sigh. */
1988 DECL_EXTERNAL (node
) = 1;
1989 /* Also, implicitly give dllimport'd variables declared within
1990 a function global scope, unless declared static. */
1991 if (current_function_decl
!= NULL_TREE
&& !TREE_STATIC (node
))
1992 TREE_PUBLIC (node
) = 1;
1993 /* Clear TREE_STATIC because DECL_EXTERNAL is set, unless
1994 it is a C++ static data member. */
1995 if (DECL_CONTEXT (node
) == NULL_TREE
1996 || !RECORD_OR_UNION_TYPE_P (DECL_CONTEXT (node
)))
1997 TREE_STATIC (node
) = 0;
2000 if (*no_add_attrs
== false)
2001 DECL_DLLIMPORT_P (node
) = 1;
2003 else if (TREE_CODE (node
) == FUNCTION_DECL
2004 && DECL_DECLARED_INLINE_P (node
)
2005 && flag_keep_inline_dllexport
)
2006 /* An exported function, even if inline, must be emitted. */
2007 DECL_EXTERNAL (node
) = 0;
2009 /* Report error if symbol is not accessible at global scope. */
2010 if (!TREE_PUBLIC (node
) && VAR_OR_FUNCTION_DECL_P (node
))
2012 error ("external linkage required for symbol %q+D because of "
2013 "%qE attribute", node
, name
);
2014 *no_add_attrs
= true;
2017 /* A dllexport'd entity must have default visibility so that other
2018 program units (shared libraries or the main executable) can see
2019 it. A dllimport'd entity must have default visibility so that
2020 the linker knows that undefined references within this program
2021 unit can be resolved by the dynamic linker. */
2024 if (DECL_VISIBILITY_SPECIFIED (node
)
2025 && DECL_VISIBILITY (node
) != VISIBILITY_DEFAULT
)
2026 error ("%qE implies default visibility, but %qD has already "
2027 "been declared with a different visibility",
2029 DECL_VISIBILITY (node
) = VISIBILITY_DEFAULT
;
2030 DECL_VISIBILITY_SPECIFIED (node
) = 1;
2036 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
2038 /* Given two lists of attributes, return true if list l2 is
2039 equivalent to l1. */
2042 attribute_list_equal (const_tree l1
, const_tree l2
)
2047 return attribute_list_contained (l1
, l2
)
2048 && attribute_list_contained (l2
, l1
);
2051 /* Given two lists of attributes, return true if list L2 is
2052 completely contained within L1. */
2053 /* ??? This would be faster if attribute names were stored in a canonicalized
2054 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
2055 must be used to show these elements are equivalent (which they are). */
2056 /* ??? It's not clear that attributes with arguments will always be handled
2060 attribute_list_contained (const_tree l1
, const_tree l2
)
2064 /* First check the obvious, maybe the lists are identical. */
2068 /* Maybe the lists are similar. */
2069 for (t1
= l1
, t2
= l2
;
2071 && get_attribute_name (t1
) == get_attribute_name (t2
)
2072 && TREE_VALUE (t1
) == TREE_VALUE (t2
);
2073 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
2076 /* Maybe the lists are equal. */
2077 if (t1
== 0 && t2
== 0)
2080 for (; t2
!= 0; t2
= TREE_CHAIN (t2
))
2083 /* This CONST_CAST is okay because lookup_attribute does not
2084 modify its argument and the return value is assigned to a
2086 for (attr
= lookup_ident_attribute (get_attribute_name (t2
),
2087 CONST_CAST_TREE (l1
));
2088 attr
!= NULL_TREE
&& !attribute_value_equal (t2
, attr
);
2089 attr
= lookup_ident_attribute (get_attribute_name (t2
),
2093 if (attr
== NULL_TREE
)
2100 /* The backbone of lookup_attribute(). ATTR_LEN is the string length
2101 of ATTR_NAME, and LIST is not NULL_TREE.
2103 The function is called from lookup_attribute in order to optimize
2107 private_lookup_attribute (const char *attr_name
, size_t attr_len
, tree list
)
2111 tree attr
= get_attribute_name (list
);
2112 size_t ident_len
= IDENTIFIER_LENGTH (attr
);
2113 if (cmp_attribs (attr_name
, attr_len
, IDENTIFIER_POINTER (attr
),
2116 list
= TREE_CHAIN (list
);
2122 /* Similarly but with also attribute namespace. */
2125 private_lookup_attribute (const char *attr_ns
, const char *attr_name
,
2126 size_t attr_ns_len
, size_t attr_len
, tree list
)
2130 tree attr
= get_attribute_name (list
);
2131 size_t ident_len
= IDENTIFIER_LENGTH (attr
);
2132 if (cmp_attribs (attr_name
, attr_len
, IDENTIFIER_POINTER (attr
),
2135 tree ns
= get_attribute_namespace (list
);
2136 if (ns
== NULL_TREE
)
2138 if (attr_ns_len
== 0)
2143 ident_len
= IDENTIFIER_LENGTH (ns
);
2144 if (attr_ns_len
== 0)
2146 if (cmp_attribs ("gnu", strlen ("gnu"),
2147 IDENTIFIER_POINTER (ns
), ident_len
))
2150 else if (cmp_attribs (attr_ns
, attr_ns_len
,
2151 IDENTIFIER_POINTER (ns
), ident_len
))
2155 list
= TREE_CHAIN (list
);
2161 /* Return true if the function decl or type NODE has been declared
2162 with attribute ANAME among attributes ATTRS. */
2165 has_attribute (tree node
, tree attrs
, const char *aname
)
2167 if (!strcmp (aname
, "const"))
2169 if (DECL_P (node
) && TREE_READONLY (node
))
2172 else if (!strcmp (aname
, "malloc"))
2174 if (DECL_P (node
) && DECL_IS_MALLOC (node
))
2177 else if (!strcmp (aname
, "noreturn"))
2179 if (DECL_P (node
) && TREE_THIS_VOLATILE (node
))
2182 else if (!strcmp (aname
, "nothrow"))
2184 if (TREE_NOTHROW (node
))
2187 else if (!strcmp (aname
, "pure"))
2189 if (DECL_P (node
) && DECL_PURE_P (node
))
2193 return lookup_attribute (aname
, attrs
);
2196 /* Return the number of mismatched function or type attributes between
2197 the "template" function declaration TMPL and DECL. The word "template"
2198 doesn't necessarily refer to a C++ template but rather a declaration
2199 whose attributes should be matched by those on DECL. For a non-zero
2200 return value append the names of the mismatcheed attributes to OUTATTRS.
2201 ATTRLIST is a list of additional attributes that SPEC should be
2202 taken to ultimately be declared with. */
2205 decls_mismatched_attributes (tree tmpl
, tree decl
, tree attrlist
,
2206 const char* const blacklist
[],
2207 auto_vec
<const char *> &outattrs
)
2209 if (TREE_CODE (tmpl
) != FUNCTION_DECL
)
2212 /* Avoid warning if either declaration or its type is deprecated. */
2213 if (TREE_DEPRECATED (tmpl
)
2214 || TREE_DEPRECATED (decl
))
2217 const tree tmpls
[] = { tmpl
, TREE_TYPE (tmpl
) };
2218 const tree decls
[] = { decl
, TREE_TYPE (decl
) };
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])))
2226 tree tmpl_attrs
[] = { DECL_ATTRIBUTES (tmpl
), TYPE_ATTRIBUTES (tmpls
[1]) };
2227 tree decl_attrs
[] = { DECL_ATTRIBUTES (decl
), TYPE_ATTRIBUTES (decls
[1]) };
2230 decl_attrs
[0] = attrlist
;
2231 else if (!decl_attrs
[1])
2232 decl_attrs
[1] = attrlist
;
2234 /* Avoid warning if the template has no attributes. */
2235 if (!tmpl_attrs
[0] && !tmpl_attrs
[1])
2238 /* Avoid warning if either declaration contains an attribute on
2239 the white list below. */
2240 const char* const whitelist
[] = {
2244 for (unsigned i
= 0; i
!= 2; ++i
)
2245 for (unsigned j
= 0; j
!= ARRAY_SIZE (whitelist
); ++j
)
2246 if (lookup_attribute (whitelist
[j
], tmpl_attrs
[i
])
2247 || lookup_attribute (whitelist
[j
], decl_attrs
[i
]))
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;
2255 for (unsigned i
= 0; blacklist
[i
]; ++i
)
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 ("leaf", blacklist
[i
]))
2263 for (unsigned j
= 0; j
!= 2; ++j
)
2265 if (!has_attribute (tmpls
[j
], tmpl_attrs
[j
], blacklist
[i
]))
2269 unsigned kmax
= 1 + !!decl_attrs
[1];
2270 for (unsigned k
= 0; k
!= kmax
; ++k
)
2272 if (has_attribute (decls
[k
], decl_attrs
[k
], blacklist
[i
]))
2281 outattrs
.safe_push (blacklist
[i
]);
2292 /* Issue a warning for the declaration ALIAS for TARGET where ALIAS
2293 specifies either attributes that are incompatible with those of
2294 TARGET, or attributes that are missing and that declaring ALIAS
2295 with would benefit. */
2298 maybe_diag_alias_attributes (tree alias
, tree target
)
2300 /* Do not expect attributes to match between aliases and ifunc
2301 resolvers. There is no obvious correspondence between them. */
2302 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (alias
)))
2305 const char* const blacklist
[] = {
2306 "alloc_align", "alloc_size", "cold", "const", "hot", "leaf", "malloc",
2307 "nonnull", "noreturn", "nothrow", "pure", "returns_nonnull",
2308 "returns_twice", NULL
2311 if (warn_attribute_alias
> 1)
2313 /* With -Wattribute-alias=2 detect alias declarations that are more
2314 restrictive than their targets first. Those indicate potential
2316 auto_vec
<const char *> mismatches
;
2317 if (unsigned n
= decls_mismatched_attributes (alias
, target
, NULL_TREE
,
2318 blacklist
, mismatches
))
2320 auto_diagnostic_group d
;
2321 pp_markup::comma_separated_quoted_strings
e (mismatches
);
2322 if (warning_n (DECL_SOURCE_LOCATION (alias
),
2323 OPT_Wattribute_alias_
, n
,
2324 "%qD specifies more restrictive attribute than "
2325 "its target %qD: %e",
2326 "%qD specifies more restrictive attributes than "
2327 "its target %qD: %e",
2329 inform (DECL_SOURCE_LOCATION (target
),
2330 "%qD target declared here", alias
);
2335 /* Detect alias declarations that are less restrictive than their
2336 targets. Those suggest potential optimization opportunities
2337 (solved by adding the missing attribute(s) to the alias). */
2338 auto_vec
<const char *> mismatches
;
2339 if (unsigned n
= decls_mismatched_attributes (target
, alias
, NULL_TREE
,
2340 blacklist
, mismatches
))
2342 auto_diagnostic_group d
;
2343 pp_markup::comma_separated_quoted_strings
e (mismatches
);
2344 if (warning_n (DECL_SOURCE_LOCATION (alias
),
2345 OPT_Wmissing_attributes
, n
,
2346 "%qD specifies less restrictive attribute than "
2347 "its target %qD: %e",
2348 "%qD specifies less restrictive attributes than "
2349 "its target %qD: %e",
2351 inform (DECL_SOURCE_LOCATION (target
),
2352 "%qD target declared here", alias
);
2356 /* Initialize a mapping RWM for a call to a function declared with
2357 attribute access in ATTRS. Each attribute positional operand
2358 inserts one entry into the mapping with the operand number as
2362 init_attr_rdwr_indices (rdwr_map
*rwm
, tree attrs
)
2367 for (tree access
= attrs
;
2368 (access
= lookup_attribute ("access", access
));
2369 access
= TREE_CHAIN (access
))
2371 /* The TREE_VALUE of an attribute is a TREE_LIST whose TREE_VALUE
2372 is the attribute argument's value. */
2373 tree mode
= TREE_VALUE (access
);
2377 /* The (optional) list of VLA bounds. */
2378 tree vblist
= TREE_CHAIN (mode
);
2379 mode
= TREE_VALUE (mode
);
2380 if (TREE_CODE (mode
) != STRING_CST
)
2382 gcc_assert (TREE_CODE (mode
) == STRING_CST
);
2385 vblist
= nreverse (copy_list (TREE_VALUE (vblist
)));
2387 for (const char *m
= TREE_STRING_POINTER (mode
); *m
; )
2389 attr_access acc
= { };
2391 /* Skip the internal-only plus sign. */
2396 acc
.mode
= acc
.from_mode_char (*m
);
2397 acc
.sizarg
= UINT_MAX
;
2400 acc
.ptrarg
= strtoul (++m
, const_cast<char**>(&end
), 10);
2405 /* Forms containing the square bracket are internal-only
2406 (not specified by an attribute declaration), and used
2407 for various forms of array and VLA parameters. */
2408 acc
.internal_p
= true;
2410 /* Search to the closing bracket and look at the preceding
2411 code: it determines the form of the most significant
2412 bound of the array. Others prior to it encode the form
2413 of interior VLA bounds. They're not of interest here. */
2414 end
= strchr (m
, ']');
2415 const char *p
= end
;
2418 while (ISDIGIT (p
[-1]))
2423 /* A digit denotes a constant bound (as in T[3]). */
2424 acc
.static_p
= p
[-1] == 's';
2425 acc
.minsize
= strtoull (p
, NULL
, 10);
2427 else if (' ' == p
[-1])
2429 /* A space denotes an ordinary array of unspecified bound
2433 else if ('*' == p
[-1] || '$' == p
[-1])
2435 /* An asterisk denotes a VLA. When the closing bracket
2436 is followed by a comma and a dollar sign its bound is
2437 on the list. Otherwise it's a VLA with an unspecified
2439 acc
.static_p
= p
[-2] == 's';
2440 acc
.minsize
= HOST_WIDE_INT_M1U
;
2454 if (!acc
.size
&& vblist
)
2456 /* Extract the list of VLA bounds for the current
2457 parameter, store it in ACC.SIZE, and advance
2458 to the list of bounds for the next VLA parameter.
2460 acc
.size
= TREE_VALUE (vblist
);
2461 vblist
= TREE_CHAIN (vblist
);
2467 /* Extract the positional argument. It's absent
2468 for VLAs whose bound doesn't name a function
2470 unsigned pos
= strtoul (m
, const_cast<char**>(&end
), 10);
2471 if (acc
.sizarg
== UINT_MAX
)
2482 auto &ref
= rwm
->get_or_insert (acc
.ptrarg
, &existing
);
2485 /* Merge the new spec with the existing. */
2486 if (acc
.minsize
== HOST_WIDE_INT_M1U
)
2487 ref
.minsize
= HOST_WIDE_INT_M1U
;
2489 if (acc
.sizarg
!= UINT_MAX
)
2490 ref
.sizarg
= acc
.sizarg
;
2493 ref
.mode
= acc
.mode
;
2498 /* Unconditionally add an entry for the required pointer
2499 operand of the attribute, and one for the optional size
2500 operand when it's specified. */
2501 if (acc
.sizarg
!= UINT_MAX
)
2502 rwm
->put (acc
.sizarg
, acc
);
2507 /* Return the access specification for a function parameter PARM
2508 or null if the current function has no such specification. */
2511 get_parm_access (rdwr_map
&rdwr_idx
, tree parm
,
2512 tree fndecl
/* = current_function_decl */)
2514 tree fntype
= TREE_TYPE (fndecl
);
2515 init_attr_rdwr_indices (&rdwr_idx
, TYPE_ATTRIBUTES (fntype
));
2517 if (rdwr_idx
.is_empty ())
2520 unsigned argpos
= 0;
2521 tree fnargs
= DECL_ARGUMENTS (fndecl
);
2522 for (tree arg
= fnargs
; arg
; arg
= TREE_CHAIN (arg
), ++argpos
)
2524 return rdwr_idx
.get (argpos
);
2529 /* Return the internal representation as STRING_CST. Internal positional
2530 arguments are zero-based. */
2533 attr_access::to_internal_string () const
2535 return build_string (end
- str
, str
);
2538 /* Return the human-readable representation of the external attribute
2539 specification (as it might appear in the source code) as STRING_CST.
2540 External positional arguments are one-based. */
2543 attr_access::to_external_string () const
2546 gcc_assert (mode
!= access_deferred
);
2547 int len
= snprintf (buf
, sizeof buf
, "access (%s, %u",
2548 mode_names
[mode
], ptrarg
+ 1);
2549 if (sizarg
!= UINT_MAX
)
2550 len
+= snprintf (buf
+ len
, sizeof buf
- len
, ", %u", sizarg
+ 1);
2551 strcpy (buf
+ len
, ")");
2552 return build_string (len
+ 2, buf
);
2555 /* Return the number of specified VLA bounds and set *nunspec to
2556 the number of unspecified ones (those designated by [*]). */
2559 attr_access::vla_bounds (unsigned *nunspec
) const
2561 unsigned nbounds
= 0;
2563 /* STR points to the beginning of the specified string for the current
2564 argument that may be followed by the string for the next argument. */
2565 for (const char* p
= strchr (str
, ']'); p
&& *p
!= '['; --p
)
2575 /* Reset front end-specific attribute access data from ATTRS.
2576 Called from the free_lang_data pass. */
2579 attr_access::free_lang_data (tree attrs
)
2581 for (tree acs
= attrs
; (acs
= lookup_attribute ("access", acs
));
2582 acs
= TREE_CHAIN (acs
))
2584 tree vblist
= TREE_VALUE (acs
);
2585 vblist
= TREE_CHAIN (vblist
);
2589 for (vblist
= TREE_VALUE (vblist
); vblist
; vblist
= TREE_CHAIN (vblist
))
2591 tree
*pvbnd
= &TREE_VALUE (vblist
);
2592 if (!*pvbnd
|| DECL_P (*pvbnd
))
2595 /* VLA bounds that are expressions as opposed to DECLs are
2596 only used in the front end. Reset them to keep front end
2597 trees leaking into the middle end (see pr97172) and to
2603 for (tree argspec
= attrs
; (argspec
= lookup_attribute ("arg spec", argspec
));
2604 argspec
= TREE_CHAIN (argspec
))
2606 /* Same as above. */
2607 tree
*pvblist
= &TREE_VALUE (argspec
);
2608 *pvblist
= NULL_TREE
;
2612 /* Defined in attr_access. */
2613 constexpr char attr_access::mode_chars
[];
2614 constexpr char attr_access::mode_names
[][11];
2616 /* Format an array, including a VLA, pointed to by TYPE and used as
2617 a function parameter as a human-readable string. ACC describes
2618 an access to the parameter and is used to determine the outermost
2619 form of the array including its bound which is otherwise obviated
2620 by its decay to pointer. Return the formatted string. */
2623 attr_access::array_as_string (tree type
) const
2627 if (type
== error_mark_node
)
2628 return std::string ();
2632 /* For array parameters (but not pointers) create a temporary array
2633 type that corresponds to the form of the parameter including its
2634 qualifiers even though they apply to the pointer, not the array
2636 const bool vla_p
= minsize
== HOST_WIDE_INT_M1U
;
2637 tree eltype
= TREE_TYPE (type
);
2638 tree index_type
= NULL_TREE
;
2640 if (minsize
== HOST_WIDE_INT_M1U
)
2642 /* Determine if this is a VLA (an array whose most significant
2643 bound is nonconstant and whose access string has "$]" in it)
2644 extract the bound expression from SIZE. */
2645 const char *p
= end
;
2646 for ( ; p
!= str
&& *p
-- != ']'; );
2648 /* SIZE may have been cleared. Use it with care. */
2649 index_type
= build_index_type (size
? TREE_VALUE (size
) : size
);
2652 index_type
= build_index_type (size_int (minsize
- 1));
2654 tree arat
= NULL_TREE
;
2655 if (static_p
|| vla_p
)
2657 tree flag
= static_p
? integer_one_node
: NULL_TREE
;
2658 /* Hack: there's no language-independent way to encode
2659 the "static" specifier or the "*" notation in an array type.
2660 Add a "fake" attribute to have the pretty-printer add "static"
2661 or "*". The "[static N]" notation is only valid in the most
2662 significant bound but [*] can be used for any bound. Because
2663 [*] is represented the same as [0] this hack only works for
2664 the most significant bound like static and the others are
2666 arat
= build_tree_list (get_identifier ("array"), flag
);
2669 const int quals
= TYPE_QUALS (type
);
2670 type
= build_array_type (eltype
, index_type
);
2671 type
= build_type_attribute_qual_variant (type
, arat
, quals
);
2674 /* Format the type using the current pretty printer. The generic tree
2675 printer does a terrible job. */
2676 pretty_printer
*pp
= global_dc
->m_printer
->clone ();
2677 pp_printf (pp
, "%qT", type
);
2678 typstr
= pp_formatted_text (pp
);
2689 /* Self-test to verify that each attribute exclusion is symmetric,
2690 meaning that if attribute A is encoded as incompatible with
2691 attribute B then the opposite relationship is also encoded.
2692 This test also detects most cases of misspelled attribute names
2696 test_attribute_exclusions ()
2698 using excl_hash_traits
= pair_hash
<nofree_string_hash
, nofree_string_hash
>;
2700 /* Iterate over the array of attribute tables first (with TI0 as
2701 the index) and over the array of attribute_spec in each table
2702 (with SI0 as the index). */
2703 hash_set
<excl_hash_traits
> excl_set
;
2705 for (auto scoped_array
: attribute_tables
)
2706 for (auto scoped_attributes
: scoped_array
)
2707 for (const attribute_spec
&attribute
: scoped_attributes
->attributes
)
2709 const attribute_spec::exclusions
*excl
= attribute
.exclude
;
2711 /* Skip each attribute that doesn't define exclusions. */
2715 /* Skip standard (non-GNU) attributes, since currently the
2716 exclusions are implicitly for GNU attributes only.
2717 Also, C++ likely and unlikely get rewritten to gnu::hot
2718 and gnu::cold, so symmetry isn't necessary there. */
2719 if (!scoped_attributes
->ns
)
2722 const char *attr_name
= attribute
.name
;
2724 /* Iterate over the set of exclusions for every attribute
2725 (with EI0 as the index) adding the exclusions defined
2726 for each to the set. */
2727 for (size_t ei0
= 0; excl
[ei0
].name
; ++ei0
)
2729 const char *excl_name
= excl
[ei0
].name
;
2731 if (!strcmp (attr_name
, excl_name
))
2734 excl_set
.add ({ attr_name
, excl_name
});
2738 /* Traverse the set of mutually exclusive pairs of attributes
2739 and verify that they are symmetric. */
2740 for (auto excl_pair
: excl_set
)
2741 if (!excl_set
.contains ({ excl_pair
.second
, excl_pair
.first
}))
2743 /* An exclusion for an attribute has been found that
2744 doesn't have a corresponding exclusion in the opposite
2747 sprintf (desc
, "'%s' attribute exclusion '%s' must be symmetric",
2748 excl_pair
.first
, excl_pair
.second
);
2749 fail (SELFTEST_LOCATION
, desc
);
2756 test_attribute_exclusions ();
2759 } /* namespace selftest */
2761 #endif /* CHECKING_P */
2763 #include "gt-attribs.h"