1 /* Check calls to formatted I/O functions (-Wformat).
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 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
29 #include "diagnostic.h"
32 /* Command line options and their associated flags. */
34 /* Warn about format/argument anomalies in calls to formatted I/O functions
35 (*printf, *scanf, strftime, strfmon, etc.). */
39 /* Warn about Y2K problems with strftime formats. */
43 /* Warn about excess arguments to formats. */
45 int warn_format_extra_args
;
47 /* Warn about non-literal format arguments. */
49 int warn_format_nonliteral
;
51 /* Warn about possible security problems with calls to format functions. */
53 int warn_format_security
;
55 /* Set format warning options according to a -Wformat=n option. */
61 warn_format
= setting
;
62 warn_format_y2k
= setting
;
63 warn_format_extra_args
= setting
;
66 warn_format_nonliteral
= setting
;
67 warn_format_security
= setting
;
72 /* Handle attributes associated with format checking. */
74 /* This must be in the same order as format_types, with format_type_error
76 enum format_type
{ printf_format_type
, scanf_format_type
,
77 strftime_format_type
, strfmon_format_type
,
80 typedef struct function_format_info
82 enum format_type format_type
; /* type of format (printf, scanf, etc.) */
83 unsigned HOST_WIDE_INT format_num
; /* number of format argument */
84 unsigned HOST_WIDE_INT first_arg_num
; /* number of first arg (zero for varargs) */
85 } function_format_info
;
87 static bool decode_format_attr
PARAMS ((tree
,
88 function_format_info
*, int));
89 static enum format_type decode_format_type
PARAMS ((const char *));
91 /* Handle a "format" attribute; arguments as in
92 struct attribute_spec.handler. */
94 handle_format_attribute (node
, name
, args
, flags
, no_add_attrs
)
96 tree name ATTRIBUTE_UNUSED
;
102 function_format_info info
;
104 unsigned HOST_WIDE_INT arg_num
;
106 if (!decode_format_attr (args
, &info
, 0))
108 *no_add_attrs
= true;
112 /* If a parameter list is specified, verify that the format_num
113 argument is actually a string, in case the format attribute
115 argument
= TYPE_ARG_TYPES (type
);
118 for (arg_num
= 1; argument
!= 0 && arg_num
!= info
.format_num
;
119 ++arg_num
, argument
= TREE_CHAIN (argument
))
123 || TREE_CODE (TREE_VALUE (argument
)) != POINTER_TYPE
124 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument
)))
127 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
128 error ("format string arg not a string type");
129 *no_add_attrs
= true;
133 else if (info
.first_arg_num
!= 0)
135 /* Verify that first_arg_num points to the last arg,
138 arg_num
++, argument
= TREE_CHAIN (argument
);
140 if (arg_num
!= info
.first_arg_num
)
142 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
143 error ("args to be formatted is not '...'");
144 *no_add_attrs
= true;
150 if (info
.format_type
== strftime_format_type
&& info
.first_arg_num
!= 0)
152 error ("strftime formats cannot format arguments");
153 *no_add_attrs
= true;
161 /* Handle a "format_arg" attribute; arguments as in
162 struct attribute_spec.handler. */
164 handle_format_arg_attribute (node
, name
, args
, flags
, no_add_attrs
)
166 tree name ATTRIBUTE_UNUSED
;
172 tree format_num_expr
= TREE_VALUE (args
);
173 unsigned HOST_WIDE_INT format_num
;
174 unsigned HOST_WIDE_INT arg_num
;
177 /* Strip any conversions from the first arg number and verify it
179 while (TREE_CODE (format_num_expr
) == NOP_EXPR
180 || TREE_CODE (format_num_expr
) == CONVERT_EXPR
181 || TREE_CODE (format_num_expr
) == NON_LVALUE_EXPR
)
182 format_num_expr
= TREE_OPERAND (format_num_expr
, 0);
184 if (TREE_CODE (format_num_expr
) != INTEGER_CST
185 || TREE_INT_CST_HIGH (format_num_expr
) != 0)
187 error ("format string has invalid operand number");
188 *no_add_attrs
= true;
192 format_num
= TREE_INT_CST_LOW (format_num_expr
);
194 /* If a parameter list is specified, verify that the format_num
195 argument is actually a string, in case the format attribute
197 argument
= TYPE_ARG_TYPES (type
);
200 for (arg_num
= 1; argument
!= 0 && arg_num
!= format_num
;
201 ++arg_num
, argument
= TREE_CHAIN (argument
))
205 || TREE_CODE (TREE_VALUE (argument
)) != POINTER_TYPE
206 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument
)))
209 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
210 error ("format string arg not a string type");
211 *no_add_attrs
= true;
216 if (TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
217 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type
)))
220 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
221 error ("function does not return string type");
222 *no_add_attrs
= true;
230 /* Decode the arguments to a "format" attribute into a function_format_info
231 structure. It is already known that the list is of the right length.
232 If VALIDATED_P is true, then these attributes have already been validated
233 and this function will abort if they are erroneous; if false, it
234 will give an error message. Returns true if the attributes are
235 successfully decoded, false otherwise. */
238 decode_format_attr (args
, info
, validated_p
)
240 function_format_info
*info
;
243 tree format_type_id
= TREE_VALUE (args
);
244 tree format_num_expr
= TREE_VALUE (TREE_CHAIN (args
));
245 tree first_arg_num_expr
246 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args
)));
248 if (TREE_CODE (format_type_id
) != IDENTIFIER_NODE
)
252 error ("unrecognized format specifier");
257 const char *p
= IDENTIFIER_POINTER (format_type_id
);
259 info
->format_type
= decode_format_type (p
);
261 if (info
->format_type
== format_type_error
)
265 warning ("`%s' is an unrecognized format function type", p
);
270 /* Strip any conversions from the string index and first arg number
271 and verify they are constants. */
272 while (TREE_CODE (format_num_expr
) == NOP_EXPR
273 || TREE_CODE (format_num_expr
) == CONVERT_EXPR
274 || TREE_CODE (format_num_expr
) == NON_LVALUE_EXPR
)
275 format_num_expr
= TREE_OPERAND (format_num_expr
, 0);
277 while (TREE_CODE (first_arg_num_expr
) == NOP_EXPR
278 || TREE_CODE (first_arg_num_expr
) == CONVERT_EXPR
279 || TREE_CODE (first_arg_num_expr
) == NON_LVALUE_EXPR
)
280 first_arg_num_expr
= TREE_OPERAND (first_arg_num_expr
, 0);
282 if (TREE_CODE (format_num_expr
) != INTEGER_CST
283 || TREE_INT_CST_HIGH (format_num_expr
) != 0
284 || TREE_CODE (first_arg_num_expr
) != INTEGER_CST
285 || TREE_INT_CST_HIGH (first_arg_num_expr
) != 0)
289 error ("format string has invalid operand number");
293 info
->format_num
= TREE_INT_CST_LOW (format_num_expr
);
294 info
->first_arg_num
= TREE_INT_CST_LOW (first_arg_num_expr
);
295 if (info
->first_arg_num
!= 0 && info
->first_arg_num
<= info
->format_num
)
299 error ("format string arg follows the args to be formatted");
306 /* Check a call to a format function against a parameter list. */
308 /* The meaningfully distinct length modifiers for format checking recognised
325 /* The standard versions in which various format features appeared. */
326 enum format_std_version
330 STD_C9L
, /* C99, but treat as C89 if -Wno-long-long. */
335 /* The C standard version C++ is treated as equivalent to
336 or inheriting from, for the purpose of format features supported. */
337 #define CPLUSPLUS_STD_VER STD_C94
338 /* The C standard version we are checking formats against when pedantic. */
339 #define C_STD_VER ((int)(c_language == clk_cplusplus \
340 ? CPLUSPLUS_STD_VER \
343 : (flag_isoc94 ? STD_C94 : STD_C89))))
344 /* The name to give to the standard version we are warning about when
345 pedantic. FEATURE_VER is the version in which the feature warned out
346 appeared, which is higher than C_STD_VER. */
347 #define C_STD_NAME(FEATURE_VER) (c_language == clk_cplusplus \
349 : ((FEATURE_VER) == STD_EXT \
352 /* Adjust a C standard version, which may be STD_C9L, to account for
353 -Wno-long-long. Returns other standard versions unchanged. */
354 #define ADJ_STD(VER) ((int)((VER) == STD_C9L \
355 ? (warn_long_long ? STD_C99 : STD_C89) \
358 /* Flags that may apply to a particular kind of format checked by GCC. */
361 /* This format converts arguments of types determined by the
363 FMT_FLAG_ARG_CONVERT
= 1,
364 /* The scanf allocation 'a' kludge applies to this format kind. */
365 FMT_FLAG_SCANF_A_KLUDGE
= 2,
366 /* A % during parsing a specifier is allowed to be a modified % rather
367 that indicating the format is broken and we are out-of-sync. */
368 FMT_FLAG_FANCY_PERCENT_OK
= 4,
369 /* With $ operand numbers, it is OK to reference the same argument more
371 FMT_FLAG_DOLLAR_MULTIPLE
= 8,
372 /* This format type uses $ operand numbers (strfmon doesn't). */
373 FMT_FLAG_USE_DOLLAR
= 16,
374 /* Zero width is bad in this type of format (scanf). */
375 FMT_FLAG_ZERO_WIDTH_BAD
= 32,
376 /* Empty precision specification is OK in this type of format (printf). */
377 FMT_FLAG_EMPTY_PREC_OK
= 64
378 /* Not included here: details of whether width or precision may occur
379 (controlled by width_char and precision_char); details of whether
380 '*' can be used for these (width_type and precision_type); details
381 of whether length modifiers can occur (length_char_specs). */
385 /* Structure describing a length modifier supported in format checking, and
386 possibly a doubled version such as "hh". */
389 /* Name of the single-character length modifier. */
390 const char *const name
;
391 /* Index into a format_char_info.types array. */
392 const enum format_lengths index
;
393 /* Standard version this length appears in. */
394 const enum format_std_version std
;
395 /* Same, if the modifier can be repeated, or NULL if it can't. */
396 const char *const double_name
;
397 const enum format_lengths double_index
;
398 const enum format_std_version double_std
;
399 } format_length_info
;
402 /* Structure desribing the combination of a conversion specifier
403 (or a set of specifiers which act identically) and a length modifier. */
406 /* The standard version this combination of length and type appeared in.
407 This is only relevant if greater than those for length and type
408 individually; otherwise it is ignored. */
409 enum format_std_version std
;
410 /* The name to use for the type, if different from that generated internally
411 (e.g., "signed size_t"). */
413 /* The type itself. */
415 } format_type_detail
;
418 /* Macros to fill out tables of these. */
419 #define BADLEN { 0, NULL, NULL }
420 #define NOLENGTHS { BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }
423 /* Structure desribing a format conversion specifier (or a set of specifiers
424 which act identically), and the length modifiers used with it. */
427 const char *const format_chars
;
428 const int pointer_count
;
429 const enum format_std_version std
;
430 /* Types accepted for each length modifier. */
431 const format_type_detail types
[FMT_LEN_MAX
];
432 /* List of other modifier characters allowed with these specifiers.
433 This lists flags, and additionally "w" for width, "p" for precision
434 (right precision, for strfmon), "#" for left precision (strfmon),
435 "a" for scanf "a" allocation extension (not applicable in C99 mode),
436 "*" for scanf suppression, and "E" and "O" for those strftime
438 const char *const flag_chars
;
439 /* List of additional flags describing these conversion specifiers.
440 "c" for generic character pointers being allowed, "2" for strftime
441 two digit year formats, "3" for strftime formats giving two digit
442 years in some locales, "4" for "2" which becomes "3" with an "E" modifier,
443 "o" if use of strftime "O" is a GNU extension beyond C99,
444 "W" if the argument is a pointer which is dereferenced and written into,
445 "R" if the argument is a pointer which is dereferenced and read from,
446 "i" for printf integer formats where the '0' flag is ignored with
447 precision, and "[" for the starting character of a scanf scanset. */
448 const char *const flags2
;
452 /* Structure describing a flag accepted by some kind of format. */
455 /* The flag character in question (0 for end of array). */
457 /* Zero if this entry describes the flag character in general, or a
458 non-zero character that may be found in flags2 if it describes the
459 flag when used with certain formats only. If the latter, only
460 the first such entry found that applies to the current conversion
461 specifier is used; the values of `name' and `long_name' it supplies
462 will be used, if non-NULL and the standard version is higher than
463 the unpredicated one, for any pedantic warning. For example, 'o'
464 for strftime formats (meaning 'O' is an extension over C99). */
466 /* Nonzero if the next character after this flag in the format should
467 be skipped ('=' in strfmon), zero otherwise. */
468 const int skip_next_char
;
469 /* The name to use for this flag in diagnostic messages. For example,
470 N_("`0' flag"), N_("field width"). */
471 const char *const name
;
472 /* Long name for this flag in diagnostic messages; currently only used for
473 "ISO C does not support ...". For example, N_("the `I' printf flag"). */
474 const char *const long_name
;
475 /* The standard version in which it appeared. */
476 const enum format_std_version std
;
480 /* Structure describing a combination of flags that is bad for some kind
484 /* The first flag character in question (0 for end of array). */
485 const int flag_char1
;
486 /* The second flag character. */
487 const int flag_char2
;
488 /* Non-zero if the message should say that the first flag is ignored with
489 the second, zero if the combination should simply be objected to. */
491 /* Zero if this entry applies whenever this flag combination occurs,
492 a non-zero character from flags2 if it only applies in some
493 circumstances (e.g. 'i' for printf formats ignoring 0 with precision). */
498 /* Structure describing a particular kind of format processed by GCC. */
501 /* The name of this kind of format, for use in diagnostics. Also
502 the name of the attribute (without preceding and following __). */
503 const char *const name
;
504 /* Specifications of the length modifiers accepted; possibly NULL. */
505 const format_length_info
*const length_char_specs
;
506 /* Details of the conversion specification characters accepted. */
507 const format_char_info
*const conversion_specs
;
508 /* String listing the flag characters that are accepted. */
509 const char *const flag_chars
;
510 /* String listing modifier characters (strftime) accepted. May be NULL. */
511 const char *const modifier_chars
;
512 /* Details of the flag characters, including pseudo-flags. */
513 const format_flag_spec
*const flag_specs
;
514 /* Details of bad combinations of flags. */
515 const format_flag_pair
*const bad_flag_pairs
;
516 /* Flags applicable to this kind of format. */
518 /* Flag character to treat a width as, or 0 if width not used. */
519 const int width_char
;
520 /* Flag character to treat a left precision (strfmon) as,
521 or 0 if left precision not used. */
522 const int left_precision_char
;
523 /* Flag character to treat a precision (for strfmon, right precision) as,
524 or 0 if precision not used. */
525 const int precision_char
;
526 /* If a flag character has the effect of suppressing the conversion of
527 an argument ('*' in scanf), that flag character, otherwise 0. */
528 const int suppression_char
;
529 /* Flag character to treat a length modifier as (ignored if length
530 modifiers not used). Need not be placed in flag_chars for conversion
531 specifiers, but is used to check for bad combinations such as length
532 modifier with assignment suppression in scanf. */
533 const int length_code_char
;
534 /* Pointer to type of argument expected if '*' is used for a width,
535 or NULL if '*' not used for widths. */
536 tree
*const width_type
;
537 /* Pointer to type of argument expected if '*' is used for a precision,
538 or NULL if '*' not used for precisions. */
539 tree
*const precision_type
;
543 /* Structure describing details of a type expected in format checking,
544 and the type to check against it. */
545 typedef struct format_wanted_type
547 /* The type wanted. */
549 /* The name of this type to use in diagnostics. */
550 const char *wanted_type_name
;
551 /* The level of indirection through pointers at which this type occurs. */
553 /* Whether, when pointer_count is 1, to allow any character type when
554 pedantic, rather than just the character or void type specified. */
555 int char_lenient_flag
;
556 /* Whether the argument, dereferenced once, is written into and so the
557 argument must not be a pointer to a const-qualified type. */
559 /* Whether the argument, dereferenced once, is read from and so
560 must not be a NULL pointer. */
561 int reading_from_flag
;
562 /* If warnings should be of the form "field precision is not type int",
563 the name to use (in this case "field precision"), otherwise NULL,
564 for "%s format, %s arg" type messages. If (in an extension), this
565 is a pointer type, wanted_type_name should be set to include the
566 terminating '*' characters of the type name to give a correct
569 /* The actual parameter to check against the wanted type. */
571 /* The argument number of that parameter. */
573 /* The next type to check for this format conversion, or NULL if none. */
574 struct format_wanted_type
*next
;
575 } format_wanted_type
;
578 static const format_length_info printf_length_specs
[] =
580 { "h", FMT_LEN_h
, STD_C89
, "hh", FMT_LEN_hh
, STD_C99
},
581 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C9L
},
582 { "q", FMT_LEN_ll
, STD_EXT
, NULL
, 0, 0 },
583 { "L", FMT_LEN_L
, STD_C89
, NULL
, 0, 0 },
584 { "z", FMT_LEN_z
, STD_C99
, NULL
, 0, 0 },
585 { "Z", FMT_LEN_z
, STD_EXT
, NULL
, 0, 0 },
586 { "t", FMT_LEN_t
, STD_C99
, NULL
, 0, 0 },
587 { "j", FMT_LEN_j
, STD_C99
, NULL
, 0, 0 },
588 { NULL
, 0, 0, NULL
, 0, 0 }
592 /* This differs from printf_length_specs only in that "Z" is not accepted. */
593 static const format_length_info scanf_length_specs
[] =
595 { "h", FMT_LEN_h
, STD_C89
, "hh", FMT_LEN_hh
, STD_C99
},
596 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C9L
},
597 { "q", FMT_LEN_ll
, STD_EXT
, NULL
, 0, 0 },
598 { "L", FMT_LEN_L
, STD_C89
, NULL
, 0, 0 },
599 { "z", FMT_LEN_z
, STD_C99
, NULL
, 0, 0 },
600 { "t", FMT_LEN_t
, STD_C99
, NULL
, 0, 0 },
601 { "j", FMT_LEN_j
, STD_C99
, NULL
, 0, 0 },
602 { NULL
, 0, 0, NULL
, 0, 0 }
606 /* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings
607 make no sense for a format type not part of any C standard version. */
608 static const format_length_info strfmon_length_specs
[] =
610 /* A GNU extension. */
611 { "L", FMT_LEN_L
, STD_C89
, NULL
, 0, 0 },
612 { NULL
, 0, 0, NULL
, 0, 0 }
615 static const format_flag_spec printf_flag_specs
[] =
617 { ' ', 0, 0, N_("` ' flag"), N_("the ` ' printf flag"), STD_C89
},
618 { '+', 0, 0, N_("`+' flag"), N_("the `+' printf flag"), STD_C89
},
619 { '#', 0, 0, N_("`#' flag"), N_("the `#' printf flag"), STD_C89
},
620 { '0', 0, 0, N_("`0' flag"), N_("the `0' printf flag"), STD_C89
},
621 { '-', 0, 0, N_("`-' flag"), N_("the `-' printf flag"), STD_C89
},
622 { '\'', 0, 0, N_("`'' flag"), N_("the `'' printf flag"), STD_EXT
},
623 { 'I', 0, 0, N_("`I' flag"), N_("the `I' printf flag"), STD_EXT
},
624 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89
},
625 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89
},
626 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89
},
627 { 0, 0, 0, NULL
, NULL
, 0 }
631 static const format_flag_pair printf_flag_pairs
[] =
635 { '0', 'p', 1, 'i' },
640 static const format_flag_spec scanf_flag_specs
[] =
642 { '*', 0, 0, N_("assignment suppression"), N_("assignment suppression"), STD_C89
},
643 { 'a', 0, 0, N_("`a' flag"), N_("the `a' scanf flag"), STD_EXT
},
644 { 'w', 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89
},
645 { 'L', 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89
},
646 { '\'', 0, 0, N_("`'' flag"), N_("the `'' scanf flag"), STD_EXT
},
647 { 'I', 0, 0, N_("`I' flag"), N_("the `I' scanf flag"), STD_EXT
},
648 { 0, 0, 0, NULL
, NULL
, 0 }
652 static const format_flag_pair scanf_flag_pairs
[] =
659 static const format_flag_spec strftime_flag_specs
[] =
661 { '_', 0, 0, N_("`_' flag"), N_("the `_' strftime flag"), STD_EXT
},
662 { '-', 0, 0, N_("`-' flag"), N_("the `-' strftime flag"), STD_EXT
},
663 { '0', 0, 0, N_("`0' flag"), N_("the `0' strftime flag"), STD_EXT
},
664 { '^', 0, 0, N_("`^' flag"), N_("the `^' strftime flag"), STD_EXT
},
665 { '#', 0, 0, N_("`#' flag"), N_("the `#' strftime flag"), STD_EXT
},
666 { 'w', 0, 0, N_("field width"), N_("field width in strftime format"), STD_EXT
},
667 { 'E', 0, 0, N_("`E' modifier"), N_("the `E' strftime modifier"), STD_C99
},
668 { 'O', 0, 0, N_("`O' modifier"), N_("the `O' strftime modifier"), STD_C99
},
669 { 'O', 'o', 0, NULL
, N_("the `O' modifier"), STD_EXT
},
670 { 0, 0, 0, NULL
, NULL
, 0 }
674 static const format_flag_pair strftime_flag_pairs
[] =
685 static const format_flag_spec strfmon_flag_specs
[] =
687 { '=', 0, 1, N_("fill character"), N_("fill character in strfmon format"), STD_C89
},
688 { '^', 0, 0, N_("`^' flag"), N_("the `^' strfmon flag"), STD_C89
},
689 { '+', 0, 0, N_("`+' flag"), N_("the `+' strfmon flag"), STD_C89
},
690 { '(', 0, 0, N_("`(' flag"), N_("the `(' strfmon flag"), STD_C89
},
691 { '!', 0, 0, N_("`!' flag"), N_("the `!' strfmon flag"), STD_C89
},
692 { '-', 0, 0, N_("`-' flag"), N_("the `-' strfmon flag"), STD_C89
},
693 { 'w', 0, 0, N_("field width"), N_("field width in strfmon format"), STD_C89
},
694 { '#', 0, 0, N_("left precision"), N_("left precision in strfmon format"), STD_C89
},
695 { 'p', 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89
},
696 { 'L', 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89
},
697 { 0, 0, 0, NULL
, NULL
, 0 }
700 static const format_flag_pair strfmon_flag_pairs
[] =
707 #define T_I &integer_type_node
708 #define T89_I { STD_C89, NULL, T_I }
709 #define T99_I { STD_C99, NULL, T_I }
710 #define T_L &long_integer_type_node
711 #define T89_L { STD_C89, NULL, T_L }
712 #define T_LL &long_long_integer_type_node
713 #define T9L_LL { STD_C9L, NULL, T_LL }
714 #define TEX_LL { STD_EXT, NULL, T_LL }
715 #define T_S &short_integer_type_node
716 #define T89_S { STD_C89, NULL, T_S }
717 #define T_UI &unsigned_type_node
718 #define T89_UI { STD_C89, NULL, T_UI }
719 #define T99_UI { STD_C99, NULL, T_UI }
720 #define T_UL &long_unsigned_type_node
721 #define T89_UL { STD_C89, NULL, T_UL }
722 #define T_ULL &long_long_unsigned_type_node
723 #define T9L_ULL { STD_C9L, NULL, T_ULL }
724 #define TEX_ULL { STD_EXT, NULL, T_ULL }
725 #define T_US &short_unsigned_type_node
726 #define T89_US { STD_C89, NULL, T_US }
727 #define T_F &float_type_node
728 #define T89_F { STD_C89, NULL, T_F }
729 #define T99_F { STD_C99, NULL, T_F }
730 #define T_D &double_type_node
731 #define T89_D { STD_C89, NULL, T_D }
732 #define T99_D { STD_C99, NULL, T_D }
733 #define T_LD &long_double_type_node
734 #define T89_LD { STD_C89, NULL, T_LD }
735 #define T99_LD { STD_C99, NULL, T_LD }
736 #define T_C &char_type_node
737 #define T89_C { STD_C89, NULL, T_C }
738 #define T_SC &signed_char_type_node
739 #define T99_SC { STD_C99, NULL, T_SC }
740 #define T_UC &unsigned_char_type_node
741 #define T99_UC { STD_C99, NULL, T_UC }
742 #define T_V &void_type_node
743 #define T89_V { STD_C89, NULL, T_V }
744 #define T_W &wchar_type_node
745 #define T94_W { STD_C94, "wchar_t", T_W }
746 #define TEX_W { STD_EXT, "wchar_t", T_W }
747 #define T_WI &wint_type_node
748 #define T94_WI { STD_C94, "wint_t", T_WI }
749 #define TEX_WI { STD_EXT, "wint_t", T_WI }
750 #define T_ST &c_size_type_node
751 #define T99_ST { STD_C99, "size_t", T_ST }
752 #define T_SST &signed_size_type_node
753 #define T99_SST { STD_C99, "signed size_t", T_SST }
754 #define T_PD &ptrdiff_type_node
755 #define T99_PD { STD_C99, "ptrdiff_t", T_PD }
756 #define T_UPD &unsigned_ptrdiff_type_node
757 #define T99_UPD { STD_C99, "unsigned ptrdiff_t", T_UPD }
758 #define T_IM &intmax_type_node
759 #define T99_IM { STD_C99, "intmax_t", T_IM }
760 #define T_UIM &uintmax_type_node
761 #define T99_UIM { STD_C99, "uintmax_t", T_UIM }
763 static const format_char_info print_char_table
[] =
765 /* C89 conversion specifiers. */
766 { "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" },
767 { "oxX", 0, STD_C89
, { T89_UI
, T99_UC
, T89_US
, T89_UL
, T9L_ULL
, TEX_ULL
, T99_ST
, T99_UPD
, T99_UIM
}, "-wp0#", "i" },
768 { "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" },
769 { "fgG", 0, STD_C89
, { T89_D
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T89_LD
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +#'", "" },
770 { "eE", 0, STD_C89
, { T89_D
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T89_LD
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +#", "" },
771 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T94_WI
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "" },
772 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "cR" },
773 { "p", 1, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "c" },
774 { "n", 1, STD_C89
, { T89_I
, T99_SC
, T89_S
, T89_L
, T9L_LL
, BADLEN
, T99_SST
, T99_PD
, T99_IM
}, "", "W" },
775 /* C99 conversion specifiers. */
776 { "F", 0, STD_C99
, { T99_D
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T99_LD
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +#'", "" },
777 { "aA", 0, STD_C99
, { T99_D
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T99_LD
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +#", "" },
778 /* X/Open conversion specifiers. */
779 { "C", 0, STD_EXT
, { TEX_WI
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "" },
780 { "S", 1, STD_EXT
, { TEX_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "R" },
781 /* GNU conversion specifiers. */
782 { "m", 0, STD_EXT
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "" },
783 { NULL
, 0, 0, NOLENGTHS
, NULL
, NULL
}
786 static const format_char_info scan_char_table
[] =
788 /* C89 conversion specifiers. */
789 { "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" },
790 { "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" },
791 { "oxX", 1, STD_C89
, { T89_UI
, T99_UC
, T89_US
, T89_UL
, T9L_ULL
, TEX_ULL
, T99_ST
, T99_UPD
, T99_UIM
}, "*w", "W" },
792 { "efgEG", 1, STD_C89
, { T89_F
, BADLEN
, BADLEN
, T89_D
, BADLEN
, T89_LD
, BADLEN
, BADLEN
, BADLEN
}, "*w'", "W" },
793 { "c", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*w", "cW" },
794 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*aw", "cW" },
795 { "[", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*aw", "cW[" },
796 { "p", 2, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*w", "W" },
797 { "n", 1, STD_C89
, { T89_I
, T99_SC
, T89_S
, T89_L
, T9L_LL
, BADLEN
, T99_SST
, T99_PD
, T99_IM
}, "", "W" },
798 /* C99 conversion specifiers. */
799 { "FaA", 1, STD_C99
, { T99_F
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T99_LD
, BADLEN
, BADLEN
, BADLEN
}, "*w'", "W" },
800 /* X/Open conversion specifiers. */
801 { "C", 1, STD_EXT
, { TEX_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*w", "W" },
802 { "S", 1, STD_EXT
, { TEX_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*aw", "W" },
803 { NULL
, 0, 0, NOLENGTHS
, NULL
, NULL
}
806 static const format_char_info time_char_table
[] =
808 /* C89 conversion specifiers. */
809 { "ABZab", 0, STD_C89
, NOLENGTHS
, "^#", "" },
810 { "cx", 0, STD_C89
, NOLENGTHS
, "E", "3" },
811 { "HIMSUWdmw", 0, STD_C89
, NOLENGTHS
, "-_0Ow", "" },
812 { "j", 0, STD_C89
, NOLENGTHS
, "-_0Ow", "o" },
813 { "p", 0, STD_C89
, NOLENGTHS
, "#", "" },
814 { "X", 0, STD_C89
, NOLENGTHS
, "E", "" },
815 { "y", 0, STD_C89
, NOLENGTHS
, "EO-_0w", "4" },
816 { "Y", 0, STD_C89
, NOLENGTHS
, "-_0EOw", "o" },
817 { "%", 0, STD_C89
, NOLENGTHS
, "", "" },
818 /* C99 conversion specifiers. */
819 { "C", 0, STD_C99
, NOLENGTHS
, "-_0EOw", "o" },
820 { "D", 0, STD_C99
, NOLENGTHS
, "", "2" },
821 { "eVu", 0, STD_C99
, NOLENGTHS
, "-_0Ow", "" },
822 { "FRTnrt", 0, STD_C99
, NOLENGTHS
, "", "" },
823 { "g", 0, STD_C99
, NOLENGTHS
, "O-_0w", "2o" },
824 { "G", 0, STD_C99
, NOLENGTHS
, "-_0Ow", "o" },
825 { "h", 0, STD_C99
, NOLENGTHS
, "^#", "" },
826 { "z", 0, STD_C99
, NOLENGTHS
, "O", "o" },
827 /* GNU conversion specifiers. */
828 { "kls", 0, STD_EXT
, NOLENGTHS
, "-_0Ow", "" },
829 { "P", 0, STD_EXT
, NOLENGTHS
, "", "" },
830 { NULL
, 0, 0, NOLENGTHS
, NULL
, NULL
}
833 static const format_char_info monetary_char_table
[] =
835 { "in", 0, STD_C89
, { T89_D
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, T89_LD
, BADLEN
, BADLEN
, BADLEN
}, "=^+(!-w#p", "" },
836 { NULL
, 0, 0, NOLENGTHS
, NULL
, NULL
}
840 /* This must be in the same order as enum format_type. */
841 static const format_kind_info format_types
[] =
843 { "printf", printf_length_specs
, print_char_table
, " +#0-'I", NULL
,
844 printf_flag_specs
, printf_flag_pairs
,
845 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_DOLLAR_MULTIPLE
|FMT_FLAG_USE_DOLLAR
|FMT_FLAG_EMPTY_PREC_OK
,
847 &integer_type_node
, &integer_type_node
849 { "scanf", scanf_length_specs
, scan_char_table
, "*'I", NULL
,
850 scanf_flag_specs
, scanf_flag_pairs
,
851 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_SCANF_A_KLUDGE
|FMT_FLAG_USE_DOLLAR
|FMT_FLAG_ZERO_WIDTH_BAD
,
855 { "strftime", NULL
, time_char_table
, "_-0^#", "EO",
856 strftime_flag_specs
, strftime_flag_pairs
,
857 FMT_FLAG_FANCY_PERCENT_OK
, 'w', 0, 0, 0, 0,
860 { "strfmon", strfmon_length_specs
, monetary_char_table
, "=^+(!-", NULL
,
861 strfmon_flag_specs
, strfmon_flag_pairs
,
862 FMT_FLAG_ARG_CONVERT
, 'w', '#', 'p', 0, 'L',
868 /* Structure detailing the results of checking a format function call
869 where the format expression may be a conditional expression with
870 many leaves resulting from nested conditional expressions. */
873 /* Number of leaves of the format argument that could not be checked
874 as they were not string literals. */
875 int number_non_literal
;
876 /* Number of leaves of the format argument that were null pointers or
877 string literals, but had extra format arguments. */
878 int number_extra_args
;
879 /* Number of leaves of the format argument that were null pointers or
880 string literals, but had extra format arguments and used $ operand
882 int number_dollar_extra_args
;
883 /* Number of leaves of the format argument that were wide string
886 /* Number of leaves of the format argument that were empty strings. */
888 /* Number of leaves of the format argument that were unterminated
890 int number_unterminated
;
891 /* Number of leaves of the format argument that were not counted above. */
893 } format_check_results
;
895 static void check_format_info
PARAMS ((int *, function_format_info
*, tree
));
896 static void check_format_info_recurse
PARAMS ((int *, format_check_results
*,
897 function_format_info
*, tree
,
898 tree
, unsigned HOST_WIDE_INT
));
899 static void check_format_info_main
PARAMS ((int *, format_check_results
*,
900 function_format_info
*,
901 const char *, int, tree
,
902 unsigned HOST_WIDE_INT
));
903 static void status_warning
PARAMS ((int *, const char *, ...))
906 static void init_dollar_format_checking
PARAMS ((int, tree
));
907 static int maybe_read_dollar_number
PARAMS ((int *, const char **, int,
909 const format_kind_info
*));
910 static void finish_dollar_format_checking
PARAMS ((int *, format_check_results
*));
912 static const format_flag_spec
*get_flag_spec
PARAMS ((const format_flag_spec
*,
915 static void check_format_types
PARAMS ((int *, format_wanted_type
*));
917 /* Decode a format type from a string, returning the type, or
918 format_type_error if not valid, in which case the caller should print an
920 static enum format_type
921 decode_format_type (s
)
927 for (i
= 0; i
< (int) format_type_error
; i
++)
930 if (!strcmp (s
, format_types
[i
].name
))
932 alen
= strlen (format_types
[i
].name
);
933 if (slen
== alen
+ 4 && s
[0] == '_' && s
[1] == '_'
934 && s
[slen
- 1] == '_' && s
[slen
- 2] == '_'
935 && !strncmp (s
+ 2, format_types
[i
].name
, alen
))
938 return ((enum format_type
) i
);
942 /* Check the argument list of a call to printf, scanf, etc.
943 ATTRS are the attributes on the function type.
944 PARAMS is the list of argument values. Also, if -Wmissing-format-attribute,
945 warn for calls to vprintf or vscanf in functions with no such format
946 attribute themselves. */
949 check_function_format (status
, attrs
, params
)
956 /* See if this function has any format attributes. */
957 for (a
= attrs
; a
; a
= TREE_CHAIN (a
))
959 if (is_attribute_p ("format", TREE_PURPOSE (a
)))
962 function_format_info info
;
963 decode_format_attr (TREE_VALUE (a
), &info
, 1);
964 check_format_info (status
, &info
, params
);
965 if (warn_missing_format_attribute
&& info
.first_arg_num
== 0
966 && (format_types
[info
.format_type
].flags
967 & (int) FMT_FLAG_ARG_CONVERT
))
970 for (c
= TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl
));
973 if (is_attribute_p ("format", TREE_PURPOSE (c
))
974 && (decode_format_type (IDENTIFIER_POINTER
975 (TREE_VALUE (TREE_VALUE (c
))))
976 == info
.format_type
))
980 /* Check if the current function has a parameter to which
981 the format attribute could be attached; if not, it
982 can't be a candidate for a format attribute, despite
983 the vprintf-like or vscanf-like call. */
985 for (args
= DECL_ARGUMENTS (current_function_decl
);
987 args
= TREE_CHAIN (args
))
989 if (TREE_CODE (TREE_TYPE (args
)) == POINTER_TYPE
990 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args
)))
995 warning ("function might be possible candidate for `%s' format attribute",
996 format_types
[info
.format_type
].name
);
1003 /* This function replaces `warning' inside the printf format checking
1004 functions. If the `status' parameter is non-NULL, then it is
1005 dereferenced and set to 1 whenever a warning is caught. Otherwise
1006 it warns as usual by replicating the innards of the warning
1007 function from diagnostic.c. */
1009 status_warning
VPARAMS ((int *status
, const char *msgid
, ...))
1011 diagnostic_context dc
;
1013 VA_OPEN (ap
, msgid
);
1014 VA_FIXEDARG (ap
, int *, status
);
1015 VA_FIXEDARG (ap
, const char *, msgid
);
1021 /* This duplicates the warning function behavior. */
1022 set_diagnostic_context
1023 (&dc
, msgid
, &ap
, input_filename
, lineno
, /* warn = */ 1);
1024 report_diagnostic (&dc
);
1030 /* Variables used by the checking of $ operand number formats. */
1031 static char *dollar_arguments_used
= NULL
;
1032 static int dollar_arguments_alloc
= 0;
1033 static int dollar_arguments_count
;
1034 static int dollar_first_arg_num
;
1035 static int dollar_max_arg_used
;
1036 static int dollar_format_warned
;
1038 /* Initialize the checking for a format string that may contain $
1039 parameter number specifications; we will need to keep track of whether
1040 each parameter has been used. FIRST_ARG_NUM is the number of the first
1041 argument that is a parameter to the format, or 0 for a vprintf-style
1042 function; PARAMS is the list of arguments starting at this argument. */
1045 init_dollar_format_checking (first_arg_num
, params
)
1049 dollar_first_arg_num
= first_arg_num
;
1050 dollar_arguments_count
= 0;
1051 dollar_max_arg_used
= 0;
1052 dollar_format_warned
= 0;
1053 if (first_arg_num
> 0)
1057 dollar_arguments_count
++;
1058 params
= TREE_CHAIN (params
);
1061 if (dollar_arguments_alloc
< dollar_arguments_count
)
1063 if (dollar_arguments_used
)
1064 free (dollar_arguments_used
);
1065 dollar_arguments_alloc
= dollar_arguments_count
;
1066 dollar_arguments_used
= xmalloc (dollar_arguments_alloc
);
1068 if (dollar_arguments_alloc
)
1069 memset (dollar_arguments_used
, 0, dollar_arguments_alloc
);
1073 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
1074 is set, it is an error if one is not found; otherwise, it is OK. If
1075 such a number is found, check whether it is within range and mark that
1076 numbered operand as being used for later checking. Returns the operand
1077 number if found and within range, zero if no such number was found and
1078 this is OK, or -1 on error. PARAMS points to the first operand of the
1079 format; PARAM_PTR is made to point to the parameter referred to. If
1080 a $ format is found, *FORMAT is updated to point just after it. */
1083 maybe_read_dollar_number (status
, format
, dollar_needed
, params
, param_ptr
,
1086 const char **format
;
1090 const format_kind_info
*fki
;
1094 const char *fcp
= *format
;
1095 if (! ISDIGIT (*fcp
))
1099 status_warning (status
, "missing $ operand number in format");
1107 while (ISDIGIT (*fcp
))
1110 nargnum
= 10 * argnum
+ (*fcp
- '0');
1111 if (nargnum
< 0 || nargnum
/ 10 != argnum
)
1120 status_warning (status
, "missing $ operand number in format");
1127 if (pedantic
&& !dollar_format_warned
)
1129 status_warning (status
,
1130 "%s does not support %%n$ operand number formats",
1131 C_STD_NAME (STD_EXT
));
1132 dollar_format_warned
= 1;
1134 if (overflow_flag
|| argnum
== 0
1135 || (dollar_first_arg_num
&& argnum
> dollar_arguments_count
))
1137 status_warning (status
, "operand number out of range in format");
1140 if (argnum
> dollar_max_arg_used
)
1141 dollar_max_arg_used
= argnum
;
1142 /* For vprintf-style functions we may need to allocate more memory to
1143 track which arguments are used. */
1144 while (dollar_arguments_alloc
< dollar_max_arg_used
)
1147 nalloc
= 2 * dollar_arguments_alloc
+ 16;
1148 dollar_arguments_used
= xrealloc (dollar_arguments_used
, nalloc
);
1149 memset (dollar_arguments_used
+ dollar_arguments_alloc
, 0,
1150 nalloc
- dollar_arguments_alloc
);
1151 dollar_arguments_alloc
= nalloc
;
1153 if (!(fki
->flags
& (int) FMT_FLAG_DOLLAR_MULTIPLE
)
1154 && dollar_arguments_used
[argnum
- 1] == 1)
1156 dollar_arguments_used
[argnum
- 1] = 2;
1157 status_warning (status
,
1158 "format argument %d used more than once in %s format",
1162 dollar_arguments_used
[argnum
- 1] = 1;
1163 if (dollar_first_arg_num
)
1166 *param_ptr
= params
;
1167 for (i
= 1; i
< argnum
&& *param_ptr
!= 0; i
++)
1168 *param_ptr
= TREE_CHAIN (*param_ptr
);
1170 if (*param_ptr
== 0)
1172 /* This case shouldn't be caught here. */
1182 /* Finish the checking for a format string that used $ operand number formats
1183 instead of non-$ formats. We check for unused operands before used ones
1184 (a serious error, since the implementation of the format function
1185 can't know what types to pass to va_arg to find the later arguments).
1186 and for unused operands at the end of the format (if we know how many
1187 arguments the format had, so not for vprintf). If there were operand
1188 numbers out of range on a non-vprintf-style format, we won't have reached
1192 finish_dollar_format_checking (status
, res
)
1194 format_check_results
*res
;
1197 for (i
= 0; i
< dollar_max_arg_used
; i
++)
1199 if (!dollar_arguments_used
[i
])
1200 status_warning (status
, "format argument %d unused before used argument %d in $-style format",
1201 i
+ 1, dollar_max_arg_used
);
1203 if (dollar_first_arg_num
&& dollar_max_arg_used
< dollar_arguments_count
)
1205 res
->number_other
--;
1206 res
->number_dollar_extra_args
++;
1211 /* Retrieve the specification for a format flag. SPEC contains the
1212 specifications for format flags for the applicable kind of format.
1213 FLAG is the flag in question. If PREDICATES is NULL, the basic
1214 spec for that flag must be retrieved and this function aborts if
1215 it cannot be found. If PREDICATES is not NULL, it is a string listing
1216 possible predicates for the spec entry; if an entry predicated on any
1217 of these is found, it is returned, otherwise NULL is returned. */
1219 static const format_flag_spec
*
1220 get_flag_spec (spec
, flag
, predicates
)
1221 const format_flag_spec
*spec
;
1223 const char *predicates
;
1226 for (i
= 0; spec
[i
].flag_char
!= 0; i
++)
1228 if (spec
[i
].flag_char
!= flag
)
1230 if (predicates
!= NULL
)
1232 if (spec
[i
].predicate
!= 0
1233 && strchr (predicates
, spec
[i
].predicate
) != 0)
1236 else if (spec
[i
].predicate
== 0)
1239 if (predicates
== NULL
)
1246 /* Check the argument list of a call to printf, scanf, etc.
1247 INFO points to the function_format_info structure.
1248 PARAMS is the list of argument values. */
1251 check_format_info (status
, info
, params
)
1253 function_format_info
*info
;
1256 unsigned HOST_WIDE_INT arg_num
;
1258 format_check_results res
;
1259 /* Skip to format argument. If the argument isn't available, there's
1260 no work for us to do; prototype checking will catch the problem. */
1261 for (arg_num
= 1; ; ++arg_num
)
1265 if (arg_num
== info
->format_num
)
1267 params
= TREE_CHAIN (params
);
1269 format_tree
= TREE_VALUE (params
);
1270 params
= TREE_CHAIN (params
);
1271 if (format_tree
== 0)
1274 res
.number_non_literal
= 0;
1275 res
.number_extra_args
= 0;
1276 res
.number_dollar_extra_args
= 0;
1277 res
.number_wide
= 0;
1278 res
.number_empty
= 0;
1279 res
.number_unterminated
= 0;
1280 res
.number_other
= 0;
1282 check_format_info_recurse (status
, &res
, info
, format_tree
, params
, arg_num
);
1284 if (res
.number_non_literal
> 0)
1286 /* Functions taking a va_list normally pass a non-literal format
1287 string. These functions typically are declared with
1288 first_arg_num == 0, so avoid warning in those cases. */
1289 if (!(format_types
[info
->format_type
].flags
& (int) FMT_FLAG_ARG_CONVERT
))
1291 /* For strftime-like formats, warn for not checking the format
1292 string; but there are no arguments to check. */
1293 if (warn_format_nonliteral
)
1294 status_warning (status
, "format not a string literal, format string not checked");
1296 else if (info
->first_arg_num
!= 0)
1298 /* If there are no arguments for the format at all, we may have
1299 printf (foo) which is likely to be a security hole. */
1300 while (arg_num
+ 1 < info
->first_arg_num
)
1304 params
= TREE_CHAIN (params
);
1307 if (params
== 0 && (warn_format_nonliteral
|| warn_format_security
))
1308 status_warning (status
, "format not a string literal and no format arguments");
1309 else if (warn_format_nonliteral
)
1310 status_warning (status
, "format not a string literal, argument types not checked");
1314 /* If there were extra arguments to the format, normally warn. However,
1315 the standard does say extra arguments are ignored, so in the specific
1316 case where we have multiple leaves (conditional expressions or
1317 ngettext) allow extra arguments if at least one leaf didn't have extra
1318 arguments, but was otherwise OK (either non-literal or checked OK).
1319 If the format is an empty string, this should be counted similarly to the
1320 case of extra format arguments. */
1321 if (res
.number_extra_args
> 0 && res
.number_non_literal
== 0
1322 && res
.number_other
== 0 && warn_format_extra_args
)
1323 status_warning (status
, "too many arguments for format");
1324 if (res
.number_dollar_extra_args
> 0 && res
.number_non_literal
== 0
1325 && res
.number_other
== 0 && warn_format_extra_args
)
1326 status_warning (status
, "unused arguments in $-style format");
1327 if (res
.number_empty
> 0 && res
.number_non_literal
== 0
1328 && res
.number_other
== 0)
1329 status_warning (status
, "zero-length format string");
1331 if (res
.number_wide
> 0)
1332 status_warning (status
, "format is a wide character string");
1334 if (res
.number_unterminated
> 0)
1335 status_warning (status
, "unterminated format string");
1339 /* Recursively check a call to a format function. FORMAT_TREE is the
1340 format parameter, which may be a conditional expression in which
1341 both halves should be checked. ARG_NUM is the number of the
1342 format argument; PARAMS points just after it in the argument list. */
1345 check_format_info_recurse (status
, res
, info
, format_tree
, params
, arg_num
)
1347 format_check_results
*res
;
1348 function_format_info
*info
;
1351 unsigned HOST_WIDE_INT arg_num
;
1354 HOST_WIDE_INT offset
;
1355 const char *format_chars
;
1356 tree array_size
= 0;
1359 if (TREE_CODE (format_tree
) == NOP_EXPR
)
1361 /* Strip coercion. */
1362 check_format_info_recurse (status
, res
, info
,
1363 TREE_OPERAND (format_tree
, 0), params
,
1368 if (TREE_CODE (format_tree
) == CALL_EXPR
)
1370 tree type
= TREE_TYPE (TREE_TYPE (TREE_OPERAND (format_tree
, 0)));
1372 bool found_format_arg
= false;
1374 /* See if this is a call to a known internationalization function
1375 that modifies the format arg. Such a function may have multiple
1376 format_arg attributes (for example, ngettext). */
1378 for (attrs
= TYPE_ATTRIBUTES (type
);
1380 attrs
= TREE_CHAIN (attrs
))
1381 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs
)))
1384 tree format_num_expr
;
1388 /* Extract the argument number, which was previously checked
1390 format_num_expr
= TREE_VALUE (TREE_VALUE (attrs
));
1391 while (TREE_CODE (format_num_expr
) == NOP_EXPR
1392 || TREE_CODE (format_num_expr
) == CONVERT_EXPR
1393 || TREE_CODE (format_num_expr
) == NON_LVALUE_EXPR
)
1394 format_num_expr
= TREE_OPERAND (format_num_expr
, 0);
1396 if (TREE_CODE (format_num_expr
) != INTEGER_CST
1397 || TREE_INT_CST_HIGH (format_num_expr
) != 0)
1400 format_num
= TREE_INT_CST_LOW (format_num_expr
);
1402 for (inner_args
= TREE_OPERAND (format_tree
, 1), i
= 1;
1404 inner_args
= TREE_CHAIN (inner_args
), i
++)
1405 if (i
== format_num
)
1407 check_format_info_recurse (status
, res
, info
,
1408 TREE_VALUE (inner_args
), params
,
1410 found_format_arg
= true;
1415 /* If we found a format_arg attribute and did a recursive check,
1416 we are done with checking this format string. Otherwise, we
1417 continue and this will count as a non-literal format string. */
1418 if (found_format_arg
)
1422 if (TREE_CODE (format_tree
) == COND_EXPR
)
1424 /* Check both halves of the conditional expression. */
1425 check_format_info_recurse (status
, res
, info
,
1426 TREE_OPERAND (format_tree
, 1), params
,
1428 check_format_info_recurse (status
, res
, info
,
1429 TREE_OPERAND (format_tree
, 2), params
,
1434 if (integer_zerop (format_tree
))
1436 /* FIXME: this warning should go away once Marc Espie's
1437 __attribute__((nonnull)) patch is in. Instead, checking for
1438 nonnull attributes should probably change this function to act
1439 specially if info == NULL and add a res->number_null entry for
1440 that case, or maybe add a function pointer to be called at
1441 the end instead of hardcoding check_format_info_main. */
1442 status_warning (status
, "null format string");
1444 /* Skip to first argument to check, so we can see if this format
1445 has any arguments (it shouldn't). */
1446 while (arg_num
+ 1 < info
->first_arg_num
)
1450 params
= TREE_CHAIN (params
);
1455 res
->number_other
++;
1457 res
->number_extra_args
++;
1463 if (TREE_CODE (format_tree
) == PLUS_EXPR
)
1467 arg0
= TREE_OPERAND (format_tree
, 0);
1468 arg1
= TREE_OPERAND (format_tree
, 1);
1471 if (TREE_CODE (arg1
) == INTEGER_CST
)
1473 else if (TREE_CODE (arg0
) == INTEGER_CST
)
1480 res
->number_non_literal
++;
1483 if (!host_integerp (arg1
, 1))
1485 res
->number_non_literal
++;
1489 offset
= TREE_INT_CST_LOW (arg1
);
1491 if (TREE_CODE (format_tree
) != ADDR_EXPR
)
1493 res
->number_non_literal
++;
1496 format_tree
= TREE_OPERAND (format_tree
, 0);
1497 if (TREE_CODE (format_tree
) == VAR_DECL
1498 && TREE_CODE (TREE_TYPE (format_tree
)) == ARRAY_TYPE
1499 && (array_init
= decl_constant_value (format_tree
)) != format_tree
1500 && TREE_CODE (array_init
) == STRING_CST
)
1502 /* Extract the string constant initializer. Note that this may include
1503 a trailing NUL character that is not in the array (e.g.
1504 const char a[3] = "foo";). */
1505 array_size
= DECL_SIZE_UNIT (format_tree
);
1506 format_tree
= array_init
;
1508 if (TREE_CODE (format_tree
) != STRING_CST
)
1510 res
->number_non_literal
++;
1513 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree
))) != char_type_node
)
1518 format_chars
= TREE_STRING_POINTER (format_tree
);
1519 format_length
= TREE_STRING_LENGTH (format_tree
);
1520 if (array_size
!= 0)
1522 /* Variable length arrays can't be initialized. */
1523 if (TREE_CODE (array_size
) != INTEGER_CST
)
1525 if (host_integerp (array_size
, 0))
1527 HOST_WIDE_INT array_size_value
= TREE_INT_CST_LOW (array_size
);
1528 if (array_size_value
> 0
1529 && array_size_value
== (int) array_size_value
1530 && format_length
> array_size_value
)
1531 format_length
= array_size_value
;
1536 if (offset
>= format_length
)
1538 res
->number_non_literal
++;
1541 format_chars
+= offset
;
1542 format_length
-= offset
;
1544 if (format_length
< 1)
1546 res
->number_unterminated
++;
1549 if (format_length
== 1)
1551 res
->number_empty
++;
1554 if (format_chars
[--format_length
] != 0)
1556 res
->number_unterminated
++;
1560 /* Skip to first argument to check. */
1561 while (arg_num
+ 1 < info
->first_arg_num
)
1565 params
= TREE_CHAIN (params
);
1568 /* Provisionally increment res->number_other; check_format_info_main
1569 will decrement it if it finds there are extra arguments, but this way
1570 need not adjust it for every return. */
1571 res
->number_other
++;
1572 check_format_info_main (status
, res
, info
, format_chars
, format_length
,
1577 /* Do the main part of checking a call to a format function. FORMAT_CHARS
1578 is the NUL-terminated format string (which at this point may contain
1579 internal NUL characters); FORMAT_LENGTH is its length (excluding the
1580 terminating NUL character). ARG_NUM is one less than the number of
1581 the first format argument to check; PARAMS points to that format
1582 argument in the list of arguments. */
1585 check_format_info_main (status
, res
, info
, format_chars
, format_length
,
1588 format_check_results
*res
;
1589 function_format_info
*info
;
1590 const char *format_chars
;
1593 unsigned HOST_WIDE_INT arg_num
;
1595 const char *orig_format_chars
= format_chars
;
1596 tree first_fillin_param
= params
;
1598 const format_kind_info
*fki
= &format_types
[info
->format_type
];
1599 const format_flag_spec
*flag_specs
= fki
->flag_specs
;
1600 const format_flag_pair
*bad_flag_pairs
= fki
->bad_flag_pairs
;
1602 /* -1 if no conversions taking an operand have been found; 0 if one has
1603 and it didn't use $; 1 if $ formats are in use. */
1604 int has_operand_number
= -1;
1606 init_dollar_format_checking (info
->first_arg_num
, first_fillin_param
);
1611 int suppressed
= FALSE
;
1612 const char *length_chars
= NULL
;
1613 enum format_lengths length_chars_val
= FMT_LEN_none
;
1614 enum format_std_version length_chars_std
= STD_C89
;
1618 int main_arg_num
= 0;
1619 tree main_arg_params
= 0;
1620 enum format_std_version wanted_type_std
;
1621 const char *wanted_type_name
;
1622 format_wanted_type width_wanted_type
;
1623 format_wanted_type precision_wanted_type
;
1624 format_wanted_type main_wanted_type
;
1625 format_wanted_type
*first_wanted_type
= NULL
;
1626 format_wanted_type
*last_wanted_type
= NULL
;
1627 const format_length_info
*fli
= NULL
;
1628 const format_char_info
*fci
= NULL
;
1629 char flag_chars
[256];
1631 if (*format_chars
== 0)
1633 if (format_chars
- orig_format_chars
!= format_length
)
1634 status_warning (status
, "embedded `\\0' in format");
1635 if (info
->first_arg_num
!= 0 && params
!= 0
1636 && has_operand_number
<= 0)
1638 res
->number_other
--;
1639 res
->number_extra_args
++;
1641 if (has_operand_number
> 0)
1642 finish_dollar_format_checking (status
, res
);
1645 if (*format_chars
++ != '%')
1647 if (*format_chars
== 0)
1649 status_warning (status
, "spurious trailing `%%' in format");
1652 if (*format_chars
== '%')
1659 if ((fki
->flags
& (int) FMT_FLAG_USE_DOLLAR
) && has_operand_number
!= 0)
1661 /* Possibly read a $ operand number at the start of the format.
1662 If one was previously used, one is required here. If one
1663 is not used here, we can't immediately conclude this is a
1664 format without them, since it could be printf %m or scanf %*. */
1666 opnum
= maybe_read_dollar_number (status
, &format_chars
, 0,
1668 &main_arg_params
, fki
);
1673 has_operand_number
= 1;
1674 main_arg_num
= opnum
+ info
->first_arg_num
- 1;
1678 /* Read any format flags, but do not yet validate them beyond removing
1679 duplicates, since in general validation depends on the rest of
1681 while (*format_chars
!= 0
1682 && strchr (fki
->flag_chars
, *format_chars
) != 0)
1684 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
1685 *format_chars
, NULL
);
1686 if (strchr (flag_chars
, *format_chars
) != 0)
1688 status_warning (status
, "repeated %s in format", _(s
->name
));
1692 i
= strlen (flag_chars
);
1693 flag_chars
[i
++] = *format_chars
;
1696 if (s
->skip_next_char
)
1699 if (*format_chars
== 0)
1701 status_warning (status
, "missing fill character at end of strfmon format");
1708 /* Read any format width, possibly * or *m$. */
1709 if (fki
->width_char
!= 0)
1711 if (fki
->width_type
!= NULL
&& *format_chars
== '*')
1713 i
= strlen (flag_chars
);
1714 flag_chars
[i
++] = fki
->width_char
;
1716 /* "...a field width...may be indicated by an asterisk.
1717 In this case, an int argument supplies the field width..." */
1721 status_warning (status
, "too few arguments for format");
1724 if (has_operand_number
!= 0)
1727 opnum
= maybe_read_dollar_number (status
, &format_chars
,
1728 has_operand_number
== 1,
1735 has_operand_number
= 1;
1736 arg_num
= opnum
+ info
->first_arg_num
- 1;
1739 has_operand_number
= 0;
1741 if (info
->first_arg_num
!= 0)
1743 cur_param
= TREE_VALUE (params
);
1744 if (has_operand_number
<= 0)
1746 params
= TREE_CHAIN (params
);
1749 width_wanted_type
.wanted_type
= *fki
->width_type
;
1750 width_wanted_type
.wanted_type_name
= NULL
;
1751 width_wanted_type
.pointer_count
= 0;
1752 width_wanted_type
.char_lenient_flag
= 0;
1753 width_wanted_type
.writing_in_flag
= 0;
1754 width_wanted_type
.reading_from_flag
= 0;
1755 width_wanted_type
.name
= _("field width");
1756 width_wanted_type
.param
= cur_param
;
1757 width_wanted_type
.arg_num
= arg_num
;
1758 width_wanted_type
.next
= NULL
;
1759 if (last_wanted_type
!= 0)
1760 last_wanted_type
->next
= &width_wanted_type
;
1761 if (first_wanted_type
== 0)
1762 first_wanted_type
= &width_wanted_type
;
1763 last_wanted_type
= &width_wanted_type
;
1768 /* Possibly read a numeric width. If the width is zero,
1769 we complain if appropriate. */
1770 int non_zero_width_char
= FALSE
;
1771 int found_width
= FALSE
;
1772 while (ISDIGIT (*format_chars
))
1775 if (*format_chars
!= '0')
1776 non_zero_width_char
= TRUE
;
1779 if (found_width
&& !non_zero_width_char
&&
1780 (fki
->flags
& (int) FMT_FLAG_ZERO_WIDTH_BAD
))
1781 status_warning (status
, "zero width in %s format",
1785 i
= strlen (flag_chars
);
1786 flag_chars
[i
++] = fki
->width_char
;
1792 /* Read any format left precision (must be a number, not *). */
1793 if (fki
->left_precision_char
!= 0 && *format_chars
== '#')
1796 i
= strlen (flag_chars
);
1797 flag_chars
[i
++] = fki
->left_precision_char
;
1799 if (!ISDIGIT (*format_chars
))
1800 status_warning (status
, "empty left precision in %s format",
1802 while (ISDIGIT (*format_chars
))
1806 /* Read any format precision, possibly * or *m$. */
1807 if (fki
->precision_char
!= 0 && *format_chars
== '.')
1810 i
= strlen (flag_chars
);
1811 flag_chars
[i
++] = fki
->precision_char
;
1813 if (fki
->precision_type
!= NULL
&& *format_chars
== '*')
1815 /* "...a...precision...may be indicated by an asterisk.
1816 In this case, an int argument supplies the...precision." */
1818 if (has_operand_number
!= 0)
1821 opnum
= maybe_read_dollar_number (status
, &format_chars
,
1822 has_operand_number
== 1,
1829 has_operand_number
= 1;
1830 arg_num
= opnum
+ info
->first_arg_num
- 1;
1833 has_operand_number
= 0;
1835 if (info
->first_arg_num
!= 0)
1839 status_warning (status
, "too few arguments for format");
1842 cur_param
= TREE_VALUE (params
);
1843 if (has_operand_number
<= 0)
1845 params
= TREE_CHAIN (params
);
1848 precision_wanted_type
.wanted_type
= *fki
->precision_type
;
1849 precision_wanted_type
.wanted_type_name
= NULL
;
1850 precision_wanted_type
.pointer_count
= 0;
1851 precision_wanted_type
.char_lenient_flag
= 0;
1852 precision_wanted_type
.writing_in_flag
= 0;
1853 precision_wanted_type
.reading_from_flag
= 0;
1854 precision_wanted_type
.name
= _("field precision");
1855 precision_wanted_type
.param
= cur_param
;
1856 precision_wanted_type
.arg_num
= arg_num
;
1857 precision_wanted_type
.next
= NULL
;
1858 if (last_wanted_type
!= 0)
1859 last_wanted_type
->next
= &precision_wanted_type
;
1860 if (first_wanted_type
== 0)
1861 first_wanted_type
= &precision_wanted_type
;
1862 last_wanted_type
= &precision_wanted_type
;
1867 if (!(fki
->flags
& (int) FMT_FLAG_EMPTY_PREC_OK
)
1868 && !ISDIGIT (*format_chars
))
1869 status_warning (status
, "empty precision in %s format",
1871 while (ISDIGIT (*format_chars
))
1876 /* Read any length modifier, if this kind of format has them. */
1877 fli
= fki
->length_char_specs
;
1878 length_chars
= NULL
;
1879 length_chars_val
= FMT_LEN_none
;
1880 length_chars_std
= STD_C89
;
1883 while (fli
->name
!= 0 && fli
->name
[0] != *format_chars
)
1888 if (fli
->double_name
!= 0 && fli
->name
[0] == *format_chars
)
1891 length_chars
= fli
->double_name
;
1892 length_chars_val
= fli
->double_index
;
1893 length_chars_std
= fli
->double_std
;
1897 length_chars
= fli
->name
;
1898 length_chars_val
= fli
->index
;
1899 length_chars_std
= fli
->std
;
1901 i
= strlen (flag_chars
);
1902 flag_chars
[i
++] = fki
->length_code_char
;
1907 /* Warn if the length modifier is non-standard. */
1908 if (ADJ_STD (length_chars_std
) > C_STD_VER
)
1909 status_warning (status
, "%s does not support the `%s' %s length modifier",
1910 C_STD_NAME (length_chars_std
), length_chars
,
1915 /* Read any modifier (strftime E/O). */
1916 if (fki
->modifier_chars
!= NULL
)
1918 while (*format_chars
!= 0
1919 && strchr (fki
->modifier_chars
, *format_chars
) != 0)
1921 if (strchr (flag_chars
, *format_chars
) != 0)
1923 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
1924 *format_chars
, NULL
);
1925 status_warning (status
, "repeated %s in format", _(s
->name
));
1929 i
= strlen (flag_chars
);
1930 flag_chars
[i
++] = *format_chars
;
1937 /* Handle the scanf allocation kludge. */
1938 if (fki
->flags
& (int) FMT_FLAG_SCANF_A_KLUDGE
)
1940 if (*format_chars
== 'a' && !flag_isoc99
)
1942 if (format_chars
[1] == 's' || format_chars
[1] == 'S'
1943 || format_chars
[1] == '[')
1945 /* `a' is used as a flag. */
1946 i
= strlen (flag_chars
);
1947 flag_chars
[i
++] = 'a';
1954 format_char
= *format_chars
;
1955 if (format_char
== 0
1956 || (!(fki
->flags
& (int) FMT_FLAG_FANCY_PERCENT_OK
)
1957 && format_char
== '%'))
1959 status_warning (status
, "conversion lacks type at end of format");
1963 fci
= fki
->conversion_specs
;
1964 while (fci
->format_chars
!= 0
1965 && strchr (fci
->format_chars
, format_char
) == 0)
1967 if (fci
->format_chars
== 0)
1969 if (ISGRAPH(format_char
))
1970 status_warning (status
, "unknown conversion type character `%c' in format",
1973 status_warning (status
, "unknown conversion type character 0x%x in format",
1979 if (ADJ_STD (fci
->std
) > C_STD_VER
)
1980 status_warning (status
, "%s does not support the `%%%c' %s format",
1981 C_STD_NAME (fci
->std
), format_char
, fki
->name
);
1984 /* Validate the individual flags used, removing any that are invalid. */
1987 for (i
= 0; flag_chars
[i
] != 0; i
++)
1989 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
1990 flag_chars
[i
], NULL
);
1991 flag_chars
[i
- d
] = flag_chars
[i
];
1992 if (flag_chars
[i
] == fki
->length_code_char
)
1994 if (strchr (fci
->flag_chars
, flag_chars
[i
]) == 0)
1996 status_warning (status
, "%s used with `%%%c' %s format",
1997 _(s
->name
), format_char
, fki
->name
);
2003 const format_flag_spec
*t
;
2004 if (ADJ_STD (s
->std
) > C_STD_VER
)
2005 status_warning (status
, "%s does not support %s",
2006 C_STD_NAME (s
->std
), _(s
->long_name
));
2007 t
= get_flag_spec (flag_specs
, flag_chars
[i
], fci
->flags2
);
2008 if (t
!= NULL
&& ADJ_STD (t
->std
) > ADJ_STD (s
->std
))
2010 const char *long_name
= (t
->long_name
!= NULL
2013 if (ADJ_STD (t
->std
) > C_STD_VER
)
2014 status_warning (status
, "%s does not support %s with the `%%%c' %s format",
2015 C_STD_NAME (t
->std
), _(long_name
),
2016 format_char
, fki
->name
);
2020 flag_chars
[i
- d
] = 0;
2023 if ((fki
->flags
& (int) FMT_FLAG_SCANF_A_KLUDGE
)
2024 && strchr (flag_chars
, 'a') != 0)
2027 if (fki
->suppression_char
2028 && strchr (flag_chars
, fki
->suppression_char
) != 0)
2031 /* Validate the pairs of flags used. */
2032 for (i
= 0; bad_flag_pairs
[i
].flag_char1
!= 0; i
++)
2034 const format_flag_spec
*s
, *t
;
2035 if (strchr (flag_chars
, bad_flag_pairs
[i
].flag_char1
) == 0)
2037 if (strchr (flag_chars
, bad_flag_pairs
[i
].flag_char2
) == 0)
2039 if (bad_flag_pairs
[i
].predicate
!= 0
2040 && strchr (fci
->flags2
, bad_flag_pairs
[i
].predicate
) == 0)
2042 s
= get_flag_spec (flag_specs
, bad_flag_pairs
[i
].flag_char1
, NULL
);
2043 t
= get_flag_spec (flag_specs
, bad_flag_pairs
[i
].flag_char2
, NULL
);
2044 if (bad_flag_pairs
[i
].ignored
)
2046 if (bad_flag_pairs
[i
].predicate
!= 0)
2047 status_warning (status
, "%s ignored with %s and `%%%c' %s format",
2048 _(s
->name
), _(t
->name
), format_char
,
2051 status_warning (status
, "%s ignored with %s in %s format",
2052 _(s
->name
), _(t
->name
), fki
->name
);
2056 if (bad_flag_pairs
[i
].predicate
!= 0)
2057 status_warning (status
, "use of %s and %s together with `%%%c' %s format",
2058 _(s
->name
), _(t
->name
), format_char
,
2061 status_warning (status
, "use of %s and %s together in %s format",
2062 _(s
->name
), _(t
->name
), fki
->name
);
2066 /* Give Y2K warnings. */
2067 if (warn_format_y2k
)
2070 if (strchr (fci
->flags2
, '4') != 0)
2071 if (strchr (flag_chars
, 'E') != 0)
2075 else if (strchr (fci
->flags2
, '3') != 0)
2077 else if (strchr (fci
->flags2
, '2') != 0)
2080 status_warning (status
, "`%%%c' yields only last 2 digits of year in some locales",
2082 else if (y2k_level
== 2)
2083 status_warning (status
, "`%%%c' yields only last 2 digits of year", format_char
);
2086 if (strchr (fci
->flags2
, '[') != 0)
2088 /* Skip over scan set, in case it happens to have '%' in it. */
2089 if (*format_chars
== '^')
2091 /* Find closing bracket; if one is hit immediately, then
2092 it's part of the scan set rather than a terminator. */
2093 if (*format_chars
== ']')
2095 while (*format_chars
&& *format_chars
!= ']')
2097 if (*format_chars
!= ']')
2098 /* The end of the format string was reached. */
2099 status_warning (status
, "no closing `]' for `%%[' format");
2103 wanted_type_name
= 0;
2104 if (fki
->flags
& (int) FMT_FLAG_ARG_CONVERT
)
2106 wanted_type
= (fci
->types
[length_chars_val
].type
2107 ? *fci
->types
[length_chars_val
].type
: 0);
2108 wanted_type_name
= fci
->types
[length_chars_val
].name
;
2109 wanted_type_std
= fci
->types
[length_chars_val
].std
;
2110 if (wanted_type
== 0)
2112 status_warning (status
, "use of `%s' length modifier with `%c' type character",
2113 length_chars
, format_char
);
2114 /* Heuristic: skip one argument when an invalid length/type
2115 combination is encountered. */
2119 status_warning (status
, "too few arguments for format");
2122 params
= TREE_CHAIN (params
);
2126 /* Warn if non-standard, provided it is more non-standard
2127 than the length and type characters that may already
2128 have been warned for. */
2129 && ADJ_STD (wanted_type_std
) > ADJ_STD (length_chars_std
)
2130 && ADJ_STD (wanted_type_std
) > ADJ_STD (fci
->std
))
2132 if (ADJ_STD (wanted_type_std
) > C_STD_VER
)
2133 status_warning (status
, "%s does not support the `%%%s%c' %s format",
2134 C_STD_NAME (wanted_type_std
), length_chars
,
2135 format_char
, fki
->name
);
2139 /* Finally. . .check type of argument against desired type! */
2140 if (info
->first_arg_num
== 0)
2142 if ((fci
->pointer_count
== 0 && wanted_type
== void_type_node
)
2145 if (main_arg_num
!= 0)
2148 status_warning (status
, "operand number specified with suppressed assignment");
2150 status_warning (status
, "operand number specified for format taking no argument");
2155 if (main_arg_num
!= 0)
2157 arg_num
= main_arg_num
;
2158 params
= main_arg_params
;
2163 if (has_operand_number
> 0)
2165 status_warning (status
, "missing $ operand number in format");
2169 has_operand_number
= 0;
2172 status_warning (status
, "too few arguments for format");
2176 cur_param
= TREE_VALUE (params
);
2177 params
= TREE_CHAIN (params
);
2178 main_wanted_type
.wanted_type
= wanted_type
;
2179 main_wanted_type
.wanted_type_name
= wanted_type_name
;
2180 main_wanted_type
.pointer_count
= fci
->pointer_count
+ aflag
;
2181 main_wanted_type
.char_lenient_flag
= 0;
2182 if (strchr (fci
->flags2
, 'c') != 0)
2183 main_wanted_type
.char_lenient_flag
= 1;
2184 main_wanted_type
.writing_in_flag
= 0;
2185 main_wanted_type
.reading_from_flag
= 0;
2187 main_wanted_type
.writing_in_flag
= 1;
2190 if (strchr (fci
->flags2
, 'W') != 0)
2191 main_wanted_type
.writing_in_flag
= 1;
2192 if (strchr (fci
->flags2
, 'R') != 0)
2193 main_wanted_type
.reading_from_flag
= 1;
2195 main_wanted_type
.name
= NULL
;
2196 main_wanted_type
.param
= cur_param
;
2197 main_wanted_type
.arg_num
= arg_num
;
2198 main_wanted_type
.next
= NULL
;
2199 if (last_wanted_type
!= 0)
2200 last_wanted_type
->next
= &main_wanted_type
;
2201 if (first_wanted_type
== 0)
2202 first_wanted_type
= &main_wanted_type
;
2203 last_wanted_type
= &main_wanted_type
;
2206 if (first_wanted_type
!= 0)
2207 check_format_types (status
, first_wanted_type
);
2213 /* Check the argument types from a single format conversion (possibly
2214 including width and precision arguments). */
2216 check_format_types (status
, types
)
2218 format_wanted_type
*types
;
2220 for (; types
!= 0; types
= types
->next
)
2230 cur_param
= types
->param
;
2231 cur_type
= TREE_TYPE (cur_param
);
2232 if (cur_type
== error_mark_node
)
2235 wanted_type
= types
->wanted_type
;
2236 arg_num
= types
->arg_num
;
2238 /* The following should not occur here. */
2239 if (wanted_type
== 0)
2241 if (wanted_type
== void_type_node
&& types
->pointer_count
== 0)
2244 if (types
->pointer_count
== 0)
2246 promoted_type
= simple_type_promotes_to (wanted_type
);
2247 if (promoted_type
!= NULL_TREE
)
2248 wanted_type
= promoted_type
;
2251 STRIP_NOPS (cur_param
);
2253 /* Check the types of any additional pointer arguments
2254 that precede the "real" argument. */
2255 for (i
= 0; i
< types
->pointer_count
; ++i
)
2257 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
2259 cur_type
= TREE_TYPE (cur_type
);
2260 if (cur_type
== error_mark_node
)
2263 /* Check for writing through a NULL pointer. */
2264 if (types
->writing_in_flag
2267 && integer_zerop (cur_param
))
2268 status_warning (status
,
2269 "writing through null pointer (arg %d)",
2272 /* Check for reading through a NULL pointer. */
2273 if (types
->reading_from_flag
2276 && integer_zerop (cur_param
))
2277 status_warning (status
,
2278 "reading through null pointer (arg %d)",
2281 if (cur_param
!= 0 && TREE_CODE (cur_param
) == ADDR_EXPR
)
2282 cur_param
= TREE_OPERAND (cur_param
, 0);
2286 /* See if this is an attempt to write into a const type with
2287 scanf or with printf "%n". Note: the writing in happens
2288 at the first indirection only, if for example
2289 void * const * is passed to scanf %p; passing
2290 const void ** is simply passing an incompatible type. */
2291 if (types
->writing_in_flag
2293 && (TYPE_READONLY (cur_type
)
2295 && (TREE_CODE_CLASS (TREE_CODE (cur_param
)) == 'c'
2296 || (DECL_P (cur_param
)
2297 && TREE_READONLY (cur_param
))))))
2298 status_warning (status
, "writing into constant object (arg %d)", arg_num
);
2300 /* If there are extra type qualifiers beyond the first
2301 indirection, then this makes the types technically
2305 && (TYPE_READONLY (cur_type
)
2306 || TYPE_VOLATILE (cur_type
)
2307 || TYPE_RESTRICT (cur_type
)))
2308 status_warning (status
, "extra type qualifiers in format argument (arg %d)",
2314 if (types
->pointer_count
== 1)
2315 status_warning (status
, "format argument is not a pointer (arg %d)", arg_num
);
2317 status_warning (status
, "format argument is not a pointer to a pointer (arg %d)", arg_num
);
2322 if (i
< types
->pointer_count
)
2325 orig_cur_type
= cur_type
;
2326 cur_type
= TYPE_MAIN_VARIANT (cur_type
);
2328 /* Check whether the argument type is a character type. This leniency
2329 only applies to certain formats, flagged with 'c'.
2331 if (types
->char_lenient_flag
)
2332 char_type_flag
= (cur_type
== char_type_node
2333 || cur_type
== signed_char_type_node
2334 || cur_type
== unsigned_char_type_node
);
2336 /* Check the type of the "real" argument, if there's a type we want. */
2337 if (wanted_type
== cur_type
)
2339 /* If we want `void *', allow any pointer type.
2340 (Anything else would already have got a warning.)
2341 With -pedantic, only allow pointers to void and to character
2343 if (wanted_type
== void_type_node
2344 && (!pedantic
|| (i
== 1 && char_type_flag
)))
2346 /* Don't warn about differences merely in signedness, unless
2347 -pedantic. With -pedantic, warn if the type is a pointer
2348 target and not a character type, and for character types at
2349 a second level of indirection. */
2350 if (TREE_CODE (wanted_type
) == INTEGER_TYPE
2351 && TREE_CODE (cur_type
) == INTEGER_TYPE
2352 && (! pedantic
|| i
== 0 || (i
== 1 && char_type_flag
))
2353 && (TREE_UNSIGNED (wanted_type
)
2354 ? wanted_type
== unsigned_type (cur_type
)
2355 : wanted_type
== signed_type (cur_type
)))
2357 /* Likewise, "signed char", "unsigned char" and "char" are
2358 equivalent but the above test won't consider them equivalent. */
2359 if (wanted_type
== char_type_node
2360 && (! pedantic
|| i
< 2)
2363 /* Now we have a type mismatch. */
2368 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type
)));
2370 if (TYPE_NAME (orig_cur_type
) != 0
2371 && TREE_CODE (orig_cur_type
) != INTEGER_TYPE
2372 && !(TREE_CODE (orig_cur_type
) == POINTER_TYPE
2373 && TREE_CODE (TREE_TYPE (orig_cur_type
)) == INTEGER_TYPE
))
2375 if (TREE_CODE (TYPE_NAME (orig_cur_type
)) == TYPE_DECL
2376 && DECL_NAME (TYPE_NAME (orig_cur_type
)) != 0)
2377 that
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (orig_cur_type
)));
2379 that
= IDENTIFIER_POINTER (TYPE_NAME (orig_cur_type
));
2382 /* A nameless type can't possibly match what the format wants.
2383 So there will be a warning for it.
2384 Make up a string to describe vaguely what it is. */
2387 if (TREE_CODE (orig_cur_type
) == POINTER_TYPE
)
2390 that
= "different type";
2393 /* Make the warning better in case of mismatch of int vs long. */
2394 if (TREE_CODE (orig_cur_type
) == INTEGER_TYPE
2395 && TREE_CODE (wanted_type
) == INTEGER_TYPE
2396 && TYPE_PRECISION (orig_cur_type
) == TYPE_PRECISION (wanted_type
)
2397 && TYPE_NAME (orig_cur_type
) != 0
2398 && TREE_CODE (TYPE_NAME (orig_cur_type
)) == TYPE_DECL
)
2399 that
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (orig_cur_type
)));
2401 if (strcmp (this, that
) != 0)
2403 /* There may be a better name for the format, e.g. size_t,
2404 but we should allow for programs with a perverse typedef
2405 making size_t something other than what the compiler
2407 if (types
->wanted_type_name
!= 0
2408 && strcmp (types
->wanted_type_name
, that
) != 0)
2409 this = types
->wanted_type_name
;
2410 if (types
->name
!= 0)
2411 status_warning (status
, "%s is not type %s (arg %d)", types
->name
, this,
2414 status_warning (status
, "%s format, %s arg (arg %d)", this, that
, arg_num
);