]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/attribs.c
re PR target/79645 (missing period in microblaze.opt)
[thirdparty/gcc.git] / gcc / attribs.c
CommitLineData
bb9f8221 1/* Functions dealing with attribute handling, used by most front ends.
a5544970 2 Copyright (C) 1992-2019 Free Software Foundation, Inc.
bb9f8221
RK
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
9dcd6f09 8Software Foundation; either version 3, or (at your option) any later
bb9f8221
RK
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
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
bb9f8221
RK
19
20#include "config.h"
21#include "system.h"
4977bab6 22#include "coretypes.h"
957060b5 23#include "target.h"
bb9f8221 24#include "tree.h"
d8a2d370 25#include "stringpool.h"
957060b5 26#include "diagnostic-core.h"
d8a2d370
DN
27#include "attribs.h"
28#include "stor-layout.h"
7ffb4fd2 29#include "langhooks.h"
d1c8e08a 30#include "plugin.h"
5d9ae53d
MS
31#include "selftest.h"
32#include "hash-set.h"
79a2c428
MS
33#include "diagnostic.h"
34#include "pretty-print.h"
35#include "intl.h"
bb9f8221 36
349ae713 37/* Table of the tables of attributes (common, language, format, machine)
bb9f8221
RK
38 searched. */
39static const struct attribute_spec *attribute_tables[4];
40
23b43207
JH
41/* Substring representation. */
42
43struct substring
44{
45 const char *str;
46 int length;
47};
48
4a8fb1a1
LC
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
8d67ee55 59struct attribute_hasher : nofree_ptr_hash <attribute_spec>
4a8fb1a1 60{
67f58944
TS
61 typedef substring *compare_type;
62 static inline hashval_t hash (const attribute_spec *);
63 static inline bool equal (const attribute_spec *, const substring *);
4a8fb1a1
LC
64};
65
66inline hashval_t
67f58944 67attribute_hasher::hash (const attribute_spec *spec)
4a8fb1a1
LC
68{
69 const int l = strlen (spec->name);
70 return substring_hash (spec->name, l);
71}
72
73inline bool
67f58944 74attribute_hasher::equal (const attribute_spec *spec, const substring *str)
4a8fb1a1
LC
75{
76 return (strncmp (spec->name, str->str, str->length) == 0
77 && !spec->name[str->length]);
78}
79
e28d52cf
DS
80/* Scoped attribute name representation. */
81
82struct scoped_attributes
83{
84 const char *ns;
9771b263 85 vec<attribute_spec> attributes;
c203e8a7 86 hash_table<attribute_hasher> *attribute_hash;
e28d52cf
DS
87};
88
e28d52cf 89/* The table of scope attributes. */
9771b263 90static vec<scoped_attributes> attributes_table;
e28d52cf
DS
91
92static scoped_attributes* find_attribute_namespace (const char*);
93static void register_scoped_attribute (const struct attribute_spec *,
94 scoped_attributes *);
95
bb9f8221
RK
96static bool attributes_initialized = false;
97
bb9f8221 98/* Default empty table of attributes. */
23b43207 99
bb9f8221
RK
100static const struct attribute_spec empty_attribute_table[] =
101{
4849deb1 102 { NULL, 0, 0, false, false, false, false, NULL, NULL }
bb9f8221
RK
103};
104
23b43207
JH
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
e28d52cf
DS
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
4849deb1
JJ
124scoped_attributes *
125register_scoped_attributes (const struct attribute_spec *attributes,
126 const char *ns)
e28d52cf
DS
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
d067e05f 138 if (attributes_table.is_empty ())
9771b263 139 attributes_table.create (64);
e28d52cf
DS
140
141 memset (&sa, 0, sizeof (sa));
142 sa.ns = ns;
9771b263
DN
143 sa.attributes.create (64);
144 result = attributes_table.safe_push (sa);
c203e8a7 145 result->attribute_hash = new hash_table<attribute_hasher> (200);
e28d52cf
DS
146 }
147
148 /* Really add the attributes to their namespace now. */
149 for (unsigned i = 0; attributes[i].name != NULL; ++i)
150 {
9771b263 151 result->attributes.safe_push (attributes[i]);
e28d52cf
DS
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
9771b263 168 FOR_EACH_VEC_ELT (attributes_table, ix, iter)
e28d52cf
DS
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
b2b29377
MM
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. */
bb9f8221 229
8dd00781 230void
4682ae04 231init_attributes (void)
bb9f8221 232{
ca7558fc 233 size_t i;
bb9f8221 234
8dd00781
JJ
235 if (attributes_initialized)
236 return;
237
349ae713
NB
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;
bb9f8221
RK
241 attribute_tables[3] = targetm.attribute_table;
242
349ae713
NB
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
b2b29377
MM
248 if (flag_checking)
249 check_attribute_tables ();
bb9f8221 250
e28d52cf
DS
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
d1c8e08a
TG
255 invoke_plugin_callbacks (PLUGIN_ATTRIBUTES, NULL);
256 attributes_initialized = true;
257}
258
259/* Insert a single ATTR into the attribute table. */
260
261void
b8698a0f 262register_attribute (const struct attribute_spec *attr)
e28d52cf
DS
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)
d1c8e08a 272{
16f7ad42 273 struct substring str;
4a8fb1a1 274 attribute_spec **slot;
16f7ad42 275
e28d52cf
DS
276 gcc_assert (attr != NULL && name_space != NULL);
277
c203e8a7 278 gcc_assert (name_space->attribute_hash);
e28d52cf 279
16f7ad42
TG
280 str.str = attr->name;
281 str.length = strlen (str.str);
70e41a6a
NP
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
4a8fb1a1 287 slot = name_space->attribute_hash
c203e8a7
TS
288 ->find_slot_with_hash (&str, substring_hash (str.str, str.length),
289 INSERT);
0a35513e 290 gcc_assert (!*slot || attr->name[0] == '*');
4a8fb1a1 291 *slot = CONST_CAST (struct attribute_spec *, attr);
bb9f8221 292}
a7f6bc8c 293
e28d52cf
DS
294/* Return the spec for the scoped attribute with namespace NS and
295 name NAME. */
a7f6bc8c 296
862d0b35 297static const struct attribute_spec *
e28d52cf 298lookup_scoped_attribute_spec (const_tree ns, const_tree name)
a7f6bc8c
JM
299{
300 struct substring attr;
e28d52cf
DS
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;
a7f6bc8c
JM
309
310 attr.str = IDENTIFIER_POINTER (name);
311 attr.length = IDENTIFIER_LENGTH (name);
312 extract_attribute_substring (&attr);
c203e8a7
TS
313 return attrs->attribute_hash->find_with_hash (&attr,
314 substring_hash (attr.str,
315 attr.length));
a7f6bc8c 316}
e28d52cf 317
7dbb85a7
JM
318/* Return the spec for the attribute named NAME. If NAME is a TREE_LIST,
319 it also specifies the attribute namespace. */
e28d52cf
DS
320
321const struct attribute_spec *
322lookup_attribute_spec (const_tree name)
323{
7dbb85a7
JM
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);
e28d52cf
DS
333}
334
862d0b35
DN
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
343static tree
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
5d9ae53d
MS
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
fba303ed
MS
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
5d9ae53d
MS
432 found = true;
433
434 /* Print a note? */
435 bool note = last_decl != NULL_TREE;
097f82ec 436 auto_diagnostic_group d;
5d9ae53d 437 if (TREE_CODE (node) == FUNCTION_DECL
3d78e008 438 && fndecl_built_in_p (node))
5d9ae53d
MS
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}
862d0b35 458
bb9f8221
RK
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
03aa99d4 466 a decl attribute to the declaration rather than to its type). */
bb9f8221
RK
467
468tree
5d9ae53d
MS
469decl_attributes (tree *node, tree attributes, int flags,
470 tree last_decl /* = NULL_TREE */)
bb9f8221
RK
471{
472 tree a;
473 tree returned_attrs = NULL_TREE;
474
e28d52cf 475 if (TREE_TYPE (*node) == error_mark_node || attributes == error_mark_node)
21516d64
VR
476 return NULL_TREE;
477
bb9f8221
RK
478 if (!attributes_initialized)
479 init_attributes ();
480
ab442df7
MM
481 /* If this is a function and the user used #pragma GCC optimize, add the
482 options to the attribute((optimize(...))) list. */
483 if (TREE_CODE (*node) == FUNCTION_DECL && current_optimize_pragma)
484 {
485 tree cur_attr = lookup_attribute ("optimize", attributes);
486 tree opts = copy_list (current_optimize_pragma);
487
488 if (! cur_attr)
489 attributes
490 = tree_cons (get_identifier ("optimize"), opts, attributes);
491 else
492 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
493 }
494
495 if (TREE_CODE (*node) == FUNCTION_DECL
496 && optimization_current_node != optimization_default_node
497 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node))
498 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) = optimization_current_node;
499
5779e713
MM
500 /* If this is a function and the user used #pragma GCC target, add the
501 options to the attribute((target(...))) list. */
ab442df7 502 if (TREE_CODE (*node) == FUNCTION_DECL
5779e713 503 && current_target_pragma
ab442df7 504 && targetm.target_option.valid_attribute_p (*node, NULL_TREE,
5779e713 505 current_target_pragma, 0))
ab442df7 506 {
5779e713
MM
507 tree cur_attr = lookup_attribute ("target", attributes);
508 tree opts = copy_list (current_target_pragma);
ab442df7
MM
509
510 if (! cur_attr)
5779e713 511 attributes = tree_cons (get_identifier ("target"), opts, attributes);
ab442df7
MM
512 else
513 TREE_VALUE (cur_attr) = chainon (opts, TREE_VALUE (cur_attr));
514 }
515
61044492
JZ
516 /* A "naked" function attribute implies "noinline" and "noclone" for
517 those targets that support it. */
518 if (TREE_CODE (*node) == FUNCTION_DECL
70e41a6a 519 && attributes
036ea399
JJ
520 && lookup_attribute ("naked", attributes) != NULL
521 && lookup_attribute_spec (get_identifier ("naked")))
61044492
JZ
522 {
523 if (lookup_attribute ("noinline", attributes) == NULL)
524 attributes = tree_cons (get_identifier ("noinline"), NULL, attributes);
525
526 if (lookup_attribute ("noclone", attributes) == NULL)
527 attributes = tree_cons (get_identifier ("noclone"), NULL, attributes);
528 }
529
036ea399
JJ
530 /* A "noipa" function attribute implies "noinline", "noclone" and "no_icf"
531 for those targets that support it. */
532 if (TREE_CODE (*node) == FUNCTION_DECL
533 && attributes
534 && lookup_attribute ("noipa", attributes) != NULL
535 && lookup_attribute_spec (get_identifier ("noipa")))
536 {
537 if (lookup_attribute ("noinline", attributes) == NULL)
538 attributes = tree_cons (get_identifier ("noinline"), NULL, attributes);
539
540 if (lookup_attribute ("noclone", attributes) == NULL)
541 attributes = tree_cons (get_identifier ("noclone"), NULL, attributes);
542
543 if (lookup_attribute ("no_icf", attributes) == NULL)
544 attributes = tree_cons (get_identifier ("no_icf"), NULL, attributes);
545 }
546
5fd9b178 547 targetm.insert_attributes (*node, &attributes);
bb9f8221 548
5d9ae53d
MS
549 /* Note that attributes on the same declaration are not necessarily
550 in the same order as in the source. */
bb9f8221
RK
551 for (a = attributes; a; a = TREE_CHAIN (a))
552 {
e28d52cf
DS
553 tree ns = get_attribute_namespace (a);
554 tree name = get_attribute_name (a);
bb9f8221
RK
555 tree args = TREE_VALUE (a);
556 tree *anode = node;
4849deb1
JJ
557 const struct attribute_spec *spec
558 = lookup_scoped_attribute_spec (ns, name);
f9ceed32 559 int fn_ptr_quals = 0;
3acef2ae 560 tree fn_ptr_tmp = NULL_TREE;
bb9f8221
RK
561
562 if (spec == NULL)
563 {
e384e6b5 564 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
e28d52cf
DS
565 {
566 if (ns == NULL_TREE || !cxx11_attribute_p (a))
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 }
bb9f8221
RK
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 {
4f1e4960
JM
580 error ("wrong number of arguments specified for %qE attribute",
581 name);
bb9f8221
RK
582 continue;
583 }
23b43207 584 gcc_assert (is_attribute_p (spec->name, name));
bb9f8221 585
e28d52cf
DS
586 if (TYPE_P (*node)
587 && cxx11_attribute_p (a)
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. */
097f82ec 593 auto_diagnostic_group d;
fe6f27c7
PC
594 if (warning (OPT_Wattributes, "attribute ignored"))
595 inform (input_location,
596 "an attribute that appertains to a type-specifier "
597 "is ignored");
e28d52cf
DS
598 continue;
599 }
600
bb9f8221
RK
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. */
5d9ae53d
MS
608 tree attr = tree_cons (name, args, NULL_TREE);
609 returned_attrs = chainon (returned_attrs, attr);
bb9f8221
RK
610 continue;
611 }
612 else
613 {
4f1e4960
JM
614 warning (OPT_Wattributes, "%qE attribute does not apply to types",
615 name);
bb9f8221
RK
616 continue;
617 }
618 }
619
dd4dc3cd
RK
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. */
bb9f8221 624 if (spec->type_required && DECL_P (*anode))
dd4dc3cd
RK
625 {
626 anode = &TREE_TYPE (*anode);
26c87b1a 627 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
dd4dc3cd 628 }
bb9f8221
RK
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 {
3acef2ae
JM
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
c22cacf3
MS
644 This would all be simpler if attributes were part of the
645 declarator, grumble grumble. */
3acef2ae 646 fn_ptr_tmp = TREE_TYPE (*anode);
f9ceed32 647 fn_ptr_quals = TYPE_QUALS (*anode);
3acef2ae
JM
648 anode = &fn_ptr_tmp;
649 flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE;
bb9f8221
RK
650 }
651 else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
652 {
653 /* Pass on this attribute to be tried again. */
5d9ae53d
MS
654 tree attr = tree_cons (name, args, NULL_TREE);
655 returned_attrs = chainon (returned_attrs, attr);
bb9f8221
RK
656 continue;
657 }
658
659 if (TREE_CODE (*anode) != FUNCTION_TYPE
660 && TREE_CODE (*anode) != METHOD_TYPE)
661 {
5c498b10 662 warning (OPT_Wattributes,
4f1e4960
JM
663 "%qE attribute only applies to function types",
664 name);
bb9f8221
RK
665 continue;
666 }
667 }
668
b9e75696
JM
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
5d9ae53d
MS
677 bool no_add_attrs = false;
678
bffc6270
MS
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)
694 || (DECL_FUNCTION_CODE (*anode) != BUILT_IN_UNREACHABLE
695 && (DECL_FUNCTION_CODE (*anode)
696 != BUILT_IN_UBSAN_HANDLE_BUILTIN_UNREACHABLE)))
697 {
698 bool no_add = diag_attr_exclusions (last_decl, *anode, name, spec);
699 if (!no_add && anode != node)
700 no_add = diag_attr_exclusions (last_decl, *node, name, spec);
701 no_add_attrs |= no_add;
702 }
703 }
704
705 if (no_add_attrs)
706 continue;
707
bb9f8221 708 if (spec->handler != NULL)
e28d52cf
DS
709 {
710 int cxx11_flag =
711 cxx11_attribute_p (a) ? ATTR_FLAG_CXX11 : 0;
712
5d9ae53d
MS
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
1b9191d2
AH
731 /* Layout the decl in case anything changed. */
732 if (spec->type_required && DECL_P (*node)
8813a647 733 && (VAR_P (*node)
67282790
JM
734 || TREE_CODE (*node) == PARM_DECL
735 || TREE_CODE (*node) == RESULT_DECL))
d1838621 736 relayout_decl (*node);
1b9191d2 737
bb9f8221
RK
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. */
759 if (DECL_P (*anode))
760 DECL_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
761 else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE)
67214984
JM
762 {
763 TYPE_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs);
764 /* If this is the main variant, also push the attributes
765 out to the other variants. */
766 if (*anode == TYPE_MAIN_VARIANT (*anode))
767 {
768 tree variant;
769 for (variant = *anode; variant;
770 variant = TYPE_NEXT_VARIANT (variant))
771 {
772 if (TYPE_ATTRIBUTES (variant) == old_attrs)
773 TYPE_ATTRIBUTES (variant)
774 = TYPE_ATTRIBUTES (*anode);
775 else if (!lookup_attribute
776 (spec->name, TYPE_ATTRIBUTES (variant)))
777 TYPE_ATTRIBUTES (variant) = tree_cons
778 (name, args, TYPE_ATTRIBUTES (variant));
779 }
780 }
781 }
bb9f8221
RK
782 else
783 *anode = build_type_attribute_variant (*anode,
784 tree_cons (name, args,
785 old_attrs));
786 }
787 }
3acef2ae
JM
788
789 if (fn_ptr_tmp)
790 {
791 /* Rebuild the function pointer type and put it in the
792 appropriate place. */
793 fn_ptr_tmp = build_pointer_type (fn_ptr_tmp);
f9ceed32
MM
794 if (fn_ptr_quals)
795 fn_ptr_tmp = build_qualified_type (fn_ptr_tmp, fn_ptr_quals);
3acef2ae
JM
796 if (DECL_P (*node))
797 TREE_TYPE (*node) = fn_ptr_tmp;
3acef2ae 798 else
298e6adc
NS
799 {
800 gcc_assert (TREE_CODE (*node) == POINTER_TYPE);
801 *node = fn_ptr_tmp;
802 }
3acef2ae 803 }
bb9f8221
RK
804 }
805
806 return returned_attrs;
807}
0a35513e 808
e28d52cf
DS
809/* Return TRUE iff ATTR has been parsed by the front-end as a C++-11
810 attribute.
811
812 When G++ parses a C++11 attribute, it is represented as
813 a TREE_LIST which TREE_PURPOSE is itself a TREE_LIST. TREE_PURPOSE
814 (TREE_PURPOSE (ATTR)) is the namespace of the attribute, and the
815 TREE_VALUE (TREE_PURPOSE (ATTR)) is its non-qualified name. Please
816 use get_attribute_namespace and get_attribute_name to retrieve the
817 namespace and name of the attribute, as these accessors work with
818 GNU attributes as well. */
819
820bool
821cxx11_attribute_p (const_tree attr)
822{
823 if (attr == NULL_TREE
824 || TREE_CODE (attr) != TREE_LIST)
825 return false;
826
827 return (TREE_CODE (TREE_PURPOSE (attr)) == TREE_LIST);
828}
829
830/* Return the name of the attribute ATTR. This accessor works on GNU
831 and C++11 (scoped) attributes.
832
833 Please read the comments of cxx11_attribute_p to understand the
834 format of attributes. */
835
836tree
837get_attribute_name (const_tree attr)
838{
839 if (cxx11_attribute_p (attr))
840 return TREE_VALUE (TREE_PURPOSE (attr));
841 return TREE_PURPOSE (attr);
842}
843
0a35513e
AH
844/* Subroutine of set_method_tm_attributes. Apply TM attribute ATTR
845 to the method FNDECL. */
846
847void
848apply_tm_attr (tree fndecl, tree attr)
849{
850 decl_attributes (&TREE_TYPE (fndecl), tree_cons (attr, NULL, NULL), 0);
851}
3b1661a9
ES
852
853/* Makes a function attribute of the form NAME(ARG_NAME) and chains
854 it to CHAIN. */
855
856tree
857make_attribute (const char *name, const char *arg_name, tree chain)
858{
859 tree attr_name;
860 tree attr_arg_name;
861 tree attr_args;
862 tree attr;
863
864 attr_name = get_identifier (name);
865 attr_arg_name = build_string (strlen (arg_name), arg_name);
866 attr_args = tree_cons (NULL_TREE, attr_arg_name, NULL_TREE);
867 attr = tree_cons (attr_name, attr_args, chain);
868 return attr;
869}
1b062c1a
MM
870
871\f
872/* Common functions used for target clone support. */
873
874/* Comparator function to be used in qsort routine to sort attribute
875 specification strings to "target". */
876
877static int
878attr_strcmp (const void *v1, const void *v2)
879{
880 const char *c1 = *(char *const*)v1;
881 const char *c2 = *(char *const*)v2;
882 return strcmp (c1, c2);
883}
884
885/* ARGLIST is the argument to target attribute. This function tokenizes
886 the comma separated arguments, sorts them and returns a string which
887 is a unique identifier for the comma separated arguments. It also
888 replaces non-identifier characters "=,-" with "_". */
889
890char *
891sorted_attr_string (tree arglist)
892{
893 tree arg;
894 size_t str_len_sum = 0;
895 char **args = NULL;
896 char *attr_str, *ret_str;
897 char *attr = NULL;
898 unsigned int argnum = 1;
899 unsigned int i;
900
901 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
902 {
903 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
904 size_t len = strlen (str);
905 str_len_sum += len + 1;
906 if (arg != arglist)
907 argnum++;
908 for (i = 0; i < strlen (str); i++)
909 if (str[i] == ',')
910 argnum++;
911 }
912
913 attr_str = XNEWVEC (char, str_len_sum);
914 str_len_sum = 0;
915 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
916 {
917 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
918 size_t len = strlen (str);
919 memcpy (attr_str + str_len_sum, str, len);
920 attr_str[str_len_sum + len] = TREE_CHAIN (arg) ? ',' : '\0';
921 str_len_sum += len + 1;
922 }
923
924 /* Replace "=,-" with "_". */
925 for (i = 0; i < strlen (attr_str); i++)
926 if (attr_str[i] == '=' || attr_str[i]== '-')
927 attr_str[i] = '_';
928
929 if (argnum == 1)
930 return attr_str;
931
932 args = XNEWVEC (char *, argnum);
933
934 i = 0;
935 attr = strtok (attr_str, ",");
936 while (attr != NULL)
937 {
938 args[i] = attr;
939 i++;
940 attr = strtok (NULL, ",");
941 }
942
943 qsort (args, argnum, sizeof (char *), attr_strcmp);
944
945 ret_str = XNEWVEC (char, str_len_sum);
946 str_len_sum = 0;
947 for (i = 0; i < argnum; i++)
948 {
949 size_t len = strlen (args[i]);
950 memcpy (ret_str + str_len_sum, args[i], len);
951 ret_str[str_len_sum + len] = i < argnum - 1 ? '_' : '\0';
952 str_len_sum += len + 1;
953 }
954
955 XDELETEVEC (args);
956 XDELETEVEC (attr_str);
957 return ret_str;
958}
959
960
961/* This function returns true if FN1 and FN2 are versions of the same function,
962 that is, the target strings of the function decls are different. This assumes
963 that FN1 and FN2 have the same signature. */
964
965bool
966common_function_versions (tree fn1, tree fn2)
967{
968 tree attr1, attr2;
969 char *target1, *target2;
970 bool result;
971
972 if (TREE_CODE (fn1) != FUNCTION_DECL
973 || TREE_CODE (fn2) != FUNCTION_DECL)
974 return false;
975
976 attr1 = lookup_attribute ("target", DECL_ATTRIBUTES (fn1));
977 attr2 = lookup_attribute ("target", DECL_ATTRIBUTES (fn2));
978
979 /* At least one function decl should have the target attribute specified. */
980 if (attr1 == NULL_TREE && attr2 == NULL_TREE)
981 return false;
982
983 /* Diagnose missing target attribute if one of the decls is already
984 multi-versioned. */
985 if (attr1 == NULL_TREE || attr2 == NULL_TREE)
986 {
987 if (DECL_FUNCTION_VERSIONED (fn1) || DECL_FUNCTION_VERSIONED (fn2))
988 {
989 if (attr2 != NULL_TREE)
990 {
991 std::swap (fn1, fn2);
992 attr1 = attr2;
993 }
994 error_at (DECL_SOURCE_LOCATION (fn2),
995 "missing %<target%> attribute for multi-versioned %qD",
996 fn2);
997 inform (DECL_SOURCE_LOCATION (fn1),
998 "previous declaration of %qD", fn1);
999 /* Prevent diagnosing of the same error multiple times. */
1000 DECL_ATTRIBUTES (fn2)
1001 = tree_cons (get_identifier ("target"),
1002 copy_node (TREE_VALUE (attr1)),
1003 DECL_ATTRIBUTES (fn2));
1004 }
1005 return false;
1006 }
1007
1008 target1 = sorted_attr_string (TREE_VALUE (attr1));
1009 target2 = sorted_attr_string (TREE_VALUE (attr2));
1010
1011 /* The sorted target strings must be different for fn1 and fn2
1012 to be versions. */
1013 if (strcmp (target1, target2) == 0)
1014 result = false;
1015 else
1016 result = true;
1017
1018 XDELETEVEC (target1);
1019 XDELETEVEC (target2);
1020
1021 return result;
1022}
1023
1024/* Return a new name by appending SUFFIX to the DECL name. If make_unique
1025 is true, append the full path name of the source file. */
1026
1027char *
1028make_unique_name (tree decl, const char *suffix, bool make_unique)
1029{
1030 char *global_var_name;
1031 int name_len;
1032 const char *name;
1033 const char *unique_name = NULL;
1034
1035 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1036
1037 /* Get a unique name that can be used globally without any chances
1038 of collision at link time. */
1039 if (make_unique)
1040 unique_name = IDENTIFIER_POINTER (get_file_function_name ("\0"));
1041
1042 name_len = strlen (name) + strlen (suffix) + 2;
1043
1044 if (make_unique)
1045 name_len += strlen (unique_name) + 1;
1046 global_var_name = XNEWVEC (char, name_len);
1047
1048 /* Use '.' to concatenate names as it is demangler friendly. */
1049 if (make_unique)
1050 snprintf (global_var_name, name_len, "%s.%s.%s", name, unique_name,
1051 suffix);
1052 else
1053 snprintf (global_var_name, name_len, "%s.%s", name, suffix);
1054
1055 return global_var_name;
1056}
1057
1058/* Make a dispatcher declaration for the multi-versioned function DECL.
1059 Calls to DECL function will be replaced with calls to the dispatcher
1060 by the front-end. Return the decl created. */
1061
1062tree
1063make_dispatcher_decl (const tree decl)
1064{
1065 tree func_decl;
1066 char *func_name;
1067 tree fn_type, func_type;
1b062c1a 1068
871cc215 1069 func_name = xstrdup (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
1b062c1a
MM
1070
1071 fn_type = TREE_TYPE (decl);
1072 func_type = build_function_type (TREE_TYPE (fn_type),
1073 TYPE_ARG_TYPES (fn_type));
1074
1075 func_decl = build_fn_decl (func_name, func_type);
1076 XDELETEVEC (func_name);
1077 TREE_USED (func_decl) = 1;
1078 DECL_CONTEXT (func_decl) = NULL_TREE;
1079 DECL_INITIAL (func_decl) = error_mark_node;
1080 DECL_ARTIFICIAL (func_decl) = 1;
1081 /* Mark this func as external, the resolver will flip it again if
1082 it gets generated. */
1083 DECL_EXTERNAL (func_decl) = 1;
1084 /* This will be of type IFUNCs have to be externally visible. */
1085 TREE_PUBLIC (func_decl) = 1;
1086
1087 return func_decl;
1088}
1089
1090/* Returns true if decl is multi-versioned and DECL is the default function,
1091 that is it is not tagged with target specific optimization. */
1092
1093bool
1094is_function_default_version (const tree decl)
1095{
1096 if (TREE_CODE (decl) != FUNCTION_DECL
1097 || !DECL_FUNCTION_VERSIONED (decl))
1098 return false;
1099 tree attr = lookup_attribute ("target", DECL_ATTRIBUTES (decl));
1100 gcc_assert (attr);
1101 attr = TREE_VALUE (TREE_VALUE (attr));
1102 return (TREE_CODE (attr) == STRING_CST
1103 && strcmp (TREE_STRING_POINTER (attr), "default") == 0);
1104}
314e6352
ML
1105
1106/* Return a declaration like DDECL except that its DECL_ATTRIBUTES
1107 is ATTRIBUTE. */
1108
1109tree
1110build_decl_attribute_variant (tree ddecl, tree attribute)
1111{
1112 DECL_ATTRIBUTES (ddecl) = attribute;
1113 return ddecl;
1114}
1115
1116/* Return a type like TTYPE except that its TYPE_ATTRIBUTE
1117 is ATTRIBUTE and its qualifiers are QUALS.
1118
1119 Record such modified types already made so we don't make duplicates. */
1120
1121tree
27c825c5 1122build_type_attribute_qual_variant (tree otype, tree attribute, int quals)
314e6352 1123{
27c825c5 1124 tree ttype = otype;
314e6352
ML
1125 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
1126 {
1127 tree ntype;
1128
1129 /* Building a distinct copy of a tagged type is inappropriate; it
1130 causes breakage in code that expects there to be a one-to-one
1131 relationship between a struct and its fields.
1132 build_duplicate_type is another solution (as used in
1133 handle_transparent_union_attribute), but that doesn't play well
1134 with the stronger C++ type identity model. */
1135 if (TREE_CODE (ttype) == RECORD_TYPE
1136 || TREE_CODE (ttype) == UNION_TYPE
1137 || TREE_CODE (ttype) == QUAL_UNION_TYPE
1138 || TREE_CODE (ttype) == ENUMERAL_TYPE)
1139 {
1140 warning (OPT_Wattributes,
1141 "ignoring attributes applied to %qT after definition",
1142 TYPE_MAIN_VARIANT (ttype));
1143 return build_qualified_type (ttype, quals);
1144 }
1145
1146 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
27c825c5
JM
1147 if (lang_hooks.types.copy_lang_qualifiers
1148 && otype != TYPE_MAIN_VARIANT (otype))
1149 ttype = (lang_hooks.types.copy_lang_qualifiers
1150 (ttype, TYPE_MAIN_VARIANT (otype)));
1151
5cedffbc 1152 tree dtype = ntype = build_distinct_type_copy (ttype);
314e6352
ML
1153
1154 TYPE_ATTRIBUTES (ntype) = attribute;
1155
1156 hashval_t hash = type_hash_canon_hash (ntype);
1157 ntype = type_hash_canon (hash, ntype);
1158
5cedffbc
JM
1159 if (ntype != dtype)
1160 /* This variant was already in the hash table, don't mess with
1161 TYPE_CANONICAL. */;
1162 else if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
1163 || !comp_type_attributes (ntype, ttype))
a5e2a41f
JM
1164 /* If the target-dependent attributes make NTYPE different from
1165 its canonical type, we will need to use structural equality
1166 checks for this type.
1167
1168 We shouldn't get here for stripping attributes from a type;
1169 the no-attribute type might not need structural comparison. But
1170 we can if was discarded from type_hash_table. */
1171 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
314e6352
ML
1172 else if (TYPE_CANONICAL (ntype) == ntype)
1173 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
1174
1175 ttype = build_qualified_type (ntype, quals);
27c825c5
JM
1176 if (lang_hooks.types.copy_lang_qualifiers
1177 && otype != TYPE_MAIN_VARIANT (otype))
1178 ttype = lang_hooks.types.copy_lang_qualifiers (ttype, otype);
314e6352
ML
1179 }
1180 else if (TYPE_QUALS (ttype) != quals)
1181 ttype = build_qualified_type (ttype, quals);
1182
1183 return ttype;
1184}
1185
1186/* Compare two identifier nodes representing attributes.
1187 Return true if they are the same, false otherwise. */
1188
1189static bool
1190cmp_attrib_identifiers (const_tree attr1, const_tree attr2)
1191{
1192 /* Make sure we're dealing with IDENTIFIER_NODEs. */
1193 gcc_checking_assert (TREE_CODE (attr1) == IDENTIFIER_NODE
1194 && TREE_CODE (attr2) == IDENTIFIER_NODE);
1195
1196 /* Identifiers can be compared directly for equality. */
1197 if (attr1 == attr2)
1198 return true;
1199
1200 return cmp_attribs (IDENTIFIER_POINTER (attr1), IDENTIFIER_LENGTH (attr1),
1201 IDENTIFIER_POINTER (attr2), IDENTIFIER_LENGTH (attr2));
1202}
1203
1204/* Compare two constructor-element-type constants. Return 1 if the lists
1205 are known to be equal; otherwise return 0. */
1206
1207static bool
1208simple_cst_list_equal (const_tree l1, const_tree l2)
1209{
1210 while (l1 != NULL_TREE && l2 != NULL_TREE)
1211 {
1212 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
1213 return false;
1214
1215 l1 = TREE_CHAIN (l1);
1216 l2 = TREE_CHAIN (l2);
1217 }
1218
1219 return l1 == l2;
1220}
1221
1222/* Check if "omp declare simd" attribute arguments, CLAUSES1 and CLAUSES2, are
1223 the same. */
1224
1225static bool
1226omp_declare_simd_clauses_equal (tree clauses1, tree clauses2)
1227{
1228 tree cl1, cl2;
1229 for (cl1 = clauses1, cl2 = clauses2;
1230 cl1 && cl2;
1231 cl1 = OMP_CLAUSE_CHAIN (cl1), cl2 = OMP_CLAUSE_CHAIN (cl2))
1232 {
1233 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_CODE (cl2))
1234 return false;
1235 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_SIMDLEN)
1236 {
1237 if (simple_cst_equal (OMP_CLAUSE_DECL (cl1),
1238 OMP_CLAUSE_DECL (cl2)) != 1)
1239 return false;
1240 }
1241 switch (OMP_CLAUSE_CODE (cl1))
1242 {
1243 case OMP_CLAUSE_ALIGNED:
1244 if (simple_cst_equal (OMP_CLAUSE_ALIGNED_ALIGNMENT (cl1),
1245 OMP_CLAUSE_ALIGNED_ALIGNMENT (cl2)) != 1)
1246 return false;
1247 break;
1248 case OMP_CLAUSE_LINEAR:
1249 if (simple_cst_equal (OMP_CLAUSE_LINEAR_STEP (cl1),
1250 OMP_CLAUSE_LINEAR_STEP (cl2)) != 1)
1251 return false;
1252 break;
1253 case OMP_CLAUSE_SIMDLEN:
1254 if (simple_cst_equal (OMP_CLAUSE_SIMDLEN_EXPR (cl1),
1255 OMP_CLAUSE_SIMDLEN_EXPR (cl2)) != 1)
1256 return false;
1257 default:
1258 break;
1259 }
1260 }
1261 return true;
1262}
1263
1264
1265/* Compare two attributes for their value identity. Return true if the
1266 attribute values are known to be equal; otherwise return false. */
1267
1268bool
1269attribute_value_equal (const_tree attr1, const_tree attr2)
1270{
1271 if (TREE_VALUE (attr1) == TREE_VALUE (attr2))
1272 return true;
1273
1274 if (TREE_VALUE (attr1) != NULL_TREE
1275 && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST
1276 && TREE_VALUE (attr2) != NULL_TREE
1277 && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST)
1278 {
1279 /* Handle attribute format. */
1280 if (is_attribute_p ("format", get_attribute_name (attr1)))
1281 {
1282 attr1 = TREE_VALUE (attr1);
1283 attr2 = TREE_VALUE (attr2);
1284 /* Compare the archetypes (printf/scanf/strftime/...). */
1285 if (!cmp_attrib_identifiers (TREE_VALUE (attr1), TREE_VALUE (attr2)))
1286 return false;
1287 /* Archetypes are the same. Compare the rest. */
1288 return (simple_cst_list_equal (TREE_CHAIN (attr1),
1289 TREE_CHAIN (attr2)) == 1);
1290 }
1291 return (simple_cst_list_equal (TREE_VALUE (attr1),
1292 TREE_VALUE (attr2)) == 1);
1293 }
1294
bc1a75dd 1295 if (TREE_VALUE (attr1)
314e6352 1296 && TREE_CODE (TREE_VALUE (attr1)) == OMP_CLAUSE
bc1a75dd 1297 && TREE_VALUE (attr2)
314e6352
ML
1298 && TREE_CODE (TREE_VALUE (attr2)) == OMP_CLAUSE)
1299 return omp_declare_simd_clauses_equal (TREE_VALUE (attr1),
1300 TREE_VALUE (attr2));
1301
1302 return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1);
1303}
1304
1305/* Return 0 if the attributes for two types are incompatible, 1 if they
1306 are compatible, and 2 if they are nearly compatible (which causes a
1307 warning to be generated). */
1308int
1309comp_type_attributes (const_tree type1, const_tree type2)
1310{
1311 const_tree a1 = TYPE_ATTRIBUTES (type1);
1312 const_tree a2 = TYPE_ATTRIBUTES (type2);
1313 const_tree a;
1314
1315 if (a1 == a2)
1316 return 1;
1317 for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
1318 {
1319 const struct attribute_spec *as;
1320 const_tree attr;
1321
1322 as = lookup_attribute_spec (get_attribute_name (a));
1323 if (!as || as->affects_type_identity == false)
1324 continue;
1325
1326 attr = lookup_attribute (as->name, CONST_CAST_TREE (a2));
1327 if (!attr || !attribute_value_equal (a, attr))
1328 break;
1329 }
1330 if (!a)
1331 {
1332 for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
1333 {
1334 const struct attribute_spec *as;
1335
1336 as = lookup_attribute_spec (get_attribute_name (a));
1337 if (!as || as->affects_type_identity == false)
1338 continue;
1339
1340 if (!lookup_attribute (as->name, CONST_CAST_TREE (a1)))
1341 break;
1342 /* We don't need to compare trees again, as we did this
1343 already in first loop. */
1344 }
1345 /* All types - affecting identity - are equal, so
1346 there is no need to call target hook for comparison. */
1347 if (!a)
1348 return 1;
1349 }
1350 if (lookup_attribute ("transaction_safe", CONST_CAST_TREE (a)))
1351 return 0;
5c5f0b65
IT
1352 if ((lookup_attribute ("nocf_check", TYPE_ATTRIBUTES (type1)) != NULL)
1353 ^ (lookup_attribute ("nocf_check", TYPE_ATTRIBUTES (type2)) != NULL))
1354 return 0;
314e6352
ML
1355 /* As some type combinations - like default calling-convention - might
1356 be compatible, we have to call the target hook to get the final result. */
1357 return targetm.comp_type_attributes (type1, type2);
1358}
1359
1360/* Return a type like TTYPE except that its TYPE_ATTRIBUTE
1361 is ATTRIBUTE.
1362
1363 Record such modified types already made so we don't make duplicates. */
1364
1365tree
1366build_type_attribute_variant (tree ttype, tree attribute)
1367{
1368 return build_type_attribute_qual_variant (ttype, attribute,
1369 TYPE_QUALS (ttype));
1370}
1371\f
1372/* A variant of lookup_attribute() that can be used with an identifier
1373 as the first argument, and where the identifier can be either
1374 'text' or '__text__'.
1375
1376 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
1377 return a pointer to the attribute's list element if the attribute
1378 is part of the list, or NULL_TREE if not found. If the attribute
1379 appears more than once, this only returns the first occurrence; the
1380 TREE_CHAIN of the return value should be passed back in if further
1381 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
1382 can be in the form 'text' or '__text__'. */
1383static tree
1384lookup_ident_attribute (tree attr_identifier, tree list)
1385{
1386 gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE);
1387
1388 while (list)
1389 {
1390 gcc_checking_assert (TREE_CODE (get_attribute_name (list))
1391 == IDENTIFIER_NODE);
1392
1393 if (cmp_attrib_identifiers (attr_identifier,
1394 get_attribute_name (list)))
1395 /* Found it. */
1396 break;
1397 list = TREE_CHAIN (list);
1398 }
1399
1400 return list;
1401}
1402
1403/* Remove any instances of attribute ATTR_NAME in LIST and return the
1404 modified list. */
1405
1406tree
1407remove_attribute (const char *attr_name, tree list)
1408{
1409 tree *p;
1410 gcc_checking_assert (attr_name[0] != '_');
1411
1412 for (p = &list; *p;)
1413 {
1414 tree l = *p;
1415
1416 tree attr = get_attribute_name (l);
1417 if (is_attribute_p (attr_name, attr))
1418 *p = TREE_CHAIN (l);
1419 else
1420 p = &TREE_CHAIN (l);
1421 }
1422
1423 return list;
1424}
1425
1426/* Return an attribute list that is the union of a1 and a2. */
1427
1428tree
1429merge_attributes (tree a1, tree a2)
1430{
1431 tree attributes;
1432
1433 /* Either one unset? Take the set one. */
1434
1435 if ((attributes = a1) == 0)
1436 attributes = a2;
1437
1438 /* One that completely contains the other? Take it. */
1439
1440 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
1441 {
1442 if (attribute_list_contained (a2, a1))
1443 attributes = a2;
1444 else
1445 {
1446 /* Pick the longest list, and hang on the other list. */
1447
1448 if (list_length (a1) < list_length (a2))
1449 attributes = a2, a2 = a1;
1450
1451 for (; a2 != 0; a2 = TREE_CHAIN (a2))
1452 {
1453 tree a;
1454 for (a = lookup_ident_attribute (get_attribute_name (a2),
1455 attributes);
1456 a != NULL_TREE && !attribute_value_equal (a, a2);
1457 a = lookup_ident_attribute (get_attribute_name (a2),
1458 TREE_CHAIN (a)))
1459 ;
1460 if (a == NULL_TREE)
1461 {
1462 a1 = copy_node (a2);
1463 TREE_CHAIN (a1) = attributes;
1464 attributes = a1;
1465 }
1466 }
1467 }
1468 }
1469 return attributes;
1470}
1471
1472/* Given types T1 and T2, merge their attributes and return
1473 the result. */
1474
1475tree
1476merge_type_attributes (tree t1, tree t2)
1477{
1478 return merge_attributes (TYPE_ATTRIBUTES (t1),
1479 TYPE_ATTRIBUTES (t2));
1480}
1481
1482/* Given decls OLDDECL and NEWDECL, merge their attributes and return
1483 the result. */
1484
1485tree
1486merge_decl_attributes (tree olddecl, tree newdecl)
1487{
1488 return merge_attributes (DECL_ATTRIBUTES (olddecl),
1489 DECL_ATTRIBUTES (newdecl));
1490}
1491
bc1a75dd
JJ
1492/* Duplicate all attributes with name NAME in ATTR list to *ATTRS if
1493 they are missing there. */
1494
1495void
1496duplicate_one_attribute (tree *attrs, tree attr, const char *name)
1497{
1498 attr = lookup_attribute (name, attr);
1499 if (!attr)
1500 return;
1501 tree a = lookup_attribute (name, *attrs);
1502 while (attr)
1503 {
1504 tree a2;
1505 for (a2 = a; a2; a2 = lookup_attribute (name, TREE_CHAIN (a2)))
1506 if (attribute_value_equal (attr, a2))
1507 break;
1508 if (!a2)
1509 {
1510 a2 = copy_node (attr);
1511 TREE_CHAIN (a2) = *attrs;
1512 *attrs = a2;
1513 }
1514 attr = lookup_attribute (name, TREE_CHAIN (attr));
1515 }
1516}
1517
1518/* Duplicate all attributes from user DECL to the corresponding
1519 builtin that should be propagated. */
1520
1521void
1522copy_attributes_to_builtin (tree decl)
1523{
1524 tree b = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
1525 if (b)
1526 duplicate_one_attribute (&DECL_ATTRIBUTES (b),
1527 DECL_ATTRIBUTES (decl), "omp declare simd");
1528}
1529
314e6352
ML
1530#if TARGET_DLLIMPORT_DECL_ATTRIBUTES
1531
1532/* Specialization of merge_decl_attributes for various Windows targets.
1533
1534 This handles the following situation:
1535
1536 __declspec (dllimport) int foo;
1537 int foo;
1538
1539 The second instance of `foo' nullifies the dllimport. */
1540
1541tree
1542merge_dllimport_decl_attributes (tree old, tree new_tree)
1543{
1544 tree a;
1545 int delete_dllimport_p = 1;
1546
1547 /* What we need to do here is remove from `old' dllimport if it doesn't
1548 appear in `new'. dllimport behaves like extern: if a declaration is
1549 marked dllimport and a definition appears later, then the object
1550 is not dllimport'd. We also remove a `new' dllimport if the old list
1551 contains dllexport: dllexport always overrides dllimport, regardless
1552 of the order of declaration. */
1553 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
1554 delete_dllimport_p = 0;
1555 else if (DECL_DLLIMPORT_P (new_tree)
1556 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
1557 {
1558 DECL_DLLIMPORT_P (new_tree) = 0;
1559 warning (OPT_Wattributes, "%q+D already declared with dllexport "
1560 "attribute: dllimport ignored", new_tree);
1561 }
1562 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
1563 {
1564 /* Warn about overriding a symbol that has already been used, e.g.:
1565 extern int __attribute__ ((dllimport)) foo;
1566 int* bar () {return &foo;}
1567 int foo;
1568 */
1569 if (TREE_USED (old))
1570 {
1571 warning (0, "%q+D redeclared without dllimport attribute "
1572 "after being referenced with dll linkage", new_tree);
1573 /* If we have used a variable's address with dllimport linkage,
1574 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
1575 decl may already have had TREE_CONSTANT computed.
1576 We still remove the attribute so that assembler code refers
1577 to '&foo rather than '_imp__foo'. */
1578 if (VAR_P (old) && TREE_ADDRESSABLE (old))
1579 DECL_DLLIMPORT_P (new_tree) = 1;
1580 }
1581
1582 /* Let an inline definition silently override the external reference,
1583 but otherwise warn about attribute inconsistency. */
1584 else if (VAR_P (new_tree) || !DECL_DECLARED_INLINE_P (new_tree))
1585 warning (OPT_Wattributes, "%q+D redeclared without dllimport "
1586 "attribute: previous dllimport ignored", new_tree);
1587 }
1588 else
1589 delete_dllimport_p = 0;
1590
1591 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
1592
1593 if (delete_dllimport_p)
1594 a = remove_attribute ("dllimport", a);
1595
1596 return a;
1597}
1598
1599/* Handle a "dllimport" or "dllexport" attribute; arguments as in
1600 struct attribute_spec.handler. */
1601
1602tree
1603handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
1604 bool *no_add_attrs)
1605{
1606 tree node = *pnode;
1607 bool is_dllimport;
1608
1609 /* These attributes may apply to structure and union types being created,
1610 but otherwise should pass to the declaration involved. */
1611 if (!DECL_P (node))
1612 {
1613 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
1614 | (int) ATTR_FLAG_ARRAY_NEXT))
1615 {
1616 *no_add_attrs = true;
1617 return tree_cons (name, args, NULL_TREE);
1618 }
1619 if (TREE_CODE (node) == RECORD_TYPE
1620 || TREE_CODE (node) == UNION_TYPE)
1621 {
1622 node = TYPE_NAME (node);
1623 if (!node)
1624 return NULL_TREE;
1625 }
1626 else
1627 {
1628 warning (OPT_Wattributes, "%qE attribute ignored",
1629 name);
1630 *no_add_attrs = true;
1631 return NULL_TREE;
1632 }
1633 }
1634
1635 if (!VAR_OR_FUNCTION_DECL_P (node) && TREE_CODE (node) != TYPE_DECL)
1636 {
1637 *no_add_attrs = true;
1638 warning (OPT_Wattributes, "%qE attribute ignored",
1639 name);
1640 return NULL_TREE;
1641 }
1642
1643 if (TREE_CODE (node) == TYPE_DECL
1644 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
1645 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
1646 {
1647 *no_add_attrs = true;
1648 warning (OPT_Wattributes, "%qE attribute ignored",
1649 name);
1650 return NULL_TREE;
1651 }
1652
1653 is_dllimport = is_attribute_p ("dllimport", name);
1654
1655 /* Report error on dllimport ambiguities seen now before they cause
1656 any damage. */
1657 if (is_dllimport)
1658 {
1659 /* Honor any target-specific overrides. */
1660 if (!targetm.valid_dllimport_attribute_p (node))
1661 *no_add_attrs = true;
1662
1663 else if (TREE_CODE (node) == FUNCTION_DECL
1664 && DECL_DECLARED_INLINE_P (node))
1665 {
1666 warning (OPT_Wattributes, "inline function %q+D declared as "
0d7bac69 1667 "dllimport: attribute ignored", node);
314e6352
ML
1668 *no_add_attrs = true;
1669 }
1670 /* Like MS, treat definition of dllimported variables and
1671 non-inlined functions on declaration as syntax errors. */
1672 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
1673 {
1674 error ("function %q+D definition is marked dllimport", node);
1675 *no_add_attrs = true;
1676 }
1677
1678 else if (VAR_P (node))
1679 {
1680 if (DECL_INITIAL (node))
1681 {
1682 error ("variable %q+D definition is marked dllimport",
1683 node);
1684 *no_add_attrs = true;
1685 }
1686
1687 /* `extern' needn't be specified with dllimport.
1688 Specify `extern' now and hope for the best. Sigh. */
1689 DECL_EXTERNAL (node) = 1;
1690 /* Also, implicitly give dllimport'd variables declared within
1691 a function global scope, unless declared static. */
1692 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
1693 TREE_PUBLIC (node) = 1;
dbf02a2c
JJ
1694 /* Clear TREE_STATIC because DECL_EXTERNAL is set. */
1695 TREE_STATIC (node) = 0;
314e6352
ML
1696 }
1697
1698 if (*no_add_attrs == false)
1699 DECL_DLLIMPORT_P (node) = 1;
1700 }
1701 else if (TREE_CODE (node) == FUNCTION_DECL
1702 && DECL_DECLARED_INLINE_P (node)
1703 && flag_keep_inline_dllexport)
1704 /* An exported function, even if inline, must be emitted. */
1705 DECL_EXTERNAL (node) = 0;
1706
1707 /* Report error if symbol is not accessible at global scope. */
1708 if (!TREE_PUBLIC (node) && VAR_OR_FUNCTION_DECL_P (node))
1709 {
1710 error ("external linkage required for symbol %q+D because of "
1711 "%qE attribute", node, name);
1712 *no_add_attrs = true;
1713 }
1714
1715 /* A dllexport'd entity must have default visibility so that other
1716 program units (shared libraries or the main executable) can see
1717 it. A dllimport'd entity must have default visibility so that
1718 the linker knows that undefined references within this program
1719 unit can be resolved by the dynamic linker. */
1720 if (!*no_add_attrs)
1721 {
1722 if (DECL_VISIBILITY_SPECIFIED (node)
1723 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
1724 error ("%qE implies default visibility, but %qD has already "
1725 "been declared with a different visibility",
1726 name, node);
1727 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
1728 DECL_VISIBILITY_SPECIFIED (node) = 1;
1729 }
1730
1731 return NULL_TREE;
1732}
1733
1734#endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
1735
1736/* Given two lists of attributes, return true if list l2 is
1737 equivalent to l1. */
1738
1739int
1740attribute_list_equal (const_tree l1, const_tree l2)
1741{
1742 if (l1 == l2)
1743 return 1;
1744
1745 return attribute_list_contained (l1, l2)
1746 && attribute_list_contained (l2, l1);
1747}
1748
1749/* Given two lists of attributes, return true if list L2 is
1750 completely contained within L1. */
1751/* ??? This would be faster if attribute names were stored in a canonicalized
1752 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
1753 must be used to show these elements are equivalent (which they are). */
1754/* ??? It's not clear that attributes with arguments will always be handled
1755 correctly. */
1756
1757int
1758attribute_list_contained (const_tree l1, const_tree l2)
1759{
1760 const_tree t1, t2;
1761
1762 /* First check the obvious, maybe the lists are identical. */
1763 if (l1 == l2)
1764 return 1;
1765
1766 /* Maybe the lists are similar. */
1767 for (t1 = l1, t2 = l2;
1768 t1 != 0 && t2 != 0
1769 && get_attribute_name (t1) == get_attribute_name (t2)
1770 && TREE_VALUE (t1) == TREE_VALUE (t2);
1771 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1772 ;
1773
1774 /* Maybe the lists are equal. */
1775 if (t1 == 0 && t2 == 0)
1776 return 1;
1777
1778 for (; t2 != 0; t2 = TREE_CHAIN (t2))
1779 {
1780 const_tree attr;
1781 /* This CONST_CAST is okay because lookup_attribute does not
1782 modify its argument and the return value is assigned to a
1783 const_tree. */
1784 for (attr = lookup_ident_attribute (get_attribute_name (t2),
1785 CONST_CAST_TREE (l1));
1786 attr != NULL_TREE && !attribute_value_equal (t2, attr);
1787 attr = lookup_ident_attribute (get_attribute_name (t2),
1788 TREE_CHAIN (attr)))
1789 ;
1790
1791 if (attr == NULL_TREE)
1792 return 0;
1793 }
1794
1795 return 1;
1796}
13bdca74
ML
1797
1798/* The backbone of lookup_attribute(). ATTR_LEN is the string length
1799 of ATTR_NAME, and LIST is not NULL_TREE.
1800
1801 The function is called from lookup_attribute in order to optimize
1802 for size. */
1803
1804tree
1805private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
1806{
1807 while (list)
1808 {
1809 tree attr = get_attribute_name (list);
1810 size_t ident_len = IDENTIFIER_LENGTH (attr);
1811 if (cmp_attribs (attr_name, attr_len, IDENTIFIER_POINTER (attr),
1812 ident_len))
1813 break;
1814 list = TREE_CHAIN (list);
1815 }
1816
1817 return list;
1818}
5d9ae53d 1819
79a2c428
MS
1820/* Return true if the function decl or type NODE has been declared
1821 with attribute ANAME among attributes ATTRS. */
1822
1823static bool
1824has_attribute (tree node, tree attrs, const char *aname)
1825{
1826 if (!strcmp (aname, "const"))
1827 {
1828 if (DECL_P (node) && TREE_READONLY (node))
1829 return true;
1830 }
1831 else if (!strcmp (aname, "malloc"))
1832 {
1833 if (DECL_P (node) && DECL_IS_MALLOC (node))
1834 return true;
1835 }
1836 else if (!strcmp (aname, "noreturn"))
1837 {
1838 if (DECL_P (node) && TREE_THIS_VOLATILE (node))
1839 return true;
1840 }
1841 else if (!strcmp (aname, "nothrow"))
1842 {
1843 if (TREE_NOTHROW (node))
1844 return true;
1845 }
1846 else if (!strcmp (aname, "pure"))
1847 {
1848 if (DECL_P (node) && DECL_PURE_P (node))
1849 return true;
1850 }
1851
1852 return lookup_attribute (aname, attrs);
1853}
1854
1855/* Return the number of mismatched function or type attributes between
1856 the "template" function declaration TMPL and DECL. The word "template"
1857 doesn't necessarily refer to a C++ template but rather a declaration
1858 whose attributes should be matched by those on DECL. For a non-zero
1859 return value set *ATTRSTR to a string representation of the list of
1860 mismatched attributes with quoted names.
1861 ATTRLIST is a list of additional attributes that SPEC should be
1862 taken to ultimately be declared with. */
1863
1864unsigned
1865decls_mismatched_attributes (tree tmpl, tree decl, tree attrlist,
1866 const char* const blacklist[],
1867 pretty_printer *attrstr)
1868{
1869 if (TREE_CODE (tmpl) != FUNCTION_DECL)
1870 return 0;
1871
1872 /* Avoid warning if either declaration or its type is deprecated. */
1873 if (TREE_DEPRECATED (tmpl)
1874 || TREE_DEPRECATED (decl))
1875 return 0;
1876
1877 const tree tmpls[] = { tmpl, TREE_TYPE (tmpl) };
1878 const tree decls[] = { decl, TREE_TYPE (decl) };
1879
1880 if (TREE_DEPRECATED (tmpls[1])
1881 || TREE_DEPRECATED (decls[1])
1882 || TREE_DEPRECATED (TREE_TYPE (tmpls[1]))
1883 || TREE_DEPRECATED (TREE_TYPE (decls[1])))
1884 return 0;
1885
1886 tree tmpl_attrs[] = { DECL_ATTRIBUTES (tmpl), TYPE_ATTRIBUTES (tmpls[1]) };
1887 tree decl_attrs[] = { DECL_ATTRIBUTES (decl), TYPE_ATTRIBUTES (decls[1]) };
1888
1889 if (!decl_attrs[0])
1890 decl_attrs[0] = attrlist;
1891 else if (!decl_attrs[1])
1892 decl_attrs[1] = attrlist;
1893
1894 /* Avoid warning if the template has no attributes. */
1895 if (!tmpl_attrs[0] && !tmpl_attrs[1])
1896 return 0;
1897
1898 /* Avoid warning if either declaration contains an attribute on
1899 the white list below. */
1900 const char* const whitelist[] = {
1901 "error", "warning"
1902 };
1903
1904 for (unsigned i = 0; i != 2; ++i)
1905 for (unsigned j = 0; j != sizeof whitelist / sizeof *whitelist; ++j)
1906 if (lookup_attribute (whitelist[j], tmpl_attrs[i])
1907 || lookup_attribute (whitelist[j], decl_attrs[i]))
1908 return 0;
1909
1910 /* Put together a list of the black-listed attributes that the template
1911 is declared with and the declaration is not, in case it's not apparent
1912 from the most recent declaration of the template. */
1913 unsigned nattrs = 0;
1914
1915 for (unsigned i = 0; blacklist[i]; ++i)
1916 {
29d24852
MS
1917 /* Attribute leaf only applies to extern functions. Avoid mentioning
1918 it when it's missing from a static declaration. */
1919 if (!TREE_PUBLIC (decl)
1920 && !strcmp ("leaf", blacklist[i]))
1921 continue;
1922
79a2c428
MS
1923 for (unsigned j = 0; j != 2; ++j)
1924 {
1925 if (!has_attribute (tmpls[j], tmpl_attrs[j], blacklist[i]))
1926 continue;
1927
1928 unsigned kmax = 1 + !!decl_attrs[1];
1929 for (unsigned k = 0; k != kmax; ++k)
1930 {
1931 if (has_attribute (decls[k], decl_attrs[k], blacklist[i]))
1932 break;
1933
1934 if (!k && kmax > 1)
1935 continue;
1936
1937 if (nattrs)
1938 pp_string (attrstr, ", ");
1939 pp_begin_quote (attrstr, pp_show_color (global_dc->printer));
1940 pp_string (attrstr, blacklist[i]);
1941 pp_end_quote (attrstr, pp_show_color (global_dc->printer));
1942 ++nattrs;
1943 }
1944 }
1945 }
1946
1947 return nattrs;
1948}
1949
1950/* Issue a warning for the declaration ALIAS for TARGET where ALIAS
1951 specifies either attributes that are incompatible with those of
1952 TARGET, or attributes that are missing and that declaring ALIAS
1953 with would benefit. */
1954
1955void
1956maybe_diag_alias_attributes (tree alias, tree target)
1957{
1958 /* Do not expect attributes to match between aliases and ifunc
1959 resolvers. There is no obvious correspondence between them. */
1960 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (alias)))
1961 return;
1962
1963 const char* const blacklist[] = {
1964 "alloc_align", "alloc_size", "cold", "const", "hot", "leaf", "malloc",
1965 "nonnull", "noreturn", "nothrow", "pure", "returns_nonnull",
1966 "returns_twice", NULL
1967 };
1968
1969 pretty_printer attrnames;
1970 if (warn_attribute_alias > 1)
1971 {
1972 /* With -Wattribute-alias=2 detect alias declarations that are more
1973 restrictive than their targets first. Those indicate potential
1974 codegen bugs. */
1975 if (unsigned n = decls_mismatched_attributes (alias, target, NULL_TREE,
1976 blacklist, &attrnames))
1977 {
1978 auto_diagnostic_group d;
1979 if (warning_n (DECL_SOURCE_LOCATION (alias),
1980 OPT_Wattribute_alias_, n,
1981 "%qD specifies more restrictive attribute than "
1982 "its target %qD: %s",
1983 "%qD specifies more restrictive attributes than "
1984 "its target %qD: %s",
1985 alias, target, pp_formatted_text (&attrnames)))
1986 inform (DECL_SOURCE_LOCATION (target),
1987 "%qD target declared here", alias);
1988 return;
1989 }
1990 }
1991
1992 /* Detect alias declarations that are less restrictive than their
1993 targets. Those suggest potential optimization opportunities
1994 (solved by adding the missing attribute(s) to the alias). */
1995 if (unsigned n = decls_mismatched_attributes (target, alias, NULL_TREE,
1996 blacklist, &attrnames))
1997 {
1998 auto_diagnostic_group d;
1999 if (warning_n (DECL_SOURCE_LOCATION (alias),
2000 OPT_Wmissing_attributes, n,
2001 "%qD specifies less restrictive attribute than "
2002 "its target %qD: %s",
2003 "%qD specifies less restrictive attributes than "
2004 "its target %qD: %s",
2005 alias, target, pp_formatted_text (&attrnames)))
2006 inform (DECL_SOURCE_LOCATION (target),
2007 "%qD target declared here", alias);
2008 }
2009}
2010
2011
5d9ae53d
MS
2012#if CHECKING_P
2013
2014namespace selftest
2015{
2016
2017/* Helper types to verify the consistency attribute exclusions. */
2018
2019typedef std::pair<const char *, const char *> excl_pair;
2020
2021struct excl_hash_traits: typed_noop_remove<excl_pair>
2022{
2023 typedef excl_pair value_type;
2024 typedef value_type compare_type;
2025
2026 static hashval_t hash (const value_type &x)
2027 {
2028 hashval_t h1 = htab_hash_string (x.first);
2029 hashval_t h2 = htab_hash_string (x.second);
2030 return h1 ^ h2;
2031 }
2032
2033 static bool equal (const value_type &x, const value_type &y)
2034 {
2035 return !strcmp (x.first, y.first) && !strcmp (x.second, y.second);
2036 }
2037
2038 static void mark_deleted (value_type &x)
2039 {
2040 x = value_type (NULL, NULL);
2041 }
2042
2043 static void mark_empty (value_type &x)
2044 {
2045 x = value_type ("", "");
2046 }
2047
2048 static bool is_deleted (const value_type &x)
2049 {
2050 return !x.first && !x.second;
2051 }
2052
2053 static bool is_empty (const value_type &x)
2054 {
2055 return !*x.first && !*x.second;
2056 }
2057};
2058
2059
2060/* Self-test to verify that each attribute exclusion is symmetric,
2061 meaning that if attribute A is encoded as incompatible with
2062 attribute B then the opposite relationship is also encoded.
2063 This test also detects most cases of misspelled attribute names
2064 in exclusions. */
2065
2066static void
2067test_attribute_exclusions ()
2068{
2069 /* Iterate over the array of attribute tables first (with TI0 as
2070 the index) and over the array of attribute_spec in each table
2071 (with SI0 as the index). */
2072 const size_t ntables = ARRAY_SIZE (attribute_tables);
2073
2074 /* Set of pairs of mutually exclusive attributes. */
2075 typedef hash_set<excl_pair, excl_hash_traits> exclusion_set;
2076 exclusion_set excl_set;
2077
2078 for (size_t ti0 = 0; ti0 != ntables; ++ti0)
2079 for (size_t s0 = 0; attribute_tables[ti0][s0].name; ++s0)
2080 {
2081 const attribute_spec::exclusions *excl
2082 = attribute_tables[ti0][s0].exclude;
2083
2084 /* Skip each attribute that doesn't define exclusions. */
2085 if (!excl)
2086 continue;
2087
2088 const char *attr_name = attribute_tables[ti0][s0].name;
2089
2090 /* Iterate over the set of exclusions for every attribute
2091 (with EI0 as the index) adding the exclusions defined
2092 for each to the set. */
2093 for (size_t ei0 = 0; excl[ei0].name; ++ei0)
2094 {
2095 const char *excl_name = excl[ei0].name;
2096
2097 if (!strcmp (attr_name, excl_name))
2098 continue;
2099
2100 excl_set.add (excl_pair (attr_name, excl_name));
2101 }
2102 }
2103
2104 /* Traverse the set of mutually exclusive pairs of attributes
2105 and verify that they are symmetric. */
2106 for (exclusion_set::iterator it = excl_set.begin ();
2107 it != excl_set.end ();
2108 ++it)
2109 {
2110 if (!excl_set.contains (excl_pair ((*it).second, (*it).first)))
2111 {
2112 /* An exclusion for an attribute has been found that
2113 doesn't have a corresponding exclusion in the opposite
2114 direction. */
2115 char desc[120];
2116 sprintf (desc, "'%s' attribute exclusion '%s' must be symmetric",
2117 (*it).first, (*it).second);
2118 fail (SELFTEST_LOCATION, desc);
2119 }
2120 }
2121}
2122
2123void
2124attribute_c_tests ()
2125{
2126 test_attribute_exclusions ();
2127}
2128
2129} /* namespace selftest */
2130
2131#endif /* CHECKING_P */