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