OSDN Git Service

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