OSDN Git Service

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