]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/attribs.c
[C++] Protect call to copy_attributes_to_builtin (PR91505)
[thirdparty/gcc.git] / gcc / attribs.c
CommitLineData
e3f6ce11 1/* Functions dealing with attribute handling, used by most front ends.
fbd26352 2 Copyright (C) 1992-2019 Free Software Foundation, Inc.
e3f6ce11 3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8c4c00c1 8Software Foundation; either version 3, or (at your option) any later
e3f6ce11 9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
8c4c00c1 17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
e3f6ce11 19
20#include "config.h"
21#include "system.h"
805e22b2 22#include "coretypes.h"
7c29e30e 23#include "target.h"
e3f6ce11 24#include "tree.h"
9ed99284 25#include "stringpool.h"
7c29e30e 26#include "diagnostic-core.h"
9ed99284 27#include "attribs.h"
28#include "stor-layout.h"
26ca6c20 29#include "langhooks.h"
e3fced1a 30#include "plugin.h"
dab0e385 31#include "selftest.h"
32#include "hash-set.h"
08cc1019 33#include "diagnostic.h"
34#include "pretty-print.h"
35#include "intl.h"
e3f6ce11 36
f8e93a2e 37/* Table of the tables of attributes (common, language, format, machine)
e3f6ce11 38 searched. */
39static const struct attribute_spec *attribute_tables[4];
40
5b634bfa 41/* Substring representation. */
42
43struct substring
44{
45 const char *str;
46 int length;
47};
48
d9dd21a8 49/* Simple hash function to avoid need to scan whole string. */
50
51static inline hashval_t
52substring_hash (const char *str, int l)
53{
54 return str[0] + str[l - 1] * 256 + l * 65536;
55}
56
57/* Used for attribute_hash. */
58
770ff93b 59struct attribute_hasher : nofree_ptr_hash <attribute_spec>
d9dd21a8 60{
9969c043 61 typedef substring *compare_type;
62 static inline hashval_t hash (const attribute_spec *);
63 static inline bool equal (const attribute_spec *, const substring *);
d9dd21a8 64};
65
66inline hashval_t
9969c043 67attribute_hasher::hash (const attribute_spec *spec)
d9dd21a8 68{
69 const int l = strlen (spec->name);
70 return substring_hash (spec->name, l);
71}
72
73inline bool
9969c043 74attribute_hasher::equal (const attribute_spec *spec, const substring *str)
d9dd21a8 75{
76 return (strncmp (spec->name, str->str, str->length) == 0
77 && !spec->name[str->length]);
78}
79
ffcdbf9c 80/* Scoped attribute name representation. */
81
82struct scoped_attributes
83{
84 const char *ns;
f1f41a6c 85 vec<attribute_spec> attributes;
c1f445d2 86 hash_table<attribute_hasher> *attribute_hash;
ffcdbf9c 87};
88
ffcdbf9c 89/* The table of scope attributes. */
f1f41a6c 90static vec<scoped_attributes> attributes_table;
ffcdbf9c 91
92static scoped_attributes* find_attribute_namespace (const char*);
93static void register_scoped_attribute (const struct attribute_spec *,
94 scoped_attributes *);
95
e3f6ce11 96static bool attributes_initialized = false;
97
e3f6ce11 98/* Default empty table of attributes. */
5b634bfa 99
e3f6ce11 100static const struct attribute_spec empty_attribute_table[] =
101{
672bc44d 102 { NULL, 0, 0, false, false, false, false, NULL, NULL }
e3f6ce11 103};
104
5b634bfa 105/* Return base name of the attribute. Ie '__attr__' is turned into 'attr'.
106 To avoid need for copying, we simply return length of the string. */
107
108static void
109extract_attribute_substring (struct substring *str)
110{
111 if (str->length > 4 && str->str[0] == '_' && str->str[1] == '_'
112 && str->str[str->length - 1] == '_' && str->str[str->length - 2] == '_')
113 {
114 str->length -= 4;
115 str->str += 2;
116 }
117}
118
ffcdbf9c 119/* Insert an array of attributes ATTRIBUTES into a namespace. This
120 array must be NULL terminated. NS is the name of attribute
121 namespace. The function returns the namespace into which the
122 attributes have been registered. */
123
672bc44d 124scoped_attributes *
125register_scoped_attributes (const struct attribute_spec *attributes,
126 const char *ns)
ffcdbf9c 127{
128 scoped_attributes *result = NULL;
129
130 /* See if we already have attributes in the namespace NS. */
131 result = find_attribute_namespace (ns);
132
133 if (result == NULL)
134 {
135 /* We don't have any namespace NS yet. Create one. */
136 scoped_attributes sa;
137
f5d49c14 138 if (attributes_table.is_empty ())
f1f41a6c 139 attributes_table.create (64);
ffcdbf9c 140
141 memset (&sa, 0, sizeof (sa));
142 sa.ns = ns;
f1f41a6c 143 sa.attributes.create (64);
144 result = attributes_table.safe_push (sa);
c1f445d2 145 result->attribute_hash = new hash_table<attribute_hasher> (200);
ffcdbf9c 146 }
147
148 /* Really add the attributes to their namespace now. */
149 for (unsigned i = 0; attributes[i].name != NULL; ++i)
150 {
f1f41a6c 151 result->attributes.safe_push (attributes[i]);
ffcdbf9c 152 register_scoped_attribute (&attributes[i], result);
153 }
154
155 gcc_assert (result != NULL);
156
157 return result;
158}
159
160/* Return the namespace which name is NS, NULL if none exist. */
161
162static scoped_attributes*
163find_attribute_namespace (const char* ns)
164{
165 unsigned ix;
166 scoped_attributes *iter;
167
f1f41a6c 168 FOR_EACH_VEC_ELT (attributes_table, ix, iter)
ffcdbf9c 169 if (ns == iter->ns
170 || (iter->ns != NULL
171 && ns != NULL
172 && !strcmp (iter->ns, ns)))
173 return iter;
174 return NULL;
175}
176
382ecba7 177/* Make some sanity checks on the attribute tables. */
178
179static void
180check_attribute_tables (void)
181{
182 for (size_t i = 0; i < ARRAY_SIZE (attribute_tables); i++)
183 for (size_t j = 0; attribute_tables[i][j].name != NULL; j++)
184 {
185 /* The name must not begin and end with __. */
186 const char *name = attribute_tables[i][j].name;
187 int len = strlen (name);
188
189 gcc_assert (!(name[0] == '_' && name[1] == '_'
190 && name[len - 1] == '_' && name[len - 2] == '_'));
191
192 /* The minimum and maximum lengths must be consistent. */
193 gcc_assert (attribute_tables[i][j].min_length >= 0);
194
195 gcc_assert (attribute_tables[i][j].max_length == -1
196 || (attribute_tables[i][j].max_length
197 >= attribute_tables[i][j].min_length));
198
199 /* An attribute cannot require both a DECL and a TYPE. */
200 gcc_assert (!attribute_tables[i][j].decl_required
201 || !attribute_tables[i][j].type_required);
202
203 /* If an attribute requires a function type, in particular
204 it requires a type. */
205 gcc_assert (!attribute_tables[i][j].function_type_required
206 || attribute_tables[i][j].type_required);
207 }
208
209 /* Check that each name occurs just once in each table. */
210 for (size_t i = 0; i < ARRAY_SIZE (attribute_tables); i++)
211 for (size_t j = 0; attribute_tables[i][j].name != NULL; j++)
212 for (size_t k = j + 1; attribute_tables[i][k].name != NULL; k++)
213 gcc_assert (strcmp (attribute_tables[i][j].name,
214 attribute_tables[i][k].name));
215
216 /* Check that no name occurs in more than one table. Names that
217 begin with '*' are exempt, and may be overridden. */
218 for (size_t i = 0; i < ARRAY_SIZE (attribute_tables); i++)
219 for (size_t j = i + 1; j < ARRAY_SIZE (attribute_tables); j++)
220 for (size_t k = 0; attribute_tables[i][k].name != NULL; k++)
221 for (size_t l = 0; attribute_tables[j][l].name != NULL; l++)
222 gcc_assert (attribute_tables[i][k].name[0] == '*'
223 || strcmp (attribute_tables[i][k].name,
224 attribute_tables[j][l].name));
225}
226
227/* Initialize attribute tables, and make some sanity checks if checking is
228 enabled. */
e3f6ce11 229
b7831f3e 230void
aecda0d6 231init_attributes (void)
e3f6ce11 232{
3585dac7 233 size_t i;
e3f6ce11 234
b7831f3e 235 if (attributes_initialized)
236 return;
237
f8e93a2e 238 attribute_tables[0] = lang_hooks.common_attribute_table;
239 attribute_tables[1] = lang_hooks.attribute_table;
240 attribute_tables[2] = lang_hooks.format_attribute_table;
e3f6ce11 241 attribute_tables[3] = targetm.attribute_table;
242
f8e93a2e 243 /* Translate NULL pointers to pointers to the empty table. */
244 for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
245 if (attribute_tables[i] == NULL)
246 attribute_tables[i] = empty_attribute_table;
247
382ecba7 248 if (flag_checking)
249 check_attribute_tables ();
e3f6ce11 250
ffcdbf9c 251 for (i = 0; i < ARRAY_SIZE (attribute_tables); ++i)
252 /* Put all the GNU attributes into the "gnu" namespace. */
253 register_scoped_attributes (attribute_tables[i], "gnu");
254
e3fced1a 255 invoke_plugin_callbacks (PLUGIN_ATTRIBUTES, NULL);
256 attributes_initialized = true;
257}
258
259/* Insert a single ATTR into the attribute table. */
260
261void
48e1416a 262register_attribute (const struct attribute_spec *attr)
ffcdbf9c 263{
264 register_scoped_attribute (attr, find_attribute_namespace ("gnu"));
265}
266
267/* Insert a single attribute ATTR into a namespace of attributes. */
268
269static void
270register_scoped_attribute (const struct attribute_spec *attr,
271 scoped_attributes *name_space)
e3fced1a 272{
4fb84273 273 struct substring str;
d9dd21a8 274 attribute_spec **slot;
4fb84273 275
ffcdbf9c 276 gcc_assert (attr != NULL && name_space != NULL);
277
c1f445d2 278 gcc_assert (name_space->attribute_hash);
ffcdbf9c 279
4fb84273 280 str.str = attr->name;
281 str.length = strlen (str.str);
c55c785f 282
283 /* Attribute names in the table must be in the form 'text' and not
284 in the form '__text__'. */
285 gcc_assert (str.length > 0 && str.str[0] != '_');
286
d9dd21a8 287 slot = name_space->attribute_hash
c1f445d2 288 ->find_slot_with_hash (&str, substring_hash (str.str, str.length),
289 INSERT);
4c0315d0 290 gcc_assert (!*slot || attr->name[0] == '*');
d9dd21a8 291 *slot = CONST_CAST (struct attribute_spec *, attr);
e3f6ce11 292}
2fdd6488 293
ffcdbf9c 294/* Return the spec for the scoped attribute with namespace NS and
295 name NAME. */
2fdd6488 296
0e80b01d 297static const struct attribute_spec *
ffcdbf9c 298lookup_scoped_attribute_spec (const_tree ns, const_tree name)
2fdd6488 299{
300 struct substring attr;
ffcdbf9c 301 scoped_attributes *attrs;
302
303 const char *ns_str = (ns != NULL_TREE) ? IDENTIFIER_POINTER (ns): NULL;
304
305 attrs = find_attribute_namespace (ns_str);
306
307 if (attrs == NULL)
308 return NULL;
2fdd6488 309
310 attr.str = IDENTIFIER_POINTER (name);
311 attr.length = IDENTIFIER_LENGTH (name);
312 extract_attribute_substring (&attr);
c1f445d2 313 return attrs->attribute_hash->find_with_hash (&attr,
314 substring_hash (attr.str,
315 attr.length));
2fdd6488 316}
ffcdbf9c 317
d4701f6c 318/* Return the spec for the attribute named NAME. If NAME is a TREE_LIST,
319 it also specifies the attribute namespace. */
ffcdbf9c 320
321const struct attribute_spec *
322lookup_attribute_spec (const_tree name)
323{
d4701f6c 324 tree ns;
325 if (TREE_CODE (name) == TREE_LIST)
326 {
327 ns = TREE_PURPOSE (name);
328 name = TREE_VALUE (name);
329 }
330 else
331 ns = get_identifier ("gnu");
332 return lookup_scoped_attribute_spec (ns, name);
ffcdbf9c 333}
334
0e80b01d 335
336/* Return the namespace of the attribute ATTR. This accessor works on
337 GNU and C++11 (scoped) attributes. On GNU attributes,
338 it returns an identifier tree for the string "gnu".
339
340 Please read the comments of cxx11_attribute_p to understand the
341 format of attributes. */
342
e7b53e8c 343tree
0e80b01d 344get_attribute_namespace (const_tree attr)
345{
346 if (cxx11_attribute_p (attr))
347 return TREE_PURPOSE (TREE_PURPOSE (attr));
348 return get_identifier ("gnu");
349}
350
dab0e385 351/* Check LAST_DECL and NODE of the same symbol for attributes that are
352 recorded in SPEC to be mutually exclusive with ATTRNAME, diagnose
353 them, and return true if any have been found. NODE can be a DECL
354 or a TYPE. */
355
356static bool
357diag_attr_exclusions (tree last_decl, tree node, tree attrname,
358 const attribute_spec *spec)
359{
360 const attribute_spec::exclusions *excl = spec->exclude;
361
362 tree_code code = TREE_CODE (node);
363
364 if ((code == FUNCTION_DECL && !excl->function
365 && (!excl->type || !spec->affects_type_identity))
366 || (code == VAR_DECL && !excl->variable
367 && (!excl->type || !spec->affects_type_identity))
368 || (((code == TYPE_DECL || RECORD_OR_UNION_TYPE_P (node)) && !excl->type)))
369 return false;
370
371 /* True if an attribute that's mutually exclusive with ATTRNAME
372 has been found. */
373 bool found = false;
374
375 if (last_decl && last_decl != node && TREE_TYPE (last_decl) != node)
376 {
377 /* Check both the last DECL and its type for conflicts with
378 the attribute being added to the current decl or type. */
379 found |= diag_attr_exclusions (last_decl, last_decl, attrname, spec);
380 tree decl_type = TREE_TYPE (last_decl);
381 found |= diag_attr_exclusions (last_decl, decl_type, attrname, spec);
382 }
383
384 /* NODE is either the current DECL to which the attribute is being
385 applied or its TYPE. For the former, consider the attributes on
386 both the DECL and its type. */
387 tree attrs[2];
388
389 if (DECL_P (node))
390 {
391 attrs[0] = DECL_ATTRIBUTES (node);
392 attrs[1] = TYPE_ATTRIBUTES (TREE_TYPE (node));
393 }
394 else
395 {
396 attrs[0] = TYPE_ATTRIBUTES (node);
397 attrs[1] = NULL_TREE;
398 }
399
400 /* Iterate over the mutually exclusive attribute names and verify
401 that the symbol doesn't contain it. */
402 for (unsigned i = 0; i != sizeof attrs / sizeof *attrs; ++i)
403 {
404 if (!attrs[i])
405 continue;
406
407 for ( ; excl->name; ++excl)
408 {
409 /* Avoid checking the attribute against itself. */
410 if (is_attribute_p (excl->name, attrname))
411 continue;
412
413 if (!lookup_attribute (excl->name, attrs[i]))
414 continue;
415
b35ec6e1 416 /* An exclusion may apply either to a function declaration,
417 type declaration, or a field/variable declaration, or
418 any subset of the three. */
419 if (TREE_CODE (node) == FUNCTION_DECL
420 && !excl->function)
421 continue;
422
423 if (TREE_CODE (node) == TYPE_DECL
424 && !excl->type)
425 continue;
426
427 if ((TREE_CODE (node) == FIELD_DECL
428 || TREE_CODE (node) == VAR_DECL)
429 && !excl->variable)
430 continue;
431
dab0e385 432 found = true;
433
434 /* Print a note? */
435 bool note = last_decl != NULL_TREE;
bc35ef65 436 auto_diagnostic_group d;
dab0e385 437 if (TREE_CODE (node) == FUNCTION_DECL
a0e9bfbb 438 && fndecl_built_in_p (node))
dab0e385 439 note &= warning (OPT_Wattributes,
440 "ignoring attribute %qE in declaration of "
441 "a built-in function %qD because it conflicts "
442 "with attribute %qs",
443 attrname, node, excl->name);
444 else
445 note &= warning (OPT_Wattributes,
446 "ignoring attribute %qE because "
447 "it conflicts with attribute %qs",
448 attrname, excl->name);
449
450 if (note)
451 inform (DECL_SOURCE_LOCATION (last_decl),
452 "previous declaration here");
453 }
454 }
455
456 return found;
457}
0e80b01d 458
e3f6ce11 459/* Process the attributes listed in ATTRIBUTES and install them in *NODE,
460 which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
461 it should be modified in place; if a TYPE, a copy should be created
462 unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further
463 information, in the form of a bitwise OR of flags in enum attribute_flags
464 from tree.h. Depending on these flags, some attributes may be
465 returned to be applied at a later stage (for example, to apply
101cc430 466 a decl attribute to the declaration rather than to its type). */
e3f6ce11 467
468tree
dab0e385 469decl_attributes (tree *node, tree attributes, int flags,
470 tree last_decl /* = NULL_TREE */)
e3f6ce11 471{
e3f6ce11 472 tree returned_attrs = NULL_TREE;
473
ffcdbf9c 474 if (TREE_TYPE (*node) == error_mark_node || attributes == error_mark_node)
1ccba7b7 475 return NULL_TREE;
476
e3f6ce11 477 if (!attributes_initialized)
478 init_attributes ();
479
46f8e3b0 480 /* If this is a function and the user used #pragma GCC optimize, add the
481 options to the attribute((optimize(...))) list. */
482 if (TREE_CODE (*node) == FUNCTION_DECL && current_optimize_pragma)
483 {
484 tree cur_attr = lookup_attribute ("optimize", attributes);
485 tree opts = copy_list (current_optimize_pragma);
486
487 if (! cur_attr)
488 attributes
489 = tree_cons (get_identifier ("optimize"), opts, attributes);
490 else
491 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
492 }
493
494 if (TREE_CODE (*node) == FUNCTION_DECL
495 && optimization_current_node != optimization_default_node
496 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node))
497 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) = optimization_current_node;
498
24470055 499 /* If this is a function and the user used #pragma GCC target, add the
500 options to the attribute((target(...))) list. */
46f8e3b0 501 if (TREE_CODE (*node) == FUNCTION_DECL
24470055 502 && current_target_pragma
46f8e3b0 503 && targetm.target_option.valid_attribute_p (*node, NULL_TREE,
24470055 504 current_target_pragma, 0))
46f8e3b0 505 {
24470055 506 tree cur_attr = lookup_attribute ("target", attributes);
507 tree opts = copy_list (current_target_pragma);
46f8e3b0 508
509 if (! cur_attr)
24470055 510 attributes = tree_cons (get_identifier ("target"), opts, attributes);
46f8e3b0 511 else
512 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
513 }
514
cd28ae7a 515 /* A "naked" function attribute implies "noinline" and "noclone" for
516 those targets that support it. */
517 if (TREE_CODE (*node) == FUNCTION_DECL
c55c785f 518 && attributes
280ce47d 519 && lookup_attribute ("naked", attributes) != NULL
520 && lookup_attribute_spec (get_identifier ("naked")))
cd28ae7a 521 {
522 if (lookup_attribute ("noinline", attributes) == NULL)
523 attributes = tree_cons (get_identifier ("noinline"), NULL, attributes);
524
525 if (lookup_attribute ("noclone", attributes) == NULL)
526 attributes = tree_cons (get_identifier ("noclone"), NULL, attributes);
527 }
528
280ce47d 529 /* A "noipa" function attribute implies "noinline", "noclone" and "no_icf"
530 for those targets that support it. */
531 if (TREE_CODE (*node) == FUNCTION_DECL
532 && attributes
533 && lookup_attribute ("noipa", attributes) != NULL
534 && lookup_attribute_spec (get_identifier ("noipa")))
535 {
536 if (lookup_attribute ("noinline", attributes) == NULL)
537 attributes = tree_cons (get_identifier ("noinline"), NULL, attributes);
538
539 if (lookup_attribute ("noclone", attributes) == NULL)
540 attributes = tree_cons (get_identifier ("noclone"), NULL, attributes);
541
542 if (lookup_attribute ("no_icf", attributes) == NULL)
543 attributes = tree_cons (get_identifier ("no_icf"), NULL, attributes);
544 }
545
883b2e73 546 targetm.insert_attributes (*node, &attributes);
e3f6ce11 547
dab0e385 548 /* Note that attributes on the same declaration are not necessarily
549 in the same order as in the source. */
e7b53e8c 550 for (tree attr = attributes; attr; attr = TREE_CHAIN (attr))
e3f6ce11 551 {
e7b53e8c 552 tree ns = get_attribute_namespace (attr);
553 tree name = get_attribute_name (attr);
554 tree args = TREE_VALUE (attr);
e3f6ce11 555 tree *anode = node;
672bc44d 556 const struct attribute_spec *spec
557 = lookup_scoped_attribute_spec (ns, name);
ce6dcb60 558 int fn_ptr_quals = 0;
79bdd5ff 559 tree fn_ptr_tmp = NULL_TREE;
e7b53e8c 560 const bool cxx11_attr_p = cxx11_attribute_p (attr);
e3f6ce11 561
562 if (spec == NULL)
563 {
c8010b80 564 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
ffcdbf9c 565 {
e7b53e8c 566 if (ns == NULL_TREE || !cxx11_attr_p)
ffcdbf9c 567 warning (OPT_Wattributes, "%qE attribute directive ignored",
568 name);
569 else
570 warning (OPT_Wattributes,
571 "%<%E::%E%> scoped attribute directive ignored",
572 ns, name);
573 }
e3f6ce11 574 continue;
575 }
576 else if (list_length (args) < spec->min_length
577 || (spec->max_length >= 0
578 && list_length (args) > spec->max_length))
579 {
abd3e6b5 580 error ("wrong number of arguments specified for %qE attribute",
581 name);
e3f6ce11 582 continue;
583 }
5b634bfa 584 gcc_assert (is_attribute_p (spec->name, name));
e3f6ce11 585
ffcdbf9c 586 if (TYPE_P (*node)
e7b53e8c 587 && cxx11_attr_p
ffcdbf9c 588 && !(flags & ATTR_FLAG_TYPE_IN_PLACE))
589 {
590 /* This is a c++11 attribute that appertains to a
591 type-specifier, outside of the definition of, a class
592 type. Ignore it. */
bc35ef65 593 auto_diagnostic_group d;
2a6eea59 594 if (warning (OPT_Wattributes, "attribute ignored"))
595 inform (input_location,
596 "an attribute that appertains to a type-specifier "
597 "is ignored");
ffcdbf9c 598 continue;
599 }
600
e3f6ce11 601 if (spec->decl_required && !DECL_P (*anode))
602 {
603 if (flags & ((int) ATTR_FLAG_DECL_NEXT
604 | (int) ATTR_FLAG_FUNCTION_NEXT
605 | (int) ATTR_FLAG_ARRAY_NEXT))
606 {
607 /* Pass on this attribute to be tried again. */
dab0e385 608 tree attr = tree_cons (name, args, NULL_TREE);
609 returned_attrs = chainon (returned_attrs, attr);
e3f6ce11 610 continue;
611 }
612 else
613 {
abd3e6b5 614 warning (OPT_Wattributes, "%qE attribute does not apply to types",
615 name);
e3f6ce11 616 continue;
617 }
618 }
619
aa9c60c1 620 /* If we require a type, but were passed a decl, set up to make a
621 new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE
622 would have applied if we'd been passed a type, but we cannot modify
623 the decl's type in place here. */
e3f6ce11 624 if (spec->type_required && DECL_P (*anode))
aa9c60c1 625 {
626 anode = &TREE_TYPE (*anode);
b0dc5308 627 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
aa9c60c1 628 }
e3f6ce11 629
630 if (spec->function_type_required && TREE_CODE (*anode) != FUNCTION_TYPE
631 && TREE_CODE (*anode) != METHOD_TYPE)
632 {
633 if (TREE_CODE (*anode) == POINTER_TYPE
634 && (TREE_CODE (TREE_TYPE (*anode)) == FUNCTION_TYPE
635 || TREE_CODE (TREE_TYPE (*anode)) == METHOD_TYPE))
636 {
79bdd5ff 637 /* OK, this is a bit convoluted. We can't just make a copy
638 of the pointer type and modify its TREE_TYPE, because if
639 we change the attributes of the target type the pointer
640 type needs to have a different TYPE_MAIN_VARIANT. So we
641 pull out the target type now, frob it as appropriate, and
642 rebuild the pointer type later.
643
a0c938f0 644 This would all be simpler if attributes were part of the
645 declarator, grumble grumble. */
79bdd5ff 646 fn_ptr_tmp = TREE_TYPE (*anode);
ce6dcb60 647 fn_ptr_quals = TYPE_QUALS (*anode);
79bdd5ff 648 anode = &fn_ptr_tmp;
649 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
e3f6ce11 650 }
651 else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
652 {
653 /* Pass on this attribute to be tried again. */
dab0e385 654 tree attr = tree_cons (name, args, NULL_TREE);
655 returned_attrs = chainon (returned_attrs, attr);
e3f6ce11 656 continue;
657 }
658
659 if (TREE_CODE (*anode) != FUNCTION_TYPE
660 && TREE_CODE (*anode) != METHOD_TYPE)
661 {
9b2d6d13 662 warning (OPT_Wattributes,
abd3e6b5 663 "%qE attribute only applies to function types",
664 name);
e3f6ce11 665 continue;
666 }
667 }
668
4a2849cb 669 if (TYPE_P (*anode)
670 && (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
671 && TYPE_SIZE (*anode) != NULL_TREE)
672 {
673 warning (OPT_Wattributes, "type attributes ignored after type is already defined");
674 continue;
675 }
676
dab0e385 677 bool no_add_attrs = false;
678
ca12c192 679 /* Check for exclusions with other attributes on the current
680 declation as well as the last declaration of the same
681 symbol already processed (if one exists). Detect and
682 reject incompatible attributes. */
683 bool built_in = flags & ATTR_FLAG_BUILT_IN;
684 if (spec->exclude
685 && (flag_checking || !built_in))
686 {
687 /* Always check attributes on user-defined functions.
688 Check them on built-ins only when -fchecking is set.
689 Ignore __builtin_unreachable -- it's both const and
690 noreturn. */
691
692 if (!built_in
693 || !DECL_P (*anode)
50b8400c 694 || DECL_BUILT_IN_CLASS (*anode) != BUILT_IN_NORMAL
ca12c192 695 || (DECL_FUNCTION_CODE (*anode) != BUILT_IN_UNREACHABLE
696 && (DECL_FUNCTION_CODE (*anode)
697 != BUILT_IN_UBSAN_HANDLE_BUILTIN_UNREACHABLE)))
698 {
699 bool no_add = diag_attr_exclusions (last_decl, *anode, name, spec);
700 if (!no_add && anode != node)
701 no_add = diag_attr_exclusions (last_decl, *node, name, spec);
702 no_add_attrs |= no_add;
703 }
704 }
705
706 if (no_add_attrs)
707 continue;
708
e3f6ce11 709 if (spec->handler != NULL)
ffcdbf9c 710 {
e7b53e8c 711 int cxx11_flag = (cxx11_attr_p ? ATTR_FLAG_CXX11 : 0);
ffcdbf9c 712
dab0e385 713 /* Pass in an array of the current declaration followed
714 by the last pushed/merged declaration if one exists.
715 If the handler changes CUR_AND_LAST_DECL[0] replace
716 *ANODE with its value. */
717 tree cur_and_last_decl[] = { *anode, last_decl };
718 tree ret = (spec->handler) (cur_and_last_decl, name, args,
719 flags|cxx11_flag, &no_add_attrs);
720
721 *anode = cur_and_last_decl[0];
722 if (ret == error_mark_node)
723 {
724 warning (OPT_Wattributes, "%qE attribute ignored", name);
725 no_add_attrs = true;
726 }
727 else
728 returned_attrs = chainon (ret, returned_attrs);
729 }
730
ae4718db 731 /* Layout the decl in case anything changed. */
732 if (spec->type_required && DECL_P (*node)
53e9c5c4 733 && (VAR_P (*node)
e56de52f 734 || TREE_CODE (*node) == PARM_DECL
735 || TREE_CODE (*node) == RESULT_DECL))
1c0a6d1e 736 relayout_decl (*node);
ae4718db 737
e3f6ce11 738 if (!no_add_attrs)
739 {
740 tree old_attrs;
741 tree a;
742
743 if (DECL_P (*anode))
744 old_attrs = DECL_ATTRIBUTES (*anode);
745 else
746 old_attrs = TYPE_ATTRIBUTES (*anode);
747
748 for (a = lookup_attribute (spec->name, old_attrs);
749 a != NULL_TREE;
750 a = lookup_attribute (spec->name, TREE_CHAIN (a)))
751 {
752 if (simple_cst_equal (TREE_VALUE (a), args) == 1)
753 break;
754 }
755
756 if (a == NULL_TREE)
757 {
758 /* This attribute isn't already in the list. */
e7b53e8c 759 tree r;
760 /* Preserve the C++11 form. */
761 if (cxx11_attr_p)
762 r = tree_cons (build_tree_list (ns, name), args, old_attrs);
763 else
764 r = tree_cons (name, args, old_attrs);
765
e3f6ce11 766 if (DECL_P (*anode))
e7b53e8c 767 DECL_ATTRIBUTES (*anode) = r;
e3f6ce11 768 else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
316e17ae 769 {
e7b53e8c 770 TYPE_ATTRIBUTES (*anode) = r;
316e17ae 771 /* If this is the main variant, also push the attributes
772 out to the other variants. */
773 if (*anode == TYPE_MAIN_VARIANT (*anode))
774 {
e7b53e8c 775 for (tree variant = *anode; variant;
316e17ae 776 variant = TYPE_NEXT_VARIANT (variant))
777 {
778 if (TYPE_ATTRIBUTES (variant) == old_attrs)
779 TYPE_ATTRIBUTES (variant)
780 = TYPE_ATTRIBUTES (*anode);
781 else if (!lookup_attribute
782 (spec->name, TYPE_ATTRIBUTES (variant)))
783 TYPE_ATTRIBUTES (variant) = tree_cons
784 (name, args, TYPE_ATTRIBUTES (variant));
785 }
786 }
787 }
e3f6ce11 788 else
e7b53e8c 789 *anode = build_type_attribute_variant (*anode, r);
e3f6ce11 790 }
791 }
79bdd5ff 792
793 if (fn_ptr_tmp)
794 {
795 /* Rebuild the function pointer type and put it in the
796 appropriate place. */
797 fn_ptr_tmp = build_pointer_type (fn_ptr_tmp);
ce6dcb60 798 if (fn_ptr_quals)
799 fn_ptr_tmp = build_qualified_type (fn_ptr_tmp, fn_ptr_quals);
79bdd5ff 800 if (DECL_P (*node))
801 TREE_TYPE (*node) = fn_ptr_tmp;
79bdd5ff 802 else
64db345d 803 {
804 gcc_assert (TREE_CODE (*node) == POINTER_TYPE);
805 *node = fn_ptr_tmp;
806 }
79bdd5ff 807 }
e3f6ce11 808 }
809
810 return returned_attrs;
811}
4c0315d0 812
ffcdbf9c 813/* Return TRUE iff ATTR has been parsed by the front-end as a C++-11
814 attribute.
815
816 When G++ parses a C++11 attribute, it is represented as
817 a TREE_LIST which TREE_PURPOSE is itself a TREE_LIST. TREE_PURPOSE
818 (TREE_PURPOSE (ATTR)) is the namespace of the attribute, and the
819 TREE_VALUE (TREE_PURPOSE (ATTR)) is its non-qualified name. Please
820 use get_attribute_namespace and get_attribute_name to retrieve the
821 namespace and name of the attribute, as these accessors work with
822 GNU attributes as well. */
823
824bool
825cxx11_attribute_p (const_tree attr)
826{
827 if (attr == NULL_TREE
828 || TREE_CODE (attr) != TREE_LIST)
829 return false;
830
831 return (TREE_CODE (TREE_PURPOSE (attr)) == TREE_LIST);
832}
833
834/* Return the name of the attribute ATTR. This accessor works on GNU
835 and C++11 (scoped) attributes.
836
837 Please read the comments of cxx11_attribute_p to understand the
838 format of attributes. */
839
840tree
841get_attribute_name (const_tree attr)
842{
843 if (cxx11_attribute_p (attr))
844 return TREE_VALUE (TREE_PURPOSE (attr));
845 return TREE_PURPOSE (attr);
846}
847
4c0315d0 848/* Subroutine of set_method_tm_attributes. Apply TM attribute ATTR
849 to the method FNDECL. */
850
851void
852apply_tm_attr (tree fndecl, tree attr)
853{
854 decl_attributes (&TREE_TYPE (fndecl), tree_cons (attr, NULL, NULL), 0);
855}
ab50af2a 856
857/* Makes a function attribute of the form NAME(ARG_NAME) and chains
858 it to CHAIN. */
859
860tree
861make_attribute (const char *name, const char *arg_name, tree chain)
862{
863 tree attr_name;
864 tree attr_arg_name;
865 tree attr_args;
866 tree attr;
867
868 attr_name = get_identifier (name);
869 attr_arg_name = build_string (strlen (arg_name), arg_name);
870 attr_args = tree_cons (NULL_TREE, attr_arg_name, NULL_TREE);
871 attr = tree_cons (attr_name, attr_args, chain);
872 return attr;
873}
fd4f3a94 874
875\f
876/* Common functions used for target clone support. */
877
878/* Comparator function to be used in qsort routine to sort attribute
879 specification strings to "target". */
880
881static int
882attr_strcmp (const void *v1, const void *v2)
883{
884 const char *c1 = *(char *const*)v1;
885 const char *c2 = *(char *const*)v2;
886 return strcmp (c1, c2);
887}
888
889/* ARGLIST is the argument to target attribute. This function tokenizes
890 the comma separated arguments, sorts them and returns a string which
891 is a unique identifier for the comma separated arguments. It also
892 replaces non-identifier characters "=,-" with "_". */
893
894char *
895sorted_attr_string (tree arglist)
896{
897 tree arg;
898 size_t str_len_sum = 0;
899 char **args = NULL;
900 char *attr_str, *ret_str;
901 char *attr = NULL;
902 unsigned int argnum = 1;
903 unsigned int i;
904
905 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
906 {
907 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
908 size_t len = strlen (str);
909 str_len_sum += len + 1;
910 if (arg != arglist)
911 argnum++;
912 for (i = 0; i < strlen (str); i++)
913 if (str[i] == ',')
914 argnum++;
915 }
916
917 attr_str = XNEWVEC (char, str_len_sum);
918 str_len_sum = 0;
919 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
920 {
921 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
922 size_t len = strlen (str);
923 memcpy (attr_str + str_len_sum, str, len);
924 attr_str[str_len_sum + len] = TREE_CHAIN (arg) ? ',' : '\0';
925 str_len_sum += len + 1;
926 }
927
928 /* Replace "=,-" with "_". */
929 for (i = 0; i < strlen (attr_str); i++)
930 if (attr_str[i] == '=' || attr_str[i]== '-')
931 attr_str[i] = '_';
932
933 if (argnum == 1)
934 return attr_str;
935
936 args = XNEWVEC (char *, argnum);
937
938 i = 0;
939 attr = strtok (attr_str, ",");
940 while (attr != NULL)
941 {
942 args[i] = attr;
943 i++;
944 attr = strtok (NULL, ",");
945 }
946
947 qsort (args, argnum, sizeof (char *), attr_strcmp);
948
949 ret_str = XNEWVEC (char, str_len_sum);
950 str_len_sum = 0;
951 for (i = 0; i < argnum; i++)
952 {
953 size_t len = strlen (args[i]);
954 memcpy (ret_str + str_len_sum, args[i], len);
955 ret_str[str_len_sum + len] = i < argnum - 1 ? '_' : '\0';
956 str_len_sum += len + 1;
957 }
958
959 XDELETEVEC (args);
960 XDELETEVEC (attr_str);
961 return ret_str;
962}
963
964
965/* This function returns true if FN1 and FN2 are versions of the same function,
966 that is, the target strings of the function decls are different. This assumes
967 that FN1 and FN2 have the same signature. */
968
969bool
970common_function_versions (tree fn1, tree fn2)
971{
972 tree attr1, attr2;
973 char *target1, *target2;
974 bool result;
975
976 if (TREE_CODE (fn1) != FUNCTION_DECL
977 || TREE_CODE (fn2) != FUNCTION_DECL)
978 return false;
979
980 attr1 = lookup_attribute ("target", DECL_ATTRIBUTES (fn1));
981 attr2 = lookup_attribute ("target", DECL_ATTRIBUTES (fn2));
982
983 /* At least one function decl should have the target attribute specified. */
984 if (attr1 == NULL_TREE && attr2 == NULL_TREE)
985 return false;
986
987 /* Diagnose missing target attribute if one of the decls is already
988 multi-versioned. */
989 if (attr1 == NULL_TREE || attr2 == NULL_TREE)
990 {
991 if (DECL_FUNCTION_VERSIONED (fn1) || DECL_FUNCTION_VERSIONED (fn2))
992 {
993 if (attr2 != NULL_TREE)
994 {
995 std::swap (fn1, fn2);
996 attr1 = attr2;
997 }
998 error_at (DECL_SOURCE_LOCATION (fn2),
999 "missing %<target%> attribute for multi-versioned %qD",
1000 fn2);
1001 inform (DECL_SOURCE_LOCATION (fn1),
1002 "previous declaration of %qD", fn1);
1003 /* Prevent diagnosing of the same error multiple times. */
1004 DECL_ATTRIBUTES (fn2)
1005 = tree_cons (get_identifier ("target"),
1006 copy_node (TREE_VALUE (attr1)),
1007 DECL_ATTRIBUTES (fn2));
1008 }
1009 return false;
1010 }
1011
1012 target1 = sorted_attr_string (TREE_VALUE (attr1));
1013 target2 = sorted_attr_string (TREE_VALUE (attr2));
1014
1015 /* The sorted target strings must be different for fn1 and fn2
1016 to be versions. */
1017 if (strcmp (target1, target2) == 0)
1018 result = false;
1019 else
1020 result = true;
1021
1022 XDELETEVEC (target1);
1023 XDELETEVEC (target2);
1024
1025 return result;
1026}
1027
1028/* Return a new name by appending SUFFIX to the DECL name. If make_unique
1029 is true, append the full path name of the source file. */
1030
1031char *
1032make_unique_name (tree decl, const char *suffix, bool make_unique)
1033{
1034 char *global_var_name;
1035 int name_len;
1036 const char *name;
1037 const char *unique_name = NULL;
1038
1039 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1040
1041 /* Get a unique name that can be used globally without any chances
1042 of collision at link time. */
1043 if (make_unique)
1044 unique_name = IDENTIFIER_POINTER (get_file_function_name ("\0"));
1045
1046 name_len = strlen (name) + strlen (suffix) + 2;
1047
1048 if (make_unique)
1049 name_len += strlen (unique_name) + 1;
1050 global_var_name = XNEWVEC (char, name_len);
1051
1052 /* Use '.' to concatenate names as it is demangler friendly. */
1053 if (make_unique)
1054 snprintf (global_var_name, name_len, "%s.%s.%s", name, unique_name,
1055 suffix);
1056 else
1057 snprintf (global_var_name, name_len, "%s.%s", name, suffix);
1058
1059 return global_var_name;
1060}
1061
1062/* Make a dispatcher declaration for the multi-versioned function DECL.
1063 Calls to DECL function will be replaced with calls to the dispatcher
1064 by the front-end. Return the decl created. */
1065
1066tree
1067make_dispatcher_decl (const tree decl)
1068{
1069 tree func_decl;
1070 char *func_name;
1071 tree fn_type, func_type;
fd4f3a94 1072
d3cd4493 1073 func_name = xstrdup (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
fd4f3a94 1074
1075 fn_type = TREE_TYPE (decl);
1076 func_type = build_function_type (TREE_TYPE (fn_type),
1077 TYPE_ARG_TYPES (fn_type));
1078
1079 func_decl = build_fn_decl (func_name, func_type);
1080 XDELETEVEC (func_name);
1081 TREE_USED (func_decl) = 1;
1082 DECL_CONTEXT (func_decl) = NULL_TREE;
1083 DECL_INITIAL (func_decl) = error_mark_node;
1084 DECL_ARTIFICIAL (func_decl) = 1;
1085 /* Mark this func as external, the resolver will flip it again if
1086 it gets generated. */
1087 DECL_EXTERNAL (func_decl) = 1;
1088 /* This will be of type IFUNCs have to be externally visible. */
1089 TREE_PUBLIC (func_decl) = 1;
1090
1091 return func_decl;
1092}
1093
1094/* Returns true if decl is multi-versioned and DECL is the default function,
1095 that is it is not tagged with target specific optimization. */
1096
1097bool
1098is_function_default_version (const tree decl)
1099{
1100 if (TREE_CODE (decl) != FUNCTION_DECL
1101 || !DECL_FUNCTION_VERSIONED (decl))
1102 return false;
1103 tree attr = lookup_attribute ("target", DECL_ATTRIBUTES (decl));
1104 gcc_assert (attr);
1105 attr = TREE_VALUE (TREE_VALUE (attr));
1106 return (TREE_CODE (attr) == STRING_CST
1107 && strcmp (TREE_STRING_POINTER (attr), "default") == 0);
1108}
30a86690 1109
1110/* Return a declaration like DDECL except that its DECL_ATTRIBUTES
1111 is ATTRIBUTE. */
1112
1113tree
1114build_decl_attribute_variant (tree ddecl, tree attribute)
1115{
1116 DECL_ATTRIBUTES (ddecl) = attribute;
1117 return ddecl;
1118}
1119
1120/* Return a type like TTYPE except that its TYPE_ATTRIBUTE
1121 is ATTRIBUTE and its qualifiers are QUALS.
1122
1123 Record such modified types already made so we don't make duplicates. */
1124
1125tree
4edbdd46 1126build_type_attribute_qual_variant (tree otype, tree attribute, int quals)
30a86690 1127{
4edbdd46 1128 tree ttype = otype;
30a86690 1129 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
1130 {
1131 tree ntype;
1132
1133 /* Building a distinct copy of a tagged type is inappropriate; it
1134 causes breakage in code that expects there to be a one-to-one
1135 relationship between a struct and its fields.
1136 build_duplicate_type is another solution (as used in
1137 handle_transparent_union_attribute), but that doesn't play well
1138 with the stronger C++ type identity model. */
1139 if (TREE_CODE (ttype) == RECORD_TYPE
1140 || TREE_CODE (ttype) == UNION_TYPE
1141 || TREE_CODE (ttype) == QUAL_UNION_TYPE
1142 || TREE_CODE (ttype) == ENUMERAL_TYPE)
1143 {
1144 warning (OPT_Wattributes,
1145 "ignoring attributes applied to %qT after definition",
1146 TYPE_MAIN_VARIANT (ttype));
1147 return build_qualified_type (ttype, quals);
1148 }
1149
1150 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
4edbdd46 1151 if (lang_hooks.types.copy_lang_qualifiers
1152 && otype != TYPE_MAIN_VARIANT (otype))
1153 ttype = (lang_hooks.types.copy_lang_qualifiers
1154 (ttype, TYPE_MAIN_VARIANT (otype)));
1155
7422ddf0 1156 tree dtype = ntype = build_distinct_type_copy (ttype);
30a86690 1157
1158 TYPE_ATTRIBUTES (ntype) = attribute;
1159
1160 hashval_t hash = type_hash_canon_hash (ntype);
1161 ntype = type_hash_canon (hash, ntype);
1162
7422ddf0 1163 if (ntype != dtype)
1164 /* This variant was already in the hash table, don't mess with
1165 TYPE_CANONICAL. */;
1166 else if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
1167 || !comp_type_attributes (ntype, ttype))
7b79cb85 1168 /* If the target-dependent attributes make NTYPE different from
1169 its canonical type, we will need to use structural equality
1170 checks for this type.
1171
1172 We shouldn't get here for stripping attributes from a type;
1173 the no-attribute type might not need structural comparison. But
1174 we can if was discarded from type_hash_table. */
1175 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
30a86690 1176 else if (TYPE_CANONICAL (ntype) == ntype)
1177 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
1178
1179 ttype = build_qualified_type (ntype, quals);
4edbdd46 1180 if (lang_hooks.types.copy_lang_qualifiers
1181 && otype != TYPE_MAIN_VARIANT (otype))
1182 ttype = lang_hooks.types.copy_lang_qualifiers (ttype, otype);
30a86690 1183 }
1184 else if (TYPE_QUALS (ttype) != quals)
1185 ttype = build_qualified_type (ttype, quals);
1186
1187 return ttype;
1188}
1189
1190/* Compare two identifier nodes representing attributes.
1191 Return true if they are the same, false otherwise. */
1192
1193static bool
1194cmp_attrib_identifiers (const_tree attr1, const_tree attr2)
1195{
1196 /* Make sure we're dealing with IDENTIFIER_NODEs. */
1197 gcc_checking_assert (TREE_CODE (attr1) == IDENTIFIER_NODE
1198 && TREE_CODE (attr2) == IDENTIFIER_NODE);
1199
1200 /* Identifiers can be compared directly for equality. */
1201 if (attr1 == attr2)
1202 return true;
1203
1204 return cmp_attribs (IDENTIFIER_POINTER (attr1), IDENTIFIER_LENGTH (attr1),
1205 IDENTIFIER_POINTER (attr2), IDENTIFIER_LENGTH (attr2));
1206}
1207
1208/* Compare two constructor-element-type constants. Return 1 if the lists
1209 are known to be equal; otherwise return 0. */
1210
1211static bool
1212simple_cst_list_equal (const_tree l1, const_tree l2)
1213{
1214 while (l1 != NULL_TREE && l2 != NULL_TREE)
1215 {
1216 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
1217 return false;
1218
1219 l1 = TREE_CHAIN (l1);
1220 l2 = TREE_CHAIN (l2);
1221 }
1222
1223 return l1 == l2;
1224}
1225
1226/* Check if "omp declare simd" attribute arguments, CLAUSES1 and CLAUSES2, are
1227 the same. */
1228
1229static bool
1230omp_declare_simd_clauses_equal (tree clauses1, tree clauses2)
1231{
1232 tree cl1, cl2;
1233 for (cl1 = clauses1, cl2 = clauses2;
1234 cl1 && cl2;
1235 cl1 = OMP_CLAUSE_CHAIN (cl1), cl2 = OMP_CLAUSE_CHAIN (cl2))
1236 {
1237 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_CODE (cl2))
1238 return false;
1239 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_SIMDLEN)
1240 {
1241 if (simple_cst_equal (OMP_CLAUSE_DECL (cl1),
1242 OMP_CLAUSE_DECL (cl2)) != 1)
1243 return false;
1244 }
1245 switch (OMP_CLAUSE_CODE (cl1))
1246 {
1247 case OMP_CLAUSE_ALIGNED:
1248 if (simple_cst_equal (OMP_CLAUSE_ALIGNED_ALIGNMENT (cl1),
1249 OMP_CLAUSE_ALIGNED_ALIGNMENT (cl2)) != 1)
1250 return false;
1251 break;
1252 case OMP_CLAUSE_LINEAR:
1253 if (simple_cst_equal (OMP_CLAUSE_LINEAR_STEP (cl1),
1254 OMP_CLAUSE_LINEAR_STEP (cl2)) != 1)
1255 return false;
1256 break;
1257 case OMP_CLAUSE_SIMDLEN:
1258 if (simple_cst_equal (OMP_CLAUSE_SIMDLEN_EXPR (cl1),
1259 OMP_CLAUSE_SIMDLEN_EXPR (cl2)) != 1)
1260 return false;
1261 default:
1262 break;
1263 }
1264 }
1265 return true;
1266}
1267
1268
1269/* Compare two attributes for their value identity. Return true if the
1270 attribute values are known to be equal; otherwise return false. */
1271
1272bool
1273attribute_value_equal (const_tree attr1, const_tree attr2)
1274{
1275 if (TREE_VALUE (attr1) == TREE_VALUE (attr2))
1276 return true;
1277
1278 if (TREE_VALUE (attr1) != NULL_TREE
1279 && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST
1280 && TREE_VALUE (attr2) != NULL_TREE
1281 && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST)
1282 {
1283 /* Handle attribute format. */
1284 if (is_attribute_p ("format", get_attribute_name (attr1)))
1285 {
1286 attr1 = TREE_VALUE (attr1);
1287 attr2 = TREE_VALUE (attr2);
1288 /* Compare the archetypes (printf/scanf/strftime/...). */
1289 if (!cmp_attrib_identifiers (TREE_VALUE (attr1), TREE_VALUE (attr2)))
1290 return false;
1291 /* Archetypes are the same. Compare the rest. */
1292 return (simple_cst_list_equal (TREE_CHAIN (attr1),
1293 TREE_CHAIN (attr2)) == 1);
1294 }
1295 return (simple_cst_list_equal (TREE_VALUE (attr1),
1296 TREE_VALUE (attr2)) == 1);
1297 }
1298
ac50cb09 1299 if (TREE_VALUE (attr1)
30a86690 1300 && TREE_CODE (TREE_VALUE (attr1)) == OMP_CLAUSE
ac50cb09 1301 && TREE_VALUE (attr2)
30a86690 1302 && TREE_CODE (TREE_VALUE (attr2)) == OMP_CLAUSE)
1303 return omp_declare_simd_clauses_equal (TREE_VALUE (attr1),
1304 TREE_VALUE (attr2));
1305
1306 return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1);
1307}
1308
1309/* Return 0 if the attributes for two types are incompatible, 1 if they
1310 are compatible, and 2 if they are nearly compatible (which causes a
1311 warning to be generated). */
1312int
1313comp_type_attributes (const_tree type1, const_tree type2)
1314{
1315 const_tree a1 = TYPE_ATTRIBUTES (type1);
1316 const_tree a2 = TYPE_ATTRIBUTES (type2);
1317 const_tree a;
1318
1319 if (a1 == a2)
1320 return 1;
1321 for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
1322 {
1323 const struct attribute_spec *as;
1324 const_tree attr;
1325
1326 as = lookup_attribute_spec (get_attribute_name (a));
1327 if (!as || as->affects_type_identity == false)
1328 continue;
1329
1330 attr = lookup_attribute (as->name, CONST_CAST_TREE (a2));
1331 if (!attr || !attribute_value_equal (a, attr))
1332 break;
1333 }
1334 if (!a)
1335 {
1336 for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
1337 {
1338 const struct attribute_spec *as;
1339
1340 as = lookup_attribute_spec (get_attribute_name (a));
1341 if (!as || as->affects_type_identity == false)
1342 continue;
1343
1344 if (!lookup_attribute (as->name, CONST_CAST_TREE (a1)))
1345 break;
1346 /* We don't need to compare trees again, as we did this
1347 already in first loop. */
1348 }
1349 /* All types - affecting identity - are equal, so
1350 there is no need to call target hook for comparison. */
1351 if (!a)
1352 return 1;
1353 }
1354 if (lookup_attribute ("transaction_safe", CONST_CAST_TREE (a)))
1355 return 0;
3c0f15b4 1356 if ((lookup_attribute ("nocf_check", TYPE_ATTRIBUTES (type1)) != NULL)
1357 ^ (lookup_attribute ("nocf_check", TYPE_ATTRIBUTES (type2)) != NULL))
1358 return 0;
30a86690 1359 /* As some type combinations - like default calling-convention - might
1360 be compatible, we have to call the target hook to get the final result. */
1361 return targetm.comp_type_attributes (type1, type2);
1362}
1363
1364/* Return a type like TTYPE except that its TYPE_ATTRIBUTE
1365 is ATTRIBUTE.
1366
1367 Record such modified types already made so we don't make duplicates. */
1368
1369tree
1370build_type_attribute_variant (tree ttype, tree attribute)
1371{
1372 return build_type_attribute_qual_variant (ttype, attribute,
1373 TYPE_QUALS (ttype));
1374}
1375\f
1376/* A variant of lookup_attribute() that can be used with an identifier
1377 as the first argument, and where the identifier can be either
1378 'text' or '__text__'.
1379
1380 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
1381 return a pointer to the attribute's list element if the attribute
1382 is part of the list, or NULL_TREE if not found. If the attribute
1383 appears more than once, this only returns the first occurrence; the
1384 TREE_CHAIN of the return value should be passed back in if further
1385 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
1386 can be in the form 'text' or '__text__'. */
1387static tree
1388lookup_ident_attribute (tree attr_identifier, tree list)
1389{
1390 gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE);
1391
1392 while (list)
1393 {
1394 gcc_checking_assert (TREE_CODE (get_attribute_name (list))
1395 == IDENTIFIER_NODE);
1396
1397 if (cmp_attrib_identifiers (attr_identifier,
1398 get_attribute_name (list)))
1399 /* Found it. */
1400 break;
1401 list = TREE_CHAIN (list);
1402 }
1403
1404 return list;
1405}
1406
1407/* Remove any instances of attribute ATTR_NAME in LIST and return the
1408 modified list. */
1409
1410tree
1411remove_attribute (const char *attr_name, tree list)
1412{
1413 tree *p;
1414 gcc_checking_assert (attr_name[0] != '_');
1415
1416 for (p = &list; *p;)
1417 {
1418 tree l = *p;
1419
1420 tree attr = get_attribute_name (l);
1421 if (is_attribute_p (attr_name, attr))
1422 *p = TREE_CHAIN (l);
1423 else
1424 p = &TREE_CHAIN (l);
1425 }
1426
1427 return list;
1428}
1429
1430/* Return an attribute list that is the union of a1 and a2. */
1431
1432tree
1433merge_attributes (tree a1, tree a2)
1434{
1435 tree attributes;
1436
1437 /* Either one unset? Take the set one. */
1438
1439 if ((attributes = a1) == 0)
1440 attributes = a2;
1441
1442 /* One that completely contains the other? Take it. */
1443
1444 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
1445 {
1446 if (attribute_list_contained (a2, a1))
1447 attributes = a2;
1448 else
1449 {
1450 /* Pick the longest list, and hang on the other list. */
1451
1452 if (list_length (a1) < list_length (a2))
1453 attributes = a2, a2 = a1;
1454
1455 for (; a2 != 0; a2 = TREE_CHAIN (a2))
1456 {
1457 tree a;
1458 for (a = lookup_ident_attribute (get_attribute_name (a2),
1459 attributes);
1460 a != NULL_TREE && !attribute_value_equal (a, a2);
1461 a = lookup_ident_attribute (get_attribute_name (a2),
1462 TREE_CHAIN (a)))
1463 ;
1464 if (a == NULL_TREE)
1465 {
1466 a1 = copy_node (a2);
1467 TREE_CHAIN (a1) = attributes;
1468 attributes = a1;
1469 }
1470 }
1471 }
1472 }
1473 return attributes;
1474}
1475
1476/* Given types T1 and T2, merge their attributes and return
1477 the result. */
1478
1479tree
1480merge_type_attributes (tree t1, tree t2)
1481{
1482 return merge_attributes (TYPE_ATTRIBUTES (t1),
1483 TYPE_ATTRIBUTES (t2));
1484}
1485
1486/* Given decls OLDDECL and NEWDECL, merge their attributes and return
1487 the result. */
1488
1489tree
1490merge_decl_attributes (tree olddecl, tree newdecl)
1491{
1492 return merge_attributes (DECL_ATTRIBUTES (olddecl),
1493 DECL_ATTRIBUTES (newdecl));
1494}
1495
ac50cb09 1496/* Duplicate all attributes with name NAME in ATTR list to *ATTRS if
1497 they are missing there. */
1498
1499void
1500duplicate_one_attribute (tree *attrs, tree attr, const char *name)
1501{
1502 attr = lookup_attribute (name, attr);
1503 if (!attr)
1504 return;
1505 tree a = lookup_attribute (name, *attrs);
1506 while (attr)
1507 {
1508 tree a2;
1509 for (a2 = a; a2; a2 = lookup_attribute (name, TREE_CHAIN (a2)))
1510 if (attribute_value_equal (attr, a2))
1511 break;
1512 if (!a2)
1513 {
1514 a2 = copy_node (attr);
1515 TREE_CHAIN (a2) = *attrs;
1516 *attrs = a2;
1517 }
1518 attr = lookup_attribute (name, TREE_CHAIN (attr));
1519 }
1520}
1521
1522/* Duplicate all attributes from user DECL to the corresponding
1523 builtin that should be propagated. */
1524
1525void
1526copy_attributes_to_builtin (tree decl)
1527{
1528 tree b = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
1529 if (b)
1530 duplicate_one_attribute (&DECL_ATTRIBUTES (b),
1531 DECL_ATTRIBUTES (decl), "omp declare simd");
1532}
1533
30a86690 1534#if TARGET_DLLIMPORT_DECL_ATTRIBUTES
1535
1536/* Specialization of merge_decl_attributes for various Windows targets.
1537
1538 This handles the following situation:
1539
1540 __declspec (dllimport) int foo;
1541 int foo;
1542
1543 The second instance of `foo' nullifies the dllimport. */
1544
1545tree
1546merge_dllimport_decl_attributes (tree old, tree new_tree)
1547{
1548 tree a;
1549 int delete_dllimport_p = 1;
1550
1551 /* What we need to do here is remove from `old' dllimport if it doesn't
1552 appear in `new'. dllimport behaves like extern: if a declaration is
1553 marked dllimport and a definition appears later, then the object
1554 is not dllimport'd. We also remove a `new' dllimport if the old list
1555 contains dllexport: dllexport always overrides dllimport, regardless
1556 of the order of declaration. */
1557 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
1558 delete_dllimport_p = 0;
1559 else if (DECL_DLLIMPORT_P (new_tree)
1560 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
1561 {
1562 DECL_DLLIMPORT_P (new_tree) = 0;
1563 warning (OPT_Wattributes, "%q+D already declared with dllexport "
1564 "attribute: dllimport ignored", new_tree);
1565 }
1566 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
1567 {
1568 /* Warn about overriding a symbol that has already been used, e.g.:
1569 extern int __attribute__ ((dllimport)) foo;
1570 int* bar () {return &foo;}
1571 int foo;
1572 */
1573 if (TREE_USED (old))
1574 {
1575 warning (0, "%q+D redeclared without dllimport attribute "
1576 "after being referenced with dll linkage", new_tree);
1577 /* If we have used a variable's address with dllimport linkage,
1578 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
1579 decl may already have had TREE_CONSTANT computed.
1580 We still remove the attribute so that assembler code refers
1581 to '&foo rather than '_imp__foo'. */
1582 if (VAR_P (old) && TREE_ADDRESSABLE (old))
1583 DECL_DLLIMPORT_P (new_tree) = 1;
1584 }
1585
1586 /* Let an inline definition silently override the external reference,
1587 but otherwise warn about attribute inconsistency. */
1588 else if (VAR_P (new_tree) || !DECL_DECLARED_INLINE_P (new_tree))
1589 warning (OPT_Wattributes, "%q+D redeclared without dllimport "
1590 "attribute: previous dllimport ignored", new_tree);
1591 }
1592 else
1593 delete_dllimport_p = 0;
1594
1595 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
1596
1597 if (delete_dllimport_p)
1598 a = remove_attribute ("dllimport", a);
1599
1600 return a;
1601}
1602
1603/* Handle a "dllimport" or "dllexport" attribute; arguments as in
1604 struct attribute_spec.handler. */
1605
1606tree
1607handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
1608 bool *no_add_attrs)
1609{
1610 tree node = *pnode;
1611 bool is_dllimport;
1612
1613 /* These attributes may apply to structure and union types being created,
1614 but otherwise should pass to the declaration involved. */
1615 if (!DECL_P (node))
1616 {
1617 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
1618 | (int) ATTR_FLAG_ARRAY_NEXT))
1619 {
1620 *no_add_attrs = true;
1621 return tree_cons (name, args, NULL_TREE);
1622 }
1623 if (TREE_CODE (node) == RECORD_TYPE
1624 || TREE_CODE (node) == UNION_TYPE)
1625 {
1626 node = TYPE_NAME (node);
1627 if (!node)
1628 return NULL_TREE;
1629 }
1630 else
1631 {
1632 warning (OPT_Wattributes, "%qE attribute ignored",
1633 name);
1634 *no_add_attrs = true;
1635 return NULL_TREE;
1636 }
1637 }
1638
1639 if (!VAR_OR_FUNCTION_DECL_P (node) && TREE_CODE (node) != TYPE_DECL)
1640 {
1641 *no_add_attrs = true;
1642 warning (OPT_Wattributes, "%qE attribute ignored",
1643 name);
1644 return NULL_TREE;
1645 }
1646
1647 if (TREE_CODE (node) == TYPE_DECL
1648 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
1649 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
1650 {
1651 *no_add_attrs = true;
1652 warning (OPT_Wattributes, "%qE attribute ignored",
1653 name);
1654 return NULL_TREE;
1655 }
1656
1657 is_dllimport = is_attribute_p ("dllimport", name);
1658
1659 /* Report error on dllimport ambiguities seen now before they cause
1660 any damage. */
1661 if (is_dllimport)
1662 {
1663 /* Honor any target-specific overrides. */
1664 if (!targetm.valid_dllimport_attribute_p (node))
1665 *no_add_attrs = true;
1666
1667 else if (TREE_CODE (node) == FUNCTION_DECL
1668 && DECL_DECLARED_INLINE_P (node))
1669 {
1670 warning (OPT_Wattributes, "inline function %q+D declared as "
000969f9 1671 "dllimport: attribute ignored", node);
30a86690 1672 *no_add_attrs = true;
1673 }
1674 /* Like MS, treat definition of dllimported variables and
1675 non-inlined functions on declaration as syntax errors. */
1676 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
1677 {
1678 error ("function %q+D definition is marked dllimport", node);
1679 *no_add_attrs = true;
1680 }
1681
1682 else if (VAR_P (node))
1683 {
1684 if (DECL_INITIAL (node))
1685 {
1686 error ("variable %q+D definition is marked dllimport",
1687 node);
1688 *no_add_attrs = true;
1689 }
1690
1691 /* `extern' needn't be specified with dllimport.
1692 Specify `extern' now and hope for the best. Sigh. */
1693 DECL_EXTERNAL (node) = 1;
1694 /* Also, implicitly give dllimport'd variables declared within
1695 a function global scope, unless declared static. */
1696 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
1697 TREE_PUBLIC (node) = 1;
909c7f25 1698 /* Clear TREE_STATIC because DECL_EXTERNAL is set, unless
1699 it is a C++ static data member. */
1700 if (DECL_CONTEXT (node) == NULL_TREE
1701 || !RECORD_OR_UNION_TYPE_P (DECL_CONTEXT (node)))
1702 TREE_STATIC (node) = 0;
30a86690 1703 }
1704
1705 if (*no_add_attrs == false)
1706 DECL_DLLIMPORT_P (node) = 1;
1707 }
1708 else if (TREE_CODE (node) == FUNCTION_DECL
1709 && DECL_DECLARED_INLINE_P (node)
1710 && flag_keep_inline_dllexport)
1711 /* An exported function, even if inline, must be emitted. */
1712 DECL_EXTERNAL (node) = 0;
1713
1714 /* Report error if symbol is not accessible at global scope. */
1715 if (!TREE_PUBLIC (node) && VAR_OR_FUNCTION_DECL_P (node))
1716 {
1717 error ("external linkage required for symbol %q+D because of "
1718 "%qE attribute", node, name);
1719 *no_add_attrs = true;
1720 }
1721
1722 /* A dllexport'd entity must have default visibility so that other
1723 program units (shared libraries or the main executable) can see
1724 it. A dllimport'd entity must have default visibility so that
1725 the linker knows that undefined references within this program
1726 unit can be resolved by the dynamic linker. */
1727 if (!*no_add_attrs)
1728 {
1729 if (DECL_VISIBILITY_SPECIFIED (node)
1730 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
1731 error ("%qE implies default visibility, but %qD has already "
1732 "been declared with a different visibility",
1733 name, node);
1734 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
1735 DECL_VISIBILITY_SPECIFIED (node) = 1;
1736 }
1737
1738 return NULL_TREE;
1739}
1740
1741#endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
1742
1743/* Given two lists of attributes, return true if list l2 is
1744 equivalent to l1. */
1745
1746int
1747attribute_list_equal (const_tree l1, const_tree l2)
1748{
1749 if (l1 == l2)
1750 return 1;
1751
1752 return attribute_list_contained (l1, l2)
1753 && attribute_list_contained (l2, l1);
1754}
1755
1756/* Given two lists of attributes, return true if list L2 is
1757 completely contained within L1. */
1758/* ??? This would be faster if attribute names were stored in a canonicalized
1759 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
1760 must be used to show these elements are equivalent (which they are). */
1761/* ??? It's not clear that attributes with arguments will always be handled
1762 correctly. */
1763
1764int
1765attribute_list_contained (const_tree l1, const_tree l2)
1766{
1767 const_tree t1, t2;
1768
1769 /* First check the obvious, maybe the lists are identical. */
1770 if (l1 == l2)
1771 return 1;
1772
1773 /* Maybe the lists are similar. */
1774 for (t1 = l1, t2 = l2;
1775 t1 != 0 && t2 != 0
1776 && get_attribute_name (t1) == get_attribute_name (t2)
1777 && TREE_VALUE (t1) == TREE_VALUE (t2);
1778 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1779 ;
1780
1781 /* Maybe the lists are equal. */
1782 if (t1 == 0 && t2 == 0)
1783 return 1;
1784
1785 for (; t2 != 0; t2 = TREE_CHAIN (t2))
1786 {
1787 const_tree attr;
1788 /* This CONST_CAST is okay because lookup_attribute does not
1789 modify its argument and the return value is assigned to a
1790 const_tree. */
1791 for (attr = lookup_ident_attribute (get_attribute_name (t2),
1792 CONST_CAST_TREE (l1));
1793 attr != NULL_TREE && !attribute_value_equal (t2, attr);
1794 attr = lookup_ident_attribute (get_attribute_name (t2),
1795 TREE_CHAIN (attr)))
1796 ;
1797
1798 if (attr == NULL_TREE)
1799 return 0;
1800 }
1801
1802 return 1;
1803}
7b35a600 1804
1805/* The backbone of lookup_attribute(). ATTR_LEN is the string length
1806 of ATTR_NAME, and LIST is not NULL_TREE.
1807
1808 The function is called from lookup_attribute in order to optimize
1809 for size. */
1810
1811tree
1812private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
1813{
1814 while (list)
1815 {
1816 tree attr = get_attribute_name (list);
1817 size_t ident_len = IDENTIFIER_LENGTH (attr);
1818 if (cmp_attribs (attr_name, attr_len, IDENTIFIER_POINTER (attr),
1819 ident_len))
1820 break;
1821 list = TREE_CHAIN (list);
1822 }
1823
1824 return list;
1825}
dab0e385 1826
08cc1019 1827/* Return true if the function decl or type NODE has been declared
1828 with attribute ANAME among attributes ATTRS. */
1829
1830static bool
1831has_attribute (tree node, tree attrs, const char *aname)
1832{
1833 if (!strcmp (aname, "const"))
1834 {
1835 if (DECL_P (node) && TREE_READONLY (node))
1836 return true;
1837 }
1838 else if (!strcmp (aname, "malloc"))
1839 {
1840 if (DECL_P (node) && DECL_IS_MALLOC (node))
1841 return true;
1842 }
1843 else if (!strcmp (aname, "noreturn"))
1844 {
1845 if (DECL_P (node) && TREE_THIS_VOLATILE (node))
1846 return true;
1847 }
1848 else if (!strcmp (aname, "nothrow"))
1849 {
1850 if (TREE_NOTHROW (node))
1851 return true;
1852 }
1853 else if (!strcmp (aname, "pure"))
1854 {
1855 if (DECL_P (node) && DECL_PURE_P (node))
1856 return true;
1857 }
1858
1859 return lookup_attribute (aname, attrs);
1860}
1861
1862/* Return the number of mismatched function or type attributes between
1863 the "template" function declaration TMPL and DECL. The word "template"
1864 doesn't necessarily refer to a C++ template but rather a declaration
1865 whose attributes should be matched by those on DECL. For a non-zero
1866 return value set *ATTRSTR to a string representation of the list of
1867 mismatched attributes with quoted names.
1868 ATTRLIST is a list of additional attributes that SPEC should be
1869 taken to ultimately be declared with. */
1870
1871unsigned
1872decls_mismatched_attributes (tree tmpl, tree decl, tree attrlist,
1873 const char* const blacklist[],
1874 pretty_printer *attrstr)
1875{
1876 if (TREE_CODE (tmpl) != FUNCTION_DECL)
1877 return 0;
1878
1879 /* Avoid warning if either declaration or its type is deprecated. */
1880 if (TREE_DEPRECATED (tmpl)
1881 || TREE_DEPRECATED (decl))
1882 return 0;
1883
1884 const tree tmpls[] = { tmpl, TREE_TYPE (tmpl) };
1885 const tree decls[] = { decl, TREE_TYPE (decl) };
1886
1887 if (TREE_DEPRECATED (tmpls[1])
1888 || TREE_DEPRECATED (decls[1])
1889 || TREE_DEPRECATED (TREE_TYPE (tmpls[1]))
1890 || TREE_DEPRECATED (TREE_TYPE (decls[1])))
1891 return 0;
1892
1893 tree tmpl_attrs[] = { DECL_ATTRIBUTES (tmpl), TYPE_ATTRIBUTES (tmpls[1]) };
1894 tree decl_attrs[] = { DECL_ATTRIBUTES (decl), TYPE_ATTRIBUTES (decls[1]) };
1895
1896 if (!decl_attrs[0])
1897 decl_attrs[0] = attrlist;
1898 else if (!decl_attrs[1])
1899 decl_attrs[1] = attrlist;
1900
1901 /* Avoid warning if the template has no attributes. */
1902 if (!tmpl_attrs[0] && !tmpl_attrs[1])
1903 return 0;
1904
1905 /* Avoid warning if either declaration contains an attribute on
1906 the white list below. */
1907 const char* const whitelist[] = {
1908 "error", "warning"
1909 };
1910
1911 for (unsigned i = 0; i != 2; ++i)
1912 for (unsigned j = 0; j != sizeof whitelist / sizeof *whitelist; ++j)
1913 if (lookup_attribute (whitelist[j], tmpl_attrs[i])
1914 || lookup_attribute (whitelist[j], decl_attrs[i]))
1915 return 0;
1916
1917 /* Put together a list of the black-listed attributes that the template
1918 is declared with and the declaration is not, in case it's not apparent
1919 from the most recent declaration of the template. */
1920 unsigned nattrs = 0;
1921
1922 for (unsigned i = 0; blacklist[i]; ++i)
1923 {
dd1ab8da 1924 /* Attribute leaf only applies to extern functions. Avoid mentioning
1925 it when it's missing from a static declaration. */
1926 if (!TREE_PUBLIC (decl)
1927 && !strcmp ("leaf", blacklist[i]))
1928 continue;
1929
08cc1019 1930 for (unsigned j = 0; j != 2; ++j)
1931 {
1932 if (!has_attribute (tmpls[j], tmpl_attrs[j], blacklist[i]))
1933 continue;
1934
8ac9f335 1935 bool found = false;
08cc1019 1936 unsigned kmax = 1 + !!decl_attrs[1];
1937 for (unsigned k = 0; k != kmax; ++k)
1938 {
1939 if (has_attribute (decls[k], decl_attrs[k], blacklist[i]))
8ac9f335 1940 {
1941 found = true;
1942 break;
1943 }
1944 }
08cc1019 1945
8ac9f335 1946 if (!found)
1947 {
08cc1019 1948 if (nattrs)
1949 pp_string (attrstr, ", ");
1950 pp_begin_quote (attrstr, pp_show_color (global_dc->printer));
1951 pp_string (attrstr, blacklist[i]);
1952 pp_end_quote (attrstr, pp_show_color (global_dc->printer));
1953 ++nattrs;
1954 }
8ac9f335 1955
1956 break;
08cc1019 1957 }
1958 }
1959
1960 return nattrs;
1961}
1962
1963/* Issue a warning for the declaration ALIAS for TARGET where ALIAS
1964 specifies either attributes that are incompatible with those of
1965 TARGET, or attributes that are missing and that declaring ALIAS
1966 with would benefit. */
1967
1968void
1969maybe_diag_alias_attributes (tree alias, tree target)
1970{
1971 /* Do not expect attributes to match between aliases and ifunc
1972 resolvers. There is no obvious correspondence between them. */
1973 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (alias)))
1974 return;
1975
1976 const char* const blacklist[] = {
1977 "alloc_align", "alloc_size", "cold", "const", "hot", "leaf", "malloc",
1978 "nonnull", "noreturn", "nothrow", "pure", "returns_nonnull",
1979 "returns_twice", NULL
1980 };
1981
1982 pretty_printer attrnames;
1983 if (warn_attribute_alias > 1)
1984 {
1985 /* With -Wattribute-alias=2 detect alias declarations that are more
1986 restrictive than their targets first. Those indicate potential
1987 codegen bugs. */
1988 if (unsigned n = decls_mismatched_attributes (alias, target, NULL_TREE,
1989 blacklist, &attrnames))
1990 {
1991 auto_diagnostic_group d;
1992 if (warning_n (DECL_SOURCE_LOCATION (alias),
1993 OPT_Wattribute_alias_, n,
1994 "%qD specifies more restrictive attribute than "
1995 "its target %qD: %s",
1996 "%qD specifies more restrictive attributes than "
1997 "its target %qD: %s",
1998 alias, target, pp_formatted_text (&attrnames)))
1999 inform (DECL_SOURCE_LOCATION (target),
2000 "%qD target declared here", alias);
2001 return;
2002 }
2003 }
2004
2005 /* Detect alias declarations that are less restrictive than their
2006 targets. Those suggest potential optimization opportunities
2007 (solved by adding the missing attribute(s) to the alias). */
2008 if (unsigned n = decls_mismatched_attributes (target, alias, NULL_TREE,
2009 blacklist, &attrnames))
2010 {
2011 auto_diagnostic_group d;
2012 if (warning_n (DECL_SOURCE_LOCATION (alias),
2013 OPT_Wmissing_attributes, n,
2014 "%qD specifies less restrictive attribute than "
2015 "its target %qD: %s",
2016 "%qD specifies less restrictive attributes than "
2017 "its target %qD: %s",
2018 alias, target, pp_formatted_text (&attrnames)))
2019 inform (DECL_SOURCE_LOCATION (target),
2020 "%qD target declared here", alias);
2021 }
2022}
2023
2024
dab0e385 2025#if CHECKING_P
2026
2027namespace selftest
2028{
2029
2030/* Helper types to verify the consistency attribute exclusions. */
2031
2032typedef std::pair<const char *, const char *> excl_pair;
2033
2034struct excl_hash_traits: typed_noop_remove<excl_pair>
2035{
2036 typedef excl_pair value_type;
2037 typedef value_type compare_type;
2038
2039 static hashval_t hash (const value_type &x)
2040 {
2041 hashval_t h1 = htab_hash_string (x.first);
2042 hashval_t h2 = htab_hash_string (x.second);
2043 return h1 ^ h2;
2044 }
2045
2046 static bool equal (const value_type &x, const value_type &y)
2047 {
2048 return !strcmp (x.first, y.first) && !strcmp (x.second, y.second);
2049 }
2050
2051 static void mark_deleted (value_type &x)
2052 {
2053 x = value_type (NULL, NULL);
2054 }
2055
2056 static void mark_empty (value_type &x)
2057 {
2058 x = value_type ("", "");
2059 }
2060
2061 static bool is_deleted (const value_type &x)
2062 {
2063 return !x.first && !x.second;
2064 }
2065
2066 static bool is_empty (const value_type &x)
2067 {
2068 return !*x.first && !*x.second;
2069 }
2070};
2071
2072
2073/* Self-test to verify that each attribute exclusion is symmetric,
2074 meaning that if attribute A is encoded as incompatible with
2075 attribute B then the opposite relationship is also encoded.
2076 This test also detects most cases of misspelled attribute names
2077 in exclusions. */
2078
2079static void
2080test_attribute_exclusions ()
2081{
2082 /* Iterate over the array of attribute tables first (with TI0 as
2083 the index) and over the array of attribute_spec in each table
2084 (with SI0 as the index). */
2085 const size_t ntables = ARRAY_SIZE (attribute_tables);
2086
2087 /* Set of pairs of mutually exclusive attributes. */
067e9a50 2088 typedef hash_set<excl_pair, false, excl_hash_traits> exclusion_set;
dab0e385 2089 exclusion_set excl_set;
2090
2091 for (size_t ti0 = 0; ti0 != ntables; ++ti0)
2092 for (size_t s0 = 0; attribute_tables[ti0][s0].name; ++s0)
2093 {
2094 const attribute_spec::exclusions *excl
2095 = attribute_tables[ti0][s0].exclude;
2096
2097 /* Skip each attribute that doesn't define exclusions. */
2098 if (!excl)
2099 continue;
2100
2101 const char *attr_name = attribute_tables[ti0][s0].name;
2102
2103 /* Iterate over the set of exclusions for every attribute
2104 (with EI0 as the index) adding the exclusions defined
2105 for each to the set. */
2106 for (size_t ei0 = 0; excl[ei0].name; ++ei0)
2107 {
2108 const char *excl_name = excl[ei0].name;
2109
2110 if (!strcmp (attr_name, excl_name))
2111 continue;
2112
2113 excl_set.add (excl_pair (attr_name, excl_name));
2114 }
2115 }
2116
2117 /* Traverse the set of mutually exclusive pairs of attributes
2118 and verify that they are symmetric. */
2119 for (exclusion_set::iterator it = excl_set.begin ();
2120 it != excl_set.end ();
2121 ++it)
2122 {
2123 if (!excl_set.contains (excl_pair ((*it).second, (*it).first)))
2124 {
2125 /* An exclusion for an attribute has been found that
2126 doesn't have a corresponding exclusion in the opposite
2127 direction. */
2128 char desc[120];
2129 sprintf (desc, "'%s' attribute exclusion '%s' must be symmetric",
2130 (*it).first, (*it).second);
2131 fail (SELFTEST_LOCATION, desc);
2132 }
2133 }
2134}
2135
2136void
2137attribute_c_tests ()
2138{
2139 test_attribute_exclusions ();
2140}
2141
2142} /* namespace selftest */
2143
2144#endif /* CHECKING_P */