1 /* Check calls to formatted I/O functions (-Wformat).
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
31 #include "diagnostic-core.h"
32 #include "langhooks.h"
34 #include "alloc-pool.h"
37 /* Set format warning options according to a -Wformat=n option. */
40 set_Wformat (int setting)
42 warn_format = setting;
43 warn_format_extra_args = setting;
44 warn_format_zero_length = setting;
45 warn_format_contains_nul = setting;
48 warn_format_nonliteral = setting;
49 warn_format_security = setting;
50 warn_format_y2k = setting;
52 /* Make sure not to disable -Wnonnull if -Wformat=0 is specified. */
54 warn_nonnull = setting;
58 /* Handle attributes associated with format checking. */
60 /* This must be in the same order as format_types, except for
61 format_type_error. Target-specific format types do not have
62 matching enum values. */
63 enum format_type { printf_format_type, asm_fprintf_format_type,
64 gcc_diag_format_type, gcc_tdiag_format_type,
65 gcc_cdiag_format_type,
66 gcc_cxxdiag_format_type, gcc_gfc_format_type,
67 gcc_objc_string_format_type,
68 format_type_error = -1};
70 typedef struct function_format_info
72 int format_type; /* type of format (printf, scanf, etc.) */
73 unsigned HOST_WIDE_INT format_num; /* number of format argument */
74 unsigned HOST_WIDE_INT first_arg_num; /* number of first arg (zero for varargs) */
75 } function_format_info;
77 static bool decode_format_attr (tree, function_format_info *, int);
78 static int decode_format_type (const char *);
80 static bool check_format_string (tree argument,
81 unsigned HOST_WIDE_INT format_num,
82 int flags, bool *no_add_attrs,
83 int expected_format_type);
84 static bool get_constant (tree expr, unsigned HOST_WIDE_INT *value,
86 static const char *convert_format_name_to_system_name (const char *attr_name);
87 static bool cmp_attribs (const char *tattr_name, const char *attr_name);
89 static int first_target_format_type;
90 static const char *format_name (int format_num);
91 static int format_flags (int format_num);
93 /* Check that we have a pointer to a string suitable for use as a format.
94 The default is to check for a char type.
95 For objective-c dialects, this is extended to include references to string
96 objects validated by objc_string_ref_type_p ().
97 Targets may also provide a string object type that can be used within c and
98 c++ and shared with their respective objective-c dialects. In this case the
99 reference to a format string is checked for validity via a hook.
101 The function returns true if strref points to any string type valid for the
102 language dialect and target. */
105 valid_stringptr_type_p (tree strref)
107 return (strref != NULL
108 && TREE_CODE (strref) == POINTER_TYPE
109 && (TYPE_MAIN_VARIANT (TREE_TYPE (strref)) == char_type_node
110 || objc_string_ref_type_p (strref)
111 || (*targetcm.string_object_ref_type_p) ((const_tree) strref)));
114 /* Handle a "format_arg" attribute; arguments as in
115 struct attribute_spec.handler. */
117 handle_format_arg_attribute (tree *node, tree ARG_UNUSED (name),
118 tree args, int flags, bool *no_add_attrs)
121 tree format_num_expr = TREE_VALUE (args);
122 unsigned HOST_WIDE_INT format_num = 0;
124 if (!get_constant (format_num_expr, &format_num, 0))
126 error ("format string has invalid operand number");
127 *no_add_attrs = true;
131 if (prototype_p (type))
133 /* The format arg can be any string reference valid for the language and
134 target. We cannot be more specific in this case. */
135 if (!check_format_string (type, format_num, flags, no_add_attrs, -1))
139 if (!valid_stringptr_type_p (TREE_TYPE (type)))
141 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
142 error ("function does not return string type");
143 *no_add_attrs = true;
150 /* Verify that the format_num argument is actually a string reference suitable,
151 for the language dialect and target (in case the format attribute is in
152 error). When we know the specific reference type expected, this is also
155 check_format_string (tree fntype, unsigned HOST_WIDE_INT format_num,
156 int flags, bool *no_add_attrs, int expected_format_type)
158 unsigned HOST_WIDE_INT i;
159 bool is_objc_sref, is_target_sref, is_char_ref;
162 function_args_iterator iter;
165 FOREACH_FUNCTION_ARGS (fntype, ref, iter)
173 || !valid_stringptr_type_p (ref))
175 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
176 error ("format string argument is not a string type");
177 *no_add_attrs = true;
181 /* We only know that we want a suitable string reference. */
182 if (expected_format_type < 0)
185 /* Now check that the arg matches the expected type. */
187 (TYPE_MAIN_VARIANT (TREE_TYPE (ref)) == char_type_node);
189 fmt_flags = format_flags (expected_format_type);
190 is_objc_sref = is_target_sref = false;
192 is_objc_sref = objc_string_ref_type_p (ref);
194 if (!(fmt_flags & FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL))
197 return true; /* OK, we expected a char and found one. */
200 /* We expected a char but found an extended string type. */
202 error ("found a %<%s%> reference but the format argument should"
203 " be a string", format_name (gcc_objc_string_format_type));
205 error ("found a %qT but the format argument should be a string",
207 *no_add_attrs = true;
212 /* We expect a string object type as the format arg. */
215 error ("format argument should be a %<%s%> reference but"
216 " a string was found", format_name (expected_format_type));
217 *no_add_attrs = true;
221 /* We will assert that objective-c will support either its own string type
222 or the target-supplied variant. */
224 is_target_sref = (*targetcm.string_object_ref_type_p) ((const_tree) ref);
226 if (expected_format_type == (int) gcc_objc_string_format_type
227 && (is_objc_sref || is_target_sref))
230 /* We will allow a target string ref to match only itself. */
231 if (first_target_format_type
232 && expected_format_type >= first_target_format_type
237 error ("format argument should be a %<%s%> reference",
238 format_name (expected_format_type));
239 *no_add_attrs = true;
246 /* Verify EXPR is a constant, and store its value.
247 If validated_p is true there should be no errors.
248 Returns true on success, false otherwise. */
250 get_constant (tree expr, unsigned HOST_WIDE_INT *value, int validated_p)
252 if (TREE_CODE (expr) != INTEGER_CST || TREE_INT_CST_HIGH (expr) != 0)
254 gcc_assert (!validated_p);
258 *value = TREE_INT_CST_LOW (expr);
263 /* Decode the arguments to a "format" attribute into a
264 function_format_info structure. It is already known that the list
265 is of the right length. If VALIDATED_P is true, then these
266 attributes have already been validated and must not be erroneous;
267 if false, it will give an error message. Returns true if the
268 attributes are successfully decoded, false otherwise. */
271 decode_format_attr (tree args, function_format_info *info, int validated_p)
273 tree format_type_id = TREE_VALUE (args);
274 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
275 tree first_arg_num_expr
276 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
278 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
280 gcc_assert (!validated_p);
281 error ("unrecognized format specifier");
286 const char *p = IDENTIFIER_POINTER (format_type_id);
288 p = convert_format_name_to_system_name (p);
290 info->format_type = decode_format_type (p);
292 if (!c_dialect_objc ()
293 && info->format_type == gcc_objc_string_format_type)
295 gcc_assert (!validated_p);
296 warning (OPT_Wformat, "%qE is only allowed in Objective-C dialects",
298 info->format_type = format_type_error;
302 if (info->format_type == format_type_error)
304 gcc_assert (!validated_p);
305 warning (OPT_Wformat, "%qE is an unrecognized format function type",
311 if (!get_constant (format_num_expr, &info->format_num, validated_p))
313 error ("format string has invalid operand number");
317 if (!get_constant (first_arg_num_expr, &info->first_arg_num, validated_p))
319 error ("%<...%> has invalid operand number");
323 if (info->first_arg_num != 0 && info->first_arg_num <= info->format_num)
325 gcc_assert (!validated_p);
326 error ("format string argument follows the args to be formatted");
333 /* Check a call to a format function against a parameter list. */
335 /* The C standard version C++ is treated as equivalent to
336 or inheriting from, for the purpose of format features supported. */
337 #define CPLUSPLUS_STD_VER STD_C94
338 /* The C standard version we are checking formats against when pedantic. */
339 #define C_STD_VER ((int) (c_dialect_cxx () \
340 ? CPLUSPLUS_STD_VER \
343 : (flag_isoc94 ? STD_C94 : STD_C89))))
344 /* The name to give to the standard version we are warning about when
345 pedantic. FEATURE_VER is the version in which the feature warned out
346 appeared, which is higher than C_STD_VER. */
347 #define C_STD_NAME(FEATURE_VER) (c_dialect_cxx () \
349 : ((FEATURE_VER) == STD_EXT \
352 /* Adjust a C standard version, which may be STD_C9L, to account for
353 -Wno-long-long. Returns other standard versions unchanged. */
354 #define ADJ_STD(VER) ((int) ((VER) == STD_C9L \
355 ? (warn_long_long ? STD_C99 : STD_C89) \
358 /* Enum describing the kind of specifiers present in the format and
359 requiring an argument. */
360 enum format_specifier_kind {
363 CF_KIND_FIELD_PRECISION
366 static const char *kind_descriptions[] = {
368 N_("field width specifier"),
369 N_("field precision specifier")
372 /* Structure describing details of a type expected in format checking,
373 and the type to check against it. */
374 typedef struct format_wanted_type
376 /* The type wanted. */
378 /* The name of this type to use in diagnostics. */
379 const char *wanted_type_name;
380 /* Should be type checked just for scalar width identity. */
381 int scalar_identity_flag;
382 /* The level of indirection through pointers at which this type occurs. */
384 /* Whether, when pointer_count is 1, to allow any character type when
385 pedantic, rather than just the character or void type specified. */
386 int char_lenient_flag;
387 /* Whether the argument, dereferenced once, is written into and so the
388 argument must not be a pointer to a const-qualified type. */
390 /* Whether the argument, dereferenced once, is read from and so
391 must not be a NULL pointer. */
392 int reading_from_flag;
393 /* The kind of specifier that this type is used for. */
394 enum format_specifier_kind kind;
395 /* The starting character of the specifier. This never includes the
396 initial percent sign. */
397 const char *format_start;
398 /* The length of the specifier. */
400 /* The actual parameter to check against the wanted type. */
402 /* The argument number of that parameter. */
404 /* The next type to check for this format conversion, or NULL if none. */
405 struct format_wanted_type *next;
406 } format_wanted_type;
408 /* Convenience macro for format_length_info meaning unused. */
409 #define NO_FMT NULL, FMT_LEN_none, STD_C89
411 static const format_length_info printf_length_specs[] =
413 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99, 0 },
414 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L, 0 },
415 { "q", FMT_LEN_ll, STD_EXT, NO_FMT, 0 },
416 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
417 { "z", FMT_LEN_z, STD_C99, NO_FMT, 0 },
418 { "Z", FMT_LEN_z, STD_EXT, NO_FMT, 0 },
419 { "t", FMT_LEN_t, STD_C99, NO_FMT, 0 },
420 { "j", FMT_LEN_j, STD_C99, NO_FMT, 0 },
421 { "H", FMT_LEN_H, STD_EXT, NO_FMT, 0 },
422 { "D", FMT_LEN_D, STD_EXT, "DD", FMT_LEN_DD, STD_EXT, 0 },
423 { NO_FMT, NO_FMT, 0 }
426 /* Length specifiers valid for asm_fprintf. */
427 static const format_length_info asm_fprintf_length_specs[] =
429 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89, 0 },
430 { "w", FMT_LEN_none, STD_C89, NO_FMT, 0 },
431 { NO_FMT, NO_FMT, 0 }
434 /* Length specifiers valid for GCC diagnostics. */
435 static const format_length_info gcc_diag_length_specs[] =
437 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89, 0 },
438 { "w", FMT_LEN_none, STD_C89, NO_FMT, 0 },
439 { NO_FMT, NO_FMT, 0 }
442 /* The custom diagnostics all accept the same length specifiers. */
443 #define gcc_tdiag_length_specs gcc_diag_length_specs
444 #define gcc_cdiag_length_specs gcc_diag_length_specs
445 #define gcc_cxxdiag_length_specs gcc_diag_length_specs
447 /* This differs from printf_length_specs only in that "Z" is not accepted. */
448 static const format_length_info scanf_length_specs[] =
450 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99, 0 },
451 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L, 0 },
452 { "q", FMT_LEN_ll, STD_EXT, NO_FMT, 0 },
453 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
454 { "z", FMT_LEN_z, STD_C99, NO_FMT, 0 },
455 { "t", FMT_LEN_t, STD_C99, NO_FMT, 0 },
456 { "j", FMT_LEN_j, STD_C99, NO_FMT, 0 },
457 { "H", FMT_LEN_H, STD_EXT, NO_FMT, 0 },
458 { "D", FMT_LEN_D, STD_EXT, "DD", FMT_LEN_DD, STD_EXT, 0 },
459 { NO_FMT, NO_FMT, 0 }
463 /* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings
464 make no sense for a format type not part of any C standard version. */
465 static const format_length_info strfmon_length_specs[] =
467 /* A GNU extension. */
468 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
469 { NO_FMT, NO_FMT, 0 }
473 /* For now, the Fortran front-end routines only use l as length modifier. */
474 static const format_length_info gcc_gfc_length_specs[] =
476 { "l", FMT_LEN_l, STD_C89, NO_FMT, 0 },
477 { NO_FMT, NO_FMT, 0 }
481 static const format_flag_spec printf_flag_specs[] =
483 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
484 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
485 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
486 { '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
487 { '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
488 { '\'', 0, 0, N_("''' flag"), N_("the ''' printf flag"), STD_EXT },
489 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' printf flag"), STD_EXT },
490 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
491 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
492 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
493 { 0, 0, 0, NULL, NULL, STD_C89 }
497 static const format_flag_pair printf_flag_pairs[] =
501 { '0', 'p', 1, 'i' },
505 static const format_flag_spec asm_fprintf_flag_specs[] =
507 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
508 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
509 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
510 { '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
511 { '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
512 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
513 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
514 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
515 { 0, 0, 0, NULL, NULL, STD_C89 }
518 static const format_flag_pair asm_fprintf_flag_pairs[] =
522 { '0', 'p', 1, 'i' },
526 static const format_flag_pair gcc_diag_flag_pairs[] =
531 #define gcc_tdiag_flag_pairs gcc_diag_flag_pairs
532 #define gcc_cdiag_flag_pairs gcc_diag_flag_pairs
533 #define gcc_cxxdiag_flag_pairs gcc_diag_flag_pairs
535 static const format_flag_pair gcc_gfc_flag_pairs[] =
540 static const format_flag_spec gcc_diag_flag_specs[] =
542 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
543 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
544 { 'q', 0, 0, N_("'q' flag"), N_("the 'q' diagnostic flag"), STD_C89 },
545 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
546 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
547 { 0, 0, 0, NULL, NULL, STD_C89 }
550 #define gcc_tdiag_flag_specs gcc_diag_flag_specs
551 #define gcc_cdiag_flag_specs gcc_diag_flag_specs
552 #define gcc_cxxdiag_flag_specs gcc_diag_flag_specs
554 static const format_flag_spec scanf_flag_specs[] =
556 { '*', 0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89 },
557 { 'a', 0, 0, N_("'a' flag"), N_("the 'a' scanf flag"), STD_EXT },
558 { 'm', 0, 0, N_("'m' flag"), N_("the 'm' scanf flag"), STD_EXT },
559 { 'w', 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89 },
560 { 'L', 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89 },
561 { '\'', 0, 0, N_("''' flag"), N_("the ''' scanf flag"), STD_EXT },
562 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' scanf flag"), STD_EXT },
563 { 0, 0, 0, NULL, NULL, STD_C89 }
567 static const format_flag_pair scanf_flag_pairs[] =
575 static const format_flag_spec strftime_flag_specs[] =
577 { '_', 0, 0, N_("'_' flag"), N_("the '_' strftime flag"), STD_EXT },
578 { '-', 0, 0, N_("'-' flag"), N_("the '-' strftime flag"), STD_EXT },
579 { '0', 0, 0, N_("'0' flag"), N_("the '0' strftime flag"), STD_EXT },
580 { '^', 0, 0, N_("'^' flag"), N_("the '^' strftime flag"), STD_EXT },
581 { '#', 0, 0, N_("'#' flag"), N_("the '#' strftime flag"), STD_EXT },
582 { 'w', 0, 0, N_("field width"), N_("field width in strftime format"), STD_EXT },
583 { 'E', 0, 0, N_("'E' modifier"), N_("the 'E' strftime modifier"), STD_C99 },
584 { 'O', 0, 0, N_("'O' modifier"), N_("the 'O' strftime modifier"), STD_C99 },
585 { 'O', 'o', 0, NULL, N_("the 'O' modifier"), STD_EXT },
586 { 0, 0, 0, NULL, NULL, STD_C89 }
590 static const format_flag_pair strftime_flag_pairs[] =
601 static const format_flag_spec strfmon_flag_specs[] =
603 { '=', 0, 1, N_("fill character"), N_("fill character in strfmon format"), STD_C89 },
604 { '^', 0, 0, N_("'^' flag"), N_("the '^' strfmon flag"), STD_C89 },
605 { '+', 0, 0, N_("'+' flag"), N_("the '+' strfmon flag"), STD_C89 },
606 { '(', 0, 0, N_("'(' flag"), N_("the '(' strfmon flag"), STD_C89 },
607 { '!', 0, 0, N_("'!' flag"), N_("the '!' strfmon flag"), STD_C89 },
608 { '-', 0, 0, N_("'-' flag"), N_("the '-' strfmon flag"), STD_C89 },
609 { 'w', 0, 0, N_("field width"), N_("field width in strfmon format"), STD_C89 },
610 { '#', 0, 0, N_("left precision"), N_("left precision in strfmon format"), STD_C89 },
611 { 'p', 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89 },
612 { 'L', 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89 },
613 { 0, 0, 0, NULL, NULL, STD_C89 }
616 static const format_flag_pair strfmon_flag_pairs[] =
623 static const format_char_info dfly_ext_char_info =
624 { NULL, 1, STD_EXT, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL };
626 static const format_char_info print_char_table[] =
628 /* C89 conversion specifiers. */
629 { "di", 0, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, TEX_LL, T99_SST, T99_PD, T99_IM, BADLEN, BADLEN, BADLEN }, "-wp0 +'I", "i", NULL },
630 { "oxX", 0, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM, BADLEN, BADLEN, BADLEN }, "-wp0#", "i", NULL },
631 { "u", 0, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM, BADLEN, BADLEN, BADLEN }, "-wp0'I", "i", NULL },
632 { "fgG", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, TEX_D32, TEX_D64, TEX_D128 }, "-wp0 +#'I", "", NULL },
633 { "eE", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, TEX_D32, TEX_D64, TEX_D128 }, "-wp0 +#I", "", NULL },
634 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, T94_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
635 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
636 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "c", NULL },
637 { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, BADLEN, T99_SST, T99_PD, T99_IM, BADLEN, BADLEN, BADLEN }, "", "W", NULL },
638 /* C99 conversion specifiers. */
639 { "F", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, TEX_D32, TEX_D64, TEX_D128 }, "-wp0 +#'I", "", NULL },
640 { "aA", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "", NULL },
641 /* X/Open conversion specifiers. */
642 { "C", 0, STD_EXT, { TEX_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
643 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "R", NULL },
644 /* GNU conversion specifiers. */
645 { "m", 0, STD_EXT, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "", NULL },
646 /* BSD conversion specifiers. */
647 /* DragonFly kernel extensions (src/sys/kern/subr_prf.c).
648 The format %b is supported to decode error registers.
649 Its usage is: printf("reg=%b\n", regval, "<base><arg>*");
650 which produces: reg=3<BITTWO,BITONE>
652 { "b", 0, STD_EXT, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "", &dfly_ext_char_info },
653 { "ry", 0, STD_EXT, { T89_I, BADLEN, BADLEN, T89_L, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "i", NULL },
654 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
657 static const format_char_info asm_fprintf_char_table[] =
659 /* C89 conversion specifiers. */
660 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +", "i", NULL },
661 { "oxX", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0#", "i", NULL },
662 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0", "i", NULL },
663 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
664 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
666 /* asm_fprintf conversion specifiers. */
667 { "O", 0, STD_C89, NOARGUMENTS, "", "", NULL },
668 { "R", 0, STD_C89, NOARGUMENTS, "", "", NULL },
669 { "I", 0, STD_C89, NOARGUMENTS, "", "", NULL },
670 { "L", 0, STD_C89, NOARGUMENTS, "", "", NULL },
671 { "U", 0, STD_C89, NOARGUMENTS, "", "", NULL },
672 { "r", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
673 { "@", 0, STD_C89, NOARGUMENTS, "", "", NULL },
674 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
677 static const format_char_info gcc_diag_char_table[] =
679 /* C89 conversion specifiers. */
680 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
681 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
682 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
683 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
684 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
685 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
687 /* Custom conversion specifiers. */
689 /* These will require a "tree" at runtime. */
690 { "K", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
692 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
693 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
694 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
697 static const format_char_info gcc_tdiag_char_table[] =
699 /* C89 conversion specifiers. */
700 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
701 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
702 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
703 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
704 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
705 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
707 /* Custom conversion specifiers. */
709 /* These will require a "tree" at runtime. */
710 { "DFKTEV", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
712 { "v", 0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
714 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
715 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
716 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
719 static const format_char_info gcc_cdiag_char_table[] =
721 /* C89 conversion specifiers. */
722 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
723 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
724 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
725 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
726 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
727 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
729 /* Custom conversion specifiers. */
731 /* These will require a "tree" at runtime. */
732 { "DEFKTV", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
734 { "v", 0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
736 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
737 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
738 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
741 static const format_char_info gcc_cxxdiag_char_table[] =
743 /* C89 conversion specifiers. */
744 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
745 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
746 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
747 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
748 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
749 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
751 /* Custom conversion specifiers. */
753 /* These will require a "tree" at runtime. */
754 { "ADEFKSTV",0,STD_C89,{ T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+#", "", NULL },
756 { "v", 0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
758 /* These accept either an 'int' or an 'enum tree_code' (which is handled as an 'int'.) */
759 { "CLOPQ",0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
761 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
762 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
763 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
766 static const format_char_info gcc_gfc_char_table[] =
768 /* C89 conversion specifiers. */
769 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
770 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
771 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
772 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL },
774 /* gfc conversion specifiers. */
776 { "C", 0, STD_C89, NOARGUMENTS, "", "", NULL },
778 /* This will require a "locus" at runtime. */
779 { "L", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "R", NULL },
781 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
784 static const format_char_info scan_char_table[] =
786 /* C89 conversion specifiers. */
787 { "di", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, TEX_LL, T99_SST, T99_PD, T99_IM, BADLEN, BADLEN, BADLEN }, "*w'I", "W", NULL },
788 { "u", 1, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM, BADLEN, BADLEN, BADLEN }, "*w'I", "W", NULL },
789 { "oxX", 1, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
790 { "efgEG", 1, STD_C89, { T89_F, BADLEN, BADLEN, T89_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, TEX_D32, TEX_D64, TEX_D128 }, "*w'", "W", NULL },
791 { "c", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*mw", "cW", NULL },
792 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "cW", NULL },
793 { "[", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "cW[", NULL },
794 { "p", 2, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
795 { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, BADLEN, T99_SST, T99_PD, T99_IM, BADLEN, BADLEN, BADLEN }, "", "W", NULL },
796 /* C99 conversion specifiers. */
797 { "F", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, TEX_D32, TEX_D64, TEX_D128 }, "*w'", "W", NULL },
798 { "aA", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w'", "W", NULL },
799 /* X/Open conversion specifiers. */
800 { "C", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*mw", "W", NULL },
801 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "W", NULL },
802 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
805 static const format_char_info time_char_table[] =
807 /* C89 conversion specifiers. */
808 { "ABZab", 0, STD_C89, NOLENGTHS, "^#", "", NULL },
809 { "cx", 0, STD_C89, NOLENGTHS, "E", "3", NULL },
810 { "HIMSUWdmw", 0, STD_C89, NOLENGTHS, "-_0Ow", "", NULL },
811 { "j", 0, STD_C89, NOLENGTHS, "-_0Ow", "o", NULL },
812 { "p", 0, STD_C89, NOLENGTHS, "#", "", NULL },
813 { "X", 0, STD_C89, NOLENGTHS, "E", "", NULL },
814 { "y", 0, STD_C89, NOLENGTHS, "EO-_0w", "4", NULL },
815 { "Y", 0, STD_C89, NOLENGTHS, "-_0EOw", "o", NULL },
816 { "%", 0, STD_C89, NOLENGTHS, "", "", NULL },
817 /* C99 conversion specifiers. */
818 { "C", 0, STD_C99, NOLENGTHS, "-_0EOw", "o", NULL },
819 { "D", 0, STD_C99, NOLENGTHS, "", "2", NULL },
820 { "eVu", 0, STD_C99, NOLENGTHS, "-_0Ow", "", NULL },
821 { "FRTnrt", 0, STD_C99, NOLENGTHS, "", "", NULL },
822 { "g", 0, STD_C99, NOLENGTHS, "O-_0w", "2o", NULL },
823 { "G", 0, STD_C99, NOLENGTHS, "-_0Ow", "o", NULL },
824 { "h", 0, STD_C99, NOLENGTHS, "^#", "", NULL },
825 { "z", 0, STD_C99, NOLENGTHS, "O", "o", NULL },
826 /* GNU conversion specifiers. */
827 { "kls", 0, STD_EXT, NOLENGTHS, "-_0Ow", "", NULL },
828 { "P", 0, STD_EXT, NOLENGTHS, "", "", NULL },
829 /* DragonFly/FreeBSD conversion specifiers. */
830 { "+", 0, STD_EXT, NOLENGTHS, "E", "3", NULL },
831 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
834 static const format_char_info monetary_char_table[] =
836 { "in", 0, STD_C89, { T89_D, BADLEN, BADLEN, BADLEN, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "=^+(!-w#p", "", NULL },
837 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
840 /* This must be in the same order as enum format_type. */
841 static const format_kind_info format_types_orig[] =
843 { "gnu_printf", printf_length_specs, print_char_table, " +#0-'I", NULL,
844 printf_flag_specs, printf_flag_pairs,
845 FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_EMPTY_PREC_OK,
846 'w', 0, 'p', 0, 'L', 0,
847 &integer_type_node, &integer_type_node
849 { "asm_fprintf", asm_fprintf_length_specs, asm_fprintf_char_table, " +#0-", NULL,
850 asm_fprintf_flag_specs, asm_fprintf_flag_pairs,
851 FMT_FLAG_ARG_CONVERT|FMT_FLAG_EMPTY_PREC_OK,
852 'w', 0, 'p', 0, 'L', 0,
855 { "gcc_diag", gcc_diag_length_specs, gcc_diag_char_table, "q+#", NULL,
856 gcc_diag_flag_specs, gcc_diag_flag_pairs,
857 FMT_FLAG_ARG_CONVERT,
858 0, 0, 'p', 0, 'L', 0,
859 NULL, &integer_type_node
861 { "gcc_tdiag", gcc_tdiag_length_specs, gcc_tdiag_char_table, "q+#", NULL,
862 gcc_tdiag_flag_specs, gcc_tdiag_flag_pairs,
863 FMT_FLAG_ARG_CONVERT,
864 0, 0, 'p', 0, 'L', 0,
865 NULL, &integer_type_node
867 { "gcc_cdiag", gcc_cdiag_length_specs, gcc_cdiag_char_table, "q+#", NULL,
868 gcc_cdiag_flag_specs, gcc_cdiag_flag_pairs,
869 FMT_FLAG_ARG_CONVERT,
870 0, 0, 'p', 0, 'L', 0,
871 NULL, &integer_type_node
873 { "gcc_cxxdiag", gcc_cxxdiag_length_specs, gcc_cxxdiag_char_table, "q+#", NULL,
874 gcc_cxxdiag_flag_specs, gcc_cxxdiag_flag_pairs,
875 FMT_FLAG_ARG_CONVERT,
876 0, 0, 'p', 0, 'L', 0,
877 NULL, &integer_type_node
879 { "gcc_gfc", gcc_gfc_length_specs, gcc_gfc_char_table, "", NULL,
880 NULL, gcc_gfc_flag_pairs,
881 FMT_FLAG_ARG_CONVERT,
885 { "NSString", NULL, NULL, NULL, NULL,
887 FMT_FLAG_ARG_CONVERT|FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL, 0, 0, 0, 0, 0, 0,
890 { "gnu_scanf", scanf_length_specs, scan_char_table, "*'I", NULL,
891 scanf_flag_specs, scanf_flag_pairs,
892 FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_ZERO_WIDTH_BAD|FMT_FLAG_DOLLAR_GAP_POINTER_OK,
893 'w', 0, 0, '*', 'L', 'm',
896 { "gnu_strftime", NULL, time_char_table, "_-0^#", "EO",
897 strftime_flag_specs, strftime_flag_pairs,
898 FMT_FLAG_FANCY_PERCENT_OK, 'w', 0, 0, 0, 0, 0,
901 { "gnu_strfmon", strfmon_length_specs, monetary_char_table, "=^+(!-", NULL,
902 strfmon_flag_specs, strfmon_flag_pairs,
903 FMT_FLAG_ARG_CONVERT, 'w', '#', 'p', 0, 'L', 0,
908 /* This layer of indirection allows GCC to reassign format_types with
909 new data if necessary, while still allowing the original data to be
911 static const format_kind_info *format_types = format_types_orig;
912 /* We can modify this one. We also add target-specific format types
913 to the end of the array. */
914 static format_kind_info *dynamic_format_types;
916 static int n_format_types = ARRAY_SIZE (format_types_orig);
918 /* Structure detailing the results of checking a format function call
919 where the format expression may be a conditional expression with
920 many leaves resulting from nested conditional expressions. */
923 /* Number of leaves of the format argument that could not be checked
924 as they were not string literals. */
925 int number_non_literal;
926 /* Number of leaves of the format argument that were null pointers or
927 string literals, but had extra format arguments. */
928 int number_extra_args;
929 /* Number of leaves of the format argument that were null pointers or
930 string literals, but had extra format arguments and used $ operand
932 int number_dollar_extra_args;
933 /* Number of leaves of the format argument that were wide string
936 /* Number of leaves of the format argument that were empty strings. */
938 /* Number of leaves of the format argument that were unterminated
940 int number_unterminated;
941 /* Number of leaves of the format argument that were not counted above. */
943 } format_check_results;
947 format_check_results *res;
948 function_format_info *info;
950 } format_check_context;
952 /* Return the format name (as specified in the original table) for the format
953 type indicated by format_num. */
955 format_name (int format_num)
957 if (format_num >= 0 && format_num < n_format_types)
958 return format_types[format_num].name;
962 /* Return the format flags (as specified in the original table) for the format
963 type indicated by format_num. */
965 format_flags (int format_num)
967 if (format_num >= 0 && format_num < n_format_types)
968 return format_types[format_num].flags;
972 static void check_format_info (function_format_info *, tree);
973 static void check_format_arg (void *, tree, unsigned HOST_WIDE_INT);
974 static void check_format_info_main (format_check_results *,
975 function_format_info *,
976 const char *, int, tree,
977 unsigned HOST_WIDE_INT, alloc_pool);
979 static void init_dollar_format_checking (int, tree);
980 static int maybe_read_dollar_number (const char **, int,
981 tree, tree *, const format_kind_info *);
982 static bool avoid_dollar_number (const char *);
983 static void finish_dollar_format_checking (format_check_results *, int);
985 static const format_flag_spec *get_flag_spec (const format_flag_spec *,
988 static void check_format_types (format_wanted_type *);
989 static void format_type_warning (format_wanted_type *, tree, tree);
991 /* Decode a format type from a string, returning the type, or
992 format_type_error if not valid, in which case the caller should print an
995 decode_format_type (const char *s)
1000 s = convert_format_name_to_system_name (s);
1002 for (i = 0; i < n_format_types; i++)
1005 if (!strcmp (s, format_types[i].name))
1007 alen = strlen (format_types[i].name);
1008 if (slen == alen + 4 && s[0] == '_' && s[1] == '_'
1009 && s[slen - 1] == '_' && s[slen - 2] == '_'
1010 && !strncmp (s + 2, format_types[i].name, alen))
1013 return format_type_error;
1017 /* Check the argument list of a call to printf, scanf, etc.
1018 ATTRS are the attributes on the function type. There are NARGS argument
1019 values in the array ARGARRAY.
1020 Also, if -Wmissing-format-attribute,
1021 warn for calls to vprintf or vscanf in functions with no such format
1022 attribute themselves. */
1025 check_function_format (tree attrs, int nargs, tree *argarray)
1029 /* See if this function has any format attributes. */
1030 for (a = attrs; a; a = TREE_CHAIN (a))
1032 if (is_attribute_p ("format", TREE_PURPOSE (a)))
1034 /* Yup; check it. */
1035 function_format_info info;
1036 decode_format_attr (TREE_VALUE (a), &info, 1);
1039 /* FIXME: Rewrite all the internal functions in this file
1040 to use the ARGARRAY directly instead of constructing this
1042 tree params = NULL_TREE;
1044 for (i = nargs - 1; i >= 0; i--)
1045 params = tree_cons (NULL_TREE, argarray[i], params);
1046 check_format_info (&info, params);
1048 if (warn_missing_format_attribute && info.first_arg_num == 0
1049 && (format_types[info.format_type].flags
1050 & (int) FMT_FLAG_ARG_CONVERT))
1053 for (c = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
1056 if (is_attribute_p ("format", TREE_PURPOSE (c))
1057 && (decode_format_type (IDENTIFIER_POINTER
1058 (TREE_VALUE (TREE_VALUE (c))))
1059 == info.format_type))
1063 /* Check if the current function has a parameter to which
1064 the format attribute could be attached; if not, it
1065 can't be a candidate for a format attribute, despite
1066 the vprintf-like or vscanf-like call. */
1068 for (args = DECL_ARGUMENTS (current_function_decl);
1070 args = DECL_CHAIN (args))
1072 if (TREE_CODE (TREE_TYPE (args)) == POINTER_TYPE
1073 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args)))
1078 warning (OPT_Wmissing_format_attribute, "function might "
1079 "be possible candidate for %qs format attribute",
1080 format_types[info.format_type].name);
1088 /* Variables used by the checking of $ operand number formats. */
1089 static char *dollar_arguments_used = NULL;
1090 static char *dollar_arguments_pointer_p = NULL;
1091 static int dollar_arguments_alloc = 0;
1092 static int dollar_arguments_count;
1093 static int dollar_first_arg_num;
1094 static int dollar_max_arg_used;
1095 static int dollar_format_warned;
1097 /* Initialize the checking for a format string that may contain $
1098 parameter number specifications; we will need to keep track of whether
1099 each parameter has been used. FIRST_ARG_NUM is the number of the first
1100 argument that is a parameter to the format, or 0 for a vprintf-style
1101 function; PARAMS is the list of arguments starting at this argument. */
1104 init_dollar_format_checking (int first_arg_num, tree params)
1106 tree oparams = params;
1108 dollar_first_arg_num = first_arg_num;
1109 dollar_arguments_count = 0;
1110 dollar_max_arg_used = 0;
1111 dollar_format_warned = 0;
1112 if (first_arg_num > 0)
1116 dollar_arguments_count++;
1117 params = TREE_CHAIN (params);
1120 if (dollar_arguments_alloc < dollar_arguments_count)
1122 free (dollar_arguments_used);
1123 free (dollar_arguments_pointer_p);
1124 dollar_arguments_alloc = dollar_arguments_count;
1125 dollar_arguments_used = XNEWVEC (char, dollar_arguments_alloc);
1126 dollar_arguments_pointer_p = XNEWVEC (char, dollar_arguments_alloc);
1128 if (dollar_arguments_alloc)
1130 memset (dollar_arguments_used, 0, dollar_arguments_alloc);
1131 if (first_arg_num > 0)
1137 dollar_arguments_pointer_p[i] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params)))
1139 params = TREE_CHAIN (params);
1147 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
1148 is set, it is an error if one is not found; otherwise, it is OK. If
1149 such a number is found, check whether it is within range and mark that
1150 numbered operand as being used for later checking. Returns the operand
1151 number if found and within range, zero if no such number was found and
1152 this is OK, or -1 on error. PARAMS points to the first operand of the
1153 format; PARAM_PTR is made to point to the parameter referred to. If
1154 a $ format is found, *FORMAT is updated to point just after it. */
1157 maybe_read_dollar_number (const char **format,
1158 int dollar_needed, tree params, tree *param_ptr,
1159 const format_kind_info *fki)
1163 const char *fcp = *format;
1164 if (!ISDIGIT (*fcp))
1168 warning (OPT_Wformat, "missing $ operand number in format");
1176 while (ISDIGIT (*fcp))
1179 nargnum = 10 * argnum + (*fcp - '0');
1180 if (nargnum < 0 || nargnum / 10 != argnum)
1189 warning (OPT_Wformat, "missing $ operand number in format");
1196 if (pedantic && !dollar_format_warned)
1198 warning (OPT_Wformat, "%s does not support %%n$ operand number formats",
1199 C_STD_NAME (STD_EXT));
1200 dollar_format_warned = 1;
1202 if (overflow_flag || argnum == 0
1203 || (dollar_first_arg_num && argnum > dollar_arguments_count))
1205 warning (OPT_Wformat, "operand number out of range in format");
1208 if (argnum > dollar_max_arg_used)
1209 dollar_max_arg_used = argnum;
1210 /* For vprintf-style functions we may need to allocate more memory to
1211 track which arguments are used. */
1212 while (dollar_arguments_alloc < dollar_max_arg_used)
1215 nalloc = 2 * dollar_arguments_alloc + 16;
1216 dollar_arguments_used = XRESIZEVEC (char, dollar_arguments_used,
1218 dollar_arguments_pointer_p = XRESIZEVEC (char, dollar_arguments_pointer_p,
1220 memset (dollar_arguments_used + dollar_arguments_alloc, 0,
1221 nalloc - dollar_arguments_alloc);
1222 dollar_arguments_alloc = nalloc;
1224 if (!(fki->flags & (int) FMT_FLAG_DOLLAR_MULTIPLE)
1225 && dollar_arguments_used[argnum - 1] == 1)
1227 dollar_arguments_used[argnum - 1] = 2;
1228 warning (OPT_Wformat, "format argument %d used more than once in %s format",
1232 dollar_arguments_used[argnum - 1] = 1;
1233 if (dollar_first_arg_num)
1236 *param_ptr = params;
1237 for (i = 1; i < argnum && *param_ptr != 0; i++)
1238 *param_ptr = TREE_CHAIN (*param_ptr);
1240 /* This case shouldn't be caught here. */
1241 gcc_assert (*param_ptr);
1248 /* Ensure that FORMAT does not start with a decimal number followed by
1249 a $; give a diagnostic and return true if it does, false otherwise. */
1252 avoid_dollar_number (const char *format)
1254 if (!ISDIGIT (*format))
1256 while (ISDIGIT (*format))
1260 warning (OPT_Wformat, "$ operand number used after format without operand number");
1267 /* Finish the checking for a format string that used $ operand number formats
1268 instead of non-$ formats. We check for unused operands before used ones
1269 (a serious error, since the implementation of the format function
1270 can't know what types to pass to va_arg to find the later arguments).
1271 and for unused operands at the end of the format (if we know how many
1272 arguments the format had, so not for vprintf). If there were operand
1273 numbers out of range on a non-vprintf-style format, we won't have reached
1274 here. If POINTER_GAP_OK, unused arguments are OK if all arguments are
1278 finish_dollar_format_checking (format_check_results *res, int pointer_gap_ok)
1281 bool found_pointer_gap = false;
1282 for (i = 0; i < dollar_max_arg_used; i++)
1284 if (!dollar_arguments_used[i])
1286 if (pointer_gap_ok && (dollar_first_arg_num == 0
1287 || dollar_arguments_pointer_p[i]))
1288 found_pointer_gap = true;
1290 warning (OPT_Wformat,
1291 "format argument %d unused before used argument %d in $-style format",
1292 i + 1, dollar_max_arg_used);
1295 if (found_pointer_gap
1296 || (dollar_first_arg_num
1297 && dollar_max_arg_used < dollar_arguments_count))
1299 res->number_other--;
1300 res->number_dollar_extra_args++;
1305 /* Retrieve the specification for a format flag. SPEC contains the
1306 specifications for format flags for the applicable kind of format.
1307 FLAG is the flag in question. If PREDICATES is NULL, the basic
1308 spec for that flag must be retrieved and must exist. If
1309 PREDICATES is not NULL, it is a string listing possible predicates
1310 for the spec entry; if an entry predicated on any of these is
1311 found, it is returned, otherwise NULL is returned. */
1313 static const format_flag_spec *
1314 get_flag_spec (const format_flag_spec *spec, int flag, const char *predicates)
1317 for (i = 0; spec[i].flag_char != 0; i++)
1319 if (spec[i].flag_char != flag)
1321 if (predicates != NULL)
1323 if (spec[i].predicate != 0
1324 && strchr (predicates, spec[i].predicate) != 0)
1327 else if (spec[i].predicate == 0)
1330 gcc_assert (predicates);
1335 /* Check the argument list of a call to printf, scanf, etc.
1336 INFO points to the function_format_info structure.
1337 PARAMS is the list of argument values. */
1340 check_format_info (function_format_info *info, tree params)
1342 format_check_context format_ctx;
1343 unsigned HOST_WIDE_INT arg_num;
1345 format_check_results res;
1346 /* Skip to format argument. If the argument isn't available, there's
1347 no work for us to do; prototype checking will catch the problem. */
1348 for (arg_num = 1; ; ++arg_num)
1352 if (arg_num == info->format_num)
1354 params = TREE_CHAIN (params);
1356 format_tree = TREE_VALUE (params);
1357 params = TREE_CHAIN (params);
1358 if (format_tree == 0)
1361 res.number_non_literal = 0;
1362 res.number_extra_args = 0;
1363 res.number_dollar_extra_args = 0;
1364 res.number_wide = 0;
1365 res.number_empty = 0;
1366 res.number_unterminated = 0;
1367 res.number_other = 0;
1369 format_ctx.res = &res;
1370 format_ctx.info = info;
1371 format_ctx.params = params;
1373 check_function_arguments_recurse (check_format_arg, &format_ctx,
1374 format_tree, arg_num);
1376 if (res.number_non_literal > 0)
1378 /* Functions taking a va_list normally pass a non-literal format
1379 string. These functions typically are declared with
1380 first_arg_num == 0, so avoid warning in those cases. */
1381 if (!(format_types[info->format_type].flags & (int) FMT_FLAG_ARG_CONVERT))
1383 /* For strftime-like formats, warn for not checking the format
1384 string; but there are no arguments to check. */
1385 warning (OPT_Wformat_nonliteral,
1386 "format not a string literal, format string not checked");
1388 else if (info->first_arg_num != 0)
1390 /* If there are no arguments for the format at all, we may have
1391 printf (foo) which is likely to be a security hole. */
1392 while (arg_num + 1 < info->first_arg_num)
1396 params = TREE_CHAIN (params);
1399 if (params == 0 && warn_format_security)
1400 warning (OPT_Wformat_security,
1401 "format not a string literal and no format arguments");
1402 else if (params == 0 && warn_format_nonliteral)
1403 warning (OPT_Wformat_nonliteral,
1404 "format not a string literal and no format arguments");
1406 warning (OPT_Wformat_nonliteral,
1407 "format not a string literal, argument types not checked");
1411 /* If there were extra arguments to the format, normally warn. However,
1412 the standard does say extra arguments are ignored, so in the specific
1413 case where we have multiple leaves (conditional expressions or
1414 ngettext) allow extra arguments if at least one leaf didn't have extra
1415 arguments, but was otherwise OK (either non-literal or checked OK).
1416 If the format is an empty string, this should be counted similarly to the
1417 case of extra format arguments. */
1418 if (res.number_extra_args > 0 && res.number_non_literal == 0
1419 && res.number_other == 0)
1420 warning (OPT_Wformat_extra_args, "too many arguments for format");
1421 if (res.number_dollar_extra_args > 0 && res.number_non_literal == 0
1422 && res.number_other == 0)
1423 warning (OPT_Wformat_extra_args, "unused arguments in $-style format");
1424 if (res.number_empty > 0 && res.number_non_literal == 0
1425 && res.number_other == 0)
1426 warning (OPT_Wformat_zero_length, "zero-length %s format string",
1427 format_types[info->format_type].name);
1429 if (res.number_wide > 0)
1430 warning (OPT_Wformat, "format is a wide character string");
1432 if (res.number_unterminated > 0)
1433 warning (OPT_Wformat, "unterminated format string");
1436 /* Callback from check_function_arguments_recurse to check a
1437 format string. FORMAT_TREE is the format parameter. ARG_NUM
1438 is the number of the format argument. CTX points to a
1439 format_check_context. */
1442 check_format_arg (void *ctx, tree format_tree,
1443 unsigned HOST_WIDE_INT arg_num)
1445 format_check_context *format_ctx = (format_check_context *) ctx;
1446 format_check_results *res = format_ctx->res;
1447 function_format_info *info = format_ctx->info;
1448 tree params = format_ctx->params;
1451 HOST_WIDE_INT offset;
1452 const char *format_chars;
1453 tree array_size = 0;
1455 alloc_pool fwt_pool;
1457 if (integer_zerop (format_tree))
1459 /* Skip to first argument to check, so we can see if this format
1460 has any arguments (it shouldn't). */
1461 while (arg_num + 1 < info->first_arg_num)
1465 params = TREE_CHAIN (params);
1470 res->number_other++;
1472 res->number_extra_args++;
1478 if (TREE_CODE (format_tree) == POINTER_PLUS_EXPR)
1482 arg0 = TREE_OPERAND (format_tree, 0);
1483 arg1 = TREE_OPERAND (format_tree, 1);
1486 if (TREE_CODE (arg1) == INTEGER_CST)
1490 res->number_non_literal++;
1493 if (!host_integerp (arg1, 0)
1494 || (offset = tree_low_cst (arg1, 0)) < 0)
1496 res->number_non_literal++;
1500 if (TREE_CODE (format_tree) != ADDR_EXPR)
1502 res->number_non_literal++;
1505 format_tree = TREE_OPERAND (format_tree, 0);
1506 if (format_types[info->format_type].flags
1507 & (int) FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL)
1509 bool objc_str = (info->format_type == gcc_objc_string_format_type);
1510 /* We cannot examine this string here - but we can check that it is
1512 if (TREE_CODE (format_tree) != CONST_DECL
1513 || !((objc_str && objc_string_ref_type_p (TREE_TYPE (format_tree)))
1514 || (*targetcm.string_object_ref_type_p)
1515 ((const_tree) TREE_TYPE (format_tree))))
1517 res->number_non_literal++;
1520 /* Skip to first argument to check. */
1521 while (arg_num + 1 < info->first_arg_num)
1525 params = TREE_CHAIN (params);
1528 /* So, we have a valid literal string object and one or more params.
1529 We need to use an external helper to parse the string into format
1530 info. For Objective-C variants we provide the resource within the
1531 objc tree, for target variants, via a hook. */
1533 objc_check_format_arg (format_tree, params);
1534 else if (targetcm.check_string_object_format_arg)
1535 (*targetcm.check_string_object_format_arg) (format_tree, params);
1536 /* Else we can't handle it and retire quietly. */
1539 if (TREE_CODE (format_tree) == ARRAY_REF
1540 && host_integerp (TREE_OPERAND (format_tree, 1), 0)
1541 && (offset += tree_low_cst (TREE_OPERAND (format_tree, 1), 0)) >= 0)
1542 format_tree = TREE_OPERAND (format_tree, 0);
1543 if (TREE_CODE (format_tree) == VAR_DECL
1544 && TREE_CODE (TREE_TYPE (format_tree)) == ARRAY_TYPE
1545 && (array_init = decl_constant_value (format_tree)) != format_tree
1546 && TREE_CODE (array_init) == STRING_CST)
1548 /* Extract the string constant initializer. Note that this may include
1549 a trailing NUL character that is not in the array (e.g.
1550 const char a[3] = "foo";). */
1551 array_size = DECL_SIZE_UNIT (format_tree);
1552 format_tree = array_init;
1554 if (TREE_CODE (format_tree) != STRING_CST)
1556 res->number_non_literal++;
1559 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree))) != char_type_node)
1564 format_chars = TREE_STRING_POINTER (format_tree);
1565 format_length = TREE_STRING_LENGTH (format_tree);
1566 if (array_size != 0)
1568 /* Variable length arrays can't be initialized. */
1569 gcc_assert (TREE_CODE (array_size) == INTEGER_CST);
1571 if (host_integerp (array_size, 0))
1573 HOST_WIDE_INT array_size_value = TREE_INT_CST_LOW (array_size);
1574 if (array_size_value > 0
1575 && array_size_value == (int) array_size_value
1576 && format_length > array_size_value)
1577 format_length = array_size_value;
1582 if (offset >= format_length)
1584 res->number_non_literal++;
1587 format_chars += offset;
1588 format_length -= offset;
1590 if (format_length < 1 || format_chars[--format_length] != 0)
1592 res->number_unterminated++;
1595 if (format_length == 0)
1597 res->number_empty++;
1601 /* Skip to first argument to check. */
1602 while (arg_num + 1 < info->first_arg_num)
1606 params = TREE_CHAIN (params);
1609 /* Provisionally increment res->number_other; check_format_info_main
1610 will decrement it if it finds there are extra arguments, but this way
1611 need not adjust it for every return. */
1612 res->number_other++;
1613 fwt_pool = create_alloc_pool ("format_wanted_type pool",
1614 sizeof (format_wanted_type), 10);
1615 check_format_info_main (res, info, format_chars, format_length,
1616 params, arg_num, fwt_pool);
1617 free_alloc_pool (fwt_pool);
1621 /* Do the main part of checking a call to a format function. FORMAT_CHARS
1622 is the NUL-terminated format string (which at this point may contain
1623 internal NUL characters); FORMAT_LENGTH is its length (excluding the
1624 terminating NUL character). ARG_NUM is one less than the number of
1625 the first format argument to check; PARAMS points to that format
1626 argument in the list of arguments. */
1629 check_format_info_main (format_check_results *res,
1630 function_format_info *info, const char *format_chars,
1631 int format_length, tree params,
1632 unsigned HOST_WIDE_INT arg_num, alloc_pool fwt_pool)
1634 const char *orig_format_chars = format_chars;
1635 tree first_fillin_param = params;
1637 const format_kind_info *fki = &format_types[info->format_type];
1638 const format_flag_spec *flag_specs = fki->flag_specs;
1639 const format_flag_pair *bad_flag_pairs = fki->bad_flag_pairs;
1641 /* -1 if no conversions taking an operand have been found; 0 if one has
1642 and it didn't use $; 1 if $ formats are in use. */
1643 int has_operand_number = -1;
1645 init_dollar_format_checking (info->first_arg_num, first_fillin_param);
1647 while (*format_chars != 0)
1650 int suppressed = FALSE;
1651 const char *length_chars = NULL;
1652 enum format_lengths length_chars_val = FMT_LEN_none;
1653 enum format_std_version length_chars_std = STD_C89;
1657 int main_arg_num = 0;
1658 tree main_arg_params = 0;
1659 enum format_std_version wanted_type_std;
1660 const char *wanted_type_name;
1661 format_wanted_type width_wanted_type;
1662 format_wanted_type precision_wanted_type;
1663 format_wanted_type main_wanted_type;
1664 format_wanted_type *first_wanted_type = NULL;
1665 format_wanted_type *last_wanted_type = NULL;
1666 const format_length_info *fli = NULL;
1667 const format_char_info *fci = NULL;
1668 char flag_chars[256];
1670 int scalar_identity_flag = 0;
1671 const char *format_start;
1673 if (*format_chars++ != '%')
1675 if (*format_chars == 0)
1677 warning (OPT_Wformat, "spurious trailing %<%%%> in format");
1680 if (*format_chars == '%')
1687 if ((fki->flags & (int) FMT_FLAG_USE_DOLLAR) && has_operand_number != 0)
1689 /* Possibly read a $ operand number at the start of the format.
1690 If one was previously used, one is required here. If one
1691 is not used here, we can't immediately conclude this is a
1692 format without them, since it could be printf %m or scanf %*. */
1694 opnum = maybe_read_dollar_number (&format_chars, 0,
1696 &main_arg_params, fki);
1701 has_operand_number = 1;
1702 main_arg_num = opnum + info->first_arg_num - 1;
1705 else if (fki->flags & FMT_FLAG_USE_DOLLAR)
1707 if (avoid_dollar_number (format_chars))
1711 /* Read any format flags, but do not yet validate them beyond removing
1712 duplicates, since in general validation depends on the rest of
1714 while (*format_chars != 0
1715 && strchr (fki->flag_chars, *format_chars) != 0)
1717 const format_flag_spec *s = get_flag_spec (flag_specs,
1718 *format_chars, NULL);
1719 if (strchr (flag_chars, *format_chars) != 0)
1721 warning (OPT_Wformat, "repeated %s in format", _(s->name));
1725 i = strlen (flag_chars);
1726 flag_chars[i++] = *format_chars;
1729 if (s->skip_next_char)
1732 if (*format_chars == 0)
1734 warning (OPT_Wformat, "missing fill character at end of strfmon format");
1741 /* Read any format width, possibly * or *m$. */
1742 if (fki->width_char != 0)
1744 if (fki->width_type != NULL && *format_chars == '*')
1746 i = strlen (flag_chars);
1747 flag_chars[i++] = fki->width_char;
1749 /* "...a field width...may be indicated by an asterisk.
1750 In this case, an int argument supplies the field width..." */
1752 if (has_operand_number != 0)
1755 opnum = maybe_read_dollar_number (&format_chars,
1756 has_operand_number == 1,
1763 has_operand_number = 1;
1764 arg_num = opnum + info->first_arg_num - 1;
1767 has_operand_number = 0;
1771 if (avoid_dollar_number (format_chars))
1774 if (info->first_arg_num != 0)
1780 cur_param = TREE_VALUE (params);
1781 if (has_operand_number <= 0)
1783 params = TREE_CHAIN (params);
1787 width_wanted_type.wanted_type = *fki->width_type;
1788 width_wanted_type.wanted_type_name = NULL;
1789 width_wanted_type.pointer_count = 0;
1790 width_wanted_type.char_lenient_flag = 0;
1791 width_wanted_type.scalar_identity_flag = 0;
1792 width_wanted_type.writing_in_flag = 0;
1793 width_wanted_type.reading_from_flag = 0;
1794 width_wanted_type.kind = CF_KIND_FIELD_WIDTH;
1795 width_wanted_type.format_start = format_chars - 1;
1796 width_wanted_type.format_length = 1;
1797 width_wanted_type.param = cur_param;
1798 width_wanted_type.arg_num = arg_num;
1799 width_wanted_type.next = NULL;
1800 if (last_wanted_type != 0)
1801 last_wanted_type->next = &width_wanted_type;
1802 if (first_wanted_type == 0)
1803 first_wanted_type = &width_wanted_type;
1804 last_wanted_type = &width_wanted_type;
1809 /* Possibly read a numeric width. If the width is zero,
1810 we complain if appropriate. */
1811 int non_zero_width_char = FALSE;
1812 int found_width = FALSE;
1813 while (ISDIGIT (*format_chars))
1816 if (*format_chars != '0')
1817 non_zero_width_char = TRUE;
1820 if (found_width && !non_zero_width_char &&
1821 (fki->flags & (int) FMT_FLAG_ZERO_WIDTH_BAD))
1822 warning (OPT_Wformat, "zero width in %s format", fki->name);
1825 i = strlen (flag_chars);
1826 flag_chars[i++] = fki->width_char;
1832 /* Read any format left precision (must be a number, not *). */
1833 if (fki->left_precision_char != 0 && *format_chars == '#')
1836 i = strlen (flag_chars);
1837 flag_chars[i++] = fki->left_precision_char;
1839 if (!ISDIGIT (*format_chars))
1840 warning (OPT_Wformat, "empty left precision in %s format", fki->name);
1841 while (ISDIGIT (*format_chars))
1845 /* Read any format precision, possibly * or *m$. */
1846 if (fki->precision_char != 0 && *format_chars == '.')
1849 i = strlen (flag_chars);
1850 flag_chars[i++] = fki->precision_char;
1852 if (fki->precision_type != NULL && *format_chars == '*')
1854 /* "...a...precision...may be indicated by an asterisk.
1855 In this case, an int argument supplies the...precision." */
1857 if (has_operand_number != 0)
1860 opnum = maybe_read_dollar_number (&format_chars,
1861 has_operand_number == 1,
1868 has_operand_number = 1;
1869 arg_num = opnum + info->first_arg_num - 1;
1872 has_operand_number = 0;
1876 if (avoid_dollar_number (format_chars))
1879 if (info->first_arg_num != 0)
1885 cur_param = TREE_VALUE (params);
1886 if (has_operand_number <= 0)
1888 params = TREE_CHAIN (params);
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.scalar_identity_flag = 0;
1897 precision_wanted_type.writing_in_flag = 0;
1898 precision_wanted_type.reading_from_flag = 0;
1899 precision_wanted_type.kind = CF_KIND_FIELD_PRECISION;
1900 precision_wanted_type.param = cur_param;
1901 precision_wanted_type.format_start = format_chars - 2;
1902 precision_wanted_type.format_length = 2;
1903 precision_wanted_type.arg_num = arg_num;
1904 precision_wanted_type.next = NULL;
1905 if (last_wanted_type != 0)
1906 last_wanted_type->next = &precision_wanted_type;
1907 if (first_wanted_type == 0)
1908 first_wanted_type = &precision_wanted_type;
1909 last_wanted_type = &precision_wanted_type;
1914 if (!(fki->flags & (int) FMT_FLAG_EMPTY_PREC_OK)
1915 && !ISDIGIT (*format_chars))
1916 warning (OPT_Wformat, "empty precision in %s format", fki->name);
1917 while (ISDIGIT (*format_chars))
1922 format_start = format_chars;
1923 if (fki->alloc_char && fki->alloc_char == *format_chars)
1925 i = strlen (flag_chars);
1926 flag_chars[i++] = fki->alloc_char;
1931 /* Handle the scanf allocation kludge. */
1932 if (fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
1934 if (*format_chars == 'a' && !flag_isoc99)
1936 if (format_chars[1] == 's' || format_chars[1] == 'S'
1937 || format_chars[1] == '[')
1939 /* 'a' is used as a flag. */
1940 i = strlen (flag_chars);
1941 flag_chars[i++] = 'a';
1948 /* Read any length modifier, if this kind of format has them. */
1949 fli = fki->length_char_specs;
1950 length_chars = NULL;
1951 length_chars_val = FMT_LEN_none;
1952 length_chars_std = STD_C89;
1953 scalar_identity_flag = 0;
1956 while (fli->name != 0
1957 && strncmp (fli->name, format_chars, strlen (fli->name)))
1961 format_chars += strlen (fli->name);
1962 if (fli->double_name != 0 && fli->name[0] == *format_chars)
1965 length_chars = fli->double_name;
1966 length_chars_val = fli->double_index;
1967 length_chars_std = fli->double_std;
1971 length_chars = fli->name;
1972 length_chars_val = fli->index;
1973 length_chars_std = fli->std;
1974 scalar_identity_flag = fli->scalar_identity_flag;
1976 i = strlen (flag_chars);
1977 flag_chars[i++] = fki->length_code_char;
1982 /* Warn if the length modifier is non-standard. */
1983 if (ADJ_STD (length_chars_std) > C_STD_VER)
1984 warning (OPT_Wformat,
1985 "%s does not support the %qs %s length modifier",
1986 C_STD_NAME (length_chars_std), length_chars,
1991 /* Read any modifier (strftime E/O). */
1992 if (fki->modifier_chars != NULL)
1994 while (*format_chars != 0
1995 && strchr (fki->modifier_chars, *format_chars) != 0)
1997 if (strchr (flag_chars, *format_chars) != 0)
1999 const format_flag_spec *s = get_flag_spec (flag_specs,
2000 *format_chars, NULL);
2001 warning (OPT_Wformat, "repeated %s in format", _(s->name));
2005 i = strlen (flag_chars);
2006 flag_chars[i++] = *format_chars;
2013 format_char = *format_chars;
2014 if (format_char == 0
2015 || (!(fki->flags & (int) FMT_FLAG_FANCY_PERCENT_OK)
2016 && format_char == '%'))
2018 warning (OPT_Wformat, "conversion lacks type at end of format");
2022 fci = fki->conversion_specs;
2023 while (fci->format_chars != 0
2024 && strchr (fci->format_chars, format_char) == 0)
2026 if (fci->format_chars == 0)
2028 if (ISGRAPH (format_char))
2029 warning (OPT_Wformat, "unknown conversion type character %qc in format",
2032 warning (OPT_Wformat, "unknown conversion type character 0x%x in format",
2038 if (ADJ_STD (fci->std) > C_STD_VER)
2039 warning (OPT_Wformat, "%s does not support the %<%%%c%> %s format",
2040 C_STD_NAME (fci->std), format_char, fki->name);
2043 /* Validate the individual flags used, removing any that are invalid. */
2046 for (i = 0; flag_chars[i] != 0; i++)
2048 const format_flag_spec *s = get_flag_spec (flag_specs,
2049 flag_chars[i], NULL);
2050 flag_chars[i - d] = flag_chars[i];
2051 if (flag_chars[i] == fki->length_code_char)
2053 if (strchr (fci->flag_chars, flag_chars[i]) == 0)
2055 warning (OPT_Wformat, "%s used with %<%%%c%> %s format",
2056 _(s->name), format_char, fki->name);
2062 const format_flag_spec *t;
2063 if (ADJ_STD (s->std) > C_STD_VER)
2064 warning (OPT_Wformat, "%s does not support %s",
2065 C_STD_NAME (s->std), _(s->long_name));
2066 t = get_flag_spec (flag_specs, flag_chars[i], fci->flags2);
2067 if (t != NULL && ADJ_STD (t->std) > ADJ_STD (s->std))
2069 const char *long_name = (t->long_name != NULL
2072 if (ADJ_STD (t->std) > C_STD_VER)
2073 warning (OPT_Wformat,
2074 "%s does not support %s with the %<%%%c%> %s format",
2075 C_STD_NAME (t->std), _(long_name),
2076 format_char, fki->name);
2080 flag_chars[i - d] = 0;
2083 if ((fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
2084 && strchr (flag_chars, 'a') != 0)
2086 if (fki->alloc_char && strchr (flag_chars, fki->alloc_char) != 0)
2089 if (fki->suppression_char
2090 && strchr (flag_chars, fki->suppression_char) != 0)
2093 /* Validate the pairs of flags used. */
2094 for (i = 0; bad_flag_pairs[i].flag_char1 != 0; i++)
2096 const format_flag_spec *s, *t;
2097 if (strchr (flag_chars, bad_flag_pairs[i].flag_char1) == 0)
2099 if (strchr (flag_chars, bad_flag_pairs[i].flag_char2) == 0)
2101 if (bad_flag_pairs[i].predicate != 0
2102 && strchr (fci->flags2, bad_flag_pairs[i].predicate) == 0)
2104 s = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char1, NULL);
2105 t = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char2, NULL);
2106 if (bad_flag_pairs[i].ignored)
2108 if (bad_flag_pairs[i].predicate != 0)
2109 warning (OPT_Wformat,
2110 "%s ignored with %s and %<%%%c%> %s format",
2111 _(s->name), _(t->name), format_char,
2114 warning (OPT_Wformat, "%s ignored with %s in %s format",
2115 _(s->name), _(t->name), fki->name);
2119 if (bad_flag_pairs[i].predicate != 0)
2120 warning (OPT_Wformat,
2121 "use of %s and %s together with %<%%%c%> %s format",
2122 _(s->name), _(t->name), format_char,
2125 warning (OPT_Wformat, "use of %s and %s together in %s format",
2126 _(s->name), _(t->name), fki->name);
2130 /* Give Y2K warnings. */
2131 if (warn_format_y2k)
2134 if (strchr (fci->flags2, '4') != 0)
2135 if (strchr (flag_chars, 'E') != 0)
2139 else if (strchr (fci->flags2, '3') != 0)
2141 else if (strchr (fci->flags2, '2') != 0)
2144 warning (OPT_Wformat_y2k, "%<%%%c%> yields only last 2 digits of "
2145 "year in some locales", format_char);
2146 else if (y2k_level == 2)
2147 warning (OPT_Wformat_y2k, "%<%%%c%> yields only last 2 digits of "
2148 "year", format_char);
2151 if (strchr (fci->flags2, '[') != 0)
2153 /* Skip over scan set, in case it happens to have '%' in it. */
2154 if (*format_chars == '^')
2156 /* Find closing bracket; if one is hit immediately, then
2157 it's part of the scan set rather than a terminator. */
2158 if (*format_chars == ']')
2160 while (*format_chars && *format_chars != ']')
2162 if (*format_chars != ']')
2163 /* The end of the format string was reached. */
2164 warning (OPT_Wformat, "no closing %<]%> for %<%%[%> format");
2168 wanted_type_name = 0;
2169 if (fki->flags & (int) FMT_FLAG_ARG_CONVERT)
2171 wanted_type = (fci->types[length_chars_val].type
2172 ? *fci->types[length_chars_val].type : 0);
2173 wanted_type_name = fci->types[length_chars_val].name;
2174 wanted_type_std = fci->types[length_chars_val].std;
2175 if (wanted_type == 0)
2177 warning (OPT_Wformat,
2178 "use of %qs length modifier with %qc type character",
2179 length_chars, format_char);
2180 /* Heuristic: skip one argument when an invalid length/type
2181 combination is encountered. */
2184 params = TREE_CHAIN (params);
2188 /* Warn if non-standard, provided it is more non-standard
2189 than the length and type characters that may already
2190 have been warned for. */
2191 && ADJ_STD (wanted_type_std) > ADJ_STD (length_chars_std)
2192 && ADJ_STD (wanted_type_std) > ADJ_STD (fci->std))
2194 if (ADJ_STD (wanted_type_std) > C_STD_VER)
2195 warning (OPT_Wformat,
2196 "%s does not support the %<%%%s%c%> %s format",
2197 C_STD_NAME (wanted_type_std), length_chars,
2198 format_char, fki->name);
2202 main_wanted_type.next = NULL;
2204 /* Finally. . .check type of argument against desired type! */
2205 if (info->first_arg_num == 0)
2207 if ((fci->pointer_count == 0 && wanted_type == void_type_node)
2210 if (main_arg_num != 0)
2213 warning (OPT_Wformat, "operand number specified with "
2214 "suppressed assignment");
2216 warning (OPT_Wformat, "operand number specified for format "
2217 "taking no argument");
2222 format_wanted_type *wanted_type_ptr;
2224 if (main_arg_num != 0)
2226 arg_num = main_arg_num;
2227 params = main_arg_params;
2232 if (has_operand_number > 0)
2234 warning (OPT_Wformat, "missing $ operand number in format");
2238 has_operand_number = 0;
2241 wanted_type_ptr = &main_wanted_type;
2248 cur_param = TREE_VALUE (params);
2249 params = TREE_CHAIN (params);
2252 wanted_type_ptr->wanted_type = wanted_type;
2253 wanted_type_ptr->wanted_type_name = wanted_type_name;
2254 wanted_type_ptr->pointer_count = fci->pointer_count + alloc_flag;
2255 wanted_type_ptr->char_lenient_flag = 0;
2256 if (strchr (fci->flags2, 'c') != 0)
2257 wanted_type_ptr->char_lenient_flag = 1;
2258 wanted_type_ptr->scalar_identity_flag = 0;
2259 if (scalar_identity_flag)
2260 wanted_type_ptr->scalar_identity_flag = 1;
2261 wanted_type_ptr->writing_in_flag = 0;
2262 wanted_type_ptr->reading_from_flag = 0;
2264 wanted_type_ptr->writing_in_flag = 1;
2267 if (strchr (fci->flags2, 'W') != 0)
2268 wanted_type_ptr->writing_in_flag = 1;
2269 if (strchr (fci->flags2, 'R') != 0)
2270 wanted_type_ptr->reading_from_flag = 1;
2272 wanted_type_ptr->kind = CF_KIND_FORMAT;
2273 wanted_type_ptr->param = cur_param;
2274 wanted_type_ptr->arg_num = arg_num;
2275 wanted_type_ptr->format_start = format_start;
2276 wanted_type_ptr->format_length = format_chars - format_start;
2277 wanted_type_ptr->next = NULL;
2278 if (last_wanted_type != 0)
2279 last_wanted_type->next = wanted_type_ptr;
2280 if (first_wanted_type == 0)
2281 first_wanted_type = wanted_type_ptr;
2282 last_wanted_type = wanted_type_ptr;
2287 wanted_type_ptr = (format_wanted_type *)
2288 pool_alloc (fwt_pool);
2290 wanted_type = *fci->types[length_chars_val].type;
2291 wanted_type_name = fci->types[length_chars_val].name;
2296 if (first_wanted_type != 0)
2297 check_format_types (first_wanted_type);
2300 if (format_chars - orig_format_chars != format_length)
2301 warning (OPT_Wformat_contains_nul, "embedded %<\\0%> in format");
2302 if (info->first_arg_num != 0 && params != 0
2303 && has_operand_number <= 0)
2305 res->number_other--;
2306 res->number_extra_args++;
2308 if (has_operand_number > 0)
2309 finish_dollar_format_checking (res, fki->flags & (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK);
2313 /* Check the argument types from a single format conversion (possibly
2314 including width and precision arguments). */
2316 check_format_types (format_wanted_type *types)
2318 for (; types != 0; types = types->next)
2328 wanted_type = types->wanted_type;
2329 arg_num = types->arg_num;
2331 /* The following should not occur here. */
2332 gcc_assert (wanted_type);
2333 gcc_assert (wanted_type != void_type_node || types->pointer_count);
2335 if (types->pointer_count == 0)
2336 wanted_type = lang_hooks.types.type_promotes_to (wanted_type);
2338 wanted_type = TYPE_MAIN_VARIANT (wanted_type);
2340 cur_param = types->param;
2343 format_type_warning (types, wanted_type, NULL);
2347 cur_type = TREE_TYPE (cur_param);
2348 if (cur_type == error_mark_node)
2350 orig_cur_type = cur_type;
2353 STRIP_NOPS (cur_param);
2355 /* Check the types of any additional pointer arguments
2356 that precede the "real" argument. */
2357 for (i = 0; i < types->pointer_count; ++i)
2359 if (TREE_CODE (cur_type) == POINTER_TYPE)
2361 cur_type = TREE_TYPE (cur_type);
2362 if (cur_type == error_mark_node)
2365 /* Check for writing through a NULL pointer. */
2366 if (types->writing_in_flag
2369 && integer_zerop (cur_param))
2370 warning (OPT_Wformat, "writing through null pointer "
2371 "(argument %d)", arg_num);
2373 /* Check for reading through a NULL pointer. */
2374 if (types->reading_from_flag
2377 && integer_zerop (cur_param))
2378 warning (OPT_Wformat, "reading through null pointer "
2379 "(argument %d)", arg_num);
2381 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
2382 cur_param = TREE_OPERAND (cur_param, 0);
2386 /* See if this is an attempt to write into a const type with
2387 scanf or with printf "%n". Note: the writing in happens
2388 at the first indirection only, if for example
2389 void * const * is passed to scanf %p; passing
2390 const void ** is simply passing an incompatible type. */
2391 if (types->writing_in_flag
2393 && (TYPE_READONLY (cur_type)
2395 && (CONSTANT_CLASS_P (cur_param)
2396 || (DECL_P (cur_param)
2397 && TREE_READONLY (cur_param))))))
2398 warning (OPT_Wformat, "writing into constant object "
2399 "(argument %d)", arg_num);
2401 /* If there are extra type qualifiers beyond the first
2402 indirection, then this makes the types technically
2406 && (TYPE_READONLY (cur_type)
2407 || TYPE_VOLATILE (cur_type)
2408 || TYPE_RESTRICT (cur_type)))
2409 warning (OPT_Wformat, "extra type qualifiers in format "
2410 "argument (argument %d)",
2416 format_type_warning (types, wanted_type, orig_cur_type);
2421 if (i < types->pointer_count)
2424 cur_type = TYPE_MAIN_VARIANT (cur_type);
2426 /* Check whether the argument type is a character type. This leniency
2427 only applies to certain formats, flagged with 'c'.
2429 if (types->char_lenient_flag)
2430 char_type_flag = (cur_type == char_type_node
2431 || cur_type == signed_char_type_node
2432 || cur_type == unsigned_char_type_node);
2434 /* Check the type of the "real" argument, if there's a type we want. */
2435 if (lang_hooks.types_compatible_p (wanted_type, cur_type))
2437 /* If we want 'void *', allow any pointer type.
2438 (Anything else would already have got a warning.)
2439 With -pedantic, only allow pointers to void and to character
2441 if (wanted_type == void_type_node
2442 && (!pedantic || (i == 1 && char_type_flag)))
2444 /* Don't warn about differences merely in signedness, unless
2445 -pedantic. With -pedantic, warn if the type is a pointer
2446 target and not a character type, and for character types at
2447 a second level of indirection. */
2448 if (TREE_CODE (wanted_type) == INTEGER_TYPE
2449 && TREE_CODE (cur_type) == INTEGER_TYPE
2450 && (!pedantic || i == 0 || (i == 1 && char_type_flag))
2451 && (TYPE_UNSIGNED (wanted_type)
2452 ? wanted_type == c_common_unsigned_type (cur_type)
2453 : wanted_type == c_common_signed_type (cur_type)))
2455 /* Likewise, "signed char", "unsigned char" and "char" are
2456 equivalent but the above test won't consider them equivalent. */
2457 if (wanted_type == char_type_node
2458 && (!pedantic || i < 2)
2461 if (types->scalar_identity_flag
2462 && (TREE_CODE (cur_type) == TREE_CODE (wanted_type)
2463 || (INTEGRAL_TYPE_P (cur_type)
2464 && INTEGRAL_TYPE_P (wanted_type)))
2465 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type))
2467 /* Now we have a type mismatch. */
2468 format_type_warning (types, wanted_type, orig_cur_type);
2473 /* Give a warning about a format argument of different type from that
2474 expected. WANTED_TYPE is the type the argument should have, possibly
2475 stripped of pointer dereferences. The description (such as "field
2476 precision"), the placement in the format string, a possibly more
2477 friendly name of WANTED_TYPE, and the number of pointer dereferences
2478 are taken from TYPE. ARG_TYPE is the type of the actual argument,
2479 or NULL if it is missing. */
2481 format_type_warning (format_wanted_type *type, tree wanted_type, tree arg_type)
2483 int kind = type->kind;
2484 const char *wanted_type_name = type->wanted_type_name;
2485 const char *format_start = type->format_start;
2486 int format_length = type->format_length;
2487 int pointer_count = type->pointer_count;
2488 int arg_num = type->arg_num;
2491 /* If ARG_TYPE is a typedef with a misleading name (for example,
2492 size_t but not the standard size_t expected by printf %zu), avoid
2493 printing the typedef name. */
2494 if (wanted_type_name
2496 && TYPE_NAME (arg_type)
2497 && TREE_CODE (TYPE_NAME (arg_type)) == TYPE_DECL
2498 && DECL_NAME (TYPE_NAME (arg_type))
2499 && !strcmp (wanted_type_name,
2500 lang_hooks.decl_printable_name (TYPE_NAME (arg_type), 2)))
2501 arg_type = TYPE_MAIN_VARIANT (arg_type);
2502 /* The format type and name exclude any '*' for pointers, so those
2503 must be formatted manually. For all the types we currently have,
2504 this is adequate, but formats taking pointers to functions or
2505 arrays would require the full type to be built up in order to
2506 print it with %T. */
2507 p = (char *) alloca (pointer_count + 2);
2508 if (pointer_count == 0)
2510 else if (c_dialect_cxx ())
2512 memset (p, '*', pointer_count);
2513 p[pointer_count] = 0;
2518 memset (p + 1, '*', pointer_count);
2519 p[pointer_count + 1] = 0;
2522 if (wanted_type_name)
2525 warning (OPT_Wformat, "%s %<%s%.*s%> expects argument of type %<%s%s%>, "
2526 "but argument %d has type %qT",
2527 gettext (kind_descriptions[kind]),
2528 (kind == CF_KIND_FORMAT ? "%" : ""),
2529 format_length, format_start,
2530 wanted_type_name, p, arg_num, arg_type);
2532 warning (OPT_Wformat, "%s %<%s%.*s%> expects a matching %<%s%s%> argument",
2533 gettext (kind_descriptions[kind]),
2534 (kind == CF_KIND_FORMAT ? "%" : ""),
2535 format_length, format_start, wanted_type_name, p);
2540 warning (OPT_Wformat, "%s %<%s%.*s%> expects argument of type %<%T%s%>, "
2541 "but argument %d has type %qT",
2542 gettext (kind_descriptions[kind]),
2543 (kind == CF_KIND_FORMAT ? "%" : ""),
2544 format_length, format_start,
2545 wanted_type, p, arg_num, arg_type);
2547 warning (OPT_Wformat, "%s %<%s%.*s%> expects a matching %<%T%s%> argument",
2548 gettext (kind_descriptions[kind]),
2549 (kind == CF_KIND_FORMAT ? "%" : ""),
2550 format_length, format_start, wanted_type, p);
2555 /* Given a format_char_info array FCI, and a character C, this function
2556 returns the index into the conversion_specs where that specifier's
2557 data is located. The character must exist. */
2559 find_char_info_specifier_index (const format_char_info *fci, int c)
2563 for (i = 0; fci->format_chars; i++, fci++)
2564 if (strchr (fci->format_chars, c))
2567 /* We shouldn't be looking for a non-existent specifier. */
2571 /* Given a format_length_info array FLI, and a character C, this
2572 function returns the index into the conversion_specs where that
2573 modifier's data is located. The character must exist. */
2575 find_length_info_modifier_index (const format_length_info *fli, int c)
2579 for (i = 0; fli->name; i++, fli++)
2580 if (strchr (fli->name, c))
2583 /* We shouldn't be looking for a non-existent modifier. */
2587 /* Determine the type of HOST_WIDE_INT in the code being compiled for
2588 use in GCC's __asm_fprintf__ custom format attribute. You must
2589 have set dynamic_format_types before calling this function. */
2591 init_dynamic_asm_fprintf_info (void)
2597 format_length_info *new_asm_fprintf_length_specs;
2600 /* Find the underlying type for HOST_WIDE_INT. For the %w
2601 length modifier to work, one must have issued: "typedef
2602 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2603 prior to using that modifier. */
2604 hwi = maybe_get_identifier ("__gcc_host_wide_int__");
2607 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2610 hwi = identifier_global_value (hwi);
2611 if (!hwi || TREE_CODE (hwi) != TYPE_DECL)
2613 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2616 hwi = DECL_ORIGINAL_TYPE (hwi);
2618 if (hwi != long_integer_type_node && hwi != long_long_integer_type_node)
2620 error ("%<__gcc_host_wide_int__%> is not defined as %<long%>"
2621 " or %<long long%>");
2625 /* Create a new (writable) copy of asm_fprintf_length_specs. */
2626 new_asm_fprintf_length_specs = (format_length_info *)
2627 xmemdup (asm_fprintf_length_specs,
2628 sizeof (asm_fprintf_length_specs),
2629 sizeof (asm_fprintf_length_specs));
2631 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2632 i = find_length_info_modifier_index (new_asm_fprintf_length_specs, 'w');
2633 if (hwi == long_integer_type_node)
2634 new_asm_fprintf_length_specs[i].index = FMT_LEN_l;
2635 else if (hwi == long_long_integer_type_node)
2636 new_asm_fprintf_length_specs[i].index = FMT_LEN_ll;
2640 /* Assign the new data for use. */
2641 dynamic_format_types[asm_fprintf_format_type].length_char_specs =
2642 new_asm_fprintf_length_specs;
2646 /* Determine the type of a "locus" in the code being compiled for use
2647 in GCC's __gcc_gfc__ custom format attribute. You must have set
2648 dynamic_format_types before calling this function. */
2650 init_dynamic_gfc_info (void)
2656 static format_char_info *gfc_fci;
2658 /* For the GCC __gcc_gfc__ custom format specifier to work, one
2659 must have declared 'locus' prior to using this attribute. If
2660 we haven't seen this declarations then you shouldn't use the
2661 specifier requiring that type. */
2662 if ((locus = maybe_get_identifier ("locus")))
2664 locus = identifier_global_value (locus);
2667 if (TREE_CODE (locus) != TYPE_DECL
2668 || TREE_TYPE (locus) == error_mark_node)
2670 error ("%<locus%> is not defined as a type");
2674 locus = TREE_TYPE (locus);
2678 /* Assign the new data for use. */
2680 /* Handle the __gcc_gfc__ format specifics. */
2682 dynamic_format_types[gcc_gfc_format_type].conversion_specs =
2683 gfc_fci = (format_char_info *)
2684 xmemdup (gcc_gfc_char_table,
2685 sizeof (gcc_gfc_char_table),
2686 sizeof (gcc_gfc_char_table));
2689 const unsigned i = find_char_info_specifier_index (gfc_fci, 'L');
2690 gfc_fci[i].types[0].type = &locus;
2691 gfc_fci[i].pointer_count = 1;
2696 /* Determine the types of "tree" and "location_t" in the code being
2697 compiled for use in GCC's diagnostic custom format attributes. You
2698 must have set dynamic_format_types before calling this function. */
2700 init_dynamic_diag_info (void)
2702 static tree t, loc, hwi;
2704 if (!loc || !t || !hwi)
2706 static format_char_info *diag_fci, *tdiag_fci, *cdiag_fci, *cxxdiag_fci;
2707 static format_length_info *diag_ls;
2710 /* For the GCC-diagnostics custom format specifiers to work, one
2711 must have declared 'tree' and/or 'location_t' prior to using
2712 those attributes. If we haven't seen these declarations then
2713 you shouldn't use the specifiers requiring these types.
2714 However we don't force a hard ICE because we may see only one
2715 or the other type. */
2716 if ((loc = maybe_get_identifier ("location_t")))
2718 loc = identifier_global_value (loc);
2721 if (TREE_CODE (loc) != TYPE_DECL)
2723 error ("%<location_t%> is not defined as a type");
2727 loc = TREE_TYPE (loc);
2731 /* We need to grab the underlying 'union tree_node' so peek into
2732 an extra type level. */
2733 if ((t = maybe_get_identifier ("tree")))
2735 t = identifier_global_value (t);
2738 if (TREE_CODE (t) != TYPE_DECL)
2740 error ("%<tree%> is not defined as a type");
2743 else if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
2745 error ("%<tree%> is not defined as a pointer type");
2749 t = TREE_TYPE (TREE_TYPE (t));
2753 /* Find the underlying type for HOST_WIDE_INT. For the %w
2754 length modifier to work, one must have issued: "typedef
2755 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2756 prior to using that modifier. */
2757 if ((hwi = maybe_get_identifier ("__gcc_host_wide_int__")))
2759 hwi = identifier_global_value (hwi);
2762 if (TREE_CODE (hwi) != TYPE_DECL)
2764 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2769 hwi = DECL_ORIGINAL_TYPE (hwi);
2771 if (hwi != long_integer_type_node
2772 && hwi != long_long_integer_type_node)
2774 error ("%<__gcc_host_wide_int__%> is not defined"
2775 " as %<long%> or %<long long%>");
2782 /* Assign the new data for use. */
2784 /* All the GCC diag formats use the same length specs. */
2786 dynamic_format_types[gcc_diag_format_type].length_char_specs =
2787 dynamic_format_types[gcc_tdiag_format_type].length_char_specs =
2788 dynamic_format_types[gcc_cdiag_format_type].length_char_specs =
2789 dynamic_format_types[gcc_cxxdiag_format_type].length_char_specs =
2790 diag_ls = (format_length_info *)
2791 xmemdup (gcc_diag_length_specs,
2792 sizeof (gcc_diag_length_specs),
2793 sizeof (gcc_diag_length_specs));
2796 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2797 i = find_length_info_modifier_index (diag_ls, 'w');
2798 if (hwi == long_integer_type_node)
2799 diag_ls[i].index = FMT_LEN_l;
2800 else if (hwi == long_long_integer_type_node)
2801 diag_ls[i].index = FMT_LEN_ll;
2806 /* Handle the __gcc_diag__ format specifics. */
2808 dynamic_format_types[gcc_diag_format_type].conversion_specs =
2809 diag_fci = (format_char_info *)
2810 xmemdup (gcc_diag_char_table,
2811 sizeof (gcc_diag_char_table),
2812 sizeof (gcc_diag_char_table));
2815 i = find_char_info_specifier_index (diag_fci, 'K');
2816 diag_fci[i].types[0].type = &t;
2817 diag_fci[i].pointer_count = 1;
2820 /* Handle the __gcc_tdiag__ format specifics. */
2822 dynamic_format_types[gcc_tdiag_format_type].conversion_specs =
2823 tdiag_fci = (format_char_info *)
2824 xmemdup (gcc_tdiag_char_table,
2825 sizeof (gcc_tdiag_char_table),
2826 sizeof (gcc_tdiag_char_table));
2829 /* All specifiers taking a tree share the same struct. */
2830 i = find_char_info_specifier_index (tdiag_fci, 'D');
2831 tdiag_fci[i].types[0].type = &t;
2832 tdiag_fci[i].pointer_count = 1;
2833 i = find_char_info_specifier_index (tdiag_fci, 'K');
2834 tdiag_fci[i].types[0].type = &t;
2835 tdiag_fci[i].pointer_count = 1;
2838 /* Handle the __gcc_cdiag__ format specifics. */
2840 dynamic_format_types[gcc_cdiag_format_type].conversion_specs =
2841 cdiag_fci = (format_char_info *)
2842 xmemdup (gcc_cdiag_char_table,
2843 sizeof (gcc_cdiag_char_table),
2844 sizeof (gcc_cdiag_char_table));
2847 /* All specifiers taking a tree share the same struct. */
2848 i = find_char_info_specifier_index (cdiag_fci, 'D');
2849 cdiag_fci[i].types[0].type = &t;
2850 cdiag_fci[i].pointer_count = 1;
2851 i = find_char_info_specifier_index (cdiag_fci, 'K');
2852 cdiag_fci[i].types[0].type = &t;
2853 cdiag_fci[i].pointer_count = 1;
2856 /* Handle the __gcc_cxxdiag__ format specifics. */
2858 dynamic_format_types[gcc_cxxdiag_format_type].conversion_specs =
2859 cxxdiag_fci = (format_char_info *)
2860 xmemdup (gcc_cxxdiag_char_table,
2861 sizeof (gcc_cxxdiag_char_table),
2862 sizeof (gcc_cxxdiag_char_table));
2865 /* All specifiers taking a tree share the same struct. */
2866 i = find_char_info_specifier_index (cxxdiag_fci, 'D');
2867 cxxdiag_fci[i].types[0].type = &t;
2868 cxxdiag_fci[i].pointer_count = 1;
2869 i = find_char_info_specifier_index (cxxdiag_fci, 'K');
2870 cxxdiag_fci[i].types[0].type = &t;
2871 cxxdiag_fci[i].pointer_count = 1;
2876 #ifdef TARGET_FORMAT_TYPES
2877 extern const format_kind_info TARGET_FORMAT_TYPES[];
2880 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
2881 extern const target_ovr_attr TARGET_OVERRIDES_FORMAT_ATTRIBUTES[];
2883 #ifdef TARGET_OVERRIDES_FORMAT_INIT
2884 extern void TARGET_OVERRIDES_FORMAT_INIT (void);
2887 /* Attributes such as "printf" are equivalent to those such as
2888 "gnu_printf" unless this is overridden by a target. */
2889 static const target_ovr_attr gnu_target_overrides_format_attributes[] =
2891 { "gnu_printf", "printf" },
2892 { "gnu_scanf", "scanf" },
2893 { "gnu_strftime", "strftime" },
2894 { "gnu_strfmon", "strfmon" },
2898 /* Translate to unified attribute name. This is used in decode_format_type and
2899 decode_format_attr. In attr_name the user specified argument is passed. It
2900 returns the unified format name from TARGET_OVERRIDES_FORMAT_ATTRIBUTES
2901 or the attr_name passed to this function, if there is no matching entry. */
2903 convert_format_name_to_system_name (const char *attr_name)
2907 if (attr_name == NULL || *attr_name == 0
2908 || strncmp (attr_name, "gcc_", 4) == 0)
2910 #ifdef TARGET_OVERRIDES_FORMAT_INIT
2911 TARGET_OVERRIDES_FORMAT_INIT ();
2914 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
2915 /* Check if format attribute is overridden by target. */
2916 if (TARGET_OVERRIDES_FORMAT_ATTRIBUTES != NULL
2917 && TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT > 0)
2919 for (i = 0; i < TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT; ++i)
2921 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_src,
2924 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_dst,
2926 return TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_src;
2930 /* Otherwise default to gnu format. */
2932 gnu_target_overrides_format_attributes[i].named_attr_src != NULL;
2935 if (cmp_attribs (gnu_target_overrides_format_attributes[i].named_attr_src,
2938 if (cmp_attribs (gnu_target_overrides_format_attributes[i].named_attr_dst,
2940 return gnu_target_overrides_format_attributes[i].named_attr_src;
2946 /* Return true if TATTR_NAME and ATTR_NAME are the same format attribute,
2947 counting "name" and "__name__" as the same, false otherwise. */
2949 cmp_attribs (const char *tattr_name, const char *attr_name)
2951 int alen = strlen (attr_name);
2952 int slen = (tattr_name ? strlen (tattr_name) : 0);
2953 if (alen > 4 && attr_name[0] == '_' && attr_name[1] == '_'
2954 && attr_name[alen - 1] == '_' && attr_name[alen - 2] == '_')
2959 if (alen != slen || strncmp (tattr_name, attr_name, alen) != 0)
2964 /* Handle a "format" attribute; arguments as in
2965 struct attribute_spec.handler. */
2967 handle_format_attribute (tree *node, tree ARG_UNUSED (name), tree args,
2968 int flags, bool *no_add_attrs)
2971 function_format_info info;
2973 #ifdef TARGET_FORMAT_TYPES
2974 /* If the target provides additional format types, we need to
2975 add them to FORMAT_TYPES at first use. */
2976 if (TARGET_FORMAT_TYPES != NULL && !dynamic_format_types)
2978 dynamic_format_types = XNEWVEC (format_kind_info,
2979 n_format_types + TARGET_N_FORMAT_TYPES);
2980 memcpy (dynamic_format_types, format_types_orig,
2981 sizeof (format_types_orig));
2982 memcpy (&dynamic_format_types[n_format_types], TARGET_FORMAT_TYPES,
2983 TARGET_N_FORMAT_TYPES * sizeof (dynamic_format_types[0]));
2985 format_types = dynamic_format_types;
2986 /* Provide a reference for the first potential external type. */
2987 first_target_format_type = n_format_types;
2988 n_format_types += TARGET_N_FORMAT_TYPES;
2992 if (!decode_format_attr (args, &info, 0))
2994 *no_add_attrs = true;
2998 if (prototype_p (type))
3000 if (!check_format_string (type, info.format_num, flags,
3001 no_add_attrs, info.format_type))
3004 if (info.first_arg_num != 0)
3006 unsigned HOST_WIDE_INT arg_num = 1;
3007 function_args_iterator iter;
3010 /* Verify that first_arg_num points to the last arg,
3012 FOREACH_FUNCTION_ARGS (type, arg_type, iter)
3015 if (arg_num != info.first_arg_num)
3017 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
3018 error ("args to be formatted is not %<...%>");
3019 *no_add_attrs = true;
3025 /* Check if this is a strftime variant. Just for this variant
3026 FMT_FLAG_ARG_CONVERT is not set. */
3027 if ((format_types[info.format_type].flags & (int) FMT_FLAG_ARG_CONVERT) == 0
3028 && info.first_arg_num != 0)
3030 error ("strftime formats cannot format arguments");
3031 *no_add_attrs = true;
3035 /* If this is a custom GCC-internal format type, we have to
3036 initialize certain bits at runtime. */
3037 if (info.format_type == asm_fprintf_format_type
3038 || info.format_type == gcc_gfc_format_type
3039 || info.format_type == gcc_diag_format_type
3040 || info.format_type == gcc_tdiag_format_type
3041 || info.format_type == gcc_cdiag_format_type
3042 || info.format_type == gcc_cxxdiag_format_type)
3044 /* Our first time through, we have to make sure that our
3045 format_type data is allocated dynamically and is modifiable. */
3046 if (!dynamic_format_types)
3047 format_types = dynamic_format_types = (format_kind_info *)
3048 xmemdup (format_types_orig, sizeof (format_types_orig),
3049 sizeof (format_types_orig));
3051 /* If this is format __asm_fprintf__, we have to initialize
3052 GCC's notion of HOST_WIDE_INT for checking %wd. */
3053 if (info.format_type == asm_fprintf_format_type)
3054 init_dynamic_asm_fprintf_info ();
3055 /* If this is format __gcc_gfc__, we have to initialize GCC's
3056 notion of 'locus' at runtime for %L. */
3057 else if (info.format_type == gcc_gfc_format_type)
3058 init_dynamic_gfc_info ();
3059 /* If this is one of the diagnostic attributes, then we have to
3060 initialize 'location_t' and 'tree' at runtime. */
3061 else if (info.format_type == gcc_diag_format_type
3062 || info.format_type == gcc_tdiag_format_type
3063 || info.format_type == gcc_cdiag_format_type
3064 || info.format_type == gcc_cxxdiag_format_type)
3065 init_dynamic_diag_info ();