1 /* Check calls to formatted I/O functions (-Wformat).
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004 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. */
37 set_Wformat (int setting
)
39 warn_format
= setting
;
40 warn_format_extra_args
= setting
;
41 warn_format_zero_length
= setting
;
44 warn_format_nonliteral
= setting
;
45 warn_format_security
= setting
;
46 warn_format_y2k
= setting
;
48 /* Make sure not to disable -Wnonnull if -Wformat=0 is specified. */
50 warn_nonnull
= setting
;
54 /* Handle attributes associated with format checking. */
56 /* This must be in the same order as format_types, with format_type_error
58 enum format_type
{ printf_format_type
, asm_fprintf_format_type
,
59 gcc_diag_format_type
, gcc_cdiag_format_type
,
60 gcc_cxxdiag_format_type
,
61 scanf_format_type
, strftime_format_type
,
62 strfmon_format_type
, format_type_error
};
64 typedef struct function_format_info
66 enum format_type format_type
; /* type of format (printf, scanf, etc.) */
67 unsigned HOST_WIDE_INT format_num
; /* number of format argument */
68 unsigned HOST_WIDE_INT first_arg_num
; /* number of first arg (zero for varargs) */
69 } function_format_info
;
71 static bool decode_format_attr (tree
, function_format_info
*, int);
72 static enum format_type
decode_format_type (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 (tree
*node
, tree name ATTRIBUTE_UNUSED
,
85 tree args
, int flags
, bool *no_add_attrs
)
88 tree format_num_expr
= TREE_VALUE (args
);
89 unsigned HOST_WIDE_INT format_num
= 0;
92 if (!get_constant (format_num_expr
, &format_num
, 0))
94 error ("format string has invalid operand number");
99 argument
= TYPE_ARG_TYPES (type
);
102 if (!check_format_string (argument
, format_num
, flags
, no_add_attrs
))
106 if (TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
107 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type
)))
110 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
111 error ("function does not return string type");
112 *no_add_attrs
= true;
119 /* Verify that the format_num argument is actually a string, in case
120 the format attribute is in error. */
122 check_format_string (tree argument
, unsigned HOST_WIDE_INT format_num
,
123 int flags
, bool *no_add_attrs
)
125 unsigned HOST_WIDE_INT i
;
127 for (i
= 1; i
!= format_num
; i
++)
131 argument
= TREE_CHAIN (argument
);
135 || TREE_CODE (TREE_VALUE (argument
)) != POINTER_TYPE
136 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument
)))
139 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
140 error ("format string arg not a string type");
141 *no_add_attrs
= true;
148 /* Strip any conversions from the expression, verify it is a constant,
149 and store its value. If validated_p is true, abort on errors.
150 Returns true on success, false otherwise. */
152 get_constant(tree expr
, unsigned HOST_WIDE_INT
*value
, int validated_p
)
154 while (TREE_CODE (expr
) == NOP_EXPR
155 || TREE_CODE (expr
) == CONVERT_EXPR
156 || TREE_CODE (expr
) == NON_LVALUE_EXPR
)
157 expr
= TREE_OPERAND (expr
, 0);
159 if (TREE_CODE (expr
) != INTEGER_CST
|| TREE_INT_CST_HIGH (expr
) != 0)
166 *value
= TREE_INT_CST_LOW (expr
);
171 /* Decode the arguments to a "format" attribute into a function_format_info
172 structure. It is already known that the list is of the right length.
173 If VALIDATED_P is true, then these attributes have already been validated
174 and this function will abort if they are erroneous; if false, it
175 will give an error message. Returns true if the attributes are
176 successfully decoded, false otherwise. */
179 decode_format_attr (tree args
, function_format_info
*info
, int validated_p
)
181 tree format_type_id
= TREE_VALUE (args
);
182 tree format_num_expr
= TREE_VALUE (TREE_CHAIN (args
));
183 tree first_arg_num_expr
184 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args
)));
186 if (TREE_CODE (format_type_id
) != IDENTIFIER_NODE
)
190 error ("unrecognized format specifier");
195 const char *p
= IDENTIFIER_POINTER (format_type_id
);
197 info
->format_type
= decode_format_type (p
);
199 if (info
->format_type
== format_type_error
)
203 warning ("%qs is an unrecognized format function type", p
);
208 if (!get_constant (format_num_expr
, &info
->format_num
, validated_p
))
210 error ("format string has invalid operand number");
214 if (!get_constant (first_arg_num_expr
, &info
->first_arg_num
, validated_p
))
216 error ("'...' has invalid operand number");
220 if (info
->first_arg_num
!= 0 && info
->first_arg_num
<= info
->format_num
)
224 error ("format string arg follows the args to be formatted");
231 /* Check a call to a format function against a parameter list. */
233 /* The meaningfully distinct length modifiers for format checking recognized
250 /* The standard versions in which various format features appeared. */
251 enum format_std_version
255 STD_C9L
, /* C99, but treat as C89 if -Wno-long-long. */
260 /* The C standard version C++ is treated as equivalent to
261 or inheriting from, for the purpose of format features supported. */
262 #define CPLUSPLUS_STD_VER STD_C94
263 /* The C standard version we are checking formats against when pedantic. */
264 #define C_STD_VER ((int)(c_dialect_cxx () \
265 ? CPLUSPLUS_STD_VER \
268 : (flag_isoc94 ? STD_C94 : STD_C89))))
269 /* The name to give to the standard version we are warning about when
270 pedantic. FEATURE_VER is the version in which the feature warned out
271 appeared, which is higher than C_STD_VER. */
272 #define C_STD_NAME(FEATURE_VER) (c_dialect_cxx () \
274 : ((FEATURE_VER) == STD_EXT \
277 /* Adjust a C standard version, which may be STD_C9L, to account for
278 -Wno-long-long. Returns other standard versions unchanged. */
279 #define ADJ_STD(VER) ((int)((VER) == STD_C9L \
280 ? (warn_long_long ? STD_C99 : STD_C89) \
283 /* Flags that may apply to a particular kind of format checked by GCC. */
286 /* This format converts arguments of types determined by the
288 FMT_FLAG_ARG_CONVERT
= 1,
289 /* The scanf allocation 'a' kludge applies to this format kind. */
290 FMT_FLAG_SCANF_A_KLUDGE
= 2,
291 /* A % during parsing a specifier is allowed to be a modified % rather
292 that indicating the format is broken and we are out-of-sync. */
293 FMT_FLAG_FANCY_PERCENT_OK
= 4,
294 /* With $ operand numbers, it is OK to reference the same argument more
296 FMT_FLAG_DOLLAR_MULTIPLE
= 8,
297 /* This format type uses $ operand numbers (strfmon doesn't). */
298 FMT_FLAG_USE_DOLLAR
= 16,
299 /* Zero width is bad in this type of format (scanf). */
300 FMT_FLAG_ZERO_WIDTH_BAD
= 32,
301 /* Empty precision specification is OK in this type of format (printf). */
302 FMT_FLAG_EMPTY_PREC_OK
= 64,
303 /* Gaps are allowed in the arguments with $ operand numbers if all
304 arguments are pointers (scanf). */
305 FMT_FLAG_DOLLAR_GAP_POINTER_OK
= 128
306 /* Not included here: details of whether width or precision may occur
307 (controlled by width_char and precision_char); details of whether
308 '*' can be used for these (width_type and precision_type); details
309 of whether length modifiers can occur (length_char_specs). */
313 /* Structure describing a length modifier supported in format checking, and
314 possibly a doubled version such as "hh". */
317 /* Name of the single-character length modifier. */
319 /* Index into a format_char_info.types array. */
320 enum format_lengths index
;
321 /* Standard version this length appears in. */
322 enum format_std_version std
;
323 /* Same, if the modifier can be repeated, or NULL if it can't. */
324 const char *double_name
;
325 enum format_lengths double_index
;
326 enum format_std_version double_std
;
327 } format_length_info
;
330 /* Structure describing the combination of a conversion specifier
331 (or a set of specifiers which act identically) and a length modifier. */
334 /* The standard version this combination of length and type appeared in.
335 This is only relevant if greater than those for length and type
336 individually; otherwise it is ignored. */
337 enum format_std_version std
;
338 /* The name to use for the type, if different from that generated internally
339 (e.g., "signed size_t"). */
341 /* The type itself. */
343 } format_type_detail
;
346 /* Macros to fill out tables of these. */
347 #define NOARGUMENTS { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }
348 #define BADLEN { 0, NULL, NULL }
349 #define NOLENGTHS { BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }
352 /* Structure describing a format conversion specifier (or a set of specifiers
353 which act identically), and the length modifiers used with it. */
356 const char *format_chars
;
358 enum format_std_version std
;
359 /* Types accepted for each length modifier. */
360 format_type_detail types
[FMT_LEN_MAX
];
361 /* List of other modifier characters allowed with these specifiers.
362 This lists flags, and additionally "w" for width, "p" for precision
363 (right precision, for strfmon), "#" for left precision (strfmon),
364 "a" for scanf "a" allocation extension (not applicable in C99 mode),
365 "*" for scanf suppression, and "E" and "O" for those strftime
367 const char *flag_chars
;
368 /* List of additional flags describing these conversion specifiers.
369 "c" for generic character pointers being allowed, "2" for strftime
370 two digit year formats, "3" for strftime formats giving two digit
371 years in some locales, "4" for "2" which becomes "3" with an "E" modifier,
372 "o" if use of strftime "O" is a GNU extension beyond C99,
373 "W" if the argument is a pointer which is dereferenced and written into,
374 "R" if the argument is a pointer which is dereferenced and read from,
375 "i" for printf integer formats where the '0' flag is ignored with
376 precision, and "[" for the starting character of a scanf scanset. */
381 /* Structure describing a flag accepted by some kind of format. */
384 /* The flag character in question (0 for end of array). */
386 /* Zero if this entry describes the flag character in general, or a
387 nonzero character that may be found in flags2 if it describes the
388 flag when used with certain formats only. If the latter, only
389 the first such entry found that applies to the current conversion
390 specifier is used; the values of `name' and `long_name' it supplies
391 will be used, if non-NULL and the standard version is higher than
392 the unpredicated one, for any pedantic warning. For example, 'o'
393 for strftime formats (meaning 'O' is an extension over C99). */
395 /* Nonzero if the next character after this flag in the format should
396 be skipped ('=' in strfmon), zero otherwise. */
398 /* The name to use for this flag in diagnostic messages. For example,
399 N_("`0' flag"), N_("field width"). */
401 /* Long name for this flag in diagnostic messages; currently only used for
402 "ISO C does not support ...". For example, N_("the `I' printf flag"). */
403 const char *long_name
;
404 /* The standard version in which it appeared. */
405 enum format_std_version std
;
409 /* Structure describing a combination of flags that is bad for some kind
413 /* The first flag character in question (0 for end of array). */
415 /* The second flag character. */
417 /* Nonzero if the message should say that the first flag is ignored with
418 the second, zero if the combination should simply be objected to. */
420 /* Zero if this entry applies whenever this flag combination occurs,
421 a nonzero character from flags2 if it only applies in some
422 circumstances (e.g. 'i' for printf formats ignoring 0 with precision). */
427 /* Structure describing a particular kind of format processed by GCC. */
430 /* The name of this kind of format, for use in diagnostics. Also
431 the name of the attribute (without preceding and following __). */
433 /* Specifications of the length modifiers accepted; possibly NULL. */
434 const format_length_info
*length_char_specs
;
435 /* Details of the conversion specification characters accepted. */
436 const format_char_info
*conversion_specs
;
437 /* String listing the flag characters that are accepted. */
438 const char *flag_chars
;
439 /* String listing modifier characters (strftime) accepted. May be NULL. */
440 const char *modifier_chars
;
441 /* Details of the flag characters, including pseudo-flags. */
442 const format_flag_spec
*flag_specs
;
443 /* Details of bad combinations of flags. */
444 const format_flag_pair
*bad_flag_pairs
;
445 /* Flags applicable to this kind of format. */
447 /* Flag character to treat a width as, or 0 if width not used. */
449 /* Flag character to treat a left precision (strfmon) as,
450 or 0 if left precision not used. */
451 int left_precision_char
;
452 /* Flag character to treat a precision (for strfmon, right precision) as,
453 or 0 if precision not used. */
455 /* If a flag character has the effect of suppressing the conversion of
456 an argument ('*' in scanf), that flag character, otherwise 0. */
457 int suppression_char
;
458 /* Flag character to treat a length modifier as (ignored if length
459 modifiers not used). Need not be placed in flag_chars for conversion
460 specifiers, but is used to check for bad combinations such as length
461 modifier with assignment suppression in scanf. */
462 int length_code_char
;
463 /* Pointer to type of argument expected if '*' is used for a width,
464 or NULL if '*' not used for widths. */
466 /* Pointer to type of argument expected if '*' is used for a precision,
467 or NULL if '*' not used for precisions. */
468 tree
*precision_type
;
472 /* Structure describing details of a type expected in format checking,
473 and the type to check against it. */
474 typedef struct format_wanted_type
476 /* The type wanted. */
478 /* The name of this type to use in diagnostics. */
479 const char *wanted_type_name
;
480 /* The level of indirection through pointers at which this type occurs. */
482 /* Whether, when pointer_count is 1, to allow any character type when
483 pedantic, rather than just the character or void type specified. */
484 int char_lenient_flag
;
485 /* Whether the argument, dereferenced once, is written into and so the
486 argument must not be a pointer to a const-qualified type. */
488 /* Whether the argument, dereferenced once, is read from and so
489 must not be a NULL pointer. */
490 int reading_from_flag
;
491 /* If warnings should be of the form "field precision should have
492 type 'int'", the name to use (in this case "field precision"),
493 otherwise NULL, for "format expects type 'long'" type
496 /* The actual parameter to check against the wanted type. */
498 /* The argument number of that parameter. */
500 /* The next type to check for this format conversion, or NULL if none. */
501 struct format_wanted_type
*next
;
502 } format_wanted_type
;
505 static const format_length_info printf_length_specs
[] =
507 { "h", FMT_LEN_h
, STD_C89
, "hh", FMT_LEN_hh
, STD_C99
},
508 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C9L
},
509 { "q", FMT_LEN_ll
, STD_EXT
, NULL
, 0, 0 },
510 { "L", FMT_LEN_L
, STD_C89
, NULL
, 0, 0 },
511 { "z", FMT_LEN_z
, STD_C99
, NULL
, 0, 0 },
512 { "Z", FMT_LEN_z
, STD_EXT
, NULL
, 0, 0 },
513 { "t", FMT_LEN_t
, STD_C99
, NULL
, 0, 0 },
514 { "j", FMT_LEN_j
, STD_C99
, NULL
, 0, 0 },
515 { NULL
, 0, 0, NULL
, 0, 0 }
518 /* Length specifiers valid for asm_fprintf. */
519 static const format_length_info asm_fprintf_length_specs
[] =
521 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C89
},
522 { "w", FMT_LEN_none
, STD_C89
, NULL
, 0, 0 },
523 { NULL
, 0, 0, NULL
, 0, 0 }
526 /* Length specifiers valid for GCC diagnostics. */
527 static const format_length_info gcc_diag_length_specs
[] =
529 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C89
},
530 { "w", FMT_LEN_none
, STD_C89
, NULL
, 0, 0 },
531 { NULL
, 0, 0, NULL
, 0, 0 }
534 /* The custom diagnostics all accept the same length specifiers. */
535 #define gcc_cdiag_length_specs gcc_diag_length_specs
536 #define gcc_cxxdiag_length_specs gcc_diag_length_specs
538 /* This differs from printf_length_specs only in that "Z" is not accepted. */
539 static const format_length_info scanf_length_specs
[] =
541 { "h", FMT_LEN_h
, STD_C89
, "hh", FMT_LEN_hh
, STD_C99
},
542 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C9L
},
543 { "q", FMT_LEN_ll
, STD_EXT
, NULL
, 0, 0 },
544 { "L", FMT_LEN_L
, STD_C89
, NULL
, 0, 0 },
545 { "z", FMT_LEN_z
, STD_C99
, NULL
, 0, 0 },
546 { "t", FMT_LEN_t
, STD_C99
, NULL
, 0, 0 },
547 { "j", FMT_LEN_j
, STD_C99
, NULL
, 0, 0 },
548 { NULL
, 0, 0, NULL
, 0, 0 }
552 /* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings
553 make no sense for a format type not part of any C standard version. */
554 static const format_length_info strfmon_length_specs
[] =
556 /* A GNU extension. */
557 { "L", FMT_LEN_L
, STD_C89
, NULL
, 0, 0 },
558 { NULL
, 0, 0, NULL
, 0, 0 }
561 static const format_flag_spec printf_flag_specs
[] =
563 { ' ', 0, 0, N_("` ' flag"), N_("the ` ' 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_C89
},
566 { '0', 0, 0, N_("`0' flag"), N_("the `0' printf flag"), STD_C89
},
567 { '-', 0, 0, N_("`-' flag"), N_("the `-' printf flag"), STD_C89
},
568 { '\'', 0, 0, N_("`'' flag"), N_("the `'' printf flag"), STD_EXT
},
569 { 'I', 0, 0, N_("`I' flag"), N_("the `I' printf flag"), STD_EXT
},
570 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89
},
571 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89
},
572 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89
},
573 { 0, 0, 0, NULL
, NULL
, 0 }
577 static const format_flag_pair printf_flag_pairs
[] =
581 { '0', 'p', 1, 'i' },
585 static const format_flag_spec asm_fprintf_flag_specs
[] =
587 { ' ', 0, 0, N_("` ' flag"), N_("the ` ' printf flag"), STD_C89
},
588 { '+', 0, 0, N_("`+' flag"), N_("the `+' printf flag"), STD_C89
},
589 { '#', 0, 0, N_("`#' flag"), N_("the `#' printf flag"), STD_C89
},
590 { '0', 0, 0, N_("`0' flag"), N_("the `0' printf flag"), STD_C89
},
591 { '-', 0, 0, N_("`-' flag"), N_("the `-' printf flag"), STD_C89
},
592 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89
},
593 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89
},
594 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89
},
595 { 0, 0, 0, NULL
, NULL
, 0 }
598 static const format_flag_pair asm_fprintf_flag_pairs
[] =
602 { '0', 'p', 1, 'i' },
606 static const format_flag_pair gcc_diag_flag_pairs
[] =
611 #define gcc_cdiag_flag_pairs gcc_diag_flag_pairs
612 #define gcc_cxxdiag_flag_pairs gcc_diag_flag_pairs
614 static const format_flag_spec gcc_diag_flag_specs
[] =
616 { 'q', 0, 0, N_("`q' flag"), N_("the `q' diagnostic flag"), STD_C89
},
617 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89
},
618 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89
},
619 { 0, 0, 0, NULL
, NULL
, 0 }
622 #define gcc_cdiag_flag_specs gcc_diag_flag_specs
624 static const format_flag_spec gcc_cxxdiag_flag_specs
[] =
626 { '+', 0, 0, N_("`+' flag"), N_("the `+' printf flag"), STD_C89
},
627 { '#', 0, 0, N_("`#' flag"), N_("the `#' printf flag"), STD_C89
},
628 { 'q', 0, 0, N_("`q' flag"), N_("the `q' diagnostic flag"), STD_C89
},
629 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89
},
630 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89
},
631 { 0, 0, 0, NULL
, NULL
, 0 }
634 static const format_flag_spec scanf_flag_specs
[] =
636 { '*', 0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89
},
637 { 'a', 0, 0, N_("`a' flag"), N_("the `a' scanf flag"), STD_EXT
},
638 { 'w', 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89
},
639 { 'L', 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89
},
640 { '\'', 0, 0, N_("`'' flag"), N_("the `'' scanf flag"), STD_EXT
},
641 { 'I', 0, 0, N_("`I' flag"), N_("the `I' scanf flag"), STD_EXT
},
642 { 0, 0, 0, NULL
, NULL
, 0 }
646 static const format_flag_pair scanf_flag_pairs
[] =
653 static const format_flag_spec strftime_flag_specs
[] =
655 { '_', 0, 0, N_("`_' flag"), N_("the `_' strftime flag"), STD_EXT
},
656 { '-', 0, 0, N_("`-' flag"), N_("the `-' strftime flag"), STD_EXT
},
657 { '0', 0, 0, N_("`0' flag"), N_("the `0' strftime flag"), STD_EXT
},
658 { '^', 0, 0, N_("`^' flag"), N_("the `^' strftime flag"), STD_EXT
},
659 { '#', 0, 0, N_("`#' flag"), N_("the `#' strftime flag"), STD_EXT
},
660 { 'w', 0, 0, N_("field width"), N_("field width in strftime format"), STD_EXT
},
661 { 'E', 0, 0, N_("`E' modifier"), N_("the `E' strftime modifier"), STD_C99
},
662 { 'O', 0, 0, N_("`O' modifier"), N_("the `O' strftime modifier"), STD_C99
},
663 { 'O', 'o', 0, NULL
, N_("the `O' modifier"), STD_EXT
},
664 { 0, 0, 0, NULL
, NULL
, 0 }
668 static const format_flag_pair strftime_flag_pairs
[] =
679 static const format_flag_spec strfmon_flag_specs
[] =
681 { '=', 0, 1, N_("fill character"), N_("fill character in strfmon format"), STD_C89
},
682 { '^', 0, 0, N_("`^' flag"), N_("the `^' strfmon flag"), STD_C89
},
683 { '+', 0, 0, N_("`+' flag"), N_("the `+' strfmon flag"), STD_C89
},
684 { '(', 0, 0, N_("`(' flag"), N_("the `(' strfmon flag"), STD_C89
},
685 { '!', 0, 0, N_("`!' flag"), N_("the `!' strfmon flag"), STD_C89
},
686 { '-', 0, 0, N_("`-' flag"), N_("the `-' strfmon flag"), STD_C89
},
687 { 'w', 0, 0, N_("field width"), N_("field width in strfmon format"), STD_C89
},
688 { '#', 0, 0, N_("left precision"), N_("left precision in strfmon format"), STD_C89
},
689 { 'p', 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89
},
690 { 'L', 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89
},
691 { 0, 0, 0, NULL
, NULL
, 0 }
694 static const format_flag_pair strfmon_flag_pairs
[] =
701 #define T_I &integer_type_node
702 #define T89_I { STD_C89, NULL, T_I }
703 #define T_L &long_integer_type_node
704 #define T89_L { STD_C89, NULL, T_L }
705 #define T_LL &long_long_integer_type_node
706 #define T9L_LL { STD_C9L, NULL, T_LL }
707 #define TEX_LL { STD_EXT, NULL, T_LL }
708 #define T_S &short_integer_type_node
709 #define T89_S { STD_C89, NULL, T_S }
710 #define T_UI &unsigned_type_node
711 #define T89_UI { STD_C89, NULL, T_UI }
712 #define T_UL &long_unsigned_type_node
713 #define T89_UL { STD_C89, NULL, T_UL }
714 #define T_ULL &long_long_unsigned_type_node
715 #define T9L_ULL { STD_C9L, NULL, T_ULL }
716 #define TEX_ULL { STD_EXT, NULL, T_ULL }
717 #define T_US &short_unsigned_type_node
718 #define T89_US { STD_C89, NULL, T_US }
719 #define T_F &float_type_node
720 #define T89_F { STD_C89, NULL, T_F }
721 #define T99_F { STD_C99, NULL, T_F }
722 #define T_D &double_type_node
723 #define T89_D { STD_C89, NULL, T_D }
724 #define T99_D { STD_C99, NULL, T_D }
725 #define T_LD &long_double_type_node
726 #define T89_LD { STD_C89, NULL, T_LD }
727 #define T99_LD { STD_C99, NULL, T_LD }
728 #define T_C &char_type_node
729 #define T89_C { STD_C89, NULL, T_C }
730 #define T_SC &signed_char_type_node
731 #define T99_SC { STD_C99, NULL, T_SC }
732 #define T_UC &unsigned_char_type_node
733 #define T99_UC { STD_C99, NULL, T_UC }
734 #define T_V &void_type_node
735 #define T89_V { STD_C89, NULL, T_V }
736 #define T_W &wchar_type_node
737 #define T94_W { STD_C94, "wchar_t", T_W }
738 #define TEX_W { STD_EXT, "wchar_t", T_W }
739 #define T_WI &wint_type_node
740 #define T94_WI { STD_C94, "wint_t", T_WI }
741 #define TEX_WI { STD_EXT, "wint_t", T_WI }
742 #define T_ST &size_type_node
743 #define T99_ST { STD_C99, "size_t", T_ST }
744 #define T_SST &signed_size_type_node
745 #define T99_SST { STD_C99, "signed size_t", T_SST }
746 #define T_PD &ptrdiff_type_node
747 #define T99_PD { STD_C99, "ptrdiff_t", T_PD }
748 #define T_UPD &unsigned_ptrdiff_type_node
749 #define T99_UPD { STD_C99, "unsigned ptrdiff_t", T_UPD }
750 #define T_IM &intmax_type_node
751 #define T99_IM { STD_C99, "intmax_t", T_IM }
752 #define T_UIM &uintmax_type_node
753 #define T99_UIM { STD_C99, "uintmax_t", T_UIM }
755 static const format_char_info print_char_table
[] =
757 /* C89 conversion specifiers. */
758 { "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" },
759 { "oxX", 0, STD_C89
, { T89_UI
, T99_UC
, T89_US
, T89_UL
, T9L_ULL
, TEX_ULL
, T99_ST
, T99_UPD
, T99_UIM
}, "-wp0#", "i" },
760 { "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" },
761 { "fgG", 0, STD_C89
, { T89_D
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T89_LD
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +#'I", "" },
762 { "eE", 0, STD_C89
, { T89_D
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T89_LD
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +#I", "" },
763 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T94_WI
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "" },
764 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "cR" },
765 { "p", 1, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "c" },
766 { "n", 1, STD_C89
, { T89_I
, T99_SC
, T89_S
, T89_L
, T9L_LL
, BADLEN
, T99_SST
, T99_PD
, T99_IM
}, "", "W" },
767 /* C99 conversion specifiers. */
768 { "F", 0, STD_C99
, { T99_D
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T99_LD
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +#'I", "" },
769 { "aA", 0, STD_C99
, { T99_D
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T99_LD
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +#", "" },
770 /* X/Open conversion specifiers. */
771 { "C", 0, STD_EXT
, { TEX_WI
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "" },
772 { "S", 1, STD_EXT
, { TEX_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "R" },
773 /* GNU conversion specifiers. */
774 { "m", 0, STD_EXT
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "" },
775 { NULL
, 0, 0, NOLENGTHS
, NULL
, NULL
}
778 static const format_char_info asm_fprintf_char_table
[] =
780 /* C89 conversion specifiers. */
781 { "di", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T89_L
, T9L_LL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +", "i" },
782 { "oxX", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0#", "i" },
783 { "u", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0", "i" },
784 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "" },
785 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "cR" },
787 /* asm_fprintf conversion specifiers. */
788 { "O", 0, STD_C89
, NOARGUMENTS
, "", "" },
789 { "R", 0, STD_C89
, NOARGUMENTS
, "", "" },
790 { "I", 0, STD_C89
, NOARGUMENTS
, "", "" },
791 { "L", 0, STD_C89
, NOARGUMENTS
, "", "" },
792 { "U", 0, STD_C89
, NOARGUMENTS
, "", "" },
793 { "r", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "" },
794 { "@", 0, STD_C89
, NOARGUMENTS
, "", "" },
795 { NULL
, 0, 0, NOLENGTHS
, NULL
, NULL
}
798 static const format_char_info gcc_diag_char_table
[] =
800 /* C89 conversion specifiers. */
801 { "di", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T89_L
, T9L_LL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "" },
802 { "ox", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "" },
803 { "u", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "" },
804 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "" },
805 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "pq", "cR" },
806 { "p", 1, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "c" },
808 /* Custom conversion specifiers. */
810 /* %H will require "location_t" at runtime. */
811 { "H", 0, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "" },
813 /* These will require a "tree" at runtime. */
814 { "J", 0, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "" },
816 { "<>'", 0, STD_C89
, NOARGUMENTS
, "", "" },
817 { "m", 0, STD_C89
, NOARGUMENTS
, "q", "" },
818 { NULL
, 0, 0, NOLENGTHS
, NULL
, NULL
}
821 static const format_char_info gcc_cdiag_char_table
[] =
823 /* C89 conversion specifiers. */
824 { "di", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T89_L
, T9L_LL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "" },
825 { "ox", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "" },
826 { "u", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "" },
827 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "" },
828 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "pq", "cR" },
829 { "p", 1, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "c" },
831 /* Custom conversion specifiers. */
833 /* %H will require "location_t" at runtime. */
834 { "H", 0, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "" },
836 /* These will require a "tree" at runtime. */
837 { "DEFJT", 0, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "" },
839 { "<>'", 0, STD_C89
, NOARGUMENTS
, "", "" },
840 { "m", 0, STD_C89
, NOARGUMENTS
, "q", "" },
841 { NULL
, 0, 0, NOLENGTHS
, NULL
, NULL
}
844 static const format_char_info gcc_cxxdiag_char_table
[] =
846 /* C89 conversion specifiers. */
847 { "di", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T89_L
, T9L_LL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "" },
848 { "ox", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "" },
849 { "u", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "" },
850 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "" },
851 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "pq", "cR" },
852 { "p", 1, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "c" },
854 /* Custom conversion specifiers. */
856 /* %H will require "location_t" at runtime. */
857 { "H", 0, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "" },
859 /* These will require a "tree" at runtime. */
860 { "ADEFJTV",0,STD_C89
,{ T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+#", "" },
862 /* These accept either an `int' or an `enum tree_code' (which is handled as an `int'.) */
863 { "CLOPQ",0,STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "" },
865 { "<>'", 0, STD_C89
, NOARGUMENTS
, "", "" },
866 { "m", 0, STD_C89
, NOARGUMENTS
, "q", "" },
867 { NULL
, 0, 0, NOLENGTHS
, NULL
, NULL
}
870 static const format_char_info scan_char_table
[] =
872 /* C89 conversion specifiers. */
873 { "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" },
874 { "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" },
875 { "oxX", 1, STD_C89
, { T89_UI
, T99_UC
, T89_US
, T89_UL
, T9L_ULL
, TEX_ULL
, T99_ST
, T99_UPD
, T99_UIM
}, "*w", "W" },
876 { "efgEG", 1, STD_C89
, { T89_F
, BADLEN
, BADLEN
, T89_D
, BADLEN
, T89_LD
, BADLEN
, BADLEN
, BADLEN
}, "*w'", "W" },
877 { "c", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*w", "cW" },
878 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*aw", "cW" },
879 { "[", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*aw", "cW[" },
880 { "p", 2, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*w", "W" },
881 { "n", 1, STD_C89
, { T89_I
, T99_SC
, T89_S
, T89_L
, T9L_LL
, BADLEN
, T99_SST
, T99_PD
, T99_IM
}, "", "W" },
882 /* C99 conversion specifiers. */
883 { "FaA", 1, STD_C99
, { T99_F
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T99_LD
, BADLEN
, BADLEN
, BADLEN
}, "*w'", "W" },
884 /* X/Open conversion specifiers. */
885 { "C", 1, STD_EXT
, { TEX_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*w", "W" },
886 { "S", 1, STD_EXT
, { TEX_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*aw", "W" },
887 { NULL
, 0, 0, NOLENGTHS
, NULL
, NULL
}
890 static const format_char_info time_char_table
[] =
892 /* C89 conversion specifiers. */
893 { "ABZab", 0, STD_C89
, NOLENGTHS
, "^#", "" },
894 { "cx", 0, STD_C89
, NOLENGTHS
, "E", "3" },
895 { "HIMSUWdmw", 0, STD_C89
, NOLENGTHS
, "-_0Ow", "" },
896 { "j", 0, STD_C89
, NOLENGTHS
, "-_0Ow", "o" },
897 { "p", 0, STD_C89
, NOLENGTHS
, "#", "" },
898 { "X", 0, STD_C89
, NOLENGTHS
, "E", "" },
899 { "y", 0, STD_C89
, NOLENGTHS
, "EO-_0w", "4" },
900 { "Y", 0, STD_C89
, NOLENGTHS
, "-_0EOw", "o" },
901 { "%", 0, STD_C89
, NOLENGTHS
, "", "" },
902 /* C99 conversion specifiers. */
903 { "C", 0, STD_C99
, NOLENGTHS
, "-_0EOw", "o" },
904 { "D", 0, STD_C99
, NOLENGTHS
, "", "2" },
905 { "eVu", 0, STD_C99
, NOLENGTHS
, "-_0Ow", "" },
906 { "FRTnrt", 0, STD_C99
, NOLENGTHS
, "", "" },
907 { "g", 0, STD_C99
, NOLENGTHS
, "O-_0w", "2o" },
908 { "G", 0, STD_C99
, NOLENGTHS
, "-_0Ow", "o" },
909 { "h", 0, STD_C99
, NOLENGTHS
, "^#", "" },
910 { "z", 0, STD_C99
, NOLENGTHS
, "O", "o" },
911 /* GNU conversion specifiers. */
912 { "kls", 0, STD_EXT
, NOLENGTHS
, "-_0Ow", "" },
913 { "P", 0, STD_EXT
, NOLENGTHS
, "", "" },
914 { NULL
, 0, 0, NOLENGTHS
, NULL
, NULL
}
917 static const format_char_info monetary_char_table
[] =
919 { "in", 0, STD_C89
, { T89_D
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, T89_LD
, BADLEN
, BADLEN
, BADLEN
}, "=^+(!-w#p", "" },
920 { NULL
, 0, 0, NOLENGTHS
, NULL
, NULL
}
924 /* This must be in the same order as enum format_type. */
925 static const format_kind_info format_types_orig
[] =
927 { "printf", printf_length_specs
, print_char_table
, " +#0-'I", NULL
,
928 printf_flag_specs
, printf_flag_pairs
,
929 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_DOLLAR_MULTIPLE
|FMT_FLAG_USE_DOLLAR
|FMT_FLAG_EMPTY_PREC_OK
,
931 &integer_type_node
, &integer_type_node
933 { "asm_fprintf", asm_fprintf_length_specs
, asm_fprintf_char_table
, " +#0-", NULL
,
934 asm_fprintf_flag_specs
, asm_fprintf_flag_pairs
,
935 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_EMPTY_PREC_OK
,
939 { "gcc_diag", gcc_diag_length_specs
, gcc_diag_char_table
, "q", NULL
,
940 gcc_diag_flag_specs
, gcc_diag_flag_pairs
,
941 FMT_FLAG_ARG_CONVERT
,
943 NULL
, &integer_type_node
945 { "gcc_cdiag", gcc_cdiag_length_specs
, gcc_cdiag_char_table
, "q", NULL
,
946 gcc_cdiag_flag_specs
, gcc_cdiag_flag_pairs
,
947 FMT_FLAG_ARG_CONVERT
,
949 NULL
, &integer_type_node
951 { "gcc_cxxdiag", gcc_cxxdiag_length_specs
, gcc_cxxdiag_char_table
, "q+#", NULL
,
952 gcc_cxxdiag_flag_specs
, gcc_cxxdiag_flag_pairs
,
953 FMT_FLAG_ARG_CONVERT
,
955 NULL
, &integer_type_node
957 { "scanf", scanf_length_specs
, scan_char_table
, "*'I", NULL
,
958 scanf_flag_specs
, scanf_flag_pairs
,
959 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_SCANF_A_KLUDGE
|FMT_FLAG_USE_DOLLAR
|FMT_FLAG_ZERO_WIDTH_BAD
|FMT_FLAG_DOLLAR_GAP_POINTER_OK
,
963 { "strftime", NULL
, time_char_table
, "_-0^#", "EO",
964 strftime_flag_specs
, strftime_flag_pairs
,
965 FMT_FLAG_FANCY_PERCENT_OK
, 'w', 0, 0, 0, 0,
968 { "strfmon", strfmon_length_specs
, monetary_char_table
, "=^+(!-", NULL
,
969 strfmon_flag_specs
, strfmon_flag_pairs
,
970 FMT_FLAG_ARG_CONVERT
, 'w', '#', 'p', 0, 'L',
975 /* This layer of indirection allows GCC to reassign format_types with
976 new data if necessary, while still allowing the original data to be
978 static const format_kind_info
*format_types
= format_types_orig
;
979 /* We can modify this one. */
980 static format_kind_info
*dynamic_format_types
;
982 /* Structure detailing the results of checking a format function call
983 where the format expression may be a conditional expression with
984 many leaves resulting from nested conditional expressions. */
987 /* Number of leaves of the format argument that could not be checked
988 as they were not string literals. */
989 int number_non_literal
;
990 /* Number of leaves of the format argument that were null pointers or
991 string literals, but had extra format arguments. */
992 int number_extra_args
;
993 /* Number of leaves of the format argument that were null pointers or
994 string literals, but had extra format arguments and used $ operand
996 int number_dollar_extra_args
;
997 /* Number of leaves of the format argument that were wide string
1000 /* Number of leaves of the format argument that were empty strings. */
1002 /* Number of leaves of the format argument that were unterminated
1004 int number_unterminated
;
1005 /* Number of leaves of the format argument that were not counted above. */
1007 } format_check_results
;
1011 format_check_results
*res
;
1012 function_format_info
*info
;
1014 } format_check_context
;
1016 static void check_format_info (function_format_info
*, tree
);
1017 static void check_format_arg (void *, tree
, unsigned HOST_WIDE_INT
);
1018 static void check_format_info_main (format_check_results
*,
1019 function_format_info
*,
1020 const char *, int, tree
,
1021 unsigned HOST_WIDE_INT
);
1023 static void init_dollar_format_checking (int, tree
);
1024 static int maybe_read_dollar_number (const char **, int,
1025 tree
, tree
*, const format_kind_info
*);
1026 static bool avoid_dollar_number (const char *);
1027 static void finish_dollar_format_checking (format_check_results
*, int);
1029 static const format_flag_spec
*get_flag_spec (const format_flag_spec
*,
1032 static void check_format_types (format_wanted_type
*, const char *, int);
1033 static void format_type_warning (const char *, const char *, int, tree
,
1034 int, const char *, tree
, int);
1036 /* Decode a format type from a string, returning the type, or
1037 format_type_error if not valid, in which case the caller should print an
1039 static enum format_type
1040 decode_format_type (const char *s
)
1045 for (i
= 0; i
< (int) format_type_error
; i
++)
1048 if (!strcmp (s
, format_types
[i
].name
))
1050 alen
= strlen (format_types
[i
].name
);
1051 if (slen
== alen
+ 4 && s
[0] == '_' && s
[1] == '_'
1052 && s
[slen
- 1] == '_' && s
[slen
- 2] == '_'
1053 && !strncmp (s
+ 2, format_types
[i
].name
, alen
))
1056 return ((enum format_type
) i
);
1060 /* Check the argument list of a call to printf, scanf, etc.
1061 ATTRS are the attributes on the function type.
1062 PARAMS is the list of argument values. Also, if -Wmissing-format-attribute,
1063 warn for calls to vprintf or vscanf in functions with no such format
1064 attribute themselves. */
1067 check_function_format (tree attrs
, tree params
)
1071 /* See if this function has any format attributes. */
1072 for (a
= attrs
; a
; a
= TREE_CHAIN (a
))
1074 if (is_attribute_p ("format", TREE_PURPOSE (a
)))
1076 /* Yup; check it. */
1077 function_format_info info
;
1078 decode_format_attr (TREE_VALUE (a
), &info
, 1);
1079 check_format_info (&info
, params
);
1080 if (warn_missing_format_attribute
&& info
.first_arg_num
== 0
1081 && (format_types
[info
.format_type
].flags
1082 & (int) FMT_FLAG_ARG_CONVERT
))
1085 for (c
= TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl
));
1088 if (is_attribute_p ("format", TREE_PURPOSE (c
))
1089 && (decode_format_type (IDENTIFIER_POINTER
1090 (TREE_VALUE (TREE_VALUE (c
))))
1091 == info
.format_type
))
1095 /* Check if the current function has a parameter to which
1096 the format attribute could be attached; if not, it
1097 can't be a candidate for a format attribute, despite
1098 the vprintf-like or vscanf-like call. */
1100 for (args
= DECL_ARGUMENTS (current_function_decl
);
1102 args
= TREE_CHAIN (args
))
1104 if (TREE_CODE (TREE_TYPE (args
)) == POINTER_TYPE
1105 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args
)))
1110 warning ("function might be possible candidate for %qs format attribute",
1111 format_types
[info
.format_type
].name
);
1119 /* Variables used by the checking of $ operand number formats. */
1120 static char *dollar_arguments_used
= NULL
;
1121 static char *dollar_arguments_pointer_p
= NULL
;
1122 static int dollar_arguments_alloc
= 0;
1123 static int dollar_arguments_count
;
1124 static int dollar_first_arg_num
;
1125 static int dollar_max_arg_used
;
1126 static int dollar_format_warned
;
1128 /* Initialize the checking for a format string that may contain $
1129 parameter number specifications; we will need to keep track of whether
1130 each parameter has been used. FIRST_ARG_NUM is the number of the first
1131 argument that is a parameter to the format, or 0 for a vprintf-style
1132 function; PARAMS is the list of arguments starting at this argument. */
1135 init_dollar_format_checking (int first_arg_num
, tree params
)
1137 tree oparams
= params
;
1139 dollar_first_arg_num
= first_arg_num
;
1140 dollar_arguments_count
= 0;
1141 dollar_max_arg_used
= 0;
1142 dollar_format_warned
= 0;
1143 if (first_arg_num
> 0)
1147 dollar_arguments_count
++;
1148 params
= TREE_CHAIN (params
);
1151 if (dollar_arguments_alloc
< dollar_arguments_count
)
1153 if (dollar_arguments_used
)
1154 free (dollar_arguments_used
);
1155 if (dollar_arguments_pointer_p
)
1156 free (dollar_arguments_pointer_p
);
1157 dollar_arguments_alloc
= dollar_arguments_count
;
1158 dollar_arguments_used
= xmalloc (dollar_arguments_alloc
);
1159 dollar_arguments_pointer_p
= xmalloc (dollar_arguments_alloc
);
1161 if (dollar_arguments_alloc
)
1163 memset (dollar_arguments_used
, 0, dollar_arguments_alloc
);
1164 if (first_arg_num
> 0)
1170 dollar_arguments_pointer_p
[i
] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params
)))
1172 params
= TREE_CHAIN (params
);
1180 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
1181 is set, it is an error if one is not found; otherwise, it is OK. If
1182 such a number is found, check whether it is within range and mark that
1183 numbered operand as being used for later checking. Returns the operand
1184 number if found and within range, zero if no such number was found and
1185 this is OK, or -1 on error. PARAMS points to the first operand of the
1186 format; PARAM_PTR is made to point to the parameter referred to. If
1187 a $ format is found, *FORMAT is updated to point just after it. */
1190 maybe_read_dollar_number (const char **format
,
1191 int dollar_needed
, tree params
, tree
*param_ptr
,
1192 const format_kind_info
*fki
)
1196 const char *fcp
= *format
;
1197 if (! ISDIGIT (*fcp
))
1201 warning ("missing $ operand number in format");
1209 while (ISDIGIT (*fcp
))
1212 nargnum
= 10 * argnum
+ (*fcp
- '0');
1213 if (nargnum
< 0 || nargnum
/ 10 != argnum
)
1222 warning ("missing $ operand number in format");
1229 if (pedantic
&& !dollar_format_warned
)
1231 warning ("%s does not support %%n$ operand number formats",
1232 C_STD_NAME (STD_EXT
));
1233 dollar_format_warned
= 1;
1235 if (overflow_flag
|| argnum
== 0
1236 || (dollar_first_arg_num
&& argnum
> dollar_arguments_count
))
1238 warning ("operand number out of range in format");
1241 if (argnum
> dollar_max_arg_used
)
1242 dollar_max_arg_used
= argnum
;
1243 /* For vprintf-style functions we may need to allocate more memory to
1244 track which arguments are used. */
1245 while (dollar_arguments_alloc
< dollar_max_arg_used
)
1248 nalloc
= 2 * dollar_arguments_alloc
+ 16;
1249 dollar_arguments_used
= xrealloc (dollar_arguments_used
, nalloc
);
1250 dollar_arguments_pointer_p
= xrealloc (dollar_arguments_pointer_p
,
1252 memset (dollar_arguments_used
+ dollar_arguments_alloc
, 0,
1253 nalloc
- dollar_arguments_alloc
);
1254 dollar_arguments_alloc
= nalloc
;
1256 if (!(fki
->flags
& (int) FMT_FLAG_DOLLAR_MULTIPLE
)
1257 && dollar_arguments_used
[argnum
- 1] == 1)
1259 dollar_arguments_used
[argnum
- 1] = 2;
1260 warning ("format argument %d used more than once in %s format",
1264 dollar_arguments_used
[argnum
- 1] = 1;
1265 if (dollar_first_arg_num
)
1268 *param_ptr
= params
;
1269 for (i
= 1; i
< argnum
&& *param_ptr
!= 0; i
++)
1270 *param_ptr
= TREE_CHAIN (*param_ptr
);
1272 if (*param_ptr
== 0)
1274 /* This case shouldn't be caught here. */
1283 /* Ensure that FORMAT does not start with a decimal number followed by
1284 a $; give a diagnostic and return true if it does, false otherwise. */
1287 avoid_dollar_number (const char *format
)
1289 if (!ISDIGIT (*format
))
1291 while (ISDIGIT (*format
))
1295 warning ("$ operand number used after format without operand number");
1302 /* Finish the checking for a format string that used $ operand number formats
1303 instead of non-$ formats. We check for unused operands before used ones
1304 (a serious error, since the implementation of the format function
1305 can't know what types to pass to va_arg to find the later arguments).
1306 and for unused operands at the end of the format (if we know how many
1307 arguments the format had, so not for vprintf). If there were operand
1308 numbers out of range on a non-vprintf-style format, we won't have reached
1309 here. If POINTER_GAP_OK, unused arguments are OK if all arguments are
1313 finish_dollar_format_checking (format_check_results
*res
, int pointer_gap_ok
)
1316 bool found_pointer_gap
= false;
1317 for (i
= 0; i
< dollar_max_arg_used
; i
++)
1319 if (!dollar_arguments_used
[i
])
1321 if (pointer_gap_ok
&& (dollar_first_arg_num
== 0
1322 || dollar_arguments_pointer_p
[i
]))
1323 found_pointer_gap
= true;
1325 warning ("format argument %d unused before used argument %d in $-style format",
1326 i
+ 1, dollar_max_arg_used
);
1329 if (found_pointer_gap
1330 || (dollar_first_arg_num
1331 && dollar_max_arg_used
< dollar_arguments_count
))
1333 res
->number_other
--;
1334 res
->number_dollar_extra_args
++;
1339 /* Retrieve the specification for a format flag. SPEC contains the
1340 specifications for format flags for the applicable kind of format.
1341 FLAG is the flag in question. If PREDICATES is NULL, the basic
1342 spec for that flag must be retrieved and this function aborts if
1343 it cannot be found. If PREDICATES is not NULL, it is a string listing
1344 possible predicates for the spec entry; if an entry predicated on any
1345 of these is found, it is returned, otherwise NULL is returned. */
1347 static const format_flag_spec
*
1348 get_flag_spec (const format_flag_spec
*spec
, int flag
, const char *predicates
)
1351 for (i
= 0; spec
[i
].flag_char
!= 0; i
++)
1353 if (spec
[i
].flag_char
!= flag
)
1355 if (predicates
!= NULL
)
1357 if (spec
[i
].predicate
!= 0
1358 && strchr (predicates
, spec
[i
].predicate
) != 0)
1361 else if (spec
[i
].predicate
== 0)
1364 if (predicates
== NULL
)
1371 /* Check the argument list of a call to printf, scanf, etc.
1372 INFO points to the function_format_info structure.
1373 PARAMS is the list of argument values. */
1376 check_format_info (function_format_info
*info
, tree params
)
1378 format_check_context format_ctx
;
1379 unsigned HOST_WIDE_INT arg_num
;
1381 format_check_results res
;
1382 /* Skip to format argument. If the argument isn't available, there's
1383 no work for us to do; prototype checking will catch the problem. */
1384 for (arg_num
= 1; ; ++arg_num
)
1388 if (arg_num
== info
->format_num
)
1390 params
= TREE_CHAIN (params
);
1392 format_tree
= TREE_VALUE (params
);
1393 params
= TREE_CHAIN (params
);
1394 if (format_tree
== 0)
1397 res
.number_non_literal
= 0;
1398 res
.number_extra_args
= 0;
1399 res
.number_dollar_extra_args
= 0;
1400 res
.number_wide
= 0;
1401 res
.number_empty
= 0;
1402 res
.number_unterminated
= 0;
1403 res
.number_other
= 0;
1405 format_ctx
.res
= &res
;
1406 format_ctx
.info
= info
;
1407 format_ctx
.params
= params
;
1409 check_function_arguments_recurse (check_format_arg
, &format_ctx
,
1410 format_tree
, arg_num
);
1412 if (res
.number_non_literal
> 0)
1414 /* Functions taking a va_list normally pass a non-literal format
1415 string. These functions typically are declared with
1416 first_arg_num == 0, so avoid warning in those cases. */
1417 if (!(format_types
[info
->format_type
].flags
& (int) FMT_FLAG_ARG_CONVERT
))
1419 /* For strftime-like formats, warn for not checking the format
1420 string; but there are no arguments to check. */
1421 if (warn_format_nonliteral
)
1422 warning ("format not a string literal, format string not checked");
1424 else if (info
->first_arg_num
!= 0)
1426 /* If there are no arguments for the format at all, we may have
1427 printf (foo) which is likely to be a security hole. */
1428 while (arg_num
+ 1 < info
->first_arg_num
)
1432 params
= TREE_CHAIN (params
);
1435 if (params
== 0 && (warn_format_nonliteral
|| warn_format_security
))
1436 warning ("format not a string literal and no format arguments");
1437 else if (warn_format_nonliteral
)
1438 warning ("format not a string literal, argument types not checked");
1442 /* If there were extra arguments to the format, normally warn. However,
1443 the standard does say extra arguments are ignored, so in the specific
1444 case where we have multiple leaves (conditional expressions or
1445 ngettext) allow extra arguments if at least one leaf didn't have extra
1446 arguments, but was otherwise OK (either non-literal or checked OK).
1447 If the format is an empty string, this should be counted similarly to the
1448 case of extra format arguments. */
1449 if (res
.number_extra_args
> 0 && res
.number_non_literal
== 0
1450 && res
.number_other
== 0 && warn_format_extra_args
)
1451 warning ("too many arguments for format");
1452 if (res
.number_dollar_extra_args
> 0 && res
.number_non_literal
== 0
1453 && res
.number_other
== 0 && warn_format_extra_args
)
1454 warning ("unused arguments in $-style format");
1455 if (res
.number_empty
> 0 && res
.number_non_literal
== 0
1456 && res
.number_other
== 0 && warn_format_zero_length
)
1457 warning ("zero-length %s format string",
1458 format_types
[info
->format_type
].name
);
1460 if (res
.number_wide
> 0)
1461 warning ("format is a wide character string");
1463 if (res
.number_unterminated
> 0)
1464 warning ("unterminated format string");
1467 /* Callback from check_function_arguments_recurse to check a
1468 format string. FORMAT_TREE is the format parameter. ARG_NUM
1469 is the number of the format argument. CTX points to a
1470 format_check_context. */
1473 check_format_arg (void *ctx
, tree format_tree
,
1474 unsigned HOST_WIDE_INT arg_num
)
1476 format_check_context
*format_ctx
= ctx
;
1477 format_check_results
*res
= format_ctx
->res
;
1478 function_format_info
*info
= format_ctx
->info
;
1479 tree params
= format_ctx
->params
;
1482 HOST_WIDE_INT offset
;
1483 const char *format_chars
;
1484 tree array_size
= 0;
1487 if (integer_zerop (format_tree
))
1489 /* Skip to first argument to check, so we can see if this format
1490 has any arguments (it shouldn't). */
1491 while (arg_num
+ 1 < info
->first_arg_num
)
1495 params
= TREE_CHAIN (params
);
1500 res
->number_other
++;
1502 res
->number_extra_args
++;
1508 if (TREE_CODE (format_tree
) == PLUS_EXPR
)
1512 arg0
= TREE_OPERAND (format_tree
, 0);
1513 arg1
= TREE_OPERAND (format_tree
, 1);
1516 if (TREE_CODE (arg1
) == INTEGER_CST
)
1518 else if (TREE_CODE (arg0
) == INTEGER_CST
)
1525 res
->number_non_literal
++;
1528 if (!host_integerp (arg1
, 0)
1529 || (offset
= tree_low_cst (arg1
, 0)) < 0)
1531 res
->number_non_literal
++;
1535 if (TREE_CODE (format_tree
) != ADDR_EXPR
)
1537 res
->number_non_literal
++;
1540 format_tree
= TREE_OPERAND (format_tree
, 0);
1541 if (TREE_CODE (format_tree
) == VAR_DECL
1542 && TREE_CODE (TREE_TYPE (format_tree
)) == ARRAY_TYPE
1543 && (array_init
= decl_constant_value (format_tree
)) != format_tree
1544 && TREE_CODE (array_init
) == STRING_CST
)
1546 /* Extract the string constant initializer. Note that this may include
1547 a trailing NUL character that is not in the array (e.g.
1548 const char a[3] = "foo";). */
1549 array_size
= DECL_SIZE_UNIT (format_tree
);
1550 format_tree
= array_init
;
1552 if (TREE_CODE (format_tree
) != STRING_CST
)
1554 res
->number_non_literal
++;
1557 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree
))) != char_type_node
)
1562 format_chars
= TREE_STRING_POINTER (format_tree
);
1563 format_length
= TREE_STRING_LENGTH (format_tree
);
1564 if (array_size
!= 0)
1566 /* Variable length arrays can't be initialized. */
1567 if (TREE_CODE (array_size
) != INTEGER_CST
)
1569 if (host_integerp (array_size
, 0))
1571 HOST_WIDE_INT array_size_value
= TREE_INT_CST_LOW (array_size
);
1572 if (array_size_value
> 0
1573 && array_size_value
== (int) array_size_value
1574 && format_length
> array_size_value
)
1575 format_length
= array_size_value
;
1580 if (offset
>= format_length
)
1582 res
->number_non_literal
++;
1585 format_chars
+= offset
;
1586 format_length
-= offset
;
1588 if (format_length
< 1)
1590 res
->number_unterminated
++;
1593 if (format_length
== 1)
1595 res
->number_empty
++;
1598 if (format_chars
[--format_length
] != 0)
1600 res
->number_unterminated
++;
1604 /* Skip to first argument to check. */
1605 while (arg_num
+ 1 < info
->first_arg_num
)
1609 params
= TREE_CHAIN (params
);
1612 /* Provisionally increment res->number_other; check_format_info_main
1613 will decrement it if it finds there are extra arguments, but this way
1614 need not adjust it for every return. */
1615 res
->number_other
++;
1616 check_format_info_main (res
, info
, format_chars
, format_length
,
1621 /* Do the main part of checking a call to a format function. FORMAT_CHARS
1622 is the NUL-terminated format string (which at this point may contain
1623 internal NUL characters); FORMAT_LENGTH is its length (excluding the
1624 terminating NUL character). ARG_NUM is one less than the number of
1625 the first format argument to check; PARAMS points to that format
1626 argument in the list of arguments. */
1629 check_format_info_main (format_check_results
*res
,
1630 function_format_info
*info
, const char *format_chars
,
1631 int format_length
, tree params
,
1632 unsigned HOST_WIDE_INT arg_num
)
1634 const char *orig_format_chars
= format_chars
;
1635 tree first_fillin_param
= params
;
1637 const format_kind_info
*fki
= &format_types
[info
->format_type
];
1638 const format_flag_spec
*flag_specs
= fki
->flag_specs
;
1639 const format_flag_pair
*bad_flag_pairs
= fki
->bad_flag_pairs
;
1641 /* -1 if no conversions taking an operand have been found; 0 if one has
1642 and it didn't use $; 1 if $ formats are in use. */
1643 int has_operand_number
= -1;
1645 init_dollar_format_checking (info
->first_arg_num
, first_fillin_param
);
1650 int suppressed
= FALSE
;
1651 const char *length_chars
= NULL
;
1652 enum format_lengths length_chars_val
= FMT_LEN_none
;
1653 enum format_std_version length_chars_std
= STD_C89
;
1657 int main_arg_num
= 0;
1658 tree main_arg_params
= 0;
1659 enum format_std_version wanted_type_std
;
1660 const char *wanted_type_name
;
1661 format_wanted_type width_wanted_type
;
1662 format_wanted_type precision_wanted_type
;
1663 format_wanted_type main_wanted_type
;
1664 format_wanted_type
*first_wanted_type
= NULL
;
1665 format_wanted_type
*last_wanted_type
= NULL
;
1666 const format_length_info
*fli
= NULL
;
1667 const format_char_info
*fci
= NULL
;
1668 char flag_chars
[256];
1670 const char *format_start
= format_chars
;
1671 if (*format_chars
== 0)
1673 if (format_chars
- orig_format_chars
!= format_length
)
1674 warning ("embedded %<\\0%> in format");
1675 if (info
->first_arg_num
!= 0 && params
!= 0
1676 && has_operand_number
<= 0)
1678 res
->number_other
--;
1679 res
->number_extra_args
++;
1681 if (has_operand_number
> 0)
1682 finish_dollar_format_checking (res
, fki
->flags
& (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK
);
1685 if (*format_chars
++ != '%')
1687 if (*format_chars
== 0)
1689 warning ("spurious trailing %<%%%> in format");
1692 if (*format_chars
== '%')
1699 if ((fki
->flags
& (int) FMT_FLAG_USE_DOLLAR
) && has_operand_number
!= 0)
1701 /* Possibly read a $ operand number at the start of the format.
1702 If one was previously used, one is required here. If one
1703 is not used here, we can't immediately conclude this is a
1704 format without them, since it could be printf %m or scanf %*. */
1706 opnum
= maybe_read_dollar_number (&format_chars
, 0,
1708 &main_arg_params
, fki
);
1713 has_operand_number
= 1;
1714 main_arg_num
= opnum
+ info
->first_arg_num
- 1;
1717 else if (fki
->flags
& FMT_FLAG_USE_DOLLAR
)
1719 if (avoid_dollar_number (format_chars
))
1723 /* Read any format flags, but do not yet validate them beyond removing
1724 duplicates, since in general validation depends on the rest of
1726 while (*format_chars
!= 0
1727 && strchr (fki
->flag_chars
, *format_chars
) != 0)
1729 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
1730 *format_chars
, NULL
);
1731 if (strchr (flag_chars
, *format_chars
) != 0)
1733 warning ("repeated %s in format", _(s
->name
));
1737 i
= strlen (flag_chars
);
1738 flag_chars
[i
++] = *format_chars
;
1741 if (s
->skip_next_char
)
1744 if (*format_chars
== 0)
1746 warning ("missing fill character at end of strfmon format");
1753 /* Read any format width, possibly * or *m$. */
1754 if (fki
->width_char
!= 0)
1756 if (fki
->width_type
!= NULL
&& *format_chars
== '*')
1758 i
= strlen (flag_chars
);
1759 flag_chars
[i
++] = fki
->width_char
;
1761 /* "...a field width...may be indicated by an asterisk.
1762 In this case, an int argument supplies the field width..." */
1764 if (has_operand_number
!= 0)
1767 opnum
= maybe_read_dollar_number (&format_chars
,
1768 has_operand_number
== 1,
1775 has_operand_number
= 1;
1776 arg_num
= opnum
+ info
->first_arg_num
- 1;
1779 has_operand_number
= 0;
1783 if (avoid_dollar_number (format_chars
))
1786 if (info
->first_arg_num
!= 0)
1790 warning ("too few arguments for format");
1793 cur_param
= TREE_VALUE (params
);
1794 if (has_operand_number
<= 0)
1796 params
= TREE_CHAIN (params
);
1799 width_wanted_type
.wanted_type
= *fki
->width_type
;
1800 width_wanted_type
.wanted_type_name
= NULL
;
1801 width_wanted_type
.pointer_count
= 0;
1802 width_wanted_type
.char_lenient_flag
= 0;
1803 width_wanted_type
.writing_in_flag
= 0;
1804 width_wanted_type
.reading_from_flag
= 0;
1805 width_wanted_type
.name
= _("field width");
1806 width_wanted_type
.param
= cur_param
;
1807 width_wanted_type
.arg_num
= arg_num
;
1808 width_wanted_type
.next
= NULL
;
1809 if (last_wanted_type
!= 0)
1810 last_wanted_type
->next
= &width_wanted_type
;
1811 if (first_wanted_type
== 0)
1812 first_wanted_type
= &width_wanted_type
;
1813 last_wanted_type
= &width_wanted_type
;
1818 /* Possibly read a numeric width. If the width is zero,
1819 we complain if appropriate. */
1820 int non_zero_width_char
= FALSE
;
1821 int found_width
= FALSE
;
1822 while (ISDIGIT (*format_chars
))
1825 if (*format_chars
!= '0')
1826 non_zero_width_char
= TRUE
;
1829 if (found_width
&& !non_zero_width_char
&&
1830 (fki
->flags
& (int) FMT_FLAG_ZERO_WIDTH_BAD
))
1831 warning ("zero width in %s format", fki
->name
);
1834 i
= strlen (flag_chars
);
1835 flag_chars
[i
++] = fki
->width_char
;
1841 /* Read any format left precision (must be a number, not *). */
1842 if (fki
->left_precision_char
!= 0 && *format_chars
== '#')
1845 i
= strlen (flag_chars
);
1846 flag_chars
[i
++] = fki
->left_precision_char
;
1848 if (!ISDIGIT (*format_chars
))
1849 warning ("empty left precision in %s format", fki
->name
);
1850 while (ISDIGIT (*format_chars
))
1854 /* Read any format precision, possibly * or *m$. */
1855 if (fki
->precision_char
!= 0 && *format_chars
== '.')
1858 i
= strlen (flag_chars
);
1859 flag_chars
[i
++] = fki
->precision_char
;
1861 if (fki
->precision_type
!= NULL
&& *format_chars
== '*')
1863 /* "...a...precision...may be indicated by an asterisk.
1864 In this case, an int argument supplies the...precision." */
1866 if (has_operand_number
!= 0)
1869 opnum
= maybe_read_dollar_number (&format_chars
,
1870 has_operand_number
== 1,
1877 has_operand_number
= 1;
1878 arg_num
= opnum
+ info
->first_arg_num
- 1;
1881 has_operand_number
= 0;
1885 if (avoid_dollar_number (format_chars
))
1888 if (info
->first_arg_num
!= 0)
1892 warning ("too few arguments for format");
1895 cur_param
= TREE_VALUE (params
);
1896 if (has_operand_number
<= 0)
1898 params
= TREE_CHAIN (params
);
1901 precision_wanted_type
.wanted_type
= *fki
->precision_type
;
1902 precision_wanted_type
.wanted_type_name
= NULL
;
1903 precision_wanted_type
.pointer_count
= 0;
1904 precision_wanted_type
.char_lenient_flag
= 0;
1905 precision_wanted_type
.writing_in_flag
= 0;
1906 precision_wanted_type
.reading_from_flag
= 0;
1907 precision_wanted_type
.name
= _("field precision");
1908 precision_wanted_type
.param
= cur_param
;
1909 precision_wanted_type
.arg_num
= arg_num
;
1910 precision_wanted_type
.next
= NULL
;
1911 if (last_wanted_type
!= 0)
1912 last_wanted_type
->next
= &precision_wanted_type
;
1913 if (first_wanted_type
== 0)
1914 first_wanted_type
= &precision_wanted_type
;
1915 last_wanted_type
= &precision_wanted_type
;
1920 if (!(fki
->flags
& (int) FMT_FLAG_EMPTY_PREC_OK
)
1921 && !ISDIGIT (*format_chars
))
1922 warning ("empty precision in %s format", fki
->name
);
1923 while (ISDIGIT (*format_chars
))
1928 /* Read any length modifier, if this kind of format has them. */
1929 fli
= fki
->length_char_specs
;
1930 length_chars
= NULL
;
1931 length_chars_val
= FMT_LEN_none
;
1932 length_chars_std
= STD_C89
;
1935 while (fli
->name
!= 0 && fli
->name
[0] != *format_chars
)
1940 if (fli
->double_name
!= 0 && fli
->name
[0] == *format_chars
)
1943 length_chars
= fli
->double_name
;
1944 length_chars_val
= fli
->double_index
;
1945 length_chars_std
= fli
->double_std
;
1949 length_chars
= fli
->name
;
1950 length_chars_val
= fli
->index
;
1951 length_chars_std
= fli
->std
;
1953 i
= strlen (flag_chars
);
1954 flag_chars
[i
++] = fki
->length_code_char
;
1959 /* Warn if the length modifier is non-standard. */
1960 if (ADJ_STD (length_chars_std
) > C_STD_VER
)
1961 warning ("%s does not support the %qs %s length modifier",
1962 C_STD_NAME (length_chars_std
), length_chars
,
1967 /* Read any modifier (strftime E/O). */
1968 if (fki
->modifier_chars
!= NULL
)
1970 while (*format_chars
!= 0
1971 && strchr (fki
->modifier_chars
, *format_chars
) != 0)
1973 if (strchr (flag_chars
, *format_chars
) != 0)
1975 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
1976 *format_chars
, NULL
);
1977 warning ("repeated %s in format", _(s
->name
));
1981 i
= strlen (flag_chars
);
1982 flag_chars
[i
++] = *format_chars
;
1989 /* Handle the scanf allocation kludge. */
1990 if (fki
->flags
& (int) FMT_FLAG_SCANF_A_KLUDGE
)
1992 if (*format_chars
== 'a' && !flag_isoc99
)
1994 if (format_chars
[1] == 's' || format_chars
[1] == 'S'
1995 || format_chars
[1] == '[')
1997 /* `a' is used as a flag. */
1998 i
= strlen (flag_chars
);
1999 flag_chars
[i
++] = 'a';
2006 format_char
= *format_chars
;
2007 if (format_char
== 0
2008 || (!(fki
->flags
& (int) FMT_FLAG_FANCY_PERCENT_OK
)
2009 && format_char
== '%'))
2011 warning ("conversion lacks type at end of format");
2015 fci
= fki
->conversion_specs
;
2016 while (fci
->format_chars
!= 0
2017 && strchr (fci
->format_chars
, format_char
) == 0)
2019 if (fci
->format_chars
== 0)
2021 if (ISGRAPH(format_char
))
2022 warning ("unknown conversion type character %qc in format",
2025 warning ("unknown conversion type character 0x%x in format",
2031 if (ADJ_STD (fci
->std
) > C_STD_VER
)
2032 warning ("%s does not support the %<%%%c%> %s format",
2033 C_STD_NAME (fci
->std
), format_char
, fki
->name
);
2036 /* Validate the individual flags used, removing any that are invalid. */
2039 for (i
= 0; flag_chars
[i
] != 0; i
++)
2041 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
2042 flag_chars
[i
], NULL
);
2043 flag_chars
[i
- d
] = flag_chars
[i
];
2044 if (flag_chars
[i
] == fki
->length_code_char
)
2046 if (strchr (fci
->flag_chars
, flag_chars
[i
]) == 0)
2048 warning ("%s used with %<%%%c%> %s format",
2049 _(s
->name
), format_char
, fki
->name
);
2055 const format_flag_spec
*t
;
2056 if (ADJ_STD (s
->std
) > C_STD_VER
)
2057 warning ("%s does not support %s",
2058 C_STD_NAME (s
->std
), _(s
->long_name
));
2059 t
= get_flag_spec (flag_specs
, flag_chars
[i
], fci
->flags2
);
2060 if (t
!= NULL
&& ADJ_STD (t
->std
) > ADJ_STD (s
->std
))
2062 const char *long_name
= (t
->long_name
!= NULL
2065 if (ADJ_STD (t
->std
) > C_STD_VER
)
2066 warning ("%s does not support %s with the %<%%%c%> %s format",
2067 C_STD_NAME (t
->std
), _(long_name
),
2068 format_char
, fki
->name
);
2072 flag_chars
[i
- d
] = 0;
2075 if ((fki
->flags
& (int) FMT_FLAG_SCANF_A_KLUDGE
)
2076 && strchr (flag_chars
, 'a') != 0)
2079 if (fki
->suppression_char
2080 && strchr (flag_chars
, fki
->suppression_char
) != 0)
2083 /* Validate the pairs of flags used. */
2084 for (i
= 0; bad_flag_pairs
[i
].flag_char1
!= 0; i
++)
2086 const format_flag_spec
*s
, *t
;
2087 if (strchr (flag_chars
, bad_flag_pairs
[i
].flag_char1
) == 0)
2089 if (strchr (flag_chars
, bad_flag_pairs
[i
].flag_char2
) == 0)
2091 if (bad_flag_pairs
[i
].predicate
!= 0
2092 && strchr (fci
->flags2
, bad_flag_pairs
[i
].predicate
) == 0)
2094 s
= get_flag_spec (flag_specs
, bad_flag_pairs
[i
].flag_char1
, NULL
);
2095 t
= get_flag_spec (flag_specs
, bad_flag_pairs
[i
].flag_char2
, NULL
);
2096 if (bad_flag_pairs
[i
].ignored
)
2098 if (bad_flag_pairs
[i
].predicate
!= 0)
2099 warning ("%s ignored with %s and %<%%%c%> %s format",
2100 _(s
->name
), _(t
->name
), format_char
,
2103 warning ("%s ignored with %s in %s format",
2104 _(s
->name
), _(t
->name
), fki
->name
);
2108 if (bad_flag_pairs
[i
].predicate
!= 0)
2109 warning ("use of %s and %s together with %<%%%c%> %s format",
2110 _(s
->name
), _(t
->name
), format_char
,
2113 warning ("use of %s and %s together in %s format",
2114 _(s
->name
), _(t
->name
), fki
->name
);
2118 /* Give Y2K warnings. */
2119 if (warn_format_y2k
)
2122 if (strchr (fci
->flags2
, '4') != 0)
2123 if (strchr (flag_chars
, 'E') != 0)
2127 else if (strchr (fci
->flags2
, '3') != 0)
2129 else if (strchr (fci
->flags2
, '2') != 0)
2132 warning ("%<%%%c%> yields only last 2 digits of year in some locales",
2134 else if (y2k_level
== 2)
2135 warning ("%<%%%c%> yields only last 2 digits of year", format_char
);
2138 if (strchr (fci
->flags2
, '[') != 0)
2140 /* Skip over scan set, in case it happens to have '%' in it. */
2141 if (*format_chars
== '^')
2143 /* Find closing bracket; if one is hit immediately, then
2144 it's part of the scan set rather than a terminator. */
2145 if (*format_chars
== ']')
2147 while (*format_chars
&& *format_chars
!= ']')
2149 if (*format_chars
!= ']')
2150 /* The end of the format string was reached. */
2151 warning ("no closing %<]%> for %<%%[%> format");
2155 wanted_type_name
= 0;
2156 if (fki
->flags
& (int) FMT_FLAG_ARG_CONVERT
)
2158 wanted_type
= (fci
->types
[length_chars_val
].type
2159 ? *fci
->types
[length_chars_val
].type
: 0);
2160 wanted_type_name
= fci
->types
[length_chars_val
].name
;
2161 wanted_type_std
= fci
->types
[length_chars_val
].std
;
2162 if (wanted_type
== 0)
2164 warning ("use of %qs length modifier with %qc type character",
2165 length_chars
, format_char
);
2166 /* Heuristic: skip one argument when an invalid length/type
2167 combination is encountered. */
2171 warning ("too few arguments for format");
2174 params
= TREE_CHAIN (params
);
2178 /* Warn if non-standard, provided it is more non-standard
2179 than the length and type characters that may already
2180 have been warned for. */
2181 && ADJ_STD (wanted_type_std
) > ADJ_STD (length_chars_std
)
2182 && ADJ_STD (wanted_type_std
) > ADJ_STD (fci
->std
))
2184 if (ADJ_STD (wanted_type_std
) > C_STD_VER
)
2185 warning ("%s does not support the %<%%%s%c%> %s format",
2186 C_STD_NAME (wanted_type_std
), length_chars
,
2187 format_char
, fki
->name
);
2191 /* Finally. . .check type of argument against desired type! */
2192 if (info
->first_arg_num
== 0)
2194 if ((fci
->pointer_count
== 0 && wanted_type
== void_type_node
)
2197 if (main_arg_num
!= 0)
2200 warning ("operand number specified with suppressed assignment");
2202 warning ("operand number specified for format taking no argument");
2207 if (main_arg_num
!= 0)
2209 arg_num
= main_arg_num
;
2210 params
= main_arg_params
;
2215 if (has_operand_number
> 0)
2217 warning ("missing $ operand number in format");
2221 has_operand_number
= 0;
2224 warning ("too few arguments for format");
2228 cur_param
= TREE_VALUE (params
);
2229 params
= TREE_CHAIN (params
);
2230 main_wanted_type
.wanted_type
= wanted_type
;
2231 main_wanted_type
.wanted_type_name
= wanted_type_name
;
2232 main_wanted_type
.pointer_count
= fci
->pointer_count
+ aflag
;
2233 main_wanted_type
.char_lenient_flag
= 0;
2234 if (strchr (fci
->flags2
, 'c') != 0)
2235 main_wanted_type
.char_lenient_flag
= 1;
2236 main_wanted_type
.writing_in_flag
= 0;
2237 main_wanted_type
.reading_from_flag
= 0;
2239 main_wanted_type
.writing_in_flag
= 1;
2242 if (strchr (fci
->flags2
, 'W') != 0)
2243 main_wanted_type
.writing_in_flag
= 1;
2244 if (strchr (fci
->flags2
, 'R') != 0)
2245 main_wanted_type
.reading_from_flag
= 1;
2247 main_wanted_type
.name
= NULL
;
2248 main_wanted_type
.param
= cur_param
;
2249 main_wanted_type
.arg_num
= arg_num
;
2250 main_wanted_type
.next
= NULL
;
2251 if (last_wanted_type
!= 0)
2252 last_wanted_type
->next
= &main_wanted_type
;
2253 if (first_wanted_type
== 0)
2254 first_wanted_type
= &main_wanted_type
;
2255 last_wanted_type
= &main_wanted_type
;
2258 if (first_wanted_type
!= 0)
2259 check_format_types (first_wanted_type
, format_start
,
2260 format_chars
- format_start
);
2266 /* Check the argument types from a single format conversion (possibly
2267 including width and precision arguments). */
2269 check_format_types (format_wanted_type
*types
, const char *format_start
,
2272 for (; types
!= 0; types
= types
->next
)
2281 cur_param
= types
->param
;
2282 cur_type
= TREE_TYPE (cur_param
);
2283 if (cur_type
== error_mark_node
)
2285 orig_cur_type
= cur_type
;
2287 wanted_type
= types
->wanted_type
;
2288 arg_num
= types
->arg_num
;
2290 /* The following should not occur here. */
2291 if (wanted_type
== 0)
2293 if (wanted_type
== void_type_node
&& types
->pointer_count
== 0)
2296 if (types
->pointer_count
== 0)
2297 wanted_type
= lang_hooks
.types
.type_promotes_to (wanted_type
);
2299 wanted_type
= TYPE_MAIN_VARIANT (wanted_type
);
2301 STRIP_NOPS (cur_param
);
2303 /* Check the types of any additional pointer arguments
2304 that precede the "real" argument. */
2305 for (i
= 0; i
< types
->pointer_count
; ++i
)
2307 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
2309 cur_type
= TREE_TYPE (cur_type
);
2310 if (cur_type
== error_mark_node
)
2313 /* Check for writing through a NULL pointer. */
2314 if (types
->writing_in_flag
2317 && integer_zerop (cur_param
))
2318 warning ("writing through null pointer (arg %d)",
2321 /* Check for reading through a NULL pointer. */
2322 if (types
->reading_from_flag
2325 && integer_zerop (cur_param
))
2326 warning ("reading through null pointer (arg %d)",
2329 if (cur_param
!= 0 && TREE_CODE (cur_param
) == ADDR_EXPR
)
2330 cur_param
= TREE_OPERAND (cur_param
, 0);
2334 /* See if this is an attempt to write into a const type with
2335 scanf or with printf "%n". Note: the writing in happens
2336 at the first indirection only, if for example
2337 void * const * is passed to scanf %p; passing
2338 const void ** is simply passing an incompatible type. */
2339 if (types
->writing_in_flag
2341 && (TYPE_READONLY (cur_type
)
2343 && (TREE_CODE_CLASS (TREE_CODE (cur_param
)) == 'c'
2344 || (DECL_P (cur_param
)
2345 && TREE_READONLY (cur_param
))))))
2346 warning ("writing into constant object (arg %d)", arg_num
);
2348 /* If there are extra type qualifiers beyond the first
2349 indirection, then this makes the types technically
2353 && (TYPE_READONLY (cur_type
)
2354 || TYPE_VOLATILE (cur_type
)
2355 || TYPE_RESTRICT (cur_type
)))
2356 warning ("extra type qualifiers in format argument (arg %d)",
2362 format_type_warning (types
->name
, format_start
, format_length
,
2363 wanted_type
, types
->pointer_count
,
2364 types
->wanted_type_name
, orig_cur_type
,
2370 if (i
< types
->pointer_count
)
2373 cur_type
= TYPE_MAIN_VARIANT (cur_type
);
2375 /* Check whether the argument type is a character type. This leniency
2376 only applies to certain formats, flagged with 'c'.
2378 if (types
->char_lenient_flag
)
2379 char_type_flag
= (cur_type
== char_type_node
2380 || cur_type
== signed_char_type_node
2381 || cur_type
== unsigned_char_type_node
);
2383 /* Check the type of the "real" argument, if there's a type we want. */
2384 if (wanted_type
== cur_type
)
2386 /* If we want `void *', allow any pointer type.
2387 (Anything else would already have got a warning.)
2388 With -pedantic, only allow pointers to void and to character
2390 if (wanted_type
== void_type_node
2391 && (!pedantic
|| (i
== 1 && char_type_flag
)))
2393 /* Don't warn about differences merely in signedness, unless
2394 -pedantic. With -pedantic, warn if the type is a pointer
2395 target and not a character type, and for character types at
2396 a second level of indirection. */
2397 if (TREE_CODE (wanted_type
) == INTEGER_TYPE
2398 && TREE_CODE (cur_type
) == INTEGER_TYPE
2399 && (! pedantic
|| i
== 0 || (i
== 1 && char_type_flag
))
2400 && (TYPE_UNSIGNED (wanted_type
)
2401 ? wanted_type
== c_common_unsigned_type (cur_type
)
2402 : wanted_type
== c_common_signed_type (cur_type
)))
2404 /* Likewise, "signed char", "unsigned char" and "char" are
2405 equivalent but the above test won't consider them equivalent. */
2406 if (wanted_type
== char_type_node
2407 && (! pedantic
|| i
< 2)
2410 /* Now we have a type mismatch. */
2411 format_type_warning (types
->name
, format_start
, format_length
,
2412 wanted_type
, types
->pointer_count
,
2413 types
->wanted_type_name
, orig_cur_type
, arg_num
);
2418 /* Give a warning about a format argument of different type from that
2419 expected. DESCR is a description such as "field precision", or
2420 NULL for an ordinary format. For an ordinary format, FORMAT_START
2421 points to where the format starts in the format string and
2422 FORMAT_LENGTH is its length. WANTED_TYPE is the type the argument
2423 should have after POINTER_COUNT pointer dereferences.
2424 WANTED_NAME_NAME is a possibly more friendly name of WANTED_TYPE,
2425 or NULL if the ordinary name of the type should be used. ARG_TYPE
2426 is the type of the actual argument. ARG_NUM is the number of that
2429 format_type_warning (const char *descr
, const char *format_start
,
2430 int format_length
, tree wanted_type
, int pointer_count
,
2431 const char *wanted_type_name
, tree arg_type
, int arg_num
)
2434 /* If ARG_TYPE is a typedef with a misleading name (for example,
2435 size_t but not the standard size_t expected by printf %zu), avoid
2436 printing the typedef name. */
2437 if (wanted_type_name
2438 && TYPE_NAME (arg_type
)
2439 && TREE_CODE (TYPE_NAME (arg_type
)) == TYPE_DECL
2440 && DECL_NAME (TYPE_NAME (arg_type
))
2441 && !strcmp (wanted_type_name
,
2442 lang_hooks
.decl_printable_name (TYPE_NAME (arg_type
), 2)))
2443 arg_type
= TYPE_MAIN_VARIANT (arg_type
);
2444 /* The format type and name exclude any '*' for pointers, so those
2445 must be formatted manually. For all the types we currently have,
2446 this is adequate, but formats taking pointers to functions or
2447 arrays would require the full type to be built up in order to
2448 print it with %T. */
2449 p
= alloca (pointer_count
+ 2);
2450 if (pointer_count
== 0)
2452 else if (c_dialect_cxx ())
2454 memset (p
, '*', pointer_count
);
2455 p
[pointer_count
] = 0;
2460 memset (p
+ 1, '*', pointer_count
);
2461 p
[pointer_count
+ 1] = 0;
2463 if (wanted_type_name
)
2466 warning ("%s should have type %<%s%s%>, but argument %d has type %qT",
2467 descr
, wanted_type_name
, p
, arg_num
, arg_type
);
2469 warning ("format %q.*s expects type %<%s%s%>, but argument %d has type %qT",
2470 format_length
, format_start
, wanted_type_name
, p
,
2476 warning ("%s should have type %<%T%s%>, but argument %d has type %qT",
2477 descr
, wanted_type
, p
, arg_num
, arg_type
);
2479 warning ("format %q.*s expects type %<%T%s%>, but argument %d has type %qT",
2480 format_length
, format_start
, wanted_type
, p
, arg_num
, arg_type
);
2485 /* Given a format_char_info array FCI, and a character C, this function
2486 returns the index into the conversion_specs where that specifier's
2487 data is located. If the character isn't found it aborts. */
2489 find_char_info_specifier_index (const format_char_info
*fci
, int c
)
2493 while (fci
->format_chars
)
2495 if (strchr (fci
->format_chars
, c
))
2500 /* We shouldn't be looking for a non-existent specifier. */
2504 /* Given a format_length_info array FLI, and a character C, this
2505 function returns the index into the conversion_specs where that
2506 modifier's data is located. If the character isn't found it
2509 find_length_info_modifier_index (const format_length_info
*fli
, int c
)
2515 if (strchr (fli
->name
, c
))
2520 /* We shouldn't be looking for a non-existent modifier. */
2524 /* Determine the type of HOST_WIDE_INT in the code being compiled for
2525 use in GCC's __asm_fprintf__ custom format attribute. You must
2526 have set dynamic_format_types before calling this function. */
2528 init_dynamic_asm_fprintf_info (void)
2534 format_length_info
*new_asm_fprintf_length_specs
;
2537 /* Find the underlying type for HOST_WIDE_INT. For the %w
2538 length modifier to work, one must have issued: "typedef
2539 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2540 prior to using that modifier. */
2541 if (!(hwi
= maybe_get_identifier ("__gcc_host_wide_int__"))
2542 || !(hwi
= DECL_ORIGINAL_TYPE (identifier_global_value (hwi
))))
2545 /* Create a new (writable) copy of asm_fprintf_length_specs. */
2546 new_asm_fprintf_length_specs
= xmemdup (asm_fprintf_length_specs
,
2547 sizeof (asm_fprintf_length_specs
),
2548 sizeof (asm_fprintf_length_specs
));
2550 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2551 i
= find_length_info_modifier_index (new_asm_fprintf_length_specs
, 'w');
2552 if (hwi
== long_integer_type_node
)
2553 new_asm_fprintf_length_specs
[i
].index
= FMT_LEN_l
;
2554 else if (hwi
== long_long_integer_type_node
)
2555 new_asm_fprintf_length_specs
[i
].index
= FMT_LEN_ll
;
2559 /* Assign the new data for use. */
2560 dynamic_format_types
[asm_fprintf_format_type
].length_char_specs
=
2561 new_asm_fprintf_length_specs
;
2565 /* Determine the types of "tree" and "location_t" in the code being
2566 compiled for use in GCC's diagnostic custom format attributes. You
2567 must have set dynamic_format_types before calling this function. */
2569 init_dynamic_diag_info (void)
2571 static tree t
, loc
, hwi
;
2573 if (!loc
|| !t
|| !hwi
)
2575 static format_char_info
*diag_fci
, *cdiag_fci
, *cxxdiag_fci
;
2576 static format_length_info
*diag_ls
;
2579 /* For the GCC-diagnostics custom format specifiers to work, one
2580 must have declared `tree' and/or `location_t' prior to using
2581 those attributes. If we haven't seen these declarations then
2582 you shouldn't use the specifiers requiring these types.
2583 However we don't force a hard ICE because we may see only one
2584 or the other type. */
2585 if ((loc
= maybe_get_identifier ("location_t")))
2586 loc
= TREE_TYPE (identifier_global_value (loc
));
2588 /* We need to grab the underlying `union tree_node' so peek into
2589 an extra type level. */
2590 if ((t
= maybe_get_identifier ("tree")))
2591 t
= TREE_TYPE (TREE_TYPE (identifier_global_value (t
)));
2593 /* Find the underlying type for HOST_WIDE_INT. For the %w
2594 length modifier to work, one must have issued: "typedef
2595 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2596 prior to using that modifier. */
2597 if ((hwi
= maybe_get_identifier ("__gcc_host_wide_int__")))
2598 hwi
= DECL_ORIGINAL_TYPE (identifier_global_value (hwi
));
2600 /* Assign the new data for use. */
2602 /* All the GCC diag formats use the same length specs. */
2604 dynamic_format_types
[gcc_diag_format_type
].length_char_specs
=
2605 dynamic_format_types
[gcc_cdiag_format_type
].length_char_specs
=
2606 dynamic_format_types
[gcc_cxxdiag_format_type
].length_char_specs
=
2607 diag_ls
= xmemdup (gcc_diag_length_specs
,
2608 sizeof (gcc_diag_length_specs
),
2609 sizeof (gcc_diag_length_specs
));
2612 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2613 i
= find_length_info_modifier_index (diag_ls
, 'w');
2614 if (hwi
== long_integer_type_node
)
2615 diag_ls
[i
].index
= FMT_LEN_l
;
2616 else if (hwi
== long_long_integer_type_node
)
2617 diag_ls
[i
].index
= FMT_LEN_ll
;
2622 /* Handle the __gcc_diag__ format specifics. */
2624 dynamic_format_types
[gcc_diag_format_type
].conversion_specs
=
2625 diag_fci
= xmemdup (gcc_diag_char_table
,
2626 sizeof(gcc_diag_char_table
),
2627 sizeof(gcc_diag_char_table
));
2630 i
= find_char_info_specifier_index (diag_fci
, 'H');
2631 diag_fci
[i
].types
[0].type
= &loc
;
2632 diag_fci
[i
].pointer_count
= 1;
2636 i
= find_char_info_specifier_index (diag_fci
, 'J');
2637 diag_fci
[i
].types
[0].type
= &t
;
2638 diag_fci
[i
].pointer_count
= 1;
2641 /* Handle the __gcc_cdiag__ format specifics. */
2643 dynamic_format_types
[gcc_cdiag_format_type
].conversion_specs
=
2644 cdiag_fci
= xmemdup (gcc_cdiag_char_table
,
2645 sizeof(gcc_cdiag_char_table
),
2646 sizeof(gcc_cdiag_char_table
));
2649 i
= find_char_info_specifier_index (cdiag_fci
, 'H');
2650 cdiag_fci
[i
].types
[0].type
= &loc
;
2651 cdiag_fci
[i
].pointer_count
= 1;
2655 /* All specifiers taking a tree share the same struct. */
2656 i
= find_char_info_specifier_index (cdiag_fci
, 'D');
2657 cdiag_fci
[i
].types
[0].type
= &t
;
2658 cdiag_fci
[i
].pointer_count
= 1;
2659 i
= find_char_info_specifier_index (cdiag_fci
, 'J');
2660 cdiag_fci
[i
].types
[0].type
= &t
;
2661 cdiag_fci
[i
].pointer_count
= 1;
2664 /* Handle the __gcc_cxxdiag__ format specifics. */
2666 dynamic_format_types
[gcc_cxxdiag_format_type
].conversion_specs
=
2667 cxxdiag_fci
= xmemdup (gcc_cxxdiag_char_table
,
2668 sizeof(gcc_cxxdiag_char_table
),
2669 sizeof(gcc_cxxdiag_char_table
));
2672 i
= find_char_info_specifier_index (cxxdiag_fci
, 'H');
2673 cxxdiag_fci
[i
].types
[0].type
= &loc
;
2674 cxxdiag_fci
[i
].pointer_count
= 1;
2678 /* All specifiers taking a tree share the same struct. */
2679 i
= find_char_info_specifier_index (cxxdiag_fci
, 'D');
2680 cxxdiag_fci
[i
].types
[0].type
= &t
;
2681 cxxdiag_fci
[i
].pointer_count
= 1;
2682 i
= find_char_info_specifier_index (cxxdiag_fci
, 'J');
2683 cxxdiag_fci
[i
].types
[0].type
= &t
;
2684 cxxdiag_fci
[i
].pointer_count
= 1;
2689 /* Handle a "format" attribute; arguments as in
2690 struct attribute_spec.handler. */
2692 handle_format_attribute (tree
*node
, tree name ATTRIBUTE_UNUSED
, tree args
,
2693 int flags
, bool *no_add_attrs
)
2696 function_format_info info
;
2699 if (!decode_format_attr (args
, &info
, 0))
2701 *no_add_attrs
= true;
2705 argument
= TYPE_ARG_TYPES (type
);
2708 if (!check_format_string (argument
, info
.format_num
, flags
,
2712 if (info
.first_arg_num
!= 0)
2714 unsigned HOST_WIDE_INT arg_num
= 1;
2716 /* Verify that first_arg_num points to the last arg,
2719 arg_num
++, argument
= TREE_CHAIN (argument
);
2721 if (arg_num
!= info
.first_arg_num
)
2723 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
2724 error ("args to be formatted is not '...'");
2725 *no_add_attrs
= true;
2731 if (info
.format_type
== strftime_format_type
&& info
.first_arg_num
!= 0)
2733 error ("strftime formats cannot format arguments");
2734 *no_add_attrs
= true;
2738 /* If this is a custom GCC-internal format type, we have to
2739 initialize certain bits a runtime. */
2740 if (info
.format_type
== asm_fprintf_format_type
2741 || info
.format_type
== gcc_diag_format_type
2742 || info
.format_type
== gcc_cdiag_format_type
2743 || info
.format_type
== gcc_cxxdiag_format_type
)
2745 /* Our first time through, we have to make sure that our
2746 format_type data is allocated dynamically and is modifiable. */
2747 if (!dynamic_format_types
)
2748 format_types
= dynamic_format_types
=
2749 xmemdup (format_types_orig
, sizeof (format_types_orig
),
2750 sizeof (format_types_orig
));
2752 /* If this is format __asm_fprintf__, we have to initialize
2753 GCC's notion of HOST_WIDE_INT for checking %wd. */
2754 if (info
.format_type
== asm_fprintf_format_type
)
2755 init_dynamic_asm_fprintf_info();
2756 /* If this is one of the diagnostic attributes, then we have to
2757 initialize `location_t' and `tree' at runtime. */
2758 else if (info
.format_type
== gcc_diag_format_type
2759 || info
.format_type
== gcc_cdiag_format_type
2760 || info
.format_type
== gcc_cxxdiag_format_type
)
2761 init_dynamic_diag_info();