]> git.ipfire.org Git - thirdparty/gcc.git/blame_incremental - gcc/c-common.c
* configure.in: Check for the mktemp command.
[thirdparty/gcc.git] / gcc / c-common.c
... / ...
CommitLineData
1/* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU CC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU CC; see the file COPYING. If not, write to
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
21
22#include "config.h"
23#include "system.h"
24#include "tree.h"
25#include "flags.h"
26#include "toplev.h"
27#include "output.h"
28#include "c-pragma.h"
29#include "rtl.h"
30#include "ggc.h"
31#include "expr.h"
32#include "c-common.h"
33#include "defaults.h"
34#include "tm_p.h"
35#include "intl.h"
36#include "diagnostic.h"
37#include "obstack.h"
38#include "cpplib.h"
39cpp_reader *parse_in; /* Declared in c-lex.h. */
40
41#undef WCHAR_TYPE_SIZE
42#define WCHAR_TYPE_SIZE TYPE_PRECISION (wchar_type_node)
43
44#ifndef PTRDIFF_TYPE
45#define PTRDIFF_TYPE "long int"
46#endif
47
48#ifndef WINT_TYPE
49#define WINT_TYPE "unsigned int"
50#endif
51
52#ifndef INTMAX_TYPE
53#define INTMAX_TYPE ((INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
54 ? "int" \
55 : ((LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
56 ? "long int" \
57 : "long long int"))
58#endif
59
60#ifndef UINTMAX_TYPE
61#define UINTMAX_TYPE ((INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
62 ? "unsigned int" \
63 : ((LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
64 ? "long unsigned int" \
65 : "long long unsigned int"))
66#endif
67
68/* The following symbols are subsumed in the c_global_trees array, and
69 listed here individually for documentation purposes.
70
71 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
72
73 tree short_integer_type_node;
74 tree long_integer_type_node;
75 tree long_long_integer_type_node;
76
77 tree short_unsigned_type_node;
78 tree long_unsigned_type_node;
79 tree long_long_unsigned_type_node;
80
81 tree boolean_type_node;
82 tree boolean_false_node;
83 tree boolean_true_node;
84
85 tree ptrdiff_type_node;
86
87 tree unsigned_char_type_node;
88 tree signed_char_type_node;
89 tree wchar_type_node;
90 tree signed_wchar_type_node;
91 tree unsigned_wchar_type_node;
92
93 tree float_type_node;
94 tree double_type_node;
95 tree long_double_type_node;
96
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;
101
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;
107
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;
113
114 tree widest_integer_literal_type_node;
115 tree widest_unsigned_literal_type_node;
116
117 Nodes for types `void *' and `const void *'.
118
119 tree ptr_type_node, const_ptr_type_node;
120
121 Nodes for types `char *' and `const char *'.
122
123 tree string_type_node, const_string_type_node;
124
125 Type `char[SOMENUMBER]'.
126 Used when an array of char is needed and the size is irrelevant.
127
128 tree char_array_type_node;
129
130 Type `int[SOMENUMBER]' or something like it.
131 Used when an array of int needed and the size is irrelevant.
132
133 tree int_array_type_node;
134
135 Type `wchar_t[SOMENUMBER]' or something like it.
136 Used when a wide string literal is created.
137
138 tree wchar_array_type_node;
139
140 Type `int ()' -- used for implicit declaration of functions.
141
142 tree default_function_type;
143
144 Function types `int (int)', etc.
145
146 tree int_ftype_int;
147 tree void_ftype;
148 tree void_ftype_ptr;
149 tree int_ftype_int;
150 tree ptr_ftype_sizetype;
151
152 A VOID_TYPE node, packaged in a TREE_LIST.
153
154 tree void_list_node;
155
156 The identifiers __FUNCTION__, __PRETTY_FUNCTION__, and __func__.
157
158 tree function_id_node;
159 tree pretty_function_id_node;
160 tree func_id_node;
161
162*/
163
164tree c_global_trees[CTI_MAX];
165
166/* Nonzero means don't recognize the non-ANSI builtin functions. */
167
168int flag_no_builtin;
169
170/* Nonzero means don't recognize the non-ANSI builtin functions.
171 -ansi sets this. */
172
173int flag_no_nonansi_builtin;
174
175/* If non-NULL, dump the tree structure for the entire translation
176 unit to this file. */
177
178const char *flag_dump_translation_unit;
179
180/* Warn about *printf or *scanf format/argument anomalies. */
181
182int warn_format;
183
184/* Warn about Y2K problems with strftime formats. */
185
186int warn_format_y2k;
187
188/* Warn about excess arguments to formats. */
189
190int warn_format_extra_args;
191
192/* Warn about non-literal format arguments. */
193
194int warn_format_nonliteral;
195
196/* Warn about possible security problems with calls to format functions. */
197
198int warn_format_security;
199
200/* Nonzero means warn about possible violations of sequence point rules. */
201
202int warn_sequence_point;
203
204/* The elements of `ridpointers' are identifier nodes for the reserved
205 type names and storage classes. It is indexed by a RID_... value. */
206tree *ridpointers;
207
208tree (*make_fname_decl) PARAMS ((tree, const char *, int));
209
210/* If non-NULL, the address of a language-specific function that
211 returns 1 for language-specific statement codes. */
212int (*lang_statement_code_p) PARAMS ((enum tree_code));
213
214/* If non-NULL, the address of a language-specific function that takes
215 any action required right before expand_function_end is called. */
216void (*lang_expand_function_end) PARAMS ((void));
217
218/* If this variable is defined to a non-NULL value, it will be called
219 after the file has been completely parsed. */
220void (*back_end_hook) PARAMS ((tree));
221
222/* Nonzero means the expression being parsed will never be evaluated.
223 This is a count, since unevaluated expressions can nest. */
224int skip_evaluation;
225
226enum 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};
231
232enum format_type { printf_format_type, scanf_format_type,
233 strftime_format_type, strfmon_format_type };
234
235static void add_attribute PARAMS ((enum attrs, const char *,
236 int, int, int));
237static void init_attributes PARAMS ((void));
238static void record_function_format PARAMS ((tree, tree, enum format_type,
239 int, int));
240static void record_international_format PARAMS ((tree, tree, int));
241static int default_valid_lang_attribute PARAMS ((tree, tree, tree, tree));
242
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. */
248typedef struct
249{
250 int compstmt_count;
251 int line;
252 const char *file;
253 int needs_warning;
254 tree if_stmt;
255} if_elt;
256
257static if_elt *if_stack;
258
259/* Amount of space in the if statement stack. */
260static int if_stack_space = 0;
261
262/* Stack pointer. */
263static int if_stack_pointer = 0;
264
265/* Record the start of an if-then, and record the start of it
266 for ambiguous else detection. */
267
268void
269c_expand_start_cond (cond, compstmt_count)
270 tree cond;
271 int compstmt_count;
272{
273 tree if_stmt;
274
275 /* Make sure there is enough space on the stack. */
276 if (if_stack_space == 0)
277 {
278 if_stack_space = 10;
279 if_stack = (if_elt *)xmalloc (10 * sizeof (if_elt));
280 }
281 else if (if_stack_space == if_stack_pointer)
282 {
283 if_stack_space += 10;
284 if_stack = (if_elt *)xrealloc (if_stack, if_stack_space * sizeof (if_elt));
285 }
286
287 if_stmt = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
288 IF_COND (if_stmt) = cond;
289 add_stmt (if_stmt);
290
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;
297 if_stack_pointer++;
298}
299
300/* Called after the then-clause for an if-statement is processed. */
301
302void
303c_finish_then ()
304{
305 tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt;
306 RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
307}
308
309/* Record the end of an if-then. Optionally warn if a nested
310 if statement had an ambiguous else clause. */
311
312void
313c_expand_end_cond ()
314{
315 if_stack_pointer--;
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;
321}
322
323/* Called between the then-clause and the else-clause
324 of an if-then-else. */
325
326void
327c_expand_start_else ()
328{
329 /* An ambiguous else warning must be generated for the enclosing if
330 statement, unless we see an else branch for that one, too. */
331 if (warn_parentheses
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;
336
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--;
342}
343
344/* Called after the else-clause for an if-statement is processed. */
345
346void
347c_finish_else ()
348{
349 tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt;
350 RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
351}
352
353/* Make bindings for __FUNCTION__, __PRETTY_FUNCTION__, and __func__. */
354
355void
356declare_function_name ()
357{
358 const char *name, *printable_name;
359
360 if (current_function_decl == NULL)
361 {
362 name = "";
363 printable_name = "top level";
364 }
365 else
366 {
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));
370 else
371 name = "";
372 printable_name = (*decl_printable_name) (current_function_decl, 2);
373
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);
377 }
378
379 (*make_fname_decl) (function_id_node, name, 0);
380 (*make_fname_decl) (pretty_function_id_node, printable_name, 1);
381}
382
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. */
386
387tree
388combine_strings (strings)
389 tree strings;
390{
391 register tree value, t;
392 register int length = 1;
393 int wide_length = 0;
394 int wide_flag = 0;
395 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
396 int nchars;
397 const int nchars_max = flag_isoc99 ? 4095 : 509;
398
399 if (TREE_CHAIN (strings))
400 {
401 /* More than one in the chain, so concatenate. */
402 register char *p, *q;
403
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))
408 {
409 if (TREE_TYPE (t) == wchar_array_type_node)
410 {
411 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
412 wide_flag = 1;
413 }
414 else
415 length += (TREE_STRING_LENGTH (t) - 1);
416 }
417
418 /* If anything is wide, the non-wides will be converted,
419 which makes them take more space. */
420 if (wide_flag)
421 length = length * wchar_bytes + wide_length;
422
423 p = alloca (length);
424
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. */
428
429 q = p;
430 for (t = strings; t; t = TREE_CHAIN (t))
431 {
432 int len = (TREE_STRING_LENGTH (t)
433 - ((TREE_TYPE (t) == wchar_array_type_node)
434 ? wchar_bytes : 1));
435 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
436 {
437 memcpy (q, TREE_STRING_POINTER (t), len);
438 q += len;
439 }
440 else
441 {
442 int i;
443 for (i = 0; i < len; i++)
444 {
445 if (WCHAR_TYPE_SIZE == HOST_BITS_PER_SHORT)
446 ((short *) q)[i] = TREE_STRING_POINTER (t)[i];
447 else
448 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
449 }
450 q += len * wchar_bytes;
451 }
452 }
453 if (wide_flag)
454 {
455 int i;
456 for (i = 0; i < wchar_bytes; i++)
457 *q++ = 0;
458 }
459 else
460 *q = 0;
461
462 value = build_string (length, p);
463 }
464 else
465 {
466 value = strings;
467 length = TREE_STRING_LENGTH (value);
468 if (TREE_TYPE (value) == wchar_array_type_node)
469 wide_flag = 1;
470 }
471
472 /* Compute the number of elements, for the array type. */
473 nchars = wide_flag ? length / wchar_bytes : length;
474
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);
478
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))
485 {
486 tree elements
487 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
488 1, 0);
489 TREE_TYPE (value)
490 = build_array_type (elements,
491 build_index_type (build_int_2 (nchars - 1, 0)));
492 }
493 else
494 TREE_TYPE (value)
495 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
496 build_index_type (build_int_2 (nchars - 1, 0)));
497
498 TREE_CONSTANT (value) = 1;
499 TREE_READONLY (value) = ! flag_writable_strings;
500 TREE_STATIC (value) = 1;
501 return value;
502}
503\f
504/* To speed up processing of attributes, we maintain an array of
505 IDENTIFIER_NODES and the corresponding attribute types. */
506
507/* Array to hold attribute information. */
508
509static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
510
511static int attrtab_idx = 0;
512
513/* Add an entry to the attribute table above. */
514
515static void
516add_attribute (id, string, min_len, max_len, decl_req)
517 enum attrs id;
518 const char *string;
519 int min_len, max_len;
520 int decl_req;
521{
522 char buf[100];
523
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;
529
530 sprintf (buf, "__%s__", string);
531
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;
537}
538
539/* Initialize attribute table. */
540
541static void
542init_attributes ()
543{
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);
566}
567\f
568/* Default implementation of valid_lang_attribute, below. By default, there
569 are no language-specific attributes. */
570
571static int
572default_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;
577{
578 return 0;
579}
580
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. */
583
584int (*valid_lang_attribute) PARAMS ((tree, tree, tree, tree))
585 = default_valid_lang_attribute;
586
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. */
591
592void
593decl_attributes (node, attributes, prefix_attributes)
594 tree node, attributes, prefix_attributes;
595{
596 tree decl = 0, type = 0;
597 int is_type = 0;
598 tree a;
599
600 if (attrtab_idx == 0)
601 init_attributes ();
602
603 if (DECL_P (node))
604 {
605 decl = node;
606 type = TREE_TYPE (decl);
607 is_type = TREE_CODE (node) == TYPE_DECL;
608 }
609 else if (TYPE_P (node))
610 type = node, is_type = 1;
611
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);
618#endif
619
620#ifdef INSERT_ATTRIBUTES
621 INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
622#endif
623
624 attributes = chainon (prefix_attributes, attributes);
625
626 for (a = attributes; a; a = TREE_CHAIN (a))
627 {
628 tree name = TREE_PURPOSE (a);
629 tree args = TREE_VALUE (a);
630 int i;
631 enum attrs id;
632
633 for (i = 0; i < attrtab_idx; i++)
634 if (attrtab[i].name == name)
635 break;
636
637 if (i == attrtab_idx)
638 {
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));
643 else if (decl != 0)
644 type = TREE_TYPE (decl);
645 continue;
646 }
647 else if (attrtab[i].decl_req && decl == 0)
648 {
649 warning ("`%s' attribute does not apply to types",
650 IDENTIFIER_POINTER (name));
651 continue;
652 }
653 else if (list_length (args) < attrtab[i].min
654 || list_length (args) > attrtab[i].max)
655 {
656 error ("wrong number of arguments specified for `%s' attribute",
657 IDENTIFIER_POINTER (name));
658 continue;
659 }
660
661 id = attrtab[i].id;
662 switch (id)
663 {
664 case A_PACKED:
665 if (is_type)
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. */
671 else
672 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
673 break;
674
675 case A_NOCOMMON:
676 if (TREE_CODE (decl) == VAR_DECL)
677 DECL_COMMON (decl) = 0;
678 else
679 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
680 break;
681
682 case A_COMMON:
683 if (TREE_CODE (decl) == VAR_DECL)
684 DECL_COMMON (decl) = 1;
685 else
686 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
687 break;
688
689 case A_NORETURN:
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
695 = build_pointer_type
696 (build_type_variant (TREE_TYPE (type),
697 TREE_READONLY (TREE_TYPE (type)), 1));
698 else
699 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
700 break;
701
702 case A_MALLOC:
703 if (TREE_CODE (decl) == FUNCTION_DECL)
704 DECL_IS_MALLOC (decl) = 1;
705 /* ??? TODO: Support types. */
706 else
707 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
708 break;
709
710 case A_UNUSED:
711 if (is_type)
712 if (decl)
713 TREE_USED (decl) = 1;
714 else
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;
721 else
722 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
723 break;
724
725 case A_CONST:
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
731 = build_pointer_type
732 (build_type_variant (TREE_TYPE (type), 1,
733 TREE_THIS_VOLATILE (TREE_TYPE (type))));
734 else
735 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
736 break;
737
738 case A_PURE:
739 if (TREE_CODE (decl) == FUNCTION_DECL)
740 DECL_IS_PURE (decl) = 1;
741 /* ??? TODO: Support types. */
742 else
743 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
744 break;
745
746
747 case A_T_UNION:
748 if (is_type
749 && TREE_CODE (type) == UNION_TYPE
750 && (decl == 0
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;
758 else
759 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
760 break;
761
762 case A_CONSTRUCTOR:
763 if (TREE_CODE (decl) == FUNCTION_DECL
764 && TREE_CODE (type) == FUNCTION_TYPE
765 && decl_function_context (decl) == 0)
766 {
767 DECL_STATIC_CONSTRUCTOR (decl) = 1;
768 TREE_USED (decl) = 1;
769 }
770 else
771 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
772 break;
773
774 case A_DESTRUCTOR:
775 if (TREE_CODE (decl) == FUNCTION_DECL
776 && TREE_CODE (type) == FUNCTION_TYPE
777 && decl_function_context (decl) == 0)
778 {
779 DECL_STATIC_DESTRUCTOR (decl) = 1;
780 TREE_USED (decl) = 1;
781 }
782 else
783 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
784 break;
785
786 case A_MODE:
787 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
788 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
789 else
790 {
791 int j;
792 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
793 int len = strlen (p);
794 enum machine_mode mode = VOIDmode;
795 tree typefm;
796
797 if (len > 4 && p[0] == '_' && p[1] == '_'
798 && p[len - 1] == '_' && p[len - 2] == '_')
799 {
800 char *newp = (char *) alloca (len - 1);
801
802 strcpy (newp, &p[2]);
803 newp[len - 4] = '\0';
804 p = newp;
805 }
806
807 /* Give this decl a type with the specified mode.
808 First check for the special modes. */
809 if (! strcmp (p, "byte"))
810 mode = byte_mode;
811 else if (!strcmp (p, "word"))
812 mode = word_mode;
813 else if (! strcmp (p, "pointer"))
814 mode = ptr_mode;
815 else
816 for (j = 0; j < NUM_MACHINE_MODES; j++)
817 if (!strcmp (p, GET_MODE_NAME (j)))
818 mode = (enum machine_mode) j;
819
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);
825 else
826 {
827 if (TYPE_PRECISION (typefm) > (TREE_UNSIGNED (type)
828 ? TYPE_PRECISION(uintmax_type_node)
829 : TYPE_PRECISION(intmax_type_node))
830 && pedantic)
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);
836 }
837 }
838 break;
839
840 case A_SECTION:
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)
845 {
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");
858 else
859 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
860 }
861 else
862 error_with_decl (node,
863 "section attribute not allowed for `%s'");
864#else
865 error_with_decl (node,
866 "section attributes are not supported for this target");
867#endif
868 break;
869
870 case A_ALIGNED:
871 {
872 tree align_expr
873 = (args ? TREE_VALUE (args)
874 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
875 int i;
876
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);
882
883 if (TREE_CODE (align_expr) != INTEGER_CST)
884 {
885 error ("requested alignment is not a constant");
886 continue;
887 }
888
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");
893 else if (is_type)
894 {
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)
899 {
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;
906 type = tt;
907 }
908
909 TYPE_ALIGN (type) = (1 << i) * BITS_PER_UNIT;
910 TYPE_USER_ALIGN (type) = 1;
911 }
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'");
916 else
917 {
918 DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT;
919 DECL_USER_ALIGN (decl) = 1;
920 }
921 }
922 break;
923
924 case A_FORMAT:
925 {
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;
932 tree argument;
933 unsigned int arg_num;
934
935 if (TREE_CODE (decl) != FUNCTION_DECL)
936 {
937 error_with_decl (decl,
938 "argument format specified for non-function `%s'");
939 continue;
940 }
941
942 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
943 {
944 error ("unrecognized format specifier");
945 continue;
946 }
947 else
948 {
949 const char *p = IDENTIFIER_POINTER (format_type_id);
950
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;
958 else if (!strcmp (p, "strfmon")
959 || !strcmp (p, "__strfmon__"))
960 format_type = strfmon_format_type;
961 else
962 {
963 warning ("`%s' is an unrecognized format function type", p);
964 continue;
965 }
966 }
967
968 /* Strip any conversions from the string index and first arg number
969 and verify they are constants. */
970 while (TREE_CODE (format_num_expr) == NOP_EXPR
971 || TREE_CODE (format_num_expr) == CONVERT_EXPR
972 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
973 format_num_expr = TREE_OPERAND (format_num_expr, 0);
974
975 while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
976 || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
977 || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
978 first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
979
980 if (TREE_CODE (format_num_expr) != INTEGER_CST
981 || TREE_INT_CST_HIGH (format_num_expr) != 0
982 || TREE_CODE (first_arg_num_expr) != INTEGER_CST
983 || TREE_INT_CST_HIGH (first_arg_num_expr) != 0)
984 {
985 error ("format string has invalid operand number");
986 continue;
987 }
988
989 format_num = TREE_INT_CST_LOW (format_num_expr);
990 first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
991 if (first_arg_num != 0 && first_arg_num <= format_num)
992 {
993 error ("format string arg follows the args to be formatted");
994 continue;
995 }
996
997 /* If a parameter list is specified, verify that the format_num
998 argument is actually a string, in case the format attribute
999 is in error. */
1000 argument = TYPE_ARG_TYPES (type);
1001 if (argument)
1002 {
1003 for (arg_num = 1; argument != 0 && arg_num != format_num;
1004 ++arg_num, argument = TREE_CHAIN (argument))
1005 ;
1006
1007 if (! argument
1008 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
1009 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
1010 != char_type_node))
1011 {
1012 error ("format string arg not a string type");
1013 continue;
1014 }
1015
1016 else if (first_arg_num != 0)
1017 {
1018 /* Verify that first_arg_num points to the last arg,
1019 the ... */
1020 while (argument)
1021 arg_num++, argument = TREE_CHAIN (argument);
1022
1023 if (arg_num != first_arg_num)
1024 {
1025 error ("args to be formatted is not '...'");
1026 continue;
1027 }
1028 }
1029 }
1030
1031 if (format_type == strftime_format_type && first_arg_num != 0)
1032 {
1033 error ("strftime formats cannot format arguments");
1034 continue;
1035 }
1036
1037 record_function_format (DECL_NAME (decl),
1038 DECL_ASSEMBLER_NAME (decl),
1039 format_type, format_num, first_arg_num);
1040 break;
1041 }
1042
1043 case A_FORMAT_ARG:
1044 {
1045 tree format_num_expr = TREE_VALUE (args);
1046 unsigned HOST_WIDE_INT format_num;
1047 unsigned int arg_num;
1048 tree argument;
1049
1050 if (TREE_CODE (decl) != FUNCTION_DECL)
1051 {
1052 error_with_decl (decl,
1053 "argument format specified for non-function `%s'");
1054 continue;
1055 }
1056
1057 /* Strip any conversions from the first arg number and verify it
1058 is a constant. */
1059 while (TREE_CODE (format_num_expr) == NOP_EXPR
1060 || TREE_CODE (format_num_expr) == CONVERT_EXPR
1061 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
1062 format_num_expr = TREE_OPERAND (format_num_expr, 0);
1063
1064 if (TREE_CODE (format_num_expr) != INTEGER_CST
1065 || TREE_INT_CST_HIGH (format_num_expr) != 0)
1066 {
1067 error ("format string has invalid operand number");
1068 continue;
1069 }
1070
1071 format_num = TREE_INT_CST_LOW (format_num_expr);
1072
1073 /* If a parameter list is specified, verify that the format_num
1074 argument is actually a string, in case the format attribute
1075 is in error. */
1076 argument = TYPE_ARG_TYPES (type);
1077 if (argument)
1078 {
1079 for (arg_num = 1; argument != 0 && arg_num != format_num;
1080 ++arg_num, argument = TREE_CHAIN (argument))
1081 ;
1082
1083 if (! argument
1084 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
1085 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
1086 != char_type_node))
1087 {
1088 error ("format string arg not a string type");
1089 continue;
1090 }
1091 }
1092
1093 if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) != POINTER_TYPE
1094 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl))))
1095 != char_type_node))
1096 {
1097 error ("function does not return string type");
1098 continue;
1099 }
1100
1101 record_international_format (DECL_NAME (decl),
1102 DECL_ASSEMBLER_NAME (decl),
1103 format_num);
1104 break;
1105 }
1106
1107 case A_WEAK:
1108 declare_weak (decl);
1109 break;
1110
1111 case A_ALIAS:
1112 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
1113 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
1114 error_with_decl (decl,
1115 "`%s' defined both normally and as an alias");
1116 else if (decl_function_context (decl) == 0)
1117 {
1118 tree id;
1119
1120 id = TREE_VALUE (args);
1121 if (TREE_CODE (id) != STRING_CST)
1122 {
1123 error ("alias arg not a string");
1124 break;
1125 }
1126 id = get_identifier (TREE_STRING_POINTER (id));
1127 /* This counts as a use of the object pointed to. */
1128 TREE_USED (id) = 1;
1129
1130 if (TREE_CODE (decl) == FUNCTION_DECL)
1131 DECL_INITIAL (decl) = error_mark_node;
1132 else
1133 DECL_EXTERNAL (decl) = 0;
1134 assemble_alias (decl, id);
1135 }
1136 else
1137 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1138 break;
1139
1140 case A_NO_CHECK_MEMORY_USAGE:
1141 if (TREE_CODE (decl) != FUNCTION_DECL)
1142 {
1143 error_with_decl (decl,
1144 "`%s' attribute applies only to functions",
1145 IDENTIFIER_POINTER (name));
1146 }
1147 else if (DECL_INITIAL (decl))
1148 {
1149 error_with_decl (decl,
1150 "can't set `%s' attribute after definition",
1151 IDENTIFIER_POINTER (name));
1152 }
1153 else
1154 DECL_NO_CHECK_MEMORY_USAGE (decl) = 1;
1155 break;
1156
1157 case A_NO_INSTRUMENT_FUNCTION:
1158 if (TREE_CODE (decl) != FUNCTION_DECL)
1159 {
1160 error_with_decl (decl,
1161 "`%s' attribute applies only to functions",
1162 IDENTIFIER_POINTER (name));
1163 }
1164 else if (DECL_INITIAL (decl))
1165 {
1166 error_with_decl (decl,
1167 "can't set `%s' attribute after definition",
1168 IDENTIFIER_POINTER (name));
1169 }
1170 else
1171 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1172 break;
1173
1174 case A_NO_LIMIT_STACK:
1175 if (TREE_CODE (decl) != FUNCTION_DECL)
1176 {
1177 error_with_decl (decl,
1178 "`%s' attribute applies only to functions",
1179 IDENTIFIER_POINTER (name));
1180 }
1181 else if (DECL_INITIAL (decl))
1182 {
1183 error_with_decl (decl,
1184 "can't set `%s' attribute after definition",
1185 IDENTIFIER_POINTER (name));
1186 }
1187 else
1188 DECL_NO_LIMIT_STACK (decl) = 1;
1189 break;
1190 }
1191 }
1192}
1193
1194/* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
1195 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
1196
1197 The head of the declspec list is stored in DECLSPECS.
1198 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
1199
1200 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
1201 the list elements. We drop the containing TREE_LIST nodes and link the
1202 resulting attributes together the way decl_attributes expects them. */
1203
1204void
1205split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
1206 tree specs_attrs;
1207 tree *declspecs, *prefix_attributes;
1208{
1209 tree t, s, a, next, specs, attrs;
1210
1211 /* This can happen after an __extension__ in pedantic mode. */
1212 if (specs_attrs != NULL_TREE
1213 && TREE_CODE (specs_attrs) == INTEGER_CST)
1214 {
1215 *declspecs = NULL_TREE;
1216 *prefix_attributes = NULL_TREE;
1217 return;
1218 }
1219
1220 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
1221 if (specs_attrs != NULL_TREE
1222 && TREE_CODE (specs_attrs) != TREE_LIST)
1223 {
1224 *declspecs = specs_attrs;
1225 *prefix_attributes = NULL_TREE;
1226 return;
1227 }
1228
1229 /* Remember to keep the lists in the same order, element-wise. */
1230
1231 specs = s = NULL_TREE;
1232 attrs = a = NULL_TREE;
1233 for (t = specs_attrs; t; t = next)
1234 {
1235 next = TREE_CHAIN (t);
1236 /* Declspecs have a non-NULL TREE_VALUE. */
1237 if (TREE_VALUE (t) != NULL_TREE)
1238 {
1239 if (specs == NULL_TREE)
1240 specs = s = t;
1241 else
1242 {
1243 TREE_CHAIN (s) = t;
1244 s = t;
1245 }
1246 }
1247 else
1248 {
1249 if (attrs == NULL_TREE)
1250 attrs = a = TREE_PURPOSE (t);
1251 else
1252 {
1253 TREE_CHAIN (a) = TREE_PURPOSE (t);
1254 a = TREE_PURPOSE (t);
1255 }
1256 /* More attrs can be linked here, move A to the end. */
1257 while (TREE_CHAIN (a) != NULL_TREE)
1258 a = TREE_CHAIN (a);
1259 }
1260 }
1261
1262 /* Terminate the lists. */
1263 if (s != NULL_TREE)
1264 TREE_CHAIN (s) = NULL_TREE;
1265 if (a != NULL_TREE)
1266 TREE_CHAIN (a) = NULL_TREE;
1267
1268 /* All done. */
1269 *declspecs = specs;
1270 *prefix_attributes = attrs;
1271}
1272
1273/* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1274 This function is used by the parser when a rule will accept attributes
1275 in a particular position, but we don't want to support that just yet.
1276
1277 A warning is issued for every ignored attribute. */
1278
1279tree
1280strip_attrs (specs_attrs)
1281 tree specs_attrs;
1282{
1283 tree specs, attrs;
1284
1285 split_specs_attrs (specs_attrs, &specs, &attrs);
1286
1287 while (attrs)
1288 {
1289 warning ("`%s' attribute ignored",
1290 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1291 attrs = TREE_CHAIN (attrs);
1292 }
1293
1294 return specs;
1295}
1296\f
1297/* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
1298 a parameter list. */
1299
1300/* The meaningfully distinct length modifiers for format checking recognised
1301 by GCC. */
1302enum format_lengths
1303{
1304 FMT_LEN_none,
1305 FMT_LEN_hh,
1306 FMT_LEN_h,
1307 FMT_LEN_l,
1308 FMT_LEN_ll,
1309 FMT_LEN_L,
1310 FMT_LEN_z,
1311 FMT_LEN_t,
1312 FMT_LEN_j,
1313 FMT_LEN_MAX
1314};
1315
1316
1317/* The standard versions in which various format features appeared. */
1318enum format_std_version
1319{
1320 STD_C89,
1321 STD_C94,
1322 STD_C9L, /* C99, but treat as C89 if -Wno-long-long. */
1323 STD_C99,
1324 STD_EXT
1325};
1326
1327/* The C standard version C++ is treated as equivalent to
1328 or inheriting from, for the purpose of format features supported. */
1329#define CPLUSPLUS_STD_VER STD_C89
1330/* The C standard version we are checking formats against when pedantic. */
1331#define C_STD_VER (c_language == clk_cplusplus \
1332 ? CPLUSPLUS_STD_VER \
1333 : (flag_isoc99 \
1334 ? STD_C99 \
1335 : (flag_isoc94 ? STD_C94 : STD_C89)))
1336/* The name to give to the standard version we are warning about when
1337 pedantic. FEATURE_VER is the version in which the feature warned out
1338 appeared, which is higher than C_STD_VER. */
1339#define C_STD_NAME(FEATURE_VER) (c_language == clk_cplusplus \
1340 ? "ISO C++" \
1341 : ((FEATURE_VER) == STD_EXT \
1342 ? "ISO C" \
1343 : "ISO C89"))
1344/* Adjust a C standard version, which may be STD_C9L, to account for
1345 -Wno-long-long. Returns other standard versions unchanged. */
1346#define ADJ_STD(VER) ((int)((VER) == STD_C9L \
1347 ? (warn_long_long ? STD_C99 : STD_C89) \
1348 : (VER)))
1349
1350/* Flags that may apply to a particular kind of format checked by GCC. */
1351enum
1352{
1353 /* This format converts arguments of types determined by the
1354 format string. */
1355 FMT_FLAG_ARG_CONVERT = 1,
1356 /* The scanf allocation 'a' kludge applies to this format kind. */
1357 FMT_FLAG_SCANF_A_KLUDGE = 2,
1358 /* A % during parsing a specifier is allowed to be a modified % rather
1359 that indicating the format is broken and we are out-of-sync. */
1360 FMT_FLAG_FANCY_PERCENT_OK = 4,
1361 /* With $ operand numbers, it is OK to reference the same argument more
1362 than once. */
1363 FMT_FLAG_DOLLAR_MULTIPLE = 8,
1364 /* This format type uses $ operand numbers (strfmon doesn't). */
1365 FMT_FLAG_USE_DOLLAR = 16,
1366 /* Zero width is bad in this type of format (scanf). */
1367 FMT_FLAG_ZERO_WIDTH_BAD = 32,
1368 /* Empty precision specification is OK in this type of format (printf). */
1369 FMT_FLAG_EMPTY_PREC_OK = 64
1370 /* Not included here: details of whether width or precision may occur
1371 (controlled by width_char and precision_char); details of whether
1372 '*' can be used for these (width_type and precision_type); details
1373 of whether length modifiers can occur (length_char_specs). */
1374};
1375
1376
1377/* Structure describing a length modifier supported in format checking, and
1378 possibly a doubled version such as "hh". */
1379typedef struct
1380{
1381 /* Name of the single-character length modifier. */
1382 const char *name;
1383 /* Index into a format_char_info.types array. */
1384 enum format_lengths index;
1385 /* Standard version this length appears in. */
1386 enum format_std_version std;
1387 /* Same, if the modifier can be repeated, or NULL if it can't. */
1388 const char *double_name;
1389 enum format_lengths double_index;
1390 enum format_std_version double_std;
1391} format_length_info;
1392
1393
1394/* Structure desribing the combination of a conversion specifier
1395 (or a set of specifiers which act identically) and a length modifier. */
1396typedef struct
1397{
1398 /* The standard version this combination of length and type appeared in.
1399 This is only relevant if greater than those for length and type
1400 individually; otherwise it is ignored. */
1401 enum format_std_version std;
1402 /* The name to use for the type, if different from that generated internally
1403 (e.g., "signed size_t"). */
1404 const char *name;
1405 /* The type itself. */
1406 tree *type;
1407} format_type_detail;
1408
1409
1410/* Macros to fill out tables of these. */
1411#define BADLEN { 0, NULL, NULL }
1412#define NOLENGTHS { BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }
1413
1414
1415/* Structure desribing a format conversion specifier (or a set of specifiers
1416 which act identically), and the length modifiers used with it. */
1417typedef struct
1418{
1419 const char *format_chars;
1420 int pointer_count;
1421 enum format_std_version std;
1422 /* Types accepted for each length modifier. */
1423 format_type_detail types[FMT_LEN_MAX];
1424 /* List of other modifier characters allowed with these specifiers.
1425 This lists flags, and additionally "w" for width, "p" for precision
1426 (right precision, for strfmon), "#" for left precision (strfmon),
1427 "a" for scanf "a" allocation extension (not applicable in C99 mode),
1428 "*" for scanf suppression, and "E" and "O" for those strftime
1429 modifiers. */
1430 const char *flag_chars;
1431 /* List of additional flags describing these conversion specifiers.
1432 "c" for generic character pointers being allowed, "2" for strftime
1433 two digit year formats, "3" for strftime formats giving two digit
1434 years in some locales, "4" for "2" which becomes "3" with an "E" modifier,
1435 "o" if use of strftime "O" is a GNU extension beyond C99,
1436 "W" if the argument is a pointer which is dereferenced and written into,
1437 "R" if the argument is a pointer which is dereferenced and read from,
1438 "i" for printf integer formats where the '0' flag is ignored with
1439 precision, and "[" for the starting character of a scanf scanset. */
1440 const char *flags2;
1441} format_char_info;
1442
1443
1444/* Structure describing a flag accepted by some kind of format. */
1445typedef struct
1446{
1447 /* The flag character in question (0 for end of array). */
1448 int flag_char;
1449 /* Zero if this entry describes the flag character in general, or a
1450 non-zero character that may be found in flags2 if it describes the
1451 flag when used with certain formats only. If the latter, only
1452 the first such entry found that applies to the current conversion
1453 specifier is used; the values of `name' and `long_name' it supplies
1454 will be used, if non-NULL and the standard version is higher than
1455 the unpredicated one, for any pedantic warning. For example, 'o'
1456 for strftime formats (meaning 'O' is an extension over C99). */
1457 int predicate;
1458 /* Nonzero if the next character after this flag in the format should
1459 be skipped ('=' in strfmon), zero otherwise. */
1460 int skip_next_char;
1461 /* The name to use for this flag in diagnostic messages. For example,
1462 N_("`0' flag"), N_("field width"). */
1463 const char *name;
1464 /* Long name for this flag in diagnostic messages; currently only used for
1465 "ISO C does not support ...". For example, N_("the `I' printf flag"). */
1466 const char *long_name;
1467 /* The standard version in which it appeared. */
1468 enum format_std_version std;
1469} format_flag_spec;
1470
1471
1472/* Structure describing a combination of flags that is bad for some kind
1473 of format. */
1474typedef struct
1475{
1476 /* The first flag character in question (0 for end of array). */
1477 int flag_char1;
1478 /* The second flag character. */
1479 int flag_char2;
1480 /* Non-zero if the message should say that the first flag is ignored with
1481 the second, zero if the combination should simply be objected to. */
1482 int ignored;
1483 /* Zero if this entry applies whenever this flag combination occurs,
1484 a non-zero character from flags2 if it only applies in some
1485 circumstances (e.g. 'i' for printf formats ignoring 0 with precision). */
1486 int predicate;
1487} format_flag_pair;
1488
1489
1490/* Structure describing a particular kind of format processed by GCC. */
1491typedef struct
1492{
1493 /* The name of this kind of format, for use in diagnostics. */
1494 const char *name;
1495 /* Specifications of the length modifiers accepted; possibly NULL. */
1496 const format_length_info *length_char_specs;
1497 /* Details of the conversion specification characters accepted. */
1498 const format_char_info *conversion_specs;
1499 /* String listing the flag characters that are accepted. */
1500 const char *flag_chars;
1501 /* String listing modifier characters (strftime) accepted. May be NULL. */
1502 const char *modifier_chars;
1503 /* Details of the flag characters, including pseudo-flags. */
1504 const format_flag_spec *flag_specs;
1505 /* Details of bad combinations of flags. */
1506 const format_flag_pair *bad_flag_pairs;
1507 /* Flags applicable to this kind of format. */
1508 int flags;
1509 /* Flag character to treat a width as, or 0 if width not used. */
1510 int width_char;
1511 /* Flag character to treat a left precision (strfmon) as,
1512 or 0 if left precision not used. */
1513 int left_precision_char;
1514 /* Flag character to treat a precision (for strfmon, right precision) as,
1515 or 0 if precision not used. */
1516 int precision_char;
1517 /* If a flag character has the effect of suppressing the conversion of
1518 an argument ('*' in scanf), that flag character, otherwise 0. */
1519 int suppression_char;
1520 /* Flag character to treat a length modifier as (ignored if length
1521 modifiers not used). Need not be placed in flag_chars for conversion
1522 specifiers, but is used to check for bad combinations such as length
1523 modifier with assignment suppression in scanf. */
1524 int length_code_char;
1525 /* Pointer to type of argument expected if '*' is used for a width,
1526 or NULL if '*' not used for widths. */
1527 tree *width_type;
1528 /* Pointer to type of argument expected if '*' is used for a precision,
1529 or NULL if '*' not used for precisions. */
1530 tree *precision_type;
1531} format_kind_info;
1532
1533
1534/* Structure describing details of a type expected in format checking,
1535 and the type to check against it. */
1536typedef struct format_wanted_type
1537{
1538 /* The type wanted. */
1539 tree wanted_type;
1540 /* The name of this type to use in diagnostics. */
1541 const char *wanted_type_name;
1542 /* The level of indirection through pointers at which this type occurs. */
1543 int pointer_count;
1544 /* Whether, when pointer_count is 1, to allow any character type when
1545 pedantic, rather than just the character or void type specified. */
1546 int char_lenient_flag;
1547 /* Whether the argument, dereferenced once, is written into and so the
1548 argument must not be a pointer to a const-qualified type. */
1549 int writing_in_flag;
1550 /* Whether the argument, dereferenced once, is read from and so
1551 must not be a NULL pointer. */
1552 int reading_from_flag;
1553 /* If warnings should be of the form "field precision is not type int",
1554 the name to use (in this case "field precision"), otherwise NULL,
1555 for "%s format, %s arg" type messages. If (in an extension), this
1556 is a pointer type, wanted_type_name should be set to include the
1557 terminating '*' characters of the type name to give a correct
1558 message. */
1559 const char *name;
1560 /* The actual parameter to check against the wanted type. */
1561 tree param;
1562 /* The argument number of that parameter. */
1563 int arg_num;
1564 /* The next type to check for this format conversion, or NULL if none. */
1565 struct format_wanted_type *next;
1566} format_wanted_type;
1567
1568
1569static const format_length_info printf_length_specs[] =
1570{
1571 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
1572 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L },
1573 { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
1574 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
1575 { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
1576 { "Z", FMT_LEN_z, STD_EXT, NULL, 0, 0 },
1577 { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
1578 { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
1579 { NULL, 0, 0, NULL, 0, 0 }
1580};
1581
1582
1583/* This differs from printf_length_specs only in that "Z" is not accepted. */
1584static const format_length_info scanf_length_specs[] =
1585{
1586 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
1587 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L },
1588 { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
1589 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
1590 { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
1591 { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
1592 { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
1593 { NULL, 0, 0, NULL, 0, 0 }
1594};
1595
1596
1597/* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings
1598 make no sense for a format type not part of any C standard version. */
1599static const format_length_info strfmon_length_specs[] =
1600{
1601 /* A GNU extension. */
1602 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
1603 { NULL, 0, 0, NULL, 0, 0 }
1604};
1605
1606static const format_flag_spec printf_flag_specs[] =
1607{
1608 { ' ', 0, 0, N_("` ' flag"), N_("the ` ' printf flag"), STD_C89 },
1609 { '+', 0, 0, N_("`+' flag"), N_("the `+' printf flag"), STD_C89 },
1610 { '#', 0, 0, N_("`#' flag"), N_("the `#' printf flag"), STD_C89 },
1611 { '0', 0, 0, N_("`0' flag"), N_("the `0' printf flag"), STD_C89 },
1612 { '-', 0, 0, N_("`-' flag"), N_("the `-' printf flag"), STD_C89 },
1613 { '\'', 0, 0, N_("`'' flag"), N_("the `'' printf flag"), STD_EXT },
1614 { 'I', 0, 0, N_("`I' flag"), N_("the `I' printf flag"), STD_EXT },
1615 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
1616 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
1617 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
1618 { 0, 0, 0, NULL, NULL, 0 }
1619};
1620
1621
1622static const format_flag_pair printf_flag_pairs[] =
1623{
1624 { ' ', '+', 1, 0 },
1625 { '0', '-', 1, 0 },
1626 { '0', 'p', 1, 'i' },
1627 { 0, 0, 0, 0 }
1628};
1629
1630
1631static const format_flag_spec scanf_flag_specs[] =
1632{
1633 { '*', 0, 0, N_("assignment suppression"), N_("assignment suppression"), STD_C89 },
1634 { 'a', 0, 0, N_("`a' flag"), N_("the `a' scanf flag"), STD_EXT },
1635 { 'w', 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89 },
1636 { 'L', 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89 },
1637 { '\'', 0, 0, N_("`'' flag"), N_("the `'' scanf flag"), STD_EXT },
1638 { 'I', 0, 0, N_("`I' flag"), N_("the `I' scanf flag"), STD_EXT },
1639 { 0, 0, 0, NULL, NULL, 0 }
1640};
1641
1642
1643static const format_flag_pair scanf_flag_pairs[] =
1644{
1645 { '*', 'L', 0, 0 },
1646 { 0, 0, 0, 0 }
1647};
1648
1649
1650static const format_flag_spec strftime_flag_specs[] =
1651{
1652 { '_', 0, 0, N_("`_' flag"), N_("the `_' strftime flag"), STD_EXT },
1653 { '-', 0, 0, N_("`-' flag"), N_("the `-' strftime flag"), STD_EXT },
1654 { '0', 0, 0, N_("`0' flag"), N_("the `0' strftime flag"), STD_EXT },
1655 { '^', 0, 0, N_("`^' flag"), N_("the `^' strftime flag"), STD_EXT },
1656 { '#', 0, 0, N_("`#' flag"), N_("the `#' strftime flag"), STD_EXT },
1657 { 'w', 0, 0, N_("field width"), N_("field width in strftime format"), STD_EXT },
1658 { 'E', 0, 0, N_("`E' modifier"), N_("the `E' strftime modifier"), STD_C99 },
1659 { 'O', 0, 0, N_("`O' modifier"), N_("the `O' strftime modifier"), STD_C99 },
1660 { 'O', 'o', 0, NULL, N_("the `O' modifier"), STD_EXT },
1661 { 0, 0, 0, NULL, NULL, 0 }
1662};
1663
1664
1665static const format_flag_pair strftime_flag_pairs[] =
1666{
1667 { 'E', 'O', 0, 0 },
1668 { '_', '-', 0, 0 },
1669 { '_', '0', 0, 0 },
1670 { '-', '0', 0, 0 },
1671 { '^', '#', 0, 0 },
1672 { 0, 0, 0, 0 }
1673};
1674
1675
1676static const format_flag_spec strfmon_flag_specs[] =
1677{
1678 { '=', 0, 1, N_("fill character"), N_("fill character in strfmon format"), STD_C89 },
1679 { '^', 0, 0, N_("`^' flag"), N_("the `^' strfmon flag"), STD_C89 },
1680 { '+', 0, 0, N_("`+' flag"), N_("the `+' strfmon flag"), STD_C89 },
1681 { '(', 0, 0, N_("`(' flag"), N_("the `(' strfmon flag"), STD_C89 },
1682 { '!', 0, 0, N_("`!' flag"), N_("the `!' strfmon flag"), STD_C89 },
1683 { '-', 0, 0, N_("`-' flag"), N_("the `-' strfmon flag"), STD_C89 },
1684 { 'w', 0, 0, N_("field width"), N_("field width in strfmon format"), STD_C89 },
1685 { '#', 0, 0, N_("left precision"), N_("left precision in strfmon format"), STD_C89 },
1686 { 'p', 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89 },
1687 { 'L', 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89 },
1688 { 0, 0, 0, NULL, NULL, 0 }
1689};
1690
1691static const format_flag_pair strfmon_flag_pairs[] =
1692{
1693 { '+', '(', 0, 0 },
1694 { 0, 0, 0, 0 }
1695};
1696
1697
1698#define T_I &integer_type_node
1699#define T89_I { STD_C89, NULL, T_I }
1700#define T99_I { STD_C99, NULL, T_I }
1701#define T_L &long_integer_type_node
1702#define T89_L { STD_C89, NULL, T_L }
1703#define T_LL &long_long_integer_type_node
1704#define T9L_LL { STD_C9L, NULL, T_LL }
1705#define TEX_LL { STD_EXT, NULL, T_LL }
1706#define T_S &short_integer_type_node
1707#define T89_S { STD_C89, NULL, T_S }
1708#define T_UI &unsigned_type_node
1709#define T89_UI { STD_C89, NULL, T_UI }
1710#define T99_UI { STD_C99, NULL, T_UI }
1711#define T_UL &long_unsigned_type_node
1712#define T89_UL { STD_C89, NULL, T_UL }
1713#define T_ULL &long_long_unsigned_type_node
1714#define T9L_ULL { STD_C9L, NULL, T_ULL }
1715#define TEX_ULL { STD_EXT, NULL, T_ULL }
1716#define T_US &short_unsigned_type_node
1717#define T89_US { STD_C89, NULL, T_US }
1718#define T_F &float_type_node
1719#define T89_F { STD_C89, NULL, T_F }
1720#define T99_F { STD_C99, NULL, T_F }
1721#define T_D &double_type_node
1722#define T89_D { STD_C89, NULL, T_D }
1723#define T99_D { STD_C99, NULL, T_D }
1724#define T_LD &long_double_type_node
1725#define T89_LD { STD_C89, NULL, T_LD }
1726#define T99_LD { STD_C99, NULL, T_LD }
1727#define T_C &char_type_node
1728#define T89_C { STD_C89, NULL, T_C }
1729#define T_SC &signed_char_type_node
1730#define T99_SC { STD_C99, NULL, T_SC }
1731#define T_UC &unsigned_char_type_node
1732#define T99_UC { STD_C99, NULL, T_UC }
1733#define T_V &void_type_node
1734#define T89_V { STD_C89, NULL, T_V }
1735#define T_W &wchar_type_node
1736#define T94_W { STD_C94, "wchar_t", T_W }
1737#define TEX_W { STD_EXT, "wchar_t", T_W }
1738#define T_WI &wint_type_node
1739#define T94_WI { STD_C94, "wint_t", T_WI }
1740#define TEX_WI { STD_EXT, "wint_t", T_WI }
1741#define T_ST &c_size_type_node
1742#define T99_ST { STD_C99, "size_t", T_ST }
1743#define T_SST &signed_size_type_node
1744#define T99_SST { STD_C99, "signed size_t", T_SST }
1745#define T_PD &ptrdiff_type_node
1746#define T99_PD { STD_C99, "ptrdiff_t", T_PD }
1747#define T_UPD &unsigned_ptrdiff_type_node
1748#define T99_UPD { STD_C99, "unsigned ptrdiff_t", T_UPD }
1749#define T_IM &intmax_type_node
1750#define T99_IM { STD_C99, "intmax_t", T_IM }
1751#define T_UIM &uintmax_type_node
1752#define T99_UIM { STD_C99, "uintmax_t", T_UIM }
1753
1754static const format_char_info print_char_table[] =
1755{
1756 /* C89 conversion specifiers. */
1757 { "di", 0, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, TEX_LL, T99_SST, T99_PD, T99_IM }, "-wp0 +'I", "i" },
1758 { "oxX", 0, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "-wp0#", "i" },
1759 { "u", 0, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "-wp0'I", "i" },
1760 { "fgG", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#'", "" },
1761 { "eE", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "" },
1762 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, T94_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "" },
1763 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR" },
1764 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "c" },
1765 { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, BADLEN, T99_SST, T99_PD, T99_IM }, "", "W" },
1766 /* C99 conversion specifiers. */
1767 { "F", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#'", "" },
1768 { "aA", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "" },
1769 /* X/Open conversion specifiers. */
1770 { "C", 0, STD_EXT, { TEX_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "" },
1771 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "R" },
1772 /* GNU conversion specifiers. */
1773 { "m", 0, STD_EXT, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "" },
1774 { NULL, 0, 0, NOLENGTHS, NULL, NULL }
1775};
1776
1777static const format_char_info scan_char_table[] =
1778{
1779 /* C89 conversion specifiers. */
1780 { "di", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, TEX_LL, T99_SST, T99_PD, T99_IM }, "*w'I", "W" },
1781 { "u", 1, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "*w'I", "W" },
1782 { "oxX", 1, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "*w", "W" },
1783 { "efgEG", 1, STD_C89, { T89_F, BADLEN, BADLEN, T89_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "*w'", "W" },
1784 { "c", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "cW" },
1785 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "cW" },
1786 { "[", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "cW[" },
1787 { "p", 2, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W" },
1788 { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, BADLEN, T99_SST, T99_PD, T99_IM }, "", "W" },
1789 /* C99 conversion specifiers. */
1790 { "FaA", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "*w'", "W" },
1791 /* X/Open conversion specifiers. */
1792 { "C", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W" },
1793 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "W" },
1794 { NULL, 0, 0, NOLENGTHS, NULL, NULL }
1795};
1796
1797static const format_char_info time_char_table[] =
1798{
1799 /* C89 conversion specifiers. */
1800 { "ABZab", 0, STD_C89, NOLENGTHS, "^#", "" },
1801 { "cx", 0, STD_C89, NOLENGTHS, "E", "3" },
1802 { "HIMSUWdmw", 0, STD_C89, NOLENGTHS, "-_0Ow", "" },
1803 { "j", 0, STD_C89, NOLENGTHS, "-_0Ow", "o" },
1804 { "p", 0, STD_C89, NOLENGTHS, "#", "" },
1805 { "X", 0, STD_C89, NOLENGTHS, "E", "" },
1806 { "y", 0, STD_C89, NOLENGTHS, "EO-_0w", "4" },
1807 { "Y", 0, STD_C89, NOLENGTHS, "-_0EOw", "o" },
1808 { "%", 0, STD_C89, NOLENGTHS, "", "" },
1809 /* C99 conversion specifiers. */
1810 { "C", 0, STD_C99, NOLENGTHS, "-_0EOw", "o" },
1811 { "D", 0, STD_C99, NOLENGTHS, "", "2" },
1812 { "eVu", 0, STD_C99, NOLENGTHS, "-_0Ow", "" },
1813 { "FRTnrt", 0, STD_C99, NOLENGTHS, "", "" },
1814 { "g", 0, STD_C99, NOLENGTHS, "O-_0w", "2o" },
1815 { "G", 0, STD_C99, NOLENGTHS, "-_0Ow", "o" },
1816 { "h", 0, STD_C99, NOLENGTHS, "^#", "" },
1817 { "z", 0, STD_C99, NOLENGTHS, "O", "o" },
1818 /* GNU conversion specifiers. */
1819 { "kls", 0, STD_EXT, NOLENGTHS, "-_0Ow", "" },
1820 { "P", 0, STD_EXT, NOLENGTHS, "", "" },
1821 { NULL, 0, 0, NOLENGTHS, NULL, NULL }
1822};
1823
1824static const format_char_info monetary_char_table[] =
1825{
1826 { "in", 0, STD_C89, { T89_D, BADLEN, BADLEN, BADLEN, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "=^+(!-w#p", "" },
1827 { NULL, 0, 0, NOLENGTHS, NULL, NULL }
1828};
1829
1830
1831/* This must be in the same order as enum format_type. */
1832static const format_kind_info format_types[] =
1833{
1834 { "printf", printf_length_specs, print_char_table, " +#0-'I", NULL,
1835 printf_flag_specs, printf_flag_pairs,
1836 FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_EMPTY_PREC_OK,
1837 'w', 0, 'p', 0, 'L',
1838 &integer_type_node, &integer_type_node
1839 },
1840 { "scanf", scanf_length_specs, scan_char_table, "*'I", NULL,
1841 scanf_flag_specs, scanf_flag_pairs,
1842 FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_ZERO_WIDTH_BAD,
1843 'w', 0, 0, '*', 'L',
1844 NULL, NULL
1845 },
1846 { "strftime", NULL, time_char_table, "_-0^#", "EO",
1847 strftime_flag_specs, strftime_flag_pairs,
1848 FMT_FLAG_FANCY_PERCENT_OK, 'w', 0, 0, 0, 0,
1849 NULL, NULL
1850 },
1851 { "strfmon", strfmon_length_specs, monetary_char_table, "=^+(!-", NULL,
1852 strfmon_flag_specs, strfmon_flag_pairs,
1853 FMT_FLAG_ARG_CONVERT, 'w', '#', 'p', 0, 'L',
1854 NULL, NULL
1855 }
1856};
1857
1858
1859typedef struct function_format_info
1860{
1861 struct function_format_info *next; /* next structure on the list */
1862 tree name; /* identifier such as "printf" */
1863 tree assembler_name; /* optional mangled identifier (for C++) */
1864 enum format_type format_type; /* type of format (printf, scanf, etc.) */
1865 int format_num; /* number of format argument */
1866 int first_arg_num; /* number of first arg (zero for varargs) */
1867} function_format_info;
1868
1869static function_format_info *function_format_list = NULL;
1870
1871typedef struct international_format_info
1872{
1873 struct international_format_info *next; /* next structure on the list */
1874 tree name; /* identifier such as "gettext" */
1875 tree assembler_name; /* optional mangled identifier (for C++) */
1876 int format_num; /* number of format argument */
1877} international_format_info;
1878
1879static international_format_info *international_format_list = NULL;
1880
1881/* Structure detailing the results of checking a format function call
1882 where the format expression may be a conditional expression with
1883 many leaves resulting from nested conditional expressions. */
1884typedef struct
1885{
1886 /* Number of leaves of the format argument that could not be checked
1887 as they were not string literals. */
1888 int number_non_literal;
1889 /* Number of leaves of the format argument that were null pointers or
1890 string literals, but had extra format arguments. */
1891 int number_extra_args;
1892 /* Number of leaves of the format argument that were null pointers or
1893 string literals, but had extra format arguments and used $ operand
1894 numbers. */
1895 int number_dollar_extra_args;
1896 /* Number of leaves of the format argument that were wide string
1897 literals. */
1898 int number_wide;
1899 /* Number of leaves of the format argument that were empty strings. */
1900 int number_empty;
1901 /* Number of leaves of the format argument that were unterminated
1902 strings. */
1903 int number_unterminated;
1904 /* Number of leaves of the format argument that were not counted above. */
1905 int number_other;
1906} format_check_results;
1907
1908static void check_format_info PARAMS ((int *, function_format_info *, tree));
1909static void check_format_info_recurse PARAMS ((int *, format_check_results *,
1910 function_format_info *, tree,
1911 tree, int));
1912static void check_format_info_main PARAMS ((int *, format_check_results *,
1913 function_format_info *,
1914 const char *, int, tree, int));
1915static void status_warning PARAMS ((int *, const char *, ...))
1916 ATTRIBUTE_PRINTF_2;
1917
1918static void init_dollar_format_checking PARAMS ((int, tree));
1919static int maybe_read_dollar_number PARAMS ((int *, const char **, int,
1920 tree, tree *,
1921 const format_kind_info *));
1922static void finish_dollar_format_checking PARAMS ((int *, format_check_results *));
1923
1924static const format_flag_spec *get_flag_spec PARAMS ((const format_flag_spec *,
1925 int, const char *));
1926
1927static void check_format_types PARAMS ((int *, format_wanted_type *));
1928static int is_valid_printf_arglist PARAMS ((tree));
1929static rtx c_expand_builtin PARAMS ((tree, rtx, enum machine_mode, enum expand_modifier));
1930static rtx c_expand_builtin_printf PARAMS ((tree, rtx, enum machine_mode,
1931 enum expand_modifier, int));
1932
1933/* Initialize the table of functions to perform format checking on.
1934 The ISO C functions are always checked (whether <stdio.h> is
1935 included or not), since it is common to call printf without
1936 including <stdio.h>. There shouldn't be a problem with this,
1937 since ISO C reserves these function names whether you include the
1938 header file or not. In any case, the checking is harmless. With
1939 -ffreestanding, these default attributes are disabled, and must be
1940 specified manually if desired.
1941
1942 Also initialize the name of function that modify the format string for
1943 internationalization purposes. */
1944
1945void
1946init_function_format_info ()
1947{
1948 if (flag_hosted)
1949 {
1950 /* Functions from ISO/IEC 9899:1990. */
1951 record_function_format (get_identifier ("printf"), NULL_TREE,
1952 printf_format_type, 1, 2);
1953 record_function_format (get_identifier ("__builtin_printf"), NULL_TREE,
1954 printf_format_type, 1, 2);
1955 record_function_format (get_identifier ("fprintf"), NULL_TREE,
1956 printf_format_type, 2, 3);
1957 record_function_format (get_identifier ("sprintf"), NULL_TREE,
1958 printf_format_type, 2, 3);
1959 record_function_format (get_identifier ("scanf"), NULL_TREE,
1960 scanf_format_type, 1, 2);
1961 record_function_format (get_identifier ("fscanf"), NULL_TREE,
1962 scanf_format_type, 2, 3);
1963 record_function_format (get_identifier ("sscanf"), NULL_TREE,
1964 scanf_format_type, 2, 3);
1965 record_function_format (get_identifier ("vprintf"), NULL_TREE,
1966 printf_format_type, 1, 0);
1967 record_function_format (get_identifier ("vfprintf"), NULL_TREE,
1968 printf_format_type, 2, 0);
1969 record_function_format (get_identifier ("vsprintf"), NULL_TREE,
1970 printf_format_type, 2, 0);
1971 record_function_format (get_identifier ("strftime"), NULL_TREE,
1972 strftime_format_type, 3, 0);
1973 }
1974
1975 if (flag_hosted && flag_isoc99)
1976 {
1977 /* ISO C99 adds the snprintf and vscanf family functions. */
1978 record_function_format (get_identifier ("snprintf"), NULL_TREE,
1979 printf_format_type, 3, 4);
1980 record_function_format (get_identifier ("vsnprintf"), NULL_TREE,
1981 printf_format_type, 3, 0);
1982 record_function_format (get_identifier ("vscanf"), NULL_TREE,
1983 scanf_format_type, 1, 0);
1984 record_function_format (get_identifier ("vfscanf"), NULL_TREE,
1985 scanf_format_type, 2, 0);
1986 record_function_format (get_identifier ("vsscanf"), NULL_TREE,
1987 scanf_format_type, 2, 0);
1988 }
1989
1990 if (flag_hosted && flag_noniso_default_format_attributes)
1991 {
1992 /* Uniforum/GNU gettext functions, not in ISO C. */
1993 record_international_format (get_identifier ("gettext"), NULL_TREE, 1);
1994 record_international_format (get_identifier ("dgettext"), NULL_TREE, 2);
1995 record_international_format (get_identifier ("dcgettext"), NULL_TREE, 2);
1996 /* X/Open strfmon function. */
1997 record_function_format (get_identifier ("strfmon"), NULL_TREE,
1998 strfmon_format_type, 3, 4);
1999 }
2000}
2001
2002/* Record information for argument format checking. FUNCTION_IDENT is
2003 the identifier node for the name of the function to check (its decl
2004 need not exist yet).
2005 FORMAT_TYPE specifies the type of format checking. FORMAT_NUM is the number
2006 of the argument which is the format control string (starting from 1).
2007 FIRST_ARG_NUM is the number of the first actual argument to check
2008 against the format string, or zero if no checking is not be done
2009 (e.g. for varargs such as vfprintf). */
2010
2011static void
2012record_function_format (name, assembler_name, format_type,
2013 format_num, first_arg_num)
2014 tree name;
2015 tree assembler_name;
2016 enum format_type format_type;
2017 int format_num;
2018 int first_arg_num;
2019{
2020 function_format_info *info;
2021
2022 /* Re-use existing structure if it's there. */
2023
2024 for (info = function_format_list; info; info = info->next)
2025 {
2026 if (info->name == name && info->assembler_name == assembler_name)
2027 break;
2028 }
2029 if (! info)
2030 {
2031 info = (function_format_info *) xmalloc (sizeof (function_format_info));
2032 info->next = function_format_list;
2033 function_format_list = info;
2034
2035 info->name = name;
2036 info->assembler_name = assembler_name;
2037 }
2038
2039 info->format_type = format_type;
2040 info->format_num = format_num;
2041 info->first_arg_num = first_arg_num;
2042}
2043
2044/* Record information for the names of function that modify the format
2045 argument to format functions. FUNCTION_IDENT is the identifier node for
2046 the name of the function (its decl need not exist yet) and FORMAT_NUM is
2047 the number of the argument which is the format control string (starting
2048 from 1). */
2049
2050static void
2051record_international_format (name, assembler_name, format_num)
2052 tree name;
2053 tree assembler_name;
2054 int format_num;
2055{
2056 international_format_info *info;
2057
2058 /* Re-use existing structure if it's there. */
2059
2060 for (info = international_format_list; info; info = info->next)
2061 {
2062 if (info->name == name && info->assembler_name == assembler_name)
2063 break;
2064 }
2065
2066 if (! info)
2067 {
2068 info
2069 = (international_format_info *)
2070 xmalloc (sizeof (international_format_info));
2071 info->next = international_format_list;
2072 international_format_list = info;
2073
2074 info->name = name;
2075 info->assembler_name = assembler_name;
2076 }
2077
2078 info->format_num = format_num;
2079}
2080\f
2081/* Check the argument list of a call to printf, scanf, etc.
2082 NAME is the function identifier.
2083 ASSEMBLER_NAME is the function's assembler identifier.
2084 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
2085 PARAMS is the list of argument values. Also, if -Wmissing-format-attribute,
2086 warn for calls to vprintf or vscanf in functions with no such format
2087 attribute themselves. */
2088
2089void
2090check_function_format (status, name, assembler_name, params)
2091 int *status;
2092 tree name;
2093 tree assembler_name;
2094 tree params;
2095{
2096 function_format_info *info;
2097
2098 /* See if this function is a format function. */
2099 for (info = function_format_list; info; info = info->next)
2100 {
2101 if (info->assembler_name
2102 ? (info->assembler_name == assembler_name)
2103 : (info->name == name))
2104 {
2105 /* Yup; check it. */
2106 check_format_info (status, info, params);
2107 if (warn_missing_format_attribute && info->first_arg_num == 0
2108 && (format_types[info->format_type].flags & FMT_FLAG_ARG_CONVERT))
2109 {
2110 function_format_info *info2;
2111 for (info2 = function_format_list; info2; info2 = info2->next)
2112 if ((info2->assembler_name
2113 ? (info2->assembler_name == DECL_ASSEMBLER_NAME (current_function_decl))
2114 : (info2->name == DECL_NAME (current_function_decl)))
2115 && info2->format_type == info->format_type)
2116 break;
2117 if (info2 == NULL)
2118 {
2119 /* Check if the current function has a parameter to which
2120 the format attribute could be attached; if not, it
2121 can't be a candidate for a format attribute, despite
2122 the vprintf-like or vscanf-like call. */
2123 tree args;
2124 for (args = DECL_ARGUMENTS (current_function_decl);
2125 args != 0;
2126 args = TREE_CHAIN (args))
2127 {
2128 if (TREE_CODE (TREE_TYPE (args)) == POINTER_TYPE
2129 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args)))
2130 == char_type_node))
2131 break;
2132 }
2133 if (args != 0)
2134 warning ("function might be possible candidate for `%s' format attribute",
2135 format_types[info->format_type].name);
2136 }
2137 }
2138 break;
2139 }
2140 }
2141}
2142
2143/* This function replaces `warning' inside the printf format checking
2144 functions. If the `status' parameter is non-NULL, then it is
2145 dereferenced and set to 1 whenever a warning is caught. Otherwise
2146 it warns as usual by replicating the innards of the warning
2147 function from diagnostic.c. */
2148static void
2149status_warning VPARAMS ((int *status, const char *msgid, ...))
2150{
2151#ifndef ANSI_PROTOTYPES
2152 int *status;
2153 const char *msgid;
2154#endif
2155 va_list ap;
2156 diagnostic_context dc;
2157
2158 VA_START (ap, msgid);
2159
2160#ifndef ANSI_PROTOTYPES
2161 status = va_arg (ap, int *);
2162 msgid = va_arg (ap, const char *);
2163#endif
2164
2165 if (status)
2166 *status = 1;
2167 else
2168 {
2169 /* This duplicates the warning function behavior. */
2170 set_diagnostic_context
2171 (&dc, msgid, &ap, input_filename, lineno, /* warn = */ 1);
2172 report_diagnostic (&dc);
2173 }
2174
2175 va_end (ap);
2176}
2177
2178/* Variables used by the checking of $ operand number formats. */
2179static char *dollar_arguments_used = NULL;
2180static int dollar_arguments_alloc = 0;
2181static int dollar_arguments_count;
2182static int dollar_first_arg_num;
2183static int dollar_max_arg_used;
2184static int dollar_format_warned;
2185
2186/* Initialize the checking for a format string that may contain $
2187 parameter number specifications; we will need to keep track of whether
2188 each parameter has been used. FIRST_ARG_NUM is the number of the first
2189 argument that is a parameter to the format, or 0 for a vprintf-style
2190 function; PARAMS is the list of arguments starting at this argument. */
2191
2192static void
2193init_dollar_format_checking (first_arg_num, params)
2194 int first_arg_num;
2195 tree params;
2196{
2197 dollar_first_arg_num = first_arg_num;
2198 dollar_arguments_count = 0;
2199 dollar_max_arg_used = 0;
2200 dollar_format_warned = 0;
2201 if (first_arg_num > 0)
2202 {
2203 while (params)
2204 {
2205 dollar_arguments_count++;
2206 params = TREE_CHAIN (params);
2207 }
2208 }
2209 if (dollar_arguments_alloc < dollar_arguments_count)
2210 {
2211 if (dollar_arguments_used)
2212 free (dollar_arguments_used);
2213 dollar_arguments_alloc = dollar_arguments_count;
2214 dollar_arguments_used = xmalloc (dollar_arguments_alloc);
2215 }
2216 if (dollar_arguments_alloc)
2217 memset (dollar_arguments_used, 0, dollar_arguments_alloc);
2218}
2219
2220
2221/* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
2222 is set, it is an error if one is not found; otherwise, it is OK. If
2223 such a number is found, check whether it is within range and mark that
2224 numbered operand as being used for later checking. Returns the operand
2225 number if found and within range, zero if no such number was found and
2226 this is OK, or -1 on error. PARAMS points to the first operand of the
2227 format; PARAM_PTR is made to point to the parameter referred to. If
2228 a $ format is found, *FORMAT is updated to point just after it. */
2229
2230static int
2231maybe_read_dollar_number (status, format, dollar_needed, params, param_ptr,
2232 fki)
2233 int *status;
2234 const char **format;
2235 int dollar_needed;
2236 tree params;
2237 tree *param_ptr;
2238 const format_kind_info *fki;
2239{
2240 int argnum;
2241 int overflow_flag;
2242 const char *fcp = *format;
2243 if (*fcp < '0' || *fcp > '9')
2244 {
2245 if (dollar_needed)
2246 {
2247 status_warning (status, "missing $ operand number in format");
2248 return -1;
2249 }
2250 else
2251 return 0;
2252 }
2253 argnum = 0;
2254 overflow_flag = 0;
2255 while (*fcp >= '0' && *fcp <= '9')
2256 {
2257 int nargnum;
2258 nargnum = 10 * argnum + (*fcp - '0');
2259 if (nargnum < 0 || nargnum / 10 != argnum)
2260 overflow_flag = 1;
2261 argnum = nargnum;
2262 fcp++;
2263 }
2264 if (*fcp != '$')
2265 {
2266 if (dollar_needed)
2267 {
2268 status_warning (status, "missing $ operand number in format");
2269 return -1;
2270 }
2271 else
2272 return 0;
2273 }
2274 *format = fcp + 1;
2275 if (pedantic && !dollar_format_warned)
2276 {
2277 status_warning (status,
2278 "%s does not support %%n$ operand number formats",
2279 C_STD_NAME (STD_EXT));
2280 dollar_format_warned = 1;
2281 }
2282 if (overflow_flag || argnum == 0
2283 || (dollar_first_arg_num && argnum > dollar_arguments_count))
2284 {
2285 status_warning (status, "operand number out of range in format");
2286 return -1;
2287 }
2288 if (argnum > dollar_max_arg_used)
2289 dollar_max_arg_used = argnum;
2290 /* For vprintf-style functions we may need to allocate more memory to
2291 track which arguments are used. */
2292 while (dollar_arguments_alloc < dollar_max_arg_used)
2293 {
2294 int nalloc;
2295 nalloc = 2 * dollar_arguments_alloc + 16;
2296 dollar_arguments_used = xrealloc (dollar_arguments_used, nalloc);
2297 memset (dollar_arguments_used + dollar_arguments_alloc, 0,
2298 nalloc - dollar_arguments_alloc);
2299 dollar_arguments_alloc = nalloc;
2300 }
2301 if (!(fki->flags & FMT_FLAG_DOLLAR_MULTIPLE)
2302 && dollar_arguments_used[argnum - 1] == 1)
2303 {
2304 dollar_arguments_used[argnum - 1] = 2;
2305 status_warning (status,
2306 "format argument %d used more than once in %s format",
2307 argnum, fki->name);
2308 }
2309 else
2310 dollar_arguments_used[argnum - 1] = 1;
2311 if (dollar_first_arg_num)
2312 {
2313 int i;
2314 *param_ptr = params;
2315 for (i = 1; i < argnum && *param_ptr != 0; i++)
2316 *param_ptr = TREE_CHAIN (*param_ptr);
2317
2318 if (*param_ptr == 0)
2319 {
2320 /* This case shouldn't be caught here. */
2321 abort ();
2322 }
2323 }
2324 else
2325 *param_ptr = 0;
2326 return argnum;
2327}
2328
2329
2330/* Finish the checking for a format string that used $ operand number formats
2331 instead of non-$ formats. We check for unused operands before used ones
2332 (a serious error, since the implementation of the format function
2333 can't know what types to pass to va_arg to find the later arguments).
2334 and for unused operands at the end of the format (if we know how many
2335 arguments the format had, so not for vprintf). If there were operand
2336 numbers out of range on a non-vprintf-style format, we won't have reached
2337 here. */
2338
2339static void
2340finish_dollar_format_checking (status, res)
2341 int *status;
2342 format_check_results *res;
2343{
2344 int i;
2345 for (i = 0; i < dollar_max_arg_used; i++)
2346 {
2347 if (!dollar_arguments_used[i])
2348 status_warning (status, "format argument %d unused before used argument %d in $-style format",
2349 i + 1, dollar_max_arg_used);
2350 }
2351 if (dollar_first_arg_num && dollar_max_arg_used < dollar_arguments_count)
2352 {
2353 res->number_other--;
2354 res->number_dollar_extra_args++;
2355 }
2356}
2357
2358
2359/* Retrieve the specification for a format flag. SPEC contains the
2360 specifications for format flags for the applicable kind of format.
2361 FLAG is the flag in question. If PREDICATES is NULL, the basic
2362 spec for that flag must be retrieved and this function aborts if
2363 it cannot be found. If PREDICATES is not NULL, it is a string listing
2364 possible predicates for the spec entry; if an entry predicated on any
2365 of these is found, it is returned, otherwise NULL is returned. */
2366
2367static const format_flag_spec *
2368get_flag_spec (spec, flag, predicates)
2369 const format_flag_spec *spec;
2370 int flag;
2371 const char *predicates;
2372{
2373 int i;
2374 for (i = 0; spec[i].flag_char != 0; i++)
2375 {
2376 if (spec[i].flag_char != flag)
2377 continue;
2378 if (predicates != NULL)
2379 {
2380 if (spec[i].predicate != 0
2381 && strchr (predicates, spec[i].predicate) != 0)
2382 return &spec[i];
2383 }
2384 else if (spec[i].predicate == 0)
2385 return &spec[i];
2386 }
2387 if (predicates == NULL)
2388 abort ();
2389 else
2390 return NULL;
2391}
2392
2393
2394/* Check the argument list of a call to printf, scanf, etc.
2395 INFO points to the function_format_info structure.
2396 PARAMS is the list of argument values. */
2397
2398static void
2399check_format_info (status, info, params)
2400 int *status;
2401 function_format_info *info;
2402 tree params;
2403{
2404 int arg_num;
2405 tree format_tree;
2406 format_check_results res;
2407 /* Skip to format argument. If the argument isn't available, there's
2408 no work for us to do; prototype checking will catch the problem. */
2409 for (arg_num = 1; ; ++arg_num)
2410 {
2411 if (params == 0)
2412 return;
2413 if (arg_num == info->format_num)
2414 break;
2415 params = TREE_CHAIN (params);
2416 }
2417 format_tree = TREE_VALUE (params);
2418 params = TREE_CHAIN (params);
2419 if (format_tree == 0)
2420 return;
2421
2422 res.number_non_literal = 0;
2423 res.number_extra_args = 0;
2424 res.number_dollar_extra_args = 0;
2425 res.number_wide = 0;
2426 res.number_empty = 0;
2427 res.number_unterminated = 0;
2428 res.number_other = 0;
2429
2430 check_format_info_recurse (status, &res, info, format_tree, params, arg_num);
2431
2432 if (res.number_non_literal > 0)
2433 {
2434 /* Functions taking a va_list normally pass a non-literal format
2435 string. These functions typically are declared with
2436 first_arg_num == 0, so avoid warning in those cases. */
2437 if (!(format_types[info->format_type].flags & FMT_FLAG_ARG_CONVERT))
2438 {
2439 /* For strftime-like formats, warn for not checking the format
2440 string; but there are no arguments to check. */
2441 if (warn_format_nonliteral)
2442 status_warning (status, "format not a string literal, format string not checked");
2443 }
2444 else if (info->first_arg_num != 0)
2445 {
2446 /* If there are no arguments for the format at all, we may have
2447 printf (foo) which is likely to be a security hole. */
2448 while (arg_num + 1 < info->first_arg_num)
2449 {
2450 if (params == 0)
2451 break;
2452 params = TREE_CHAIN (params);
2453 ++arg_num;
2454 }
2455 if (params == 0 && (warn_format_nonliteral || warn_format_security))
2456 status_warning (status, "format not a string literal and no format arguments");
2457 else if (warn_format_nonliteral)
2458 status_warning (status, "format not a string literal, argument types not checked");
2459 }
2460 }
2461
2462 /* If there were extra arguments to the format, normally warn. However,
2463 the standard does say extra arguments are ignored, so in the specific
2464 case where we have multiple leaves (conditional expressions or
2465 ngettext) allow extra arguments if at least one leaf didn't have extra
2466 arguments, but was otherwise OK (either non-literal or checked OK).
2467 If the format is an empty string, this should be counted similarly to the
2468 case of extra format arguments. */
2469 if (res.number_extra_args > 0 && res.number_non_literal == 0
2470 && res.number_other == 0 && warn_format_extra_args)
2471 status_warning (status, "too many arguments for format");
2472 if (res.number_dollar_extra_args > 0 && res.number_non_literal == 0
2473 && res.number_other == 0 && warn_format_extra_args)
2474 status_warning (status, "unused arguments in $-style format");
2475 if (res.number_empty > 0 && res.number_non_literal == 0
2476 && res.number_other == 0)
2477 status_warning (status, "zero-length format string");
2478
2479 if (res.number_wide > 0)
2480 status_warning (status, "format is a wide character string");
2481
2482 if (res.number_unterminated > 0)
2483 status_warning (status, "unterminated format string");
2484}
2485
2486
2487/* Recursively check a call to a format function. FORMAT_TREE is the
2488 format parameter, which may be a conditional expression in which
2489 both halves should be checked. ARG_NUM is the number of the
2490 format argument; PARAMS points just after it in the argument list. */
2491
2492static void
2493check_format_info_recurse (status, res, info, format_tree, params, arg_num)
2494 int *status;
2495 format_check_results *res;
2496 function_format_info *info;
2497 tree format_tree;
2498 tree params;
2499 int arg_num;
2500{
2501 int format_length;
2502 const char *format_chars;
2503 tree array_size = 0;
2504 tree array_init;
2505
2506 if (TREE_CODE (format_tree) == NOP_EXPR)
2507 {
2508 /* Strip coercion. */
2509 check_format_info_recurse (status, res, info,
2510 TREE_OPERAND (format_tree, 0), params,
2511 arg_num);
2512 return;
2513 }
2514
2515 if (TREE_CODE (format_tree) == CALL_EXPR
2516 && TREE_CODE (TREE_OPERAND (format_tree, 0)) == ADDR_EXPR
2517 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0))
2518 == FUNCTION_DECL))
2519 {
2520 tree function = TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0);
2521
2522 /* See if this is a call to a known internationalization function
2523 that modifies the format arg. */
2524 international_format_info *iinfo;
2525
2526 for (iinfo = international_format_list; iinfo; iinfo = iinfo->next)
2527 if (iinfo->assembler_name
2528 ? (iinfo->assembler_name == DECL_ASSEMBLER_NAME (function))
2529 : (iinfo->name == DECL_NAME (function)))
2530 {
2531 tree inner_args;
2532 int i;
2533
2534 for (inner_args = TREE_OPERAND (format_tree, 1), i = 1;
2535 inner_args != 0;
2536 inner_args = TREE_CHAIN (inner_args), i++)
2537 if (i == iinfo->format_num)
2538 {
2539 /* FIXME: with Marc Espie's __attribute__((nonnull))
2540 patch in GCC, we will have chained attributes,
2541 and be able to handle functions like ngettext
2542 with multiple format_arg attributes properly. */
2543 check_format_info_recurse (status, res, info,
2544 TREE_VALUE (inner_args), params,
2545 arg_num);
2546 return;
2547 }
2548 }
2549 }
2550
2551 if (TREE_CODE (format_tree) == COND_EXPR)
2552 {
2553 /* Check both halves of the conditional expression. */
2554 check_format_info_recurse (status, res, info,
2555 TREE_OPERAND (format_tree, 1), params,
2556 arg_num);
2557 check_format_info_recurse (status, res, info,
2558 TREE_OPERAND (format_tree, 2), params,
2559 arg_num);
2560 return;
2561 }
2562
2563 if (integer_zerop (format_tree))
2564 {
2565 /* FIXME: this warning should go away once Marc Espie's
2566 __attribute__((nonnull)) patch is in. Instead, checking for
2567 nonnull attributes should probably change this function to act
2568 specially if info == NULL and add a res->number_null entry for
2569 that case, or maybe add a function pointer to be called at
2570 the end instead of hardcoding check_format_info_main. */
2571 status_warning (status, "null format string");
2572
2573 /* Skip to first argument to check, so we can see if this format
2574 has any arguments (it shouldn't). */
2575 while (arg_num + 1 < info->first_arg_num)
2576 {
2577 if (params == 0)
2578 return;
2579 params = TREE_CHAIN (params);
2580 ++arg_num;
2581 }
2582
2583 if (params == 0)
2584 res->number_other++;
2585 else
2586 res->number_extra_args++;
2587
2588 return;
2589 }
2590
2591 if (TREE_CODE (format_tree) != ADDR_EXPR)
2592 {
2593 res->number_non_literal++;
2594 return;
2595 }
2596 format_tree = TREE_OPERAND (format_tree, 0);
2597 if (TREE_CODE (format_tree) == VAR_DECL
2598 && TREE_CODE (TREE_TYPE (format_tree)) == ARRAY_TYPE
2599 && (array_init = decl_constant_value (format_tree)) != format_tree
2600 && TREE_CODE (array_init) == STRING_CST)
2601 {
2602 /* Extract the string constant initializer. Note that this may include
2603 a trailing NUL character that is not in the array (e.g.
2604 const char a[3] = "foo";). */
2605 array_size = DECL_SIZE_UNIT (format_tree);
2606 format_tree = array_init;
2607 }
2608 if (TREE_CODE (format_tree) != STRING_CST)
2609 {
2610 res->number_non_literal++;
2611 return;
2612 }
2613 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree))) != char_type_node)
2614 {
2615 res->number_wide++;
2616 return;
2617 }
2618 format_chars = TREE_STRING_POINTER (format_tree);
2619 format_length = TREE_STRING_LENGTH (format_tree);
2620 if (array_size != 0)
2621 {
2622 /* Variable length arrays can't be initialized. */
2623 if (TREE_CODE (array_size) != INTEGER_CST)
2624 abort ();
2625 if (host_integerp (array_size, 0))
2626 {
2627 HOST_WIDE_INT array_size_value = TREE_INT_CST_LOW (array_size);
2628 if (array_size_value > 0
2629 && array_size_value == (int) array_size_value
2630 && format_length > array_size_value)
2631 format_length = array_size_value;
2632 }
2633 }
2634 if (format_length < 1)
2635 {
2636 res->number_unterminated++;
2637 return;
2638 }
2639 if (format_length == 1)
2640 {
2641 res->number_empty++;
2642 return;
2643 }
2644 if (format_chars[--format_length] != 0)
2645 {
2646 res->number_unterminated++;
2647 return;
2648 }
2649
2650 /* Skip to first argument to check. */
2651 while (arg_num + 1 < info->first_arg_num)
2652 {
2653 if (params == 0)
2654 return;
2655 params = TREE_CHAIN (params);
2656 ++arg_num;
2657 }
2658 /* Provisionally increment res->number_other; check_format_info_main
2659 will decrement it if it finds there are extra arguments, but this way
2660 need not adjust it for every return. */
2661 res->number_other++;
2662 check_format_info_main (status, res, info, format_chars, format_length,
2663 params, arg_num);
2664}
2665
2666
2667/* Do the main part of checking a call to a format function. FORMAT_CHARS
2668 is the NUL-terminated format string (which at this point may contain
2669 internal NUL characters); FORMAT_LENGTH is its length (excluding the
2670 terminating NUL character). ARG_NUM is one less than the number of
2671 the first format argument to check; PARAMS points to that format
2672 argument in the list of arguments. */
2673
2674static void
2675check_format_info_main (status, res, info, format_chars, format_length,
2676 params, arg_num)
2677 int *status;
2678 format_check_results *res;
2679 function_format_info *info;
2680 const char *format_chars;
2681 int format_length;
2682 tree params;
2683 int arg_num;
2684{
2685 const char *orig_format_chars = format_chars;
2686 tree first_fillin_param = params;
2687
2688 const format_kind_info *fki = &format_types[info->format_type];
2689 const format_flag_spec *flag_specs = fki->flag_specs;
2690 const format_flag_pair *bad_flag_pairs = fki->bad_flag_pairs;
2691
2692 /* -1 if no conversions taking an operand have been found; 0 if one has
2693 and it didn't use $; 1 if $ formats are in use. */
2694 int has_operand_number = -1;
2695
2696 init_dollar_format_checking (info->first_arg_num, first_fillin_param);
2697
2698 while (1)
2699 {
2700 int i;
2701 int suppressed = FALSE;
2702 const char *length_chars = NULL;
2703 enum format_lengths length_chars_val = FMT_LEN_none;
2704 enum format_std_version length_chars_std = STD_C89;
2705 int format_char;
2706 tree cur_param;
2707 tree wanted_type;
2708 int main_arg_num = 0;
2709 tree main_arg_params = 0;
2710 enum format_std_version wanted_type_std;
2711 const char *wanted_type_name;
2712 format_wanted_type width_wanted_type;
2713 format_wanted_type precision_wanted_type;
2714 format_wanted_type main_wanted_type;
2715 format_wanted_type *first_wanted_type = NULL;
2716 format_wanted_type *last_wanted_type = NULL;
2717 const format_length_info *fli = NULL;
2718 const format_char_info *fci = NULL;
2719 char flag_chars[256];
2720 int aflag = 0;
2721 if (*format_chars == 0)
2722 {
2723 if (format_chars - orig_format_chars != format_length)
2724 status_warning (status, "embedded `\\0' in format");
2725 if (info->first_arg_num != 0 && params != 0
2726 && has_operand_number <= 0)
2727 {
2728 res->number_other--;
2729 res->number_extra_args++;
2730 }
2731 if (has_operand_number > 0)
2732 finish_dollar_format_checking (status, res);
2733 return;
2734 }
2735 if (*format_chars++ != '%')
2736 continue;
2737 if (*format_chars == 0)
2738 {
2739 status_warning (status, "spurious trailing `%%' in format");
2740 continue;
2741 }
2742 if (*format_chars == '%')
2743 {
2744 ++format_chars;
2745 continue;
2746 }
2747 flag_chars[0] = 0;
2748
2749 if ((fki->flags & FMT_FLAG_USE_DOLLAR) && has_operand_number != 0)
2750 {
2751 /* Possibly read a $ operand number at the start of the format.
2752 If one was previously used, one is required here. If one
2753 is not used here, we can't immediately conclude this is a
2754 format without them, since it could be printf %m or scanf %*. */
2755 int opnum;
2756 opnum = maybe_read_dollar_number (status, &format_chars, 0,
2757 first_fillin_param,
2758 &main_arg_params, fki);
2759 if (opnum == -1)
2760 return;
2761 else if (opnum > 0)
2762 {
2763 has_operand_number = 1;
2764 main_arg_num = opnum + info->first_arg_num - 1;
2765 }
2766 }
2767
2768 /* Read any format flags, but do not yet validate them beyond removing
2769 duplicates, since in general validation depends on the rest of
2770 the format. */
2771 while (*format_chars != 0
2772 && strchr (fki->flag_chars, *format_chars) != 0)
2773 {
2774 const format_flag_spec *s = get_flag_spec (flag_specs,
2775 *format_chars, NULL);
2776 if (strchr (flag_chars, *format_chars) != 0)
2777 {
2778 status_warning (status, "repeated %s in format", _(s->name));
2779 }
2780 else
2781 {
2782 i = strlen (flag_chars);
2783 flag_chars[i++] = *format_chars;
2784 flag_chars[i] = 0;
2785 }
2786 if (s->skip_next_char)
2787 {
2788 ++format_chars;
2789 if (*format_chars == 0)
2790 {
2791 status_warning (status, "missing fill character at end of strfmon format");
2792 return;
2793 }
2794 }
2795 ++format_chars;
2796 }
2797
2798 /* Read any format width, possibly * or *m$. */
2799 if (fki->width_char != 0)
2800 {
2801 if (fki->width_type != NULL && *format_chars == '*')
2802 {
2803 i = strlen (flag_chars);
2804 flag_chars[i++] = fki->width_char;
2805 flag_chars[i] = 0;
2806 /* "...a field width...may be indicated by an asterisk.
2807 In this case, an int argument supplies the field width..." */
2808 ++format_chars;
2809 if (params == 0)
2810 {
2811 status_warning (status, "too few arguments for format");
2812 return;
2813 }
2814 if (has_operand_number != 0)
2815 {
2816 int opnum;
2817 opnum = maybe_read_dollar_number (status, &format_chars,
2818 has_operand_number == 1,
2819 first_fillin_param,
2820 &params, fki);
2821 if (opnum == -1)
2822 return;
2823 else if (opnum > 0)
2824 {
2825 has_operand_number = 1;
2826 arg_num = opnum + info->first_arg_num - 1;
2827 }
2828 else
2829 has_operand_number = 0;
2830 }
2831 if (info->first_arg_num != 0)
2832 {
2833 cur_param = TREE_VALUE (params);
2834 if (has_operand_number <= 0)
2835 {
2836 params = TREE_CHAIN (params);
2837 ++arg_num;
2838 }
2839 width_wanted_type.wanted_type = *fki->width_type;
2840 width_wanted_type.wanted_type_name = NULL;
2841 width_wanted_type.pointer_count = 0;
2842 width_wanted_type.char_lenient_flag = 0;
2843 width_wanted_type.writing_in_flag = 0;
2844 width_wanted_type.reading_from_flag = 0;
2845 width_wanted_type.name = _("field width");
2846 width_wanted_type.param = cur_param;
2847 width_wanted_type.arg_num = arg_num;
2848 width_wanted_type.next = NULL;
2849 if (last_wanted_type != 0)
2850 last_wanted_type->next = &width_wanted_type;
2851 if (first_wanted_type == 0)
2852 first_wanted_type = &width_wanted_type;
2853 last_wanted_type = &width_wanted_type;
2854 }
2855 }
2856 else
2857 {
2858 /* Possibly read a numeric width. If the width is zero,
2859 we complain if appropriate. */
2860 int non_zero_width_char = FALSE;
2861 int found_width = FALSE;
2862 while (ISDIGIT (*format_chars))
2863 {
2864 found_width = TRUE;
2865 if (*format_chars != '0')
2866 non_zero_width_char = TRUE;
2867 ++format_chars;
2868 }
2869 if (found_width && !non_zero_width_char &&
2870 (fki->flags & FMT_FLAG_ZERO_WIDTH_BAD))
2871 status_warning (status, "zero width in %s format",
2872 fki->name);
2873 if (found_width)
2874 {
2875 i = strlen (flag_chars);
2876 flag_chars[i++] = fki->width_char;
2877 flag_chars[i] = 0;
2878 }
2879 }
2880 }
2881
2882 /* Read any format left precision (must be a number, not *). */
2883 if (fki->left_precision_char != 0 && *format_chars == '#')
2884 {
2885 ++format_chars;
2886 i = strlen (flag_chars);
2887 flag_chars[i++] = fki->left_precision_char;
2888 flag_chars[i] = 0;
2889 if (!ISDIGIT (*format_chars))
2890 status_warning (status, "empty left precision in %s format",
2891 fki->name);
2892 while (ISDIGIT (*format_chars))
2893 ++format_chars;
2894 }
2895
2896 /* Read any format precision, possibly * or *m$. */
2897 if (fki->precision_char != 0 && *format_chars == '.')
2898 {
2899 ++format_chars;
2900 i = strlen (flag_chars);
2901 flag_chars[i++] = fki->precision_char;
2902 flag_chars[i] = 0;
2903 if (fki->precision_type != NULL && *format_chars == '*')
2904 {
2905 /* "...a...precision...may be indicated by an asterisk.
2906 In this case, an int argument supplies the...precision." */
2907 ++format_chars;
2908 if (has_operand_number != 0)
2909 {
2910 int opnum;
2911 opnum = maybe_read_dollar_number (status, &format_chars,
2912 has_operand_number == 1,
2913 first_fillin_param,
2914 &params, fki);
2915 if (opnum == -1)
2916 return;
2917 else if (opnum > 0)
2918 {
2919 has_operand_number = 1;
2920 arg_num = opnum + info->first_arg_num - 1;
2921 }
2922 else
2923 has_operand_number = 0;
2924 }
2925 if (info->first_arg_num != 0)
2926 {
2927 if (params == 0)
2928 {
2929 status_warning (status, "too few arguments for format");
2930 return;
2931 }
2932 cur_param = TREE_VALUE (params);
2933 if (has_operand_number <= 0)
2934 {
2935 params = TREE_CHAIN (params);
2936 ++arg_num;
2937 }
2938 precision_wanted_type.wanted_type = *fki->precision_type;
2939 precision_wanted_type.wanted_type_name = NULL;
2940 precision_wanted_type.pointer_count = 0;
2941 precision_wanted_type.char_lenient_flag = 0;
2942 precision_wanted_type.writing_in_flag = 0;
2943 precision_wanted_type.reading_from_flag = 0;
2944 precision_wanted_type.name = _("field precision");
2945 precision_wanted_type.param = cur_param;
2946 precision_wanted_type.arg_num = arg_num;
2947 precision_wanted_type.next = NULL;
2948 if (last_wanted_type != 0)
2949 last_wanted_type->next = &precision_wanted_type;
2950 if (first_wanted_type == 0)
2951 first_wanted_type = &precision_wanted_type;
2952 last_wanted_type = &precision_wanted_type;
2953 }
2954 }
2955 else
2956 {
2957 if (!(fki->flags & FMT_FLAG_EMPTY_PREC_OK)
2958 && !ISDIGIT (*format_chars))
2959 status_warning (status, "empty precision in %s format",
2960 fki->name);
2961 while (ISDIGIT (*format_chars))
2962 ++format_chars;
2963 }
2964 }
2965
2966 /* Read any length modifier, if this kind of format has them. */
2967 fli = fki->length_char_specs;
2968 length_chars = NULL;
2969 length_chars_val = FMT_LEN_none;
2970 length_chars_std = STD_C89;
2971 if (fli)
2972 {
2973 while (fli->name != 0 && fli->name[0] != *format_chars)
2974 fli++;
2975 if (fli->name != 0)
2976 {
2977 format_chars++;
2978 if (fli->double_name != 0 && fli->name[0] == *format_chars)
2979 {
2980 format_chars++;
2981 length_chars = fli->double_name;
2982 length_chars_val = fli->double_index;
2983 length_chars_std = fli->double_std;
2984 }
2985 else
2986 {
2987 length_chars = fli->name;
2988 length_chars_val = fli->index;
2989 length_chars_std = fli->std;
2990 }
2991 i = strlen (flag_chars);
2992 flag_chars[i++] = fki->length_code_char;
2993 flag_chars[i] = 0;
2994 }
2995 if (pedantic)
2996 {
2997 /* Warn if the length modifier is non-standard. */
2998 if (ADJ_STD (length_chars_std) > C_STD_VER)
2999 status_warning (status, "%s does not support the `%s' %s length modifier",
3000 C_STD_NAME (length_chars_std), length_chars,
3001 fki->name);
3002 }
3003 }
3004
3005 /* Read any modifier (strftime E/O). */
3006 if (fki->modifier_chars != NULL)
3007 {
3008 while (*format_chars != 0
3009 && strchr (fki->modifier_chars, *format_chars) != 0)
3010 {
3011 if (strchr (flag_chars, *format_chars) != 0)
3012 {
3013 const format_flag_spec *s = get_flag_spec (flag_specs,
3014 *format_chars, NULL);
3015 status_warning (status, "repeated %s in format", _(s->name));
3016 }
3017 else
3018 {
3019 i = strlen (flag_chars);
3020 flag_chars[i++] = *format_chars;
3021 flag_chars[i] = 0;
3022 }
3023 ++format_chars;
3024 }
3025 }
3026
3027 /* Handle the scanf allocation kludge. */
3028 if (fki->flags & FMT_FLAG_SCANF_A_KLUDGE)
3029 {
3030 if (*format_chars == 'a' && !flag_isoc99)
3031 {
3032 if (format_chars[1] == 's' || format_chars[1] == 'S'
3033 || format_chars[1] == '[')
3034 {
3035 /* `a' is used as a flag. */
3036 i = strlen (flag_chars);
3037 flag_chars[i++] = 'a';
3038 flag_chars[i] = 0;
3039 format_chars++;
3040 }
3041 }
3042 }
3043
3044 format_char = *format_chars;
3045 if (format_char == 0
3046 || (!(fki->flags & FMT_FLAG_FANCY_PERCENT_OK) && format_char == '%'))
3047 {
3048 status_warning (status, "conversion lacks type at end of format");
3049 continue;
3050 }
3051 format_chars++;
3052 fci = fki->conversion_specs;
3053 while (fci->format_chars != 0
3054 && strchr (fci->format_chars, format_char) == 0)
3055 ++fci;
3056 if (fci->format_chars == 0)
3057 {
3058 if (ISGRAPH(format_char))
3059 status_warning (status, "unknown conversion type character `%c' in format",
3060 format_char);
3061 else
3062 status_warning (status, "unknown conversion type character 0x%x in format",
3063 format_char);
3064 continue;
3065 }
3066 if (pedantic)
3067 {
3068 if (ADJ_STD (fci->std) > C_STD_VER)
3069 status_warning (status, "%s does not support the `%%%c' %s format",
3070 C_STD_NAME (fci->std), format_char, fki->name);
3071 }
3072
3073 /* Validate the individual flags used, removing any that are invalid. */
3074 {
3075 int d = 0;
3076 for (i = 0; flag_chars[i] != 0; i++)
3077 {
3078 const format_flag_spec *s = get_flag_spec (flag_specs,
3079 flag_chars[i], NULL);
3080 flag_chars[i - d] = flag_chars[i];
3081 if (flag_chars[i] == fki->length_code_char)
3082 continue;
3083 if (strchr (fci->flag_chars, flag_chars[i]) == 0)
3084 {
3085 status_warning (status, "%s used with `%%%c' %s format",
3086 _(s->name), format_char, fki->name);
3087 d++;
3088 continue;
3089 }
3090 if (pedantic)
3091 {
3092 const format_flag_spec *t;
3093 if (ADJ_STD (s->std) > C_STD_VER)
3094 status_warning (status, "%s does not support %s",
3095 C_STD_NAME (s->std), _(s->long_name));
3096 t = get_flag_spec (flag_specs, flag_chars[i], fci->flags2);
3097 if (t != NULL && ADJ_STD (t->std) > ADJ_STD (s->std))
3098 {
3099 const char *long_name = (t->long_name != NULL
3100 ? t->long_name
3101 : s->long_name);
3102 if (ADJ_STD (t->std) > C_STD_VER)
3103 status_warning (status, "%s does not support %s with the `%%%c' %s format",
3104 C_STD_NAME (t->std), _(long_name),
3105 format_char, fki->name);
3106 }
3107 }
3108 }
3109 flag_chars[i - d] = 0;
3110 }
3111
3112 if ((fki->flags & FMT_FLAG_SCANF_A_KLUDGE)
3113 && strchr (flag_chars, 'a') != 0)
3114 aflag = 1;
3115
3116 if (fki->suppression_char
3117 && strchr (flag_chars, fki->suppression_char) != 0)
3118 suppressed = 1;
3119
3120 /* Validate the pairs of flags used. */
3121 for (i = 0; bad_flag_pairs[i].flag_char1 != 0; i++)
3122 {
3123 const format_flag_spec *s, *t;
3124 if (strchr (flag_chars, bad_flag_pairs[i].flag_char1) == 0)
3125 continue;
3126 if (strchr (flag_chars, bad_flag_pairs[i].flag_char2) == 0)
3127 continue;
3128 if (bad_flag_pairs[i].predicate != 0
3129 && strchr (fci->flags2, bad_flag_pairs[i].predicate) == 0)
3130 continue;
3131 s = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char1, NULL);
3132 t = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char2, NULL);
3133 if (bad_flag_pairs[i].ignored)
3134 {
3135 if (bad_flag_pairs[i].predicate != 0)
3136 status_warning (status, "%s ignored with %s and `%%%c' %s format",
3137 _(s->name), _(t->name), format_char,
3138 fki->name);
3139 else
3140 status_warning (status, "%s ignored with %s in %s format",
3141 _(s->name), _(t->name), fki->name);
3142 }
3143 else
3144 {
3145 if (bad_flag_pairs[i].predicate != 0)
3146 status_warning (status, "use of %s and %s together with `%%%c' %s format",
3147 _(s->name), _(t->name), format_char,
3148 fki->name);
3149 else
3150 status_warning (status, "use of %s and %s together in %s format",
3151 _(s->name), _(t->name), fki->name);
3152 }
3153 }
3154
3155 /* Give Y2K warnings. */
3156 if (warn_format_y2k)
3157 {
3158 int y2k_level = 0;
3159 if (strchr (fci->flags2, '4') != 0)
3160 if (strchr (flag_chars, 'E') != 0)
3161 y2k_level = 3;
3162 else
3163 y2k_level = 2;
3164 else if (strchr (fci->flags2, '3') != 0)
3165 y2k_level = 3;
3166 else if (strchr (fci->flags2, '2') != 0)
3167 y2k_level = 2;
3168 if (y2k_level == 3)
3169 status_warning (status, "`%%%c' yields only last 2 digits of year in some locales",
3170 format_char);
3171 else if (y2k_level == 2)
3172 status_warning (status, "`%%%c' yields only last 2 digits of year", format_char);
3173 }
3174
3175 if (strchr (fci->flags2, '[') != 0)
3176 {
3177 /* Skip over scan set, in case it happens to have '%' in it. */
3178 if (*format_chars == '^')
3179 ++format_chars;
3180 /* Find closing bracket; if one is hit immediately, then
3181 it's part of the scan set rather than a terminator. */
3182 if (*format_chars == ']')
3183 ++format_chars;
3184 while (*format_chars && *format_chars != ']')
3185 ++format_chars;
3186 if (*format_chars != ']')
3187 /* The end of the format string was reached. */
3188 status_warning (status, "no closing `]' for `%%[' format");
3189 }
3190
3191 wanted_type = 0;
3192 wanted_type_name = 0;
3193 if (fki->flags & FMT_FLAG_ARG_CONVERT)
3194 {
3195 wanted_type = (fci->types[length_chars_val].type
3196 ? *fci->types[length_chars_val].type : 0);
3197 wanted_type_name = fci->types[length_chars_val].name;
3198 wanted_type_std = fci->types[length_chars_val].std;
3199 if (wanted_type == 0)
3200 {
3201 status_warning (status, "use of `%s' length modifier with `%c' type character",
3202 length_chars, format_char);
3203 /* Heuristic: skip one argument when an invalid length/type
3204 combination is encountered. */
3205 arg_num++;
3206 if (params == 0)
3207 {
3208 status_warning (status, "too few arguments for format");
3209 return;
3210 }
3211 params = TREE_CHAIN (params);
3212 continue;
3213 }
3214 else if (pedantic
3215 /* Warn if non-standard, provided it is more non-standard
3216 than the length and type characters that may already
3217 have been warned for. */
3218 && ADJ_STD (wanted_type_std) > ADJ_STD (length_chars_std)
3219 && ADJ_STD (wanted_type_std) > ADJ_STD (fci->std))
3220 {
3221 if (ADJ_STD (wanted_type_std) > C_STD_VER)
3222 status_warning (status, "%s does not support the `%%%s%c' %s format",
3223 C_STD_NAME (wanted_type_std), length_chars,
3224 format_char, fki->name);
3225 }
3226 }
3227
3228 /* Finally. . .check type of argument against desired type! */
3229 if (info->first_arg_num == 0)
3230 continue;
3231 if ((fci->pointer_count == 0 && wanted_type == void_type_node)
3232 || suppressed)
3233 {
3234 if (main_arg_num != 0)
3235 {
3236 if (suppressed)
3237 status_warning (status, "operand number specified with suppressed assignment");
3238 else
3239 status_warning (status, "operand number specified for format taking no argument");
3240 }
3241 }
3242 else
3243 {
3244 if (main_arg_num != 0)
3245 {
3246 arg_num = main_arg_num;
3247 params = main_arg_params;
3248 }
3249 else
3250 {
3251 ++arg_num;
3252 if (has_operand_number > 0)
3253 {
3254 status_warning (status, "missing $ operand number in format");
3255 return;
3256 }
3257 else
3258 has_operand_number = 0;
3259 if (params == 0)
3260 {
3261 status_warning (status, "too few arguments for format");
3262 return;
3263 }
3264 }
3265 cur_param = TREE_VALUE (params);
3266 params = TREE_CHAIN (params);
3267 main_wanted_type.wanted_type = wanted_type;
3268 main_wanted_type.wanted_type_name = wanted_type_name;
3269 main_wanted_type.pointer_count = fci->pointer_count + aflag;
3270 main_wanted_type.char_lenient_flag = 0;
3271 if (strchr (fci->flags2, 'c') != 0)
3272 main_wanted_type.char_lenient_flag = 1;
3273 main_wanted_type.writing_in_flag = 0;
3274 main_wanted_type.reading_from_flag = 0;
3275 if (aflag)
3276 main_wanted_type.writing_in_flag = 1;
3277 else
3278 {
3279 if (strchr (fci->flags2, 'W') != 0)
3280 main_wanted_type.writing_in_flag = 1;
3281 if (strchr (fci->flags2, 'R') != 0)
3282 main_wanted_type.reading_from_flag = 1;
3283 }
3284 main_wanted_type.name = NULL;
3285 main_wanted_type.param = cur_param;
3286 main_wanted_type.arg_num = arg_num;
3287 main_wanted_type.next = NULL;
3288 if (last_wanted_type != 0)
3289 last_wanted_type->next = &main_wanted_type;
3290 if (first_wanted_type == 0)
3291 first_wanted_type = &main_wanted_type;
3292 last_wanted_type = &main_wanted_type;
3293 }
3294
3295 if (first_wanted_type != 0)
3296 check_format_types (status, first_wanted_type);
3297
3298 }
3299}
3300
3301
3302/* Check the argument types from a single format conversion (possibly
3303 including width and precision arguments). */
3304static void
3305check_format_types (status, types)
3306 int *status;
3307 format_wanted_type *types;
3308{
3309 for (; types != 0; types = types->next)
3310 {
3311 tree cur_param;
3312 tree cur_type;
3313 tree orig_cur_type;
3314 tree wanted_type;
3315 tree promoted_type;
3316 int arg_num;
3317 int i;
3318 int char_type_flag;
3319 cur_param = types->param;
3320 cur_type = TREE_TYPE (cur_param);
3321 if (cur_type == error_mark_node)
3322 continue;
3323 char_type_flag = 0;
3324 wanted_type = types->wanted_type;
3325 arg_num = types->arg_num;
3326
3327 /* The following should not occur here. */
3328 if (wanted_type == 0)
3329 abort ();
3330 if (wanted_type == void_type_node && types->pointer_count == 0)
3331 abort ();
3332
3333 if (types->pointer_count == 0)
3334 {
3335 promoted_type = simple_type_promotes_to (wanted_type);
3336 if (promoted_type != NULL_TREE)
3337 wanted_type = promoted_type;
3338 }
3339
3340 STRIP_NOPS (cur_param);
3341
3342 /* Check the types of any additional pointer arguments
3343 that precede the "real" argument. */
3344 for (i = 0; i < types->pointer_count; ++i)
3345 {
3346 if (TREE_CODE (cur_type) == POINTER_TYPE)
3347 {
3348 cur_type = TREE_TYPE (cur_type);
3349 if (cur_type == error_mark_node)
3350 break;
3351
3352 /* Check for writing through a NULL pointer. */
3353 if (types->writing_in_flag
3354 && i == 0
3355 && cur_param != 0
3356 && integer_zerop (cur_param))
3357 status_warning (status,
3358 "writing through null pointer (arg %d)",
3359 arg_num);
3360
3361 /* Check for reading through a NULL pointer. */
3362 if (types->reading_from_flag
3363 && i == 0
3364 && cur_param != 0
3365 && integer_zerop (cur_param))
3366 status_warning (status,
3367 "reading through null pointer (arg %d)",
3368 arg_num);
3369
3370 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
3371 cur_param = TREE_OPERAND (cur_param, 0);
3372 else
3373 cur_param = 0;
3374
3375 /* See if this is an attempt to write into a const type with
3376 scanf or with printf "%n". Note: the writing in happens
3377 at the first indirection only, if for example
3378 void * const * is passed to scanf %p; passing
3379 const void ** is simply passing an incompatible type. */
3380 if (types->writing_in_flag
3381 && i == 0
3382 && (TYPE_READONLY (cur_type)
3383 || (cur_param != 0
3384 && (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'c'
3385 || (DECL_P (cur_param)
3386 && TREE_READONLY (cur_param))))))
3387 status_warning (status, "writing into constant object (arg %d)", arg_num);
3388
3389 /* If there are extra type qualifiers beyond the first
3390 indirection, then this makes the types technically
3391 incompatible. */
3392 if (i > 0
3393 && pedantic
3394 && (TYPE_READONLY (cur_type)
3395 || TYPE_VOLATILE (cur_type)
3396 || TYPE_RESTRICT (cur_type)))
3397 status_warning (status, "extra type qualifiers in format argument (arg %d)",
3398 arg_num);
3399
3400 }
3401 else
3402 {
3403 if (types->pointer_count == 1)
3404 status_warning (status, "format argument is not a pointer (arg %d)", arg_num);
3405 else
3406 status_warning (status, "format argument is not a pointer to a pointer (arg %d)", arg_num);
3407 break;
3408 }
3409 }
3410
3411 if (i < types->pointer_count)
3412 continue;
3413
3414 orig_cur_type = cur_type;
3415 cur_type = TYPE_MAIN_VARIANT (cur_type);
3416
3417 /* Check whether the argument type is a character type. This leniency
3418 only applies to certain formats, flagged with 'c'.
3419 */
3420 if (types->char_lenient_flag)
3421 char_type_flag = (cur_type == char_type_node
3422 || cur_type == signed_char_type_node
3423 || cur_type == unsigned_char_type_node);
3424
3425 /* Check the type of the "real" argument, if there's a type we want. */
3426 if (wanted_type == cur_type)
3427 continue;
3428 /* If we want `void *', allow any pointer type.
3429 (Anything else would already have got a warning.)
3430 With -pedantic, only allow pointers to void and to character
3431 types. */
3432 if (wanted_type == void_type_node
3433 && (!pedantic || (i == 1 && char_type_flag)))
3434 continue;
3435 /* Don't warn about differences merely in signedness, unless
3436 -pedantic. With -pedantic, warn if the type is a pointer
3437 target and not a character type, and for character types at
3438 a second level of indirection. */
3439 if (TREE_CODE (wanted_type) == INTEGER_TYPE
3440 && TREE_CODE (cur_type) == INTEGER_TYPE
3441 && (! pedantic || i == 0 || (i == 1 && char_type_flag))
3442 && (TREE_UNSIGNED (wanted_type)
3443 ? wanted_type == unsigned_type (cur_type)
3444 : wanted_type == signed_type (cur_type)))
3445 continue;
3446 /* Likewise, "signed char", "unsigned char" and "char" are
3447 equivalent but the above test won't consider them equivalent. */
3448 if (wanted_type == char_type_node
3449 && (! pedantic || i < 2)
3450 && char_type_flag)
3451 continue;
3452 /* Now we have a type mismatch. */
3453 {
3454 register const char *this;
3455 register const char *that;
3456
3457 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
3458 that = 0;
3459 if (TYPE_NAME (orig_cur_type) != 0
3460 && TREE_CODE (orig_cur_type) != INTEGER_TYPE
3461 && !(TREE_CODE (orig_cur_type) == POINTER_TYPE
3462 && TREE_CODE (TREE_TYPE (orig_cur_type)) == INTEGER_TYPE))
3463 {
3464 if (TREE_CODE (TYPE_NAME (orig_cur_type)) == TYPE_DECL
3465 && DECL_NAME (TYPE_NAME (orig_cur_type)) != 0)
3466 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (orig_cur_type)));
3467 else
3468 that = IDENTIFIER_POINTER (TYPE_NAME (orig_cur_type));
3469 }
3470
3471 /* A nameless type can't possibly match what the format wants.
3472 So there will be a warning for it.
3473 Make up a string to describe vaguely what it is. */
3474 if (that == 0)
3475 {
3476 if (TREE_CODE (orig_cur_type) == POINTER_TYPE)
3477 that = "pointer";
3478 else
3479 that = "different type";
3480 }
3481
3482 /* Make the warning better in case of mismatch of int vs long. */
3483 if (TREE_CODE (orig_cur_type) == INTEGER_TYPE
3484 && TREE_CODE (wanted_type) == INTEGER_TYPE
3485 && TYPE_PRECISION (orig_cur_type) == TYPE_PRECISION (wanted_type)
3486 && TYPE_NAME (orig_cur_type) != 0
3487 && TREE_CODE (TYPE_NAME (orig_cur_type)) == TYPE_DECL)
3488 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (orig_cur_type)));
3489
3490 if (strcmp (this, that) != 0)
3491 {
3492 /* There may be a better name for the format, e.g. size_t,
3493 but we should allow for programs with a perverse typedef
3494 making size_t something other than what the compiler
3495 thinks. */
3496 if (types->wanted_type_name != 0
3497 && strcmp (types->wanted_type_name, that) != 0)
3498 this = types->wanted_type_name;
3499 if (types->name != 0)
3500 status_warning (status, "%s is not type %s (arg %d)", types->name, this,
3501 arg_num);
3502 else
3503 status_warning (status, "%s format, %s arg (arg %d)", this, that, arg_num);
3504 }
3505 }
3506 }
3507}
3508
3509/* Set format warning options according to a -Wformat=n option. */
3510
3511void
3512set_Wformat (setting)
3513 int setting;
3514{
3515 warn_format = setting;
3516 warn_format_y2k = setting;
3517 warn_format_extra_args = setting;
3518 if (setting != 1)
3519 {
3520 warn_format_nonliteral = setting;
3521 warn_format_security = setting;
3522 }
3523}
3524\f
3525/* Print a warning if a constant expression had overflow in folding.
3526 Invoke this function on every expression that the language
3527 requires to be a constant expression.
3528 Note the ANSI C standard says it is erroneous for a
3529 constant expression to overflow. */
3530
3531void
3532constant_expression_warning (value)
3533 tree value;
3534{
3535 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
3536 || TREE_CODE (value) == COMPLEX_CST)
3537 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
3538 pedwarn ("overflow in constant expression");
3539}
3540
3541/* Print a warning if an expression had overflow in folding.
3542 Invoke this function on every expression that
3543 (1) appears in the source code, and
3544 (2) might be a constant expression that overflowed, and
3545 (3) is not already checked by convert_and_check;
3546 however, do not invoke this function on operands of explicit casts. */
3547
3548void
3549overflow_warning (value)
3550 tree value;
3551{
3552 if ((TREE_CODE (value) == INTEGER_CST
3553 || (TREE_CODE (value) == COMPLEX_CST
3554 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
3555 && TREE_OVERFLOW (value))
3556 {
3557 TREE_OVERFLOW (value) = 0;
3558 if (skip_evaluation == 0)
3559 warning ("integer overflow in expression");
3560 }
3561 else if ((TREE_CODE (value) == REAL_CST
3562 || (TREE_CODE (value) == COMPLEX_CST
3563 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
3564 && TREE_OVERFLOW (value))
3565 {
3566 TREE_OVERFLOW (value) = 0;
3567 if (skip_evaluation == 0)
3568 warning ("floating point overflow in expression");
3569 }
3570}
3571
3572/* Print a warning if a large constant is truncated to unsigned,
3573 or if -Wconversion is used and a constant < 0 is converted to unsigned.
3574 Invoke this function on every expression that might be implicitly
3575 converted to an unsigned type. */
3576
3577void
3578unsigned_conversion_warning (result, operand)
3579 tree result, operand;
3580{
3581 if (TREE_CODE (operand) == INTEGER_CST
3582 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
3583 && TREE_UNSIGNED (TREE_TYPE (result))
3584 && skip_evaluation == 0
3585 && !int_fits_type_p (operand, TREE_TYPE (result)))
3586 {
3587 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
3588 /* This detects cases like converting -129 or 256 to unsigned char. */
3589 warning ("large integer implicitly truncated to unsigned type");
3590 else if (warn_conversion)
3591 warning ("negative integer implicitly converted to unsigned type");
3592 }
3593}
3594
3595/* Convert EXPR to TYPE, warning about conversion problems with constants.
3596 Invoke this function on every expression that is converted implicitly,
3597 i.e. because of language rules and not because of an explicit cast. */
3598
3599tree
3600convert_and_check (type, expr)
3601 tree type, expr;
3602{
3603 tree t = convert (type, expr);
3604 if (TREE_CODE (t) == INTEGER_CST)
3605 {
3606 if (TREE_OVERFLOW (t))
3607 {
3608 TREE_OVERFLOW (t) = 0;
3609
3610 /* Do not diagnose overflow in a constant expression merely
3611 because a conversion overflowed. */
3612 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
3613
3614 /* No warning for converting 0x80000000 to int. */
3615 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
3616 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
3617 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
3618 /* If EXPR fits in the unsigned version of TYPE,
3619 don't warn unless pedantic. */
3620 if ((pedantic
3621 || TREE_UNSIGNED (type)
3622 || ! int_fits_type_p (expr, unsigned_type (type)))
3623 && skip_evaluation == 0)
3624 warning ("overflow in implicit constant conversion");
3625 }
3626 else
3627 unsigned_conversion_warning (t, expr);
3628 }
3629 return t;
3630}
3631\f
3632/* A node in a list that describes references to variables (EXPR), which are
3633 either read accesses if WRITER is zero, or write accesses, in which case
3634 WRITER is the parent of EXPR. */
3635struct tlist
3636{
3637 struct tlist *next;
3638 tree expr, writer;
3639};
3640
3641/* Used to implement a cache the results of a call to verify_tree. We only
3642 use this for SAVE_EXPRs. */
3643struct tlist_cache
3644{
3645 struct tlist_cache *next;
3646 struct tlist *cache_before_sp;
3647 struct tlist *cache_after_sp;
3648 tree expr;
3649};
3650
3651/* Obstack to use when allocating tlist structures, and corresponding
3652 firstobj. */
3653static struct obstack tlist_obstack;
3654static char *tlist_firstobj = 0;
3655
3656/* Keep track of the identifiers we've warned about, so we can avoid duplicate
3657 warnings. */
3658static struct tlist *warned_ids;
3659/* SAVE_EXPRs need special treatment. We process them only once and then
3660 cache the results. */
3661static struct tlist_cache *save_expr_cache;
3662
3663static void add_tlist PARAMS ((struct tlist **, struct tlist *, tree, int));
3664static void merge_tlist PARAMS ((struct tlist **, struct tlist *, int));
3665static void verify_tree PARAMS ((tree, struct tlist **, struct tlist **, tree));
3666static int warning_candidate_p PARAMS ((tree));
3667static void warn_for_collisions PARAMS ((struct tlist *));
3668static void warn_for_collisions_1 PARAMS ((tree, tree, struct tlist *, int));
3669static struct tlist *new_tlist PARAMS ((struct tlist *, tree, tree));
3670static void verify_sequence_points PARAMS ((tree));
3671
3672/* Create a new struct tlist and fill in its fields. */
3673static struct tlist *
3674new_tlist (next, t, writer)
3675 struct tlist *next;
3676 tree t;
3677 tree writer;
3678{
3679 struct tlist *l;
3680 l = (struct tlist *) obstack_alloc (&tlist_obstack, sizeof *l);
3681 l->next = next;
3682 l->expr = t;
3683 l->writer = writer;
3684 return l;
3685}
3686
3687/* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
3688 is nonnull, we ignore any node we find which has a writer equal to it. */
3689
3690static void
3691add_tlist (to, add, exclude_writer, copy)
3692 struct tlist **to;
3693 struct tlist *add;
3694 tree exclude_writer;
3695 int copy;
3696{
3697 while (add)
3698 {
3699 struct tlist *next = add->next;
3700 if (! copy)
3701 add->next = *to;
3702 if (! exclude_writer || add->writer != exclude_writer)
3703 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
3704 add = next;
3705 }
3706}
3707
3708/* Merge the nodes of ADD into TO. This merging process is done so that for
3709 each variable that already exists in TO, no new node is added; however if
3710 there is a write access recorded in ADD, and an occurrence on TO is only
3711 a read access, then the occurrence in TO will be modified to record the
3712 write. */
3713
3714static void
3715merge_tlist (to, add, copy)
3716 struct tlist **to;
3717 struct tlist *add;
3718 int copy;
3719{
3720 struct tlist **end = to;
3721
3722 while (*end)
3723 end = &(*end)->next;
3724
3725 while (add)
3726 {
3727 int found = 0;
3728 struct tlist *tmp2;
3729 struct tlist *next = add->next;
3730
3731 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
3732 if (tmp2->expr == add->expr)
3733 {
3734 found = 1;
3735 if (! tmp2->writer)
3736 tmp2->writer = add->writer;
3737 }
3738 if (! found)
3739 {
3740 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
3741 end = &(*end)->next;
3742 *end = 0;
3743 }
3744 add = next;
3745 }
3746}
3747
3748/* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
3749 references in list LIST conflict with it, excluding reads if ONLY writers
3750 is nonzero. */
3751
3752static void
3753warn_for_collisions_1 (written, writer, list, only_writes)
3754 tree written, writer;
3755 struct tlist *list;
3756 int only_writes;
3757{
3758 struct tlist *tmp;
3759
3760 /* Avoid duplicate warnings. */
3761 for (tmp = warned_ids; tmp; tmp = tmp->next)
3762 if (tmp->expr == written)
3763 return;
3764
3765 while (list)
3766 {
3767 if (list->expr == written
3768 && list->writer != writer
3769 && (! only_writes || list->writer))
3770 {
3771 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3772 warning ("operation on `%s' may be undefined",
3773 IDENTIFIER_POINTER (DECL_NAME (list->expr)));
3774 }
3775 list = list->next;
3776 }
3777}
3778
3779/* Given a list LIST of references to variables, find whether any of these
3780 can cause conflicts due to missing sequence points. */
3781
3782static void
3783warn_for_collisions (list)
3784 struct tlist *list;
3785{
3786 struct tlist *tmp;
3787
3788 for (tmp = list; tmp; tmp = tmp->next)
3789 {
3790 if (tmp->writer)
3791 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3792 }
3793}
3794
3795/* Return nonzero if X is a tree that can be verified by the sequence poitn
3796 warnings. */
3797static int
3798warning_candidate_p (x)
3799 tree x;
3800{
3801 return TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == PARM_DECL;
3802}
3803
3804/* Walk the tree X, and record accesses to variables. If X is written by the
3805 parent tree, WRITER is the parent.
3806 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3807 expression or its only operand forces a sequence point, then everything up
3808 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3809 in PNO_SP.
3810 Once we return, we will have emitted warnings if any subexpression before
3811 such a sequence point could be undefined. On a higher level, however, the
3812 sequence point may not be relevant, and we'll merge the two lists.
3813
3814 Example: (b++, a) + b;
3815 The call that processes the COMPOUND_EXPR will store the increment of B
3816 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3817 processes the PLUS_EXPR will need to merge the two lists so that
3818 eventually, all accesses end up on the same list (and we'll warn about the
3819 unordered subexpressions b++ and b.
3820
3821 A note on merging. If we modify the former example so that our expression
3822 becomes
3823 (b++, b) + a
3824 care must be taken not simply to add all three expressions into the final
3825 PNO_SP list. The function merge_tlist takes care of that by merging the
3826 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3827 way, so that no more than one access to B is recorded. */
3828
3829static void
3830verify_tree (x, pbefore_sp, pno_sp, writer)
3831 tree x;
3832 struct tlist **pbefore_sp, **pno_sp;
3833 tree writer;
3834{
3835 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3836 enum tree_code code;
3837 char class;
3838
3839 restart:
3840 code = TREE_CODE (x);
3841 class = TREE_CODE_CLASS (code);
3842
3843 if (warning_candidate_p (x))
3844 {
3845 *pno_sp = new_tlist (*pno_sp, x, writer);
3846 return;
3847 }
3848
3849 switch (code)
3850 {
3851 case CONSTRUCTOR:
3852 return;
3853
3854 case COMPOUND_EXPR:
3855 case TRUTH_ANDIF_EXPR:
3856 case TRUTH_ORIF_EXPR:
3857 tmp_before = tmp_nosp = tmp_list3 = 0;
3858 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3859 warn_for_collisions (tmp_nosp);
3860 merge_tlist (pbefore_sp, tmp_before, 0);
3861 merge_tlist (pbefore_sp, tmp_nosp, 0);
3862 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3863 merge_tlist (pbefore_sp, tmp_list3, 0);
3864 return;
3865
3866 case COND_EXPR:
3867 tmp_before = tmp_list2 = 0;
3868 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3869 warn_for_collisions (tmp_list2);
3870 merge_tlist (pbefore_sp, tmp_before, 0);
3871 merge_tlist (pbefore_sp, tmp_list2, 1);
3872
3873 tmp_list3 = tmp_nosp = 0;
3874 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3875 warn_for_collisions (tmp_nosp);
3876 merge_tlist (pbefore_sp, tmp_list3, 0);
3877
3878 tmp_list3 = tmp_list2 = 0;
3879 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3880 warn_for_collisions (tmp_list2);
3881 merge_tlist (pbefore_sp, tmp_list3, 0);
3882 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3883 two first, to avoid warning for (a ? b++ : b++). */
3884 merge_tlist (&tmp_nosp, tmp_list2, 0);
3885 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3886 return;
3887
3888 case PREDECREMENT_EXPR:
3889 case PREINCREMENT_EXPR:
3890 case POSTDECREMENT_EXPR:
3891 case POSTINCREMENT_EXPR:
3892 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3893 return;
3894
3895 case MODIFY_EXPR:
3896 tmp_before = tmp_nosp = tmp_list3 = 0;
3897 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3898 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3899 /* Expressions inside the LHS are not ordered wrt. the sequence points
3900 in the RHS. Example:
3901 *a = (a++, 2)
3902 Despite the fact that the modification of "a" is in the before_sp
3903 list (tmp_before), it conflicts with the use of "a" in the LHS.
3904 We can handle this by adding the contents of tmp_list3
3905 to those of tmp_before, and redoing the collision warnings for that
3906 list. */
3907 add_tlist (&tmp_before, tmp_list3, x, 1);
3908 warn_for_collisions (tmp_before);
3909 /* Exclude the LHS itself here; we first have to merge it into the
3910 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3911 didn't exclude the LHS, we'd get it twice, once as a read and once
3912 as a write. */
3913 add_tlist (pno_sp, tmp_list3, x, 0);
3914 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3915
3916 merge_tlist (pbefore_sp, tmp_before, 0);
3917 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3918 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3919 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3920 return;
3921
3922 case CALL_EXPR:
3923 /* We need to warn about conflicts among arguments and conflicts between
3924 args and the function address. Side effects of the function address,
3925 however, are not ordered by the sequence point of the call. */
3926 tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0;
3927 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3928 if (TREE_OPERAND (x, 1))
3929 verify_tree (TREE_OPERAND (x, 1), &tmp_list2, &tmp_list3, NULL_TREE);
3930 merge_tlist (&tmp_list3, tmp_list2, 0);
3931 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3932 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3933 warn_for_collisions (tmp_before);
3934 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3935 return;
3936
3937 case TREE_LIST:
3938 /* Scan all the list, e.g. indices of multi dimensional array. */
3939 while (x)
3940 {
3941 tmp_before = tmp_nosp = 0;
3942 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3943 merge_tlist (&tmp_nosp, tmp_before, 0);
3944 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3945 x = TREE_CHAIN (x);
3946 }
3947 return;
3948
3949 case SAVE_EXPR:
3950 {
3951 struct tlist_cache *t;
3952 for (t = save_expr_cache; t; t = t->next)
3953 if (t->expr == x)
3954 break;
3955
3956 if (! t)
3957 {
3958 t = (struct tlist_cache *) obstack_alloc (&tlist_obstack,
3959 sizeof *t);
3960 t->next = save_expr_cache;
3961 t->expr = x;
3962 save_expr_cache = t;
3963
3964 tmp_before = tmp_nosp = 0;
3965 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3966 warn_for_collisions (tmp_nosp);
3967
3968 tmp_list3 = 0;
3969 while (tmp_nosp)
3970 {
3971 struct tlist *t = tmp_nosp;
3972 tmp_nosp = t->next;
3973 merge_tlist (&tmp_list3, t, 0);
3974 }
3975 t->cache_before_sp = tmp_before;
3976 t->cache_after_sp = tmp_list3;
3977 }
3978 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3979 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3980 return;
3981 }
3982 default:
3983 break;
3984 }
3985
3986 if (class == '1')
3987 {
3988 if (first_rtl_op (code) == 0)
3989 return;
3990 x = TREE_OPERAND (x, 0);
3991 writer = 0;
3992 goto restart;
3993 }
3994
3995 switch (class)
3996 {
3997 case 'r':
3998 case '<':
3999 case '2':
4000 case 'b':
4001 case 'e':
4002 case 's':
4003 case 'x':
4004 {
4005 int lp;
4006 int max = first_rtl_op (TREE_CODE (x));
4007 for (lp = 0; lp < max; lp++)
4008 {
4009 tmp_before = tmp_nosp = 0;
4010 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, NULL_TREE);
4011 merge_tlist (&tmp_nosp, tmp_before, 0);
4012 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
4013 }
4014 break;
4015 }
4016 }
4017}
4018
4019/* Try to warn for undefined behaviour in EXPR due to missing sequence
4020 points. */
4021
4022static void
4023verify_sequence_points (expr)
4024 tree expr;
4025{
4026 struct tlist *before_sp = 0, *after_sp = 0;
4027
4028 warned_ids = 0;
4029 save_expr_cache = 0;
4030 if (tlist_firstobj == 0)
4031 {
4032 gcc_obstack_init (&tlist_obstack);
4033 tlist_firstobj = obstack_alloc (&tlist_obstack, 0);
4034 }
4035
4036 verify_tree (expr, &before_sp, &after_sp, 0);
4037 warn_for_collisions (after_sp);
4038 obstack_free (&tlist_obstack, tlist_firstobj);
4039}
4040
4041tree
4042c_expand_expr_stmt (expr)
4043 tree expr;
4044{
4045 /* Do default conversion if safe and possibly important,
4046 in case within ({...}). */
4047 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
4048 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
4049 expr = default_conversion (expr);
4050
4051 if (warn_sequence_point)
4052 verify_sequence_points (expr);
4053
4054 if (TREE_TYPE (expr) != error_mark_node
4055 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
4056 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
4057 error ("expression statement has incomplete type");
4058
4059 last_expr_type = TREE_TYPE (expr);
4060 return add_stmt (build_stmt (EXPR_STMT, expr));
4061}
4062\f
4063/* Validate the expression after `case' and apply default promotions. */
4064
4065tree
4066check_case_value (value)
4067 tree value;
4068{
4069 if (value == NULL_TREE)
4070 return value;
4071
4072 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4073 STRIP_TYPE_NOPS (value);
4074 /* In C++, the following is allowed:
4075
4076 const int i = 3;
4077 switch (...) { case i: ... }
4078
4079 So, we try to reduce the VALUE to a constant that way. */
4080 if (c_language == clk_cplusplus)
4081 {
4082 value = decl_constant_value (value);
4083 STRIP_TYPE_NOPS (value);
4084 value = fold (value);
4085 }
4086
4087 if (TREE_CODE (value) != INTEGER_CST
4088 && value != error_mark_node)
4089 {
4090 error ("case label does not reduce to an integer constant");
4091 value = error_mark_node;
4092 }
4093 else
4094 /* Promote char or short to int. */
4095 value = default_conversion (value);
4096
4097 constant_expression_warning (value);
4098
4099 return value;
4100}
4101\f
4102/* Return an integer type with BITS bits of precision,
4103 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
4104
4105tree
4106type_for_size (bits, unsignedp)
4107 unsigned bits;
4108 int unsignedp;
4109{
4110 if (bits == TYPE_PRECISION (integer_type_node))
4111 return unsignedp ? unsigned_type_node : integer_type_node;
4112
4113 if (bits == TYPE_PRECISION (signed_char_type_node))
4114 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4115
4116 if (bits == TYPE_PRECISION (short_integer_type_node))
4117 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4118
4119 if (bits == TYPE_PRECISION (long_integer_type_node))
4120 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4121
4122 if (bits == TYPE_PRECISION (long_long_integer_type_node))
4123 return (unsignedp ? long_long_unsigned_type_node
4124 : long_long_integer_type_node);
4125
4126 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
4127 return (unsignedp ? widest_unsigned_literal_type_node
4128 : widest_integer_literal_type_node);
4129
4130 if (bits <= TYPE_PRECISION (intQI_type_node))
4131 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4132
4133 if (bits <= TYPE_PRECISION (intHI_type_node))
4134 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4135
4136 if (bits <= TYPE_PRECISION (intSI_type_node))
4137 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4138
4139 if (bits <= TYPE_PRECISION (intDI_type_node))
4140 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4141
4142 return 0;
4143}
4144
4145/* Return a data type that has machine mode MODE.
4146 If the mode is an integer,
4147 then UNSIGNEDP selects between signed and unsigned types. */
4148
4149tree
4150type_for_mode (mode, unsignedp)
4151 enum machine_mode mode;
4152 int unsignedp;
4153{
4154 if (mode == TYPE_MODE (integer_type_node))
4155 return unsignedp ? unsigned_type_node : integer_type_node;
4156
4157 if (mode == TYPE_MODE (signed_char_type_node))
4158 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4159
4160 if (mode == TYPE_MODE (short_integer_type_node))
4161 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4162
4163 if (mode == TYPE_MODE (long_integer_type_node))
4164 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4165
4166 if (mode == TYPE_MODE (long_long_integer_type_node))
4167 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
4168
4169 if (mode == TYPE_MODE (widest_integer_literal_type_node))
4170 return unsignedp ? widest_unsigned_literal_type_node
4171 : widest_integer_literal_type_node;
4172
4173 if (mode == TYPE_MODE (intQI_type_node))
4174 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4175
4176 if (mode == TYPE_MODE (intHI_type_node))
4177 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4178
4179 if (mode == TYPE_MODE (intSI_type_node))
4180 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4181
4182 if (mode == TYPE_MODE (intDI_type_node))
4183 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4184
4185#if HOST_BITS_PER_WIDE_INT >= 64
4186 if (mode == TYPE_MODE (intTI_type_node))
4187 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4188#endif
4189
4190 if (mode == TYPE_MODE (float_type_node))
4191 return float_type_node;
4192
4193 if (mode == TYPE_MODE (double_type_node))
4194 return double_type_node;
4195
4196 if (mode == TYPE_MODE (long_double_type_node))
4197 return long_double_type_node;
4198
4199 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
4200 return build_pointer_type (char_type_node);
4201
4202 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
4203 return build_pointer_type (integer_type_node);
4204
4205#ifdef VECTOR_MODE_SUPPORTED_P
4206 if (mode == TYPE_MODE (V4SF_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
4207 return V4SF_type_node;
4208 if (mode == TYPE_MODE (V4SI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
4209 return V4SI_type_node;
4210 if (mode == TYPE_MODE (V2SI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
4211 return V2SI_type_node;
4212 if (mode == TYPE_MODE (V4HI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
4213 return V4HI_type_node;
4214 if (mode == TYPE_MODE (V8QI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
4215 return V8QI_type_node;
4216#endif
4217
4218 return 0;
4219}
4220
4221/* Return an unsigned type the same as TYPE in other respects. */
4222tree
4223unsigned_type (type)
4224 tree type;
4225{
4226 tree type1 = TYPE_MAIN_VARIANT (type);
4227 if (type1 == signed_char_type_node || type1 == char_type_node)
4228 return unsigned_char_type_node;
4229 if (type1 == integer_type_node)
4230 return unsigned_type_node;
4231 if (type1 == short_integer_type_node)
4232 return short_unsigned_type_node;
4233 if (type1 == long_integer_type_node)
4234 return long_unsigned_type_node;
4235 if (type1 == long_long_integer_type_node)
4236 return long_long_unsigned_type_node;
4237 if (type1 == widest_integer_literal_type_node)
4238 return widest_unsigned_literal_type_node;
4239#if HOST_BITS_PER_WIDE_INT >= 64
4240 if (type1 == intTI_type_node)
4241 return unsigned_intTI_type_node;
4242#endif
4243 if (type1 == intDI_type_node)
4244 return unsigned_intDI_type_node;
4245 if (type1 == intSI_type_node)
4246 return unsigned_intSI_type_node;
4247 if (type1 == intHI_type_node)
4248 return unsigned_intHI_type_node;
4249 if (type1 == intQI_type_node)
4250 return unsigned_intQI_type_node;
4251
4252 return signed_or_unsigned_type (1, type);
4253}
4254
4255/* Return a signed type the same as TYPE in other respects. */
4256
4257tree
4258signed_type (type)
4259 tree type;
4260{
4261 tree type1 = TYPE_MAIN_VARIANT (type);
4262 if (type1 == unsigned_char_type_node || type1 == char_type_node)
4263 return signed_char_type_node;
4264 if (type1 == unsigned_type_node)
4265 return integer_type_node;
4266 if (type1 == short_unsigned_type_node)
4267 return short_integer_type_node;
4268 if (type1 == long_unsigned_type_node)
4269 return long_integer_type_node;
4270 if (type1 == long_long_unsigned_type_node)
4271 return long_long_integer_type_node;
4272 if (type1 == widest_unsigned_literal_type_node)
4273 return widest_integer_literal_type_node;
4274#if HOST_BITS_PER_WIDE_INT >= 64
4275 if (type1 == unsigned_intTI_type_node)
4276 return intTI_type_node;
4277#endif
4278 if (type1 == unsigned_intDI_type_node)
4279 return intDI_type_node;
4280 if (type1 == unsigned_intSI_type_node)
4281 return intSI_type_node;
4282 if (type1 == unsigned_intHI_type_node)
4283 return intHI_type_node;
4284 if (type1 == unsigned_intQI_type_node)
4285 return intQI_type_node;
4286
4287 return signed_or_unsigned_type (0, type);
4288}
4289
4290/* Return a type the same as TYPE except unsigned or
4291 signed according to UNSIGNEDP. */
4292
4293tree
4294signed_or_unsigned_type (unsignedp, type)
4295 int unsignedp;
4296 tree type;
4297{
4298 if (! INTEGRAL_TYPE_P (type)
4299 || TREE_UNSIGNED (type) == unsignedp)
4300 return type;
4301
4302 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
4303 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4304 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
4305 return unsignedp ? unsigned_type_node : integer_type_node;
4306 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
4307 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4308 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
4309 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4310 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
4311 return (unsignedp ? long_long_unsigned_type_node
4312 : long_long_integer_type_node);
4313 if (TYPE_PRECISION (type) == TYPE_PRECISION (widest_integer_literal_type_node))
4314 return (unsignedp ? widest_unsigned_literal_type_node
4315 : widest_integer_literal_type_node);
4316 return type;
4317}
4318\f
4319/* Return the minimum number of bits needed to represent VALUE in a
4320 signed or unsigned type, UNSIGNEDP says which. */
4321
4322unsigned int
4323min_precision (value, unsignedp)
4324 tree value;
4325 int unsignedp;
4326{
4327 int log;
4328
4329 /* If the value is negative, compute its negative minus 1. The latter
4330 adjustment is because the absolute value of the largest negative value
4331 is one larger than the largest positive value. This is equivalent to
4332 a bit-wise negation, so use that operation instead. */
4333
4334 if (tree_int_cst_sgn (value) < 0)
4335 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
4336
4337 /* Return the number of bits needed, taking into account the fact
4338 that we need one more bit for a signed than unsigned type. */
4339
4340 if (integer_zerop (value))
4341 log = 0;
4342 else
4343 log = tree_floor_log2 (value);
4344
4345 return log + 1 + ! unsignedp;
4346}
4347\f
4348/* Print an error message for invalid operands to arith operation CODE.
4349 NOP_EXPR is used as a special case (see truthvalue_conversion). */
4350
4351void
4352binary_op_error (code)
4353 enum tree_code code;
4354{
4355 register const char *opname;
4356
4357 switch (code)
4358 {
4359 case NOP_EXPR:
4360 error ("invalid truth-value expression");
4361 return;
4362
4363 case PLUS_EXPR:
4364 opname = "+"; break;
4365 case MINUS_EXPR:
4366 opname = "-"; break;
4367 case MULT_EXPR:
4368 opname = "*"; break;
4369 case MAX_EXPR:
4370 opname = "max"; break;
4371 case MIN_EXPR:
4372 opname = "min"; break;
4373 case EQ_EXPR:
4374 opname = "=="; break;
4375 case NE_EXPR:
4376 opname = "!="; break;
4377 case LE_EXPR:
4378 opname = "<="; break;
4379 case GE_EXPR:
4380 opname = ">="; break;
4381 case LT_EXPR:
4382 opname = "<"; break;
4383 case GT_EXPR:
4384 opname = ">"; break;
4385 case LSHIFT_EXPR:
4386 opname = "<<"; break;
4387 case RSHIFT_EXPR:
4388 opname = ">>"; break;
4389 case TRUNC_MOD_EXPR:
4390 case FLOOR_MOD_EXPR:
4391 opname = "%"; break;
4392 case TRUNC_DIV_EXPR:
4393 case FLOOR_DIV_EXPR:
4394 opname = "/"; break;
4395 case BIT_AND_EXPR:
4396 opname = "&"; break;
4397 case BIT_IOR_EXPR:
4398 opname = "|"; break;
4399 case TRUTH_ANDIF_EXPR:
4400 opname = "&&"; break;
4401 case TRUTH_ORIF_EXPR:
4402 opname = "||"; break;
4403 case BIT_XOR_EXPR:
4404 opname = "^"; break;
4405 case LROTATE_EXPR:
4406 case RROTATE_EXPR:
4407 opname = "rotate"; break;
4408 default:
4409 opname = "unknown"; break;
4410 }
4411 error ("invalid operands to binary %s", opname);
4412}
4413\f
4414/* Subroutine of build_binary_op, used for comparison operations.
4415 See if the operands have both been converted from subword integer types
4416 and, if so, perhaps change them both back to their original type.
4417 This function is also responsible for converting the two operands
4418 to the proper common type for comparison.
4419
4420 The arguments of this function are all pointers to local variables
4421 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4422 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4423
4424 If this function returns nonzero, it means that the comparison has
4425 a constant value. What this function returns is an expression for
4426 that value. */
4427
4428tree
4429shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
4430 tree *op0_ptr, *op1_ptr;
4431 tree *restype_ptr;
4432 enum tree_code *rescode_ptr;
4433{
4434 register tree type;
4435 tree op0 = *op0_ptr;
4436 tree op1 = *op1_ptr;
4437 int unsignedp0, unsignedp1;
4438 int real1, real2;
4439 tree primop0, primop1;
4440 enum tree_code code = *rescode_ptr;
4441
4442 /* Throw away any conversions to wider types
4443 already present in the operands. */
4444
4445 primop0 = get_narrower (op0, &unsignedp0);
4446 primop1 = get_narrower (op1, &unsignedp1);
4447
4448 /* Handle the case that OP0 does not *contain* a conversion
4449 but it *requires* conversion to FINAL_TYPE. */
4450
4451 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4452 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
4453 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4454 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
4455
4456 /* If one of the operands must be floated, we cannot optimize. */
4457 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4458 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4459
4460 /* If first arg is constant, swap the args (changing operation
4461 so value is preserved), for canonicalization. Don't do this if
4462 the second arg is 0. */
4463
4464 if (TREE_CONSTANT (primop0)
4465 && ! integer_zerop (primop1) && ! real_zerop (primop1))
4466 {
4467 register tree tem = primop0;
4468 register int temi = unsignedp0;
4469 primop0 = primop1;
4470 primop1 = tem;
4471 tem = op0;
4472 op0 = op1;
4473 op1 = tem;
4474 *op0_ptr = op0;
4475 *op1_ptr = op1;
4476 unsignedp0 = unsignedp1;
4477 unsignedp1 = temi;
4478 temi = real1;
4479 real1 = real2;
4480 real2 = temi;
4481
4482 switch (code)
4483 {
4484 case LT_EXPR:
4485 code = GT_EXPR;
4486 break;
4487 case GT_EXPR:
4488 code = LT_EXPR;
4489 break;
4490 case LE_EXPR:
4491 code = GE_EXPR;
4492 break;
4493 case GE_EXPR:
4494 code = LE_EXPR;
4495 break;
4496 default:
4497 break;
4498 }
4499 *rescode_ptr = code;
4500 }
4501
4502 /* If comparing an integer against a constant more bits wide,
4503 maybe we can deduce a value of 1 or 0 independent of the data.
4504 Or else truncate the constant now
4505 rather than extend the variable at run time.
4506
4507 This is only interesting if the constant is the wider arg.
4508 Also, it is not safe if the constant is unsigned and the
4509 variable arg is signed, since in this case the variable
4510 would be sign-extended and then regarded as unsigned.
4511 Our technique fails in this case because the lowest/highest
4512 possible unsigned results don't follow naturally from the
4513 lowest/highest possible values of the variable operand.
4514 For just EQ_EXPR and NE_EXPR there is another technique that
4515 could be used: see if the constant can be faithfully represented
4516 in the other operand's type, by truncating it and reextending it
4517 and see if that preserves the constant's value. */
4518
4519 if (!real1 && !real2
4520 && TREE_CODE (primop1) == INTEGER_CST
4521 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4522 {
4523 int min_gt, max_gt, min_lt, max_lt;
4524 tree maxval, minval;
4525 /* 1 if comparison is nominally unsigned. */
4526 int unsignedp = TREE_UNSIGNED (*restype_ptr);
4527 tree val;
4528
4529 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
4530
4531 /* If TYPE is an enumeration, then we need to get its min/max
4532 values from it's underlying integral type, not the enumerated
4533 type itself. */
4534 if (TREE_CODE (type) == ENUMERAL_TYPE)
4535 type = type_for_size (TYPE_PRECISION (type), unsignedp0);
4536
4537 maxval = TYPE_MAX_VALUE (type);
4538 minval = TYPE_MIN_VALUE (type);
4539
4540 if (unsignedp && !unsignedp0)
4541 *restype_ptr = signed_type (*restype_ptr);
4542
4543 if (TREE_TYPE (primop1) != *restype_ptr)
4544 primop1 = convert (*restype_ptr, primop1);
4545 if (type != *restype_ptr)
4546 {
4547 minval = convert (*restype_ptr, minval);
4548 maxval = convert (*restype_ptr, maxval);
4549 }
4550
4551 if (unsignedp && unsignedp0)
4552 {
4553 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
4554 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
4555 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
4556 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
4557 }
4558 else
4559 {
4560 min_gt = INT_CST_LT (primop1, minval);
4561 max_gt = INT_CST_LT (primop1, maxval);
4562 min_lt = INT_CST_LT (minval, primop1);
4563 max_lt = INT_CST_LT (maxval, primop1);
4564 }
4565
4566 val = 0;
4567 /* This used to be a switch, but Genix compiler can't handle that. */
4568 if (code == NE_EXPR)
4569 {
4570 if (max_lt || min_gt)
4571 val = boolean_true_node;
4572 }
4573 else if (code == EQ_EXPR)
4574 {
4575 if (max_lt || min_gt)
4576 val = boolean_false_node;
4577 }
4578 else if (code == LT_EXPR)
4579 {
4580 if (max_lt)
4581 val = boolean_true_node;
4582 if (!min_lt)
4583 val = boolean_false_node;
4584 }
4585 else if (code == GT_EXPR)
4586 {
4587 if (min_gt)
4588 val = boolean_true_node;
4589 if (!max_gt)
4590 val = boolean_false_node;
4591 }
4592 else if (code == LE_EXPR)
4593 {
4594 if (!max_gt)
4595 val = boolean_true_node;
4596 if (min_gt)
4597 val = boolean_false_node;
4598 }
4599 else if (code == GE_EXPR)
4600 {
4601 if (!min_lt)
4602 val = boolean_true_node;
4603 if (max_lt)
4604 val = boolean_false_node;
4605 }
4606
4607 /* If primop0 was sign-extended and unsigned comparison specd,
4608 we did a signed comparison above using the signed type bounds.
4609 But the comparison we output must be unsigned.
4610
4611 Also, for inequalities, VAL is no good; but if the signed
4612 comparison had *any* fixed result, it follows that the
4613 unsigned comparison just tests the sign in reverse
4614 (positive values are LE, negative ones GE).
4615 So we can generate an unsigned comparison
4616 against an extreme value of the signed type. */
4617
4618 if (unsignedp && !unsignedp0)
4619 {
4620 if (val != 0)
4621 switch (code)
4622 {
4623 case LT_EXPR:
4624 case GE_EXPR:
4625 primop1 = TYPE_MIN_VALUE (type);
4626 val = 0;
4627 break;
4628
4629 case LE_EXPR:
4630 case GT_EXPR:
4631 primop1 = TYPE_MAX_VALUE (type);
4632 val = 0;
4633 break;
4634
4635 default:
4636 break;
4637 }
4638 type = unsigned_type (type);
4639 }
4640
4641 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
4642 {
4643 /* This is the case of (char)x >?< 0x80, which people used to use
4644 expecting old C compilers to change the 0x80 into -0x80. */
4645 if (val == boolean_false_node)
4646 warning ("comparison is always false due to limited range of data type");
4647 if (val == boolean_true_node)
4648 warning ("comparison is always true due to limited range of data type");
4649 }
4650
4651 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
4652 {
4653 /* This is the case of (unsigned char)x >?< -1 or < 0. */
4654 if (val == boolean_false_node)
4655 warning ("comparison is always false due to limited range of data type");
4656 if (val == boolean_true_node)
4657 warning ("comparison is always true due to limited range of data type");
4658 }
4659
4660 if (val != 0)
4661 {
4662 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4663 if (TREE_SIDE_EFFECTS (primop0))
4664 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4665 return val;
4666 }
4667
4668 /* Value is not predetermined, but do the comparison
4669 in the type of the operand that is not constant.
4670 TYPE is already properly set. */
4671 }
4672 else if (real1 && real2
4673 && (TYPE_PRECISION (TREE_TYPE (primop0))
4674 == TYPE_PRECISION (TREE_TYPE (primop1))))
4675 type = TREE_TYPE (primop0);
4676
4677 /* If args' natural types are both narrower than nominal type
4678 and both extend in the same manner, compare them
4679 in the type of the wider arg.
4680 Otherwise must actually extend both to the nominal
4681 common type lest different ways of extending
4682 alter the result.
4683 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4684
4685 else if (unsignedp0 == unsignedp1 && real1 == real2
4686 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4687 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4688 {
4689 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4690 type = signed_or_unsigned_type (unsignedp0
4691 || TREE_UNSIGNED (*restype_ptr),
4692 type);
4693 /* Make sure shorter operand is extended the right way
4694 to match the longer operand. */
4695 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
4696 primop0);
4697 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
4698 primop1);
4699 }
4700 else
4701 {
4702 /* Here we must do the comparison on the nominal type
4703 using the args exactly as we received them. */
4704 type = *restype_ptr;
4705 primop0 = op0;
4706 primop1 = op1;
4707
4708 if (!real1 && !real2 && integer_zerop (primop1)
4709 && TREE_UNSIGNED (*restype_ptr))
4710 {
4711 tree value = 0;
4712 switch (code)
4713 {
4714 case GE_EXPR:
4715 /* All unsigned values are >= 0, so we warn if extra warnings
4716 are requested. However, if OP0 is a constant that is
4717 >= 0, the signedness of the comparison isn't an issue,
4718 so suppress the warning. */
4719 if (extra_warnings && !in_system_header
4720 && ! (TREE_CODE (primop0) == INTEGER_CST
4721 && ! TREE_OVERFLOW (convert (signed_type (type),
4722 primop0))))
4723 warning ("comparison of unsigned expression >= 0 is always true");
4724 value = boolean_true_node;
4725 break;
4726
4727 case LT_EXPR:
4728 if (extra_warnings && !in_system_header
4729 && ! (TREE_CODE (primop0) == INTEGER_CST
4730 && ! TREE_OVERFLOW (convert (signed_type (type),
4731 primop0))))
4732 warning ("comparison of unsigned expression < 0 is always false");
4733 value = boolean_false_node;
4734 break;
4735
4736 default:
4737 break;
4738 }
4739
4740 if (value != 0)
4741 {
4742 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4743 if (TREE_SIDE_EFFECTS (primop0))
4744 return build (COMPOUND_EXPR, TREE_TYPE (value),
4745 primop0, value);
4746 return value;
4747 }
4748 }
4749 }
4750
4751 *op0_ptr = convert (type, primop0);
4752 *op1_ptr = convert (type, primop1);
4753
4754 *restype_ptr = boolean_type_node;
4755
4756 return 0;
4757}
4758\f
4759/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4760 or validate its data type for an `if' or `while' statement or ?..: exp.
4761
4762 This preparation consists of taking the ordinary
4763 representation of an expression expr and producing a valid tree
4764 boolean expression describing whether expr is nonzero. We could
4765 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
4766 but we optimize comparisons, &&, ||, and !.
4767
4768 The resulting type should always be `boolean_type_node'. */
4769
4770tree
4771truthvalue_conversion (expr)
4772 tree expr;
4773{
4774 if (TREE_CODE (expr) == ERROR_MARK)
4775 return expr;
4776
4777#if 0 /* This appears to be wrong for C++. */
4778 /* These really should return error_mark_node after 2.4 is stable.
4779 But not all callers handle ERROR_MARK properly. */
4780 switch (TREE_CODE (TREE_TYPE (expr)))
4781 {
4782 case RECORD_TYPE:
4783 error ("struct type value used where scalar is required");
4784 return boolean_false_node;
4785
4786 case UNION_TYPE:
4787 error ("union type value used where scalar is required");
4788 return boolean_false_node;
4789
4790 case ARRAY_TYPE:
4791 error ("array type value used where scalar is required");
4792 return boolean_false_node;
4793
4794 default:
4795 break;
4796 }
4797#endif /* 0 */
4798
4799 switch (TREE_CODE (expr))
4800 {
4801 case EQ_EXPR:
4802 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4803 case TRUTH_ANDIF_EXPR:
4804 case TRUTH_ORIF_EXPR:
4805 case TRUTH_AND_EXPR:
4806 case TRUTH_OR_EXPR:
4807 case TRUTH_XOR_EXPR:
4808 case TRUTH_NOT_EXPR:
4809 TREE_TYPE (expr) = boolean_type_node;
4810 return expr;
4811
4812 case ERROR_MARK:
4813 return expr;
4814
4815 case INTEGER_CST:
4816 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
4817
4818 case REAL_CST:
4819 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
4820
4821 case ADDR_EXPR:
4822 /* If we are taking the address of a external decl, it might be zero
4823 if it is weak, so we cannot optimize. */
4824 if (DECL_P (TREE_OPERAND (expr, 0))
4825 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
4826 break;
4827
4828 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
4829 return build (COMPOUND_EXPR, boolean_type_node,
4830 TREE_OPERAND (expr, 0), boolean_true_node);
4831 else
4832 return boolean_true_node;
4833
4834 case COMPLEX_EXPR:
4835 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4836 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4837 truthvalue_conversion (TREE_OPERAND (expr, 0)),
4838 truthvalue_conversion (TREE_OPERAND (expr, 1)),
4839 0);
4840
4841 case NEGATE_EXPR:
4842 case ABS_EXPR:
4843 case FLOAT_EXPR:
4844 case FFS_EXPR:
4845 /* These don't change whether an object is non-zero or zero. */
4846 return truthvalue_conversion (TREE_OPERAND (expr, 0));
4847
4848 case LROTATE_EXPR:
4849 case RROTATE_EXPR:
4850 /* These don't change whether an object is zero or non-zero, but
4851 we can't ignore them if their second arg has side-effects. */
4852 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4853 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
4854 truthvalue_conversion (TREE_OPERAND (expr, 0)));
4855 else
4856 return truthvalue_conversion (TREE_OPERAND (expr, 0));
4857
4858 case COND_EXPR:
4859 /* Distribute the conversion into the arms of a COND_EXPR. */
4860 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
4861 truthvalue_conversion (TREE_OPERAND (expr, 1)),
4862 truthvalue_conversion (TREE_OPERAND (expr, 2))));
4863
4864 case CONVERT_EXPR:
4865 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4866 since that affects how `default_conversion' will behave. */
4867 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
4868 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
4869 break;
4870 /* fall through... */
4871 case NOP_EXPR:
4872 /* If this is widening the argument, we can ignore it. */
4873 if (TYPE_PRECISION (TREE_TYPE (expr))
4874 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
4875 return truthvalue_conversion (TREE_OPERAND (expr, 0));
4876 break;
4877
4878 case MINUS_EXPR:
4879 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
4880 this case. */
4881 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
4882 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
4883 break;
4884 /* fall through... */
4885 case BIT_XOR_EXPR:
4886 /* This and MINUS_EXPR can be changed into a comparison of the
4887 two objects. */
4888 if (TREE_TYPE (TREE_OPERAND (expr, 0))
4889 == TREE_TYPE (TREE_OPERAND (expr, 1)))
4890 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
4891 TREE_OPERAND (expr, 1), 1);
4892 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
4893 fold (build1 (NOP_EXPR,
4894 TREE_TYPE (TREE_OPERAND (expr, 0)),
4895 TREE_OPERAND (expr, 1))), 1);
4896
4897 case BIT_AND_EXPR:
4898 if (integer_onep (TREE_OPERAND (expr, 1))
4899 && TREE_TYPE (expr) != boolean_type_node)
4900 /* Using convert here would cause infinite recursion. */
4901 return build1 (NOP_EXPR, boolean_type_node, expr);
4902 break;
4903
4904 case MODIFY_EXPR:
4905 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
4906 warning ("suggest parentheses around assignment used as truth value");
4907 break;
4908
4909 default:
4910 break;
4911 }
4912
4913 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4914 {
4915 tree tem = save_expr (expr);
4916 return (build_binary_op
4917 ((TREE_SIDE_EFFECTS (expr)
4918 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4919 truthvalue_conversion (build_unary_op (REALPART_EXPR, tem, 0)),
4920 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, tem, 0)),
4921 0));
4922 }
4923
4924 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
4925}
4926\f
4927static tree builtin_function_2 PARAMS ((const char *, const char *, tree, tree,
4928 int, enum built_in_class, int, int,
4929 int));
4930
4931/* Make a variant type in the proper way for C/C++, propagating qualifiers
4932 down to the element type of an array. */
4933
4934tree
4935c_build_qualified_type (type, type_quals)
4936 tree type;
4937 int type_quals;
4938{
4939 /* A restrict-qualified pointer type must be a pointer to object or
4940 incomplete type. Note that the use of POINTER_TYPE_P also allows
4941 REFERENCE_TYPEs, which is appropriate for C++. Unfortunately,
4942 the C++ front-end also use POINTER_TYPE for pointer-to-member
4943 values, so even though it should be illegal to use `restrict'
4944 with such an entity we don't flag that here. Thus, special case
4945 code for that case is required in the C++ front-end. */
4946 if ((type_quals & TYPE_QUAL_RESTRICT)
4947 && (!POINTER_TYPE_P (type)
4948 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
4949 {
4950 error ("invalid use of `restrict'");
4951 type_quals &= ~TYPE_QUAL_RESTRICT;
4952 }
4953
4954 if (TREE_CODE (type) == ARRAY_TYPE)
4955 return build_array_type (c_build_qualified_type (TREE_TYPE (type),
4956 type_quals),
4957 TYPE_DOMAIN (type));
4958 return build_qualified_type (type, type_quals);
4959}
4960
4961/* Apply the TYPE_QUALS to the new DECL. */
4962
4963void
4964c_apply_type_quals_to_decl (type_quals, decl)
4965 int type_quals;
4966 tree decl;
4967{
4968 if ((type_quals & TYPE_QUAL_CONST)
4969 || (TREE_TYPE (decl)
4970 && TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE))
4971 TREE_READONLY (decl) = 1;
4972 if (type_quals & TYPE_QUAL_VOLATILE)
4973 {
4974 TREE_SIDE_EFFECTS (decl) = 1;
4975 TREE_THIS_VOLATILE (decl) = 1;
4976 }
4977 if (type_quals & TYPE_QUAL_RESTRICT)
4978 {
4979 if (!TREE_TYPE (decl)
4980 || !POINTER_TYPE_P (TREE_TYPE (decl))
4981 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl))))
4982 error ("invalid use of `restrict'");
4983 else if (flag_strict_aliasing)
4984 {
4985 /* No two restricted pointers can point at the same thing.
4986 However, a restricted pointer can point at the same thing
4987 as an unrestricted pointer, if that unrestricted pointer
4988 is based on the restricted pointer. So, we make the
4989 alias set for the restricted pointer a subset of the
4990 alias set for the type pointed to by the type of the
4991 decl. */
4992
4993 HOST_WIDE_INT pointed_to_alias_set
4994 = get_alias_set (TREE_TYPE (TREE_TYPE (decl)));
4995
4996 if (pointed_to_alias_set == 0)
4997 /* It's not legal to make a subset of alias set zero. */
4998 ;
4999 else
5000 {
5001 DECL_POINTER_ALIAS_SET (decl) = new_alias_set ();
5002 record_alias_subset (pointed_to_alias_set,
5003 DECL_POINTER_ALIAS_SET (decl));
5004 }
5005 }
5006 }
5007}
5008
5009
5010/* Return the typed-based alias set for T, which may be an expression
5011 or a type. Return -1 if we don't do anything special. */
5012
5013HOST_WIDE_INT
5014lang_get_alias_set (t)
5015 tree t;
5016{
5017 tree u;
5018
5019 /* Permit type-punning when accessing a union, provided the access
5020 is directly through the union. For example, this code does not
5021 permit taking the address of a union member and then storing
5022 through it. Even the type-punning allowed here is a GCC
5023 extension, albeit a common and useful one; the C standard says
5024 that such accesses have implementation-defined behavior. */
5025 for (u = t;
5026 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
5027 u = TREE_OPERAND (u, 0))
5028 if (TREE_CODE (u) == COMPONENT_REF
5029 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
5030 return 0;
5031
5032 /* If this is a char *, the ANSI C standard says it can alias
5033 anything. Note that all references need do this. */
5034 if (TREE_CODE_CLASS (TREE_CODE (t)) == 'r'
5035 && TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
5036 && TYPE_PRECISION (TREE_TYPE (t)) == TYPE_PRECISION (char_type_node))
5037 return 0;
5038
5039 /* That's all the expressions we handle specially. */
5040 if (! TYPE_P (t))
5041 return -1;
5042
5043 /* The C standard specifically allows aliasing between signed and
5044 unsigned variants of the same type. We treat the signed
5045 variant as canonical. */
5046 if (TREE_CODE (t) == INTEGER_TYPE && TREE_UNSIGNED (t))
5047 {
5048 tree t1 = signed_type (t);
5049
5050 /* t1 == t can happen for boolean nodes which are always unsigned. */
5051 if (t1 != t)
5052 return get_alias_set (t1);
5053 }
5054 else if (POINTER_TYPE_P (t))
5055 {
5056 tree t1;
5057
5058 /* Unfortunately, there is no canonical form of a pointer type.
5059 In particular, if we have `typedef int I', then `int *', and
5060 `I *' are different types. So, we have to pick a canonical
5061 representative. We do this below.
5062
5063 Technically, this approach is actually more conservative that
5064 it needs to be. In particular, `const int *' and `int *'
5065 chould be in different alias sets, according to the C and C++
5066 standard, since their types are not the same, and so,
5067 technically, an `int **' and `const int **' cannot point at
5068 the same thing.
5069
5070 But, the standard is wrong. In particular, this code is
5071 legal C++:
5072
5073 int *ip;
5074 int **ipp = &ip;
5075 const int* const* cipp = &ip;
5076
5077 And, it doesn't make sense for that to be legal unless you
5078 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
5079 the pointed-to types. This issue has been reported to the
5080 C++ committee. */
5081 t1 = build_type_no_quals (t);
5082 if (t1 != t)
5083 return get_alias_set (t1);
5084 }
5085 /* It's not yet safe to use alias sets for classes in C++ because
5086 the TYPE_FIELDs list for a class doesn't mention base classes. */
5087 else if (c_language == clk_cplusplus && AGGREGATE_TYPE_P (t))
5088 return 0;
5089
5090 return -1;
5091}
5092
5093/* Build tree nodes and builtin functions common to both C and C++ language
5094 frontends. */
5095
5096void
5097c_common_nodes_and_builtins ()
5098{
5099 tree temp;
5100 tree memcpy_ftype, memset_ftype, strlen_ftype;
5101 tree bzero_ftype, bcmp_ftype, puts_ftype, printf_ftype;
5102 tree fputs_ftype, fputc_ftype, fwrite_ftype;
5103 tree endlink, int_endlink, double_endlink, unsigned_endlink;
5104 tree cstring_endlink, sizetype_endlink;
5105 tree ptr_ftype, ptr_ftype_unsigned;
5106 tree void_ftype_any, void_ftype_int, int_ftype_any;
5107 tree double_ftype_double, double_ftype_double_double;
5108 tree float_ftype_float, ldouble_ftype_ldouble;
5109 tree int_ftype_cptr_cptr_sizet, sizet_ftype_cstring_cstring;
5110 tree int_ftype_cstring_cstring, string_ftype_string_cstring;
5111 tree string_ftype_cstring_int, string_ftype_cstring_cstring;
5112 tree string_ftype_string_cstring_sizet, int_ftype_cstring_cstring_sizet;
5113 tree long_ftype_long;
5114 tree longlong_ftype_longlong;
5115 tree intmax_ftype_intmax;
5116 /* Either char* or void*. */
5117 tree traditional_ptr_type_node;
5118 /* Either const char* or const void*. */
5119 tree traditional_cptr_type_node;
5120 tree traditional_len_type_node;
5121 tree traditional_len_endlink;
5122 tree va_list_ref_type_node;
5123 tree va_list_arg_type_node;
5124
5125 string_type_node = build_pointer_type (char_type_node);
5126 const_string_type_node
5127 = build_pointer_type (build_type_variant (char_type_node, 1, 0));
5128
5129 wint_type_node =
5130 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5131
5132 intmax_type_node =
5133 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5134 uintmax_type_node =
5135 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5136
5137 default_function_type = build_function_type (integer_type_node, NULL_TREE);
5138 ptrdiff_type_node
5139 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5140 unsigned_ptrdiff_type_node = unsigned_type (ptrdiff_type_node);
5141
5142 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_va_list"),
5143 va_list_type_node));
5144
5145 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_ptrdiff_t"),
5146 ptrdiff_type_node));
5147
5148 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_size_t"),
5149 sizetype));
5150
5151 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5152 {
5153 va_list_arg_type_node = va_list_ref_type_node =
5154 build_pointer_type (TREE_TYPE (va_list_type_node));
5155 }
5156 else
5157 {
5158 va_list_arg_type_node = va_list_type_node;
5159 va_list_ref_type_node = build_reference_type (va_list_type_node);
5160 }
5161
5162 endlink = void_list_node;
5163 int_endlink = tree_cons (NULL_TREE, integer_type_node, endlink);
5164 double_endlink = tree_cons (NULL_TREE, double_type_node, endlink);
5165 unsigned_endlink = tree_cons (NULL_TREE, unsigned_type_node, endlink);
5166 cstring_endlink = tree_cons (NULL_TREE, const_string_type_node, endlink);
5167
5168 ptr_ftype = build_function_type (ptr_type_node, NULL_TREE);
5169 ptr_ftype_unsigned = build_function_type (ptr_type_node, unsigned_endlink);
5170 sizetype_endlink = tree_cons (NULL_TREE, TYPE_DOMAIN (sizetype), endlink);
5171 /* We realloc here because sizetype could be int or unsigned. S'ok. */
5172 ptr_ftype_sizetype = build_function_type (ptr_type_node, sizetype_endlink);
5173
5174 int_ftype_any = build_function_type (integer_type_node, NULL_TREE);
5175 void_ftype_any = build_function_type (void_type_node, NULL_TREE);
5176 void_ftype = build_function_type (void_type_node, endlink);
5177 void_ftype_int = build_function_type (void_type_node, int_endlink);
5178 void_ftype_ptr
5179 = build_function_type (void_type_node,
5180 tree_cons (NULL_TREE, ptr_type_node, endlink));
5181
5182 float_ftype_float
5183 = build_function_type (float_type_node,
5184 tree_cons (NULL_TREE, float_type_node, endlink));
5185
5186 double_ftype_double
5187 = build_function_type (double_type_node, double_endlink);
5188
5189 ldouble_ftype_ldouble
5190 = build_function_type (long_double_type_node,
5191 tree_cons (NULL_TREE, long_double_type_node,
5192 endlink));
5193
5194 double_ftype_double_double
5195 = build_function_type (double_type_node,
5196 tree_cons (NULL_TREE, double_type_node,
5197 double_endlink));
5198
5199 int_ftype_int
5200 = build_function_type (integer_type_node, int_endlink);
5201
5202 long_ftype_long
5203 = build_function_type (long_integer_type_node,
5204 tree_cons (NULL_TREE, long_integer_type_node,
5205 endlink));
5206
5207 longlong_ftype_longlong
5208 = build_function_type (long_long_integer_type_node,
5209 tree_cons (NULL_TREE, long_long_integer_type_node,
5210 endlink));
5211
5212 intmax_ftype_intmax
5213 = build_function_type (intmax_type_node,
5214 tree_cons (NULL_TREE, intmax_type_node,
5215 endlink));
5216
5217 int_ftype_cptr_cptr_sizet
5218 = build_function_type (integer_type_node,
5219 tree_cons (NULL_TREE, const_ptr_type_node,
5220 tree_cons (NULL_TREE,
5221 const_ptr_type_node,
5222 sizetype_endlink)));
5223
5224 void_zero_node = build_int_2 (0, 0);
5225 TREE_TYPE (void_zero_node) = void_type_node;
5226
5227 /* Prototype for strcpy/strcat. */
5228 string_ftype_string_cstring
5229 = build_function_type (string_type_node,
5230 tree_cons (NULL_TREE, string_type_node,
5231 cstring_endlink));
5232
5233 /* Prototype for strncpy/strncat. */
5234 string_ftype_string_cstring_sizet
5235 = build_function_type (string_type_node,
5236 tree_cons (NULL_TREE, string_type_node,
5237 tree_cons (NULL_TREE,
5238 const_string_type_node,
5239 sizetype_endlink)));
5240
5241 traditional_len_type_node = ((flag_traditional &&
5242 c_language != clk_cplusplus)
5243 ? integer_type_node : sizetype);
5244 traditional_len_endlink = tree_cons (NULL_TREE, traditional_len_type_node,
5245 endlink);
5246
5247 /* Prototype for strcmp. */
5248 int_ftype_cstring_cstring
5249 = build_function_type (integer_type_node,
5250 tree_cons (NULL_TREE, const_string_type_node,
5251 cstring_endlink));
5252
5253 /* Prototype for strspn/strcspn. */
5254 sizet_ftype_cstring_cstring
5255 = build_function_type (c_size_type_node,
5256 tree_cons (NULL_TREE, const_string_type_node,
5257 cstring_endlink));
5258
5259 /* Prototype for strncmp. */
5260 int_ftype_cstring_cstring_sizet
5261 = build_function_type (integer_type_node,
5262 tree_cons (NULL_TREE, const_string_type_node,
5263 tree_cons (NULL_TREE,
5264 const_string_type_node,
5265 sizetype_endlink)));
5266
5267 /* Prototype for strstr, strpbrk, etc. */
5268 string_ftype_cstring_cstring
5269 = build_function_type (string_type_node,
5270 tree_cons (NULL_TREE, const_string_type_node,
5271 cstring_endlink));
5272
5273 /* Prototype for strchr. */
5274 string_ftype_cstring_int
5275 = build_function_type (string_type_node,
5276 tree_cons (NULL_TREE, const_string_type_node,
5277 int_endlink));
5278
5279 /* Prototype for strlen. */
5280 strlen_ftype
5281 = build_function_type (traditional_len_type_node, cstring_endlink);
5282
5283 traditional_ptr_type_node = ((flag_traditional &&
5284 c_language != clk_cplusplus)
5285 ? string_type_node : ptr_type_node);
5286 traditional_cptr_type_node = ((flag_traditional &&
5287 c_language != clk_cplusplus)
5288 ? const_string_type_node : const_ptr_type_node);
5289
5290 /* Prototype for memcpy. */
5291 memcpy_ftype
5292 = build_function_type (traditional_ptr_type_node,
5293 tree_cons (NULL_TREE, ptr_type_node,
5294 tree_cons (NULL_TREE, const_ptr_type_node,
5295 sizetype_endlink)));
5296
5297 /* Prototype for memset. */
5298 memset_ftype
5299 = build_function_type (traditional_ptr_type_node,
5300 tree_cons (NULL_TREE, ptr_type_node,
5301 tree_cons (NULL_TREE, integer_type_node,
5302 sizetype_endlink)));
5303
5304 /* Prototype for bzero. */
5305 bzero_ftype
5306 = build_function_type (void_type_node,
5307 tree_cons (NULL_TREE, traditional_ptr_type_node,
5308 traditional_len_endlink));
5309
5310 /* Prototype for bcmp. */
5311 bcmp_ftype
5312 = build_function_type (integer_type_node,
5313 tree_cons (NULL_TREE, traditional_cptr_type_node,
5314 tree_cons (NULL_TREE,
5315 traditional_cptr_type_node,
5316 traditional_len_endlink)));
5317
5318 /* Prototype for puts. */
5319 puts_ftype
5320 = build_function_type (integer_type_node, cstring_endlink);
5321
5322 /* Prototype for printf. */
5323 printf_ftype
5324 = build_function_type (integer_type_node,
5325 tree_cons (NULL_TREE, const_string_type_node,
5326 NULL_TREE));
5327
5328 /* These stdio prototypes are declared using void* in place of
5329 FILE*. They are only used for __builtin_ style calls, regular
5330 style builtin prototypes omit the arguments and merge those
5331 provided by stdio.h. */
5332 /* Prototype for fwrite. */
5333 fwrite_ftype
5334 = build_function_type (c_size_type_node,
5335 tree_cons (NULL_TREE, const_ptr_type_node,
5336 tree_cons (NULL_TREE, c_size_type_node,
5337 tree_cons (NULL_TREE, c_size_type_node,
5338 tree_cons (NULL_TREE, ptr_type_node, endlink)))));
5339
5340 /* Prototype for fputc. */
5341 fputc_ftype
5342 = build_function_type (integer_type_node,
5343 tree_cons (NULL_TREE, integer_type_node,
5344 tree_cons (NULL_TREE, ptr_type_node, endlink)));
5345
5346 /* Prototype for fputs. */
5347 fputs_ftype
5348 = build_function_type (integer_type_node,
5349 tree_cons (NULL_TREE, const_string_type_node,
5350 tree_cons (NULL_TREE, ptr_type_node, endlink)));
5351
5352 builtin_function ("__builtin_constant_p", default_function_type,
5353 BUILT_IN_CONSTANT_P, BUILT_IN_NORMAL, NULL_PTR);
5354
5355 builtin_function ("__builtin_return_address", ptr_ftype_unsigned,
5356 BUILT_IN_RETURN_ADDRESS, BUILT_IN_NORMAL, NULL_PTR);
5357
5358 builtin_function ("__builtin_frame_address", ptr_ftype_unsigned,
5359 BUILT_IN_FRAME_ADDRESS, BUILT_IN_NORMAL, NULL_PTR);
5360
5361 builtin_function ("__builtin_alloca", ptr_ftype_sizetype,
5362 BUILT_IN_ALLOCA, BUILT_IN_NORMAL, "alloca");
5363 builtin_function_2 ("__builtin_ffs", "ffs",
5364 int_ftype_int, int_ftype_int,
5365 BUILT_IN_FFS, BUILT_IN_NORMAL, 0, 1, 0);
5366 /* Define alloca as builtin, unless SMALL_STACK. */
5367#ifndef SMALL_STACK
5368 builtin_function_2 (NULL_PTR, "alloca", NULL_TREE, ptr_ftype_sizetype,
5369 BUILT_IN_ALLOCA, BUILT_IN_NORMAL, 0, 1, 0);
5370#endif
5371 /* Declare _exit just to mark it as non-returning. */
5372 builtin_function_2 (NULL_PTR, "_exit", NULL_TREE, void_ftype_int,
5373 0, NOT_BUILT_IN, 0, 1, 1);
5374
5375 builtin_function_2 ("__builtin_index", "index",
5376 string_ftype_cstring_int, string_ftype_cstring_int,
5377 BUILT_IN_INDEX, BUILT_IN_NORMAL, 1, 1, 0);
5378 builtin_function_2 ("__builtin_rindex", "rindex",
5379 string_ftype_cstring_int, string_ftype_cstring_int,
5380 BUILT_IN_RINDEX, BUILT_IN_NORMAL, 1, 1, 0);
5381
5382 /* The system prototypes for these functions have many
5383 variations, so don't specify parameters to avoid conflicts.
5384 The expand_* functions check the argument types anyway. */
5385 builtin_function_2 ("__builtin_bzero", "bzero",
5386 bzero_ftype, void_ftype_any,
5387 BUILT_IN_BZERO, BUILT_IN_NORMAL, 1, 1, 0);
5388 builtin_function_2 ("__builtin_bcmp", "bcmp",
5389 bcmp_ftype, int_ftype_any,
5390 BUILT_IN_BCMP, BUILT_IN_NORMAL, 1, 1, 0);
5391
5392 builtin_function_2 ("__builtin_abs", "abs",
5393 int_ftype_int, int_ftype_int,
5394 BUILT_IN_ABS, BUILT_IN_NORMAL, 0, 0, 0);
5395 builtin_function_2 ("__builtin_fabsf", "fabsf",
5396 float_ftype_float, float_ftype_float,
5397 BUILT_IN_FABS, BUILT_IN_NORMAL, 0, 0, 0);
5398 builtin_function_2 ("__builtin_fabs", "fabs",
5399 double_ftype_double, double_ftype_double,
5400 BUILT_IN_FABS, BUILT_IN_NORMAL, 0, 0, 0);
5401 builtin_function_2 ("__builtin_fabsl", "fabsl",
5402 ldouble_ftype_ldouble, ldouble_ftype_ldouble,
5403 BUILT_IN_FABS, BUILT_IN_NORMAL, 0, 0, 0);
5404 builtin_function_2 ("__builtin_labs", "labs",
5405 long_ftype_long, long_ftype_long,
5406 BUILT_IN_ABS, BUILT_IN_NORMAL, 0, 0, 0);
5407 builtin_function_2 ("__builtin_llabs", "llabs",
5408 longlong_ftype_longlong, longlong_ftype_longlong,
5409 BUILT_IN_ABS, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
5410 builtin_function_2 ("__builtin_imaxabs", "imaxabs",
5411 intmax_ftype_intmax, intmax_ftype_intmax,
5412 BUILT_IN_ABS, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
5413
5414 builtin_function ("__builtin_saveregs", ptr_ftype, BUILT_IN_SAVEREGS,
5415 BUILT_IN_NORMAL, NULL_PTR);
5416 builtin_function ("__builtin_classify_type", default_function_type,
5417 BUILT_IN_CLASSIFY_TYPE, BUILT_IN_NORMAL, NULL_PTR);
5418 builtin_function ("__builtin_next_arg", ptr_ftype, BUILT_IN_NEXT_ARG,
5419 BUILT_IN_NORMAL, NULL_PTR);
5420 builtin_function ("__builtin_args_info", int_ftype_int, BUILT_IN_ARGS_INFO,
5421 BUILT_IN_NORMAL, NULL_PTR);
5422 builtin_function ("__builtin_setjmp",
5423 build_function_type (integer_type_node,
5424 tree_cons (NULL_TREE, ptr_type_node,
5425 endlink)),
5426 BUILT_IN_SETJMP, BUILT_IN_NORMAL, NULL_PTR);
5427 builtin_function ("__builtin_longjmp",
5428 build_function_type (void_type_node,
5429 tree_cons (NULL_TREE, ptr_type_node,
5430 int_endlink)),
5431 BUILT_IN_LONGJMP, BUILT_IN_NORMAL, NULL_PTR);
5432 builtin_function ("__builtin_trap", void_ftype, BUILT_IN_TRAP,
5433 BUILT_IN_NORMAL, NULL_PTR);
5434
5435 /* ISO C99 IEEE Unordered compares. */
5436 builtin_function ("__builtin_isgreater", default_function_type,
5437 BUILT_IN_ISGREATER, BUILT_IN_NORMAL, NULL_PTR);
5438 builtin_function ("__builtin_isgreaterequal", default_function_type,
5439 BUILT_IN_ISGREATEREQUAL, BUILT_IN_NORMAL, NULL_PTR);
5440 builtin_function ("__builtin_isless", default_function_type,
5441 BUILT_IN_ISLESS, BUILT_IN_NORMAL, NULL_PTR);
5442 builtin_function ("__builtin_islessequal", default_function_type,
5443 BUILT_IN_ISLESSEQUAL, BUILT_IN_NORMAL, NULL_PTR);
5444 builtin_function ("__builtin_islessgreater", default_function_type,
5445 BUILT_IN_ISLESSGREATER, BUILT_IN_NORMAL, NULL_PTR);
5446 builtin_function ("__builtin_isunordered", default_function_type,
5447 BUILT_IN_ISUNORDERED, BUILT_IN_NORMAL, NULL_PTR);
5448
5449 /* Untyped call and return. */
5450 builtin_function ("__builtin_apply_args", ptr_ftype,
5451 BUILT_IN_APPLY_ARGS, BUILT_IN_NORMAL, NULL_PTR);
5452
5453 temp = tree_cons (NULL_TREE,
5454 build_pointer_type (build_function_type (void_type_node,
5455 NULL_TREE)),
5456 tree_cons (NULL_TREE, ptr_type_node, sizetype_endlink));
5457 builtin_function ("__builtin_apply",
5458 build_function_type (ptr_type_node, temp),
5459 BUILT_IN_APPLY, BUILT_IN_NORMAL, NULL_PTR);
5460 builtin_function ("__builtin_return", void_ftype_ptr,
5461 BUILT_IN_RETURN, BUILT_IN_NORMAL, NULL_PTR);
5462
5463 /* Support for varargs.h and stdarg.h. */
5464 builtin_function ("__builtin_varargs_start",
5465 build_function_type (void_type_node,
5466 tree_cons (NULL_TREE,
5467 va_list_ref_type_node,
5468 endlink)),
5469 BUILT_IN_VARARGS_START, BUILT_IN_NORMAL, NULL_PTR);
5470
5471 builtin_function ("__builtin_stdarg_start",
5472 build_function_type (void_type_node,
5473 tree_cons (NULL_TREE,
5474 va_list_ref_type_node,
5475 NULL_TREE)),
5476 BUILT_IN_STDARG_START, BUILT_IN_NORMAL, NULL_PTR);
5477
5478 builtin_function ("__builtin_va_end",
5479 build_function_type (void_type_node,
5480 tree_cons (NULL_TREE,
5481 va_list_ref_type_node,
5482 endlink)),
5483 BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL_PTR);
5484
5485 builtin_function ("__builtin_va_copy",
5486 build_function_type (void_type_node,
5487 tree_cons (NULL_TREE,
5488 va_list_ref_type_node,
5489 tree_cons (NULL_TREE,
5490 va_list_arg_type_node,
5491 endlink))),
5492 BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL_PTR);
5493
5494 /* ??? Ought to be `T __builtin_expect(T, T)' for any type T. */
5495 builtin_function ("__builtin_expect",
5496 build_function_type (long_integer_type_node,
5497 tree_cons (NULL_TREE,
5498 long_integer_type_node,
5499 tree_cons (NULL_TREE,
5500 long_integer_type_node,
5501 endlink))),
5502 BUILT_IN_EXPECT, BUILT_IN_NORMAL, NULL_PTR);
5503
5504 /* Currently under experimentation. */
5505 builtin_function_2 ("__builtin_memcpy", "memcpy",
5506 memcpy_ftype, memcpy_ftype,
5507 BUILT_IN_MEMCPY, BUILT_IN_NORMAL, 1, 0, 0);
5508 builtin_function_2 ("__builtin_memcmp", "memcmp",
5509 int_ftype_cptr_cptr_sizet, int_ftype_cptr_cptr_sizet,
5510 BUILT_IN_MEMCMP, BUILT_IN_NORMAL, 1, 0, 0);
5511 builtin_function_2 ("__builtin_memset", "memset",
5512 memset_ftype, memset_ftype,
5513 BUILT_IN_MEMSET, BUILT_IN_NORMAL, 1, 0, 0);
5514 built_in_decls[BUILT_IN_STRCMP] =
5515 builtin_function_2 ("__builtin_strcmp", "strcmp",
5516 int_ftype_cstring_cstring, int_ftype_cstring_cstring,
5517 BUILT_IN_STRCMP, BUILT_IN_NORMAL, 1, 0, 0);
5518 builtin_function_2 ("__builtin_strncmp", "strncmp",
5519 int_ftype_cstring_cstring_sizet,
5520 int_ftype_cstring_cstring_sizet,
5521 BUILT_IN_STRNCMP, BUILT_IN_NORMAL, 1, 0, 0);
5522 builtin_function_2 ("__builtin_strstr", "strstr",
5523 string_ftype_cstring_cstring, string_ftype_cstring_cstring,
5524 BUILT_IN_STRSTR, BUILT_IN_NORMAL, 1, 0, 0);
5525 builtin_function_2 ("__builtin_strpbrk", "strpbrk",
5526 string_ftype_cstring_cstring, string_ftype_cstring_cstring,
5527 BUILT_IN_STRPBRK, BUILT_IN_NORMAL, 1, 0, 0);
5528 built_in_decls[BUILT_IN_STRCHR] =
5529 builtin_function_2 ("__builtin_strchr", "strchr",
5530 string_ftype_cstring_int, string_ftype_cstring_int,
5531 BUILT_IN_STRCHR, BUILT_IN_NORMAL, 1, 0, 0);
5532 builtin_function_2 ("__builtin_strrchr", "strrchr",
5533 string_ftype_cstring_int, string_ftype_cstring_int,
5534 BUILT_IN_STRRCHR, BUILT_IN_NORMAL, 1, 0, 0);
5535 builtin_function_2 ("__builtin_strcpy", "strcpy",
5536 string_ftype_string_cstring, string_ftype_string_cstring,
5537 BUILT_IN_STRCPY, BUILT_IN_NORMAL, 1, 0, 0);
5538 builtin_function_2 ("__builtin_strncpy", "strncpy",
5539 string_ftype_string_cstring_sizet,
5540 string_ftype_string_cstring_sizet,
5541 BUILT_IN_STRNCPY, BUILT_IN_NORMAL, 1, 0, 0);
5542 built_in_decls[BUILT_IN_STRCAT] =
5543 builtin_function_2 ("__builtin_strcat", "strcat",
5544 string_ftype_string_cstring,
5545 string_ftype_string_cstring,
5546 BUILT_IN_STRCAT, BUILT_IN_NORMAL, 1, 0, 0);
5547 builtin_function_2 ("__builtin_strncat", "strncat",
5548 string_ftype_string_cstring_sizet,
5549 string_ftype_string_cstring_sizet,
5550 BUILT_IN_STRNCAT, BUILT_IN_NORMAL, 1, 0, 0);
5551 builtin_function_2 ("__builtin_strspn", "strspn",
5552 sizet_ftype_cstring_cstring, sizet_ftype_cstring_cstring,
5553 BUILT_IN_STRSPN, BUILT_IN_NORMAL, 1, 0, 0);
5554 builtin_function_2 ("__builtin_strcspn", "strcspn",
5555 sizet_ftype_cstring_cstring, sizet_ftype_cstring_cstring,
5556 BUILT_IN_STRCSPN, BUILT_IN_NORMAL, 1, 0, 0);
5557 built_in_decls[BUILT_IN_STRLEN] =
5558 builtin_function_2 ("__builtin_strlen", "strlen",
5559 strlen_ftype, strlen_ftype,
5560 BUILT_IN_STRLEN, BUILT_IN_NORMAL, 1, 0, 0);
5561
5562 builtin_function_2 ("__builtin_sqrtf", "sqrtf",
5563 float_ftype_float, float_ftype_float,
5564 BUILT_IN_FSQRT, BUILT_IN_NORMAL, 1, 0, 0);
5565 builtin_function_2 ("__builtin_fsqrt", "sqrt",
5566 double_ftype_double, double_ftype_double,
5567 BUILT_IN_FSQRT, BUILT_IN_NORMAL, 1, 0, 0);
5568 builtin_function_2 ("__builtin_sqrtl", "sqrtl",
5569 ldouble_ftype_ldouble, ldouble_ftype_ldouble,
5570 BUILT_IN_FSQRT, BUILT_IN_NORMAL, 1, 0, 0);
5571 builtin_function_2 ("__builtin_sinf", "sinf",
5572 float_ftype_float, float_ftype_float,
5573 BUILT_IN_SIN, BUILT_IN_NORMAL, 1, 0, 0);
5574 builtin_function_2 ("__builtin_sin", "sin",
5575 double_ftype_double, double_ftype_double,
5576 BUILT_IN_SIN, BUILT_IN_NORMAL, 1, 0, 0);
5577 builtin_function_2 ("__builtin_sinl", "sinl",
5578 ldouble_ftype_ldouble, ldouble_ftype_ldouble,
5579 BUILT_IN_SIN, BUILT_IN_NORMAL, 1, 0, 0);
5580 builtin_function_2 ("__builtin_cosf", "cosf",
5581 float_ftype_float, float_ftype_float,
5582 BUILT_IN_COS, BUILT_IN_NORMAL, 1, 0, 0);
5583 builtin_function_2 ("__builtin_cos", "cos",
5584 double_ftype_double, double_ftype_double,
5585 BUILT_IN_COS, BUILT_IN_NORMAL, 1, 0, 0);
5586 builtin_function_2 ("__builtin_cosl", "cosl",
5587 ldouble_ftype_ldouble, ldouble_ftype_ldouble,
5588 BUILT_IN_COS, BUILT_IN_NORMAL, 1, 0, 0);
5589 built_in_decls[BUILT_IN_PUTCHAR] =
5590 builtin_function ("__builtin_putchar", int_ftype_int,
5591 BUILT_IN_PUTCHAR, BUILT_IN_NORMAL, "putchar");
5592 built_in_decls[BUILT_IN_PUTS] =
5593 builtin_function ("__builtin_puts", puts_ftype,
5594 BUILT_IN_PUTS, BUILT_IN_NORMAL, "puts");
5595 builtin_function_2 ("__builtin_printf", "printf",
5596 printf_ftype, printf_ftype,
5597 BUILT_IN_PRINTF, BUILT_IN_FRONTEND, 1, 0, 0);
5598 built_in_decls[BUILT_IN_FWRITE] =
5599 builtin_function ("__builtin_fwrite", fwrite_ftype,
5600 BUILT_IN_FWRITE, BUILT_IN_NORMAL, "fwrite");
5601 built_in_decls[BUILT_IN_FPUTC] =
5602 builtin_function ("__builtin_fputc", fputc_ftype,
5603 BUILT_IN_FPUTC, BUILT_IN_NORMAL, "fputc");
5604 /* Declare the __builtin_ style with arguments and the regular style
5605 without them. We rely on stdio.h to supply the arguments for the
5606 regular style declaration since we had to use void* instead of
5607 FILE* in the __builtin_ prototype supplied here. */
5608 built_in_decls[BUILT_IN_FPUTS] =
5609 builtin_function_2 ("__builtin_fputs", "fputs",
5610 fputs_ftype, int_ftype_any,
5611 BUILT_IN_FPUTS, BUILT_IN_NORMAL, 1, 0, 0);
5612
5613 /* Declare these functions non-returning
5614 to avoid spurious "control drops through" warnings. */
5615 builtin_function_2 (NULL_PTR, "abort",
5616 NULL_TREE, ((c_language == clk_cplusplus)
5617 ? void_ftype : void_ftype_any),
5618 0, NOT_BUILT_IN, 0, 0, 1);
5619
5620 builtin_function_2 (NULL_PTR, "exit",
5621 NULL_TREE, ((c_language == clk_cplusplus)
5622 ? void_ftype_int : void_ftype_any),
5623 0, NOT_BUILT_IN, 0, 0, 1);
5624
5625#if 0
5626 /* Support for these has not been written in either expand_builtin
5627 or build_function_call. */
5628 builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV,
5629 BUILT_IN_NORMAL, NULL_PTR);
5630 builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV,
5631 BUILT_IN_NORMAL, NULL_PTR);
5632 builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR,
5633 BUILT_IN_NORMAL, NULL_PTR);
5634 builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL,
5635 BUILT_IN_NORMAL, NULL_PTR);
5636 builtin_function ("__builtin_fmod", double_ftype_double_double,
5637 BUILT_IN_FMOD, BUILT_IN_NORMAL, NULL_PTR);
5638 builtin_function ("__builtin_frem", double_ftype_double_double,
5639 BUILT_IN_FREM, BUILT_IN_NORMAL, NULL_PTR);
5640 builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP,
5641 BUILT_IN_NORMAL, NULL_PTR);
5642 builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN,
5643 BUILT_IN_NORMAL, NULL_PTR);
5644#endif
5645
5646 main_identifier_node = get_identifier ("main");
5647
5648 /* ??? Perhaps there's a better place to do this. But it is related
5649 to __builtin_va_arg, so it isn't that off-the-wall. */
5650 lang_type_promotes_to = simple_type_promotes_to;
5651}
5652
5653tree
5654build_va_arg (expr, type)
5655 tree expr, type;
5656{
5657 return build1 (VA_ARG_EXPR, type, expr);
5658}
5659
5660
5661/* Possibly define a builtin function with one or two names. BUILTIN_NAME
5662 is an __builtin_-prefixed name; NAME is the ordinary name; one or both
5663 of these may be NULL (though both being NULL is useless).
5664 BUILTIN_TYPE is the type of the __builtin_-prefixed function;
5665 TYPE is the type of the function with the ordinary name. These
5666 may differ if the ordinary name is declared with a looser type to avoid
5667 conflicts with headers. FUNCTION_CODE and CLASS are as for
5668 builtin_function. If LIBRARY_NAME_P is nonzero, NAME is passed as
5669 the LIBRARY_NAME parameter to builtin_function when declaring BUILTIN_NAME.
5670 If NONANSI_P is nonzero, the name NAME is treated as a non-ANSI name; if
5671 NORETURN_P is nonzero, the function is marked as non-returning.
5672 Returns the declaration of BUILTIN_NAME, if any, otherwise
5673 the declaration of NAME. Does not declare NAME if flag_no_builtin,
5674 or if NONANSI_P and flag_no_nonansi_builtin. */
5675
5676static tree
5677builtin_function_2 (builtin_name, name, builtin_type, type, function_code,
5678 class, library_name_p, nonansi_p, noreturn_p)
5679 const char *builtin_name;
5680 const char *name;
5681 tree builtin_type;
5682 tree type;
5683 int function_code;
5684 enum built_in_class class;
5685 int library_name_p;
5686 int nonansi_p;
5687 int noreturn_p;
5688{
5689 tree bdecl = NULL_TREE;
5690 tree decl = NULL_TREE;
5691 if (builtin_name != 0)
5692 {
5693 bdecl = builtin_function (builtin_name, builtin_type, function_code,
5694 class, library_name_p ? name : NULL_PTR);
5695 if (noreturn_p)
5696 {
5697 TREE_THIS_VOLATILE (bdecl) = 1;
5698 TREE_SIDE_EFFECTS (bdecl) = 1;
5699 }
5700 }
5701 if (name != 0 && !flag_no_builtin && !(nonansi_p && flag_no_nonansi_builtin))
5702 {
5703 decl = builtin_function (name, type, function_code, class, NULL_PTR);
5704 if (nonansi_p)
5705 DECL_BUILT_IN_NONANSI (decl) = 1;
5706 if (noreturn_p)
5707 {
5708 TREE_THIS_VOLATILE (decl) = 1;
5709 TREE_SIDE_EFFECTS (decl) = 1;
5710 }
5711 }
5712 return (bdecl != 0 ? bdecl : decl);
5713}
5714\f
5715/* Given a type, apply default promotions wrt unnamed function arguments
5716 and return the new type. Return NULL_TREE if no change. */
5717/* ??? There is a function of the same name in the C++ front end that
5718 does something similar, but is more thorough and does not return NULL
5719 if no change. We could perhaps share code, but it would make the
5720 self_promoting_type property harder to identify. */
5721
5722tree
5723simple_type_promotes_to (type)
5724 tree type;
5725{
5726 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5727 return double_type_node;
5728
5729 if (C_PROMOTING_INTEGER_TYPE_P (type))
5730 {
5731 /* Traditionally, unsignedness is preserved in default promotions.
5732 Also preserve unsignedness if not really getting any wider. */
5733 if (TREE_UNSIGNED (type)
5734 && (flag_traditional
5735 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
5736 return unsigned_type_node;
5737 return integer_type_node;
5738 }
5739
5740 return NULL_TREE;
5741}
5742
5743/* Return 1 if PARMS specifies a fixed number of parameters
5744 and none of their types is affected by default promotions. */
5745
5746int
5747self_promoting_args_p (parms)
5748 tree parms;
5749{
5750 register tree t;
5751 for (t = parms; t; t = TREE_CHAIN (t))
5752 {
5753 register tree type = TREE_VALUE (t);
5754
5755 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5756 return 0;
5757
5758 if (type == 0)
5759 return 0;
5760
5761 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5762 return 0;
5763
5764 if (C_PROMOTING_INTEGER_TYPE_P (type))
5765 return 0;
5766 }
5767 return 1;
5768}
5769
5770/* Recursively examines the array elements of TYPE, until a non-array
5771 element type is found. */
5772
5773tree
5774strip_array_types (type)
5775 tree type;
5776{
5777 while (TREE_CODE (type) == ARRAY_TYPE)
5778 type = TREE_TYPE (type);
5779
5780 return type;
5781}
5782
5783/* Recognize certain built-in functions so we can make tree-codes
5784 other than CALL_EXPR. We do this when it enables fold-const.c
5785 to do something useful. */
5786/* ??? By rights this should go in builtins.c, but only C and C++
5787 implement build_{binary,unary}_op. Not exactly sure what bits
5788 of functionality are actually needed from those functions, or
5789 where the similar functionality exists in the other front ends. */
5790
5791tree
5792expand_tree_builtin (function, params, coerced_params)
5793 tree function, params, coerced_params;
5794{
5795 enum tree_code code;
5796
5797 if (DECL_BUILT_IN_CLASS (function) != BUILT_IN_NORMAL)
5798 return NULL_TREE;
5799
5800 switch (DECL_FUNCTION_CODE (function))
5801 {
5802 case BUILT_IN_ABS:
5803 case BUILT_IN_FABS:
5804 if (coerced_params == 0)
5805 return integer_zero_node;
5806 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
5807
5808 case BUILT_IN_ISGREATER:
5809 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
5810 code = UNLE_EXPR;
5811 else
5812 code = LE_EXPR;
5813 goto unordered_cmp;
5814
5815 case BUILT_IN_ISGREATEREQUAL:
5816 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
5817 code = UNLT_EXPR;
5818 else
5819 code = LT_EXPR;
5820 goto unordered_cmp;
5821
5822 case BUILT_IN_ISLESS:
5823 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
5824 code = UNGE_EXPR;
5825 else
5826 code = GE_EXPR;
5827 goto unordered_cmp;
5828
5829 case BUILT_IN_ISLESSEQUAL:
5830 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
5831 code = UNGT_EXPR;
5832 else
5833 code = GT_EXPR;
5834 goto unordered_cmp;
5835
5836 case BUILT_IN_ISLESSGREATER:
5837 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
5838 code = UNEQ_EXPR;
5839 else
5840 code = EQ_EXPR;
5841 goto unordered_cmp;
5842
5843 case BUILT_IN_ISUNORDERED:
5844 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT)
5845 return integer_zero_node;
5846 code = UNORDERED_EXPR;
5847 goto unordered_cmp;
5848
5849 unordered_cmp:
5850 {
5851 tree arg0, arg1;
5852
5853 if (params == 0
5854 || TREE_CHAIN (params) == 0)
5855 {
5856 error ("too few arguments to function `%s'",
5857 IDENTIFIER_POINTER (DECL_NAME (function)));
5858 return error_mark_node;
5859 }
5860 else if (TREE_CHAIN (TREE_CHAIN (params)) != 0)
5861 {
5862 error ("too many arguments to function `%s'",
5863 IDENTIFIER_POINTER (DECL_NAME (function)));
5864 return error_mark_node;
5865 }
5866
5867 arg0 = TREE_VALUE (params);
5868 arg1 = TREE_VALUE (TREE_CHAIN (params));
5869 arg0 = build_binary_op (code, arg0, arg1, 0);
5870 if (code != UNORDERED_EXPR)
5871 arg0 = build_unary_op (TRUTH_NOT_EXPR, arg0, 0);
5872 return arg0;
5873 }
5874 break;
5875
5876 default:
5877 break;
5878 }
5879
5880 return NULL_TREE;
5881}
5882
5883/* Returns non-zero if CODE is the code for a statement. */
5884
5885int
5886statement_code_p (code)
5887 enum tree_code code;
5888{
5889 switch (code)
5890 {
5891 case EXPR_STMT:
5892 case COMPOUND_STMT:
5893 case DECL_STMT:
5894 case IF_STMT:
5895 case FOR_STMT:
5896 case WHILE_STMT:
5897 case DO_STMT:
5898 case RETURN_STMT:
5899 case BREAK_STMT:
5900 case CONTINUE_STMT:
5901 case SCOPE_STMT:
5902 case SWITCH_STMT:
5903 case GOTO_STMT:
5904 case LABEL_STMT:
5905 case ASM_STMT:
5906 case CASE_LABEL:
5907 return 1;
5908
5909 default:
5910 if (lang_statement_code_p)
5911 return (*lang_statement_code_p) (code);
5912 return 0;
5913 }
5914}
5915
5916/* Walk the statemen tree, rooted at *tp. Apply FUNC to all the
5917 sub-trees of *TP in a pre-order traversal. FUNC is called with the
5918 DATA and the address of each sub-tree. If FUNC returns a non-NULL
5919 value, the traversal is aborted, and the value returned by FUNC is
5920 returned. If FUNC sets WALK_SUBTREES to zero, then the subtrees of
5921 the node being visited are not walked.
5922
5923 We don't need a without_duplicates variant of this one because the
5924 statement tree is a tree, not a graph. */
5925
5926tree
5927walk_stmt_tree (tp, func, data)
5928 tree *tp;
5929 walk_tree_fn func;
5930 void *data;
5931{
5932 enum tree_code code;
5933 int walk_subtrees;
5934 tree result;
5935 int i, len;
5936
5937#define WALK_SUBTREE(NODE) \
5938 do \
5939 { \
5940 result = walk_stmt_tree (&(NODE), func, data); \
5941 if (result) \
5942 return result; \
5943 } \
5944 while (0)
5945
5946 /* Skip empty subtrees. */
5947 if (!*tp)
5948 return NULL_TREE;
5949
5950 /* Skip subtrees below non-statement nodes. */
5951 if (!statement_code_p (TREE_CODE (*tp)))
5952 return NULL_TREE;
5953
5954 /* Call the function. */
5955 walk_subtrees = 1;
5956 result = (*func) (tp, &walk_subtrees, data);
5957
5958 /* If we found something, return it. */
5959 if (result)
5960 return result;
5961
5962 /* Even if we didn't, FUNC may have decided that there was nothing
5963 interesting below this point in the tree. */
5964 if (!walk_subtrees)
5965 return NULL_TREE;
5966
5967 /* FUNC may have modified the tree, recheck that we're looking at a
5968 statement node. */
5969 code = TREE_CODE (*tp);
5970 if (!statement_code_p (code))
5971 return NULL_TREE;
5972
5973 /* Walk over all the sub-trees of this operand. Statement nodes never
5974 contain RTL, and we needn't worry about TARGET_EXPRs. */
5975 len = TREE_CODE_LENGTH (code);
5976
5977 /* Go through the subtrees. We need to do this in forward order so
5978 that the scope of a FOR_EXPR is handled properly. */
5979 for (i = 0; i < len; ++i)
5980 WALK_SUBTREE (TREE_OPERAND (*tp, i));
5981
5982 /* Finally visit the chain. This can be tail-recursion optimized if
5983 we write it this way. */
5984 return walk_stmt_tree (&TREE_CHAIN (*tp), func, data);
5985
5986#undef WALK_SUBTREE
5987}
5988
5989/* Used to compare case labels. K1 and K2 are actually tree nodes
5990 representing case labels, or NULL_TREE for a `default' label.
5991 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5992 K2, and 0 if K1 and K2 are equal. */
5993
5994int
5995case_compare (k1, k2)
5996 splay_tree_key k1;
5997 splay_tree_key k2;
5998{
5999 /* Consider a NULL key (such as arises with a `default' label) to be
6000 smaller than anything else. */
6001 if (!k1)
6002 return k2 ? -1 : 0;
6003 else if (!k2)
6004 return k1 ? 1 : 0;
6005
6006 return tree_int_cst_compare ((tree) k1, (tree) k2);
6007}
6008
6009/* Process a case label for the range LOW_VALUE ... HIGH_VALUE. If
6010 LOW_VALUE and HIGH_VALUE are both NULL_TREE then this case label is
6011 actually a `default' label. If only HIGH_VALUE is NULL_TREE, then
6012 case label was declared using the usual C/C++ syntax, rather than
6013 the GNU case range extension. CASES is a tree containing all the
6014 case ranges processed so far; COND is the condition for the
6015 switch-statement itself. Returns the CASE_LABEL created, or
6016 ERROR_MARK_NODE if no CASE_LABEL is created. */
6017
6018tree
6019c_add_case_label (cases, cond, low_value, high_value)
6020 splay_tree cases;
6021 tree cond;
6022 tree low_value;
6023 tree high_value;
6024{
6025 tree type;
6026 tree label;
6027 tree case_label;
6028 splay_tree_node node;
6029
6030 /* Create the LABEL_DECL itself. */
6031 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
6032 DECL_CONTEXT (label) = current_function_decl;
6033
6034 /* If there was an error processing the switch condition, bail now
6035 before we get more confused. */
6036 if (!cond || cond == error_mark_node)
6037 {
6038 /* Add a label anyhow so that the back-end doesn't think that
6039 the beginning of the switch is unreachable. */
6040 if (!cases->root)
6041 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
6042 return error_mark_node;
6043 }
6044
6045 if ((low_value && TREE_TYPE (low_value)
6046 && POINTER_TYPE_P (TREE_TYPE (low_value)))
6047 || (high_value && TREE_TYPE (high_value)
6048 && POINTER_TYPE_P (TREE_TYPE (high_value))))
6049 error ("pointers are not permitted as case values");
6050
6051 /* Case ranges are a GNU extension. */
6052 if (high_value && pedantic)
6053 {
6054 if (c_language == clk_cplusplus)
6055 pedwarn ("ISO C++ forbids range expressions in switch statements");
6056 else
6057 pedwarn ("ISO C forbids range expressions in switch statements");
6058 }
6059
6060 type = TREE_TYPE (cond);
6061 if (low_value)
6062 {
6063 low_value = check_case_value (low_value);
6064 low_value = convert_and_check (type, low_value);
6065 }
6066 if (high_value)
6067 {
6068 high_value = check_case_value (high_value);
6069 high_value = convert_and_check (type, high_value);
6070 }
6071
6072 /* If an error has occurred, bail out now. */
6073 if (low_value == error_mark_node || high_value == error_mark_node)
6074 {
6075 if (!cases->root)
6076 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
6077 return error_mark_node;
6078 }
6079
6080 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
6081 really a case range, even though it was written that way. Remove
6082 the HIGH_VALUE to simplify later processing. */
6083 if (tree_int_cst_equal (low_value, high_value))
6084 high_value = NULL_TREE;
6085 if (low_value && high_value
6086 && !tree_int_cst_lt (low_value, high_value))
6087 warning ("empty range specified");
6088
6089 /* Look up the LOW_VALUE in the table of case labels we already
6090 have. */
6091 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6092 /* If there was not an exact match, check for overlapping ranges.
6093 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6094 that's a `default' label and the only overlap is an exact match. */
6095 if (!node && (low_value || high_value))
6096 {
6097 splay_tree_node low_bound;
6098 splay_tree_node high_bound;
6099
6100 /* Even though there wasn't an exact match, there might be an
6101 overlap between this case range and another case range.
6102 Since we've (inductively) not allowed any overlapping case
6103 ranges, we simply need to find the greatest low case label
6104 that is smaller that LOW_VALUE, and the smallest low case
6105 label that is greater than LOW_VALUE. If there is an overlap
6106 it will occur in one of these two ranges. */
6107 low_bound = splay_tree_predecessor (cases,
6108 (splay_tree_key) low_value);
6109 high_bound = splay_tree_successor (cases,
6110 (splay_tree_key) low_value);
6111
6112 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6113 the LOW_VALUE, so there is no need to check unless the
6114 LOW_BOUND is in fact itself a case range. */
6115 if (low_bound
6116 && CASE_HIGH ((tree) low_bound->value)
6117 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6118 low_value) >= 0)
6119 node = low_bound;
6120 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6121 range is bigger than the low end of the current range, so we
6122 are only interested if the current range is a real range, and
6123 not an ordinary case label. */
6124 else if (high_bound
6125 && high_value
6126 && (tree_int_cst_compare ((tree) high_bound->key,
6127 high_value)
6128 <= 0))
6129 node = high_bound;
6130 }
6131 /* If there was an overlap, issue an error. */
6132 if (node)
6133 {
6134 tree duplicate = CASE_LABEL_DECL ((tree) node->value);
6135
6136 if (high_value)
6137 {
6138 error ("duplicate (or overlapping) case value");
6139 error_with_decl (duplicate,
6140 "this is the first entry overlapping that value");
6141 }
6142 else if (low_value)
6143 {
6144 error ("duplicate case value") ;
6145 error_with_decl (duplicate, "previously used here");
6146 }
6147 else
6148 {
6149 error ("multiple default labels in one switch");
6150 error_with_decl (duplicate, "this is the first default label");
6151 }
6152 if (!cases->root)
6153 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
6154 }
6155
6156 /* Add a CASE_LABEL to the statement-tree. */
6157 case_label = add_stmt (build_case_label (low_value, high_value, label));
6158 /* Register this case label in the splay tree. */
6159 splay_tree_insert (cases,
6160 (splay_tree_key) low_value,
6161 (splay_tree_value) case_label);
6162
6163 return case_label;
6164}
6165
6166/* Mark P (a stmt_tree) for GC. The use of a `void *' for the
6167 parameter allows this function to be used as a GC-marking
6168 function. */
6169
6170void
6171mark_stmt_tree (p)
6172 void *p;
6173{
6174 stmt_tree st = (stmt_tree) p;
6175
6176 ggc_mark_tree (st->x_last_stmt);
6177 ggc_mark_tree (st->x_last_expr_type);
6178}
6179
6180/* Mark LD for GC. */
6181
6182void
6183c_mark_lang_decl (c)
6184 struct c_lang_decl *c;
6185{
6186 ggc_mark_tree (c->saved_tree);
6187}
6188
6189/* Mark F for GC. */
6190
6191void
6192mark_c_language_function (f)
6193 struct language_function *f;
6194{
6195 if (!f)
6196 return;
6197
6198 mark_stmt_tree (&f->x_stmt_tree);
6199 ggc_mark_tree (f->x_scope_stmt_stack);
6200}
6201
6202/* Hook used by expand_expr to expand language-specific tree codes. */
6203
6204rtx
6205c_expand_expr (exp, target, tmode, modifier)
6206 tree exp;
6207 rtx target;
6208 enum machine_mode tmode;
6209 enum expand_modifier modifier;
6210{
6211 switch (TREE_CODE (exp))
6212 {
6213 case STMT_EXPR:
6214 {
6215 tree rtl_expr;
6216 rtx result;
6217
6218 /* Since expand_expr_stmt calls free_temp_slots after every
6219 expression statement, we must call push_temp_slots here.
6220 Otherwise, any temporaries in use now would be considered
6221 out-of-scope after the first EXPR_STMT from within the
6222 STMT_EXPR. */
6223 push_temp_slots ();
6224 rtl_expr = expand_start_stmt_expr ();
6225 expand_stmt (STMT_EXPR_STMT (exp));
6226 expand_end_stmt_expr (rtl_expr);
6227 result = expand_expr (rtl_expr, target, tmode, modifier);
6228 pop_temp_slots ();
6229 return result;
6230 }
6231 break;
6232
6233 case CALL_EXPR:
6234 {
6235 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6236 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6237 == FUNCTION_DECL)
6238 && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6239 && (DECL_BUILT_IN_CLASS (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6240 == BUILT_IN_FRONTEND))
6241 return c_expand_builtin (exp, target, tmode, modifier);
6242 else
6243 abort();
6244 }
6245 break;
6246
6247 default:
6248 abort ();
6249 }
6250
6251 abort ();
6252 return NULL;
6253}
6254
6255/* Hook used by safe_from_p to handle language-specific tree codes. */
6256
6257int
6258c_safe_from_p (target, exp)
6259 rtx target;
6260 tree exp;
6261{
6262 /* We can see statements here when processing the body of a
6263 statement-expression. For a declaration statement declaring a
6264 variable, look at the variable's initializer. */
6265 if (TREE_CODE (exp) == DECL_STMT)
6266 {
6267 tree decl = DECL_STMT_DECL (exp);
6268
6269 if (TREE_CODE (decl) == VAR_DECL
6270 && DECL_INITIAL (decl)
6271 && !safe_from_p (target, DECL_INITIAL (decl), /*top_p=*/0))
6272 return 0;
6273 }
6274
6275 /* For any statement, we must follow the statement-chain. */
6276 if (statement_code_p (TREE_CODE (exp)) && TREE_CHAIN (exp))
6277 return safe_from_p (target, TREE_CHAIN (exp), /*top_p=*/0);
6278
6279 /* Assume everything else is safe. */
6280 return 1;
6281}
6282
6283/* Hook used by unsafe_for_reeval to handle language-specific tree codes. */
6284
6285int
6286c_unsafe_for_reeval (exp)
6287 tree exp;
6288{
6289 /* Statement expressions may not be reevaluated. */
6290 if (TREE_CODE (exp) == STMT_EXPR)
6291 return 2;
6292
6293 /* Walk all other expressions. */
6294 return -1;
6295}
6296
6297/* Tree code classes. */
6298
6299#define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
6300
6301static char c_tree_code_type[] = {
6302 'x',
6303#include "c-common.def"
6304};
6305#undef DEFTREECODE
6306
6307/* Table indexed by tree code giving number of expression
6308 operands beyond the fixed part of the node structure.
6309 Not used for types or decls. */
6310
6311#define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
6312
6313static int c_tree_code_length[] = {
6314 0,
6315#include "c-common.def"
6316};
6317#undef DEFTREECODE
6318
6319/* Names of tree components.
6320 Used for printing out the tree and error messages. */
6321#define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
6322
6323static const char *c_tree_code_name[] = {
6324 "@@dummy",
6325#include "c-common.def"
6326};
6327#undef DEFTREECODE
6328
6329/* Adds the tree codes specific to the C front end to the list of all
6330 tree codes. */
6331
6332void
6333add_c_tree_codes ()
6334{
6335 memcpy (tree_code_type + (int) LAST_AND_UNUSED_TREE_CODE,
6336 c_tree_code_type,
6337 (int)LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE);
6338 memcpy (tree_code_length + (int) LAST_AND_UNUSED_TREE_CODE,
6339 c_tree_code_length,
6340 (LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (int));
6341 memcpy (tree_code_name + (int) LAST_AND_UNUSED_TREE_CODE,
6342 c_tree_code_name,
6343 (LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (char *));
6344 lang_unsafe_for_reeval = c_unsafe_for_reeval;
6345}
6346
6347#define CALLED_AS_BUILT_IN(NODE) \
6348 (!strncmp (IDENTIFIER_POINTER (DECL_NAME (NODE)), "__builtin_", 10))
6349
6350static rtx
6351c_expand_builtin (exp, target, tmode, modifier)
6352 tree exp;
6353 rtx target;
6354 enum machine_mode tmode;
6355 enum expand_modifier modifier;
6356{
6357 tree type = TREE_TYPE (exp);
6358 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6359 tree arglist = TREE_OPERAND (exp, 1);
6360 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6361 enum tree_code code = TREE_CODE (exp);
6362 const int ignore = (target == const0_rtx
6363 || ((code == NON_LVALUE_EXPR || code == NOP_EXPR
6364 || code == CONVERT_EXPR || code == REFERENCE_EXPR
6365 || code == COND_EXPR)
6366 && TREE_CODE (type) == VOID_TYPE));
6367
6368 if (! optimize && ! CALLED_AS_BUILT_IN (fndecl))
6369 return expand_call (exp, target, ignore);
6370
6371 switch (fcode)
6372 {
6373 case BUILT_IN_PRINTF:
6374 target = c_expand_builtin_printf (arglist, target, tmode,
6375 modifier, ignore);
6376 if (target)
6377 return target;
6378 break;
6379
6380 default: /* just do library call, if unknown builtin */
6381 error ("built-in function `%s' not currently supported",
6382 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
6383 }
6384
6385 /* The switch statement above can drop through to cause the function
6386 to be called normally. */
6387 return expand_call (exp, target, ignore);
6388}
6389
6390/* Check an arglist to *printf for problems. The arglist should start
6391 at the format specifier, with the remaining arguments immediately
6392 following it. */
6393static int
6394is_valid_printf_arglist (arglist)
6395 tree arglist;
6396{
6397 /* Save this value so we can restore it later. */
6398 const int SAVE_pedantic = pedantic;
6399 int diagnostic_occurred = 0;
6400
6401 /* Set this to a known value so the user setting won't affect code
6402 generation. */
6403 pedantic = 1;
6404 /* Check to make sure there are no format specifier errors. */
6405 check_function_format (&diagnostic_occurred,
6406 maybe_get_identifier("printf"),
6407 NULL_TREE, arglist);
6408
6409 /* Restore the value of `pedantic'. */
6410 pedantic = SAVE_pedantic;
6411
6412 /* If calling `check_function_format_ptr' produces a warning, we
6413 return false, otherwise we return true. */
6414 return ! diagnostic_occurred;
6415}
6416
6417/* If the arguments passed to printf are suitable for optimizations,
6418 we attempt to transform the call. */
6419static rtx
6420c_expand_builtin_printf (arglist, target, tmode, modifier, ignore)
6421 tree arglist;
6422 rtx target;
6423 enum machine_mode tmode;
6424 enum expand_modifier modifier;
6425 int ignore;
6426{
6427 tree fn_putchar = built_in_decls[BUILT_IN_PUTCHAR],
6428 fn_puts = built_in_decls[BUILT_IN_PUTS];
6429 tree fn, format_arg, stripped_string;
6430
6431 /* If the return value is used, or the replacement _DECL isn't
6432 initialized, don't do the transformation. */
6433 if (!ignore || !fn_putchar || !fn_puts)
6434 return 0;
6435
6436 /* Verify the required arguments in the original call. */
6437 if (arglist == 0
6438 || (TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE))
6439 return 0;
6440
6441 /* Check the specifier vs. the parameters. */
6442 if (!is_valid_printf_arglist (arglist))
6443 return 0;
6444
6445 format_arg = TREE_VALUE (arglist);
6446 stripped_string = format_arg;
6447 STRIP_NOPS (stripped_string);
6448 if (stripped_string && TREE_CODE (stripped_string) == ADDR_EXPR)
6449 stripped_string = TREE_OPERAND (stripped_string, 0);
6450
6451 /* If the format specifier isn't a STRING_CST, punt. */
6452 if (TREE_CODE (stripped_string) != STRING_CST)
6453 return 0;
6454
6455 /* OK! We can attempt optimization. */
6456
6457 /* If the format specifier was "%s\n", call __builtin_puts(arg2). */
6458 if (strcmp (TREE_STRING_POINTER (stripped_string), "%s\n") == 0)
6459 {
6460 arglist = TREE_CHAIN (arglist);
6461 fn = fn_puts;
6462 }
6463 /* If the format specifier was "%c", call __builtin_putchar (arg2). */
6464 else if (strcmp (TREE_STRING_POINTER (stripped_string), "%c") == 0)
6465 {
6466 arglist = TREE_CHAIN (arglist);
6467 fn = fn_putchar;
6468 }
6469 else
6470 {
6471 /* We can't handle anything else with % args or %% ... yet. */
6472 if (strchr (TREE_STRING_POINTER (stripped_string), '%'))
6473 return 0;
6474
6475 /* If the resulting constant string has a length of 1, call
6476 putchar. Note, TREE_STRING_LENGTH includes the terminating
6477 NULL in its count. */
6478 if (TREE_STRING_LENGTH (stripped_string) == 2)
6479 {
6480 /* Given printf("c"), (where c is any one character,)
6481 convert "c"[0] to an int and pass that to the replacement
6482 function. */
6483 arglist = build_int_2 (TREE_STRING_POINTER (stripped_string)[0], 0);
6484 arglist = build_tree_list (NULL_TREE, arglist);
6485
6486 fn = fn_putchar;
6487 }
6488 /* If the resulting constant was "string\n", call
6489 __builtin_puts("string"). Ensure "string" has at least one
6490 character besides the trailing \n. Note, TREE_STRING_LENGTH
6491 includes the terminating NULL in its count. */
6492 else if (TREE_STRING_LENGTH (stripped_string) > 2
6493 && TREE_STRING_POINTER (stripped_string)
6494 [TREE_STRING_LENGTH (stripped_string) - 2] == '\n')
6495 {
6496 /* Create a NULL-terminated string that's one char shorter
6497 than the original, stripping off the trailing '\n'. */
6498 const int newlen = TREE_STRING_LENGTH (stripped_string) - 1;
6499 char *newstr = (char *) alloca (newlen);
6500 memcpy (newstr, TREE_STRING_POINTER (stripped_string), newlen - 1);
6501 newstr[newlen - 1] = 0;
6502
6503 arglist = combine_strings (build_string (newlen, newstr));
6504 arglist = build_tree_list (NULL_TREE, arglist);
6505 fn = fn_puts;
6506 }
6507 else
6508 /* We'd like to arrange to call fputs(string) here, but we
6509 need stdout and don't have a way to get it ... yet. */
6510 return 0;
6511 }
6512
6513 return expand_expr (build_function_call (fn, arglist),
6514 (ignore ? const0_rtx : target),
6515 tmode, modifier);
6516}
6517\f
6518
6519/* Given a boolean expression ARG, return a tree representing an increment
6520 or decrement (as indicated by CODE) of ARG. The front end must check for
6521 invalid cases (e.g., decrement in C++). */
6522tree
6523boolean_increment (code, arg)
6524 enum tree_code code;
6525 tree arg;
6526{
6527 tree val;
6528 tree true_res = (c_language == clk_cplusplus
6529 ? boolean_true_node
6530 : c_bool_true_node);
6531 arg = stabilize_reference (arg);
6532 switch (code)
6533 {
6534 case PREINCREMENT_EXPR:
6535 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6536 break;
6537 case POSTINCREMENT_EXPR:
6538 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6539 arg = save_expr (arg);
6540 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6541 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6542 break;
6543 case PREDECREMENT_EXPR:
6544 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, invert_truthvalue (arg));
6545 break;
6546 case POSTDECREMENT_EXPR:
6547 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, invert_truthvalue (arg));
6548 arg = save_expr (arg);
6549 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6550 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6551 break;
6552 default:
6553 abort ();
6554 }
6555 TREE_SIDE_EFFECTS (val) = 1;
6556 return val;
6557}