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