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