1 /* Check calls to formatted I/O functions (-Wformat).
2 Copyright (C) 1992-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
27 #include "double-int.h"
35 #include "stringpool.h"
40 #include "diagnostic-core.h"
41 #include "langhooks.h"
43 #include "alloc-pool.h"
46 /* Handle attributes associated with format checking. */
48 /* This must be in the same order as format_types, except for
49 format_type_error. Target-specific format types do not have
50 matching enum values. */
51 enum format_type { printf_format_type, asm_fprintf_format_type,
52 gcc_diag_format_type, gcc_tdiag_format_type,
53 gcc_cdiag_format_type,
54 gcc_cxxdiag_format_type, gcc_gfc_format_type,
55 gcc_objc_string_format_type,
56 format_type_error = -1};
58 typedef struct function_format_info
60 int format_type; /* type of format (printf, scanf, etc.) */
61 unsigned HOST_WIDE_INT format_num; /* number of format argument */
62 unsigned HOST_WIDE_INT first_arg_num; /* number of first arg (zero for varargs) */
63 } function_format_info;
65 static bool decode_format_attr (tree, function_format_info *, int);
66 static int decode_format_type (const char *);
68 static bool check_format_string (tree argument,
69 unsigned HOST_WIDE_INT format_num,
70 int flags, bool *no_add_attrs,
71 int expected_format_type);
72 static bool get_constant (tree expr, unsigned HOST_WIDE_INT *value,
74 static const char *convert_format_name_to_system_name (const char *attr_name);
75 static bool cmp_attribs (const char *tattr_name, const char *attr_name);
77 static int first_target_format_type;
78 static const char *format_name (int format_num);
79 static int format_flags (int format_num);
81 /* Check that we have a pointer to a string suitable for use as a format.
82 The default is to check for a char type.
83 For objective-c dialects, this is extended to include references to string
84 objects validated by objc_string_ref_type_p ().
85 Targets may also provide a string object type that can be used within c and
86 c++ and shared with their respective objective-c dialects. In this case the
87 reference to a format string is checked for validity via a hook.
89 The function returns true if strref points to any string type valid for the
90 language dialect and target. */
93 valid_stringptr_type_p (tree strref)
95 return (strref != NULL
96 && TREE_CODE (strref) == POINTER_TYPE
97 && (TYPE_MAIN_VARIANT (TREE_TYPE (strref)) == char_type_node
98 || objc_string_ref_type_p (strref)
99 || (*targetcm.string_object_ref_type_p) ((const_tree) strref)));
102 /* Handle a "format_arg" attribute; arguments as in
103 struct attribute_spec.handler. */
105 handle_format_arg_attribute (tree *node, tree ARG_UNUSED (name),
106 tree args, int flags, bool *no_add_attrs)
109 tree format_num_expr = TREE_VALUE (args);
110 unsigned HOST_WIDE_INT format_num = 0;
112 if (!get_constant (format_num_expr, &format_num, 0))
114 error ("format string has invalid operand number");
115 *no_add_attrs = true;
119 if (prototype_p (type))
121 /* The format arg can be any string reference valid for the language and
122 target. We cannot be more specific in this case. */
123 if (!check_format_string (type, format_num, flags, no_add_attrs, -1))
127 if (!valid_stringptr_type_p (TREE_TYPE (type)))
129 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
130 error ("function does not return string type");
131 *no_add_attrs = true;
138 /* Verify that the format_num argument is actually a string reference suitable,
139 for the language dialect and target (in case the format attribute is in
140 error). When we know the specific reference type expected, this is also
143 check_format_string (tree fntype, unsigned HOST_WIDE_INT format_num,
144 int flags, bool *no_add_attrs, int expected_format_type)
146 unsigned HOST_WIDE_INT i;
147 bool is_objc_sref, is_target_sref, is_char_ref;
150 function_args_iterator iter;
153 FOREACH_FUNCTION_ARGS (fntype, ref, iter)
161 || !valid_stringptr_type_p (ref))
163 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
164 error ("format string argument is not a string type");
165 *no_add_attrs = true;
169 /* We only know that we want a suitable string reference. */
170 if (expected_format_type < 0)
173 /* Now check that the arg matches the expected type. */
175 (TYPE_MAIN_VARIANT (TREE_TYPE (ref)) == char_type_node);
177 fmt_flags = format_flags (expected_format_type);
178 is_objc_sref = is_target_sref = false;
180 is_objc_sref = objc_string_ref_type_p (ref);
182 if (!(fmt_flags & FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL))
185 return true; /* OK, we expected a char and found one. */
188 /* We expected a char but found an extended string type. */
190 error ("found a %<%s%> reference but the format argument should"
191 " be a string", format_name (gcc_objc_string_format_type));
193 error ("found a %qT but the format argument should be a string",
195 *no_add_attrs = true;
200 /* We expect a string object type as the format arg. */
203 error ("format argument should be a %<%s%> reference but"
204 " a string was found", format_name (expected_format_type));
205 *no_add_attrs = true;
209 /* We will assert that objective-c will support either its own string type
210 or the target-supplied variant. */
212 is_target_sref = (*targetcm.string_object_ref_type_p) ((const_tree) ref);
214 if (expected_format_type == (int) gcc_objc_string_format_type
215 && (is_objc_sref || is_target_sref))
218 /* We will allow a target string ref to match only itself. */
219 if (first_target_format_type
220 && expected_format_type >= first_target_format_type
225 error ("format argument should be a %<%s%> reference",
226 format_name (expected_format_type));
227 *no_add_attrs = true;
234 /* Verify EXPR is a constant, and store its value.
235 If validated_p is true there should be no errors.
236 Returns true on success, false otherwise. */
238 get_constant (tree expr, unsigned HOST_WIDE_INT *value, int validated_p)
240 if (!tree_fits_uhwi_p (expr))
242 gcc_assert (!validated_p);
246 *value = TREE_INT_CST_LOW (expr);
251 /* Decode the arguments to a "format" attribute into a
252 function_format_info structure. It is already known that the list
253 is of the right length. If VALIDATED_P is true, then these
254 attributes have already been validated and must not be erroneous;
255 if false, it will give an error message. Returns true if the
256 attributes are successfully decoded, false otherwise. */
259 decode_format_attr (tree args, function_format_info *info, int validated_p)
261 tree format_type_id = TREE_VALUE (args);
262 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
263 tree first_arg_num_expr
264 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
266 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
268 gcc_assert (!validated_p);
269 error ("unrecognized format specifier");
274 const char *p = IDENTIFIER_POINTER (format_type_id);
276 p = convert_format_name_to_system_name (p);
278 info->format_type = decode_format_type (p);
280 if (!c_dialect_objc ()
281 && info->format_type == gcc_objc_string_format_type)
283 gcc_assert (!validated_p);
284 warning (OPT_Wformat_, "%qE is only allowed in Objective-C dialects",
286 info->format_type = format_type_error;
290 if (info->format_type == format_type_error)
292 gcc_assert (!validated_p);
293 warning (OPT_Wformat_, "%qE is an unrecognized format function type",
299 if (!get_constant (format_num_expr, &info->format_num, validated_p))
301 error ("format string has invalid operand number");
305 if (!get_constant (first_arg_num_expr, &info->first_arg_num, validated_p))
307 error ("%<...%> has invalid operand number");
311 if (info->first_arg_num != 0 && info->first_arg_num <= info->format_num)
313 gcc_assert (!validated_p);
314 error ("format string argument follows the args to be formatted");
321 /* Check a call to a format function against a parameter list. */
323 /* The C standard version C++ is treated as equivalent to
324 or inheriting from, for the purpose of format features supported. */
325 #define CPLUSPLUS_STD_VER (cxx_dialect < cxx11 ? STD_C94 : STD_C99)
326 /* The C standard version we are checking formats against when pedantic. */
327 #define C_STD_VER ((int) (c_dialect_cxx () \
328 ? CPLUSPLUS_STD_VER \
331 : (flag_isoc94 ? STD_C94 : STD_C89))))
332 /* The name to give to the standard version we are warning about when
333 pedantic. FEATURE_VER is the version in which the feature warned out
334 appeared, which is higher than C_STD_VER. */
335 #define C_STD_NAME(FEATURE_VER) (c_dialect_cxx () \
336 ? (cxx_dialect < cxx11 ? "ISO C++98" \
338 : ((FEATURE_VER) == STD_EXT \
341 /* Adjust a C standard version, which may be STD_C9L, to account for
342 -Wno-long-long. Returns other standard versions unchanged. */
343 #define ADJ_STD(VER) ((int) ((VER) == STD_C9L \
344 ? (warn_long_long ? STD_C99 : STD_C89) \
347 /* Enum describing the kind of specifiers present in the format and
348 requiring an argument. */
349 enum format_specifier_kind {
352 CF_KIND_FIELD_PRECISION
355 static const char *kind_descriptions[] = {
357 N_("field width specifier"),
358 N_("field precision specifier")
361 /* Structure describing details of a type expected in format checking,
362 and the type to check against it. */
363 typedef struct format_wanted_type
365 /* The type wanted. */
367 /* The name of this type to use in diagnostics. */
368 const char *wanted_type_name;
369 /* Should be type checked just for scalar width identity. */
370 int scalar_identity_flag;
371 /* The level of indirection through pointers at which this type occurs. */
373 /* Whether, when pointer_count is 1, to allow any character type when
374 pedantic, rather than just the character or void type specified. */
375 int char_lenient_flag;
376 /* Whether the argument, dereferenced once, is written into and so the
377 argument must not be a pointer to a const-qualified type. */
379 /* Whether the argument, dereferenced once, is read from and so
380 must not be a NULL pointer. */
381 int reading_from_flag;
382 /* The kind of specifier that this type is used for. */
383 enum format_specifier_kind kind;
384 /* The starting character of the specifier. This never includes the
385 initial percent sign. */
386 const char *format_start;
387 /* The length of the specifier. */
389 /* The actual parameter to check against the wanted type. */
391 /* The argument number of that parameter. */
393 /* The next type to check for this format conversion, or NULL if none. */
394 struct format_wanted_type *next;
395 } format_wanted_type;
397 /* Convenience macro for format_length_info meaning unused. */
398 #define NO_FMT NULL, FMT_LEN_none, STD_C89
400 static const format_length_info printf_length_specs[] =
402 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99, 0 },
403 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L, 0 },
404 { "q", FMT_LEN_ll, STD_EXT, NO_FMT, 0 },
405 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
406 { "z", FMT_LEN_z, STD_C99, NO_FMT, 0 },
407 { "Z", FMT_LEN_z, STD_EXT, NO_FMT, 0 },
408 { "t", FMT_LEN_t, STD_C99, NO_FMT, 0 },
409 { "j", FMT_LEN_j, STD_C99, NO_FMT, 0 },
410 { "H", FMT_LEN_H, STD_EXT, NO_FMT, 0 },
411 { "D", FMT_LEN_D, STD_EXT, "DD", FMT_LEN_DD, STD_EXT, 0 },
412 { NO_FMT, NO_FMT, 0 }
415 /* Length specifiers valid for asm_fprintf. */
416 static const format_length_info asm_fprintf_length_specs[] =
418 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89, 0 },
419 { "w", FMT_LEN_none, STD_C89, NO_FMT, 0 },
420 { NO_FMT, NO_FMT, 0 }
423 /* Length specifiers valid for GCC diagnostics. */
424 static const format_length_info gcc_diag_length_specs[] =
426 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89, 0 },
427 { "w", FMT_LEN_none, STD_C89, NO_FMT, 0 },
428 { NO_FMT, NO_FMT, 0 }
431 /* The custom diagnostics all accept the same length specifiers. */
432 #define gcc_tdiag_length_specs gcc_diag_length_specs
433 #define gcc_cdiag_length_specs gcc_diag_length_specs
434 #define gcc_cxxdiag_length_specs gcc_diag_length_specs
436 /* This differs from printf_length_specs only in that "Z" is not accepted. */
437 static const format_length_info scanf_length_specs[] =
439 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99, 0 },
440 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L, 0 },
441 { "q", FMT_LEN_ll, STD_EXT, NO_FMT, 0 },
442 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
443 { "z", FMT_LEN_z, STD_C99, NO_FMT, 0 },
444 { "t", FMT_LEN_t, STD_C99, NO_FMT, 0 },
445 { "j", FMT_LEN_j, STD_C99, NO_FMT, 0 },
446 { "H", FMT_LEN_H, STD_EXT, NO_FMT, 0 },
447 { "D", FMT_LEN_D, STD_EXT, "DD", FMT_LEN_DD, STD_EXT, 0 },
448 { NO_FMT, NO_FMT, 0 }
452 /* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings
453 make no sense for a format type not part of any C standard version. */
454 static const format_length_info strfmon_length_specs[] =
456 /* A GNU extension. */
457 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
458 { NO_FMT, NO_FMT, 0 }
462 /* For now, the Fortran front-end routines only use l as length modifier. */
463 static const format_length_info gcc_gfc_length_specs[] =
465 { "l", FMT_LEN_l, STD_C89, NO_FMT, 0 },
466 { NO_FMT, NO_FMT, 0 }
470 static const format_flag_spec printf_flag_specs[] =
472 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
473 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
474 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
475 { '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
476 { '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
477 { '\'', 0, 0, N_("''' flag"), N_("the ''' printf flag"), STD_EXT },
478 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' printf flag"), STD_EXT },
479 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
480 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
481 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
482 { 0, 0, 0, NULL, NULL, STD_C89 }
486 static const format_flag_pair printf_flag_pairs[] =
490 { '0', 'p', 1, 'i' },
494 static const format_flag_spec asm_fprintf_flag_specs[] =
496 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
497 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
498 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
499 { '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
500 { '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
501 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
502 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
503 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
504 { 0, 0, 0, NULL, NULL, STD_C89 }
507 static const format_flag_pair asm_fprintf_flag_pairs[] =
511 { '0', 'p', 1, 'i' },
515 static const format_flag_pair gcc_diag_flag_pairs[] =
520 #define gcc_tdiag_flag_pairs gcc_diag_flag_pairs
521 #define gcc_cdiag_flag_pairs gcc_diag_flag_pairs
522 #define gcc_cxxdiag_flag_pairs gcc_diag_flag_pairs
523 #define gcc_gfc_flag_pairs gcc_diag_flag_pairs
525 static const format_flag_spec gcc_diag_flag_specs[] =
527 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
528 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
529 { 'q', 0, 0, N_("'q' flag"), N_("the 'q' diagnostic flag"), STD_C89 },
530 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
531 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
532 { 0, 0, 0, NULL, NULL, STD_C89 }
535 #define gcc_tdiag_flag_specs gcc_diag_flag_specs
536 #define gcc_cdiag_flag_specs gcc_diag_flag_specs
537 #define gcc_cxxdiag_flag_specs gcc_diag_flag_specs
538 #define gcc_gfc_flag_specs gcc_diag_flag_specs
540 static const format_flag_spec scanf_flag_specs[] =
542 { '*', 0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89 },
543 { 'a', 0, 0, N_("'a' flag"), N_("the 'a' scanf flag"), STD_EXT },
544 { 'm', 0, 0, N_("'m' flag"), N_("the 'm' scanf flag"), STD_EXT },
545 { 'w', 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89 },
546 { 'L', 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89 },
547 { '\'', 0, 0, N_("''' flag"), N_("the ''' scanf flag"), STD_EXT },
548 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' scanf flag"), STD_EXT },
549 { 0, 0, 0, NULL, NULL, STD_C89 }
553 static const format_flag_pair scanf_flag_pairs[] =
561 static const format_flag_spec strftime_flag_specs[] =
563 { '_', 0, 0, N_("'_' flag"), N_("the '_' strftime flag"), STD_EXT },
564 { '-', 0, 0, N_("'-' flag"), N_("the '-' strftime flag"), STD_EXT },
565 { '0', 0, 0, N_("'0' flag"), N_("the '0' strftime flag"), STD_EXT },
566 { '^', 0, 0, N_("'^' flag"), N_("the '^' strftime flag"), STD_EXT },
567 { '#', 0, 0, N_("'#' flag"), N_("the '#' strftime flag"), STD_EXT },
568 { 'w', 0, 0, N_("field width"), N_("field width in strftime format"), STD_EXT },
569 { 'E', 0, 0, N_("'E' modifier"), N_("the 'E' strftime modifier"), STD_C99 },
570 { 'O', 0, 0, N_("'O' modifier"), N_("the 'O' strftime modifier"), STD_C99 },
571 { 'O', 'o', 0, NULL, N_("the 'O' modifier"), STD_EXT },
572 { 0, 0, 0, NULL, NULL, STD_C89 }
576 static const format_flag_pair strftime_flag_pairs[] =
587 static const format_flag_spec strfmon_flag_specs[] =
589 { '=', 0, 1, N_("fill character"), N_("fill character in strfmon format"), STD_C89 },
590 { '^', 0, 0, N_("'^' flag"), N_("the '^' strfmon flag"), STD_C89 },
591 { '+', 0, 0, N_("'+' flag"), N_("the '+' strfmon flag"), STD_C89 },
592 { '(', 0, 0, N_("'(' flag"), N_("the '(' strfmon flag"), STD_C89 },
593 { '!', 0, 0, N_("'!' flag"), N_("the '!' strfmon flag"), STD_C89 },
594 { '-', 0, 0, N_("'-' flag"), N_("the '-' strfmon flag"), STD_C89 },
595 { 'w', 0, 0, N_("field width"), N_("field width in strfmon format"), STD_C89 },
596 { '#', 0, 0, N_("left precision"), N_("left precision in strfmon format"), STD_C89 },
597 { 'p', 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89 },
598 { 'L', 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89 },
599 { 0, 0, 0, NULL, NULL, STD_C89 }
602 static const format_flag_pair strfmon_flag_pairs[] =
609 static const format_char_info dfly_ext_char_info =
610 { NULL, 1, STD_EXT, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL };
612 static const format_char_info print_char_table[] =
614 /* C89 conversion specifiers. */
615 { "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 },
616 { "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 },
617 { "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 },
618 { "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 },
619 { "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 },
620 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, T94_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
621 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
622 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "c", NULL },
623 { "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 },
624 /* C99 conversion specifiers. */
625 { "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 },
626 { "aA", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "", NULL },
627 /* X/Open conversion specifiers. */
628 { "C", 0, STD_EXT, { TEX_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
629 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "R", NULL },
630 /* GNU conversion specifiers. */
631 { "m", 0, STD_EXT, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "", NULL },
632 /* BSD conversion specifiers. */
633 /* DragonFly kernel extensions (src/sys/kern/subr_prf.c).
634 The format %b is supported to decode error registers.
635 Its usage is: printf("reg=%b\n", regval, "<base><arg>*");
636 which produces: reg=3<BITTWO,BITONE>
638 { "b", 0, STD_EXT, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "", &dfly_ext_char_info },
639 { "ry", 0, STD_EXT, { T89_I, BADLEN, BADLEN, T89_L, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "i", NULL },
640 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
643 static const format_char_info asm_fprintf_char_table[] =
645 /* C89 conversion specifiers. */
646 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +", "i", NULL },
647 { "oxX", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0#", "i", NULL },
648 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0", "i", NULL },
649 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
650 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
652 /* asm_fprintf conversion specifiers. */
653 { "O", 0, STD_C89, NOARGUMENTS, "", "", NULL },
654 { "R", 0, STD_C89, NOARGUMENTS, "", "", NULL },
655 { "I", 0, STD_C89, NOARGUMENTS, "", "", NULL },
656 { "L", 0, STD_C89, NOARGUMENTS, "", "", NULL },
657 { "U", 0, STD_C89, NOARGUMENTS, "", "", NULL },
658 { "r", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
659 { "@", 0, STD_C89, NOARGUMENTS, "", "", NULL },
660 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
663 static const format_char_info gcc_diag_char_table[] =
665 /* C89 conversion specifiers. */
666 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
667 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
668 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
669 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
670 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
671 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
673 /* Custom conversion specifiers. */
675 /* These will require a "tree" at runtime. */
676 { "K", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
678 { "r", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL },
679 { "<>'R",0, STD_C89, NOARGUMENTS, "", "", NULL },
680 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
681 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
684 static const format_char_info gcc_tdiag_char_table[] =
686 /* C89 conversion specifiers. */
687 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
688 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
689 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
690 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
691 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
692 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
694 /* Custom conversion specifiers. */
696 /* These will require a "tree" at runtime. */
697 { "DFKTEV", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
699 { "v", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
701 { "r", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL },
702 { "<>'R",0, STD_C89, NOARGUMENTS, "", "", NULL },
703 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
704 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
707 static const format_char_info gcc_cdiag_char_table[] =
709 /* C89 conversion specifiers. */
710 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
711 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
712 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
713 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
714 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
715 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
717 /* Custom conversion specifiers. */
719 /* These will require a "tree" at runtime. */
720 { "DEFKTV", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
722 { "v", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
724 { "r", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL },
725 { "<>'R",0, STD_C89, NOARGUMENTS, "", "", NULL },
726 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
727 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
730 static const format_char_info gcc_cxxdiag_char_table[] =
732 /* C89 conversion specifiers. */
733 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
734 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
735 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
736 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
737 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
738 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
740 /* Custom conversion specifiers. */
742 /* These will require a "tree" at runtime. */
743 { "ADEFKSTVX",0,STD_C89,{ T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+#", "", NULL },
745 { "v", 0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
747 /* These accept either an 'int' or an 'enum tree_code' (which is handled as an 'int'.) */
748 { "CLOPQ",0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
750 { "r", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL },
751 { "<>'R",0, STD_C89, NOARGUMENTS, "", "", NULL },
752 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
753 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
756 static const format_char_info gcc_gfc_char_table[] =
758 /* C89 conversion specifiers. */
759 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
760 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
761 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
762 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "cR", NULL },
764 /* gfc conversion specifiers. */
766 { "C", 0, STD_C89, NOARGUMENTS, "", "", NULL },
768 /* This will require a "locus" at runtime. */
769 { "L", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "R", NULL },
771 /* These will require nothing. */
772 { "<>",0, STD_C89, NOARGUMENTS, "", "", NULL },
773 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
776 static const format_char_info scan_char_table[] =
778 /* C89 conversion specifiers. */
779 { "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 },
780 { "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 },
781 { "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 },
782 { "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 },
783 { "c", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*mw", "cW", NULL },
784 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "cW", NULL },
785 { "[", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "cW[", NULL },
786 { "p", 2, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
787 { "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 },
788 /* C99 conversion specifiers. */
789 { "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 },
790 { "aA", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w'", "W", NULL },
791 /* X/Open conversion specifiers. */
792 { "C", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*mw", "W", NULL },
793 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "W", NULL },
794 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
797 static const format_char_info time_char_table[] =
799 /* C89 conversion specifiers. */
800 { "ABZab", 0, STD_C89, NOLENGTHS, "^#", "", NULL },
801 { "cx", 0, STD_C89, NOLENGTHS, "E", "3", NULL },
802 { "HIMSUWdmw", 0, STD_C89, NOLENGTHS, "-_0Ow", "", NULL },
803 { "j", 0, STD_C89, NOLENGTHS, "-_0Ow", "o", NULL },
804 { "p", 0, STD_C89, NOLENGTHS, "#", "", NULL },
805 { "X", 0, STD_C89, NOLENGTHS, "E", "", NULL },
806 { "y", 0, STD_C89, NOLENGTHS, "EO-_0w", "4", NULL },
807 { "Y", 0, STD_C89, NOLENGTHS, "-_0EOw", "o", NULL },
808 { "%", 0, STD_C89, NOLENGTHS, "", "", NULL },
809 /* C99 conversion specifiers. */
810 { "C", 0, STD_C99, NOLENGTHS, "-_0EOw", "o", NULL },
811 { "D", 0, STD_C99, NOLENGTHS, "", "2", NULL },
812 { "eVu", 0, STD_C99, NOLENGTHS, "-_0Ow", "", NULL },
813 { "FRTnrt", 0, STD_C99, NOLENGTHS, "", "", NULL },
814 { "g", 0, STD_C99, NOLENGTHS, "O-_0w", "2o", NULL },
815 { "G", 0, STD_C99, NOLENGTHS, "-_0Ow", "o", NULL },
816 { "h", 0, STD_C99, NOLENGTHS, "^#", "", NULL },
817 { "z", 0, STD_C99, NOLENGTHS, "O", "o", NULL },
818 /* GNU conversion specifiers. */
819 { "kls", 0, STD_EXT, NOLENGTHS, "-_0Ow", "", NULL },
820 { "P", 0, STD_EXT, NOLENGTHS, "", "", NULL },
821 /* DragonFly/FreeBSD conversion specifiers. */
822 { "+", 0, STD_EXT, NOLENGTHS, "E", "3", NULL },
823 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
826 static const format_char_info monetary_char_table[] =
828 { "in", 0, STD_C89, { T89_D, BADLEN, BADLEN, BADLEN, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "=^+(!-w#p", "", NULL },
829 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
832 /* This must be in the same order as enum format_type. */
833 static const format_kind_info format_types_orig[] =
835 { "gnu_printf", printf_length_specs, print_char_table, " +#0-'I", NULL,
836 printf_flag_specs, printf_flag_pairs,
837 FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_EMPTY_PREC_OK,
838 'w', 0, 'p', 0, 'L', 0,
839 &integer_type_node, &integer_type_node
841 { "asm_fprintf", asm_fprintf_length_specs, asm_fprintf_char_table, " +#0-", NULL,
842 asm_fprintf_flag_specs, asm_fprintf_flag_pairs,
843 FMT_FLAG_ARG_CONVERT|FMT_FLAG_EMPTY_PREC_OK,
844 'w', 0, 'p', 0, 'L', 0,
847 { "gcc_diag", gcc_diag_length_specs, gcc_diag_char_table, "q+#", NULL,
848 gcc_diag_flag_specs, gcc_diag_flag_pairs,
849 FMT_FLAG_ARG_CONVERT,
850 0, 0, 'p', 0, 'L', 0,
851 NULL, &integer_type_node
853 { "gcc_tdiag", gcc_tdiag_length_specs, gcc_tdiag_char_table, "q+#", NULL,
854 gcc_tdiag_flag_specs, gcc_tdiag_flag_pairs,
855 FMT_FLAG_ARG_CONVERT,
856 0, 0, 'p', 0, 'L', 0,
857 NULL, &integer_type_node
859 { "gcc_cdiag", gcc_cdiag_length_specs, gcc_cdiag_char_table, "q+#", NULL,
860 gcc_cdiag_flag_specs, gcc_cdiag_flag_pairs,
861 FMT_FLAG_ARG_CONVERT,
862 0, 0, 'p', 0, 'L', 0,
863 NULL, &integer_type_node
865 { "gcc_cxxdiag", gcc_cxxdiag_length_specs, gcc_cxxdiag_char_table, "q+#", NULL,
866 gcc_cxxdiag_flag_specs, gcc_cxxdiag_flag_pairs,
867 FMT_FLAG_ARG_CONVERT,
868 0, 0, 'p', 0, 'L', 0,
869 NULL, &integer_type_node
871 { "gcc_gfc", gcc_gfc_length_specs, gcc_gfc_char_table, "q+#", NULL,
872 gcc_gfc_flag_specs, gcc_gfc_flag_pairs,
873 FMT_FLAG_ARG_CONVERT,
877 { "NSString", NULL, NULL, NULL, NULL,
879 FMT_FLAG_ARG_CONVERT|FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL, 0, 0, 0, 0, 0, 0,
882 { "gnu_scanf", scanf_length_specs, scan_char_table, "*'I", NULL,
883 scanf_flag_specs, scanf_flag_pairs,
884 FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_ZERO_WIDTH_BAD|FMT_FLAG_DOLLAR_GAP_POINTER_OK,
885 'w', 0, 0, '*', 'L', 'm',
888 { "gnu_strftime", NULL, time_char_table, "_-0^#", "EO",
889 strftime_flag_specs, strftime_flag_pairs,
890 FMT_FLAG_FANCY_PERCENT_OK, 'w', 0, 0, 0, 0, 0,
893 { "gnu_strfmon", strfmon_length_specs, monetary_char_table, "=^+(!-", NULL,
894 strfmon_flag_specs, strfmon_flag_pairs,
895 FMT_FLAG_ARG_CONVERT, 'w', '#', 'p', 0, 'L', 0,
900 /* This layer of indirection allows GCC to reassign format_types with
901 new data if necessary, while still allowing the original data to be
903 static const format_kind_info *format_types = format_types_orig;
904 /* We can modify this one. We also add target-specific format types
905 to the end of the array. */
906 static format_kind_info *dynamic_format_types;
908 static int n_format_types = ARRAY_SIZE (format_types_orig);
910 /* Structure detailing the results of checking a format function call
911 where the format expression may be a conditional expression with
912 many leaves resulting from nested conditional expressions. */
915 /* Number of leaves of the format argument that could not be checked
916 as they were not string literals. */
917 int number_non_literal;
918 /* Number of leaves of the format argument that were null pointers or
919 string literals, but had extra format arguments. */
920 int number_extra_args;
921 location_t extra_arg_loc;
922 /* Number of leaves of the format argument that were null pointers or
923 string literals, but had extra format arguments and used $ operand
925 int number_dollar_extra_args;
926 /* Number of leaves of the format argument that were wide string
929 /* Number of leaves of the format argument that were empty strings. */
931 /* Number of leaves of the format argument that were unterminated
933 int number_unterminated;
934 /* Number of leaves of the format argument that were not counted above. */
936 /* Location of the format string. */
937 location_t format_string_loc;
938 } format_check_results;
942 format_check_results *res;
943 function_format_info *info;
945 } format_check_context;
947 /* Return the format name (as specified in the original table) for the format
948 type indicated by format_num. */
950 format_name (int format_num)
952 if (format_num >= 0 && format_num < n_format_types)
953 return format_types[format_num].name;
957 /* Return the format flags (as specified in the original table) for the format
958 type indicated by format_num. */
960 format_flags (int format_num)
962 if (format_num >= 0 && format_num < n_format_types)
963 return format_types[format_num].flags;
967 static void check_format_info (function_format_info *, tree);
968 static void check_format_arg (void *, tree, unsigned HOST_WIDE_INT);
969 static void check_format_info_main (format_check_results *,
970 function_format_info *,
971 const char *, int, tree,
972 unsigned HOST_WIDE_INT, alloc_pool);
974 static void init_dollar_format_checking (int, tree);
975 static int maybe_read_dollar_number (const char **, int,
976 tree, tree *, const format_kind_info *);
977 static bool avoid_dollar_number (const char *);
978 static void finish_dollar_format_checking (format_check_results *, int);
980 static const format_flag_spec *get_flag_spec (const format_flag_spec *,
983 static void check_format_types (location_t, format_wanted_type *);
984 static void format_type_warning (location_t, format_wanted_type *, tree, tree);
986 /* Decode a format type from a string, returning the type, or
987 format_type_error if not valid, in which case the caller should print an
990 decode_format_type (const char *s)
995 s = convert_format_name_to_system_name (s);
997 for (i = 0; i < n_format_types; i++)
1000 if (!strcmp (s, format_types[i].name))
1002 alen = strlen (format_types[i].name);
1003 if (slen == alen + 4 && s[0] == '_' && s[1] == '_'
1004 && s[slen - 1] == '_' && s[slen - 2] == '_'
1005 && !strncmp (s + 2, format_types[i].name, alen))
1008 return format_type_error;
1012 /* Check the argument list of a call to printf, scanf, etc.
1013 ATTRS are the attributes on the function type. There are NARGS argument
1014 values in the array ARGARRAY.
1015 Also, if -Wsuggest-attribute=format,
1016 warn for calls to vprintf or vscanf in functions with no such format
1017 attribute themselves. */
1020 check_function_format (tree attrs, int nargs, tree *argarray)
1024 /* See if this function has any format attributes. */
1025 for (a = attrs; a; a = TREE_CHAIN (a))
1027 if (is_attribute_p ("format", TREE_PURPOSE (a)))
1029 /* Yup; check it. */
1030 function_format_info info;
1031 decode_format_attr (TREE_VALUE (a), &info, /*validated=*/true);
1034 /* FIXME: Rewrite all the internal functions in this file
1035 to use the ARGARRAY directly instead of constructing this
1037 tree params = NULL_TREE;
1039 for (i = nargs - 1; i >= 0; i--)
1040 params = tree_cons (NULL_TREE, argarray[i], params);
1041 check_format_info (&info, params);
1043 if (warn_suggest_attribute_format && info.first_arg_num == 0
1044 && (format_types[info.format_type].flags
1045 & (int) FMT_FLAG_ARG_CONVERT))
1048 for (c = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
1051 if (is_attribute_p ("format", TREE_PURPOSE (c))
1052 && (decode_format_type (IDENTIFIER_POINTER
1053 (TREE_VALUE (TREE_VALUE (c))))
1054 == info.format_type))
1058 /* Check if the current function has a parameter to which
1059 the format attribute could be attached; if not, it
1060 can't be a candidate for a format attribute, despite
1061 the vprintf-like or vscanf-like call. */
1063 for (args = DECL_ARGUMENTS (current_function_decl);
1065 args = DECL_CHAIN (args))
1067 if (TREE_CODE (TREE_TYPE (args)) == POINTER_TYPE
1068 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args)))
1073 warning (OPT_Wsuggest_attribute_format, "function might "
1074 "be possible candidate for %qs format attribute",
1075 format_types[info.format_type].name);
1083 /* Variables used by the checking of $ operand number formats. */
1084 static char *dollar_arguments_used = NULL;
1085 static char *dollar_arguments_pointer_p = NULL;
1086 static int dollar_arguments_alloc = 0;
1087 static int dollar_arguments_count;
1088 static int dollar_first_arg_num;
1089 static int dollar_max_arg_used;
1090 static int dollar_format_warned;
1092 /* Initialize the checking for a format string that may contain $
1093 parameter number specifications; we will need to keep track of whether
1094 each parameter has been used. FIRST_ARG_NUM is the number of the first
1095 argument that is a parameter to the format, or 0 for a vprintf-style
1096 function; PARAMS is the list of arguments starting at this argument. */
1099 init_dollar_format_checking (int first_arg_num, tree params)
1101 tree oparams = params;
1103 dollar_first_arg_num = first_arg_num;
1104 dollar_arguments_count = 0;
1105 dollar_max_arg_used = 0;
1106 dollar_format_warned = 0;
1107 if (first_arg_num > 0)
1111 dollar_arguments_count++;
1112 params = TREE_CHAIN (params);
1115 if (dollar_arguments_alloc < dollar_arguments_count)
1117 free (dollar_arguments_used);
1118 free (dollar_arguments_pointer_p);
1119 dollar_arguments_alloc = dollar_arguments_count;
1120 dollar_arguments_used = XNEWVEC (char, dollar_arguments_alloc);
1121 dollar_arguments_pointer_p = XNEWVEC (char, dollar_arguments_alloc);
1123 if (dollar_arguments_alloc)
1125 memset (dollar_arguments_used, 0, dollar_arguments_alloc);
1126 if (first_arg_num > 0)
1132 dollar_arguments_pointer_p[i] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params)))
1134 params = TREE_CHAIN (params);
1142 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
1143 is set, it is an error if one is not found; otherwise, it is OK. If
1144 such a number is found, check whether it is within range and mark that
1145 numbered operand as being used for later checking. Returns the operand
1146 number if found and within range, zero if no such number was found and
1147 this is OK, or -1 on error. PARAMS points to the first operand of the
1148 format; PARAM_PTR is made to point to the parameter referred to. If
1149 a $ format is found, *FORMAT is updated to point just after it. */
1152 maybe_read_dollar_number (const char **format,
1153 int dollar_needed, tree params, tree *param_ptr,
1154 const format_kind_info *fki)
1158 const char *fcp = *format;
1159 if (!ISDIGIT (*fcp))
1163 warning (OPT_Wformat_, "missing $ operand number in format");
1171 while (ISDIGIT (*fcp))
1174 nargnum = 10 * argnum + (*fcp - '0');
1175 if (nargnum < 0 || nargnum / 10 != argnum)
1184 warning (OPT_Wformat_, "missing $ operand number in format");
1191 if (pedantic && !dollar_format_warned)
1193 warning (OPT_Wformat_, "%s does not support %%n$ operand number formats",
1194 C_STD_NAME (STD_EXT));
1195 dollar_format_warned = 1;
1197 if (overflow_flag || argnum == 0
1198 || (dollar_first_arg_num && argnum > dollar_arguments_count))
1200 warning (OPT_Wformat_, "operand number out of range in format");
1203 if (argnum > dollar_max_arg_used)
1204 dollar_max_arg_used = argnum;
1205 /* For vprintf-style functions we may need to allocate more memory to
1206 track which arguments are used. */
1207 while (dollar_arguments_alloc < dollar_max_arg_used)
1210 nalloc = 2 * dollar_arguments_alloc + 16;
1211 dollar_arguments_used = XRESIZEVEC (char, dollar_arguments_used,
1213 dollar_arguments_pointer_p = XRESIZEVEC (char, dollar_arguments_pointer_p,
1215 memset (dollar_arguments_used + dollar_arguments_alloc, 0,
1216 nalloc - dollar_arguments_alloc);
1217 dollar_arguments_alloc = nalloc;
1219 if (!(fki->flags & (int) FMT_FLAG_DOLLAR_MULTIPLE)
1220 && dollar_arguments_used[argnum - 1] == 1)
1222 dollar_arguments_used[argnum - 1] = 2;
1223 warning (OPT_Wformat_, "format argument %d used more than once in %s format",
1227 dollar_arguments_used[argnum - 1] = 1;
1228 if (dollar_first_arg_num)
1231 *param_ptr = params;
1232 for (i = 1; i < argnum && *param_ptr != 0; i++)
1233 *param_ptr = TREE_CHAIN (*param_ptr);
1235 /* This case shouldn't be caught here. */
1236 gcc_assert (*param_ptr);
1243 /* Ensure that FORMAT does not start with a decimal number followed by
1244 a $; give a diagnostic and return true if it does, false otherwise. */
1247 avoid_dollar_number (const char *format)
1249 if (!ISDIGIT (*format))
1251 while (ISDIGIT (*format))
1255 warning (OPT_Wformat_, "$ operand number used after format without operand number");
1262 /* Finish the checking for a format string that used $ operand number formats
1263 instead of non-$ formats. We check for unused operands before used ones
1264 (a serious error, since the implementation of the format function
1265 can't know what types to pass to va_arg to find the later arguments).
1266 and for unused operands at the end of the format (if we know how many
1267 arguments the format had, so not for vprintf). If there were operand
1268 numbers out of range on a non-vprintf-style format, we won't have reached
1269 here. If POINTER_GAP_OK, unused arguments are OK if all arguments are
1273 finish_dollar_format_checking (format_check_results *res, int pointer_gap_ok)
1276 bool found_pointer_gap = false;
1277 for (i = 0; i < dollar_max_arg_used; i++)
1279 if (!dollar_arguments_used[i])
1281 if (pointer_gap_ok && (dollar_first_arg_num == 0
1282 || dollar_arguments_pointer_p[i]))
1283 found_pointer_gap = true;
1285 warning_at (res->format_string_loc, OPT_Wformat_,
1286 "format argument %d unused before used argument %d in $-style format",
1287 i + 1, dollar_max_arg_used);
1290 if (found_pointer_gap
1291 || (dollar_first_arg_num
1292 && dollar_max_arg_used < dollar_arguments_count))
1294 res->number_other--;
1295 res->number_dollar_extra_args++;
1300 /* Retrieve the specification for a format flag. SPEC contains the
1301 specifications for format flags for the applicable kind of format.
1302 FLAG is the flag in question. If PREDICATES is NULL, the basic
1303 spec for that flag must be retrieved and must exist. If
1304 PREDICATES is not NULL, it is a string listing possible predicates
1305 for the spec entry; if an entry predicated on any of these is
1306 found, it is returned, otherwise NULL is returned. */
1308 static const format_flag_spec *
1309 get_flag_spec (const format_flag_spec *spec, int flag, const char *predicates)
1312 for (i = 0; spec[i].flag_char != 0; i++)
1314 if (spec[i].flag_char != flag)
1316 if (predicates != NULL)
1318 if (spec[i].predicate != 0
1319 && strchr (predicates, spec[i].predicate) != 0)
1322 else if (spec[i].predicate == 0)
1325 gcc_assert (predicates);
1330 /* Check the argument list of a call to printf, scanf, etc.
1331 INFO points to the function_format_info structure.
1332 PARAMS is the list of argument values. */
1335 check_format_info (function_format_info *info, tree params)
1337 format_check_context format_ctx;
1338 unsigned HOST_WIDE_INT arg_num;
1340 format_check_results res;
1341 /* Skip to format argument. If the argument isn't available, there's
1342 no work for us to do; prototype checking will catch the problem. */
1343 for (arg_num = 1; ; ++arg_num)
1347 if (arg_num == info->format_num)
1349 params = TREE_CHAIN (params);
1351 format_tree = TREE_VALUE (params);
1352 params = TREE_CHAIN (params);
1353 if (format_tree == 0)
1356 res.number_non_literal = 0;
1357 res.number_extra_args = 0;
1358 res.extra_arg_loc = UNKNOWN_LOCATION;
1359 res.number_dollar_extra_args = 0;
1360 res.number_wide = 0;
1361 res.number_empty = 0;
1362 res.number_unterminated = 0;
1363 res.number_other = 0;
1364 res.format_string_loc = input_location;
1366 format_ctx.res = &res;
1367 format_ctx.info = info;
1368 format_ctx.params = params;
1370 check_function_arguments_recurse (check_format_arg, &format_ctx,
1371 format_tree, arg_num);
1373 location_t loc = format_ctx.res->format_string_loc;
1374 if (res.extra_arg_loc == UNKNOWN_LOCATION)
1375 res.extra_arg_loc = loc;
1377 if (res.number_non_literal > 0)
1379 /* Functions taking a va_list normally pass a non-literal format
1380 string. These functions typically are declared with
1381 first_arg_num == 0, so avoid warning in those cases. */
1382 if (!(format_types[info->format_type].flags & (int) FMT_FLAG_ARG_CONVERT))
1384 /* For strftime-like formats, warn for not checking the format
1385 string; but there are no arguments to check. */
1386 warning_at (loc, OPT_Wformat_nonliteral,
1387 "format not a string literal, format string not checked");
1389 else if (info->first_arg_num != 0)
1391 /* If there are no arguments for the format at all, we may have
1392 printf (foo) which is likely to be a security hole. */
1393 while (arg_num + 1 < info->first_arg_num)
1397 params = TREE_CHAIN (params);
1400 if (params == 0 && warn_format_security)
1401 warning_at (loc, OPT_Wformat_security,
1402 "format not a string literal and no format arguments");
1403 else if (params == 0 && warn_format_nonliteral)
1404 warning_at (loc, OPT_Wformat_nonliteral,
1405 "format not a string literal and no format arguments");
1407 warning_at (loc, OPT_Wformat_nonliteral,
1408 "format not a string literal, argument types not checked");
1412 /* If there were extra arguments to the format, normally warn. However,
1413 the standard does say extra arguments are ignored, so in the specific
1414 case where we have multiple leaves (conditional expressions or
1415 ngettext) allow extra arguments if at least one leaf didn't have extra
1416 arguments, but was otherwise OK (either non-literal or checked OK).
1417 If the format is an empty string, this should be counted similarly to the
1418 case of extra format arguments. */
1419 if (res.number_extra_args > 0 && res.number_non_literal == 0
1420 && res.number_other == 0)
1421 warning_at (res.extra_arg_loc, OPT_Wformat_extra_args,
1422 "too many arguments for format");
1423 if (res.number_dollar_extra_args > 0 && res.number_non_literal == 0
1424 && res.number_other == 0)
1425 warning_at (loc, OPT_Wformat_extra_args, "unused arguments in $-style format");
1426 if (res.number_empty > 0 && res.number_non_literal == 0
1427 && res.number_other == 0)
1428 warning_at (loc, OPT_Wformat_zero_length, "zero-length %s format string",
1429 format_types[info->format_type].name);
1431 if (res.number_wide > 0)
1432 warning_at (loc, OPT_Wformat_, "format is a wide character string");
1434 if (res.number_unterminated > 0)
1435 warning_at (loc, OPT_Wformat_, "unterminated format string");
1438 /* Callback from check_function_arguments_recurse to check a
1439 format string. FORMAT_TREE is the format parameter. ARG_NUM
1440 is the number of the format argument. CTX points to a
1441 format_check_context. */
1444 check_format_arg (void *ctx, tree format_tree,
1445 unsigned HOST_WIDE_INT arg_num)
1447 format_check_context *format_ctx = (format_check_context *) ctx;
1448 format_check_results *res = format_ctx->res;
1449 function_format_info *info = format_ctx->info;
1450 tree params = format_ctx->params;
1453 HOST_WIDE_INT offset;
1454 const char *format_chars;
1455 tree array_size = 0;
1457 alloc_pool fwt_pool;
1459 if (TREE_CODE (format_tree) == VAR_DECL)
1461 /* Pull out a constant value if the front end didn't. */
1462 format_tree = decl_constant_value (format_tree);
1463 STRIP_NOPS (format_tree);
1466 if (integer_zerop (format_tree))
1468 /* Skip to first argument to check, so we can see if this format
1469 has any arguments (it shouldn't). */
1470 while (arg_num + 1 < info->first_arg_num)
1474 params = TREE_CHAIN (params);
1479 res->number_other++;
1482 if (res->number_extra_args == 0)
1483 res->extra_arg_loc = EXPR_LOC_OR_LOC (TREE_VALUE (params),
1485 res->number_extra_args++;
1491 if (TREE_CODE (format_tree) == POINTER_PLUS_EXPR)
1495 arg0 = TREE_OPERAND (format_tree, 0);
1496 arg1 = TREE_OPERAND (format_tree, 1);
1499 if (TREE_CODE (arg1) == INTEGER_CST)
1503 res->number_non_literal++;
1506 /* POINTER_PLUS_EXPR offsets are to be interpreted signed. */
1507 if (!cst_and_fits_in_hwi (arg1))
1509 res->number_non_literal++;
1512 offset = int_cst_value (arg1);
1514 if (TREE_CODE (format_tree) != ADDR_EXPR)
1516 res->number_non_literal++;
1519 res->format_string_loc = EXPR_LOC_OR_LOC (format_tree, input_location);
1520 format_tree = TREE_OPERAND (format_tree, 0);
1521 if (format_types[info->format_type].flags
1522 & (int) FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL)
1524 bool objc_str = (info->format_type == gcc_objc_string_format_type);
1525 /* We cannot examine this string here - but we can check that it is
1527 if (TREE_CODE (format_tree) != CONST_DECL
1528 || !((objc_str && objc_string_ref_type_p (TREE_TYPE (format_tree)))
1529 || (*targetcm.string_object_ref_type_p)
1530 ((const_tree) TREE_TYPE (format_tree))))
1532 res->number_non_literal++;
1535 /* Skip to first argument to check. */
1536 while (arg_num + 1 < info->first_arg_num)
1540 params = TREE_CHAIN (params);
1543 /* So, we have a valid literal string object and one or more params.
1544 We need to use an external helper to parse the string into format
1545 info. For Objective-C variants we provide the resource within the
1546 objc tree, for target variants, via a hook. */
1548 objc_check_format_arg (format_tree, params);
1549 else if (targetcm.check_string_object_format_arg)
1550 (*targetcm.check_string_object_format_arg) (format_tree, params);
1551 /* Else we can't handle it and retire quietly. */
1554 if (TREE_CODE (format_tree) == ARRAY_REF
1555 && tree_fits_shwi_p (TREE_OPERAND (format_tree, 1))
1556 && (offset += tree_to_shwi (TREE_OPERAND (format_tree, 1))) >= 0)
1557 format_tree = TREE_OPERAND (format_tree, 0);
1560 res->number_non_literal++;
1563 if (TREE_CODE (format_tree) == VAR_DECL
1564 && TREE_CODE (TREE_TYPE (format_tree)) == ARRAY_TYPE
1565 && (array_init = decl_constant_value (format_tree)) != format_tree
1566 && TREE_CODE (array_init) == STRING_CST)
1568 /* Extract the string constant initializer. Note that this may include
1569 a trailing NUL character that is not in the array (e.g.
1570 const char a[3] = "foo";). */
1571 array_size = DECL_SIZE_UNIT (format_tree);
1572 format_tree = array_init;
1574 if (TREE_CODE (format_tree) != STRING_CST)
1576 res->number_non_literal++;
1579 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree))) != char_type_node)
1584 format_chars = TREE_STRING_POINTER (format_tree);
1585 format_length = TREE_STRING_LENGTH (format_tree);
1586 if (array_size != 0)
1588 /* Variable length arrays can't be initialized. */
1589 gcc_assert (TREE_CODE (array_size) == INTEGER_CST);
1591 if (tree_fits_shwi_p (array_size))
1593 HOST_WIDE_INT array_size_value = tree_to_shwi (array_size);
1594 if (array_size_value > 0
1595 && array_size_value == (int) array_size_value
1596 && format_length > array_size_value)
1597 format_length = array_size_value;
1602 if (offset >= format_length)
1604 res->number_non_literal++;
1607 format_chars += offset;
1608 format_length -= offset;
1610 if (format_length < 1 || format_chars[--format_length] != 0)
1612 res->number_unterminated++;
1615 if (format_length == 0)
1617 res->number_empty++;
1621 /* Skip to first argument to check. */
1622 while (arg_num + 1 < info->first_arg_num)
1626 params = TREE_CHAIN (params);
1629 /* Provisionally increment res->number_other; check_format_info_main
1630 will decrement it if it finds there are extra arguments, but this way
1631 need not adjust it for every return. */
1632 res->number_other++;
1633 fwt_pool = create_alloc_pool ("format_wanted_type pool",
1634 sizeof (format_wanted_type), 10);
1635 check_format_info_main (res, info, format_chars, format_length,
1636 params, arg_num, fwt_pool);
1637 free_alloc_pool (fwt_pool);
1641 /* Do the main part of checking a call to a format function. FORMAT_CHARS
1642 is the NUL-terminated format string (which at this point may contain
1643 internal NUL characters); FORMAT_LENGTH is its length (excluding the
1644 terminating NUL character). ARG_NUM is one less than the number of
1645 the first format argument to check; PARAMS points to that format
1646 argument in the list of arguments. */
1649 check_format_info_main (format_check_results *res,
1650 function_format_info *info, const char *format_chars,
1651 int format_length, tree params,
1652 unsigned HOST_WIDE_INT arg_num, alloc_pool fwt_pool)
1654 const char *orig_format_chars = format_chars;
1655 tree first_fillin_param = params;
1657 const format_kind_info *fki = &format_types[info->format_type];
1658 const format_flag_spec *flag_specs = fki->flag_specs;
1659 const format_flag_pair *bad_flag_pairs = fki->bad_flag_pairs;
1660 location_t format_string_loc = res->format_string_loc;
1662 /* -1 if no conversions taking an operand have been found; 0 if one has
1663 and it didn't use $; 1 if $ formats are in use. */
1664 int has_operand_number = -1;
1666 init_dollar_format_checking (info->first_arg_num, first_fillin_param);
1668 while (*format_chars != 0)
1671 int suppressed = FALSE;
1672 const char *length_chars = NULL;
1673 enum format_lengths length_chars_val = FMT_LEN_none;
1674 enum format_std_version length_chars_std = STD_C89;
1678 int main_arg_num = 0;
1679 tree main_arg_params = 0;
1680 enum format_std_version wanted_type_std;
1681 const char *wanted_type_name;
1682 format_wanted_type width_wanted_type;
1683 format_wanted_type precision_wanted_type;
1684 format_wanted_type main_wanted_type;
1685 format_wanted_type *first_wanted_type = NULL;
1686 format_wanted_type *last_wanted_type = NULL;
1687 const format_length_info *fli = NULL;
1688 const format_char_info *fci = NULL;
1689 char flag_chars[256];
1691 int scalar_identity_flag = 0;
1692 const char *format_start;
1694 if (*format_chars++ != '%')
1696 if (*format_chars == 0)
1698 warning_at (format_string_loc, OPT_Wformat_,
1699 "spurious trailing %<%%%> in format");
1702 if (*format_chars == '%')
1709 if ((fki->flags & (int) FMT_FLAG_USE_DOLLAR) && has_operand_number != 0)
1711 /* Possibly read a $ operand number at the start of the format.
1712 If one was previously used, one is required here. If one
1713 is not used here, we can't immediately conclude this is a
1714 format without them, since it could be printf %m or scanf %*. */
1716 opnum = maybe_read_dollar_number (&format_chars, 0,
1718 &main_arg_params, fki);
1723 has_operand_number = 1;
1724 main_arg_num = opnum + info->first_arg_num - 1;
1727 else if (fki->flags & FMT_FLAG_USE_DOLLAR)
1729 if (avoid_dollar_number (format_chars))
1733 /* Read any format flags, but do not yet validate them beyond removing
1734 duplicates, since in general validation depends on the rest of
1736 while (*format_chars != 0
1737 && strchr (fki->flag_chars, *format_chars) != 0)
1739 const format_flag_spec *s = get_flag_spec (flag_specs,
1740 *format_chars, NULL);
1741 if (strchr (flag_chars, *format_chars) != 0)
1743 warning_at (format_string_loc, OPT_Wformat_,
1744 "repeated %s in format", _(s->name));
1748 i = strlen (flag_chars);
1749 flag_chars[i++] = *format_chars;
1752 if (s->skip_next_char)
1755 if (*format_chars == 0)
1757 warning_at (format_string_loc, OPT_Wformat_,
1758 "missing fill character at end of strfmon format");
1765 /* Read any format width, possibly * or *m$. */
1766 if (fki->width_char != 0)
1768 if (fki->width_type != NULL && *format_chars == '*')
1770 i = strlen (flag_chars);
1771 flag_chars[i++] = fki->width_char;
1773 /* "...a field width...may be indicated by an asterisk.
1774 In this case, an int argument supplies the field width..." */
1776 if (has_operand_number != 0)
1779 opnum = maybe_read_dollar_number (&format_chars,
1780 has_operand_number == 1,
1787 has_operand_number = 1;
1788 arg_num = opnum + info->first_arg_num - 1;
1791 has_operand_number = 0;
1795 if (avoid_dollar_number (format_chars))
1798 if (info->first_arg_num != 0)
1804 cur_param = TREE_VALUE (params);
1805 if (has_operand_number <= 0)
1807 params = TREE_CHAIN (params);
1811 width_wanted_type.wanted_type = *fki->width_type;
1812 width_wanted_type.wanted_type_name = NULL;
1813 width_wanted_type.pointer_count = 0;
1814 width_wanted_type.char_lenient_flag = 0;
1815 width_wanted_type.scalar_identity_flag = 0;
1816 width_wanted_type.writing_in_flag = 0;
1817 width_wanted_type.reading_from_flag = 0;
1818 width_wanted_type.kind = CF_KIND_FIELD_WIDTH;
1819 width_wanted_type.format_start = format_chars - 1;
1820 width_wanted_type.format_length = 1;
1821 width_wanted_type.param = cur_param;
1822 width_wanted_type.arg_num = arg_num;
1823 width_wanted_type.next = NULL;
1824 if (last_wanted_type != 0)
1825 last_wanted_type->next = &width_wanted_type;
1826 if (first_wanted_type == 0)
1827 first_wanted_type = &width_wanted_type;
1828 last_wanted_type = &width_wanted_type;
1833 /* Possibly read a numeric width. If the width is zero,
1834 we complain if appropriate. */
1835 int non_zero_width_char = FALSE;
1836 int found_width = FALSE;
1837 while (ISDIGIT (*format_chars))
1840 if (*format_chars != '0')
1841 non_zero_width_char = TRUE;
1844 if (found_width && !non_zero_width_char &&
1845 (fki->flags & (int) FMT_FLAG_ZERO_WIDTH_BAD))
1846 warning_at (format_string_loc, OPT_Wformat_,
1847 "zero width in %s format", fki->name);
1850 i = strlen (flag_chars);
1851 flag_chars[i++] = fki->width_char;
1857 /* Read any format left precision (must be a number, not *). */
1858 if (fki->left_precision_char != 0 && *format_chars == '#')
1861 i = strlen (flag_chars);
1862 flag_chars[i++] = fki->left_precision_char;
1864 if (!ISDIGIT (*format_chars))
1865 warning_at (format_string_loc, OPT_Wformat_,
1866 "empty left precision in %s format", fki->name);
1867 while (ISDIGIT (*format_chars))
1871 /* Read any format precision, possibly * or *m$. */
1872 if (fki->precision_char != 0 && *format_chars == '.')
1875 i = strlen (flag_chars);
1876 flag_chars[i++] = fki->precision_char;
1878 if (fki->precision_type != NULL && *format_chars == '*')
1880 /* "...a...precision...may be indicated by an asterisk.
1881 In this case, an int argument supplies the...precision." */
1883 if (has_operand_number != 0)
1886 opnum = maybe_read_dollar_number (&format_chars,
1887 has_operand_number == 1,
1894 has_operand_number = 1;
1895 arg_num = opnum + info->first_arg_num - 1;
1898 has_operand_number = 0;
1902 if (avoid_dollar_number (format_chars))
1905 if (info->first_arg_num != 0)
1911 cur_param = TREE_VALUE (params);
1912 if (has_operand_number <= 0)
1914 params = TREE_CHAIN (params);
1918 precision_wanted_type.wanted_type = *fki->precision_type;
1919 precision_wanted_type.wanted_type_name = NULL;
1920 precision_wanted_type.pointer_count = 0;
1921 precision_wanted_type.char_lenient_flag = 0;
1922 precision_wanted_type.scalar_identity_flag = 0;
1923 precision_wanted_type.writing_in_flag = 0;
1924 precision_wanted_type.reading_from_flag = 0;
1925 precision_wanted_type.kind = CF_KIND_FIELD_PRECISION;
1926 precision_wanted_type.param = cur_param;
1927 precision_wanted_type.format_start = format_chars - 2;
1928 precision_wanted_type.format_length = 2;
1929 precision_wanted_type.arg_num = arg_num;
1930 precision_wanted_type.next = NULL;
1931 if (last_wanted_type != 0)
1932 last_wanted_type->next = &precision_wanted_type;
1933 if (first_wanted_type == 0)
1934 first_wanted_type = &precision_wanted_type;
1935 last_wanted_type = &precision_wanted_type;
1940 if (!(fki->flags & (int) FMT_FLAG_EMPTY_PREC_OK)
1941 && !ISDIGIT (*format_chars))
1942 warning_at (format_string_loc, OPT_Wformat_,
1943 "empty precision in %s format", fki->name);
1944 while (ISDIGIT (*format_chars))
1949 format_start = format_chars;
1950 if (fki->alloc_char && fki->alloc_char == *format_chars)
1952 i = strlen (flag_chars);
1953 flag_chars[i++] = fki->alloc_char;
1958 /* Handle the scanf allocation kludge. */
1959 if (fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
1961 if (*format_chars == 'a' && !flag_isoc99)
1963 if (format_chars[1] == 's' || format_chars[1] == 'S'
1964 || format_chars[1] == '[')
1966 /* 'a' is used as a flag. */
1967 i = strlen (flag_chars);
1968 flag_chars[i++] = 'a';
1975 /* Read any length modifier, if this kind of format has them. */
1976 fli = fki->length_char_specs;
1977 length_chars = NULL;
1978 length_chars_val = FMT_LEN_none;
1979 length_chars_std = STD_C89;
1980 scalar_identity_flag = 0;
1983 while (fli->name != 0
1984 && strncmp (fli->name, format_chars, strlen (fli->name)))
1988 format_chars += strlen (fli->name);
1989 if (fli->double_name != 0 && fli->name[0] == *format_chars)
1992 length_chars = fli->double_name;
1993 length_chars_val = fli->double_index;
1994 length_chars_std = fli->double_std;
1998 length_chars = fli->name;
1999 length_chars_val = fli->index;
2000 length_chars_std = fli->std;
2001 scalar_identity_flag = fli->scalar_identity_flag;
2003 i = strlen (flag_chars);
2004 flag_chars[i++] = fki->length_code_char;
2009 /* Warn if the length modifier is non-standard. */
2010 if (ADJ_STD (length_chars_std) > C_STD_VER)
2011 warning_at (format_string_loc, OPT_Wformat_,
2012 "%s does not support the %qs %s length modifier",
2013 C_STD_NAME (length_chars_std), length_chars,
2018 /* Read any modifier (strftime E/O). */
2019 if (fki->modifier_chars != NULL)
2021 while (*format_chars != 0
2022 && strchr (fki->modifier_chars, *format_chars) != 0)
2024 if (strchr (flag_chars, *format_chars) != 0)
2026 const format_flag_spec *s = get_flag_spec (flag_specs,
2027 *format_chars, NULL);
2028 warning_at (format_string_loc, OPT_Wformat_,
2029 "repeated %s in format", _(s->name));
2033 i = strlen (flag_chars);
2034 flag_chars[i++] = *format_chars;
2041 format_char = *format_chars;
2042 if (format_char == 0
2043 || (!(fki->flags & (int) FMT_FLAG_FANCY_PERCENT_OK)
2044 && format_char == '%'))
2046 warning_at (format_string_loc, OPT_Wformat_,
2047 "conversion lacks type at end of format");
2051 fci = fki->conversion_specs;
2052 while (fci->format_chars != 0
2053 && strchr (fci->format_chars, format_char) == 0)
2055 if (fci->format_chars == 0)
2057 if (ISGRAPH (format_char))
2058 warning_at (format_string_loc, OPT_Wformat_,
2059 "unknown conversion type character %qc in format",
2062 warning_at (format_string_loc, OPT_Wformat_,
2063 "unknown conversion type character 0x%x in format",
2069 if (ADJ_STD (fci->std) > C_STD_VER)
2070 warning_at (format_string_loc, OPT_Wformat_,
2071 "%s does not support the %<%%%c%> %s format",
2072 C_STD_NAME (fci->std), format_char, fki->name);
2075 /* Validate the individual flags used, removing any that are invalid. */
2078 for (i = 0; flag_chars[i] != 0; i++)
2080 const format_flag_spec *s = get_flag_spec (flag_specs,
2081 flag_chars[i], NULL);
2082 flag_chars[i - d] = flag_chars[i];
2083 if (flag_chars[i] == fki->length_code_char)
2085 if (strchr (fci->flag_chars, flag_chars[i]) == 0)
2087 warning_at (format_string_loc,
2088 OPT_Wformat_, "%s used with %<%%%c%> %s format",
2089 _(s->name), format_char, fki->name);
2095 const format_flag_spec *t;
2096 if (ADJ_STD (s->std) > C_STD_VER)
2097 warning_at (format_string_loc, OPT_Wformat_,
2098 "%s does not support %s",
2099 C_STD_NAME (s->std), _(s->long_name));
2100 t = get_flag_spec (flag_specs, flag_chars[i], fci->flags2);
2101 if (t != NULL && ADJ_STD (t->std) > ADJ_STD (s->std))
2103 const char *long_name = (t->long_name != NULL
2106 if (ADJ_STD (t->std) > C_STD_VER)
2107 warning_at (format_string_loc, OPT_Wformat_,
2108 "%s does not support %s with the %<%%%c%> %s format",
2109 C_STD_NAME (t->std), _(long_name),
2110 format_char, fki->name);
2114 flag_chars[i - d] = 0;
2117 if ((fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
2118 && strchr (flag_chars, 'a') != 0)
2120 if (fki->alloc_char && strchr (flag_chars, fki->alloc_char) != 0)
2123 if (fki->suppression_char
2124 && strchr (flag_chars, fki->suppression_char) != 0)
2127 /* Validate the pairs of flags used. */
2128 for (i = 0; bad_flag_pairs[i].flag_char1 != 0; i++)
2130 const format_flag_spec *s, *t;
2131 if (strchr (flag_chars, bad_flag_pairs[i].flag_char1) == 0)
2133 if (strchr (flag_chars, bad_flag_pairs[i].flag_char2) == 0)
2135 if (bad_flag_pairs[i].predicate != 0
2136 && strchr (fci->flags2, bad_flag_pairs[i].predicate) == 0)
2138 s = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char1, NULL);
2139 t = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char2, NULL);
2140 if (bad_flag_pairs[i].ignored)
2142 if (bad_flag_pairs[i].predicate != 0)
2143 warning_at (format_string_loc, OPT_Wformat_,
2144 "%s ignored with %s and %<%%%c%> %s format",
2145 _(s->name), _(t->name), format_char,
2148 warning_at (format_string_loc, OPT_Wformat_,
2149 "%s ignored with %s in %s format",
2150 _(s->name), _(t->name), fki->name);
2154 if (bad_flag_pairs[i].predicate != 0)
2155 warning_at (format_string_loc, OPT_Wformat_,
2156 "use of %s and %s together with %<%%%c%> %s format",
2157 _(s->name), _(t->name), format_char,
2160 warning_at (format_string_loc, OPT_Wformat_,
2161 "use of %s and %s together in %s format",
2162 _(s->name), _(t->name), fki->name);
2166 /* Give Y2K warnings. */
2167 if (warn_format_y2k)
2170 if (strchr (fci->flags2, '4') != 0)
2171 if (strchr (flag_chars, 'E') != 0)
2175 else if (strchr (fci->flags2, '3') != 0)
2177 else if (strchr (fci->flags2, '2') != 0)
2180 warning_at (format_string_loc, OPT_Wformat_y2k,
2181 "%<%%%c%> yields only last 2 digits of "
2182 "year in some locales", format_char);
2183 else if (y2k_level == 2)
2184 warning_at (format_string_loc, OPT_Wformat_y2k,
2185 "%<%%%c%> yields only last 2 digits of year",
2189 if (strchr (fci->flags2, '[') != 0)
2191 /* Skip over scan set, in case it happens to have '%' in it. */
2192 if (*format_chars == '^')
2194 /* Find closing bracket; if one is hit immediately, then
2195 it's part of the scan set rather than a terminator. */
2196 if (*format_chars == ']')
2198 while (*format_chars && *format_chars != ']')
2200 if (*format_chars != ']')
2201 /* The end of the format string was reached. */
2202 warning_at (format_string_loc, OPT_Wformat_,
2203 "no closing %<]%> for %<%%[%> format");
2207 wanted_type_name = 0;
2208 if (fki->flags & (int) FMT_FLAG_ARG_CONVERT)
2210 wanted_type = (fci->types[length_chars_val].type
2211 ? *fci->types[length_chars_val].type : 0);
2212 wanted_type_name = fci->types[length_chars_val].name;
2213 wanted_type_std = fci->types[length_chars_val].std;
2214 if (wanted_type == 0)
2216 warning_at (format_string_loc, OPT_Wformat_,
2217 "use of %qs length modifier with %qc type character",
2218 length_chars, format_char);
2219 /* Heuristic: skip one argument when an invalid length/type
2220 combination is encountered. */
2223 params = TREE_CHAIN (params);
2227 /* Warn if non-standard, provided it is more non-standard
2228 than the length and type characters that may already
2229 have been warned for. */
2230 && ADJ_STD (wanted_type_std) > ADJ_STD (length_chars_std)
2231 && ADJ_STD (wanted_type_std) > ADJ_STD (fci->std))
2233 if (ADJ_STD (wanted_type_std) > C_STD_VER)
2234 warning_at (format_string_loc, OPT_Wformat_,
2235 "%s does not support the %<%%%s%c%> %s format",
2236 C_STD_NAME (wanted_type_std), length_chars,
2237 format_char, fki->name);
2241 main_wanted_type.next = NULL;
2243 /* Finally. . .check type of argument against desired type! */
2244 if (info->first_arg_num == 0)
2246 if ((fci->pointer_count == 0 && wanted_type == void_type_node)
2249 if (main_arg_num != 0)
2252 warning_at (format_string_loc, OPT_Wformat_,
2253 "operand number specified with "
2254 "suppressed assignment");
2256 warning_at (format_string_loc, OPT_Wformat_,
2257 "operand number specified for format "
2258 "taking no argument");
2263 format_wanted_type *wanted_type_ptr;
2265 if (main_arg_num != 0)
2267 arg_num = main_arg_num;
2268 params = main_arg_params;
2273 if (has_operand_number > 0)
2275 warning_at (format_string_loc, OPT_Wformat_,
2276 "missing $ operand number in format");
2280 has_operand_number = 0;
2283 wanted_type_ptr = &main_wanted_type;
2290 cur_param = TREE_VALUE (params);
2291 params = TREE_CHAIN (params);
2294 wanted_type_ptr->wanted_type = wanted_type;
2295 wanted_type_ptr->wanted_type_name = wanted_type_name;
2296 wanted_type_ptr->pointer_count = fci->pointer_count + alloc_flag;
2297 wanted_type_ptr->char_lenient_flag = 0;
2298 if (strchr (fci->flags2, 'c') != 0)
2299 wanted_type_ptr->char_lenient_flag = 1;
2300 wanted_type_ptr->scalar_identity_flag = 0;
2301 if (scalar_identity_flag)
2302 wanted_type_ptr->scalar_identity_flag = 1;
2303 wanted_type_ptr->writing_in_flag = 0;
2304 wanted_type_ptr->reading_from_flag = 0;
2306 wanted_type_ptr->writing_in_flag = 1;
2309 if (strchr (fci->flags2, 'W') != 0)
2310 wanted_type_ptr->writing_in_flag = 1;
2311 if (strchr (fci->flags2, 'R') != 0)
2312 wanted_type_ptr->reading_from_flag = 1;
2314 wanted_type_ptr->kind = CF_KIND_FORMAT;
2315 wanted_type_ptr->param = cur_param;
2316 wanted_type_ptr->arg_num = arg_num;
2317 wanted_type_ptr->format_start = format_start;
2318 wanted_type_ptr->format_length = format_chars - format_start;
2319 wanted_type_ptr->next = NULL;
2320 if (last_wanted_type != 0)
2321 last_wanted_type->next = wanted_type_ptr;
2322 if (first_wanted_type == 0)
2323 first_wanted_type = wanted_type_ptr;
2324 last_wanted_type = wanted_type_ptr;
2329 wanted_type_ptr = (format_wanted_type *)
2330 pool_alloc (fwt_pool);
2332 wanted_type = *fci->types[length_chars_val].type;
2333 wanted_type_name = fci->types[length_chars_val].name;
2338 if (first_wanted_type != 0)
2339 check_format_types (format_string_loc, first_wanted_type);
2342 if (format_chars - orig_format_chars != format_length)
2343 warning_at (format_string_loc, OPT_Wformat_contains_nul,
2344 "embedded %<\\0%> in format");
2345 if (info->first_arg_num != 0 && params != 0
2346 && has_operand_number <= 0)
2348 res->number_other--;
2349 res->number_extra_args++;
2351 if (has_operand_number > 0)
2352 finish_dollar_format_checking (res, fki->flags & (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK);
2356 /* Check the argument types from a single format conversion (possibly
2357 including width and precision arguments). LOC is the location of
2358 the format string. */
2360 check_format_types (location_t loc, format_wanted_type *types)
2362 for (; types != 0; types = types->next)
2372 wanted_type = types->wanted_type;
2373 arg_num = types->arg_num;
2375 /* The following should not occur here. */
2376 gcc_assert (wanted_type);
2377 gcc_assert (wanted_type != void_type_node || types->pointer_count);
2379 if (types->pointer_count == 0)
2380 wanted_type = lang_hooks.types.type_promotes_to (wanted_type);
2382 wanted_type = TYPE_MAIN_VARIANT (wanted_type);
2384 cur_param = types->param;
2387 format_type_warning (loc, types, wanted_type, NULL);
2391 cur_type = TREE_TYPE (cur_param);
2392 if (cur_type == error_mark_node)
2394 orig_cur_type = cur_type;
2397 STRIP_NOPS (cur_param);
2399 /* Check the types of any additional pointer arguments
2400 that precede the "real" argument. */
2401 for (i = 0; i < types->pointer_count; ++i)
2403 if (TREE_CODE (cur_type) == POINTER_TYPE)
2405 cur_type = TREE_TYPE (cur_type);
2406 if (cur_type == error_mark_node)
2409 /* Check for writing through a NULL pointer. */
2410 if (types->writing_in_flag
2413 && integer_zerop (cur_param))
2414 warning (OPT_Wformat_, "writing through null pointer "
2415 "(argument %d)", arg_num);
2417 /* Check for reading through a NULL pointer. */
2418 if (types->reading_from_flag
2421 && integer_zerop (cur_param))
2422 warning (OPT_Wformat_, "reading through null pointer "
2423 "(argument %d)", arg_num);
2425 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
2426 cur_param = TREE_OPERAND (cur_param, 0);
2430 /* See if this is an attempt to write into a const type with
2431 scanf or with printf "%n". Note: the writing in happens
2432 at the first indirection only, if for example
2433 void * const * is passed to scanf %p; passing
2434 const void ** is simply passing an incompatible type. */
2435 if (types->writing_in_flag
2437 && (TYPE_READONLY (cur_type)
2439 && (CONSTANT_CLASS_P (cur_param)
2440 || (DECL_P (cur_param)
2441 && TREE_READONLY (cur_param))))))
2442 warning (OPT_Wformat_, "writing into constant object "
2443 "(argument %d)", arg_num);
2445 /* If there are extra type qualifiers beyond the first
2446 indirection, then this makes the types technically
2450 && (TYPE_READONLY (cur_type)
2451 || TYPE_VOLATILE (cur_type)
2452 || TYPE_ATOMIC (cur_type)
2453 || TYPE_RESTRICT (cur_type)))
2454 warning (OPT_Wformat_, "extra type qualifiers in format "
2455 "argument (argument %d)",
2461 format_type_warning (loc, types, wanted_type, orig_cur_type);
2466 if (i < types->pointer_count)
2469 cur_type = TYPE_MAIN_VARIANT (cur_type);
2471 /* Check whether the argument type is a character type. This leniency
2472 only applies to certain formats, flagged with 'c'. */
2473 if (types->char_lenient_flag)
2474 char_type_flag = (cur_type == char_type_node
2475 || cur_type == signed_char_type_node
2476 || cur_type == unsigned_char_type_node);
2478 /* Check the type of the "real" argument, if there's a type we want. */
2479 if (lang_hooks.types_compatible_p (wanted_type, cur_type))
2481 /* If we want 'void *', allow any pointer type.
2482 (Anything else would already have got a warning.)
2483 With -Wpedantic, only allow pointers to void and to character
2485 if (wanted_type == void_type_node
2486 && (!pedantic || (i == 1 && char_type_flag)))
2488 /* Don't warn about differences merely in signedness, unless
2489 -Wpedantic. With -Wpedantic, warn if the type is a pointer
2490 target and not a character type, and for character types at
2491 a second level of indirection. */
2492 if (TREE_CODE (wanted_type) == INTEGER_TYPE
2493 && TREE_CODE (cur_type) == INTEGER_TYPE
2494 && ((!pedantic && !warn_format_signedness)
2495 || (i == 0 && !warn_format_signedness)
2496 || (i == 1 && char_type_flag))
2497 && (TYPE_UNSIGNED (wanted_type)
2498 ? wanted_type == c_common_unsigned_type (cur_type)
2499 : wanted_type == c_common_signed_type (cur_type)))
2501 /* Don't warn about differences merely in signedness if we know
2502 that the current type is integer-promoted and its original type
2503 was unsigned such as that it is in the range of WANTED_TYPE. */
2504 if (TREE_CODE (wanted_type) == INTEGER_TYPE
2505 && TREE_CODE (cur_type) == INTEGER_TYPE
2506 && warn_format_signedness
2507 && TYPE_UNSIGNED (wanted_type)
2508 && cur_param != NULL_TREE
2509 && TREE_CODE (cur_param) == NOP_EXPR)
2511 tree t = TREE_TYPE (TREE_OPERAND (cur_param, 0));
2512 if (TYPE_UNSIGNED (t)
2513 && cur_type == lang_hooks.types.type_promotes_to (t))
2516 /* Likewise, "signed char", "unsigned char" and "char" are
2517 equivalent but the above test won't consider them equivalent. */
2518 if (wanted_type == char_type_node
2519 && (!pedantic || i < 2)
2522 if (types->scalar_identity_flag
2523 && (TREE_CODE (cur_type) == TREE_CODE (wanted_type)
2524 || (INTEGRAL_TYPE_P (cur_type)
2525 && INTEGRAL_TYPE_P (wanted_type)))
2526 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type))
2528 /* Now we have a type mismatch. */
2529 format_type_warning (loc, types, wanted_type, orig_cur_type);
2534 /* Give a warning at LOC about a format argument of different type from that
2535 expected. WANTED_TYPE is the type the argument should have, possibly
2536 stripped of pointer dereferences. The description (such as "field
2537 precision"), the placement in the format string, a possibly more
2538 friendly name of WANTED_TYPE, and the number of pointer dereferences
2539 are taken from TYPE. ARG_TYPE is the type of the actual argument,
2540 or NULL if it is missing. */
2542 format_type_warning (location_t loc, format_wanted_type *type,
2543 tree wanted_type, tree arg_type)
2545 int kind = type->kind;
2546 const char *wanted_type_name = type->wanted_type_name;
2547 const char *format_start = type->format_start;
2548 int format_length = type->format_length;
2549 int pointer_count = type->pointer_count;
2550 int arg_num = type->arg_num;
2553 /* If ARG_TYPE is a typedef with a misleading name (for example,
2554 size_t but not the standard size_t expected by printf %zu), avoid
2555 printing the typedef name. */
2556 if (wanted_type_name
2558 && TYPE_NAME (arg_type)
2559 && TREE_CODE (TYPE_NAME (arg_type)) == TYPE_DECL
2560 && DECL_NAME (TYPE_NAME (arg_type))
2561 && !strcmp (wanted_type_name,
2562 lang_hooks.decl_printable_name (TYPE_NAME (arg_type), 2)))
2563 arg_type = TYPE_MAIN_VARIANT (arg_type);
2564 /* The format type and name exclude any '*' for pointers, so those
2565 must be formatted manually. For all the types we currently have,
2566 this is adequate, but formats taking pointers to functions or
2567 arrays would require the full type to be built up in order to
2568 print it with %T. */
2569 p = (char *) alloca (pointer_count + 2);
2570 if (pointer_count == 0)
2572 else if (c_dialect_cxx ())
2574 memset (p, '*', pointer_count);
2575 p[pointer_count] = 0;
2580 memset (p + 1, '*', pointer_count);
2581 p[pointer_count + 1] = 0;
2584 if (wanted_type_name)
2587 warning_at (loc, OPT_Wformat_,
2588 "%s %<%s%.*s%> expects argument of type %<%s%s%>, "
2589 "but argument %d has type %qT",
2590 gettext (kind_descriptions[kind]),
2591 (kind == CF_KIND_FORMAT ? "%" : ""),
2592 format_length, format_start,
2593 wanted_type_name, p, arg_num, arg_type);
2595 warning_at (loc, OPT_Wformat_,
2596 "%s %<%s%.*s%> expects a matching %<%s%s%> argument",
2597 gettext (kind_descriptions[kind]),
2598 (kind == CF_KIND_FORMAT ? "%" : ""),
2599 format_length, format_start, wanted_type_name, p);
2604 warning_at (loc, OPT_Wformat_,
2605 "%s %<%s%.*s%> expects argument of type %<%T%s%>, "
2606 "but argument %d has type %qT",
2607 gettext (kind_descriptions[kind]),
2608 (kind == CF_KIND_FORMAT ? "%" : ""),
2609 format_length, format_start,
2610 wanted_type, p, arg_num, arg_type);
2612 warning_at (loc, OPT_Wformat_,
2613 "%s %<%s%.*s%> expects a matching %<%T%s%> argument",
2614 gettext (kind_descriptions[kind]),
2615 (kind == CF_KIND_FORMAT ? "%" : ""),
2616 format_length, format_start, wanted_type, p);
2621 /* Given a format_char_info array FCI, and a character C, this function
2622 returns the index into the conversion_specs where that specifier's
2623 data is located. The character must exist. */
2625 find_char_info_specifier_index (const format_char_info *fci, int c)
2629 for (i = 0; fci->format_chars; i++, fci++)
2630 if (strchr (fci->format_chars, c))
2633 /* We shouldn't be looking for a non-existent specifier. */
2637 /* Given a format_length_info array FLI, and a character C, this
2638 function returns the index into the conversion_specs where that
2639 modifier's data is located. The character must exist. */
2641 find_length_info_modifier_index (const format_length_info *fli, int c)
2645 for (i = 0; fli->name; i++, fli++)
2646 if (strchr (fli->name, c))
2649 /* We shouldn't be looking for a non-existent modifier. */
2653 /* Determine the type of HOST_WIDE_INT in the code being compiled for
2654 use in GCC's __asm_fprintf__ custom format attribute. You must
2655 have set dynamic_format_types before calling this function. */
2657 init_dynamic_asm_fprintf_info (void)
2663 format_length_info *new_asm_fprintf_length_specs;
2666 /* Find the underlying type for HOST_WIDE_INT. For the %w
2667 length modifier to work, one must have issued: "typedef
2668 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2669 prior to using that modifier. */
2670 hwi = maybe_get_identifier ("__gcc_host_wide_int__");
2673 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2676 hwi = identifier_global_value (hwi);
2677 if (!hwi || TREE_CODE (hwi) != TYPE_DECL)
2679 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2682 hwi = DECL_ORIGINAL_TYPE (hwi);
2684 if (hwi != long_integer_type_node && hwi != long_long_integer_type_node)
2686 error ("%<__gcc_host_wide_int__%> is not defined as %<long%>"
2687 " or %<long long%>");
2691 /* Create a new (writable) copy of asm_fprintf_length_specs. */
2692 new_asm_fprintf_length_specs = (format_length_info *)
2693 xmemdup (asm_fprintf_length_specs,
2694 sizeof (asm_fprintf_length_specs),
2695 sizeof (asm_fprintf_length_specs));
2697 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2698 i = find_length_info_modifier_index (new_asm_fprintf_length_specs, 'w');
2699 if (hwi == long_integer_type_node)
2700 new_asm_fprintf_length_specs[i].index = FMT_LEN_l;
2701 else if (hwi == long_long_integer_type_node)
2702 new_asm_fprintf_length_specs[i].index = FMT_LEN_ll;
2706 /* Assign the new data for use. */
2707 dynamic_format_types[asm_fprintf_format_type].length_char_specs =
2708 new_asm_fprintf_length_specs;
2712 /* Determine the type of a "locus" in the code being compiled for use
2713 in GCC's __gcc_gfc__ custom format attribute. You must have set
2714 dynamic_format_types before calling this function. */
2716 init_dynamic_gfc_info (void)
2722 static format_char_info *gfc_fci;
2724 /* For the GCC __gcc_gfc__ custom format specifier to work, one
2725 must have declared 'locus' prior to using this attribute. If
2726 we haven't seen this declarations then you shouldn't use the
2727 specifier requiring that type. */
2728 if ((locus = maybe_get_identifier ("locus")))
2730 locus = identifier_global_value (locus);
2733 if (TREE_CODE (locus) != TYPE_DECL
2734 || TREE_TYPE (locus) == error_mark_node)
2736 error ("%<locus%> is not defined as a type");
2740 locus = TREE_TYPE (locus);
2744 /* Assign the new data for use. */
2746 /* Handle the __gcc_gfc__ format specifics. */
2748 dynamic_format_types[gcc_gfc_format_type].conversion_specs =
2749 gfc_fci = (format_char_info *)
2750 xmemdup (gcc_gfc_char_table,
2751 sizeof (gcc_gfc_char_table),
2752 sizeof (gcc_gfc_char_table));
2755 const unsigned i = find_char_info_specifier_index (gfc_fci, 'L');
2756 gfc_fci[i].types[0].type = &locus;
2757 gfc_fci[i].pointer_count = 1;
2762 /* Determine the types of "tree" and "location_t" in the code being
2763 compiled for use in GCC's diagnostic custom format attributes. You
2764 must have set dynamic_format_types before calling this function. */
2766 init_dynamic_diag_info (void)
2768 static tree t, loc, hwi;
2770 if (!loc || !t || !hwi)
2772 static format_char_info *diag_fci, *tdiag_fci, *cdiag_fci, *cxxdiag_fci;
2773 static format_length_info *diag_ls;
2776 /* For the GCC-diagnostics custom format specifiers to work, one
2777 must have declared 'tree' and/or 'location_t' prior to using
2778 those attributes. If we haven't seen these declarations then
2779 you shouldn't use the specifiers requiring these types.
2780 However we don't force a hard ICE because we may see only one
2781 or the other type. */
2782 if ((loc = maybe_get_identifier ("location_t")))
2784 loc = identifier_global_value (loc);
2787 if (TREE_CODE (loc) != TYPE_DECL)
2789 error ("%<location_t%> is not defined as a type");
2793 loc = TREE_TYPE (loc);
2797 /* We need to grab the underlying 'union tree_node' so peek into
2798 an extra type level. */
2799 if ((t = maybe_get_identifier ("tree")))
2801 t = identifier_global_value (t);
2804 if (TREE_CODE (t) != TYPE_DECL)
2806 error ("%<tree%> is not defined as a type");
2809 else if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
2811 error ("%<tree%> is not defined as a pointer type");
2815 t = TREE_TYPE (TREE_TYPE (t));
2819 /* Find the underlying type for HOST_WIDE_INT. For the %w
2820 length modifier to work, one must have issued: "typedef
2821 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2822 prior to using that modifier. */
2823 if ((hwi = maybe_get_identifier ("__gcc_host_wide_int__")))
2825 hwi = identifier_global_value (hwi);
2828 if (TREE_CODE (hwi) != TYPE_DECL)
2830 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2835 hwi = DECL_ORIGINAL_TYPE (hwi);
2837 if (hwi != long_integer_type_node
2838 && hwi != long_long_integer_type_node)
2840 error ("%<__gcc_host_wide_int__%> is not defined"
2841 " as %<long%> or %<long long%>");
2848 /* Assign the new data for use. */
2850 /* All the GCC diag formats use the same length specs. */
2852 dynamic_format_types[gcc_diag_format_type].length_char_specs =
2853 dynamic_format_types[gcc_tdiag_format_type].length_char_specs =
2854 dynamic_format_types[gcc_cdiag_format_type].length_char_specs =
2855 dynamic_format_types[gcc_cxxdiag_format_type].length_char_specs =
2856 diag_ls = (format_length_info *)
2857 xmemdup (gcc_diag_length_specs,
2858 sizeof (gcc_diag_length_specs),
2859 sizeof (gcc_diag_length_specs));
2862 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2863 i = find_length_info_modifier_index (diag_ls, 'w');
2864 if (hwi == long_integer_type_node)
2865 diag_ls[i].index = FMT_LEN_l;
2866 else if (hwi == long_long_integer_type_node)
2867 diag_ls[i].index = FMT_LEN_ll;
2872 /* Handle the __gcc_diag__ format specifics. */
2874 dynamic_format_types[gcc_diag_format_type].conversion_specs =
2875 diag_fci = (format_char_info *)
2876 xmemdup (gcc_diag_char_table,
2877 sizeof (gcc_diag_char_table),
2878 sizeof (gcc_diag_char_table));
2881 i = find_char_info_specifier_index (diag_fci, 'K');
2882 diag_fci[i].types[0].type = &t;
2883 diag_fci[i].pointer_count = 1;
2886 /* Handle the __gcc_tdiag__ format specifics. */
2888 dynamic_format_types[gcc_tdiag_format_type].conversion_specs =
2889 tdiag_fci = (format_char_info *)
2890 xmemdup (gcc_tdiag_char_table,
2891 sizeof (gcc_tdiag_char_table),
2892 sizeof (gcc_tdiag_char_table));
2895 /* All specifiers taking a tree share the same struct. */
2896 i = find_char_info_specifier_index (tdiag_fci, 'D');
2897 tdiag_fci[i].types[0].type = &t;
2898 tdiag_fci[i].pointer_count = 1;
2899 i = find_char_info_specifier_index (tdiag_fci, 'K');
2900 tdiag_fci[i].types[0].type = &t;
2901 tdiag_fci[i].pointer_count = 1;
2904 /* Handle the __gcc_cdiag__ format specifics. */
2906 dynamic_format_types[gcc_cdiag_format_type].conversion_specs =
2907 cdiag_fci = (format_char_info *)
2908 xmemdup (gcc_cdiag_char_table,
2909 sizeof (gcc_cdiag_char_table),
2910 sizeof (gcc_cdiag_char_table));
2913 /* All specifiers taking a tree share the same struct. */
2914 i = find_char_info_specifier_index (cdiag_fci, 'D');
2915 cdiag_fci[i].types[0].type = &t;
2916 cdiag_fci[i].pointer_count = 1;
2917 i = find_char_info_specifier_index (cdiag_fci, 'K');
2918 cdiag_fci[i].types[0].type = &t;
2919 cdiag_fci[i].pointer_count = 1;
2922 /* Handle the __gcc_cxxdiag__ format specifics. */
2924 dynamic_format_types[gcc_cxxdiag_format_type].conversion_specs =
2925 cxxdiag_fci = (format_char_info *)
2926 xmemdup (gcc_cxxdiag_char_table,
2927 sizeof (gcc_cxxdiag_char_table),
2928 sizeof (gcc_cxxdiag_char_table));
2931 /* All specifiers taking a tree share the same struct. */
2932 i = find_char_info_specifier_index (cxxdiag_fci, 'D');
2933 cxxdiag_fci[i].types[0].type = &t;
2934 cxxdiag_fci[i].pointer_count = 1;
2935 i = find_char_info_specifier_index (cxxdiag_fci, 'K');
2936 cxxdiag_fci[i].types[0].type = &t;
2937 cxxdiag_fci[i].pointer_count = 1;
2942 #ifdef TARGET_FORMAT_TYPES
2943 extern const format_kind_info TARGET_FORMAT_TYPES[];
2946 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
2947 extern const target_ovr_attr TARGET_OVERRIDES_FORMAT_ATTRIBUTES[];
2949 #ifdef TARGET_OVERRIDES_FORMAT_INIT
2950 extern void TARGET_OVERRIDES_FORMAT_INIT (void);
2953 /* Attributes such as "printf" are equivalent to those such as
2954 "gnu_printf" unless this is overridden by a target. */
2955 static const target_ovr_attr gnu_target_overrides_format_attributes[] =
2957 { "gnu_printf", "printf" },
2958 { "gnu_scanf", "scanf" },
2959 { "gnu_strftime", "strftime" },
2960 { "gnu_strfmon", "strfmon" },
2964 /* Translate to unified attribute name. This is used in decode_format_type and
2965 decode_format_attr. In attr_name the user specified argument is passed. It
2966 returns the unified format name from TARGET_OVERRIDES_FORMAT_ATTRIBUTES
2967 or the attr_name passed to this function, if there is no matching entry. */
2969 convert_format_name_to_system_name (const char *attr_name)
2973 if (attr_name == NULL || *attr_name == 0
2974 || strncmp (attr_name, "gcc_", 4) == 0)
2976 #ifdef TARGET_OVERRIDES_FORMAT_INIT
2977 TARGET_OVERRIDES_FORMAT_INIT ();
2980 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
2981 /* Check if format attribute is overridden by target. */
2982 if (TARGET_OVERRIDES_FORMAT_ATTRIBUTES != NULL
2983 && TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT > 0)
2985 for (i = 0; i < TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT; ++i)
2987 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_src,
2990 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_dst,
2992 return TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_src;
2996 /* Otherwise default to gnu format. */
2998 gnu_target_overrides_format_attributes[i].named_attr_src != NULL;
3001 if (cmp_attribs (gnu_target_overrides_format_attributes[i].named_attr_src,
3004 if (cmp_attribs (gnu_target_overrides_format_attributes[i].named_attr_dst,
3006 return gnu_target_overrides_format_attributes[i].named_attr_src;
3012 /* Return true if TATTR_NAME and ATTR_NAME are the same format attribute,
3013 counting "name" and "__name__" as the same, false otherwise. */
3015 cmp_attribs (const char *tattr_name, const char *attr_name)
3017 int alen = strlen (attr_name);
3018 int slen = (tattr_name ? strlen (tattr_name) : 0);
3019 if (alen > 4 && attr_name[0] == '_' && attr_name[1] == '_'
3020 && attr_name[alen - 1] == '_' && attr_name[alen - 2] == '_')
3025 if (alen != slen || strncmp (tattr_name, attr_name, alen) != 0)
3030 /* Handle a "format" attribute; arguments as in
3031 struct attribute_spec.handler. */
3033 handle_format_attribute (tree *node, tree ARG_UNUSED (name), tree args,
3034 int flags, bool *no_add_attrs)
3037 function_format_info info;
3039 #ifdef TARGET_FORMAT_TYPES
3040 /* If the target provides additional format types, we need to
3041 add them to FORMAT_TYPES at first use. */
3042 if (TARGET_FORMAT_TYPES != NULL && !dynamic_format_types)
3044 dynamic_format_types = XNEWVEC (format_kind_info,
3045 n_format_types + TARGET_N_FORMAT_TYPES);
3046 memcpy (dynamic_format_types, format_types_orig,
3047 sizeof (format_types_orig));
3048 memcpy (&dynamic_format_types[n_format_types], TARGET_FORMAT_TYPES,
3049 TARGET_N_FORMAT_TYPES * sizeof (dynamic_format_types[0]));
3051 format_types = dynamic_format_types;
3052 /* Provide a reference for the first potential external type. */
3053 first_target_format_type = n_format_types;
3054 n_format_types += TARGET_N_FORMAT_TYPES;
3058 if (!decode_format_attr (args, &info, 0))
3060 *no_add_attrs = true;
3064 if (prototype_p (type))
3066 if (!check_format_string (type, info.format_num, flags,
3067 no_add_attrs, info.format_type))
3070 if (info.first_arg_num != 0)
3072 unsigned HOST_WIDE_INT arg_num = 1;
3073 function_args_iterator iter;
3076 /* Verify that first_arg_num points to the last arg,
3078 FOREACH_FUNCTION_ARGS (type, arg_type, iter)
3081 if (arg_num != info.first_arg_num)
3083 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
3084 error ("args to be formatted is not %<...%>");
3085 *no_add_attrs = true;
3091 /* Check if this is a strftime variant. Just for this variant
3092 FMT_FLAG_ARG_CONVERT is not set. */
3093 if ((format_types[info.format_type].flags & (int) FMT_FLAG_ARG_CONVERT) == 0
3094 && info.first_arg_num != 0)
3096 error ("strftime formats cannot format arguments");
3097 *no_add_attrs = true;
3101 /* If this is a custom GCC-internal format type, we have to
3102 initialize certain bits at runtime. */
3103 if (info.format_type == asm_fprintf_format_type
3104 || info.format_type == gcc_gfc_format_type
3105 || info.format_type == gcc_diag_format_type
3106 || info.format_type == gcc_tdiag_format_type
3107 || info.format_type == gcc_cdiag_format_type
3108 || info.format_type == gcc_cxxdiag_format_type)
3110 /* Our first time through, we have to make sure that our
3111 format_type data is allocated dynamically and is modifiable. */
3112 if (!dynamic_format_types)
3113 format_types = dynamic_format_types = (format_kind_info *)
3114 xmemdup (format_types_orig, sizeof (format_types_orig),
3115 sizeof (format_types_orig));
3117 /* If this is format __asm_fprintf__, we have to initialize
3118 GCC's notion of HOST_WIDE_INT for checking %wd. */
3119 if (info.format_type == asm_fprintf_format_type)
3120 init_dynamic_asm_fprintf_info ();
3121 /* If this is format __gcc_gfc__, we have to initialize GCC's
3122 notion of 'locus' at runtime for %L. */
3123 else if (info.format_type == gcc_gfc_format_type)
3124 init_dynamic_gfc_info ();
3125 /* If this is one of the diagnostic attributes, then we have to
3126 initialize 'location_t' and 'tree' at runtime. */
3127 else if (info.format_type == gcc_diag_format_type
3128 || info.format_type == gcc_tdiag_format_type
3129 || info.format_type == gcc_cdiag_format_type
3130 || info.format_type == gcc_cxxdiag_format_type)
3131 init_dynamic_diag_info ();