2003-09-04 Eric Christopher <echristo@redhat.com>
[official-gcc.git] / gcc / c-format.c
blob665b6d1cdc949bca7077a3ee74f63d74bc46026e
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 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "toplev.h"
29 #include "c-common.h"
30 #include "intl.h"
31 #include "diagnostic.h"
32 #include "langhooks.h"
34 /* Set format warning options according to a -Wformat=n option. */
36 void
37 set_Wformat (int setting)
39 warn_format = setting;
40 warn_format_y2k = setting;
41 warn_format_extra_args = setting;
42 warn_format_zero_length = setting;
43 if (setting != 1)
45 warn_format_nonliteral = setting;
46 warn_format_security = setting;
48 /* Make sure not to disable -Wnonnull if -Wformat=0 is specified. */
49 if (setting)
50 warn_nonnull = setting;
54 /* Handle attributes associated with format checking. */
56 /* This must be in the same order as format_types, with format_type_error
57 last. */
58 enum format_type { printf_format_type, asm_fprintf_format_type,
59 gcc_diag_format_type, gcc_cdiag_format_type,
60 gcc_cxxdiag_format_type,
61 scanf_format_type, strftime_format_type,
62 strfmon_format_type, format_type_error };
64 typedef struct function_format_info
66 enum format_type format_type; /* type of format (printf, scanf, etc.) */
67 unsigned HOST_WIDE_INT format_num; /* number of format argument */
68 unsigned HOST_WIDE_INT first_arg_num; /* number of first arg (zero for varargs) */
69 } function_format_info;
71 static bool decode_format_attr (tree, function_format_info *, int);
72 static enum format_type decode_format_type (const char *);
74 static bool check_format_string (tree argument,
75 unsigned HOST_WIDE_INT format_num,
76 int flags, bool *no_add_attrs);
77 static bool get_constant (tree expr, unsigned HOST_WIDE_INT *value,
78 int validated_p);
81 /* Handle a "format_arg" attribute; arguments as in
82 struct attribute_spec.handler. */
83 tree
84 handle_format_arg_attribute (tree *node, tree name ATTRIBUTE_UNUSED,
85 tree args, int flags, bool *no_add_attrs)
87 tree type = *node;
88 tree format_num_expr = TREE_VALUE (args);
89 unsigned HOST_WIDE_INT format_num;
90 tree argument;
92 if (!get_constant (format_num_expr, &format_num, 0))
94 error ("format string has invalid operand number");
95 *no_add_attrs = true;
96 return NULL_TREE;
99 argument = TYPE_ARG_TYPES (type);
100 if (argument)
102 if (!check_format_string (argument, format_num, flags, no_add_attrs))
103 return NULL_TREE;
106 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
107 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
108 != char_type_node))
110 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
111 error ("function does not return string type");
112 *no_add_attrs = true;
113 return NULL_TREE;
116 return NULL_TREE;
119 /* Verify that the format_num argument is actually a string, in case
120 the format attribute is in error. */
121 static bool
122 check_format_string (tree argument, unsigned HOST_WIDE_INT format_num,
123 int flags, bool *no_add_attrs)
125 unsigned HOST_WIDE_INT i;
127 for (i = 1; i != format_num; i++)
129 if (argument == 0)
130 break;
131 argument = TREE_CHAIN (argument);
134 if (!argument
135 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
136 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
137 != char_type_node))
139 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
140 error ("format string arg not a string type");
141 *no_add_attrs = true;
142 return false;
145 return true;
148 /* Strip any conversions from the expression, verify it is a constant,
149 and store its value. If validated_p is true, abort on errors.
150 Returns true on success, false otherwise. */
151 static bool
152 get_constant(tree expr, unsigned HOST_WIDE_INT *value, int validated_p)
154 while (TREE_CODE (expr) == NOP_EXPR
155 || TREE_CODE (expr) == CONVERT_EXPR
156 || TREE_CODE (expr) == NON_LVALUE_EXPR)
157 expr = TREE_OPERAND (expr, 0);
159 if (TREE_CODE (expr) != INTEGER_CST || TREE_INT_CST_HIGH (expr) != 0)
161 if (validated_p)
162 abort ();
163 return false;
166 *value = TREE_INT_CST_LOW (expr);
168 return true;
171 /* Decode the arguments to a "format" attribute into a function_format_info
172 structure. It is already known that the list is of the right length.
173 If VALIDATED_P is true, then these attributes have already been validated
174 and this function will abort if they are erroneous; if false, it
175 will give an error message. Returns true if the attributes are
176 successfully decoded, false otherwise. */
178 static bool
179 decode_format_attr (tree args, function_format_info *info, int validated_p)
181 tree format_type_id = TREE_VALUE (args);
182 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
183 tree first_arg_num_expr
184 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
186 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
188 if (validated_p)
189 abort ();
190 error ("unrecognized format specifier");
191 return false;
193 else
195 const char *p = IDENTIFIER_POINTER (format_type_id);
197 info->format_type = decode_format_type (p);
199 if (info->format_type == format_type_error)
201 if (validated_p)
202 abort ();
203 warning ("`%s' is an unrecognized format function type", p);
204 return false;
208 if (!get_constant (format_num_expr, &info->format_num, validated_p))
210 error ("format string has invalid operand number");
211 return false;
214 if (!get_constant (first_arg_num_expr, &info->first_arg_num, validated_p))
216 error ("'...' has invalid operand number");
217 return false;
220 if (info->first_arg_num != 0 && info->first_arg_num <= info->format_num)
222 if (validated_p)
223 abort ();
224 error ("format string arg follows the args to be formatted");
225 return false;
228 return true;
231 /* Check a call to a format function against a parameter list. */
233 /* The meaningfully distinct length modifiers for format checking recognized
234 by GCC. */
235 enum format_lengths
237 FMT_LEN_none,
238 FMT_LEN_hh,
239 FMT_LEN_h,
240 FMT_LEN_l,
241 FMT_LEN_ll,
242 FMT_LEN_L,
243 FMT_LEN_z,
244 FMT_LEN_t,
245 FMT_LEN_j,
246 FMT_LEN_MAX
250 /* The standard versions in which various format features appeared. */
251 enum format_std_version
253 STD_C89,
254 STD_C94,
255 STD_C9L, /* C99, but treat as C89 if -Wno-long-long. */
256 STD_C99,
257 STD_EXT
260 /* The C standard version C++ is treated as equivalent to
261 or inheriting from, for the purpose of format features supported. */
262 #define CPLUSPLUS_STD_VER STD_C94
263 /* The C standard version we are checking formats against when pedantic. */
264 #define C_STD_VER ((int)(c_dialect_cxx () \
265 ? CPLUSPLUS_STD_VER \
266 : (flag_isoc99 \
267 ? STD_C99 \
268 : (flag_isoc94 ? STD_C94 : STD_C89))))
269 /* The name to give to the standard version we are warning about when
270 pedantic. FEATURE_VER is the version in which the feature warned out
271 appeared, which is higher than C_STD_VER. */
272 #define C_STD_NAME(FEATURE_VER) (c_dialect_cxx () \
273 ? "ISO C++" \
274 : ((FEATURE_VER) == STD_EXT \
275 ? "ISO C" \
276 : "ISO C90"))
277 /* Adjust a C standard version, which may be STD_C9L, to account for
278 -Wno-long-long. Returns other standard versions unchanged. */
279 #define ADJ_STD(VER) ((int)((VER) == STD_C9L \
280 ? (warn_long_long ? STD_C99 : STD_C89) \
281 : (VER)))
283 /* Flags that may apply to a particular kind of format checked by GCC. */
284 enum
286 /* This format converts arguments of types determined by the
287 format string. */
288 FMT_FLAG_ARG_CONVERT = 1,
289 /* The scanf allocation 'a' kludge applies to this format kind. */
290 FMT_FLAG_SCANF_A_KLUDGE = 2,
291 /* A % during parsing a specifier is allowed to be a modified % rather
292 that indicating the format is broken and we are out-of-sync. */
293 FMT_FLAG_FANCY_PERCENT_OK = 4,
294 /* With $ operand numbers, it is OK to reference the same argument more
295 than once. */
296 FMT_FLAG_DOLLAR_MULTIPLE = 8,
297 /* This format type uses $ operand numbers (strfmon doesn't). */
298 FMT_FLAG_USE_DOLLAR = 16,
299 /* Zero width is bad in this type of format (scanf). */
300 FMT_FLAG_ZERO_WIDTH_BAD = 32,
301 /* Empty precision specification is OK in this type of format (printf). */
302 FMT_FLAG_EMPTY_PREC_OK = 64,
303 /* Gaps are allowed in the arguments with $ operand numbers if all
304 arguments are pointers (scanf). */
305 FMT_FLAG_DOLLAR_GAP_POINTER_OK = 128
306 /* Not included here: details of whether width or precision may occur
307 (controlled by width_char and precision_char); details of whether
308 '*' can be used for these (width_type and precision_type); details
309 of whether length modifiers can occur (length_char_specs). */
313 /* Structure describing a length modifier supported in format checking, and
314 possibly a doubled version such as "hh". */
315 typedef struct
317 /* Name of the single-character length modifier. */
318 const char *name;
319 /* Index into a format_char_info.types array. */
320 enum format_lengths index;
321 /* Standard version this length appears in. */
322 enum format_std_version std;
323 /* Same, if the modifier can be repeated, or NULL if it can't. */
324 const char *double_name;
325 enum format_lengths double_index;
326 enum format_std_version double_std;
327 } format_length_info;
330 /* Structure describing the combination of a conversion specifier
331 (or a set of specifiers which act identically) and a length modifier. */
332 typedef struct
334 /* The standard version this combination of length and type appeared in.
335 This is only relevant if greater than those for length and type
336 individually; otherwise it is ignored. */
337 enum format_std_version std;
338 /* The name to use for the type, if different from that generated internally
339 (e.g., "signed size_t"). */
340 const char *name;
341 /* The type itself. */
342 tree *type;
343 } format_type_detail;
346 /* Macros to fill out tables of these. */
347 #define NOARGUMENTS { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }
348 #define BADLEN { 0, NULL, NULL }
349 #define NOLENGTHS { BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }
352 /* Structure describing a format conversion specifier (or a set of specifiers
353 which act identically), and the length modifiers used with it. */
354 typedef struct
356 const char *format_chars;
357 int pointer_count;
358 enum format_std_version std;
359 /* Types accepted for each length modifier. */
360 format_type_detail types[FMT_LEN_MAX];
361 /* List of other modifier characters allowed with these specifiers.
362 This lists flags, and additionally "w" for width, "p" for precision
363 (right precision, for strfmon), "#" for left precision (strfmon),
364 "a" for scanf "a" allocation extension (not applicable in C99 mode),
365 "*" for scanf suppression, and "E" and "O" for those strftime
366 modifiers. */
367 const char *flag_chars;
368 /* List of additional flags describing these conversion specifiers.
369 "c" for generic character pointers being allowed, "2" for strftime
370 two digit year formats, "3" for strftime formats giving two digit
371 years in some locales, "4" for "2" which becomes "3" with an "E" modifier,
372 "o" if use of strftime "O" is a GNU extension beyond C99,
373 "W" if the argument is a pointer which is dereferenced and written into,
374 "R" if the argument is a pointer which is dereferenced and read from,
375 "i" for printf integer formats where the '0' flag is ignored with
376 precision, and "[" for the starting character of a scanf scanset. */
377 const char *flags2;
378 } format_char_info;
381 /* Structure describing a flag accepted by some kind of format. */
382 typedef struct
384 /* The flag character in question (0 for end of array). */
385 int flag_char;
386 /* Zero if this entry describes the flag character in general, or a
387 nonzero character that may be found in flags2 if it describes the
388 flag when used with certain formats only. If the latter, only
389 the first such entry found that applies to the current conversion
390 specifier is used; the values of `name' and `long_name' it supplies
391 will be used, if non-NULL and the standard version is higher than
392 the unpredicated one, for any pedantic warning. For example, 'o'
393 for strftime formats (meaning 'O' is an extension over C99). */
394 int predicate;
395 /* Nonzero if the next character after this flag in the format should
396 be skipped ('=' in strfmon), zero otherwise. */
397 int skip_next_char;
398 /* The name to use for this flag in diagnostic messages. For example,
399 N_("`0' flag"), N_("field width"). */
400 const char *name;
401 /* Long name for this flag in diagnostic messages; currently only used for
402 "ISO C does not support ...". For example, N_("the `I' printf flag"). */
403 const char *long_name;
404 /* The standard version in which it appeared. */
405 enum format_std_version std;
406 } format_flag_spec;
409 /* Structure describing a combination of flags that is bad for some kind
410 of format. */
411 typedef struct
413 /* The first flag character in question (0 for end of array). */
414 int flag_char1;
415 /* The second flag character. */
416 int flag_char2;
417 /* Nonzero if the message should say that the first flag is ignored with
418 the second, zero if the combination should simply be objected to. */
419 int ignored;
420 /* Zero if this entry applies whenever this flag combination occurs,
421 a nonzero character from flags2 if it only applies in some
422 circumstances (e.g. 'i' for printf formats ignoring 0 with precision). */
423 int predicate;
424 } format_flag_pair;
427 /* Structure describing a particular kind of format processed by GCC. */
428 typedef struct
430 /* The name of this kind of format, for use in diagnostics. Also
431 the name of the attribute (without preceding and following __). */
432 const char *name;
433 /* Specifications of the length modifiers accepted; possibly NULL. */
434 const format_length_info *length_char_specs;
435 /* Details of the conversion specification characters accepted. */
436 const format_char_info *conversion_specs;
437 /* String listing the flag characters that are accepted. */
438 const char *flag_chars;
439 /* String listing modifier characters (strftime) accepted. May be NULL. */
440 const char *modifier_chars;
441 /* Details of the flag characters, including pseudo-flags. */
442 const format_flag_spec *flag_specs;
443 /* Details of bad combinations of flags. */
444 const format_flag_pair *bad_flag_pairs;
445 /* Flags applicable to this kind of format. */
446 int flags;
447 /* Flag character to treat a width as, or 0 if width not used. */
448 int width_char;
449 /* Flag character to treat a left precision (strfmon) as,
450 or 0 if left precision not used. */
451 int left_precision_char;
452 /* Flag character to treat a precision (for strfmon, right precision) as,
453 or 0 if precision not used. */
454 int precision_char;
455 /* If a flag character has the effect of suppressing the conversion of
456 an argument ('*' in scanf), that flag character, otherwise 0. */
457 int suppression_char;
458 /* Flag character to treat a length modifier as (ignored if length
459 modifiers not used). Need not be placed in flag_chars for conversion
460 specifiers, but is used to check for bad combinations such as length
461 modifier with assignment suppression in scanf. */
462 int length_code_char;
463 /* Pointer to type of argument expected if '*' is used for a width,
464 or NULL if '*' not used for widths. */
465 tree *width_type;
466 /* Pointer to type of argument expected if '*' is used for a precision,
467 or NULL if '*' not used for precisions. */
468 tree *precision_type;
469 } format_kind_info;
472 /* Structure describing details of a type expected in format checking,
473 and the type to check against it. */
474 typedef struct format_wanted_type
476 /* The type wanted. */
477 tree wanted_type;
478 /* The name of this type to use in diagnostics. */
479 const char *wanted_type_name;
480 /* The level of indirection through pointers at which this type occurs. */
481 int pointer_count;
482 /* Whether, when pointer_count is 1, to allow any character type when
483 pedantic, rather than just the character or void type specified. */
484 int char_lenient_flag;
485 /* Whether the argument, dereferenced once, is written into and so the
486 argument must not be a pointer to a const-qualified type. */
487 int writing_in_flag;
488 /* Whether the argument, dereferenced once, is read from and so
489 must not be a NULL pointer. */
490 int reading_from_flag;
491 /* If warnings should be of the form "field precision is not type int",
492 the name to use (in this case "field precision"), otherwise NULL,
493 for "%s format, %s arg" type messages. If (in an extension), this
494 is a pointer type, wanted_type_name should be set to include the
495 terminating '*' characters of the type name to give a correct
496 message. */
497 const char *name;
498 /* The actual parameter to check against the wanted type. */
499 tree param;
500 /* The argument number of that parameter. */
501 int arg_num;
502 /* The next type to check for this format conversion, or NULL if none. */
503 struct format_wanted_type *next;
504 } format_wanted_type;
507 static const format_length_info printf_length_specs[] =
509 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
510 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L },
511 { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
512 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
513 { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
514 { "Z", FMT_LEN_z, STD_EXT, NULL, 0, 0 },
515 { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
516 { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
517 { NULL, 0, 0, NULL, 0, 0 }
520 /* Length specifiers valid for asm_fprintf. */
521 static const format_length_info asm_fprintf_length_specs[] =
523 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89 },
524 { "w", FMT_LEN_none, STD_C89, NULL, 0, 0 },
525 { NULL, 0, 0, NULL, 0, 0 }
528 /* Length specifiers valid for GCC diagnostics. */
529 static const format_length_info gcc_diag_length_specs[] =
531 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89 },
532 { "w", FMT_LEN_none, STD_C89, NULL, 0, 0 },
533 { NULL, 0, 0, NULL, 0, 0 }
536 /* The custom diagnostics all accept the same length specifiers. */
537 #define gcc_cdiag_length_specs gcc_diag_length_specs
538 #define gcc_cxxdiag_length_specs gcc_diag_length_specs
540 /* This differs from printf_length_specs only in that "Z" is not accepted. */
541 static const format_length_info scanf_length_specs[] =
543 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
544 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L },
545 { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
546 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
547 { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
548 { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
549 { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
550 { NULL, 0, 0, NULL, 0, 0 }
554 /* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings
555 make no sense for a format type not part of any C standard version. */
556 static const format_length_info strfmon_length_specs[] =
558 /* A GNU extension. */
559 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
560 { NULL, 0, 0, NULL, 0, 0 }
563 static const format_flag_spec printf_flag_specs[] =
565 { ' ', 0, 0, N_("` ' flag"), N_("the ` ' printf flag"), STD_C89 },
566 { '+', 0, 0, N_("`+' flag"), N_("the `+' printf flag"), STD_C89 },
567 { '#', 0, 0, N_("`#' flag"), N_("the `#' printf flag"), STD_C89 },
568 { '0', 0, 0, N_("`0' flag"), N_("the `0' printf flag"), STD_C89 },
569 { '-', 0, 0, N_("`-' flag"), N_("the `-' printf flag"), STD_C89 },
570 { '\'', 0, 0, N_("`'' flag"), N_("the `'' printf flag"), STD_EXT },
571 { 'I', 0, 0, N_("`I' flag"), N_("the `I' printf flag"), STD_EXT },
572 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
573 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
574 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
575 { 0, 0, 0, NULL, NULL, 0 }
579 static const format_flag_pair printf_flag_pairs[] =
581 { ' ', '+', 1, 0 },
582 { '0', '-', 1, 0 },
583 { '0', 'p', 1, 'i' },
584 { 0, 0, 0, 0 }
587 static const format_flag_spec asm_fprintf_flag_specs[] =
589 { ' ', 0, 0, N_("` ' flag"), N_("the ` ' printf flag"), STD_C89 },
590 { '+', 0, 0, N_("`+' flag"), N_("the `+' printf flag"), STD_C89 },
591 { '#', 0, 0, N_("`#' flag"), N_("the `#' printf flag"), STD_C89 },
592 { '0', 0, 0, N_("`0' flag"), N_("the `0' printf flag"), STD_C89 },
593 { '-', 0, 0, N_("`-' flag"), N_("the `-' printf flag"), STD_C89 },
594 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
595 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
596 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
597 { 0, 0, 0, NULL, NULL, 0 }
600 static const format_flag_pair asm_fprintf_flag_pairs[] =
602 { ' ', '+', 1, 0 },
603 { '0', '-', 1, 0 },
604 { '0', 'p', 1, 'i' },
605 { 0, 0, 0, 0 }
608 static const format_flag_pair gcc_diag_flag_pairs[] =
610 { 0, 0, 0, 0 }
613 #define gcc_cdiag_flag_pairs gcc_diag_flag_pairs
614 #define gcc_cxxdiag_flag_pairs gcc_diag_flag_pairs
616 static const format_flag_spec gcc_diag_flag_specs[] =
618 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
619 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
620 { 0, 0, 0, NULL, NULL, 0 }
623 #define gcc_cdiag_flag_specs gcc_diag_flag_specs
625 static const format_flag_spec gcc_cxxdiag_flag_specs[] =
627 { '+', 0, 0, N_("`+' flag"), N_("the `+' printf flag"), STD_C89 },
628 { '#', 0, 0, N_("`#' flag"), N_("the `#' printf flag"), STD_C89 },
629 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
630 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
631 { 0, 0, 0, NULL, NULL, 0 }
634 static const format_flag_spec scanf_flag_specs[] =
636 { '*', 0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89 },
637 { 'a', 0, 0, N_("`a' flag"), N_("the `a' scanf flag"), STD_EXT },
638 { 'w', 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89 },
639 { 'L', 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89 },
640 { '\'', 0, 0, N_("`'' flag"), N_("the `'' scanf flag"), STD_EXT },
641 { 'I', 0, 0, N_("`I' flag"), N_("the `I' scanf flag"), STD_EXT },
642 { 0, 0, 0, NULL, NULL, 0 }
646 static const format_flag_pair scanf_flag_pairs[] =
648 { '*', 'L', 0, 0 },
649 { 0, 0, 0, 0 }
653 static const format_flag_spec strftime_flag_specs[] =
655 { '_', 0, 0, N_("`_' flag"), N_("the `_' strftime flag"), STD_EXT },
656 { '-', 0, 0, N_("`-' flag"), N_("the `-' strftime flag"), STD_EXT },
657 { '0', 0, 0, N_("`0' flag"), N_("the `0' strftime flag"), STD_EXT },
658 { '^', 0, 0, N_("`^' flag"), N_("the `^' strftime flag"), STD_EXT },
659 { '#', 0, 0, N_("`#' flag"), N_("the `#' strftime flag"), STD_EXT },
660 { 'w', 0, 0, N_("field width"), N_("field width in strftime format"), STD_EXT },
661 { 'E', 0, 0, N_("`E' modifier"), N_("the `E' strftime modifier"), STD_C99 },
662 { 'O', 0, 0, N_("`O' modifier"), N_("the `O' strftime modifier"), STD_C99 },
663 { 'O', 'o', 0, NULL, N_("the `O' modifier"), STD_EXT },
664 { 0, 0, 0, NULL, NULL, 0 }
668 static const format_flag_pair strftime_flag_pairs[] =
670 { 'E', 'O', 0, 0 },
671 { '_', '-', 0, 0 },
672 { '_', '0', 0, 0 },
673 { '-', '0', 0, 0 },
674 { '^', '#', 0, 0 },
675 { 0, 0, 0, 0 }
679 static const format_flag_spec strfmon_flag_specs[] =
681 { '=', 0, 1, N_("fill character"), N_("fill character in strfmon format"), STD_C89 },
682 { '^', 0, 0, N_("`^' flag"), N_("the `^' strfmon flag"), STD_C89 },
683 { '+', 0, 0, N_("`+' flag"), N_("the `+' strfmon flag"), STD_C89 },
684 { '(', 0, 0, N_("`(' flag"), N_("the `(' strfmon flag"), STD_C89 },
685 { '!', 0, 0, N_("`!' flag"), N_("the `!' strfmon flag"), STD_C89 },
686 { '-', 0, 0, N_("`-' flag"), N_("the `-' strfmon flag"), STD_C89 },
687 { 'w', 0, 0, N_("field width"), N_("field width in strfmon format"), STD_C89 },
688 { '#', 0, 0, N_("left precision"), N_("left precision in strfmon format"), STD_C89 },
689 { 'p', 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89 },
690 { 'L', 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89 },
691 { 0, 0, 0, NULL, NULL, 0 }
694 static const format_flag_pair strfmon_flag_pairs[] =
696 { '+', '(', 0, 0 },
697 { 0, 0, 0, 0 }
701 #define T_I &integer_type_node
702 #define T89_I { STD_C89, NULL, T_I }
703 #define T_L &long_integer_type_node
704 #define T89_L { STD_C89, NULL, T_L }
705 #define T_LL &long_long_integer_type_node
706 #define T9L_LL { STD_C9L, NULL, T_LL }
707 #define TEX_LL { STD_EXT, NULL, T_LL }
708 #define T_S &short_integer_type_node
709 #define T89_S { STD_C89, NULL, T_S }
710 #define T_UI &unsigned_type_node
711 #define T89_UI { STD_C89, NULL, T_UI }
712 #define T_UL &long_unsigned_type_node
713 #define T89_UL { STD_C89, NULL, T_UL }
714 #define T_ULL &long_long_unsigned_type_node
715 #define T9L_ULL { STD_C9L, NULL, T_ULL }
716 #define TEX_ULL { STD_EXT, NULL, T_ULL }
717 #define T_US &short_unsigned_type_node
718 #define T89_US { STD_C89, NULL, T_US }
719 #define T_F &float_type_node
720 #define T89_F { STD_C89, NULL, T_F }
721 #define T99_F { STD_C99, NULL, T_F }
722 #define T_D &double_type_node
723 #define T89_D { STD_C89, NULL, T_D }
724 #define T99_D { STD_C99, NULL, T_D }
725 #define T_LD &long_double_type_node
726 #define T89_LD { STD_C89, NULL, T_LD }
727 #define T99_LD { STD_C99, NULL, T_LD }
728 #define T_C &char_type_node
729 #define T89_C { STD_C89, NULL, T_C }
730 #define T_SC &signed_char_type_node
731 #define T99_SC { STD_C99, NULL, T_SC }
732 #define T_UC &unsigned_char_type_node
733 #define T99_UC { STD_C99, NULL, T_UC }
734 #define T_V &void_type_node
735 #define T89_V { STD_C89, NULL, T_V }
736 #define T_W &wchar_type_node
737 #define T94_W { STD_C94, "wchar_t", T_W }
738 #define TEX_W { STD_EXT, "wchar_t", T_W }
739 #define T_WI &wint_type_node
740 #define T94_WI { STD_C94, "wint_t", T_WI }
741 #define TEX_WI { STD_EXT, "wint_t", T_WI }
742 #define T_ST &size_type_node
743 #define T99_ST { STD_C99, "size_t", T_ST }
744 #define T_SST &signed_size_type_node
745 #define T99_SST { STD_C99, "signed size_t", T_SST }
746 #define T_PD &ptrdiff_type_node
747 #define T99_PD { STD_C99, "ptrdiff_t", T_PD }
748 #define T_UPD &unsigned_ptrdiff_type_node
749 #define T99_UPD { STD_C99, "unsigned ptrdiff_t", T_UPD }
750 #define T_IM &intmax_type_node
751 #define T99_IM { STD_C99, "intmax_t", T_IM }
752 #define T_UIM &uintmax_type_node
753 #define T99_UIM { STD_C99, "uintmax_t", T_UIM }
755 static const format_char_info print_char_table[] =
757 /* C89 conversion specifiers. */
758 { "di", 0, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, TEX_LL, T99_SST, T99_PD, T99_IM }, "-wp0 +'I", "i" },
759 { "oxX", 0, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "-wp0#", "i" },
760 { "u", 0, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "-wp0'I", "i" },
761 { "fgG", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#'", "" },
762 { "eE", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "" },
763 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, T94_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "" },
764 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR" },
765 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "c" },
766 { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, BADLEN, T99_SST, T99_PD, T99_IM }, "", "W" },
767 /* C99 conversion specifiers. */
768 { "F", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#'", "" },
769 { "aA", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "" },
770 /* X/Open conversion specifiers. */
771 { "C", 0, STD_EXT, { TEX_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "" },
772 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "R" },
773 /* GNU conversion specifiers. */
774 { "m", 0, STD_EXT, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "" },
775 { NULL, 0, 0, NOLENGTHS, NULL, NULL }
778 static const format_char_info asm_fprintf_char_table[] =
780 /* C89 conversion specifiers. */
781 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +", "i" },
782 { "oxX", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0#", "i" },
783 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0", "i" },
784 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "" },
785 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR" },
787 /* asm_fprintf conversion specifiers. */
788 { "O", 0, STD_C89, NOARGUMENTS, "", "" },
789 { "R", 0, STD_C89, NOARGUMENTS, "", "" },
790 { "I", 0, STD_C89, NOARGUMENTS, "", "" },
791 { "L", 0, STD_C89, NOARGUMENTS, "", "" },
792 { "U", 0, STD_C89, NOARGUMENTS, "", "" },
793 { "r", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "" },
794 { "@", 0, STD_C89, NOARGUMENTS, "", "" },
795 { NULL, 0, 0, NOLENGTHS, NULL, NULL }
798 static const format_char_info gcc_diag_char_table[] =
800 /* C89 conversion specifiers. */
801 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "", "" },
802 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "", "" },
803 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "", "" },
804 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "" },
805 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "p", "cR" },
806 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "c" },
808 /* Custom conversion specifiers. */
810 /* %H will require "location_t" at runtime. */
811 { "H", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "" },
813 { "m", 0, STD_C89, NOARGUMENTS, "", "" },
814 { NULL, 0, 0, NOLENGTHS, NULL, NULL }
817 static const format_char_info gcc_cdiag_char_table[] =
819 /* C89 conversion specifiers. */
820 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "", "" },
821 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "", "" },
822 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "", "" },
823 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "" },
824 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "p", "cR" },
825 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "c" },
827 /* Custom conversion specifiers. */
829 /* %H will require "location_t" at runtime. */
830 { "H", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "" },
832 /* These will require a "tree" at runtime. */
833 { "DEFT", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "" },
835 { "m", 0, STD_C89, NOARGUMENTS, "", "" },
836 { NULL, 0, 0, NOLENGTHS, NULL, NULL }
839 static const format_char_info gcc_cxxdiag_char_table[] =
841 /* C89 conversion specifiers. */
842 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "", "" },
843 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "", "" },
844 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "", "" },
845 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "" },
846 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "p", "cR" },
847 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "c" },
849 /* Custom conversion specifiers. */
851 /* %H will require "location_t" at runtime. */
852 { "H", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "" },
854 /* These will require a "tree" at runtime. */
855 { "ADEFTV",0,STD_C89,{ T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "+#", "" },
857 /* These accept either an `int' or an `enum tree_code' (which is handled as an `int'.) */
858 { "CLOPQ",0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "" },
860 { "m", 0, STD_C89, NOARGUMENTS, "", "" },
861 { NULL, 0, 0, NOLENGTHS, NULL, NULL }
864 static const format_char_info scan_char_table[] =
866 /* C89 conversion specifiers. */
867 { "di", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, TEX_LL, T99_SST, T99_PD, T99_IM }, "*w'I", "W" },
868 { "u", 1, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "*w'I", "W" },
869 { "oxX", 1, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "*w", "W" },
870 { "efgEG", 1, STD_C89, { T89_F, BADLEN, BADLEN, T89_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "*w'", "W" },
871 { "c", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "cW" },
872 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "cW" },
873 { "[", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "cW[" },
874 { "p", 2, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W" },
875 { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, BADLEN, T99_SST, T99_PD, T99_IM }, "", "W" },
876 /* C99 conversion specifiers. */
877 { "FaA", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "*w'", "W" },
878 /* X/Open conversion specifiers. */
879 { "C", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W" },
880 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "W" },
881 { NULL, 0, 0, NOLENGTHS, NULL, NULL }
884 static const format_char_info time_char_table[] =
886 /* C89 conversion specifiers. */
887 { "ABZab", 0, STD_C89, NOLENGTHS, "^#", "" },
888 { "cx", 0, STD_C89, NOLENGTHS, "E", "3" },
889 { "HIMSUWdmw", 0, STD_C89, NOLENGTHS, "-_0Ow", "" },
890 { "j", 0, STD_C89, NOLENGTHS, "-_0Ow", "o" },
891 { "p", 0, STD_C89, NOLENGTHS, "#", "" },
892 { "X", 0, STD_C89, NOLENGTHS, "E", "" },
893 { "y", 0, STD_C89, NOLENGTHS, "EO-_0w", "4" },
894 { "Y", 0, STD_C89, NOLENGTHS, "-_0EOw", "o" },
895 { "%", 0, STD_C89, NOLENGTHS, "", "" },
896 /* C99 conversion specifiers. */
897 { "C", 0, STD_C99, NOLENGTHS, "-_0EOw", "o" },
898 { "D", 0, STD_C99, NOLENGTHS, "", "2" },
899 { "eVu", 0, STD_C99, NOLENGTHS, "-_0Ow", "" },
900 { "FRTnrt", 0, STD_C99, NOLENGTHS, "", "" },
901 { "g", 0, STD_C99, NOLENGTHS, "O-_0w", "2o" },
902 { "G", 0, STD_C99, NOLENGTHS, "-_0Ow", "o" },
903 { "h", 0, STD_C99, NOLENGTHS, "^#", "" },
904 { "z", 0, STD_C99, NOLENGTHS, "O", "o" },
905 /* GNU conversion specifiers. */
906 { "kls", 0, STD_EXT, NOLENGTHS, "-_0Ow", "" },
907 { "P", 0, STD_EXT, NOLENGTHS, "", "" },
908 { NULL, 0, 0, NOLENGTHS, NULL, NULL }
911 static const format_char_info monetary_char_table[] =
913 { "in", 0, STD_C89, { T89_D, BADLEN, BADLEN, BADLEN, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "=^+(!-w#p", "" },
914 { NULL, 0, 0, NOLENGTHS, NULL, NULL }
918 /* This must be in the same order as enum format_type. */
919 static const format_kind_info format_types_orig[] =
921 { "printf", printf_length_specs, print_char_table, " +#0-'I", NULL,
922 printf_flag_specs, printf_flag_pairs,
923 FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_EMPTY_PREC_OK,
924 'w', 0, 'p', 0, 'L',
925 &integer_type_node, &integer_type_node
927 { "asm_fprintf", asm_fprintf_length_specs, asm_fprintf_char_table, " +#0-", NULL,
928 asm_fprintf_flag_specs, asm_fprintf_flag_pairs,
929 FMT_FLAG_ARG_CONVERT|FMT_FLAG_EMPTY_PREC_OK,
930 'w', 0, 'p', 0, 'L',
931 NULL, NULL
933 { "gcc_diag", gcc_diag_length_specs, gcc_diag_char_table, "", NULL,
934 gcc_diag_flag_specs, gcc_diag_flag_pairs,
935 FMT_FLAG_ARG_CONVERT,
936 0, 0, 'p', 0, 'L',
937 NULL, &integer_type_node
939 { "gcc_cdiag", gcc_cdiag_length_specs, gcc_cdiag_char_table, "", NULL,
940 gcc_cdiag_flag_specs, gcc_cdiag_flag_pairs,
941 FMT_FLAG_ARG_CONVERT,
942 0, 0, 'p', 0, 'L',
943 NULL, &integer_type_node
945 { "gcc_cxxdiag", gcc_cxxdiag_length_specs, gcc_cxxdiag_char_table, "+#", NULL,
946 gcc_cxxdiag_flag_specs, gcc_cxxdiag_flag_pairs,
947 FMT_FLAG_ARG_CONVERT,
948 0, 0, 'p', 0, 'L',
949 NULL, &integer_type_node
951 { "scanf", scanf_length_specs, scan_char_table, "*'I", NULL,
952 scanf_flag_specs, scanf_flag_pairs,
953 FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_ZERO_WIDTH_BAD|FMT_FLAG_DOLLAR_GAP_POINTER_OK,
954 'w', 0, 0, '*', 'L',
955 NULL, NULL
957 { "strftime", NULL, time_char_table, "_-0^#", "EO",
958 strftime_flag_specs, strftime_flag_pairs,
959 FMT_FLAG_FANCY_PERCENT_OK, 'w', 0, 0, 0, 0,
960 NULL, NULL
962 { "strfmon", strfmon_length_specs, monetary_char_table, "=^+(!-", NULL,
963 strfmon_flag_specs, strfmon_flag_pairs,
964 FMT_FLAG_ARG_CONVERT, 'w', '#', 'p', 0, 'L',
965 NULL, NULL
969 /* This layer of indirection allows GCC to reassign format_types with
970 new data if necessary, while still allowing the original data to be
971 const. */
972 static const format_kind_info *format_types = format_types_orig;
973 /* We can modify this one. */
974 static format_kind_info *dynamic_format_types;
976 /* Structure detailing the results of checking a format function call
977 where the format expression may be a conditional expression with
978 many leaves resulting from nested conditional expressions. */
979 typedef struct
981 /* Number of leaves of the format argument that could not be checked
982 as they were not string literals. */
983 int number_non_literal;
984 /* Number of leaves of the format argument that were null pointers or
985 string literals, but had extra format arguments. */
986 int number_extra_args;
987 /* Number of leaves of the format argument that were null pointers or
988 string literals, but had extra format arguments and used $ operand
989 numbers. */
990 int number_dollar_extra_args;
991 /* Number of leaves of the format argument that were wide string
992 literals. */
993 int number_wide;
994 /* Number of leaves of the format argument that were empty strings. */
995 int number_empty;
996 /* Number of leaves of the format argument that were unterminated
997 strings. */
998 int number_unterminated;
999 /* Number of leaves of the format argument that were not counted above. */
1000 int number_other;
1001 } format_check_results;
1003 typedef struct
1005 format_check_results *res;
1006 function_format_info *info;
1007 tree params;
1008 int *status;
1009 } format_check_context;
1011 static void check_format_info (int *, function_format_info *, tree);
1012 static void check_format_arg (void *, tree, unsigned HOST_WIDE_INT);
1013 static void check_format_info_main (int *, format_check_results *,
1014 function_format_info *,
1015 const char *, int, tree,
1016 unsigned HOST_WIDE_INT);
1017 static void status_warning (int *, const char *, ...)
1018 ATTRIBUTE_PRINTF_2;
1020 static void init_dollar_format_checking (int, tree);
1021 static int maybe_read_dollar_number (int *, const char **, int,
1022 tree, tree *, const format_kind_info *);
1023 static void finish_dollar_format_checking (int *, format_check_results *, int);
1025 static const format_flag_spec *get_flag_spec (const format_flag_spec *,
1026 int, const char *);
1028 static void check_format_types (int *, format_wanted_type *);
1030 /* Decode a format type from a string, returning the type, or
1031 format_type_error if not valid, in which case the caller should print an
1032 error message. */
1033 static enum format_type
1034 decode_format_type (const char *s)
1036 int i;
1037 int slen;
1038 slen = strlen (s);
1039 for (i = 0; i < (int) format_type_error; i++)
1041 int alen;
1042 if (!strcmp (s, format_types[i].name))
1043 break;
1044 alen = strlen (format_types[i].name);
1045 if (slen == alen + 4 && s[0] == '_' && s[1] == '_'
1046 && s[slen - 1] == '_' && s[slen - 2] == '_'
1047 && !strncmp (s + 2, format_types[i].name, alen))
1048 break;
1050 return ((enum format_type) i);
1054 /* Check the argument list of a call to printf, scanf, etc.
1055 ATTRS are the attributes on the function type.
1056 PARAMS is the list of argument values. Also, if -Wmissing-format-attribute,
1057 warn for calls to vprintf or vscanf in functions with no such format
1058 attribute themselves. */
1060 void
1061 check_function_format (int *status, tree attrs, tree params)
1063 tree a;
1065 /* See if this function has any format attributes. */
1066 for (a = attrs; a; a = TREE_CHAIN (a))
1068 if (is_attribute_p ("format", TREE_PURPOSE (a)))
1070 /* Yup; check it. */
1071 function_format_info info;
1072 decode_format_attr (TREE_VALUE (a), &info, 1);
1073 check_format_info (status, &info, params);
1074 if (warn_missing_format_attribute && info.first_arg_num == 0
1075 && (format_types[info.format_type].flags
1076 & (int) FMT_FLAG_ARG_CONVERT))
1078 tree c;
1079 for (c = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
1081 c = TREE_CHAIN (c))
1082 if (is_attribute_p ("format", TREE_PURPOSE (c))
1083 && (decode_format_type (IDENTIFIER_POINTER
1084 (TREE_VALUE (TREE_VALUE (c))))
1085 == info.format_type))
1086 break;
1087 if (c == NULL_TREE)
1089 /* Check if the current function has a parameter to which
1090 the format attribute could be attached; if not, it
1091 can't be a candidate for a format attribute, despite
1092 the vprintf-like or vscanf-like call. */
1093 tree args;
1094 for (args = DECL_ARGUMENTS (current_function_decl);
1095 args != 0;
1096 args = TREE_CHAIN (args))
1098 if (TREE_CODE (TREE_TYPE (args)) == POINTER_TYPE
1099 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args)))
1100 == char_type_node))
1101 break;
1103 if (args != 0)
1104 warning ("function might be possible candidate for `%s' format attribute",
1105 format_types[info.format_type].name);
1112 /* This function replaces `warning' inside the printf format checking
1113 functions. If the `status' parameter is non-NULL, then it is
1114 dereferenced and set to 1 whenever a warning is caught. Otherwise
1115 it warns as usual by replicating the innards of the warning
1116 function from diagnostic.c. */
1117 static void
1118 status_warning (int *status, const char *msgid, ...)
1120 diagnostic_info diagnostic ;
1121 va_list ap;
1123 va_start (ap, msgid);
1125 if (status)
1126 *status = 1;
1127 else
1129 /* This duplicates the warning function behavior. */
1130 diagnostic_set_info (&diagnostic, _(msgid), &ap,
1131 input_location, DK_WARNING);
1132 report_diagnostic (&diagnostic);
1135 va_end (ap);
1138 /* Variables used by the checking of $ operand number formats. */
1139 static char *dollar_arguments_used = NULL;
1140 static char *dollar_arguments_pointer_p = NULL;
1141 static int dollar_arguments_alloc = 0;
1142 static int dollar_arguments_count;
1143 static int dollar_first_arg_num;
1144 static int dollar_max_arg_used;
1145 static int dollar_format_warned;
1147 /* Initialize the checking for a format string that may contain $
1148 parameter number specifications; we will need to keep track of whether
1149 each parameter has been used. FIRST_ARG_NUM is the number of the first
1150 argument that is a parameter to the format, or 0 for a vprintf-style
1151 function; PARAMS is the list of arguments starting at this argument. */
1153 static void
1154 init_dollar_format_checking (int first_arg_num, tree params)
1156 tree oparams = params;
1158 dollar_first_arg_num = first_arg_num;
1159 dollar_arguments_count = 0;
1160 dollar_max_arg_used = 0;
1161 dollar_format_warned = 0;
1162 if (first_arg_num > 0)
1164 while (params)
1166 dollar_arguments_count++;
1167 params = TREE_CHAIN (params);
1170 if (dollar_arguments_alloc < dollar_arguments_count)
1172 if (dollar_arguments_used)
1173 free (dollar_arguments_used);
1174 if (dollar_arguments_pointer_p)
1175 free (dollar_arguments_pointer_p);
1176 dollar_arguments_alloc = dollar_arguments_count;
1177 dollar_arguments_used = xmalloc (dollar_arguments_alloc);
1178 dollar_arguments_pointer_p = xmalloc (dollar_arguments_alloc);
1180 if (dollar_arguments_alloc)
1182 memset (dollar_arguments_used, 0, dollar_arguments_alloc);
1183 if (first_arg_num > 0)
1185 int i = 0;
1186 params = oparams;
1187 while (params)
1189 dollar_arguments_pointer_p[i] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params)))
1190 == POINTER_TYPE);
1191 params = TREE_CHAIN (params);
1192 i++;
1199 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
1200 is set, it is an error if one is not found; otherwise, it is OK. If
1201 such a number is found, check whether it is within range and mark that
1202 numbered operand as being used for later checking. Returns the operand
1203 number if found and within range, zero if no such number was found and
1204 this is OK, or -1 on error. PARAMS points to the first operand of the
1205 format; PARAM_PTR is made to point to the parameter referred to. If
1206 a $ format is found, *FORMAT is updated to point just after it. */
1208 static int
1209 maybe_read_dollar_number (int *status, const char **format,
1210 int dollar_needed, tree params, tree *param_ptr,
1211 const format_kind_info *fki)
1213 int argnum;
1214 int overflow_flag;
1215 const char *fcp = *format;
1216 if (! ISDIGIT (*fcp))
1218 if (dollar_needed)
1220 status_warning (status, "missing $ operand number in format");
1221 return -1;
1223 else
1224 return 0;
1226 argnum = 0;
1227 overflow_flag = 0;
1228 while (ISDIGIT (*fcp))
1230 int nargnum;
1231 nargnum = 10 * argnum + (*fcp - '0');
1232 if (nargnum < 0 || nargnum / 10 != argnum)
1233 overflow_flag = 1;
1234 argnum = nargnum;
1235 fcp++;
1237 if (*fcp != '$')
1239 if (dollar_needed)
1241 status_warning (status, "missing $ operand number in format");
1242 return -1;
1244 else
1245 return 0;
1247 *format = fcp + 1;
1248 if (pedantic && !dollar_format_warned)
1250 status_warning (status,
1251 "%s does not support %%n$ operand number formats",
1252 C_STD_NAME (STD_EXT));
1253 dollar_format_warned = 1;
1255 if (overflow_flag || argnum == 0
1256 || (dollar_first_arg_num && argnum > dollar_arguments_count))
1258 status_warning (status, "operand number out of range in format");
1259 return -1;
1261 if (argnum > dollar_max_arg_used)
1262 dollar_max_arg_used = argnum;
1263 /* For vprintf-style functions we may need to allocate more memory to
1264 track which arguments are used. */
1265 while (dollar_arguments_alloc < dollar_max_arg_used)
1267 int nalloc;
1268 nalloc = 2 * dollar_arguments_alloc + 16;
1269 dollar_arguments_used = xrealloc (dollar_arguments_used, nalloc);
1270 dollar_arguments_pointer_p = xrealloc (dollar_arguments_pointer_p,
1271 nalloc);
1272 memset (dollar_arguments_used + dollar_arguments_alloc, 0,
1273 nalloc - dollar_arguments_alloc);
1274 dollar_arguments_alloc = nalloc;
1276 if (!(fki->flags & (int) FMT_FLAG_DOLLAR_MULTIPLE)
1277 && dollar_arguments_used[argnum - 1] == 1)
1279 dollar_arguments_used[argnum - 1] = 2;
1280 status_warning (status,
1281 "format argument %d used more than once in %s format",
1282 argnum, fki->name);
1284 else
1285 dollar_arguments_used[argnum - 1] = 1;
1286 if (dollar_first_arg_num)
1288 int i;
1289 *param_ptr = params;
1290 for (i = 1; i < argnum && *param_ptr != 0; i++)
1291 *param_ptr = TREE_CHAIN (*param_ptr);
1293 if (*param_ptr == 0)
1295 /* This case shouldn't be caught here. */
1296 abort ();
1299 else
1300 *param_ptr = 0;
1301 return argnum;
1305 /* Finish the checking for a format string that used $ operand number formats
1306 instead of non-$ formats. We check for unused operands before used ones
1307 (a serious error, since the implementation of the format function
1308 can't know what types to pass to va_arg to find the later arguments).
1309 and for unused operands at the end of the format (if we know how many
1310 arguments the format had, so not for vprintf). If there were operand
1311 numbers out of range on a non-vprintf-style format, we won't have reached
1312 here. If POINTER_GAP_OK, unused arguments are OK if all arguments are
1313 pointers. */
1315 static void
1316 finish_dollar_format_checking (int *status, format_check_results *res, int pointer_gap_ok)
1318 int i;
1319 bool found_pointer_gap = false;
1320 for (i = 0; i < dollar_max_arg_used; i++)
1322 if (!dollar_arguments_used[i])
1324 if (pointer_gap_ok && (dollar_first_arg_num == 0
1325 || dollar_arguments_pointer_p[i]))
1326 found_pointer_gap = true;
1327 else
1328 status_warning (status, "format argument %d unused before used argument %d in $-style format",
1329 i + 1, dollar_max_arg_used);
1332 if (found_pointer_gap
1333 || (dollar_first_arg_num
1334 && dollar_max_arg_used < dollar_arguments_count))
1336 res->number_other--;
1337 res->number_dollar_extra_args++;
1342 /* Retrieve the specification for a format flag. SPEC contains the
1343 specifications for format flags for the applicable kind of format.
1344 FLAG is the flag in question. If PREDICATES is NULL, the basic
1345 spec for that flag must be retrieved and this function aborts if
1346 it cannot be found. If PREDICATES is not NULL, it is a string listing
1347 possible predicates for the spec entry; if an entry predicated on any
1348 of these is found, it is returned, otherwise NULL is returned. */
1350 static const format_flag_spec *
1351 get_flag_spec (const format_flag_spec *spec, int flag, const char *predicates)
1353 int i;
1354 for (i = 0; spec[i].flag_char != 0; i++)
1356 if (spec[i].flag_char != flag)
1357 continue;
1358 if (predicates != NULL)
1360 if (spec[i].predicate != 0
1361 && strchr (predicates, spec[i].predicate) != 0)
1362 return &spec[i];
1364 else if (spec[i].predicate == 0)
1365 return &spec[i];
1367 if (predicates == NULL)
1368 abort ();
1369 else
1370 return NULL;
1374 /* Check the argument list of a call to printf, scanf, etc.
1375 INFO points to the function_format_info structure.
1376 PARAMS is the list of argument values. */
1378 static void
1379 check_format_info (int *status, function_format_info *info, tree params)
1381 format_check_context format_ctx;
1382 unsigned HOST_WIDE_INT arg_num;
1383 tree format_tree;
1384 format_check_results res;
1385 /* Skip to format argument. If the argument isn't available, there's
1386 no work for us to do; prototype checking will catch the problem. */
1387 for (arg_num = 1; ; ++arg_num)
1389 if (params == 0)
1390 return;
1391 if (arg_num == info->format_num)
1392 break;
1393 params = TREE_CHAIN (params);
1395 format_tree = TREE_VALUE (params);
1396 params = TREE_CHAIN (params);
1397 if (format_tree == 0)
1398 return;
1400 res.number_non_literal = 0;
1401 res.number_extra_args = 0;
1402 res.number_dollar_extra_args = 0;
1403 res.number_wide = 0;
1404 res.number_empty = 0;
1405 res.number_unterminated = 0;
1406 res.number_other = 0;
1408 format_ctx.res = &res;
1409 format_ctx.info = info;
1410 format_ctx.params = params;
1411 format_ctx.status = status;
1413 check_function_arguments_recurse (check_format_arg, &format_ctx,
1414 format_tree, arg_num);
1416 if (res.number_non_literal > 0)
1418 /* Functions taking a va_list normally pass a non-literal format
1419 string. These functions typically are declared with
1420 first_arg_num == 0, so avoid warning in those cases. */
1421 if (!(format_types[info->format_type].flags & (int) FMT_FLAG_ARG_CONVERT))
1423 /* For strftime-like formats, warn for not checking the format
1424 string; but there are no arguments to check. */
1425 if (warn_format_nonliteral)
1426 status_warning (status, "format not a string literal, format string not checked");
1428 else if (info->first_arg_num != 0)
1430 /* If there are no arguments for the format at all, we may have
1431 printf (foo) which is likely to be a security hole. */
1432 while (arg_num + 1 < info->first_arg_num)
1434 if (params == 0)
1435 break;
1436 params = TREE_CHAIN (params);
1437 ++arg_num;
1439 if (params == 0 && (warn_format_nonliteral || warn_format_security))
1440 status_warning (status, "format not a string literal and no format arguments");
1441 else if (warn_format_nonliteral)
1442 status_warning (status, "format not a string literal, argument types not checked");
1446 /* If there were extra arguments to the format, normally warn. However,
1447 the standard does say extra arguments are ignored, so in the specific
1448 case where we have multiple leaves (conditional expressions or
1449 ngettext) allow extra arguments if at least one leaf didn't have extra
1450 arguments, but was otherwise OK (either non-literal or checked OK).
1451 If the format is an empty string, this should be counted similarly to the
1452 case of extra format arguments. */
1453 if (res.number_extra_args > 0 && res.number_non_literal == 0
1454 && res.number_other == 0 && warn_format_extra_args)
1455 status_warning (status, "too many arguments for format");
1456 if (res.number_dollar_extra_args > 0 && res.number_non_literal == 0
1457 && res.number_other == 0 && warn_format_extra_args)
1458 status_warning (status, "unused arguments in $-style format");
1459 if (res.number_empty > 0 && res.number_non_literal == 0
1460 && res.number_other == 0 && warn_format_zero_length)
1461 status_warning (status, "zero-length %s format string",
1462 format_types[info->format_type].name);
1464 if (res.number_wide > 0)
1465 status_warning (status, "format is a wide character string");
1467 if (res.number_unterminated > 0)
1468 status_warning (status, "unterminated format string");
1471 /* Callback from check_function_arguments_recurse to check a
1472 format string. FORMAT_TREE is the format parameter. ARG_NUM
1473 is the number of the format argument. CTX points to a
1474 format_check_context. */
1476 static void
1477 check_format_arg (void *ctx, tree format_tree,
1478 unsigned HOST_WIDE_INT arg_num)
1480 format_check_context *format_ctx = ctx;
1481 format_check_results *res = format_ctx->res;
1482 function_format_info *info = format_ctx->info;
1483 tree params = format_ctx->params;
1484 int *status = format_ctx->status;
1486 int format_length;
1487 HOST_WIDE_INT offset;
1488 const char *format_chars;
1489 tree array_size = 0;
1490 tree array_init;
1492 if (integer_zerop (format_tree))
1494 /* Skip to first argument to check, so we can see if this format
1495 has any arguments (it shouldn't). */
1496 while (arg_num + 1 < info->first_arg_num)
1498 if (params == 0)
1499 return;
1500 params = TREE_CHAIN (params);
1501 ++arg_num;
1504 if (params == 0)
1505 res->number_other++;
1506 else
1507 res->number_extra_args++;
1509 return;
1512 offset = 0;
1513 if (TREE_CODE (format_tree) == PLUS_EXPR)
1515 tree arg0, arg1;
1517 arg0 = TREE_OPERAND (format_tree, 0);
1518 arg1 = TREE_OPERAND (format_tree, 1);
1519 STRIP_NOPS (arg0);
1520 STRIP_NOPS (arg1);
1521 if (TREE_CODE (arg1) == INTEGER_CST)
1522 format_tree = arg0;
1523 else if (TREE_CODE (arg0) == INTEGER_CST)
1525 format_tree = arg1;
1526 arg1 = arg0;
1528 else
1530 res->number_non_literal++;
1531 return;
1533 if (!host_integerp (arg1, 0)
1534 || (offset = tree_low_cst (arg1, 0)) < 0)
1536 res->number_non_literal++;
1537 return;
1540 if (TREE_CODE (format_tree) != ADDR_EXPR)
1542 res->number_non_literal++;
1543 return;
1545 format_tree = TREE_OPERAND (format_tree, 0);
1546 if (TREE_CODE (format_tree) == VAR_DECL
1547 && TREE_CODE (TREE_TYPE (format_tree)) == ARRAY_TYPE
1548 && (array_init = decl_constant_value (format_tree)) != format_tree
1549 && TREE_CODE (array_init) == STRING_CST)
1551 /* Extract the string constant initializer. Note that this may include
1552 a trailing NUL character that is not in the array (e.g.
1553 const char a[3] = "foo";). */
1554 array_size = DECL_SIZE_UNIT (format_tree);
1555 format_tree = array_init;
1557 if (TREE_CODE (format_tree) != STRING_CST)
1559 res->number_non_literal++;
1560 return;
1562 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree))) != char_type_node)
1564 res->number_wide++;
1565 return;
1567 format_chars = TREE_STRING_POINTER (format_tree);
1568 format_length = TREE_STRING_LENGTH (format_tree);
1569 if (array_size != 0)
1571 /* Variable length arrays can't be initialized. */
1572 if (TREE_CODE (array_size) != INTEGER_CST)
1573 abort ();
1574 if (host_integerp (array_size, 0))
1576 HOST_WIDE_INT array_size_value = TREE_INT_CST_LOW (array_size);
1577 if (array_size_value > 0
1578 && array_size_value == (int) array_size_value
1579 && format_length > array_size_value)
1580 format_length = array_size_value;
1583 if (offset)
1585 if (offset >= format_length)
1587 res->number_non_literal++;
1588 return;
1590 format_chars += offset;
1591 format_length -= offset;
1593 if (format_length < 1)
1595 res->number_unterminated++;
1596 return;
1598 if (format_length == 1)
1600 res->number_empty++;
1601 return;
1603 if (format_chars[--format_length] != 0)
1605 res->number_unterminated++;
1606 return;
1609 /* Skip to first argument to check. */
1610 while (arg_num + 1 < info->first_arg_num)
1612 if (params == 0)
1613 return;
1614 params = TREE_CHAIN (params);
1615 ++arg_num;
1617 /* Provisionally increment res->number_other; check_format_info_main
1618 will decrement it if it finds there are extra arguments, but this way
1619 need not adjust it for every return. */
1620 res->number_other++;
1621 check_format_info_main (status, res, info, format_chars, format_length,
1622 params, arg_num);
1626 /* Do the main part of checking a call to a format function. FORMAT_CHARS
1627 is the NUL-terminated format string (which at this point may contain
1628 internal NUL characters); FORMAT_LENGTH is its length (excluding the
1629 terminating NUL character). ARG_NUM is one less than the number of
1630 the first format argument to check; PARAMS points to that format
1631 argument in the list of arguments. */
1633 static void
1634 check_format_info_main (int *status, format_check_results *res,
1635 function_format_info *info, const char *format_chars,
1636 int format_length, tree params,
1637 unsigned HOST_WIDE_INT arg_num)
1639 const char *orig_format_chars = format_chars;
1640 tree first_fillin_param = params;
1642 const format_kind_info *fki = &format_types[info->format_type];
1643 const format_flag_spec *flag_specs = fki->flag_specs;
1644 const format_flag_pair *bad_flag_pairs = fki->bad_flag_pairs;
1646 /* -1 if no conversions taking an operand have been found; 0 if one has
1647 and it didn't use $; 1 if $ formats are in use. */
1648 int has_operand_number = -1;
1650 init_dollar_format_checking (info->first_arg_num, first_fillin_param);
1652 while (1)
1654 int i;
1655 int suppressed = FALSE;
1656 const char *length_chars = NULL;
1657 enum format_lengths length_chars_val = FMT_LEN_none;
1658 enum format_std_version length_chars_std = STD_C89;
1659 int format_char;
1660 tree cur_param;
1661 tree wanted_type;
1662 int main_arg_num = 0;
1663 tree main_arg_params = 0;
1664 enum format_std_version wanted_type_std;
1665 const char *wanted_type_name;
1666 format_wanted_type width_wanted_type;
1667 format_wanted_type precision_wanted_type;
1668 format_wanted_type main_wanted_type;
1669 format_wanted_type *first_wanted_type = NULL;
1670 format_wanted_type *last_wanted_type = NULL;
1671 const format_length_info *fli = NULL;
1672 const format_char_info *fci = NULL;
1673 char flag_chars[256];
1674 int aflag = 0;
1675 if (*format_chars == 0)
1677 if (format_chars - orig_format_chars != format_length)
1678 status_warning (status, "embedded `\\0' in format");
1679 if (info->first_arg_num != 0 && params != 0
1680 && has_operand_number <= 0)
1682 res->number_other--;
1683 res->number_extra_args++;
1685 if (has_operand_number > 0)
1686 finish_dollar_format_checking (status, res, fki->flags & (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK);
1687 return;
1689 if (*format_chars++ != '%')
1690 continue;
1691 if (*format_chars == 0)
1693 status_warning (status, "spurious trailing `%%' in format");
1694 continue;
1696 if (*format_chars == '%')
1698 ++format_chars;
1699 continue;
1701 flag_chars[0] = 0;
1703 if ((fki->flags & (int) FMT_FLAG_USE_DOLLAR) && has_operand_number != 0)
1705 /* Possibly read a $ operand number at the start of the format.
1706 If one was previously used, one is required here. If one
1707 is not used here, we can't immediately conclude this is a
1708 format without them, since it could be printf %m or scanf %*. */
1709 int opnum;
1710 opnum = maybe_read_dollar_number (status, &format_chars, 0,
1711 first_fillin_param,
1712 &main_arg_params, fki);
1713 if (opnum == -1)
1714 return;
1715 else if (opnum > 0)
1717 has_operand_number = 1;
1718 main_arg_num = opnum + info->first_arg_num - 1;
1722 /* Read any format flags, but do not yet validate them beyond removing
1723 duplicates, since in general validation depends on the rest of
1724 the format. */
1725 while (*format_chars != 0
1726 && strchr (fki->flag_chars, *format_chars) != 0)
1728 const format_flag_spec *s = get_flag_spec (flag_specs,
1729 *format_chars, NULL);
1730 if (strchr (flag_chars, *format_chars) != 0)
1732 status_warning (status, "repeated %s in format", _(s->name));
1734 else
1736 i = strlen (flag_chars);
1737 flag_chars[i++] = *format_chars;
1738 flag_chars[i] = 0;
1740 if (s->skip_next_char)
1742 ++format_chars;
1743 if (*format_chars == 0)
1745 status_warning (status, "missing fill character at end of strfmon format");
1746 return;
1749 ++format_chars;
1752 /* Read any format width, possibly * or *m$. */
1753 if (fki->width_char != 0)
1755 if (fki->width_type != NULL && *format_chars == '*')
1757 i = strlen (flag_chars);
1758 flag_chars[i++] = fki->width_char;
1759 flag_chars[i] = 0;
1760 /* "...a field width...may be indicated by an asterisk.
1761 In this case, an int argument supplies the field width..." */
1762 ++format_chars;
1763 if (has_operand_number != 0)
1765 int opnum;
1766 opnum = maybe_read_dollar_number (status, &format_chars,
1767 has_operand_number == 1,
1768 first_fillin_param,
1769 &params, fki);
1770 if (opnum == -1)
1771 return;
1772 else if (opnum > 0)
1774 has_operand_number = 1;
1775 arg_num = opnum + info->first_arg_num - 1;
1777 else
1778 has_operand_number = 0;
1780 if (info->first_arg_num != 0)
1782 if (params == 0)
1784 status_warning (status, "too few arguments for format");
1785 return;
1787 cur_param = TREE_VALUE (params);
1788 if (has_operand_number <= 0)
1790 params = TREE_CHAIN (params);
1791 ++arg_num;
1793 width_wanted_type.wanted_type = *fki->width_type;
1794 width_wanted_type.wanted_type_name = NULL;
1795 width_wanted_type.pointer_count = 0;
1796 width_wanted_type.char_lenient_flag = 0;
1797 width_wanted_type.writing_in_flag = 0;
1798 width_wanted_type.reading_from_flag = 0;
1799 width_wanted_type.name = _("field width");
1800 width_wanted_type.param = cur_param;
1801 width_wanted_type.arg_num = arg_num;
1802 width_wanted_type.next = NULL;
1803 if (last_wanted_type != 0)
1804 last_wanted_type->next = &width_wanted_type;
1805 if (first_wanted_type == 0)
1806 first_wanted_type = &width_wanted_type;
1807 last_wanted_type = &width_wanted_type;
1810 else
1812 /* Possibly read a numeric width. If the width is zero,
1813 we complain if appropriate. */
1814 int non_zero_width_char = FALSE;
1815 int found_width = FALSE;
1816 while (ISDIGIT (*format_chars))
1818 found_width = TRUE;
1819 if (*format_chars != '0')
1820 non_zero_width_char = TRUE;
1821 ++format_chars;
1823 if (found_width && !non_zero_width_char &&
1824 (fki->flags & (int) FMT_FLAG_ZERO_WIDTH_BAD))
1825 status_warning (status, "zero width in %s format",
1826 fki->name);
1827 if (found_width)
1829 i = strlen (flag_chars);
1830 flag_chars[i++] = fki->width_char;
1831 flag_chars[i] = 0;
1836 /* Read any format left precision (must be a number, not *). */
1837 if (fki->left_precision_char != 0 && *format_chars == '#')
1839 ++format_chars;
1840 i = strlen (flag_chars);
1841 flag_chars[i++] = fki->left_precision_char;
1842 flag_chars[i] = 0;
1843 if (!ISDIGIT (*format_chars))
1844 status_warning (status, "empty left precision in %s format",
1845 fki->name);
1846 while (ISDIGIT (*format_chars))
1847 ++format_chars;
1850 /* Read any format precision, possibly * or *m$. */
1851 if (fki->precision_char != 0 && *format_chars == '.')
1853 ++format_chars;
1854 i = strlen (flag_chars);
1855 flag_chars[i++] = fki->precision_char;
1856 flag_chars[i] = 0;
1857 if (fki->precision_type != NULL && *format_chars == '*')
1859 /* "...a...precision...may be indicated by an asterisk.
1860 In this case, an int argument supplies the...precision." */
1861 ++format_chars;
1862 if (has_operand_number != 0)
1864 int opnum;
1865 opnum = maybe_read_dollar_number (status, &format_chars,
1866 has_operand_number == 1,
1867 first_fillin_param,
1868 &params, fki);
1869 if (opnum == -1)
1870 return;
1871 else if (opnum > 0)
1873 has_operand_number = 1;
1874 arg_num = opnum + info->first_arg_num - 1;
1876 else
1877 has_operand_number = 0;
1879 if (info->first_arg_num != 0)
1881 if (params == 0)
1883 status_warning (status, "too few arguments for format");
1884 return;
1886 cur_param = TREE_VALUE (params);
1887 if (has_operand_number <= 0)
1889 params = TREE_CHAIN (params);
1890 ++arg_num;
1892 precision_wanted_type.wanted_type = *fki->precision_type;
1893 precision_wanted_type.wanted_type_name = NULL;
1894 precision_wanted_type.pointer_count = 0;
1895 precision_wanted_type.char_lenient_flag = 0;
1896 precision_wanted_type.writing_in_flag = 0;
1897 precision_wanted_type.reading_from_flag = 0;
1898 precision_wanted_type.name = _("field precision");
1899 precision_wanted_type.param = cur_param;
1900 precision_wanted_type.arg_num = arg_num;
1901 precision_wanted_type.next = NULL;
1902 if (last_wanted_type != 0)
1903 last_wanted_type->next = &precision_wanted_type;
1904 if (first_wanted_type == 0)
1905 first_wanted_type = &precision_wanted_type;
1906 last_wanted_type = &precision_wanted_type;
1909 else
1911 if (!(fki->flags & (int) FMT_FLAG_EMPTY_PREC_OK)
1912 && !ISDIGIT (*format_chars))
1913 status_warning (status, "empty precision in %s format",
1914 fki->name);
1915 while (ISDIGIT (*format_chars))
1916 ++format_chars;
1920 /* Read any length modifier, if this kind of format has them. */
1921 fli = fki->length_char_specs;
1922 length_chars = NULL;
1923 length_chars_val = FMT_LEN_none;
1924 length_chars_std = STD_C89;
1925 if (fli)
1927 while (fli->name != 0 && fli->name[0] != *format_chars)
1928 fli++;
1929 if (fli->name != 0)
1931 format_chars++;
1932 if (fli->double_name != 0 && fli->name[0] == *format_chars)
1934 format_chars++;
1935 length_chars = fli->double_name;
1936 length_chars_val = fli->double_index;
1937 length_chars_std = fli->double_std;
1939 else
1941 length_chars = fli->name;
1942 length_chars_val = fli->index;
1943 length_chars_std = fli->std;
1945 i = strlen (flag_chars);
1946 flag_chars[i++] = fki->length_code_char;
1947 flag_chars[i] = 0;
1949 if (pedantic)
1951 /* Warn if the length modifier is non-standard. */
1952 if (ADJ_STD (length_chars_std) > C_STD_VER)
1953 status_warning (status, "%s does not support the `%s' %s length modifier",
1954 C_STD_NAME (length_chars_std), length_chars,
1955 fki->name);
1959 /* Read any modifier (strftime E/O). */
1960 if (fki->modifier_chars != NULL)
1962 while (*format_chars != 0
1963 && strchr (fki->modifier_chars, *format_chars) != 0)
1965 if (strchr (flag_chars, *format_chars) != 0)
1967 const format_flag_spec *s = get_flag_spec (flag_specs,
1968 *format_chars, NULL);
1969 status_warning (status, "repeated %s in format", _(s->name));
1971 else
1973 i = strlen (flag_chars);
1974 flag_chars[i++] = *format_chars;
1975 flag_chars[i] = 0;
1977 ++format_chars;
1981 /* Handle the scanf allocation kludge. */
1982 if (fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
1984 if (*format_chars == 'a' && !flag_isoc99)
1986 if (format_chars[1] == 's' || format_chars[1] == 'S'
1987 || format_chars[1] == '[')
1989 /* `a' is used as a flag. */
1990 i = strlen (flag_chars);
1991 flag_chars[i++] = 'a';
1992 flag_chars[i] = 0;
1993 format_chars++;
1998 format_char = *format_chars;
1999 if (format_char == 0
2000 || (!(fki->flags & (int) FMT_FLAG_FANCY_PERCENT_OK)
2001 && format_char == '%'))
2003 status_warning (status, "conversion lacks type at end of format");
2004 continue;
2006 format_chars++;
2007 fci = fki->conversion_specs;
2008 while (fci->format_chars != 0
2009 && strchr (fci->format_chars, format_char) == 0)
2010 ++fci;
2011 if (fci->format_chars == 0)
2013 if (ISGRAPH(format_char))
2014 status_warning (status, "unknown conversion type character `%c' in format",
2015 format_char);
2016 else
2017 status_warning (status, "unknown conversion type character 0x%x in format",
2018 format_char);
2019 continue;
2021 if (pedantic)
2023 if (ADJ_STD (fci->std) > C_STD_VER)
2024 status_warning (status, "%s does not support the `%%%c' %s format",
2025 C_STD_NAME (fci->std), format_char, fki->name);
2028 /* Validate the individual flags used, removing any that are invalid. */
2030 int d = 0;
2031 for (i = 0; flag_chars[i] != 0; i++)
2033 const format_flag_spec *s = get_flag_spec (flag_specs,
2034 flag_chars[i], NULL);
2035 flag_chars[i - d] = flag_chars[i];
2036 if (flag_chars[i] == fki->length_code_char)
2037 continue;
2038 if (strchr (fci->flag_chars, flag_chars[i]) == 0)
2040 status_warning (status, "%s used with `%%%c' %s format",
2041 _(s->name), format_char, fki->name);
2042 d++;
2043 continue;
2045 if (pedantic)
2047 const format_flag_spec *t;
2048 if (ADJ_STD (s->std) > C_STD_VER)
2049 status_warning (status, "%s does not support %s",
2050 C_STD_NAME (s->std), _(s->long_name));
2051 t = get_flag_spec (flag_specs, flag_chars[i], fci->flags2);
2052 if (t != NULL && ADJ_STD (t->std) > ADJ_STD (s->std))
2054 const char *long_name = (t->long_name != NULL
2055 ? t->long_name
2056 : s->long_name);
2057 if (ADJ_STD (t->std) > C_STD_VER)
2058 status_warning (status, "%s does not support %s with the `%%%c' %s format",
2059 C_STD_NAME (t->std), _(long_name),
2060 format_char, fki->name);
2064 flag_chars[i - d] = 0;
2067 if ((fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
2068 && strchr (flag_chars, 'a') != 0)
2069 aflag = 1;
2071 if (fki->suppression_char
2072 && strchr (flag_chars, fki->suppression_char) != 0)
2073 suppressed = 1;
2075 /* Validate the pairs of flags used. */
2076 for (i = 0; bad_flag_pairs[i].flag_char1 != 0; i++)
2078 const format_flag_spec *s, *t;
2079 if (strchr (flag_chars, bad_flag_pairs[i].flag_char1) == 0)
2080 continue;
2081 if (strchr (flag_chars, bad_flag_pairs[i].flag_char2) == 0)
2082 continue;
2083 if (bad_flag_pairs[i].predicate != 0
2084 && strchr (fci->flags2, bad_flag_pairs[i].predicate) == 0)
2085 continue;
2086 s = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char1, NULL);
2087 t = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char2, NULL);
2088 if (bad_flag_pairs[i].ignored)
2090 if (bad_flag_pairs[i].predicate != 0)
2091 status_warning (status, "%s ignored with %s and `%%%c' %s format",
2092 _(s->name), _(t->name), format_char,
2093 fki->name);
2094 else
2095 status_warning (status, "%s ignored with %s in %s format",
2096 _(s->name), _(t->name), fki->name);
2098 else
2100 if (bad_flag_pairs[i].predicate != 0)
2101 status_warning (status, "use of %s and %s together with `%%%c' %s format",
2102 _(s->name), _(t->name), format_char,
2103 fki->name);
2104 else
2105 status_warning (status, "use of %s and %s together in %s format",
2106 _(s->name), _(t->name), fki->name);
2110 /* Give Y2K warnings. */
2111 if (warn_format_y2k)
2113 int y2k_level = 0;
2114 if (strchr (fci->flags2, '4') != 0)
2115 if (strchr (flag_chars, 'E') != 0)
2116 y2k_level = 3;
2117 else
2118 y2k_level = 2;
2119 else if (strchr (fci->flags2, '3') != 0)
2120 y2k_level = 3;
2121 else if (strchr (fci->flags2, '2') != 0)
2122 y2k_level = 2;
2123 if (y2k_level == 3)
2124 status_warning (status, "`%%%c' yields only last 2 digits of year in some locales",
2125 format_char);
2126 else if (y2k_level == 2)
2127 status_warning (status, "`%%%c' yields only last 2 digits of year", format_char);
2130 if (strchr (fci->flags2, '[') != 0)
2132 /* Skip over scan set, in case it happens to have '%' in it. */
2133 if (*format_chars == '^')
2134 ++format_chars;
2135 /* Find closing bracket; if one is hit immediately, then
2136 it's part of the scan set rather than a terminator. */
2137 if (*format_chars == ']')
2138 ++format_chars;
2139 while (*format_chars && *format_chars != ']')
2140 ++format_chars;
2141 if (*format_chars != ']')
2142 /* The end of the format string was reached. */
2143 status_warning (status, "no closing `]' for `%%[' format");
2146 wanted_type = 0;
2147 wanted_type_name = 0;
2148 if (fki->flags & (int) FMT_FLAG_ARG_CONVERT)
2150 wanted_type = (fci->types[length_chars_val].type
2151 ? *fci->types[length_chars_val].type : 0);
2152 wanted_type_name = fci->types[length_chars_val].name;
2153 wanted_type_std = fci->types[length_chars_val].std;
2154 if (wanted_type == 0)
2156 status_warning (status, "use of `%s' length modifier with `%c' type character",
2157 length_chars, format_char);
2158 /* Heuristic: skip one argument when an invalid length/type
2159 combination is encountered. */
2160 arg_num++;
2161 if (params == 0)
2163 status_warning (status, "too few arguments for format");
2164 return;
2166 params = TREE_CHAIN (params);
2167 continue;
2169 else if (pedantic
2170 /* Warn if non-standard, provided it is more non-standard
2171 than the length and type characters that may already
2172 have been warned for. */
2173 && ADJ_STD (wanted_type_std) > ADJ_STD (length_chars_std)
2174 && ADJ_STD (wanted_type_std) > ADJ_STD (fci->std))
2176 if (ADJ_STD (wanted_type_std) > C_STD_VER)
2177 status_warning (status, "%s does not support the `%%%s%c' %s format",
2178 C_STD_NAME (wanted_type_std), length_chars,
2179 format_char, fki->name);
2183 /* Finally. . .check type of argument against desired type! */
2184 if (info->first_arg_num == 0)
2185 continue;
2186 if ((fci->pointer_count == 0 && wanted_type == void_type_node)
2187 || suppressed)
2189 if (main_arg_num != 0)
2191 if (suppressed)
2192 status_warning (status, "operand number specified with suppressed assignment");
2193 else
2194 status_warning (status, "operand number specified for format taking no argument");
2197 else
2199 if (main_arg_num != 0)
2201 arg_num = main_arg_num;
2202 params = main_arg_params;
2204 else
2206 ++arg_num;
2207 if (has_operand_number > 0)
2209 status_warning (status, "missing $ operand number in format");
2210 return;
2212 else
2213 has_operand_number = 0;
2214 if (params == 0)
2216 status_warning (status, "too few arguments for format");
2217 return;
2220 cur_param = TREE_VALUE (params);
2221 params = TREE_CHAIN (params);
2222 main_wanted_type.wanted_type = wanted_type;
2223 main_wanted_type.wanted_type_name = wanted_type_name;
2224 main_wanted_type.pointer_count = fci->pointer_count + aflag;
2225 main_wanted_type.char_lenient_flag = 0;
2226 if (strchr (fci->flags2, 'c') != 0)
2227 main_wanted_type.char_lenient_flag = 1;
2228 main_wanted_type.writing_in_flag = 0;
2229 main_wanted_type.reading_from_flag = 0;
2230 if (aflag)
2231 main_wanted_type.writing_in_flag = 1;
2232 else
2234 if (strchr (fci->flags2, 'W') != 0)
2235 main_wanted_type.writing_in_flag = 1;
2236 if (strchr (fci->flags2, 'R') != 0)
2237 main_wanted_type.reading_from_flag = 1;
2239 main_wanted_type.name = NULL;
2240 main_wanted_type.param = cur_param;
2241 main_wanted_type.arg_num = arg_num;
2242 main_wanted_type.next = NULL;
2243 if (last_wanted_type != 0)
2244 last_wanted_type->next = &main_wanted_type;
2245 if (first_wanted_type == 0)
2246 first_wanted_type = &main_wanted_type;
2247 last_wanted_type = &main_wanted_type;
2250 if (first_wanted_type != 0)
2251 check_format_types (status, first_wanted_type);
2257 /* Check the argument types from a single format conversion (possibly
2258 including width and precision arguments). */
2259 static void
2260 check_format_types (int *status, format_wanted_type *types)
2262 for (; types != 0; types = types->next)
2264 tree cur_param;
2265 tree cur_type;
2266 tree orig_cur_type;
2267 tree wanted_type;
2268 int arg_num;
2269 int i;
2270 int char_type_flag;
2271 cur_param = types->param;
2272 cur_type = TREE_TYPE (cur_param);
2273 if (cur_type == error_mark_node)
2274 continue;
2275 char_type_flag = 0;
2276 wanted_type = types->wanted_type;
2277 arg_num = types->arg_num;
2279 /* The following should not occur here. */
2280 if (wanted_type == 0)
2281 abort ();
2282 if (wanted_type == void_type_node && types->pointer_count == 0)
2283 abort ();
2285 if (types->pointer_count == 0)
2286 wanted_type = (*lang_hooks.types.type_promotes_to) (wanted_type);
2288 STRIP_NOPS (cur_param);
2290 /* Check the types of any additional pointer arguments
2291 that precede the "real" argument. */
2292 for (i = 0; i < types->pointer_count; ++i)
2294 if (TREE_CODE (cur_type) == POINTER_TYPE)
2296 cur_type = TREE_TYPE (cur_type);
2297 if (cur_type == error_mark_node)
2298 break;
2300 /* Check for writing through a NULL pointer. */
2301 if (types->writing_in_flag
2302 && i == 0
2303 && cur_param != 0
2304 && integer_zerop (cur_param))
2305 status_warning (status,
2306 "writing through null pointer (arg %d)",
2307 arg_num);
2309 /* Check for reading through a NULL pointer. */
2310 if (types->reading_from_flag
2311 && i == 0
2312 && cur_param != 0
2313 && integer_zerop (cur_param))
2314 status_warning (status,
2315 "reading through null pointer (arg %d)",
2316 arg_num);
2318 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
2319 cur_param = TREE_OPERAND (cur_param, 0);
2320 else
2321 cur_param = 0;
2323 /* See if this is an attempt to write into a const type with
2324 scanf or with printf "%n". Note: the writing in happens
2325 at the first indirection only, if for example
2326 void * const * is passed to scanf %p; passing
2327 const void ** is simply passing an incompatible type. */
2328 if (types->writing_in_flag
2329 && i == 0
2330 && (TYPE_READONLY (cur_type)
2331 || (cur_param != 0
2332 && (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'c'
2333 || (DECL_P (cur_param)
2334 && TREE_READONLY (cur_param))))))
2335 status_warning (status, "writing into constant object (arg %d)", arg_num);
2337 /* If there are extra type qualifiers beyond the first
2338 indirection, then this makes the types technically
2339 incompatible. */
2340 if (i > 0
2341 && pedantic
2342 && (TYPE_READONLY (cur_type)
2343 || TYPE_VOLATILE (cur_type)
2344 || TYPE_RESTRICT (cur_type)))
2345 status_warning (status, "extra type qualifiers in format argument (arg %d)",
2346 arg_num);
2349 else
2351 if (types->pointer_count == 1)
2352 status_warning (status, "format argument is not a pointer (arg %d)", arg_num);
2353 else
2354 status_warning (status, "format argument is not a pointer to a pointer (arg %d)", arg_num);
2355 break;
2359 if (i < types->pointer_count)
2360 continue;
2362 orig_cur_type = cur_type;
2363 cur_type = TYPE_MAIN_VARIANT (cur_type);
2365 /* Check whether the argument type is a character type. This leniency
2366 only applies to certain formats, flagged with 'c'.
2368 if (types->char_lenient_flag)
2369 char_type_flag = (cur_type == char_type_node
2370 || cur_type == signed_char_type_node
2371 || cur_type == unsigned_char_type_node);
2373 /* Check the type of the "real" argument, if there's a type we want. */
2374 if (wanted_type == cur_type)
2375 continue;
2376 /* If we want `void *', allow any pointer type.
2377 (Anything else would already have got a warning.)
2378 With -pedantic, only allow pointers to void and to character
2379 types. */
2380 if (wanted_type == void_type_node
2381 && (!pedantic || (i == 1 && char_type_flag)))
2382 continue;
2383 /* Don't warn about differences merely in signedness, unless
2384 -pedantic. With -pedantic, warn if the type is a pointer
2385 target and not a character type, and for character types at
2386 a second level of indirection. */
2387 if (TREE_CODE (wanted_type) == INTEGER_TYPE
2388 && TREE_CODE (cur_type) == INTEGER_TYPE
2389 && (! pedantic || i == 0 || (i == 1 && char_type_flag))
2390 && (TREE_UNSIGNED (wanted_type)
2391 ? wanted_type == c_common_unsigned_type (cur_type)
2392 : wanted_type == c_common_signed_type (cur_type)))
2393 continue;
2394 /* Likewise, "signed char", "unsigned char" and "char" are
2395 equivalent but the above test won't consider them equivalent. */
2396 if (wanted_type == char_type_node
2397 && (! pedantic || i < 2)
2398 && char_type_flag)
2399 continue;
2400 /* Now we have a type mismatch. */
2402 const char *this;
2403 const char *that;
2405 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
2406 that = 0;
2407 if (TYPE_NAME (orig_cur_type) != 0
2408 && TREE_CODE (orig_cur_type) != INTEGER_TYPE
2409 && !(TREE_CODE (orig_cur_type) == POINTER_TYPE
2410 && TREE_CODE (TREE_TYPE (orig_cur_type)) == INTEGER_TYPE))
2412 if (TREE_CODE (TYPE_NAME (orig_cur_type)) == TYPE_DECL
2413 && DECL_NAME (TYPE_NAME (orig_cur_type)) != 0)
2414 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (orig_cur_type)));
2415 else
2416 that = IDENTIFIER_POINTER (TYPE_NAME (orig_cur_type));
2419 /* A nameless type can't possibly match what the format wants.
2420 So there will be a warning for it.
2421 Make up a string to describe vaguely what it is. */
2422 if (that == 0)
2424 if (TREE_CODE (orig_cur_type) == POINTER_TYPE)
2425 that = _("pointer");
2426 else
2427 that = _("different type");
2430 /* Make the warning better in case of mismatch of int vs long. */
2431 if (TREE_CODE (orig_cur_type) == INTEGER_TYPE
2432 && TREE_CODE (wanted_type) == INTEGER_TYPE
2433 && TYPE_PRECISION (orig_cur_type) == TYPE_PRECISION (wanted_type)
2434 && TYPE_NAME (orig_cur_type) != 0
2435 && TREE_CODE (TYPE_NAME (orig_cur_type)) == TYPE_DECL)
2436 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (orig_cur_type)));
2438 if (strcmp (this, that) != 0)
2440 /* There may be a better name for the format, e.g. size_t,
2441 but we should allow for programs with a perverse typedef
2442 making size_t something other than what the compiler
2443 thinks. */
2444 if (types->wanted_type_name != 0
2445 && strcmp (types->wanted_type_name, that) != 0)
2446 this = types->wanted_type_name;
2447 if (types->name != 0)
2448 status_warning (status, "%s is not type %s (arg %d)", types->name, this,
2449 arg_num);
2450 else
2451 status_warning (status, "%s format, %s arg (arg %d)", this, that, arg_num);
2457 /* Given a format_char_info array FCI, and a character C, this function
2458 returns the index into the conversion_specs where that specifier's
2459 data is located. If the character isn't found it aborts. */
2460 static unsigned int
2461 find_char_info_specifier_index (const format_char_info *fci, int c)
2463 unsigned int i = 0;
2465 while (fci->format_chars)
2467 if (strchr (fci->format_chars, c))
2468 return i;
2469 i++; fci++;
2472 /* We shouldn't be looking for a non-existent specifier. */
2473 abort ();
2477 /* Given a format_length_info array FLI, and a character C, this
2478 function returns the index into the conversion_specs where that
2479 modifier's data is located. If the character isn't found it
2480 aborts. */
2481 static unsigned int
2482 find_length_info_modifier_index (const format_length_info *fli, int c)
2484 unsigned int i = 0;
2486 while (fli->name)
2488 if (strchr (fli->name, c))
2489 return i;
2490 i++; fli++;
2493 /* We shouldn't be looking for a non-existent modifier. */
2494 abort ();
2497 /* Determine the type of HOST_WIDE_INT in the code being compiled for
2498 use in GCC's __asm_fprintf__ custom format attribute. You must
2499 have set dynamic_format_types before calling this function. */
2500 static void
2501 init_dynamic_asm_fprintf_info (void)
2503 static tree hwi;
2505 if (!hwi)
2507 format_length_info *new_asm_fprintf_length_specs;
2508 unsigned int i;
2510 /* Find the underlying type for HOST_WIDE_INT. For the %w
2511 length modifier to work, one must have issued: "typedef
2512 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2513 prior to using that modifier. */
2514 if (!(hwi = maybe_get_identifier ("__gcc_host_wide_int__"))
2515 || !(hwi = DECL_ORIGINAL_TYPE (identifier_global_value (hwi))))
2516 abort ();
2518 /* Create a new (writable) copy of asm_fprintf_length_specs. */
2519 new_asm_fprintf_length_specs = xmemdup (asm_fprintf_length_specs,
2520 sizeof (asm_fprintf_length_specs),
2521 sizeof (asm_fprintf_length_specs));
2523 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2524 i = find_length_info_modifier_index (new_asm_fprintf_length_specs, 'w');
2525 if (hwi == long_integer_type_node)
2526 new_asm_fprintf_length_specs[i].index = FMT_LEN_l;
2527 else if (hwi == long_long_integer_type_node)
2528 new_asm_fprintf_length_specs[i].index = FMT_LEN_ll;
2529 else
2530 abort ();
2532 /* Assign the new data for use. */
2533 dynamic_format_types[asm_fprintf_format_type].length_char_specs =
2534 new_asm_fprintf_length_specs;
2538 /* Determine the types of "tree" and "location_t" in the code being
2539 compiled for use in GCC's diagnostic custom format attributes. You
2540 must have set dynamic_format_types before calling this function. */
2541 static void
2542 init_dynamic_diag_info (void)
2544 static tree t, loc, hwi;
2546 if (!loc || !t || !hwi)
2548 static format_char_info *diag_fci, *cdiag_fci, *cxxdiag_fci;
2549 static format_length_info *diag_ls;
2550 unsigned int i;
2552 /* For the GCC-diagnostics custom format specifiers to work, one
2553 must have declared `tree' and/or `location_t' prior to using
2554 those attributes. If we haven't seen these declarations then
2555 you shouldn't use the specifiers requiring these types.
2556 However we don't force a hard ICE because we may see only one
2557 or the other type. */
2558 if ((loc = maybe_get_identifier ("location_t")))
2559 loc = TREE_TYPE (identifier_global_value (loc));
2561 /* We need to grab the underlying `union tree_node' so peek into
2562 an extra type level. */
2563 if ((t = maybe_get_identifier ("tree")))
2564 t = TREE_TYPE (TREE_TYPE (identifier_global_value (t)));
2566 /* Find the underlying type for HOST_WIDE_INT. For the %w
2567 length modifier to work, one must have issued: "typedef
2568 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2569 prior to using that modifier. */
2570 if ((hwi = maybe_get_identifier ("__gcc_host_wide_int__")))
2571 hwi = DECL_ORIGINAL_TYPE (identifier_global_value (hwi));
2573 /* Assign the new data for use. */
2575 /* All the GCC diag formats use the same length specs. */
2576 if (! diag_ls)
2577 dynamic_format_types[gcc_diag_format_type].length_char_specs =
2578 dynamic_format_types[gcc_cdiag_format_type].length_char_specs =
2579 dynamic_format_types[gcc_cxxdiag_format_type].length_char_specs =
2580 diag_ls = xmemdup (gcc_diag_length_specs,
2581 sizeof (gcc_diag_length_specs),
2582 sizeof (gcc_diag_length_specs));
2583 if (hwi)
2585 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2586 i = find_length_info_modifier_index (diag_ls, 'w');
2587 if (hwi == long_integer_type_node)
2588 diag_ls[i].index = FMT_LEN_l;
2589 else if (hwi == long_long_integer_type_node)
2590 diag_ls[i].index = FMT_LEN_ll;
2591 else
2592 abort ();
2595 /* Handle the __gcc_diag__ format specifics. */
2596 if (! diag_fci)
2597 dynamic_format_types[gcc_diag_format_type].conversion_specs =
2598 diag_fci = xmemdup (gcc_diag_char_table,
2599 sizeof(gcc_diag_char_table),
2600 sizeof(gcc_diag_char_table));
2601 if (loc)
2603 i = find_char_info_specifier_index (diag_fci, 'H');
2604 diag_fci[i].types[0].type = &loc;
2605 diag_fci[i].pointer_count = 1;
2608 /* Handle the __gcc_cdiag__ format specifics. */
2609 if (! cdiag_fci)
2610 dynamic_format_types[gcc_cdiag_format_type].conversion_specs =
2611 cdiag_fci = xmemdup (gcc_cdiag_char_table,
2612 sizeof(gcc_cdiag_char_table),
2613 sizeof(gcc_cdiag_char_table));
2614 if (loc)
2616 i = find_char_info_specifier_index (cdiag_fci, 'H');
2617 cdiag_fci[i].types[0].type = &loc;
2618 cdiag_fci[i].pointer_count = 1;
2620 if (t)
2622 /* All specifiers taking a tree share the same struct. */
2623 i = find_char_info_specifier_index (cdiag_fci, 'D');
2624 cdiag_fci[i].types[0].type = &t;
2625 cdiag_fci[i].pointer_count = 1;
2628 /* Handle the __gcc_cxxdiag__ format specifics. */
2629 if (! cxxdiag_fci)
2630 dynamic_format_types[gcc_cxxdiag_format_type].conversion_specs =
2631 cxxdiag_fci = xmemdup (gcc_cxxdiag_char_table,
2632 sizeof(gcc_cxxdiag_char_table),
2633 sizeof(gcc_cxxdiag_char_table));
2634 if (loc)
2636 i = find_char_info_specifier_index (cxxdiag_fci, 'H');
2637 cxxdiag_fci[i].types[0].type = &loc;
2638 cxxdiag_fci[i].pointer_count = 1;
2640 if (t)
2642 /* All specifiers taking a tree share the same struct. */
2643 i = find_char_info_specifier_index (cxxdiag_fci, 'D');
2644 cxxdiag_fci[i].types[0].type = &t;
2645 cxxdiag_fci[i].pointer_count = 1;
2650 /* Handle a "format" attribute; arguments as in
2651 struct attribute_spec.handler. */
2652 tree
2653 handle_format_attribute (tree *node, tree name ATTRIBUTE_UNUSED, tree args,
2654 int flags, bool *no_add_attrs)
2656 tree type = *node;
2657 function_format_info info;
2658 tree argument;
2660 if (!decode_format_attr (args, &info, 0))
2662 *no_add_attrs = true;
2663 return NULL_TREE;
2666 argument = TYPE_ARG_TYPES (type);
2667 if (argument)
2669 if (!check_format_string (argument, info.format_num, flags,
2670 no_add_attrs))
2671 return NULL_TREE;
2673 if (info.first_arg_num != 0)
2675 unsigned HOST_WIDE_INT arg_num = 1;
2677 /* Verify that first_arg_num points to the last arg,
2678 the ... */
2679 while (argument)
2680 arg_num++, argument = TREE_CHAIN (argument);
2682 if (arg_num != info.first_arg_num)
2684 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
2685 error ("args to be formatted is not '...'");
2686 *no_add_attrs = true;
2687 return NULL_TREE;
2692 if (info.format_type == strftime_format_type && info.first_arg_num != 0)
2694 error ("strftime formats cannot format arguments");
2695 *no_add_attrs = true;
2696 return NULL_TREE;
2699 /* If this is a custom GCC-internal format type, we have to
2700 initialize certain bits a runtime. */
2701 if (info.format_type == asm_fprintf_format_type
2702 || info.format_type == gcc_diag_format_type
2703 || info.format_type == gcc_cdiag_format_type
2704 || info.format_type == gcc_cxxdiag_format_type)
2706 /* Our first time through, we have to make sure that our
2707 format_type data is allocated dynamically and is modifiable. */
2708 if (!dynamic_format_types)
2709 format_types = dynamic_format_types =
2710 xmemdup (format_types_orig, sizeof (format_types_orig),
2711 sizeof (format_types_orig));
2713 /* If this is format __asm_fprintf__, we have to initialize
2714 GCC's notion of HOST_WIDE_INT for checking %wd. */
2715 if (info.format_type == asm_fprintf_format_type)
2716 init_dynamic_asm_fprintf_info();
2717 /* If this is one of the diagnostic attributes, then we have to
2718 initialize `location_t' and `tree' at runtime. */
2719 else if (info.format_type == gcc_diag_format_type
2720 || info.format_type == gcc_cdiag_format_type
2721 || info.format_type == gcc_cxxdiag_format_type)
2722 init_dynamic_diag_info();
2723 else
2724 abort();
2727 return NULL_TREE;