Allow returning something of type void in a function that returns void
[delight/core.git] / d-bi-attrs-41.h
blobbff94d30eb605dff7a50b04d74dd531d1c557276
1 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
2 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
3 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
4 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
5 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
6 static tree handle_always_inline_attribute (tree *, tree, tree, int,
7 bool *);
8 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
9 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
10 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
11 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
12 bool *);
13 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
14 static tree handle_transparent_union_attribute (tree *, tree, tree,
15 int, bool *);
16 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
17 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
18 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
19 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
20 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
21 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
22 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
23 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
24 static tree handle_visibility_attribute (tree *, tree, tree, int,
25 bool *);
26 static tree handle_tls_model_attribute (tree *, tree, tree, int,
27 bool *);
28 static tree handle_no_instrument_function_attribute (tree *, tree,
29 tree, int, bool *);
30 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
31 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
32 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
33 bool *);
34 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
35 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
36 static tree handle_deprecated_attribute (tree *, tree, tree, int,
37 bool *);
38 static tree handle_vector_size_attribute (tree *, tree, tree, int,
39 bool *);
40 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
41 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
42 //static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
43 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
44 bool *);
45 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
47 //static void check_function_nonnull (tree, tree);
48 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
49 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
50 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
51 //static int resort_field_decl_cmp (const void *, const void *);
53 /* extra for gdc copy: */
54 static void check_function_arguments_recurse (void (*)
55 (void *, tree,
56 unsigned HOST_WIDE_INT),
57 void *, tree,
58 unsigned HOST_WIDE_INT);
60 static tree
61 handle_format_arg_attribute (tree *node ATTRIBUTE_UNUSED, tree name ATTRIBUTE_UNUSED,
62 tree args ATTRIBUTE_UNUSED, int flags ATTRIBUTE_UNUSED, bool *no_add_attrs ATTRIBUTE_UNUSED)
64 return NULL_TREE;
66 static tree
67 handle_format_attribute (tree *node ATTRIBUTE_UNUSED, tree name ATTRIBUTE_UNUSED, tree args ATTRIBUTE_UNUSED,
68 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs ATTRIBUTE_UNUSED)
70 return NULL_TREE;
72 /* -- end extra */
74 /* more copied... */
76 /* Warn about using __null (as NULL in C++) as sentinel. For code compiled
77 with GCC this doesn't matter as __null is guaranteed to have the right
78 size. */
80 int warn_strict_null_sentinel;
82 /* Warn about format/argument anomalies in calls to formatted I/O functions
83 (*printf, *scanf, strftime, strfmon, etc.). */
85 int warn_format;
87 /* end more copied-- */
89 /* Table of machine-independent attributes common to all C-like languages. */
90 const struct attribute_spec d_common_attribute_table[] =
92 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
93 { "packed", 0, 0, false, false, false,
94 handle_packed_attribute },
95 { "nocommon", 0, 0, true, false, false,
96 handle_nocommon_attribute },
97 { "common", 0, 0, true, false, false,
98 handle_common_attribute },
99 /* FIXME: logically, noreturn attributes should be listed as
100 "false, true, true" and apply to function types. But implementing this
101 would require all the places in the compiler that use TREE_THIS_VOLATILE
102 on a decl to identify non-returning functions to be located and fixed
103 to check the function type instead. */
104 { "noreturn", 0, 0, true, false, false,
105 handle_noreturn_attribute },
106 { "volatile", 0, 0, true, false, false,
107 handle_noreturn_attribute },
108 { "noinline", 0, 0, true, false, false,
109 handle_noinline_attribute },
110 { "always_inline", 0, 0, true, false, false,
111 handle_always_inline_attribute },
112 { "flatten", 0, 0, true, false, false,
113 handle_flatten_attribute },
114 { "used", 0, 0, true, false, false,
115 handle_used_attribute },
116 { "unused", 0, 0, false, false, false,
117 handle_unused_attribute },
118 { "externally_visible", 0, 0, true, false, false,
119 handle_externally_visible_attribute },
120 /* The same comments as for noreturn attributes apply to const ones. */
121 { "const", 0, 0, true, false, false,
122 handle_const_attribute },
123 { "transparent_union", 0, 0, false, false, false,
124 handle_transparent_union_attribute },
125 { "constructor", 0, 0, true, false, false,
126 handle_constructor_attribute },
127 { "destructor", 0, 0, true, false, false,
128 handle_destructor_attribute },
129 { "mode", 1, 1, false, true, false,
130 handle_mode_attribute },
131 { "section", 1, 1, true, false, false,
132 handle_section_attribute },
133 { "aligned", 0, 1, false, false, false,
134 handle_aligned_attribute },
135 { "weak", 0, 0, true, false, false,
136 handle_weak_attribute },
137 { "alias", 1, 1, true, false, false,
138 handle_alias_attribute },
139 { "weakref", 0, 1, true, false, false,
140 handle_weakref_attribute },
141 { "no_instrument_function", 0, 0, true, false, false,
142 handle_no_instrument_function_attribute },
143 { "malloc", 0, 0, true, false, false,
144 handle_malloc_attribute },
145 { "returns_twice", 0, 0, true, false, false,
146 handle_returns_twice_attribute },
147 { "no_stack_limit", 0, 0, true, false, false,
148 handle_no_limit_stack_attribute },
149 { "pure", 0, 0, true, false, false,
150 handle_pure_attribute },
151 /* For internal use (marking of builtins) only. The name contains space
152 to prevent its usage in source code. */
153 { "no vops", 0, 0, true, false, false,
154 handle_novops_attribute },
155 { "deprecated", 0, 0, false, false, false,
156 handle_deprecated_attribute },
157 { "vector_size", 1, 1, false, true, false,
158 handle_vector_size_attribute },
159 { "visibility", 1, 1, false, false, false,
160 handle_visibility_attribute },
161 { "tls_model", 1, 1, true, false, false,
162 handle_tls_model_attribute },
163 { "nonnull", 0, -1, false, true, true,
164 handle_nonnull_attribute },
165 { "nothrow", 0, 0, true, false, false,
166 handle_nothrow_attribute },
167 { "may_alias", 0, 0, false, true, false, NULL },
168 /* not in gdc
169 { "cleanup", 1, 1, true, false, false,
170 handle_cleanup_attribute },
172 { "warn_unused_result", 0, 0, false, true, true,
173 handle_warn_unused_result_attribute },
174 { "sentinel", 0, 1, false, true, true,
175 handle_sentinel_attribute },
176 { NULL, 0, 0, false, false, false, NULL }
179 /* Give the specifications for the format attributes, used by C and all
180 descendants. */
182 const struct attribute_spec d_common_format_attribute_table[] =
184 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
185 { "format", 3, 3, false, true, true,
186 handle_format_attribute },
187 { "format_arg", 1, 1, false, true, true,
188 handle_format_arg_attribute },
189 { NULL, 0, 0, false, false, false, NULL }
192 /* Attribute handlers common to C front ends. */
194 /* Handle a "packed" attribute; arguments as in
195 struct attribute_spec.handler. */
197 static tree
198 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
199 int flags, bool *no_add_attrs)
201 if (TYPE_P (*node))
203 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
204 *node = build_variant_type_copy (*node);
205 TYPE_PACKED (*node) = 1;
206 if (TYPE_MAIN_VARIANT (*node) == *node)
208 /* If it is the main variant, then pack the other variants
209 too. This happens in,
211 struct Foo {
212 struct Foo const *ptr; // creates a variant w/o packed flag
213 } __ attribute__((packed)); // packs it now.
215 tree probe;
217 for (probe = *node; probe; probe = TYPE_NEXT_VARIANT (probe))
218 TYPE_PACKED (probe) = 1;
221 else if (TREE_CODE (*node) == FIELD_DECL)
223 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT)
224 warning (OPT_Wattributes,
225 "%qE attribute ignored for field of type %qT",
226 name, TREE_TYPE (*node));
227 else
228 DECL_PACKED (*node) = 1;
230 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
231 used for DECL_REGISTER. It wouldn't mean anything anyway.
232 We can't set DECL_PACKED on the type of a TYPE_DECL, because
233 that changes what the typedef is typing. */
234 else
236 warning (OPT_Wattributes, "%qE attribute ignored", name);
237 *no_add_attrs = true;
240 return NULL_TREE;
243 /* Handle a "nocommon" attribute; arguments as in
244 struct attribute_spec.handler. */
246 static tree
247 handle_nocommon_attribute (tree *node, tree name,
248 tree ARG_UNUSED (args),
249 int ARG_UNUSED (flags), bool *no_add_attrs)
251 if (TREE_CODE (*node) == VAR_DECL)
252 DECL_COMMON (*node) = 0;
253 else
255 warning (OPT_Wattributes, "%qE attribute ignored", name);
256 *no_add_attrs = true;
259 return NULL_TREE;
262 /* Handle a "common" attribute; arguments as in
263 struct attribute_spec.handler. */
265 static tree
266 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
267 int ARG_UNUSED (flags), bool *no_add_attrs)
269 if (TREE_CODE (*node) == VAR_DECL)
270 DECL_COMMON (*node) = 1;
271 else
273 warning (OPT_Wattributes, "%qE attribute ignored", name);
274 *no_add_attrs = true;
277 return NULL_TREE;
280 /* Handle a "noreturn" attribute; arguments as in
281 struct attribute_spec.handler. */
283 static tree
284 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
285 int ARG_UNUSED (flags), bool *no_add_attrs)
287 tree type = TREE_TYPE (*node);
289 /* See FIXME comment in c_common_attribute_table. */
290 if (TREE_CODE (*node) == FUNCTION_DECL)
291 TREE_THIS_VOLATILE (*node) = 1;
292 else if (TREE_CODE (type) == POINTER_TYPE
293 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
294 TREE_TYPE (*node)
295 = build_pointer_type
296 (build_type_variant (TREE_TYPE (type),
297 TYPE_READONLY (TREE_TYPE (type)), 1));
298 else
300 warning (OPT_Wattributes, "%qE attribute ignored", name);
301 *no_add_attrs = true;
304 return NULL_TREE;
307 /* Handle a "noinline" attribute; arguments as in
308 struct attribute_spec.handler. */
310 static tree
311 handle_noinline_attribute (tree *node, tree name,
312 tree ARG_UNUSED (args),
313 int ARG_UNUSED (flags), bool *no_add_attrs)
315 if (TREE_CODE (*node) == FUNCTION_DECL)
316 DECL_UNINLINABLE (*node) = 1;
317 else
319 warning (OPT_Wattributes, "%qE attribute ignored", name);
320 *no_add_attrs = true;
323 return NULL_TREE;
326 /* Handle a "always_inline" attribute; arguments as in
327 struct attribute_spec.handler. */
329 static tree
330 handle_always_inline_attribute (tree *node, tree name,
331 tree ARG_UNUSED (args),
332 int ARG_UNUSED (flags),
333 bool *no_add_attrs)
335 if (TREE_CODE (*node) == FUNCTION_DECL)
337 /* Do nothing else, just set the attribute. We'll get at
338 it later with lookup_attribute. */
340 else
342 warning (OPT_Wattributes, "%qE attribute ignored", name);
343 *no_add_attrs = true;
346 return NULL_TREE;
349 /* Handle a "flatten" attribute; arguments as in
350 struct attribute_spec.handler. */
352 static tree
353 handle_flatten_attribute (tree *node, tree name,
354 tree args ATTRIBUTE_UNUSED,
355 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
357 if (TREE_CODE (*node) == FUNCTION_DECL)
358 /* Do nothing else, just set the attribute. We'll get at
359 it later with lookup_attribute. */
361 else
363 warning (OPT_Wattributes, "%qE attribute ignored", name);
364 *no_add_attrs = true;
367 return NULL_TREE;
371 /* Handle a "used" attribute; arguments as in
372 struct attribute_spec.handler. */
374 static tree
375 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
376 int ARG_UNUSED (flags), bool *no_add_attrs)
378 tree node = *pnode;
380 if (TREE_CODE (node) == FUNCTION_DECL
381 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node)))
383 TREE_USED (node) = 1;
384 DECL_PRESERVE_P (node) = 1;
386 else
388 warning (OPT_Wattributes, "%qE attribute ignored", name);
389 *no_add_attrs = true;
392 return NULL_TREE;
395 /* Handle a "unused" attribute; arguments as in
396 struct attribute_spec.handler. */
398 static tree
399 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
400 int flags, bool *no_add_attrs)
402 if (DECL_P (*node))
404 tree decl = *node;
406 if (TREE_CODE (decl) == PARM_DECL
407 || TREE_CODE (decl) == VAR_DECL
408 || TREE_CODE (decl) == FUNCTION_DECL
409 || TREE_CODE (decl) == LABEL_DECL
410 || TREE_CODE (decl) == TYPE_DECL)
411 TREE_USED (decl) = 1;
412 else
414 warning (OPT_Wattributes, "%qE attribute ignored", name);
415 *no_add_attrs = true;
418 else
420 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
421 *node = build_variant_type_copy (*node);
422 TREE_USED (*node) = 1;
425 return NULL_TREE;
428 /* Handle a "externally_visible" attribute; arguments as in
429 struct attribute_spec.handler. */
431 static tree
432 handle_externally_visible_attribute (tree *pnode, tree name,
433 tree ARG_UNUSED (args),
434 int ARG_UNUSED (flags),
435 bool *no_add_attrs)
437 tree node = *pnode;
439 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL)
440 || !TREE_PUBLIC (node))
442 warning (OPT_Wattributes,
443 "%qE attribute have effect only on public objects", name);
444 *no_add_attrs = true;
446 else if (TREE_CODE (node) == FUNCTION_DECL)
448 struct cgraph_node *n = cgraph_node (node);
449 n->local.externally_visible = true;
450 if (n->local.finalized)
451 cgraph_mark_needed_node (n);
453 else if (TREE_CODE (node) == VAR_DECL)
455 struct cgraph_varpool_node *n = cgraph_varpool_node (node);
456 n->externally_visible = true;
457 if (n->finalized)
458 cgraph_varpool_mark_needed_node (n);
460 else
462 warning (OPT_Wattributes, "%qE attribute ignored", name);
463 *no_add_attrs = true;
466 return NULL_TREE;
469 /* Handle a "const" attribute; arguments as in
470 struct attribute_spec.handler. */
472 static tree
473 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
474 int ARG_UNUSED (flags), bool *no_add_attrs)
476 tree type = TREE_TYPE (*node);
478 /* See FIXME comment on noreturn in c_common_attribute_table. */
479 if (TREE_CODE (*node) == FUNCTION_DECL)
480 TREE_READONLY (*node) = 1;
481 else if (TREE_CODE (type) == POINTER_TYPE
482 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
483 TREE_TYPE (*node)
484 = build_pointer_type
485 (build_type_variant (TREE_TYPE (type), 1,
486 TREE_THIS_VOLATILE (TREE_TYPE (type))));
487 else
489 warning (OPT_Wattributes, "%qE attribute ignored", name);
490 *no_add_attrs = true;
493 return NULL_TREE;
496 /* Handle a "transparent_union" attribute; arguments as in
497 struct attribute_spec.handler. */
499 static tree
500 handle_transparent_union_attribute (tree *node, tree name,
501 tree ARG_UNUSED (args), int flags,
502 bool *no_add_attrs)
504 tree type = NULL;
506 *no_add_attrs = true;
508 if (DECL_P (*node))
510 if (TREE_CODE (*node) != TYPE_DECL)
511 goto ignored;
512 node = &TREE_TYPE (*node);
513 type = *node;
515 else if (TYPE_P (*node))
516 type = *node;
517 else
518 goto ignored;
520 if (TREE_CODE (type) == UNION_TYPE)
522 /* When IN_PLACE is set, leave the check for FIELDS and MODE to
523 the code in finish_struct. */
524 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
526 if (TYPE_FIELDS (type) == NULL_TREE
527 || TYPE_MODE (type) != DECL_MODE (TYPE_FIELDS (type)))
528 goto ignored;
530 /* A type variant isn't good enough, since we don't a cast
531 to such a type removed as a no-op. */
532 *node = type = build_duplicate_type (type);
535 TYPE_TRANSPARENT_UNION (type) = 1;
536 return NULL_TREE;
539 ignored:
540 warning (OPT_Wattributes, "%qE attribute ignored", name);
541 return NULL_TREE;
544 /* Handle a "constructor" attribute; arguments as in
545 struct attribute_spec.handler. */
547 static tree
548 handle_constructor_attribute (tree *node, tree name,
549 tree ARG_UNUSED (args),
550 int ARG_UNUSED (flags),
551 bool *no_add_attrs)
553 tree decl = *node;
554 tree type = TREE_TYPE (decl);
556 if (TREE_CODE (decl) == FUNCTION_DECL
557 && TREE_CODE (type) == FUNCTION_TYPE
558 && decl_function_context (decl) == 0)
560 DECL_STATIC_CONSTRUCTOR (decl) = 1;
561 TREE_USED (decl) = 1;
563 else
565 warning (OPT_Wattributes, "%qE attribute ignored", name);
566 *no_add_attrs = true;
569 return NULL_TREE;
572 /* Handle a "destructor" attribute; arguments as in
573 struct attribute_spec.handler. */
575 static tree
576 handle_destructor_attribute (tree *node, tree name,
577 tree ARG_UNUSED (args),
578 int ARG_UNUSED (flags),
579 bool *no_add_attrs)
581 tree decl = *node;
582 tree type = TREE_TYPE (decl);
584 if (TREE_CODE (decl) == FUNCTION_DECL
585 && TREE_CODE (type) == FUNCTION_TYPE
586 && decl_function_context (decl) == 0)
588 DECL_STATIC_DESTRUCTOR (decl) = 1;
589 TREE_USED (decl) = 1;
591 else
593 warning (OPT_Wattributes, "%qE attribute ignored", name);
594 *no_add_attrs = true;
597 return NULL_TREE;
600 /* Handle a "mode" attribute; arguments as in
601 struct attribute_spec.handler. */
603 static tree
604 handle_mode_attribute (tree *node, tree name, tree args,
605 int ARG_UNUSED (flags), bool *no_add_attrs)
607 tree type = *node;
609 *no_add_attrs = true;
611 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
612 warning (OPT_Wattributes, "%qE attribute ignored", name);
613 else
615 int j;
616 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
617 int len = strlen (p);
618 enum machine_mode mode = VOIDmode;
619 tree typefm;
620 bool valid_mode;
622 if (len > 4 && p[0] == '_' && p[1] == '_'
623 && p[len - 1] == '_' && p[len - 2] == '_')
625 char *newp = (char *) alloca (len - 1);
627 strcpy (newp, &p[2]);
628 newp[len - 4] = '\0';
629 p = newp;
632 /* Change this type to have a type with the specified mode.
633 First check for the special modes. */
634 if (!strcmp (p, "byte"))
635 mode = byte_mode;
636 else if (!strcmp (p, "word"))
637 mode = word_mode;
638 else if (!strcmp (p, "pointer"))
639 mode = ptr_mode;
640 else
641 for (j = 0; j < NUM_MACHINE_MODES; j++)
642 if (!strcmp (p, GET_MODE_NAME (j)))
644 mode = (enum machine_mode) j;
645 break;
648 if (mode == VOIDmode)
650 error ("unknown machine mode %qs", p);
651 return NULL_TREE;
654 valid_mode = false;
655 switch (GET_MODE_CLASS (mode))
657 case MODE_INT:
658 case MODE_PARTIAL_INT:
659 case MODE_FLOAT:
660 valid_mode = targetm.scalar_mode_supported_p (mode);
661 break;
663 case MODE_COMPLEX_INT:
664 case MODE_COMPLEX_FLOAT:
665 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
666 break;
668 case MODE_VECTOR_INT:
669 case MODE_VECTOR_FLOAT:
670 warning (OPT_Wattributes, "specifying vector types with "
671 "__attribute__ ((mode)) is deprecated");
672 warning (OPT_Wattributes,
673 "use __attribute__ ((vector_size)) instead");
674 valid_mode = vector_mode_valid_p (mode);
675 break;
677 default:
678 break;
680 if (!valid_mode)
682 error ("unable to emulate %qs", p);
683 return NULL_TREE;
686 if (POINTER_TYPE_P (type))
688 tree (*fn)(tree, enum machine_mode, bool);
690 if (!targetm.valid_pointer_mode (mode))
692 error ("invalid pointer mode %qs", p);
693 return NULL_TREE;
696 if (TREE_CODE (type) == POINTER_TYPE)
697 fn = build_pointer_type_for_mode;
698 else
699 fn = build_reference_type_for_mode;
700 typefm = fn (TREE_TYPE (type), mode, false);
702 else
703 typefm = lang_hooks.types.type_for_mode (mode, TYPE_UNSIGNED (type));
705 if (typefm == NULL_TREE)
707 error ("no data type for mode %qs", p);
708 return NULL_TREE;
710 else if (TREE_CODE (type) == ENUMERAL_TYPE)
712 /* For enumeral types, copy the precision from the integer
713 type returned above. If not an INTEGER_TYPE, we can't use
714 this mode for this type. */
715 if (TREE_CODE (typefm) != INTEGER_TYPE)
717 error ("cannot use mode %qs for enumeral types", p);
718 return NULL_TREE;
721 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
723 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
724 typefm = type;
726 else
728 /* We cannot build a type variant, as there's code that assumes
729 that TYPE_MAIN_VARIANT has the same mode. This includes the
730 debug generators. Instead, create a subrange type. This
731 results in all of the enumeral values being emitted only once
732 in the original, and the subtype gets them by reference. */
733 if (TYPE_UNSIGNED (type))
734 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
735 else
736 typefm = make_signed_type (TYPE_PRECISION (typefm));
737 TREE_TYPE (typefm) = type;
740 else if (VECTOR_MODE_P (mode)
741 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
742 : TREE_CODE (type) != TREE_CODE (typefm))
744 error ("mode %qs applied to inappropriate type", p);
745 return NULL_TREE;
748 *node = typefm;
751 return NULL_TREE;
754 /* Handle a "section" attribute; arguments as in
755 struct attribute_spec.handler. */
757 static tree
758 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
759 int ARG_UNUSED (flags), bool *no_add_attrs)
761 tree decl = *node;
763 if (targetm.have_named_sections)
765 user_defined_section_attribute = true;
767 if ((TREE_CODE (decl) == FUNCTION_DECL
768 || TREE_CODE (decl) == VAR_DECL)
769 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
771 if (TREE_CODE (decl) == VAR_DECL
772 && current_function_decl != NULL_TREE
773 && ! (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
775 error ("%Jsection attribute cannot be specified for "
776 "local variables", decl);
777 *no_add_attrs = true;
780 /* The decl may have already been given a section attribute
781 from a previous declaration. Ensure they match. */
782 else if (DECL_SECTION_NAME (decl) != NULL_TREE
783 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
784 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
786 error ("section of %q+D conflicts with previous declaration",
787 *node);
788 *no_add_attrs = true;
790 else
791 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
793 else
795 error ("section attribute not allowed for %q+D", *node);
796 *no_add_attrs = true;
799 else
801 error ("%Jsection attributes are not supported for this target", *node);
802 *no_add_attrs = true;
805 return NULL_TREE;
808 /* Handle a "aligned" attribute; arguments as in
809 struct attribute_spec.handler. */
811 static tree
812 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
813 int flags, bool *no_add_attrs)
815 tree decl = NULL_TREE;
816 tree *type = NULL;
817 int is_type = 0;
818 tree align_expr = (args ? TREE_VALUE (args)
819 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
820 int i;
822 if (DECL_P (*node))
824 decl = *node;
825 type = &TREE_TYPE (decl);
826 is_type = TREE_CODE (*node) == TYPE_DECL;
828 else if (TYPE_P (*node))
829 type = node, is_type = 1;
831 if (TREE_CODE (align_expr) != INTEGER_CST)
833 error ("requested alignment is not a constant");
834 *no_add_attrs = true;
836 else if ((i = tree_log2 (align_expr)) == -1)
838 error ("requested alignment is not a power of 2");
839 *no_add_attrs = true;
841 else if (i > HOST_BITS_PER_INT - 2)
843 error ("requested alignment is too large");
844 *no_add_attrs = true;
846 else if (is_type)
848 /* If we have a TYPE_DECL, then copy the type, so that we
849 don't accidentally modify a builtin type. See pushdecl. */
850 if (decl && TREE_TYPE (decl) != error_mark_node
851 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
853 tree tt = TREE_TYPE (decl);
854 *type = build_variant_type_copy (*type);
855 DECL_ORIGINAL_TYPE (decl) = tt;
856 TYPE_NAME (*type) = decl;
857 TREE_USED (*type) = TREE_USED (decl);
858 TREE_TYPE (decl) = *type;
860 else if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
861 *type = build_variant_type_copy (*type);
863 TYPE_ALIGN (*type) = (1 << i) * BITS_PER_UNIT;
864 TYPE_USER_ALIGN (*type) = 1;
866 else if (TREE_CODE (decl) != VAR_DECL
867 && TREE_CODE (decl) != FIELD_DECL)
869 error ("alignment may not be specified for %q+D", decl);
870 *no_add_attrs = true;
872 else
874 DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT;
875 DECL_USER_ALIGN (decl) = 1;
878 return NULL_TREE;
881 /* Handle a "weak" attribute; arguments as in
882 struct attribute_spec.handler. */
884 static tree
885 handle_weak_attribute (tree *node, tree ARG_UNUSED (name),
886 tree ARG_UNUSED (args),
887 int ARG_UNUSED (flags),
888 bool * ARG_UNUSED (no_add_attrs))
890 declare_weak (*node);
892 return NULL_TREE;
895 /* Handle an "alias" attribute; arguments as in
896 struct attribute_spec.handler. */
898 static tree
899 handle_alias_attribute (tree *node, tree name, tree args,
900 int ARG_UNUSED (flags), bool *no_add_attrs)
902 tree decl = *node;
904 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
905 || (TREE_CODE (decl) != FUNCTION_DECL && !DECL_EXTERNAL (decl)))
907 error ("%q+D defined both normally and as an alias", decl);
908 *no_add_attrs = true;
911 /* Note that the very first time we process a nested declaration,
912 decl_function_context will not be set. Indeed, *would* never
913 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
914 we do below. After such frobbery, pushdecl would set the context.
915 In any case, this is never what we want. */
916 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
918 tree id;
920 id = TREE_VALUE (args);
921 if (TREE_CODE (id) != STRING_CST)
923 error ("alias argument not a string");
924 *no_add_attrs = true;
925 return NULL_TREE;
927 id = get_identifier (TREE_STRING_POINTER (id));
928 /* This counts as a use of the object pointed to. */
929 TREE_USED (id) = 1;
931 if (TREE_CODE (decl) == FUNCTION_DECL)
932 DECL_INITIAL (decl) = error_mark_node;
933 else
935 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
936 DECL_EXTERNAL (decl) = 1;
937 else
938 DECL_EXTERNAL (decl) = 0;
939 TREE_STATIC (decl) = 1;
942 else
944 warning (OPT_Wattributes, "%qE attribute ignored", name);
945 *no_add_attrs = true;
948 return NULL_TREE;
951 /* Handle a "weakref" attribute; arguments as in struct
952 attribute_spec.handler. */
954 static tree
955 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
956 int flags, bool *no_add_attrs)
958 tree attr = NULL_TREE;
960 /* The idea here is that `weakref("name")' mutates into `weakref,
961 alias("name")', and weakref without arguments, in turn,
962 implicitly adds weak. */
964 if (args)
966 attr = tree_cons (get_identifier ("alias"), args, attr);
967 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
969 *no_add_attrs = true;
971 else
973 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
974 error ("%Jweakref attribute must appear before alias attribute",
975 *node);
977 attr = tree_cons (get_identifier ("weak"), NULL_TREE, attr);
980 decl_attributes (node, attr, flags);
982 return NULL_TREE;
985 /* Handle an "visibility" attribute; arguments as in
986 struct attribute_spec.handler. */
988 static tree
989 handle_visibility_attribute (tree *node, tree name, tree args,
990 int ARG_UNUSED (flags),
991 bool *no_add_attrs)
993 tree decl = *node;
994 tree id = TREE_VALUE (args);
996 *no_add_attrs = true;
998 if (TYPE_P (*node))
1000 if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
1002 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
1003 name);
1004 return NULL_TREE;
1007 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
1009 warning (OPT_Wattributes, "%qE attribute ignored", name);
1010 return NULL_TREE;
1013 if (TREE_CODE (id) != STRING_CST)
1015 error ("visibility argument not a string");
1016 return NULL_TREE;
1019 /* If this is a type, set the visibility on the type decl. */
1020 if (TYPE_P (decl))
1022 decl = TYPE_NAME (decl);
1023 if (!decl)
1024 return NULL_TREE;
1025 if (TREE_CODE (decl) == IDENTIFIER_NODE)
1027 warning (OPT_Wattributes, "%qE attribute ignored on types",
1028 name);
1029 return NULL_TREE;
1033 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
1034 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
1035 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
1036 DECL_VISIBILITY (decl) = VISIBILITY_INTERNAL;
1037 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
1038 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
1039 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
1040 DECL_VISIBILITY (decl) = VISIBILITY_PROTECTED;
1041 else
1042 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
1043 DECL_VISIBILITY_SPECIFIED (decl) = 1;
1045 /* For decls only, go ahead and attach the attribute to the node as well.
1046 This is needed so we can determine whether we have VISIBILITY_DEFAULT
1047 because the visibility was not specified, or because it was explicitly
1048 overridden from the class visibility. */
1049 if (DECL_P (*node))
1050 *no_add_attrs = false;
1052 return NULL_TREE;
1055 /* Determine the ELF symbol visibility for DECL, which is either a
1056 variable or a function. It is an error to use this function if a
1057 definition of DECL is not available in this translation unit.
1058 Returns true if the final visibility has been determined by this
1059 function; false if the caller is free to make additional
1060 modifications. */
1062 bool
1063 c_determine_visibility (tree decl)
1065 gcc_assert (TREE_CODE (decl) == VAR_DECL
1066 || TREE_CODE (decl) == FUNCTION_DECL);
1068 /* If the user explicitly specified the visibility with an
1069 attribute, honor that. DECL_VISIBILITY will have been set during
1070 the processing of the attribute. We check for an explicit
1071 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
1072 to distinguish the use of an attribute from the use of a "#pragma
1073 GCC visibility push(...)"; in the latter case we still want other
1074 considerations to be able to overrule the #pragma. */
1075 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl)))
1076 return true;
1078 /* Anything that is exported must have default visibility. */
1079 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
1080 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
1082 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
1083 DECL_VISIBILITY_SPECIFIED (decl) = 1;
1084 return true;
1087 return false;
1090 /* Handle an "tls_model" attribute; arguments as in
1091 struct attribute_spec.handler. */
1093 static tree
1094 handle_tls_model_attribute (tree *node, tree name, tree args,
1095 int ARG_UNUSED (flags), bool *no_add_attrs)
1097 tree id;
1098 tree decl = *node;
1099 enum tls_model kind;
1101 *no_add_attrs = true;
1103 if (!DECL_THREAD_LOCAL_P (decl))
1105 warning (OPT_Wattributes, "%qE attribute ignored", name);
1106 return NULL_TREE;
1109 kind = DECL_TLS_MODEL (decl);
1110 id = TREE_VALUE (args);
1111 if (TREE_CODE (id) != STRING_CST)
1113 error ("tls_model argument not a string");
1114 return NULL_TREE;
1117 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
1118 kind = TLS_MODEL_LOCAL_EXEC;
1119 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
1120 kind = TLS_MODEL_INITIAL_EXEC;
1121 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
1122 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
1123 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
1124 kind = TLS_MODEL_GLOBAL_DYNAMIC;
1125 else
1126 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
1128 DECL_TLS_MODEL (decl) = kind;
1129 return NULL_TREE;
1132 /* Handle a "no_instrument_function" attribute; arguments as in
1133 struct attribute_spec.handler. */
1135 static tree
1136 handle_no_instrument_function_attribute (tree *node, tree name,
1137 tree ARG_UNUSED (args),
1138 int ARG_UNUSED (flags),
1139 bool *no_add_attrs)
1141 tree decl = *node;
1143 if (TREE_CODE (decl) != FUNCTION_DECL)
1145 error ("%J%qE attribute applies only to functions", decl, name);
1146 *no_add_attrs = true;
1148 else if (DECL_INITIAL (decl))
1150 error ("%Jcan%'t set %qE attribute after definition", decl, name);
1151 *no_add_attrs = true;
1153 else
1154 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1156 return NULL_TREE;
1159 /* Handle a "malloc" attribute; arguments as in
1160 struct attribute_spec.handler. */
1162 static tree
1163 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
1164 int ARG_UNUSED (flags), bool *no_add_attrs)
1166 if (TREE_CODE (*node) == FUNCTION_DECL
1167 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
1168 DECL_IS_MALLOC (*node) = 1;
1169 else
1171 warning (OPT_Wattributes, "%qE attribute ignored", name);
1172 *no_add_attrs = true;
1175 return NULL_TREE;
1178 /* Handle a "returns_twice" attribute; arguments as in
1179 struct attribute_spec.handler. */
1181 static tree
1182 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
1183 int ARG_UNUSED (flags), bool *no_add_attrs)
1185 if (TREE_CODE (*node) == FUNCTION_DECL)
1186 DECL_IS_RETURNS_TWICE (*node) = 1;
1187 else
1189 warning (OPT_Wattributes, "%qE attribute ignored", name);
1190 *no_add_attrs = true;
1193 return NULL_TREE;
1196 /* Handle a "no_limit_stack" attribute; arguments as in
1197 struct attribute_spec.handler. */
1199 static tree
1200 handle_no_limit_stack_attribute (tree *node, tree name,
1201 tree ARG_UNUSED (args),
1202 int ARG_UNUSED (flags),
1203 bool *no_add_attrs)
1205 tree decl = *node;
1207 if (TREE_CODE (decl) != FUNCTION_DECL)
1209 error ("%J%qE attribute applies only to functions", decl, name);
1210 *no_add_attrs = true;
1212 else if (DECL_INITIAL (decl))
1214 error ("%Jcan%'t set %qE attribute after definition", decl, name);
1215 *no_add_attrs = true;
1217 else
1218 DECL_NO_LIMIT_STACK (decl) = 1;
1220 return NULL_TREE;
1223 /* Handle a "pure" attribute; arguments as in
1224 struct attribute_spec.handler. */
1226 static tree
1227 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
1228 int ARG_UNUSED (flags), bool *no_add_attrs)
1230 if (TREE_CODE (*node) == FUNCTION_DECL)
1231 DECL_IS_PURE (*node) = 1;
1232 /* ??? TODO: Support types. */
1233 else
1235 warning (OPT_Wattributes, "%qE attribute ignored", name);
1236 *no_add_attrs = true;
1239 return NULL_TREE;
1242 /* Handle a "no vops" attribute; arguments as in
1243 struct attribute_spec.handler. */
1245 static tree
1246 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
1247 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
1248 bool *ARG_UNUSED (no_add_attrs))
1250 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
1251 DECL_IS_NOVOPS (*node) = 1;
1252 return NULL_TREE;
1255 /* Handle a "deprecated" attribute; arguments as in
1256 struct attribute_spec.handler. */
1258 static tree
1259 handle_deprecated_attribute (tree *node, tree name,
1260 tree ARG_UNUSED (args), int flags,
1261 bool *no_add_attrs)
1263 tree type = NULL_TREE;
1264 int warn = 0;
1265 tree what = NULL_TREE;
1267 if (DECL_P (*node))
1269 tree decl = *node;
1270 type = TREE_TYPE (decl);
1272 if (TREE_CODE (decl) == TYPE_DECL
1273 || TREE_CODE (decl) == PARM_DECL
1274 || TREE_CODE (decl) == VAR_DECL
1275 || TREE_CODE (decl) == FUNCTION_DECL
1276 || TREE_CODE (decl) == FIELD_DECL)
1277 TREE_DEPRECATED (decl) = 1;
1278 else
1279 warn = 1;
1281 else if (TYPE_P (*node))
1283 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1284 *node = build_variant_type_copy (*node);
1285 TREE_DEPRECATED (*node) = 1;
1286 type = *node;
1288 else
1289 warn = 1;
1291 if (warn)
1293 *no_add_attrs = true;
1294 if (type && TYPE_NAME (type))
1296 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
1297 what = TYPE_NAME (*node);
1298 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1299 && DECL_NAME (TYPE_NAME (type)))
1300 what = DECL_NAME (TYPE_NAME (type));
1302 if (what)
1303 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
1304 else
1305 warning (OPT_Wattributes, "%qE attribute ignored", name);
1308 return NULL_TREE;
1311 /* Handle a "vector_size" attribute; arguments as in
1312 struct attribute_spec.handler. */
1314 static tree
1315 handle_vector_size_attribute (tree *node, tree name, tree args,
1316 int ARG_UNUSED (flags),
1317 bool *no_add_attrs)
1319 unsigned HOST_WIDE_INT vecsize, nunits;
1320 enum machine_mode orig_mode;
1321 tree type = *node, new_type, size;
1323 *no_add_attrs = true;
1325 size = TREE_VALUE (args);
1327 if (!host_integerp (size, 1))
1329 warning (OPT_Wattributes, "%qE attribute ignored", name);
1330 return NULL_TREE;
1333 /* Get the vector size (in bytes). */
1334 vecsize = tree_low_cst (size, 1);
1336 /* We need to provide for vector pointers, vector arrays, and
1337 functions returning vectors. For example:
1339 __attribute__((vector_size(16))) short *foo;
1341 In this case, the mode is SI, but the type being modified is
1342 HI, so we need to look further. */
1344 while (POINTER_TYPE_P (type)
1345 || TREE_CODE (type) == FUNCTION_TYPE
1346 || TREE_CODE (type) == METHOD_TYPE
1347 || TREE_CODE (type) == ARRAY_TYPE)
1348 type = TREE_TYPE (type);
1350 /* Get the mode of the type being modified. */
1351 orig_mode = TYPE_MODE (type);
1353 if (TREE_CODE (type) == RECORD_TYPE
1354 || (GET_MODE_CLASS (orig_mode) != MODE_FLOAT
1355 && GET_MODE_CLASS (orig_mode) != MODE_INT)
1356 || !host_integerp (TYPE_SIZE_UNIT (type), 1))
1358 error ("invalid vector type for attribute %qE", name);
1359 return NULL_TREE;
1362 if (vecsize % tree_low_cst (TYPE_SIZE_UNIT (type), 1))
1364 error ("vector size not an integral multiple of component size");
1365 return NULL;
1368 if (vecsize == 0)
1370 error ("zero vector size");
1371 return NULL;
1374 /* Calculate how many units fit in the vector. */
1375 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
1376 if (nunits & (nunits - 1))
1378 error ("number of components of the vector not a power of two");
1379 return NULL_TREE;
1382 new_type = build_vector_type (type, nunits);
1384 /* Build back pointers if needed. */
1385 *node = reconstruct_complex_type (*node, new_type);
1387 return NULL_TREE;
1390 /* Handle the "nonnull" attribute. */
1391 static tree
1392 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
1393 tree args, int ARG_UNUSED (flags),
1394 bool *no_add_attrs)
1396 tree type = *node;
1397 unsigned HOST_WIDE_INT attr_arg_num;
1399 /* If no arguments are specified, all pointer arguments should be
1400 non-null. Verify a full prototype is given so that the arguments
1401 will have the correct types when we actually check them later. */
1402 if (!args)
1404 if (!TYPE_ARG_TYPES (type))
1406 error ("nonnull attribute without arguments on a non-prototype");
1407 *no_add_attrs = true;
1409 return NULL_TREE;
1412 /* Argument list specified. Verify that each argument number references
1413 a pointer argument. */
1414 for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
1416 tree argument;
1417 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
1419 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
1421 error ("nonnull argument has invalid operand number (argument %lu)",
1422 (unsigned long) attr_arg_num);
1423 *no_add_attrs = true;
1424 return NULL_TREE;
1427 argument = TYPE_ARG_TYPES (type);
1428 if (argument)
1430 for (ck_num = 1; ; ck_num++)
1432 if (!argument || ck_num == arg_num)
1433 break;
1434 argument = TREE_CHAIN (argument);
1437 if (!argument
1438 || TREE_CODE (TREE_VALUE (argument)) == VOID_TYPE)
1440 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
1441 (unsigned long) attr_arg_num, (unsigned long) arg_num);
1442 *no_add_attrs = true;
1443 return NULL_TREE;
1446 if (TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE)
1448 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
1449 (unsigned long) attr_arg_num, (unsigned long) arg_num);
1450 *no_add_attrs = true;
1451 return NULL_TREE;
1456 return NULL_TREE;
1459 /* Check the argument list of a function call for null in argument slots
1460 that are marked as requiring a non-null pointer argument. */
1462 static void
1463 check_function_nonnull (tree attrs, tree params)
1465 tree a, args, param;
1466 int param_num;
1468 for (a = attrs; a; a = TREE_CHAIN (a))
1470 if (is_attribute_p ("nonnull", TREE_PURPOSE (a)))
1472 args = TREE_VALUE (a);
1474 /* Walk the argument list. If we encounter an argument number we
1475 should check for non-null, do it. If the attribute has no args,
1476 then every pointer argument is checked (in which case the check
1477 for pointer type is done in check_nonnull_arg). */
1478 for (param = params, param_num = 1; ;
1479 param_num++, param = TREE_CHAIN (param))
1481 if (!param)
1482 break;
1483 if (!args || nonnull_check_p (args, param_num))
1484 check_function_arguments_recurse (check_nonnull_arg, NULL,
1485 TREE_VALUE (param),
1486 param_num);
1492 /* Check that the Nth argument of a function call (counting backwards
1493 from the end) is a (pointer)0. */
1495 static void
1496 check_function_sentinel (tree attrs, tree params, tree typelist)
1498 tree attr = lookup_attribute ("sentinel", attrs);
1500 if (attr)
1502 /* Skip over the named arguments. */
1503 while (typelist && params)
1505 typelist = TREE_CHAIN (typelist);
1506 params = TREE_CHAIN (params);
1509 if (typelist || !params)
1510 warning (OPT_Wformat,
1511 "not enough variable arguments to fit a sentinel");
1512 else
1514 tree sentinel, end;
1515 unsigned pos = 0;
1517 if (TREE_VALUE (attr))
1519 tree p = TREE_VALUE (TREE_VALUE (attr));
1520 pos = TREE_INT_CST_LOW (p);
1523 sentinel = end = params;
1525 /* Advance `end' ahead of `sentinel' by `pos' positions. */
1526 while (pos > 0 && TREE_CHAIN (end))
1528 pos--;
1529 end = TREE_CHAIN (end);
1531 if (pos > 0)
1533 warning (OPT_Wformat,
1534 "not enough variable arguments to fit a sentinel");
1535 return;
1538 /* Now advance both until we find the last parameter. */
1539 while (TREE_CHAIN (end))
1541 end = TREE_CHAIN (end);
1542 sentinel = TREE_CHAIN (sentinel);
1545 /* Validate the sentinel. */
1546 if ((!POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (sentinel)))
1547 || !integer_zerop (TREE_VALUE (sentinel)))
1548 /* Although __null (in C++) is only an integer we allow it
1549 nevertheless, as we are guaranteed that it's exactly
1550 as wide as a pointer, and we don't want to force
1551 users to cast the NULL they have written there.
1552 We warn with -Wstrict-null-sentinel, though. */
1553 && (warn_strict_null_sentinel
1554 || null_node != TREE_VALUE (sentinel)))
1555 warning (OPT_Wformat, "missing sentinel in function call");
1560 /* Helper for check_function_nonnull; given a list of operands which
1561 must be non-null in ARGS, determine if operand PARAM_NUM should be
1562 checked. */
1564 static bool
1565 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
1567 unsigned HOST_WIDE_INT arg_num = 0;
1569 for (; args; args = TREE_CHAIN (args))
1571 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
1573 gcc_assert (found);
1575 if (arg_num == param_num)
1576 return true;
1578 return false;
1581 /* Check that the function argument PARAM (which is operand number
1582 PARAM_NUM) is non-null. This is called by check_function_nonnull
1583 via check_function_arguments_recurse. */
1585 static void
1586 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
1587 unsigned HOST_WIDE_INT param_num)
1589 /* Just skip checking the argument if it's not a pointer. This can
1590 happen if the "nonnull" attribute was given without an operand
1591 list (which means to check every pointer argument). */
1593 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
1594 return;
1596 if (integer_zerop (param))
1597 warning (OPT_Wnonnull, "null argument where non-null required "
1598 "(argument %lu)", (unsigned long) param_num);
1601 /* Helper for nonnull attribute handling; fetch the operand number
1602 from the attribute argument list. */
1604 static bool
1605 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
1607 /* Verify the arg number is a constant. */
1608 if (TREE_CODE (arg_num_expr) != INTEGER_CST
1609 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
1610 return false;
1612 *valp = TREE_INT_CST_LOW (arg_num_expr);
1613 return true;
1616 /* Handle a "nothrow" attribute; arguments as in
1617 struct attribute_spec.handler. */
1619 static tree
1620 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
1621 int ARG_UNUSED (flags), bool *no_add_attrs)
1623 if (TREE_CODE (*node) == FUNCTION_DECL)
1624 TREE_NOTHROW (*node) = 1;
1625 /* ??? TODO: Support types. */
1626 else
1628 warning (OPT_Wattributes, "%qE attribute ignored", name);
1629 *no_add_attrs = true;
1632 return NULL_TREE;
1635 /* Handle a "warn_unused_result" attribute. No special handling. */
1637 static tree
1638 handle_warn_unused_result_attribute (tree *node, tree name,
1639 tree ARG_UNUSED (args),
1640 int ARG_UNUSED (flags), bool *no_add_attrs)
1642 /* Ignore the attribute for functions not returning any value. */
1643 if (VOID_TYPE_P (TREE_TYPE (*node)))
1645 warning (OPT_Wattributes, "%qE attribute ignored", name);
1646 *no_add_attrs = true;
1649 return NULL_TREE;
1652 /* Handle a "sentinel" attribute. */
1654 static tree
1655 handle_sentinel_attribute (tree *node, tree name, tree args,
1656 int ARG_UNUSED (flags), bool *no_add_attrs)
1658 tree params = TYPE_ARG_TYPES (*node);
1660 if (!params)
1662 warning (OPT_Wattributes,
1663 "%qE attribute requires prototypes with named arguments", name);
1664 *no_add_attrs = true;
1666 else
1668 while (TREE_CHAIN (params))
1669 params = TREE_CHAIN (params);
1671 if (VOID_TYPE_P (TREE_VALUE (params)))
1673 warning (OPT_Wattributes,
1674 "%qE attribute only applies to variadic functions", name);
1675 *no_add_attrs = true;
1679 if (args)
1681 tree position = TREE_VALUE (args);
1683 if (TREE_CODE (position) != INTEGER_CST)
1685 warning (0, "requested position is not an integer constant");
1686 *no_add_attrs = true;
1688 else
1690 if (tree_int_cst_lt (position, integer_zero_node))
1692 warning (0, "requested position is less than zero");
1693 *no_add_attrs = true;
1698 return NULL_TREE;
1701 /* Check for valid arguments being passed to a function. */
1702 void
1703 check_function_arguments (tree attrs, tree params, tree typelist)
1705 /* Check for null being passed in a pointer argument that must be
1706 non-null. We also need to do this if format checking is enabled. */
1708 if (warn_nonnull)
1709 check_function_nonnull (attrs, params);
1711 /* Check for errors in format strings. */
1713 /* disabled in gdc
1714 if (warn_format || warn_missing_format_attribute)
1715 check_function_format (attrs, params);
1718 if (warn_format)
1719 check_function_sentinel (attrs, params, typelist);
1722 /* Generic argument checking recursion routine. PARAM is the argument to
1723 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
1724 once the argument is resolved. CTX is context for the callback. */
1725 void
1726 check_function_arguments_recurse (void (*callback)
1727 (void *, tree, unsigned HOST_WIDE_INT),
1728 void *ctx, tree param,
1729 unsigned HOST_WIDE_INT param_num)
1731 if (TREE_CODE (param) == NOP_EXPR)
1733 /* Strip coercion. */
1734 check_function_arguments_recurse (callback, ctx,
1735 TREE_OPERAND (param, 0), param_num);
1736 return;
1739 if (TREE_CODE (param) == CALL_EXPR)
1741 tree type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (param, 0)));
1742 tree attrs;
1743 bool found_format_arg = false;
1745 /* See if this is a call to a known internationalization function
1746 that modifies a format arg. Such a function may have multiple
1747 format_arg attributes (for example, ngettext). */
1749 for (attrs = TYPE_ATTRIBUTES (type);
1750 attrs;
1751 attrs = TREE_CHAIN (attrs))
1752 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
1754 tree inner_args;
1755 tree format_num_expr;
1756 int format_num;
1757 int i;
1759 /* Extract the argument number, which was previously checked
1760 to be valid. */
1761 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
1763 gcc_assert (TREE_CODE (format_num_expr) == INTEGER_CST
1764 && !TREE_INT_CST_HIGH (format_num_expr));
1766 format_num = TREE_INT_CST_LOW (format_num_expr);
1768 for (inner_args = TREE_OPERAND (param, 1), i = 1;
1769 inner_args != 0;
1770 inner_args = TREE_CHAIN (inner_args), i++)
1771 if (i == format_num)
1773 check_function_arguments_recurse (callback, ctx,
1774 TREE_VALUE (inner_args),
1775 param_num);
1776 found_format_arg = true;
1777 break;
1781 /* If we found a format_arg attribute and did a recursive check,
1782 we are done with checking this argument. Otherwise, we continue
1783 and this will be considered a non-literal. */
1784 if (found_format_arg)
1785 return;
1788 if (TREE_CODE (param) == COND_EXPR)
1790 /* Check both halves of the conditional expression. */
1791 check_function_arguments_recurse (callback, ctx,
1792 TREE_OPERAND (param, 1), param_num);
1793 check_function_arguments_recurse (callback, ctx,
1794 TREE_OPERAND (param, 2), param_num);
1795 return;
1798 (*callback) (ctx, param, param_num);