1 /* Functions dealing with attribute handling, used by most front ends.
2 Copyright (C) 1992-2022 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 "stor-layout.h"
31 #include "langhooks.h"
35 #include "diagnostic.h"
36 #include "pretty-print.h"
37 #include "tree-pretty-print.h"
40 /* Table of the tables of attributes (common, language, format, machine)
42 static const struct attribute_spec
*attribute_tables
[4];
44 /* Substring representation. */
52 /* Simple hash function to avoid need to scan whole string. */
54 static inline hashval_t
55 substring_hash (const char *str
, int l
)
57 return str
[0] + str
[l
- 1] * 256 + l
* 65536;
60 /* Used for attribute_hash. */
62 struct attribute_hasher
: nofree_ptr_hash
<attribute_spec
>
64 typedef substring
*compare_type
;
65 static inline hashval_t
hash (const attribute_spec
*);
66 static inline bool equal (const attribute_spec
*, const substring
*);
70 attribute_hasher::hash (const attribute_spec
*spec
)
72 const int l
= strlen (spec
->name
);
73 return substring_hash (spec
->name
, l
);
77 attribute_hasher::equal (const attribute_spec
*spec
, const substring
*str
)
79 return (strncmp (spec
->name
, str
->str
, str
->length
) == 0
80 && !spec
->name
[str
->length
]);
83 /* Scoped attribute name representation. */
85 struct scoped_attributes
88 vec
<attribute_spec
> attributes
;
89 hash_table
<attribute_hasher
> *attribute_hash
;
90 /* True if we should not warn about unknown attributes in this NS. */
94 /* The table of scope attributes. */
95 static vec
<scoped_attributes
> attributes_table
;
97 static scoped_attributes
* find_attribute_namespace (const char*);
98 static void register_scoped_attribute (const struct attribute_spec
*,
100 static const struct attribute_spec
*lookup_scoped_attribute_spec (const_tree
,
103 static bool attributes_initialized
= false;
105 /* Default empty table of attributes. */
107 static const struct attribute_spec empty_attribute_table
[] =
109 { NULL
, 0, 0, false, false, false, false, NULL
, NULL
}
112 /* Return base name of the attribute. Ie '__attr__' is turned into 'attr'.
113 To avoid need for copying, we simply return length of the string. */
116 extract_attribute_substring (struct substring
*str
)
118 canonicalize_attr_name (str
->str
, str
->length
);
121 /* Insert an array of attributes ATTRIBUTES into a namespace. This
122 array must be NULL terminated. NS is the name of attribute
123 namespace. IGNORED_P is true iff all unknown attributes in this
124 namespace should be ignored for the purposes of -Wattributes. The
125 function returns the namespace into which the attributes have been
129 register_scoped_attributes (const struct attribute_spec
*attributes
,
130 const char *ns
, bool ignored_p
/*=false*/)
132 scoped_attributes
*result
= NULL
;
134 /* See if we already have attributes in the namespace NS. */
135 result
= find_attribute_namespace (ns
);
139 /* We don't have any namespace NS yet. Create one. */
140 scoped_attributes sa
;
142 if (attributes_table
.is_empty ())
143 attributes_table
.create (64);
145 memset (&sa
, 0, sizeof (sa
));
147 sa
.attributes
.create (64);
148 sa
.ignored_p
= ignored_p
;
149 result
= attributes_table
.safe_push (sa
);
150 result
->attribute_hash
= new hash_table
<attribute_hasher
> (200);
153 result
->ignored_p
|= ignored_p
;
155 /* Really add the attributes to their namespace now. */
156 for (unsigned i
= 0; attributes
[i
].name
!= NULL
; ++i
)
158 result
->attributes
.safe_push (attributes
[i
]);
159 register_scoped_attribute (&attributes
[i
], result
);
162 gcc_assert (result
!= NULL
);
167 /* Return the namespace which name is NS, NULL if none exist. */
169 static scoped_attributes
*
170 find_attribute_namespace (const char* ns
)
172 for (scoped_attributes
&iter
: attributes_table
)
176 && !strcmp (iter
.ns
, ns
)))
181 /* Make some sanity checks on the attribute tables. */
184 check_attribute_tables (void)
186 for (size_t i
= 0; i
< ARRAY_SIZE (attribute_tables
); i
++)
187 for (size_t j
= 0; attribute_tables
[i
][j
].name
!= NULL
; j
++)
189 /* The name must not begin and end with __. */
190 const char *name
= attribute_tables
[i
][j
].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_tables
[i
][j
].min_length
>= 0);
199 gcc_assert (attribute_tables
[i
][j
].max_length
== -1
200 || (attribute_tables
[i
][j
].max_length
201 >= attribute_tables
[i
][j
].min_length
));
203 /* An attribute cannot require both a DECL and a TYPE. */
204 gcc_assert (!attribute_tables
[i
][j
].decl_required
205 || !attribute_tables
[i
][j
].type_required
);
207 /* If an attribute requires a function type, in particular
208 it requires a type. */
209 gcc_assert (!attribute_tables
[i
][j
].function_type_required
210 || attribute_tables
[i
][j
].type_required
);
213 /* Check that each name occurs just once in each table. */
214 for (size_t i
= 0; i
< ARRAY_SIZE (attribute_tables
); i
++)
215 for (size_t j
= 0; attribute_tables
[i
][j
].name
!= NULL
; j
++)
216 for (size_t k
= j
+ 1; attribute_tables
[i
][k
].name
!= NULL
; k
++)
217 gcc_assert (strcmp (attribute_tables
[i
][j
].name
,
218 attribute_tables
[i
][k
].name
));
220 /* Check that no name occurs in more than one table. Names that
221 begin with '*' are exempt, and may be overridden. */
222 for (size_t i
= 0; i
< ARRAY_SIZE (attribute_tables
); i
++)
223 for (size_t j
= i
+ 1; j
< ARRAY_SIZE (attribute_tables
); j
++)
224 for (size_t k
= 0; attribute_tables
[i
][k
].name
!= NULL
; k
++)
225 for (size_t l
= 0; attribute_tables
[j
][l
].name
!= NULL
; l
++)
226 gcc_assert (attribute_tables
[i
][k
].name
[0] == '*'
227 || strcmp (attribute_tables
[i
][k
].name
,
228 attribute_tables
[j
][l
].name
));
231 /* Used to stash pointers to allocated memory so that we can free them at
232 the end of parsing of all TUs. */
233 static vec
<attribute_spec
*> ignored_attributes_table
;
235 /* Parse arguments V of -Wno-attributes=.
239 This functions also registers the parsed attributes so that we don't
240 warn that we don't recognize them. */
243 handle_ignored_attributes_option (vec
<char *> *v
)
250 char *cln
= strstr (opt
, "::");
251 /* We don't accept '::attr'. */
252 if (cln
== nullptr || cln
== opt
)
254 error ("wrong argument to ignored attributes");
255 inform (input_location
, "valid format is %<ns::attr%> or %<ns::%>");
258 const char *vendor_start
= opt
;
259 ptrdiff_t vendor_len
= cln
- opt
;
260 const char *attr_start
= cln
+ 2;
261 /* This could really use rawmemchr :(. */
262 ptrdiff_t attr_len
= strchr (attr_start
, '\0') - attr_start
;
263 /* Verify that they look valid. */
264 auto valid_p
= [](const char *const s
, ptrdiff_t len
) {
267 for (int i
= 0; i
< len
; ++i
)
270 else if (s
[i
] != '_')
275 if (!valid_p (vendor_start
, vendor_len
))
277 error ("wrong argument to ignored attributes");
280 canonicalize_attr_name (vendor_start
, vendor_len
);
281 /* We perform all this hijinks so that we don't have to copy OPT. */
282 tree vendor_id
= get_identifier_with_length (vendor_start
, vendor_len
);
284 /* In the "vendor::" case, we should ignore *any* attribute coming
285 from this attribute namespace. */
288 if (!valid_p (attr_start
, attr_len
))
290 error ("wrong argument to ignored attributes");
293 canonicalize_attr_name (attr_start
, attr_len
);
294 tree attr_id
= get_identifier_with_length (attr_start
, attr_len
);
295 attr
= IDENTIFIER_POINTER (attr_id
);
296 /* If we've already seen this vendor::attr, ignore it. Attempting to
297 register it twice would lead to a crash. */
298 if (lookup_scoped_attribute_spec (vendor_id
, attr_id
))
303 /* Create a table with extra attributes which we will register.
304 We can't free it here, so squirrel away the pointers. */
305 attribute_spec
*table
= new attribute_spec
[2];
306 ignored_attributes_table
.safe_push (table
);
307 table
[0] = { attr
, 0, -2, false, false, false, false, nullptr, nullptr };
308 table
[1] = { nullptr, 0, 0, false, false, false, false, nullptr,
310 register_scoped_attributes (table
, IDENTIFIER_POINTER (vendor_id
), !attr
);
314 /* Free data we might have allocated when adding extra attributes. */
319 for (auto x
: ignored_attributes_table
)
321 ignored_attributes_table
.release ();
324 /* Initialize attribute tables, and make some sanity checks if checking is
328 init_attributes (void)
332 if (attributes_initialized
)
335 attribute_tables
[0] = lang_hooks
.common_attribute_table
;
336 attribute_tables
[1] = lang_hooks
.attribute_table
;
337 attribute_tables
[2] = lang_hooks
.format_attribute_table
;
338 attribute_tables
[3] = targetm
.attribute_table
;
340 /* Translate NULL pointers to pointers to the empty table. */
341 for (i
= 0; i
< ARRAY_SIZE (attribute_tables
); i
++)
342 if (attribute_tables
[i
] == NULL
)
343 attribute_tables
[i
] = empty_attribute_table
;
346 check_attribute_tables ();
348 for (i
= 0; i
< ARRAY_SIZE (attribute_tables
); ++i
)
349 /* Put all the GNU attributes into the "gnu" namespace. */
350 register_scoped_attributes (attribute_tables
[i
], "gnu");
352 vec
<char *> *ignored
= (vec
<char *> *) flag_ignored_attributes
;
353 handle_ignored_attributes_option (ignored
);
355 invoke_plugin_callbacks (PLUGIN_ATTRIBUTES
, NULL
);
356 attributes_initialized
= true;
359 /* Insert a single ATTR into the attribute table. */
362 register_attribute (const struct attribute_spec
*attr
)
364 register_scoped_attribute (attr
, find_attribute_namespace ("gnu"));
367 /* Insert a single attribute ATTR into a namespace of attributes. */
370 register_scoped_attribute (const struct attribute_spec
*attr
,
371 scoped_attributes
*name_space
)
373 struct substring str
;
374 attribute_spec
**slot
;
376 gcc_assert (attr
!= NULL
&& name_space
!= NULL
);
378 gcc_assert (name_space
->attribute_hash
);
380 str
.str
= attr
->name
;
381 str
.length
= strlen (str
.str
);
383 /* Attribute names in the table must be in the form 'text' and not
384 in the form '__text__'. */
385 gcc_checking_assert (!canonicalize_attr_name (str
.str
, str
.length
));
387 slot
= name_space
->attribute_hash
388 ->find_slot_with_hash (&str
, substring_hash (str
.str
, str
.length
),
390 gcc_assert (!*slot
|| attr
->name
[0] == '*');
391 *slot
= CONST_CAST (struct attribute_spec
*, attr
);
394 /* Return the spec for the scoped attribute with namespace NS and
397 static const struct attribute_spec
*
398 lookup_scoped_attribute_spec (const_tree ns
, const_tree name
)
400 struct substring attr
;
401 scoped_attributes
*attrs
;
403 const char *ns_str
= (ns
!= NULL_TREE
) ? IDENTIFIER_POINTER (ns
): NULL
;
405 attrs
= find_attribute_namespace (ns_str
);
410 attr
.str
= IDENTIFIER_POINTER (name
);
411 attr
.length
= IDENTIFIER_LENGTH (name
);
412 extract_attribute_substring (&attr
);
413 return attrs
->attribute_hash
->find_with_hash (&attr
,
414 substring_hash (attr
.str
,
418 /* Return the spec for the attribute named NAME. If NAME is a TREE_LIST,
419 it also specifies the attribute namespace. */
421 const struct attribute_spec
*
422 lookup_attribute_spec (const_tree name
)
425 if (TREE_CODE (name
) == TREE_LIST
)
427 ns
= TREE_PURPOSE (name
);
428 name
= TREE_VALUE (name
);
431 ns
= get_identifier ("gnu");
432 return lookup_scoped_attribute_spec (ns
, name
);
436 /* Return the namespace of the attribute ATTR. This accessor works on
437 GNU and C++11 (scoped) attributes. On GNU attributes,
438 it returns an identifier tree for the string "gnu".
440 Please read the comments of cxx11_attribute_p to understand the
441 format of attributes. */
444 get_attribute_namespace (const_tree attr
)
446 if (cxx11_attribute_p (attr
))
447 return TREE_PURPOSE (TREE_PURPOSE (attr
));
448 return get_identifier ("gnu");
451 /* Check LAST_DECL and NODE of the same symbol for attributes that are
452 recorded in SPEC to be mutually exclusive with ATTRNAME, diagnose
453 them, and return true if any have been found. NODE can be a DECL
457 diag_attr_exclusions (tree last_decl
, tree node
, tree attrname
,
458 const attribute_spec
*spec
)
460 const attribute_spec::exclusions
*excl
= spec
->exclude
;
462 tree_code code
= TREE_CODE (node
);
464 if ((code
== FUNCTION_DECL
&& !excl
->function
465 && (!excl
->type
|| !spec
->affects_type_identity
))
466 || (code
== VAR_DECL
&& !excl
->variable
467 && (!excl
->type
|| !spec
->affects_type_identity
))
468 || (((code
== TYPE_DECL
|| RECORD_OR_UNION_TYPE_P (node
)) && !excl
->type
)))
471 /* True if an attribute that's mutually exclusive with ATTRNAME
475 if (last_decl
&& last_decl
!= node
&& TREE_TYPE (last_decl
) != node
)
477 /* Check both the last DECL and its type for conflicts with
478 the attribute being added to the current decl or type. */
479 found
|= diag_attr_exclusions (last_decl
, last_decl
, attrname
, spec
);
480 tree decl_type
= TREE_TYPE (last_decl
);
481 found
|= diag_attr_exclusions (last_decl
, decl_type
, attrname
, spec
);
484 /* NODE is either the current DECL to which the attribute is being
485 applied or its TYPE. For the former, consider the attributes on
486 both the DECL and its type. */
491 attrs
[0] = DECL_ATTRIBUTES (node
);
492 attrs
[1] = TYPE_ATTRIBUTES (TREE_TYPE (node
));
496 attrs
[0] = TYPE_ATTRIBUTES (node
);
497 attrs
[1] = NULL_TREE
;
500 /* Iterate over the mutually exclusive attribute names and verify
501 that the symbol doesn't contain it. */
502 for (unsigned i
= 0; i
!= sizeof attrs
/ sizeof *attrs
; ++i
)
507 for ( ; excl
->name
; ++excl
)
509 /* Avoid checking the attribute against itself. */
510 if (is_attribute_p (excl
->name
, attrname
))
513 if (!lookup_attribute (excl
->name
, attrs
[i
]))
516 /* An exclusion may apply either to a function declaration,
517 type declaration, or a field/variable declaration, or
518 any subset of the three. */
519 if (TREE_CODE (node
) == FUNCTION_DECL
523 if (TREE_CODE (node
) == TYPE_DECL
527 if ((TREE_CODE (node
) == FIELD_DECL
528 || TREE_CODE (node
) == VAR_DECL
)
535 bool note
= last_decl
!= NULL_TREE
;
536 auto_diagnostic_group d
;
537 if (TREE_CODE (node
) == FUNCTION_DECL
538 && fndecl_built_in_p (node
))
539 note
&= warning (OPT_Wattributes
,
540 "ignoring attribute %qE in declaration of "
541 "a built-in function %qD because it conflicts "
542 "with attribute %qs",
543 attrname
, node
, excl
->name
);
545 note
&= warning (OPT_Wattributes
,
546 "ignoring attribute %qE because "
547 "it conflicts with attribute %qs",
548 attrname
, excl
->name
);
551 inform (DECL_SOURCE_LOCATION (last_decl
),
552 "previous declaration here");
559 /* Return true iff we should not complain about unknown attributes
560 coming from the attribute namespace NS. This is the case for
561 the -Wno-attributes=ns:: command-line option. */
564 attr_namespace_ignored_p (tree ns
)
568 scoped_attributes
*r
= find_attribute_namespace (IDENTIFIER_POINTER (ns
));
569 return r
&& r
->ignored_p
;
572 /* Return true if the attribute ATTR should not be warned about. */
575 attribute_ignored_p (tree attr
)
577 if (!cxx11_attribute_p (attr
))
579 if (tree ns
= get_attribute_namespace (attr
))
581 if (attr_namespace_ignored_p (ns
))
583 const attribute_spec
*as
= lookup_attribute_spec (TREE_PURPOSE (attr
));
584 if (as
&& as
->max_length
== -2)
590 /* Like above, but takes an attribute_spec AS, which must be nonnull. */
593 attribute_ignored_p (const attribute_spec
*const as
)
595 return as
->max_length
== -2;
598 /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
599 which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
600 it should be modified in place; if a TYPE, a copy should be created
601 unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
602 information, in the form of a bitwise OR of flags in enum attribute_flags
603 from tree.h. Depending on these flags, some attributes may be
604 returned to be applied at a later stage (for example, to apply
605 a decl attribute to the declaration rather than to its type). */
608 decl_attributes (tree
*node
, tree attributes
, int flags
,
609 tree last_decl
/* = NULL_TREE */)
611 tree returned_attrs
= NULL_TREE
;
613 if (TREE_TYPE (*node
) == error_mark_node
|| attributes
== error_mark_node
)
616 if (!attributes_initialized
)
619 /* If this is a function and the user used #pragma GCC optimize, add the
620 options to the attribute((optimize(...))) list. */
621 if (TREE_CODE (*node
) == FUNCTION_DECL
&& current_optimize_pragma
)
623 tree cur_attr
= lookup_attribute ("optimize", attributes
);
624 tree opts
= copy_list (current_optimize_pragma
);
628 = tree_cons (get_identifier ("optimize"), opts
, attributes
);
630 TREE_VALUE (cur_attr
) = chainon (opts
, TREE_VALUE (cur_attr
));
633 if (TREE_CODE (*node
) == FUNCTION_DECL
634 && (optimization_current_node
!= optimization_default_node
635 || target_option_current_node
!= target_option_default_node
)
636 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node
))
638 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node
) = optimization_current_node
;
639 /* Don't set DECL_FUNCTION_SPECIFIC_TARGET for targets that don't
640 support #pragma GCC target or target attribute. */
641 if (target_option_default_node
)
644 = build_target_option_node (&global_options
, &global_options_set
);
645 tree old_tree
= DECL_FUNCTION_SPECIFIC_TARGET (*node
);
647 old_tree
= target_option_default_node
;
648 /* The changes on optimization options can cause the changes in
649 target options, update it accordingly if it's changed. */
650 if (old_tree
!= cur_tree
)
651 DECL_FUNCTION_SPECIFIC_TARGET (*node
) = cur_tree
;
655 /* If this is a function and the user used #pragma GCC target, add the
656 options to the attribute((target(...))) list. */
657 if (TREE_CODE (*node
) == FUNCTION_DECL
658 && current_target_pragma
659 && targetm
.target_option
.valid_attribute_p (*node
, NULL_TREE
,
660 current_target_pragma
, 0))
662 tree cur_attr
= lookup_attribute ("target", attributes
);
663 tree opts
= copy_list (current_target_pragma
);
666 attributes
= tree_cons (get_identifier ("target"), opts
, attributes
);
668 TREE_VALUE (cur_attr
) = chainon (opts
, TREE_VALUE (cur_attr
));
671 /* A "naked" function attribute implies "noinline" and "noclone" for
672 those targets that support it. */
673 if (TREE_CODE (*node
) == FUNCTION_DECL
675 && lookup_attribute ("naked", attributes
) != NULL
676 && lookup_attribute_spec (get_identifier ("naked"))
677 && lookup_attribute ("noipa", attributes
) == NULL
)
678 attributes
= tree_cons (get_identifier ("noipa"), NULL
, attributes
);
680 /* A "noipa" function attribute implies "noinline", "noclone" and "no_icf"
681 for those targets that support it. */
682 if (TREE_CODE (*node
) == FUNCTION_DECL
684 && lookup_attribute ("noipa", attributes
) != NULL
685 && lookup_attribute_spec (get_identifier ("noipa")))
687 if (lookup_attribute ("noinline", attributes
) == NULL
)
688 attributes
= tree_cons (get_identifier ("noinline"), NULL
, attributes
);
690 if (lookup_attribute ("noclone", attributes
) == NULL
)
691 attributes
= tree_cons (get_identifier ("noclone"), NULL
, attributes
);
693 if (lookup_attribute ("no_icf", attributes
) == NULL
)
694 attributes
= tree_cons (get_identifier ("no_icf"), NULL
, attributes
);
697 targetm
.insert_attributes (*node
, &attributes
);
699 /* Note that attributes on the same declaration are not necessarily
700 in the same order as in the source. */
701 for (tree attr
= attributes
; attr
; attr
= TREE_CHAIN (attr
))
703 tree ns
= get_attribute_namespace (attr
);
704 tree name
= get_attribute_name (attr
);
705 tree args
= TREE_VALUE (attr
);
707 const struct attribute_spec
*spec
708 = lookup_scoped_attribute_spec (ns
, name
);
709 int fn_ptr_quals
= 0;
710 tree fn_ptr_tmp
= NULL_TREE
;
711 const bool cxx11_attr_p
= cxx11_attribute_p (attr
);
715 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
)
716 && !attr_namespace_ignored_p (ns
))
718 if (ns
== NULL_TREE
|| !cxx11_attr_p
)
719 warning (OPT_Wattributes
, "%qE attribute directive ignored",
722 warning (OPT_Wattributes
,
723 "%<%E::%E%> scoped attribute directive ignored",
730 int nargs
= list_length (args
);
731 if (nargs
< spec
->min_length
732 || (spec
->max_length
>= 0
733 && nargs
> spec
->max_length
))
735 error ("wrong number of arguments specified for %qE attribute",
737 if (spec
->max_length
< 0)
738 inform (input_location
, "expected %i or more, found %i",
739 spec
->min_length
, nargs
);
741 inform (input_location
, "expected between %i and %i, found %i",
742 spec
->min_length
, spec
->max_length
, nargs
);
746 gcc_assert (is_attribute_p (spec
->name
, name
));
748 if (spec
->decl_required
&& !DECL_P (*anode
))
750 if (flags
& ((int) ATTR_FLAG_DECL_NEXT
751 | (int) ATTR_FLAG_FUNCTION_NEXT
752 | (int) ATTR_FLAG_ARRAY_NEXT
))
754 /* Pass on this attribute to be tried again. */
755 tree attr
= tree_cons (name
, args
, NULL_TREE
);
756 returned_attrs
= chainon (returned_attrs
, attr
);
761 warning (OPT_Wattributes
, "%qE attribute does not apply to types",
767 /* If we require a type, but were passed a decl, set up to make a
768 new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE
769 would have applied if we'd been passed a type, but we cannot modify
770 the decl's type in place here. */
771 if (spec
->type_required
&& DECL_P (*anode
))
773 anode
= &TREE_TYPE (*anode
);
774 flags
&= ~(int) ATTR_FLAG_TYPE_IN_PLACE
;
777 if (spec
->function_type_required
&& TREE_CODE (*anode
) != FUNCTION_TYPE
778 && TREE_CODE (*anode
) != METHOD_TYPE
)
780 if (TREE_CODE (*anode
) == POINTER_TYPE
781 && (TREE_CODE (TREE_TYPE (*anode
)) == FUNCTION_TYPE
782 || TREE_CODE (TREE_TYPE (*anode
)) == METHOD_TYPE
))
784 /* OK, this is a bit convoluted. We can't just make a copy
785 of the pointer type and modify its TREE_TYPE, because if
786 we change the attributes of the target type the pointer
787 type needs to have a different TYPE_MAIN_VARIANT. So we
788 pull out the target type now, frob it as appropriate, and
789 rebuild the pointer type later.
791 This would all be simpler if attributes were part of the
792 declarator, grumble grumble. */
793 fn_ptr_tmp
= TREE_TYPE (*anode
);
794 fn_ptr_quals
= TYPE_QUALS (*anode
);
796 flags
&= ~(int) ATTR_FLAG_TYPE_IN_PLACE
;
798 else if (flags
& (int) ATTR_FLAG_FUNCTION_NEXT
)
800 /* Pass on this attribute to be tried again. */
801 tree attr
= tree_cons (name
, args
, NULL_TREE
);
802 returned_attrs
= chainon (returned_attrs
, attr
);
806 if (TREE_CODE (*anode
) != FUNCTION_TYPE
807 && TREE_CODE (*anode
) != METHOD_TYPE
)
809 warning (OPT_Wattributes
,
810 "%qE attribute only applies to function types",
817 && (flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
)
818 && TYPE_SIZE (*anode
) != NULL_TREE
)
820 warning (OPT_Wattributes
, "type attributes ignored after type is already defined");
824 bool no_add_attrs
= false;
826 /* Check for exclusions with other attributes on the current
827 declation as well as the last declaration of the same
828 symbol already processed (if one exists). Detect and
829 reject incompatible attributes. */
830 bool built_in
= flags
& ATTR_FLAG_BUILT_IN
;
832 && (flag_checking
|| !built_in
)
833 && !error_operand_p (last_decl
))
835 /* Always check attributes on user-defined functions.
836 Check them on built-ins only when -fchecking is set.
837 Ignore __builtin_unreachable -- it's both const and
842 || DECL_BUILT_IN_CLASS (*anode
) != BUILT_IN_NORMAL
843 || (DECL_FUNCTION_CODE (*anode
) != BUILT_IN_UNREACHABLE
844 && (DECL_FUNCTION_CODE (*anode
)
845 != BUILT_IN_UBSAN_HANDLE_BUILTIN_UNREACHABLE
)))
847 bool no_add
= diag_attr_exclusions (last_decl
, *anode
, name
, spec
);
848 if (!no_add
&& anode
!= node
)
849 no_add
= diag_attr_exclusions (last_decl
, *node
, name
, spec
);
850 no_add_attrs
|= no_add
;
857 if (spec
->handler
!= NULL
)
859 int cxx11_flag
= (cxx11_attr_p
? ATTR_FLAG_CXX11
: 0);
861 /* Pass in an array of the current declaration followed
862 by the last pushed/merged declaration if one exists.
863 For calls that modify the type attributes of a DECL
864 and for which *ANODE is *NODE's type, also pass in
865 the DECL as the third element to use in diagnostics.
866 If the handler changes CUR_AND_LAST_DECL[0] replace
867 *ANODE with its value. */
868 tree cur_and_last_decl
[3] = { *anode
, last_decl
};
869 if (anode
!= node
&& DECL_P (*node
))
870 cur_and_last_decl
[2] = *node
;
872 tree ret
= (spec
->handler
) (cur_and_last_decl
, name
, args
,
873 flags
|cxx11_flag
, &no_add_attrs
);
875 *anode
= cur_and_last_decl
[0];
876 if (ret
== error_mark_node
)
878 warning (OPT_Wattributes
, "%qE attribute ignored", name
);
882 returned_attrs
= chainon (ret
, returned_attrs
);
885 /* Layout the decl in case anything changed. */
886 if (spec
->type_required
&& DECL_P (*node
)
888 || TREE_CODE (*node
) == PARM_DECL
889 || TREE_CODE (*node
) == RESULT_DECL
))
890 relayout_decl (*node
);
898 old_attrs
= DECL_ATTRIBUTES (*anode
);
900 old_attrs
= TYPE_ATTRIBUTES (*anode
);
902 for (a
= lookup_attribute (spec
->name
, old_attrs
);
904 a
= lookup_attribute (spec
->name
, TREE_CHAIN (a
)))
906 if (simple_cst_equal (TREE_VALUE (a
), args
) == 1)
912 /* This attribute isn't already in the list. */
914 /* Preserve the C++11 form. */
916 r
= tree_cons (build_tree_list (ns
, name
), args
, old_attrs
);
918 r
= tree_cons (name
, args
, old_attrs
);
921 DECL_ATTRIBUTES (*anode
) = r
;
922 else if (flags
& (int) ATTR_FLAG_TYPE_IN_PLACE
)
924 TYPE_ATTRIBUTES (*anode
) = r
;
925 /* If this is the main variant, also push the attributes
926 out to the other variants. */
927 if (*anode
== TYPE_MAIN_VARIANT (*anode
))
929 for (tree variant
= *anode
; variant
;
930 variant
= TYPE_NEXT_VARIANT (variant
))
932 if (TYPE_ATTRIBUTES (variant
) == old_attrs
)
933 TYPE_ATTRIBUTES (variant
)
934 = TYPE_ATTRIBUTES (*anode
);
935 else if (!lookup_attribute
936 (spec
->name
, TYPE_ATTRIBUTES (variant
)))
937 TYPE_ATTRIBUTES (variant
) = tree_cons
938 (name
, args
, TYPE_ATTRIBUTES (variant
));
943 *anode
= build_type_attribute_variant (*anode
, r
);
949 /* Rebuild the function pointer type and put it in the
950 appropriate place. */
951 fn_ptr_tmp
= build_pointer_type (fn_ptr_tmp
);
953 fn_ptr_tmp
= build_qualified_type (fn_ptr_tmp
, fn_ptr_quals
);
955 TREE_TYPE (*node
) = fn_ptr_tmp
;
958 gcc_assert (TREE_CODE (*node
) == POINTER_TYPE
);
964 return returned_attrs
;
967 /* Return TRUE iff ATTR has been parsed by the front-end as a C++-11
970 When G++ parses a C++11 attribute, it is represented as
971 a TREE_LIST which TREE_PURPOSE is itself a TREE_LIST. TREE_PURPOSE
972 (TREE_PURPOSE (ATTR)) is the namespace of the attribute, and the
973 TREE_VALUE (TREE_PURPOSE (ATTR)) is its non-qualified name. Please
974 use get_attribute_namespace and get_attribute_name to retrieve the
975 namespace and name of the attribute, as these accessors work with
976 GNU attributes as well. */
979 cxx11_attribute_p (const_tree attr
)
981 if (attr
== NULL_TREE
982 || TREE_CODE (attr
) != TREE_LIST
)
985 return (TREE_CODE (TREE_PURPOSE (attr
)) == TREE_LIST
);
988 /* Return the name of the attribute ATTR. This accessor works on GNU
989 and C++11 (scoped) attributes.
991 Please read the comments of cxx11_attribute_p to understand the
992 format of attributes. */
995 get_attribute_name (const_tree attr
)
997 if (cxx11_attribute_p (attr
))
998 return TREE_VALUE (TREE_PURPOSE (attr
));
999 return TREE_PURPOSE (attr
);
1002 /* Subroutine of set_method_tm_attributes. Apply TM attribute ATTR
1003 to the method FNDECL. */
1006 apply_tm_attr (tree fndecl
, tree attr
)
1008 decl_attributes (&TREE_TYPE (fndecl
), tree_cons (attr
, NULL
, NULL
), 0);
1011 /* Makes a function attribute of the form NAME(ARG_NAME) and chains
1015 make_attribute (const char *name
, const char *arg_name
, tree chain
)
1022 attr_name
= get_identifier (name
);
1023 attr_arg_name
= build_string (strlen (arg_name
), arg_name
);
1024 attr_args
= tree_cons (NULL_TREE
, attr_arg_name
, NULL_TREE
);
1025 attr
= tree_cons (attr_name
, attr_args
, chain
);
1030 /* Common functions used for target clone support. */
1032 /* Comparator function to be used in qsort routine to sort attribute
1033 specification strings to "target". */
1036 attr_strcmp (const void *v1
, const void *v2
)
1038 const char *c1
= *(char *const*)v1
;
1039 const char *c2
= *(char *const*)v2
;
1040 return strcmp (c1
, c2
);
1043 /* ARGLIST is the argument to target attribute. This function tokenizes
1044 the comma separated arguments, sorts them and returns a string which
1045 is a unique identifier for the comma separated arguments. It also
1046 replaces non-identifier characters "=,-" with "_". */
1049 sorted_attr_string (tree arglist
)
1052 size_t str_len_sum
= 0;
1054 char *attr_str
, *ret_str
;
1056 unsigned int argnum
= 1;
1059 for (arg
= arglist
; arg
; arg
= TREE_CHAIN (arg
))
1061 const char *str
= TREE_STRING_POINTER (TREE_VALUE (arg
));
1062 size_t len
= strlen (str
);
1063 str_len_sum
+= len
+ 1;
1066 for (i
= 0; i
< strlen (str
); i
++)
1071 attr_str
= XNEWVEC (char, str_len_sum
);
1073 for (arg
= arglist
; arg
; arg
= TREE_CHAIN (arg
))
1075 const char *str
= TREE_STRING_POINTER (TREE_VALUE (arg
));
1076 size_t len
= strlen (str
);
1077 memcpy (attr_str
+ str_len_sum
, str
, len
);
1078 attr_str
[str_len_sum
+ len
] = TREE_CHAIN (arg
) ? ',' : '\0';
1079 str_len_sum
+= len
+ 1;
1082 /* Replace "=,-" with "_". */
1083 for (i
= 0; i
< strlen (attr_str
); i
++)
1084 if (attr_str
[i
] == '=' || attr_str
[i
]== '-')
1090 args
= XNEWVEC (char *, argnum
);
1093 attr
= strtok (attr_str
, ",");
1094 while (attr
!= NULL
)
1098 attr
= strtok (NULL
, ",");
1101 qsort (args
, argnum
, sizeof (char *), attr_strcmp
);
1103 ret_str
= XNEWVEC (char, str_len_sum
);
1105 for (i
= 0; i
< argnum
; i
++)
1107 size_t len
= strlen (args
[i
]);
1108 memcpy (ret_str
+ str_len_sum
, args
[i
], len
);
1109 ret_str
[str_len_sum
+ len
] = i
< argnum
- 1 ? '_' : '\0';
1110 str_len_sum
+= len
+ 1;
1114 XDELETEVEC (attr_str
);
1119 /* This function returns true if FN1 and FN2 are versions of the same function,
1120 that is, the target strings of the function decls are different. This assumes
1121 that FN1 and FN2 have the same signature. */
1124 common_function_versions (tree fn1
, tree fn2
)
1127 char *target1
, *target2
;
1130 if (TREE_CODE (fn1
) != FUNCTION_DECL
1131 || TREE_CODE (fn2
) != FUNCTION_DECL
)
1134 attr1
= lookup_attribute ("target", DECL_ATTRIBUTES (fn1
));
1135 attr2
= lookup_attribute ("target", DECL_ATTRIBUTES (fn2
));
1137 /* At least one function decl should have the target attribute specified. */
1138 if (attr1
== NULL_TREE
&& attr2
== NULL_TREE
)
1141 /* Diagnose missing target attribute if one of the decls is already
1143 if (attr1
== NULL_TREE
|| attr2
== NULL_TREE
)
1145 if (DECL_FUNCTION_VERSIONED (fn1
) || DECL_FUNCTION_VERSIONED (fn2
))
1147 if (attr2
!= NULL_TREE
)
1149 std::swap (fn1
, fn2
);
1152 error_at (DECL_SOURCE_LOCATION (fn2
),
1153 "missing %<target%> attribute for multi-versioned %qD",
1155 inform (DECL_SOURCE_LOCATION (fn1
),
1156 "previous declaration of %qD", fn1
);
1157 /* Prevent diagnosing of the same error multiple times. */
1158 DECL_ATTRIBUTES (fn2
)
1159 = tree_cons (get_identifier ("target"),
1160 copy_node (TREE_VALUE (attr1
)),
1161 DECL_ATTRIBUTES (fn2
));
1166 target1
= sorted_attr_string (TREE_VALUE (attr1
));
1167 target2
= sorted_attr_string (TREE_VALUE (attr2
));
1169 /* The sorted target strings must be different for fn1 and fn2
1171 if (strcmp (target1
, target2
) == 0)
1176 XDELETEVEC (target1
);
1177 XDELETEVEC (target2
);
1182 /* Make a dispatcher declaration for the multi-versioned function DECL.
1183 Calls to DECL function will be replaced with calls to the dispatcher
1184 by the front-end. Return the decl created. */
1187 make_dispatcher_decl (const tree decl
)
1191 tree fn_type
, func_type
;
1193 func_name
= xstrdup (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
)));
1195 fn_type
= TREE_TYPE (decl
);
1196 func_type
= build_function_type (TREE_TYPE (fn_type
),
1197 TYPE_ARG_TYPES (fn_type
));
1199 func_decl
= build_fn_decl (func_name
, func_type
);
1200 XDELETEVEC (func_name
);
1201 TREE_USED (func_decl
) = 1;
1202 DECL_CONTEXT (func_decl
) = NULL_TREE
;
1203 DECL_INITIAL (func_decl
) = error_mark_node
;
1204 DECL_ARTIFICIAL (func_decl
) = 1;
1205 /* Mark this func as external, the resolver will flip it again if
1206 it gets generated. */
1207 DECL_EXTERNAL (func_decl
) = 1;
1208 /* This will be of type IFUNCs have to be externally visible. */
1209 TREE_PUBLIC (func_decl
) = 1;
1214 /* Returns true if decl is multi-versioned and DECL is the default function,
1215 that is it is not tagged with target specific optimization. */
1218 is_function_default_version (const tree decl
)
1220 if (TREE_CODE (decl
) != FUNCTION_DECL
1221 || !DECL_FUNCTION_VERSIONED (decl
))
1223 tree attr
= lookup_attribute ("target", DECL_ATTRIBUTES (decl
));
1225 attr
= TREE_VALUE (TREE_VALUE (attr
));
1226 return (TREE_CODE (attr
) == STRING_CST
1227 && strcmp (TREE_STRING_POINTER (attr
), "default") == 0);
1230 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
1234 build_decl_attribute_variant (tree ddecl
, tree attribute
)
1236 DECL_ATTRIBUTES (ddecl
) = attribute
;
1240 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
1241 is ATTRIBUTE and its qualifiers are QUALS.
1243 Record such modified types already made so we don't make duplicates. */
1246 build_type_attribute_qual_variant (tree otype
, tree attribute
, int quals
)
1249 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype
), attribute
))
1253 /* Building a distinct copy of a tagged type is inappropriate; it
1254 causes breakage in code that expects there to be a one-to-one
1255 relationship between a struct and its fields.
1256 build_duplicate_type is another solution (as used in
1257 handle_transparent_union_attribute), but that doesn't play well
1258 with the stronger C++ type identity model. */
1259 if (TREE_CODE (ttype
) == RECORD_TYPE
1260 || TREE_CODE (ttype
) == UNION_TYPE
1261 || TREE_CODE (ttype
) == QUAL_UNION_TYPE
1262 || TREE_CODE (ttype
) == ENUMERAL_TYPE
)
1264 warning (OPT_Wattributes
,
1265 "ignoring attributes applied to %qT after definition",
1266 TYPE_MAIN_VARIANT (ttype
));
1267 return build_qualified_type (ttype
, quals
);
1270 ttype
= build_qualified_type (ttype
, TYPE_UNQUALIFIED
);
1271 if (lang_hooks
.types
.copy_lang_qualifiers
1272 && otype
!= TYPE_MAIN_VARIANT (otype
))
1273 ttype
= (lang_hooks
.types
.copy_lang_qualifiers
1274 (ttype
, TYPE_MAIN_VARIANT (otype
)));
1276 tree dtype
= ntype
= build_distinct_type_copy (ttype
);
1278 TYPE_ATTRIBUTES (ntype
) = attribute
;
1280 hashval_t hash
= type_hash_canon_hash (ntype
);
1281 ntype
= type_hash_canon (hash
, ntype
);
1284 /* This variant was already in the hash table, don't mess with
1286 else if (TYPE_STRUCTURAL_EQUALITY_P (ttype
)
1287 || !comp_type_attributes (ntype
, ttype
))
1288 /* If the target-dependent attributes make NTYPE different from
1289 its canonical type, we will need to use structural equality
1290 checks for this type.
1292 We shouldn't get here for stripping attributes from a type;
1293 the no-attribute type might not need structural comparison. But
1294 we can if was discarded from type_hash_table. */
1295 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
1296 else if (TYPE_CANONICAL (ntype
) == ntype
)
1297 TYPE_CANONICAL (ntype
) = TYPE_CANONICAL (ttype
);
1299 ttype
= build_qualified_type (ntype
, quals
);
1300 if (lang_hooks
.types
.copy_lang_qualifiers
1301 && otype
!= TYPE_MAIN_VARIANT (otype
))
1302 ttype
= lang_hooks
.types
.copy_lang_qualifiers (ttype
, otype
);
1304 else if (TYPE_QUALS (ttype
) != quals
)
1305 ttype
= build_qualified_type (ttype
, quals
);
1310 /* Compare two identifier nodes representing attributes.
1311 Return true if they are the same, false otherwise. */
1314 cmp_attrib_identifiers (const_tree attr1
, const_tree attr2
)
1316 /* Make sure we're dealing with IDENTIFIER_NODEs. */
1317 gcc_checking_assert (TREE_CODE (attr1
) == IDENTIFIER_NODE
1318 && TREE_CODE (attr2
) == IDENTIFIER_NODE
);
1320 /* Identifiers can be compared directly for equality. */
1324 return cmp_attribs (IDENTIFIER_POINTER (attr1
), IDENTIFIER_LENGTH (attr1
),
1325 IDENTIFIER_POINTER (attr2
), IDENTIFIER_LENGTH (attr2
));
1328 /* Compare two constructor-element-type constants. Return 1 if the lists
1329 are known to be equal; otherwise return 0. */
1332 simple_cst_list_equal (const_tree l1
, const_tree l2
)
1334 while (l1
!= NULL_TREE
&& l2
!= NULL_TREE
)
1336 if (simple_cst_equal (TREE_VALUE (l1
), TREE_VALUE (l2
)) != 1)
1339 l1
= TREE_CHAIN (l1
);
1340 l2
= TREE_CHAIN (l2
);
1346 /* Check if "omp declare simd" attribute arguments, CLAUSES1 and CLAUSES2, are
1350 omp_declare_simd_clauses_equal (tree clauses1
, tree clauses2
)
1353 for (cl1
= clauses1
, cl2
= clauses2
;
1355 cl1
= OMP_CLAUSE_CHAIN (cl1
), cl2
= OMP_CLAUSE_CHAIN (cl2
))
1357 if (OMP_CLAUSE_CODE (cl1
) != OMP_CLAUSE_CODE (cl2
))
1359 if (OMP_CLAUSE_CODE (cl1
) != OMP_CLAUSE_SIMDLEN
)
1361 if (simple_cst_equal (OMP_CLAUSE_DECL (cl1
),
1362 OMP_CLAUSE_DECL (cl2
)) != 1)
1365 switch (OMP_CLAUSE_CODE (cl1
))
1367 case OMP_CLAUSE_ALIGNED
:
1368 if (simple_cst_equal (OMP_CLAUSE_ALIGNED_ALIGNMENT (cl1
),
1369 OMP_CLAUSE_ALIGNED_ALIGNMENT (cl2
)) != 1)
1372 case OMP_CLAUSE_LINEAR
:
1373 if (simple_cst_equal (OMP_CLAUSE_LINEAR_STEP (cl1
),
1374 OMP_CLAUSE_LINEAR_STEP (cl2
)) != 1)
1377 case OMP_CLAUSE_SIMDLEN
:
1378 if (simple_cst_equal (OMP_CLAUSE_SIMDLEN_EXPR (cl1
),
1379 OMP_CLAUSE_SIMDLEN_EXPR (cl2
)) != 1)
1389 /* Compare two attributes for their value identity. Return true if the
1390 attribute values are known to be equal; otherwise return false. */
1393 attribute_value_equal (const_tree attr1
, const_tree attr2
)
1395 if (TREE_VALUE (attr1
) == TREE_VALUE (attr2
))
1398 if (TREE_VALUE (attr1
) != NULL_TREE
1399 && TREE_CODE (TREE_VALUE (attr1
)) == TREE_LIST
1400 && TREE_VALUE (attr2
) != NULL_TREE
1401 && TREE_CODE (TREE_VALUE (attr2
)) == TREE_LIST
)
1403 /* Handle attribute format. */
1404 if (is_attribute_p ("format", get_attribute_name (attr1
)))
1406 attr1
= TREE_VALUE (attr1
);
1407 attr2
= TREE_VALUE (attr2
);
1408 /* Compare the archetypes (printf/scanf/strftime/...). */
1409 if (!cmp_attrib_identifiers (TREE_VALUE (attr1
), TREE_VALUE (attr2
)))
1411 /* Archetypes are the same. Compare the rest. */
1412 return (simple_cst_list_equal (TREE_CHAIN (attr1
),
1413 TREE_CHAIN (attr2
)) == 1);
1415 return (simple_cst_list_equal (TREE_VALUE (attr1
),
1416 TREE_VALUE (attr2
)) == 1);
1419 if (TREE_VALUE (attr1
)
1420 && TREE_CODE (TREE_VALUE (attr1
)) == OMP_CLAUSE
1421 && TREE_VALUE (attr2
)
1422 && TREE_CODE (TREE_VALUE (attr2
)) == OMP_CLAUSE
)
1423 return omp_declare_simd_clauses_equal (TREE_VALUE (attr1
),
1424 TREE_VALUE (attr2
));
1426 return (simple_cst_equal (TREE_VALUE (attr1
), TREE_VALUE (attr2
)) == 1);
1429 /* Return 0 if the attributes for two types are incompatible, 1 if they
1430 are compatible, and 2 if they are nearly compatible (which causes a
1431 warning to be generated). */
1433 comp_type_attributes (const_tree type1
, const_tree type2
)
1435 const_tree a1
= TYPE_ATTRIBUTES (type1
);
1436 const_tree a2
= TYPE_ATTRIBUTES (type2
);
1441 for (a
= a1
; a
!= NULL_TREE
; a
= TREE_CHAIN (a
))
1443 const struct attribute_spec
*as
;
1446 as
= lookup_attribute_spec (get_attribute_name (a
));
1447 if (!as
|| as
->affects_type_identity
== false)
1450 attr
= lookup_attribute (as
->name
, CONST_CAST_TREE (a2
));
1451 if (!attr
|| !attribute_value_equal (a
, attr
))
1456 for (a
= a2
; a
!= NULL_TREE
; a
= TREE_CHAIN (a
))
1458 const struct attribute_spec
*as
;
1460 as
= lookup_attribute_spec (get_attribute_name (a
));
1461 if (!as
|| as
->affects_type_identity
== false)
1464 if (!lookup_attribute (as
->name
, CONST_CAST_TREE (a1
)))
1466 /* We don't need to compare trees again, as we did this
1467 already in first loop. */
1469 /* All types - affecting identity - are equal, so
1470 there is no need to call target hook for comparison. */
1474 if (lookup_attribute ("transaction_safe", CONST_CAST_TREE (a
)))
1476 if ((lookup_attribute ("nocf_check", TYPE_ATTRIBUTES (type1
)) != NULL
)
1477 ^ (lookup_attribute ("nocf_check", TYPE_ATTRIBUTES (type2
)) != NULL
))
1479 /* As some type combinations - like default calling-convention - might
1480 be compatible, we have to call the target hook to get the final result. */
1481 return targetm
.comp_type_attributes (type1
, type2
);
1484 /* PREDICATE acts as a function of type:
1486 (const_tree attr, const attribute_spec *as) -> bool
1488 where ATTR is an attribute and AS is its possibly-null specification.
1489 Return a list of every attribute in attribute list ATTRS for which
1490 PREDICATE is true. Return ATTRS itself if PREDICATE returns true
1491 for every attribute. */
1493 template<typename Predicate
>
1495 remove_attributes_matching (tree attrs
, Predicate predicate
)
1497 tree new_attrs
= NULL_TREE
;
1498 tree
*ptr
= &new_attrs
;
1499 const_tree start
= attrs
;
1500 for (const_tree attr
= attrs
; attr
; attr
= TREE_CHAIN (attr
))
1502 tree name
= get_attribute_name (attr
);
1503 const attribute_spec
*as
= lookup_attribute_spec (name
);
1505 if (!predicate (attr
, as
))
1507 else if (start
== attrs
)
1510 end
= TREE_CHAIN (attr
);
1512 for (; start
!= end
; start
= TREE_CHAIN (start
))
1514 *ptr
= tree_cons (TREE_PURPOSE (start
),
1515 TREE_VALUE (start
), NULL_TREE
);
1516 TREE_CHAIN (*ptr
) = NULL_TREE
;
1517 ptr
= &TREE_CHAIN (*ptr
);
1519 start
= TREE_CHAIN (attr
);
1521 gcc_assert (!start
|| start
== attrs
);
1522 return start
? attrs
: new_attrs
;
1525 /* If VALUE is true, return the subset of ATTRS that affect type identity,
1526 otherwise return the subset of ATTRS that don't affect type identity. */
1529 affects_type_identity_attributes (tree attrs
, bool value
)
1531 auto predicate
= [value
](const_tree
, const attribute_spec
*as
) -> bool
1533 return bool (as
&& as
->affects_type_identity
) == value
;
1535 return remove_attributes_matching (attrs
, predicate
);
1538 /* Remove attributes that affect type identity from ATTRS unless the
1539 same attributes occur in OK_ATTRS. */
1542 restrict_type_identity_attributes_to (tree attrs
, tree ok_attrs
)
1544 auto predicate
= [ok_attrs
](const_tree attr
,
1545 const attribute_spec
*as
) -> bool
1547 if (!as
|| !as
->affects_type_identity
)
1550 for (tree ok_attr
= lookup_attribute (as
->name
, ok_attrs
);
1552 ok_attr
= lookup_attribute (as
->name
, TREE_CHAIN (ok_attr
)))
1553 if (simple_cst_equal (TREE_VALUE (ok_attr
), TREE_VALUE (attr
)) == 1)
1558 return remove_attributes_matching (attrs
, predicate
);
1561 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
1564 Record such modified types already made so we don't make duplicates. */
1567 build_type_attribute_variant (tree ttype
, tree attribute
)
1569 return build_type_attribute_qual_variant (ttype
, attribute
,
1570 TYPE_QUALS (ttype
));
1573 /* A variant of lookup_attribute() that can be used with an identifier
1574 as the first argument, and where the identifier can be either
1575 'text' or '__text__'.
1577 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
1578 return a pointer to the attribute's list element if the attribute
1579 is part of the list, or NULL_TREE if not found. If the attribute
1580 appears more than once, this only returns the first occurrence; the
1581 TREE_CHAIN of the return value should be passed back in if further
1582 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
1583 can be in the form 'text' or '__text__'. */
1585 lookup_ident_attribute (tree attr_identifier
, tree list
)
1587 gcc_checking_assert (TREE_CODE (attr_identifier
) == IDENTIFIER_NODE
);
1591 gcc_checking_assert (TREE_CODE (get_attribute_name (list
))
1592 == IDENTIFIER_NODE
);
1594 if (cmp_attrib_identifiers (attr_identifier
,
1595 get_attribute_name (list
)))
1598 list
= TREE_CHAIN (list
);
1604 /* Remove any instances of attribute ATTR_NAME in LIST and return the
1608 remove_attribute (const char *attr_name
, tree list
)
1611 gcc_checking_assert (attr_name
[0] != '_');
1613 for (p
= &list
; *p
;)
1617 tree attr
= get_attribute_name (l
);
1618 if (is_attribute_p (attr_name
, attr
))
1619 *p
= TREE_CHAIN (l
);
1621 p
= &TREE_CHAIN (l
);
1627 /* Return an attribute list that is the union of a1 and a2. */
1630 merge_attributes (tree a1
, tree a2
)
1634 /* Either one unset? Take the set one. */
1636 if ((attributes
= a1
) == 0)
1639 /* One that completely contains the other? Take it. */
1641 else if (a2
!= 0 && ! attribute_list_contained (a1
, a2
))
1643 if (attribute_list_contained (a2
, a1
))
1647 /* Pick the longest list, and hang on the other list. */
1649 if (list_length (a1
) < list_length (a2
))
1650 attributes
= a2
, a2
= a1
;
1652 for (; a2
!= 0; a2
= TREE_CHAIN (a2
))
1655 for (a
= lookup_ident_attribute (get_attribute_name (a2
),
1657 a
!= NULL_TREE
&& !attribute_value_equal (a
, a2
);
1658 a
= lookup_ident_attribute (get_attribute_name (a2
),
1663 a1
= copy_node (a2
);
1664 TREE_CHAIN (a1
) = attributes
;
1673 /* Given types T1 and T2, merge their attributes and return
1677 merge_type_attributes (tree t1
, tree t2
)
1679 return merge_attributes (TYPE_ATTRIBUTES (t1
),
1680 TYPE_ATTRIBUTES (t2
));
1683 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
1687 merge_decl_attributes (tree olddecl
, tree newdecl
)
1689 return merge_attributes (DECL_ATTRIBUTES (olddecl
),
1690 DECL_ATTRIBUTES (newdecl
));
1693 /* Duplicate all attributes with name NAME in ATTR list to *ATTRS if
1694 they are missing there. */
1697 duplicate_one_attribute (tree
*attrs
, tree attr
, const char *name
)
1699 attr
= lookup_attribute (name
, attr
);
1702 tree a
= lookup_attribute (name
, *attrs
);
1706 for (a2
= a
; a2
; a2
= lookup_attribute (name
, TREE_CHAIN (a2
)))
1707 if (attribute_value_equal (attr
, a2
))
1711 a2
= copy_node (attr
);
1712 TREE_CHAIN (a2
) = *attrs
;
1715 attr
= lookup_attribute (name
, TREE_CHAIN (attr
));
1719 /* Duplicate all attributes from user DECL to the corresponding
1720 builtin that should be propagated. */
1723 copy_attributes_to_builtin (tree decl
)
1725 tree b
= builtin_decl_explicit (DECL_FUNCTION_CODE (decl
));
1727 duplicate_one_attribute (&DECL_ATTRIBUTES (b
),
1728 DECL_ATTRIBUTES (decl
), "omp declare simd");
1731 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
1733 /* Specialization of merge_decl_attributes for various Windows targets.
1735 This handles the following situation:
1737 __declspec (dllimport) int foo;
1740 The second instance of `foo' nullifies the dllimport. */
1743 merge_dllimport_decl_attributes (tree old
, tree new_tree
)
1746 int delete_dllimport_p
= 1;
1748 /* What we need to do here is remove from `old' dllimport if it doesn't
1749 appear in `new'. dllimport behaves like extern: if a declaration is
1750 marked dllimport and a definition appears later, then the object
1751 is not dllimport'd. We also remove a `new' dllimport if the old list
1752 contains dllexport: dllexport always overrides dllimport, regardless
1753 of the order of declaration. */
1754 if (!VAR_OR_FUNCTION_DECL_P (new_tree
))
1755 delete_dllimport_p
= 0;
1756 else if (DECL_DLLIMPORT_P (new_tree
)
1757 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old
)))
1759 DECL_DLLIMPORT_P (new_tree
) = 0;
1760 warning (OPT_Wattributes
, "%q+D already declared with dllexport "
1761 "attribute: dllimport ignored", new_tree
);
1763 else if (DECL_DLLIMPORT_P (old
) && !DECL_DLLIMPORT_P (new_tree
))
1765 /* Warn about overriding a symbol that has already been used, e.g.:
1766 extern int __attribute__ ((dllimport)) foo;
1767 int* bar () {return &foo;}
1770 if (TREE_USED (old
))
1772 warning (0, "%q+D redeclared without dllimport attribute "
1773 "after being referenced with dll linkage", new_tree
);
1774 /* If we have used a variable's address with dllimport linkage,
1775 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
1776 decl may already have had TREE_CONSTANT computed.
1777 We still remove the attribute so that assembler code refers
1778 to '&foo rather than '_imp__foo'. */
1779 if (VAR_P (old
) && TREE_ADDRESSABLE (old
))
1780 DECL_DLLIMPORT_P (new_tree
) = 1;
1783 /* Let an inline definition silently override the external reference,
1784 but otherwise warn about attribute inconsistency. */
1785 else if (VAR_P (new_tree
) || !DECL_DECLARED_INLINE_P (new_tree
))
1786 warning (OPT_Wattributes
, "%q+D redeclared without dllimport "
1787 "attribute: previous dllimport ignored", new_tree
);
1790 delete_dllimport_p
= 0;
1792 a
= merge_attributes (DECL_ATTRIBUTES (old
), DECL_ATTRIBUTES (new_tree
));
1794 if (delete_dllimport_p
)
1795 a
= remove_attribute ("dllimport", a
);
1800 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
1801 struct attribute_spec.handler. */
1804 handle_dll_attribute (tree
* pnode
, tree name
, tree args
, int flags
,
1810 /* These attributes may apply to structure and union types being created,
1811 but otherwise should pass to the declaration involved. */
1814 if (flags
& ((int) ATTR_FLAG_DECL_NEXT
| (int) ATTR_FLAG_FUNCTION_NEXT
1815 | (int) ATTR_FLAG_ARRAY_NEXT
))
1817 *no_add_attrs
= true;
1818 return tree_cons (name
, args
, NULL_TREE
);
1820 if (TREE_CODE (node
) == RECORD_TYPE
1821 || TREE_CODE (node
) == UNION_TYPE
)
1823 node
= TYPE_NAME (node
);
1829 warning (OPT_Wattributes
, "%qE attribute ignored",
1831 *no_add_attrs
= true;
1836 if (!VAR_OR_FUNCTION_DECL_P (node
) && TREE_CODE (node
) != TYPE_DECL
)
1838 *no_add_attrs
= true;
1839 warning (OPT_Wattributes
, "%qE attribute ignored",
1844 if (TREE_CODE (node
) == TYPE_DECL
1845 && TREE_CODE (TREE_TYPE (node
)) != RECORD_TYPE
1846 && TREE_CODE (TREE_TYPE (node
)) != UNION_TYPE
)
1848 *no_add_attrs
= true;
1849 warning (OPT_Wattributes
, "%qE attribute ignored",
1854 is_dllimport
= is_attribute_p ("dllimport", name
);
1856 /* Report error on dllimport ambiguities seen now before they cause
1860 /* Honor any target-specific overrides. */
1861 if (!targetm
.valid_dllimport_attribute_p (node
))
1862 *no_add_attrs
= true;
1864 else if (TREE_CODE (node
) == FUNCTION_DECL
1865 && DECL_DECLARED_INLINE_P (node
))
1867 warning (OPT_Wattributes
, "inline function %q+D declared as "
1868 "dllimport: attribute ignored", node
);
1869 *no_add_attrs
= true;
1871 /* Like MS, treat definition of dllimported variables and
1872 non-inlined functions on declaration as syntax errors. */
1873 else if (TREE_CODE (node
) == FUNCTION_DECL
&& DECL_INITIAL (node
))
1875 error ("function %q+D definition is marked dllimport", node
);
1876 *no_add_attrs
= true;
1879 else if (VAR_P (node
))
1881 if (DECL_INITIAL (node
))
1883 error ("variable %q+D definition is marked dllimport",
1885 *no_add_attrs
= true;
1888 /* `extern' needn't be specified with dllimport.
1889 Specify `extern' now and hope for the best. Sigh. */
1890 DECL_EXTERNAL (node
) = 1;
1891 /* Also, implicitly give dllimport'd variables declared within
1892 a function global scope, unless declared static. */
1893 if (current_function_decl
!= NULL_TREE
&& !TREE_STATIC (node
))
1894 TREE_PUBLIC (node
) = 1;
1895 /* Clear TREE_STATIC because DECL_EXTERNAL is set, unless
1896 it is a C++ static data member. */
1897 if (DECL_CONTEXT (node
) == NULL_TREE
1898 || !RECORD_OR_UNION_TYPE_P (DECL_CONTEXT (node
)))
1899 TREE_STATIC (node
) = 0;
1902 if (*no_add_attrs
== false)
1903 DECL_DLLIMPORT_P (node
) = 1;
1905 else if (TREE_CODE (node
) == FUNCTION_DECL
1906 && DECL_DECLARED_INLINE_P (node
)
1907 && flag_keep_inline_dllexport
)
1908 /* An exported function, even if inline, must be emitted. */
1909 DECL_EXTERNAL (node
) = 0;
1911 /* Report error if symbol is not accessible at global scope. */
1912 if (!TREE_PUBLIC (node
) && VAR_OR_FUNCTION_DECL_P (node
))
1914 error ("external linkage required for symbol %q+D because of "
1915 "%qE attribute", node
, name
);
1916 *no_add_attrs
= true;
1919 /* A dllexport'd entity must have default visibility so that other
1920 program units (shared libraries or the main executable) can see
1921 it. A dllimport'd entity must have default visibility so that
1922 the linker knows that undefined references within this program
1923 unit can be resolved by the dynamic linker. */
1926 if (DECL_VISIBILITY_SPECIFIED (node
)
1927 && DECL_VISIBILITY (node
) != VISIBILITY_DEFAULT
)
1928 error ("%qE implies default visibility, but %qD has already "
1929 "been declared with a different visibility",
1931 DECL_VISIBILITY (node
) = VISIBILITY_DEFAULT
;
1932 DECL_VISIBILITY_SPECIFIED (node
) = 1;
1938 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
1940 /* Given two lists of attributes, return true if list l2 is
1941 equivalent to l1. */
1944 attribute_list_equal (const_tree l1
, const_tree l2
)
1949 return attribute_list_contained (l1
, l2
)
1950 && attribute_list_contained (l2
, l1
);
1953 /* Given two lists of attributes, return true if list L2 is
1954 completely contained within L1. */
1955 /* ??? This would be faster if attribute names were stored in a canonicalized
1956 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
1957 must be used to show these elements are equivalent (which they are). */
1958 /* ??? It's not clear that attributes with arguments will always be handled
1962 attribute_list_contained (const_tree l1
, const_tree l2
)
1966 /* First check the obvious, maybe the lists are identical. */
1970 /* Maybe the lists are similar. */
1971 for (t1
= l1
, t2
= l2
;
1973 && get_attribute_name (t1
) == get_attribute_name (t2
)
1974 && TREE_VALUE (t1
) == TREE_VALUE (t2
);
1975 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1978 /* Maybe the lists are equal. */
1979 if (t1
== 0 && t2
== 0)
1982 for (; t2
!= 0; t2
= TREE_CHAIN (t2
))
1985 /* This CONST_CAST is okay because lookup_attribute does not
1986 modify its argument and the return value is assigned to a
1988 for (attr
= lookup_ident_attribute (get_attribute_name (t2
),
1989 CONST_CAST_TREE (l1
));
1990 attr
!= NULL_TREE
&& !attribute_value_equal (t2
, attr
);
1991 attr
= lookup_ident_attribute (get_attribute_name (t2
),
1995 if (attr
== NULL_TREE
)
2002 /* The backbone of lookup_attribute(). ATTR_LEN is the string length
2003 of ATTR_NAME, and LIST is not NULL_TREE.
2005 The function is called from lookup_attribute in order to optimize
2009 private_lookup_attribute (const char *attr_name
, size_t attr_len
, tree list
)
2013 tree attr
= get_attribute_name (list
);
2014 size_t ident_len
= IDENTIFIER_LENGTH (attr
);
2015 if (cmp_attribs (attr_name
, attr_len
, IDENTIFIER_POINTER (attr
),
2018 list
= TREE_CHAIN (list
);
2024 /* Return true if the function decl or type NODE has been declared
2025 with attribute ANAME among attributes ATTRS. */
2028 has_attribute (tree node
, tree attrs
, const char *aname
)
2030 if (!strcmp (aname
, "const"))
2032 if (DECL_P (node
) && TREE_READONLY (node
))
2035 else if (!strcmp (aname
, "malloc"))
2037 if (DECL_P (node
) && DECL_IS_MALLOC (node
))
2040 else if (!strcmp (aname
, "noreturn"))
2042 if (DECL_P (node
) && TREE_THIS_VOLATILE (node
))
2045 else if (!strcmp (aname
, "nothrow"))
2047 if (TREE_NOTHROW (node
))
2050 else if (!strcmp (aname
, "pure"))
2052 if (DECL_P (node
) && DECL_PURE_P (node
))
2056 return lookup_attribute (aname
, attrs
);
2059 /* Return the number of mismatched function or type attributes between
2060 the "template" function declaration TMPL and DECL. The word "template"
2061 doesn't necessarily refer to a C++ template but rather a declaration
2062 whose attributes should be matched by those on DECL. For a non-zero
2063 return value set *ATTRSTR to a string representation of the list of
2064 mismatched attributes with quoted names.
2065 ATTRLIST is a list of additional attributes that SPEC should be
2066 taken to ultimately be declared with. */
2069 decls_mismatched_attributes (tree tmpl
, tree decl
, tree attrlist
,
2070 const char* const blacklist
[],
2071 pretty_printer
*attrstr
)
2073 if (TREE_CODE (tmpl
) != FUNCTION_DECL
)
2076 /* Avoid warning if either declaration or its type is deprecated. */
2077 if (TREE_DEPRECATED (tmpl
)
2078 || TREE_DEPRECATED (decl
))
2081 const tree tmpls
[] = { tmpl
, TREE_TYPE (tmpl
) };
2082 const tree decls
[] = { decl
, TREE_TYPE (decl
) };
2084 if (TREE_DEPRECATED (tmpls
[1])
2085 || TREE_DEPRECATED (decls
[1])
2086 || TREE_DEPRECATED (TREE_TYPE (tmpls
[1]))
2087 || TREE_DEPRECATED (TREE_TYPE (decls
[1])))
2090 tree tmpl_attrs
[] = { DECL_ATTRIBUTES (tmpl
), TYPE_ATTRIBUTES (tmpls
[1]) };
2091 tree decl_attrs
[] = { DECL_ATTRIBUTES (decl
), TYPE_ATTRIBUTES (decls
[1]) };
2094 decl_attrs
[0] = attrlist
;
2095 else if (!decl_attrs
[1])
2096 decl_attrs
[1] = attrlist
;
2098 /* Avoid warning if the template has no attributes. */
2099 if (!tmpl_attrs
[0] && !tmpl_attrs
[1])
2102 /* Avoid warning if either declaration contains an attribute on
2103 the white list below. */
2104 const char* const whitelist
[] = {
2108 for (unsigned i
= 0; i
!= 2; ++i
)
2109 for (unsigned j
= 0; j
!= sizeof whitelist
/ sizeof *whitelist
; ++j
)
2110 if (lookup_attribute (whitelist
[j
], tmpl_attrs
[i
])
2111 || lookup_attribute (whitelist
[j
], decl_attrs
[i
]))
2114 /* Put together a list of the black-listed attributes that the template
2115 is declared with and the declaration is not, in case it's not apparent
2116 from the most recent declaration of the template. */
2117 unsigned nattrs
= 0;
2119 for (unsigned i
= 0; blacklist
[i
]; ++i
)
2121 /* Attribute leaf only applies to extern functions. Avoid mentioning
2122 it when it's missing from a static declaration. */
2123 if (!TREE_PUBLIC (decl
)
2124 && !strcmp ("leaf", blacklist
[i
]))
2127 for (unsigned j
= 0; j
!= 2; ++j
)
2129 if (!has_attribute (tmpls
[j
], tmpl_attrs
[j
], blacklist
[i
]))
2133 unsigned kmax
= 1 + !!decl_attrs
[1];
2134 for (unsigned k
= 0; k
!= kmax
; ++k
)
2136 if (has_attribute (decls
[k
], decl_attrs
[k
], blacklist
[i
]))
2146 pp_string (attrstr
, ", ");
2147 pp_begin_quote (attrstr
, pp_show_color (global_dc
->printer
));
2148 pp_string (attrstr
, blacklist
[i
]);
2149 pp_end_quote (attrstr
, pp_show_color (global_dc
->printer
));
2160 /* Issue a warning for the declaration ALIAS for TARGET where ALIAS
2161 specifies either attributes that are incompatible with those of
2162 TARGET, or attributes that are missing and that declaring ALIAS
2163 with would benefit. */
2166 maybe_diag_alias_attributes (tree alias
, tree target
)
2168 /* Do not expect attributes to match between aliases and ifunc
2169 resolvers. There is no obvious correspondence between them. */
2170 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (alias
)))
2173 const char* const blacklist
[] = {
2174 "alloc_align", "alloc_size", "cold", "const", "hot", "leaf", "malloc",
2175 "nonnull", "noreturn", "nothrow", "pure", "returns_nonnull",
2176 "returns_twice", NULL
2179 pretty_printer attrnames
;
2180 if (warn_attribute_alias
> 1)
2182 /* With -Wattribute-alias=2 detect alias declarations that are more
2183 restrictive than their targets first. Those indicate potential
2185 if (unsigned n
= decls_mismatched_attributes (alias
, target
, NULL_TREE
,
2186 blacklist
, &attrnames
))
2188 auto_diagnostic_group d
;
2189 if (warning_n (DECL_SOURCE_LOCATION (alias
),
2190 OPT_Wattribute_alias_
, n
,
2191 "%qD specifies more restrictive attribute than "
2192 "its target %qD: %s",
2193 "%qD specifies more restrictive attributes than "
2194 "its target %qD: %s",
2195 alias
, target
, pp_formatted_text (&attrnames
)))
2196 inform (DECL_SOURCE_LOCATION (target
),
2197 "%qD target declared here", alias
);
2202 /* Detect alias declarations that are less restrictive than their
2203 targets. Those suggest potential optimization opportunities
2204 (solved by adding the missing attribute(s) to the alias). */
2205 if (unsigned n
= decls_mismatched_attributes (target
, alias
, NULL_TREE
,
2206 blacklist
, &attrnames
))
2208 auto_diagnostic_group d
;
2209 if (warning_n (DECL_SOURCE_LOCATION (alias
),
2210 OPT_Wmissing_attributes
, n
,
2211 "%qD specifies less restrictive attribute than "
2212 "its target %qD: %s",
2213 "%qD specifies less restrictive attributes than "
2214 "its target %qD: %s",
2215 alias
, target
, pp_formatted_text (&attrnames
)))
2216 inform (DECL_SOURCE_LOCATION (target
),
2217 "%qD target declared here", alias
);
2221 /* Initialize a mapping RWM for a call to a function declared with
2222 attribute access in ATTRS. Each attribute positional operand
2223 inserts one entry into the mapping with the operand number as
2227 init_attr_rdwr_indices (rdwr_map
*rwm
, tree attrs
)
2232 for (tree access
= attrs
;
2233 (access
= lookup_attribute ("access", access
));
2234 access
= TREE_CHAIN (access
))
2236 /* The TREE_VALUE of an attribute is a TREE_LIST whose TREE_VALUE
2237 is the attribute argument's value. */
2238 tree mode
= TREE_VALUE (access
);
2242 /* The (optional) list of VLA bounds. */
2243 tree vblist
= TREE_CHAIN (mode
);
2244 mode
= TREE_VALUE (mode
);
2245 if (TREE_CODE (mode
) != STRING_CST
)
2247 gcc_assert (TREE_CODE (mode
) == STRING_CST
);
2250 vblist
= nreverse (copy_list (TREE_VALUE (vblist
)));
2252 for (const char *m
= TREE_STRING_POINTER (mode
); *m
; )
2254 attr_access acc
= { };
2256 /* Skip the internal-only plus sign. */
2261 acc
.mode
= acc
.from_mode_char (*m
);
2262 acc
.sizarg
= UINT_MAX
;
2265 acc
.ptrarg
= strtoul (++m
, const_cast<char**>(&end
), 10);
2270 /* Forms containing the square bracket are internal-only
2271 (not specified by an attribute declaration), and used
2272 for various forms of array and VLA parameters. */
2273 acc
.internal_p
= true;
2275 /* Search to the closing bracket and look at the preceding
2276 code: it determines the form of the most significant
2277 bound of the array. Others prior to it encode the form
2278 of interior VLA bounds. They're not of interest here. */
2279 end
= strchr (m
, ']');
2280 const char *p
= end
;
2283 while (ISDIGIT (p
[-1]))
2288 /* A digit denotes a constant bound (as in T[3]). */
2289 acc
.static_p
= p
[-1] == 's';
2290 acc
.minsize
= strtoull (p
, NULL
, 10);
2292 else if (' ' == p
[-1])
2294 /* A space denotes an ordinary array of unspecified bound
2298 else if ('*' == p
[-1] || '$' == p
[-1])
2300 /* An asterisk denotes a VLA. When the closing bracket
2301 is followed by a comma and a dollar sign its bound is
2302 on the list. Otherwise it's a VLA with an unspecified
2304 acc
.static_p
= p
[-2] == 's';
2305 acc
.minsize
= HOST_WIDE_INT_M1U
;
2319 if (!acc
.size
&& vblist
)
2321 /* Extract the list of VLA bounds for the current
2322 parameter, store it in ACC.SIZE, and advance
2323 to the list of bounds for the next VLA parameter.
2325 acc
.size
= TREE_VALUE (vblist
);
2326 vblist
= TREE_CHAIN (vblist
);
2332 /* Extract the positional argument. It's absent
2333 for VLAs whose bound doesn't name a function
2335 unsigned pos
= strtoul (m
, const_cast<char**>(&end
), 10);
2336 if (acc
.sizarg
== UINT_MAX
)
2347 auto &ref
= rwm
->get_or_insert (acc
.ptrarg
, &existing
);
2350 /* Merge the new spec with the existing. */
2351 if (acc
.minsize
== HOST_WIDE_INT_M1U
)
2352 ref
.minsize
= HOST_WIDE_INT_M1U
;
2354 if (acc
.sizarg
!= UINT_MAX
)
2355 ref
.sizarg
= acc
.sizarg
;
2358 ref
.mode
= acc
.mode
;
2363 /* Unconditionally add an entry for the required pointer
2364 operand of the attribute, and one for the optional size
2365 operand when it's specified. */
2366 if (acc
.sizarg
!= UINT_MAX
)
2367 rwm
->put (acc
.sizarg
, acc
);
2372 /* Return the access specification for a function parameter PARM
2373 or null if the current function has no such specification. */
2376 get_parm_access (rdwr_map
&rdwr_idx
, tree parm
,
2377 tree fndecl
/* = current_function_decl */)
2379 tree fntype
= TREE_TYPE (fndecl
);
2380 init_attr_rdwr_indices (&rdwr_idx
, TYPE_ATTRIBUTES (fntype
));
2382 if (rdwr_idx
.is_empty ())
2385 unsigned argpos
= 0;
2386 tree fnargs
= DECL_ARGUMENTS (fndecl
);
2387 for (tree arg
= fnargs
; arg
; arg
= TREE_CHAIN (arg
), ++argpos
)
2389 return rdwr_idx
.get (argpos
);
2394 /* Return the internal representation as STRING_CST. Internal positional
2395 arguments are zero-based. */
2398 attr_access::to_internal_string () const
2400 return build_string (end
- str
, str
);
2403 /* Return the human-readable representation of the external attribute
2404 specification (as it might appear in the source code) as STRING_CST.
2405 External positional arguments are one-based. */
2408 attr_access::to_external_string () const
2411 gcc_assert (mode
!= access_deferred
);
2412 int len
= snprintf (buf
, sizeof buf
, "access (%s, %u",
2413 mode_names
[mode
], ptrarg
+ 1);
2414 if (sizarg
!= UINT_MAX
)
2415 len
+= snprintf (buf
+ len
, sizeof buf
- len
, ", %u", sizarg
+ 1);
2416 strcpy (buf
+ len
, ")");
2417 return build_string (len
+ 2, buf
);
2420 /* Return the number of specified VLA bounds and set *nunspec to
2421 the number of unspecified ones (those designated by [*]). */
2424 attr_access::vla_bounds (unsigned *nunspec
) const
2426 unsigned nbounds
= 0;
2428 /* STR points to the beginning of the specified string for the current
2429 argument that may be followed by the string for the next argument. */
2430 for (const char* p
= strchr (str
, ']'); p
&& *p
!= '['; --p
)
2440 /* Reset front end-specific attribute access data from ATTRS.
2441 Called from the free_lang_data pass. */
2444 attr_access::free_lang_data (tree attrs
)
2446 for (tree acs
= attrs
; (acs
= lookup_attribute ("access", acs
));
2447 acs
= TREE_CHAIN (acs
))
2449 tree vblist
= TREE_VALUE (acs
);
2450 vblist
= TREE_CHAIN (vblist
);
2454 for (vblist
= TREE_VALUE (vblist
); vblist
; vblist
= TREE_CHAIN (vblist
))
2456 tree
*pvbnd
= &TREE_VALUE (vblist
);
2457 if (!*pvbnd
|| DECL_P (*pvbnd
))
2460 /* VLA bounds that are expressions as opposed to DECLs are
2461 only used in the front end. Reset them to keep front end
2462 trees leaking into the middle end (see pr97172) and to
2468 for (tree argspec
= attrs
; (argspec
= lookup_attribute ("arg spec", argspec
));
2469 argspec
= TREE_CHAIN (argspec
))
2471 /* Same as above. */
2472 tree
*pvblist
= &TREE_VALUE (argspec
);
2473 *pvblist
= NULL_TREE
;
2477 /* Defined in attr_access. */
2478 constexpr char attr_access::mode_chars
[];
2479 constexpr char attr_access::mode_names
[][11];
2481 /* Format an array, including a VLA, pointed to by TYPE and used as
2482 a function parameter as a human-readable string. ACC describes
2483 an access to the parameter and is used to determine the outermost
2484 form of the array including its bound which is otherwise obviated
2485 by its decay to pointer. Return the formatted string. */
2488 attr_access::array_as_string (tree type
) const
2492 if (type
== error_mark_node
)
2493 return std::string ();
2497 /* For array parameters (but not pointers) create a temporary array
2498 type that corresponds to the form of the parameter including its
2499 qualifiers even though they apply to the pointer, not the array
2501 const bool vla_p
= minsize
== HOST_WIDE_INT_M1U
;
2502 tree eltype
= TREE_TYPE (type
);
2503 tree index_type
= NULL_TREE
;
2505 if (minsize
== HOST_WIDE_INT_M1U
)
2507 /* Determine if this is a VLA (an array whose most significant
2508 bound is nonconstant and whose access string has "$]" in it)
2509 extract the bound expression from SIZE. */
2510 const char *p
= end
;
2511 for ( ; p
!= str
&& *p
-- != ']'; );
2513 /* SIZE may have been cleared. Use it with care. */
2514 index_type
= build_index_type (size
? TREE_VALUE (size
) : size
);
2517 index_type
= build_index_type (size_int (minsize
- 1));
2519 tree arat
= NULL_TREE
;
2520 if (static_p
|| vla_p
)
2522 tree flag
= static_p
? integer_one_node
: NULL_TREE
;
2523 /* Hack: there's no language-independent way to encode
2524 the "static" specifier or the "*" notation in an array type.
2525 Add a "fake" attribute to have the pretty-printer add "static"
2526 or "*". The "[static N]" notation is only valid in the most
2527 significant bound but [*] can be used for any bound. Because
2528 [*] is represented the same as [0] this hack only works for
2529 the most significant bound like static and the others are
2531 arat
= build_tree_list (get_identifier ("array"), flag
);
2534 const int quals
= TYPE_QUALS (type
);
2535 type
= build_array_type (eltype
, index_type
);
2536 type
= build_type_attribute_qual_variant (type
, arat
, quals
);
2539 /* Format the type using the current pretty printer. The generic tree
2540 printer does a terrible job. */
2541 pretty_printer
*pp
= global_dc
->printer
->clone ();
2542 pp_printf (pp
, "%qT", type
);
2543 typstr
= pp_formatted_text (pp
);
2554 /* Helper types to verify the consistency attribute exclusions. */
2556 typedef std::pair
<const char *, const char *> excl_pair
;
2558 struct excl_hash_traits
: typed_noop_remove
<excl_pair
>
2560 typedef excl_pair value_type
;
2561 typedef value_type compare_type
;
2563 static hashval_t
hash (const value_type
&x
)
2565 hashval_t h1
= htab_hash_string (x
.first
);
2566 hashval_t h2
= htab_hash_string (x
.second
);
2570 static bool equal (const value_type
&x
, const value_type
&y
)
2572 return !strcmp (x
.first
, y
.first
) && !strcmp (x
.second
, y
.second
);
2575 static void mark_deleted (value_type
&x
)
2577 x
= value_type (NULL
, NULL
);
2580 static const bool empty_zero_p
= false;
2582 static void mark_empty (value_type
&x
)
2584 x
= value_type ("", "");
2587 static bool is_deleted (const value_type
&x
)
2589 return !x
.first
&& !x
.second
;
2592 static bool is_empty (const value_type
&x
)
2594 return !*x
.first
&& !*x
.second
;
2599 /* Self-test to verify that each attribute exclusion is symmetric,
2600 meaning that if attribute A is encoded as incompatible with
2601 attribute B then the opposite relationship is also encoded.
2602 This test also detects most cases of misspelled attribute names
2606 test_attribute_exclusions ()
2608 /* Iterate over the array of attribute tables first (with TI0 as
2609 the index) and over the array of attribute_spec in each table
2610 (with SI0 as the index). */
2611 const size_t ntables
= ARRAY_SIZE (attribute_tables
);
2613 /* Set of pairs of mutually exclusive attributes. */
2614 typedef hash_set
<excl_pair
, false, excl_hash_traits
> exclusion_set
;
2615 exclusion_set excl_set
;
2617 for (size_t ti0
= 0; ti0
!= ntables
; ++ti0
)
2618 for (size_t s0
= 0; attribute_tables
[ti0
][s0
].name
; ++s0
)
2620 const attribute_spec::exclusions
*excl
2621 = attribute_tables
[ti0
][s0
].exclude
;
2623 /* Skip each attribute that doesn't define exclusions. */
2627 const char *attr_name
= attribute_tables
[ti0
][s0
].name
;
2629 /* Iterate over the set of exclusions for every attribute
2630 (with EI0 as the index) adding the exclusions defined
2631 for each to the set. */
2632 for (size_t ei0
= 0; excl
[ei0
].name
; ++ei0
)
2634 const char *excl_name
= excl
[ei0
].name
;
2636 if (!strcmp (attr_name
, excl_name
))
2639 excl_set
.add (excl_pair (attr_name
, excl_name
));
2643 /* Traverse the set of mutually exclusive pairs of attributes
2644 and verify that they are symmetric. */
2645 for (exclusion_set::iterator it
= excl_set
.begin ();
2646 it
!= excl_set
.end ();
2649 if (!excl_set
.contains (excl_pair ((*it
).second
, (*it
).first
)))
2651 /* An exclusion for an attribute has been found that
2652 doesn't have a corresponding exclusion in the opposite
2655 sprintf (desc
, "'%s' attribute exclusion '%s' must be symmetric",
2656 (*it
).first
, (*it
).second
);
2657 fail (SELFTEST_LOCATION
, desc
);
2665 test_attribute_exclusions ();
2668 } /* namespace selftest */
2670 #endif /* CHECKING_P */