OSDN Git Service

* Fix for g++/15861
[pf3gnuchains/gcc-fork.git] / gcc / c-format.c
1 /* Check calls to formatted I/O functions (-Wformat).
2    Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3    2001, 2002, 2003, 2004 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 = 0;
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 ("%qs 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 should have
492      type 'int'", the name to use (in this case "field precision"),
493      otherwise NULL, for "format expects type 'long'" type
494      messages.  */
495   const char *name;
496   /* The actual parameter to check against the wanted type.  */
497   tree param;
498   /* The argument number of that parameter.  */
499   int arg_num;
500   /* The next type to check for this format conversion, or NULL if none.  */
501   struct format_wanted_type *next;
502 } format_wanted_type;
503
504
505 static const format_length_info printf_length_specs[] =
506 {
507   { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
508   { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L },
509   { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
510   { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
511   { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
512   { "Z", FMT_LEN_z, STD_EXT, NULL, 0, 0 },
513   { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
514   { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
515   { NULL, 0, 0, NULL, 0, 0 }
516 };
517
518 /* Length specifiers valid for asm_fprintf.  */
519 static const format_length_info asm_fprintf_length_specs[] =
520 {
521   { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89 },
522   { "w", FMT_LEN_none, STD_C89, NULL, 0, 0 },
523   { NULL, 0, 0, NULL, 0, 0 }
524 };
525
526 /* Length specifiers valid for GCC diagnostics.  */
527 static const format_length_info gcc_diag_length_specs[] =
528 {
529   { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89 },
530   { "w", FMT_LEN_none, STD_C89, NULL, 0, 0 },
531   { NULL, 0, 0, NULL, 0, 0 }
532 };
533
534 /* The custom diagnostics all accept the same length specifiers.  */
535 #define gcc_cdiag_length_specs gcc_diag_length_specs
536 #define gcc_cxxdiag_length_specs gcc_diag_length_specs
537
538 /* This differs from printf_length_specs only in that "Z" is not accepted.  */
539 static const format_length_info scanf_length_specs[] =
540 {
541   { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
542   { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L },
543   { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
544   { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
545   { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
546   { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
547   { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
548   { NULL, 0, 0, NULL, 0, 0 }
549 };
550
551
552 /* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings
553    make no sense for a format type not part of any C standard version.  */
554 static const format_length_info strfmon_length_specs[] =
555 {
556   /* A GNU extension.  */
557   { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
558   { NULL, 0, 0, NULL, 0, 0 }
559 };
560
561 static const format_flag_spec printf_flag_specs[] =
562 {
563   { ' ',  0, 0, N_("` ' flag"),        N_("the ` ' printf flag"),              STD_C89 },
564   { '+',  0, 0, N_("`+' flag"),        N_("the `+' printf flag"),              STD_C89 },
565   { '#',  0, 0, N_("`#' flag"),        N_("the `#' printf flag"),              STD_C89 },
566   { '0',  0, 0, N_("`0' flag"),        N_("the `0' printf flag"),              STD_C89 },
567   { '-',  0, 0, N_("`-' flag"),        N_("the `-' printf flag"),              STD_C89 },
568   { '\'', 0, 0, N_("`'' flag"),        N_("the `'' printf flag"),              STD_EXT },
569   { 'I',  0, 0, N_("`I' flag"),        N_("the `I' printf flag"),              STD_EXT },
570   { 'w',  0, 0, N_("field width"),     N_("field width in printf format"),     STD_C89 },
571   { 'p',  0, 0, N_("precision"),       N_("precision in printf format"),       STD_C89 },
572   { 'L',  0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
573   { 0, 0, 0, NULL, NULL, 0 }
574 };
575
576
577 static const format_flag_pair printf_flag_pairs[] =
578 {
579   { ' ', '+', 1, 0   },
580   { '0', '-', 1, 0   },
581   { '0', 'p', 1, 'i' },
582   { 0, 0, 0, 0 }
583 };
584
585 static const format_flag_spec asm_fprintf_flag_specs[] =
586 {
587   { ' ',  0, 0, N_("` ' flag"),        N_("the ` ' printf flag"),              STD_C89 },
588   { '+',  0, 0, N_("`+' flag"),        N_("the `+' printf flag"),              STD_C89 },
589   { '#',  0, 0, N_("`#' flag"),        N_("the `#' printf flag"),              STD_C89 },
590   { '0',  0, 0, N_("`0' flag"),        N_("the `0' printf flag"),              STD_C89 },
591   { '-',  0, 0, N_("`-' flag"),        N_("the `-' printf flag"),              STD_C89 },
592   { 'w',  0, 0, N_("field width"),     N_("field width in printf format"),     STD_C89 },
593   { 'p',  0, 0, N_("precision"),       N_("precision in printf format"),       STD_C89 },
594   { 'L',  0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
595   { 0, 0, 0, NULL, NULL, 0 }
596 };
597
598 static const format_flag_pair asm_fprintf_flag_pairs[] =
599 {
600   { ' ', '+', 1, 0   },
601   { '0', '-', 1, 0   },
602   { '0', 'p', 1, 'i' },
603   { 0, 0, 0, 0 }
604 };
605
606 static const format_flag_pair gcc_diag_flag_pairs[] =
607 {
608   { 0, 0, 0, 0 }
609 };
610
611 #define gcc_cdiag_flag_pairs gcc_diag_flag_pairs
612 #define gcc_cxxdiag_flag_pairs gcc_diag_flag_pairs
613
614 static const format_flag_spec gcc_diag_flag_specs[] =
615 {
616   { 'q',  0, 0, N_("`q' flag"),        N_("the `q' diagnostic flag"),          STD_C89 },
617   { 'p',  0, 0, N_("precision"),       N_("precision in printf format"),       STD_C89 },
618   { 'L',  0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
619   { 0, 0, 0, NULL, NULL, 0 }
620 };
621
622 #define gcc_cdiag_flag_specs gcc_diag_flag_specs
623
624 static const format_flag_spec gcc_cxxdiag_flag_specs[] =
625 {
626   { '+',  0, 0, N_("`+' flag"),        N_("the `+' printf flag"),              STD_C89 },
627   { '#',  0, 0, N_("`#' flag"),        N_("the `#' printf flag"),              STD_C89 },
628   { 'q',  0, 0, N_("`q' flag"),        N_("the `q' diagnostic 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  }, "q",  ""   },
802   { "ox",  0, STD_C89, { T89_UI,  BADLEN,  BADLEN,  T89_UL,  T9L_ULL, BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  ""   },
803   { "u",   0, STD_C89, { T89_UI,  BADLEN,  BADLEN,  T89_UL,  T9L_ULL, BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  ""   },
804   { "c",   0, STD_C89, { T89_I,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  ""   },
805   { "s",   1, STD_C89, { T89_C,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "pq", "cR" },
806   { "p",   1, STD_C89, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "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  }, "q",  ""   },
812
813   /* These will require a "tree" at runtime.  */
814   { "J", 0, STD_C89, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",    ""   },
815
816   { "<>'", 0, STD_C89, NOARGUMENTS, "",      ""   },
817   { "m",   0, STD_C89, NOARGUMENTS, "q",     ""   },
818   { NULL,  0, 0, NOLENGTHS, NULL, NULL }
819 };
820
821 static const format_char_info gcc_cdiag_char_table[] =
822 {
823   /* C89 conversion specifiers.  */
824   { "di",  0, STD_C89, { T89_I,   BADLEN,  BADLEN,  T89_L,   T9L_LL,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  ""   },
825   { "ox",  0, STD_C89, { T89_UI,  BADLEN,  BADLEN,  T89_UL,  T9L_ULL, BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  ""   },
826   { "u",   0, STD_C89, { T89_UI,  BADLEN,  BADLEN,  T89_UL,  T9L_ULL, BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  ""   },
827   { "c",   0, STD_C89, { T89_I,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  ""   },
828   { "s",   1, STD_C89, { T89_C,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "pq", "cR" },
829   { "p",   1, STD_C89, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "c"  },
830
831   /* Custom conversion specifiers.  */
832
833   /* %H will require "location_t" at runtime.  */
834   { "H",   0, STD_C89, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  ""   },
835
836   /* These will require a "tree" at runtime.  */
837   { "DEFJT", 0, STD_C89, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q", ""   },
838
839   { "<>'", 0, STD_C89, NOARGUMENTS, "",      ""   },
840   { "m",   0, STD_C89, NOARGUMENTS, "q",     ""   },
841   { NULL,  0, 0, NOLENGTHS, NULL, NULL }
842 };
843
844 static const format_char_info gcc_cxxdiag_char_table[] =
845 {
846   /* C89 conversion specifiers.  */
847   { "di",  0, STD_C89, { T89_I,   BADLEN,  BADLEN,  T89_L,   T9L_LL,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  ""   },
848   { "ox",  0, STD_C89, { T89_UI,  BADLEN,  BADLEN,  T89_UL,  T9L_ULL, BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  ""   },
849   { "u",   0, STD_C89, { T89_UI,  BADLEN,  BADLEN,  T89_UL,  T9L_ULL, BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  ""   },
850   { "c",   0, STD_C89, { T89_I,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  ""   },
851   { "s",   1, STD_C89, { T89_C,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "pq", "cR" },
852   { "p",   1, STD_C89, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "c"  },
853
854   /* Custom conversion specifiers.  */
855
856   /* %H will require "location_t" at runtime.  */
857   { "H",   0, STD_C89, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  ""   },
858
859   /* These will require a "tree" at runtime.  */
860   { "ADEFJTV",0,STD_C89,{ T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q+#",   ""   },
861
862   /* These accept either an `int' or an `enum tree_code' (which is handled as an `int'.)  */
863   { "CLOPQ",0,STD_C89, { T89_I,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  ""   },
864
865   { "<>'", 0, STD_C89, NOARGUMENTS, "",      ""   },
866   { "m",   0, STD_C89, NOARGUMENTS, "q",     ""   },
867   { NULL,  0, 0, NOLENGTHS, NULL, NULL }
868 };
869
870 static const format_char_info scan_char_table[] =
871 {
872   /* C89 conversion specifiers.  */
873   { "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"   },
874   { "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"   },
875   { "oxX",   1, STD_C89, { T89_UI,  T99_UC,  T89_US,  T89_UL,  T9L_ULL, TEX_ULL, T99_ST,  T99_UPD, T99_UIM }, "*w",   "W"   },
876   { "efgEG", 1, STD_C89, { T89_F,   BADLEN,  BADLEN,  T89_D,   BADLEN,  T89_LD,  BADLEN,  BADLEN,  BADLEN  }, "*w'",  "W"   },
877   { "c",     1, STD_C89, { T89_C,   BADLEN,  BADLEN,  T94_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "*w",   "cW"  },
878   { "s",     1, STD_C89, { T89_C,   BADLEN,  BADLEN,  T94_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "*aw",  "cW"  },
879   { "[",     1, STD_C89, { T89_C,   BADLEN,  BADLEN,  T94_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "*aw",  "cW[" },
880   { "p",     2, STD_C89, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "*w",   "W"   },
881   { "n",     1, STD_C89, { T89_I,   T99_SC,  T89_S,   T89_L,   T9L_LL,  BADLEN,  T99_SST, T99_PD,  T99_IM  }, "",     "W"   },
882   /* C99 conversion specifiers.  */
883   { "FaA",   1, STD_C99, { T99_F,   BADLEN,  BADLEN,  T99_D,   BADLEN,  T99_LD,  BADLEN,  BADLEN,  BADLEN  }, "*w'",  "W"   },
884   /* X/Open conversion specifiers.  */
885   { "C",     1, STD_EXT, { TEX_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "*w",   "W"   },
886   { "S",     1, STD_EXT, { TEX_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "*aw",  "W"   },
887   { NULL, 0, 0, NOLENGTHS, NULL, NULL }
888 };
889
890 static const format_char_info time_char_table[] =
891 {
892   /* C89 conversion specifiers.  */
893   { "ABZab",            0, STD_C89, NOLENGTHS, "^#",     ""   },
894   { "cx",               0, STD_C89, NOLENGTHS, "E",      "3"  },
895   { "HIMSUWdmw",        0, STD_C89, NOLENGTHS, "-_0Ow",  ""   },
896   { "j",                0, STD_C89, NOLENGTHS, "-_0Ow",  "o"  },
897   { "p",                0, STD_C89, NOLENGTHS, "#",      ""   },
898   { "X",                0, STD_C89, NOLENGTHS, "E",      ""   },
899   { "y",                0, STD_C89, NOLENGTHS, "EO-_0w", "4"  },
900   { "Y",                0, STD_C89, NOLENGTHS, "-_0EOw", "o"  },
901   { "%",                0, STD_C89, NOLENGTHS, "",       ""   },
902   /* C99 conversion specifiers.  */
903   { "C",                0, STD_C99, NOLENGTHS, "-_0EOw", "o"  },
904   { "D",                0, STD_C99, NOLENGTHS, "",       "2"  },
905   { "eVu",              0, STD_C99, NOLENGTHS, "-_0Ow",  ""   },
906   { "FRTnrt",           0, STD_C99, NOLENGTHS, "",       ""   },
907   { "g",                0, STD_C99, NOLENGTHS, "O-_0w",  "2o" },
908   { "G",                0, STD_C99, NOLENGTHS, "-_0Ow",  "o"  },
909   { "h",                0, STD_C99, NOLENGTHS, "^#",     ""   },
910   { "z",                0, STD_C99, NOLENGTHS, "O",      "o"  },
911   /* GNU conversion specifiers.  */
912   { "kls",              0, STD_EXT, NOLENGTHS, "-_0Ow",  ""   },
913   { "P",                0, STD_EXT, NOLENGTHS, "",       ""   },
914   { NULL,               0, 0, NOLENGTHS, NULL, NULL }
915 };
916
917 static const format_char_info monetary_char_table[] =
918 {
919   { "in", 0, STD_C89, { T89_D, BADLEN, BADLEN, BADLEN, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "=^+(!-w#p", "" },
920   { NULL, 0, 0, NOLENGTHS, NULL, NULL }
921 };
922
923
924 /* This must be in the same order as enum format_type.  */
925 static const format_kind_info format_types_orig[] =
926 {
927   { "printf",   printf_length_specs,  print_char_table, " +#0-'I", NULL, 
928     printf_flag_specs, printf_flag_pairs,
929     FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_EMPTY_PREC_OK,
930     'w', 0, 'p', 0, 'L',
931     &integer_type_node, &integer_type_node
932   },
933   { "asm_fprintf",   asm_fprintf_length_specs,  asm_fprintf_char_table, " +#0-", NULL, 
934     asm_fprintf_flag_specs, asm_fprintf_flag_pairs,
935     FMT_FLAG_ARG_CONVERT|FMT_FLAG_EMPTY_PREC_OK,
936     'w', 0, 'p', 0, 'L',
937     NULL, NULL
938   },
939   { "gcc_diag",   gcc_diag_length_specs,  gcc_diag_char_table, "q", NULL, 
940     gcc_diag_flag_specs, gcc_diag_flag_pairs,
941     FMT_FLAG_ARG_CONVERT,
942     0, 0, 'p', 0, 'L',
943     NULL, &integer_type_node
944   },
945   { "gcc_cdiag",   gcc_cdiag_length_specs,  gcc_cdiag_char_table, "q", NULL, 
946     gcc_cdiag_flag_specs, gcc_cdiag_flag_pairs,
947     FMT_FLAG_ARG_CONVERT,
948     0, 0, 'p', 0, 'L',
949     NULL, &integer_type_node
950   },
951   { "gcc_cxxdiag",   gcc_cxxdiag_length_specs,  gcc_cxxdiag_char_table, "q+#", NULL, 
952     gcc_cxxdiag_flag_specs, gcc_cxxdiag_flag_pairs,
953     FMT_FLAG_ARG_CONVERT,
954     0, 0, 'p', 0, 'L',
955     NULL, &integer_type_node
956   },
957   { "scanf",    scanf_length_specs,   scan_char_table,  "*'I", NULL, 
958     scanf_flag_specs, scanf_flag_pairs,
959     FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_ZERO_WIDTH_BAD|FMT_FLAG_DOLLAR_GAP_POINTER_OK,
960     'w', 0, 0, '*', 'L',
961     NULL, NULL
962   },
963   { "strftime", NULL,                 time_char_table,  "_-0^#", "EO",
964     strftime_flag_specs, strftime_flag_pairs,
965     FMT_FLAG_FANCY_PERCENT_OK, 'w', 0, 0, 0, 0,
966     NULL, NULL
967   },
968   { "strfmon",  strfmon_length_specs, monetary_char_table, "=^+(!-", NULL, 
969     strfmon_flag_specs, strfmon_flag_pairs,
970     FMT_FLAG_ARG_CONVERT, 'w', '#', 'p', 0, 'L',
971     NULL, NULL
972   }
973 };
974
975 /* This layer of indirection allows GCC to reassign format_types with
976    new data if necessary, while still allowing the original data to be
977    const.  */
978 static const format_kind_info *format_types = format_types_orig;
979 /* We can modify this one.  */
980 static format_kind_info *dynamic_format_types;
981
982 /* Structure detailing the results of checking a format function call
983    where the format expression may be a conditional expression with
984    many leaves resulting from nested conditional expressions.  */
985 typedef struct
986 {
987   /* Number of leaves of the format argument that could not be checked
988      as they were not string literals.  */
989   int number_non_literal;
990   /* Number of leaves of the format argument that were null pointers or
991      string literals, but had extra format arguments.  */
992   int number_extra_args;
993   /* Number of leaves of the format argument that were null pointers or
994      string literals, but had extra format arguments and used $ operand
995      numbers.  */
996   int number_dollar_extra_args;
997   /* Number of leaves of the format argument that were wide string
998      literals.  */
999   int number_wide;
1000   /* Number of leaves of the format argument that were empty strings.  */
1001   int number_empty;
1002   /* Number of leaves of the format argument that were unterminated
1003      strings.  */
1004   int number_unterminated;
1005   /* Number of leaves of the format argument that were not counted above.  */
1006   int number_other;
1007 } format_check_results;
1008
1009 typedef struct
1010 {
1011   format_check_results *res;
1012   function_format_info *info;
1013   tree params;
1014 } format_check_context;
1015
1016 static void check_format_info (function_format_info *, tree);
1017 static void check_format_arg (void *, tree, unsigned HOST_WIDE_INT);
1018 static void check_format_info_main (format_check_results *,
1019                                     function_format_info *,
1020                                     const char *, int, tree,
1021                                     unsigned HOST_WIDE_INT);
1022
1023 static void init_dollar_format_checking (int, tree);
1024 static int maybe_read_dollar_number (const char **, int,
1025                                      tree, tree *, const format_kind_info *);
1026 static bool avoid_dollar_number (const char *);
1027 static void finish_dollar_format_checking (format_check_results *, int);
1028
1029 static const format_flag_spec *get_flag_spec (const format_flag_spec *,
1030                                               int, const char *);
1031
1032 static void check_format_types (format_wanted_type *, const char *, int);
1033 static void format_type_warning (const char *, const char *, int, tree,
1034                                  int, const char *, tree, int);
1035
1036 /* Decode a format type from a string, returning the type, or
1037    format_type_error if not valid, in which case the caller should print an
1038    error message.  */
1039 static enum format_type
1040 decode_format_type (const char *s)
1041 {
1042   int i;
1043   int slen;
1044   slen = strlen (s);
1045   for (i = 0; i < (int) format_type_error; i++)
1046     {
1047       int alen;
1048       if (!strcmp (s, format_types[i].name))
1049         break;
1050       alen = strlen (format_types[i].name);
1051       if (slen == alen + 4 && s[0] == '_' && s[1] == '_'
1052           && s[slen - 1] == '_' && s[slen - 2] == '_'
1053           && !strncmp (s + 2, format_types[i].name, alen))
1054         break;
1055     }
1056   return ((enum format_type) i);
1057 }
1058
1059 \f
1060 /* Check the argument list of a call to printf, scanf, etc.
1061    ATTRS are the attributes on the function type.
1062    PARAMS is the list of argument values.  Also, if -Wmissing-format-attribute,
1063    warn for calls to vprintf or vscanf in functions with no such format
1064    attribute themselves.  */
1065
1066 void
1067 check_function_format (tree attrs, tree params)
1068 {
1069   tree a;
1070
1071   /* See if this function has any format attributes.  */
1072   for (a = attrs; a; a = TREE_CHAIN (a))
1073     {
1074       if (is_attribute_p ("format", TREE_PURPOSE (a)))
1075         {
1076           /* Yup; check it.  */
1077           function_format_info info;
1078           decode_format_attr (TREE_VALUE (a), &info, 1);
1079           check_format_info (&info, params);
1080           if (warn_missing_format_attribute && info.first_arg_num == 0
1081               && (format_types[info.format_type].flags
1082                   & (int) FMT_FLAG_ARG_CONVERT))
1083             {
1084               tree c;
1085               for (c = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
1086                    c;
1087                    c = TREE_CHAIN (c))
1088                 if (is_attribute_p ("format", TREE_PURPOSE (c))
1089                     && (decode_format_type (IDENTIFIER_POINTER
1090                                             (TREE_VALUE (TREE_VALUE (c))))
1091                         == info.format_type))
1092                   break;
1093               if (c == NULL_TREE)
1094                 {
1095                   /* Check if the current function has a parameter to which
1096                      the format attribute could be attached; if not, it
1097                      can't be a candidate for a format attribute, despite
1098                      the vprintf-like or vscanf-like call.  */
1099                   tree args;
1100                   for (args = DECL_ARGUMENTS (current_function_decl);
1101                        args != 0;
1102                        args = TREE_CHAIN (args))
1103                     {
1104                       if (TREE_CODE (TREE_TYPE (args)) == POINTER_TYPE
1105                           && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args)))
1106                               == char_type_node))
1107                         break;
1108                     }
1109                   if (args != 0)
1110                     warning ("function might be possible candidate for %qs format attribute",
1111                              format_types[info.format_type].name);
1112                 }
1113             }
1114         }
1115     }
1116 }
1117
1118
1119 /* Variables used by the checking of $ operand number formats.  */
1120 static char *dollar_arguments_used = NULL;
1121 static char *dollar_arguments_pointer_p = NULL;
1122 static int dollar_arguments_alloc = 0;
1123 static int dollar_arguments_count;
1124 static int dollar_first_arg_num;
1125 static int dollar_max_arg_used;
1126 static int dollar_format_warned;
1127
1128 /* Initialize the checking for a format string that may contain $
1129    parameter number specifications; we will need to keep track of whether
1130    each parameter has been used.  FIRST_ARG_NUM is the number of the first
1131    argument that is a parameter to the format, or 0 for a vprintf-style
1132    function; PARAMS is the list of arguments starting at this argument.  */
1133
1134 static void
1135 init_dollar_format_checking (int first_arg_num, tree params)
1136 {
1137   tree oparams = params;
1138
1139   dollar_first_arg_num = first_arg_num;
1140   dollar_arguments_count = 0;
1141   dollar_max_arg_used = 0;
1142   dollar_format_warned = 0;
1143   if (first_arg_num > 0)
1144     {
1145       while (params)
1146         {
1147           dollar_arguments_count++;
1148           params = TREE_CHAIN (params);
1149         }
1150     }
1151   if (dollar_arguments_alloc < dollar_arguments_count)
1152     {
1153       if (dollar_arguments_used)
1154         free (dollar_arguments_used);
1155       if (dollar_arguments_pointer_p)
1156         free (dollar_arguments_pointer_p);
1157       dollar_arguments_alloc = dollar_arguments_count;
1158       dollar_arguments_used = xmalloc (dollar_arguments_alloc);
1159       dollar_arguments_pointer_p = xmalloc (dollar_arguments_alloc);
1160     }
1161   if (dollar_arguments_alloc)
1162     {
1163       memset (dollar_arguments_used, 0, dollar_arguments_alloc);
1164       if (first_arg_num > 0)
1165         {
1166           int i = 0;
1167           params = oparams;
1168           while (params)
1169             {
1170               dollar_arguments_pointer_p[i] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params)))
1171                                                == POINTER_TYPE);
1172               params = TREE_CHAIN (params);
1173               i++;
1174             }
1175         }
1176     }
1177 }
1178
1179
1180 /* Look for a decimal number followed by a $ in *FORMAT.  If DOLLAR_NEEDED
1181    is set, it is an error if one is not found; otherwise, it is OK.  If
1182    such a number is found, check whether it is within range and mark that
1183    numbered operand as being used for later checking.  Returns the operand
1184    number if found and within range, zero if no such number was found and
1185    this is OK, or -1 on error.  PARAMS points to the first operand of the
1186    format; PARAM_PTR is made to point to the parameter referred to.  If
1187    a $ format is found, *FORMAT is updated to point just after it.  */
1188
1189 static int
1190 maybe_read_dollar_number (const char **format,
1191                           int dollar_needed, tree params, tree *param_ptr,
1192                           const format_kind_info *fki)
1193 {
1194   int argnum;
1195   int overflow_flag;
1196   const char *fcp = *format;
1197   if (! ISDIGIT (*fcp))
1198     {
1199       if (dollar_needed)
1200         {
1201           warning ("missing $ operand number in format");
1202           return -1;
1203         }
1204       else
1205         return 0;
1206     }
1207   argnum = 0;
1208   overflow_flag = 0;
1209   while (ISDIGIT (*fcp))
1210     {
1211       int nargnum;
1212       nargnum = 10 * argnum + (*fcp - '0');
1213       if (nargnum < 0 || nargnum / 10 != argnum)
1214         overflow_flag = 1;
1215       argnum = nargnum;
1216       fcp++;
1217     }
1218   if (*fcp != '$')
1219     {
1220       if (dollar_needed)
1221         {
1222           warning ("missing $ operand number in format");
1223           return -1;
1224         }
1225       else
1226         return 0;
1227     }
1228   *format = fcp + 1;
1229   if (pedantic && !dollar_format_warned)
1230     {
1231       warning ("%s does not support %%n$ operand number formats",
1232                C_STD_NAME (STD_EXT));
1233       dollar_format_warned = 1;
1234     }
1235   if (overflow_flag || argnum == 0
1236       || (dollar_first_arg_num && argnum > dollar_arguments_count))
1237     {
1238       warning ("operand number out of range in format");
1239       return -1;
1240     }
1241   if (argnum > dollar_max_arg_used)
1242     dollar_max_arg_used = argnum;
1243   /* For vprintf-style functions we may need to allocate more memory to
1244      track which arguments are used.  */
1245   while (dollar_arguments_alloc < dollar_max_arg_used)
1246     {
1247       int nalloc;
1248       nalloc = 2 * dollar_arguments_alloc + 16;
1249       dollar_arguments_used = xrealloc (dollar_arguments_used, nalloc);
1250       dollar_arguments_pointer_p = xrealloc (dollar_arguments_pointer_p,
1251                                              nalloc);
1252       memset (dollar_arguments_used + dollar_arguments_alloc, 0,
1253               nalloc - dollar_arguments_alloc);
1254       dollar_arguments_alloc = nalloc;
1255     }
1256   if (!(fki->flags & (int) FMT_FLAG_DOLLAR_MULTIPLE)
1257       && dollar_arguments_used[argnum - 1] == 1)
1258     {
1259       dollar_arguments_used[argnum - 1] = 2;
1260       warning ("format argument %d used more than once in %s format",
1261                argnum, fki->name);
1262     }
1263   else
1264     dollar_arguments_used[argnum - 1] = 1;
1265   if (dollar_first_arg_num)
1266     {
1267       int i;
1268       *param_ptr = params;
1269       for (i = 1; i < argnum && *param_ptr != 0; i++)
1270         *param_ptr = TREE_CHAIN (*param_ptr);
1271
1272       if (*param_ptr == 0)
1273         {
1274           /* This case shouldn't be caught here.  */
1275           abort ();
1276         }
1277     }
1278   else
1279     *param_ptr = 0;
1280   return argnum;
1281 }
1282
1283 /* Ensure that FORMAT does not start with a decimal number followed by
1284    a $; give a diagnostic and return true if it does, false otherwise.  */
1285
1286 static bool
1287 avoid_dollar_number (const char *format)
1288 {
1289   if (!ISDIGIT (*format))
1290     return false;
1291   while (ISDIGIT (*format))
1292     format++;
1293   if (*format == '$')
1294     {
1295       warning ("$ operand number used after format without operand number");
1296       return true;
1297     }
1298   return false;
1299 }
1300
1301
1302 /* Finish the checking for a format string that used $ operand number formats
1303    instead of non-$ formats.  We check for unused operands before used ones
1304    (a serious error, since the implementation of the format function
1305    can't know what types to pass to va_arg to find the later arguments).
1306    and for unused operands at the end of the format (if we know how many
1307    arguments the format had, so not for vprintf).  If there were operand
1308    numbers out of range on a non-vprintf-style format, we won't have reached
1309    here.  If POINTER_GAP_OK, unused arguments are OK if all arguments are
1310    pointers.  */
1311
1312 static void
1313 finish_dollar_format_checking (format_check_results *res, int pointer_gap_ok)
1314 {
1315   int i;
1316   bool found_pointer_gap = false;
1317   for (i = 0; i < dollar_max_arg_used; i++)
1318     {
1319       if (!dollar_arguments_used[i])
1320         {
1321           if (pointer_gap_ok && (dollar_first_arg_num == 0
1322                                  || dollar_arguments_pointer_p[i]))
1323             found_pointer_gap = true;
1324           else
1325             warning ("format argument %d unused before used argument %d in $-style format",
1326                      i + 1, dollar_max_arg_used);
1327         }
1328     }
1329   if (found_pointer_gap
1330       || (dollar_first_arg_num
1331           && dollar_max_arg_used < dollar_arguments_count))
1332     {
1333       res->number_other--;
1334       res->number_dollar_extra_args++;
1335     }
1336 }
1337
1338
1339 /* Retrieve the specification for a format flag.  SPEC contains the
1340    specifications for format flags for the applicable kind of format.
1341    FLAG is the flag in question.  If PREDICATES is NULL, the basic
1342    spec for that flag must be retrieved and this function aborts if
1343    it cannot be found.  If PREDICATES is not NULL, it is a string listing
1344    possible predicates for the spec entry; if an entry predicated on any
1345    of these is found, it is returned, otherwise NULL is returned.  */
1346
1347 static const format_flag_spec *
1348 get_flag_spec (const format_flag_spec *spec, int flag, const char *predicates)
1349 {
1350   int i;
1351   for (i = 0; spec[i].flag_char != 0; i++)
1352     {
1353       if (spec[i].flag_char != flag)
1354         continue;
1355       if (predicates != NULL)
1356         {
1357           if (spec[i].predicate != 0
1358               && strchr (predicates, spec[i].predicate) != 0)
1359             return &spec[i];
1360         }
1361       else if (spec[i].predicate == 0)
1362         return &spec[i];
1363     }
1364   if (predicates == NULL)
1365     abort ();
1366   else
1367     return NULL;
1368 }
1369
1370
1371 /* Check the argument list of a call to printf, scanf, etc.
1372    INFO points to the function_format_info structure.
1373    PARAMS is the list of argument values.  */
1374
1375 static void
1376 check_format_info (function_format_info *info, tree params)
1377 {
1378   format_check_context format_ctx;
1379   unsigned HOST_WIDE_INT arg_num;
1380   tree format_tree;
1381   format_check_results res;
1382   /* Skip to format argument.  If the argument isn't available, there's
1383      no work for us to do; prototype checking will catch the problem.  */
1384   for (arg_num = 1; ; ++arg_num)
1385     {
1386       if (params == 0)
1387         return;
1388       if (arg_num == info->format_num)
1389         break;
1390       params = TREE_CHAIN (params);
1391     }
1392   format_tree = TREE_VALUE (params);
1393   params = TREE_CHAIN (params);
1394   if (format_tree == 0)
1395     return;
1396
1397   res.number_non_literal = 0;
1398   res.number_extra_args = 0;
1399   res.number_dollar_extra_args = 0;
1400   res.number_wide = 0;
1401   res.number_empty = 0;
1402   res.number_unterminated = 0;
1403   res.number_other = 0;
1404
1405   format_ctx.res = &res;
1406   format_ctx.info = info;
1407   format_ctx.params = params;
1408
1409   check_function_arguments_recurse (check_format_arg, &format_ctx,
1410                                     format_tree, arg_num);
1411
1412   if (res.number_non_literal > 0)
1413     {
1414       /* Functions taking a va_list normally pass a non-literal format
1415          string.  These functions typically are declared with
1416          first_arg_num == 0, so avoid warning in those cases.  */
1417       if (!(format_types[info->format_type].flags & (int) FMT_FLAG_ARG_CONVERT))
1418         {
1419           /* For strftime-like formats, warn for not checking the format
1420              string; but there are no arguments to check.  */
1421           if (warn_format_nonliteral)
1422             warning ("format not a string literal, format string not checked");
1423         }
1424       else if (info->first_arg_num != 0)
1425         {
1426           /* If there are no arguments for the format at all, we may have
1427              printf (foo) which is likely to be a security hole.  */
1428           while (arg_num + 1 < info->first_arg_num)
1429             {
1430               if (params == 0)
1431                 break;
1432               params = TREE_CHAIN (params);
1433               ++arg_num;
1434             }
1435           if (params == 0 && (warn_format_nonliteral || warn_format_security))
1436             warning ("format not a string literal and no format arguments");
1437           else if (warn_format_nonliteral)
1438             warning ("format not a string literal, argument types not checked");
1439         }
1440     }
1441
1442   /* If there were extra arguments to the format, normally warn.  However,
1443      the standard does say extra arguments are ignored, so in the specific
1444      case where we have multiple leaves (conditional expressions or
1445      ngettext) allow extra arguments if at least one leaf didn't have extra
1446      arguments, but was otherwise OK (either non-literal or checked OK).
1447      If the format is an empty string, this should be counted similarly to the
1448      case of extra format arguments.  */
1449   if (res.number_extra_args > 0 && res.number_non_literal == 0
1450       && res.number_other == 0 && warn_format_extra_args)
1451     warning ("too many arguments for format");
1452   if (res.number_dollar_extra_args > 0 && res.number_non_literal == 0
1453       && res.number_other == 0 && warn_format_extra_args)
1454     warning ("unused arguments in $-style format");
1455   if (res.number_empty > 0 && res.number_non_literal == 0
1456       && res.number_other == 0 && warn_format_zero_length)
1457     warning ("zero-length %s format string",
1458              format_types[info->format_type].name);
1459
1460   if (res.number_wide > 0)
1461     warning ("format is a wide character string");
1462
1463   if (res.number_unterminated > 0)
1464     warning ("unterminated format string");
1465 }
1466
1467 /* Callback from check_function_arguments_recurse to check a
1468    format string.  FORMAT_TREE is the format parameter.  ARG_NUM
1469    is the number of the format argument.  CTX points to a
1470    format_check_context.  */
1471
1472 static void
1473 check_format_arg (void *ctx, tree format_tree,
1474                   unsigned HOST_WIDE_INT arg_num)
1475 {
1476   format_check_context *format_ctx = ctx;
1477   format_check_results *res = format_ctx->res;
1478   function_format_info *info = format_ctx->info;
1479   tree params = format_ctx->params;
1480
1481   int format_length;
1482   HOST_WIDE_INT offset;
1483   const char *format_chars;
1484   tree array_size = 0;
1485   tree array_init;
1486
1487   if (integer_zerop (format_tree))
1488     {
1489       /* Skip to first argument to check, so we can see if this format
1490          has any arguments (it shouldn't).  */
1491       while (arg_num + 1 < info->first_arg_num)
1492         {
1493           if (params == 0)
1494             return;
1495           params = TREE_CHAIN (params);
1496           ++arg_num;
1497         }
1498
1499       if (params == 0)
1500         res->number_other++;
1501       else
1502         res->number_extra_args++;
1503
1504       return;
1505     }
1506
1507   offset = 0;
1508   if (TREE_CODE (format_tree) == PLUS_EXPR)
1509     {
1510       tree arg0, arg1;
1511
1512       arg0 = TREE_OPERAND (format_tree, 0);
1513       arg1 = TREE_OPERAND (format_tree, 1);
1514       STRIP_NOPS (arg0);
1515       STRIP_NOPS (arg1);
1516       if (TREE_CODE (arg1) == INTEGER_CST)
1517         format_tree = arg0;
1518       else if (TREE_CODE (arg0) == INTEGER_CST)
1519         {
1520           format_tree = arg1;
1521           arg1 = arg0;
1522         }
1523       else
1524         {
1525           res->number_non_literal++;
1526           return;
1527         }
1528       if (!host_integerp (arg1, 0)
1529           || (offset = tree_low_cst (arg1, 0)) < 0)
1530         {
1531           res->number_non_literal++;
1532           return;
1533         }
1534     }
1535   if (TREE_CODE (format_tree) != ADDR_EXPR)
1536     {
1537       res->number_non_literal++;
1538       return;
1539     }
1540   format_tree = TREE_OPERAND (format_tree, 0);
1541   if (TREE_CODE (format_tree) == VAR_DECL
1542       && TREE_CODE (TREE_TYPE (format_tree)) == ARRAY_TYPE
1543       && (array_init = decl_constant_value (format_tree)) != format_tree
1544       && TREE_CODE (array_init) == STRING_CST)
1545     {
1546       /* Extract the string constant initializer.  Note that this may include
1547          a trailing NUL character that is not in the array (e.g.
1548          const char a[3] = "foo";).  */
1549       array_size = DECL_SIZE_UNIT (format_tree);
1550       format_tree = array_init;
1551     }
1552   if (TREE_CODE (format_tree) != STRING_CST)
1553     {
1554       res->number_non_literal++;
1555       return;
1556     }
1557   if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree))) != char_type_node)
1558     {
1559       res->number_wide++;
1560       return;
1561     }
1562   format_chars = TREE_STRING_POINTER (format_tree);
1563   format_length = TREE_STRING_LENGTH (format_tree);
1564   if (array_size != 0)
1565     {
1566       /* Variable length arrays can't be initialized.  */
1567       if (TREE_CODE (array_size) != INTEGER_CST)
1568         abort ();
1569       if (host_integerp (array_size, 0))
1570         {
1571           HOST_WIDE_INT array_size_value = TREE_INT_CST_LOW (array_size);
1572           if (array_size_value > 0
1573               && array_size_value == (int) array_size_value
1574               && format_length > array_size_value)
1575             format_length = array_size_value;
1576         }
1577     }
1578   if (offset)
1579     {
1580       if (offset >= format_length)
1581         {
1582           res->number_non_literal++;
1583           return;
1584         }
1585       format_chars += offset;
1586       format_length -= offset;
1587     }
1588   if (format_length < 1)
1589     {
1590       res->number_unterminated++;
1591       return;
1592     }
1593   if (format_length == 1)
1594     {
1595       res->number_empty++;
1596       return;
1597     }
1598   if (format_chars[--format_length] != 0)
1599     {
1600       res->number_unterminated++;
1601       return;
1602     }
1603
1604   /* Skip to first argument to check.  */
1605   while (arg_num + 1 < info->first_arg_num)
1606     {
1607       if (params == 0)
1608         return;
1609       params = TREE_CHAIN (params);
1610       ++arg_num;
1611     }
1612   /* Provisionally increment res->number_other; check_format_info_main
1613      will decrement it if it finds there are extra arguments, but this way
1614      need not adjust it for every return.  */
1615   res->number_other++;
1616   check_format_info_main (res, info, format_chars, format_length,
1617                           params, arg_num);
1618 }
1619
1620
1621 /* Do the main part of checking a call to a format function.  FORMAT_CHARS
1622    is the NUL-terminated format string (which at this point may contain
1623    internal NUL characters); FORMAT_LENGTH is its length (excluding the
1624    terminating NUL character).  ARG_NUM is one less than the number of
1625    the first format argument to check; PARAMS points to that format
1626    argument in the list of arguments.  */
1627
1628 static void
1629 check_format_info_main (format_check_results *res,
1630                         function_format_info *info, const char *format_chars,
1631                         int format_length, tree params,
1632                         unsigned HOST_WIDE_INT arg_num)
1633 {
1634   const char *orig_format_chars = format_chars;
1635   tree first_fillin_param = params;
1636
1637   const format_kind_info *fki = &format_types[info->format_type];
1638   const format_flag_spec *flag_specs = fki->flag_specs;
1639   const format_flag_pair *bad_flag_pairs = fki->bad_flag_pairs;
1640
1641   /* -1 if no conversions taking an operand have been found; 0 if one has
1642      and it didn't use $; 1 if $ formats are in use.  */
1643   int has_operand_number = -1;
1644
1645   init_dollar_format_checking (info->first_arg_num, first_fillin_param);
1646
1647   while (1)
1648     {
1649       int i;
1650       int suppressed = FALSE;
1651       const char *length_chars = NULL;
1652       enum format_lengths length_chars_val = FMT_LEN_none;
1653       enum format_std_version length_chars_std = STD_C89;
1654       int format_char;
1655       tree cur_param;
1656       tree wanted_type;
1657       int main_arg_num = 0;
1658       tree main_arg_params = 0;
1659       enum format_std_version wanted_type_std;
1660       const char *wanted_type_name;
1661       format_wanted_type width_wanted_type;
1662       format_wanted_type precision_wanted_type;
1663       format_wanted_type main_wanted_type;
1664       format_wanted_type *first_wanted_type = NULL;
1665       format_wanted_type *last_wanted_type = NULL;
1666       const format_length_info *fli = NULL;
1667       const format_char_info *fci = NULL;
1668       char flag_chars[256];
1669       int aflag = 0;
1670       const char *format_start = format_chars;
1671       if (*format_chars == 0)
1672         {
1673           if (format_chars - orig_format_chars != format_length)
1674             warning ("embedded %<\\0%> in format");
1675           if (info->first_arg_num != 0 && params != 0
1676               && has_operand_number <= 0)
1677             {
1678               res->number_other--;
1679               res->number_extra_args++;
1680             }
1681           if (has_operand_number > 0)
1682             finish_dollar_format_checking (res, fki->flags & (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK);
1683           return;
1684         }
1685       if (*format_chars++ != '%')
1686         continue;
1687       if (*format_chars == 0)
1688         {
1689           warning ("spurious trailing %<%%%> in format");
1690           continue;
1691         }
1692       if (*format_chars == '%')
1693         {
1694           ++format_chars;
1695           continue;
1696         }
1697       flag_chars[0] = 0;
1698
1699       if ((fki->flags & (int) FMT_FLAG_USE_DOLLAR) && has_operand_number != 0)
1700         {
1701           /* Possibly read a $ operand number at the start of the format.
1702              If one was previously used, one is required here.  If one
1703              is not used here, we can't immediately conclude this is a
1704              format without them, since it could be printf %m or scanf %*.  */
1705           int opnum;
1706           opnum = maybe_read_dollar_number (&format_chars, 0,
1707                                             first_fillin_param,
1708                                             &main_arg_params, fki);
1709           if (opnum == -1)
1710             return;
1711           else if (opnum > 0)
1712             {
1713               has_operand_number = 1;
1714               main_arg_num = opnum + info->first_arg_num - 1;
1715             }
1716         }
1717       else if (fki->flags & FMT_FLAG_USE_DOLLAR)
1718         {
1719           if (avoid_dollar_number (format_chars))
1720             return;
1721         }
1722
1723       /* Read any format flags, but do not yet validate them beyond removing
1724          duplicates, since in general validation depends on the rest of
1725          the format.  */
1726       while (*format_chars != 0
1727              && strchr (fki->flag_chars, *format_chars) != 0)
1728         {
1729           const format_flag_spec *s = get_flag_spec (flag_specs,
1730                                                      *format_chars, NULL);
1731           if (strchr (flag_chars, *format_chars) != 0)
1732             {
1733               warning ("repeated %s in format", _(s->name));
1734             }
1735           else
1736             {
1737               i = strlen (flag_chars);
1738               flag_chars[i++] = *format_chars;
1739               flag_chars[i] = 0;
1740             }
1741           if (s->skip_next_char)
1742             {
1743               ++format_chars;
1744               if (*format_chars == 0)
1745                 {
1746                   warning ("missing fill character at end of strfmon format");
1747                   return;
1748                 }
1749             }
1750           ++format_chars;
1751         }
1752
1753       /* Read any format width, possibly * or *m$.  */
1754       if (fki->width_char != 0)
1755         {
1756           if (fki->width_type != NULL && *format_chars == '*')
1757             {
1758               i = strlen (flag_chars);
1759               flag_chars[i++] = fki->width_char;
1760               flag_chars[i] = 0;
1761               /* "...a field width...may be indicated by an asterisk.
1762                  In this case, an int argument supplies the field width..."  */
1763               ++format_chars;
1764               if (has_operand_number != 0)
1765                 {
1766                   int opnum;
1767                   opnum = maybe_read_dollar_number (&format_chars,
1768                                                     has_operand_number == 1,
1769                                                     first_fillin_param,
1770                                                     &params, fki);
1771                   if (opnum == -1)
1772                     return;
1773                   else if (opnum > 0)
1774                     {
1775                       has_operand_number = 1;
1776                       arg_num = opnum + info->first_arg_num - 1;
1777                     }
1778                   else
1779                     has_operand_number = 0;
1780                 }
1781               else
1782                 {
1783                   if (avoid_dollar_number (format_chars))
1784                     return;
1785                 }
1786               if (info->first_arg_num != 0)
1787                 {
1788                   if (params == 0)
1789                     {
1790                       warning ("too few arguments for format");
1791                       return;
1792                     }
1793                   cur_param = TREE_VALUE (params);
1794                   if (has_operand_number <= 0)
1795                     {
1796                       params = TREE_CHAIN (params);
1797                       ++arg_num;
1798                     }
1799                   width_wanted_type.wanted_type = *fki->width_type;
1800                   width_wanted_type.wanted_type_name = NULL;
1801                   width_wanted_type.pointer_count = 0;
1802                   width_wanted_type.char_lenient_flag = 0;
1803                   width_wanted_type.writing_in_flag = 0;
1804                   width_wanted_type.reading_from_flag = 0;
1805                   width_wanted_type.name = _("field width");
1806                   width_wanted_type.param = cur_param;
1807                   width_wanted_type.arg_num = arg_num;
1808                   width_wanted_type.next = NULL;
1809                   if (last_wanted_type != 0)
1810                     last_wanted_type->next = &width_wanted_type;
1811                   if (first_wanted_type == 0)
1812                     first_wanted_type = &width_wanted_type;
1813                   last_wanted_type = &width_wanted_type;
1814                 }
1815             }
1816           else
1817             {
1818               /* Possibly read a numeric width.  If the width is zero,
1819                  we complain if appropriate.  */
1820               int non_zero_width_char = FALSE;
1821               int found_width = FALSE;
1822               while (ISDIGIT (*format_chars))
1823                 {
1824                   found_width = TRUE;
1825                   if (*format_chars != '0')
1826                     non_zero_width_char = TRUE;
1827                   ++format_chars;
1828                 }
1829               if (found_width && !non_zero_width_char &&
1830                   (fki->flags & (int) FMT_FLAG_ZERO_WIDTH_BAD))
1831                 warning ("zero width in %s format", fki->name);
1832               if (found_width)
1833                 {
1834                   i = strlen (flag_chars);
1835                   flag_chars[i++] = fki->width_char;
1836                   flag_chars[i] = 0;
1837                 }
1838             }
1839         }
1840
1841       /* Read any format left precision (must be a number, not *).  */
1842       if (fki->left_precision_char != 0 && *format_chars == '#')
1843         {
1844           ++format_chars;
1845           i = strlen (flag_chars);
1846           flag_chars[i++] = fki->left_precision_char;
1847           flag_chars[i] = 0;
1848           if (!ISDIGIT (*format_chars))
1849             warning ("empty left precision in %s format", fki->name);
1850           while (ISDIGIT (*format_chars))
1851             ++format_chars;
1852         }
1853
1854       /* Read any format precision, possibly * or *m$.  */
1855       if (fki->precision_char != 0 && *format_chars == '.')
1856         {
1857           ++format_chars;
1858           i = strlen (flag_chars);
1859           flag_chars[i++] = fki->precision_char;
1860           flag_chars[i] = 0;
1861           if (fki->precision_type != NULL && *format_chars == '*')
1862             {
1863               /* "...a...precision...may be indicated by an asterisk.
1864                  In this case, an int argument supplies the...precision."  */
1865               ++format_chars;
1866               if (has_operand_number != 0)
1867                 {
1868                   int opnum;
1869                   opnum = maybe_read_dollar_number (&format_chars,
1870                                                     has_operand_number == 1,
1871                                                     first_fillin_param,
1872                                                     &params, fki);
1873                   if (opnum == -1)
1874                     return;
1875                   else if (opnum > 0)
1876                     {
1877                       has_operand_number = 1;
1878                       arg_num = opnum + info->first_arg_num - 1;
1879                     }
1880                   else
1881                     has_operand_number = 0;
1882                 }
1883               else
1884                 {
1885                   if (avoid_dollar_number (format_chars))
1886                     return;
1887                 }
1888               if (info->first_arg_num != 0)
1889                 {
1890                   if (params == 0)
1891                     {
1892                       warning ("too few arguments for format");
1893                       return;
1894                     }
1895                   cur_param = TREE_VALUE (params);
1896                   if (has_operand_number <= 0)
1897                     {
1898                       params = TREE_CHAIN (params);
1899                       ++arg_num;
1900                     }
1901                   precision_wanted_type.wanted_type = *fki->precision_type;
1902                   precision_wanted_type.wanted_type_name = NULL;
1903                   precision_wanted_type.pointer_count = 0;
1904                   precision_wanted_type.char_lenient_flag = 0;
1905                   precision_wanted_type.writing_in_flag = 0;
1906                   precision_wanted_type.reading_from_flag = 0;
1907                   precision_wanted_type.name = _("field precision");
1908                   precision_wanted_type.param = cur_param;
1909                   precision_wanted_type.arg_num = arg_num;
1910                   precision_wanted_type.next = NULL;
1911                   if (last_wanted_type != 0)
1912                     last_wanted_type->next = &precision_wanted_type;
1913                   if (first_wanted_type == 0)
1914                     first_wanted_type = &precision_wanted_type;
1915                   last_wanted_type = &precision_wanted_type;
1916                 }
1917             }
1918           else
1919             {
1920               if (!(fki->flags & (int) FMT_FLAG_EMPTY_PREC_OK)
1921                   && !ISDIGIT (*format_chars))
1922                 warning ("empty precision in %s format", fki->name);
1923               while (ISDIGIT (*format_chars))
1924                 ++format_chars;
1925             }
1926         }
1927
1928       /* Read any length modifier, if this kind of format has them.  */
1929       fli = fki->length_char_specs;
1930       length_chars = NULL;
1931       length_chars_val = FMT_LEN_none;
1932       length_chars_std = STD_C89;
1933       if (fli)
1934         {
1935           while (fli->name != 0 && fli->name[0] != *format_chars)
1936             fli++;
1937           if (fli->name != 0)
1938             {
1939               format_chars++;
1940               if (fli->double_name != 0 && fli->name[0] == *format_chars)
1941                 {
1942                   format_chars++;
1943                   length_chars = fli->double_name;
1944                   length_chars_val = fli->double_index;
1945                   length_chars_std = fli->double_std;
1946                 }
1947               else
1948                 {
1949                   length_chars = fli->name;
1950                   length_chars_val = fli->index;
1951                   length_chars_std = fli->std;
1952                 }
1953               i = strlen (flag_chars);
1954               flag_chars[i++] = fki->length_code_char;
1955               flag_chars[i] = 0;
1956             }
1957           if (pedantic)
1958             {
1959               /* Warn if the length modifier is non-standard.  */
1960               if (ADJ_STD (length_chars_std) > C_STD_VER)
1961                 warning ("%s does not support the %qs %s length modifier",
1962                          C_STD_NAME (length_chars_std), length_chars,
1963                          fki->name);
1964             }
1965         }
1966
1967       /* Read any modifier (strftime E/O).  */
1968       if (fki->modifier_chars != NULL)
1969         {
1970           while (*format_chars != 0
1971                  && strchr (fki->modifier_chars, *format_chars) != 0)
1972             {
1973               if (strchr (flag_chars, *format_chars) != 0)
1974                 {
1975                   const format_flag_spec *s = get_flag_spec (flag_specs,
1976                                                              *format_chars, NULL);
1977                   warning ("repeated %s in format", _(s->name));
1978                 }
1979               else
1980                 {
1981                   i = strlen (flag_chars);
1982                   flag_chars[i++] = *format_chars;
1983                   flag_chars[i] = 0;
1984                 }
1985               ++format_chars;
1986             }
1987         }
1988
1989       /* Handle the scanf allocation kludge.  */
1990       if (fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
1991         {
1992           if (*format_chars == 'a' && !flag_isoc99)
1993             {
1994               if (format_chars[1] == 's' || format_chars[1] == 'S'
1995                   || format_chars[1] == '[')
1996                 {
1997                   /* `a' is used as a flag.  */
1998                   i = strlen (flag_chars);
1999                   flag_chars[i++] = 'a';
2000                   flag_chars[i] = 0;
2001                   format_chars++;
2002                 }
2003             }
2004         }
2005
2006       format_char = *format_chars;
2007       if (format_char == 0
2008           || (!(fki->flags & (int) FMT_FLAG_FANCY_PERCENT_OK)
2009               && format_char == '%'))
2010         {
2011           warning ("conversion lacks type at end of format");
2012           continue;
2013         }
2014       format_chars++;
2015       fci = fki->conversion_specs;
2016       while (fci->format_chars != 0
2017              && strchr (fci->format_chars, format_char) == 0)
2018           ++fci;
2019       if (fci->format_chars == 0)
2020         {
2021           if (ISGRAPH(format_char))
2022             warning ("unknown conversion type character %qc in format",
2023                      format_char);
2024           else
2025             warning ("unknown conversion type character 0x%x in format",
2026                      format_char);
2027           continue;
2028         }
2029       if (pedantic)
2030         {
2031           if (ADJ_STD (fci->std) > C_STD_VER)
2032             warning ("%s does not support the %<%%%c%> %s format",
2033                      C_STD_NAME (fci->std), format_char, fki->name);
2034         }
2035
2036       /* Validate the individual flags used, removing any that are invalid.  */
2037       {
2038         int d = 0;
2039         for (i = 0; flag_chars[i] != 0; i++)
2040           {
2041             const format_flag_spec *s = get_flag_spec (flag_specs,
2042                                                        flag_chars[i], NULL);
2043             flag_chars[i - d] = flag_chars[i];
2044             if (flag_chars[i] == fki->length_code_char)
2045               continue;
2046             if (strchr (fci->flag_chars, flag_chars[i]) == 0)
2047               {
2048                 warning ("%s used with %<%%%c%> %s format",
2049                          _(s->name), format_char, fki->name);
2050                 d++;
2051                 continue;
2052               }
2053             if (pedantic)
2054               {
2055                 const format_flag_spec *t;
2056                 if (ADJ_STD (s->std) > C_STD_VER)
2057                   warning ("%s does not support %s",
2058                            C_STD_NAME (s->std), _(s->long_name));
2059                 t = get_flag_spec (flag_specs, flag_chars[i], fci->flags2);
2060                 if (t != NULL && ADJ_STD (t->std) > ADJ_STD (s->std))
2061                   {
2062                     const char *long_name = (t->long_name != NULL
2063                                              ? t->long_name
2064                                              : s->long_name);
2065                     if (ADJ_STD (t->std) > C_STD_VER)
2066                       warning ("%s does not support %s with the %<%%%c%> %s format",
2067                                C_STD_NAME (t->std), _(long_name),
2068                                format_char, fki->name);
2069                   }
2070               }
2071           }
2072         flag_chars[i - d] = 0;
2073       }
2074
2075       if ((fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
2076           && strchr (flag_chars, 'a') != 0)
2077         aflag = 1;
2078
2079       if (fki->suppression_char
2080           && strchr (flag_chars, fki->suppression_char) != 0)
2081         suppressed = 1;
2082
2083       /* Validate the pairs of flags used.  */
2084       for (i = 0; bad_flag_pairs[i].flag_char1 != 0; i++)
2085         {
2086           const format_flag_spec *s, *t;
2087           if (strchr (flag_chars, bad_flag_pairs[i].flag_char1) == 0)
2088             continue;
2089           if (strchr (flag_chars, bad_flag_pairs[i].flag_char2) == 0)
2090             continue;
2091           if (bad_flag_pairs[i].predicate != 0
2092               && strchr (fci->flags2, bad_flag_pairs[i].predicate) == 0)
2093             continue;
2094           s = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char1, NULL);
2095           t = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char2, NULL);
2096           if (bad_flag_pairs[i].ignored)
2097             {
2098               if (bad_flag_pairs[i].predicate != 0)
2099                 warning ("%s ignored with %s and %<%%%c%> %s format",
2100                          _(s->name), _(t->name), format_char,
2101                          fki->name);
2102               else
2103                 warning ("%s ignored with %s in %s format",
2104                          _(s->name), _(t->name), fki->name);
2105             }
2106           else
2107             {
2108               if (bad_flag_pairs[i].predicate != 0)
2109                 warning ("use of %s and %s together with %<%%%c%> %s format",
2110                          _(s->name), _(t->name), format_char,
2111                          fki->name);
2112               else
2113                 warning ("use of %s and %s together in %s format",
2114                          _(s->name), _(t->name), fki->name);
2115             }
2116         }
2117
2118       /* Give Y2K warnings.  */
2119       if (warn_format_y2k)
2120         {
2121           int y2k_level = 0;
2122           if (strchr (fci->flags2, '4') != 0)
2123             if (strchr (flag_chars, 'E') != 0)
2124               y2k_level = 3;
2125             else
2126               y2k_level = 2;
2127           else if (strchr (fci->flags2, '3') != 0)
2128             y2k_level = 3;
2129           else if (strchr (fci->flags2, '2') != 0)
2130             y2k_level = 2;
2131           if (y2k_level == 3)
2132             warning ("%<%%%c%> yields only last 2 digits of year in some locales",
2133                      format_char);
2134           else if (y2k_level == 2)
2135             warning ("%<%%%c%> yields only last 2 digits of year", format_char);
2136         }
2137
2138       if (strchr (fci->flags2, '[') != 0)
2139         {
2140           /* Skip over scan set, in case it happens to have '%' in it.  */
2141           if (*format_chars == '^')
2142             ++format_chars;
2143           /* Find closing bracket; if one is hit immediately, then
2144              it's part of the scan set rather than a terminator.  */
2145           if (*format_chars == ']')
2146             ++format_chars;
2147           while (*format_chars && *format_chars != ']')
2148             ++format_chars;
2149           if (*format_chars != ']')
2150             /* The end of the format string was reached.  */
2151             warning ("no closing %<]%> for %<%%[%> format");
2152         }
2153
2154       wanted_type = 0;
2155       wanted_type_name = 0;
2156       if (fki->flags & (int) FMT_FLAG_ARG_CONVERT)
2157         {
2158           wanted_type = (fci->types[length_chars_val].type
2159                          ? *fci->types[length_chars_val].type : 0);
2160           wanted_type_name = fci->types[length_chars_val].name;
2161           wanted_type_std = fci->types[length_chars_val].std;
2162           if (wanted_type == 0)
2163             {
2164               warning ("use of %qs length modifier with %qc type character",
2165                        length_chars, format_char);
2166               /* Heuristic: skip one argument when an invalid length/type
2167                  combination is encountered.  */
2168               arg_num++;
2169               if (params == 0)
2170                 {
2171                   warning ("too few arguments for format");
2172                   return;
2173                 }
2174               params = TREE_CHAIN (params);
2175               continue;
2176             }
2177           else if (pedantic
2178                    /* Warn if non-standard, provided it is more non-standard
2179                       than the length and type characters that may already
2180                       have been warned for.  */
2181                    && ADJ_STD (wanted_type_std) > ADJ_STD (length_chars_std)
2182                    && ADJ_STD (wanted_type_std) > ADJ_STD (fci->std))
2183             {
2184               if (ADJ_STD (wanted_type_std) > C_STD_VER)
2185                 warning ("%s does not support the %<%%%s%c%> %s format",
2186                          C_STD_NAME (wanted_type_std), length_chars,
2187                          format_char, fki->name);
2188             }
2189         }
2190
2191       /* Finally. . .check type of argument against desired type!  */
2192       if (info->first_arg_num == 0)
2193         continue;
2194       if ((fci->pointer_count == 0 && wanted_type == void_type_node)
2195           || suppressed)
2196         {
2197           if (main_arg_num != 0)
2198             {
2199               if (suppressed)
2200                 warning ("operand number specified with suppressed assignment");
2201               else
2202                 warning ("operand number specified for format taking no argument");
2203             }
2204         }
2205       else
2206         {
2207           if (main_arg_num != 0)
2208             {
2209               arg_num = main_arg_num;
2210               params = main_arg_params;
2211             }
2212           else
2213             {
2214               ++arg_num;
2215               if (has_operand_number > 0)
2216                 {
2217                   warning ("missing $ operand number in format");
2218                   return;
2219                 }
2220               else
2221                 has_operand_number = 0;
2222               if (params == 0)
2223                 {
2224                   warning ("too few arguments for format");
2225                   return;
2226                 }
2227             }
2228           cur_param = TREE_VALUE (params);
2229           params = TREE_CHAIN (params);
2230           main_wanted_type.wanted_type = wanted_type;
2231           main_wanted_type.wanted_type_name = wanted_type_name;
2232           main_wanted_type.pointer_count = fci->pointer_count + aflag;
2233           main_wanted_type.char_lenient_flag = 0;
2234           if (strchr (fci->flags2, 'c') != 0)
2235             main_wanted_type.char_lenient_flag = 1;
2236           main_wanted_type.writing_in_flag = 0;
2237           main_wanted_type.reading_from_flag = 0;
2238           if (aflag)
2239             main_wanted_type.writing_in_flag = 1;
2240           else
2241             {
2242               if (strchr (fci->flags2, 'W') != 0)
2243                 main_wanted_type.writing_in_flag = 1;
2244               if (strchr (fci->flags2, 'R') != 0)
2245                 main_wanted_type.reading_from_flag = 1;
2246             }
2247           main_wanted_type.name = NULL;
2248           main_wanted_type.param = cur_param;
2249           main_wanted_type.arg_num = arg_num;
2250           main_wanted_type.next = NULL;
2251           if (last_wanted_type != 0)
2252             last_wanted_type->next = &main_wanted_type;
2253           if (first_wanted_type == 0)
2254             first_wanted_type = &main_wanted_type;
2255           last_wanted_type = &main_wanted_type;
2256         }
2257
2258       if (first_wanted_type != 0)
2259         check_format_types (first_wanted_type, format_start,
2260                             format_chars - format_start);
2261
2262     }
2263 }
2264
2265
2266 /* Check the argument types from a single format conversion (possibly
2267    including width and precision arguments).  */
2268 static void
2269 check_format_types (format_wanted_type *types, const char *format_start,
2270                     int format_length)
2271 {
2272   for (; types != 0; types = types->next)
2273     {
2274       tree cur_param;
2275       tree cur_type;
2276       tree orig_cur_type;
2277       tree wanted_type;
2278       int arg_num;
2279       int i;
2280       int char_type_flag;
2281       cur_param = types->param;
2282       cur_type = TREE_TYPE (cur_param);
2283       if (cur_type == error_mark_node)
2284         continue;
2285       orig_cur_type = cur_type;
2286       char_type_flag = 0;
2287       wanted_type = types->wanted_type;
2288       arg_num = types->arg_num;
2289
2290       /* The following should not occur here.  */
2291       if (wanted_type == 0)
2292         abort ();
2293       if (wanted_type == void_type_node && types->pointer_count == 0)
2294         abort ();
2295
2296       if (types->pointer_count == 0)
2297         wanted_type = lang_hooks.types.type_promotes_to (wanted_type);
2298
2299       wanted_type = TYPE_MAIN_VARIANT (wanted_type);
2300
2301       STRIP_NOPS (cur_param);
2302
2303       /* Check the types of any additional pointer arguments
2304          that precede the "real" argument.  */
2305       for (i = 0; i < types->pointer_count; ++i)
2306         {
2307           if (TREE_CODE (cur_type) == POINTER_TYPE)
2308             {
2309               cur_type = TREE_TYPE (cur_type);
2310               if (cur_type == error_mark_node)
2311                 break;
2312
2313               /* Check for writing through a NULL pointer.  */
2314               if (types->writing_in_flag
2315                   && i == 0
2316                   && cur_param != 0
2317                   && integer_zerop (cur_param))
2318                 warning ("writing through null pointer (arg %d)",
2319                          arg_num);
2320
2321               /* Check for reading through a NULL pointer.  */
2322               if (types->reading_from_flag
2323                   && i == 0
2324                   && cur_param != 0
2325                   && integer_zerop (cur_param))
2326                 warning ("reading through null pointer (arg %d)",
2327                          arg_num);
2328
2329               if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
2330                 cur_param = TREE_OPERAND (cur_param, 0);
2331               else
2332                 cur_param = 0;
2333
2334               /* See if this is an attempt to write into a const type with
2335                  scanf or with printf "%n".  Note: the writing in happens
2336                  at the first indirection only, if for example
2337                  void * const * is passed to scanf %p; passing
2338                  const void ** is simply passing an incompatible type.  */
2339               if (types->writing_in_flag
2340                   && i == 0
2341                   && (TYPE_READONLY (cur_type)
2342                       || (cur_param != 0
2343                           && (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'c'
2344                               || (DECL_P (cur_param)
2345                                   && TREE_READONLY (cur_param))))))
2346                 warning ("writing into constant object (arg %d)", arg_num);
2347
2348               /* If there are extra type qualifiers beyond the first
2349                  indirection, then this makes the types technically
2350                  incompatible.  */
2351               if (i > 0
2352                   && pedantic
2353                   && (TYPE_READONLY (cur_type)
2354                       || TYPE_VOLATILE (cur_type)
2355                       || TYPE_RESTRICT (cur_type)))
2356                 warning ("extra type qualifiers in format argument (arg %d)",
2357                          arg_num);
2358
2359             }
2360           else
2361             {
2362               format_type_warning (types->name, format_start, format_length,
2363                                    wanted_type, types->pointer_count,
2364                                    types->wanted_type_name, orig_cur_type,
2365                                    arg_num);
2366               break;
2367             }
2368         }
2369
2370       if (i < types->pointer_count)
2371         continue;
2372
2373       cur_type = TYPE_MAIN_VARIANT (cur_type);
2374
2375       /* Check whether the argument type is a character type.  This leniency
2376          only applies to certain formats, flagged with 'c'.
2377       */
2378       if (types->char_lenient_flag)
2379         char_type_flag = (cur_type == char_type_node
2380                           || cur_type == signed_char_type_node
2381                           || cur_type == unsigned_char_type_node);
2382
2383       /* Check the type of the "real" argument, if there's a type we want.  */
2384       if (wanted_type == cur_type)
2385         continue;
2386       /* If we want `void *', allow any pointer type.
2387          (Anything else would already have got a warning.)
2388          With -pedantic, only allow pointers to void and to character
2389          types.  */
2390       if (wanted_type == void_type_node
2391           && (!pedantic || (i == 1 && char_type_flag)))
2392         continue;
2393       /* Don't warn about differences merely in signedness, unless
2394          -pedantic.  With -pedantic, warn if the type is a pointer
2395          target and not a character type, and for character types at
2396          a second level of indirection.  */
2397       if (TREE_CODE (wanted_type) == INTEGER_TYPE
2398           && TREE_CODE (cur_type) == INTEGER_TYPE
2399           && (! pedantic || i == 0 || (i == 1 && char_type_flag))
2400           && (TYPE_UNSIGNED (wanted_type)
2401               ? wanted_type == c_common_unsigned_type (cur_type)
2402               : wanted_type == c_common_signed_type (cur_type)))
2403         continue;
2404       /* Likewise, "signed char", "unsigned char" and "char" are
2405          equivalent but the above test won't consider them equivalent.  */
2406       if (wanted_type == char_type_node
2407           && (! pedantic || i < 2)
2408           && char_type_flag)
2409         continue;
2410       /* Now we have a type mismatch.  */
2411       format_type_warning (types->name, format_start, format_length,
2412                            wanted_type, types->pointer_count,
2413                            types->wanted_type_name, orig_cur_type, arg_num);
2414     }
2415 }
2416
2417
2418 /* Give a warning about a format argument of different type from that
2419    expected.  DESCR is a description such as "field precision", or
2420    NULL for an ordinary format.  For an ordinary format, FORMAT_START
2421    points to where the format starts in the format string and
2422    FORMAT_LENGTH is its length.  WANTED_TYPE is the type the argument
2423    should have after POINTER_COUNT pointer dereferences.
2424    WANTED_NAME_NAME is a possibly more friendly name of WANTED_TYPE,
2425    or NULL if the ordinary name of the type should be used.  ARG_TYPE
2426    is the type of the actual argument.  ARG_NUM is the number of that
2427    argument.  */
2428 static void
2429 format_type_warning (const char *descr, const char *format_start,
2430                      int format_length, tree wanted_type, int pointer_count,
2431                      const char *wanted_type_name, tree arg_type, int arg_num)
2432 {
2433   char *p;
2434   /* If ARG_TYPE is a typedef with a misleading name (for example,
2435      size_t but not the standard size_t expected by printf %zu), avoid
2436      printing the typedef name.  */
2437   if (wanted_type_name
2438       && TYPE_NAME (arg_type)
2439       && TREE_CODE (TYPE_NAME (arg_type)) == TYPE_DECL
2440       && DECL_NAME (TYPE_NAME (arg_type))
2441       && !strcmp (wanted_type_name,
2442                   lang_hooks.decl_printable_name (TYPE_NAME (arg_type), 2)))
2443     arg_type = TYPE_MAIN_VARIANT (arg_type);
2444   /* The format type and name exclude any '*' for pointers, so those
2445      must be formatted manually.  For all the types we currently have,
2446      this is adequate, but formats taking pointers to functions or
2447      arrays would require the full type to be built up in order to
2448      print it with %T.  */
2449   p = alloca (pointer_count + 2);
2450   if (pointer_count == 0)
2451     p[0] = 0;
2452   else if (c_dialect_cxx ())
2453     {
2454       memset (p, '*', pointer_count);
2455       p[pointer_count] = 0;
2456     }
2457   else
2458     {
2459       p[0] = ' ';
2460       memset (p + 1, '*', pointer_count);
2461       p[pointer_count + 1] = 0;
2462     }
2463   if (wanted_type_name)
2464     {
2465       if (descr)
2466         warning ("%s should have type %<%s%s%>, but argument %d has type %qT",
2467                  descr, wanted_type_name, p, arg_num, arg_type);
2468       else
2469         warning ("format %q.*s expects type %<%s%s%>, but argument %d has type %qT",
2470                  format_length, format_start, wanted_type_name, p,
2471                  arg_num, arg_type);
2472     }
2473   else
2474     {
2475       if (descr)
2476         warning ("%s should have type %<%T%s%>, but argument %d has type %qT",
2477                  descr, wanted_type, p, arg_num, arg_type);
2478       else
2479         warning ("format %q.*s expects type %<%T%s%>, but argument %d has type %qT",
2480                  format_length, format_start, wanted_type, p, arg_num, arg_type);
2481     }
2482 }
2483
2484
2485 /* Given a format_char_info array FCI, and a character C, this function
2486    returns the index into the conversion_specs where that specifier's
2487    data is located.  If the character isn't found it aborts.  */
2488 static unsigned int
2489 find_char_info_specifier_index (const format_char_info *fci, int c)
2490 {
2491   unsigned int i = 0;
2492   
2493   while (fci->format_chars)
2494     {
2495       if (strchr (fci->format_chars, c))
2496         return i;
2497       i++; fci++;
2498     }
2499   
2500   /* We shouldn't be looking for a non-existent specifier.  */
2501   abort ();
2502 }
2503
2504 /* Given a format_length_info array FLI, and a character C, this
2505    function returns the index into the conversion_specs where that
2506    modifier's data is located.  If the character isn't found it
2507    aborts.  */
2508 static unsigned int
2509 find_length_info_modifier_index (const format_length_info *fli, int c)
2510 {
2511   unsigned int i = 0;
2512   
2513   while (fli->name)
2514     {
2515       if (strchr (fli->name, c))
2516         return i;
2517       i++; fli++;
2518     }
2519   
2520   /* We shouldn't be looking for a non-existent modifier.  */
2521   abort ();
2522 }
2523
2524 /* Determine the type of HOST_WIDE_INT in the code being compiled for
2525    use in GCC's __asm_fprintf__ custom format attribute.  You must
2526    have set dynamic_format_types before calling this function.  */
2527 static void
2528 init_dynamic_asm_fprintf_info (void)
2529 {
2530   static tree hwi;
2531       
2532   if (!hwi)
2533     {
2534       format_length_info *new_asm_fprintf_length_specs;
2535       unsigned int i;
2536           
2537       /* Find the underlying type for HOST_WIDE_INT.  For the %w
2538          length modifier to work, one must have issued: "typedef
2539          HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2540          prior to using that modifier.  */
2541       if (!(hwi = maybe_get_identifier ("__gcc_host_wide_int__"))
2542           || !(hwi = DECL_ORIGINAL_TYPE (identifier_global_value (hwi))))
2543         abort ();
2544
2545       /* Create a new (writable) copy of asm_fprintf_length_specs.  */
2546       new_asm_fprintf_length_specs = xmemdup (asm_fprintf_length_specs,
2547                                               sizeof (asm_fprintf_length_specs),
2548                                               sizeof (asm_fprintf_length_specs));
2549
2550       /* HOST_WIDE_INT must be one of 'long' or 'long long'.  */
2551       i = find_length_info_modifier_index (new_asm_fprintf_length_specs, 'w');
2552       if (hwi == long_integer_type_node)
2553         new_asm_fprintf_length_specs[i].index = FMT_LEN_l;
2554       else if (hwi == long_long_integer_type_node)
2555         new_asm_fprintf_length_specs[i].index = FMT_LEN_ll;
2556       else
2557         abort ();
2558
2559       /* Assign the new data for use.  */
2560       dynamic_format_types[asm_fprintf_format_type].length_char_specs =
2561         new_asm_fprintf_length_specs;
2562     }
2563 }
2564
2565 /* Determine the types of "tree" and "location_t" in the code being
2566    compiled for use in GCC's diagnostic custom format attributes.  You
2567    must have set dynamic_format_types before calling this function.  */
2568 static void
2569 init_dynamic_diag_info (void)
2570 {
2571   static tree t, loc, hwi;
2572       
2573   if (!loc || !t || !hwi)
2574     {
2575       static format_char_info *diag_fci, *cdiag_fci, *cxxdiag_fci;
2576       static format_length_info *diag_ls;
2577       unsigned int i;
2578
2579       /* For the GCC-diagnostics custom format specifiers to work, one
2580          must have declared `tree' and/or `location_t' prior to using
2581          those attributes.  If we haven't seen these declarations then
2582          you shouldn't use the specifiers requiring these types.
2583          However we don't force a hard ICE because we may see only one
2584          or the other type.  */
2585       if ((loc = maybe_get_identifier ("location_t")))
2586         loc = TREE_TYPE (identifier_global_value (loc));
2587
2588       /* We need to grab the underlying `union tree_node' so peek into
2589          an extra type level.  */
2590       if ((t = maybe_get_identifier ("tree")))
2591         t = TREE_TYPE (TREE_TYPE (identifier_global_value (t)));
2592     
2593       /* Find the underlying type for HOST_WIDE_INT.  For the %w
2594          length modifier to work, one must have issued: "typedef
2595          HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2596          prior to using that modifier.  */
2597       if ((hwi = maybe_get_identifier ("__gcc_host_wide_int__")))
2598         hwi = DECL_ORIGINAL_TYPE (identifier_global_value (hwi));
2599       
2600       /* Assign the new data for use.  */
2601
2602       /* All the GCC diag formats use the same length specs.  */
2603       if (! diag_ls)
2604         dynamic_format_types[gcc_diag_format_type].length_char_specs =
2605           dynamic_format_types[gcc_cdiag_format_type].length_char_specs =
2606           dynamic_format_types[gcc_cxxdiag_format_type].length_char_specs =
2607           diag_ls = xmemdup (gcc_diag_length_specs,
2608                              sizeof (gcc_diag_length_specs),
2609                              sizeof (gcc_diag_length_specs)); 
2610       if (hwi)
2611         {
2612           /* HOST_WIDE_INT must be one of 'long' or 'long long'.  */
2613           i = find_length_info_modifier_index (diag_ls, 'w');
2614           if (hwi == long_integer_type_node)
2615             diag_ls[i].index = FMT_LEN_l;
2616           else if (hwi == long_long_integer_type_node)
2617             diag_ls[i].index = FMT_LEN_ll;
2618           else
2619             abort ();
2620         }
2621
2622       /* Handle the __gcc_diag__ format specifics.  */
2623       if (! diag_fci)
2624         dynamic_format_types[gcc_diag_format_type].conversion_specs =
2625           diag_fci = xmemdup (gcc_diag_char_table,
2626                               sizeof(gcc_diag_char_table),
2627                               sizeof(gcc_diag_char_table));
2628       if (loc)
2629         {
2630           i = find_char_info_specifier_index (diag_fci, 'H');
2631           diag_fci[i].types[0].type = &loc;
2632           diag_fci[i].pointer_count = 1;
2633         }
2634       if (t)
2635         {
2636           i = find_char_info_specifier_index (diag_fci, 'J');
2637           diag_fci[i].types[0].type = &t;
2638           diag_fci[i].pointer_count = 1;
2639         }
2640
2641       /* Handle the __gcc_cdiag__ format specifics.  */
2642       if (! cdiag_fci)
2643         dynamic_format_types[gcc_cdiag_format_type].conversion_specs =
2644           cdiag_fci = xmemdup (gcc_cdiag_char_table,
2645                                sizeof(gcc_cdiag_char_table),
2646                                sizeof(gcc_cdiag_char_table));
2647       if (loc)
2648         {
2649           i = find_char_info_specifier_index (cdiag_fci, 'H');
2650           cdiag_fci[i].types[0].type = &loc;
2651           cdiag_fci[i].pointer_count = 1;
2652         }
2653       if (t)
2654         {
2655           /* All specifiers taking a tree share the same struct.  */
2656           i = find_char_info_specifier_index (cdiag_fci, 'D');
2657           cdiag_fci[i].types[0].type = &t;
2658           cdiag_fci[i].pointer_count = 1;
2659           i = find_char_info_specifier_index (cdiag_fci, 'J');
2660           cdiag_fci[i].types[0].type = &t;
2661           cdiag_fci[i].pointer_count = 1;
2662         }
2663
2664       /* Handle the __gcc_cxxdiag__ format specifics.  */
2665       if (! cxxdiag_fci)
2666         dynamic_format_types[gcc_cxxdiag_format_type].conversion_specs =
2667           cxxdiag_fci = xmemdup (gcc_cxxdiag_char_table,
2668                                  sizeof(gcc_cxxdiag_char_table),
2669                                  sizeof(gcc_cxxdiag_char_table));
2670       if (loc)
2671         {
2672           i = find_char_info_specifier_index (cxxdiag_fci, 'H');
2673           cxxdiag_fci[i].types[0].type = &loc;
2674           cxxdiag_fci[i].pointer_count = 1;
2675         }
2676       if (t)
2677         {
2678           /* All specifiers taking a tree share the same struct.  */
2679           i = find_char_info_specifier_index (cxxdiag_fci, 'D');
2680           cxxdiag_fci[i].types[0].type = &t;
2681           cxxdiag_fci[i].pointer_count = 1;
2682           i = find_char_info_specifier_index (cxxdiag_fci, 'J');
2683           cxxdiag_fci[i].types[0].type = &t;
2684           cxxdiag_fci[i].pointer_count = 1;
2685         }
2686     }
2687 }
2688
2689 /* Handle a "format" attribute; arguments as in
2690    struct attribute_spec.handler.  */
2691 tree
2692 handle_format_attribute (tree *node, tree name ATTRIBUTE_UNUSED, tree args,
2693                          int flags, bool *no_add_attrs)
2694 {
2695   tree type = *node;
2696   function_format_info info;
2697   tree argument;
2698
2699   if (!decode_format_attr (args, &info, 0))
2700     {
2701       *no_add_attrs = true;
2702       return NULL_TREE;
2703     }
2704
2705   argument = TYPE_ARG_TYPES (type);
2706   if (argument)
2707     {
2708       if (!check_format_string (argument, info.format_num, flags,
2709                                 no_add_attrs))
2710         return NULL_TREE;
2711
2712       if (info.first_arg_num != 0)
2713         {
2714           unsigned HOST_WIDE_INT arg_num = 1;
2715
2716           /* Verify that first_arg_num points to the last arg,
2717              the ...  */
2718           while (argument)
2719             arg_num++, argument = TREE_CHAIN (argument);
2720
2721           if (arg_num != info.first_arg_num)
2722             {
2723               if (!(flags & (int) ATTR_FLAG_BUILT_IN))
2724                 error ("args to be formatted is not '...'");
2725               *no_add_attrs = true;
2726               return NULL_TREE;
2727             }
2728         }
2729     }
2730
2731   if (info.format_type == strftime_format_type && info.first_arg_num != 0)
2732     {
2733       error ("strftime formats cannot format arguments");
2734       *no_add_attrs = true;
2735       return NULL_TREE;
2736     }
2737
2738   /* If this is a custom GCC-internal format type, we have to
2739      initialize certain bits a runtime.  */
2740   if (info.format_type == asm_fprintf_format_type
2741       || info.format_type == gcc_diag_format_type
2742       || info.format_type == gcc_cdiag_format_type
2743       || info.format_type == gcc_cxxdiag_format_type)
2744     {
2745       /* Our first time through, we have to make sure that our
2746          format_type data is allocated dynamically and is modifiable.  */
2747       if (!dynamic_format_types)
2748         format_types = dynamic_format_types =
2749           xmemdup (format_types_orig, sizeof (format_types_orig),
2750                    sizeof (format_types_orig));
2751
2752       /* If this is format __asm_fprintf__, we have to initialize
2753          GCC's notion of HOST_WIDE_INT for checking %wd.  */
2754       if (info.format_type == asm_fprintf_format_type)
2755         init_dynamic_asm_fprintf_info();
2756       /* If this is one of the diagnostic attributes, then we have to
2757          initialize `location_t' and `tree' at runtime.  */
2758       else if (info.format_type == gcc_diag_format_type
2759                || info.format_type == gcc_cdiag_format_type
2760                || info.format_type == gcc_cxxdiag_format_type)
2761         init_dynamic_diag_info();
2762       else
2763         abort();
2764     }
2765
2766   return NULL_TREE;
2767 }