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