1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000
3 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
36 #include "diagnostic.h"
39 cpp_reader *parse_in; /* Declared in c-lex.h. */
41 #undef WCHAR_TYPE_SIZE
42 #define WCHAR_TYPE_SIZE TYPE_PRECISION (wchar_type_node)
45 #define PTRDIFF_TYPE "long int"
49 #define WINT_TYPE "unsigned int"
53 #define INTMAX_TYPE ((INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
55 : ((LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
61 #define UINTMAX_TYPE ((INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
63 : ((LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
64 ? "long unsigned int" \
65 : "long long unsigned int"))
68 /* The following symbols are subsumed in the c_global_trees array, and
69 listed here individually for documentation purposes.
71 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
73 tree short_integer_type_node;
74 tree long_integer_type_node;
75 tree long_long_integer_type_node;
77 tree short_unsigned_type_node;
78 tree long_unsigned_type_node;
79 tree long_long_unsigned_type_node;
81 tree boolean_type_node;
82 tree boolean_false_node;
83 tree boolean_true_node;
85 tree ptrdiff_type_node;
87 tree unsigned_char_type_node;
88 tree signed_char_type_node;
90 tree signed_wchar_type_node;
91 tree unsigned_wchar_type_node;
94 tree double_type_node;
95 tree long_double_type_node;
97 tree complex_integer_type_node;
98 tree complex_float_type_node;
99 tree complex_double_type_node;
100 tree complex_long_double_type_node;
102 tree intQI_type_node;
103 tree intHI_type_node;
104 tree intSI_type_node;
105 tree intDI_type_node;
106 tree intTI_type_node;
108 tree unsigned_intQI_type_node;
109 tree unsigned_intHI_type_node;
110 tree unsigned_intSI_type_node;
111 tree unsigned_intDI_type_node;
112 tree unsigned_intTI_type_node;
114 tree widest_integer_literal_type_node;
115 tree widest_unsigned_literal_type_node;
117 Nodes for types `void *' and `const void *'.
119 tree ptr_type_node, const_ptr_type_node;
121 Nodes for types `char *' and `const char *'.
123 tree string_type_node, const_string_type_node;
125 Type `char[SOMENUMBER]'.
126 Used when an array of char is needed and the size is irrelevant.
128 tree char_array_type_node;
130 Type `int[SOMENUMBER]' or something like it.
131 Used when an array of int needed and the size is irrelevant.
133 tree int_array_type_node;
135 Type `wchar_t[SOMENUMBER]' or something like it.
136 Used when a wide string literal is created.
138 tree wchar_array_type_node;
140 Type `int ()' -- used for implicit declaration of functions.
142 tree default_function_type;
144 Function types `int (int)', etc.
150 tree ptr_ftype_sizetype;
152 A VOID_TYPE node, packaged in a TREE_LIST.
156 The identifiers __FUNCTION__, __PRETTY_FUNCTION__, and __func__.
158 tree function_id_node;
159 tree pretty_function_id_node;
164 tree c_global_trees[CTI_MAX];
166 /* Nonzero means don't recognize the non-ANSI builtin functions. */
170 /* Nonzero means don't recognize the non-ANSI builtin functions.
173 int flag_no_nonansi_builtin;
175 /* If non-NULL, dump the tree structure for the entire translation
176 unit to this file. */
178 const char *flag_dump_translation_unit;
180 /* Warn about *printf or *scanf format/argument anomalies. */
184 /* Warn about Y2K problems with strftime formats. */
188 /* Warn about excess arguments to formats. */
190 int warn_format_extra_args;
192 /* Warn about non-literal format arguments. */
194 int warn_format_nonliteral;
196 /* Warn about possible security problems with calls to format functions. */
198 int warn_format_security;
200 /* Nonzero means warn about possible violations of sequence point rules. */
202 int warn_sequence_point;
204 /* The elements of `ridpointers' are identifier nodes for the reserved
205 type names and storage classes. It is indexed by a RID_... value. */
208 tree (*make_fname_decl) PARAMS ((tree, const char *, int));
210 /* If non-NULL, the address of a language-specific function that
211 returns 1 for language-specific statement codes. */
212 int (*lang_statement_code_p) PARAMS ((enum tree_code));
214 /* If non-NULL, the address of a language-specific function that takes
215 any action required right before expand_function_end is called. */
216 void (*lang_expand_function_end) PARAMS ((void));
218 /* If this variable is defined to a non-NULL value, it will be called
219 after the file has been completely parsed. */
220 void (*back_end_hook) PARAMS ((tree));
222 /* Nonzero means the expression being parsed will never be evaluated.
223 This is a count, since unevaluated expressions can nest. */
226 enum attrs {A_PACKED, A_NOCOMMON, A_COMMON, A_NORETURN, A_CONST, A_T_UNION,
227 A_NO_CHECK_MEMORY_USAGE, A_NO_INSTRUMENT_FUNCTION,
228 A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
229 A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS, A_MALLOC,
230 A_NO_LIMIT_STACK, A_PURE};
232 enum format_type { printf_format_type, scanf_format_type,
233 strftime_format_type };
235 static void add_attribute PARAMS ((enum attrs, const char *,
237 static void init_attributes PARAMS ((void));
238 static void record_function_format PARAMS ((tree, tree, enum format_type,
240 static void record_international_format PARAMS ((tree, tree, int));
241 static int default_valid_lang_attribute PARAMS ((tree, tree, tree, tree));
243 /* Keep a stack of if statements. We record the number of compound
244 statements seen up to the if keyword, as well as the line number
245 and file of the if. If a potentially ambiguous else is seen, that
246 fact is recorded; the warning is issued when we can be sure that
247 the enclosing if statement does not have an else branch. */
257 static if_elt *if_stack;
259 /* Amount of space in the if statement stack. */
260 static int if_stack_space = 0;
263 static int if_stack_pointer = 0;
265 /* Record the start of an if-then, and record the start of it
266 for ambiguous else detection. */
269 c_expand_start_cond (cond, compstmt_count)
275 /* Make sure there is enough space on the stack. */
276 if (if_stack_space == 0)
279 if_stack = (if_elt *)xmalloc (10 * sizeof (if_elt));
281 else if (if_stack_space == if_stack_pointer)
283 if_stack_space += 10;
284 if_stack = (if_elt *)xrealloc (if_stack, if_stack_space * sizeof (if_elt));
287 if_stmt = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
288 IF_COND (if_stmt) = cond;
291 /* Record this if statement. */
292 if_stack[if_stack_pointer].compstmt_count = compstmt_count;
293 if_stack[if_stack_pointer].file = input_filename;
294 if_stack[if_stack_pointer].line = lineno;
295 if_stack[if_stack_pointer].needs_warning = 0;
296 if_stack[if_stack_pointer].if_stmt = if_stmt;
300 /* Called after the then-clause for an if-statement is processed. */
305 tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt;
306 RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
309 /* Record the end of an if-then. Optionally warn if a nested
310 if statement had an ambiguous else clause. */
316 if (if_stack[if_stack_pointer].needs_warning)
317 warning_with_file_and_line (if_stack[if_stack_pointer].file,
318 if_stack[if_stack_pointer].line,
319 "suggest explicit braces to avoid ambiguous `else'");
320 last_expr_type = NULL_TREE;
323 /* Called between the then-clause and the else-clause
324 of an if-then-else. */
327 c_expand_start_else ()
329 /* An ambiguous else warning must be generated for the enclosing if
330 statement, unless we see an else branch for that one, too. */
332 && if_stack_pointer > 1
333 && (if_stack[if_stack_pointer - 1].compstmt_count
334 == if_stack[if_stack_pointer - 2].compstmt_count))
335 if_stack[if_stack_pointer - 2].needs_warning = 1;
337 /* Even if a nested if statement had an else branch, it can't be
338 ambiguous if this one also has an else. So don't warn in that
339 case. Also don't warn for any if statements nested in this else. */
340 if_stack[if_stack_pointer - 1].needs_warning = 0;
341 if_stack[if_stack_pointer - 1].compstmt_count--;
344 /* Called after the else-clause for an if-statement is processed. */
349 tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt;
350 RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
353 /* Make bindings for __FUNCTION__, __PRETTY_FUNCTION__, and __func__. */
356 declare_function_name ()
358 const char *name, *printable_name;
360 if (current_function_decl == NULL)
363 printable_name = "top level";
367 /* Allow functions to be nameless (such as artificial ones). */
368 if (DECL_NAME (current_function_decl))
369 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
372 printable_name = (*decl_printable_name) (current_function_decl, 2);
374 /* ISO C99 defines __func__, which is a variable, not a string
375 constant, and which is not a defined symbol at file scope. */
376 (*make_fname_decl) (func_id_node, name, 0);
379 (*make_fname_decl) (function_id_node, name, 0);
380 (*make_fname_decl) (pretty_function_id_node, printable_name, 1);
383 /* Given a chain of STRING_CST nodes,
384 concatenate them into one STRING_CST
385 and give it a suitable array-of-chars data type. */
388 combine_strings (strings)
391 register tree value, t;
392 register int length = 1;
395 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
397 const int nchars_max = flag_isoc99 ? 4095 : 509;
399 if (TREE_CHAIN (strings))
401 /* More than one in the chain, so concatenate. */
402 register char *p, *q;
404 /* Don't include the \0 at the end of each substring,
405 except for the last one.
406 Count wide strings and ordinary strings separately. */
407 for (t = strings; t; t = TREE_CHAIN (t))
409 if (TREE_TYPE (t) == wchar_array_type_node)
411 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
415 length += (TREE_STRING_LENGTH (t) - 1);
418 /* If anything is wide, the non-wides will be converted,
419 which makes them take more space. */
421 length = length * wchar_bytes + wide_length;
425 /* Copy the individual strings into the new combined string.
426 If the combined string is wide, convert the chars to ints
427 for any individual strings that are not wide. */
430 for (t = strings; t; t = TREE_CHAIN (t))
432 int len = (TREE_STRING_LENGTH (t)
433 - ((TREE_TYPE (t) == wchar_array_type_node)
435 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
437 memcpy (q, TREE_STRING_POINTER (t), len);
443 for (i = 0; i < len; i++)
445 if (WCHAR_TYPE_SIZE == HOST_BITS_PER_SHORT)
446 ((short *) q)[i] = TREE_STRING_POINTER (t)[i];
448 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
450 q += len * wchar_bytes;
456 for (i = 0; i < wchar_bytes; i++)
462 value = build_string (length, p);
467 length = TREE_STRING_LENGTH (value);
468 if (TREE_TYPE (value) == wchar_array_type_node)
472 /* Compute the number of elements, for the array type. */
473 nchars = wide_flag ? length / wchar_bytes : length;
475 if (pedantic && nchars - 1 > nchars_max && c_language == clk_c)
476 pedwarn ("string length `%d' is greater than the minimum length `%d' ISO C%d is required to support",
477 nchars - 1, nchars_max, flag_isoc99 ? 99 : 89);
479 /* Create the array type for the string constant.
480 -Wwrite-strings says make the string constant an array of const char
481 so that copying it to a non-const pointer will get a warning.
482 For C++, this is the standard behavior. */
483 if (flag_const_strings
484 && (! flag_traditional && ! flag_writable_strings))
487 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
490 = build_array_type (elements,
491 build_index_type (build_int_2 (nchars - 1, 0)));
495 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
496 build_index_type (build_int_2 (nchars - 1, 0)));
498 TREE_CONSTANT (value) = 1;
499 TREE_READONLY (value) = ! flag_writable_strings;
500 TREE_STATIC (value) = 1;
504 /* To speed up processing of attributes, we maintain an array of
505 IDENTIFIER_NODES and the corresponding attribute types. */
507 /* Array to hold attribute information. */
509 static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
511 static int attrtab_idx = 0;
513 /* Add an entry to the attribute table above. */
516 add_attribute (id, string, min_len, max_len, decl_req)
519 int min_len, max_len;
524 attrtab[attrtab_idx].id = id;
525 attrtab[attrtab_idx].name = get_identifier (string);
526 attrtab[attrtab_idx].min = min_len;
527 attrtab[attrtab_idx].max = max_len;
528 attrtab[attrtab_idx++].decl_req = decl_req;
530 sprintf (buf, "__%s__", string);
532 attrtab[attrtab_idx].id = id;
533 attrtab[attrtab_idx].name = get_identifier (buf);
534 attrtab[attrtab_idx].min = min_len;
535 attrtab[attrtab_idx].max = max_len;
536 attrtab[attrtab_idx++].decl_req = decl_req;
539 /* Initialize attribute table. */
544 add_attribute (A_PACKED, "packed", 0, 0, 0);
545 add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
546 add_attribute (A_COMMON, "common", 0, 0, 1);
547 add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
548 add_attribute (A_NORETURN, "volatile", 0, 0, 1);
549 add_attribute (A_UNUSED, "unused", 0, 0, 0);
550 add_attribute (A_CONST, "const", 0, 0, 1);
551 add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
552 add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
553 add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
554 add_attribute (A_MODE, "mode", 1, 1, 1);
555 add_attribute (A_SECTION, "section", 1, 1, 1);
556 add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
557 add_attribute (A_FORMAT, "format", 3, 3, 1);
558 add_attribute (A_FORMAT_ARG, "format_arg", 1, 1, 1);
559 add_attribute (A_WEAK, "weak", 0, 0, 1);
560 add_attribute (A_ALIAS, "alias", 1, 1, 1);
561 add_attribute (A_NO_INSTRUMENT_FUNCTION, "no_instrument_function", 0, 0, 1);
562 add_attribute (A_NO_CHECK_MEMORY_USAGE, "no_check_memory_usage", 0, 0, 1);
563 add_attribute (A_MALLOC, "malloc", 0, 0, 1);
564 add_attribute (A_NO_LIMIT_STACK, "no_stack_limit", 0, 0, 1);
565 add_attribute (A_PURE, "pure", 0, 0, 1);
568 /* Default implementation of valid_lang_attribute, below. By default, there
569 are no language-specific attributes. */
572 default_valid_lang_attribute (attr_name, attr_args, decl, type)
573 tree attr_name ATTRIBUTE_UNUSED;
574 tree attr_args ATTRIBUTE_UNUSED;
575 tree decl ATTRIBUTE_UNUSED;
576 tree type ATTRIBUTE_UNUSED;
581 /* Return a 1 if ATTR_NAME and ATTR_ARGS denote a valid language-specific
582 attribute for either declaration DECL or type TYPE and 0 otherwise. */
584 int (*valid_lang_attribute) PARAMS ((tree, tree, tree, tree))
585 = default_valid_lang_attribute;
587 /* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
588 and install them in NODE, which is either a DECL (including a TYPE_DECL)
589 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
590 and declaration modifiers but before the declaration proper. */
593 decl_attributes (node, attributes, prefix_attributes)
594 tree node, attributes, prefix_attributes;
596 tree decl = 0, type = 0;
600 if (attrtab_idx == 0)
606 type = TREE_TYPE (decl);
607 is_type = TREE_CODE (node) == TYPE_DECL;
609 else if (TYPE_P (node))
610 type = node, is_type = 1;
612 #ifdef PRAGMA_INSERT_ATTRIBUTES
613 /* If the code in c-pragma.c wants to insert some attributes then
614 allow it to do so. Do this before allowing machine back ends to
615 insert attributes, so that they have the opportunity to override
616 anything done here. */
617 PRAGMA_INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
620 #ifdef INSERT_ATTRIBUTES
621 INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
624 attributes = chainon (prefix_attributes, attributes);
626 for (a = attributes; a; a = TREE_CHAIN (a))
628 tree name = TREE_PURPOSE (a);
629 tree args = TREE_VALUE (a);
633 for (i = 0; i < attrtab_idx; i++)
634 if (attrtab[i].name == name)
637 if (i == attrtab_idx)
639 if (! valid_machine_attribute (name, args, decl, type)
640 && ! (* valid_lang_attribute) (name, args, decl, type))
641 warning ("`%s' attribute directive ignored",
642 IDENTIFIER_POINTER (name));
644 type = TREE_TYPE (decl);
647 else if (attrtab[i].decl_req && decl == 0)
649 warning ("`%s' attribute does not apply to types",
650 IDENTIFIER_POINTER (name));
653 else if (list_length (args) < attrtab[i].min
654 || list_length (args) > attrtab[i].max)
656 error ("wrong number of arguments specified for `%s' attribute",
657 IDENTIFIER_POINTER (name));
666 TYPE_PACKED (type) = 1;
667 else if (TREE_CODE (decl) == FIELD_DECL)
668 DECL_PACKED (decl) = 1;
669 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
670 used for DECL_REGISTER. It wouldn't mean anything anyway. */
672 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
676 if (TREE_CODE (decl) == VAR_DECL)
677 DECL_COMMON (decl) = 0;
679 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
683 if (TREE_CODE (decl) == VAR_DECL)
684 DECL_COMMON (decl) = 1;
686 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
690 if (TREE_CODE (decl) == FUNCTION_DECL)
691 TREE_THIS_VOLATILE (decl) = 1;
692 else if (TREE_CODE (type) == POINTER_TYPE
693 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
694 TREE_TYPE (decl) = type
696 (build_type_variant (TREE_TYPE (type),
697 TREE_READONLY (TREE_TYPE (type)), 1));
699 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
703 if (TREE_CODE (decl) == FUNCTION_DECL)
704 DECL_IS_MALLOC (decl) = 1;
705 /* ??? TODO: Support types. */
707 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
713 TREE_USED (decl) = 1;
715 TREE_USED (type) = 1;
716 else if (TREE_CODE (decl) == PARM_DECL
717 || TREE_CODE (decl) == VAR_DECL
718 || TREE_CODE (decl) == FUNCTION_DECL
719 || TREE_CODE (decl) == LABEL_DECL)
720 TREE_USED (decl) = 1;
722 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
726 if (TREE_CODE (decl) == FUNCTION_DECL)
727 TREE_READONLY (decl) = 1;
728 else if (TREE_CODE (type) == POINTER_TYPE
729 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
730 TREE_TYPE (decl) = type
732 (build_type_variant (TREE_TYPE (type), 1,
733 TREE_THIS_VOLATILE (TREE_TYPE (type))));
735 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
739 if (TREE_CODE (decl) == FUNCTION_DECL)
740 DECL_IS_PURE (decl) = 1;
741 /* ??? TODO: Support types. */
743 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
749 && TREE_CODE (type) == UNION_TYPE
751 || (TYPE_FIELDS (type) != 0
752 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))))
753 TYPE_TRANSPARENT_UNION (type) = 1;
754 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
755 && TREE_CODE (type) == UNION_TYPE
756 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
757 DECL_TRANSPARENT_UNION (decl) = 1;
759 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
763 if (TREE_CODE (decl) == FUNCTION_DECL
764 && TREE_CODE (type) == FUNCTION_TYPE
765 && decl_function_context (decl) == 0)
767 DECL_STATIC_CONSTRUCTOR (decl) = 1;
768 TREE_USED (decl) = 1;
771 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
775 if (TREE_CODE (decl) == FUNCTION_DECL
776 && TREE_CODE (type) == FUNCTION_TYPE
777 && decl_function_context (decl) == 0)
779 DECL_STATIC_DESTRUCTOR (decl) = 1;
780 TREE_USED (decl) = 1;
783 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
787 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
788 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
792 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
793 int len = strlen (p);
794 enum machine_mode mode = VOIDmode;
797 if (len > 4 && p[0] == '_' && p[1] == '_'
798 && p[len - 1] == '_' && p[len - 2] == '_')
800 char *newp = (char *) alloca (len - 1);
802 strcpy (newp, &p[2]);
803 newp[len - 4] = '\0';
807 /* Give this decl a type with the specified mode.
808 First check for the special modes. */
809 if (! strcmp (p, "byte"))
811 else if (!strcmp (p, "word"))
813 else if (! strcmp (p, "pointer"))
816 for (j = 0; j < NUM_MACHINE_MODES; j++)
817 if (!strcmp (p, GET_MODE_NAME (j)))
818 mode = (enum machine_mode) j;
820 if (mode == VOIDmode)
821 error ("unknown machine mode `%s'", p);
822 else if (0 == (typefm = type_for_mode (mode,
823 TREE_UNSIGNED (type))))
824 error ("no data type for mode `%s'", p);
827 if (TYPE_PRECISION (typefm) > (TREE_UNSIGNED (type)
828 ? TYPE_PRECISION(uintmax_type_node)
829 : TYPE_PRECISION(intmax_type_node))
831 pedwarn ("type with more precision than %s",
832 TREE_UNSIGNED (type) ? "uintmax_t" : "intmax_t");
833 TREE_TYPE (decl) = type = typefm;
834 DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = 0;
835 layout_decl (decl, 0);
841 #ifdef ASM_OUTPUT_SECTION_NAME
842 if ((TREE_CODE (decl) == FUNCTION_DECL
843 || TREE_CODE (decl) == VAR_DECL)
844 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
846 if (TREE_CODE (decl) == VAR_DECL
847 && current_function_decl != NULL_TREE
848 && ! TREE_STATIC (decl))
849 error_with_decl (decl,
850 "section attribute cannot be specified for local variables");
851 /* The decl may have already been given a section attribute from
852 a previous declaration. Ensure they match. */
853 else if (DECL_SECTION_NAME (decl) != NULL_TREE
854 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
855 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
856 error_with_decl (node,
857 "section of `%s' conflicts with previous declaration");
859 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
862 error_with_decl (node,
863 "section attribute not allowed for `%s'");
865 error_with_decl (node,
866 "section attributes are not supported for this target");
873 = (args ? TREE_VALUE (args)
874 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
877 /* Strip any NOPs of any kind. */
878 while (TREE_CODE (align_expr) == NOP_EXPR
879 || TREE_CODE (align_expr) == CONVERT_EXPR
880 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
881 align_expr = TREE_OPERAND (align_expr, 0);
883 if (TREE_CODE (align_expr) != INTEGER_CST)
885 error ("requested alignment is not a constant");
889 if ((i = tree_log2 (align_expr)) == -1)
890 error ("requested alignment is not a power of 2");
891 else if (i > HOST_BITS_PER_INT - 2)
892 error ("requested alignment is too large");
895 /* If we have a TYPE_DECL, then copy the type, so that we
896 don't accidentally modify a builtin type. See pushdecl. */
897 if (decl && TREE_TYPE (decl) != error_mark_node
898 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
900 tree tt = TREE_TYPE (decl);
901 DECL_ORIGINAL_TYPE (decl) = tt;
902 tt = build_type_copy (tt);
903 TYPE_NAME (tt) = decl;
904 TREE_USED (tt) = TREE_USED (decl);
905 TREE_TYPE (decl) = tt;
909 TYPE_ALIGN (type) = (1 << i) * BITS_PER_UNIT;
910 TYPE_USER_ALIGN (type) = 1;
912 else if (TREE_CODE (decl) != VAR_DECL
913 && TREE_CODE (decl) != FIELD_DECL)
914 error_with_decl (decl,
915 "alignment may not be specified for `%s'");
918 DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT;
919 DECL_USER_ALIGN (decl) = 1;
926 tree format_type_id = TREE_VALUE (args);
927 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
928 tree first_arg_num_expr
929 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
930 unsigned HOST_WIDE_INT format_num, first_arg_num;
931 enum format_type format_type;
933 unsigned int arg_num;
935 if (TREE_CODE (decl) != FUNCTION_DECL)
937 error_with_decl (decl,
938 "argument format specified for non-function `%s'");
942 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
944 error ("unrecognized format specifier");
949 const char *p = IDENTIFIER_POINTER (format_type_id);
951 if (!strcmp (p, "printf") || !strcmp (p, "__printf__"))
952 format_type = printf_format_type;
953 else if (!strcmp (p, "scanf") || !strcmp (p, "__scanf__"))
954 format_type = scanf_format_type;
955 else if (!strcmp (p, "strftime")
956 || !strcmp (p, "__strftime__"))
957 format_type = strftime_format_type;
960 warning ("`%s' is an unrecognized format function type", p);
965 /* Strip any conversions from the string index and first arg number
966 and verify they are constants. */
967 while (TREE_CODE (format_num_expr) == NOP_EXPR
968 || TREE_CODE (format_num_expr) == CONVERT_EXPR
969 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
970 format_num_expr = TREE_OPERAND (format_num_expr, 0);
972 while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
973 || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
974 || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
975 first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
977 if (TREE_CODE (format_num_expr) != INTEGER_CST
978 || TREE_INT_CST_HIGH (format_num_expr) != 0
979 || TREE_CODE (first_arg_num_expr) != INTEGER_CST
980 || TREE_INT_CST_HIGH (first_arg_num_expr) != 0)
982 error ("format string has invalid operand number");
986 format_num = TREE_INT_CST_LOW (format_num_expr);
987 first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
988 if (first_arg_num != 0 && first_arg_num <= format_num)
990 error ("format string arg follows the args to be formatted");
994 /* If a parameter list is specified, verify that the format_num
995 argument is actually a string, in case the format attribute
997 argument = TYPE_ARG_TYPES (type);
1000 for (arg_num = 1; argument != 0 && arg_num != format_num;
1001 ++arg_num, argument = TREE_CHAIN (argument))
1005 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
1006 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
1009 error ("format string arg not a string type");
1013 else if (first_arg_num != 0)
1015 /* Verify that first_arg_num points to the last arg,
1018 arg_num++, argument = TREE_CHAIN (argument);
1020 if (arg_num != first_arg_num)
1022 error ("args to be formatted is not '...'");
1028 if (format_type == strftime_format_type && first_arg_num != 0)
1030 error ("strftime formats cannot format arguments");
1034 record_function_format (DECL_NAME (decl),
1035 DECL_ASSEMBLER_NAME (decl),
1036 format_type, format_num, first_arg_num);
1042 tree format_num_expr = TREE_VALUE (args);
1043 unsigned HOST_WIDE_INT format_num;
1044 unsigned int arg_num;
1047 if (TREE_CODE (decl) != FUNCTION_DECL)
1049 error_with_decl (decl,
1050 "argument format specified for non-function `%s'");
1054 /* Strip any conversions from the first arg number and verify it
1056 while (TREE_CODE (format_num_expr) == NOP_EXPR
1057 || TREE_CODE (format_num_expr) == CONVERT_EXPR
1058 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
1059 format_num_expr = TREE_OPERAND (format_num_expr, 0);
1061 if (TREE_CODE (format_num_expr) != INTEGER_CST
1062 || TREE_INT_CST_HIGH (format_num_expr) != 0)
1064 error ("format string has invalid operand number");
1068 format_num = TREE_INT_CST_LOW (format_num_expr);
1070 /* If a parameter list is specified, verify that the format_num
1071 argument is actually a string, in case the format attribute
1073 argument = TYPE_ARG_TYPES (type);
1076 for (arg_num = 1; argument != 0 && arg_num != format_num;
1077 ++arg_num, argument = TREE_CHAIN (argument))
1081 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
1082 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
1085 error ("format string arg not a string type");
1090 if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) != POINTER_TYPE
1091 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl))))
1094 error ("function does not return string type");
1098 record_international_format (DECL_NAME (decl),
1099 DECL_ASSEMBLER_NAME (decl),
1105 declare_weak (decl);
1109 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
1110 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
1111 error_with_decl (decl,
1112 "`%s' defined both normally and as an alias");
1113 else if (decl_function_context (decl) == 0)
1117 id = TREE_VALUE (args);
1118 if (TREE_CODE (id) != STRING_CST)
1120 error ("alias arg not a string");
1123 id = get_identifier (TREE_STRING_POINTER (id));
1124 /* This counts as a use of the object pointed to. */
1127 if (TREE_CODE (decl) == FUNCTION_DECL)
1128 DECL_INITIAL (decl) = error_mark_node;
1130 DECL_EXTERNAL (decl) = 0;
1131 assemble_alias (decl, id);
1134 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1137 case A_NO_CHECK_MEMORY_USAGE:
1138 if (TREE_CODE (decl) != FUNCTION_DECL)
1140 error_with_decl (decl,
1141 "`%s' attribute applies only to functions",
1142 IDENTIFIER_POINTER (name));
1144 else if (DECL_INITIAL (decl))
1146 error_with_decl (decl,
1147 "can't set `%s' attribute after definition",
1148 IDENTIFIER_POINTER (name));
1151 DECL_NO_CHECK_MEMORY_USAGE (decl) = 1;
1154 case A_NO_INSTRUMENT_FUNCTION:
1155 if (TREE_CODE (decl) != FUNCTION_DECL)
1157 error_with_decl (decl,
1158 "`%s' attribute applies only to functions",
1159 IDENTIFIER_POINTER (name));
1161 else if (DECL_INITIAL (decl))
1163 error_with_decl (decl,
1164 "can't set `%s' attribute after definition",
1165 IDENTIFIER_POINTER (name));
1168 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1171 case A_NO_LIMIT_STACK:
1172 if (TREE_CODE (decl) != FUNCTION_DECL)
1174 error_with_decl (decl,
1175 "`%s' attribute applies only to functions",
1176 IDENTIFIER_POINTER (name));
1178 else if (DECL_INITIAL (decl))
1180 error_with_decl (decl,
1181 "can't set `%s' attribute after definition",
1182 IDENTIFIER_POINTER (name));
1185 DECL_NO_LIMIT_STACK (decl) = 1;
1191 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
1192 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
1194 The head of the declspec list is stored in DECLSPECS.
1195 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
1197 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
1198 the list elements. We drop the containing TREE_LIST nodes and link the
1199 resulting attributes together the way decl_attributes expects them. */
1202 split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
1204 tree *declspecs, *prefix_attributes;
1206 tree t, s, a, next, specs, attrs;
1208 /* This can happen after an __extension__ in pedantic mode. */
1209 if (specs_attrs != NULL_TREE
1210 && TREE_CODE (specs_attrs) == INTEGER_CST)
1212 *declspecs = NULL_TREE;
1213 *prefix_attributes = NULL_TREE;
1217 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
1218 if (specs_attrs != NULL_TREE
1219 && TREE_CODE (specs_attrs) != TREE_LIST)
1221 *declspecs = specs_attrs;
1222 *prefix_attributes = NULL_TREE;
1226 /* Remember to keep the lists in the same order, element-wise. */
1228 specs = s = NULL_TREE;
1229 attrs = a = NULL_TREE;
1230 for (t = specs_attrs; t; t = next)
1232 next = TREE_CHAIN (t);
1233 /* Declspecs have a non-NULL TREE_VALUE. */
1234 if (TREE_VALUE (t) != NULL_TREE)
1236 if (specs == NULL_TREE)
1246 if (attrs == NULL_TREE)
1247 attrs = a = TREE_PURPOSE (t);
1250 TREE_CHAIN (a) = TREE_PURPOSE (t);
1251 a = TREE_PURPOSE (t);
1253 /* More attrs can be linked here, move A to the end. */
1254 while (TREE_CHAIN (a) != NULL_TREE)
1259 /* Terminate the lists. */
1261 TREE_CHAIN (s) = NULL_TREE;
1263 TREE_CHAIN (a) = NULL_TREE;
1267 *prefix_attributes = attrs;
1270 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1271 This function is used by the parser when a rule will accept attributes
1272 in a particular position, but we don't want to support that just yet.
1274 A warning is issued for every ignored attribute. */
1277 strip_attrs (specs_attrs)
1282 split_specs_attrs (specs_attrs, &specs, &attrs);
1286 warning ("`%s' attribute ignored",
1287 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1288 attrs = TREE_CHAIN (attrs);
1294 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
1295 a parameter list. */
1297 /* The meaningfully distinct length modifiers for format checking recognised
1314 /* The standard versions in which various format features appeared. */
1315 enum format_std_version
1323 /* The C standard version C++ is treated as equivalent to
1324 or inheriting from, for the purpose of format features supported. */
1325 #define CPLUSPLUS_STD_VER STD_C89
1326 /* The C standard version we are checking formats against when pedantic. */
1327 #define C_STD_VER (c_language == clk_cplusplus \
1328 ? CPLUSPLUS_STD_VER \
1331 : (flag_isoc94 ? STD_C94 : STD_C89)))
1332 /* The name to give to the standard version we are warning about when
1333 pedantic. FEATURE_VER is the version in which the feature warned out
1334 appeared, which is higher than C_STD_VER. */
1335 #define C_STD_NAME(FEATURE_VER) (c_language == clk_cplusplus \
1337 : ((FEATURE_VER) == STD_EXT \
1341 /* Flags that may apply to a particular kind of format checked by GCC. */
1344 /* This format converts arguments of types determined by the
1346 FMT_FLAG_ARG_CONVERT = 1,
1347 /* The scanf allocation 'a' kludge applies to this format kind. */
1348 FMT_FLAG_SCANF_A_KLUDGE = 2,
1349 /* A % during parsing a specifier is allowed to be a modified % rather
1350 that indicating the format is broken and we are out-of-sync. */
1351 FMT_FLAG_FANCY_PERCENT_OK = 4,
1352 /* With $ operand numbers, it is OK to reference the same argument more
1354 FMT_FLAG_DOLLAR_MULTIPLE = 8
1355 /* Not included here: details of whether width or precision may occur
1356 (controlled by width_char and precision_char); details of whether
1357 '*' can be used for these (width_type and precision_type); details
1358 of whether length modifiers can occur (length_char_specs); details
1359 of when $ operand numbers are allowed (always, for the formats
1360 supported, if arguments are converted). */
1364 /* Structure describing a length modifier supported in format checking, and
1365 possibly a doubled version such as "hh". */
1368 /* Name of the single-character length modifier. */
1370 /* Index into a format_char_info.types array. */
1371 enum format_lengths index;
1372 /* Standard version this length appears in. */
1373 enum format_std_version std;
1374 /* Same, if the modifier can be repeated, or NULL if it can't. */
1375 const char *double_name;
1376 enum format_lengths double_index;
1377 enum format_std_version double_std;
1378 } format_length_info;
1381 /* Structure desribing the combination of a conversion specifier
1382 (or a set of specifiers which act identically) and a length modifier. */
1385 /* The standard version this combination of length and type appeared in.
1386 This is only relevant if greater than those for length and type
1387 individually; otherwise it is ignored. */
1388 enum format_std_version std;
1389 /* The name to use for the type, if different from that generated internally
1390 (e.g., "signed size_t"). */
1392 /* The type itself. */
1394 } format_type_detail;
1397 /* Macros to fill out tables of these. */
1398 #define BADLEN { 0, NULL, NULL }
1399 #define NOLENGTHS { BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }
1402 /* Structure desribing a format conversion specifier (or a set of specifiers
1403 which act identically), and the length modifiers used with it. */
1406 const char *format_chars;
1408 enum format_std_version std;
1409 /* Types accepted for each length modifier. */
1410 format_type_detail types[FMT_LEN_MAX];
1411 /* List of other modifier characters allowed with these specifiers.
1412 This lists flags, and additionally "w" for width, "p" for precision,
1413 "a" for scanf "a" allocation extension (not applicable in C99 mode),
1414 "*" for scanf suppression, and "E" and "O" for those strftime
1416 const char *flag_chars;
1417 /* List of additional flags describing these conversion specifiers.
1418 "c" for generic character pointers being allowed, "2" for strftime
1419 two digit year formats, "3" for strftime formats giving two digit
1420 years in some locales, "4" for "2" which becomes "3" with an "E" modifier,
1421 "o" if use of strftime "O" is a GNU extension beyond C99,
1422 "W" if the argument is a pointer which is dereferenced and written into,
1423 "R" if the argument is a pointer which is dereferenced and read from,
1424 "i" for printf integer formats where the '0' flag is ignored with
1425 precision, and "[" for the starting character of a scanf scanset. */
1430 /* Structure describing a flag accepted by some kind of format. */
1433 /* The flag character in question (0 for end of array). */
1435 /* Zero if this entry describes the flag character in general, or a
1436 non-zero character that may be found in flags2 if it describes the
1437 flag when used with certain formats only. If the latter, only
1438 the first such entry found that applies to the current conversion
1439 specifier is used; the values of `name' and `long_name' it supplies
1440 will be used, if non-NULL and the standard version is higher than
1441 the unpredicated one, for any pedantic warning. For example, 'o'
1442 for strftime formats (meaning 'O' is an extension over C99). */
1444 /* The name to use for this flag in diagnostic messages. For example,
1445 N_("`0' flag"), N_("field width"). */
1447 /* Long name for this flag in diagnostic messages; currently only used for
1448 "ISO C does not support ...". For example, N_("the `I' printf flag"). */
1449 const char *long_name;
1450 /* The standard version in which it appeared. */
1451 enum format_std_version std;
1455 /* Structure describing a combination of flags that is bad for some kind
1459 /* The first flag character in question (0 for end of array). */
1461 /* The second flag character. */
1463 /* Non-zero if the message should say that the first flag is ignored with
1464 the second, zero if the combination should simply be objected to. */
1466 /* Zero if this entry applies whenever this flag combination occurs,
1467 a non-zero character from flags2 if it only applies in some
1468 circumstances (e.g. 'i' for printf formats ignoring 0 with precision). */
1473 /* Structure describing a particular kind of format processed by GCC. */
1476 /* The name of this kind of format, for use in diagnostics. */
1478 /* Specifications of the length modifiers accepted; possibly NULL. */
1479 const format_length_info *length_char_specs;
1480 /* Details of the conversion specification characters accepted. */
1481 const format_char_info *conversion_specs;
1482 /* String listing the flag characters that are accepted. */
1483 const char *flag_chars;
1484 /* String listing modifier characters (strftime) accepted. May be NULL. */
1485 const char *modifier_chars;
1486 /* Details of the flag characters, including pseudo-flags. */
1487 const format_flag_spec *flag_specs;
1488 /* Details of bad combinations of flags. */
1489 const format_flag_pair *bad_flag_pairs;
1490 /* Flags applicable to this kind of format. */
1492 /* Flag character to treat a width as, or 0 if width not used. */
1494 /* Flag character to treat a precision as, or 0 if precision not used. */
1496 /* If a flag character has the effect of suppressing the conversion of
1497 an argument ('*' in scanf), that flag character, otherwise 0. */
1498 int suppression_char;
1499 /* Flag character to treat a length modifier as (ignored if length
1500 modifiers not used). Need not be placed in flag_chars for conversion
1501 specifiers, but is used to check for bad combinations such as length
1502 modifier with assignment suppression in scanf. */
1503 int length_code_char;
1504 /* Pointer to type of argument expected if '*' is used for a width,
1505 or NULL if '*' not used for widths. */
1507 /* Pointer to type of argument expected if '*' is used for a precision,
1508 or NULL if '*' not used for precisions. */
1509 tree *precision_type;
1513 /* Structure describing details of a type expected in format checking,
1514 and the type to check against it. */
1515 typedef struct format_wanted_type
1517 /* The type wanted. */
1519 /* The name of this type to use in diagnostics. */
1520 const char *wanted_type_name;
1521 /* The level of indirection through pointers at which this type occurs. */
1523 /* Whether, when pointer_count is 1, to allow any character type when
1524 pedantic, rather than just the character or void type specified. */
1525 int char_lenient_flag;
1526 /* Whether the argument, dereferenced once, is written into and so the
1527 argument must not be a pointer to a const-qualified type. */
1528 int writing_in_flag;
1529 /* Whether the argument, dereferenced once, is read from and so
1530 must not be a NULL pointer. */
1531 int reading_from_flag;
1532 /* If warnings should be of the form "field precision is not type int",
1533 the name to use (in this case "field precision"), otherwise NULL,
1534 for "%s format, %s arg" type messages. If (in an extension), this
1535 is a pointer type, wanted_type_name should be set to include the
1536 terminating '*' characters of the type name to give a correct
1539 /* The actual parameter to check against the wanted type. */
1541 /* The argument number of that parameter. */
1543 /* The next type to check for this format conversion, or NULL if none. */
1544 struct format_wanted_type *next;
1545 } format_wanted_type;
1548 static const format_length_info printf_length_specs[] =
1550 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
1551 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C99 },
1552 { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
1553 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
1554 { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
1555 { "Z", FMT_LEN_z, STD_EXT, NULL, 0, 0 },
1556 { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
1557 { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
1558 { NULL, 0, 0, NULL, 0, 0 }
1562 /* This differs from printf_length_specs only in that "Z" is not accepted. */
1563 static const format_length_info scanf_length_specs[] =
1565 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
1566 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C99 },
1567 { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
1568 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
1569 { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
1570 { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
1571 { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
1572 { NULL, 0, 0, NULL, 0, 0 }
1576 static const format_flag_spec printf_flag_specs[] =
1578 { ' ', 0, N_("` ' flag"), N_("the ` ' printf flag"), STD_C89 },
1579 { '+', 0, N_("`+' flag"), N_("the `+' printf flag"), STD_C89 },
1580 { '#', 0, N_("`#' flag"), N_("the `#' printf flag"), STD_C89 },
1581 { '0', 0, N_("`0' flag"), N_("the `0' printf flag"), STD_C89 },
1582 { '-', 0, N_("`-' flag"), N_("the `-' printf flag"), STD_C89 },
1583 { '\'', 0, N_("`'' flag"), N_("the `'' printf flag"), STD_EXT },
1584 { 'I', 0, N_("`I' flag"), N_("the `I' printf flag"), STD_EXT },
1585 { 'w', 0, N_("field width"), N_("field width in printf format"), STD_C89 },
1586 { 'p', 0, N_("precision"), N_("precision in printf format"), STD_C89 },
1587 { 'L', 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
1588 { 0, 0, NULL, NULL, 0 }
1592 static const format_flag_pair printf_flag_pairs[] =
1596 { '0', 'p', 1, 'i' },
1601 static const format_flag_spec scanf_flag_specs[] =
1603 { '*', 0, N_("assignment suppression"), N_("assignment suppression"), STD_C89 },
1604 { 'a', 0, N_("`a' flag"), N_("the `a' scanf flag"), STD_EXT },
1605 { 'w', 0, N_("field width"), N_("field width in scanf format"), STD_C89 },
1606 { 'L', 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89 },
1607 { '\'', 0, N_("`'' flag"), N_("the `'' scanf flag"), STD_EXT },
1608 { 'I', 0, N_("`I' flag"), N_("the `I' scanf flag"), STD_EXT },
1609 { 0, 0, NULL, NULL, 0 }
1613 static const format_flag_pair scanf_flag_pairs[] =
1620 static const format_flag_spec strftime_flag_specs[] =
1622 { '_', 0, N_("`_' flag"), N_("the `_' strftime flag"), STD_EXT },
1623 { '-', 0, N_("`-' flag"), N_("the `-' strftime flag"), STD_EXT },
1624 { '0', 0, N_("`0' flag"), N_("the `0' strftime flag"), STD_EXT },
1625 { '^', 0, N_("`^' flag"), N_("the `^' strftime flag"), STD_EXT },
1626 { '#', 0, N_("`#' flag"), N_("the `#' strftime flag"), STD_EXT },
1627 { 'w', 0, N_("field width"), N_("field width in strftime format"), STD_EXT },
1628 { 'E', 0, N_("`E' modifier"), N_("the `E' strftime modifier"), STD_C99 },
1629 { 'O', 0, N_("`O' modifier"), N_("the `O' strftime modifier"), STD_C99 },
1630 { 'O', 'o', NULL, N_("the `O' modifier"), STD_EXT },
1631 { 0, 0, NULL, NULL, 0 }
1635 static const format_flag_pair strftime_flag_pairs[] =
1646 #define T_I &integer_type_node
1647 #define T89_I { STD_C89, NULL, T_I }
1648 #define T99_I { STD_C99, NULL, T_I }
1649 #define T_L &long_integer_type_node
1650 #define T89_L { STD_C89, NULL, T_L }
1651 #define T_LL &long_long_integer_type_node
1652 #define T99_LL { STD_C99, NULL, T_LL }
1653 #define TEX_LL { STD_EXT, NULL, T_LL }
1654 #define T_S &short_integer_type_node
1655 #define T89_S { STD_C89, NULL, T_S }
1656 #define T_UI &unsigned_type_node
1657 #define T89_UI { STD_C89, NULL, T_UI }
1658 #define T99_UI { STD_C99, NULL, T_UI }
1659 #define T_UL &long_unsigned_type_node
1660 #define T89_UL { STD_C89, NULL, T_UL }
1661 #define T_ULL &long_long_unsigned_type_node
1662 #define T99_ULL { STD_C99, NULL, T_ULL }
1663 #define TEX_ULL { STD_EXT, NULL, T_ULL }
1664 #define T_US &short_unsigned_type_node
1665 #define T89_US { STD_C89, NULL, T_US }
1666 #define T_F &float_type_node
1667 #define T89_F { STD_C89, NULL, T_F }
1668 #define T99_F { STD_C99, NULL, T_F }
1669 #define T_D &double_type_node
1670 #define T89_D { STD_C89, NULL, T_D }
1671 #define T99_D { STD_C99, NULL, T_D }
1672 #define T_LD &long_double_type_node
1673 #define T89_LD { STD_C89, NULL, T_LD }
1674 #define T99_LD { STD_C99, NULL, T_LD }
1675 #define T_C &char_type_node
1676 #define T89_C { STD_C89, NULL, T_C }
1677 #define T_SC &signed_char_type_node
1678 #define T99_SC { STD_C99, NULL, T_SC }
1679 #define T_UC &unsigned_char_type_node
1680 #define T99_UC { STD_C99, NULL, T_UC }
1681 #define T_V &void_type_node
1682 #define T89_V { STD_C89, NULL, T_V }
1683 #define T_W &wchar_type_node
1684 #define T94_W { STD_C94, "wchar_t", T_W }
1685 #define TEX_W { STD_EXT, "wchar_t", T_W }
1686 #define T_WI &wint_type_node
1687 #define T94_WI { STD_C94, "wint_t", T_WI }
1688 #define TEX_WI { STD_EXT, "wint_t", T_WI }
1689 #define T_ST &c_size_type_node
1690 #define T99_ST { STD_C99, "size_t", T_ST }
1691 #define T_SST &signed_size_type_node
1692 #define T99_SST { STD_C99, "signed size_t", T_SST }
1693 #define T_PD &ptrdiff_type_node
1694 #define T99_PD { STD_C99, "ptrdiff_t", T_PD }
1695 #define T_UPD &unsigned_ptrdiff_type_node
1696 #define T99_UPD { STD_C99, "unsigned ptrdiff_t", T_UPD }
1697 #define T_IM &intmax_type_node
1698 #define T99_IM { STD_C99, "intmax_t", T_IM }
1699 #define T_UIM &uintmax_type_node
1700 #define T99_UIM { STD_C99, "uintmax_t", T_UIM }
1702 static const format_char_info print_char_table[] =
1704 /* C89 conversion specifiers. */
1705 { "di", 0, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T99_LL, TEX_LL, T99_SST, T99_PD, T99_IM }, "-wp0 +'I", "i" },
1706 { "oxX", 0, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T99_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "-wp0#", "i" },
1707 { "u", 0, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T99_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "-wp0'I", "i" },
1708 { "fgG", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#'", "" },
1709 { "eE", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "" },
1710 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, T94_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "" },
1711 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR" },
1712 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "c" },
1713 { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T99_LL, BADLEN, T99_SST, T99_PD, T99_IM }, "", "W" },
1714 /* C99 conversion specifiers. */
1715 { "F", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#'", "" },
1716 { "aA", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "" },
1717 /* X/Open conversion specifiers. */
1718 { "C", 0, STD_EXT, { TEX_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "" },
1719 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "R" },
1720 /* GNU conversion specifiers. */
1721 { "m", 0, STD_EXT, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "" },
1722 { NULL, 0, 0, NOLENGTHS, NULL, NULL }
1725 static const format_char_info scan_char_table[] =
1727 /* C89 conversion specifiers. */
1728 { "di", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T99_LL, TEX_LL, T99_SST, T99_PD, T99_IM }, "*w'I", "W" },
1729 { "u", 1, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T99_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "*w'I", "W" },
1730 { "oxX", 1, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T99_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "*w", "W" },
1731 { "efgEG", 1, STD_C89, { T89_F, BADLEN, BADLEN, T89_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "*w'", "W" },
1732 { "c", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "cW" },
1733 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "cW" },
1734 { "[", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "cW[" },
1735 { "p", 2, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W" },
1736 { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T99_LL, BADLEN, T99_SST, T99_PD, T99_IM }, "", "W" },
1737 /* C99 conversion specifiers. */
1738 { "FaA", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "*w'", "W" },
1739 /* X/Open conversion specifiers. */
1740 { "C", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W" },
1741 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "W" },
1742 { NULL, 0, 0, NOLENGTHS, NULL, NULL }
1745 static format_char_info time_char_table[] =
1747 /* C89 conversion specifiers. */
1748 { "ABZab", 0, STD_C89, NOLENGTHS, "^#", "" },
1749 { "cx", 0, STD_C89, NOLENGTHS, "E", "3" },
1750 { "HIMSUWdmw", 0, STD_C89, NOLENGTHS, "-_0Ow", "" },
1751 { "j", 0, STD_C89, NOLENGTHS, "-_0Ow", "o" },
1752 { "p", 0, STD_C89, NOLENGTHS, "#", "" },
1753 { "X", 0, STD_C89, NOLENGTHS, "E", "" },
1754 { "y", 0, STD_C89, NOLENGTHS, "EO-_0w", "4" },
1755 { "Y", 0, STD_C89, NOLENGTHS, "-_0EOw", "o" },
1756 { "%", 0, STD_C89, NOLENGTHS, "", "" },
1757 /* C99 conversion specifiers. */
1758 { "C", 0, STD_C99, NOLENGTHS, "-_0EOw", "o" },
1759 { "D", 0, STD_C99, NOLENGTHS, "", "2" },
1760 { "eVu", 0, STD_C99, NOLENGTHS, "-_0Ow", "" },
1761 { "FRTnrt", 0, STD_C99, NOLENGTHS, "", "" },
1762 { "g", 0, STD_C99, NOLENGTHS, "O-_0w", "2o" },
1763 { "G", 0, STD_C99, NOLENGTHS, "-_0Ow", "o" },
1764 { "h", 0, STD_C99, NOLENGTHS, "^#", "" },
1765 { "z", 0, STD_C99, NOLENGTHS, "O", "o" },
1766 /* GNU conversion specifiers. */
1767 { "kls", 0, STD_EXT, NOLENGTHS, "-_0Ow", "" },
1768 { "P", 0, STD_EXT, NOLENGTHS, "", "" },
1769 { NULL, 0, 0, NOLENGTHS, NULL, NULL }
1773 /* This must be in the same order as enum format_type. */
1774 static const format_kind_info format_types[] =
1776 { "printf", printf_length_specs, print_char_table, " +#0-'I", NULL,
1777 printf_flag_specs, printf_flag_pairs,
1778 FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE, 'w', 'p', 0, 'L',
1779 &integer_type_node, &integer_type_node
1781 { "scanf", scanf_length_specs, scan_char_table, "*'I", NULL,
1782 scanf_flag_specs, scanf_flag_pairs,
1783 FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE, 'w', 0, '*', 'L',
1786 { "strftime", NULL, time_char_table, "_-0^#", "EO",
1787 strftime_flag_specs, strftime_flag_pairs,
1788 FMT_FLAG_FANCY_PERCENT_OK, 'w', 0, 0, 0,
1794 typedef struct function_format_info
1796 struct function_format_info *next; /* next structure on the list */
1797 tree name; /* identifier such as "printf" */
1798 tree assembler_name; /* optional mangled identifier (for C++) */
1799 enum format_type format_type; /* type of format (printf, scanf, etc.) */
1800 int format_num; /* number of format argument */
1801 int first_arg_num; /* number of first arg (zero for varargs) */
1802 } function_format_info;
1804 static function_format_info *function_format_list = NULL;
1806 typedef struct international_format_info
1808 struct international_format_info *next; /* next structure on the list */
1809 tree name; /* identifier such as "gettext" */
1810 tree assembler_name; /* optional mangled identifier (for C++) */
1811 int format_num; /* number of format argument */
1812 } international_format_info;
1814 static international_format_info *international_format_list = NULL;
1816 /* Structure detailing the results of checking a format function call
1817 where the format expression may be a conditional expression with
1818 many leaves resulting from nested conditional expressions. */
1821 /* Number of leaves of the format argument that could not be checked
1822 as they were not string literals. */
1823 int number_non_literal;
1824 /* Number of leaves of the format argument that were null pointers or
1825 string literals, but had extra format arguments. */
1826 int number_extra_args;
1827 /* Number of leaves of the format argument that were null pointers or
1828 string literals, but had extra format arguments and used $ operand
1830 int number_dollar_extra_args;
1831 /* Number of leaves of the format argument that were wide string
1834 /* Number of leaves of the format argument that were empty strings. */
1836 /* Number of leaves of the format argument that were unterminated
1838 int number_unterminated;
1839 /* Number of leaves of the format argument that were not counted above. */
1841 } format_check_results;
1843 static void check_format_info PARAMS ((int *, function_format_info *, tree));
1844 static void check_format_info_recurse PARAMS ((int *, format_check_results *,
1845 function_format_info *, tree,
1847 static void check_format_info_main PARAMS ((int *, format_check_results *,
1848 function_format_info *,
1849 const char *, int, tree, int));
1850 static void status_warning PARAMS ((int *, const char *, ...))
1853 static void init_dollar_format_checking PARAMS ((int, tree));
1854 static int maybe_read_dollar_number PARAMS ((int *, const char **, int,
1856 const format_kind_info *));
1857 static void finish_dollar_format_checking PARAMS ((int *, format_check_results *));
1859 static const format_flag_spec *get_flag_spec PARAMS ((const format_flag_spec *,
1860 int, const char *));
1862 static void check_format_types PARAMS ((int *, format_wanted_type *));
1863 static int is_valid_printf_arglist PARAMS ((tree));
1864 static rtx c_expand_builtin PARAMS ((tree, rtx, enum machine_mode, enum expand_modifier));
1865 static rtx c_expand_builtin_printf PARAMS ((tree, rtx, enum machine_mode,
1866 enum expand_modifier, int));
1868 /* Initialize the table of functions to perform format checking on.
1869 The ISO C functions are always checked (whether <stdio.h> is
1870 included or not), since it is common to call printf without
1871 including <stdio.h>. There shouldn't be a problem with this,
1872 since ISO C reserves these function names whether you include the
1873 header file or not. In any case, the checking is harmless. With
1874 -ffreestanding, these default attributes are disabled, and must be
1875 specified manually if desired.
1877 Also initialize the name of function that modify the format string for
1878 internationalization purposes. */
1881 init_function_format_info ()
1885 /* Functions from ISO/IEC 9899:1990. */
1886 record_function_format (get_identifier ("printf"), NULL_TREE,
1887 printf_format_type, 1, 2);
1888 record_function_format (get_identifier ("__builtin_printf"), NULL_TREE,
1889 printf_format_type, 1, 2);
1890 record_function_format (get_identifier ("fprintf"), NULL_TREE,
1891 printf_format_type, 2, 3);
1892 record_function_format (get_identifier ("sprintf"), NULL_TREE,
1893 printf_format_type, 2, 3);
1894 record_function_format (get_identifier ("scanf"), NULL_TREE,
1895 scanf_format_type, 1, 2);
1896 record_function_format (get_identifier ("fscanf"), NULL_TREE,
1897 scanf_format_type, 2, 3);
1898 record_function_format (get_identifier ("sscanf"), NULL_TREE,
1899 scanf_format_type, 2, 3);
1900 record_function_format (get_identifier ("vprintf"), NULL_TREE,
1901 printf_format_type, 1, 0);
1902 record_function_format (get_identifier ("vfprintf"), NULL_TREE,
1903 printf_format_type, 2, 0);
1904 record_function_format (get_identifier ("vsprintf"), NULL_TREE,
1905 printf_format_type, 2, 0);
1906 record_function_format (get_identifier ("strftime"), NULL_TREE,
1907 strftime_format_type, 3, 0);
1910 if (flag_hosted && flag_isoc99)
1912 /* ISO C99 adds the snprintf and vscanf family functions. */
1913 record_function_format (get_identifier ("snprintf"), NULL_TREE,
1914 printf_format_type, 3, 4);
1915 record_function_format (get_identifier ("vsnprintf"), NULL_TREE,
1916 printf_format_type, 3, 0);
1917 record_function_format (get_identifier ("vscanf"), NULL_TREE,
1918 scanf_format_type, 1, 0);
1919 record_function_format (get_identifier ("vfscanf"), NULL_TREE,
1920 scanf_format_type, 2, 0);
1921 record_function_format (get_identifier ("vsscanf"), NULL_TREE,
1922 scanf_format_type, 2, 0);
1925 if (flag_hosted && flag_noniso_default_format_attributes)
1927 /* Uniforum/GNU gettext functions, not in ISO C. */
1928 record_international_format (get_identifier ("gettext"), NULL_TREE, 1);
1929 record_international_format (get_identifier ("dgettext"), NULL_TREE, 2);
1930 record_international_format (get_identifier ("dcgettext"), NULL_TREE, 2);
1934 /* Record information for argument format checking. FUNCTION_IDENT is
1935 the identifier node for the name of the function to check (its decl
1936 need not exist yet).
1937 FORMAT_TYPE specifies the type of format checking. FORMAT_NUM is the number
1938 of the argument which is the format control string (starting from 1).
1939 FIRST_ARG_NUM is the number of the first actual argument to check
1940 against the format string, or zero if no checking is not be done
1941 (e.g. for varargs such as vfprintf). */
1944 record_function_format (name, assembler_name, format_type,
1945 format_num, first_arg_num)
1947 tree assembler_name;
1948 enum format_type format_type;
1952 function_format_info *info;
1954 /* Re-use existing structure if it's there. */
1956 for (info = function_format_list; info; info = info->next)
1958 if (info->name == name && info->assembler_name == assembler_name)
1963 info = (function_format_info *) xmalloc (sizeof (function_format_info));
1964 info->next = function_format_list;
1965 function_format_list = info;
1968 info->assembler_name = assembler_name;
1971 info->format_type = format_type;
1972 info->format_num = format_num;
1973 info->first_arg_num = first_arg_num;
1976 /* Record information for the names of function that modify the format
1977 argument to format functions. FUNCTION_IDENT is the identifier node for
1978 the name of the function (its decl need not exist yet) and FORMAT_NUM is
1979 the number of the argument which is the format control string (starting
1983 record_international_format (name, assembler_name, format_num)
1985 tree assembler_name;
1988 international_format_info *info;
1990 /* Re-use existing structure if it's there. */
1992 for (info = international_format_list; info; info = info->next)
1994 if (info->name == name && info->assembler_name == assembler_name)
2001 = (international_format_info *)
2002 xmalloc (sizeof (international_format_info));
2003 info->next = international_format_list;
2004 international_format_list = info;
2007 info->assembler_name = assembler_name;
2010 info->format_num = format_num;
2013 /* Check the argument list of a call to printf, scanf, etc.
2014 NAME is the function identifier.
2015 ASSEMBLER_NAME is the function's assembler identifier.
2016 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
2017 PARAMS is the list of argument values. Also, if -Wmissing-format-attribute,
2018 warn for calls to vprintf or vscanf in functions with no such format
2019 attribute themselves. */
2022 check_function_format (status, name, assembler_name, params)
2025 tree assembler_name;
2028 function_format_info *info;
2030 /* See if this function is a format function. */
2031 for (info = function_format_list; info; info = info->next)
2033 if (info->assembler_name
2034 ? (info->assembler_name == assembler_name)
2035 : (info->name == name))
2037 /* Yup; check it. */
2038 check_format_info (status, info, params);
2039 if (warn_missing_format_attribute && info->first_arg_num == 0
2040 && (format_types[info->format_type].flags & FMT_FLAG_ARG_CONVERT))
2042 function_format_info *info2;
2043 for (info2 = function_format_list; info2; info2 = info2->next)
2044 if ((info2->assembler_name
2045 ? (info2->assembler_name == DECL_ASSEMBLER_NAME (current_function_decl))
2046 : (info2->name == DECL_NAME (current_function_decl)))
2047 && info2->format_type == info->format_type)
2050 warning ("function might be possible candidate for `%s' format attribute",
2051 format_types[info->format_type].name);
2058 /* This function replaces `warning' inside the printf format checking
2059 functions. If the `status' parameter is non-NULL, then it is
2060 dereferenced and set to 1 whenever a warning is caught. Otherwise
2061 it warns as usual by replicating the innards of the warning
2062 function from diagnostic.c. */
2064 status_warning VPARAMS ((int *status, const char *msgid, ...))
2066 #ifndef ANSI_PROTOTYPES
2071 diagnostic_context dc;
2073 VA_START (ap, msgid);
2075 #ifndef ANSI_PROTOTYPES
2076 status = va_arg (ap, int *);
2077 msgid = va_arg (ap, const char *);
2084 /* This duplicates the warning function behavior. */
2085 set_diagnostic_context
2086 (&dc, msgid, &ap, input_filename, lineno, /* warn = */ 1);
2087 report_diagnostic (&dc);
2093 /* Variables used by the checking of $ operand number formats. */
2094 static char *dollar_arguments_used = NULL;
2095 static int dollar_arguments_alloc = 0;
2096 static int dollar_arguments_count;
2097 static int dollar_first_arg_num;
2098 static int dollar_max_arg_used;
2099 static int dollar_format_warned;
2101 /* Initialize the checking for a format string that may contain $
2102 parameter number specifications; we will need to keep track of whether
2103 each parameter has been used. FIRST_ARG_NUM is the number of the first
2104 argument that is a parameter to the format, or 0 for a vprintf-style
2105 function; PARAMS is the list of arguments starting at this argument. */
2108 init_dollar_format_checking (first_arg_num, params)
2112 dollar_first_arg_num = first_arg_num;
2113 dollar_arguments_count = 0;
2114 dollar_max_arg_used = 0;
2115 dollar_format_warned = 0;
2116 if (first_arg_num > 0)
2120 dollar_arguments_count++;
2121 params = TREE_CHAIN (params);
2124 if (dollar_arguments_alloc < dollar_arguments_count)
2126 if (dollar_arguments_used)
2127 free (dollar_arguments_used);
2128 dollar_arguments_alloc = dollar_arguments_count;
2129 dollar_arguments_used = xmalloc (dollar_arguments_alloc);
2131 if (dollar_arguments_alloc)
2132 memset (dollar_arguments_used, 0, dollar_arguments_alloc);
2136 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
2137 is set, it is an error if one is not found; otherwise, it is OK. If
2138 such a number is found, check whether it is within range and mark that
2139 numbered operand as being used for later checking. Returns the operand
2140 number if found and within range, zero if no such number was found and
2141 this is OK, or -1 on error. PARAMS points to the first operand of the
2142 format; PARAM_PTR is made to point to the parameter referred to. If
2143 a $ format is found, *FORMAT is updated to point just after it. */
2146 maybe_read_dollar_number (status, format, dollar_needed, params, param_ptr,
2149 const char **format;
2153 const format_kind_info *fki;
2157 const char *fcp = *format;
2158 if (*fcp < '0' || *fcp > '9')
2162 status_warning (status, "missing $ operand number in format");
2170 while (*fcp >= '0' && *fcp <= '9')
2173 nargnum = 10 * argnum + (*fcp - '0');
2174 if (nargnum < 0 || nargnum / 10 != argnum)
2183 status_warning (status, "missing $ operand number in format");
2190 if (pedantic && !dollar_format_warned)
2192 status_warning (status,
2193 "%s does not support %%n$ operand number formats",
2194 C_STD_NAME (STD_EXT));
2195 dollar_format_warned = 1;
2197 if (overflow_flag || argnum == 0
2198 || (dollar_first_arg_num && argnum > dollar_arguments_count))
2200 status_warning (status, "operand number out of range in format");
2203 if (argnum > dollar_max_arg_used)
2204 dollar_max_arg_used = argnum;
2205 /* For vprintf-style functions we may need to allocate more memory to
2206 track which arguments are used. */
2207 while (dollar_arguments_alloc < dollar_max_arg_used)
2210 nalloc = 2 * dollar_arguments_alloc + 16;
2211 dollar_arguments_used = xrealloc (dollar_arguments_used, nalloc);
2212 memset (dollar_arguments_used + dollar_arguments_alloc, 0,
2213 nalloc - dollar_arguments_alloc);
2214 dollar_arguments_alloc = nalloc;
2216 if (!(fki->flags & FMT_FLAG_DOLLAR_MULTIPLE)
2217 && dollar_arguments_used[argnum - 1] == 1)
2219 dollar_arguments_used[argnum - 1] = 2;
2220 status_warning (status,
2221 "format argument %d used more than once in %s format",
2225 dollar_arguments_used[argnum - 1] = 1;
2226 if (dollar_first_arg_num)
2229 *param_ptr = params;
2230 for (i = 1; i < argnum && *param_ptr != 0; i++)
2231 *param_ptr = TREE_CHAIN (*param_ptr);
2233 if (*param_ptr == 0)
2235 /* This case shouldn't be caught here. */
2245 /* Finish the checking for a format string that used $ operand number formats
2246 instead of non-$ formats. We check for unused operands before used ones
2247 (a serious error, since the implementation of the format function
2248 can't know what types to pass to va_arg to find the later arguments).
2249 and for unused operands at the end of the format (if we know how many
2250 arguments the format had, so not for vprintf). If there were operand
2251 numbers out of range on a non-vprintf-style format, we won't have reached
2255 finish_dollar_format_checking (status, res)
2257 format_check_results *res;
2260 for (i = 0; i < dollar_max_arg_used; i++)
2262 if (!dollar_arguments_used[i])
2263 status_warning (status, "format argument %d unused before used argument %d in $-style format",
2264 i + 1, dollar_max_arg_used);
2266 if (dollar_first_arg_num && dollar_max_arg_used < dollar_arguments_count)
2268 res->number_other--;
2269 res->number_dollar_extra_args++;
2274 /* Retrieve the specification for a format flag. SPEC contains the
2275 specifications for format flags for the applicable kind of format.
2276 FLAG is the flag in question. If PREDICATES is NULL, the basic
2277 spec for that flag must be retrieved and this function aborts if
2278 it cannot be found. If PREDICATES is not NULL, it is a string listing
2279 possible predicates for the spec entry; if an entry predicated on any
2280 of these is found, it is returned, otherwise NULL is returned. */
2282 static const format_flag_spec *
2283 get_flag_spec (spec, flag, predicates)
2284 const format_flag_spec *spec;
2286 const char *predicates;
2289 for (i = 0; spec[i].flag_char != 0; i++)
2291 if (spec[i].flag_char != flag)
2293 if (predicates != NULL)
2295 if (spec[i].predicate != 0
2296 && strchr (predicates, spec[i].predicate) != 0)
2299 else if (spec[i].predicate == 0)
2302 if (predicates == NULL)
2309 /* Check the argument list of a call to printf, scanf, etc.
2310 INFO points to the function_format_info structure.
2311 PARAMS is the list of argument values. */
2314 check_format_info (status, info, params)
2316 function_format_info *info;
2321 format_check_results res;
2322 /* Skip to format argument. If the argument isn't available, there's
2323 no work for us to do; prototype checking will catch the problem. */
2324 for (arg_num = 1; ; ++arg_num)
2328 if (arg_num == info->format_num)
2330 params = TREE_CHAIN (params);
2332 format_tree = TREE_VALUE (params);
2333 params = TREE_CHAIN (params);
2334 if (format_tree == 0)
2337 res.number_non_literal = 0;
2338 res.number_extra_args = 0;
2339 res.number_dollar_extra_args = 0;
2340 res.number_wide = 0;
2341 res.number_empty = 0;
2342 res.number_unterminated = 0;
2343 res.number_other = 0;
2345 check_format_info_recurse (status, &res, info, format_tree, params, arg_num);
2347 if (res.number_non_literal > 0)
2349 /* Functions taking a va_list normally pass a non-literal format
2350 string. These functions typically are declared with
2351 first_arg_num == 0, so avoid warning in those cases. */
2352 if (!(format_types[info->format_type].flags & FMT_FLAG_ARG_CONVERT))
2354 /* For strftime-like formats, warn for not checking the format
2355 string; but there are no arguments to check. */
2356 if (warn_format_nonliteral)
2357 status_warning (status, "format not a string literal, format string not checked");
2359 else if (info->first_arg_num != 0)
2361 /* If there are no arguments for the format at all, we may have
2362 printf (foo) which is likely to be a security hole. */
2363 while (arg_num + 1 < info->first_arg_num)
2367 params = TREE_CHAIN (params);
2370 if (params == 0 && (warn_format_nonliteral || warn_format_security))
2371 status_warning (status, "format not a string literal and no format arguments");
2372 else if (warn_format_nonliteral)
2373 status_warning (status, "format not a string literal, argument types not checked");
2377 /* If there were extra arguments to the format, normally warn. However,
2378 the standard does say extra arguments are ignored, so in the specific
2379 case where we have multiple leaves (conditional expressions or
2380 ngettext) allow extra arguments if at least one leaf didn't have extra
2381 arguments, but was otherwise OK (either non-literal or checked OK).
2382 If the format is an empty string, this should be counted similarly to the
2383 case of extra format arguments. */
2384 if (res.number_extra_args > 0 && res.number_non_literal == 0
2385 && res.number_other == 0 && warn_format_extra_args)
2386 status_warning (status, "too many arguments for format");
2387 if (res.number_dollar_extra_args > 0 && res.number_non_literal == 0
2388 && res.number_other == 0 && warn_format_extra_args)
2389 status_warning (status, "unused arguments in $-style format");
2390 if (res.number_empty > 0 && res.number_non_literal == 0
2391 && res.number_other == 0)
2392 status_warning (status, "zero-length format string");
2394 if (res.number_wide > 0)
2395 status_warning (status, "format is a wide character string");
2397 if (res.number_unterminated > 0)
2398 status_warning (status, "unterminated format string");
2402 /* Recursively check a call to a format function. FORMAT_TREE is the
2403 format parameter, which may be a conditional expression in which
2404 both halves should be checked. ARG_NUM is the number of the
2405 format argument; PARAMS points just after it in the argument list. */
2408 check_format_info_recurse (status, res, info, format_tree, params, arg_num)
2410 format_check_results *res;
2411 function_format_info *info;
2417 const char *format_chars;
2418 tree array_size = 0;
2421 if (TREE_CODE (format_tree) == NOP_EXPR)
2423 /* Strip coercion. */
2424 check_format_info_recurse (status, res, info,
2425 TREE_OPERAND (format_tree, 0), params,
2430 if (TREE_CODE (format_tree) == CALL_EXPR
2431 && TREE_CODE (TREE_OPERAND (format_tree, 0)) == ADDR_EXPR
2432 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0))
2435 tree function = TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0);
2437 /* See if this is a call to a known internationalization function
2438 that modifies the format arg. */
2439 international_format_info *iinfo;
2441 for (iinfo = international_format_list; iinfo; iinfo = iinfo->next)
2442 if (iinfo->assembler_name
2443 ? (iinfo->assembler_name == DECL_ASSEMBLER_NAME (function))
2444 : (iinfo->name == DECL_NAME (function)))
2449 for (inner_args = TREE_OPERAND (format_tree, 1), i = 1;
2451 inner_args = TREE_CHAIN (inner_args), i++)
2452 if (i == iinfo->format_num)
2454 /* FIXME: with Marc Espie's __attribute__((nonnull))
2455 patch in GCC, we will have chained attributes,
2456 and be able to handle functions like ngettext
2457 with multiple format_arg attributes properly. */
2458 check_format_info_recurse (status, res, info,
2459 TREE_VALUE (inner_args), params,
2466 if (TREE_CODE (format_tree) == COND_EXPR)
2468 /* Check both halves of the conditional expression. */
2469 check_format_info_recurse (status, res, info,
2470 TREE_OPERAND (format_tree, 1), params,
2472 check_format_info_recurse (status, res, info,
2473 TREE_OPERAND (format_tree, 2), params,
2478 if (integer_zerop (format_tree))
2480 /* FIXME: this warning should go away once Marc Espie's
2481 __attribute__((nonnull)) patch is in. Instead, checking for
2482 nonnull attributes should probably change this function to act
2483 specially if info == NULL and add a res->number_null entry for
2484 that case, or maybe add a function pointer to be called at
2485 the end instead of hardcoding check_format_info_main. */
2486 status_warning (status, "null format string");
2488 /* Skip to first argument to check, so we can see if this format
2489 has any arguments (it shouldn't). */
2490 while (arg_num + 1 < info->first_arg_num)
2494 params = TREE_CHAIN (params);
2499 res->number_other++;
2501 res->number_extra_args++;
2506 if (TREE_CODE (format_tree) != ADDR_EXPR)
2508 res->number_non_literal++;
2511 format_tree = TREE_OPERAND (format_tree, 0);
2512 if (TREE_CODE (format_tree) == VAR_DECL
2513 && TREE_CODE (TREE_TYPE (format_tree)) == ARRAY_TYPE
2514 && (array_init = decl_constant_value (format_tree)) != format_tree
2515 && TREE_CODE (array_init) == STRING_CST)
2517 /* Extract the string constant initializer. Note that this may include
2518 a trailing NUL character that is not in the array (e.g.
2519 const char a[3] = "foo";). */
2520 array_size = DECL_SIZE_UNIT (format_tree);
2521 format_tree = array_init;
2523 if (TREE_CODE (format_tree) != STRING_CST)
2525 res->number_non_literal++;
2528 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree))) != char_type_node)
2533 format_chars = TREE_STRING_POINTER (format_tree);
2534 format_length = TREE_STRING_LENGTH (format_tree);
2535 if (array_size != 0)
2537 /* Variable length arrays can't be initialized. */
2538 if (TREE_CODE (array_size) != INTEGER_CST)
2540 if (host_integerp (array_size, 0))
2542 HOST_WIDE_INT array_size_value = TREE_INT_CST_LOW (array_size);
2543 if (array_size_value > 0
2544 && array_size_value == (int) array_size_value
2545 && format_length > array_size_value)
2546 format_length = array_size_value;
2549 if (format_length < 1)
2551 res->number_unterminated++;
2554 if (format_length == 1)
2556 res->number_empty++;
2559 if (format_chars[--format_length] != 0)
2561 res->number_unterminated++;
2565 /* Skip to first argument to check. */
2566 while (arg_num + 1 < info->first_arg_num)
2570 params = TREE_CHAIN (params);
2573 /* Provisionally increment res->number_other; check_format_info_main
2574 will decrement it if it finds there are extra arguments, but this way
2575 need not adjust it for every return. */
2576 res->number_other++;
2577 check_format_info_main (status, res, info, format_chars, format_length,
2582 /* Do the main part of checking a call to a format function. FORMAT_CHARS
2583 is the NUL-terminated format string (which at this point may contain
2584 internal NUL characters); FORMAT_LENGTH is its length (excluding the
2585 terminating NUL character). ARG_NUM is one less than the number of
2586 the first format argument to check; PARAMS points to that format
2587 argument in the list of arguments. */
2590 check_format_info_main (status, res, info, format_chars, format_length,
2593 format_check_results *res;
2594 function_format_info *info;
2595 const char *format_chars;
2600 const char *orig_format_chars = format_chars;
2601 tree first_fillin_param = params;
2603 const format_kind_info *fki = &format_types[info->format_type];
2604 const format_flag_spec *flag_specs = fki->flag_specs;
2605 const format_flag_pair *bad_flag_pairs = fki->bad_flag_pairs;
2607 /* -1 if no conversions taking an operand have been found; 0 if one has
2608 and it didn't use $; 1 if $ formats are in use. */
2609 int has_operand_number = -1;
2611 init_dollar_format_checking (info->first_arg_num, first_fillin_param);
2616 int suppressed = FALSE;
2617 const char *length_chars = NULL;
2618 enum format_lengths length_chars_val = FMT_LEN_none;
2619 enum format_std_version length_chars_std = STD_C89;
2623 int main_arg_num = 0;
2624 tree main_arg_params = 0;
2625 enum format_std_version wanted_type_std;
2626 const char *wanted_type_name;
2627 format_wanted_type width_wanted_type;
2628 format_wanted_type precision_wanted_type;
2629 format_wanted_type main_wanted_type;
2630 format_wanted_type *first_wanted_type = NULL;
2631 format_wanted_type *last_wanted_type = NULL;
2632 const format_length_info *fli = NULL;
2633 const format_char_info *fci = NULL;
2634 char flag_chars[256];
2636 if (*format_chars == 0)
2638 if (format_chars - orig_format_chars != format_length)
2639 status_warning (status, "embedded `\\0' in format");
2640 if (info->first_arg_num != 0 && params != 0
2641 && has_operand_number <= 0)
2643 res->number_other--;
2644 res->number_extra_args++;
2646 if (has_operand_number > 0)
2647 finish_dollar_format_checking (status, res);
2650 if (*format_chars++ != '%')
2652 if (*format_chars == 0)
2654 status_warning (status, "spurious trailing `%%' in format");
2657 if (*format_chars == '%')
2664 if ((fki->flags & FMT_FLAG_ARG_CONVERT) && has_operand_number != 0)
2666 /* Possibly read a $ operand number at the start of the format.
2667 If one was previously used, one is required here. If one
2668 is not used here, we can't immediately conclude this is a
2669 format without them, since it could be printf %m or scanf %*. */
2671 opnum = maybe_read_dollar_number (status, &format_chars, 0,
2673 &main_arg_params, fki);
2678 has_operand_number = 1;
2679 main_arg_num = opnum + info->first_arg_num - 1;
2683 /* Read any format flags, but do not yet validate them beyond removing
2684 duplicates, since in general validation depends on the rest of
2686 while (*format_chars != 0
2687 && strchr (fki->flag_chars, *format_chars) != 0)
2689 if (strchr (flag_chars, *format_chars) != 0)
2691 const format_flag_spec *s = get_flag_spec (flag_specs,
2692 *format_chars, NULL);
2693 status_warning (status, "repeated %s in format", _(s->name));
2697 i = strlen (flag_chars);
2698 flag_chars[i++] = *format_chars;
2704 /* Read any format width, possibly * or *m$. */
2705 if (fki->width_char != 0)
2707 if (fki->width_type != NULL && *format_chars == '*')
2709 i = strlen (flag_chars);
2710 flag_chars[i++] = fki->width_char;
2712 /* "...a field width...may be indicated by an asterisk.
2713 In this case, an int argument supplies the field width..." */
2717 status_warning (status, "too few arguments for format");
2720 if (has_operand_number != 0)
2723 opnum = maybe_read_dollar_number (status, &format_chars,
2724 has_operand_number == 1,
2731 has_operand_number = 1;
2732 arg_num = opnum + info->first_arg_num - 1;
2735 has_operand_number = 0;
2737 if (info->first_arg_num != 0)
2739 cur_param = TREE_VALUE (params);
2740 if (has_operand_number <= 0)
2742 params = TREE_CHAIN (params);
2745 width_wanted_type.wanted_type = *fki->width_type;
2746 width_wanted_type.wanted_type_name = NULL;
2747 width_wanted_type.pointer_count = 0;
2748 width_wanted_type.char_lenient_flag = 0;
2749 width_wanted_type.writing_in_flag = 0;
2750 width_wanted_type.reading_from_flag = 0;
2751 width_wanted_type.name = _("field width");
2752 width_wanted_type.param = cur_param;
2753 width_wanted_type.arg_num = arg_num;
2754 width_wanted_type.next = NULL;
2755 if (last_wanted_type != 0)
2756 last_wanted_type->next = &width_wanted_type;
2757 if (first_wanted_type == 0)
2758 first_wanted_type = &width_wanted_type;
2759 last_wanted_type = &width_wanted_type;
2764 /* Possibly read a numeric width. If the width is zero,
2765 we complain; for scanf this is bad according to the
2766 standard, and for printf and strftime it cannot occur
2767 because 0 is a flag. */
2768 int non_zero_width_char = FALSE;
2769 int found_width = FALSE;
2770 while (ISDIGIT (*format_chars))
2773 if (*format_chars != '0')
2774 non_zero_width_char = TRUE;
2777 if (found_width && !non_zero_width_char)
2778 status_warning (status, "zero width in %s format",
2782 i = strlen (flag_chars);
2783 flag_chars[i++] = fki->width_char;
2789 /* Read any format precision, possibly * or *m$. */
2790 if (fki->precision_char != 0 && *format_chars == '.')
2793 i = strlen (flag_chars);
2794 flag_chars[i++] = fki->precision_char;
2796 if (fki->precision_type != NULL && *format_chars == '*')
2798 /* "...a...precision...may be indicated by an asterisk.
2799 In this case, an int argument supplies the...precision." */
2801 if (has_operand_number != 0)
2804 opnum = maybe_read_dollar_number (status, &format_chars,
2805 has_operand_number == 1,
2812 has_operand_number = 1;
2813 arg_num = opnum + info->first_arg_num - 1;
2816 has_operand_number = 0;
2818 if (info->first_arg_num != 0)
2822 status_warning (status, "too few arguments for format");
2825 cur_param = TREE_VALUE (params);
2826 if (has_operand_number <= 0)
2828 params = TREE_CHAIN (params);
2831 precision_wanted_type.wanted_type = *fki->precision_type;
2832 precision_wanted_type.wanted_type_name = NULL;
2833 precision_wanted_type.pointer_count = 0;
2834 precision_wanted_type.char_lenient_flag = 0;
2835 precision_wanted_type.writing_in_flag = 0;
2836 precision_wanted_type.reading_from_flag = 0;
2837 precision_wanted_type.name = _("field precision");
2838 precision_wanted_type.param = cur_param;
2839 precision_wanted_type.arg_num = arg_num;
2840 precision_wanted_type.next = NULL;
2841 if (last_wanted_type != 0)
2842 last_wanted_type->next = &precision_wanted_type;
2843 if (first_wanted_type == 0)
2844 first_wanted_type = &precision_wanted_type;
2845 last_wanted_type = &precision_wanted_type;
2850 while (ISDIGIT (*format_chars))
2855 /* Read any length modifier, if this kind of format has them. */
2856 fli = fki->length_char_specs;
2857 length_chars = NULL;
2858 length_chars_val = FMT_LEN_none;
2859 length_chars_std = STD_C89;
2862 while (fli->name != 0 && fli->name[0] != *format_chars)
2867 if (fli->double_name != 0 && fli->name[0] == *format_chars)
2870 length_chars = fli->double_name;
2871 length_chars_val = fli->double_index;
2872 length_chars_std = fli->double_std;
2876 length_chars = fli->name;
2877 length_chars_val = fli->index;
2878 length_chars_std = fli->std;
2880 i = strlen (flag_chars);
2881 flag_chars[i++] = fki->length_code_char;
2886 /* Warn if the length modifier is non-standard. */
2887 if (length_chars_std > C_STD_VER)
2888 status_warning (status, "%s does not support the `%s' %s length modifier",
2889 C_STD_NAME (length_chars_std), length_chars,
2894 /* Read any modifier (strftime E/O). */
2895 if (fki->modifier_chars != NULL)
2897 while (*format_chars != 0
2898 && strchr (fki->modifier_chars, *format_chars) != 0)
2900 if (strchr (flag_chars, *format_chars) != 0)
2902 const format_flag_spec *s = get_flag_spec (flag_specs,
2903 *format_chars, NULL);
2904 status_warning (status, "repeated %s in format", _(s->name));
2908 i = strlen (flag_chars);
2909 flag_chars[i++] = *format_chars;
2916 /* Handle the scanf allocation kludge. */
2917 if (fki->flags & FMT_FLAG_SCANF_A_KLUDGE)
2919 if (*format_chars == 'a' && !flag_isoc99)
2921 if (format_chars[1] == 's' || format_chars[1] == 'S'
2922 || format_chars[1] == '[')
2924 /* `a' is used as a flag. */
2925 i = strlen (flag_chars);
2926 flag_chars[i++] = 'a';
2933 format_char = *format_chars;
2934 if (format_char == 0
2935 || (!(fki->flags & FMT_FLAG_FANCY_PERCENT_OK) && format_char == '%'))
2937 status_warning (status, "conversion lacks type at end of format");
2941 fci = fki->conversion_specs;
2942 while (fci->format_chars != 0
2943 && strchr (fci->format_chars, format_char) == 0)
2945 if (fci->format_chars == 0)
2947 if (ISGRAPH(format_char))
2948 status_warning (status, "unknown conversion type character `%c' in format",
2951 status_warning (status, "unknown conversion type character 0x%x in format",
2957 if (fci->std > C_STD_VER)
2958 status_warning (status, "%s does not support the `%%%c' %s format",
2959 C_STD_NAME (fci->std), format_char, fki->name);
2962 /* Validate the individual flags used, removing any that are invalid. */
2965 for (i = 0; flag_chars[i] != 0; i++)
2967 const format_flag_spec *s = get_flag_spec (flag_specs,
2968 flag_chars[i], NULL);
2969 flag_chars[i - d] = flag_chars[i];
2970 if (flag_chars[i] == fki->length_code_char)
2972 if (strchr (fci->flag_chars, flag_chars[i]) == 0)
2974 status_warning (status, "%s used with `%%%c' %s format",
2975 _(s->name), format_char, fki->name);
2981 const format_flag_spec *t;
2982 if (s->std > C_STD_VER)
2983 status_warning (status, "%s does not support %s",
2984 C_STD_NAME (s->std), _(s->long_name));
2985 t = get_flag_spec (flag_specs, flag_chars[i], fci->flags2);
2986 if (t != NULL && t->std > s->std)
2988 const char *long_name = (t->long_name != NULL
2991 if (t->std > C_STD_VER)
2992 status_warning (status, "%s does not support %s with the `%%%c' %s format",
2993 C_STD_NAME (t->std), _(long_name),
2994 format_char, fki->name);
2998 flag_chars[i - d] = 0;
3001 if ((fki->flags & FMT_FLAG_SCANF_A_KLUDGE)
3002 && strchr (flag_chars, 'a') != 0)
3005 if (fki->suppression_char
3006 && strchr (flag_chars, fki->suppression_char) != 0)
3009 /* Validate the pairs of flags used. */
3010 for (i = 0; bad_flag_pairs[i].flag_char1 != 0; i++)
3012 const format_flag_spec *s, *t;
3013 if (strchr (flag_chars, bad_flag_pairs[i].flag_char1) == 0)
3015 if (strchr (flag_chars, bad_flag_pairs[i].flag_char2) == 0)
3017 if (bad_flag_pairs[i].predicate != 0
3018 && strchr (fci->flags2, bad_flag_pairs[i].predicate) == 0)
3020 s = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char1, NULL);
3021 t = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char2, NULL);
3022 if (bad_flag_pairs[i].ignored)
3024 if (bad_flag_pairs[i].predicate != 0)
3025 status_warning (status, "%s ignored with %s and `%%%c' %s format",
3026 _(s->name), _(t->name), format_char,
3029 status_warning (status, "%s ignored with %s in %s format",
3030 _(s->name), _(t->name), fki->name);
3034 if (bad_flag_pairs[i].predicate != 0)
3035 status_warning (status, "use of %s and %s together with `%%%c' %s format",
3036 _(s->name), _(t->name), format_char,
3039 status_warning (status, "use of %s and %s together in %s format",
3040 _(s->name), _(t->name), fki->name);
3044 /* Give Y2K warnings. */
3045 if (warn_format_y2k)
3048 if (strchr (fci->flags2, '4') != 0)
3049 if (strchr (flag_chars, 'E') != 0)
3053 else if (strchr (fci->flags2, '3') != 0)
3055 else if (strchr (fci->flags2, '2') != 0)
3058 status_warning (status, "`%%%c' yields only last 2 digits of year in some locales",
3060 else if (y2k_level == 2)
3061 status_warning (status, "`%%%c' yields only last 2 digits of year", format_char);
3064 if (strchr (fci->flags2, '[') != 0)
3066 /* Skip over scan set, in case it happens to have '%' in it. */
3067 if (*format_chars == '^')
3069 /* Find closing bracket; if one is hit immediately, then
3070 it's part of the scan set rather than a terminator. */
3071 if (*format_chars == ']')
3073 while (*format_chars && *format_chars != ']')
3075 if (*format_chars != ']')
3076 /* The end of the format string was reached. */
3077 status_warning (status, "no closing `]' for `%%[' format");
3081 wanted_type_name = 0;
3082 if (fki->flags & FMT_FLAG_ARG_CONVERT)
3084 wanted_type = (fci->types[length_chars_val].type
3085 ? *fci->types[length_chars_val].type : 0);
3086 wanted_type_name = fci->types[length_chars_val].name;
3087 wanted_type_std = fci->types[length_chars_val].std;
3088 if (wanted_type == 0)
3090 status_warning (status, "use of `%s' length modifier with `%c' type character",
3091 length_chars, format_char);
3092 /* Heuristic: skip one argument when an invalid length/type
3093 combination is encountered. */
3097 status_warning (status, "too few arguments for format");
3100 params = TREE_CHAIN (params);
3104 /* Warn if non-standard, provided it is more non-standard
3105 than the length and type characters that may already
3106 have been warned for. */
3107 && wanted_type_std > length_chars_std
3108 && wanted_type_std > fci->std)
3110 if (wanted_type_std > C_STD_VER)
3111 status_warning (status, "%s does not support the `%%%s%c' %s format",
3112 C_STD_NAME (wanted_type_std), length_chars,
3113 format_char, fki->name);
3117 /* Finally. . .check type of argument against desired type! */
3118 if (info->first_arg_num == 0)
3120 if ((fci->pointer_count == 0 && wanted_type == void_type_node)
3123 if (main_arg_num != 0)
3126 status_warning (status, "operand number specified with suppressed assignment");
3128 status_warning (status, "operand number specified for format taking no argument");
3133 if (main_arg_num != 0)
3135 arg_num = main_arg_num;
3136 params = main_arg_params;
3141 if (has_operand_number > 0)
3143 status_warning (status, "missing $ operand number in format");
3147 has_operand_number = 0;
3150 status_warning (status, "too few arguments for format");
3154 cur_param = TREE_VALUE (params);
3155 params = TREE_CHAIN (params);
3156 main_wanted_type.wanted_type = wanted_type;
3157 main_wanted_type.wanted_type_name = wanted_type_name;
3158 main_wanted_type.pointer_count = fci->pointer_count + aflag;
3159 main_wanted_type.char_lenient_flag = 0;
3160 if (strchr (fci->flags2, 'c') != 0)
3161 main_wanted_type.char_lenient_flag = 1;
3162 main_wanted_type.writing_in_flag = 0;
3163 main_wanted_type.reading_from_flag = 0;
3165 main_wanted_type.writing_in_flag = 1;
3168 if (strchr (fci->flags2, 'W') != 0)
3169 main_wanted_type.writing_in_flag = 1;
3170 if (strchr (fci->flags2, 'R') != 0)
3171 main_wanted_type.reading_from_flag = 1;
3173 main_wanted_type.name = NULL;
3174 main_wanted_type.param = cur_param;
3175 main_wanted_type.arg_num = arg_num;
3176 main_wanted_type.next = NULL;
3177 if (last_wanted_type != 0)
3178 last_wanted_type->next = &main_wanted_type;
3179 if (first_wanted_type == 0)
3180 first_wanted_type = &main_wanted_type;
3181 last_wanted_type = &main_wanted_type;
3184 if (first_wanted_type != 0)
3185 check_format_types (status, first_wanted_type);
3191 /* Check the argument types from a single format conversion (possibly
3192 including width and precision arguments). */
3194 check_format_types (status, types)
3196 format_wanted_type *types;
3198 for (; types != 0; types = types->next)
3208 cur_param = types->param;
3209 cur_type = TREE_TYPE (cur_param);
3210 if (cur_type == error_mark_node)
3213 wanted_type = types->wanted_type;
3214 arg_num = types->arg_num;
3216 /* The following should not occur here. */
3217 if (wanted_type == 0)
3219 if (wanted_type == void_type_node && types->pointer_count == 0)
3222 if (types->pointer_count == 0)
3224 promoted_type = simple_type_promotes_to (wanted_type);
3225 if (promoted_type != NULL_TREE)
3226 wanted_type = promoted_type;
3229 STRIP_NOPS (cur_param);
3231 /* Check the types of any additional pointer arguments
3232 that precede the "real" argument. */
3233 for (i = 0; i < types->pointer_count; ++i)
3235 if (TREE_CODE (cur_type) == POINTER_TYPE)
3237 cur_type = TREE_TYPE (cur_type);
3238 if (cur_type == error_mark_node)
3241 /* Check for writing through a NULL pointer. */
3242 if (types->writing_in_flag
3245 && integer_zerop (cur_param))
3246 status_warning (status,
3247 "writing through null pointer (arg %d)",
3250 /* Check for reading through a NULL pointer. */
3251 if (types->reading_from_flag
3254 && integer_zerop (cur_param))
3255 status_warning (status,
3256 "reading through null pointer (arg %d)",
3259 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
3260 cur_param = TREE_OPERAND (cur_param, 0);
3264 /* See if this is an attempt to write into a const type with
3265 scanf or with printf "%n". Note: the writing in happens
3266 at the first indirection only, if for example
3267 void * const * is passed to scanf %p; passing
3268 const void ** is simply passing an incompatible type. */
3269 if (types->writing_in_flag
3271 && (TYPE_READONLY (cur_type)
3273 && (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'c'
3274 || (DECL_P (cur_param)
3275 && TREE_READONLY (cur_param))))))
3276 status_warning (status, "writing into constant object (arg %d)", arg_num);
3278 /* If there are extra type qualifiers beyond the first
3279 indirection, then this makes the types technically
3283 && (TYPE_READONLY (cur_type)
3284 || TYPE_VOLATILE (cur_type)
3285 || TYPE_RESTRICT (cur_type)))
3286 status_warning (status, "extra type qualifiers in format argument (arg %d)",
3292 if (types->pointer_count == 1)
3293 status_warning (status, "format argument is not a pointer (arg %d)", arg_num);
3295 status_warning (status, "format argument is not a pointer to a pointer (arg %d)", arg_num);
3300 if (i < types->pointer_count)
3303 orig_cur_type = cur_type;
3304 cur_type = TYPE_MAIN_VARIANT (cur_type);
3306 /* Check whether the argument type is a character type. This leniency
3307 only applies to certain formats, flagged with 'c'.
3309 if (types->char_lenient_flag)
3310 char_type_flag = (cur_type == char_type_node
3311 || cur_type == signed_char_type_node
3312 || cur_type == unsigned_char_type_node);
3314 /* Check the type of the "real" argument, if there's a type we want. */
3315 if (wanted_type == cur_type)
3317 /* If we want `void *', allow any pointer type.
3318 (Anything else would already have got a warning.)
3319 With -pedantic, only allow pointers to void and to character
3321 if (wanted_type == void_type_node
3322 && (!pedantic || (i == 1 && char_type_flag)))
3324 /* Don't warn about differences merely in signedness, unless
3325 -pedantic. With -pedantic, warn if the type is a pointer
3326 target and not a character type, and for character types at
3327 a second level of indirection. */
3328 if (TREE_CODE (wanted_type) == INTEGER_TYPE
3329 && TREE_CODE (cur_type) == INTEGER_TYPE
3330 && (! pedantic || i == 0 || (i == 1 && char_type_flag))
3331 && (TREE_UNSIGNED (wanted_type)
3332 ? wanted_type == unsigned_type (cur_type)
3333 : wanted_type == signed_type (cur_type)))
3335 /* Likewise, "signed char", "unsigned char" and "char" are
3336 equivalent but the above test won't consider them equivalent. */
3337 if (wanted_type == char_type_node
3338 && (! pedantic || i < 2)
3341 /* Now we have a type mismatch. */
3343 register const char *this;
3344 register const char *that;
3346 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
3348 if (TYPE_NAME (orig_cur_type) != 0
3349 && TREE_CODE (orig_cur_type) != INTEGER_TYPE
3350 && !(TREE_CODE (orig_cur_type) == POINTER_TYPE
3351 && TREE_CODE (TREE_TYPE (orig_cur_type)) == INTEGER_TYPE))
3353 if (TREE_CODE (TYPE_NAME (orig_cur_type)) == TYPE_DECL
3354 && DECL_NAME (TYPE_NAME (orig_cur_type)) != 0)
3355 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (orig_cur_type)));
3357 that = IDENTIFIER_POINTER (TYPE_NAME (orig_cur_type));
3360 /* A nameless type can't possibly match what the format wants.
3361 So there will be a warning for it.
3362 Make up a string to describe vaguely what it is. */
3365 if (TREE_CODE (orig_cur_type) == POINTER_TYPE)
3368 that = "different type";
3371 /* Make the warning better in case of mismatch of int vs long. */
3372 if (TREE_CODE (orig_cur_type) == INTEGER_TYPE
3373 && TREE_CODE (wanted_type) == INTEGER_TYPE
3374 && TYPE_PRECISION (orig_cur_type) == TYPE_PRECISION (wanted_type)
3375 && TYPE_NAME (orig_cur_type) != 0
3376 && TREE_CODE (TYPE_NAME (orig_cur_type)) == TYPE_DECL)
3377 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (orig_cur_type)));
3379 if (strcmp (this, that) != 0)
3381 /* There may be a better name for the format, e.g. size_t,
3382 but we should allow for programs with a perverse typedef
3383 making size_t something other than what the compiler
3385 if (types->wanted_type_name != 0
3386 && strcmp (types->wanted_type_name, that) != 0)
3387 this = types->wanted_type_name;
3388 if (types->name != 0)
3389 status_warning (status, "%s is not type %s (arg %d)", types->name, this,
3392 status_warning (status, "%s format, %s arg (arg %d)", this, that, arg_num);
3398 /* Set format warning options according to a -Wformat=n option. */
3401 set_Wformat (setting)
3404 warn_format = setting;
3405 warn_format_y2k = setting;
3406 warn_format_extra_args = setting;
3409 warn_format_nonliteral = setting;
3410 warn_format_security = setting;
3414 /* Print a warning if a constant expression had overflow in folding.
3415 Invoke this function on every expression that the language
3416 requires to be a constant expression.
3417 Note the ANSI C standard says it is erroneous for a
3418 constant expression to overflow. */
3421 constant_expression_warning (value)
3424 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
3425 || TREE_CODE (value) == COMPLEX_CST)
3426 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
3427 pedwarn ("overflow in constant expression");
3430 /* Print a warning if an expression had overflow in folding.
3431 Invoke this function on every expression that
3432 (1) appears in the source code, and
3433 (2) might be a constant expression that overflowed, and
3434 (3) is not already checked by convert_and_check;
3435 however, do not invoke this function on operands of explicit casts. */
3438 overflow_warning (value)
3441 if ((TREE_CODE (value) == INTEGER_CST
3442 || (TREE_CODE (value) == COMPLEX_CST
3443 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
3444 && TREE_OVERFLOW (value))
3446 TREE_OVERFLOW (value) = 0;
3447 if (skip_evaluation == 0)
3448 warning ("integer overflow in expression");
3450 else if ((TREE_CODE (value) == REAL_CST
3451 || (TREE_CODE (value) == COMPLEX_CST
3452 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
3453 && TREE_OVERFLOW (value))
3455 TREE_OVERFLOW (value) = 0;
3456 if (skip_evaluation == 0)
3457 warning ("floating point overflow in expression");
3461 /* Print a warning if a large constant is truncated to unsigned,
3462 or if -Wconversion is used and a constant < 0 is converted to unsigned.
3463 Invoke this function on every expression that might be implicitly
3464 converted to an unsigned type. */
3467 unsigned_conversion_warning (result, operand)
3468 tree result, operand;
3470 if (TREE_CODE (operand) == INTEGER_CST
3471 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
3472 && TREE_UNSIGNED (TREE_TYPE (result))
3473 && skip_evaluation == 0
3474 && !int_fits_type_p (operand, TREE_TYPE (result)))
3476 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
3477 /* This detects cases like converting -129 or 256 to unsigned char. */
3478 warning ("large integer implicitly truncated to unsigned type");
3479 else if (warn_conversion)
3480 warning ("negative integer implicitly converted to unsigned type");
3484 /* Convert EXPR to TYPE, warning about conversion problems with constants.
3485 Invoke this function on every expression that is converted implicitly,
3486 i.e. because of language rules and not because of an explicit cast. */
3489 convert_and_check (type, expr)
3492 tree t = convert (type, expr);
3493 if (TREE_CODE (t) == INTEGER_CST)
3495 if (TREE_OVERFLOW (t))
3497 TREE_OVERFLOW (t) = 0;
3499 /* Do not diagnose overflow in a constant expression merely
3500 because a conversion overflowed. */
3501 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
3503 /* No warning for converting 0x80000000 to int. */
3504 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
3505 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
3506 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
3507 /* If EXPR fits in the unsigned version of TYPE,
3508 don't warn unless pedantic. */
3510 || TREE_UNSIGNED (type)
3511 || ! int_fits_type_p (expr, unsigned_type (type)))
3512 && skip_evaluation == 0)
3513 warning ("overflow in implicit constant conversion");
3516 unsigned_conversion_warning (t, expr);
3521 /* A node in a list that describes references to variables (EXPR), which are
3522 either read accesses if WRITER is zero, or write accesses, in which case
3523 WRITER is the parent of EXPR. */
3530 /* Used to implement a cache the results of a call to verify_tree. We only
3531 use this for SAVE_EXPRs. */
3534 struct tlist_cache *next;
3535 struct tlist *cache_before_sp;
3536 struct tlist *cache_after_sp;
3540 /* Obstack to use when allocating tlist structures, and corresponding
3542 static struct obstack tlist_obstack;
3543 static char *tlist_firstobj = 0;
3545 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
3547 static struct tlist *warned_ids;
3548 /* SAVE_EXPRs need special treatment. We process them only once and then
3549 cache the results. */
3550 static struct tlist_cache *save_expr_cache;
3552 static void add_tlist PARAMS ((struct tlist **, struct tlist *, tree, int));
3553 static void merge_tlist PARAMS ((struct tlist **, struct tlist *, int));
3554 static void verify_tree PARAMS ((tree, struct tlist **, struct tlist **, tree));
3555 static int warning_candidate_p PARAMS ((tree));
3556 static void warn_for_collisions PARAMS ((struct tlist *));
3557 static void warn_for_collisions_1 PARAMS ((tree, tree, struct tlist *, int));
3558 static struct tlist *new_tlist PARAMS ((struct tlist *, tree, tree));
3559 static void verify_sequence_points PARAMS ((tree));
3561 /* Create a new struct tlist and fill in its fields. */
3562 static struct tlist *
3563 new_tlist (next, t, writer)
3569 l = (struct tlist *) obstack_alloc (&tlist_obstack, sizeof *l);
3576 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
3577 is nonnull, we ignore any node we find which has a writer equal to it. */
3580 add_tlist (to, add, exclude_writer, copy)
3583 tree exclude_writer;
3588 struct tlist *next = add->next;
3591 if (! exclude_writer || add->writer != exclude_writer)
3592 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
3597 /* Merge the nodes of ADD into TO. This merging process is done so that for
3598 each variable that already exists in TO, no new node is added; however if
3599 there is a write access recorded in ADD, and an occurrence on TO is only
3600 a read access, then the occurrence in TO will be modified to record the
3604 merge_tlist (to, add, copy)
3609 struct tlist **end = to;
3612 end = &(*end)->next;
3618 struct tlist *next = add->next;
3620 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
3621 if (tmp2->expr == add->expr)
3625 tmp2->writer = add->writer;
3629 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
3630 end = &(*end)->next;
3637 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
3638 references in list LIST conflict with it, excluding reads if ONLY writers
3642 warn_for_collisions_1 (written, writer, list, only_writes)
3643 tree written, writer;
3649 /* Avoid duplicate warnings. */
3650 for (tmp = warned_ids; tmp; tmp = tmp->next)
3651 if (tmp->expr == written)
3656 if (list->expr == written
3657 && list->writer != writer
3658 && (! only_writes || list->writer))
3660 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3661 warning ("operation on `%s' may be undefined",
3662 IDENTIFIER_POINTER (DECL_NAME (list->expr)));
3668 /* Given a list LIST of references to variables, find whether any of these
3669 can cause conflicts due to missing sequence points. */
3672 warn_for_collisions (list)
3677 for (tmp = list; tmp; tmp = tmp->next)
3680 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3684 /* Return nonzero if X is a tree that can be verified by the sequence poitn
3687 warning_candidate_p (x)
3690 return TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == PARM_DECL;
3693 /* Walk the tree X, and record accesses to variables. If X is written by the
3694 parent tree, WRITER is the parent.
3695 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3696 expression or its only operand forces a sequence point, then everything up
3697 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3699 Once we return, we will have emitted warnings if any subexpression before
3700 such a sequence point could be undefined. On a higher level, however, the
3701 sequence point may not be relevant, and we'll merge the two lists.
3703 Example: (b++, a) + b;
3704 The call that processes the COMPOUND_EXPR will store the increment of B
3705 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3706 processes the PLUS_EXPR will need to merge the two lists so that
3707 eventually, all accesses end up on the same list (and we'll warn about the
3708 unordered subexpressions b++ and b.
3710 A note on merging. If we modify the former example so that our expression
3713 care must be taken not simply to add all three expressions into the final
3714 PNO_SP list. The function merge_tlist takes care of that by merging the
3715 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3716 way, so that no more than one access to B is recorded. */
3719 verify_tree (x, pbefore_sp, pno_sp, writer)
3721 struct tlist **pbefore_sp, **pno_sp;
3724 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3725 enum tree_code code;
3729 code = TREE_CODE (x);
3730 class = TREE_CODE_CLASS (code);
3732 if (warning_candidate_p (x))
3734 *pno_sp = new_tlist (*pno_sp, x, writer);
3744 case TRUTH_ANDIF_EXPR:
3745 case TRUTH_ORIF_EXPR:
3746 tmp_before = tmp_nosp = tmp_list3 = 0;
3747 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3748 warn_for_collisions (tmp_nosp);
3749 merge_tlist (pbefore_sp, tmp_before, 0);
3750 merge_tlist (pbefore_sp, tmp_nosp, 0);
3751 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3752 merge_tlist (pbefore_sp, tmp_list3, 0);
3756 tmp_before = tmp_list2 = 0;
3757 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3758 warn_for_collisions (tmp_list2);
3759 merge_tlist (pbefore_sp, tmp_before, 0);
3760 merge_tlist (pbefore_sp, tmp_list2, 1);
3762 tmp_list3 = tmp_nosp = 0;
3763 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3764 warn_for_collisions (tmp_nosp);
3765 merge_tlist (pbefore_sp, tmp_list3, 0);
3767 tmp_list3 = tmp_list2 = 0;
3768 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3769 warn_for_collisions (tmp_list2);
3770 merge_tlist (pbefore_sp, tmp_list3, 0);
3771 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3772 two first, to avoid warning for (a ? b++ : b++). */
3773 merge_tlist (&tmp_nosp, tmp_list2, 0);
3774 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3777 case PREDECREMENT_EXPR:
3778 case PREINCREMENT_EXPR:
3779 case POSTDECREMENT_EXPR:
3780 case POSTINCREMENT_EXPR:
3781 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3785 tmp_before = tmp_nosp = tmp_list3 = 0;
3786 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3787 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3788 /* Expressions inside the LHS are not ordered wrt. the sequence points
3789 in the RHS. Example:
3791 Despite the fact that the modification of "a" is in the before_sp
3792 list (tmp_before), it conflicts with the use of "a" in the LHS.
3793 We can handle this by adding the contents of tmp_list3
3794 to those of tmp_before, and redoing the collision warnings for that
3796 add_tlist (&tmp_before, tmp_list3, x, 1);
3797 warn_for_collisions (tmp_before);
3798 /* Exclude the LHS itself here; we first have to merge it into the
3799 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3800 didn't exclude the LHS, we'd get it twice, once as a read and once
3802 add_tlist (pno_sp, tmp_list3, x, 0);
3803 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3805 merge_tlist (pbefore_sp, tmp_before, 0);
3806 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3807 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3808 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3812 /* We need to warn about conflicts among arguments and conflicts between
3813 args and the function address. Side effects of the function address,
3814 however, are not ordered by the sequence point of the call. */
3815 tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0;
3816 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3817 if (TREE_OPERAND (x, 1))
3818 verify_tree (TREE_OPERAND (x, 1), &tmp_list2, &tmp_list3, NULL_TREE);
3819 merge_tlist (&tmp_list3, tmp_list2, 0);
3820 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3821 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3822 warn_for_collisions (tmp_before);
3823 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3827 /* Scan all the list, e.g. indices of multi dimensional array. */
3830 tmp_before = tmp_nosp = 0;
3831 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3832 merge_tlist (&tmp_nosp, tmp_before, 0);
3833 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3840 struct tlist_cache *t;
3841 for (t = save_expr_cache; t; t = t->next)
3847 t = (struct tlist_cache *) obstack_alloc (&tlist_obstack,
3849 t->next = save_expr_cache;
3851 save_expr_cache = t;
3853 tmp_before = tmp_nosp = 0;
3854 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3855 warn_for_collisions (tmp_nosp);
3860 struct tlist *t = tmp_nosp;
3862 merge_tlist (&tmp_list3, t, 0);
3864 t->cache_before_sp = tmp_before;
3865 t->cache_after_sp = tmp_list3;
3867 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3868 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3877 if (first_rtl_op (code) == 0)
3879 x = TREE_OPERAND (x, 0);
3895 int max = first_rtl_op (TREE_CODE (x));
3896 for (lp = 0; lp < max; lp++)
3898 tmp_before = tmp_nosp = 0;
3899 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, NULL_TREE);
3900 merge_tlist (&tmp_nosp, tmp_before, 0);
3901 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3908 /* Try to warn for undefined behaviour in EXPR due to missing sequence
3912 verify_sequence_points (expr)
3915 struct tlist *before_sp = 0, *after_sp = 0;
3918 save_expr_cache = 0;
3919 if (tlist_firstobj == 0)
3921 gcc_obstack_init (&tlist_obstack);
3922 tlist_firstobj = obstack_alloc (&tlist_obstack, 0);
3925 verify_tree (expr, &before_sp, &after_sp, 0);
3926 warn_for_collisions (after_sp);
3927 obstack_free (&tlist_obstack, tlist_firstobj);
3931 c_expand_expr_stmt (expr)
3934 /* Do default conversion if safe and possibly important,
3935 in case within ({...}). */
3936 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
3937 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
3938 expr = default_conversion (expr);
3940 if (warn_sequence_point)
3941 verify_sequence_points (expr);
3943 if (TREE_TYPE (expr) != error_mark_node
3944 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
3945 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
3946 error ("expression statement has incomplete type");
3948 last_expr_type = TREE_TYPE (expr);
3949 add_stmt (build_stmt (EXPR_STMT, expr));
3952 /* Validate the expression after `case' and apply default promotions. */
3955 check_case_value (value)
3958 if (value == NULL_TREE)
3961 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3962 STRIP_TYPE_NOPS (value);
3963 /* In C++, the following is allowed:
3966 switch (...) { case i: ... }
3968 So, we try to reduce the VALUE to a constant that way. */
3969 if (c_language == clk_cplusplus)
3971 value = decl_constant_value (value);
3972 STRIP_TYPE_NOPS (value);
3973 value = fold (value);
3976 if (TREE_CODE (value) != INTEGER_CST
3977 && value != error_mark_node)
3979 error ("case label does not reduce to an integer constant");
3980 value = error_mark_node;
3983 /* Promote char or short to int. */
3984 value = default_conversion (value);
3986 constant_expression_warning (value);
3991 /* Return an integer type with BITS bits of precision,
3992 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3995 type_for_size (bits, unsignedp)
3999 if (bits == TYPE_PRECISION (integer_type_node))
4000 return unsignedp ? unsigned_type_node : integer_type_node;
4002 if (bits == TYPE_PRECISION (signed_char_type_node))
4003 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4005 if (bits == TYPE_PRECISION (short_integer_type_node))
4006 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4008 if (bits == TYPE_PRECISION (long_integer_type_node))
4009 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4011 if (bits == TYPE_PRECISION (long_long_integer_type_node))
4012 return (unsignedp ? long_long_unsigned_type_node
4013 : long_long_integer_type_node);
4015 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
4016 return (unsignedp ? widest_unsigned_literal_type_node
4017 : widest_integer_literal_type_node);
4019 if (bits <= TYPE_PRECISION (intQI_type_node))
4020 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4022 if (bits <= TYPE_PRECISION (intHI_type_node))
4023 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4025 if (bits <= TYPE_PRECISION (intSI_type_node))
4026 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4028 if (bits <= TYPE_PRECISION (intDI_type_node))
4029 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4034 /* Return a data type that has machine mode MODE.
4035 If the mode is an integer,
4036 then UNSIGNEDP selects between signed and unsigned types. */
4039 type_for_mode (mode, unsignedp)
4040 enum machine_mode mode;
4043 if (mode == TYPE_MODE (integer_type_node))
4044 return unsignedp ? unsigned_type_node : integer_type_node;
4046 if (mode == TYPE_MODE (signed_char_type_node))
4047 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4049 if (mode == TYPE_MODE (short_integer_type_node))
4050 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4052 if (mode == TYPE_MODE (long_integer_type_node))
4053 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4055 if (mode == TYPE_MODE (long_long_integer_type_node))
4056 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
4058 if (mode == TYPE_MODE (widest_integer_literal_type_node))
4059 return unsignedp ? widest_unsigned_literal_type_node
4060 : widest_integer_literal_type_node;
4062 if (mode == TYPE_MODE (intQI_type_node))
4063 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4065 if (mode == TYPE_MODE (intHI_type_node))
4066 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4068 if (mode == TYPE_MODE (intSI_type_node))
4069 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4071 if (mode == TYPE_MODE (intDI_type_node))
4072 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4074 #if HOST_BITS_PER_WIDE_INT >= 64
4075 if (mode == TYPE_MODE (intTI_type_node))
4076 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4079 if (mode == TYPE_MODE (float_type_node))
4080 return float_type_node;
4082 if (mode == TYPE_MODE (double_type_node))
4083 return double_type_node;
4085 if (mode == TYPE_MODE (long_double_type_node))
4086 return long_double_type_node;
4088 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
4089 return build_pointer_type (char_type_node);
4091 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
4092 return build_pointer_type (integer_type_node);
4094 #ifdef VECTOR_MODE_SUPPORTED_P
4095 if (mode == TYPE_MODE (V4SF_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
4096 return V4SF_type_node;
4097 if (mode == TYPE_MODE (V4SI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
4098 return V4SI_type_node;
4099 if (mode == TYPE_MODE (V2SI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
4100 return V2SI_type_node;
4101 if (mode == TYPE_MODE (V4HI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
4102 return V4HI_type_node;
4103 if (mode == TYPE_MODE (V8QI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
4104 return V8QI_type_node;
4110 /* Return an unsigned type the same as TYPE in other respects. */
4112 unsigned_type (type)
4115 tree type1 = TYPE_MAIN_VARIANT (type);
4116 if (type1 == signed_char_type_node || type1 == char_type_node)
4117 return unsigned_char_type_node;
4118 if (type1 == integer_type_node)
4119 return unsigned_type_node;
4120 if (type1 == short_integer_type_node)
4121 return short_unsigned_type_node;
4122 if (type1 == long_integer_type_node)
4123 return long_unsigned_type_node;
4124 if (type1 == long_long_integer_type_node)
4125 return long_long_unsigned_type_node;
4126 if (type1 == widest_integer_literal_type_node)
4127 return widest_unsigned_literal_type_node;
4128 #if HOST_BITS_PER_WIDE_INT >= 64
4129 if (type1 == intTI_type_node)
4130 return unsigned_intTI_type_node;
4132 if (type1 == intDI_type_node)
4133 return unsigned_intDI_type_node;
4134 if (type1 == intSI_type_node)
4135 return unsigned_intSI_type_node;
4136 if (type1 == intHI_type_node)
4137 return unsigned_intHI_type_node;
4138 if (type1 == intQI_type_node)
4139 return unsigned_intQI_type_node;
4141 return signed_or_unsigned_type (1, type);
4144 /* Return a signed type the same as TYPE in other respects. */
4150 tree type1 = TYPE_MAIN_VARIANT (type);
4151 if (type1 == unsigned_char_type_node || type1 == char_type_node)
4152 return signed_char_type_node;
4153 if (type1 == unsigned_type_node)
4154 return integer_type_node;
4155 if (type1 == short_unsigned_type_node)
4156 return short_integer_type_node;
4157 if (type1 == long_unsigned_type_node)
4158 return long_integer_type_node;
4159 if (type1 == long_long_unsigned_type_node)
4160 return long_long_integer_type_node;
4161 if (type1 == widest_unsigned_literal_type_node)
4162 return widest_integer_literal_type_node;
4163 #if HOST_BITS_PER_WIDE_INT >= 64
4164 if (type1 == unsigned_intTI_type_node)
4165 return intTI_type_node;
4167 if (type1 == unsigned_intDI_type_node)
4168 return intDI_type_node;
4169 if (type1 == unsigned_intSI_type_node)
4170 return intSI_type_node;
4171 if (type1 == unsigned_intHI_type_node)
4172 return intHI_type_node;
4173 if (type1 == unsigned_intQI_type_node)
4174 return intQI_type_node;
4176 return signed_or_unsigned_type (0, type);
4179 /* Return a type the same as TYPE except unsigned or
4180 signed according to UNSIGNEDP. */
4183 signed_or_unsigned_type (unsignedp, type)
4187 if (! INTEGRAL_TYPE_P (type)
4188 || TREE_UNSIGNED (type) == unsignedp)
4191 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
4192 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4193 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
4194 return unsignedp ? unsigned_type_node : integer_type_node;
4195 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
4196 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4197 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
4198 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4199 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
4200 return (unsignedp ? long_long_unsigned_type_node
4201 : long_long_integer_type_node);
4202 if (TYPE_PRECISION (type) == TYPE_PRECISION (widest_integer_literal_type_node))
4203 return (unsignedp ? widest_unsigned_literal_type_node
4204 : widest_integer_literal_type_node);
4208 /* Return the minimum number of bits needed to represent VALUE in a
4209 signed or unsigned type, UNSIGNEDP says which. */
4212 min_precision (value, unsignedp)
4218 /* If the value is negative, compute its negative minus 1. The latter
4219 adjustment is because the absolute value of the largest negative value
4220 is one larger than the largest positive value. This is equivalent to
4221 a bit-wise negation, so use that operation instead. */
4223 if (tree_int_cst_sgn (value) < 0)
4224 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
4226 /* Return the number of bits needed, taking into account the fact
4227 that we need one more bit for a signed than unsigned type. */
4229 if (integer_zerop (value))
4232 log = tree_floor_log2 (value);
4234 return log + 1 + ! unsignedp;
4237 /* Print an error message for invalid operands to arith operation CODE.
4238 NOP_EXPR is used as a special case (see truthvalue_conversion). */
4241 binary_op_error (code)
4242 enum tree_code code;
4244 register const char *opname;
4249 error ("invalid truth-value expression");
4253 opname = "+"; break;
4255 opname = "-"; break;
4257 opname = "*"; break;
4259 opname = "max"; break;
4261 opname = "min"; break;
4263 opname = "=="; break;
4265 opname = "!="; break;
4267 opname = "<="; break;
4269 opname = ">="; break;
4271 opname = "<"; break;
4273 opname = ">"; break;
4275 opname = "<<"; break;
4277 opname = ">>"; break;
4278 case TRUNC_MOD_EXPR:
4279 case FLOOR_MOD_EXPR:
4280 opname = "%"; break;
4281 case TRUNC_DIV_EXPR:
4282 case FLOOR_DIV_EXPR:
4283 opname = "/"; break;
4285 opname = "&"; break;
4287 opname = "|"; break;
4288 case TRUTH_ANDIF_EXPR:
4289 opname = "&&"; break;
4290 case TRUTH_ORIF_EXPR:
4291 opname = "||"; break;
4293 opname = "^"; break;
4296 opname = "rotate"; break;
4298 opname = "unknown"; break;
4300 error ("invalid operands to binary %s", opname);
4303 /* Subroutine of build_binary_op, used for comparison operations.
4304 See if the operands have both been converted from subword integer types
4305 and, if so, perhaps change them both back to their original type.
4306 This function is also responsible for converting the two operands
4307 to the proper common type for comparison.
4309 The arguments of this function are all pointers to local variables
4310 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4311 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4313 If this function returns nonzero, it means that the comparison has
4314 a constant value. What this function returns is an expression for
4318 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
4319 tree *op0_ptr, *op1_ptr;
4321 enum tree_code *rescode_ptr;
4324 tree op0 = *op0_ptr;
4325 tree op1 = *op1_ptr;
4326 int unsignedp0, unsignedp1;
4328 tree primop0, primop1;
4329 enum tree_code code = *rescode_ptr;
4331 /* Throw away any conversions to wider types
4332 already present in the operands. */
4334 primop0 = get_narrower (op0, &unsignedp0);
4335 primop1 = get_narrower (op1, &unsignedp1);
4337 /* Handle the case that OP0 does not *contain* a conversion
4338 but it *requires* conversion to FINAL_TYPE. */
4340 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4341 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
4342 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4343 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
4345 /* If one of the operands must be floated, we cannot optimize. */
4346 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4347 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4349 /* If first arg is constant, swap the args (changing operation
4350 so value is preserved), for canonicalization. Don't do this if
4351 the second arg is 0. */
4353 if (TREE_CONSTANT (primop0)
4354 && ! integer_zerop (primop1) && ! real_zerop (primop1))
4356 register tree tem = primop0;
4357 register int temi = unsignedp0;
4365 unsignedp0 = unsignedp1;
4388 *rescode_ptr = code;
4391 /* If comparing an integer against a constant more bits wide,
4392 maybe we can deduce a value of 1 or 0 independent of the data.
4393 Or else truncate the constant now
4394 rather than extend the variable at run time.
4396 This is only interesting if the constant is the wider arg.
4397 Also, it is not safe if the constant is unsigned and the
4398 variable arg is signed, since in this case the variable
4399 would be sign-extended and then regarded as unsigned.
4400 Our technique fails in this case because the lowest/highest
4401 possible unsigned results don't follow naturally from the
4402 lowest/highest possible values of the variable operand.
4403 For just EQ_EXPR and NE_EXPR there is another technique that
4404 could be used: see if the constant can be faithfully represented
4405 in the other operand's type, by truncating it and reextending it
4406 and see if that preserves the constant's value. */
4408 if (!real1 && !real2
4409 && TREE_CODE (primop1) == INTEGER_CST
4410 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4412 int min_gt, max_gt, min_lt, max_lt;
4413 tree maxval, minval;
4414 /* 1 if comparison is nominally unsigned. */
4415 int unsignedp = TREE_UNSIGNED (*restype_ptr);
4418 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
4420 /* If TYPE is an enumeration, then we need to get its min/max
4421 values from it's underlying integral type, not the enumerated
4423 if (TREE_CODE (type) == ENUMERAL_TYPE)
4424 type = type_for_size (TYPE_PRECISION (type), unsignedp0);
4426 maxval = TYPE_MAX_VALUE (type);
4427 minval = TYPE_MIN_VALUE (type);
4429 if (unsignedp && !unsignedp0)
4430 *restype_ptr = signed_type (*restype_ptr);
4432 if (TREE_TYPE (primop1) != *restype_ptr)
4433 primop1 = convert (*restype_ptr, primop1);
4434 if (type != *restype_ptr)
4436 minval = convert (*restype_ptr, minval);
4437 maxval = convert (*restype_ptr, maxval);
4440 if (unsignedp && unsignedp0)
4442 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
4443 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
4444 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
4445 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
4449 min_gt = INT_CST_LT (primop1, minval);
4450 max_gt = INT_CST_LT (primop1, maxval);
4451 min_lt = INT_CST_LT (minval, primop1);
4452 max_lt = INT_CST_LT (maxval, primop1);
4456 /* This used to be a switch, but Genix compiler can't handle that. */
4457 if (code == NE_EXPR)
4459 if (max_lt || min_gt)
4460 val = boolean_true_node;
4462 else if (code == EQ_EXPR)
4464 if (max_lt || min_gt)
4465 val = boolean_false_node;
4467 else if (code == LT_EXPR)
4470 val = boolean_true_node;
4472 val = boolean_false_node;
4474 else if (code == GT_EXPR)
4477 val = boolean_true_node;
4479 val = boolean_false_node;
4481 else if (code == LE_EXPR)
4484 val = boolean_true_node;
4486 val = boolean_false_node;
4488 else if (code == GE_EXPR)
4491 val = boolean_true_node;
4493 val = boolean_false_node;
4496 /* If primop0 was sign-extended and unsigned comparison specd,
4497 we did a signed comparison above using the signed type bounds.
4498 But the comparison we output must be unsigned.
4500 Also, for inequalities, VAL is no good; but if the signed
4501 comparison had *any* fixed result, it follows that the
4502 unsigned comparison just tests the sign in reverse
4503 (positive values are LE, negative ones GE).
4504 So we can generate an unsigned comparison
4505 against an extreme value of the signed type. */
4507 if (unsignedp && !unsignedp0)
4514 primop1 = TYPE_MIN_VALUE (type);
4520 primop1 = TYPE_MAX_VALUE (type);
4527 type = unsigned_type (type);
4530 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
4532 /* This is the case of (char)x >?< 0x80, which people used to use
4533 expecting old C compilers to change the 0x80 into -0x80. */
4534 if (val == boolean_false_node)
4535 warning ("comparison is always false due to limited range of data type");
4536 if (val == boolean_true_node)
4537 warning ("comparison is always true due to limited range of data type");
4540 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
4542 /* This is the case of (unsigned char)x >?< -1 or < 0. */
4543 if (val == boolean_false_node)
4544 warning ("comparison is always false due to limited range of data type");
4545 if (val == boolean_true_node)
4546 warning ("comparison is always true due to limited range of data type");
4551 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4552 if (TREE_SIDE_EFFECTS (primop0))
4553 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4557 /* Value is not predetermined, but do the comparison
4558 in the type of the operand that is not constant.
4559 TYPE is already properly set. */
4561 else if (real1 && real2
4562 && (TYPE_PRECISION (TREE_TYPE (primop0))
4563 == TYPE_PRECISION (TREE_TYPE (primop1))))
4564 type = TREE_TYPE (primop0);
4566 /* If args' natural types are both narrower than nominal type
4567 and both extend in the same manner, compare them
4568 in the type of the wider arg.
4569 Otherwise must actually extend both to the nominal
4570 common type lest different ways of extending
4572 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4574 else if (unsignedp0 == unsignedp1 && real1 == real2
4575 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4576 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4578 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4579 type = signed_or_unsigned_type (unsignedp0
4580 || TREE_UNSIGNED (*restype_ptr),
4582 /* Make sure shorter operand is extended the right way
4583 to match the longer operand. */
4584 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
4586 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
4591 /* Here we must do the comparison on the nominal type
4592 using the args exactly as we received them. */
4593 type = *restype_ptr;
4597 if (!real1 && !real2 && integer_zerop (primop1)
4598 && TREE_UNSIGNED (*restype_ptr))
4604 /* All unsigned values are >= 0, so we warn if extra warnings
4605 are requested. However, if OP0 is a constant that is
4606 >= 0, the signedness of the comparison isn't an issue,
4607 so suppress the warning. */
4608 if (extra_warnings && !in_system_header
4609 && ! (TREE_CODE (primop0) == INTEGER_CST
4610 && ! TREE_OVERFLOW (convert (signed_type (type),
4612 warning ("comparison of unsigned expression >= 0 is always true");
4613 value = boolean_true_node;
4617 if (extra_warnings && !in_system_header
4618 && ! (TREE_CODE (primop0) == INTEGER_CST
4619 && ! TREE_OVERFLOW (convert (signed_type (type),
4621 warning ("comparison of unsigned expression < 0 is always false");
4622 value = boolean_false_node;
4631 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4632 if (TREE_SIDE_EFFECTS (primop0))
4633 return build (COMPOUND_EXPR, TREE_TYPE (value),
4640 *op0_ptr = convert (type, primop0);
4641 *op1_ptr = convert (type, primop1);
4643 *restype_ptr = boolean_type_node;
4648 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4649 or validate its data type for an `if' or `while' statement or ?..: exp.
4651 This preparation consists of taking the ordinary
4652 representation of an expression expr and producing a valid tree
4653 boolean expression describing whether expr is nonzero. We could
4654 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
4655 but we optimize comparisons, &&, ||, and !.
4657 The resulting type should always be `boolean_type_node'. */
4660 truthvalue_conversion (expr)
4663 if (TREE_CODE (expr) == ERROR_MARK)
4666 #if 0 /* This appears to be wrong for C++. */
4667 /* These really should return error_mark_node after 2.4 is stable.
4668 But not all callers handle ERROR_MARK properly. */
4669 switch (TREE_CODE (TREE_TYPE (expr)))
4672 error ("struct type value used where scalar is required");
4673 return boolean_false_node;
4676 error ("union type value used where scalar is required");
4677 return boolean_false_node;
4680 error ("array type value used where scalar is required");
4681 return boolean_false_node;
4688 switch (TREE_CODE (expr))
4691 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4692 case TRUTH_ANDIF_EXPR:
4693 case TRUTH_ORIF_EXPR:
4694 case TRUTH_AND_EXPR:
4696 case TRUTH_XOR_EXPR:
4697 case TRUTH_NOT_EXPR:
4698 TREE_TYPE (expr) = boolean_type_node;
4705 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
4708 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
4711 /* If we are taking the address of a external decl, it might be zero
4712 if it is weak, so we cannot optimize. */
4713 if (DECL_P (TREE_OPERAND (expr, 0))
4714 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
4717 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
4718 return build (COMPOUND_EXPR, boolean_type_node,
4719 TREE_OPERAND (expr, 0), boolean_true_node);
4721 return boolean_true_node;
4724 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4725 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4726 truthvalue_conversion (TREE_OPERAND (expr, 0)),
4727 truthvalue_conversion (TREE_OPERAND (expr, 1)),
4734 /* These don't change whether an object is non-zero or zero. */
4735 return truthvalue_conversion (TREE_OPERAND (expr, 0));
4739 /* These don't change whether an object is zero or non-zero, but
4740 we can't ignore them if their second arg has side-effects. */
4741 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4742 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
4743 truthvalue_conversion (TREE_OPERAND (expr, 0)));
4745 return truthvalue_conversion (TREE_OPERAND (expr, 0));
4748 /* Distribute the conversion into the arms of a COND_EXPR. */
4749 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
4750 truthvalue_conversion (TREE_OPERAND (expr, 1)),
4751 truthvalue_conversion (TREE_OPERAND (expr, 2))));
4754 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4755 since that affects how `default_conversion' will behave. */
4756 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
4757 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
4759 /* fall through... */
4761 /* If this is widening the argument, we can ignore it. */
4762 if (TYPE_PRECISION (TREE_TYPE (expr))
4763 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
4764 return truthvalue_conversion (TREE_OPERAND (expr, 0));
4768 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
4770 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
4771 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
4773 /* fall through... */
4775 /* This and MINUS_EXPR can be changed into a comparison of the
4777 if (TREE_TYPE (TREE_OPERAND (expr, 0))
4778 == TREE_TYPE (TREE_OPERAND (expr, 1)))
4779 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
4780 TREE_OPERAND (expr, 1), 1);
4781 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
4782 fold (build1 (NOP_EXPR,
4783 TREE_TYPE (TREE_OPERAND (expr, 0)),
4784 TREE_OPERAND (expr, 1))), 1);
4787 if (integer_onep (TREE_OPERAND (expr, 1))
4788 && TREE_TYPE (expr) != boolean_type_node)
4789 /* Using convert here would cause infinite recursion. */
4790 return build1 (NOP_EXPR, boolean_type_node, expr);
4794 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
4795 warning ("suggest parentheses around assignment used as truth value");
4802 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4804 tree tem = save_expr (expr);
4805 return (build_binary_op
4806 ((TREE_SIDE_EFFECTS (expr)
4807 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4808 truthvalue_conversion (build_unary_op (REALPART_EXPR, tem, 0)),
4809 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, tem, 0)),
4813 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
4816 /* Make a variant type in the proper way for C/C++, propagating qualifiers
4817 down to the element type of an array. */
4820 c_build_qualified_type (type, type_quals)
4824 /* A restrict-qualified pointer type must be a pointer to object or
4825 incomplete type. Note that the use of POINTER_TYPE_P also allows
4826 REFERENCE_TYPEs, which is appropriate for C++. Unfortunately,
4827 the C++ front-end also use POINTER_TYPE for pointer-to-member
4828 values, so even though it should be illegal to use `restrict'
4829 with such an entity we don't flag that here. Thus, special case
4830 code for that case is required in the C++ front-end. */
4831 if ((type_quals & TYPE_QUAL_RESTRICT)
4832 && (!POINTER_TYPE_P (type)
4833 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
4835 error ("invalid use of `restrict'");
4836 type_quals &= ~TYPE_QUAL_RESTRICT;
4839 if (TREE_CODE (type) == ARRAY_TYPE)
4840 return build_array_type (c_build_qualified_type (TREE_TYPE (type),
4842 TYPE_DOMAIN (type));
4843 return build_qualified_type (type, type_quals);
4846 /* Apply the TYPE_QUALS to the new DECL. */
4849 c_apply_type_quals_to_decl (type_quals, decl)
4853 if ((type_quals & TYPE_QUAL_CONST)
4854 || (TREE_TYPE (decl)
4855 && TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE))
4856 TREE_READONLY (decl) = 1;
4857 if (type_quals & TYPE_QUAL_VOLATILE)
4859 TREE_SIDE_EFFECTS (decl) = 1;
4860 TREE_THIS_VOLATILE (decl) = 1;
4862 if (type_quals & TYPE_QUAL_RESTRICT)
4864 if (!TREE_TYPE (decl)
4865 || !POINTER_TYPE_P (TREE_TYPE (decl))
4866 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl))))
4867 error ("invalid use of `restrict'");
4868 else if (flag_strict_aliasing)
4870 /* No two restricted pointers can point at the same thing.
4871 However, a restricted pointer can point at the same thing
4872 as an unrestricted pointer, if that unrestricted pointer
4873 is based on the restricted pointer. So, we make the
4874 alias set for the restricted pointer a subset of the
4875 alias set for the type pointed to by the type of the
4878 HOST_WIDE_INT pointed_to_alias_set
4879 = get_alias_set (TREE_TYPE (TREE_TYPE (decl)));
4881 if (pointed_to_alias_set == 0)
4882 /* It's not legal to make a subset of alias set zero. */
4886 DECL_POINTER_ALIAS_SET (decl) = new_alias_set ();
4887 record_alias_subset (pointed_to_alias_set,
4888 DECL_POINTER_ALIAS_SET (decl));
4895 /* Return the typed-based alias set for T, which may be an expression
4896 or a type. Return -1 if we don't do anything special. */
4899 lang_get_alias_set (t)
4904 /* Permit type-punning when accessing a union, provided the access
4905 is directly through the union. For example, this code does not
4906 permit taking the address of a union member and then storing
4907 through it. Even the type-punning allowed here is a GCC
4908 extension, albeit a common and useful one; the C standard says
4909 that such accesses have implementation-defined behavior. */
4911 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4912 u = TREE_OPERAND (u, 0))
4913 if (TREE_CODE (u) == COMPONENT_REF
4914 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4917 /* If this is a char *, the ANSI C standard says it can alias
4918 anything. Note that all references need do this. */
4919 if (TREE_CODE_CLASS (TREE_CODE (t)) == 'r'
4920 && TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
4921 && TYPE_PRECISION (TREE_TYPE (t)) == TYPE_PRECISION (char_type_node))
4924 /* That's all the expressions we handle specially. */
4928 /* The C standard specifically allows aliasing between signed and
4929 unsigned variants of the same type. We treat the signed
4930 variant as canonical. */
4931 if (TREE_CODE (t) == INTEGER_TYPE && TREE_UNSIGNED (t))
4933 tree t1 = signed_type (t);
4935 /* t1 == t can happen for boolean nodes which are always unsigned. */
4937 return get_alias_set (t1);
4939 else if (POINTER_TYPE_P (t))
4943 /* Unfortunately, there is no canonical form of a pointer type.
4944 In particular, if we have `typedef int I', then `int *', and
4945 `I *' are different types. So, we have to pick a canonical
4946 representative. We do this below.
4948 Technically, this approach is actually more conservative that
4949 it needs to be. In particular, `const int *' and `int *'
4950 chould be in different alias sets, according to the C and C++
4951 standard, since their types are not the same, and so,
4952 technically, an `int **' and `const int **' cannot point at
4955 But, the standard is wrong. In particular, this code is
4960 const int* const* cipp = &ip;
4962 And, it doesn't make sense for that to be legal unless you
4963 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
4964 the pointed-to types. This issue has been reported to the
4966 t1 = build_type_no_quals (t);
4968 return get_alias_set (t1);
4970 /* It's not yet safe to use alias sets for classes in C++ because
4971 the TYPE_FIELDs list for a class doesn't mention base classes. */
4972 else if (c_language == clk_cplusplus && AGGREGATE_TYPE_P (t))
4978 /* Build tree nodes and builtin functions common to both C and C++ language
4982 c_common_nodes_and_builtins ()
4985 tree memcpy_ftype, memset_ftype, strlen_ftype;
4986 tree bzero_ftype, bcmp_ftype, puts_ftype, printf_ftype;
4987 tree endlink, int_endlink, double_endlink, unsigned_endlink;
4988 tree sizetype_endlink;
4989 tree ptr_ftype, ptr_ftype_unsigned;
4990 tree void_ftype_any, void_ftype_int, int_ftype_any, sizet_ftype_any;
4991 tree double_ftype_double, double_ftype_double_double;
4992 tree float_ftype_float, ldouble_ftype_ldouble;
4993 tree int_ftype_cptr_cptr_sizet, sizet_ftype_cstring_cstring;
4994 tree int_ftype_string_string, string_ftype_string_cstring;
4995 tree string_ftype_string_int, string_ftype_string_string;
4996 tree string_ftype_string_cstring_sizet, int_ftype_cstring_cstring_sizet;
4997 tree long_ftype_long;
4998 tree longlong_ftype_longlong;
4999 tree intmax_ftype_intmax;
5000 /* Either char* or void*. */
5001 tree traditional_ptr_type_node;
5002 /* Either const char* or const void*. */
5003 tree traditional_cptr_type_node;
5004 tree traditional_len_type_node;
5005 tree traditional_len_endlink;
5006 tree va_list_ref_type_node;
5007 tree va_list_arg_type_node;
5009 string_type_node = build_pointer_type (char_type_node);
5010 const_string_type_node
5011 = build_pointer_type (build_type_variant (char_type_node, 1, 0));
5014 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5017 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5019 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5021 default_function_type = build_function_type (integer_type_node, NULL_TREE);
5023 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5024 unsigned_ptrdiff_type_node = unsigned_type (ptrdiff_type_node);
5026 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_va_list"),
5027 va_list_type_node));
5029 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_ptrdiff_t"),
5030 ptrdiff_type_node));
5032 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_size_t"),
5035 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5037 va_list_arg_type_node = va_list_ref_type_node =
5038 build_pointer_type (TREE_TYPE (va_list_type_node));
5042 va_list_arg_type_node = va_list_type_node;
5043 va_list_ref_type_node = build_reference_type (va_list_type_node);
5046 endlink = void_list_node;
5047 int_endlink = tree_cons (NULL_TREE, integer_type_node, endlink);
5048 double_endlink = tree_cons (NULL_TREE, double_type_node, endlink);
5049 unsigned_endlink = tree_cons (NULL_TREE, unsigned_type_node, endlink);
5051 ptr_ftype = build_function_type (ptr_type_node, NULL_TREE);
5052 ptr_ftype_unsigned = build_function_type (ptr_type_node, unsigned_endlink);
5053 sizetype_endlink = tree_cons (NULL_TREE, TYPE_DOMAIN (sizetype), endlink);
5054 /* We realloc here because sizetype could be int or unsigned. S'ok. */
5055 ptr_ftype_sizetype = build_function_type (ptr_type_node, sizetype_endlink);
5057 sizet_ftype_any = build_function_type (sizetype, NULL_TREE);
5058 int_ftype_any = build_function_type (integer_type_node, NULL_TREE);
5059 void_ftype_any = build_function_type (void_type_node, NULL_TREE);
5060 void_ftype = build_function_type (void_type_node, endlink);
5061 void_ftype_int = build_function_type (void_type_node, int_endlink);
5063 = build_function_type (void_type_node,
5064 tree_cons (NULL_TREE, ptr_type_node, endlink));
5067 = build_function_type (float_type_node,
5068 tree_cons (NULL_TREE, float_type_node, endlink));
5071 = build_function_type (double_type_node, double_endlink);
5073 ldouble_ftype_ldouble
5074 = build_function_type (long_double_type_node,
5075 tree_cons (NULL_TREE, long_double_type_node,
5078 double_ftype_double_double
5079 = build_function_type (double_type_node,
5080 tree_cons (NULL_TREE, double_type_node,
5084 = build_function_type (integer_type_node, int_endlink);
5087 = build_function_type (long_integer_type_node,
5088 tree_cons (NULL_TREE, long_integer_type_node,
5091 longlong_ftype_longlong
5092 = build_function_type (long_long_integer_type_node,
5093 tree_cons (NULL_TREE, long_long_integer_type_node,
5097 = build_function_type (intmax_type_node,
5098 tree_cons (NULL_TREE, intmax_type_node,
5101 int_ftype_cptr_cptr_sizet
5102 = build_function_type (integer_type_node,
5103 tree_cons (NULL_TREE, const_ptr_type_node,
5104 tree_cons (NULL_TREE, const_ptr_type_node,
5105 tree_cons (NULL_TREE,
5109 void_zero_node = build_int_2 (0, 0);
5110 TREE_TYPE (void_zero_node) = void_type_node;
5112 /* Prototype for strcpy/strcat. */
5113 string_ftype_string_cstring
5114 = build_function_type (string_type_node,
5115 tree_cons (NULL_TREE, string_type_node,
5116 tree_cons (NULL_TREE,
5117 const_string_type_node,
5120 /* Prototype for strncpy/strncat. */
5121 string_ftype_string_cstring_sizet
5122 = build_function_type (string_type_node,
5123 tree_cons (NULL_TREE, string_type_node,
5124 tree_cons (NULL_TREE,
5125 const_string_type_node,
5126 sizetype_endlink)));
5128 traditional_len_type_node = ((flag_traditional &&
5129 c_language != clk_cplusplus)
5130 ? integer_type_node : sizetype);
5131 traditional_len_endlink = tree_cons (NULL_TREE, traditional_len_type_node,
5134 /* Prototype for strcmp. */
5135 int_ftype_string_string
5136 = build_function_type (integer_type_node,
5137 tree_cons (NULL_TREE, const_string_type_node,
5138 tree_cons (NULL_TREE,
5139 const_string_type_node,
5142 /* Prototype for strspn/strcspn. */
5143 sizet_ftype_cstring_cstring
5144 = build_function_type (c_size_type_node,
5145 tree_cons (NULL_TREE, const_string_type_node,
5146 tree_cons (NULL_TREE,
5147 const_string_type_node,
5150 /* Prototype for strncmp. */
5151 int_ftype_cstring_cstring_sizet
5152 = build_function_type (integer_type_node,
5153 tree_cons (NULL_TREE, const_string_type_node,
5154 tree_cons (NULL_TREE,
5155 const_string_type_node,
5156 sizetype_endlink)));
5158 /* Prototype for strstr, strpbrk, etc. */
5159 string_ftype_string_string
5160 = build_function_type (string_type_node,
5161 tree_cons (NULL_TREE, const_string_type_node,
5162 tree_cons (NULL_TREE,
5163 const_string_type_node,
5166 /* Prototype for strchr. */
5167 string_ftype_string_int
5168 = build_function_type (string_type_node,
5169 tree_cons (NULL_TREE, const_string_type_node,
5170 tree_cons (NULL_TREE,
5174 /* Prototype for strlen. */
5176 = build_function_type (traditional_len_type_node,
5177 tree_cons (NULL_TREE, const_string_type_node,
5180 traditional_ptr_type_node = ((flag_traditional &&
5181 c_language != clk_cplusplus)
5182 ? string_type_node : ptr_type_node);
5183 traditional_cptr_type_node = ((flag_traditional &&
5184 c_language != clk_cplusplus)
5185 ? const_string_type_node : const_ptr_type_node);
5187 /* Prototype for memcpy. */
5189 = build_function_type (traditional_ptr_type_node,
5190 tree_cons (NULL_TREE, ptr_type_node,
5191 tree_cons (NULL_TREE, const_ptr_type_node,
5192 sizetype_endlink)));
5194 /* Prototype for memset. */
5196 = build_function_type (traditional_ptr_type_node,
5197 tree_cons (NULL_TREE, ptr_type_node,
5198 tree_cons (NULL_TREE, integer_type_node,
5199 tree_cons (NULL_TREE,
5203 /* Prototype for bzero. */
5205 = build_function_type (void_type_node,
5206 tree_cons (NULL_TREE, traditional_ptr_type_node,
5207 traditional_len_endlink));
5209 /* Prototype for bcmp. */
5211 = build_function_type (integer_type_node,
5212 tree_cons (NULL_TREE, traditional_cptr_type_node,
5213 tree_cons (NULL_TREE,
5214 traditional_cptr_type_node,
5215 traditional_len_endlink)));
5217 /* Prototype for puts. */
5219 = build_function_type (integer_type_node,
5220 tree_cons (NULL_TREE, const_string_type_node,
5223 /* Prototype for printf. */
5225 = build_function_type (integer_type_node,
5226 tree_cons (NULL_TREE, const_string_type_node,
5229 builtin_function ("__builtin_constant_p", default_function_type,
5230 BUILT_IN_CONSTANT_P, BUILT_IN_NORMAL, NULL_PTR);
5232 builtin_function ("__builtin_return_address", ptr_ftype_unsigned,
5233 BUILT_IN_RETURN_ADDRESS, BUILT_IN_NORMAL, NULL_PTR);
5235 builtin_function ("__builtin_frame_address", ptr_ftype_unsigned,
5236 BUILT_IN_FRAME_ADDRESS, BUILT_IN_NORMAL, NULL_PTR);
5238 builtin_function ("__builtin_alloca", ptr_ftype_sizetype,
5239 BUILT_IN_ALLOCA, BUILT_IN_NORMAL, "alloca");
5240 builtin_function ("__builtin_ffs", int_ftype_int, BUILT_IN_FFS,
5241 BUILT_IN_NORMAL, NULL_PTR);
5242 /* Define alloca, ffs as builtins.
5243 Declare _exit just to mark it as volatile. */
5244 if (! flag_no_builtin && ! flag_no_nonansi_builtin)
5247 temp = builtin_function ("alloca", ptr_ftype_sizetype,
5248 BUILT_IN_ALLOCA, BUILT_IN_NORMAL, NULL_PTR);
5249 /* Suppress error if redefined as a non-function. */
5250 DECL_BUILT_IN_NONANSI (temp) = 1;
5252 temp = builtin_function ("ffs", int_ftype_int, BUILT_IN_FFS,
5253 BUILT_IN_NORMAL, NULL_PTR);
5254 /* Suppress error if redefined as a non-function. */
5255 DECL_BUILT_IN_NONANSI (temp) = 1;
5256 temp = builtin_function ("_exit", void_ftype_int,
5257 0, NOT_BUILT_IN, NULL_PTR);
5258 TREE_THIS_VOLATILE (temp) = 1;
5259 TREE_SIDE_EFFECTS (temp) = 1;
5260 /* Suppress error if redefined as a non-function. */
5261 DECL_BUILT_IN_NONANSI (temp) = 1;
5263 temp = builtin_function ("index", string_ftype_string_int,
5264 BUILT_IN_INDEX, BUILT_IN_NORMAL, NULL_PTR);
5265 DECL_BUILT_IN_NONANSI (temp) = 1;
5266 temp = builtin_function ("rindex", string_ftype_string_int,
5267 BUILT_IN_RINDEX, BUILT_IN_NORMAL, NULL_PTR);
5268 DECL_BUILT_IN_NONANSI (temp) = 1;
5269 /* The system prototypes for these functions have many
5270 variations, so don't specify parameters to avoid conflicts.
5271 The expand_* functions check the argument types anyway. */
5272 temp = builtin_function ("bzero", void_ftype_any,
5273 BUILT_IN_BZERO, BUILT_IN_NORMAL, NULL_PTR);
5274 DECL_BUILT_IN_NONANSI (temp) = 1;
5275 temp = builtin_function ("bcmp", int_ftype_any,
5276 BUILT_IN_BCMP, BUILT_IN_NORMAL, NULL_PTR);
5277 DECL_BUILT_IN_NONANSI (temp) = 1;
5280 builtin_function ("__builtin_abs", int_ftype_int, BUILT_IN_ABS,
5281 BUILT_IN_NORMAL, NULL_PTR);
5282 builtin_function ("__builtin_fabsf", float_ftype_float, BUILT_IN_FABS,
5283 BUILT_IN_NORMAL, NULL_PTR);
5284 builtin_function ("__builtin_fabs", double_ftype_double, BUILT_IN_FABS,
5285 BUILT_IN_NORMAL, NULL_PTR);
5286 builtin_function ("__builtin_fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
5287 BUILT_IN_NORMAL, NULL_PTR);
5288 builtin_function ("__builtin_labs", long_ftype_long, BUILT_IN_LABS,
5289 BUILT_IN_NORMAL, NULL_PTR);
5290 builtin_function ("__builtin_llabs", longlong_ftype_longlong, BUILT_IN_LLABS,
5291 BUILT_IN_NORMAL, NULL_PTR);
5292 builtin_function ("__builtin_imaxabs", intmax_ftype_intmax, BUILT_IN_IMAXABS,
5293 BUILT_IN_NORMAL, NULL_PTR);
5294 builtin_function ("__builtin_saveregs", ptr_ftype, BUILT_IN_SAVEREGS,
5295 BUILT_IN_NORMAL, NULL_PTR);
5296 builtin_function ("__builtin_classify_type", default_function_type,
5297 BUILT_IN_CLASSIFY_TYPE, BUILT_IN_NORMAL, NULL_PTR);
5298 builtin_function ("__builtin_next_arg", ptr_ftype, BUILT_IN_NEXT_ARG,
5299 BUILT_IN_NORMAL, NULL_PTR);
5300 builtin_function ("__builtin_args_info", int_ftype_int, BUILT_IN_ARGS_INFO,
5301 BUILT_IN_NORMAL, NULL_PTR);
5302 builtin_function ("__builtin_setjmp",
5303 build_function_type (integer_type_node,
5304 tree_cons (NULL_TREE, ptr_type_node,
5306 BUILT_IN_SETJMP, BUILT_IN_NORMAL, NULL_PTR);
5307 builtin_function ("__builtin_longjmp",
5308 build_function_type (void_type_node,
5309 tree_cons (NULL_TREE, ptr_type_node,
5310 tree_cons (NULL_TREE,
5313 BUILT_IN_LONGJMP, BUILT_IN_NORMAL, NULL_PTR);
5314 builtin_function ("__builtin_trap", void_ftype, BUILT_IN_TRAP,
5315 BUILT_IN_NORMAL, NULL_PTR);
5317 /* ISO C99 IEEE Unordered compares. */
5318 builtin_function ("__builtin_isgreater", default_function_type,
5319 BUILT_IN_ISGREATER, BUILT_IN_NORMAL, NULL_PTR);
5320 builtin_function ("__builtin_isgreaterequal", default_function_type,
5321 BUILT_IN_ISGREATEREQUAL, BUILT_IN_NORMAL, NULL_PTR);
5322 builtin_function ("__builtin_isless", default_function_type,
5323 BUILT_IN_ISLESS, BUILT_IN_NORMAL, NULL_PTR);
5324 builtin_function ("__builtin_islessequal", default_function_type,
5325 BUILT_IN_ISLESSEQUAL, BUILT_IN_NORMAL, NULL_PTR);
5326 builtin_function ("__builtin_islessgreater", default_function_type,
5327 BUILT_IN_ISLESSGREATER, BUILT_IN_NORMAL, NULL_PTR);
5328 builtin_function ("__builtin_isunordered", default_function_type,
5329 BUILT_IN_ISUNORDERED, BUILT_IN_NORMAL, NULL_PTR);
5331 /* Untyped call and return. */
5332 builtin_function ("__builtin_apply_args", ptr_ftype,
5333 BUILT_IN_APPLY_ARGS, BUILT_IN_NORMAL, NULL_PTR);
5335 temp = tree_cons (NULL_TREE,
5336 build_pointer_type (build_function_type (void_type_node,
5338 tree_cons (NULL_TREE,
5340 tree_cons (NULL_TREE,
5343 builtin_function ("__builtin_apply",
5344 build_function_type (ptr_type_node, temp),
5345 BUILT_IN_APPLY, BUILT_IN_NORMAL, NULL_PTR);
5346 builtin_function ("__builtin_return", void_ftype_ptr,
5347 BUILT_IN_RETURN, BUILT_IN_NORMAL, NULL_PTR);
5349 /* Support for varargs.h and stdarg.h. */
5350 builtin_function ("__builtin_varargs_start",
5351 build_function_type (void_type_node,
5352 tree_cons (NULL_TREE,
5353 va_list_ref_type_node,
5355 BUILT_IN_VARARGS_START, BUILT_IN_NORMAL, NULL_PTR);
5357 builtin_function ("__builtin_stdarg_start",
5358 build_function_type (void_type_node,
5359 tree_cons (NULL_TREE,
5360 va_list_ref_type_node,
5362 BUILT_IN_STDARG_START, BUILT_IN_NORMAL, NULL_PTR);
5364 builtin_function ("__builtin_va_end",
5365 build_function_type (void_type_node,
5366 tree_cons (NULL_TREE,
5367 va_list_ref_type_node,
5369 BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL_PTR);
5371 builtin_function ("__builtin_va_copy",
5372 build_function_type (void_type_node,
5373 tree_cons (NULL_TREE,
5374 va_list_ref_type_node,
5375 tree_cons (NULL_TREE,
5376 va_list_arg_type_node,
5378 BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL_PTR);
5380 /* ??? Ought to be `T __builtin_expect(T, T)' for any type T. */
5381 builtin_function ("__builtin_expect",
5382 build_function_type (long_integer_type_node,
5383 tree_cons (NULL_TREE,
5384 long_integer_type_node,
5385 tree_cons (NULL_TREE,
5386 long_integer_type_node,
5388 BUILT_IN_EXPECT, BUILT_IN_NORMAL, NULL_PTR);
5390 /* Currently under experimentation. */
5391 builtin_function ("__builtin_memcpy", memcpy_ftype, BUILT_IN_MEMCPY,
5392 BUILT_IN_NORMAL, "memcpy");
5393 builtin_function ("__builtin_memcmp", int_ftype_cptr_cptr_sizet,
5394 BUILT_IN_MEMCMP, BUILT_IN_NORMAL, "memcmp");
5395 builtin_function ("__builtin_memset", memset_ftype,
5396 BUILT_IN_MEMSET, BUILT_IN_NORMAL, "memset");
5397 builtin_function ("__builtin_bzero", bzero_ftype,
5398 BUILT_IN_BZERO, BUILT_IN_NORMAL, "bzero");
5399 builtin_function ("__builtin_bcmp", bcmp_ftype,
5400 BUILT_IN_BCMP, BUILT_IN_NORMAL, "bcmp");
5401 builtin_function ("__builtin_index", string_ftype_string_int,
5402 BUILT_IN_INDEX, BUILT_IN_NORMAL, "index");
5403 builtin_function ("__builtin_rindex", string_ftype_string_int,
5404 BUILT_IN_RINDEX, BUILT_IN_NORMAL, "rindex");
5405 built_in_decls[BUILT_IN_STRCMP] =
5406 builtin_function ("__builtin_strcmp", int_ftype_string_string,
5407 BUILT_IN_STRCMP, BUILT_IN_NORMAL, "strcmp");
5408 builtin_function ("__builtin_strncmp", int_ftype_cstring_cstring_sizet,
5409 BUILT_IN_STRNCMP, BUILT_IN_NORMAL, "strncmp");
5410 builtin_function ("__builtin_strstr", string_ftype_string_string,
5411 BUILT_IN_STRSTR, BUILT_IN_NORMAL, "strstr");
5412 builtin_function ("__builtin_strpbrk", string_ftype_string_string,
5413 BUILT_IN_STRPBRK, BUILT_IN_NORMAL, "strpbrk");
5414 built_in_decls[BUILT_IN_STRCHR] =
5415 builtin_function ("__builtin_strchr", string_ftype_string_int,
5416 BUILT_IN_STRCHR, BUILT_IN_NORMAL, "strchr");
5417 builtin_function ("__builtin_strrchr", string_ftype_string_int,
5418 BUILT_IN_STRRCHR, BUILT_IN_NORMAL, "strrchr");
5419 builtin_function ("__builtin_strcpy", string_ftype_string_cstring,
5420 BUILT_IN_STRCPY, BUILT_IN_NORMAL, "strcpy");
5421 builtin_function ("__builtin_strncpy", string_ftype_string_cstring_sizet,
5422 BUILT_IN_STRNCPY, BUILT_IN_NORMAL, "strncpy");
5423 built_in_decls[BUILT_IN_STRCAT] =
5424 builtin_function ("__builtin_strcat", string_ftype_string_cstring,
5425 BUILT_IN_STRCAT, BUILT_IN_NORMAL, "strcat");
5426 builtin_function ("__builtin_strncat", string_ftype_string_cstring_sizet,
5427 BUILT_IN_STRNCAT, BUILT_IN_NORMAL, "strncat");
5428 builtin_function ("__builtin_strspn", string_ftype_string_cstring,
5429 BUILT_IN_STRSPN, BUILT_IN_NORMAL, "strspn");
5430 builtin_function ("__builtin_strcspn", string_ftype_string_cstring_sizet,
5431 BUILT_IN_STRCSPN, BUILT_IN_NORMAL, "strcspn");
5432 built_in_decls[BUILT_IN_STRLEN] =
5433 builtin_function ("__builtin_strlen", strlen_ftype,
5434 BUILT_IN_STRLEN, BUILT_IN_NORMAL, "strlen");
5435 builtin_function ("__builtin_sqrtf", float_ftype_float,
5436 BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrtf");
5437 builtin_function ("__builtin_fsqrt", double_ftype_double,
5438 BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrt");
5439 builtin_function ("__builtin_sqrtl", ldouble_ftype_ldouble,
5440 BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrtl");
5441 builtin_function ("__builtin_sinf", float_ftype_float,
5442 BUILT_IN_SIN, BUILT_IN_NORMAL, "sinf");
5443 builtin_function ("__builtin_sin", double_ftype_double,
5444 BUILT_IN_SIN, BUILT_IN_NORMAL, "sin");
5445 builtin_function ("__builtin_sinl", ldouble_ftype_ldouble,
5446 BUILT_IN_SIN, BUILT_IN_NORMAL, "sinl");
5447 builtin_function ("__builtin_cosf", float_ftype_float,
5448 BUILT_IN_COS, BUILT_IN_NORMAL, "cosf");
5449 builtin_function ("__builtin_cos", double_ftype_double,
5450 BUILT_IN_COS, BUILT_IN_NORMAL, "cos");
5451 builtin_function ("__builtin_cosl", ldouble_ftype_ldouble,
5452 BUILT_IN_COS, BUILT_IN_NORMAL, "cosl");
5453 built_in_decls[BUILT_IN_PUTCHAR] =
5454 builtin_function ("__builtin_putchar", int_ftype_int,
5455 BUILT_IN_PUTCHAR, BUILT_IN_NORMAL, "putchar");
5456 built_in_decls[BUILT_IN_PUTS] =
5457 builtin_function ("__builtin_puts", puts_ftype,
5458 BUILT_IN_PUTS, BUILT_IN_NORMAL, "puts");
5459 builtin_function ("__builtin_printf", printf_ftype,
5460 BUILT_IN_PRINTF, BUILT_IN_FRONTEND, "printf");
5461 /* We declare these without argument so that the initial declaration
5462 for these identifiers is a builtin. That allows us to redeclare
5463 them later with argument without worrying about the explicit
5464 declarations in stdio.h being taken as the initial declaration.
5465 Also, save the _DECL for these so we can use them later. */
5466 built_in_decls[BUILT_IN_FWRITE] =
5467 builtin_function ("__builtin_fwrite", sizet_ftype_any,
5468 BUILT_IN_FWRITE, BUILT_IN_NORMAL, "fwrite");
5469 built_in_decls[BUILT_IN_FPUTC] =
5470 builtin_function ("__builtin_fputc", int_ftype_any,
5471 BUILT_IN_FPUTC, BUILT_IN_NORMAL, "fputc");
5472 built_in_decls[BUILT_IN_FPUTS] =
5473 builtin_function ("__builtin_fputs", int_ftype_any,
5474 BUILT_IN_FPUTS, BUILT_IN_NORMAL, "fputs");
5476 if (! flag_no_builtin)
5478 builtin_function ("abs", int_ftype_int, BUILT_IN_ABS,
5479 BUILT_IN_NORMAL, NULL_PTR);
5480 builtin_function ("fabsf", float_ftype_float, BUILT_IN_FABS,
5481 BUILT_IN_NORMAL, NULL_PTR);
5482 builtin_function ("fabs", double_ftype_double, BUILT_IN_FABS,
5483 BUILT_IN_NORMAL, NULL_PTR);
5484 builtin_function ("fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
5485 BUILT_IN_NORMAL, NULL_PTR);
5486 builtin_function ("labs", long_ftype_long, BUILT_IN_LABS,
5487 BUILT_IN_NORMAL, NULL_PTR);
5488 if (flag_isoc99 || ! flag_no_nonansi_builtin)
5490 builtin_function ("llabs", longlong_ftype_longlong, BUILT_IN_LLABS,
5491 BUILT_IN_NORMAL, NULL_PTR);
5492 builtin_function ("imaxabs", intmax_ftype_intmax, BUILT_IN_IMAXABS,
5493 BUILT_IN_NORMAL, NULL_PTR);
5495 builtin_function ("memcpy", memcpy_ftype, BUILT_IN_MEMCPY,
5496 BUILT_IN_NORMAL, NULL_PTR);
5497 builtin_function ("memcmp", int_ftype_cptr_cptr_sizet, BUILT_IN_MEMCMP,
5498 BUILT_IN_NORMAL, NULL_PTR);
5499 builtin_function ("memset", memset_ftype, BUILT_IN_MEMSET,
5500 BUILT_IN_NORMAL, NULL_PTR);
5501 builtin_function ("strcmp", int_ftype_string_string, BUILT_IN_STRCMP,
5502 BUILT_IN_NORMAL, NULL_PTR);
5503 builtin_function ("strncmp", int_ftype_cstring_cstring_sizet,
5504 BUILT_IN_STRNCMP, BUILT_IN_NORMAL, NULL_PTR);
5505 builtin_function ("strstr", string_ftype_string_string, BUILT_IN_STRSTR,
5506 BUILT_IN_NORMAL, NULL_PTR);
5507 builtin_function ("strchr", string_ftype_string_int, BUILT_IN_STRCHR,
5508 BUILT_IN_NORMAL, NULL_PTR);
5509 builtin_function ("strrchr", string_ftype_string_int, BUILT_IN_STRRCHR,
5510 BUILT_IN_NORMAL, NULL_PTR);
5511 builtin_function ("strpbrk", string_ftype_string_string, BUILT_IN_STRPBRK,
5512 BUILT_IN_NORMAL, NULL_PTR);
5513 builtin_function ("strcpy", string_ftype_string_cstring, BUILT_IN_STRCPY,
5514 BUILT_IN_NORMAL, NULL_PTR);
5515 builtin_function ("strncpy", string_ftype_string_cstring_sizet,
5516 BUILT_IN_STRNCPY, BUILT_IN_NORMAL, NULL_PTR);
5517 builtin_function ("strcat", string_ftype_string_cstring, BUILT_IN_STRCAT,
5518 BUILT_IN_NORMAL, NULL_PTR);
5519 builtin_function ("strncat", string_ftype_string_cstring_sizet,
5520 BUILT_IN_STRNCAT, BUILT_IN_NORMAL, NULL_PTR);
5521 builtin_function ("strspn", sizet_ftype_cstring_cstring, BUILT_IN_STRSPN,
5522 BUILT_IN_NORMAL, NULL_PTR);
5523 builtin_function ("strcspn", sizet_ftype_cstring_cstring,
5524 BUILT_IN_STRCSPN, BUILT_IN_NORMAL, NULL_PTR);
5525 builtin_function ("strlen", strlen_ftype, BUILT_IN_STRLEN,
5526 BUILT_IN_NORMAL, NULL_PTR);
5527 builtin_function ("sqrtf", float_ftype_float, BUILT_IN_FSQRT,
5528 BUILT_IN_NORMAL, NULL_PTR);
5529 builtin_function ("sqrt", double_ftype_double, BUILT_IN_FSQRT,
5530 BUILT_IN_NORMAL, NULL_PTR);
5531 builtin_function ("sqrtl", ldouble_ftype_ldouble, BUILT_IN_FSQRT,
5532 BUILT_IN_NORMAL, NULL_PTR);
5533 builtin_function ("sinf", float_ftype_float, BUILT_IN_SIN,
5534 BUILT_IN_NORMAL, NULL_PTR);
5535 builtin_function ("sin", double_ftype_double, BUILT_IN_SIN,
5536 BUILT_IN_NORMAL, NULL_PTR);
5537 builtin_function ("sinl", ldouble_ftype_ldouble, BUILT_IN_SIN,
5538 BUILT_IN_NORMAL, NULL_PTR);
5539 builtin_function ("cosf", float_ftype_float, BUILT_IN_COS,
5540 BUILT_IN_NORMAL, NULL_PTR);
5541 builtin_function ("cos", double_ftype_double, BUILT_IN_COS,
5542 BUILT_IN_NORMAL, NULL_PTR);
5543 builtin_function ("cosl", ldouble_ftype_ldouble, BUILT_IN_COS,
5544 BUILT_IN_NORMAL, NULL_PTR);
5545 builtin_function ("printf", printf_ftype, BUILT_IN_PRINTF,
5546 BUILT_IN_FRONTEND, NULL_PTR);
5547 /* We declare these without argument so that the initial
5548 declaration for these identifiers is a builtin. That allows
5549 us to redeclare them later with argument without worrying
5550 about the explicit declarations in stdio.h being taken as the
5551 initial declaration. */
5552 builtin_function ("fputc", int_ftype_any, BUILT_IN_FPUTC,
5553 BUILT_IN_NORMAL, NULL_PTR);
5554 builtin_function ("fputs", int_ftype_any, BUILT_IN_FPUTS,
5555 BUILT_IN_NORMAL, NULL_PTR);
5557 /* Declare these functions volatile
5558 to avoid spurious "control drops through" warnings. */
5559 temp = builtin_function ("abort",
5560 ((c_language == clk_cplusplus)
5561 ? void_ftype : void_ftype_any),
5562 0, NOT_BUILT_IN, NULL_PTR);
5563 TREE_THIS_VOLATILE (temp) = 1;
5564 TREE_SIDE_EFFECTS (temp) = 1;
5566 temp = builtin_function ("exit",
5567 ((c_language == clk_cplusplus)
5568 ? void_ftype_int : void_ftype_any),
5569 0, NOT_BUILT_IN, NULL_PTR);
5570 TREE_THIS_VOLATILE (temp) = 1;
5571 TREE_SIDE_EFFECTS (temp) = 1;
5575 /* Support for these has not been written in either expand_builtin
5576 or build_function_call. */
5577 builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV,
5578 BUILT_IN_NORMAL, NULL_PTR);
5579 builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV,
5580 BUILT_IN_NORMAL, NULL_PTR);
5581 builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR,
5582 BUILT_IN_NORMAL, NULL_PTR);
5583 builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL,
5584 BUILT_IN_NORMAL, NULL_PTR);
5585 builtin_function ("__builtin_fmod", double_ftype_double_double,
5586 BUILT_IN_FMOD, BUILT_IN_NORMAL, NULL_PTR);
5587 builtin_function ("__builtin_frem", double_ftype_double_double,
5588 BUILT_IN_FREM, BUILT_IN_NORMAL, NULL_PTR);
5589 builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP,
5590 BUILT_IN_NORMAL, NULL_PTR);
5591 builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN,
5592 BUILT_IN_NORMAL, NULL_PTR);
5595 main_identifier_node = get_identifier ("main");
5597 /* ??? Perhaps there's a better place to do this. But it is related
5598 to __builtin_va_arg, so it isn't that off-the-wall. */
5599 lang_type_promotes_to = simple_type_promotes_to;
5603 build_va_arg (expr, type)
5606 return build1 (VA_ARG_EXPR, type, expr);
5609 /* Given a type, apply default promotions wrt unnamed function arguments
5610 and return the new type. Return NULL_TREE if no change. */
5611 /* ??? There is a function of the same name in the C++ front end that
5612 does something similar, but is more thorough and does not return NULL
5613 if no change. We could perhaps share code, but it would make the
5614 self_promoting_type property harder to identify. */
5617 simple_type_promotes_to (type)
5620 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5621 return double_type_node;
5623 if (C_PROMOTING_INTEGER_TYPE_P (type))
5625 /* Traditionally, unsignedness is preserved in default promotions.
5626 Also preserve unsignedness if not really getting any wider. */
5627 if (TREE_UNSIGNED (type)
5628 && (flag_traditional
5629 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
5630 return unsigned_type_node;
5631 return integer_type_node;
5637 /* Return 1 if PARMS specifies a fixed number of parameters
5638 and none of their types is affected by default promotions. */
5641 self_promoting_args_p (parms)
5645 for (t = parms; t; t = TREE_CHAIN (t))
5647 register tree type = TREE_VALUE (t);
5649 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5655 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5658 if (C_PROMOTING_INTEGER_TYPE_P (type))
5664 /* Recursively examines the array elements of TYPE, until a non-array
5665 element type is found. */
5668 strip_array_types (type)
5671 while (TREE_CODE (type) == ARRAY_TYPE)
5672 type = TREE_TYPE (type);
5677 /* Recognize certain built-in functions so we can make tree-codes
5678 other than CALL_EXPR. We do this when it enables fold-const.c
5679 to do something useful. */
5680 /* ??? By rights this should go in builtins.c, but only C and C++
5681 implement build_{binary,unary}_op. Not exactly sure what bits
5682 of functionality are actually needed from those functions, or
5683 where the similar functionality exists in the other front ends. */
5686 expand_tree_builtin (function, params, coerced_params)
5687 tree function, params, coerced_params;
5689 enum tree_code code;
5691 if (DECL_BUILT_IN_CLASS (function) != BUILT_IN_NORMAL)
5694 switch (DECL_FUNCTION_CODE (function))
5698 case BUILT_IN_LLABS:
5699 case BUILT_IN_IMAXABS:
5701 if (coerced_params == 0)
5702 return integer_zero_node;
5703 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
5705 case BUILT_IN_ISGREATER:
5706 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
5712 case BUILT_IN_ISGREATEREQUAL:
5713 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
5719 case BUILT_IN_ISLESS:
5720 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
5726 case BUILT_IN_ISLESSEQUAL:
5727 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
5733 case BUILT_IN_ISLESSGREATER:
5734 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
5740 case BUILT_IN_ISUNORDERED:
5741 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT)
5742 return integer_zero_node;
5743 code = UNORDERED_EXPR;
5751 || TREE_CHAIN (params) == 0)
5753 error ("too few arguments to function `%s'",
5754 IDENTIFIER_POINTER (DECL_NAME (function)));
5755 return error_mark_node;
5757 else if (TREE_CHAIN (TREE_CHAIN (params)) != 0)
5759 error ("too many arguments to function `%s'",
5760 IDENTIFIER_POINTER (DECL_NAME (function)));
5761 return error_mark_node;
5764 arg0 = TREE_VALUE (params);
5765 arg1 = TREE_VALUE (TREE_CHAIN (params));
5766 arg0 = build_binary_op (code, arg0, arg1, 0);
5767 if (code != UNORDERED_EXPR)
5768 arg0 = build_unary_op (TRUTH_NOT_EXPR, arg0, 0);
5780 /* Returns non-zero if CODE is the code for a statement. */
5783 statement_code_p (code)
5784 enum tree_code code;
5807 if (lang_statement_code_p)
5808 return (*lang_statement_code_p) (code);
5813 /* Walk the statemen tree, rooted at *tp. Apply FUNC to all the
5814 sub-trees of *TP in a pre-order traversal. FUNC is called with the
5815 DATA and the address of each sub-tree. If FUNC returns a non-NULL
5816 value, the traversal is aborted, and the value returned by FUNC is
5817 returned. If FUNC sets WALK_SUBTREES to zero, then the subtrees of
5818 the node being visited are not walked.
5820 We don't need a without_duplicates variant of this one because the
5821 statement tree is a tree, not a graph. */
5824 walk_stmt_tree (tp, func, data)
5829 enum tree_code code;
5834 #define WALK_SUBTREE(NODE) \
5837 result = walk_stmt_tree (&(NODE), func, data); \
5843 /* Skip empty subtrees. */
5847 /* Skip subtrees below non-statement nodes. */
5848 if (!statement_code_p (TREE_CODE (*tp)))
5851 /* Call the function. */
5853 result = (*func) (tp, &walk_subtrees, data);
5855 /* If we found something, return it. */
5859 /* Even if we didn't, FUNC may have decided that there was nothing
5860 interesting below this point in the tree. */
5864 /* FUNC may have modified the tree, recheck that we're looking at a
5866 code = TREE_CODE (*tp);
5867 if (!statement_code_p (code))
5870 /* Walk over all the sub-trees of this operand. Statement nodes never
5871 contain RTL, and we needn't worry about TARGET_EXPRs. */
5872 len = TREE_CODE_LENGTH (code);
5874 /* Go through the subtrees. We need to do this in forward order so
5875 that the scope of a FOR_EXPR is handled properly. */
5876 for (i = 0; i < len; ++i)
5877 WALK_SUBTREE (TREE_OPERAND (*tp, i));
5879 /* Finally visit the chain. This can be tail-recursion optimized if
5880 we write it this way. */
5881 return walk_stmt_tree (&TREE_CHAIN (*tp), func, data);
5886 /* Used to compare case labels. K1 and K2 are actually tree nodes
5887 representing case labels, or NULL_TREE for a `default' label.
5888 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5889 K2, and 0 if K1 and K2 are equal. */
5892 case_compare (k1, k2)
5896 /* Consider a NULL key (such as arises with a `default' label) to be
5897 smaller than anything else. */
5903 return tree_int_cst_compare ((tree) k1, (tree) k2);
5906 /* Process a case label for the range LOW_VALUE ... HIGH_VALUE. If
5907 LOW_VALUE and HIGH_VALUE are both NULL_TREE then this case label is
5908 actually a `default' label. If only HIGH_VALUE is NULL_TREE, then
5909 case label was declared using the usual C/C++ syntax, rather than
5910 the GNU case range extension. CASES is a tree containing all the
5911 case ranges processed so far; COND is the condition for the
5912 switch-statement itself. Returns the CASE_LABEL created, or
5913 ERROR_MARK_NODE if no CASE_LABEL is created. */
5916 c_add_case_label (cases, cond, low_value, high_value)
5925 splay_tree_node node;
5927 /* Create the LABEL_DECL itself. */
5928 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
5929 DECL_CONTEXT (label) = current_function_decl;
5931 /* If there was an error processing the switch condition, bail now
5932 before we get more confused. */
5933 if (!cond || cond == error_mark_node)
5935 /* Add a label anyhow so that the back-end doesn't think that
5936 the beginning of the switch is unreachable. */
5938 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
5939 return error_mark_node;
5942 if ((low_value && TREE_TYPE (low_value)
5943 && POINTER_TYPE_P (TREE_TYPE (low_value)))
5944 || (high_value && TREE_TYPE (high_value)
5945 && POINTER_TYPE_P (TREE_TYPE (high_value))))
5946 error ("pointers are not permitted as case values");
5948 /* Case ranges are a GNU extension. */
5949 if (high_value && pedantic)
5951 if (c_language == clk_cplusplus)
5952 pedwarn ("ISO C++ forbids range expressions in switch statements");
5954 pedwarn ("ISO C forbids range expressions in switch statements");
5957 type = TREE_TYPE (cond);
5960 low_value = check_case_value (low_value);
5961 low_value = convert_and_check (type, low_value);
5965 high_value = check_case_value (high_value);
5966 high_value = convert_and_check (type, high_value);
5969 /* If an error has occurred, bail out now. */
5970 if (low_value == error_mark_node || high_value == error_mark_node)
5973 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
5974 return error_mark_node;
5977 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
5978 really a case range, even though it was written that way. Remove
5979 the HIGH_VALUE to simplify later processing. */
5980 if (tree_int_cst_equal (low_value, high_value))
5981 high_value = NULL_TREE;
5982 if (low_value && high_value
5983 && !tree_int_cst_lt (low_value, high_value))
5984 warning ("empty range specified");
5986 /* Look up the LOW_VALUE in the table of case labels we already
5988 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
5989 /* If there was not an exact match, check for overlapping ranges.
5990 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
5991 that's a `default' label and the only overlap is an exact match. */
5992 if (!node && (low_value || high_value))
5994 splay_tree_node low_bound;
5995 splay_tree_node high_bound;
5997 /* Even though there wasn't an exact match, there might be an
5998 overlap between this case range and another case range.
5999 Since we've (inductively) not allowed any overlapping case
6000 ranges, we simply need to find the greatest low case label
6001 that is smaller that LOW_VALUE, and the smallest low case
6002 label that is greater than LOW_VALUE. If there is an overlap
6003 it will occur in one of these two ranges. */
6004 low_bound = splay_tree_predecessor (cases,
6005 (splay_tree_key) low_value);
6006 high_bound = splay_tree_successor (cases,
6007 (splay_tree_key) low_value);
6009 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6010 the LOW_VALUE, so there is no need to check unless the
6011 LOW_BOUND is in fact itself a case range. */
6013 && CASE_HIGH ((tree) low_bound->value)
6014 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6017 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6018 range is bigger than the low end of the current range, so we
6019 are only interested if the current range is a real range, and
6020 not an ordinary case label. */
6023 && (tree_int_cst_compare ((tree) high_bound->key,
6028 /* If there was an overlap, issue an error. */
6031 tree duplicate = CASE_LABEL_DECL ((tree) node->value);
6035 error ("duplicate (or overlapping) case value");
6036 error_with_decl (duplicate,
6037 "this is the first entry overlapping that value");
6041 error ("duplicate case value") ;
6042 error_with_decl (duplicate, "previously used here");
6046 error ("multiple default labels in one switch");
6047 error_with_decl (duplicate, "this is the first default label");
6050 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
6053 /* Add a CASE_LABEL to the statement-tree. */
6054 case_label = add_stmt (build_case_label (low_value, high_value, label));
6055 /* Register this case label in the splay tree. */
6056 splay_tree_insert (cases,
6057 (splay_tree_key) low_value,
6058 (splay_tree_value) case_label);
6063 /* Mark P (a stmt_tree) for GC. The use of a `void *' for the
6064 parameter allows this function to be used as a GC-marking
6071 stmt_tree st = (stmt_tree) p;
6073 ggc_mark_tree (st->x_last_stmt);
6074 ggc_mark_tree (st->x_last_expr_type);
6077 /* Mark LD for GC. */
6080 c_mark_lang_decl (c)
6081 struct c_lang_decl *c;
6083 ggc_mark_tree (c->saved_tree);
6086 /* Mark F for GC. */
6089 mark_c_language_function (f)
6090 struct language_function *f;
6095 mark_stmt_tree (&f->x_stmt_tree);
6096 ggc_mark_tree (f->x_scope_stmt_stack);
6099 /* Hook used by expand_expr to expand language-specific tree codes. */
6102 c_expand_expr (exp, target, tmode, modifier)
6105 enum machine_mode tmode;
6106 enum expand_modifier modifier;
6108 switch (TREE_CODE (exp))
6115 /* Since expand_expr_stmt calls free_temp_slots after every
6116 expression statement, we must call push_temp_slots here.
6117 Otherwise, any temporaries in use now would be considered
6118 out-of-scope after the first EXPR_STMT from within the
6121 rtl_expr = expand_start_stmt_expr ();
6122 expand_stmt (STMT_EXPR_STMT (exp));
6123 expand_end_stmt_expr (rtl_expr);
6124 result = expand_expr (rtl_expr, target, tmode, modifier);
6132 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6133 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6135 && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6136 && (DECL_BUILT_IN_CLASS (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6137 == BUILT_IN_FRONTEND))
6138 return c_expand_builtin (exp, target, tmode, modifier);
6152 /* Hook used by safe_from_p to handle language-specific tree codes. */
6155 c_safe_from_p (target, exp)
6159 /* We can see statements here when processing the body of a
6160 statement-expression. For a declaration statement declaring a
6161 variable, look at the variable's initializer. */
6162 if (TREE_CODE (exp) == DECL_STMT)
6164 tree decl = DECL_STMT_DECL (exp);
6166 if (TREE_CODE (decl) == VAR_DECL
6167 && DECL_INITIAL (decl)
6168 && !safe_from_p (target, DECL_INITIAL (decl), /*top_p=*/0))
6172 /* For any statement, we must follow the statement-chain. */
6173 if (statement_code_p (TREE_CODE (exp)) && TREE_CHAIN (exp))
6174 return safe_from_p (target, TREE_CHAIN (exp), /*top_p=*/0);
6176 /* Assume everything else is safe. */
6180 /* Hook used by unsafe_for_reeval to handle language-specific tree codes. */
6183 c_unsafe_for_reeval (exp)
6186 /* Statement expressions may not be reevaluated. */
6187 if (TREE_CODE (exp) == STMT_EXPR)
6190 /* Walk all other expressions. */
6194 /* Tree code classes. */
6196 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
6198 static char c_tree_code_type[] = {
6200 #include "c-common.def"
6204 /* Table indexed by tree code giving number of expression
6205 operands beyond the fixed part of the node structure.
6206 Not used for types or decls. */
6208 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
6210 static int c_tree_code_length[] = {
6212 #include "c-common.def"
6216 /* Names of tree components.
6217 Used for printing out the tree and error messages. */
6218 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
6220 static const char *c_tree_code_name[] = {
6222 #include "c-common.def"
6226 /* Adds the tree codes specific to the C front end to the list of all
6232 memcpy (tree_code_type + (int) LAST_AND_UNUSED_TREE_CODE,
6234 (int)LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE);
6235 memcpy (tree_code_length + (int) LAST_AND_UNUSED_TREE_CODE,
6237 (LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (int));
6238 memcpy (tree_code_name + (int) LAST_AND_UNUSED_TREE_CODE,
6240 (LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (char *));
6241 lang_unsafe_for_reeval = c_unsafe_for_reeval;
6244 #define CALLED_AS_BUILT_IN(NODE) \
6245 (!strncmp (IDENTIFIER_POINTER (DECL_NAME (NODE)), "__builtin_", 10))
6248 c_expand_builtin (exp, target, tmode, modifier)
6251 enum machine_mode tmode;
6252 enum expand_modifier modifier;
6254 tree type = TREE_TYPE (exp);
6255 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6256 tree arglist = TREE_OPERAND (exp, 1);
6257 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6258 enum tree_code code = TREE_CODE (exp);
6259 const int ignore = (target == const0_rtx
6260 || ((code == NON_LVALUE_EXPR || code == NOP_EXPR
6261 || code == CONVERT_EXPR || code == REFERENCE_EXPR
6262 || code == COND_EXPR)
6263 && TREE_CODE (type) == VOID_TYPE));
6265 if (! optimize && ! CALLED_AS_BUILT_IN (fndecl))
6266 return expand_call (exp, target, ignore);
6270 case BUILT_IN_PRINTF:
6271 target = c_expand_builtin_printf (arglist, target, tmode,
6277 default: /* just do library call, if unknown builtin */
6278 error ("built-in function `%s' not currently supported",
6279 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
6282 /* The switch statement above can drop through to cause the function
6283 to be called normally. */
6284 return expand_call (exp, target, ignore);
6287 /* Check an arglist to *printf for problems. The arglist should start
6288 at the format specifier, with the remaining arguments immediately
6291 is_valid_printf_arglist (arglist)
6294 /* Save this value so we can restore it later. */
6295 const int SAVE_pedantic = pedantic;
6296 int diagnostic_occurred = 0;
6298 /* Set this to a known value so the user setting won't affect code
6301 /* Check to make sure there are no format specifier errors. */
6302 check_function_format (&diagnostic_occurred,
6303 maybe_get_identifier("printf"),
6304 NULL_TREE, arglist);
6306 /* Restore the value of `pedantic'. */
6307 pedantic = SAVE_pedantic;
6309 /* If calling `check_function_format_ptr' produces a warning, we
6310 return false, otherwise we return true. */
6311 return ! diagnostic_occurred;
6314 /* If the arguments passed to printf are suitable for optimizations,
6315 we attempt to transform the call. */
6317 c_expand_builtin_printf (arglist, target, tmode, modifier, ignore)
6320 enum machine_mode tmode;
6321 enum expand_modifier modifier;
6324 tree fn_putchar = built_in_decls[BUILT_IN_PUTCHAR],
6325 fn_puts = built_in_decls[BUILT_IN_PUTS];
6326 tree fn, format_arg, stripped_string;
6328 /* If the return value is used, or the replacement _DECL isn't
6329 initialized, don't do the transformation. */
6330 if (!ignore || !fn_putchar || !fn_puts)
6333 /* Verify the required arguments in the original call. */
6335 || (TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE))
6338 /* Check the specifier vs. the parameters. */
6339 if (!is_valid_printf_arglist (arglist))
6342 format_arg = TREE_VALUE (arglist);
6343 stripped_string = format_arg;
6344 STRIP_NOPS (stripped_string);
6345 if (stripped_string && TREE_CODE (stripped_string) == ADDR_EXPR)
6346 stripped_string = TREE_OPERAND (stripped_string, 0);
6348 /* If the format specifier isn't a STRING_CST, punt. */
6349 if (TREE_CODE (stripped_string) != STRING_CST)
6352 /* OK! We can attempt optimization. */
6354 /* If the format specifier was "%s\n", call __builtin_puts(arg2). */
6355 if (strcmp (TREE_STRING_POINTER (stripped_string), "%s\n") == 0)
6357 arglist = TREE_CHAIN (arglist);
6360 /* If the format specifier was "%c", call __builtin_putchar (arg2). */
6361 else if (strcmp (TREE_STRING_POINTER (stripped_string), "%c") == 0)
6363 arglist = TREE_CHAIN (arglist);
6368 /* We can't handle anything else with % args or %% ... yet. */
6369 if (strchr (TREE_STRING_POINTER (stripped_string), '%'))
6372 /* If the resulting constant string has a length of 1, call
6373 putchar. Note, TREE_STRING_LENGTH includes the terminating
6374 NULL in its count. */
6375 if (TREE_STRING_LENGTH (stripped_string) == 2)
6377 /* Given printf("c"), (where c is any one character,)
6378 convert "c"[0] to an int and pass that to the replacement
6380 arglist = build_int_2 (TREE_STRING_POINTER (stripped_string)[0], 0);
6381 arglist = build_tree_list (NULL_TREE, arglist);
6385 /* If the resulting constant was "string\n", call
6386 __builtin_puts("string"). Ensure "string" has at least one
6387 character besides the trailing \n. Note, TREE_STRING_LENGTH
6388 includes the terminating NULL in its count. */
6389 else if (TREE_STRING_LENGTH (stripped_string) > 2
6390 && TREE_STRING_POINTER (stripped_string)
6391 [TREE_STRING_LENGTH (stripped_string) - 2] == '\n')
6393 /* Create a NULL-terminated string that's one char shorter
6394 than the original, stripping off the trailing '\n'. */
6395 const int newlen = TREE_STRING_LENGTH (stripped_string) - 1;
6396 char *newstr = (char *) alloca (newlen);
6397 memcpy (newstr, TREE_STRING_POINTER (stripped_string), newlen - 1);
6398 newstr[newlen - 1] = 0;
6400 arglist = combine_strings (build_string (newlen, newstr));
6401 arglist = build_tree_list (NULL_TREE, arglist);
6405 /* We'd like to arrange to call fputs(string) here, but we
6406 need stdout and don't have a way to get it ... yet. */
6410 return expand_expr (build_function_call (fn, arglist),
6411 (ignore ? const0_rtx : target),
6416 /* Given a boolean expression ARG, return a tree representing an increment
6417 or decrement (as indicated by CODE) of ARG. The front end must check for
6418 invalid cases (e.g., decrement in C++). */
6420 boolean_increment (code, arg)
6421 enum tree_code code;
6425 tree true_res = (c_language == clk_cplusplus
6427 : c_bool_true_node);
6428 arg = stabilize_reference (arg);
6431 case PREINCREMENT_EXPR:
6432 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6434 case POSTINCREMENT_EXPR:
6435 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6436 arg = save_expr (arg);
6437 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6438 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6440 case PREDECREMENT_EXPR:
6441 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, invert_truthvalue (arg));
6443 case POSTDECREMENT_EXPR:
6444 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, invert_truthvalue (arg));
6445 arg = save_expr (arg);
6446 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6447 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6452 TREE_SIDE_EFFECTS (val) = 1;