OSDN Git Service

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