1 /* Check calls to formatted I/O functions (-Wformat).
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
31 #include "diagnostic-core.h"
32 #include "langhooks.h"
34 #include "alloc-pool.h"
37 /* Set format warning options according to a -Wformat=n option. */
40 set_Wformat (int setting)
42 warn_format = setting;
43 warn_format_extra_args = setting;
44 warn_format_zero_length = setting;
45 warn_format_contains_nul = setting;
48 warn_format_nonliteral = setting;
49 warn_format_security = setting;
50 warn_format_y2k = setting;
52 /* Make sure not to disable -Wnonnull if -Wformat=0 is specified. */
54 warn_nonnull = setting;
58 /* Handle attributes associated with format checking. */
60 /* This must be in the same order as format_types, except for
61 format_type_error. Target-specific format types do not have
62 matching enum values. */
63 enum format_type { printf_format_type, asm_fprintf_format_type,
64 gcc_diag_format_type, gcc_tdiag_format_type,
65 gcc_cdiag_format_type,
66 gcc_cxxdiag_format_type, gcc_gfc_format_type,
67 gcc_objc_string_format_type,
68 format_type_error = -1};
70 typedef struct function_format_info
72 int format_type; /* type of format (printf, scanf, etc.) */
73 unsigned HOST_WIDE_INT format_num; /* number of format argument */
74 unsigned HOST_WIDE_INT first_arg_num; /* number of first arg (zero for varargs) */
75 } function_format_info;
77 static bool decode_format_attr (tree, function_format_info *, int);
78 static int decode_format_type (const char *);
80 static bool check_format_string (tree argument,
81 unsigned HOST_WIDE_INT format_num,
82 int flags, bool *no_add_attrs,
83 int expected_format_type);
84 static bool get_constant (tree expr, unsigned HOST_WIDE_INT *value,
86 static const char *convert_format_name_to_system_name (const char *attr_name);
87 static bool cmp_attribs (const char *tattr_name, const char *attr_name);
89 static int first_target_format_type;
90 static const char *format_name (int format_num);
91 static int format_flags (int format_num);
93 /* Check that we have a pointer to a string suitable for use as a format.
94 The default is to check for a char type.
95 For objective-c dialects, this is extended to include references to string
96 objects validated by objc_string_ref_type_p ().
97 Targets may also provide a string object type that can be used within c and
98 c++ and shared with their respective objective-c dialects. In this case the
99 reference to a format string is checked for validity via a hook.
101 The function returns true if strref points to any string type valid for the
102 language dialect and target. */
105 valid_stringptr_type_p (tree strref)
107 return (strref != NULL
108 && TREE_CODE (strref) == POINTER_TYPE
109 && (TYPE_MAIN_VARIANT (TREE_TYPE (strref)) == char_type_node
110 || objc_string_ref_type_p (strref)
111 || (*targetcm.string_object_ref_type_p) ((const_tree) strref)));
114 /* Handle a "format_arg" attribute; arguments as in
115 struct attribute_spec.handler. */
117 handle_format_arg_attribute (tree *node, tree ARG_UNUSED (name),
118 tree args, int flags, bool *no_add_attrs)
121 tree format_num_expr = TREE_VALUE (args);
122 unsigned HOST_WIDE_INT format_num = 0;
125 if (!get_constant (format_num_expr, &format_num, 0))
127 error ("format string has invalid operand number");
128 *no_add_attrs = true;
132 argument = TYPE_ARG_TYPES (type);
135 /* The format arg can be any string reference valid for the language and
136 target. We cannot be more specific in this case. */
137 if (!check_format_string (argument, format_num, flags, no_add_attrs, -1))
141 if (!valid_stringptr_type_p (TREE_TYPE (type)))
143 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
144 error ("function does not return string type");
145 *no_add_attrs = true;
152 /* Verify that the format_num argument is actually a string reference suitable,
153 for the language dialect and target (in case the format attribute is in
154 error). When we know the specific reference type expected, this is also
157 check_format_string (tree argument, unsigned HOST_WIDE_INT format_num,
158 int flags, bool *no_add_attrs, int expected_format_type)
160 unsigned HOST_WIDE_INT i;
161 bool is_objc_sref, is_target_sref, is_char_ref;
165 for (i = 1; i != format_num; i++)
169 argument = TREE_CHAIN (argument);
173 || !(ref = TREE_VALUE (argument))
174 || !valid_stringptr_type_p (ref))
176 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
177 error ("format string argument is not a string type");
178 *no_add_attrs = true;
182 /* We only know that we want a suitable string reference. */
183 if (expected_format_type < 0)
186 /* Now check that the arg matches the expected type. */
188 (TYPE_MAIN_VARIANT (TREE_TYPE (ref)) == char_type_node);
190 fmt_flags = format_flags (expected_format_type);
191 is_objc_sref = is_target_sref = false;
193 is_objc_sref = objc_string_ref_type_p (ref);
195 if (!(fmt_flags & FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL))
198 return true; /* OK, we expected a char and found one. */
201 /* We expected a char but found an extended string type. */
203 error ("found a %<%s%> reference but the format argument should"
204 " be a string", format_name (gcc_objc_string_format_type));
206 error ("found a %qT but the format argument should be a string",
208 *no_add_attrs = true;
213 /* We expect a string object type as the format arg. */
216 error ("format argument should be a %<%s%> reference but"
217 " a string was found", format_name (expected_format_type));
218 *no_add_attrs = true;
222 /* We will assert that objective-c will support either its own string type
223 or the target-supplied variant. */
225 is_target_sref = (*targetcm.string_object_ref_type_p) ((const_tree) ref);
227 if (expected_format_type == (int) gcc_objc_string_format_type
228 && (is_objc_sref || is_target_sref))
231 /* We will allow a target string ref to match only itself. */
232 if (first_target_format_type
233 && expected_format_type >= first_target_format_type
238 error ("format argument should be a %<%s%> reference",
239 format_name (expected_format_type));
240 *no_add_attrs = true;
247 /* Verify EXPR is a constant, and store its value.
248 If validated_p is true there should be no errors.
249 Returns true on success, false otherwise. */
251 get_constant (tree expr, unsigned HOST_WIDE_INT *value, int validated_p)
253 if (TREE_CODE (expr) != INTEGER_CST || TREE_INT_CST_HIGH (expr) != 0)
255 gcc_assert (!validated_p);
259 *value = TREE_INT_CST_LOW (expr);
264 /* Decode the arguments to a "format" attribute into a
265 function_format_info structure. It is already known that the list
266 is of the right length. If VALIDATED_P is true, then these
267 attributes have already been validated and must not be erroneous;
268 if false, it will give an error message. Returns true if the
269 attributes are successfully decoded, false otherwise. */
272 decode_format_attr (tree args, function_format_info *info, int validated_p)
274 tree format_type_id = TREE_VALUE (args);
275 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
276 tree first_arg_num_expr
277 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
279 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
281 gcc_assert (!validated_p);
282 error ("unrecognized format specifier");
287 const char *p = IDENTIFIER_POINTER (format_type_id);
289 p = convert_format_name_to_system_name (p);
291 info->format_type = decode_format_type (p);
293 if (!c_dialect_objc ()
294 && info->format_type == gcc_objc_string_format_type)
296 gcc_assert (!validated_p);
297 warning (OPT_Wformat, "%qE is only allowed in Objective-C dialects",
299 info->format_type = format_type_error;
303 if (info->format_type == format_type_error)
305 gcc_assert (!validated_p);
306 warning (OPT_Wformat, "%qE is an unrecognized format function type",
312 if (!get_constant (format_num_expr, &info->format_num, validated_p))
314 error ("format string has invalid operand number");
318 if (!get_constant (first_arg_num_expr, &info->first_arg_num, validated_p))
320 error ("%<...%> has invalid operand number");
324 if (info->first_arg_num != 0 && info->first_arg_num <= info->format_num)
326 gcc_assert (!validated_p);
327 error ("format string argument follows the args to be formatted");
334 /* Check a call to a format function against a parameter list. */
336 /* The C standard version C++ is treated as equivalent to
337 or inheriting from, for the purpose of format features supported. */
338 #define CPLUSPLUS_STD_VER STD_C94
339 /* The C standard version we are checking formats against when pedantic. */
340 #define C_STD_VER ((int) (c_dialect_cxx () \
341 ? CPLUSPLUS_STD_VER \
344 : (flag_isoc94 ? STD_C94 : STD_C89))))
345 /* The name to give to the standard version we are warning about when
346 pedantic. FEATURE_VER is the version in which the feature warned out
347 appeared, which is higher than C_STD_VER. */
348 #define C_STD_NAME(FEATURE_VER) (c_dialect_cxx () \
350 : ((FEATURE_VER) == STD_EXT \
353 /* Adjust a C standard version, which may be STD_C9L, to account for
354 -Wno-long-long. Returns other standard versions unchanged. */
355 #define ADJ_STD(VER) ((int) ((VER) == STD_C9L \
356 ? (warn_long_long ? STD_C99 : STD_C89) \
359 /* Enum describing the kind of specifiers present in the format and
360 requiring an argument. */
361 enum format_specifier_kind {
364 CF_KIND_FIELD_PRECISION
367 static const char *kind_descriptions[] = {
369 N_("field width specifier"),
370 N_("field precision specifier")
373 /* Structure describing details of a type expected in format checking,
374 and the type to check against it. */
375 typedef struct format_wanted_type
377 /* The type wanted. */
379 /* The name of this type to use in diagnostics. */
380 const char *wanted_type_name;
381 /* Should be type checked just for scalar width identity. */
382 int scalar_identity_flag;
383 /* The level of indirection through pointers at which this type occurs. */
385 /* Whether, when pointer_count is 1, to allow any character type when
386 pedantic, rather than just the character or void type specified. */
387 int char_lenient_flag;
388 /* Whether the argument, dereferenced once, is written into and so the
389 argument must not be a pointer to a const-qualified type. */
391 /* Whether the argument, dereferenced once, is read from and so
392 must not be a NULL pointer. */
393 int reading_from_flag;
394 /* The kind of specifier that this type is used for. */
395 enum format_specifier_kind kind;
396 /* The starting character of the specifier. This never includes the
397 initial percent sign. */
398 const char *format_start;
399 /* The length of the specifier. */
401 /* The actual parameter to check against the wanted type. */
403 /* The argument number of that parameter. */
405 /* The next type to check for this format conversion, or NULL if none. */
406 struct format_wanted_type *next;
407 } format_wanted_type;
409 /* Convenience macro for format_length_info meaning unused. */
410 #define NO_FMT NULL, FMT_LEN_none, STD_C89
412 static const format_length_info printf_length_specs[] =
414 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99, 0 },
415 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L, 0 },
416 { "q", FMT_LEN_ll, STD_EXT, NO_FMT, 0 },
417 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
418 { "z", FMT_LEN_z, STD_C99, NO_FMT, 0 },
419 { "Z", FMT_LEN_z, STD_EXT, NO_FMT, 0 },
420 { "t", FMT_LEN_t, STD_C99, NO_FMT, 0 },
421 { "j", FMT_LEN_j, STD_C99, NO_FMT, 0 },
422 { "H", FMT_LEN_H, STD_EXT, NO_FMT, 0 },
423 { "D", FMT_LEN_D, STD_EXT, "DD", FMT_LEN_DD, STD_EXT, 0 },
424 { NO_FMT, NO_FMT, 0 }
427 /* Length specifiers valid for asm_fprintf. */
428 static const format_length_info asm_fprintf_length_specs[] =
430 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89, 0 },
431 { "w", FMT_LEN_none, STD_C89, NO_FMT, 0 },
432 { NO_FMT, NO_FMT, 0 }
435 /* Length specifiers valid for GCC diagnostics. */
436 static const format_length_info gcc_diag_length_specs[] =
438 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89, 0 },
439 { "w", FMT_LEN_none, STD_C89, NO_FMT, 0 },
440 { NO_FMT, NO_FMT, 0 }
443 /* The custom diagnostics all accept the same length specifiers. */
444 #define gcc_tdiag_length_specs gcc_diag_length_specs
445 #define gcc_cdiag_length_specs gcc_diag_length_specs
446 #define gcc_cxxdiag_length_specs gcc_diag_length_specs
448 /* This differs from printf_length_specs only in that "Z" is not accepted. */
449 static const format_length_info scanf_length_specs[] =
451 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99, 0 },
452 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L, 0 },
453 { "q", FMT_LEN_ll, STD_EXT, NO_FMT, 0 },
454 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
455 { "z", FMT_LEN_z, STD_C99, NO_FMT, 0 },
456 { "t", FMT_LEN_t, STD_C99, NO_FMT, 0 },
457 { "j", FMT_LEN_j, STD_C99, NO_FMT, 0 },
458 { "H", FMT_LEN_H, STD_EXT, NO_FMT, 0 },
459 { "D", FMT_LEN_D, STD_EXT, "DD", FMT_LEN_DD, STD_EXT, 0 },
460 { NO_FMT, NO_FMT, 0 }
464 /* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings
465 make no sense for a format type not part of any C standard version. */
466 static const format_length_info strfmon_length_specs[] =
468 /* A GNU extension. */
469 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
470 { NO_FMT, NO_FMT, 0 }
474 /* For now, the Fortran front-end routines only use l as length modifier. */
475 static const format_length_info gcc_gfc_length_specs[] =
477 { "l", FMT_LEN_l, STD_C89, NO_FMT, 0 },
478 { NO_FMT, NO_FMT, 0 }
482 static const format_flag_spec printf_flag_specs[] =
484 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
485 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
486 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
487 { '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
488 { '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
489 { '\'', 0, 0, N_("''' flag"), N_("the ''' printf flag"), STD_EXT },
490 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' printf flag"), STD_EXT },
491 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
492 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
493 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
494 { 0, 0, 0, NULL, NULL, STD_C89 }
498 static const format_flag_pair printf_flag_pairs[] =
502 { '0', 'p', 1, 'i' },
506 static const format_flag_spec asm_fprintf_flag_specs[] =
508 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
509 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
510 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
511 { '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
512 { '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
513 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
514 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
515 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
516 { 0, 0, 0, NULL, NULL, STD_C89 }
519 static const format_flag_pair asm_fprintf_flag_pairs[] =
523 { '0', 'p', 1, 'i' },
527 static const format_flag_pair gcc_diag_flag_pairs[] =
532 #define gcc_tdiag_flag_pairs gcc_diag_flag_pairs
533 #define gcc_cdiag_flag_pairs gcc_diag_flag_pairs
534 #define gcc_cxxdiag_flag_pairs gcc_diag_flag_pairs
536 static const format_flag_pair gcc_gfc_flag_pairs[] =
541 static const format_flag_spec gcc_diag_flag_specs[] =
543 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
544 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
545 { 'q', 0, 0, N_("'q' flag"), N_("the 'q' diagnostic flag"), STD_C89 },
546 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
547 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
548 { 0, 0, 0, NULL, NULL, STD_C89 }
551 #define gcc_tdiag_flag_specs gcc_diag_flag_specs
552 #define gcc_cdiag_flag_specs gcc_diag_flag_specs
553 #define gcc_cxxdiag_flag_specs gcc_diag_flag_specs
555 static const format_flag_spec scanf_flag_specs[] =
557 { '*', 0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89 },
558 { 'a', 0, 0, N_("'a' flag"), N_("the 'a' scanf flag"), STD_EXT },
559 { 'm', 0, 0, N_("'m' flag"), N_("the 'm' scanf flag"), STD_EXT },
560 { 'w', 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89 },
561 { 'L', 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89 },
562 { '\'', 0, 0, N_("''' flag"), N_("the ''' scanf flag"), STD_EXT },
563 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' scanf flag"), STD_EXT },
564 { 0, 0, 0, NULL, NULL, STD_C89 }
568 static const format_flag_pair scanf_flag_pairs[] =
576 static const format_flag_spec strftime_flag_specs[] =
578 { '_', 0, 0, N_("'_' flag"), N_("the '_' strftime flag"), STD_EXT },
579 { '-', 0, 0, N_("'-' flag"), N_("the '-' strftime flag"), STD_EXT },
580 { '0', 0, 0, N_("'0' flag"), N_("the '0' strftime flag"), STD_EXT },
581 { '^', 0, 0, N_("'^' flag"), N_("the '^' strftime flag"), STD_EXT },
582 { '#', 0, 0, N_("'#' flag"), N_("the '#' strftime flag"), STD_EXT },
583 { 'w', 0, 0, N_("field width"), N_("field width in strftime format"), STD_EXT },
584 { 'E', 0, 0, N_("'E' modifier"), N_("the 'E' strftime modifier"), STD_C99 },
585 { 'O', 0, 0, N_("'O' modifier"), N_("the 'O' strftime modifier"), STD_C99 },
586 { 'O', 'o', 0, NULL, N_("the 'O' modifier"), STD_EXT },
587 { 0, 0, 0, NULL, NULL, STD_C89 }
591 static const format_flag_pair strftime_flag_pairs[] =
602 static const format_flag_spec strfmon_flag_specs[] =
604 { '=', 0, 1, N_("fill character"), N_("fill character in strfmon format"), STD_C89 },
605 { '^', 0, 0, N_("'^' flag"), N_("the '^' strfmon flag"), STD_C89 },
606 { '+', 0, 0, N_("'+' flag"), N_("the '+' strfmon flag"), STD_C89 },
607 { '(', 0, 0, N_("'(' flag"), N_("the '(' strfmon flag"), STD_C89 },
608 { '!', 0, 0, N_("'!' flag"), N_("the '!' strfmon flag"), STD_C89 },
609 { '-', 0, 0, N_("'-' flag"), N_("the '-' strfmon flag"), STD_C89 },
610 { 'w', 0, 0, N_("field width"), N_("field width in strfmon format"), STD_C89 },
611 { '#', 0, 0, N_("left precision"), N_("left precision in strfmon format"), STD_C89 },
612 { 'p', 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89 },
613 { 'L', 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89 },
614 { 0, 0, 0, NULL, NULL, STD_C89 }
617 static const format_flag_pair strfmon_flag_pairs[] =
624 static const format_char_info print_char_table[] =
626 /* C89 conversion specifiers. */
627 { "di", 0, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, TEX_LL, T99_SST, T99_PD, T99_IM, BADLEN, BADLEN, BADLEN }, "-wp0 +'I", "i", NULL },
628 { "oxX", 0, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM, BADLEN, BADLEN, BADLEN }, "-wp0#", "i", NULL },
629 { "u", 0, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM, BADLEN, BADLEN, BADLEN }, "-wp0'I", "i", NULL },
630 { "fgG", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, TEX_D32, TEX_D64, TEX_D128 }, "-wp0 +#'I", "", NULL },
631 { "eE", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, TEX_D32, TEX_D64, TEX_D128 }, "-wp0 +#I", "", NULL },
632 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, T94_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
633 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
634 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "c", NULL },
635 { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, BADLEN, T99_SST, T99_PD, T99_IM, BADLEN, BADLEN, BADLEN }, "", "W", NULL },
636 /* C99 conversion specifiers. */
637 { "F", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, TEX_D32, TEX_D64, TEX_D128 }, "-wp0 +#'I", "", NULL },
638 { "aA", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "", NULL },
639 /* X/Open conversion specifiers. */
640 { "C", 0, STD_EXT, { TEX_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
641 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "R", NULL },
642 /* GNU conversion specifiers. */
643 { "m", 0, STD_EXT, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "", NULL },
644 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
647 static const format_char_info asm_fprintf_char_table[] =
649 /* C89 conversion specifiers. */
650 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +", "i", NULL },
651 { "oxX", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0#", "i", NULL },
652 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0", "i", NULL },
653 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
654 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
656 /* asm_fprintf conversion specifiers. */
657 { "O", 0, STD_C89, NOARGUMENTS, "", "", NULL },
658 { "R", 0, STD_C89, NOARGUMENTS, "", "", NULL },
659 { "I", 0, STD_C89, NOARGUMENTS, "", "", NULL },
660 { "L", 0, STD_C89, NOARGUMENTS, "", "", NULL },
661 { "U", 0, STD_C89, NOARGUMENTS, "", "", NULL },
662 { "r", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
663 { "@", 0, STD_C89, NOARGUMENTS, "", "", NULL },
664 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
667 static const format_char_info gcc_diag_char_table[] =
669 /* C89 conversion specifiers. */
670 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
671 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
672 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
673 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
674 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
675 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
677 /* Custom conversion specifiers. */
679 /* These will require a "tree" at runtime. */
680 { "K", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
682 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
683 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
684 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
687 static const format_char_info gcc_tdiag_char_table[] =
689 /* C89 conversion specifiers. */
690 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
691 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
692 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
693 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
694 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
695 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
697 /* Custom conversion specifiers. */
699 /* These will require a "tree" at runtime. */
700 { "DFKTEV", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
702 { "v", 0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
704 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
705 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
706 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
709 static const format_char_info gcc_cdiag_char_table[] =
711 /* C89 conversion specifiers. */
712 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
713 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
714 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
715 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
716 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
717 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
719 /* Custom conversion specifiers. */
721 /* These will require a "tree" at runtime. */
722 { "DEFKTV", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
724 { "v", 0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
726 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
727 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
728 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
731 static const format_char_info gcc_cxxdiag_char_table[] =
733 /* C89 conversion specifiers. */
734 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
735 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
736 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
737 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
738 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
739 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
741 /* Custom conversion specifiers. */
743 /* These will require a "tree" at runtime. */
744 { "ADEFKTV",0,STD_C89,{ T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+#", "", NULL },
746 { "v", 0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
748 /* These accept either an 'int' or an 'enum tree_code' (which is handled as an 'int'.) */
749 { "CLOPQ",0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
751 { "<>'", 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 }, "", "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 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
774 static const format_char_info scan_char_table[] =
776 /* C89 conversion specifiers. */
777 { "di", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, TEX_LL, T99_SST, T99_PD, T99_IM, BADLEN, BADLEN, BADLEN }, "*w'I", "W", NULL },
778 { "u", 1, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM, BADLEN, BADLEN, BADLEN }, "*w'I", "W", NULL },
779 { "oxX", 1, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
780 { "efgEG", 1, STD_C89, { T89_F, BADLEN, BADLEN, T89_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, TEX_D32, TEX_D64, TEX_D128 }, "*w'", "W", NULL },
781 { "c", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*mw", "cW", NULL },
782 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "cW", NULL },
783 { "[", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "cW[", NULL },
784 { "p", 2, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
785 { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, BADLEN, T99_SST, T99_PD, T99_IM, BADLEN, BADLEN, BADLEN }, "", "W", NULL },
786 /* C99 conversion specifiers. */
787 { "F", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, TEX_D32, TEX_D64, TEX_D128 }, "*w'", "W", NULL },
788 { "aA", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w'", "W", NULL },
789 /* X/Open conversion specifiers. */
790 { "C", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*mw", "W", NULL },
791 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "W", NULL },
792 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
795 static const format_char_info time_char_table[] =
797 /* C89 conversion specifiers. */
798 { "ABZab", 0, STD_C89, NOLENGTHS, "^#", "", NULL },
799 { "cx", 0, STD_C89, NOLENGTHS, "E", "3", NULL },
800 { "HIMSUWdmw", 0, STD_C89, NOLENGTHS, "-_0Ow", "", NULL },
801 { "j", 0, STD_C89, NOLENGTHS, "-_0Ow", "o", NULL },
802 { "p", 0, STD_C89, NOLENGTHS, "#", "", NULL },
803 { "X", 0, STD_C89, NOLENGTHS, "E", "", NULL },
804 { "y", 0, STD_C89, NOLENGTHS, "EO-_0w", "4", NULL },
805 { "Y", 0, STD_C89, NOLENGTHS, "-_0EOw", "o", NULL },
806 { "%", 0, STD_C89, NOLENGTHS, "", "", NULL },
807 /* C99 conversion specifiers. */
808 { "C", 0, STD_C99, NOLENGTHS, "-_0EOw", "o", NULL },
809 { "D", 0, STD_C99, NOLENGTHS, "", "2", NULL },
810 { "eVu", 0, STD_C99, NOLENGTHS, "-_0Ow", "", NULL },
811 { "FRTnrt", 0, STD_C99, NOLENGTHS, "", "", NULL },
812 { "g", 0, STD_C99, NOLENGTHS, "O-_0w", "2o", NULL },
813 { "G", 0, STD_C99, NOLENGTHS, "-_0Ow", "o", NULL },
814 { "h", 0, STD_C99, NOLENGTHS, "^#", "", NULL },
815 { "z", 0, STD_C99, NOLENGTHS, "O", "o", NULL },
816 /* GNU conversion specifiers. */
817 { "kls", 0, STD_EXT, NOLENGTHS, "-_0Ow", "", NULL },
818 { "P", 0, STD_EXT, NOLENGTHS, "", "", NULL },
819 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
822 static const format_char_info monetary_char_table[] =
824 { "in", 0, STD_C89, { T89_D, BADLEN, BADLEN, BADLEN, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "=^+(!-w#p", "", NULL },
825 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
828 /* This must be in the same order as enum format_type. */
829 static const format_kind_info format_types_orig[] =
831 { "gnu_printf", printf_length_specs, print_char_table, " +#0-'I", NULL,
832 printf_flag_specs, printf_flag_pairs,
833 FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_EMPTY_PREC_OK,
834 'w', 0, 'p', 0, 'L', 0,
835 &integer_type_node, &integer_type_node
837 { "asm_fprintf", asm_fprintf_length_specs, asm_fprintf_char_table, " +#0-", NULL,
838 asm_fprintf_flag_specs, asm_fprintf_flag_pairs,
839 FMT_FLAG_ARG_CONVERT|FMT_FLAG_EMPTY_PREC_OK,
840 'w', 0, 'p', 0, 'L', 0,
843 { "gcc_diag", gcc_diag_length_specs, gcc_diag_char_table, "q+#", NULL,
844 gcc_diag_flag_specs, gcc_diag_flag_pairs,
845 FMT_FLAG_ARG_CONVERT,
846 0, 0, 'p', 0, 'L', 0,
847 NULL, &integer_type_node
849 { "gcc_tdiag", gcc_tdiag_length_specs, gcc_tdiag_char_table, "q+#", NULL,
850 gcc_tdiag_flag_specs, gcc_tdiag_flag_pairs,
851 FMT_FLAG_ARG_CONVERT,
852 0, 0, 'p', 0, 'L', 0,
853 NULL, &integer_type_node
855 { "gcc_cdiag", gcc_cdiag_length_specs, gcc_cdiag_char_table, "q+#", NULL,
856 gcc_cdiag_flag_specs, gcc_cdiag_flag_pairs,
857 FMT_FLAG_ARG_CONVERT,
858 0, 0, 'p', 0, 'L', 0,
859 NULL, &integer_type_node
861 { "gcc_cxxdiag", gcc_cxxdiag_length_specs, gcc_cxxdiag_char_table, "q+#", NULL,
862 gcc_cxxdiag_flag_specs, gcc_cxxdiag_flag_pairs,
863 FMT_FLAG_ARG_CONVERT,
864 0, 0, 'p', 0, 'L', 0,
865 NULL, &integer_type_node
867 { "gcc_gfc", gcc_gfc_length_specs, gcc_gfc_char_table, "", NULL,
868 NULL, gcc_gfc_flag_pairs,
869 FMT_FLAG_ARG_CONVERT,
873 { "NSString", NULL, NULL, NULL, NULL,
875 FMT_FLAG_ARG_CONVERT|FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL, 0, 0, 0, 0, 0, 0,
878 { "gnu_scanf", scanf_length_specs, scan_char_table, "*'I", NULL,
879 scanf_flag_specs, scanf_flag_pairs,
880 FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_ZERO_WIDTH_BAD|FMT_FLAG_DOLLAR_GAP_POINTER_OK,
881 'w', 0, 0, '*', 'L', 'm',
884 { "gnu_strftime", NULL, time_char_table, "_-0^#", "EO",
885 strftime_flag_specs, strftime_flag_pairs,
886 FMT_FLAG_FANCY_PERCENT_OK, 'w', 0, 0, 0, 0, 0,
889 { "gnu_strfmon", strfmon_length_specs, monetary_char_table, "=^+(!-", NULL,
890 strfmon_flag_specs, strfmon_flag_pairs,
891 FMT_FLAG_ARG_CONVERT, 'w', '#', 'p', 0, 'L', 0,
896 /* This layer of indirection allows GCC to reassign format_types with
897 new data if necessary, while still allowing the original data to be
899 static const format_kind_info *format_types = format_types_orig;
900 /* We can modify this one. We also add target-specific format types
901 to the end of the array. */
902 static format_kind_info *dynamic_format_types;
904 static int n_format_types = ARRAY_SIZE (format_types_orig);
906 /* Structure detailing the results of checking a format function call
907 where the format expression may be a conditional expression with
908 many leaves resulting from nested conditional expressions. */
911 /* Number of leaves of the format argument that could not be checked
912 as they were not string literals. */
913 int number_non_literal;
914 /* Number of leaves of the format argument that were null pointers or
915 string literals, but had extra format arguments. */
916 int number_extra_args;
917 /* Number of leaves of the format argument that were null pointers or
918 string literals, but had extra format arguments and used $ operand
920 int number_dollar_extra_args;
921 /* Number of leaves of the format argument that were wide string
924 /* Number of leaves of the format argument that were empty strings. */
926 /* Number of leaves of the format argument that were unterminated
928 int number_unterminated;
929 /* Number of leaves of the format argument that were not counted above. */
931 } format_check_results;
935 format_check_results *res;
936 function_format_info *info;
938 } format_check_context;
940 /* Return the format name (as specified in the original table) for the format
941 type indicated by format_num. */
943 format_name (int format_num)
945 if (format_num >= 0 && format_num < n_format_types)
946 return format_types[format_num].name;
950 /* Return the format flags (as specified in the original table) for the format
951 type indicated by format_num. */
953 format_flags (int format_num)
955 if (format_num >= 0 && format_num < n_format_types)
956 return format_types[format_num].flags;
960 static void check_format_info (function_format_info *, tree);
961 static void check_format_arg (void *, tree, unsigned HOST_WIDE_INT);
962 static void check_format_info_main (format_check_results *,
963 function_format_info *,
964 const char *, int, tree,
965 unsigned HOST_WIDE_INT, alloc_pool);
967 static void init_dollar_format_checking (int, tree);
968 static int maybe_read_dollar_number (const char **, int,
969 tree, tree *, const format_kind_info *);
970 static bool avoid_dollar_number (const char *);
971 static void finish_dollar_format_checking (format_check_results *, int);
973 static const format_flag_spec *get_flag_spec (const format_flag_spec *,
976 static void check_format_types (format_wanted_type *);
977 static void format_type_warning (format_wanted_type *, tree, tree);
979 /* Decode a format type from a string, returning the type, or
980 format_type_error if not valid, in which case the caller should print an
983 decode_format_type (const char *s)
988 s = convert_format_name_to_system_name (s);
990 for (i = 0; i < n_format_types; i++)
993 if (!strcmp (s, format_types[i].name))
995 alen = strlen (format_types[i].name);
996 if (slen == alen + 4 && s[0] == '_' && s[1] == '_'
997 && s[slen - 1] == '_' && s[slen - 2] == '_'
998 && !strncmp (s + 2, format_types[i].name, alen))
1001 return format_type_error;
1005 /* Check the argument list of a call to printf, scanf, etc.
1006 ATTRS are the attributes on the function type. There are NARGS argument
1007 values in the array ARGARRAY.
1008 Also, if -Wmissing-format-attribute,
1009 warn for calls to vprintf or vscanf in functions with no such format
1010 attribute themselves. */
1013 check_function_format (tree attrs, int nargs, tree *argarray)
1017 /* See if this function has any format attributes. */
1018 for (a = attrs; a; a = TREE_CHAIN (a))
1020 if (is_attribute_p ("format", TREE_PURPOSE (a)))
1022 /* Yup; check it. */
1023 function_format_info info;
1024 decode_format_attr (TREE_VALUE (a), &info, 1);
1027 /* FIXME: Rewrite all the internal functions in this file
1028 to use the ARGARRAY directly instead of constructing this
1030 tree params = NULL_TREE;
1032 for (i = nargs - 1; i >= 0; i--)
1033 params = tree_cons (NULL_TREE, argarray[i], params);
1034 check_format_info (&info, params);
1036 if (warn_missing_format_attribute && info.first_arg_num == 0
1037 && (format_types[info.format_type].flags
1038 & (int) FMT_FLAG_ARG_CONVERT))
1041 for (c = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
1044 if (is_attribute_p ("format", TREE_PURPOSE (c))
1045 && (decode_format_type (IDENTIFIER_POINTER
1046 (TREE_VALUE (TREE_VALUE (c))))
1047 == info.format_type))
1051 /* Check if the current function has a parameter to which
1052 the format attribute could be attached; if not, it
1053 can't be a candidate for a format attribute, despite
1054 the vprintf-like or vscanf-like call. */
1056 for (args = DECL_ARGUMENTS (current_function_decl);
1058 args = DECL_CHAIN (args))
1060 if (TREE_CODE (TREE_TYPE (args)) == POINTER_TYPE
1061 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args)))
1066 warning (OPT_Wmissing_format_attribute, "function might "
1067 "be possible candidate for %qs format attribute",
1068 format_types[info.format_type].name);
1076 /* Variables used by the checking of $ operand number formats. */
1077 static char *dollar_arguments_used = NULL;
1078 static char *dollar_arguments_pointer_p = NULL;
1079 static int dollar_arguments_alloc = 0;
1080 static int dollar_arguments_count;
1081 static int dollar_first_arg_num;
1082 static int dollar_max_arg_used;
1083 static int dollar_format_warned;
1085 /* Initialize the checking for a format string that may contain $
1086 parameter number specifications; we will need to keep track of whether
1087 each parameter has been used. FIRST_ARG_NUM is the number of the first
1088 argument that is a parameter to the format, or 0 for a vprintf-style
1089 function; PARAMS is the list of arguments starting at this argument. */
1092 init_dollar_format_checking (int first_arg_num, tree params)
1094 tree oparams = params;
1096 dollar_first_arg_num = first_arg_num;
1097 dollar_arguments_count = 0;
1098 dollar_max_arg_used = 0;
1099 dollar_format_warned = 0;
1100 if (first_arg_num > 0)
1104 dollar_arguments_count++;
1105 params = TREE_CHAIN (params);
1108 if (dollar_arguments_alloc < dollar_arguments_count)
1110 if (dollar_arguments_used)
1111 free (dollar_arguments_used);
1112 if (dollar_arguments_pointer_p)
1113 free (dollar_arguments_pointer_p);
1114 dollar_arguments_alloc = dollar_arguments_count;
1115 dollar_arguments_used = XNEWVEC (char, dollar_arguments_alloc);
1116 dollar_arguments_pointer_p = XNEWVEC (char, dollar_arguments_alloc);
1118 if (dollar_arguments_alloc)
1120 memset (dollar_arguments_used, 0, dollar_arguments_alloc);
1121 if (first_arg_num > 0)
1127 dollar_arguments_pointer_p[i] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params)))
1129 params = TREE_CHAIN (params);
1137 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
1138 is set, it is an error if one is not found; otherwise, it is OK. If
1139 such a number is found, check whether it is within range and mark that
1140 numbered operand as being used for later checking. Returns the operand
1141 number if found and within range, zero if no such number was found and
1142 this is OK, or -1 on error. PARAMS points to the first operand of the
1143 format; PARAM_PTR is made to point to the parameter referred to. If
1144 a $ format is found, *FORMAT is updated to point just after it. */
1147 maybe_read_dollar_number (const char **format,
1148 int dollar_needed, tree params, tree *param_ptr,
1149 const format_kind_info *fki)
1153 const char *fcp = *format;
1154 if (!ISDIGIT (*fcp))
1158 warning (OPT_Wformat, "missing $ operand number in format");
1166 while (ISDIGIT (*fcp))
1169 nargnum = 10 * argnum + (*fcp - '0');
1170 if (nargnum < 0 || nargnum / 10 != argnum)
1179 warning (OPT_Wformat, "missing $ operand number in format");
1186 if (pedantic && !dollar_format_warned)
1188 warning (OPT_Wformat, "%s does not support %%n$ operand number formats",
1189 C_STD_NAME (STD_EXT));
1190 dollar_format_warned = 1;
1192 if (overflow_flag || argnum == 0
1193 || (dollar_first_arg_num && argnum > dollar_arguments_count))
1195 warning (OPT_Wformat, "operand number out of range in format");
1198 if (argnum > dollar_max_arg_used)
1199 dollar_max_arg_used = argnum;
1200 /* For vprintf-style functions we may need to allocate more memory to
1201 track which arguments are used. */
1202 while (dollar_arguments_alloc < dollar_max_arg_used)
1205 nalloc = 2 * dollar_arguments_alloc + 16;
1206 dollar_arguments_used = XRESIZEVEC (char, dollar_arguments_used,
1208 dollar_arguments_pointer_p = XRESIZEVEC (char, dollar_arguments_pointer_p,
1210 memset (dollar_arguments_used + dollar_arguments_alloc, 0,
1211 nalloc - dollar_arguments_alloc);
1212 dollar_arguments_alloc = nalloc;
1214 if (!(fki->flags & (int) FMT_FLAG_DOLLAR_MULTIPLE)
1215 && dollar_arguments_used[argnum - 1] == 1)
1217 dollar_arguments_used[argnum - 1] = 2;
1218 warning (OPT_Wformat, "format argument %d used more than once in %s format",
1222 dollar_arguments_used[argnum - 1] = 1;
1223 if (dollar_first_arg_num)
1226 *param_ptr = params;
1227 for (i = 1; i < argnum && *param_ptr != 0; i++)
1228 *param_ptr = TREE_CHAIN (*param_ptr);
1230 /* This case shouldn't be caught here. */
1231 gcc_assert (*param_ptr);
1238 /* Ensure that FORMAT does not start with a decimal number followed by
1239 a $; give a diagnostic and return true if it does, false otherwise. */
1242 avoid_dollar_number (const char *format)
1244 if (!ISDIGIT (*format))
1246 while (ISDIGIT (*format))
1250 warning (OPT_Wformat, "$ operand number used after format without operand number");
1257 /* Finish the checking for a format string that used $ operand number formats
1258 instead of non-$ formats. We check for unused operands before used ones
1259 (a serious error, since the implementation of the format function
1260 can't know what types to pass to va_arg to find the later arguments).
1261 and for unused operands at the end of the format (if we know how many
1262 arguments the format had, so not for vprintf). If there were operand
1263 numbers out of range on a non-vprintf-style format, we won't have reached
1264 here. If POINTER_GAP_OK, unused arguments are OK if all arguments are
1268 finish_dollar_format_checking (format_check_results *res, int pointer_gap_ok)
1271 bool found_pointer_gap = false;
1272 for (i = 0; i < dollar_max_arg_used; i++)
1274 if (!dollar_arguments_used[i])
1276 if (pointer_gap_ok && (dollar_first_arg_num == 0
1277 || dollar_arguments_pointer_p[i]))
1278 found_pointer_gap = true;
1280 warning (OPT_Wformat,
1281 "format argument %d unused before used argument %d in $-style format",
1282 i + 1, dollar_max_arg_used);
1285 if (found_pointer_gap
1286 || (dollar_first_arg_num
1287 && dollar_max_arg_used < dollar_arguments_count))
1289 res->number_other--;
1290 res->number_dollar_extra_args++;
1295 /* Retrieve the specification for a format flag. SPEC contains the
1296 specifications for format flags for the applicable kind of format.
1297 FLAG is the flag in question. If PREDICATES is NULL, the basic
1298 spec for that flag must be retrieved and must exist. If
1299 PREDICATES is not NULL, it is a string listing possible predicates
1300 for the spec entry; if an entry predicated on any of these is
1301 found, it is returned, otherwise NULL is returned. */
1303 static const format_flag_spec *
1304 get_flag_spec (const format_flag_spec *spec, int flag, const char *predicates)
1307 for (i = 0; spec[i].flag_char != 0; i++)
1309 if (spec[i].flag_char != flag)
1311 if (predicates != NULL)
1313 if (spec[i].predicate != 0
1314 && strchr (predicates, spec[i].predicate) != 0)
1317 else if (spec[i].predicate == 0)
1320 gcc_assert (predicates);
1325 /* Check the argument list of a call to printf, scanf, etc.
1326 INFO points to the function_format_info structure.
1327 PARAMS is the list of argument values. */
1330 check_format_info (function_format_info *info, tree params)
1332 format_check_context format_ctx;
1333 unsigned HOST_WIDE_INT arg_num;
1335 format_check_results res;
1336 /* Skip to format argument. If the argument isn't available, there's
1337 no work for us to do; prototype checking will catch the problem. */
1338 for (arg_num = 1; ; ++arg_num)
1342 if (arg_num == info->format_num)
1344 params = TREE_CHAIN (params);
1346 format_tree = TREE_VALUE (params);
1347 params = TREE_CHAIN (params);
1348 if (format_tree == 0)
1351 res.number_non_literal = 0;
1352 res.number_extra_args = 0;
1353 res.number_dollar_extra_args = 0;
1354 res.number_wide = 0;
1355 res.number_empty = 0;
1356 res.number_unterminated = 0;
1357 res.number_other = 0;
1359 format_ctx.res = &res;
1360 format_ctx.info = info;
1361 format_ctx.params = params;
1363 check_function_arguments_recurse (check_format_arg, &format_ctx,
1364 format_tree, arg_num);
1366 if (res.number_non_literal > 0)
1368 /* Functions taking a va_list normally pass a non-literal format
1369 string. These functions typically are declared with
1370 first_arg_num == 0, so avoid warning in those cases. */
1371 if (!(format_types[info->format_type].flags & (int) FMT_FLAG_ARG_CONVERT))
1373 /* For strftime-like formats, warn for not checking the format
1374 string; but there are no arguments to check. */
1375 warning (OPT_Wformat_nonliteral,
1376 "format not a string literal, format string not checked");
1378 else if (info->first_arg_num != 0)
1380 /* If there are no arguments for the format at all, we may have
1381 printf (foo) which is likely to be a security hole. */
1382 while (arg_num + 1 < info->first_arg_num)
1386 params = TREE_CHAIN (params);
1389 if (params == 0 && warn_format_security)
1390 warning (OPT_Wformat_security,
1391 "format not a string literal and no format arguments");
1392 else if (params == 0 && warn_format_nonliteral)
1393 warning (OPT_Wformat_nonliteral,
1394 "format not a string literal and no format arguments");
1396 warning (OPT_Wformat_nonliteral,
1397 "format not a string literal, argument types not checked");
1401 /* If there were extra arguments to the format, normally warn. However,
1402 the standard does say extra arguments are ignored, so in the specific
1403 case where we have multiple leaves (conditional expressions or
1404 ngettext) allow extra arguments if at least one leaf didn't have extra
1405 arguments, but was otherwise OK (either non-literal or checked OK).
1406 If the format is an empty string, this should be counted similarly to the
1407 case of extra format arguments. */
1408 if (res.number_extra_args > 0 && res.number_non_literal == 0
1409 && res.number_other == 0)
1410 warning (OPT_Wformat_extra_args, "too many arguments for format");
1411 if (res.number_dollar_extra_args > 0 && res.number_non_literal == 0
1412 && res.number_other == 0)
1413 warning (OPT_Wformat_extra_args, "unused arguments in $-style format");
1414 if (res.number_empty > 0 && res.number_non_literal == 0
1415 && res.number_other == 0)
1416 warning (OPT_Wformat_zero_length, "zero-length %s format string",
1417 format_types[info->format_type].name);
1419 if (res.number_wide > 0)
1420 warning (OPT_Wformat, "format is a wide character string");
1422 if (res.number_unterminated > 0)
1423 warning (OPT_Wformat, "unterminated format string");
1426 /* Callback from check_function_arguments_recurse to check a
1427 format string. FORMAT_TREE is the format parameter. ARG_NUM
1428 is the number of the format argument. CTX points to a
1429 format_check_context. */
1432 check_format_arg (void *ctx, tree format_tree,
1433 unsigned HOST_WIDE_INT arg_num)
1435 format_check_context *format_ctx = (format_check_context *) ctx;
1436 format_check_results *res = format_ctx->res;
1437 function_format_info *info = format_ctx->info;
1438 tree params = format_ctx->params;
1441 HOST_WIDE_INT offset;
1442 const char *format_chars;
1443 tree array_size = 0;
1445 alloc_pool fwt_pool;
1447 if (integer_zerop (format_tree))
1449 /* Skip to first argument to check, so we can see if this format
1450 has any arguments (it shouldn't). */
1451 while (arg_num + 1 < info->first_arg_num)
1455 params = TREE_CHAIN (params);
1460 res->number_other++;
1462 res->number_extra_args++;
1468 if (TREE_CODE (format_tree) == POINTER_PLUS_EXPR)
1472 arg0 = TREE_OPERAND (format_tree, 0);
1473 arg1 = TREE_OPERAND (format_tree, 1);
1476 if (TREE_CODE (arg1) == INTEGER_CST)
1480 res->number_non_literal++;
1483 if (!host_integerp (arg1, 0)
1484 || (offset = tree_low_cst (arg1, 0)) < 0)
1486 res->number_non_literal++;
1490 if (TREE_CODE (format_tree) != ADDR_EXPR)
1492 res->number_non_literal++;
1495 format_tree = TREE_OPERAND (format_tree, 0);
1496 if (format_types[info->format_type].flags
1497 & (int) FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL)
1499 bool objc_str = (info->format_type == gcc_objc_string_format_type);
1500 /* We cannot examine this string here - but we can check that it is
1502 if (TREE_CODE (format_tree) != CONST_DECL
1503 || !((objc_str && objc_string_ref_type_p (TREE_TYPE (format_tree)))
1504 || (*targetcm.string_object_ref_type_p)
1505 ((const_tree) TREE_TYPE (format_tree))))
1507 res->number_non_literal++;
1510 /* Skip to first argument to check. */
1511 while (arg_num + 1 < info->first_arg_num)
1515 params = TREE_CHAIN (params);
1518 /* So, we have a valid literal string object and one or more params.
1519 We need to use an external helper to parse the string into format
1520 info. For Objective-C variants we provide the resource within the
1521 objc tree, for target variants, via a hook. */
1523 objc_check_format_arg (format_tree, params);
1524 else if (targetcm.check_string_object_format_arg)
1525 (*targetcm.check_string_object_format_arg) (format_tree, params);
1526 /* Else we can't handle it and retire quietly. */
1529 if (TREE_CODE (format_tree) == ARRAY_REF
1530 && host_integerp (TREE_OPERAND (format_tree, 1), 0)
1531 && (offset += tree_low_cst (TREE_OPERAND (format_tree, 1), 0)) >= 0)
1532 format_tree = TREE_OPERAND (format_tree, 0);
1533 if (TREE_CODE (format_tree) == VAR_DECL
1534 && TREE_CODE (TREE_TYPE (format_tree)) == ARRAY_TYPE
1535 && (array_init = decl_constant_value (format_tree)) != format_tree
1536 && TREE_CODE (array_init) == STRING_CST)
1538 /* Extract the string constant initializer. Note that this may include
1539 a trailing NUL character that is not in the array (e.g.
1540 const char a[3] = "foo";). */
1541 array_size = DECL_SIZE_UNIT (format_tree);
1542 format_tree = array_init;
1544 if (TREE_CODE (format_tree) != STRING_CST)
1546 res->number_non_literal++;
1549 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree))) != char_type_node)
1554 format_chars = TREE_STRING_POINTER (format_tree);
1555 format_length = TREE_STRING_LENGTH (format_tree);
1556 if (array_size != 0)
1558 /* Variable length arrays can't be initialized. */
1559 gcc_assert (TREE_CODE (array_size) == INTEGER_CST);
1561 if (host_integerp (array_size, 0))
1563 HOST_WIDE_INT array_size_value = TREE_INT_CST_LOW (array_size);
1564 if (array_size_value > 0
1565 && array_size_value == (int) array_size_value
1566 && format_length > array_size_value)
1567 format_length = array_size_value;
1572 if (offset >= format_length)
1574 res->number_non_literal++;
1577 format_chars += offset;
1578 format_length -= offset;
1580 if (format_length < 1 || format_chars[--format_length] != 0)
1582 res->number_unterminated++;
1585 if (format_length == 0)
1587 res->number_empty++;
1591 /* Skip to first argument to check. */
1592 while (arg_num + 1 < info->first_arg_num)
1596 params = TREE_CHAIN (params);
1599 /* Provisionally increment res->number_other; check_format_info_main
1600 will decrement it if it finds there are extra arguments, but this way
1601 need not adjust it for every return. */
1602 res->number_other++;
1603 fwt_pool = create_alloc_pool ("format_wanted_type pool",
1604 sizeof (format_wanted_type), 10);
1605 check_format_info_main (res, info, format_chars, format_length,
1606 params, arg_num, fwt_pool);
1607 free_alloc_pool (fwt_pool);
1611 /* Do the main part of checking a call to a format function. FORMAT_CHARS
1612 is the NUL-terminated format string (which at this point may contain
1613 internal NUL characters); FORMAT_LENGTH is its length (excluding the
1614 terminating NUL character). ARG_NUM is one less than the number of
1615 the first format argument to check; PARAMS points to that format
1616 argument in the list of arguments. */
1619 check_format_info_main (format_check_results *res,
1620 function_format_info *info, const char *format_chars,
1621 int format_length, tree params,
1622 unsigned HOST_WIDE_INT arg_num, alloc_pool fwt_pool)
1624 const char *orig_format_chars = format_chars;
1625 tree first_fillin_param = params;
1627 const format_kind_info *fki = &format_types[info->format_type];
1628 const format_flag_spec *flag_specs = fki->flag_specs;
1629 const format_flag_pair *bad_flag_pairs = fki->bad_flag_pairs;
1631 /* -1 if no conversions taking an operand have been found; 0 if one has
1632 and it didn't use $; 1 if $ formats are in use. */
1633 int has_operand_number = -1;
1635 init_dollar_format_checking (info->first_arg_num, first_fillin_param);
1637 while (*format_chars != 0)
1640 int suppressed = FALSE;
1641 const char *length_chars = NULL;
1642 enum format_lengths length_chars_val = FMT_LEN_none;
1643 enum format_std_version length_chars_std = STD_C89;
1647 int main_arg_num = 0;
1648 tree main_arg_params = 0;
1649 enum format_std_version wanted_type_std;
1650 const char *wanted_type_name;
1651 format_wanted_type width_wanted_type;
1652 format_wanted_type precision_wanted_type;
1653 format_wanted_type main_wanted_type;
1654 format_wanted_type *first_wanted_type = NULL;
1655 format_wanted_type *last_wanted_type = NULL;
1656 const format_length_info *fli = NULL;
1657 const format_char_info *fci = NULL;
1658 char flag_chars[256];
1660 int scalar_identity_flag = 0;
1661 const char *format_start;
1663 if (*format_chars++ != '%')
1665 if (*format_chars == 0)
1667 warning (OPT_Wformat, "spurious trailing %<%%%> in format");
1670 if (*format_chars == '%')
1677 if ((fki->flags & (int) FMT_FLAG_USE_DOLLAR) && has_operand_number != 0)
1679 /* Possibly read a $ operand number at the start of the format.
1680 If one was previously used, one is required here. If one
1681 is not used here, we can't immediately conclude this is a
1682 format without them, since it could be printf %m or scanf %*. */
1684 opnum = maybe_read_dollar_number (&format_chars, 0,
1686 &main_arg_params, fki);
1691 has_operand_number = 1;
1692 main_arg_num = opnum + info->first_arg_num - 1;
1695 else if (fki->flags & FMT_FLAG_USE_DOLLAR)
1697 if (avoid_dollar_number (format_chars))
1701 /* Read any format flags, but do not yet validate them beyond removing
1702 duplicates, since in general validation depends on the rest of
1704 while (*format_chars != 0
1705 && strchr (fki->flag_chars, *format_chars) != 0)
1707 const format_flag_spec *s = get_flag_spec (flag_specs,
1708 *format_chars, NULL);
1709 if (strchr (flag_chars, *format_chars) != 0)
1711 warning (OPT_Wformat, "repeated %s in format", _(s->name));
1715 i = strlen (flag_chars);
1716 flag_chars[i++] = *format_chars;
1719 if (s->skip_next_char)
1722 if (*format_chars == 0)
1724 warning (OPT_Wformat, "missing fill character at end of strfmon format");
1731 /* Read any format width, possibly * or *m$. */
1732 if (fki->width_char != 0)
1734 if (fki->width_type != NULL && *format_chars == '*')
1736 i = strlen (flag_chars);
1737 flag_chars[i++] = fki->width_char;
1739 /* "...a field width...may be indicated by an asterisk.
1740 In this case, an int argument supplies the field width..." */
1742 if (has_operand_number != 0)
1745 opnum = maybe_read_dollar_number (&format_chars,
1746 has_operand_number == 1,
1753 has_operand_number = 1;
1754 arg_num = opnum + info->first_arg_num - 1;
1757 has_operand_number = 0;
1761 if (avoid_dollar_number (format_chars))
1764 if (info->first_arg_num != 0)
1770 cur_param = TREE_VALUE (params);
1771 if (has_operand_number <= 0)
1773 params = TREE_CHAIN (params);
1777 width_wanted_type.wanted_type = *fki->width_type;
1778 width_wanted_type.wanted_type_name = NULL;
1779 width_wanted_type.pointer_count = 0;
1780 width_wanted_type.char_lenient_flag = 0;
1781 width_wanted_type.scalar_identity_flag = 0;
1782 width_wanted_type.writing_in_flag = 0;
1783 width_wanted_type.reading_from_flag = 0;
1784 width_wanted_type.kind = CF_KIND_FIELD_WIDTH;
1785 width_wanted_type.format_start = format_chars - 1;
1786 width_wanted_type.format_length = 1;
1787 width_wanted_type.param = cur_param;
1788 width_wanted_type.arg_num = arg_num;
1789 width_wanted_type.next = NULL;
1790 if (last_wanted_type != 0)
1791 last_wanted_type->next = &width_wanted_type;
1792 if (first_wanted_type == 0)
1793 first_wanted_type = &width_wanted_type;
1794 last_wanted_type = &width_wanted_type;
1799 /* Possibly read a numeric width. If the width is zero,
1800 we complain if appropriate. */
1801 int non_zero_width_char = FALSE;
1802 int found_width = FALSE;
1803 while (ISDIGIT (*format_chars))
1806 if (*format_chars != '0')
1807 non_zero_width_char = TRUE;
1810 if (found_width && !non_zero_width_char &&
1811 (fki->flags & (int) FMT_FLAG_ZERO_WIDTH_BAD))
1812 warning (OPT_Wformat, "zero width in %s format", fki->name);
1815 i = strlen (flag_chars);
1816 flag_chars[i++] = fki->width_char;
1822 /* Read any format left precision (must be a number, not *). */
1823 if (fki->left_precision_char != 0 && *format_chars == '#')
1826 i = strlen (flag_chars);
1827 flag_chars[i++] = fki->left_precision_char;
1829 if (!ISDIGIT (*format_chars))
1830 warning (OPT_Wformat, "empty left precision in %s format", fki->name);
1831 while (ISDIGIT (*format_chars))
1835 /* Read any format precision, possibly * or *m$. */
1836 if (fki->precision_char != 0 && *format_chars == '.')
1839 i = strlen (flag_chars);
1840 flag_chars[i++] = fki->precision_char;
1842 if (fki->precision_type != NULL && *format_chars == '*')
1844 /* "...a...precision...may be indicated by an asterisk.
1845 In this case, an int argument supplies the...precision." */
1847 if (has_operand_number != 0)
1850 opnum = maybe_read_dollar_number (&format_chars,
1851 has_operand_number == 1,
1858 has_operand_number = 1;
1859 arg_num = opnum + info->first_arg_num - 1;
1862 has_operand_number = 0;
1866 if (avoid_dollar_number (format_chars))
1869 if (info->first_arg_num != 0)
1875 cur_param = TREE_VALUE (params);
1876 if (has_operand_number <= 0)
1878 params = TREE_CHAIN (params);
1882 precision_wanted_type.wanted_type = *fki->precision_type;
1883 precision_wanted_type.wanted_type_name = NULL;
1884 precision_wanted_type.pointer_count = 0;
1885 precision_wanted_type.char_lenient_flag = 0;
1886 precision_wanted_type.scalar_identity_flag = 0;
1887 precision_wanted_type.writing_in_flag = 0;
1888 precision_wanted_type.reading_from_flag = 0;
1889 precision_wanted_type.kind = CF_KIND_FIELD_PRECISION;
1890 precision_wanted_type.param = cur_param;
1891 precision_wanted_type.format_start = format_chars - 2;
1892 precision_wanted_type.format_length = 2;
1893 precision_wanted_type.arg_num = arg_num;
1894 precision_wanted_type.next = NULL;
1895 if (last_wanted_type != 0)
1896 last_wanted_type->next = &precision_wanted_type;
1897 if (first_wanted_type == 0)
1898 first_wanted_type = &precision_wanted_type;
1899 last_wanted_type = &precision_wanted_type;
1904 if (!(fki->flags & (int) FMT_FLAG_EMPTY_PREC_OK)
1905 && !ISDIGIT (*format_chars))
1906 warning (OPT_Wformat, "empty precision in %s format", fki->name);
1907 while (ISDIGIT (*format_chars))
1912 format_start = format_chars;
1913 if (fki->alloc_char && fki->alloc_char == *format_chars)
1915 i = strlen (flag_chars);
1916 flag_chars[i++] = fki->alloc_char;
1921 /* Handle the scanf allocation kludge. */
1922 if (fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
1924 if (*format_chars == 'a' && !flag_isoc99)
1926 if (format_chars[1] == 's' || format_chars[1] == 'S'
1927 || format_chars[1] == '[')
1929 /* 'a' is used as a flag. */
1930 i = strlen (flag_chars);
1931 flag_chars[i++] = 'a';
1938 /* Read any length modifier, if this kind of format has them. */
1939 fli = fki->length_char_specs;
1940 length_chars = NULL;
1941 length_chars_val = FMT_LEN_none;
1942 length_chars_std = STD_C89;
1943 scalar_identity_flag = 0;
1946 while (fli->name != 0
1947 && strncmp (fli->name, format_chars, strlen (fli->name)))
1951 format_chars += strlen (fli->name);
1952 if (fli->double_name != 0 && fli->name[0] == *format_chars)
1955 length_chars = fli->double_name;
1956 length_chars_val = fli->double_index;
1957 length_chars_std = fli->double_std;
1961 length_chars = fli->name;
1962 length_chars_val = fli->index;
1963 length_chars_std = fli->std;
1964 scalar_identity_flag = fli->scalar_identity_flag;
1966 i = strlen (flag_chars);
1967 flag_chars[i++] = fki->length_code_char;
1972 /* Warn if the length modifier is non-standard. */
1973 if (ADJ_STD (length_chars_std) > C_STD_VER)
1974 warning (OPT_Wformat,
1975 "%s does not support the %qs %s length modifier",
1976 C_STD_NAME (length_chars_std), length_chars,
1981 /* Read any modifier (strftime E/O). */
1982 if (fki->modifier_chars != NULL)
1984 while (*format_chars != 0
1985 && strchr (fki->modifier_chars, *format_chars) != 0)
1987 if (strchr (flag_chars, *format_chars) != 0)
1989 const format_flag_spec *s = get_flag_spec (flag_specs,
1990 *format_chars, NULL);
1991 warning (OPT_Wformat, "repeated %s in format", _(s->name));
1995 i = strlen (flag_chars);
1996 flag_chars[i++] = *format_chars;
2003 format_char = *format_chars;
2004 if (format_char == 0
2005 || (!(fki->flags & (int) FMT_FLAG_FANCY_PERCENT_OK)
2006 && format_char == '%'))
2008 warning (OPT_Wformat, "conversion lacks type at end of format");
2012 fci = fki->conversion_specs;
2013 while (fci->format_chars != 0
2014 && strchr (fci->format_chars, format_char) == 0)
2016 if (fci->format_chars == 0)
2018 if (ISGRAPH (format_char))
2019 warning (OPT_Wformat, "unknown conversion type character %qc in format",
2022 warning (OPT_Wformat, "unknown conversion type character 0x%x in format",
2028 if (ADJ_STD (fci->std) > C_STD_VER)
2029 warning (OPT_Wformat, "%s does not support the %<%%%c%> %s format",
2030 C_STD_NAME (fci->std), format_char, fki->name);
2033 /* Validate the individual flags used, removing any that are invalid. */
2036 for (i = 0; flag_chars[i] != 0; i++)
2038 const format_flag_spec *s = get_flag_spec (flag_specs,
2039 flag_chars[i], NULL);
2040 flag_chars[i - d] = flag_chars[i];
2041 if (flag_chars[i] == fki->length_code_char)
2043 if (strchr (fci->flag_chars, flag_chars[i]) == 0)
2045 warning (OPT_Wformat, "%s used with %<%%%c%> %s format",
2046 _(s->name), format_char, fki->name);
2052 const format_flag_spec *t;
2053 if (ADJ_STD (s->std) > C_STD_VER)
2054 warning (OPT_Wformat, "%s does not support %s",
2055 C_STD_NAME (s->std), _(s->long_name));
2056 t = get_flag_spec (flag_specs, flag_chars[i], fci->flags2);
2057 if (t != NULL && ADJ_STD (t->std) > ADJ_STD (s->std))
2059 const char *long_name = (t->long_name != NULL
2062 if (ADJ_STD (t->std) > C_STD_VER)
2063 warning (OPT_Wformat,
2064 "%s does not support %s with the %<%%%c%> %s format",
2065 C_STD_NAME (t->std), _(long_name),
2066 format_char, fki->name);
2070 flag_chars[i - d] = 0;
2073 if ((fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
2074 && strchr (flag_chars, 'a') != 0)
2076 if (fki->alloc_char && strchr (flag_chars, fki->alloc_char) != 0)
2079 if (fki->suppression_char
2080 && strchr (flag_chars, fki->suppression_char) != 0)
2083 /* Validate the pairs of flags used. */
2084 for (i = 0; bad_flag_pairs[i].flag_char1 != 0; i++)
2086 const format_flag_spec *s, *t;
2087 if (strchr (flag_chars, bad_flag_pairs[i].flag_char1) == 0)
2089 if (strchr (flag_chars, bad_flag_pairs[i].flag_char2) == 0)
2091 if (bad_flag_pairs[i].predicate != 0
2092 && strchr (fci->flags2, bad_flag_pairs[i].predicate) == 0)
2094 s = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char1, NULL);
2095 t = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char2, NULL);
2096 if (bad_flag_pairs[i].ignored)
2098 if (bad_flag_pairs[i].predicate != 0)
2099 warning (OPT_Wformat,
2100 "%s ignored with %s and %<%%%c%> %s format",
2101 _(s->name), _(t->name), format_char,
2104 warning (OPT_Wformat, "%s ignored with %s in %s format",
2105 _(s->name), _(t->name), fki->name);
2109 if (bad_flag_pairs[i].predicate != 0)
2110 warning (OPT_Wformat,
2111 "use of %s and %s together with %<%%%c%> %s format",
2112 _(s->name), _(t->name), format_char,
2115 warning (OPT_Wformat, "use of %s and %s together in %s format",
2116 _(s->name), _(t->name), fki->name);
2120 /* Give Y2K warnings. */
2121 if (warn_format_y2k)
2124 if (strchr (fci->flags2, '4') != 0)
2125 if (strchr (flag_chars, 'E') != 0)
2129 else if (strchr (fci->flags2, '3') != 0)
2131 else if (strchr (fci->flags2, '2') != 0)
2134 warning (OPT_Wformat_y2k, "%<%%%c%> yields only last 2 digits of "
2135 "year in some locales", format_char);
2136 else if (y2k_level == 2)
2137 warning (OPT_Wformat_y2k, "%<%%%c%> yields only last 2 digits of "
2138 "year", format_char);
2141 if (strchr (fci->flags2, '[') != 0)
2143 /* Skip over scan set, in case it happens to have '%' in it. */
2144 if (*format_chars == '^')
2146 /* Find closing bracket; if one is hit immediately, then
2147 it's part of the scan set rather than a terminator. */
2148 if (*format_chars == ']')
2150 while (*format_chars && *format_chars != ']')
2152 if (*format_chars != ']')
2153 /* The end of the format string was reached. */
2154 warning (OPT_Wformat, "no closing %<]%> for %<%%[%> format");
2158 wanted_type_name = 0;
2159 if (fki->flags & (int) FMT_FLAG_ARG_CONVERT)
2161 wanted_type = (fci->types[length_chars_val].type
2162 ? *fci->types[length_chars_val].type : 0);
2163 wanted_type_name = fci->types[length_chars_val].name;
2164 wanted_type_std = fci->types[length_chars_val].std;
2165 if (wanted_type == 0)
2167 warning (OPT_Wformat,
2168 "use of %qs length modifier with %qc type character",
2169 length_chars, format_char);
2170 /* Heuristic: skip one argument when an invalid length/type
2171 combination is encountered. */
2174 params = TREE_CHAIN (params);
2178 /* Warn if non-standard, provided it is more non-standard
2179 than the length and type characters that may already
2180 have been warned for. */
2181 && ADJ_STD (wanted_type_std) > ADJ_STD (length_chars_std)
2182 && ADJ_STD (wanted_type_std) > ADJ_STD (fci->std))
2184 if (ADJ_STD (wanted_type_std) > C_STD_VER)
2185 warning (OPT_Wformat,
2186 "%s does not support the %<%%%s%c%> %s format",
2187 C_STD_NAME (wanted_type_std), length_chars,
2188 format_char, fki->name);
2192 main_wanted_type.next = NULL;
2194 /* Finally. . .check type of argument against desired type! */
2195 if (info->first_arg_num == 0)
2197 if ((fci->pointer_count == 0 && wanted_type == void_type_node)
2200 if (main_arg_num != 0)
2203 warning (OPT_Wformat, "operand number specified with "
2204 "suppressed assignment");
2206 warning (OPT_Wformat, "operand number specified for format "
2207 "taking no argument");
2212 format_wanted_type *wanted_type_ptr;
2214 if (main_arg_num != 0)
2216 arg_num = main_arg_num;
2217 params = main_arg_params;
2222 if (has_operand_number > 0)
2224 warning (OPT_Wformat, "missing $ operand number in format");
2228 has_operand_number = 0;
2231 wanted_type_ptr = &main_wanted_type;
2238 cur_param = TREE_VALUE (params);
2239 params = TREE_CHAIN (params);
2242 wanted_type_ptr->wanted_type = wanted_type;
2243 wanted_type_ptr->wanted_type_name = wanted_type_name;
2244 wanted_type_ptr->pointer_count = fci->pointer_count + alloc_flag;
2245 wanted_type_ptr->char_lenient_flag = 0;
2246 if (strchr (fci->flags2, 'c') != 0)
2247 wanted_type_ptr->char_lenient_flag = 1;
2248 wanted_type_ptr->scalar_identity_flag = 0;
2249 if (scalar_identity_flag)
2250 wanted_type_ptr->scalar_identity_flag = 1;
2251 wanted_type_ptr->writing_in_flag = 0;
2252 wanted_type_ptr->reading_from_flag = 0;
2254 wanted_type_ptr->writing_in_flag = 1;
2257 if (strchr (fci->flags2, 'W') != 0)
2258 wanted_type_ptr->writing_in_flag = 1;
2259 if (strchr (fci->flags2, 'R') != 0)
2260 wanted_type_ptr->reading_from_flag = 1;
2262 wanted_type_ptr->kind = CF_KIND_FORMAT;
2263 wanted_type_ptr->param = cur_param;
2264 wanted_type_ptr->arg_num = arg_num;
2265 wanted_type_ptr->format_start = format_start;
2266 wanted_type_ptr->format_length = format_chars - format_start;
2267 wanted_type_ptr->next = NULL;
2268 if (last_wanted_type != 0)
2269 last_wanted_type->next = wanted_type_ptr;
2270 if (first_wanted_type == 0)
2271 first_wanted_type = wanted_type_ptr;
2272 last_wanted_type = wanted_type_ptr;
2277 wanted_type_ptr = (format_wanted_type *)
2278 pool_alloc (fwt_pool);
2280 wanted_type = *fci->types[length_chars_val].type;
2281 wanted_type_name = fci->types[length_chars_val].name;
2286 if (first_wanted_type != 0)
2287 check_format_types (first_wanted_type);
2290 if (format_chars - orig_format_chars != format_length)
2291 warning (OPT_Wformat_contains_nul, "embedded %<\\0%> in format");
2292 if (info->first_arg_num != 0 && params != 0
2293 && has_operand_number <= 0)
2295 res->number_other--;
2296 res->number_extra_args++;
2298 if (has_operand_number > 0)
2299 finish_dollar_format_checking (res, fki->flags & (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK);
2303 /* Check the argument types from a single format conversion (possibly
2304 including width and precision arguments). */
2306 check_format_types (format_wanted_type *types)
2308 for (; types != 0; types = types->next)
2318 wanted_type = types->wanted_type;
2319 arg_num = types->arg_num;
2321 /* The following should not occur here. */
2322 gcc_assert (wanted_type);
2323 gcc_assert (wanted_type != void_type_node || types->pointer_count);
2325 if (types->pointer_count == 0)
2326 wanted_type = lang_hooks.types.type_promotes_to (wanted_type);
2328 wanted_type = TYPE_MAIN_VARIANT (wanted_type);
2330 cur_param = types->param;
2333 format_type_warning (types, wanted_type, NULL);
2337 cur_type = TREE_TYPE (cur_param);
2338 if (cur_type == error_mark_node)
2340 orig_cur_type = cur_type;
2343 STRIP_NOPS (cur_param);
2345 /* Check the types of any additional pointer arguments
2346 that precede the "real" argument. */
2347 for (i = 0; i < types->pointer_count; ++i)
2349 if (TREE_CODE (cur_type) == POINTER_TYPE)
2351 cur_type = TREE_TYPE (cur_type);
2352 if (cur_type == error_mark_node)
2355 /* Check for writing through a NULL pointer. */
2356 if (types->writing_in_flag
2359 && integer_zerop (cur_param))
2360 warning (OPT_Wformat, "writing through null pointer "
2361 "(argument %d)", arg_num);
2363 /* Check for reading through a NULL pointer. */
2364 if (types->reading_from_flag
2367 && integer_zerop (cur_param))
2368 warning (OPT_Wformat, "reading through null pointer "
2369 "(argument %d)", arg_num);
2371 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
2372 cur_param = TREE_OPERAND (cur_param, 0);
2376 /* See if this is an attempt to write into a const type with
2377 scanf or with printf "%n". Note: the writing in happens
2378 at the first indirection only, if for example
2379 void * const * is passed to scanf %p; passing
2380 const void ** is simply passing an incompatible type. */
2381 if (types->writing_in_flag
2383 && (TYPE_READONLY (cur_type)
2385 && (CONSTANT_CLASS_P (cur_param)
2386 || (DECL_P (cur_param)
2387 && TREE_READONLY (cur_param))))))
2388 warning (OPT_Wformat, "writing into constant object "
2389 "(argument %d)", arg_num);
2391 /* If there are extra type qualifiers beyond the first
2392 indirection, then this makes the types technically
2396 && (TYPE_READONLY (cur_type)
2397 || TYPE_VOLATILE (cur_type)
2398 || TYPE_RESTRICT (cur_type)))
2399 warning (OPT_Wformat, "extra type qualifiers in format "
2400 "argument (argument %d)",
2406 format_type_warning (types, wanted_type, orig_cur_type);
2411 if (i < types->pointer_count)
2414 cur_type = TYPE_MAIN_VARIANT (cur_type);
2416 /* Check whether the argument type is a character type. This leniency
2417 only applies to certain formats, flagged with 'c'.
2419 if (types->char_lenient_flag)
2420 char_type_flag = (cur_type == char_type_node
2421 || cur_type == signed_char_type_node
2422 || cur_type == unsigned_char_type_node);
2424 /* Check the type of the "real" argument, if there's a type we want. */
2425 if (lang_hooks.types_compatible_p (wanted_type, cur_type))
2427 /* If we want 'void *', allow any pointer type.
2428 (Anything else would already have got a warning.)
2429 With -pedantic, only allow pointers to void and to character
2431 if (wanted_type == void_type_node
2432 && (!pedantic || (i == 1 && char_type_flag)))
2434 /* Don't warn about differences merely in signedness, unless
2435 -pedantic. With -pedantic, warn if the type is a pointer
2436 target and not a character type, and for character types at
2437 a second level of indirection. */
2438 if (TREE_CODE (wanted_type) == INTEGER_TYPE
2439 && TREE_CODE (cur_type) == INTEGER_TYPE
2440 && (!pedantic || i == 0 || (i == 1 && char_type_flag))
2441 && (TYPE_UNSIGNED (wanted_type)
2442 ? wanted_type == c_common_unsigned_type (cur_type)
2443 : wanted_type == c_common_signed_type (cur_type)))
2445 /* Likewise, "signed char", "unsigned char" and "char" are
2446 equivalent but the above test won't consider them equivalent. */
2447 if (wanted_type == char_type_node
2448 && (!pedantic || i < 2)
2451 if (types->scalar_identity_flag
2452 && (TREE_CODE (cur_type) == TREE_CODE (wanted_type)
2453 || (INTEGRAL_TYPE_P (cur_type)
2454 && INTEGRAL_TYPE_P (wanted_type)))
2455 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type))
2457 /* Now we have a type mismatch. */
2458 format_type_warning (types, wanted_type, orig_cur_type);
2463 /* Give a warning about a format argument of different type from that
2464 expected. WANTED_TYPE is the type the argument should have, possibly
2465 stripped of pointer dereferences. The description (such as "field
2466 precision"), the placement in the format string, a possibly more
2467 friendly name of WANTED_TYPE, and the number of pointer dereferences
2468 are taken from TYPE. ARG_TYPE is the type of the actual argument,
2469 or NULL if it is missing. */
2471 format_type_warning (format_wanted_type *type, tree wanted_type, tree arg_type)
2473 int kind = type->kind;
2474 const char *wanted_type_name = type->wanted_type_name;
2475 const char *format_start = type->format_start;
2476 int format_length = type->format_length;
2477 int pointer_count = type->pointer_count;
2478 int arg_num = type->arg_num;
2481 /* If ARG_TYPE is a typedef with a misleading name (for example,
2482 size_t but not the standard size_t expected by printf %zu), avoid
2483 printing the typedef name. */
2484 if (wanted_type_name
2486 && TYPE_NAME (arg_type)
2487 && TREE_CODE (TYPE_NAME (arg_type)) == TYPE_DECL
2488 && DECL_NAME (TYPE_NAME (arg_type))
2489 && !strcmp (wanted_type_name,
2490 lang_hooks.decl_printable_name (TYPE_NAME (arg_type), 2)))
2491 arg_type = TYPE_MAIN_VARIANT (arg_type);
2492 /* The format type and name exclude any '*' for pointers, so those
2493 must be formatted manually. For all the types we currently have,
2494 this is adequate, but formats taking pointers to functions or
2495 arrays would require the full type to be built up in order to
2496 print it with %T. */
2497 p = (char *) alloca (pointer_count + 2);
2498 if (pointer_count == 0)
2500 else if (c_dialect_cxx ())
2502 memset (p, '*', pointer_count);
2503 p[pointer_count] = 0;
2508 memset (p + 1, '*', pointer_count);
2509 p[pointer_count + 1] = 0;
2512 if (wanted_type_name)
2515 warning (OPT_Wformat, "%s %<%s%.*s%> expects argument of type %<%s%s%>, "
2516 "but argument %d has type %qT",
2517 gettext (kind_descriptions[kind]),
2518 (kind == CF_KIND_FORMAT ? "%" : ""),
2519 format_length, format_start,
2520 wanted_type_name, p, arg_num, arg_type);
2522 warning (OPT_Wformat, "%s %<%s%.*s%> expects a matching %<%s%s%> argument",
2523 gettext (kind_descriptions[kind]),
2524 (kind == CF_KIND_FORMAT ? "%" : ""),
2525 format_length, format_start, wanted_type_name, p);
2530 warning (OPT_Wformat, "%s %<%s%.*s%> expects argument of type %<%T%s%>, "
2531 "but argument %d has type %qT",
2532 gettext (kind_descriptions[kind]),
2533 (kind == CF_KIND_FORMAT ? "%" : ""),
2534 format_length, format_start,
2535 wanted_type, p, arg_num, arg_type);
2537 warning (OPT_Wformat, "%s %<%s%.*s%> expects a matching %<%T%s%> argument",
2538 gettext (kind_descriptions[kind]),
2539 (kind == CF_KIND_FORMAT ? "%" : ""),
2540 format_length, format_start, wanted_type, p);
2545 /* Given a format_char_info array FCI, and a character C, this function
2546 returns the index into the conversion_specs where that specifier's
2547 data is located. The character must exist. */
2549 find_char_info_specifier_index (const format_char_info *fci, int c)
2553 for (i = 0; fci->format_chars; i++, fci++)
2554 if (strchr (fci->format_chars, c))
2557 /* We shouldn't be looking for a non-existent specifier. */
2561 /* Given a format_length_info array FLI, and a character C, this
2562 function returns the index into the conversion_specs where that
2563 modifier's data is located. The character must exist. */
2565 find_length_info_modifier_index (const format_length_info *fli, int c)
2569 for (i = 0; fli->name; i++, fli++)
2570 if (strchr (fli->name, c))
2573 /* We shouldn't be looking for a non-existent modifier. */
2577 /* Determine the type of HOST_WIDE_INT in the code being compiled for
2578 use in GCC's __asm_fprintf__ custom format attribute. You must
2579 have set dynamic_format_types before calling this function. */
2581 init_dynamic_asm_fprintf_info (void)
2587 format_length_info *new_asm_fprintf_length_specs;
2590 /* Find the underlying type for HOST_WIDE_INT. For the %w
2591 length modifier to work, one must have issued: "typedef
2592 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2593 prior to using that modifier. */
2594 hwi = maybe_get_identifier ("__gcc_host_wide_int__");
2597 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2600 hwi = identifier_global_value (hwi);
2601 if (!hwi || TREE_CODE (hwi) != TYPE_DECL)
2603 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2606 hwi = DECL_ORIGINAL_TYPE (hwi);
2608 if (hwi != long_integer_type_node && hwi != long_long_integer_type_node)
2610 error ("%<__gcc_host_wide_int__%> is not defined as %<long%>"
2611 " or %<long long%>");
2615 /* Create a new (writable) copy of asm_fprintf_length_specs. */
2616 new_asm_fprintf_length_specs = (format_length_info *)
2617 xmemdup (asm_fprintf_length_specs,
2618 sizeof (asm_fprintf_length_specs),
2619 sizeof (asm_fprintf_length_specs));
2621 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2622 i = find_length_info_modifier_index (new_asm_fprintf_length_specs, 'w');
2623 if (hwi == long_integer_type_node)
2624 new_asm_fprintf_length_specs[i].index = FMT_LEN_l;
2625 else if (hwi == long_long_integer_type_node)
2626 new_asm_fprintf_length_specs[i].index = FMT_LEN_ll;
2630 /* Assign the new data for use. */
2631 dynamic_format_types[asm_fprintf_format_type].length_char_specs =
2632 new_asm_fprintf_length_specs;
2636 /* Determine the type of a "locus" in the code being compiled for use
2637 in GCC's __gcc_gfc__ custom format attribute. You must have set
2638 dynamic_format_types before calling this function. */
2640 init_dynamic_gfc_info (void)
2646 static format_char_info *gfc_fci;
2648 /* For the GCC __gcc_gfc__ custom format specifier to work, one
2649 must have declared 'locus' prior to using this attribute. If
2650 we haven't seen this declarations then you shouldn't use the
2651 specifier requiring that type. */
2652 if ((locus = maybe_get_identifier ("locus")))
2654 locus = identifier_global_value (locus);
2657 if (TREE_CODE (locus) != TYPE_DECL
2658 || TREE_TYPE (locus) == error_mark_node)
2660 error ("%<locus%> is not defined as a type");
2664 locus = TREE_TYPE (locus);
2668 /* Assign the new data for use. */
2670 /* Handle the __gcc_gfc__ format specifics. */
2672 dynamic_format_types[gcc_gfc_format_type].conversion_specs =
2673 gfc_fci = (format_char_info *)
2674 xmemdup (gcc_gfc_char_table,
2675 sizeof (gcc_gfc_char_table),
2676 sizeof (gcc_gfc_char_table));
2679 const unsigned i = find_char_info_specifier_index (gfc_fci, 'L');
2680 gfc_fci[i].types[0].type = &locus;
2681 gfc_fci[i].pointer_count = 1;
2686 /* Determine the types of "tree" and "location_t" in the code being
2687 compiled for use in GCC's diagnostic custom format attributes. You
2688 must have set dynamic_format_types before calling this function. */
2690 init_dynamic_diag_info (void)
2692 static tree t, loc, hwi;
2694 if (!loc || !t || !hwi)
2696 static format_char_info *diag_fci, *tdiag_fci, *cdiag_fci, *cxxdiag_fci;
2697 static format_length_info *diag_ls;
2700 /* For the GCC-diagnostics custom format specifiers to work, one
2701 must have declared 'tree' and/or 'location_t' prior to using
2702 those attributes. If we haven't seen these declarations then
2703 you shouldn't use the specifiers requiring these types.
2704 However we don't force a hard ICE because we may see only one
2705 or the other type. */
2706 if ((loc = maybe_get_identifier ("location_t")))
2708 loc = identifier_global_value (loc);
2711 if (TREE_CODE (loc) != TYPE_DECL)
2713 error ("%<location_t%> is not defined as a type");
2717 loc = TREE_TYPE (loc);
2721 /* We need to grab the underlying 'union tree_node' so peek into
2722 an extra type level. */
2723 if ((t = maybe_get_identifier ("tree")))
2725 t = identifier_global_value (t);
2728 if (TREE_CODE (t) != TYPE_DECL)
2730 error ("%<tree%> is not defined as a type");
2733 else if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
2735 error ("%<tree%> is not defined as a pointer type");
2739 t = TREE_TYPE (TREE_TYPE (t));
2743 /* Find the underlying type for HOST_WIDE_INT. For the %w
2744 length modifier to work, one must have issued: "typedef
2745 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2746 prior to using that modifier. */
2747 if ((hwi = maybe_get_identifier ("__gcc_host_wide_int__")))
2749 hwi = identifier_global_value (hwi);
2752 if (TREE_CODE (hwi) != TYPE_DECL)
2754 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2759 hwi = DECL_ORIGINAL_TYPE (hwi);
2761 if (hwi != long_integer_type_node
2762 && hwi != long_long_integer_type_node)
2764 error ("%<__gcc_host_wide_int__%> is not defined"
2765 " as %<long%> or %<long long%>");
2772 /* Assign the new data for use. */
2774 /* All the GCC diag formats use the same length specs. */
2776 dynamic_format_types[gcc_diag_format_type].length_char_specs =
2777 dynamic_format_types[gcc_tdiag_format_type].length_char_specs =
2778 dynamic_format_types[gcc_cdiag_format_type].length_char_specs =
2779 dynamic_format_types[gcc_cxxdiag_format_type].length_char_specs =
2780 diag_ls = (format_length_info *)
2781 xmemdup (gcc_diag_length_specs,
2782 sizeof (gcc_diag_length_specs),
2783 sizeof (gcc_diag_length_specs));
2786 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2787 i = find_length_info_modifier_index (diag_ls, 'w');
2788 if (hwi == long_integer_type_node)
2789 diag_ls[i].index = FMT_LEN_l;
2790 else if (hwi == long_long_integer_type_node)
2791 diag_ls[i].index = FMT_LEN_ll;
2796 /* Handle the __gcc_diag__ format specifics. */
2798 dynamic_format_types[gcc_diag_format_type].conversion_specs =
2799 diag_fci = (format_char_info *)
2800 xmemdup (gcc_diag_char_table,
2801 sizeof (gcc_diag_char_table),
2802 sizeof (gcc_diag_char_table));
2805 i = find_char_info_specifier_index (diag_fci, 'K');
2806 diag_fci[i].types[0].type = &t;
2807 diag_fci[i].pointer_count = 1;
2810 /* Handle the __gcc_tdiag__ format specifics. */
2812 dynamic_format_types[gcc_tdiag_format_type].conversion_specs =
2813 tdiag_fci = (format_char_info *)
2814 xmemdup (gcc_tdiag_char_table,
2815 sizeof (gcc_tdiag_char_table),
2816 sizeof (gcc_tdiag_char_table));
2819 /* All specifiers taking a tree share the same struct. */
2820 i = find_char_info_specifier_index (tdiag_fci, 'D');
2821 tdiag_fci[i].types[0].type = &t;
2822 tdiag_fci[i].pointer_count = 1;
2823 i = find_char_info_specifier_index (tdiag_fci, 'K');
2824 tdiag_fci[i].types[0].type = &t;
2825 tdiag_fci[i].pointer_count = 1;
2828 /* Handle the __gcc_cdiag__ format specifics. */
2830 dynamic_format_types[gcc_cdiag_format_type].conversion_specs =
2831 cdiag_fci = (format_char_info *)
2832 xmemdup (gcc_cdiag_char_table,
2833 sizeof (gcc_cdiag_char_table),
2834 sizeof (gcc_cdiag_char_table));
2837 /* All specifiers taking a tree share the same struct. */
2838 i = find_char_info_specifier_index (cdiag_fci, 'D');
2839 cdiag_fci[i].types[0].type = &t;
2840 cdiag_fci[i].pointer_count = 1;
2841 i = find_char_info_specifier_index (cdiag_fci, 'K');
2842 cdiag_fci[i].types[0].type = &t;
2843 cdiag_fci[i].pointer_count = 1;
2846 /* Handle the __gcc_cxxdiag__ format specifics. */
2848 dynamic_format_types[gcc_cxxdiag_format_type].conversion_specs =
2849 cxxdiag_fci = (format_char_info *)
2850 xmemdup (gcc_cxxdiag_char_table,
2851 sizeof (gcc_cxxdiag_char_table),
2852 sizeof (gcc_cxxdiag_char_table));
2855 /* All specifiers taking a tree share the same struct. */
2856 i = find_char_info_specifier_index (cxxdiag_fci, 'D');
2857 cxxdiag_fci[i].types[0].type = &t;
2858 cxxdiag_fci[i].pointer_count = 1;
2859 i = find_char_info_specifier_index (cxxdiag_fci, 'K');
2860 cxxdiag_fci[i].types[0].type = &t;
2861 cxxdiag_fci[i].pointer_count = 1;
2866 #ifdef TARGET_FORMAT_TYPES
2867 extern const format_kind_info TARGET_FORMAT_TYPES[];
2870 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
2871 extern const target_ovr_attr TARGET_OVERRIDES_FORMAT_ATTRIBUTES[];
2873 #ifdef TARGET_OVERRIDES_FORMAT_INIT
2874 extern void TARGET_OVERRIDES_FORMAT_INIT (void);
2877 /* Attributes such as "printf" are equivalent to those such as
2878 "gnu_printf" unless this is overridden by a target. */
2879 static const target_ovr_attr gnu_target_overrides_format_attributes[] =
2881 { "gnu_printf", "printf" },
2882 { "gnu_scanf", "scanf" },
2883 { "gnu_strftime", "strftime" },
2884 { "gnu_strfmon", "strfmon" },
2888 /* Translate to unified attribute name. This is used in decode_format_type and
2889 decode_format_attr. In attr_name the user specified argument is passed. It
2890 returns the unified format name from TARGET_OVERRIDES_FORMAT_ATTRIBUTES
2891 or the attr_name passed to this function, if there is no matching entry. */
2893 convert_format_name_to_system_name (const char *attr_name)
2897 if (attr_name == NULL || *attr_name == 0
2898 || strncmp (attr_name, "gcc_", 4) == 0)
2900 #ifdef TARGET_OVERRIDES_FORMAT_INIT
2901 TARGET_OVERRIDES_FORMAT_INIT ();
2904 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
2905 /* Check if format attribute is overridden by target. */
2906 if (TARGET_OVERRIDES_FORMAT_ATTRIBUTES != NULL
2907 && TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT > 0)
2909 for (i = 0; i < TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT; ++i)
2911 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_src,
2914 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_dst,
2916 return TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_src;
2920 /* Otherwise default to gnu format. */
2922 gnu_target_overrides_format_attributes[i].named_attr_src != NULL;
2925 if (cmp_attribs (gnu_target_overrides_format_attributes[i].named_attr_src,
2928 if (cmp_attribs (gnu_target_overrides_format_attributes[i].named_attr_dst,
2930 return gnu_target_overrides_format_attributes[i].named_attr_src;
2936 /* Return true if TATTR_NAME and ATTR_NAME are the same format attribute,
2937 counting "name" and "__name__" as the same, false otherwise. */
2939 cmp_attribs (const char *tattr_name, const char *attr_name)
2941 int alen = strlen (attr_name);
2942 int slen = (tattr_name ? strlen (tattr_name) : 0);
2943 if (alen > 4 && attr_name[0] == '_' && attr_name[1] == '_'
2944 && attr_name[alen - 1] == '_' && attr_name[alen - 2] == '_')
2949 if (alen != slen || strncmp (tattr_name, attr_name, alen) != 0)
2954 /* Handle a "format" attribute; arguments as in
2955 struct attribute_spec.handler. */
2957 handle_format_attribute (tree *node, tree ARG_UNUSED (name), tree args,
2958 int flags, bool *no_add_attrs)
2961 function_format_info info;
2964 #ifdef TARGET_FORMAT_TYPES
2965 /* If the target provides additional format types, we need to
2966 add them to FORMAT_TYPES at first use. */
2967 if (TARGET_FORMAT_TYPES != NULL && !dynamic_format_types)
2969 dynamic_format_types = XNEWVEC (format_kind_info,
2970 n_format_types + TARGET_N_FORMAT_TYPES);
2971 memcpy (dynamic_format_types, format_types_orig,
2972 sizeof (format_types_orig));
2973 memcpy (&dynamic_format_types[n_format_types], TARGET_FORMAT_TYPES,
2974 TARGET_N_FORMAT_TYPES * sizeof (dynamic_format_types[0]));
2976 format_types = dynamic_format_types;
2977 /* Provide a reference for the first potential external type. */
2978 first_target_format_type = n_format_types;
2979 n_format_types += TARGET_N_FORMAT_TYPES;
2983 if (!decode_format_attr (args, &info, 0))
2985 *no_add_attrs = true;
2989 argument = TYPE_ARG_TYPES (type);
2992 if (!check_format_string (argument, info.format_num, flags,
2993 no_add_attrs, info.format_type))
2996 if (info.first_arg_num != 0)
2998 unsigned HOST_WIDE_INT arg_num = 1;
3000 /* Verify that first_arg_num points to the last arg,
3003 arg_num++, argument = TREE_CHAIN (argument);
3005 if (arg_num != info.first_arg_num)
3007 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
3008 error ("args to be formatted is not %<...%>");
3009 *no_add_attrs = true;
3015 /* Check if this is a strftime variant. Just for this variant
3016 FMT_FLAG_ARG_CONVERT is not set. */
3017 if ((format_types[info.format_type].flags & (int) FMT_FLAG_ARG_CONVERT) == 0
3018 && info.first_arg_num != 0)
3020 error ("strftime formats cannot format arguments");
3021 *no_add_attrs = true;
3025 /* If this is a custom GCC-internal format type, we have to
3026 initialize certain bits at runtime. */
3027 if (info.format_type == asm_fprintf_format_type
3028 || info.format_type == gcc_gfc_format_type
3029 || info.format_type == gcc_diag_format_type
3030 || info.format_type == gcc_tdiag_format_type
3031 || info.format_type == gcc_cdiag_format_type
3032 || info.format_type == gcc_cxxdiag_format_type)
3034 /* Our first time through, we have to make sure that our
3035 format_type data is allocated dynamically and is modifiable. */
3036 if (!dynamic_format_types)
3037 format_types = dynamic_format_types = (format_kind_info *)
3038 xmemdup (format_types_orig, sizeof (format_types_orig),
3039 sizeof (format_types_orig));
3041 /* If this is format __asm_fprintf__, we have to initialize
3042 GCC's notion of HOST_WIDE_INT for checking %wd. */
3043 if (info.format_type == asm_fprintf_format_type)
3044 init_dynamic_asm_fprintf_info ();
3045 /* If this is format __gcc_gfc__, we have to initialize GCC's
3046 notion of 'locus' at runtime for %L. */
3047 else if (info.format_type == gcc_gfc_format_type)
3048 init_dynamic_gfc_info ();
3049 /* If this is one of the diagnostic attributes, then we have to
3050 initialize 'location_t' and 'tree' at runtime. */
3051 else if (info.format_type == gcc_diag_format_type
3052 || info.format_type == gcc_tdiag_format_type
3053 || info.format_type == gcc_cdiag_format_type
3054 || info.format_type == gcc_cxxdiag_format_type)
3055 init_dynamic_diag_info ();