OSDN Git Service

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