OSDN Git Service

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