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