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