1 /* Check calls to formatted I/O functions (-Wformat).
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "coretypes.h"
31 #include "diagnostic.h"
32 #include "langhooks.h"
34 /* Set format warning options according to a -Wformat=n option. */
40 warn_format
= setting
;
41 warn_format_y2k
= setting
;
42 warn_format_extra_args
= setting
;
43 warn_format_zero_length
= setting
;
46 warn_format_nonliteral
= setting
;
47 warn_format_security
= setting
;
49 /* Make sure not to disable -Wnonnull if -Wformat=0 is specified. */
51 warn_nonnull
= setting
;
55 /* Handle attributes associated with format checking. */
57 /* This must be in the same order as format_types, with format_type_error
59 enum format_type
{ printf_format_type
, asm_fprintf_format_type
,
60 scanf_format_type
, strftime_format_type
,
61 strfmon_format_type
, format_type_error
};
63 typedef struct function_format_info
65 enum format_type format_type
; /* type of format (printf, scanf, etc.) */
66 unsigned HOST_WIDE_INT format_num
; /* number of format argument */
67 unsigned HOST_WIDE_INT first_arg_num
; /* number of first arg (zero for varargs) */
68 } function_format_info
;
70 static bool decode_format_attr
PARAMS ((tree
,
71 function_format_info
*, int));
72 static enum format_type decode_format_type
PARAMS ((const char *));
74 static bool check_format_string (tree argument
,
75 unsigned HOST_WIDE_INT format_num
,
76 int flags
, bool *no_add_attrs
);
77 static bool get_constant (tree expr
, unsigned HOST_WIDE_INT
*value
,
81 /* Handle a "format_arg" attribute; arguments as in
82 struct attribute_spec.handler. */
84 handle_format_arg_attribute (node
, name
, args
, flags
, no_add_attrs
)
86 tree name ATTRIBUTE_UNUSED
;
92 tree format_num_expr
= TREE_VALUE (args
);
93 unsigned HOST_WIDE_INT format_num
;
96 if (!get_constant (format_num_expr
, &format_num
, 0))
98 error ("format string has invalid operand number");
103 argument
= TYPE_ARG_TYPES (type
);
106 if (!check_format_string (argument
, format_num
, flags
, no_add_attrs
))
110 if (TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
111 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type
)))
114 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
115 error ("function does not return string type");
116 *no_add_attrs
= true;
123 /* Verify that the format_num argument is actually a string, in case
124 the format attribute is in error. */
126 check_format_string (tree argument
, unsigned HOST_WIDE_INT format_num
,
127 int flags
, bool *no_add_attrs
)
129 unsigned HOST_WIDE_INT i
;
131 for (i
= 1; i
!= format_num
; i
++)
135 argument
= TREE_CHAIN (argument
);
139 || TREE_CODE (TREE_VALUE (argument
)) != POINTER_TYPE
140 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument
)))
143 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
144 error ("format string arg not a string type");
145 *no_add_attrs
= true;
152 /* Strip any conversions from the expression, verify it is a constant,
153 and store its value. If validated_p is true, abort on errors.
154 Returns true on success, false otherwise. */
156 get_constant(tree expr
, unsigned HOST_WIDE_INT
*value
, int validated_p
)
158 while (TREE_CODE (expr
) == NOP_EXPR
159 || TREE_CODE (expr
) == CONVERT_EXPR
160 || TREE_CODE (expr
) == NON_LVALUE_EXPR
)
161 expr
= TREE_OPERAND (expr
, 0);
163 if (TREE_CODE (expr
) != INTEGER_CST
|| TREE_INT_CST_HIGH (expr
) != 0)
170 *value
= TREE_INT_CST_LOW (expr
);
175 /* Decode the arguments to a "format" attribute into a function_format_info
176 structure. It is already known that the list is of the right length.
177 If VALIDATED_P is true, then these attributes have already been validated
178 and this function will abort if they are erroneous; if false, it
179 will give an error message. Returns true if the attributes are
180 successfully decoded, false otherwise. */
183 decode_format_attr (args
, info
, validated_p
)
185 function_format_info
*info
;
188 tree format_type_id
= TREE_VALUE (args
);
189 tree format_num_expr
= TREE_VALUE (TREE_CHAIN (args
));
190 tree first_arg_num_expr
191 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args
)));
193 if (TREE_CODE (format_type_id
) != IDENTIFIER_NODE
)
197 error ("unrecognized format specifier");
202 const char *p
= IDENTIFIER_POINTER (format_type_id
);
204 info
->format_type
= decode_format_type (p
);
206 if (info
->format_type
== format_type_error
)
210 warning ("`%s' is an unrecognized format function type", p
);
215 if (!get_constant (format_num_expr
, &info
->format_num
, validated_p
))
217 error ("format string has invalid operand number");
221 if (!get_constant (first_arg_num_expr
, &info
->first_arg_num
, validated_p
))
223 error ("'...' has invalid operand number");
227 if (info
->first_arg_num
!= 0 && info
->first_arg_num
<= info
->format_num
)
231 error ("format string arg follows the args to be formatted");
238 /* Check a call to a format function against a parameter list. */
240 /* The meaningfully distinct length modifiers for format checking recognized
257 /* The standard versions in which various format features appeared. */
258 enum format_std_version
262 STD_C9L
, /* C99, but treat as C89 if -Wno-long-long. */
267 /* The C standard version C++ is treated as equivalent to
268 or inheriting from, for the purpose of format features supported. */
269 #define CPLUSPLUS_STD_VER STD_C94
270 /* The C standard version we are checking formats against when pedantic. */
271 #define C_STD_VER ((int)(c_language == clk_cplusplus \
272 ? CPLUSPLUS_STD_VER \
275 : (flag_isoc94 ? STD_C94 : STD_C89))))
276 /* The name to give to the standard version we are warning about when
277 pedantic. FEATURE_VER is the version in which the feature warned out
278 appeared, which is higher than C_STD_VER. */
279 #define C_STD_NAME(FEATURE_VER) (c_language == clk_cplusplus \
281 : ((FEATURE_VER) == STD_EXT \
284 /* Adjust a C standard version, which may be STD_C9L, to account for
285 -Wno-long-long. Returns other standard versions unchanged. */
286 #define ADJ_STD(VER) ((int)((VER) == STD_C9L \
287 ? (warn_long_long ? STD_C99 : STD_C89) \
290 /* Flags that may apply to a particular kind of format checked by GCC. */
293 /* This format converts arguments of types determined by the
295 FMT_FLAG_ARG_CONVERT
= 1,
296 /* The scanf allocation 'a' kludge applies to this format kind. */
297 FMT_FLAG_SCANF_A_KLUDGE
= 2,
298 /* A % during parsing a specifier is allowed to be a modified % rather
299 that indicating the format is broken and we are out-of-sync. */
300 FMT_FLAG_FANCY_PERCENT_OK
= 4,
301 /* With $ operand numbers, it is OK to reference the same argument more
303 FMT_FLAG_DOLLAR_MULTIPLE
= 8,
304 /* This format type uses $ operand numbers (strfmon doesn't). */
305 FMT_FLAG_USE_DOLLAR
= 16,
306 /* Zero width is bad in this type of format (scanf). */
307 FMT_FLAG_ZERO_WIDTH_BAD
= 32,
308 /* Empty precision specification is OK in this type of format (printf). */
309 FMT_FLAG_EMPTY_PREC_OK
= 64,
310 /* Gaps are allowed in the arguments with $ operand numbers if all
311 arguments are pointers (scanf). */
312 FMT_FLAG_DOLLAR_GAP_POINTER_OK
= 128
313 /* Not included here: details of whether width or precision may occur
314 (controlled by width_char and precision_char); details of whether
315 '*' can be used for these (width_type and precision_type); details
316 of whether length modifiers can occur (length_char_specs). */
320 /* Structure describing a length modifier supported in format checking, and
321 possibly a doubled version such as "hh". */
324 /* Name of the single-character length modifier. */
326 /* Index into a format_char_info.types array. */
327 enum format_lengths index
;
328 /* Standard version this length appears in. */
329 enum format_std_version std
;
330 /* Same, if the modifier can be repeated, or NULL if it can't. */
331 const char *double_name
;
332 enum format_lengths double_index
;
333 enum format_std_version double_std
;
334 } format_length_info
;
337 /* Structure describing the combination of a conversion specifier
338 (or a set of specifiers which act identically) and a length modifier. */
341 /* The standard version this combination of length and type appeared in.
342 This is only relevant if greater than those for length and type
343 individually; otherwise it is ignored. */
344 enum format_std_version std
;
345 /* The name to use for the type, if different from that generated internally
346 (e.g., "signed size_t"). */
348 /* The type itself. */
350 } format_type_detail
;
353 /* Macros to fill out tables of these. */
354 #define NOARGUMENTS { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }
355 #define BADLEN { 0, NULL, NULL }
356 #define NOLENGTHS { BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }
359 /* Structure describing a format conversion specifier (or a set of specifiers
360 which act identically), and the length modifiers used with it. */
363 const char *format_chars
;
365 enum format_std_version std
;
366 /* Types accepted for each length modifier. */
367 format_type_detail types
[FMT_LEN_MAX
];
368 /* List of other modifier characters allowed with these specifiers.
369 This lists flags, and additionally "w" for width, "p" for precision
370 (right precision, for strfmon), "#" for left precision (strfmon),
371 "a" for scanf "a" allocation extension (not applicable in C99 mode),
372 "*" for scanf suppression, and "E" and "O" for those strftime
374 const char *flag_chars
;
375 /* List of additional flags describing these conversion specifiers.
376 "c" for generic character pointers being allowed, "2" for strftime
377 two digit year formats, "3" for strftime formats giving two digit
378 years in some locales, "4" for "2" which becomes "3" with an "E" modifier,
379 "o" if use of strftime "O" is a GNU extension beyond C99,
380 "W" if the argument is a pointer which is dereferenced and written into,
381 "R" if the argument is a pointer which is dereferenced and read from,
382 "i" for printf integer formats where the '0' flag is ignored with
383 precision, and "[" for the starting character of a scanf scanset. */
388 /* Structure describing a flag accepted by some kind of format. */
391 /* The flag character in question (0 for end of array). */
393 /* Zero if this entry describes the flag character in general, or a
394 nonzero character that may be found in flags2 if it describes the
395 flag when used with certain formats only. If the latter, only
396 the first such entry found that applies to the current conversion
397 specifier is used; the values of `name' and `long_name' it supplies
398 will be used, if non-NULL and the standard version is higher than
399 the unpredicated one, for any pedantic warning. For example, 'o'
400 for strftime formats (meaning 'O' is an extension over C99). */
402 /* Nonzero if the next character after this flag in the format should
403 be skipped ('=' in strfmon), zero otherwise. */
405 /* The name to use for this flag in diagnostic messages. For example,
406 N_("`0' flag"), N_("field width"). */
408 /* Long name for this flag in diagnostic messages; currently only used for
409 "ISO C does not support ...". For example, N_("the `I' printf flag"). */
410 const char *long_name
;
411 /* The standard version in which it appeared. */
412 enum format_std_version std
;
416 /* Structure describing a combination of flags that is bad for some kind
420 /* The first flag character in question (0 for end of array). */
422 /* The second flag character. */
424 /* Nonzero if the message should say that the first flag is ignored with
425 the second, zero if the combination should simply be objected to. */
427 /* Zero if this entry applies whenever this flag combination occurs,
428 a nonzero character from flags2 if it only applies in some
429 circumstances (e.g. 'i' for printf formats ignoring 0 with precision). */
434 /* Structure describing a particular kind of format processed by GCC. */
437 /* The name of this kind of format, for use in diagnostics. Also
438 the name of the attribute (without preceding and following __). */
440 /* Specifications of the length modifiers accepted; possibly NULL. */
441 const format_length_info
*length_char_specs
;
442 /* Details of the conversion specification characters accepted. */
443 const format_char_info
*conversion_specs
;
444 /* String listing the flag characters that are accepted. */
445 const char *flag_chars
;
446 /* String listing modifier characters (strftime) accepted. May be NULL. */
447 const char *modifier_chars
;
448 /* Details of the flag characters, including pseudo-flags. */
449 const format_flag_spec
*flag_specs
;
450 /* Details of bad combinations of flags. */
451 const format_flag_pair
*bad_flag_pairs
;
452 /* Flags applicable to this kind of format. */
454 /* Flag character to treat a width as, or 0 if width not used. */
456 /* Flag character to treat a left precision (strfmon) as,
457 or 0 if left precision not used. */
458 int left_precision_char
;
459 /* Flag character to treat a precision (for strfmon, right precision) as,
460 or 0 if precision not used. */
462 /* If a flag character has the effect of suppressing the conversion of
463 an argument ('*' in scanf), that flag character, otherwise 0. */
464 int suppression_char
;
465 /* Flag character to treat a length modifier as (ignored if length
466 modifiers not used). Need not be placed in flag_chars for conversion
467 specifiers, but is used to check for bad combinations such as length
468 modifier with assignment suppression in scanf. */
469 int length_code_char
;
470 /* Pointer to type of argument expected if '*' is used for a width,
471 or NULL if '*' not used for widths. */
473 /* Pointer to type of argument expected if '*' is used for a precision,
474 or NULL if '*' not used for precisions. */
475 tree
*precision_type
;
479 /* Structure describing details of a type expected in format checking,
480 and the type to check against it. */
481 typedef struct format_wanted_type
483 /* The type wanted. */
485 /* The name of this type to use in diagnostics. */
486 const char *wanted_type_name
;
487 /* The level of indirection through pointers at which this type occurs. */
489 /* Whether, when pointer_count is 1, to allow any character type when
490 pedantic, rather than just the character or void type specified. */
491 int char_lenient_flag
;
492 /* Whether the argument, dereferenced once, is written into and so the
493 argument must not be a pointer to a const-qualified type. */
495 /* Whether the argument, dereferenced once, is read from and so
496 must not be a NULL pointer. */
497 int reading_from_flag
;
498 /* If warnings should be of the form "field precision is not type int",
499 the name to use (in this case "field precision"), otherwise NULL,
500 for "%s format, %s arg" type messages. If (in an extension), this
501 is a pointer type, wanted_type_name should be set to include the
502 terminating '*' characters of the type name to give a correct
505 /* The actual parameter to check against the wanted type. */
507 /* The argument number of that parameter. */
509 /* The next type to check for this format conversion, or NULL if none. */
510 struct format_wanted_type
*next
;
511 } format_wanted_type
;
514 static const format_length_info printf_length_specs
[] =
516 { "h", FMT_LEN_h
, STD_C89
, "hh", FMT_LEN_hh
, STD_C99
},
517 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C9L
},
518 { "q", FMT_LEN_ll
, STD_EXT
, NULL
, 0, 0 },
519 { "L", FMT_LEN_L
, STD_C89
, NULL
, 0, 0 },
520 { "z", FMT_LEN_z
, STD_C99
, NULL
, 0, 0 },
521 { "Z", FMT_LEN_z
, STD_EXT
, NULL
, 0, 0 },
522 { "t", FMT_LEN_t
, STD_C99
, NULL
, 0, 0 },
523 { "j", FMT_LEN_j
, STD_C99
, NULL
, 0, 0 },
524 { NULL
, 0, 0, NULL
, 0, 0 }
527 /* Length specifiers valid for asm_fprintf. */
528 static const format_length_info asm_fprintf_length_specs
[] =
530 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C89
},
531 { "w", FMT_LEN_none
, STD_C89
, NULL
, 0, 0 },
532 { NULL
, 0, 0, NULL
, 0, 0 }
535 /* This differs from printf_length_specs only in that "Z" is not accepted. */
536 static const format_length_info scanf_length_specs
[] =
538 { "h", FMT_LEN_h
, STD_C89
, "hh", FMT_LEN_hh
, STD_C99
},
539 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C9L
},
540 { "q", FMT_LEN_ll
, STD_EXT
, NULL
, 0, 0 },
541 { "L", FMT_LEN_L
, STD_C89
, NULL
, 0, 0 },
542 { "z", FMT_LEN_z
, STD_C99
, NULL
, 0, 0 },
543 { "t", FMT_LEN_t
, STD_C99
, NULL
, 0, 0 },
544 { "j", FMT_LEN_j
, STD_C99
, NULL
, 0, 0 },
545 { NULL
, 0, 0, NULL
, 0, 0 }
549 /* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings
550 make no sense for a format type not part of any C standard version. */
551 static const format_length_info strfmon_length_specs
[] =
553 /* A GNU extension. */
554 { "L", FMT_LEN_L
, STD_C89
, NULL
, 0, 0 },
555 { NULL
, 0, 0, NULL
, 0, 0 }
558 static const format_flag_spec printf_flag_specs
[] =
560 { ' ', 0, 0, N_("` ' flag"), N_("the ` ' printf flag"), STD_C89
},
561 { '+', 0, 0, N_("`+' flag"), N_("the `+' printf flag"), STD_C89
},
562 { '#', 0, 0, N_("`#' flag"), N_("the `#' printf flag"), STD_C89
},
563 { '0', 0, 0, N_("`0' flag"), N_("the `0' printf flag"), STD_C89
},
564 { '-', 0, 0, N_("`-' flag"), N_("the `-' printf flag"), STD_C89
},
565 { '\'', 0, 0, N_("`'' flag"), N_("the `'' printf flag"), STD_EXT
},
566 { 'I', 0, 0, N_("`I' flag"), N_("the `I' printf flag"), STD_EXT
},
567 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89
},
568 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89
},
569 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89
},
570 { 0, 0, 0, NULL
, NULL
, 0 }
574 static const format_flag_pair printf_flag_pairs
[] =
578 { '0', 'p', 1, 'i' },
582 static const format_flag_spec asm_fprintf_flag_specs
[] =
584 { ' ', 0, 0, N_("` ' flag"), N_("the ` ' printf flag"), STD_C89
},
585 { '+', 0, 0, N_("`+' flag"), N_("the `+' printf flag"), STD_C89
},
586 { '#', 0, 0, N_("`#' flag"), N_("the `#' printf flag"), STD_C89
},
587 { '0', 0, 0, N_("`0' flag"), N_("the `0' printf flag"), STD_C89
},
588 { '-', 0, 0, N_("`-' flag"), N_("the `-' printf flag"), STD_C89
},
589 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89
},
590 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89
},
591 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89
},
592 { 0, 0, 0, NULL
, NULL
, 0 }
595 static const format_flag_pair asm_fprintf_flag_pairs
[] =
599 { '0', 'p', 1, 'i' },
603 static const format_flag_spec scanf_flag_specs
[] =
605 { '*', 0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89
},
606 { 'a', 0, 0, N_("`a' flag"), N_("the `a' scanf flag"), STD_EXT
},
607 { 'w', 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89
},
608 { 'L', 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89
},
609 { '\'', 0, 0, N_("`'' flag"), N_("the `'' scanf flag"), STD_EXT
},
610 { 'I', 0, 0, N_("`I' flag"), N_("the `I' scanf flag"), STD_EXT
},
611 { 0, 0, 0, NULL
, NULL
, 0 }
615 static const format_flag_pair scanf_flag_pairs
[] =
622 static const format_flag_spec strftime_flag_specs
[] =
624 { '_', 0, 0, N_("`_' flag"), N_("the `_' strftime flag"), STD_EXT
},
625 { '-', 0, 0, N_("`-' flag"), N_("the `-' strftime flag"), STD_EXT
},
626 { '0', 0, 0, N_("`0' flag"), N_("the `0' strftime flag"), STD_EXT
},
627 { '^', 0, 0, N_("`^' flag"), N_("the `^' strftime flag"), STD_EXT
},
628 { '#', 0, 0, N_("`#' flag"), N_("the `#' strftime flag"), STD_EXT
},
629 { 'w', 0, 0, N_("field width"), N_("field width in strftime format"), STD_EXT
},
630 { 'E', 0, 0, N_("`E' modifier"), N_("the `E' strftime modifier"), STD_C99
},
631 { 'O', 0, 0, N_("`O' modifier"), N_("the `O' strftime modifier"), STD_C99
},
632 { 'O', 'o', 0, NULL
, N_("the `O' modifier"), STD_EXT
},
633 { 0, 0, 0, NULL
, NULL
, 0 }
637 static const format_flag_pair strftime_flag_pairs
[] =
648 static const format_flag_spec strfmon_flag_specs
[] =
650 { '=', 0, 1, N_("fill character"), N_("fill character in strfmon format"), STD_C89
},
651 { '^', 0, 0, N_("`^' flag"), N_("the `^' strfmon flag"), STD_C89
},
652 { '+', 0, 0, N_("`+' flag"), N_("the `+' strfmon flag"), STD_C89
},
653 { '(', 0, 0, N_("`(' flag"), N_("the `(' strfmon flag"), STD_C89
},
654 { '!', 0, 0, N_("`!' flag"), N_("the `!' strfmon flag"), STD_C89
},
655 { '-', 0, 0, N_("`-' flag"), N_("the `-' strfmon flag"), STD_C89
},
656 { 'w', 0, 0, N_("field width"), N_("field width in strfmon format"), STD_C89
},
657 { '#', 0, 0, N_("left precision"), N_("left precision in strfmon format"), STD_C89
},
658 { 'p', 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89
},
659 { 'L', 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89
},
660 { 0, 0, 0, NULL
, NULL
, 0 }
663 static const format_flag_pair strfmon_flag_pairs
[] =
670 #define T_I &integer_type_node
671 #define T89_I { STD_C89, NULL, T_I }
672 #define T_L &long_integer_type_node
673 #define T89_L { STD_C89, NULL, T_L }
674 #define T_LL &long_long_integer_type_node
675 #define T9L_LL { STD_C9L, NULL, T_LL }
676 #define TEX_LL { STD_EXT, NULL, T_LL }
677 #define T_S &short_integer_type_node
678 #define T89_S { STD_C89, NULL, T_S }
679 #define T_UI &unsigned_type_node
680 #define T89_UI { STD_C89, NULL, T_UI }
681 #define T_UL &long_unsigned_type_node
682 #define T89_UL { STD_C89, NULL, T_UL }
683 #define T_ULL &long_long_unsigned_type_node
684 #define T9L_ULL { STD_C9L, NULL, T_ULL }
685 #define TEX_ULL { STD_EXT, NULL, T_ULL }
686 #define T_US &short_unsigned_type_node
687 #define T89_US { STD_C89, NULL, T_US }
688 #define T_F &float_type_node
689 #define T89_F { STD_C89, NULL, T_F }
690 #define T99_F { STD_C99, NULL, T_F }
691 #define T_D &double_type_node
692 #define T89_D { STD_C89, NULL, T_D }
693 #define T99_D { STD_C99, NULL, T_D }
694 #define T_LD &long_double_type_node
695 #define T89_LD { STD_C89, NULL, T_LD }
696 #define T99_LD { STD_C99, NULL, T_LD }
697 #define T_C &char_type_node
698 #define T89_C { STD_C89, NULL, T_C }
699 #define T_SC &signed_char_type_node
700 #define T99_SC { STD_C99, NULL, T_SC }
701 #define T_UC &unsigned_char_type_node
702 #define T99_UC { STD_C99, NULL, T_UC }
703 #define T_V &void_type_node
704 #define T89_V { STD_C89, NULL, T_V }
705 #define T_W &wchar_type_node
706 #define T94_W { STD_C94, "wchar_t", T_W }
707 #define TEX_W { STD_EXT, "wchar_t", T_W }
708 #define T_WI &wint_type_node
709 #define T94_WI { STD_C94, "wint_t", T_WI }
710 #define TEX_WI { STD_EXT, "wint_t", T_WI }
711 #define T_ST &size_type_node
712 #define T99_ST { STD_C99, "size_t", T_ST }
713 #define T_SST &signed_size_type_node
714 #define T99_SST { STD_C99, "signed size_t", T_SST }
715 #define T_PD &ptrdiff_type_node
716 #define T99_PD { STD_C99, "ptrdiff_t", T_PD }
717 #define T_UPD &unsigned_ptrdiff_type_node
718 #define T99_UPD { STD_C99, "unsigned ptrdiff_t", T_UPD }
719 #define T_IM &intmax_type_node
720 #define T99_IM { STD_C99, "intmax_t", T_IM }
721 #define T_UIM &uintmax_type_node
722 #define T99_UIM { STD_C99, "uintmax_t", T_UIM }
724 static const format_char_info print_char_table
[] =
726 /* C89 conversion specifiers. */
727 { "di", 0, STD_C89
, { T89_I
, T99_SC
, T89_S
, T89_L
, T9L_LL
, TEX_LL
, T99_SST
, T99_PD
, T99_IM
}, "-wp0 +'I", "i" },
728 { "oxX", 0, STD_C89
, { T89_UI
, T99_UC
, T89_US
, T89_UL
, T9L_ULL
, TEX_ULL
, T99_ST
, T99_UPD
, T99_UIM
}, "-wp0#", "i" },
729 { "u", 0, STD_C89
, { T89_UI
, T99_UC
, T89_US
, T89_UL
, T9L_ULL
, TEX_ULL
, T99_ST
, T99_UPD
, T99_UIM
}, "-wp0'I", "i" },
730 { "fgG", 0, STD_C89
, { T89_D
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T89_LD
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +#'", "" },
731 { "eE", 0, STD_C89
, { T89_D
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T89_LD
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +#", "" },
732 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T94_WI
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "" },
733 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "cR" },
734 { "p", 1, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "c" },
735 { "n", 1, STD_C89
, { T89_I
, T99_SC
, T89_S
, T89_L
, T9L_LL
, BADLEN
, T99_SST
, T99_PD
, T99_IM
}, "", "W" },
736 /* C99 conversion specifiers. */
737 { "F", 0, STD_C99
, { T99_D
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T99_LD
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +#'", "" },
738 { "aA", 0, STD_C99
, { T99_D
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T99_LD
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +#", "" },
739 /* X/Open conversion specifiers. */
740 { "C", 0, STD_EXT
, { TEX_WI
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "" },
741 { "S", 1, STD_EXT
, { TEX_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "R" },
742 /* GNU conversion specifiers. */
743 { "m", 0, STD_EXT
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "" },
744 { NULL
, 0, 0, NOLENGTHS
, NULL
, NULL
}
747 static const format_char_info asm_fprintf_char_table
[] =
749 /* C89 conversion specifiers. */
750 { "di", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T89_L
, T9L_LL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +", "i" },
751 { "oxX", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0#", "i" },
752 { "u", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0", "i" },
753 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "" },
754 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "cR" },
756 /* asm_fprintf conversion specifiers. */
757 { "O", 0, STD_C89
, NOARGUMENTS
, "", "" },
758 { "R", 0, STD_C89
, NOARGUMENTS
, "", "" },
759 { "I", 0, STD_C89
, NOARGUMENTS
, "", "" },
760 { "L", 0, STD_C89
, NOARGUMENTS
, "", "" },
761 { "U", 0, STD_C89
, NOARGUMENTS
, "", "" },
762 { "r", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "" },
763 { "@", 0, STD_C89
, NOARGUMENTS
, "", "" },
764 { NULL
, 0, 0, NOLENGTHS
, NULL
, NULL
}
767 static const format_char_info scan_char_table
[] =
769 /* C89 conversion specifiers. */
770 { "di", 1, STD_C89
, { T89_I
, T99_SC
, T89_S
, T89_L
, T9L_LL
, TEX_LL
, T99_SST
, T99_PD
, T99_IM
}, "*w'I", "W" },
771 { "u", 1, STD_C89
, { T89_UI
, T99_UC
, T89_US
, T89_UL
, T9L_ULL
, TEX_ULL
, T99_ST
, T99_UPD
, T99_UIM
}, "*w'I", "W" },
772 { "oxX", 1, STD_C89
, { T89_UI
, T99_UC
, T89_US
, T89_UL
, T9L_ULL
, TEX_ULL
, T99_ST
, T99_UPD
, T99_UIM
}, "*w", "W" },
773 { "efgEG", 1, STD_C89
, { T89_F
, BADLEN
, BADLEN
, T89_D
, BADLEN
, T89_LD
, BADLEN
, BADLEN
, BADLEN
}, "*w'", "W" },
774 { "c", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*w", "cW" },
775 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*aw", "cW" },
776 { "[", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*aw", "cW[" },
777 { "p", 2, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*w", "W" },
778 { "n", 1, STD_C89
, { T89_I
, T99_SC
, T89_S
, T89_L
, T9L_LL
, BADLEN
, T99_SST
, T99_PD
, T99_IM
}, "", "W" },
779 /* C99 conversion specifiers. */
780 { "FaA", 1, STD_C99
, { T99_F
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T99_LD
, BADLEN
, BADLEN
, BADLEN
}, "*w'", "W" },
781 /* X/Open conversion specifiers. */
782 { "C", 1, STD_EXT
, { TEX_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*w", "W" },
783 { "S", 1, STD_EXT
, { TEX_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*aw", "W" },
784 { NULL
, 0, 0, NOLENGTHS
, NULL
, NULL
}
787 static const format_char_info time_char_table
[] =
789 /* C89 conversion specifiers. */
790 { "ABZab", 0, STD_C89
, NOLENGTHS
, "^#", "" },
791 { "cx", 0, STD_C89
, NOLENGTHS
, "E", "3" },
792 { "HIMSUWdmw", 0, STD_C89
, NOLENGTHS
, "-_0Ow", "" },
793 { "j", 0, STD_C89
, NOLENGTHS
, "-_0Ow", "o" },
794 { "p", 0, STD_C89
, NOLENGTHS
, "#", "" },
795 { "X", 0, STD_C89
, NOLENGTHS
, "E", "" },
796 { "y", 0, STD_C89
, NOLENGTHS
, "EO-_0w", "4" },
797 { "Y", 0, STD_C89
, NOLENGTHS
, "-_0EOw", "o" },
798 { "%", 0, STD_C89
, NOLENGTHS
, "", "" },
799 /* C99 conversion specifiers. */
800 { "C", 0, STD_C99
, NOLENGTHS
, "-_0EOw", "o" },
801 { "D", 0, STD_C99
, NOLENGTHS
, "", "2" },
802 { "eVu", 0, STD_C99
, NOLENGTHS
, "-_0Ow", "" },
803 { "FRTnrt", 0, STD_C99
, NOLENGTHS
, "", "" },
804 { "g", 0, STD_C99
, NOLENGTHS
, "O-_0w", "2o" },
805 { "G", 0, STD_C99
, NOLENGTHS
, "-_0Ow", "o" },
806 { "h", 0, STD_C99
, NOLENGTHS
, "^#", "" },
807 { "z", 0, STD_C99
, NOLENGTHS
, "O", "o" },
808 /* GNU conversion specifiers. */
809 { "kls", 0, STD_EXT
, NOLENGTHS
, "-_0Ow", "" },
810 { "P", 0, STD_EXT
, NOLENGTHS
, "", "" },
811 { NULL
, 0, 0, NOLENGTHS
, NULL
, NULL
}
814 static const format_char_info monetary_char_table
[] =
816 { "in", 0, STD_C89
, { T89_D
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, T89_LD
, BADLEN
, BADLEN
, BADLEN
}, "=^+(!-w#p", "" },
817 { NULL
, 0, 0, NOLENGTHS
, NULL
, NULL
}
821 /* This must be in the same order as enum format_type. */
822 static const format_kind_info format_types_orig
[] =
824 { "printf", printf_length_specs
, print_char_table
, " +#0-'I", NULL
,
825 printf_flag_specs
, printf_flag_pairs
,
826 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_DOLLAR_MULTIPLE
|FMT_FLAG_USE_DOLLAR
|FMT_FLAG_EMPTY_PREC_OK
,
828 &integer_type_node
, &integer_type_node
830 { "asm_fprintf", asm_fprintf_length_specs
, asm_fprintf_char_table
, " +#0-", NULL
,
831 asm_fprintf_flag_specs
, asm_fprintf_flag_pairs
,
832 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_EMPTY_PREC_OK
,
836 { "scanf", scanf_length_specs
, scan_char_table
, "*'I", NULL
,
837 scanf_flag_specs
, scanf_flag_pairs
,
838 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_SCANF_A_KLUDGE
|FMT_FLAG_USE_DOLLAR
|FMT_FLAG_ZERO_WIDTH_BAD
|FMT_FLAG_DOLLAR_GAP_POINTER_OK
,
842 { "strftime", NULL
, time_char_table
, "_-0^#", "EO",
843 strftime_flag_specs
, strftime_flag_pairs
,
844 FMT_FLAG_FANCY_PERCENT_OK
, 'w', 0, 0, 0, 0,
847 { "strfmon", strfmon_length_specs
, monetary_char_table
, "=^+(!-", NULL
,
848 strfmon_flag_specs
, strfmon_flag_pairs
,
849 FMT_FLAG_ARG_CONVERT
, 'w', '#', 'p', 0, 'L',
854 /* This layer of indirection allows GCC to reassign format_types with
855 new data if necessary, while still allowing the original data to be
857 static const format_kind_info
*format_types
= format_types_orig
;
859 /* Structure detailing the results of checking a format function call
860 where the format expression may be a conditional expression with
861 many leaves resulting from nested conditional expressions. */
864 /* Number of leaves of the format argument that could not be checked
865 as they were not string literals. */
866 int number_non_literal
;
867 /* Number of leaves of the format argument that were null pointers or
868 string literals, but had extra format arguments. */
869 int number_extra_args
;
870 /* Number of leaves of the format argument that were null pointers or
871 string literals, but had extra format arguments and used $ operand
873 int number_dollar_extra_args
;
874 /* Number of leaves of the format argument that were wide string
877 /* Number of leaves of the format argument that were empty strings. */
879 /* Number of leaves of the format argument that were unterminated
881 int number_unterminated
;
882 /* Number of leaves of the format argument that were not counted above. */
884 } format_check_results
;
888 format_check_results
*res
;
889 function_format_info
*info
;
892 } format_check_context
;
894 static void check_format_info
PARAMS ((int *, function_format_info
*, tree
));
895 static void check_format_arg
PARAMS ((void *, tree
, unsigned HOST_WIDE_INT
));
896 static void check_format_info_main
PARAMS ((int *, format_check_results
*,
897 function_format_info
*,
898 const char *, int, tree
,
899 unsigned HOST_WIDE_INT
));
900 static void status_warning
PARAMS ((int *, const char *, ...))
903 static void init_dollar_format_checking
PARAMS ((int, tree
));
904 static int maybe_read_dollar_number
PARAMS ((int *, const char **, int,
906 const format_kind_info
*));
907 static void finish_dollar_format_checking
PARAMS ((int *, format_check_results
*, int));
909 static const format_flag_spec
*get_flag_spec
PARAMS ((const format_flag_spec
*,
912 static void check_format_types
PARAMS ((int *, format_wanted_type
*));
914 /* Decode a format type from a string, returning the type, or
915 format_type_error if not valid, in which case the caller should print an
917 static enum format_type
918 decode_format_type (s
)
924 for (i
= 0; i
< (int) format_type_error
; i
++)
927 if (!strcmp (s
, format_types
[i
].name
))
929 alen
= strlen (format_types
[i
].name
);
930 if (slen
== alen
+ 4 && s
[0] == '_' && s
[1] == '_'
931 && s
[slen
- 1] == '_' && s
[slen
- 2] == '_'
932 && !strncmp (s
+ 2, format_types
[i
].name
, alen
))
935 return ((enum format_type
) i
);
939 /* Check the argument list of a call to printf, scanf, etc.
940 ATTRS are the attributes on the function type.
941 PARAMS is the list of argument values. Also, if -Wmissing-format-attribute,
942 warn for calls to vprintf or vscanf in functions with no such format
943 attribute themselves. */
946 check_function_format (status
, attrs
, params
)
953 /* See if this function has any format attributes. */
954 for (a
= attrs
; a
; a
= TREE_CHAIN (a
))
956 if (is_attribute_p ("format", TREE_PURPOSE (a
)))
959 function_format_info info
;
960 decode_format_attr (TREE_VALUE (a
), &info
, 1);
961 check_format_info (status
, &info
, params
);
962 if (warn_missing_format_attribute
&& info
.first_arg_num
== 0
963 && (format_types
[info
.format_type
].flags
964 & (int) FMT_FLAG_ARG_CONVERT
))
967 for (c
= TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl
));
970 if (is_attribute_p ("format", TREE_PURPOSE (c
))
971 && (decode_format_type (IDENTIFIER_POINTER
972 (TREE_VALUE (TREE_VALUE (c
))))
973 == info
.format_type
))
977 /* Check if the current function has a parameter to which
978 the format attribute could be attached; if not, it
979 can't be a candidate for a format attribute, despite
980 the vprintf-like or vscanf-like call. */
982 for (args
= DECL_ARGUMENTS (current_function_decl
);
984 args
= TREE_CHAIN (args
))
986 if (TREE_CODE (TREE_TYPE (args
)) == POINTER_TYPE
987 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args
)))
992 warning ("function might be possible candidate for `%s' format attribute",
993 format_types
[info
.format_type
].name
);
1000 /* This function replaces `warning' inside the printf format checking
1001 functions. If the `status' parameter is non-NULL, then it is
1002 dereferenced and set to 1 whenever a warning is caught. Otherwise
1003 it warns as usual by replicating the innards of the warning
1004 function from diagnostic.c. */
1006 status_warning (int *status
, const char *msgid
, ...)
1008 diagnostic_info diagnostic
;
1011 va_start (ap
, msgid
);
1017 /* This duplicates the warning function behavior. */
1018 diagnostic_set_info (&diagnostic
, _(msgid
), &ap
,
1019 input_filename
, input_line
, DK_WARNING
);
1020 report_diagnostic (&diagnostic
);
1026 /* Variables used by the checking of $ operand number formats. */
1027 static char *dollar_arguments_used
= NULL
;
1028 static char *dollar_arguments_pointer_p
= NULL
;
1029 static int dollar_arguments_alloc
= 0;
1030 static int dollar_arguments_count
;
1031 static int dollar_first_arg_num
;
1032 static int dollar_max_arg_used
;
1033 static int dollar_format_warned
;
1035 /* Initialize the checking for a format string that may contain $
1036 parameter number specifications; we will need to keep track of whether
1037 each parameter has been used. FIRST_ARG_NUM is the number of the first
1038 argument that is a parameter to the format, or 0 for a vprintf-style
1039 function; PARAMS is the list of arguments starting at this argument. */
1042 init_dollar_format_checking (first_arg_num
, params
)
1046 tree oparams
= params
;
1048 dollar_first_arg_num
= first_arg_num
;
1049 dollar_arguments_count
= 0;
1050 dollar_max_arg_used
= 0;
1051 dollar_format_warned
= 0;
1052 if (first_arg_num
> 0)
1056 dollar_arguments_count
++;
1057 params
= TREE_CHAIN (params
);
1060 if (dollar_arguments_alloc
< dollar_arguments_count
)
1062 if (dollar_arguments_used
)
1063 free (dollar_arguments_used
);
1064 if (dollar_arguments_pointer_p
)
1065 free (dollar_arguments_pointer_p
);
1066 dollar_arguments_alloc
= dollar_arguments_count
;
1067 dollar_arguments_used
= xmalloc (dollar_arguments_alloc
);
1068 dollar_arguments_pointer_p
= xmalloc (dollar_arguments_alloc
);
1070 if (dollar_arguments_alloc
)
1072 memset (dollar_arguments_used
, 0, dollar_arguments_alloc
);
1073 if (first_arg_num
> 0)
1079 dollar_arguments_pointer_p
[i
] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params
)))
1081 params
= TREE_CHAIN (params
);
1089 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
1090 is set, it is an error if one is not found; otherwise, it is OK. If
1091 such a number is found, check whether it is within range and mark that
1092 numbered operand as being used for later checking. Returns the operand
1093 number if found and within range, zero if no such number was found and
1094 this is OK, or -1 on error. PARAMS points to the first operand of the
1095 format; PARAM_PTR is made to point to the parameter referred to. If
1096 a $ format is found, *FORMAT is updated to point just after it. */
1099 maybe_read_dollar_number (status
, format
, dollar_needed
, params
, param_ptr
,
1102 const char **format
;
1106 const format_kind_info
*fki
;
1110 const char *fcp
= *format
;
1111 if (! ISDIGIT (*fcp
))
1115 status_warning (status
, "missing $ operand number in format");
1123 while (ISDIGIT (*fcp
))
1126 nargnum
= 10 * argnum
+ (*fcp
- '0');
1127 if (nargnum
< 0 || nargnum
/ 10 != argnum
)
1136 status_warning (status
, "missing $ operand number in format");
1143 if (pedantic
&& !dollar_format_warned
)
1145 status_warning (status
,
1146 "%s does not support %%n$ operand number formats",
1147 C_STD_NAME (STD_EXT
));
1148 dollar_format_warned
= 1;
1150 if (overflow_flag
|| argnum
== 0
1151 || (dollar_first_arg_num
&& argnum
> dollar_arguments_count
))
1153 status_warning (status
, "operand number out of range in format");
1156 if (argnum
> dollar_max_arg_used
)
1157 dollar_max_arg_used
= argnum
;
1158 /* For vprintf-style functions we may need to allocate more memory to
1159 track which arguments are used. */
1160 while (dollar_arguments_alloc
< dollar_max_arg_used
)
1163 nalloc
= 2 * dollar_arguments_alloc
+ 16;
1164 dollar_arguments_used
= xrealloc (dollar_arguments_used
, nalloc
);
1165 dollar_arguments_pointer_p
= xrealloc (dollar_arguments_pointer_p
,
1167 memset (dollar_arguments_used
+ dollar_arguments_alloc
, 0,
1168 nalloc
- dollar_arguments_alloc
);
1169 dollar_arguments_alloc
= nalloc
;
1171 if (!(fki
->flags
& (int) FMT_FLAG_DOLLAR_MULTIPLE
)
1172 && dollar_arguments_used
[argnum
- 1] == 1)
1174 dollar_arguments_used
[argnum
- 1] = 2;
1175 status_warning (status
,
1176 "format argument %d used more than once in %s format",
1180 dollar_arguments_used
[argnum
- 1] = 1;
1181 if (dollar_first_arg_num
)
1184 *param_ptr
= params
;
1185 for (i
= 1; i
< argnum
&& *param_ptr
!= 0; i
++)
1186 *param_ptr
= TREE_CHAIN (*param_ptr
);
1188 if (*param_ptr
== 0)
1190 /* This case shouldn't be caught here. */
1200 /* Finish the checking for a format string that used $ operand number formats
1201 instead of non-$ formats. We check for unused operands before used ones
1202 (a serious error, since the implementation of the format function
1203 can't know what types to pass to va_arg to find the later arguments).
1204 and for unused operands at the end of the format (if we know how many
1205 arguments the format had, so not for vprintf). If there were operand
1206 numbers out of range on a non-vprintf-style format, we won't have reached
1207 here. If POINTER_GAP_OK, unused arguments are OK if all arguments are
1211 finish_dollar_format_checking (status
, res
, pointer_gap_ok
)
1213 format_check_results
*res
;
1217 bool found_pointer_gap
= false;
1218 for (i
= 0; i
< dollar_max_arg_used
; i
++)
1220 if (!dollar_arguments_used
[i
])
1222 if (pointer_gap_ok
&& (dollar_first_arg_num
== 0
1223 || dollar_arguments_pointer_p
[i
]))
1224 found_pointer_gap
= true;
1226 status_warning (status
, "format argument %d unused before used argument %d in $-style format",
1227 i
+ 1, dollar_max_arg_used
);
1230 if (found_pointer_gap
1231 || (dollar_first_arg_num
1232 && dollar_max_arg_used
< dollar_arguments_count
))
1234 res
->number_other
--;
1235 res
->number_dollar_extra_args
++;
1240 /* Retrieve the specification for a format flag. SPEC contains the
1241 specifications for format flags for the applicable kind of format.
1242 FLAG is the flag in question. If PREDICATES is NULL, the basic
1243 spec for that flag must be retrieved and this function aborts if
1244 it cannot be found. If PREDICATES is not NULL, it is a string listing
1245 possible predicates for the spec entry; if an entry predicated on any
1246 of these is found, it is returned, otherwise NULL is returned. */
1248 static const format_flag_spec
*
1249 get_flag_spec (spec
, flag
, predicates
)
1250 const format_flag_spec
*spec
;
1252 const char *predicates
;
1255 for (i
= 0; spec
[i
].flag_char
!= 0; i
++)
1257 if (spec
[i
].flag_char
!= flag
)
1259 if (predicates
!= NULL
)
1261 if (spec
[i
].predicate
!= 0
1262 && strchr (predicates
, spec
[i
].predicate
) != 0)
1265 else if (spec
[i
].predicate
== 0)
1268 if (predicates
== NULL
)
1275 /* Check the argument list of a call to printf, scanf, etc.
1276 INFO points to the function_format_info structure.
1277 PARAMS is the list of argument values. */
1280 check_format_info (status
, info
, params
)
1282 function_format_info
*info
;
1285 format_check_context format_ctx
;
1286 unsigned HOST_WIDE_INT arg_num
;
1288 format_check_results res
;
1289 /* Skip to format argument. If the argument isn't available, there's
1290 no work for us to do; prototype checking will catch the problem. */
1291 for (arg_num
= 1; ; ++arg_num
)
1295 if (arg_num
== info
->format_num
)
1297 params
= TREE_CHAIN (params
);
1299 format_tree
= TREE_VALUE (params
);
1300 params
= TREE_CHAIN (params
);
1301 if (format_tree
== 0)
1304 res
.number_non_literal
= 0;
1305 res
.number_extra_args
= 0;
1306 res
.number_dollar_extra_args
= 0;
1307 res
.number_wide
= 0;
1308 res
.number_empty
= 0;
1309 res
.number_unterminated
= 0;
1310 res
.number_other
= 0;
1312 format_ctx
.res
= &res
;
1313 format_ctx
.info
= info
;
1314 format_ctx
.params
= params
;
1315 format_ctx
.status
= status
;
1317 check_function_arguments_recurse (check_format_arg
, &format_ctx
,
1318 format_tree
, arg_num
);
1320 if (res
.number_non_literal
> 0)
1322 /* Functions taking a va_list normally pass a non-literal format
1323 string. These functions typically are declared with
1324 first_arg_num == 0, so avoid warning in those cases. */
1325 if (!(format_types
[info
->format_type
].flags
& (int) FMT_FLAG_ARG_CONVERT
))
1327 /* For strftime-like formats, warn for not checking the format
1328 string; but there are no arguments to check. */
1329 if (warn_format_nonliteral
)
1330 status_warning (status
, "format not a string literal, format string not checked");
1332 else if (info
->first_arg_num
!= 0)
1334 /* If there are no arguments for the format at all, we may have
1335 printf (foo) which is likely to be a security hole. */
1336 while (arg_num
+ 1 < info
->first_arg_num
)
1340 params
= TREE_CHAIN (params
);
1343 if (params
== 0 && (warn_format_nonliteral
|| warn_format_security
))
1344 status_warning (status
, "format not a string literal and no format arguments");
1345 else if (warn_format_nonliteral
)
1346 status_warning (status
, "format not a string literal, argument types not checked");
1350 /* If there were extra arguments to the format, normally warn. However,
1351 the standard does say extra arguments are ignored, so in the specific
1352 case where we have multiple leaves (conditional expressions or
1353 ngettext) allow extra arguments if at least one leaf didn't have extra
1354 arguments, but was otherwise OK (either non-literal or checked OK).
1355 If the format is an empty string, this should be counted similarly to the
1356 case of extra format arguments. */
1357 if (res
.number_extra_args
> 0 && res
.number_non_literal
== 0
1358 && res
.number_other
== 0 && warn_format_extra_args
)
1359 status_warning (status
, "too many arguments for format");
1360 if (res
.number_dollar_extra_args
> 0 && res
.number_non_literal
== 0
1361 && res
.number_other
== 0 && warn_format_extra_args
)
1362 status_warning (status
, "unused arguments in $-style format");
1363 if (res
.number_empty
> 0 && res
.number_non_literal
== 0
1364 && res
.number_other
== 0 && warn_format_zero_length
)
1365 status_warning (status
, "zero-length %s format string",
1366 format_types
[info
->format_type
].name
);
1368 if (res
.number_wide
> 0)
1369 status_warning (status
, "format is a wide character string");
1371 if (res
.number_unterminated
> 0)
1372 status_warning (status
, "unterminated format string");
1375 /* Callback from check_function_arguments_recurse to check a
1376 format string. FORMAT_TREE is the format parameter. ARG_NUM
1377 is the number of the format argument. CTX points to a
1378 format_check_context. */
1381 check_format_arg (ctx
, format_tree
, arg_num
)
1384 unsigned HOST_WIDE_INT arg_num
;
1386 format_check_context
*format_ctx
= ctx
;
1387 format_check_results
*res
= format_ctx
->res
;
1388 function_format_info
*info
= format_ctx
->info
;
1389 tree params
= format_ctx
->params
;
1390 int *status
= format_ctx
->status
;
1393 HOST_WIDE_INT offset
;
1394 const char *format_chars
;
1395 tree array_size
= 0;
1398 if (integer_zerop (format_tree
))
1400 /* Skip to first argument to check, so we can see if this format
1401 has any arguments (it shouldn't). */
1402 while (arg_num
+ 1 < info
->first_arg_num
)
1406 params
= TREE_CHAIN (params
);
1411 res
->number_other
++;
1413 res
->number_extra_args
++;
1419 if (TREE_CODE (format_tree
) == PLUS_EXPR
)
1423 arg0
= TREE_OPERAND (format_tree
, 0);
1424 arg1
= TREE_OPERAND (format_tree
, 1);
1427 if (TREE_CODE (arg1
) == INTEGER_CST
)
1429 else if (TREE_CODE (arg0
) == INTEGER_CST
)
1436 res
->number_non_literal
++;
1439 if (!host_integerp (arg1
, 0)
1440 || (offset
= tree_low_cst (arg1
, 0)) < 0)
1442 res
->number_non_literal
++;
1446 if (TREE_CODE (format_tree
) != ADDR_EXPR
)
1448 res
->number_non_literal
++;
1451 format_tree
= TREE_OPERAND (format_tree
, 0);
1452 if (TREE_CODE (format_tree
) == VAR_DECL
1453 && TREE_CODE (TREE_TYPE (format_tree
)) == ARRAY_TYPE
1454 && (array_init
= decl_constant_value (format_tree
)) != format_tree
1455 && TREE_CODE (array_init
) == STRING_CST
)
1457 /* Extract the string constant initializer. Note that this may include
1458 a trailing NUL character that is not in the array (e.g.
1459 const char a[3] = "foo";). */
1460 array_size
= DECL_SIZE_UNIT (format_tree
);
1461 format_tree
= array_init
;
1463 if (TREE_CODE (format_tree
) != STRING_CST
)
1465 res
->number_non_literal
++;
1468 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree
))) != char_type_node
)
1473 format_chars
= TREE_STRING_POINTER (format_tree
);
1474 format_length
= TREE_STRING_LENGTH (format_tree
);
1475 if (array_size
!= 0)
1477 /* Variable length arrays can't be initialized. */
1478 if (TREE_CODE (array_size
) != INTEGER_CST
)
1480 if (host_integerp (array_size
, 0))
1482 HOST_WIDE_INT array_size_value
= TREE_INT_CST_LOW (array_size
);
1483 if (array_size_value
> 0
1484 && array_size_value
== (int) array_size_value
1485 && format_length
> array_size_value
)
1486 format_length
= array_size_value
;
1491 if (offset
>= format_length
)
1493 res
->number_non_literal
++;
1496 format_chars
+= offset
;
1497 format_length
-= offset
;
1499 if (format_length
< 1)
1501 res
->number_unterminated
++;
1504 if (format_length
== 1)
1506 res
->number_empty
++;
1509 if (format_chars
[--format_length
] != 0)
1511 res
->number_unterminated
++;
1515 /* Skip to first argument to check. */
1516 while (arg_num
+ 1 < info
->first_arg_num
)
1520 params
= TREE_CHAIN (params
);
1523 /* Provisionally increment res->number_other; check_format_info_main
1524 will decrement it if it finds there are extra arguments, but this way
1525 need not adjust it for every return. */
1526 res
->number_other
++;
1527 check_format_info_main (status
, res
, info
, format_chars
, format_length
,
1532 /* Do the main part of checking a call to a format function. FORMAT_CHARS
1533 is the NUL-terminated format string (which at this point may contain
1534 internal NUL characters); FORMAT_LENGTH is its length (excluding the
1535 terminating NUL character). ARG_NUM is one less than the number of
1536 the first format argument to check; PARAMS points to that format
1537 argument in the list of arguments. */
1540 check_format_info_main (status
, res
, info
, format_chars
, format_length
,
1543 format_check_results
*res
;
1544 function_format_info
*info
;
1545 const char *format_chars
;
1548 unsigned HOST_WIDE_INT arg_num
;
1550 const char *orig_format_chars
= format_chars
;
1551 tree first_fillin_param
= params
;
1553 const format_kind_info
*fki
= &format_types
[info
->format_type
];
1554 const format_flag_spec
*flag_specs
= fki
->flag_specs
;
1555 const format_flag_pair
*bad_flag_pairs
= fki
->bad_flag_pairs
;
1557 /* -1 if no conversions taking an operand have been found; 0 if one has
1558 and it didn't use $; 1 if $ formats are in use. */
1559 int has_operand_number
= -1;
1561 init_dollar_format_checking (info
->first_arg_num
, first_fillin_param
);
1566 int suppressed
= FALSE
;
1567 const char *length_chars
= NULL
;
1568 enum format_lengths length_chars_val
= FMT_LEN_none
;
1569 enum format_std_version length_chars_std
= STD_C89
;
1573 int main_arg_num
= 0;
1574 tree main_arg_params
= 0;
1575 enum format_std_version wanted_type_std
;
1576 const char *wanted_type_name
;
1577 format_wanted_type width_wanted_type
;
1578 format_wanted_type precision_wanted_type
;
1579 format_wanted_type main_wanted_type
;
1580 format_wanted_type
*first_wanted_type
= NULL
;
1581 format_wanted_type
*last_wanted_type
= NULL
;
1582 const format_length_info
*fli
= NULL
;
1583 const format_char_info
*fci
= NULL
;
1584 char flag_chars
[256];
1586 if (*format_chars
== 0)
1588 if (format_chars
- orig_format_chars
!= format_length
)
1589 status_warning (status
, "embedded `\\0' in format");
1590 if (info
->first_arg_num
!= 0 && params
!= 0
1591 && has_operand_number
<= 0)
1593 res
->number_other
--;
1594 res
->number_extra_args
++;
1596 if (has_operand_number
> 0)
1597 finish_dollar_format_checking (status
, res
, fki
->flags
& (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK
);
1600 if (*format_chars
++ != '%')
1602 if (*format_chars
== 0)
1604 status_warning (status
, "spurious trailing `%%' in format");
1607 if (*format_chars
== '%')
1614 if ((fki
->flags
& (int) FMT_FLAG_USE_DOLLAR
) && has_operand_number
!= 0)
1616 /* Possibly read a $ operand number at the start of the format.
1617 If one was previously used, one is required here. If one
1618 is not used here, we can't immediately conclude this is a
1619 format without them, since it could be printf %m or scanf %*. */
1621 opnum
= maybe_read_dollar_number (status
, &format_chars
, 0,
1623 &main_arg_params
, fki
);
1628 has_operand_number
= 1;
1629 main_arg_num
= opnum
+ info
->first_arg_num
- 1;
1633 /* Read any format flags, but do not yet validate them beyond removing
1634 duplicates, since in general validation depends on the rest of
1636 while (*format_chars
!= 0
1637 && strchr (fki
->flag_chars
, *format_chars
) != 0)
1639 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
1640 *format_chars
, NULL
);
1641 if (strchr (flag_chars
, *format_chars
) != 0)
1643 status_warning (status
, "repeated %s in format", _(s
->name
));
1647 i
= strlen (flag_chars
);
1648 flag_chars
[i
++] = *format_chars
;
1651 if (s
->skip_next_char
)
1654 if (*format_chars
== 0)
1656 status_warning (status
, "missing fill character at end of strfmon format");
1663 /* Read any format width, possibly * or *m$. */
1664 if (fki
->width_char
!= 0)
1666 if (fki
->width_type
!= NULL
&& *format_chars
== '*')
1668 i
= strlen (flag_chars
);
1669 flag_chars
[i
++] = fki
->width_char
;
1671 /* "...a field width...may be indicated by an asterisk.
1672 In this case, an int argument supplies the field width..." */
1674 if (has_operand_number
!= 0)
1677 opnum
= maybe_read_dollar_number (status
, &format_chars
,
1678 has_operand_number
== 1,
1685 has_operand_number
= 1;
1686 arg_num
= opnum
+ info
->first_arg_num
- 1;
1689 has_operand_number
= 0;
1691 if (info
->first_arg_num
!= 0)
1695 status_warning (status
, "too few arguments for format");
1698 cur_param
= TREE_VALUE (params
);
1699 if (has_operand_number
<= 0)
1701 params
= TREE_CHAIN (params
);
1704 width_wanted_type
.wanted_type
= *fki
->width_type
;
1705 width_wanted_type
.wanted_type_name
= NULL
;
1706 width_wanted_type
.pointer_count
= 0;
1707 width_wanted_type
.char_lenient_flag
= 0;
1708 width_wanted_type
.writing_in_flag
= 0;
1709 width_wanted_type
.reading_from_flag
= 0;
1710 width_wanted_type
.name
= _("field width");
1711 width_wanted_type
.param
= cur_param
;
1712 width_wanted_type
.arg_num
= arg_num
;
1713 width_wanted_type
.next
= NULL
;
1714 if (last_wanted_type
!= 0)
1715 last_wanted_type
->next
= &width_wanted_type
;
1716 if (first_wanted_type
== 0)
1717 first_wanted_type
= &width_wanted_type
;
1718 last_wanted_type
= &width_wanted_type
;
1723 /* Possibly read a numeric width. If the width is zero,
1724 we complain if appropriate. */
1725 int non_zero_width_char
= FALSE
;
1726 int found_width
= FALSE
;
1727 while (ISDIGIT (*format_chars
))
1730 if (*format_chars
!= '0')
1731 non_zero_width_char
= TRUE
;
1734 if (found_width
&& !non_zero_width_char
&&
1735 (fki
->flags
& (int) FMT_FLAG_ZERO_WIDTH_BAD
))
1736 status_warning (status
, "zero width in %s format",
1740 i
= strlen (flag_chars
);
1741 flag_chars
[i
++] = fki
->width_char
;
1747 /* Read any format left precision (must be a number, not *). */
1748 if (fki
->left_precision_char
!= 0 && *format_chars
== '#')
1751 i
= strlen (flag_chars
);
1752 flag_chars
[i
++] = fki
->left_precision_char
;
1754 if (!ISDIGIT (*format_chars
))
1755 status_warning (status
, "empty left precision in %s format",
1757 while (ISDIGIT (*format_chars
))
1761 /* Read any format precision, possibly * or *m$. */
1762 if (fki
->precision_char
!= 0 && *format_chars
== '.')
1765 i
= strlen (flag_chars
);
1766 flag_chars
[i
++] = fki
->precision_char
;
1768 if (fki
->precision_type
!= NULL
&& *format_chars
== '*')
1770 /* "...a...precision...may be indicated by an asterisk.
1771 In this case, an int argument supplies the...precision." */
1773 if (has_operand_number
!= 0)
1776 opnum
= maybe_read_dollar_number (status
, &format_chars
,
1777 has_operand_number
== 1,
1784 has_operand_number
= 1;
1785 arg_num
= opnum
+ info
->first_arg_num
- 1;
1788 has_operand_number
= 0;
1790 if (info
->first_arg_num
!= 0)
1794 status_warning (status
, "too few arguments for format");
1797 cur_param
= TREE_VALUE (params
);
1798 if (has_operand_number
<= 0)
1800 params
= TREE_CHAIN (params
);
1803 precision_wanted_type
.wanted_type
= *fki
->precision_type
;
1804 precision_wanted_type
.wanted_type_name
= NULL
;
1805 precision_wanted_type
.pointer_count
= 0;
1806 precision_wanted_type
.char_lenient_flag
= 0;
1807 precision_wanted_type
.writing_in_flag
= 0;
1808 precision_wanted_type
.reading_from_flag
= 0;
1809 precision_wanted_type
.name
= _("field precision");
1810 precision_wanted_type
.param
= cur_param
;
1811 precision_wanted_type
.arg_num
= arg_num
;
1812 precision_wanted_type
.next
= NULL
;
1813 if (last_wanted_type
!= 0)
1814 last_wanted_type
->next
= &precision_wanted_type
;
1815 if (first_wanted_type
== 0)
1816 first_wanted_type
= &precision_wanted_type
;
1817 last_wanted_type
= &precision_wanted_type
;
1822 if (!(fki
->flags
& (int) FMT_FLAG_EMPTY_PREC_OK
)
1823 && !ISDIGIT (*format_chars
))
1824 status_warning (status
, "empty precision in %s format",
1826 while (ISDIGIT (*format_chars
))
1831 /* Read any length modifier, if this kind of format has them. */
1832 fli
= fki
->length_char_specs
;
1833 length_chars
= NULL
;
1834 length_chars_val
= FMT_LEN_none
;
1835 length_chars_std
= STD_C89
;
1838 while (fli
->name
!= 0 && fli
->name
[0] != *format_chars
)
1843 if (fli
->double_name
!= 0 && fli
->name
[0] == *format_chars
)
1846 length_chars
= fli
->double_name
;
1847 length_chars_val
= fli
->double_index
;
1848 length_chars_std
= fli
->double_std
;
1852 length_chars
= fli
->name
;
1853 length_chars_val
= fli
->index
;
1854 length_chars_std
= fli
->std
;
1856 i
= strlen (flag_chars
);
1857 flag_chars
[i
++] = fki
->length_code_char
;
1862 /* Warn if the length modifier is non-standard. */
1863 if (ADJ_STD (length_chars_std
) > C_STD_VER
)
1864 status_warning (status
, "%s does not support the `%s' %s length modifier",
1865 C_STD_NAME (length_chars_std
), length_chars
,
1870 /* Read any modifier (strftime E/O). */
1871 if (fki
->modifier_chars
!= NULL
)
1873 while (*format_chars
!= 0
1874 && strchr (fki
->modifier_chars
, *format_chars
) != 0)
1876 if (strchr (flag_chars
, *format_chars
) != 0)
1878 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
1879 *format_chars
, NULL
);
1880 status_warning (status
, "repeated %s in format", _(s
->name
));
1884 i
= strlen (flag_chars
);
1885 flag_chars
[i
++] = *format_chars
;
1892 /* Handle the scanf allocation kludge. */
1893 if (fki
->flags
& (int) FMT_FLAG_SCANF_A_KLUDGE
)
1895 if (*format_chars
== 'a' && !flag_isoc99
)
1897 if (format_chars
[1] == 's' || format_chars
[1] == 'S'
1898 || format_chars
[1] == '[')
1900 /* `a' is used as a flag. */
1901 i
= strlen (flag_chars
);
1902 flag_chars
[i
++] = 'a';
1909 format_char
= *format_chars
;
1910 if (format_char
== 0
1911 || (!(fki
->flags
& (int) FMT_FLAG_FANCY_PERCENT_OK
)
1912 && format_char
== '%'))
1914 status_warning (status
, "conversion lacks type at end of format");
1918 fci
= fki
->conversion_specs
;
1919 while (fci
->format_chars
!= 0
1920 && strchr (fci
->format_chars
, format_char
) == 0)
1922 if (fci
->format_chars
== 0)
1924 if (ISGRAPH(format_char
))
1925 status_warning (status
, "unknown conversion type character `%c' in format",
1928 status_warning (status
, "unknown conversion type character 0x%x in format",
1934 if (ADJ_STD (fci
->std
) > C_STD_VER
)
1935 status_warning (status
, "%s does not support the `%%%c' %s format",
1936 C_STD_NAME (fci
->std
), format_char
, fki
->name
);
1939 /* Validate the individual flags used, removing any that are invalid. */
1942 for (i
= 0; flag_chars
[i
] != 0; i
++)
1944 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
1945 flag_chars
[i
], NULL
);
1946 flag_chars
[i
- d
] = flag_chars
[i
];
1947 if (flag_chars
[i
] == fki
->length_code_char
)
1949 if (strchr (fci
->flag_chars
, flag_chars
[i
]) == 0)
1951 status_warning (status
, "%s used with `%%%c' %s format",
1952 _(s
->name
), format_char
, fki
->name
);
1958 const format_flag_spec
*t
;
1959 if (ADJ_STD (s
->std
) > C_STD_VER
)
1960 status_warning (status
, "%s does not support %s",
1961 C_STD_NAME (s
->std
), _(s
->long_name
));
1962 t
= get_flag_spec (flag_specs
, flag_chars
[i
], fci
->flags2
);
1963 if (t
!= NULL
&& ADJ_STD (t
->std
) > ADJ_STD (s
->std
))
1965 const char *long_name
= (t
->long_name
!= NULL
1968 if (ADJ_STD (t
->std
) > C_STD_VER
)
1969 status_warning (status
, "%s does not support %s with the `%%%c' %s format",
1970 C_STD_NAME (t
->std
), _(long_name
),
1971 format_char
, fki
->name
);
1975 flag_chars
[i
- d
] = 0;
1978 if ((fki
->flags
& (int) FMT_FLAG_SCANF_A_KLUDGE
)
1979 && strchr (flag_chars
, 'a') != 0)
1982 if (fki
->suppression_char
1983 && strchr (flag_chars
, fki
->suppression_char
) != 0)
1986 /* Validate the pairs of flags used. */
1987 for (i
= 0; bad_flag_pairs
[i
].flag_char1
!= 0; i
++)
1989 const format_flag_spec
*s
, *t
;
1990 if (strchr (flag_chars
, bad_flag_pairs
[i
].flag_char1
) == 0)
1992 if (strchr (flag_chars
, bad_flag_pairs
[i
].flag_char2
) == 0)
1994 if (bad_flag_pairs
[i
].predicate
!= 0
1995 && strchr (fci
->flags2
, bad_flag_pairs
[i
].predicate
) == 0)
1997 s
= get_flag_spec (flag_specs
, bad_flag_pairs
[i
].flag_char1
, NULL
);
1998 t
= get_flag_spec (flag_specs
, bad_flag_pairs
[i
].flag_char2
, NULL
);
1999 if (bad_flag_pairs
[i
].ignored
)
2001 if (bad_flag_pairs
[i
].predicate
!= 0)
2002 status_warning (status
, "%s ignored with %s and `%%%c' %s format",
2003 _(s
->name
), _(t
->name
), format_char
,
2006 status_warning (status
, "%s ignored with %s in %s format",
2007 _(s
->name
), _(t
->name
), fki
->name
);
2011 if (bad_flag_pairs
[i
].predicate
!= 0)
2012 status_warning (status
, "use of %s and %s together with `%%%c' %s format",
2013 _(s
->name
), _(t
->name
), format_char
,
2016 status_warning (status
, "use of %s and %s together in %s format",
2017 _(s
->name
), _(t
->name
), fki
->name
);
2021 /* Give Y2K warnings. */
2022 if (warn_format_y2k
)
2025 if (strchr (fci
->flags2
, '4') != 0)
2026 if (strchr (flag_chars
, 'E') != 0)
2030 else if (strchr (fci
->flags2
, '3') != 0)
2032 else if (strchr (fci
->flags2
, '2') != 0)
2035 status_warning (status
, "`%%%c' yields only last 2 digits of year in some locales",
2037 else if (y2k_level
== 2)
2038 status_warning (status
, "`%%%c' yields only last 2 digits of year", format_char
);
2041 if (strchr (fci
->flags2
, '[') != 0)
2043 /* Skip over scan set, in case it happens to have '%' in it. */
2044 if (*format_chars
== '^')
2046 /* Find closing bracket; if one is hit immediately, then
2047 it's part of the scan set rather than a terminator. */
2048 if (*format_chars
== ']')
2050 while (*format_chars
&& *format_chars
!= ']')
2052 if (*format_chars
!= ']')
2053 /* The end of the format string was reached. */
2054 status_warning (status
, "no closing `]' for `%%[' format");
2058 wanted_type_name
= 0;
2059 if (fki
->flags
& (int) FMT_FLAG_ARG_CONVERT
)
2061 wanted_type
= (fci
->types
[length_chars_val
].type
2062 ? *fci
->types
[length_chars_val
].type
: 0);
2063 wanted_type_name
= fci
->types
[length_chars_val
].name
;
2064 wanted_type_std
= fci
->types
[length_chars_val
].std
;
2065 if (wanted_type
== 0)
2067 status_warning (status
, "use of `%s' length modifier with `%c' type character",
2068 length_chars
, format_char
);
2069 /* Heuristic: skip one argument when an invalid length/type
2070 combination is encountered. */
2074 status_warning (status
, "too few arguments for format");
2077 params
= TREE_CHAIN (params
);
2081 /* Warn if non-standard, provided it is more non-standard
2082 than the length and type characters that may already
2083 have been warned for. */
2084 && ADJ_STD (wanted_type_std
) > ADJ_STD (length_chars_std
)
2085 && ADJ_STD (wanted_type_std
) > ADJ_STD (fci
->std
))
2087 if (ADJ_STD (wanted_type_std
) > C_STD_VER
)
2088 status_warning (status
, "%s does not support the `%%%s%c' %s format",
2089 C_STD_NAME (wanted_type_std
), length_chars
,
2090 format_char
, fki
->name
);
2094 /* Finally. . .check type of argument against desired type! */
2095 if (info
->first_arg_num
== 0)
2097 if ((fci
->pointer_count
== 0 && wanted_type
== void_type_node
)
2100 if (main_arg_num
!= 0)
2103 status_warning (status
, "operand number specified with suppressed assignment");
2105 status_warning (status
, "operand number specified for format taking no argument");
2110 if (main_arg_num
!= 0)
2112 arg_num
= main_arg_num
;
2113 params
= main_arg_params
;
2118 if (has_operand_number
> 0)
2120 status_warning (status
, "missing $ operand number in format");
2124 has_operand_number
= 0;
2127 status_warning (status
, "too few arguments for format");
2131 cur_param
= TREE_VALUE (params
);
2132 params
= TREE_CHAIN (params
);
2133 main_wanted_type
.wanted_type
= wanted_type
;
2134 main_wanted_type
.wanted_type_name
= wanted_type_name
;
2135 main_wanted_type
.pointer_count
= fci
->pointer_count
+ aflag
;
2136 main_wanted_type
.char_lenient_flag
= 0;
2137 if (strchr (fci
->flags2
, 'c') != 0)
2138 main_wanted_type
.char_lenient_flag
= 1;
2139 main_wanted_type
.writing_in_flag
= 0;
2140 main_wanted_type
.reading_from_flag
= 0;
2142 main_wanted_type
.writing_in_flag
= 1;
2145 if (strchr (fci
->flags2
, 'W') != 0)
2146 main_wanted_type
.writing_in_flag
= 1;
2147 if (strchr (fci
->flags2
, 'R') != 0)
2148 main_wanted_type
.reading_from_flag
= 1;
2150 main_wanted_type
.name
= NULL
;
2151 main_wanted_type
.param
= cur_param
;
2152 main_wanted_type
.arg_num
= arg_num
;
2153 main_wanted_type
.next
= NULL
;
2154 if (last_wanted_type
!= 0)
2155 last_wanted_type
->next
= &main_wanted_type
;
2156 if (first_wanted_type
== 0)
2157 first_wanted_type
= &main_wanted_type
;
2158 last_wanted_type
= &main_wanted_type
;
2161 if (first_wanted_type
!= 0)
2162 check_format_types (status
, first_wanted_type
);
2168 /* Check the argument types from a single format conversion (possibly
2169 including width and precision arguments). */
2171 check_format_types (status
, types
)
2173 format_wanted_type
*types
;
2175 for (; types
!= 0; types
= types
->next
)
2184 cur_param
= types
->param
;
2185 cur_type
= TREE_TYPE (cur_param
);
2186 if (cur_type
== error_mark_node
)
2189 wanted_type
= types
->wanted_type
;
2190 arg_num
= types
->arg_num
;
2192 /* The following should not occur here. */
2193 if (wanted_type
== 0)
2195 if (wanted_type
== void_type_node
&& types
->pointer_count
== 0)
2198 if (types
->pointer_count
== 0)
2199 wanted_type
= (*lang_hooks
.types
.type_promotes_to
) (wanted_type
);
2201 STRIP_NOPS (cur_param
);
2203 /* Check the types of any additional pointer arguments
2204 that precede the "real" argument. */
2205 for (i
= 0; i
< types
->pointer_count
; ++i
)
2207 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
2209 cur_type
= TREE_TYPE (cur_type
);
2210 if (cur_type
== error_mark_node
)
2213 /* Check for writing through a NULL pointer. */
2214 if (types
->writing_in_flag
2217 && integer_zerop (cur_param
))
2218 status_warning (status
,
2219 "writing through null pointer (arg %d)",
2222 /* Check for reading through a NULL pointer. */
2223 if (types
->reading_from_flag
2226 && integer_zerop (cur_param
))
2227 status_warning (status
,
2228 "reading through null pointer (arg %d)",
2231 if (cur_param
!= 0 && TREE_CODE (cur_param
) == ADDR_EXPR
)
2232 cur_param
= TREE_OPERAND (cur_param
, 0);
2236 /* See if this is an attempt to write into a const type with
2237 scanf or with printf "%n". Note: the writing in happens
2238 at the first indirection only, if for example
2239 void * const * is passed to scanf %p; passing
2240 const void ** is simply passing an incompatible type. */
2241 if (types
->writing_in_flag
2243 && (TYPE_READONLY (cur_type
)
2245 && (TREE_CODE_CLASS (TREE_CODE (cur_param
)) == 'c'
2246 || (DECL_P (cur_param
)
2247 && TREE_READONLY (cur_param
))))))
2248 status_warning (status
, "writing into constant object (arg %d)", arg_num
);
2250 /* If there are extra type qualifiers beyond the first
2251 indirection, then this makes the types technically
2255 && (TYPE_READONLY (cur_type
)
2256 || TYPE_VOLATILE (cur_type
)
2257 || TYPE_RESTRICT (cur_type
)))
2258 status_warning (status
, "extra type qualifiers in format argument (arg %d)",
2264 if (types
->pointer_count
== 1)
2265 status_warning (status
, "format argument is not a pointer (arg %d)", arg_num
);
2267 status_warning (status
, "format argument is not a pointer to a pointer (arg %d)", arg_num
);
2272 if (i
< types
->pointer_count
)
2275 orig_cur_type
= cur_type
;
2276 cur_type
= TYPE_MAIN_VARIANT (cur_type
);
2278 /* Check whether the argument type is a character type. This leniency
2279 only applies to certain formats, flagged with 'c'.
2281 if (types
->char_lenient_flag
)
2282 char_type_flag
= (cur_type
== char_type_node
2283 || cur_type
== signed_char_type_node
2284 || cur_type
== unsigned_char_type_node
);
2286 /* Check the type of the "real" argument, if there's a type we want. */
2287 if (wanted_type
== cur_type
)
2289 /* If we want `void *', allow any pointer type.
2290 (Anything else would already have got a warning.)
2291 With -pedantic, only allow pointers to void and to character
2293 if (wanted_type
== void_type_node
2294 && (!pedantic
|| (i
== 1 && char_type_flag
)))
2296 /* Don't warn about differences merely in signedness, unless
2297 -pedantic. With -pedantic, warn if the type is a pointer
2298 target and not a character type, and for character types at
2299 a second level of indirection. */
2300 if (TREE_CODE (wanted_type
) == INTEGER_TYPE
2301 && TREE_CODE (cur_type
) == INTEGER_TYPE
2302 && (! pedantic
|| i
== 0 || (i
== 1 && char_type_flag
))
2303 && (TREE_UNSIGNED (wanted_type
)
2304 ? wanted_type
== c_common_unsigned_type (cur_type
)
2305 : wanted_type
== c_common_signed_type (cur_type
)))
2307 /* Likewise, "signed char", "unsigned char" and "char" are
2308 equivalent but the above test won't consider them equivalent. */
2309 if (wanted_type
== char_type_node
2310 && (! pedantic
|| i
< 2)
2313 /* Now we have a type mismatch. */
2318 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type
)));
2320 if (TYPE_NAME (orig_cur_type
) != 0
2321 && TREE_CODE (orig_cur_type
) != INTEGER_TYPE
2322 && !(TREE_CODE (orig_cur_type
) == POINTER_TYPE
2323 && TREE_CODE (TREE_TYPE (orig_cur_type
)) == INTEGER_TYPE
))
2325 if (TREE_CODE (TYPE_NAME (orig_cur_type
)) == TYPE_DECL
2326 && DECL_NAME (TYPE_NAME (orig_cur_type
)) != 0)
2327 that
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (orig_cur_type
)));
2329 that
= IDENTIFIER_POINTER (TYPE_NAME (orig_cur_type
));
2332 /* A nameless type can't possibly match what the format wants.
2333 So there will be a warning for it.
2334 Make up a string to describe vaguely what it is. */
2337 if (TREE_CODE (orig_cur_type
) == POINTER_TYPE
)
2338 that
= _("pointer");
2340 that
= _("different type");
2343 /* Make the warning better in case of mismatch of int vs long. */
2344 if (TREE_CODE (orig_cur_type
) == INTEGER_TYPE
2345 && TREE_CODE (wanted_type
) == INTEGER_TYPE
2346 && TYPE_PRECISION (orig_cur_type
) == TYPE_PRECISION (wanted_type
)
2347 && TYPE_NAME (orig_cur_type
) != 0
2348 && TREE_CODE (TYPE_NAME (orig_cur_type
)) == TYPE_DECL
)
2349 that
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (orig_cur_type
)));
2351 if (strcmp (this, that
) != 0)
2353 /* There may be a better name for the format, e.g. size_t,
2354 but we should allow for programs with a perverse typedef
2355 making size_t something other than what the compiler
2357 if (types
->wanted_type_name
!= 0
2358 && strcmp (types
->wanted_type_name
, that
) != 0)
2359 this = types
->wanted_type_name
;
2360 if (types
->name
!= 0)
2361 status_warning (status
, "%s is not type %s (arg %d)", types
->name
, this,
2364 status_warning (status
, "%s format, %s arg (arg %d)", this, that
, arg_num
);
2370 /* Handle a "format" attribute; arguments as in
2371 struct attribute_spec.handler. */
2373 handle_format_attribute (node
, name
, args
, flags
, no_add_attrs
)
2375 tree name ATTRIBUTE_UNUSED
;
2381 function_format_info info
;
2384 if (!decode_format_attr (args
, &info
, 0))
2386 *no_add_attrs
= true;
2390 argument
= TYPE_ARG_TYPES (type
);
2393 if (!check_format_string (argument
, info
.format_num
, flags
,
2397 if (info
.first_arg_num
!= 0)
2399 unsigned HOST_WIDE_INT arg_num
= 1;
2401 /* Verify that first_arg_num points to the last arg,
2404 arg_num
++, argument
= TREE_CHAIN (argument
);
2406 if (arg_num
!= info
.first_arg_num
)
2408 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
2409 error ("args to be formatted is not '...'");
2410 *no_add_attrs
= true;
2416 if (info
.format_type
== strftime_format_type
&& info
.first_arg_num
!= 0)
2418 error ("strftime formats cannot format arguments");
2419 *no_add_attrs
= true;
2423 /* If this is format type __asm_fprintf__, we have to initialize
2424 GCC's notion of HOST_WIDE_INT for checking %wd. */
2425 if (info
.format_type
== asm_fprintf_format_type
)
2430 /* For this custom check to work, one must have issued:
2431 "typedef HOST_WIDE_INT __gcc_host_wide_int__;"
2432 in your source code prior to using this attribute. */
2435 format_kind_info
*new_format_types
;
2436 format_length_info
*new_asm_fprintf_length_specs
;
2438 if (!(hwi
= maybe_get_identifier ("__gcc_host_wide_int__")))
2441 /* Create a new (writable) copy of asm_fprintf_length_specs. */
2442 new_asm_fprintf_length_specs
=
2443 xmalloc (sizeof (asm_fprintf_length_specs
));
2444 memcpy (new_asm_fprintf_length_specs
, asm_fprintf_length_specs
,
2445 sizeof (asm_fprintf_length_specs
));
2447 /* Create a new (writable) copy of format_types. */
2448 new_format_types
= xmalloc (sizeof (format_types_orig
));
2449 memcpy (new_format_types
, format_types_orig
, sizeof (format_types_orig
));
2451 /* Find the underlying type for HOST_WIDE_INT. */
2452 orig
= DECL_ORIGINAL_TYPE (identifier_global_value (hwi
));
2453 if (orig
== long_integer_type_node
)
2454 new_asm_fprintf_length_specs
[1].index
= FMT_LEN_l
;
2455 else if (orig
== long_long_integer_type_node
)
2456 new_asm_fprintf_length_specs
[1].index
= FMT_LEN_ll
;
2460 /* Assign the new data for use. */
2461 new_format_types
[asm_fprintf_format_type
].length_char_specs
=
2462 new_asm_fprintf_length_specs
;
2463 format_types
= new_format_types
;