OSDN Git Service

a6ae983196df186b7c75e455cecd5afcaa7a2b45
[pf3gnuchains/gcc-fork.git] / gcc / c-format.c
1 /* Check calls to formatted I/O functions (-Wformat).
2    Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3    2001, 2002, 2003 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "toplev.h"
29 #include "c-common.h"
30 #include "intl.h"
31 #include "diagnostic.h"
32 #include "langhooks.h"
33 \f
34 /* Set format warning options according to a -Wformat=n option.  */
35
36 void
37 set_Wformat (setting)
38      int setting;
39 {
40   warn_format = setting;
41   warn_format_y2k = setting;
42   warn_format_extra_args = setting;
43   warn_format_zero_length = setting;
44   if (setting != 1)
45     {
46       warn_format_nonliteral = setting;
47       warn_format_security = setting;
48     }
49   /* Make sure not to disable -Wnonnull if -Wformat=0 is specified.  */
50   if (setting)
51     warn_nonnull = setting;
52 }
53
54 \f
55 /* Handle attributes associated with format checking.  */
56
57 /* This must be in the same order as format_types, with format_type_error
58    last.  */
59 enum format_type { printf_format_type, asm_fprintf_format_type,
60                    scanf_format_type, strftime_format_type,
61                    strfmon_format_type, format_type_error };
62
63 typedef struct function_format_info
64 {
65   enum format_type format_type; /* type of format (printf, scanf, etc.) */
66   unsigned HOST_WIDE_INT format_num;    /* number of format argument */
67   unsigned HOST_WIDE_INT first_arg_num; /* number of first arg (zero for varargs) */
68 } function_format_info;
69
70 static bool decode_format_attr          PARAMS ((tree,
71                                                  function_format_info *, int));
72 static enum format_type decode_format_type      PARAMS ((const char *));
73
74
75
76 /* Handle a "format_arg" attribute; arguments as in
77    struct attribute_spec.handler.  */
78 tree
79 handle_format_arg_attribute (node, name, args, flags, no_add_attrs)
80      tree *node;
81      tree name ATTRIBUTE_UNUSED;
82      tree args;
83      int flags;
84      bool *no_add_attrs;
85 {
86   tree type = *node;
87   tree format_num_expr = TREE_VALUE (args);
88   unsigned HOST_WIDE_INT format_num;
89   unsigned HOST_WIDE_INT arg_num;
90   tree argument;
91
92   /* Strip any conversions from the first arg number and verify it
93      is a constant.  */
94   while (TREE_CODE (format_num_expr) == NOP_EXPR
95          || TREE_CODE (format_num_expr) == CONVERT_EXPR
96          || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
97     format_num_expr = TREE_OPERAND (format_num_expr, 0);
98
99   if (TREE_CODE (format_num_expr) != INTEGER_CST
100       || TREE_INT_CST_HIGH (format_num_expr) != 0)
101     {
102       error ("format string has invalid operand number");
103       *no_add_attrs = true;
104       return NULL_TREE;
105     }
106
107   format_num = TREE_INT_CST_LOW (format_num_expr);
108
109   /* If a parameter list is specified, verify that the format_num
110      argument is actually a string, in case the format attribute
111      is in error.  */
112   argument = TYPE_ARG_TYPES (type);
113   if (argument)
114     {
115       for (arg_num = 1; argument != 0 && arg_num != format_num;
116            ++arg_num, argument = TREE_CHAIN (argument))
117         ;
118
119       if (! argument
120           || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
121           || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
122               != char_type_node))
123         {
124           if (!(flags & (int) ATTR_FLAG_BUILT_IN))
125             error ("format string arg not a string type");
126           *no_add_attrs = true;
127           return NULL_TREE;
128         }
129     }
130
131   if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
132       || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
133           != char_type_node))
134     {
135       if (!(flags & (int) ATTR_FLAG_BUILT_IN))
136         error ("function does not return string type");
137       *no_add_attrs = true;
138       return NULL_TREE;
139     }
140
141   return NULL_TREE;
142 }
143
144
145 /* Decode the arguments to a "format" attribute into a function_format_info
146    structure.  It is already known that the list is of the right length.
147    If VALIDATED_P is true, then these attributes have already been validated
148    and this function will abort if they are erroneous; if false, it
149    will give an error message.  Returns true if the attributes are
150    successfully decoded, false otherwise.  */
151
152 static bool
153 decode_format_attr (args, info, validated_p)
154      tree args;
155      function_format_info *info;
156      int validated_p;
157 {
158   tree format_type_id = TREE_VALUE (args);
159   tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
160   tree first_arg_num_expr
161     = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
162
163   if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
164     {
165       if (validated_p)
166         abort ();
167       error ("unrecognized format specifier");
168       return false;
169     }
170   else
171     {
172       const char *p = IDENTIFIER_POINTER (format_type_id);
173
174       info->format_type = decode_format_type (p);
175
176       if (info->format_type == format_type_error)
177         {
178           if (validated_p)
179             abort ();
180           warning ("`%s' is an unrecognized format function type", p);
181           return false;
182         }
183     }
184
185   /* Strip any conversions from the string index and first arg number
186      and verify they are constants.  */
187   while (TREE_CODE (format_num_expr) == NOP_EXPR
188          || TREE_CODE (format_num_expr) == CONVERT_EXPR
189          || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
190     format_num_expr = TREE_OPERAND (format_num_expr, 0);
191
192   while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
193          || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
194          || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
195     first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
196
197   if (TREE_CODE (format_num_expr) != INTEGER_CST
198       || TREE_INT_CST_HIGH (format_num_expr) != 0
199       || TREE_CODE (first_arg_num_expr) != INTEGER_CST
200       || TREE_INT_CST_HIGH (first_arg_num_expr) != 0)
201     {
202       if (validated_p)
203         abort ();
204       error ("format string has invalid operand number");
205       return false;
206     }
207
208   info->format_num = TREE_INT_CST_LOW (format_num_expr);
209   info->first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
210   if (info->first_arg_num != 0 && info->first_arg_num <= info->format_num)
211     {
212       if (validated_p)
213         abort ();
214       error ("format string arg follows the args to be formatted");
215       return false;
216     }
217
218   return true;
219 }
220 \f
221 /* Check a call to a format function against a parameter list.  */
222
223 /* The meaningfully distinct length modifiers for format checking recognized
224    by GCC.  */
225 enum format_lengths
226 {
227   FMT_LEN_none,
228   FMT_LEN_hh,
229   FMT_LEN_h,
230   FMT_LEN_l,
231   FMT_LEN_ll,
232   FMT_LEN_L,
233   FMT_LEN_z,
234   FMT_LEN_t,
235   FMT_LEN_j,
236   FMT_LEN_MAX
237 };
238
239
240 /* The standard versions in which various format features appeared.  */
241 enum format_std_version
242 {
243   STD_C89,
244   STD_C94,
245   STD_C9L, /* C99, but treat as C89 if -Wno-long-long.  */
246   STD_C99,
247   STD_EXT
248 };
249
250 /* The C standard version C++ is treated as equivalent to
251    or inheriting from, for the purpose of format features supported.  */
252 #define CPLUSPLUS_STD_VER       STD_C94
253 /* The C standard version we are checking formats against when pedantic.  */
254 #define C_STD_VER               ((int)(c_language == clk_cplusplus        \
255                                  ? CPLUSPLUS_STD_VER                      \
256                                  : (flag_isoc99                           \
257                                     ? STD_C99                             \
258                                     : (flag_isoc94 ? STD_C94 : STD_C89))))
259 /* The name to give to the standard version we are warning about when
260    pedantic.  FEATURE_VER is the version in which the feature warned out
261    appeared, which is higher than C_STD_VER.  */
262 #define C_STD_NAME(FEATURE_VER) (c_language == clk_cplusplus    \
263                                  ? "ISO C++"                    \
264                                  : ((FEATURE_VER) == STD_EXT    \
265                                     ? "ISO C"                   \
266                                     : "ISO C90"))
267 /* Adjust a C standard version, which may be STD_C9L, to account for
268    -Wno-long-long.  Returns other standard versions unchanged.  */
269 #define ADJ_STD(VER)            ((int)((VER) == STD_C9L                       \
270                                        ? (warn_long_long ? STD_C99 : STD_C89) \
271                                        : (VER)))
272
273 /* Flags that may apply to a particular kind of format checked by GCC.  */
274 enum
275 {
276   /* This format converts arguments of types determined by the
277      format string.  */
278   FMT_FLAG_ARG_CONVERT = 1,
279   /* The scanf allocation 'a' kludge applies to this format kind.  */
280   FMT_FLAG_SCANF_A_KLUDGE = 2,
281   /* A % during parsing a specifier is allowed to be a modified % rather
282      that indicating the format is broken and we are out-of-sync.  */
283   FMT_FLAG_FANCY_PERCENT_OK = 4,
284   /* With $ operand numbers, it is OK to reference the same argument more
285      than once.  */
286   FMT_FLAG_DOLLAR_MULTIPLE = 8,
287   /* This format type uses $ operand numbers (strfmon doesn't).  */
288   FMT_FLAG_USE_DOLLAR = 16,
289   /* Zero width is bad in this type of format (scanf).  */
290   FMT_FLAG_ZERO_WIDTH_BAD = 32,
291   /* Empty precision specification is OK in this type of format (printf).  */
292   FMT_FLAG_EMPTY_PREC_OK = 64,
293   /* Gaps are allowed in the arguments with $ operand numbers if all
294      arguments are pointers (scanf).  */
295   FMT_FLAG_DOLLAR_GAP_POINTER_OK = 128
296   /* Not included here: details of whether width or precision may occur
297      (controlled by width_char and precision_char); details of whether
298      '*' can be used for these (width_type and precision_type); details
299      of whether length modifiers can occur (length_char_specs).  */
300 };
301
302
303 /* Structure describing a length modifier supported in format checking, and
304    possibly a doubled version such as "hh".  */
305 typedef struct
306 {
307   /* Name of the single-character length modifier.  */
308   const char *name;
309   /* Index into a format_char_info.types array.  */
310   enum format_lengths index;
311   /* Standard version this length appears in.  */
312   enum format_std_version std;
313   /* Same, if the modifier can be repeated, or NULL if it can't.  */
314   const char *double_name;
315   enum format_lengths double_index;
316   enum format_std_version double_std;
317 } format_length_info;
318
319
320 /* Structure describing the combination of a conversion specifier
321    (or a set of specifiers which act identically) and a length modifier.  */
322 typedef struct
323 {
324   /* The standard version this combination of length and type appeared in.
325      This is only relevant if greater than those for length and type
326      individually; otherwise it is ignored.  */
327   enum format_std_version std;
328   /* The name to use for the type, if different from that generated internally
329      (e.g., "signed size_t").  */
330   const char *name;
331   /* The type itself.  */
332   tree *type;
333 } format_type_detail;
334
335
336 /* Macros to fill out tables of these.  */
337 #define NOARGUMENTS     { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }
338 #define BADLEN  { 0, NULL, NULL }
339 #define NOLENGTHS       { BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }
340
341
342 /* Structure describing a format conversion specifier (or a set of specifiers
343    which act identically), and the length modifiers used with it.  */
344 typedef struct
345 {
346   const char *format_chars;
347   int pointer_count;
348   enum format_std_version std;
349   /* Types accepted for each length modifier.  */
350   format_type_detail types[FMT_LEN_MAX];
351   /* List of other modifier characters allowed with these specifiers.
352      This lists flags, and additionally "w" for width, "p" for precision
353      (right precision, for strfmon), "#" for left precision (strfmon),
354      "a" for scanf "a" allocation extension (not applicable in C99 mode),
355      "*" for scanf suppression, and "E" and "O" for those strftime
356      modifiers.  */
357   const char *flag_chars;
358   /* List of additional flags describing these conversion specifiers.
359      "c" for generic character pointers being allowed, "2" for strftime
360      two digit year formats, "3" for strftime formats giving two digit
361      years in some locales, "4" for "2" which becomes "3" with an "E" modifier,
362      "o" if use of strftime "O" is a GNU extension beyond C99,
363      "W" if the argument is a pointer which is dereferenced and written into,
364      "R" if the argument is a pointer which is dereferenced and read from,
365      "i" for printf integer formats where the '0' flag is ignored with
366      precision, and "[" for the starting character of a scanf scanset.  */
367   const char *flags2;
368 } format_char_info;
369
370
371 /* Structure describing a flag accepted by some kind of format.  */
372 typedef struct
373 {
374   /* The flag character in question (0 for end of array).  */
375   int flag_char;
376   /* Zero if this entry describes the flag character in general, or a
377      nonzero character that may be found in flags2 if it describes the
378      flag when used with certain formats only.  If the latter, only
379      the first such entry found that applies to the current conversion
380      specifier is used; the values of `name' and `long_name' it supplies
381      will be used, if non-NULL and the standard version is higher than
382      the unpredicated one, for any pedantic warning.  For example, 'o'
383      for strftime formats (meaning 'O' is an extension over C99).  */
384   int predicate;
385   /* Nonzero if the next character after this flag in the format should
386      be skipped ('=' in strfmon), zero otherwise.  */
387   int skip_next_char;
388   /* The name to use for this flag in diagnostic messages.  For example,
389      N_("`0' flag"), N_("field width").  */
390   const char *name;
391   /* Long name for this flag in diagnostic messages; currently only used for
392      "ISO C does not support ...".  For example, N_("the `I' printf flag").  */
393   const char *long_name;
394   /* The standard version in which it appeared.  */
395   enum format_std_version std;
396 } format_flag_spec;
397
398
399 /* Structure describing a combination of flags that is bad for some kind
400    of format.  */
401 typedef struct
402 {
403   /* The first flag character in question (0 for end of array).  */
404   int flag_char1;
405   /* The second flag character.  */
406   int flag_char2;
407   /* Nonzero if the message should say that the first flag is ignored with
408      the second, zero if the combination should simply be objected to.  */
409   int ignored;
410   /* Zero if this entry applies whenever this flag combination occurs,
411      a nonzero character from flags2 if it only applies in some
412      circumstances (e.g. 'i' for printf formats ignoring 0 with precision).  */
413   int predicate;
414 } format_flag_pair;
415
416
417 /* Structure describing a particular kind of format processed by GCC.  */
418 typedef struct
419 {
420   /* The name of this kind of format, for use in diagnostics.  Also
421      the name of the attribute (without preceding and following __).  */
422   const char *name;
423   /* Specifications of the length modifiers accepted; possibly NULL.  */
424   const format_length_info *length_char_specs;
425   /* Details of the conversion specification characters accepted.  */
426   const format_char_info *conversion_specs;
427   /* String listing the flag characters that are accepted.  */
428   const char *flag_chars;
429   /* String listing modifier characters (strftime) accepted.  May be NULL.  */
430   const char *modifier_chars;
431   /* Details of the flag characters, including pseudo-flags.  */
432   const format_flag_spec *flag_specs;
433   /* Details of bad combinations of flags.  */
434   const format_flag_pair *bad_flag_pairs;
435   /* Flags applicable to this kind of format.  */
436   int flags;
437   /* Flag character to treat a width as, or 0 if width not used.  */
438   int width_char;
439   /* Flag character to treat a left precision (strfmon) as,
440      or 0 if left precision not used.  */
441   int left_precision_char;
442   /* Flag character to treat a precision (for strfmon, right precision) as,
443      or 0 if precision not used.  */
444   int precision_char;
445   /* If a flag character has the effect of suppressing the conversion of
446      an argument ('*' in scanf), that flag character, otherwise 0.  */
447   int suppression_char;
448   /* Flag character to treat a length modifier as (ignored if length
449      modifiers not used).  Need not be placed in flag_chars for conversion
450      specifiers, but is used to check for bad combinations such as length
451      modifier with assignment suppression in scanf.  */
452   int length_code_char;
453   /* Pointer to type of argument expected if '*' is used for a width,
454      or NULL if '*' not used for widths.  */
455   tree *width_type;
456   /* Pointer to type of argument expected if '*' is used for a precision,
457      or NULL if '*' not used for precisions.  */
458   tree *precision_type;
459 } format_kind_info;
460
461
462 /* Structure describing details of a type expected in format checking,
463    and the type to check against it.  */
464 typedef struct format_wanted_type
465 {
466   /* The type wanted.  */
467   tree wanted_type;
468   /* The name of this type to use in diagnostics.  */
469   const char *wanted_type_name;
470   /* The level of indirection through pointers at which this type occurs.  */
471   int pointer_count;
472   /* Whether, when pointer_count is 1, to allow any character type when
473      pedantic, rather than just the character or void type specified.  */
474   int char_lenient_flag;
475   /* Whether the argument, dereferenced once, is written into and so the
476      argument must not be a pointer to a const-qualified type.  */
477   int writing_in_flag;
478   /* Whether the argument, dereferenced once, is read from and so
479      must not be a NULL pointer.  */
480   int reading_from_flag;
481   /* If warnings should be of the form "field precision is not type int",
482      the name to use (in this case "field precision"), otherwise NULL,
483      for "%s format, %s arg" type messages.  If (in an extension), this
484      is a pointer type, wanted_type_name should be set to include the
485      terminating '*' characters of the type name to give a correct
486      message.  */
487   const char *name;
488   /* The actual parameter to check against the wanted type.  */
489   tree param;
490   /* The argument number of that parameter.  */
491   int arg_num;
492   /* The next type to check for this format conversion, or NULL if none.  */
493   struct format_wanted_type *next;
494 } format_wanted_type;
495
496
497 static const format_length_info printf_length_specs[] =
498 {
499   { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
500   { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L },
501   { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
502   { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
503   { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
504   { "Z", FMT_LEN_z, STD_EXT, NULL, 0, 0 },
505   { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
506   { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
507   { NULL, 0, 0, NULL, 0, 0 }
508 };
509
510 /* Length specifiers valid for asm_fprintf.  */
511 static const format_length_info asm_fprintf_length_specs[] =
512 {
513   { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89 },
514   { "w", FMT_LEN_none, STD_C89, NULL, 0, 0 },
515   { NULL, 0, 0, NULL, 0, 0 }
516 };
517
518 /* This differs from printf_length_specs only in that "Z" is not accepted.  */
519 static const format_length_info scanf_length_specs[] =
520 {
521   { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
522   { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L },
523   { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
524   { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
525   { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
526   { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
527   { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
528   { NULL, 0, 0, NULL, 0, 0 }
529 };
530
531
532 /* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings
533    make no sense for a format type not part of any C standard version.  */
534 static const format_length_info strfmon_length_specs[] =
535 {
536   /* A GNU extension.  */
537   { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
538   { NULL, 0, 0, NULL, 0, 0 }
539 };
540
541 static const format_flag_spec printf_flag_specs[] =
542 {
543   { ' ',  0, 0, N_("` ' flag"),        N_("the ` ' printf flag"),              STD_C89 },
544   { '+',  0, 0, N_("`+' flag"),        N_("the `+' printf flag"),              STD_C89 },
545   { '#',  0, 0, N_("`#' flag"),        N_("the `#' printf flag"),              STD_C89 },
546   { '0',  0, 0, N_("`0' flag"),        N_("the `0' printf flag"),              STD_C89 },
547   { '-',  0, 0, N_("`-' flag"),        N_("the `-' printf flag"),              STD_C89 },
548   { '\'', 0, 0, N_("`'' flag"),        N_("the `'' printf flag"),              STD_EXT },
549   { 'I',  0, 0, N_("`I' flag"),        N_("the `I' printf flag"),              STD_EXT },
550   { 'w',  0, 0, N_("field width"),     N_("field width in printf format"),     STD_C89 },
551   { 'p',  0, 0, N_("precision"),       N_("precision in printf format"),       STD_C89 },
552   { 'L',  0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
553   { 0, 0, 0, NULL, NULL, 0 }
554 };
555
556
557 static const format_flag_pair printf_flag_pairs[] =
558 {
559   { ' ', '+', 1, 0   },
560   { '0', '-', 1, 0   },
561   { '0', 'p', 1, 'i' },
562   { 0, 0, 0, 0 }
563 };
564
565 static const format_flag_spec asm_fprintf_flag_specs[] =
566 {
567   { ' ',  0, 0, N_("` ' flag"),        N_("the ` ' printf flag"),              STD_C89 },
568   { '+',  0, 0, N_("`+' flag"),        N_("the `+' printf flag"),              STD_C89 },
569   { '#',  0, 0, N_("`#' flag"),        N_("the `#' printf flag"),              STD_C89 },
570   { '0',  0, 0, N_("`0' flag"),        N_("the `0' printf flag"),              STD_C89 },
571   { '-',  0, 0, N_("`-' flag"),        N_("the `-' printf flag"),              STD_C89 },
572   { 'w',  0, 0, N_("field width"),     N_("field width in printf format"),     STD_C89 },
573   { 'p',  0, 0, N_("precision"),       N_("precision in printf format"),       STD_C89 },
574   { 'L',  0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
575   { 0, 0, 0, NULL, NULL, 0 }
576 };
577
578 static const format_flag_pair asm_fprintf_flag_pairs[] =
579 {
580   { ' ', '+', 1, 0   },
581   { '0', '-', 1, 0   },
582   { '0', 'p', 1, 'i' },
583   { 0, 0, 0, 0 }
584 };
585
586 static const format_flag_spec scanf_flag_specs[] =
587 {
588   { '*',  0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89 },
589   { 'a',  0, 0, N_("`a' flag"),               N_("the `a' scanf flag"),                       STD_EXT },
590   { 'w',  0, 0, N_("field width"),            N_("field width in scanf format"),              STD_C89 },
591   { 'L',  0, 0, N_("length modifier"),        N_("length modifier in scanf format"),          STD_C89 },
592   { '\'', 0, 0, N_("`'' flag"),               N_("the `'' scanf flag"),                       STD_EXT },
593   { 'I',  0, 0, N_("`I' flag"),               N_("the `I' scanf flag"),                       STD_EXT },
594   { 0, 0, 0, NULL, NULL, 0 }
595 };
596
597
598 static const format_flag_pair scanf_flag_pairs[] =
599 {
600   { '*', 'L', 0, 0 },
601   { 0, 0, 0, 0 }
602 };
603
604
605 static const format_flag_spec strftime_flag_specs[] =
606 {
607   { '_', 0,   0, N_("`_' flag"),     N_("the `_' strftime flag"),          STD_EXT },
608   { '-', 0,   0, N_("`-' flag"),     N_("the `-' strftime flag"),          STD_EXT },
609   { '0', 0,   0, N_("`0' flag"),     N_("the `0' strftime flag"),          STD_EXT },
610   { '^', 0,   0, N_("`^' flag"),     N_("the `^' strftime flag"),          STD_EXT },
611   { '#', 0,   0, N_("`#' flag"),     N_("the `#' strftime flag"),          STD_EXT },
612   { 'w', 0,   0, N_("field width"),  N_("field width in strftime format"), STD_EXT },
613   { 'E', 0,   0, N_("`E' modifier"), N_("the `E' strftime modifier"),      STD_C99 },
614   { 'O', 0,   0, N_("`O' modifier"), N_("the `O' strftime modifier"),      STD_C99 },
615   { 'O', 'o', 0, NULL,               N_("the `O' modifier"),               STD_EXT },
616   { 0, 0, 0, NULL, NULL, 0 }
617 };
618
619
620 static const format_flag_pair strftime_flag_pairs[] =
621 {
622   { 'E', 'O', 0, 0 },
623   { '_', '-', 0, 0 },
624   { '_', '0', 0, 0 },
625   { '-', '0', 0, 0 },
626   { '^', '#', 0, 0 },
627   { 0, 0, 0, 0 }
628 };
629
630
631 static const format_flag_spec strfmon_flag_specs[] =
632 {
633   { '=',  0, 1, N_("fill character"),  N_("fill character in strfmon format"),  STD_C89 },
634   { '^',  0, 0, N_("`^' flag"),        N_("the `^' strfmon flag"),              STD_C89 },
635   { '+',  0, 0, N_("`+' flag"),        N_("the `+' strfmon flag"),              STD_C89 },
636   { '(',  0, 0, N_("`(' flag"),        N_("the `(' strfmon flag"),              STD_C89 },
637   { '!',  0, 0, N_("`!' flag"),        N_("the `!' strfmon flag"),              STD_C89 },
638   { '-',  0, 0, N_("`-' flag"),        N_("the `-' strfmon flag"),              STD_C89 },
639   { 'w',  0, 0, N_("field width"),     N_("field width in strfmon format"),     STD_C89 },
640   { '#',  0, 0, N_("left precision"),  N_("left precision in strfmon format"),  STD_C89 },
641   { 'p',  0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89 },
642   { 'L',  0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89 },
643   { 0, 0, 0, NULL, NULL, 0 }
644 };
645
646 static const format_flag_pair strfmon_flag_pairs[] =
647 {
648   { '+', '(', 0, 0 },
649   { 0, 0, 0, 0 }
650 };
651
652
653 #define T_I     &integer_type_node
654 #define T89_I   { STD_C89, NULL, T_I }
655 #define T_L     &long_integer_type_node
656 #define T89_L   { STD_C89, NULL, T_L }
657 #define T_LL    &long_long_integer_type_node
658 #define T9L_LL  { STD_C9L, NULL, T_LL }
659 #define TEX_LL  { STD_EXT, NULL, T_LL }
660 #define T_S     &short_integer_type_node
661 #define T89_S   { STD_C89, NULL, T_S }
662 #define T_UI    &unsigned_type_node
663 #define T89_UI  { STD_C89, NULL, T_UI }
664 #define T_UL    &long_unsigned_type_node
665 #define T89_UL  { STD_C89, NULL, T_UL }
666 #define T_ULL   &long_long_unsigned_type_node
667 #define T9L_ULL { STD_C9L, NULL, T_ULL }
668 #define TEX_ULL { STD_EXT, NULL, T_ULL }
669 #define T_US    &short_unsigned_type_node
670 #define T89_US  { STD_C89, NULL, T_US }
671 #define T_F     &float_type_node
672 #define T89_F   { STD_C89, NULL, T_F }
673 #define T99_F   { STD_C99, NULL, T_F }
674 #define T_D     &double_type_node
675 #define T89_D   { STD_C89, NULL, T_D }
676 #define T99_D   { STD_C99, NULL, T_D }
677 #define T_LD    &long_double_type_node
678 #define T89_LD  { STD_C89, NULL, T_LD }
679 #define T99_LD  { STD_C99, NULL, T_LD }
680 #define T_C     &char_type_node
681 #define T89_C   { STD_C89, NULL, T_C }
682 #define T_SC    &signed_char_type_node
683 #define T99_SC  { STD_C99, NULL, T_SC }
684 #define T_UC    &unsigned_char_type_node
685 #define T99_UC  { STD_C99, NULL, T_UC }
686 #define T_V     &void_type_node
687 #define T89_V   { STD_C89, NULL, T_V }
688 #define T_W     &wchar_type_node
689 #define T94_W   { STD_C94, "wchar_t", T_W }
690 #define TEX_W   { STD_EXT, "wchar_t", T_W }
691 #define T_WI    &wint_type_node
692 #define T94_WI  { STD_C94, "wint_t", T_WI }
693 #define TEX_WI  { STD_EXT, "wint_t", T_WI }
694 #define T_ST    &size_type_node
695 #define T99_ST  { STD_C99, "size_t", T_ST }
696 #define T_SST   &signed_size_type_node
697 #define T99_SST { STD_C99, "signed size_t", T_SST }
698 #define T_PD    &ptrdiff_type_node
699 #define T99_PD  { STD_C99, "ptrdiff_t", T_PD }
700 #define T_UPD   &unsigned_ptrdiff_type_node
701 #define T99_UPD { STD_C99, "unsigned ptrdiff_t", T_UPD }
702 #define T_IM    &intmax_type_node
703 #define T99_IM  { STD_C99, "intmax_t", T_IM }
704 #define T_UIM   &uintmax_type_node
705 #define T99_UIM { STD_C99, "uintmax_t", T_UIM }
706
707 static const format_char_info print_char_table[] =
708 {
709   /* C89 conversion specifiers.  */
710   { "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"  },
711   { "oxX", 0, STD_C89, { T89_UI,  T99_UC,  T89_US,  T89_UL,  T9L_ULL, TEX_ULL, T99_ST,  T99_UPD, T99_UIM }, "-wp0#",    "i"  },
712   { "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"  },
713   { "fgG", 0, STD_C89, { T89_D,   BADLEN,  BADLEN,  T99_D,   BADLEN,  T89_LD,  BADLEN,  BADLEN,  BADLEN  }, "-wp0 +#'", ""   },
714   { "eE",  0, STD_C89, { T89_D,   BADLEN,  BADLEN,  T99_D,   BADLEN,  T89_LD,  BADLEN,  BADLEN,  BADLEN  }, "-wp0 +#",  ""   },
715   { "c",   0, STD_C89, { T89_I,   BADLEN,  BADLEN,  T94_WI,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-w",       ""   },
716   { "s",   1, STD_C89, { T89_C,   BADLEN,  BADLEN,  T94_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-wp",      "cR" },
717   { "p",   1, STD_C89, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-w",       "c"  },
718   { "n",   1, STD_C89, { T89_I,   T99_SC,  T89_S,   T89_L,   T9L_LL,  BADLEN,  T99_SST, T99_PD,  T99_IM  }, "",         "W"  },
719   /* C99 conversion specifiers.  */
720   { "F",   0, STD_C99, { T99_D,   BADLEN,  BADLEN,  T99_D,   BADLEN,  T99_LD,  BADLEN,  BADLEN,  BADLEN  }, "-wp0 +#'", ""   },
721   { "aA",  0, STD_C99, { T99_D,   BADLEN,  BADLEN,  T99_D,   BADLEN,  T99_LD,  BADLEN,  BADLEN,  BADLEN  }, "-wp0 +#",  ""   },
722   /* X/Open conversion specifiers.  */
723   { "C",   0, STD_EXT, { TEX_WI,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-w",       ""   },
724   { "S",   1, STD_EXT, { TEX_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-wp",      "R"  },
725   /* GNU conversion specifiers.  */
726   { "m",   0, STD_EXT, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-wp",      ""   },
727   { NULL,  0, 0, NOLENGTHS, NULL, NULL }
728 };
729
730 static const format_char_info asm_fprintf_char_table[] =
731 {
732   /* C89 conversion specifiers.  */
733   { "di",  0, STD_C89, { T89_I,   BADLEN,  BADLEN,  T89_L,   T9L_LL,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-wp0 +",  "i" },
734   { "oxX", 0, STD_C89, { T89_UI,  BADLEN,  BADLEN,  T89_UL,  T9L_ULL, BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-wp0#",   "i" },
735   { "u",   0, STD_C89, { T89_UI,  BADLEN,  BADLEN,  T89_UL,  T9L_ULL, BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-wp0",    "i" },
736   { "c",   0, STD_C89, { T89_I,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-w",       "" },
737   { "s",   1, STD_C89, { T89_C,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-wp",    "cR" },
738
739   /* asm_fprintf conversion specifiers.  */
740   { "O",   0, STD_C89, NOARGUMENTS, "",      ""   },
741   { "R",   0, STD_C89, NOARGUMENTS, "",      ""   },
742   { "I",   0, STD_C89, NOARGUMENTS, "",      ""   },
743   { "L",   0, STD_C89, NOARGUMENTS, "",      ""   },
744   { "U",   0, STD_C89, NOARGUMENTS, "",      ""   },
745   { "r",   0, STD_C89, { T89_I,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "",  "" },
746   { "@",   0, STD_C89, NOARGUMENTS, "",      ""   },
747   { NULL,  0, 0, NOLENGTHS, NULL, NULL }
748 };
749
750 static const format_char_info scan_char_table[] =
751 {
752   /* C89 conversion specifiers.  */
753   { "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"   },
754   { "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"   },
755   { "oxX",   1, STD_C89, { T89_UI,  T99_UC,  T89_US,  T89_UL,  T9L_ULL, TEX_ULL, T99_ST,  T99_UPD, T99_UIM }, "*w",   "W"   },
756   { "efgEG", 1, STD_C89, { T89_F,   BADLEN,  BADLEN,  T89_D,   BADLEN,  T89_LD,  BADLEN,  BADLEN,  BADLEN  }, "*w'",  "W"   },
757   { "c",     1, STD_C89, { T89_C,   BADLEN,  BADLEN,  T94_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "*w",   "cW"  },
758   { "s",     1, STD_C89, { T89_C,   BADLEN,  BADLEN,  T94_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "*aw",  "cW"  },
759   { "[",     1, STD_C89, { T89_C,   BADLEN,  BADLEN,  T94_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "*aw",  "cW[" },
760   { "p",     2, STD_C89, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "*w",   "W"   },
761   { "n",     1, STD_C89, { T89_I,   T99_SC,  T89_S,   T89_L,   T9L_LL,  BADLEN,  T99_SST, T99_PD,  T99_IM  }, "",     "W"   },
762   /* C99 conversion specifiers.  */
763   { "FaA",   1, STD_C99, { T99_F,   BADLEN,  BADLEN,  T99_D,   BADLEN,  T99_LD,  BADLEN,  BADLEN,  BADLEN  }, "*w'",  "W"   },
764   /* X/Open conversion specifiers.  */
765   { "C",     1, STD_EXT, { TEX_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "*w",   "W"   },
766   { "S",     1, STD_EXT, { TEX_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "*aw",  "W"   },
767   { NULL, 0, 0, NOLENGTHS, NULL, NULL }
768 };
769
770 static const format_char_info time_char_table[] =
771 {
772   /* C89 conversion specifiers.  */
773   { "ABZab",            0, STD_C89, NOLENGTHS, "^#",     ""   },
774   { "cx",               0, STD_C89, NOLENGTHS, "E",      "3"  },
775   { "HIMSUWdmw",        0, STD_C89, NOLENGTHS, "-_0Ow",  ""   },
776   { "j",                0, STD_C89, NOLENGTHS, "-_0Ow",  "o"  },
777   { "p",                0, STD_C89, NOLENGTHS, "#",      ""   },
778   { "X",                0, STD_C89, NOLENGTHS, "E",      ""   },
779   { "y",                0, STD_C89, NOLENGTHS, "EO-_0w", "4"  },
780   { "Y",                0, STD_C89, NOLENGTHS, "-_0EOw", "o"  },
781   { "%",                0, STD_C89, NOLENGTHS, "",       ""   },
782   /* C99 conversion specifiers.  */
783   { "C",                0, STD_C99, NOLENGTHS, "-_0EOw", "o"  },
784   { "D",                0, STD_C99, NOLENGTHS, "",       "2"  },
785   { "eVu",              0, STD_C99, NOLENGTHS, "-_0Ow",  ""   },
786   { "FRTnrt",           0, STD_C99, NOLENGTHS, "",       ""   },
787   { "g",                0, STD_C99, NOLENGTHS, "O-_0w",  "2o" },
788   { "G",                0, STD_C99, NOLENGTHS, "-_0Ow",  "o"  },
789   { "h",                0, STD_C99, NOLENGTHS, "^#",     ""   },
790   { "z",                0, STD_C99, NOLENGTHS, "O",      "o"  },
791   /* GNU conversion specifiers.  */
792   { "kls",              0, STD_EXT, NOLENGTHS, "-_0Ow",  ""   },
793   { "P",                0, STD_EXT, NOLENGTHS, "",       ""   },
794   { NULL,               0, 0, NOLENGTHS, NULL, NULL }
795 };
796
797 static const format_char_info monetary_char_table[] =
798 {
799   { "in", 0, STD_C89, { T89_D, BADLEN, BADLEN, BADLEN, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "=^+(!-w#p", "" },
800   { NULL, 0, 0, NOLENGTHS, NULL, NULL }
801 };
802
803
804 /* This must be in the same order as enum format_type.  */
805 static const format_kind_info format_types_orig[] =
806 {
807   { "printf",   printf_length_specs,  print_char_table, " +#0-'I", NULL, 
808     printf_flag_specs, printf_flag_pairs,
809     FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_EMPTY_PREC_OK,
810     'w', 0, 'p', 0, 'L',
811     &integer_type_node, &integer_type_node
812   },
813   { "asm_fprintf",   asm_fprintf_length_specs,  asm_fprintf_char_table, " +#0-", NULL, 
814     asm_fprintf_flag_specs, asm_fprintf_flag_pairs,
815     FMT_FLAG_ARG_CONVERT|FMT_FLAG_EMPTY_PREC_OK,
816     'w', 0, 'p', 0, 'L',
817     &integer_type_node, &integer_type_node
818   },
819   { "scanf",    scanf_length_specs,   scan_char_table,  "*'I", NULL, 
820     scanf_flag_specs, scanf_flag_pairs,
821     FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_ZERO_WIDTH_BAD|FMT_FLAG_DOLLAR_GAP_POINTER_OK,
822     'w', 0, 0, '*', 'L',
823     NULL, NULL
824   },
825   { "strftime", NULL,                 time_char_table,  "_-0^#", "EO",
826     strftime_flag_specs, strftime_flag_pairs,
827     FMT_FLAG_FANCY_PERCENT_OK, 'w', 0, 0, 0, 0,
828     NULL, NULL
829   },
830   { "strfmon",  strfmon_length_specs, monetary_char_table, "=^+(!-", NULL, 
831     strfmon_flag_specs, strfmon_flag_pairs,
832     FMT_FLAG_ARG_CONVERT, 'w', '#', 'p', 0, 'L',
833     NULL, NULL
834   }
835 };
836
837 /* This layer of indirection allows GCC to reassign format_types with
838    new data if necessary, while still allowing the original data to be
839    const.  */
840 static const format_kind_info *format_types = format_types_orig;
841
842 /* Structure detailing the results of checking a format function call
843    where the format expression may be a conditional expression with
844    many leaves resulting from nested conditional expressions.  */
845 typedef struct
846 {
847   /* Number of leaves of the format argument that could not be checked
848      as they were not string literals.  */
849   int number_non_literal;
850   /* Number of leaves of the format argument that were null pointers or
851      string literals, but had extra format arguments.  */
852   int number_extra_args;
853   /* Number of leaves of the format argument that were null pointers or
854      string literals, but had extra format arguments and used $ operand
855      numbers.  */
856   int number_dollar_extra_args;
857   /* Number of leaves of the format argument that were wide string
858      literals.  */
859   int number_wide;
860   /* Number of leaves of the format argument that were empty strings.  */
861   int number_empty;
862   /* Number of leaves of the format argument that were unterminated
863      strings.  */
864   int number_unterminated;
865   /* Number of leaves of the format argument that were not counted above.  */
866   int number_other;
867 } format_check_results;
868
869 typedef struct
870 {
871   format_check_results *res;
872   function_format_info *info;
873   tree params;
874   int *status;
875 } format_check_context;
876
877 static void check_format_info   PARAMS ((int *, function_format_info *, tree));
878 static void check_format_arg    PARAMS ((void *, tree, unsigned HOST_WIDE_INT));
879 static void check_format_info_main PARAMS ((int *, format_check_results *,
880                                             function_format_info *,
881                                             const char *, int, tree,
882                                             unsigned HOST_WIDE_INT));
883 static void status_warning PARAMS ((int *, const char *, ...))
884      ATTRIBUTE_PRINTF_2;
885
886 static void init_dollar_format_checking         PARAMS ((int, tree));
887 static int maybe_read_dollar_number             PARAMS ((int *, const char **, int,
888                                                          tree, tree *,
889                                                          const format_kind_info *));
890 static void finish_dollar_format_checking       PARAMS ((int *, format_check_results *, int));
891
892 static const format_flag_spec *get_flag_spec    PARAMS ((const format_flag_spec *,
893                                                          int, const char *));
894
895 static void check_format_types  PARAMS ((int *, format_wanted_type *));
896
897 /* Decode a format type from a string, returning the type, or
898    format_type_error if not valid, in which case the caller should print an
899    error message.  */
900 static enum format_type
901 decode_format_type (s)
902      const char *s;
903 {
904   int i;
905   int slen;
906   slen = strlen (s);
907   for (i = 0; i < (int) format_type_error; i++)
908     {
909       int alen;
910       if (!strcmp (s, format_types[i].name))
911         break;
912       alen = strlen (format_types[i].name);
913       if (slen == alen + 4 && s[0] == '_' && s[1] == '_'
914           && s[slen - 1] == '_' && s[slen - 2] == '_'
915           && !strncmp (s + 2, format_types[i].name, alen))
916         break;
917     }
918   return ((enum format_type) i);
919 }
920
921 \f
922 /* Check the argument list of a call to printf, scanf, etc.
923    ATTRS are the attributes on the function type.
924    PARAMS is the list of argument values.  Also, if -Wmissing-format-attribute,
925    warn for calls to vprintf or vscanf in functions with no such format
926    attribute themselves.  */
927
928 void
929 check_function_format (status, attrs, params)
930      int *status;
931      tree attrs;
932      tree params;
933 {
934   tree a;
935
936   /* See if this function has any format attributes.  */
937   for (a = attrs; a; a = TREE_CHAIN (a))
938     {
939       if (is_attribute_p ("format", TREE_PURPOSE (a)))
940         {
941           /* Yup; check it.  */
942           function_format_info info;
943           decode_format_attr (TREE_VALUE (a), &info, 1);
944           check_format_info (status, &info, params);
945           if (warn_missing_format_attribute && info.first_arg_num == 0
946               && (format_types[info.format_type].flags
947                   & (int) FMT_FLAG_ARG_CONVERT))
948             {
949               tree c;
950               for (c = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
951                    c;
952                    c = TREE_CHAIN (c))
953                 if (is_attribute_p ("format", TREE_PURPOSE (c))
954                     && (decode_format_type (IDENTIFIER_POINTER
955                                             (TREE_VALUE (TREE_VALUE (c))))
956                         == info.format_type))
957                   break;
958               if (c == NULL_TREE)
959                 {
960                   /* Check if the current function has a parameter to which
961                      the format attribute could be attached; if not, it
962                      can't be a candidate for a format attribute, despite
963                      the vprintf-like or vscanf-like call.  */
964                   tree args;
965                   for (args = DECL_ARGUMENTS (current_function_decl);
966                        args != 0;
967                        args = TREE_CHAIN (args))
968                     {
969                       if (TREE_CODE (TREE_TYPE (args)) == POINTER_TYPE
970                           && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args)))
971                               == char_type_node))
972                         break;
973                     }
974                   if (args != 0)
975                     warning ("function might be possible candidate for `%s' format attribute",
976                              format_types[info.format_type].name);
977                 }
978             }
979         }
980     }
981 }
982
983 /* This function replaces `warning' inside the printf format checking
984    functions.  If the `status' parameter is non-NULL, then it is
985    dereferenced and set to 1 whenever a warning is caught.  Otherwise
986    it warns as usual by replicating the innards of the warning
987    function from diagnostic.c.  */
988 static void
989 status_warning (int *status, const char *msgid, ...)
990 {
991   diagnostic_info diagnostic ;
992   va_list ap;
993   
994   va_start (ap, msgid);
995
996   if (status)
997     *status = 1;
998   else
999     {
1000       /* This duplicates the warning function behavior.  */
1001       diagnostic_set_info (&diagnostic, _(msgid), &ap,
1002                            input_filename, input_line, DK_WARNING);
1003       report_diagnostic (&diagnostic);
1004     }
1005
1006   va_end (ap);
1007 }
1008
1009 /* Variables used by the checking of $ operand number formats.  */
1010 static char *dollar_arguments_used = NULL;
1011 static char *dollar_arguments_pointer_p = NULL;
1012 static int dollar_arguments_alloc = 0;
1013 static int dollar_arguments_count;
1014 static int dollar_first_arg_num;
1015 static int dollar_max_arg_used;
1016 static int dollar_format_warned;
1017
1018 /* Initialize the checking for a format string that may contain $
1019    parameter number specifications; we will need to keep track of whether
1020    each parameter has been used.  FIRST_ARG_NUM is the number of the first
1021    argument that is a parameter to the format, or 0 for a vprintf-style
1022    function; PARAMS is the list of arguments starting at this argument.  */
1023
1024 static void
1025 init_dollar_format_checking (first_arg_num, params)
1026      int first_arg_num;
1027      tree params;
1028 {
1029   tree oparams = params;
1030
1031   dollar_first_arg_num = first_arg_num;
1032   dollar_arguments_count = 0;
1033   dollar_max_arg_used = 0;
1034   dollar_format_warned = 0;
1035   if (first_arg_num > 0)
1036     {
1037       while (params)
1038         {
1039           dollar_arguments_count++;
1040           params = TREE_CHAIN (params);
1041         }
1042     }
1043   if (dollar_arguments_alloc < dollar_arguments_count)
1044     {
1045       if (dollar_arguments_used)
1046         free (dollar_arguments_used);
1047       if (dollar_arguments_pointer_p)
1048         free (dollar_arguments_pointer_p);
1049       dollar_arguments_alloc = dollar_arguments_count;
1050       dollar_arguments_used = xmalloc (dollar_arguments_alloc);
1051       dollar_arguments_pointer_p = xmalloc (dollar_arguments_alloc);
1052     }
1053   if (dollar_arguments_alloc)
1054     {
1055       memset (dollar_arguments_used, 0, dollar_arguments_alloc);
1056       if (first_arg_num > 0)
1057         {
1058           int i = 0;
1059           params = oparams;
1060           while (params)
1061             {
1062               dollar_arguments_pointer_p[i] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params)))
1063                                                == POINTER_TYPE);
1064               params = TREE_CHAIN (params);
1065               i++;
1066             }
1067         }
1068     }
1069 }
1070
1071
1072 /* Look for a decimal number followed by a $ in *FORMAT.  If DOLLAR_NEEDED
1073    is set, it is an error if one is not found; otherwise, it is OK.  If
1074    such a number is found, check whether it is within range and mark that
1075    numbered operand as being used for later checking.  Returns the operand
1076    number if found and within range, zero if no such number was found and
1077    this is OK, or -1 on error.  PARAMS points to the first operand of the
1078    format; PARAM_PTR is made to point to the parameter referred to.  If
1079    a $ format is found, *FORMAT is updated to point just after it.  */
1080
1081 static int
1082 maybe_read_dollar_number (status, format, dollar_needed, params, param_ptr,
1083                           fki)
1084      int *status;
1085      const char **format;
1086      int dollar_needed;
1087      tree params;
1088      tree *param_ptr;
1089      const format_kind_info *fki;
1090 {
1091   int argnum;
1092   int overflow_flag;
1093   const char *fcp = *format;
1094   if (! ISDIGIT (*fcp))
1095     {
1096       if (dollar_needed)
1097         {
1098           status_warning (status, "missing $ operand number in format");
1099           return -1;
1100         }
1101       else
1102         return 0;
1103     }
1104   argnum = 0;
1105   overflow_flag = 0;
1106   while (ISDIGIT (*fcp))
1107     {
1108       int nargnum;
1109       nargnum = 10 * argnum + (*fcp - '0');
1110       if (nargnum < 0 || nargnum / 10 != argnum)
1111         overflow_flag = 1;
1112       argnum = nargnum;
1113       fcp++;
1114     }
1115   if (*fcp != '$')
1116     {
1117       if (dollar_needed)
1118         {
1119           status_warning (status, "missing $ operand number in format");
1120           return -1;
1121         }
1122       else
1123         return 0;
1124     }
1125   *format = fcp + 1;
1126   if (pedantic && !dollar_format_warned)
1127     {
1128       status_warning (status,
1129                       "%s does not support %%n$ operand number formats",
1130                       C_STD_NAME (STD_EXT));
1131       dollar_format_warned = 1;
1132     }
1133   if (overflow_flag || argnum == 0
1134       || (dollar_first_arg_num && argnum > dollar_arguments_count))
1135     {
1136       status_warning (status, "operand number out of range in format");
1137       return -1;
1138     }
1139   if (argnum > dollar_max_arg_used)
1140     dollar_max_arg_used = argnum;
1141   /* For vprintf-style functions we may need to allocate more memory to
1142      track which arguments are used.  */
1143   while (dollar_arguments_alloc < dollar_max_arg_used)
1144     {
1145       int nalloc;
1146       nalloc = 2 * dollar_arguments_alloc + 16;
1147       dollar_arguments_used = xrealloc (dollar_arguments_used, nalloc);
1148       dollar_arguments_pointer_p = xrealloc (dollar_arguments_pointer_p,
1149                                              nalloc);
1150       memset (dollar_arguments_used + dollar_arguments_alloc, 0,
1151               nalloc - dollar_arguments_alloc);
1152       dollar_arguments_alloc = nalloc;
1153     }
1154   if (!(fki->flags & (int) FMT_FLAG_DOLLAR_MULTIPLE)
1155       && dollar_arguments_used[argnum - 1] == 1)
1156     {
1157       dollar_arguments_used[argnum - 1] = 2;
1158       status_warning (status,
1159                       "format argument %d used more than once in %s format",
1160                       argnum, fki->name);
1161     }
1162   else
1163     dollar_arguments_used[argnum - 1] = 1;
1164   if (dollar_first_arg_num)
1165     {
1166       int i;
1167       *param_ptr = params;
1168       for (i = 1; i < argnum && *param_ptr != 0; i++)
1169         *param_ptr = TREE_CHAIN (*param_ptr);
1170
1171       if (*param_ptr == 0)
1172         {
1173           /* This case shouldn't be caught here.  */
1174           abort ();
1175         }
1176     }
1177   else
1178     *param_ptr = 0;
1179   return argnum;
1180 }
1181
1182
1183 /* Finish the checking for a format string that used $ operand number formats
1184    instead of non-$ formats.  We check for unused operands before used ones
1185    (a serious error, since the implementation of the format function
1186    can't know what types to pass to va_arg to find the later arguments).
1187    and for unused operands at the end of the format (if we know how many
1188    arguments the format had, so not for vprintf).  If there were operand
1189    numbers out of range on a non-vprintf-style format, we won't have reached
1190    here.  If POINTER_GAP_OK, unused arguments are OK if all arguments are
1191    pointers.  */
1192
1193 static void
1194 finish_dollar_format_checking (status, res, pointer_gap_ok)
1195      int *status;
1196      format_check_results *res;
1197      int pointer_gap_ok;
1198 {
1199   int i;
1200   bool found_pointer_gap = false;
1201   for (i = 0; i < dollar_max_arg_used; i++)
1202     {
1203       if (!dollar_arguments_used[i])
1204         {
1205           if (pointer_gap_ok && (dollar_first_arg_num == 0
1206                                  || dollar_arguments_pointer_p[i]))
1207             found_pointer_gap = true;
1208           else
1209             status_warning (status, "format argument %d unused before used argument %d in $-style format",
1210                             i + 1, dollar_max_arg_used);
1211         }
1212     }
1213   if (found_pointer_gap
1214       || (dollar_first_arg_num
1215           && dollar_max_arg_used < dollar_arguments_count))
1216     {
1217       res->number_other--;
1218       res->number_dollar_extra_args++;
1219     }
1220 }
1221
1222
1223 /* Retrieve the specification for a format flag.  SPEC contains the
1224    specifications for format flags for the applicable kind of format.
1225    FLAG is the flag in question.  If PREDICATES is NULL, the basic
1226    spec for that flag must be retrieved and this function aborts if
1227    it cannot be found.  If PREDICATES is not NULL, it is a string listing
1228    possible predicates for the spec entry; if an entry predicated on any
1229    of these is found, it is returned, otherwise NULL is returned.  */
1230
1231 static const format_flag_spec *
1232 get_flag_spec (spec, flag, predicates)
1233      const format_flag_spec *spec;
1234      int flag;
1235      const char *predicates;
1236 {
1237   int i;
1238   for (i = 0; spec[i].flag_char != 0; i++)
1239     {
1240       if (spec[i].flag_char != flag)
1241         continue;
1242       if (predicates != NULL)
1243         {
1244           if (spec[i].predicate != 0
1245               && strchr (predicates, spec[i].predicate) != 0)
1246             return &spec[i];
1247         }
1248       else if (spec[i].predicate == 0)
1249         return &spec[i];
1250     }
1251   if (predicates == NULL)
1252     abort ();
1253   else
1254     return NULL;
1255 }
1256
1257
1258 /* Check the argument list of a call to printf, scanf, etc.
1259    INFO points to the function_format_info structure.
1260    PARAMS is the list of argument values.  */
1261
1262 static void
1263 check_format_info (status, info, params)
1264      int *status;
1265      function_format_info *info;
1266      tree params;
1267 {
1268   format_check_context format_ctx;
1269   unsigned HOST_WIDE_INT arg_num;
1270   tree format_tree;
1271   format_check_results res;
1272   /* Skip to format argument.  If the argument isn't available, there's
1273      no work for us to do; prototype checking will catch the problem.  */
1274   for (arg_num = 1; ; ++arg_num)
1275     {
1276       if (params == 0)
1277         return;
1278       if (arg_num == info->format_num)
1279         break;
1280       params = TREE_CHAIN (params);
1281     }
1282   format_tree = TREE_VALUE (params);
1283   params = TREE_CHAIN (params);
1284   if (format_tree == 0)
1285     return;
1286
1287   res.number_non_literal = 0;
1288   res.number_extra_args = 0;
1289   res.number_dollar_extra_args = 0;
1290   res.number_wide = 0;
1291   res.number_empty = 0;
1292   res.number_unterminated = 0;
1293   res.number_other = 0;
1294
1295   format_ctx.res = &res;
1296   format_ctx.info = info;
1297   format_ctx.params = params;
1298   format_ctx.status = status;
1299
1300   check_function_arguments_recurse (check_format_arg, &format_ctx,
1301                                     format_tree, arg_num);
1302
1303   if (res.number_non_literal > 0)
1304     {
1305       /* Functions taking a va_list normally pass a non-literal format
1306          string.  These functions typically are declared with
1307          first_arg_num == 0, so avoid warning in those cases.  */
1308       if (!(format_types[info->format_type].flags & (int) FMT_FLAG_ARG_CONVERT))
1309         {
1310           /* For strftime-like formats, warn for not checking the format
1311              string; but there are no arguments to check.  */
1312           if (warn_format_nonliteral)
1313             status_warning (status, "format not a string literal, format string not checked");
1314         }
1315       else if (info->first_arg_num != 0)
1316         {
1317           /* If there are no arguments for the format at all, we may have
1318              printf (foo) which is likely to be a security hole.  */
1319           while (arg_num + 1 < info->first_arg_num)
1320             {
1321               if (params == 0)
1322                 break;
1323               params = TREE_CHAIN (params);
1324               ++arg_num;
1325             }
1326           if (params == 0 && (warn_format_nonliteral || warn_format_security))
1327             status_warning (status, "format not a string literal and no format arguments");
1328           else if (warn_format_nonliteral)
1329             status_warning (status, "format not a string literal, argument types not checked");
1330         }
1331     }
1332
1333   /* If there were extra arguments to the format, normally warn.  However,
1334      the standard does say extra arguments are ignored, so in the specific
1335      case where we have multiple leaves (conditional expressions or
1336      ngettext) allow extra arguments if at least one leaf didn't have extra
1337      arguments, but was otherwise OK (either non-literal or checked OK).
1338      If the format is an empty string, this should be counted similarly to the
1339      case of extra format arguments.  */
1340   if (res.number_extra_args > 0 && res.number_non_literal == 0
1341       && res.number_other == 0 && warn_format_extra_args)
1342     status_warning (status, "too many arguments for format");
1343   if (res.number_dollar_extra_args > 0 && res.number_non_literal == 0
1344       && res.number_other == 0 && warn_format_extra_args)
1345     status_warning (status, "unused arguments in $-style format");
1346   if (res.number_empty > 0 && res.number_non_literal == 0
1347       && res.number_other == 0 && warn_format_zero_length)
1348     status_warning (status, "zero-length %s format string",
1349                     format_types[info->format_type].name);
1350
1351   if (res.number_wide > 0)
1352     status_warning (status, "format is a wide character string");
1353
1354   if (res.number_unterminated > 0)
1355     status_warning (status, "unterminated format string");
1356 }
1357
1358 /* Callback from check_function_arguments_recurse to check a
1359    format string.  FORMAT_TREE is the format parameter.  ARG_NUM
1360    is the number of the format argument.  CTX points to a
1361    format_check_context.  */
1362
1363 static void
1364 check_format_arg (ctx, format_tree, arg_num)
1365      void *ctx;
1366      tree format_tree;
1367      unsigned HOST_WIDE_INT arg_num;
1368 {
1369   format_check_context *format_ctx = ctx;
1370   format_check_results *res = format_ctx->res;
1371   function_format_info *info = format_ctx->info;
1372   tree params = format_ctx->params;
1373   int *status = format_ctx->status;
1374
1375   int format_length;
1376   HOST_WIDE_INT offset;
1377   const char *format_chars;
1378   tree array_size = 0;
1379   tree array_init;
1380
1381   if (integer_zerop (format_tree))
1382     {
1383       /* Skip to first argument to check, so we can see if this format
1384          has any arguments (it shouldn't).  */
1385       while (arg_num + 1 < info->first_arg_num)
1386         {
1387           if (params == 0)
1388             return;
1389           params = TREE_CHAIN (params);
1390           ++arg_num;
1391         }
1392
1393       if (params == 0)
1394         res->number_other++;
1395       else
1396         res->number_extra_args++;
1397
1398       return;
1399     }
1400
1401   offset = 0;
1402   if (TREE_CODE (format_tree) == PLUS_EXPR)
1403     {
1404       tree arg0, arg1;
1405
1406       arg0 = TREE_OPERAND (format_tree, 0);
1407       arg1 = TREE_OPERAND (format_tree, 1);
1408       STRIP_NOPS (arg0);
1409       STRIP_NOPS (arg1);
1410       if (TREE_CODE (arg1) == INTEGER_CST)
1411         format_tree = arg0;
1412       else if (TREE_CODE (arg0) == INTEGER_CST)
1413         {
1414           format_tree = arg1;
1415           arg1 = arg0;
1416         }
1417       else
1418         {
1419           res->number_non_literal++;
1420           return;
1421         }
1422       if (!host_integerp (arg1, 0)
1423           || (offset = tree_low_cst (arg1, 0)) < 0)
1424         {
1425           res->number_non_literal++;
1426           return;
1427         }
1428     }
1429   if (TREE_CODE (format_tree) != ADDR_EXPR)
1430     {
1431       res->number_non_literal++;
1432       return;
1433     }
1434   format_tree = TREE_OPERAND (format_tree, 0);
1435   if (TREE_CODE (format_tree) == VAR_DECL
1436       && TREE_CODE (TREE_TYPE (format_tree)) == ARRAY_TYPE
1437       && (array_init = decl_constant_value (format_tree)) != format_tree
1438       && TREE_CODE (array_init) == STRING_CST)
1439     {
1440       /* Extract the string constant initializer.  Note that this may include
1441          a trailing NUL character that is not in the array (e.g.
1442          const char a[3] = "foo";).  */
1443       array_size = DECL_SIZE_UNIT (format_tree);
1444       format_tree = array_init;
1445     }
1446   if (TREE_CODE (format_tree) != STRING_CST)
1447     {
1448       res->number_non_literal++;
1449       return;
1450     }
1451   if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree))) != char_type_node)
1452     {
1453       res->number_wide++;
1454       return;
1455     }
1456   format_chars = TREE_STRING_POINTER (format_tree);
1457   format_length = TREE_STRING_LENGTH (format_tree);
1458   if (array_size != 0)
1459     {
1460       /* Variable length arrays can't be initialized.  */
1461       if (TREE_CODE (array_size) != INTEGER_CST)
1462         abort ();
1463       if (host_integerp (array_size, 0))
1464         {
1465           HOST_WIDE_INT array_size_value = TREE_INT_CST_LOW (array_size);
1466           if (array_size_value > 0
1467               && array_size_value == (int) array_size_value
1468               && format_length > array_size_value)
1469             format_length = array_size_value;
1470         }
1471     }
1472   if (offset)
1473     {
1474       if (offset >= format_length)
1475         {
1476           res->number_non_literal++;
1477           return;
1478         }
1479       format_chars += offset;
1480       format_length -= offset;
1481     }
1482   if (format_length < 1)
1483     {
1484       res->number_unterminated++;
1485       return;
1486     }
1487   if (format_length == 1)
1488     {
1489       res->number_empty++;
1490       return;
1491     }
1492   if (format_chars[--format_length] != 0)
1493     {
1494       res->number_unterminated++;
1495       return;
1496     }
1497
1498   /* Skip to first argument to check.  */
1499   while (arg_num + 1 < info->first_arg_num)
1500     {
1501       if (params == 0)
1502         return;
1503       params = TREE_CHAIN (params);
1504       ++arg_num;
1505     }
1506   /* Provisionally increment res->number_other; check_format_info_main
1507      will decrement it if it finds there are extra arguments, but this way
1508      need not adjust it for every return.  */
1509   res->number_other++;
1510   check_format_info_main (status, res, info, format_chars, format_length,
1511                           params, arg_num);
1512 }
1513
1514
1515 /* Do the main part of checking a call to a format function.  FORMAT_CHARS
1516    is the NUL-terminated format string (which at this point may contain
1517    internal NUL characters); FORMAT_LENGTH is its length (excluding the
1518    terminating NUL character).  ARG_NUM is one less than the number of
1519    the first format argument to check; PARAMS points to that format
1520    argument in the list of arguments.  */
1521
1522 static void
1523 check_format_info_main (status, res, info, format_chars, format_length,
1524                         params, arg_num)
1525      int *status;
1526      format_check_results *res;
1527      function_format_info *info;
1528      const char *format_chars;
1529      int format_length;
1530      tree params;
1531      unsigned HOST_WIDE_INT arg_num;
1532 {
1533   const char *orig_format_chars = format_chars;
1534   tree first_fillin_param = params;
1535
1536   const format_kind_info *fki = &format_types[info->format_type];
1537   const format_flag_spec *flag_specs = fki->flag_specs;
1538   const format_flag_pair *bad_flag_pairs = fki->bad_flag_pairs;
1539
1540   /* -1 if no conversions taking an operand have been found; 0 if one has
1541      and it didn't use $; 1 if $ formats are in use.  */
1542   int has_operand_number = -1;
1543
1544   init_dollar_format_checking (info->first_arg_num, first_fillin_param);
1545
1546   while (1)
1547     {
1548       int i;
1549       int suppressed = FALSE;
1550       const char *length_chars = NULL;
1551       enum format_lengths length_chars_val = FMT_LEN_none;
1552       enum format_std_version length_chars_std = STD_C89;
1553       int format_char;
1554       tree cur_param;
1555       tree wanted_type;
1556       int main_arg_num = 0;
1557       tree main_arg_params = 0;
1558       enum format_std_version wanted_type_std;
1559       const char *wanted_type_name;
1560       format_wanted_type width_wanted_type;
1561       format_wanted_type precision_wanted_type;
1562       format_wanted_type main_wanted_type;
1563       format_wanted_type *first_wanted_type = NULL;
1564       format_wanted_type *last_wanted_type = NULL;
1565       const format_length_info *fli = NULL;
1566       const format_char_info *fci = NULL;
1567       char flag_chars[256];
1568       int aflag = 0;
1569       if (*format_chars == 0)
1570         {
1571           if (format_chars - orig_format_chars != format_length)
1572             status_warning (status, "embedded `\\0' in format");
1573           if (info->first_arg_num != 0 && params != 0
1574               && has_operand_number <= 0)
1575             {
1576               res->number_other--;
1577               res->number_extra_args++;
1578             }
1579           if (has_operand_number > 0)
1580             finish_dollar_format_checking (status, res, fki->flags & (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK);
1581           return;
1582         }
1583       if (*format_chars++ != '%')
1584         continue;
1585       if (*format_chars == 0)
1586         {
1587           status_warning (status, "spurious trailing `%%' in format");
1588           continue;
1589         }
1590       if (*format_chars == '%')
1591         {
1592           ++format_chars;
1593           continue;
1594         }
1595       flag_chars[0] = 0;
1596
1597       if ((fki->flags & (int) FMT_FLAG_USE_DOLLAR) && has_operand_number != 0)
1598         {
1599           /* Possibly read a $ operand number at the start of the format.
1600              If one was previously used, one is required here.  If one
1601              is not used here, we can't immediately conclude this is a
1602              format without them, since it could be printf %m or scanf %*.  */
1603           int opnum;
1604           opnum = maybe_read_dollar_number (status, &format_chars, 0,
1605                                             first_fillin_param,
1606                                             &main_arg_params, fki);
1607           if (opnum == -1)
1608             return;
1609           else if (opnum > 0)
1610             {
1611               has_operand_number = 1;
1612               main_arg_num = opnum + info->first_arg_num - 1;
1613             }
1614         }
1615
1616       /* Read any format flags, but do not yet validate them beyond removing
1617          duplicates, since in general validation depends on the rest of
1618          the format.  */
1619       while (*format_chars != 0
1620              && strchr (fki->flag_chars, *format_chars) != 0)
1621         {
1622           const format_flag_spec *s = get_flag_spec (flag_specs,
1623                                                      *format_chars, NULL);
1624           if (strchr (flag_chars, *format_chars) != 0)
1625             {
1626               status_warning (status, "repeated %s in format", _(s->name));
1627             }
1628           else
1629             {
1630               i = strlen (flag_chars);
1631               flag_chars[i++] = *format_chars;
1632               flag_chars[i] = 0;
1633             }
1634           if (s->skip_next_char)
1635             {
1636               ++format_chars;
1637               if (*format_chars == 0)
1638                 {
1639                   status_warning (status, "missing fill character at end of strfmon format");
1640                   return;
1641                 }
1642             }
1643           ++format_chars;
1644         }
1645
1646       /* Read any format width, possibly * or *m$.  */
1647       if (fki->width_char != 0)
1648         {
1649           if (fki->width_type != NULL && *format_chars == '*')
1650             {
1651               i = strlen (flag_chars);
1652               flag_chars[i++] = fki->width_char;
1653               flag_chars[i] = 0;
1654               /* "...a field width...may be indicated by an asterisk.
1655                  In this case, an int argument supplies the field width..."  */
1656               ++format_chars;
1657               if (has_operand_number != 0)
1658                 {
1659                   int opnum;
1660                   opnum = maybe_read_dollar_number (status, &format_chars,
1661                                                     has_operand_number == 1,
1662                                                     first_fillin_param,
1663                                                     &params, fki);
1664                   if (opnum == -1)
1665                     return;
1666                   else if (opnum > 0)
1667                     {
1668                       has_operand_number = 1;
1669                       arg_num = opnum + info->first_arg_num - 1;
1670                     }
1671                   else
1672                     has_operand_number = 0;
1673                 }
1674               if (info->first_arg_num != 0)
1675                 {
1676                   if (params == 0)
1677                     {
1678                       status_warning (status, "too few arguments for format");
1679                       return;
1680                     }
1681                   cur_param = TREE_VALUE (params);
1682                   if (has_operand_number <= 0)
1683                     {
1684                       params = TREE_CHAIN (params);
1685                       ++arg_num;
1686                     }
1687                   width_wanted_type.wanted_type = *fki->width_type;
1688                   width_wanted_type.wanted_type_name = NULL;
1689                   width_wanted_type.pointer_count = 0;
1690                   width_wanted_type.char_lenient_flag = 0;
1691                   width_wanted_type.writing_in_flag = 0;
1692                   width_wanted_type.reading_from_flag = 0;
1693                   width_wanted_type.name = _("field width");
1694                   width_wanted_type.param = cur_param;
1695                   width_wanted_type.arg_num = arg_num;
1696                   width_wanted_type.next = NULL;
1697                   if (last_wanted_type != 0)
1698                     last_wanted_type->next = &width_wanted_type;
1699                   if (first_wanted_type == 0)
1700                     first_wanted_type = &width_wanted_type;
1701                   last_wanted_type = &width_wanted_type;
1702                 }
1703             }
1704           else
1705             {
1706               /* Possibly read a numeric width.  If the width is zero,
1707                  we complain if appropriate.  */
1708               int non_zero_width_char = FALSE;
1709               int found_width = FALSE;
1710               while (ISDIGIT (*format_chars))
1711                 {
1712                   found_width = TRUE;
1713                   if (*format_chars != '0')
1714                     non_zero_width_char = TRUE;
1715                   ++format_chars;
1716                 }
1717               if (found_width && !non_zero_width_char &&
1718                   (fki->flags & (int) FMT_FLAG_ZERO_WIDTH_BAD))
1719                 status_warning (status, "zero width in %s format",
1720                                 fki->name);
1721               if (found_width)
1722                 {
1723                   i = strlen (flag_chars);
1724                   flag_chars[i++] = fki->width_char;
1725                   flag_chars[i] = 0;
1726                 }
1727             }
1728         }
1729
1730       /* Read any format left precision (must be a number, not *).  */
1731       if (fki->left_precision_char != 0 && *format_chars == '#')
1732         {
1733           ++format_chars;
1734           i = strlen (flag_chars);
1735           flag_chars[i++] = fki->left_precision_char;
1736           flag_chars[i] = 0;
1737           if (!ISDIGIT (*format_chars))
1738             status_warning (status, "empty left precision in %s format",
1739                             fki->name);
1740           while (ISDIGIT (*format_chars))
1741             ++format_chars;
1742         }
1743
1744       /* Read any format precision, possibly * or *m$.  */
1745       if (fki->precision_char != 0 && *format_chars == '.')
1746         {
1747           ++format_chars;
1748           i = strlen (flag_chars);
1749           flag_chars[i++] = fki->precision_char;
1750           flag_chars[i] = 0;
1751           if (fki->precision_type != NULL && *format_chars == '*')
1752             {
1753               /* "...a...precision...may be indicated by an asterisk.
1754                  In this case, an int argument supplies the...precision."  */
1755               ++format_chars;
1756               if (has_operand_number != 0)
1757                 {
1758                   int opnum;
1759                   opnum = maybe_read_dollar_number (status, &format_chars,
1760                                                     has_operand_number == 1,
1761                                                     first_fillin_param,
1762                                                     &params, fki);
1763                   if (opnum == -1)
1764                     return;
1765                   else if (opnum > 0)
1766                     {
1767                       has_operand_number = 1;
1768                       arg_num = opnum + info->first_arg_num - 1;
1769                     }
1770                   else
1771                     has_operand_number = 0;
1772                 }
1773               if (info->first_arg_num != 0)
1774                 {
1775                   if (params == 0)
1776                     {
1777                       status_warning (status, "too few arguments for format");
1778                       return;
1779                     }
1780                   cur_param = TREE_VALUE (params);
1781                   if (has_operand_number <= 0)
1782                     {
1783                       params = TREE_CHAIN (params);
1784                       ++arg_num;
1785                     }
1786                   precision_wanted_type.wanted_type = *fki->precision_type;
1787                   precision_wanted_type.wanted_type_name = NULL;
1788                   precision_wanted_type.pointer_count = 0;
1789                   precision_wanted_type.char_lenient_flag = 0;
1790                   precision_wanted_type.writing_in_flag = 0;
1791                   precision_wanted_type.reading_from_flag = 0;
1792                   precision_wanted_type.name = _("field precision");
1793                   precision_wanted_type.param = cur_param;
1794                   precision_wanted_type.arg_num = arg_num;
1795                   precision_wanted_type.next = NULL;
1796                   if (last_wanted_type != 0)
1797                     last_wanted_type->next = &precision_wanted_type;
1798                   if (first_wanted_type == 0)
1799                     first_wanted_type = &precision_wanted_type;
1800                   last_wanted_type = &precision_wanted_type;
1801                 }
1802             }
1803           else
1804             {
1805               if (!(fki->flags & (int) FMT_FLAG_EMPTY_PREC_OK)
1806                   && !ISDIGIT (*format_chars))
1807                 status_warning (status, "empty precision in %s format",
1808                                 fki->name);
1809               while (ISDIGIT (*format_chars))
1810                 ++format_chars;
1811             }
1812         }
1813
1814       /* Read any length modifier, if this kind of format has them.  */
1815       fli = fki->length_char_specs;
1816       length_chars = NULL;
1817       length_chars_val = FMT_LEN_none;
1818       length_chars_std = STD_C89;
1819       if (fli)
1820         {
1821           while (fli->name != 0 && fli->name[0] != *format_chars)
1822             fli++;
1823           if (fli->name != 0)
1824             {
1825               format_chars++;
1826               if (fli->double_name != 0 && fli->name[0] == *format_chars)
1827                 {
1828                   format_chars++;
1829                   length_chars = fli->double_name;
1830                   length_chars_val = fli->double_index;
1831                   length_chars_std = fli->double_std;
1832                 }
1833               else
1834                 {
1835                   length_chars = fli->name;
1836                   length_chars_val = fli->index;
1837                   length_chars_std = fli->std;
1838                 }
1839               i = strlen (flag_chars);
1840               flag_chars[i++] = fki->length_code_char;
1841               flag_chars[i] = 0;
1842             }
1843           if (pedantic)
1844             {
1845               /* Warn if the length modifier is non-standard.  */
1846               if (ADJ_STD (length_chars_std) > C_STD_VER)
1847                 status_warning (status, "%s does not support the `%s' %s length modifier",
1848                                 C_STD_NAME (length_chars_std), length_chars,
1849                                 fki->name);
1850             }
1851         }
1852
1853       /* Read any modifier (strftime E/O).  */
1854       if (fki->modifier_chars != NULL)
1855         {
1856           while (*format_chars != 0
1857                  && strchr (fki->modifier_chars, *format_chars) != 0)
1858             {
1859               if (strchr (flag_chars, *format_chars) != 0)
1860                 {
1861                   const format_flag_spec *s = get_flag_spec (flag_specs,
1862                                                              *format_chars, NULL);
1863                   status_warning (status, "repeated %s in format", _(s->name));
1864                 }
1865               else
1866                 {
1867                   i = strlen (flag_chars);
1868                   flag_chars[i++] = *format_chars;
1869                   flag_chars[i] = 0;
1870                 }
1871               ++format_chars;
1872             }
1873         }
1874
1875       /* Handle the scanf allocation kludge.  */
1876       if (fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
1877         {
1878           if (*format_chars == 'a' && !flag_isoc99)
1879             {
1880               if (format_chars[1] == 's' || format_chars[1] == 'S'
1881                   || format_chars[1] == '[')
1882                 {
1883                   /* `a' is used as a flag.  */
1884                   i = strlen (flag_chars);
1885                   flag_chars[i++] = 'a';
1886                   flag_chars[i] = 0;
1887                   format_chars++;
1888                 }
1889             }
1890         }
1891
1892       format_char = *format_chars;
1893       if (format_char == 0
1894           || (!(fki->flags & (int) FMT_FLAG_FANCY_PERCENT_OK)
1895               && format_char == '%'))
1896         {
1897           status_warning (status, "conversion lacks type at end of format");
1898           continue;
1899         }
1900       format_chars++;
1901       fci = fki->conversion_specs;
1902       while (fci->format_chars != 0
1903              && strchr (fci->format_chars, format_char) == 0)
1904           ++fci;
1905       if (fci->format_chars == 0)
1906         {
1907           if (ISGRAPH(format_char))
1908             status_warning (status, "unknown conversion type character `%c' in format",
1909                      format_char);
1910           else
1911             status_warning (status, "unknown conversion type character 0x%x in format",
1912                      format_char);
1913           continue;
1914         }
1915       if (pedantic)
1916         {
1917           if (ADJ_STD (fci->std) > C_STD_VER)
1918             status_warning (status, "%s does not support the `%%%c' %s format",
1919                             C_STD_NAME (fci->std), format_char, fki->name);
1920         }
1921
1922       /* Validate the individual flags used, removing any that are invalid.  */
1923       {
1924         int d = 0;
1925         for (i = 0; flag_chars[i] != 0; i++)
1926           {
1927             const format_flag_spec *s = get_flag_spec (flag_specs,
1928                                                        flag_chars[i], NULL);
1929             flag_chars[i - d] = flag_chars[i];
1930             if (flag_chars[i] == fki->length_code_char)
1931               continue;
1932             if (strchr (fci->flag_chars, flag_chars[i]) == 0)
1933               {
1934                 status_warning (status, "%s used with `%%%c' %s format",
1935                                 _(s->name), format_char, fki->name);
1936                 d++;
1937                 continue;
1938               }
1939             if (pedantic)
1940               {
1941                 const format_flag_spec *t;
1942                 if (ADJ_STD (s->std) > C_STD_VER)
1943                   status_warning (status, "%s does not support %s",
1944                                   C_STD_NAME (s->std), _(s->long_name));
1945                 t = get_flag_spec (flag_specs, flag_chars[i], fci->flags2);
1946                 if (t != NULL && ADJ_STD (t->std) > ADJ_STD (s->std))
1947                   {
1948                     const char *long_name = (t->long_name != NULL
1949                                              ? t->long_name
1950                                              : s->long_name);
1951                     if (ADJ_STD (t->std) > C_STD_VER)
1952                       status_warning (status, "%s does not support %s with the `%%%c' %s format",
1953                                       C_STD_NAME (t->std), _(long_name),
1954                                       format_char, fki->name);
1955                   }
1956               }
1957           }
1958         flag_chars[i - d] = 0;
1959       }
1960
1961       if ((fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
1962           && strchr (flag_chars, 'a') != 0)
1963         aflag = 1;
1964
1965       if (fki->suppression_char
1966           && strchr (flag_chars, fki->suppression_char) != 0)
1967         suppressed = 1;
1968
1969       /* Validate the pairs of flags used.  */
1970       for (i = 0; bad_flag_pairs[i].flag_char1 != 0; i++)
1971         {
1972           const format_flag_spec *s, *t;
1973           if (strchr (flag_chars, bad_flag_pairs[i].flag_char1) == 0)
1974             continue;
1975           if (strchr (flag_chars, bad_flag_pairs[i].flag_char2) == 0)
1976             continue;
1977           if (bad_flag_pairs[i].predicate != 0
1978               && strchr (fci->flags2, bad_flag_pairs[i].predicate) == 0)
1979             continue;
1980           s = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char1, NULL);
1981           t = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char2, NULL);
1982           if (bad_flag_pairs[i].ignored)
1983             {
1984               if (bad_flag_pairs[i].predicate != 0)
1985                 status_warning (status, "%s ignored with %s and `%%%c' %s format",
1986                                 _(s->name), _(t->name), format_char,
1987                                 fki->name);
1988               else
1989                 status_warning (status, "%s ignored with %s in %s format",
1990                                 _(s->name), _(t->name), fki->name);
1991             }
1992           else
1993             {
1994               if (bad_flag_pairs[i].predicate != 0)
1995                 status_warning (status, "use of %s and %s together with `%%%c' %s format",
1996                                 _(s->name), _(t->name), format_char,
1997                                 fki->name);
1998               else
1999                 status_warning (status, "use of %s and %s together in %s format",
2000                                 _(s->name), _(t->name), fki->name);
2001             }
2002         }
2003
2004       /* Give Y2K warnings.  */
2005       if (warn_format_y2k)
2006         {
2007           int y2k_level = 0;
2008           if (strchr (fci->flags2, '4') != 0)
2009             if (strchr (flag_chars, 'E') != 0)
2010               y2k_level = 3;
2011             else
2012               y2k_level = 2;
2013           else if (strchr (fci->flags2, '3') != 0)
2014             y2k_level = 3;
2015           else if (strchr (fci->flags2, '2') != 0)
2016             y2k_level = 2;
2017           if (y2k_level == 3)
2018             status_warning (status, "`%%%c' yields only last 2 digits of year in some locales",
2019                             format_char);
2020           else if (y2k_level == 2)
2021             status_warning (status, "`%%%c' yields only last 2 digits of year", format_char);
2022         }
2023
2024       if (strchr (fci->flags2, '[') != 0)
2025         {
2026           /* Skip over scan set, in case it happens to have '%' in it.  */
2027           if (*format_chars == '^')
2028             ++format_chars;
2029           /* Find closing bracket; if one is hit immediately, then
2030              it's part of the scan set rather than a terminator.  */
2031           if (*format_chars == ']')
2032             ++format_chars;
2033           while (*format_chars && *format_chars != ']')
2034             ++format_chars;
2035           if (*format_chars != ']')
2036             /* The end of the format string was reached.  */
2037             status_warning (status, "no closing `]' for `%%[' format");
2038         }
2039
2040       wanted_type = 0;
2041       wanted_type_name = 0;
2042       if (fki->flags & (int) FMT_FLAG_ARG_CONVERT)
2043         {
2044           wanted_type = (fci->types[length_chars_val].type
2045                          ? *fci->types[length_chars_val].type : 0);
2046           wanted_type_name = fci->types[length_chars_val].name;
2047           wanted_type_std = fci->types[length_chars_val].std;
2048           if (wanted_type == 0)
2049             {
2050               status_warning (status, "use of `%s' length modifier with `%c' type character",
2051                               length_chars, format_char);
2052               /* Heuristic: skip one argument when an invalid length/type
2053                  combination is encountered.  */
2054               arg_num++;
2055               if (params == 0)
2056                 {
2057                   status_warning (status, "too few arguments for format");
2058                   return;
2059                 }
2060               params = TREE_CHAIN (params);
2061               continue;
2062             }
2063           else if (pedantic
2064                    /* Warn if non-standard, provided it is more non-standard
2065                       than the length and type characters that may already
2066                       have been warned for.  */
2067                    && ADJ_STD (wanted_type_std) > ADJ_STD (length_chars_std)
2068                    && ADJ_STD (wanted_type_std) > ADJ_STD (fci->std))
2069             {
2070               if (ADJ_STD (wanted_type_std) > C_STD_VER)
2071                 status_warning (status, "%s does not support the `%%%s%c' %s format",
2072                                 C_STD_NAME (wanted_type_std), length_chars,
2073                                 format_char, fki->name);
2074             }
2075         }
2076
2077       /* Finally. . .check type of argument against desired type!  */
2078       if (info->first_arg_num == 0)
2079         continue;
2080       if ((fci->pointer_count == 0 && wanted_type == void_type_node)
2081           || suppressed)
2082         {
2083           if (main_arg_num != 0)
2084             {
2085               if (suppressed)
2086                 status_warning (status, "operand number specified with suppressed assignment");
2087               else
2088                 status_warning (status, "operand number specified for format taking no argument");
2089             }
2090         }
2091       else
2092         {
2093           if (main_arg_num != 0)
2094             {
2095               arg_num = main_arg_num;
2096               params = main_arg_params;
2097             }
2098           else
2099             {
2100               ++arg_num;
2101               if (has_operand_number > 0)
2102                 {
2103                   status_warning (status, "missing $ operand number in format");
2104                   return;
2105                 }
2106               else
2107                 has_operand_number = 0;
2108               if (params == 0)
2109                 {
2110                   status_warning (status, "too few arguments for format");
2111                   return;
2112                 }
2113             }
2114           cur_param = TREE_VALUE (params);
2115           params = TREE_CHAIN (params);
2116           main_wanted_type.wanted_type = wanted_type;
2117           main_wanted_type.wanted_type_name = wanted_type_name;
2118           main_wanted_type.pointer_count = fci->pointer_count + aflag;
2119           main_wanted_type.char_lenient_flag = 0;
2120           if (strchr (fci->flags2, 'c') != 0)
2121             main_wanted_type.char_lenient_flag = 1;
2122           main_wanted_type.writing_in_flag = 0;
2123           main_wanted_type.reading_from_flag = 0;
2124           if (aflag)
2125             main_wanted_type.writing_in_flag = 1;
2126           else
2127             {
2128               if (strchr (fci->flags2, 'W') != 0)
2129                 main_wanted_type.writing_in_flag = 1;
2130               if (strchr (fci->flags2, 'R') != 0)
2131                 main_wanted_type.reading_from_flag = 1;
2132             }
2133           main_wanted_type.name = NULL;
2134           main_wanted_type.param = cur_param;
2135           main_wanted_type.arg_num = arg_num;
2136           main_wanted_type.next = NULL;
2137           if (last_wanted_type != 0)
2138             last_wanted_type->next = &main_wanted_type;
2139           if (first_wanted_type == 0)
2140             first_wanted_type = &main_wanted_type;
2141           last_wanted_type = &main_wanted_type;
2142         }
2143
2144       if (first_wanted_type != 0)
2145         check_format_types (status, first_wanted_type);
2146
2147     }
2148 }
2149
2150
2151 /* Check the argument types from a single format conversion (possibly
2152    including width and precision arguments).  */
2153 static void
2154 check_format_types (status, types)
2155      int *status;
2156      format_wanted_type *types;
2157 {
2158   for (; types != 0; types = types->next)
2159     {
2160       tree cur_param;
2161       tree cur_type;
2162       tree orig_cur_type;
2163       tree wanted_type;
2164       int arg_num;
2165       int i;
2166       int char_type_flag;
2167       cur_param = types->param;
2168       cur_type = TREE_TYPE (cur_param);
2169       if (cur_type == error_mark_node)
2170         continue;
2171       char_type_flag = 0;
2172       wanted_type = types->wanted_type;
2173       arg_num = types->arg_num;
2174
2175       /* The following should not occur here.  */
2176       if (wanted_type == 0)
2177         abort ();
2178       if (wanted_type == void_type_node && types->pointer_count == 0)
2179         abort ();
2180
2181       if (types->pointer_count == 0)
2182         wanted_type = (*lang_hooks.types.type_promotes_to) (wanted_type);
2183
2184       STRIP_NOPS (cur_param);
2185
2186       /* Check the types of any additional pointer arguments
2187          that precede the "real" argument.  */
2188       for (i = 0; i < types->pointer_count; ++i)
2189         {
2190           if (TREE_CODE (cur_type) == POINTER_TYPE)
2191             {
2192               cur_type = TREE_TYPE (cur_type);
2193               if (cur_type == error_mark_node)
2194                 break;
2195
2196               /* Check for writing through a NULL pointer.  */
2197               if (types->writing_in_flag
2198                   && i == 0
2199                   && cur_param != 0
2200                   && integer_zerop (cur_param))
2201                 status_warning (status,
2202                                 "writing through null pointer (arg %d)",
2203                                 arg_num);
2204
2205               /* Check for reading through a NULL pointer.  */
2206               if (types->reading_from_flag
2207                   && i == 0
2208                   && cur_param != 0
2209                   && integer_zerop (cur_param))
2210                 status_warning (status,
2211                                 "reading through null pointer (arg %d)",
2212                                 arg_num);
2213
2214               if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
2215                 cur_param = TREE_OPERAND (cur_param, 0);
2216               else
2217                 cur_param = 0;
2218
2219               /* See if this is an attempt to write into a const type with
2220                  scanf or with printf "%n".  Note: the writing in happens
2221                  at the first indirection only, if for example
2222                  void * const * is passed to scanf %p; passing
2223                  const void ** is simply passing an incompatible type.  */
2224               if (types->writing_in_flag
2225                   && i == 0
2226                   && (TYPE_READONLY (cur_type)
2227                       || (cur_param != 0
2228                           && (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'c'
2229                               || (DECL_P (cur_param)
2230                                   && TREE_READONLY (cur_param))))))
2231                 status_warning (status, "writing into constant object (arg %d)", arg_num);
2232
2233               /* If there are extra type qualifiers beyond the first
2234                  indirection, then this makes the types technically
2235                  incompatible.  */
2236               if (i > 0
2237                   && pedantic
2238                   && (TYPE_READONLY (cur_type)
2239                       || TYPE_VOLATILE (cur_type)
2240                       || TYPE_RESTRICT (cur_type)))
2241                 status_warning (status, "extra type qualifiers in format argument (arg %d)",
2242                          arg_num);
2243
2244             }
2245           else
2246             {
2247               if (types->pointer_count == 1)
2248                 status_warning (status, "format argument is not a pointer (arg %d)", arg_num);
2249               else
2250                 status_warning (status, "format argument is not a pointer to a pointer (arg %d)", arg_num);
2251               break;
2252             }
2253         }
2254
2255       if (i < types->pointer_count)
2256         continue;
2257
2258       orig_cur_type = cur_type;
2259       cur_type = TYPE_MAIN_VARIANT (cur_type);
2260
2261       /* Check whether the argument type is a character type.  This leniency
2262          only applies to certain formats, flagged with 'c'.
2263       */
2264       if (types->char_lenient_flag)
2265         char_type_flag = (cur_type == char_type_node
2266                           || cur_type == signed_char_type_node
2267                           || cur_type == unsigned_char_type_node);
2268
2269       /* Check the type of the "real" argument, if there's a type we want.  */
2270       if (wanted_type == cur_type)
2271         continue;
2272       /* If we want `void *', allow any pointer type.
2273          (Anything else would already have got a warning.)
2274          With -pedantic, only allow pointers to void and to character
2275          types.  */
2276       if (wanted_type == void_type_node
2277           && (!pedantic || (i == 1 && char_type_flag)))
2278         continue;
2279       /* Don't warn about differences merely in signedness, unless
2280          -pedantic.  With -pedantic, warn if the type is a pointer
2281          target and not a character type, and for character types at
2282          a second level of indirection.  */
2283       if (TREE_CODE (wanted_type) == INTEGER_TYPE
2284           && TREE_CODE (cur_type) == INTEGER_TYPE
2285           && (! pedantic || i == 0 || (i == 1 && char_type_flag))
2286           && (TREE_UNSIGNED (wanted_type)
2287               ? wanted_type == c_common_unsigned_type (cur_type)
2288               : wanted_type == c_common_signed_type (cur_type)))
2289         continue;
2290       /* Likewise, "signed char", "unsigned char" and "char" are
2291          equivalent but the above test won't consider them equivalent.  */
2292       if (wanted_type == char_type_node
2293           && (! pedantic || i < 2)
2294           && char_type_flag)
2295         continue;
2296       /* Now we have a type mismatch.  */
2297       {
2298         const char *this;
2299         const char *that;
2300
2301         this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
2302         that = 0;
2303         if (TYPE_NAME (orig_cur_type) != 0
2304             && TREE_CODE (orig_cur_type) != INTEGER_TYPE
2305             && !(TREE_CODE (orig_cur_type) == POINTER_TYPE
2306                  && TREE_CODE (TREE_TYPE (orig_cur_type)) == INTEGER_TYPE))
2307           {
2308             if (TREE_CODE (TYPE_NAME (orig_cur_type)) == TYPE_DECL
2309                 && DECL_NAME (TYPE_NAME (orig_cur_type)) != 0)
2310               that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (orig_cur_type)));
2311             else
2312               that = IDENTIFIER_POINTER (TYPE_NAME (orig_cur_type));
2313           }
2314
2315         /* A nameless type can't possibly match what the format wants.
2316            So there will be a warning for it.
2317            Make up a string to describe vaguely what it is.  */
2318         if (that == 0)
2319           {
2320             if (TREE_CODE (orig_cur_type) == POINTER_TYPE)
2321               that = _("pointer");
2322             else
2323               that = _("different type");
2324           }
2325
2326         /* Make the warning better in case of mismatch of int vs long.  */
2327         if (TREE_CODE (orig_cur_type) == INTEGER_TYPE
2328             && TREE_CODE (wanted_type) == INTEGER_TYPE
2329             && TYPE_PRECISION (orig_cur_type) == TYPE_PRECISION (wanted_type)
2330             && TYPE_NAME (orig_cur_type) != 0
2331             && TREE_CODE (TYPE_NAME (orig_cur_type)) == TYPE_DECL)
2332           that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (orig_cur_type)));
2333
2334         if (strcmp (this, that) != 0)
2335           {
2336             /* There may be a better name for the format, e.g. size_t,
2337                but we should allow for programs with a perverse typedef
2338                making size_t something other than what the compiler
2339                thinks.  */
2340             if (types->wanted_type_name != 0
2341                 && strcmp (types->wanted_type_name, that) != 0)
2342               this = types->wanted_type_name;
2343             if (types->name != 0)
2344               status_warning (status, "%s is not type %s (arg %d)", types->name, this,
2345                        arg_num);
2346             else
2347               status_warning (status, "%s format, %s arg (arg %d)", this, that, arg_num);
2348           }
2349       }
2350     }
2351 }
2352
2353 /* Handle a "format" attribute; arguments as in
2354    struct attribute_spec.handler.  */
2355 tree
2356 handle_format_attribute (node, name, args, flags, no_add_attrs)
2357      tree *node;
2358      tree name ATTRIBUTE_UNUSED;
2359      tree args;
2360      int flags;
2361      bool *no_add_attrs;
2362 {
2363   tree type = *node;
2364   function_format_info info;
2365   tree argument;
2366   unsigned HOST_WIDE_INT arg_num;
2367
2368   if (!decode_format_attr (args, &info, 0))
2369     {
2370       *no_add_attrs = true;
2371       return NULL_TREE;
2372     }
2373
2374   /* If a parameter list is specified, verify that the format_num
2375      argument is actually a string, in case the format attribute
2376      is in error.  */
2377   argument = TYPE_ARG_TYPES (type);
2378   if (argument)
2379     {
2380       for (arg_num = 1; argument != 0 && arg_num != info.format_num;
2381            ++arg_num, argument = TREE_CHAIN (argument))
2382         ;
2383
2384       if (! argument
2385           || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
2386           || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
2387               != char_type_node))
2388         {
2389           if (!(flags & (int) ATTR_FLAG_BUILT_IN))
2390             error ("format string arg not a string type");
2391           *no_add_attrs = true;
2392           return NULL_TREE;
2393         }
2394
2395       else if (info.first_arg_num != 0)
2396         {
2397           /* Verify that first_arg_num points to the last arg,
2398              the ...  */
2399           while (argument)
2400             arg_num++, argument = TREE_CHAIN (argument);
2401
2402           if (arg_num != info.first_arg_num)
2403             {
2404               if (!(flags & (int) ATTR_FLAG_BUILT_IN))
2405                 error ("args to be formatted is not '...'");
2406               *no_add_attrs = true;
2407               return NULL_TREE;
2408             }
2409         }
2410     }
2411
2412   if (info.format_type == strftime_format_type && info.first_arg_num != 0)
2413     {
2414       error ("strftime formats cannot format arguments");
2415       *no_add_attrs = true;
2416       return NULL_TREE;
2417     }
2418
2419   /* If this is format type __asm_fprintf__, we have to initialize
2420      GCC's notion of HOST_WIDE_INT for checking %wd.  */
2421   if (info.format_type == asm_fprintf_format_type)
2422     {
2423       static tree hwi;
2424       tree orig;
2425       
2426       /* For this custom check to work, one must have issued:
2427          "typedef HOST_WIDE_INT __gcc_host_wide_int__;"
2428          in your source code prior to using this attribute.  */
2429       if (!hwi)
2430         {
2431           format_kind_info *new_format_types;
2432           format_length_info *new_asm_fprintf_length_specs;
2433           
2434           if (!(hwi = maybe_get_identifier ("__gcc_host_wide_int__")))
2435             abort ();
2436
2437           /* Create a new (writable) copy of asm_fprintf_length_specs.  */
2438           new_asm_fprintf_length_specs =
2439             xmalloc (sizeof (asm_fprintf_length_specs));
2440           memcpy (new_asm_fprintf_length_specs, asm_fprintf_length_specs,
2441                   sizeof (asm_fprintf_length_specs));
2442
2443           /* Create a new (writable) copy of format_types.  */
2444           new_format_types = xmalloc (sizeof (format_types_orig));
2445           memcpy (new_format_types, format_types_orig, sizeof (format_types_orig));
2446           
2447           /* Find the underlying type for HOST_WIDE_INT.  */
2448           orig = DECL_ORIGINAL_TYPE (identifier_global_value (hwi));
2449           if (orig == long_integer_type_node)
2450             new_asm_fprintf_length_specs[1].index = FMT_LEN_l;
2451           else if (orig == long_long_integer_type_node)
2452             new_asm_fprintf_length_specs[1].index = FMT_LEN_ll;
2453           else
2454             abort ();
2455
2456           /* Assign the new data for use.  */
2457           new_format_types[asm_fprintf_format_type].length_char_specs =
2458             new_asm_fprintf_length_specs;
2459           format_types = new_format_types;
2460         }
2461     }
2462
2463   return NULL_TREE;
2464 }