OSDN Git Service

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