1 /* Check calls to formatted I/O functions (-Wformat).
2 Copyright (C) 1992-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
26 #include "alloc-pool.h"
27 #include "stringpool.h"
30 #include "langhooks.h"
32 #include "diagnostic.h"
33 #include "substring-locations.h"
37 /* Handle attributes associated with format checking. */
39 /* This must be in the same order as format_types, except for
40 format_type_error. Target-specific format types do not have
41 matching enum values. */
42 enum format_type
{ printf_format_type
, asm_fprintf_format_type
,
43 gcc_diag_format_type
, gcc_tdiag_format_type
,
44 gcc_cdiag_format_type
,
45 gcc_cxxdiag_format_type
, gcc_gfc_format_type
,
46 gcc_objc_string_format_type
,
47 format_type_error
= -1};
49 struct function_format_info
51 int format_type
; /* type of format (printf, scanf, etc.) */
52 unsigned HOST_WIDE_INT format_num
; /* number of format argument */
53 unsigned HOST_WIDE_INT first_arg_num
; /* number of first arg (zero for varargs) */
56 static bool decode_format_attr (tree
, function_format_info
*, int);
57 static int decode_format_type (const char *);
59 static bool check_format_string (tree argument
,
60 unsigned HOST_WIDE_INT format_num
,
61 int flags
, bool *no_add_attrs
,
62 int expected_format_type
);
63 static bool get_constant (tree expr
, unsigned HOST_WIDE_INT
*value
,
65 static const char *convert_format_name_to_system_name (const char *attr_name
);
66 static bool cmp_attribs (const char *tattr_name
, const char *attr_name
);
68 static int first_target_format_type
;
69 static const char *format_name (int format_num
);
70 static int format_flags (int format_num
);
72 /* Emit a warning as per format_warning_va, but construct the substring_loc
73 for the character at offset (CHAR_IDX - 1) within a string constant
74 FORMAT_STRING_CST at FMT_STRING_LOC. */
76 ATTRIBUTE_GCC_DIAG (5,6)
78 format_warning_at_char (location_t fmt_string_loc
, tree format_string_cst
,
79 int char_idx
, int opt
, const char *gmsgid
, ...)
82 va_start (ap
, gmsgid
);
83 tree string_type
= TREE_TYPE (format_string_cst
);
85 /* The callers are of the form:
86 format_warning (format_string_loc, format_string_cst,
87 format_chars - orig_format_chars,
88 where format_chars has already been incremented, so that
89 CHAR_IDX is one character beyond where the warning should
90 be emitted. Fix it. */
93 substring_loc
fmt_loc (fmt_string_loc
, string_type
, char_idx
, char_idx
,
95 bool warned
= format_warning_va (fmt_loc
, NULL
, NULL
, opt
, gmsgid
, &ap
);
101 /* Check that we have a pointer to a string suitable for use as a format.
102 The default is to check for a char type.
103 For objective-c dialects, this is extended to include references to string
104 objects validated by objc_string_ref_type_p ().
105 Targets may also provide a string object type that can be used within c and
106 c++ and shared with their respective objective-c dialects. In this case the
107 reference to a format string is checked for validity via a hook.
109 The function returns true if strref points to any string type valid for the
110 language dialect and target. */
113 valid_stringptr_type_p (tree strref
)
115 return (strref
!= NULL
116 && TREE_CODE (strref
) == POINTER_TYPE
117 && (TYPE_MAIN_VARIANT (TREE_TYPE (strref
)) == char_type_node
118 || objc_string_ref_type_p (strref
)
119 || (*targetcm
.string_object_ref_type_p
) ((const_tree
) strref
)));
122 /* Handle a "format_arg" attribute; arguments as in
123 struct attribute_spec.handler. */
125 handle_format_arg_attribute (tree
*node
, tree
ARG_UNUSED (name
),
126 tree args
, int flags
, bool *no_add_attrs
)
129 tree format_num_expr
= TREE_VALUE (args
);
130 unsigned HOST_WIDE_INT format_num
= 0;
132 if (!get_constant (format_num_expr
, &format_num
, 0))
134 error ("format string has invalid operand number");
135 *no_add_attrs
= true;
139 if (prototype_p (type
))
141 /* The format arg can be any string reference valid for the language and
142 target. We cannot be more specific in this case. */
143 if (!check_format_string (type
, format_num
, flags
, no_add_attrs
, -1))
147 if (!valid_stringptr_type_p (TREE_TYPE (type
)))
149 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
150 error ("function does not return string type");
151 *no_add_attrs
= true;
158 /* Verify that the format_num argument is actually a string reference suitable,
159 for the language dialect and target (in case the format attribute is in
160 error). When we know the specific reference type expected, this is also
163 check_format_string (tree fntype
, unsigned HOST_WIDE_INT format_num
,
164 int flags
, bool *no_add_attrs
, int expected_format_type
)
166 unsigned HOST_WIDE_INT i
;
167 bool is_objc_sref
, is_target_sref
, is_char_ref
;
170 function_args_iterator iter
;
173 FOREACH_FUNCTION_ARGS (fntype
, ref
, iter
)
181 || !valid_stringptr_type_p (ref
))
183 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
184 error ("format string argument is not a string type");
185 *no_add_attrs
= true;
189 /* We only know that we want a suitable string reference. */
190 if (expected_format_type
< 0)
193 /* Now check that the arg matches the expected type. */
195 (TYPE_MAIN_VARIANT (TREE_TYPE (ref
)) == char_type_node
);
197 fmt_flags
= format_flags (expected_format_type
);
198 is_objc_sref
= is_target_sref
= false;
200 is_objc_sref
= objc_string_ref_type_p (ref
);
202 if (!(fmt_flags
& FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL
))
205 return true; /* OK, we expected a char and found one. */
208 /* We expected a char but found an extended string type. */
210 error ("found a %<%s%> reference but the format argument should"
211 " be a string", format_name (gcc_objc_string_format_type
));
213 error ("found a %qT but the format argument should be a string",
215 *no_add_attrs
= true;
220 /* We expect a string object type as the format arg. */
223 error ("format argument should be a %<%s%> reference but"
224 " a string was found", format_name (expected_format_type
));
225 *no_add_attrs
= true;
229 /* We will assert that objective-c will support either its own string type
230 or the target-supplied variant. */
232 is_target_sref
= (*targetcm
.string_object_ref_type_p
) ((const_tree
) ref
);
234 if (expected_format_type
== (int) gcc_objc_string_format_type
235 && (is_objc_sref
|| is_target_sref
))
238 /* We will allow a target string ref to match only itself. */
239 if (first_target_format_type
240 && expected_format_type
>= first_target_format_type
245 error ("format argument should be a %<%s%> reference",
246 format_name (expected_format_type
));
247 *no_add_attrs
= true;
254 /* Verify EXPR is a constant, and store its value.
255 If validated_p is true there should be no errors.
256 Returns true on success, false otherwise. */
258 get_constant (tree expr
, unsigned HOST_WIDE_INT
*value
, int validated_p
)
260 if (!tree_fits_uhwi_p (expr
))
262 gcc_assert (!validated_p
);
266 *value
= TREE_INT_CST_LOW (expr
);
271 /* Decode the arguments to a "format" attribute into a
272 function_format_info structure. It is already known that the list
273 is of the right length. If VALIDATED_P is true, then these
274 attributes have already been validated and must not be erroneous;
275 if false, it will give an error message. Returns true if the
276 attributes are successfully decoded, false otherwise. */
279 decode_format_attr (tree args
, function_format_info
*info
, int validated_p
)
281 tree format_type_id
= TREE_VALUE (args
);
282 tree format_num_expr
= TREE_VALUE (TREE_CHAIN (args
));
283 tree first_arg_num_expr
284 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args
)));
286 if (TREE_CODE (format_type_id
) != IDENTIFIER_NODE
)
288 gcc_assert (!validated_p
);
289 error ("unrecognized format specifier");
294 const char *p
= IDENTIFIER_POINTER (format_type_id
);
296 p
= convert_format_name_to_system_name (p
);
298 info
->format_type
= decode_format_type (p
);
300 if (!c_dialect_objc ()
301 && info
->format_type
== gcc_objc_string_format_type
)
303 gcc_assert (!validated_p
);
304 warning (OPT_Wformat_
, "%qE is only allowed in Objective-C dialects",
306 info
->format_type
= format_type_error
;
310 if (info
->format_type
== format_type_error
)
312 gcc_assert (!validated_p
);
313 warning (OPT_Wformat_
, "%qE is an unrecognized format function type",
319 if (!get_constant (format_num_expr
, &info
->format_num
, validated_p
))
321 error ("format string has invalid operand number");
325 if (!get_constant (first_arg_num_expr
, &info
->first_arg_num
, validated_p
))
327 error ("%<...%> has invalid operand number");
331 if (info
->first_arg_num
!= 0 && info
->first_arg_num
<= info
->format_num
)
333 gcc_assert (!validated_p
);
334 error ("format string argument follows the args to be formatted");
341 /* Check a call to a format function against a parameter list. */
343 /* The C standard version C++ is treated as equivalent to
344 or inheriting from, for the purpose of format features supported. */
345 #define CPLUSPLUS_STD_VER (cxx_dialect < cxx11 ? STD_C94 : STD_C99)
346 /* The C standard version we are checking formats against when pedantic. */
347 #define C_STD_VER ((int) (c_dialect_cxx () \
348 ? CPLUSPLUS_STD_VER \
351 : (flag_isoc94 ? STD_C94 : STD_C89))))
352 /* The name to give to the standard version we are warning about when
353 pedantic. FEATURE_VER is the version in which the feature warned out
354 appeared, which is higher than C_STD_VER. */
355 #define C_STD_NAME(FEATURE_VER) (c_dialect_cxx () \
356 ? (cxx_dialect < cxx11 ? "ISO C++98" \
358 : ((FEATURE_VER) == STD_EXT \
361 /* Adjust a C standard version, which may be STD_C9L, to account for
362 -Wno-long-long. Returns other standard versions unchanged. */
363 #define ADJ_STD(VER) ((int) ((VER) == STD_C9L \
364 ? (warn_long_long ? STD_C99 : STD_C89) \
367 /* Enum describing the kind of specifiers present in the format and
368 requiring an argument. */
369 enum format_specifier_kind
{
372 CF_KIND_FIELD_PRECISION
375 static const char *kind_descriptions
[] = {
377 N_("field width specifier"),
378 N_("field precision specifier")
381 /* Structure describing details of a type expected in format checking,
382 and the type to check against it. */
383 struct format_wanted_type
385 /* The type wanted. */
387 /* The name of this type to use in diagnostics. */
388 const char *wanted_type_name
;
389 /* Should be type checked just for scalar width identity. */
390 int scalar_identity_flag
;
391 /* The level of indirection through pointers at which this type occurs. */
393 /* Whether, when pointer_count is 1, to allow any character type when
394 pedantic, rather than just the character or void type specified. */
395 int char_lenient_flag
;
396 /* Whether the argument, dereferenced once, is written into and so the
397 argument must not be a pointer to a const-qualified type. */
399 /* Whether the argument, dereferenced once, is read from and so
400 must not be a NULL pointer. */
401 int reading_from_flag
;
402 /* The kind of specifier that this type is used for. */
403 enum format_specifier_kind kind
;
404 /* The starting character of the specifier. This never includes the
405 initial percent sign. */
406 const char *format_start
;
407 /* The length of the specifier. */
409 /* The actual parameter to check against the wanted type. */
411 /* The argument number of that parameter. */
413 /* The offset location of this argument with respect to the format
415 unsigned int offset_loc
;
416 /* The next type to check for this format conversion, or NULL if none. */
417 struct format_wanted_type
*next
;
420 /* Convenience macro for format_length_info meaning unused. */
421 #define NO_FMT NULL, FMT_LEN_none, STD_C89
423 static const format_length_info printf_length_specs
[] =
425 { "h", FMT_LEN_h
, STD_C89
, "hh", FMT_LEN_hh
, STD_C99
, 0 },
426 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C9L
, 0 },
427 { "q", FMT_LEN_ll
, STD_EXT
, NO_FMT
, 0 },
428 { "L", FMT_LEN_L
, STD_C89
, NO_FMT
, 0 },
429 { "z", FMT_LEN_z
, STD_C99
, NO_FMT
, 0 },
430 { "Z", FMT_LEN_z
, STD_EXT
, NO_FMT
, 0 },
431 { "t", FMT_LEN_t
, STD_C99
, NO_FMT
, 0 },
432 { "j", FMT_LEN_j
, STD_C99
, NO_FMT
, 0 },
433 { "H", FMT_LEN_H
, STD_EXT
, NO_FMT
, 0 },
434 { "D", FMT_LEN_D
, STD_EXT
, "DD", FMT_LEN_DD
, STD_EXT
, 0 },
435 { NO_FMT
, NO_FMT
, 0 }
438 /* Length specifiers valid for asm_fprintf. */
439 static const format_length_info asm_fprintf_length_specs
[] =
441 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C89
, 0 },
442 { "w", FMT_LEN_none
, STD_C89
, NO_FMT
, 0 },
443 { NO_FMT
, NO_FMT
, 0 }
446 /* Length specifiers valid for GCC diagnostics. */
447 static const format_length_info gcc_diag_length_specs
[] =
449 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C89
, 0 },
450 { "w", FMT_LEN_none
, STD_C89
, NO_FMT
, 0 },
451 { NO_FMT
, NO_FMT
, 0 }
454 /* The custom diagnostics all accept the same length specifiers. */
455 #define gcc_tdiag_length_specs gcc_diag_length_specs
456 #define gcc_cdiag_length_specs gcc_diag_length_specs
457 #define gcc_cxxdiag_length_specs gcc_diag_length_specs
459 /* This differs from printf_length_specs only in that "Z" is not accepted. */
460 static const format_length_info scanf_length_specs
[] =
462 { "h", FMT_LEN_h
, STD_C89
, "hh", FMT_LEN_hh
, STD_C99
, 0 },
463 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C9L
, 0 },
464 { "q", FMT_LEN_ll
, STD_EXT
, NO_FMT
, 0 },
465 { "L", FMT_LEN_L
, STD_C89
, NO_FMT
, 0 },
466 { "z", FMT_LEN_z
, STD_C99
, NO_FMT
, 0 },
467 { "t", FMT_LEN_t
, STD_C99
, NO_FMT
, 0 },
468 { "j", FMT_LEN_j
, STD_C99
, NO_FMT
, 0 },
469 { "H", FMT_LEN_H
, STD_EXT
, NO_FMT
, 0 },
470 { "D", FMT_LEN_D
, STD_EXT
, "DD", FMT_LEN_DD
, STD_EXT
, 0 },
471 { NO_FMT
, NO_FMT
, 0 }
475 /* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings
476 make no sense for a format type not part of any C standard version. */
477 static const format_length_info strfmon_length_specs
[] =
479 /* A GNU extension. */
480 { "L", FMT_LEN_L
, STD_C89
, NO_FMT
, 0 },
481 { NO_FMT
, NO_FMT
, 0 }
485 /* For now, the Fortran front-end routines only use l as length modifier. */
486 static const format_length_info gcc_gfc_length_specs
[] =
488 { "l", FMT_LEN_l
, STD_C89
, NO_FMT
, 0 },
489 { NO_FMT
, NO_FMT
, 0 }
493 static const format_flag_spec printf_flag_specs
[] =
495 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89
},
496 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89
},
497 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89
},
498 { '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89
},
499 { '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89
},
500 { '\'', 0, 0, N_("''' flag"), N_("the ''' printf flag"), STD_EXT
},
501 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' printf flag"), STD_EXT
},
502 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89
},
503 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89
},
504 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89
},
505 { 0, 0, 0, NULL
, NULL
, STD_C89
}
509 static const format_flag_pair printf_flag_pairs
[] =
513 { '0', 'p', 1, 'i' },
517 static const format_flag_spec asm_fprintf_flag_specs
[] =
519 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89
},
520 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89
},
521 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89
},
522 { '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89
},
523 { '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89
},
524 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89
},
525 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89
},
526 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89
},
527 { 0, 0, 0, NULL
, NULL
, STD_C89
}
530 static const format_flag_pair asm_fprintf_flag_pairs
[] =
534 { '0', 'p', 1, 'i' },
538 static const format_flag_pair gcc_diag_flag_pairs
[] =
543 #define gcc_tdiag_flag_pairs gcc_diag_flag_pairs
544 #define gcc_cdiag_flag_pairs gcc_diag_flag_pairs
545 #define gcc_cxxdiag_flag_pairs gcc_diag_flag_pairs
546 #define gcc_gfc_flag_pairs gcc_diag_flag_pairs
548 static const format_flag_spec gcc_diag_flag_specs
[] =
550 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89
},
551 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89
},
552 { 'q', 0, 0, N_("'q' flag"), N_("the 'q' diagnostic flag"), STD_C89
},
553 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89
},
554 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89
},
555 { 0, 0, 0, NULL
, NULL
, STD_C89
}
558 #define gcc_tdiag_flag_specs gcc_diag_flag_specs
559 #define gcc_cdiag_flag_specs gcc_diag_flag_specs
560 #define gcc_cxxdiag_flag_specs gcc_diag_flag_specs
561 #define gcc_gfc_flag_specs gcc_diag_flag_specs
563 static const format_flag_spec scanf_flag_specs
[] =
565 { '*', 0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89
},
566 { 'a', 0, 0, N_("'a' flag"), N_("the 'a' scanf flag"), STD_EXT
},
567 { 'm', 0, 0, N_("'m' flag"), N_("the 'm' scanf flag"), STD_EXT
},
568 { 'w', 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89
},
569 { 'L', 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89
},
570 { '\'', 0, 0, N_("''' flag"), N_("the ''' scanf flag"), STD_EXT
},
571 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' scanf flag"), STD_EXT
},
572 { 0, 0, 0, NULL
, NULL
, STD_C89
}
576 static const format_flag_pair scanf_flag_pairs
[] =
584 static const format_flag_spec strftime_flag_specs
[] =
586 { '_', 0, 0, N_("'_' flag"), N_("the '_' strftime flag"), STD_EXT
},
587 { '-', 0, 0, N_("'-' flag"), N_("the '-' strftime flag"), STD_EXT
},
588 { '0', 0, 0, N_("'0' flag"), N_("the '0' strftime flag"), STD_EXT
},
589 { '^', 0, 0, N_("'^' flag"), N_("the '^' strftime flag"), STD_EXT
},
590 { '#', 0, 0, N_("'#' flag"), N_("the '#' strftime flag"), STD_EXT
},
591 { 'w', 0, 0, N_("field width"), N_("field width in strftime format"), STD_EXT
},
592 { 'E', 0, 0, N_("'E' modifier"), N_("the 'E' strftime modifier"), STD_C99
},
593 { 'O', 0, 0, N_("'O' modifier"), N_("the 'O' strftime modifier"), STD_C99
},
594 { 'O', 'o', 0, NULL
, N_("the 'O' modifier"), STD_EXT
},
595 { 0, 0, 0, NULL
, NULL
, STD_C89
}
599 static const format_flag_pair strftime_flag_pairs
[] =
610 static const format_flag_spec strfmon_flag_specs
[] =
612 { '=', 0, 1, N_("fill character"), N_("fill character in strfmon format"), STD_C89
},
613 { '^', 0, 0, N_("'^' flag"), N_("the '^' strfmon flag"), STD_C89
},
614 { '+', 0, 0, N_("'+' flag"), N_("the '+' strfmon flag"), STD_C89
},
615 { '(', 0, 0, N_("'(' flag"), N_("the '(' strfmon flag"), STD_C89
},
616 { '!', 0, 0, N_("'!' flag"), N_("the '!' strfmon flag"), STD_C89
},
617 { '-', 0, 0, N_("'-' flag"), N_("the '-' strfmon flag"), STD_C89
},
618 { 'w', 0, 0, N_("field width"), N_("field width in strfmon format"), STD_C89
},
619 { '#', 0, 0, N_("left precision"), N_("left precision in strfmon format"), STD_C89
},
620 { 'p', 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89
},
621 { 'L', 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89
},
622 { 0, 0, 0, NULL
, NULL
, STD_C89
}
625 static const format_flag_pair strfmon_flag_pairs
[] =
632 static const format_char_info print_char_table
[] =
634 /* C89 conversion specifiers. */
635 { "di", 0, STD_C89
, { T89_I
, T99_SC
, T89_S
, T89_L
, T9L_LL
, TEX_LL
, T99_SST
, T99_PD
, T99_IM
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +'I", "i", NULL
},
636 { "oxX", 0, STD_C89
, { T89_UI
, T99_UC
, T89_US
, T89_UL
, T9L_ULL
, TEX_ULL
, T99_ST
, T99_UPD
, T99_UIM
, BADLEN
, BADLEN
, BADLEN
}, "-wp0#", "i", NULL
},
637 { "u", 0, STD_C89
, { T89_UI
, T99_UC
, T89_US
, T89_UL
, T9L_ULL
, TEX_ULL
, T99_ST
, T99_UPD
, T99_UIM
, BADLEN
, BADLEN
, BADLEN
}, "-wp0'I", "i", NULL
},
638 { "fgG", 0, STD_C89
, { T89_D
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T89_LD
, BADLEN
, BADLEN
, BADLEN
, TEX_D32
, TEX_D64
, TEX_D128
}, "-wp0 +#'I", "", NULL
},
639 { "eE", 0, STD_C89
, { T89_D
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T89_LD
, BADLEN
, BADLEN
, BADLEN
, TEX_D32
, TEX_D64
, TEX_D128
}, "-wp0 +#I", "", NULL
},
640 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T94_WI
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "", NULL
},
641 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "cR", NULL
},
642 { "p", 1, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "c", NULL
},
643 { "n", 1, STD_C89
, { T89_I
, T99_SC
, T89_S
, T89_L
, T9L_LL
, BADLEN
, T99_SST
, T99_PD
, T99_IM
, BADLEN
, BADLEN
, BADLEN
}, "", "W", NULL
},
644 /* C99 conversion specifiers. */
645 { "F", 0, STD_C99
, { T99_D
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T99_LD
, BADLEN
, BADLEN
, BADLEN
, TEX_D32
, TEX_D64
, TEX_D128
}, "-wp0 +#'I", "", NULL
},
646 { "aA", 0, STD_C99
, { T99_D
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T99_LD
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +#", "", NULL
},
647 /* X/Open conversion specifiers. */
648 { "C", 0, STD_EXT
, { TEX_WI
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "", NULL
},
649 { "S", 1, STD_EXT
, { TEX_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "R", NULL
},
650 /* GNU conversion specifiers. */
651 { "m", 0, STD_EXT
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "", NULL
},
652 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
655 static const format_char_info asm_fprintf_char_table
[] =
657 /* C89 conversion specifiers. */
658 { "di", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T89_L
, T9L_LL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +", "i", NULL
},
659 { "oxX", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0#", "i", NULL
},
660 { "u", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0", "i", NULL
},
661 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "", NULL
},
662 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "cR", NULL
},
664 /* asm_fprintf conversion specifiers. */
665 { "O", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
666 { "R", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
667 { "I", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
668 { "L", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
669 { "U", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
670 { "r", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "", NULL
},
671 { "@", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
672 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
675 static const format_char_info gcc_diag_char_table
[] =
677 /* C89 conversion specifiers. */
678 { "di", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T89_L
, T9L_LL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
679 { "ox", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
680 { "u", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
681 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
682 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "pq", "cR", NULL
},
683 { "p", 1, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "c", NULL
},
685 /* Custom conversion specifiers. */
687 /* These will require a "tree" at runtime. */
688 { "K", 0, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
690 { "r", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "cR", NULL
},
691 { "<>'R",0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
692 { "m", 0, STD_C89
, NOARGUMENTS
, "q", "", NULL
},
693 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
696 static const format_char_info gcc_tdiag_char_table
[] =
698 /* C89 conversion specifiers. */
699 { "di", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T89_L
, T9L_LL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
700 { "ox", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
701 { "u", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
702 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
703 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "pq", "cR", NULL
},
704 { "p", 1, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "c", NULL
},
706 /* Custom conversion specifiers. */
708 /* These will require a "tree" at runtime. */
709 { "DFKTEV", 0, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+", "", NULL
},
711 { "v", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q#", "", NULL
},
713 { "r", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "cR", NULL
},
714 { "<>'R",0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
715 { "m", 0, STD_C89
, NOARGUMENTS
, "q", "", NULL
},
716 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
719 static const format_char_info gcc_cdiag_char_table
[] =
721 /* C89 conversion specifiers. */
722 { "di", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T89_L
, T9L_LL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
723 { "ox", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
724 { "u", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
725 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
726 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "pq", "cR", NULL
},
727 { "p", 1, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "c", NULL
},
729 /* Custom conversion specifiers. */
731 /* These will require a "tree" at runtime. */
732 { "DEFKTV", 0, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+", "", NULL
},
734 { "v", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q#", "", NULL
},
736 { "r", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "cR", NULL
},
737 { "<>'R",0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
738 { "m", 0, STD_C89
, NOARGUMENTS
, "q", "", NULL
},
739 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
742 static const format_char_info gcc_cxxdiag_char_table
[] =
744 /* C89 conversion specifiers. */
745 { "di", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T89_L
, T9L_LL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
746 { "ox", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
747 { "u", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
748 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
749 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "pq", "cR", NULL
},
750 { "p", 1, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "c", NULL
},
752 /* Custom conversion specifiers. */
754 /* These will require a "tree" at runtime. */
755 { "ADEFKSTVX",0,STD_C89
,{ T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+#", "", NULL
},
757 { "v", 0,STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q#", "", NULL
},
759 /* These accept either an 'int' or an 'enum tree_code' (which is handled as an 'int'.) */
760 { "CLOPQ",0,STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
762 { "r", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "cR", NULL
},
763 { "<>'R",0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
764 { "m", 0, STD_C89
, NOARGUMENTS
, "q", "", NULL
},
765 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
768 static const format_char_info gcc_gfc_char_table
[] =
770 /* C89 conversion specifiers. */
771 { "di", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T89_L
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
772 { "u", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
773 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
774 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "cR", NULL
},
776 /* gfc conversion specifiers. */
778 { "C", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
780 /* This will require a "locus" at runtime. */
781 { "L", 0, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "R", NULL
},
783 /* These will require nothing. */
784 { "<>",0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
785 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
788 static const format_char_info scan_char_table
[] =
790 /* C89 conversion specifiers. */
791 { "di", 1, STD_C89
, { T89_I
, T99_SC
, T89_S
, T89_L
, T9L_LL
, TEX_LL
, T99_SST
, T99_PD
, T99_IM
, BADLEN
, BADLEN
, BADLEN
}, "*w'I", "W", NULL
},
792 { "u", 1, STD_C89
, { T89_UI
, T99_UC
, T89_US
, T89_UL
, T9L_ULL
, TEX_ULL
, T99_ST
, T99_UPD
, T99_UIM
, BADLEN
, BADLEN
, BADLEN
}, "*w'I", "W", NULL
},
793 { "oxX", 1, STD_C89
, { T89_UI
, T99_UC
, T89_US
, T89_UL
, T9L_ULL
, TEX_ULL
, T99_ST
, T99_UPD
, T99_UIM
, BADLEN
, BADLEN
, BADLEN
}, "*w", "W", NULL
},
794 { "efgEG", 1, STD_C89
, { T89_F
, BADLEN
, BADLEN
, T89_D
, BADLEN
, T89_LD
, BADLEN
, BADLEN
, BADLEN
, TEX_D32
, TEX_D64
, TEX_D128
}, "*w'", "W", NULL
},
795 { "c", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*mw", "cW", NULL
},
796 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*amw", "cW", NULL
},
797 { "[", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*amw", "cW[", NULL
},
798 { "p", 2, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*w", "W", NULL
},
799 { "n", 1, STD_C89
, { T89_I
, T99_SC
, T89_S
, T89_L
, T9L_LL
, BADLEN
, T99_SST
, T99_PD
, T99_IM
, BADLEN
, BADLEN
, BADLEN
}, "", "W", NULL
},
800 /* C99 conversion specifiers. */
801 { "F", 1, STD_C99
, { T99_F
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T99_LD
, BADLEN
, BADLEN
, BADLEN
, TEX_D32
, TEX_D64
, TEX_D128
}, "*w'", "W", NULL
},
802 { "aA", 1, STD_C99
, { T99_F
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T99_LD
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*w'", "W", NULL
},
803 /* X/Open conversion specifiers. */
804 { "C", 1, STD_EXT
, { TEX_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*mw", "W", NULL
},
805 { "S", 1, STD_EXT
, { TEX_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*amw", "W", NULL
},
806 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
809 static const format_char_info time_char_table
[] =
811 /* C89 conversion specifiers. */
812 { "ABZab", 0, STD_C89
, NOLENGTHS
, "^#", "", NULL
},
813 { "cx", 0, STD_C89
, NOLENGTHS
, "E", "3", NULL
},
814 { "HIMSUWdmw", 0, STD_C89
, NOLENGTHS
, "-_0Ow", "", NULL
},
815 { "j", 0, STD_C89
, NOLENGTHS
, "-_0Ow", "o", NULL
},
816 { "p", 0, STD_C89
, NOLENGTHS
, "#", "", NULL
},
817 { "X", 0, STD_C89
, NOLENGTHS
, "E", "", NULL
},
818 { "y", 0, STD_C89
, NOLENGTHS
, "EO-_0w", "4", NULL
},
819 { "Y", 0, STD_C89
, NOLENGTHS
, "-_0EOw", "o", NULL
},
820 { "%", 0, STD_C89
, NOLENGTHS
, "", "", NULL
},
821 /* C99 conversion specifiers. */
822 { "C", 0, STD_C99
, NOLENGTHS
, "-_0EOw", "o", NULL
},
823 { "D", 0, STD_C99
, NOLENGTHS
, "", "2", NULL
},
824 { "eVu", 0, STD_C99
, NOLENGTHS
, "-_0Ow", "", NULL
},
825 { "FRTnrt", 0, STD_C99
, NOLENGTHS
, "", "", NULL
},
826 { "g", 0, STD_C99
, NOLENGTHS
, "O-_0w", "2o", NULL
},
827 { "G", 0, STD_C99
, NOLENGTHS
, "-_0Ow", "o", NULL
},
828 { "h", 0, STD_C99
, NOLENGTHS
, "^#", "", NULL
},
829 { "z", 0, STD_C99
, NOLENGTHS
, "O", "o", NULL
},
830 /* GNU conversion specifiers. */
831 { "kls", 0, STD_EXT
, NOLENGTHS
, "-_0Ow", "", NULL
},
832 { "P", 0, STD_EXT
, NOLENGTHS
, "", "", NULL
},
833 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
836 static const format_char_info monetary_char_table
[] =
838 { "in", 0, STD_C89
, { T89_D
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, T89_LD
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "=^+(!-w#p", "", NULL
},
839 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
842 /* This must be in the same order as enum format_type. */
843 static const format_kind_info format_types_orig
[] =
845 { "gnu_printf", printf_length_specs
, print_char_table
, " +#0-'I", NULL
,
846 printf_flag_specs
, printf_flag_pairs
,
847 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_DOLLAR_MULTIPLE
|FMT_FLAG_USE_DOLLAR
|FMT_FLAG_EMPTY_PREC_OK
,
848 'w', 0, 'p', 0, 'L', 0,
849 &integer_type_node
, &integer_type_node
851 { "asm_fprintf", asm_fprintf_length_specs
, asm_fprintf_char_table
, " +#0-", NULL
,
852 asm_fprintf_flag_specs
, asm_fprintf_flag_pairs
,
853 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_EMPTY_PREC_OK
,
854 'w', 0, 'p', 0, 'L', 0,
857 { "gcc_diag", gcc_diag_length_specs
, gcc_diag_char_table
, "q+#", NULL
,
858 gcc_diag_flag_specs
, gcc_diag_flag_pairs
,
859 FMT_FLAG_ARG_CONVERT
,
860 0, 0, 'p', 0, 'L', 0,
861 NULL
, &integer_type_node
863 { "gcc_tdiag", gcc_tdiag_length_specs
, gcc_tdiag_char_table
, "q+#", NULL
,
864 gcc_tdiag_flag_specs
, gcc_tdiag_flag_pairs
,
865 FMT_FLAG_ARG_CONVERT
,
866 0, 0, 'p', 0, 'L', 0,
867 NULL
, &integer_type_node
869 { "gcc_cdiag", gcc_cdiag_length_specs
, gcc_cdiag_char_table
, "q+#", NULL
,
870 gcc_cdiag_flag_specs
, gcc_cdiag_flag_pairs
,
871 FMT_FLAG_ARG_CONVERT
,
872 0, 0, 'p', 0, 'L', 0,
873 NULL
, &integer_type_node
875 { "gcc_cxxdiag", gcc_cxxdiag_length_specs
, gcc_cxxdiag_char_table
, "q+#", NULL
,
876 gcc_cxxdiag_flag_specs
, gcc_cxxdiag_flag_pairs
,
877 FMT_FLAG_ARG_CONVERT
,
878 0, 0, 'p', 0, 'L', 0,
879 NULL
, &integer_type_node
881 { "gcc_gfc", gcc_gfc_length_specs
, gcc_gfc_char_table
, "q+#", NULL
,
882 gcc_gfc_flag_specs
, gcc_gfc_flag_pairs
,
883 FMT_FLAG_ARG_CONVERT
,
887 { "NSString", NULL
, NULL
, NULL
, NULL
,
889 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL
, 0, 0, 0, 0, 0, 0,
892 { "gnu_scanf", scanf_length_specs
, scan_char_table
, "*'I", NULL
,
893 scanf_flag_specs
, scanf_flag_pairs
,
894 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_SCANF_A_KLUDGE
|FMT_FLAG_USE_DOLLAR
|FMT_FLAG_ZERO_WIDTH_BAD
|FMT_FLAG_DOLLAR_GAP_POINTER_OK
,
895 'w', 0, 0, '*', 'L', 'm',
898 { "gnu_strftime", NULL
, time_char_table
, "_-0^#", "EO",
899 strftime_flag_specs
, strftime_flag_pairs
,
900 FMT_FLAG_FANCY_PERCENT_OK
, 'w', 0, 0, 0, 0, 0,
903 { "gnu_strfmon", strfmon_length_specs
, monetary_char_table
, "=^+(!-", NULL
,
904 strfmon_flag_specs
, strfmon_flag_pairs
,
905 FMT_FLAG_ARG_CONVERT
, 'w', '#', 'p', 0, 'L', 0,
910 /* This layer of indirection allows GCC to reassign format_types with
911 new data if necessary, while still allowing the original data to be
913 static const format_kind_info
*format_types
= format_types_orig
;
914 /* We can modify this one. We also add target-specific format types
915 to the end of the array. */
916 static format_kind_info
*dynamic_format_types
;
918 static int n_format_types
= ARRAY_SIZE (format_types_orig
);
920 /* Structure detailing the results of checking a format function call
921 where the format expression may be a conditional expression with
922 many leaves resulting from nested conditional expressions. */
923 struct format_check_results
925 /* Number of leaves of the format argument that could not be checked
926 as they were not string literals. */
927 int number_non_literal
;
928 /* Number of leaves of the format argument that were null pointers or
929 string literals, but had extra format arguments. */
930 int number_extra_args
;
931 location_t extra_arg_loc
;
932 /* Number of leaves of the format argument that were null pointers or
933 string literals, but had extra format arguments and used $ operand
935 int number_dollar_extra_args
;
936 /* Number of leaves of the format argument that were wide string
939 /* Number of leaves of the format argument that were empty strings. */
941 /* Number of leaves of the format argument that were unterminated
943 int number_unterminated
;
944 /* Number of leaves of the format argument that were not counted above. */
946 /* Location of the format string. */
947 location_t format_string_loc
;
950 struct format_check_context
952 format_check_results
*res
;
953 function_format_info
*info
;
957 /* Return the format name (as specified in the original table) for the format
958 type indicated by format_num. */
960 format_name (int format_num
)
962 if (format_num
>= 0 && format_num
< n_format_types
)
963 return format_types
[format_num
].name
;
967 /* Return the format flags (as specified in the original table) for the format
968 type indicated by format_num. */
970 format_flags (int format_num
)
972 if (format_num
>= 0 && format_num
< n_format_types
)
973 return format_types
[format_num
].flags
;
977 static void check_format_info (function_format_info
*, tree
);
978 static void check_format_arg (void *, tree
, unsigned HOST_WIDE_INT
);
979 static void check_format_info_main (format_check_results
*,
980 function_format_info
*, const char *,
983 unsigned HOST_WIDE_INT
,
984 object_allocator
<format_wanted_type
> &);
986 static void init_dollar_format_checking (int, tree
);
987 static int maybe_read_dollar_number (const char **, int,
988 tree
, tree
*, const format_kind_info
*);
989 static bool avoid_dollar_number (const char *);
990 static void finish_dollar_format_checking (format_check_results
*, int);
992 static const format_flag_spec
*get_flag_spec (const format_flag_spec
*,
995 static void check_format_types (const substring_loc
&fmt_loc
,
996 format_wanted_type
*,
997 const format_kind_info
*fki
,
998 int offset_to_type_start
,
999 char conversion_char
);
1000 static void format_type_warning (const substring_loc
&fmt_loc
,
1001 source_range
*param_range
,
1002 format_wanted_type
*, tree
,
1004 const format_kind_info
*fki
,
1005 int offset_to_type_start
,
1006 char conversion_char
);
1008 /* Decode a format type from a string, returning the type, or
1009 format_type_error if not valid, in which case the caller should print an
1012 decode_format_type (const char *s
)
1017 s
= convert_format_name_to_system_name (s
);
1019 for (i
= 0; i
< n_format_types
; i
++)
1022 if (!strcmp (s
, format_types
[i
].name
))
1024 alen
= strlen (format_types
[i
].name
);
1025 if (slen
== alen
+ 4 && s
[0] == '_' && s
[1] == '_'
1026 && s
[slen
- 1] == '_' && s
[slen
- 2] == '_'
1027 && !strncmp (s
+ 2, format_types
[i
].name
, alen
))
1030 return format_type_error
;
1034 /* Check the argument list of a call to printf, scanf, etc.
1035 ATTRS are the attributes on the function type. There are NARGS argument
1036 values in the array ARGARRAY.
1037 Also, if -Wsuggest-attribute=format,
1038 warn for calls to vprintf or vscanf in functions with no such format
1039 attribute themselves. */
1042 check_function_format (tree attrs
, int nargs
, tree
*argarray
)
1046 /* See if this function has any format attributes. */
1047 for (a
= attrs
; a
; a
= TREE_CHAIN (a
))
1049 if (is_attribute_p ("format", TREE_PURPOSE (a
)))
1051 /* Yup; check it. */
1052 function_format_info info
;
1053 decode_format_attr (TREE_VALUE (a
), &info
, /*validated=*/true);
1056 /* FIXME: Rewrite all the internal functions in this file
1057 to use the ARGARRAY directly instead of constructing this
1059 tree params
= NULL_TREE
;
1061 for (i
= nargs
- 1; i
>= 0; i
--)
1062 params
= tree_cons (NULL_TREE
, argarray
[i
], params
);
1063 check_format_info (&info
, params
);
1066 /* Attempt to detect whether the current function might benefit
1067 from the format attribute if the called function is decorated
1068 with it. Avoid using calls with string literal formats for
1069 guidance since those are unlikely to be viable candidates. */
1070 if (warn_suggest_attribute_format
&& info
.first_arg_num
== 0
1071 && (format_types
[info
.format_type
].flags
1072 & (int) FMT_FLAG_ARG_CONVERT
)
1073 /* c_strlen will fail for a function parameter but succeed
1074 for a literal or constant array. */
1075 && !c_strlen (argarray
[info
.format_num
- 1], 1))
1078 for (c
= TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl
));
1081 if (is_attribute_p ("format", TREE_PURPOSE (c
))
1082 && (decode_format_type (IDENTIFIER_POINTER
1083 (TREE_VALUE (TREE_VALUE (c
))))
1084 == info
.format_type
))
1088 /* Check if the current function has a parameter to which
1089 the format attribute could be attached; if not, it
1090 can't be a candidate for a format attribute, despite
1091 the vprintf-like or vscanf-like call. */
1093 for (args
= DECL_ARGUMENTS (current_function_decl
);
1095 args
= DECL_CHAIN (args
))
1097 if (TREE_CODE (TREE_TYPE (args
)) == POINTER_TYPE
1098 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args
)))
1103 warning (OPT_Wsuggest_attribute_format
, "function %qD "
1104 "might be a candidate for %qs format attribute",
1105 current_function_decl
,
1106 format_types
[info
.format_type
].name
);
1114 /* Variables used by the checking of $ operand number formats. */
1115 static char *dollar_arguments_used
= NULL
;
1116 static char *dollar_arguments_pointer_p
= NULL
;
1117 static int dollar_arguments_alloc
= 0;
1118 static int dollar_arguments_count
;
1119 static int dollar_first_arg_num
;
1120 static int dollar_max_arg_used
;
1121 static int dollar_format_warned
;
1123 /* Initialize the checking for a format string that may contain $
1124 parameter number specifications; we will need to keep track of whether
1125 each parameter has been used. FIRST_ARG_NUM is the number of the first
1126 argument that is a parameter to the format, or 0 for a vprintf-style
1127 function; PARAMS is the list of arguments starting at this argument. */
1130 init_dollar_format_checking (int first_arg_num
, tree params
)
1132 tree oparams
= params
;
1134 dollar_first_arg_num
= first_arg_num
;
1135 dollar_arguments_count
= 0;
1136 dollar_max_arg_used
= 0;
1137 dollar_format_warned
= 0;
1138 if (first_arg_num
> 0)
1142 dollar_arguments_count
++;
1143 params
= TREE_CHAIN (params
);
1146 if (dollar_arguments_alloc
< dollar_arguments_count
)
1148 free (dollar_arguments_used
);
1149 free (dollar_arguments_pointer_p
);
1150 dollar_arguments_alloc
= dollar_arguments_count
;
1151 dollar_arguments_used
= XNEWVEC (char, dollar_arguments_alloc
);
1152 dollar_arguments_pointer_p
= XNEWVEC (char, dollar_arguments_alloc
);
1154 if (dollar_arguments_alloc
)
1156 memset (dollar_arguments_used
, 0, dollar_arguments_alloc
);
1157 if (first_arg_num
> 0)
1163 dollar_arguments_pointer_p
[i
] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params
)))
1165 params
= TREE_CHAIN (params
);
1173 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
1174 is set, it is an error if one is not found; otherwise, it is OK. If
1175 such a number is found, check whether it is within range and mark that
1176 numbered operand as being used for later checking. Returns the operand
1177 number if found and within range, zero if no such number was found and
1178 this is OK, or -1 on error. PARAMS points to the first operand of the
1179 format; PARAM_PTR is made to point to the parameter referred to. If
1180 a $ format is found, *FORMAT is updated to point just after it. */
1183 maybe_read_dollar_number (const char **format
,
1184 int dollar_needed
, tree params
, tree
*param_ptr
,
1185 const format_kind_info
*fki
)
1189 const char *fcp
= *format
;
1190 if (!ISDIGIT (*fcp
))
1194 warning (OPT_Wformat_
, "missing $ operand number in format");
1202 while (ISDIGIT (*fcp
))
1205 nargnum
= 10 * argnum
+ (*fcp
- '0');
1206 if (nargnum
< 0 || nargnum
/ 10 != argnum
)
1215 warning (OPT_Wformat_
, "missing $ operand number in format");
1222 if (pedantic
&& !dollar_format_warned
)
1224 warning (OPT_Wformat_
, "%s does not support %%n$ operand number formats",
1225 C_STD_NAME (STD_EXT
));
1226 dollar_format_warned
= 1;
1228 if (overflow_flag
|| argnum
== 0
1229 || (dollar_first_arg_num
&& argnum
> dollar_arguments_count
))
1231 warning (OPT_Wformat_
, "operand number out of range in format");
1234 if (argnum
> dollar_max_arg_used
)
1235 dollar_max_arg_used
= argnum
;
1236 /* For vprintf-style functions we may need to allocate more memory to
1237 track which arguments are used. */
1238 while (dollar_arguments_alloc
< dollar_max_arg_used
)
1241 nalloc
= 2 * dollar_arguments_alloc
+ 16;
1242 dollar_arguments_used
= XRESIZEVEC (char, dollar_arguments_used
,
1244 dollar_arguments_pointer_p
= XRESIZEVEC (char, dollar_arguments_pointer_p
,
1246 memset (dollar_arguments_used
+ dollar_arguments_alloc
, 0,
1247 nalloc
- dollar_arguments_alloc
);
1248 dollar_arguments_alloc
= nalloc
;
1250 if (!(fki
->flags
& (int) FMT_FLAG_DOLLAR_MULTIPLE
)
1251 && dollar_arguments_used
[argnum
- 1] == 1)
1253 dollar_arguments_used
[argnum
- 1] = 2;
1254 warning (OPT_Wformat_
, "format argument %d used more than once in %s format",
1258 dollar_arguments_used
[argnum
- 1] = 1;
1259 if (dollar_first_arg_num
)
1262 *param_ptr
= params
;
1263 for (i
= 1; i
< argnum
&& *param_ptr
!= 0; i
++)
1264 *param_ptr
= TREE_CHAIN (*param_ptr
);
1266 /* This case shouldn't be caught here. */
1267 gcc_assert (*param_ptr
);
1274 /* Ensure that FORMAT does not start with a decimal number followed by
1275 a $; give a diagnostic and return true if it does, false otherwise. */
1278 avoid_dollar_number (const char *format
)
1280 if (!ISDIGIT (*format
))
1282 while (ISDIGIT (*format
))
1286 warning (OPT_Wformat_
, "$ operand number used after format without operand number");
1293 /* Finish the checking for a format string that used $ operand number formats
1294 instead of non-$ formats. We check for unused operands before used ones
1295 (a serious error, since the implementation of the format function
1296 can't know what types to pass to va_arg to find the later arguments).
1297 and for unused operands at the end of the format (if we know how many
1298 arguments the format had, so not for vprintf). If there were operand
1299 numbers out of range on a non-vprintf-style format, we won't have reached
1300 here. If POINTER_GAP_OK, unused arguments are OK if all arguments are
1304 finish_dollar_format_checking (format_check_results
*res
, int pointer_gap_ok
)
1307 bool found_pointer_gap
= false;
1308 for (i
= 0; i
< dollar_max_arg_used
; i
++)
1310 if (!dollar_arguments_used
[i
])
1312 if (pointer_gap_ok
&& (dollar_first_arg_num
== 0
1313 || dollar_arguments_pointer_p
[i
]))
1314 found_pointer_gap
= true;
1316 warning_at (res
->format_string_loc
, OPT_Wformat_
,
1317 "format argument %d unused before used argument %d in $-style format",
1318 i
+ 1, dollar_max_arg_used
);
1321 if (found_pointer_gap
1322 || (dollar_first_arg_num
1323 && dollar_max_arg_used
< dollar_arguments_count
))
1325 res
->number_other
--;
1326 res
->number_dollar_extra_args
++;
1331 /* Retrieve the specification for a format flag. SPEC contains the
1332 specifications for format flags for the applicable kind of format.
1333 FLAG is the flag in question. If PREDICATES is NULL, the basic
1334 spec for that flag must be retrieved and must exist. If
1335 PREDICATES is not NULL, it is a string listing possible predicates
1336 for the spec entry; if an entry predicated on any of these is
1337 found, it is returned, otherwise NULL is returned. */
1339 static const format_flag_spec
*
1340 get_flag_spec (const format_flag_spec
*spec
, int flag
, const char *predicates
)
1343 for (i
= 0; spec
[i
].flag_char
!= 0; i
++)
1345 if (spec
[i
].flag_char
!= flag
)
1347 if (predicates
!= NULL
)
1349 if (spec
[i
].predicate
!= 0
1350 && strchr (predicates
, spec
[i
].predicate
) != 0)
1353 else if (spec
[i
].predicate
== 0)
1356 gcc_assert (predicates
);
1361 /* Check the argument list of a call to printf, scanf, etc.
1362 INFO points to the function_format_info structure.
1363 PARAMS is the list of argument values. */
1366 check_format_info (function_format_info
*info
, tree params
)
1368 format_check_context format_ctx
;
1369 unsigned HOST_WIDE_INT arg_num
;
1371 format_check_results res
;
1372 /* Skip to format argument. If the argument isn't available, there's
1373 no work for us to do; prototype checking will catch the problem. */
1374 for (arg_num
= 1; ; ++arg_num
)
1378 if (arg_num
== info
->format_num
)
1380 params
= TREE_CHAIN (params
);
1382 format_tree
= TREE_VALUE (params
);
1383 params
= TREE_CHAIN (params
);
1384 if (format_tree
== 0)
1387 res
.number_non_literal
= 0;
1388 res
.number_extra_args
= 0;
1389 res
.extra_arg_loc
= UNKNOWN_LOCATION
;
1390 res
.number_dollar_extra_args
= 0;
1391 res
.number_wide
= 0;
1392 res
.number_empty
= 0;
1393 res
.number_unterminated
= 0;
1394 res
.number_other
= 0;
1395 res
.format_string_loc
= input_location
;
1397 format_ctx
.res
= &res
;
1398 format_ctx
.info
= info
;
1399 format_ctx
.params
= params
;
1401 check_function_arguments_recurse (check_format_arg
, &format_ctx
,
1402 format_tree
, arg_num
);
1404 location_t loc
= format_ctx
.res
->format_string_loc
;
1406 if (res
.number_non_literal
> 0)
1408 /* Functions taking a va_list normally pass a non-literal format
1409 string. These functions typically are declared with
1410 first_arg_num == 0, so avoid warning in those cases. */
1411 if (!(format_types
[info
->format_type
].flags
& (int) FMT_FLAG_ARG_CONVERT
))
1413 /* For strftime-like formats, warn for not checking the format
1414 string; but there are no arguments to check. */
1415 warning_at (loc
, OPT_Wformat_nonliteral
,
1416 "format not a string literal, format string not checked");
1418 else if (info
->first_arg_num
!= 0)
1420 /* If there are no arguments for the format at all, we may have
1421 printf (foo) which is likely to be a security hole. */
1422 while (arg_num
+ 1 < info
->first_arg_num
)
1426 params
= TREE_CHAIN (params
);
1429 if (params
== 0 && warn_format_security
)
1430 warning_at (loc
, OPT_Wformat_security
,
1431 "format not a string literal and no format arguments");
1432 else if (params
== 0 && warn_format_nonliteral
)
1433 warning_at (loc
, OPT_Wformat_nonliteral
,
1434 "format not a string literal and no format arguments");
1436 warning_at (loc
, OPT_Wformat_nonliteral
,
1437 "format not a string literal, argument types not checked");
1441 /* If there were extra arguments to the format, normally warn. However,
1442 the standard does say extra arguments are ignored, so in the specific
1443 case where we have multiple leaves (conditional expressions or
1444 ngettext) allow extra arguments if at least one leaf didn't have extra
1445 arguments, but was otherwise OK (either non-literal or checked OK).
1446 If the format is an empty string, this should be counted similarly to the
1447 case of extra format arguments. */
1448 if (res
.number_extra_args
> 0 && res
.number_non_literal
== 0
1449 && res
.number_other
== 0)
1451 if (res
.extra_arg_loc
== UNKNOWN_LOCATION
)
1452 res
.extra_arg_loc
= loc
;
1453 warning_at (res
.extra_arg_loc
, OPT_Wformat_extra_args
,
1454 "too many arguments for format");
1456 if (res
.number_dollar_extra_args
> 0 && res
.number_non_literal
== 0
1457 && res
.number_other
== 0)
1458 warning_at (loc
, OPT_Wformat_extra_args
, "unused arguments in $-style format");
1459 if (res
.number_empty
> 0 && res
.number_non_literal
== 0
1460 && res
.number_other
== 0)
1461 warning_at (loc
, OPT_Wformat_zero_length
, "zero-length %s format string",
1462 format_types
[info
->format_type
].name
);
1464 if (res
.number_wide
> 0)
1465 warning_at (loc
, OPT_Wformat_
, "format is a wide character string");
1467 if (res
.number_unterminated
> 0)
1468 warning_at (loc
, OPT_Wformat_
, "unterminated format string");
1471 /* Callback from check_function_arguments_recurse to check a
1472 format string. FORMAT_TREE is the format parameter. ARG_NUM
1473 is the number of the format argument. CTX points to a
1474 format_check_context. */
1477 check_format_arg (void *ctx
, tree format_tree
,
1478 unsigned HOST_WIDE_INT arg_num
)
1480 format_check_context
*format_ctx
= (format_check_context
*) ctx
;
1481 format_check_results
*res
= format_ctx
->res
;
1482 function_format_info
*info
= format_ctx
->info
;
1483 tree params
= format_ctx
->params
;
1486 HOST_WIDE_INT offset
;
1487 const char *format_chars
;
1488 tree array_size
= 0;
1491 location_t fmt_param_loc
= EXPR_LOC_OR_LOC (format_tree
, input_location
);
1493 if (VAR_P (format_tree
))
1495 /* Pull out a constant value if the front end didn't. */
1496 format_tree
= decl_constant_value (format_tree
);
1497 STRIP_NOPS (format_tree
);
1500 if (integer_zerop (format_tree
))
1502 /* Skip to first argument to check, so we can see if this format
1503 has any arguments (it shouldn't). */
1504 while (arg_num
+ 1 < info
->first_arg_num
)
1508 params
= TREE_CHAIN (params
);
1513 res
->number_other
++;
1516 if (res
->number_extra_args
== 0)
1517 res
->extra_arg_loc
= EXPR_LOC_OR_LOC (TREE_VALUE (params
),
1519 res
->number_extra_args
++;
1525 if (TREE_CODE (format_tree
) == POINTER_PLUS_EXPR
)
1529 arg0
= TREE_OPERAND (format_tree
, 0);
1530 arg1
= TREE_OPERAND (format_tree
, 1);
1533 if (TREE_CODE (arg1
) == INTEGER_CST
)
1537 res
->number_non_literal
++;
1540 /* POINTER_PLUS_EXPR offsets are to be interpreted signed. */
1541 if (!cst_and_fits_in_hwi (arg1
))
1543 res
->number_non_literal
++;
1546 offset
= int_cst_value (arg1
);
1548 if (TREE_CODE (format_tree
) != ADDR_EXPR
)
1550 res
->number_non_literal
++;
1553 res
->format_string_loc
= EXPR_LOC_OR_LOC (format_tree
, input_location
);
1554 format_tree
= TREE_OPERAND (format_tree
, 0);
1555 if (format_types
[info
->format_type
].flags
1556 & (int) FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL
)
1558 bool objc_str
= (info
->format_type
== gcc_objc_string_format_type
);
1559 /* We cannot examine this string here - but we can check that it is
1561 if (TREE_CODE (format_tree
) != CONST_DECL
1562 || !((objc_str
&& objc_string_ref_type_p (TREE_TYPE (format_tree
)))
1563 || (*targetcm
.string_object_ref_type_p
)
1564 ((const_tree
) TREE_TYPE (format_tree
))))
1566 res
->number_non_literal
++;
1569 /* Skip to first argument to check. */
1570 while (arg_num
+ 1 < info
->first_arg_num
)
1574 params
= TREE_CHAIN (params
);
1577 /* So, we have a valid literal string object and one or more params.
1578 We need to use an external helper to parse the string into format
1579 info. For Objective-C variants we provide the resource within the
1580 objc tree, for target variants, via a hook. */
1582 objc_check_format_arg (format_tree
, params
);
1583 else if (targetcm
.check_string_object_format_arg
)
1584 (*targetcm
.check_string_object_format_arg
) (format_tree
, params
);
1585 /* Else we can't handle it and retire quietly. */
1588 if (TREE_CODE (format_tree
) == ARRAY_REF
1589 && tree_fits_shwi_p (TREE_OPERAND (format_tree
, 1))
1590 && (offset
+= tree_to_shwi (TREE_OPERAND (format_tree
, 1))) >= 0)
1591 format_tree
= TREE_OPERAND (format_tree
, 0);
1594 res
->number_non_literal
++;
1597 if (VAR_P (format_tree
)
1598 && TREE_CODE (TREE_TYPE (format_tree
)) == ARRAY_TYPE
1599 && (array_init
= decl_constant_value (format_tree
)) != format_tree
1600 && TREE_CODE (array_init
) == STRING_CST
)
1602 /* Extract the string constant initializer. Note that this may include
1603 a trailing NUL character that is not in the array (e.g.
1604 const char a[3] = "foo";). */
1605 array_size
= DECL_SIZE_UNIT (format_tree
);
1606 format_tree
= array_init
;
1608 if (TREE_CODE (format_tree
) != STRING_CST
)
1610 res
->number_non_literal
++;
1613 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree
))) != char_type_node
)
1618 format_chars
= TREE_STRING_POINTER (format_tree
);
1619 format_length
= TREE_STRING_LENGTH (format_tree
);
1620 if (array_size
!= 0)
1622 /* Variable length arrays can't be initialized. */
1623 gcc_assert (TREE_CODE (array_size
) == INTEGER_CST
);
1625 if (tree_fits_shwi_p (array_size
))
1627 HOST_WIDE_INT array_size_value
= tree_to_shwi (array_size
);
1628 if (array_size_value
> 0
1629 && array_size_value
== (int) array_size_value
1630 && format_length
> array_size_value
)
1631 format_length
= array_size_value
;
1636 if (offset
>= format_length
)
1638 res
->number_non_literal
++;
1641 format_chars
+= offset
;
1642 format_length
-= offset
;
1644 if (format_length
< 1 || format_chars
[--format_length
] != 0)
1646 res
->number_unterminated
++;
1649 if (format_length
== 0)
1651 res
->number_empty
++;
1655 /* Skip to first argument to check. */
1656 while (arg_num
+ 1 < info
->first_arg_num
)
1660 params
= TREE_CHAIN (params
);
1663 /* Provisionally increment res->number_other; check_format_info_main
1664 will decrement it if it finds there are extra arguments, but this way
1665 need not adjust it for every return. */
1666 res
->number_other
++;
1667 object_allocator
<format_wanted_type
> fwt_pool ("format_wanted_type pool");
1668 check_format_info_main (res
, info
, format_chars
, fmt_param_loc
, format_tree
,
1669 format_length
, params
, arg_num
, fwt_pool
);
1672 /* Support class for argument_parser and check_format_info_main.
1673 Tracks any flag characters that have been applied to the
1674 current argument. */
1680 bool has_char_p (char ch
) const;
1681 void add_char (char ch
);
1682 void validate (const format_kind_info
*fki
,
1683 const format_char_info
*fci
,
1684 const format_flag_spec
*flag_specs
,
1685 const char * const format_chars
,
1686 tree format_string_cst
,
1687 location_t format_string_loc
,
1688 const char * const orig_format_chars
,
1690 int get_alloc_flag (const format_kind_info
*fki
);
1691 int assignment_suppression_p (const format_kind_info
*fki
);
1694 char m_flag_chars
[256];
1697 /* Support struct for argument_parser and check_format_info_main.
1698 Encapsulates any length modifier applied to the current argument. */
1700 struct length_modifier
1703 : chars (NULL
), val (FMT_LEN_none
), std (STD_C89
),
1704 scalar_identity_flag (0)
1708 length_modifier (const char *chars_
,
1709 enum format_lengths val_
,
1710 enum format_std_version std_
,
1711 int scalar_identity_flag_
)
1712 : chars (chars_
), val (val_
), std (std_
),
1713 scalar_identity_flag (scalar_identity_flag_
)
1718 enum format_lengths val
;
1719 enum format_std_version std
;
1720 int scalar_identity_flag
;
1723 /* Parsing one argument within a format string. */
1725 class argument_parser
1728 argument_parser (function_format_info
*info
, const char *&format_chars
,
1729 tree format_string_cst
,
1730 const char * const orig_format_chars
,
1731 location_t format_string_loc
, flag_chars_t
&flag_chars
,
1732 int &has_operand_number
, tree first_fillin_param
,
1733 object_allocator
<format_wanted_type
> &fwt_pool_
);
1735 bool read_any_dollar ();
1737 bool read_format_flags ();
1740 read_any_format_width (tree
¶ms
,
1741 unsigned HOST_WIDE_INT
&arg_num
);
1744 read_any_format_left_precision ();
1747 read_any_format_precision (tree
¶ms
,
1748 unsigned HOST_WIDE_INT
&arg_num
);
1750 void handle_alloc_chars ();
1752 length_modifier
read_any_length_modifier ();
1754 void read_any_other_modifier ();
1756 const format_char_info
*find_format_char_info (char format_char
);
1759 validate_flag_pairs (const format_char_info
*fci
,
1763 give_y2k_warnings (const format_char_info
*fci
,
1766 void parse_any_scan_set (const format_char_info
*fci
);
1768 bool handle_conversions (const format_char_info
*fci
,
1769 const length_modifier
&len_modifier
,
1771 const char *&wanted_type_name
,
1772 unsigned HOST_WIDE_INT
&arg_num
,
1777 check_argument_type (const format_char_info
*fci
,
1778 const length_modifier
&len_modifier
,
1780 const char *&wanted_type_name
,
1781 const bool suppressed
,
1782 unsigned HOST_WIDE_INT
&arg_num
,
1784 const int alloc_flag
,
1785 const char * const format_start
,
1786 const char * const type_start
,
1787 location_t fmt_param_loc
,
1788 char conversion_char
);
1791 const function_format_info
*const info
;
1792 const format_kind_info
* const fki
;
1793 const format_flag_spec
* const flag_specs
;
1794 const char *start_of_this_format
;
1795 const char *&format_chars
;
1796 const tree format_string_cst
;
1797 const char * const orig_format_chars
;
1798 const location_t format_string_loc
;
1799 object_allocator
<format_wanted_type
> &fwt_pool
;
1800 flag_chars_t
&flag_chars
;
1802 tree main_arg_params
;
1803 int &has_operand_number
;
1804 const tree first_fillin_param
;
1805 format_wanted_type width_wanted_type
;
1806 format_wanted_type precision_wanted_type
;
1808 format_wanted_type main_wanted_type
;
1810 format_wanted_type
*first_wanted_type
;
1811 format_wanted_type
*last_wanted_type
;
1814 /* flag_chars_t's constructor. */
1816 flag_chars_t::flag_chars_t ()
1818 m_flag_chars
[0] = 0;
1821 /* Has CH been seen as a flag within the current argument? */
1824 flag_chars_t::has_char_p (char ch
) const
1826 return strchr (m_flag_chars
, ch
) != 0;
1829 /* Add CH to the flags seen within the current argument. */
1832 flag_chars_t::add_char (char ch
)
1834 int i
= strlen (m_flag_chars
);
1835 m_flag_chars
[i
++] = ch
;
1836 m_flag_chars
[i
] = 0;
1839 /* Validate the individual flags used, removing any that are invalid. */
1842 flag_chars_t::validate (const format_kind_info
*fki
,
1843 const format_char_info
*fci
,
1844 const format_flag_spec
*flag_specs
,
1845 const char * const format_chars
,
1846 tree format_string_cst
,
1847 location_t format_string_loc
,
1848 const char * const orig_format_chars
,
1853 for (i
= 0; m_flag_chars
[i
] != 0; i
++)
1855 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
1856 m_flag_chars
[i
], NULL
);
1857 m_flag_chars
[i
- d
] = m_flag_chars
[i
];
1858 if (m_flag_chars
[i
] == fki
->length_code_char
)
1860 if (strchr (fci
->flag_chars
, m_flag_chars
[i
]) == 0)
1862 format_warning_at_char (format_string_loc
, format_string_cst
,
1863 format_chars
- orig_format_chars
,
1865 "%s used with %<%%%c%> %s format",
1866 _(s
->name
), format_char
, fki
->name
);
1872 const format_flag_spec
*t
;
1873 if (ADJ_STD (s
->std
) > C_STD_VER
)
1874 warning_at (format_string_loc
, OPT_Wformat_
,
1875 "%s does not support %s",
1876 C_STD_NAME (s
->std
), _(s
->long_name
));
1877 t
= get_flag_spec (flag_specs
, m_flag_chars
[i
], fci
->flags2
);
1878 if (t
!= NULL
&& ADJ_STD (t
->std
) > ADJ_STD (s
->std
))
1880 const char *long_name
= (t
->long_name
!= NULL
1883 if (ADJ_STD (t
->std
) > C_STD_VER
)
1884 warning_at (format_string_loc
, OPT_Wformat_
,
1885 "%s does not support %s with"
1886 " the %<%%%c%> %s format",
1887 C_STD_NAME (t
->std
), _(long_name
),
1888 format_char
, fki
->name
);
1892 m_flag_chars
[i
- d
] = 0;
1895 /* Determine if an assignment-allocation has been set, requiring
1896 an extra char ** for writing back a dynamically-allocated char *.
1897 This is for handling the optional 'm' character in scanf. */
1900 flag_chars_t::get_alloc_flag (const format_kind_info
*fki
)
1902 if ((fki
->flags
& (int) FMT_FLAG_SCANF_A_KLUDGE
)
1903 && has_char_p ('a'))
1905 if (fki
->alloc_char
&& has_char_p (fki
->alloc_char
))
1910 /* Determine if an assignment-suppression character was seen.
1911 ('*' in scanf, for discarding the converted input). */
1914 flag_chars_t::assignment_suppression_p (const format_kind_info
*fki
)
1916 if (fki
->suppression_char
1917 && has_char_p (fki
->suppression_char
))
1922 /* Constructor for argument_parser. Initialize for parsing one
1923 argument within a format string. */
1926 argument_parser (function_format_info
*info_
, const char *&format_chars_
,
1927 tree format_string_cst_
,
1928 const char * const orig_format_chars_
,
1929 location_t format_string_loc_
,
1930 flag_chars_t
&flag_chars_
,
1931 int &has_operand_number_
,
1932 tree first_fillin_param_
,
1933 object_allocator
<format_wanted_type
> &fwt_pool_
)
1935 fki (&format_types
[info
->format_type
]),
1936 flag_specs (fki
->flag_specs
),
1937 start_of_this_format (format_chars_
),
1938 format_chars (format_chars_
),
1939 format_string_cst (format_string_cst_
),
1940 orig_format_chars (orig_format_chars_
),
1941 format_string_loc (format_string_loc_
),
1942 fwt_pool (fwt_pool_
),
1943 flag_chars (flag_chars_
),
1945 main_arg_params (NULL
),
1946 has_operand_number (has_operand_number_
),
1947 first_fillin_param (first_fillin_param_
),
1948 first_wanted_type (NULL
),
1949 last_wanted_type (NULL
)
1953 /* Handle dollars at the start of format arguments, setting up main_arg_params
1956 Return true if format parsing is to continue, false otherwise. */
1959 argument_parser::read_any_dollar ()
1961 if ((fki
->flags
& (int) FMT_FLAG_USE_DOLLAR
) && has_operand_number
!= 0)
1963 /* Possibly read a $ operand number at the start of the format.
1964 If one was previously used, one is required here. If one
1965 is not used here, we can't immediately conclude this is a
1966 format without them, since it could be printf %m or scanf %*. */
1968 opnum
= maybe_read_dollar_number (&format_chars
, 0,
1970 &main_arg_params
, fki
);
1975 has_operand_number
= 1;
1976 main_arg_num
= opnum
+ info
->first_arg_num
- 1;
1979 else if (fki
->flags
& FMT_FLAG_USE_DOLLAR
)
1981 if (avoid_dollar_number (format_chars
))
1987 /* Read any format flags, but do not yet validate them beyond removing
1988 duplicates, since in general validation depends on the rest of
1991 Return true if format parsing is to continue, false otherwise. */
1994 argument_parser::read_format_flags ()
1996 while (*format_chars
!= 0
1997 && strchr (fki
->flag_chars
, *format_chars
) != 0)
1999 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
2000 *format_chars
, NULL
);
2001 if (flag_chars
.has_char_p (*format_chars
))
2003 format_warning_at_char (format_string_loc
, format_string_cst
,
2004 format_chars
+ 1 - orig_format_chars
,
2006 "repeated %s in format", _(s
->name
));
2009 flag_chars
.add_char (*format_chars
);
2011 if (s
->skip_next_char
)
2014 if (*format_chars
== 0)
2016 warning_at (format_string_loc
, OPT_Wformat_
,
2017 "missing fill character at end of strfmon format");
2027 /* Read any format width, possibly * or *m$.
2029 Return true if format parsing is to continue, false otherwise. */
2033 read_any_format_width (tree
¶ms
,
2034 unsigned HOST_WIDE_INT
&arg_num
)
2036 if (!fki
->width_char
)
2039 if (fki
->width_type
!= NULL
&& *format_chars
== '*')
2041 flag_chars
.add_char (fki
->width_char
);
2042 /* "...a field width...may be indicated by an asterisk.
2043 In this case, an int argument supplies the field width..." */
2045 if (has_operand_number
!= 0)
2048 opnum
= maybe_read_dollar_number (&format_chars
,
2049 has_operand_number
== 1,
2056 has_operand_number
= 1;
2057 arg_num
= opnum
+ info
->first_arg_num
- 1;
2060 has_operand_number
= 0;
2064 if (avoid_dollar_number (format_chars
))
2067 if (info
->first_arg_num
!= 0)
2074 cur_param
= TREE_VALUE (params
);
2075 if (has_operand_number
<= 0)
2077 params
= TREE_CHAIN (params
);
2081 width_wanted_type
.wanted_type
= *fki
->width_type
;
2082 width_wanted_type
.wanted_type_name
= NULL
;
2083 width_wanted_type
.pointer_count
= 0;
2084 width_wanted_type
.char_lenient_flag
= 0;
2085 width_wanted_type
.scalar_identity_flag
= 0;
2086 width_wanted_type
.writing_in_flag
= 0;
2087 width_wanted_type
.reading_from_flag
= 0;
2088 width_wanted_type
.kind
= CF_KIND_FIELD_WIDTH
;
2089 width_wanted_type
.format_start
= format_chars
- 1;
2090 width_wanted_type
.format_length
= 1;
2091 width_wanted_type
.param
= cur_param
;
2092 width_wanted_type
.arg_num
= arg_num
;
2093 width_wanted_type
.offset_loc
=
2094 format_chars
- orig_format_chars
;
2095 width_wanted_type
.next
= NULL
;
2096 if (last_wanted_type
!= 0)
2097 last_wanted_type
->next
= &width_wanted_type
;
2098 if (first_wanted_type
== 0)
2099 first_wanted_type
= &width_wanted_type
;
2100 last_wanted_type
= &width_wanted_type
;
2105 /* Possibly read a numeric width. If the width is zero,
2106 we complain if appropriate. */
2107 int non_zero_width_char
= FALSE
;
2108 int found_width
= FALSE
;
2109 while (ISDIGIT (*format_chars
))
2112 if (*format_chars
!= '0')
2113 non_zero_width_char
= TRUE
;
2116 if (found_width
&& !non_zero_width_char
&&
2117 (fki
->flags
& (int) FMT_FLAG_ZERO_WIDTH_BAD
))
2118 warning_at (format_string_loc
, OPT_Wformat_
,
2119 "zero width in %s format", fki
->name
);
2121 flag_chars
.add_char (fki
->width_char
);
2127 /* Read any format left precision (must be a number, not *). */
2129 argument_parser::read_any_format_left_precision ()
2131 if (fki
->left_precision_char
== 0)
2133 if (*format_chars
!= '#')
2137 flag_chars
.add_char (fki
->left_precision_char
);
2138 if (!ISDIGIT (*format_chars
))
2139 format_warning_at_char (format_string_loc
, format_string_cst
,
2140 format_chars
- orig_format_chars
,
2142 "empty left precision in %s format", fki
->name
);
2143 while (ISDIGIT (*format_chars
))
2147 /* Read any format precision, possibly * or *m$.
2149 Return true if format parsing is to continue, false otherwise. */
2153 read_any_format_precision (tree
¶ms
,
2154 unsigned HOST_WIDE_INT
&arg_num
)
2156 if (fki
->precision_char
== 0)
2158 if (*format_chars
!= '.')
2162 flag_chars
.add_char (fki
->precision_char
);
2163 if (fki
->precision_type
!= NULL
&& *format_chars
== '*')
2165 /* "...a...precision...may be indicated by an asterisk.
2166 In this case, an int argument supplies the...precision." */
2168 if (has_operand_number
!= 0)
2171 opnum
= maybe_read_dollar_number (&format_chars
,
2172 has_operand_number
== 1,
2179 has_operand_number
= 1;
2180 arg_num
= opnum
+ info
->first_arg_num
- 1;
2183 has_operand_number
= 0;
2187 if (avoid_dollar_number (format_chars
))
2190 if (info
->first_arg_num
!= 0)
2197 cur_param
= TREE_VALUE (params
);
2198 if (has_operand_number
<= 0)
2200 params
= TREE_CHAIN (params
);
2204 precision_wanted_type
.wanted_type
= *fki
->precision_type
;
2205 precision_wanted_type
.wanted_type_name
= NULL
;
2206 precision_wanted_type
.pointer_count
= 0;
2207 precision_wanted_type
.char_lenient_flag
= 0;
2208 precision_wanted_type
.scalar_identity_flag
= 0;
2209 precision_wanted_type
.writing_in_flag
= 0;
2210 precision_wanted_type
.reading_from_flag
= 0;
2211 precision_wanted_type
.kind
= CF_KIND_FIELD_PRECISION
;
2212 precision_wanted_type
.param
= cur_param
;
2213 precision_wanted_type
.format_start
= format_chars
- 2;
2214 precision_wanted_type
.format_length
= 2;
2215 precision_wanted_type
.arg_num
= arg_num
;
2216 precision_wanted_type
.offset_loc
=
2217 format_chars
- orig_format_chars
;
2218 precision_wanted_type
.next
= NULL
;
2219 if (last_wanted_type
!= 0)
2220 last_wanted_type
->next
= &precision_wanted_type
;
2221 if (first_wanted_type
== 0)
2222 first_wanted_type
= &precision_wanted_type
;
2223 last_wanted_type
= &precision_wanted_type
;
2228 if (!(fki
->flags
& (int) FMT_FLAG_EMPTY_PREC_OK
)
2229 && !ISDIGIT (*format_chars
))
2230 format_warning_at_char (format_string_loc
, format_string_cst
,
2231 format_chars
- orig_format_chars
,
2233 "empty precision in %s format", fki
->name
);
2234 while (ISDIGIT (*format_chars
))
2241 /* Parse any assignment-allocation flags, which request an extra
2242 char ** for writing back a dynamically-allocated char *.
2243 This is for handling the optional 'm' character in scanf,
2244 and, before C99, 'a' (for compatibility with a non-standard
2245 GNU libc extension). */
2248 argument_parser::handle_alloc_chars ()
2250 if (fki
->alloc_char
&& fki
->alloc_char
== *format_chars
)
2252 flag_chars
.add_char (fki
->alloc_char
);
2256 /* Handle the scanf allocation kludge. */
2257 if (fki
->flags
& (int) FMT_FLAG_SCANF_A_KLUDGE
)
2259 if (*format_chars
== 'a' && !flag_isoc99
)
2261 if (format_chars
[1] == 's' || format_chars
[1] == 'S'
2262 || format_chars
[1] == '[')
2264 /* 'a' is used as a flag. */
2265 flag_chars
.add_char ('a');
2272 /* Look for length modifiers within the current format argument,
2273 returning a length_modifier instance describing it (or the
2274 default if one is not found).
2276 Issue warnings about non-standard modifiers. */
2279 argument_parser::read_any_length_modifier ()
2281 length_modifier result
;
2283 const format_length_info
*fli
= fki
->length_char_specs
;
2287 while (fli
->name
!= 0
2288 && strncmp (fli
->name
, format_chars
, strlen (fli
->name
)))
2292 format_chars
+= strlen (fli
->name
);
2293 if (fli
->double_name
!= 0 && fli
->name
[0] == *format_chars
)
2296 result
= length_modifier (fli
->double_name
, fli
->double_index
,
2297 fli
->double_std
, 0);
2301 result
= length_modifier (fli
->name
, fli
->index
, fli
->std
,
2302 fli
->scalar_identity_flag
);
2304 flag_chars
.add_char (fki
->length_code_char
);
2308 /* Warn if the length modifier is non-standard. */
2309 if (ADJ_STD (result
.std
) > C_STD_VER
)
2310 warning_at (format_string_loc
, OPT_Wformat_
,
2311 "%s does not support the %qs %s length modifier",
2312 C_STD_NAME (result
.std
), result
.chars
,
2319 /* Read any other modifier (strftime E/O). */
2322 argument_parser::read_any_other_modifier ()
2324 if (fki
->modifier_chars
== NULL
)
2327 while (*format_chars
!= 0
2328 && strchr (fki
->modifier_chars
, *format_chars
) != 0)
2330 if (flag_chars
.has_char_p (*format_chars
))
2332 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
2333 *format_chars
, NULL
);
2334 format_warning_at_char (format_string_loc
, format_string_cst
,
2335 format_chars
- orig_format_chars
,
2337 "repeated %s in format", _(s
->name
));
2340 flag_chars
.add_char (*format_chars
);
2345 /* Return the format_char_info corresponding to FORMAT_CHAR,
2346 potentially issuing a warning if the format char is
2347 not supported in the C standard version we are checking
2350 Issue a warning and return NULL if it is not found.
2352 Issue warnings about non-standard modifiers. */
2354 const format_char_info
*
2355 argument_parser::find_format_char_info (char format_char
)
2357 const format_char_info
*fci
= fki
->conversion_specs
;
2359 while (fci
->format_chars
!= 0
2360 && strchr (fci
->format_chars
, format_char
) == 0)
2362 if (fci
->format_chars
== 0)
2364 format_warning_at_char (format_string_loc
, format_string_cst
,
2365 format_chars
- orig_format_chars
,
2367 "unknown conversion type character"
2375 if (ADJ_STD (fci
->std
) > C_STD_VER
)
2376 format_warning_at_char (format_string_loc
, format_string_cst
,
2377 format_chars
- orig_format_chars
,
2379 "%s does not support the %<%%%c%> %s format",
2380 C_STD_NAME (fci
->std
), format_char
, fki
->name
);
2386 /* Validate the pairs of flags used.
2387 Issue warnings about incompatible combinations of flags. */
2390 argument_parser::validate_flag_pairs (const format_char_info
*fci
,
2393 const format_flag_pair
* const bad_flag_pairs
= fki
->bad_flag_pairs
;
2395 for (int i
= 0; bad_flag_pairs
[i
].flag_char1
!= 0; i
++)
2397 const format_flag_spec
*s
, *t
;
2398 if (!flag_chars
.has_char_p (bad_flag_pairs
[i
].flag_char1
))
2400 if (!flag_chars
.has_char_p (bad_flag_pairs
[i
].flag_char2
))
2402 if (bad_flag_pairs
[i
].predicate
!= 0
2403 && strchr (fci
->flags2
, bad_flag_pairs
[i
].predicate
) == 0)
2405 s
= get_flag_spec (flag_specs
, bad_flag_pairs
[i
].flag_char1
, NULL
);
2406 t
= get_flag_spec (flag_specs
, bad_flag_pairs
[i
].flag_char2
, NULL
);
2407 if (bad_flag_pairs
[i
].ignored
)
2409 if (bad_flag_pairs
[i
].predicate
!= 0)
2410 warning_at (format_string_loc
, OPT_Wformat_
,
2411 "%s ignored with %s and %<%%%c%> %s format",
2412 _(s
->name
), _(t
->name
), format_char
,
2415 warning_at (format_string_loc
, OPT_Wformat_
,
2416 "%s ignored with %s in %s format",
2417 _(s
->name
), _(t
->name
), fki
->name
);
2421 if (bad_flag_pairs
[i
].predicate
!= 0)
2422 warning_at (format_string_loc
, OPT_Wformat_
,
2423 "use of %s and %s together with %<%%%c%> %s format",
2424 _(s
->name
), _(t
->name
), format_char
,
2427 warning_at (format_string_loc
, OPT_Wformat_
,
2428 "use of %s and %s together in %s format",
2429 _(s
->name
), _(t
->name
), fki
->name
);
2434 /* Give Y2K warnings. */
2437 argument_parser::give_y2k_warnings (const format_char_info
*fci
,
2440 if (!warn_format_y2k
)
2444 if (strchr (fci
->flags2
, '4') != 0)
2445 if (flag_chars
.has_char_p ('E'))
2449 else if (strchr (fci
->flags2
, '3') != 0)
2451 else if (strchr (fci
->flags2
, '2') != 0)
2454 warning_at (format_string_loc
, OPT_Wformat_y2k
,
2455 "%<%%%c%> yields only last 2 digits of "
2456 "year in some locales", format_char
);
2457 else if (y2k_level
== 2)
2458 warning_at (format_string_loc
, OPT_Wformat_y2k
,
2459 "%<%%%c%> yields only last 2 digits of year",
2463 /* Parse any "scan sets" enclosed in square brackets, e.g.
2464 for scanf-style calls. */
2467 argument_parser::parse_any_scan_set (const format_char_info
*fci
)
2469 if (strchr (fci
->flags2
, '[') == NULL
)
2472 /* Skip over scan set, in case it happens to have '%' in it. */
2473 if (*format_chars
== '^')
2475 /* Find closing bracket; if one is hit immediately, then
2476 it's part of the scan set rather than a terminator. */
2477 if (*format_chars
== ']')
2479 while (*format_chars
&& *format_chars
!= ']')
2481 if (*format_chars
!= ']')
2482 /* The end of the format string was reached. */
2483 format_warning_at_char (format_string_loc
, format_string_cst
,
2484 format_chars
- orig_format_chars
,
2486 "no closing %<]%> for %<%%[%> format");
2489 /* Return true if this argument is to be continued to be parsed,
2490 false to skip to next argument. */
2493 argument_parser::handle_conversions (const format_char_info
*fci
,
2494 const length_modifier
&len_modifier
,
2496 const char *&wanted_type_name
,
2497 unsigned HOST_WIDE_INT
&arg_num
,
2501 enum format_std_version wanted_type_std
;
2503 if (!(fki
->flags
& (int) FMT_FLAG_ARG_CONVERT
))
2506 wanted_type
= (fci
->types
[len_modifier
.val
].type
2507 ? *fci
->types
[len_modifier
.val
].type
: 0);
2508 wanted_type_name
= fci
->types
[len_modifier
.val
].name
;
2509 wanted_type_std
= fci
->types
[len_modifier
.val
].std
;
2510 if (wanted_type
== 0)
2512 format_warning_at_char (format_string_loc
, format_string_cst
,
2513 format_chars
- orig_format_chars
,
2515 "use of %qs length modifier with %qc type"
2516 " character has either no effect"
2517 " or undefined behavior",
2518 len_modifier
.chars
, format_char
);
2519 /* Heuristic: skip one argument when an invalid length/type
2520 combination is encountered. */
2523 params
= TREE_CHAIN (params
);
2527 /* Warn if non-standard, provided it is more non-standard
2528 than the length and type characters that may already
2529 have been warned for. */
2530 && ADJ_STD (wanted_type_std
) > ADJ_STD (len_modifier
.std
)
2531 && ADJ_STD (wanted_type_std
) > ADJ_STD (fci
->std
))
2533 if (ADJ_STD (wanted_type_std
) > C_STD_VER
)
2534 format_warning_at_char (format_string_loc
, format_string_cst
,
2535 format_chars
- orig_format_chars
,
2537 "%s does not support the %<%%%s%c%> %s format",
2538 C_STD_NAME (wanted_type_std
),
2540 format_char
, fki
->name
);
2546 /* Check type of argument against desired type.
2548 Return true if format parsing is to continue, false otherwise. */
2552 check_argument_type (const format_char_info
*fci
,
2553 const length_modifier
&len_modifier
,
2555 const char *&wanted_type_name
,
2556 const bool suppressed
,
2557 unsigned HOST_WIDE_INT
&arg_num
,
2559 const int alloc_flag
,
2560 const char * const format_start
,
2561 const char * const type_start
,
2562 location_t fmt_param_loc
,
2563 char conversion_char
)
2565 if (info
->first_arg_num
== 0)
2568 if ((fci
->pointer_count
== 0 && wanted_type
== void_type_node
)
2571 if (main_arg_num
!= 0)
2574 warning_at (format_string_loc
, OPT_Wformat_
,
2575 "operand number specified with "
2576 "suppressed assignment");
2578 warning_at (format_string_loc
, OPT_Wformat_
,
2579 "operand number specified for format "
2580 "taking no argument");
2585 format_wanted_type
*wanted_type_ptr
;
2587 if (main_arg_num
!= 0)
2589 arg_num
= main_arg_num
;
2590 params
= main_arg_params
;
2595 if (has_operand_number
> 0)
2597 warning_at (format_string_loc
, OPT_Wformat_
,
2598 "missing $ operand number in format");
2602 has_operand_number
= 0;
2605 wanted_type_ptr
= &main_wanted_type
;
2613 cur_param
= TREE_VALUE (params
);
2614 params
= TREE_CHAIN (params
);
2617 wanted_type_ptr
->wanted_type
= wanted_type
;
2618 wanted_type_ptr
->wanted_type_name
= wanted_type_name
;
2619 wanted_type_ptr
->pointer_count
= fci
->pointer_count
+ alloc_flag
;
2620 wanted_type_ptr
->char_lenient_flag
= 0;
2621 if (strchr (fci
->flags2
, 'c') != 0)
2622 wanted_type_ptr
->char_lenient_flag
= 1;
2623 wanted_type_ptr
->scalar_identity_flag
= 0;
2624 if (len_modifier
.scalar_identity_flag
)
2625 wanted_type_ptr
->scalar_identity_flag
= 1;
2626 wanted_type_ptr
->writing_in_flag
= 0;
2627 wanted_type_ptr
->reading_from_flag
= 0;
2629 wanted_type_ptr
->writing_in_flag
= 1;
2632 if (strchr (fci
->flags2
, 'W') != 0)
2633 wanted_type_ptr
->writing_in_flag
= 1;
2634 if (strchr (fci
->flags2
, 'R') != 0)
2635 wanted_type_ptr
->reading_from_flag
= 1;
2637 wanted_type_ptr
->kind
= CF_KIND_FORMAT
;
2638 wanted_type_ptr
->param
= cur_param
;
2639 wanted_type_ptr
->arg_num
= arg_num
;
2640 wanted_type_ptr
->format_start
= format_start
;
2641 wanted_type_ptr
->format_length
= format_chars
- format_start
;
2642 wanted_type_ptr
->offset_loc
= format_chars
- orig_format_chars
;
2643 wanted_type_ptr
->next
= NULL
;
2644 if (last_wanted_type
!= 0)
2645 last_wanted_type
->next
= wanted_type_ptr
;
2646 if (first_wanted_type
== 0)
2647 first_wanted_type
= wanted_type_ptr
;
2648 last_wanted_type
= wanted_type_ptr
;
2653 wanted_type_ptr
= fwt_pool
.allocate ();
2655 wanted_type
= *fci
->types
[len_modifier
.val
].type
;
2656 wanted_type_name
= fci
->types
[len_modifier
.val
].name
;
2661 if (first_wanted_type
!= 0)
2663 ptrdiff_t offset_to_format_start
= (start_of_this_format
- 1) - orig_format_chars
;
2664 ptrdiff_t offset_to_format_end
= (format_chars
- 1) - orig_format_chars
;
2665 /* By default, use the end of the range for the caret location. */
2666 substring_loc
fmt_loc (fmt_param_loc
, TREE_TYPE (format_string_cst
),
2667 offset_to_format_end
,
2668 offset_to_format_start
, offset_to_format_end
);
2669 ptrdiff_t offset_to_type_start
= type_start
- orig_format_chars
;
2670 check_format_types (fmt_loc
, first_wanted_type
, fki
,
2671 offset_to_type_start
,
2678 /* Do the main part of checking a call to a format function. FORMAT_CHARS
2679 is the NUL-terminated format string (which at this point may contain
2680 internal NUL characters); FORMAT_LENGTH is its length (excluding the
2681 terminating NUL character). ARG_NUM is one less than the number of
2682 the first format argument to check; PARAMS points to that format
2683 argument in the list of arguments. */
2686 check_format_info_main (format_check_results
*res
,
2687 function_format_info
*info
, const char *format_chars
,
2688 location_t fmt_param_loc
, tree format_string_cst
,
2689 int format_length
, tree params
,
2690 unsigned HOST_WIDE_INT arg_num
,
2691 object_allocator
<format_wanted_type
> &fwt_pool
)
2693 const char * const orig_format_chars
= format_chars
;
2694 const tree first_fillin_param
= params
;
2696 const format_kind_info
* const fki
= &format_types
[info
->format_type
];
2697 const format_flag_spec
* const flag_specs
= fki
->flag_specs
;
2698 const location_t format_string_loc
= res
->format_string_loc
;
2700 /* -1 if no conversions taking an operand have been found; 0 if one has
2701 and it didn't use $; 1 if $ formats are in use. */
2702 int has_operand_number
= -1;
2704 init_dollar_format_checking (info
->first_arg_num
, first_fillin_param
);
2706 while (*format_chars
!= 0)
2708 if (*format_chars
++ != '%')
2710 if (*format_chars
== 0)
2712 format_warning_at_char (format_string_loc
, format_string_cst
,
2713 format_chars
- orig_format_chars
,
2715 "spurious trailing %<%%%> in format");
2718 if (*format_chars
== '%')
2724 flag_chars_t flag_chars
;
2725 argument_parser
arg_parser (info
, format_chars
, format_string_cst
,
2726 orig_format_chars
, format_string_loc
,
2727 flag_chars
, has_operand_number
,
2728 first_fillin_param
, fwt_pool
);
2730 if (!arg_parser
.read_any_dollar ())
2733 if (!arg_parser
.read_format_flags ())
2736 /* Read any format width, possibly * or *m$. */
2737 if (!arg_parser
.read_any_format_width (params
, arg_num
))
2740 /* Read any format left precision (must be a number, not *). */
2741 arg_parser
.read_any_format_left_precision ();
2743 /* Read any format precision, possibly * or *m$. */
2744 if (!arg_parser
.read_any_format_precision (params
, arg_num
))
2747 const char *format_start
= format_chars
;
2749 arg_parser
.handle_alloc_chars ();
2751 /* The rest of the conversion specification is the length modifier
2752 (if any), and the conversion specifier, so this is where the
2753 type information starts. If we need to issue a suggestion
2754 about a type mismatch, then we should preserve everything up
2756 const char *type_start
= format_chars
;
2758 /* Read any length modifier, if this kind of format has them. */
2759 const length_modifier len_modifier
2760 = arg_parser
.read_any_length_modifier ();
2762 /* Read any modifier (strftime E/O). */
2763 arg_parser
.read_any_other_modifier ();
2765 char format_char
= *format_chars
;
2766 if (format_char
== 0
2767 || (!(fki
->flags
& (int) FMT_FLAG_FANCY_PERCENT_OK
)
2768 && format_char
== '%'))
2770 format_warning_at_char (format_string_loc
, format_string_cst
,
2771 format_chars
- orig_format_chars
,
2773 "conversion lacks type at end of format");
2778 const format_char_info
* const fci
2779 = arg_parser
.find_format_char_info (format_char
);
2783 flag_chars
.validate (fki
, fci
, flag_specs
, format_chars
,
2785 format_string_loc
, orig_format_chars
, format_char
);
2787 const int alloc_flag
= flag_chars
.get_alloc_flag (fki
);
2788 const bool suppressed
= flag_chars
.assignment_suppression_p (fki
);
2790 /* Validate the pairs of flags used. */
2791 arg_parser
.validate_flag_pairs (fci
, format_char
);
2793 arg_parser
.give_y2k_warnings (fci
, format_char
);
2795 arg_parser
.parse_any_scan_set (fci
);
2797 tree wanted_type
= NULL
;
2798 const char *wanted_type_name
= NULL
;
2800 if (!arg_parser
.handle_conversions (fci
, len_modifier
,
2801 wanted_type
, wanted_type_name
,
2807 arg_parser
.main_wanted_type
.next
= NULL
;
2809 /* Finally. . .check type of argument against desired type! */
2810 if (!arg_parser
.check_argument_type (fci
, len_modifier
,
2811 wanted_type
, wanted_type_name
,
2815 format_start
, type_start
,
2821 if (format_chars
- orig_format_chars
!= format_length
)
2822 format_warning_at_char (format_string_loc
, format_string_cst
,
2823 format_chars
+ 1 - orig_format_chars
,
2824 OPT_Wformat_contains_nul
,
2825 "embedded %<\\0%> in format");
2826 if (info
->first_arg_num
!= 0 && params
!= 0
2827 && has_operand_number
<= 0)
2829 res
->number_other
--;
2830 res
->number_extra_args
++;
2832 if (has_operand_number
> 0)
2833 finish_dollar_format_checking (res
, fki
->flags
& (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK
);
2836 /* Check the argument types from a single format conversion (possibly
2837 including width and precision arguments).
2839 FMT_LOC is the location of the format conversion.
2841 TYPES is a singly-linked list expressing the parts of the format
2842 conversion that expect argument types, and the arguments they
2845 OFFSET_TO_TYPE_START is the offset within the execution-charset encoded
2846 format string to where type information begins for the conversion
2847 (the length modifier and conversion specifier).
2849 CONVERSION_CHAR is the user-provided conversion specifier.
2853 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
2855 then FMT_LOC covers this range:
2857 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
2860 and TYPES in this case is a three-entry singly-linked list consisting of:
2861 (1) the check for the field width here:
2862 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
2865 (2) the check for the field precision here:
2866 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
2869 (3) the check for the length modifier and conversion char here:
2870 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
2874 OFFSET_TO_TYPE_START is 13, the offset to the "lld" within the
2877 0000000000111111111122
2878 0123456789012345678901
2879 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
2881 | ` CONVERSION_CHAR: 'd'
2882 type starts here. */
2885 check_format_types (const substring_loc
&fmt_loc
,
2886 format_wanted_type
*types
, const format_kind_info
*fki
,
2887 int offset_to_type_start
,
2888 char conversion_char
)
2890 for (; types
!= 0; types
= types
->next
)
2900 wanted_type
= types
->wanted_type
;
2901 arg_num
= types
->arg_num
;
2903 /* The following should not occur here. */
2904 gcc_assert (wanted_type
);
2905 gcc_assert (wanted_type
!= void_type_node
|| types
->pointer_count
);
2907 if (types
->pointer_count
== 0)
2908 wanted_type
= lang_hooks
.types
.type_promotes_to (wanted_type
);
2910 wanted_type
= TYPE_MAIN_VARIANT (wanted_type
);
2912 cur_param
= types
->param
;
2915 format_type_warning (fmt_loc
, NULL
, types
, wanted_type
, NULL
, fki
,
2916 offset_to_type_start
, conversion_char
);
2920 cur_type
= TREE_TYPE (cur_param
);
2921 if (cur_type
== error_mark_node
)
2923 orig_cur_type
= cur_type
;
2926 source_range param_range
;
2927 source_range
*param_range_ptr
;
2928 if (CAN_HAVE_LOCATION_P (cur_param
))
2930 param_range
= EXPR_LOCATION_RANGE (cur_param
);
2931 param_range_ptr
= ¶m_range
;
2934 param_range_ptr
= NULL
;
2936 STRIP_NOPS (cur_param
);
2938 /* Check the types of any additional pointer arguments
2939 that precede the "real" argument. */
2940 for (i
= 0; i
< types
->pointer_count
; ++i
)
2942 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
2944 cur_type
= TREE_TYPE (cur_type
);
2945 if (cur_type
== error_mark_node
)
2948 /* Check for writing through a NULL pointer. */
2949 if (types
->writing_in_flag
2952 && integer_zerop (cur_param
))
2953 warning (OPT_Wformat_
, "writing through null pointer "
2954 "(argument %d)", arg_num
);
2956 /* Check for reading through a NULL pointer. */
2957 if (types
->reading_from_flag
2960 && integer_zerop (cur_param
))
2961 warning (OPT_Wformat_
, "reading through null pointer "
2962 "(argument %d)", arg_num
);
2964 if (cur_param
!= 0 && TREE_CODE (cur_param
) == ADDR_EXPR
)
2965 cur_param
= TREE_OPERAND (cur_param
, 0);
2969 /* See if this is an attempt to write into a const type with
2970 scanf or with printf "%n". Note: the writing in happens
2971 at the first indirection only, if for example
2972 void * const * is passed to scanf %p; passing
2973 const void ** is simply passing an incompatible type. */
2974 if (types
->writing_in_flag
2976 && (TYPE_READONLY (cur_type
)
2978 && (CONSTANT_CLASS_P (cur_param
)
2979 || (DECL_P (cur_param
)
2980 && TREE_READONLY (cur_param
))))))
2981 warning (OPT_Wformat_
, "writing into constant object "
2982 "(argument %d)", arg_num
);
2984 /* If there are extra type qualifiers beyond the first
2985 indirection, then this makes the types technically
2989 && (TYPE_READONLY (cur_type
)
2990 || TYPE_VOLATILE (cur_type
)
2991 || TYPE_ATOMIC (cur_type
)
2992 || TYPE_RESTRICT (cur_type
)))
2993 warning (OPT_Wformat_
, "extra type qualifiers in format "
2994 "argument (argument %d)",
3000 format_type_warning (fmt_loc
, param_range_ptr
,
3001 types
, wanted_type
, orig_cur_type
, fki
,
3002 offset_to_type_start
, conversion_char
);
3007 if (i
< types
->pointer_count
)
3010 cur_type
= TYPE_MAIN_VARIANT (cur_type
);
3012 /* Check whether the argument type is a character type. This leniency
3013 only applies to certain formats, flagged with 'c'. */
3014 if (types
->char_lenient_flag
)
3015 char_type_flag
= (cur_type
== char_type_node
3016 || cur_type
== signed_char_type_node
3017 || cur_type
== unsigned_char_type_node
);
3019 /* Check the type of the "real" argument, if there's a type we want. */
3020 if (lang_hooks
.types_compatible_p (wanted_type
, cur_type
))
3022 /* If we want 'void *', allow any pointer type.
3023 (Anything else would already have got a warning.)
3024 With -Wpedantic, only allow pointers to void and to character
3026 if (wanted_type
== void_type_node
3027 && (!pedantic
|| (i
== 1 && char_type_flag
)))
3029 /* Don't warn about differences merely in signedness, unless
3030 -Wpedantic. With -Wpedantic, warn if the type is a pointer
3031 target and not a character type, and for character types at
3032 a second level of indirection. */
3033 if (TREE_CODE (wanted_type
) == INTEGER_TYPE
3034 && TREE_CODE (cur_type
) == INTEGER_TYPE
3035 && ((!pedantic
&& !warn_format_signedness
)
3036 || (i
== 0 && !warn_format_signedness
)
3037 || (i
== 1 && char_type_flag
))
3038 && (TYPE_UNSIGNED (wanted_type
)
3039 ? wanted_type
== c_common_unsigned_type (cur_type
)
3040 : wanted_type
== c_common_signed_type (cur_type
)))
3042 /* Don't warn about differences merely in signedness if we know
3043 that the current type is integer-promoted and its original type
3044 was unsigned such as that it is in the range of WANTED_TYPE. */
3045 if (TREE_CODE (wanted_type
) == INTEGER_TYPE
3046 && TREE_CODE (cur_type
) == INTEGER_TYPE
3047 && warn_format_signedness
3048 && TYPE_UNSIGNED (wanted_type
)
3049 && cur_param
!= NULL_TREE
3050 && TREE_CODE (cur_param
) == NOP_EXPR
)
3052 tree t
= TREE_TYPE (TREE_OPERAND (cur_param
, 0));
3053 if (TYPE_UNSIGNED (t
)
3054 && cur_type
== lang_hooks
.types
.type_promotes_to (t
))
3057 /* Likewise, "signed char", "unsigned char" and "char" are
3058 equivalent but the above test won't consider them equivalent. */
3059 if (wanted_type
== char_type_node
3060 && (!pedantic
|| i
< 2)
3063 if (types
->scalar_identity_flag
3064 && (TREE_CODE (cur_type
) == TREE_CODE (wanted_type
)
3065 || (INTEGRAL_TYPE_P (cur_type
)
3066 && INTEGRAL_TYPE_P (wanted_type
)))
3067 && TYPE_PRECISION (cur_type
) == TYPE_PRECISION (wanted_type
))
3069 /* Now we have a type mismatch. */
3070 format_type_warning (fmt_loc
, param_range_ptr
, types
,
3071 wanted_type
, orig_cur_type
, fki
,
3072 offset_to_type_start
, conversion_char
);
3076 /* Given type TYPE, attempt to dereference the type N times
3077 (e.g. from ("int ***", 2) to "int *")
3079 Return the derefenced type, with any qualifiers
3080 such as "const" stripped from the result, or
3081 NULL if unsuccessful (e.g. TYPE is not a pointer type). */
3084 deref_n_times (tree type
, int n
)
3088 for (int i
= n
; i
> 0; i
--)
3090 if (TREE_CODE (type
) != POINTER_TYPE
)
3092 type
= TREE_TYPE (type
);
3094 /* Strip off any "const" etc. */
3095 return build_qualified_type (type
, 0);
3098 /* Lookup the format code for FORMAT_LEN within FLI,
3099 returning the string code for expressing it, or NULL
3100 if it is not found. */
3103 get_modifier_for_format_len (const format_length_info
*fli
,
3104 enum format_lengths format_len
)
3106 for (; fli
->name
; fli
++)
3108 if (fli
->index
== format_len
)
3110 if (fli
->double_index
== format_len
)
3111 return fli
->double_name
;
3118 namespace selftest
{
3121 test_get_modifier_for_format_len ()
3124 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_h
));
3126 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_hh
));
3128 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_L
));
3130 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_none
));
3133 } // namespace selftest
3135 #endif /* CHECKING_P */
3137 /* Determine if SPEC_TYPE and ARG_TYPE are sufficiently similar for a
3138 format_type_detail using SPEC_TYPE to be offered as a suggestion for
3139 Wformat type errors where the argument has type ARG_TYPE. */
3142 matching_type_p (tree spec_type
, tree arg_type
)
3144 gcc_assert (spec_type
);
3145 gcc_assert (arg_type
);
3147 spec_type
= TYPE_CANONICAL (spec_type
);
3148 arg_type
= TYPE_CANONICAL (arg_type
);
3150 if (TREE_CODE (spec_type
) == INTEGER_TYPE
3151 && TREE_CODE (arg_type
) == INTEGER_TYPE
3152 && (TYPE_UNSIGNED (spec_type
)
3153 ? spec_type
== c_common_unsigned_type (arg_type
)
3154 : spec_type
== c_common_signed_type (arg_type
)))
3157 return spec_type
== arg_type
;
3160 /* Subroutine of get_format_for_type.
3162 Generate a string containing the length modifier and conversion specifier
3163 that should be used to format arguments of type ARG_TYPE within FKI
3164 (effectively the inverse of the checking code).
3166 If CONVERSION_CHAR is not zero (the first pass), the resulting suggestion
3167 is required to use it, for correcting bogus length modifiers.
3168 If CONVERSION_CHAR is zero (the second pass), then allow any suggestion
3169 that matches ARG_TYPE.
3171 If successful, returns a non-NULL string which should be freed
3173 Otherwise, returns NULL. */
3176 get_format_for_type_1 (const format_kind_info
*fki
, tree arg_type
,
3177 char conversion_char
)
3179 gcc_assert (arg_type
);
3181 const format_char_info
*spec
;
3182 for (spec
= &fki
->conversion_specs
[0];
3186 if (conversion_char
)
3187 if (!strchr (spec
->format_chars
, conversion_char
))
3190 tree effective_arg_type
= deref_n_times (arg_type
,
3191 spec
->pointer_count
);
3192 if (!effective_arg_type
)
3194 for (int i
= 0; i
< FMT_LEN_MAX
; i
++)
3196 const format_type_detail
*ftd
= &spec
->types
[i
];
3199 if (matching_type_p (*ftd
->type
, effective_arg_type
))
3201 const char *len_modifier
3202 = get_modifier_for_format_len (fki
->length_char_specs
,
3203 (enum format_lengths
)i
);
3207 if (conversion_char
)
3208 /* We found a match, using the given conversion char - the
3209 length modifier was incorrect (or absent).
3210 Provide a suggestion using the conversion char with the
3211 correct length modifier for the type. */
3212 return xasprintf ("%s%c", len_modifier
, conversion_char
);
3214 /* 2nd pass: no match was possible using the user-provided
3215 conversion char, but we do have a match without using it.
3216 Provide a suggestion using the first conversion char
3217 listed for the given type. */
3218 return xasprintf ("%s%c", len_modifier
, spec
->format_chars
[0]);
3226 /* Generate a string containing the length modifier and conversion specifier
3227 that should be used to format arguments of type ARG_TYPE within FKI
3228 (effectively the inverse of the checking code).
3230 If successful, returns a non-NULL string which should be freed
3232 Otherwise, returns NULL. */
3235 get_format_for_type (const format_kind_info
*fki
, tree arg_type
,
3236 char conversion_char
)
3238 gcc_assert (arg_type
);
3239 gcc_assert (conversion_char
);
3241 /* First pass: look for a format_char_info containing CONVERSION_CHAR
3242 If we find one, then presumably the length modifier was incorrect
3244 char *result
= get_format_for_type_1 (fki
, arg_type
, conversion_char
);
3248 /* Second pass: we didn't find a match for CONVERSION_CHAR, so try
3249 matching just on the type. */
3250 return get_format_for_type_1 (fki
, arg_type
, '\0');
3253 /* Attempt to get a string for use as a replacement fix-it hint for the
3254 source range in FMT_LOC.
3256 Preserve all of the text within the range of FMT_LOC up to
3257 OFFSET_TO_TYPE_START, replacing the rest with an appropriate
3258 length modifier and conversion specifier for ARG_TYPE, attempting
3259 to keep the user-provided CONVERSION_CHAR if possible.
3261 For example, given a long vs long long mismatch for arg5 here:
3263 000000000111111111122222222223333333333|
3264 123456789012345678901234567890123456789` column numbers
3265 0000000000111111111122|
3266 0123456789012345678901` string offsets
3267 V~~~~~~~~ : range of FMT_LOC, from cols 23-31
3268 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3270 | ` CONVERSION_CHAR: 'd'
3273 where OFFSET_TO_TYPE_START is 13 (the offset to the "lld" within the
3274 STRING_CST), where the user provided:
3276 the result (assuming "long" argument 5) should be:
3279 If successful, returns a non-NULL string which should be freed
3281 Otherwise, returns NULL. */
3284 get_corrected_substring (const substring_loc
&fmt_loc
,
3285 format_wanted_type
*type
, tree arg_type
,
3286 const format_kind_info
*fki
,
3287 int offset_to_type_start
, char conversion_char
)
3289 /* Attempt to provide hints for argument types, but not for field widths
3293 if (type
->kind
!= CF_KIND_FORMAT
)
3296 /* Locate the current code within the source range, rejecting
3297 any awkward cases where the format string occupies more than
3299 Lookup the place where the type starts (including any length
3300 modifiers), getting it as the caret location. */
3301 substring_loc
type_loc (fmt_loc
);
3302 type_loc
.set_caret_index (offset_to_type_start
);
3304 location_t fmt_substring_loc
;
3305 const char *err
= type_loc
.get_location (&fmt_substring_loc
);
3309 source_range fmt_substring_range
3310 = get_range_from_loc (line_table
, fmt_substring_loc
);
3312 expanded_location caret
3313 = expand_location_to_spelling_point (fmt_substring_loc
);
3314 expanded_location start
3315 = expand_location_to_spelling_point (fmt_substring_range
.m_start
);
3316 expanded_location finish
3317 = expand_location_to_spelling_point (fmt_substring_range
.m_finish
);
3318 if (caret
.file
!= start
.file
)
3320 if (start
.file
!= finish
.file
)
3322 if (caret
.line
!= start
.line
)
3324 if (start
.line
!= finish
.line
)
3326 if (start
.column
> caret
.column
)
3328 if (start
.column
> finish
.column
)
3330 if (caret
.column
> finish
.column
)
3334 const char *line
= location_get_source_line (start
.file
, start
.line
,
3339 /* If we got this far, then we have the line containing the
3340 existing conversion specification.
3342 Generate a trimmed copy, containing the prefix part of the conversion
3343 specification, up to the (but not including) the length modifier.
3344 In the above example, this would be "%-+*.*". */
3345 const char *current_content
= line
+ start
.column
- 1;
3346 int length_up_to_type
= caret
.column
- start
.column
;
3347 char *prefix
= xstrndup (current_content
, length_up_to_type
);
3349 /* Now attempt to generate a suggestion for the rest of the specification
3350 (length modifier and conversion char), based on ARG_TYPE and
3352 In the above example, this would be "ld". */
3353 char *format_for_type
= get_format_for_type (fki
, arg_type
, conversion_char
);
3354 if (!format_for_type
)
3360 /* Success. Generate the resulting suggestion for the whole range of
3361 FMT_LOC by concatenating the two strings.
3362 In the above example, this would be "%-+*.*ld". */
3363 char *result
= concat (prefix
, format_for_type
, NULL
);
3364 free (format_for_type
);
3369 /* Give a warning about a format argument of different type from that expected.
3370 The range of the diagnostic is taken from WHOLE_FMT_LOC; the caret location
3371 is based on the location of the char at TYPE->offset_loc.
3372 If non-NULL, PARAM_RANGE is the source range of the
3373 relevant argument. WANTED_TYPE is the type the argument should have,
3374 possibly stripped of pointer dereferences. The description (such as "field
3375 precision"), the placement in the format string, a possibly more
3376 friendly name of WANTED_TYPE, and the number of pointer dereferences
3377 are taken from TYPE. ARG_TYPE is the type of the actual argument,
3378 or NULL if it is missing.
3380 OFFSET_TO_TYPE_START is the offset within the execution-charset encoded
3381 format string to where type information begins for the conversion
3382 (the length modifier and conversion specifier).
3383 CONVERSION_CHAR is the user-provided conversion specifier.
3385 For example, given a type mismatch for argument 5 here:
3387 00000000011111111112222222222333333333344444444445555555555|
3388 12345678901234567890123456789012345678901234567890123456789` column numbers
3389 0000000000111111111122|
3390 0123456789012345678901` offsets within STRING_CST
3391 V~~~~~~~~ : range of WHOLE_FMT_LOC, from cols 23-31
3392 sprintf (d, "before %-+*.*lld after", int_expr, int_expr, long_expr);
3394 | ` CONVERSION_CHAR: 'd' *PARAM_RANGE
3397 OFFSET_TO_TYPE_START is 13, the offset to the "lld" within the
3401 format_type_warning (const substring_loc
&whole_fmt_loc
,
3402 source_range
*param_range
,
3403 format_wanted_type
*type
,
3404 tree wanted_type
, tree arg_type
,
3405 const format_kind_info
*fki
,
3406 int offset_to_type_start
,
3407 char conversion_char
)
3409 enum format_specifier_kind kind
= type
->kind
;
3410 const char *wanted_type_name
= type
->wanted_type_name
;
3411 const char *format_start
= type
->format_start
;
3412 int format_length
= type
->format_length
;
3413 int pointer_count
= type
->pointer_count
;
3414 int arg_num
= type
->arg_num
;
3417 /* If ARG_TYPE is a typedef with a misleading name (for example,
3418 size_t but not the standard size_t expected by printf %zu), avoid
3419 printing the typedef name. */
3420 if (wanted_type_name
3422 && TYPE_NAME (arg_type
)
3423 && TREE_CODE (TYPE_NAME (arg_type
)) == TYPE_DECL
3424 && DECL_NAME (TYPE_NAME (arg_type
))
3425 && !strcmp (wanted_type_name
,
3426 lang_hooks
.decl_printable_name (TYPE_NAME (arg_type
), 2)))
3427 arg_type
= TYPE_MAIN_VARIANT (arg_type
);
3428 /* The format type and name exclude any '*' for pointers, so those
3429 must be formatted manually. For all the types we currently have,
3430 this is adequate, but formats taking pointers to functions or
3431 arrays would require the full type to be built up in order to
3432 print it with %T. */
3433 p
= (char *) alloca (pointer_count
+ 2);
3434 if (pointer_count
== 0)
3436 else if (c_dialect_cxx ())
3438 memset (p
, '*', pointer_count
);
3439 p
[pointer_count
] = 0;
3444 memset (p
+ 1, '*', pointer_count
);
3445 p
[pointer_count
+ 1] = 0;
3448 /* WHOLE_FMT_LOC has the caret at the end of the range.
3449 Set the caret to be at the offset from TYPE. Subtract one
3450 from the offset for the same reason as in format_warning_at_char. */
3451 substring_loc
fmt_loc (whole_fmt_loc
);
3452 fmt_loc
.set_caret_index (type
->offset_loc
- 1);
3454 /* Get a string for use as a replacement fix-it hint for the range in
3455 fmt_loc, or NULL. */
3456 char *corrected_substring
3457 = get_corrected_substring (fmt_loc
, type
, arg_type
, fki
,
3458 offset_to_type_start
, conversion_char
);
3460 if (wanted_type_name
)
3463 format_warning_at_substring
3464 (fmt_loc
, param_range
,
3465 corrected_substring
, OPT_Wformat_
,
3466 "%s %<%s%.*s%> expects argument of type %<%s%s%>, "
3467 "but argument %d has type %qT",
3468 gettext (kind_descriptions
[kind
]),
3469 (kind
== CF_KIND_FORMAT
? "%" : ""),
3470 format_length
, format_start
,
3471 wanted_type_name
, p
, arg_num
, arg_type
);
3473 format_warning_at_substring
3474 (fmt_loc
, param_range
,
3475 corrected_substring
, OPT_Wformat_
,
3476 "%s %<%s%.*s%> expects a matching %<%s%s%> argument",
3477 gettext (kind_descriptions
[kind
]),
3478 (kind
== CF_KIND_FORMAT
? "%" : ""),
3479 format_length
, format_start
, wanted_type_name
, p
);
3484 format_warning_at_substring
3485 (fmt_loc
, param_range
,
3486 corrected_substring
, OPT_Wformat_
,
3487 "%s %<%s%.*s%> expects argument of type %<%T%s%>, "
3488 "but argument %d has type %qT",
3489 gettext (kind_descriptions
[kind
]),
3490 (kind
== CF_KIND_FORMAT
? "%" : ""),
3491 format_length
, format_start
,
3492 wanted_type
, p
, arg_num
, arg_type
);
3494 format_warning_at_substring
3495 (fmt_loc
, param_range
,
3496 corrected_substring
, OPT_Wformat_
,
3497 "%s %<%s%.*s%> expects a matching %<%T%s%> argument",
3498 gettext (kind_descriptions
[kind
]),
3499 (kind
== CF_KIND_FORMAT
? "%" : ""),
3500 format_length
, format_start
, wanted_type
, p
);
3503 free (corrected_substring
);
3507 /* Given a format_char_info array FCI, and a character C, this function
3508 returns the index into the conversion_specs where that specifier's
3509 data is located. The character must exist. */
3511 find_char_info_specifier_index (const format_char_info
*fci
, int c
)
3515 for (i
= 0; fci
->format_chars
; i
++, fci
++)
3516 if (strchr (fci
->format_chars
, c
))
3519 /* We shouldn't be looking for a non-existent specifier. */
3523 /* Given a format_length_info array FLI, and a character C, this
3524 function returns the index into the conversion_specs where that
3525 modifier's data is located. The character must exist. */
3527 find_length_info_modifier_index (const format_length_info
*fli
, int c
)
3531 for (i
= 0; fli
->name
; i
++, fli
++)
3532 if (strchr (fli
->name
, c
))
3535 /* We shouldn't be looking for a non-existent modifier. */
3539 /* Determine the type of HOST_WIDE_INT in the code being compiled for
3540 use in GCC's __asm_fprintf__ custom format attribute. You must
3541 have set dynamic_format_types before calling this function. */
3543 init_dynamic_asm_fprintf_info (void)
3549 format_length_info
*new_asm_fprintf_length_specs
;
3552 /* Find the underlying type for HOST_WIDE_INT. For the %w
3553 length modifier to work, one must have issued: "typedef
3554 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
3555 prior to using that modifier. */
3556 hwi
= maybe_get_identifier ("__gcc_host_wide_int__");
3559 error ("%<__gcc_host_wide_int__%> is not defined as a type");
3562 hwi
= identifier_global_value (hwi
);
3563 if (!hwi
|| TREE_CODE (hwi
) != TYPE_DECL
)
3565 error ("%<__gcc_host_wide_int__%> is not defined as a type");
3568 hwi
= DECL_ORIGINAL_TYPE (hwi
);
3570 if (hwi
!= long_integer_type_node
&& hwi
!= long_long_integer_type_node
)
3572 error ("%<__gcc_host_wide_int__%> is not defined as %<long%>"
3573 " or %<long long%>");
3577 /* Create a new (writable) copy of asm_fprintf_length_specs. */
3578 new_asm_fprintf_length_specs
= (format_length_info
*)
3579 xmemdup (asm_fprintf_length_specs
,
3580 sizeof (asm_fprintf_length_specs
),
3581 sizeof (asm_fprintf_length_specs
));
3583 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
3584 i
= find_length_info_modifier_index (new_asm_fprintf_length_specs
, 'w');
3585 if (hwi
== long_integer_type_node
)
3586 new_asm_fprintf_length_specs
[i
].index
= FMT_LEN_l
;
3587 else if (hwi
== long_long_integer_type_node
)
3588 new_asm_fprintf_length_specs
[i
].index
= FMT_LEN_ll
;
3592 /* Assign the new data for use. */
3593 dynamic_format_types
[asm_fprintf_format_type
].length_char_specs
=
3594 new_asm_fprintf_length_specs
;
3598 /* Determine the type of a "locus" in the code being compiled for use
3599 in GCC's __gcc_gfc__ custom format attribute. You must have set
3600 dynamic_format_types before calling this function. */
3602 init_dynamic_gfc_info (void)
3608 static format_char_info
*gfc_fci
;
3610 /* For the GCC __gcc_gfc__ custom format specifier to work, one
3611 must have declared 'locus' prior to using this attribute. If
3612 we haven't seen this declarations then you shouldn't use the
3613 specifier requiring that type. */
3614 if ((locus
= maybe_get_identifier ("locus")))
3616 locus
= identifier_global_value (locus
);
3619 if (TREE_CODE (locus
) != TYPE_DECL
3620 || TREE_TYPE (locus
) == error_mark_node
)
3622 error ("%<locus%> is not defined as a type");
3626 locus
= TREE_TYPE (locus
);
3630 /* Assign the new data for use. */
3632 /* Handle the __gcc_gfc__ format specifics. */
3634 dynamic_format_types
[gcc_gfc_format_type
].conversion_specs
=
3635 gfc_fci
= (format_char_info
*)
3636 xmemdup (gcc_gfc_char_table
,
3637 sizeof (gcc_gfc_char_table
),
3638 sizeof (gcc_gfc_char_table
));
3641 const unsigned i
= find_char_info_specifier_index (gfc_fci
, 'L');
3642 gfc_fci
[i
].types
[0].type
= &locus
;
3643 gfc_fci
[i
].pointer_count
= 1;
3648 /* Determine the types of "tree" and "location_t" in the code being
3649 compiled for use in GCC's diagnostic custom format attributes. You
3650 must have set dynamic_format_types before calling this function. */
3652 init_dynamic_diag_info (void)
3654 static tree t
, loc
, hwi
;
3656 if (!loc
|| !t
|| !hwi
)
3658 static format_char_info
*diag_fci
, *tdiag_fci
, *cdiag_fci
, *cxxdiag_fci
;
3659 static format_length_info
*diag_ls
;
3662 /* For the GCC-diagnostics custom format specifiers to work, one
3663 must have declared 'tree' and/or 'location_t' prior to using
3664 those attributes. If we haven't seen these declarations then
3665 you shouldn't use the specifiers requiring these types.
3666 However we don't force a hard ICE because we may see only one
3667 or the other type. */
3668 if ((loc
= maybe_get_identifier ("location_t")))
3670 loc
= identifier_global_value (loc
);
3673 if (TREE_CODE (loc
) != TYPE_DECL
)
3675 error ("%<location_t%> is not defined as a type");
3679 loc
= TREE_TYPE (loc
);
3683 /* We need to grab the underlying 'union tree_node' so peek into
3684 an extra type level. */
3685 if ((t
= maybe_get_identifier ("tree")))
3687 t
= identifier_global_value (t
);
3690 if (TREE_CODE (t
) != TYPE_DECL
)
3692 error ("%<tree%> is not defined as a type");
3695 else if (TREE_CODE (TREE_TYPE (t
)) != POINTER_TYPE
)
3697 error ("%<tree%> is not defined as a pointer type");
3701 t
= TREE_TYPE (TREE_TYPE (t
));
3705 /* Find the underlying type for HOST_WIDE_INT. For the %w
3706 length modifier to work, one must have issued: "typedef
3707 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
3708 prior to using that modifier. */
3709 if ((hwi
= maybe_get_identifier ("__gcc_host_wide_int__")))
3711 hwi
= identifier_global_value (hwi
);
3714 if (TREE_CODE (hwi
) != TYPE_DECL
)
3716 error ("%<__gcc_host_wide_int__%> is not defined as a type");
3721 hwi
= DECL_ORIGINAL_TYPE (hwi
);
3723 if (hwi
!= long_integer_type_node
3724 && hwi
!= long_long_integer_type_node
)
3726 error ("%<__gcc_host_wide_int__%> is not defined"
3727 " as %<long%> or %<long long%>");
3734 /* Assign the new data for use. */
3736 /* All the GCC diag formats use the same length specs. */
3738 dynamic_format_types
[gcc_diag_format_type
].length_char_specs
=
3739 dynamic_format_types
[gcc_tdiag_format_type
].length_char_specs
=
3740 dynamic_format_types
[gcc_cdiag_format_type
].length_char_specs
=
3741 dynamic_format_types
[gcc_cxxdiag_format_type
].length_char_specs
=
3742 diag_ls
= (format_length_info
*)
3743 xmemdup (gcc_diag_length_specs
,
3744 sizeof (gcc_diag_length_specs
),
3745 sizeof (gcc_diag_length_specs
));
3748 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
3749 i
= find_length_info_modifier_index (diag_ls
, 'w');
3750 if (hwi
== long_integer_type_node
)
3751 diag_ls
[i
].index
= FMT_LEN_l
;
3752 else if (hwi
== long_long_integer_type_node
)
3753 diag_ls
[i
].index
= FMT_LEN_ll
;
3758 /* Handle the __gcc_diag__ format specifics. */
3760 dynamic_format_types
[gcc_diag_format_type
].conversion_specs
=
3761 diag_fci
= (format_char_info
*)
3762 xmemdup (gcc_diag_char_table
,
3763 sizeof (gcc_diag_char_table
),
3764 sizeof (gcc_diag_char_table
));
3767 i
= find_char_info_specifier_index (diag_fci
, 'K');
3768 diag_fci
[i
].types
[0].type
= &t
;
3769 diag_fci
[i
].pointer_count
= 1;
3772 /* Handle the __gcc_tdiag__ format specifics. */
3774 dynamic_format_types
[gcc_tdiag_format_type
].conversion_specs
=
3775 tdiag_fci
= (format_char_info
*)
3776 xmemdup (gcc_tdiag_char_table
,
3777 sizeof (gcc_tdiag_char_table
),
3778 sizeof (gcc_tdiag_char_table
));
3781 /* All specifiers taking a tree share the same struct. */
3782 i
= find_char_info_specifier_index (tdiag_fci
, 'D');
3783 tdiag_fci
[i
].types
[0].type
= &t
;
3784 tdiag_fci
[i
].pointer_count
= 1;
3785 i
= find_char_info_specifier_index (tdiag_fci
, 'K');
3786 tdiag_fci
[i
].types
[0].type
= &t
;
3787 tdiag_fci
[i
].pointer_count
= 1;
3790 /* Handle the __gcc_cdiag__ format specifics. */
3792 dynamic_format_types
[gcc_cdiag_format_type
].conversion_specs
=
3793 cdiag_fci
= (format_char_info
*)
3794 xmemdup (gcc_cdiag_char_table
,
3795 sizeof (gcc_cdiag_char_table
),
3796 sizeof (gcc_cdiag_char_table
));
3799 /* All specifiers taking a tree share the same struct. */
3800 i
= find_char_info_specifier_index (cdiag_fci
, 'D');
3801 cdiag_fci
[i
].types
[0].type
= &t
;
3802 cdiag_fci
[i
].pointer_count
= 1;
3803 i
= find_char_info_specifier_index (cdiag_fci
, 'K');
3804 cdiag_fci
[i
].types
[0].type
= &t
;
3805 cdiag_fci
[i
].pointer_count
= 1;
3808 /* Handle the __gcc_cxxdiag__ format specifics. */
3810 dynamic_format_types
[gcc_cxxdiag_format_type
].conversion_specs
=
3811 cxxdiag_fci
= (format_char_info
*)
3812 xmemdup (gcc_cxxdiag_char_table
,
3813 sizeof (gcc_cxxdiag_char_table
),
3814 sizeof (gcc_cxxdiag_char_table
));
3817 /* All specifiers taking a tree share the same struct. */
3818 i
= find_char_info_specifier_index (cxxdiag_fci
, 'D');
3819 cxxdiag_fci
[i
].types
[0].type
= &t
;
3820 cxxdiag_fci
[i
].pointer_count
= 1;
3821 i
= find_char_info_specifier_index (cxxdiag_fci
, 'K');
3822 cxxdiag_fci
[i
].types
[0].type
= &t
;
3823 cxxdiag_fci
[i
].pointer_count
= 1;
3828 #ifdef TARGET_FORMAT_TYPES
3829 extern const format_kind_info TARGET_FORMAT_TYPES
[];
3832 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
3833 extern const target_ovr_attr TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[];
3835 #ifdef TARGET_OVERRIDES_FORMAT_INIT
3836 extern void TARGET_OVERRIDES_FORMAT_INIT (void);
3839 /* Attributes such as "printf" are equivalent to those such as
3840 "gnu_printf" unless this is overridden by a target. */
3841 static const target_ovr_attr gnu_target_overrides_format_attributes
[] =
3843 { "gnu_printf", "printf" },
3844 { "gnu_scanf", "scanf" },
3845 { "gnu_strftime", "strftime" },
3846 { "gnu_strfmon", "strfmon" },
3850 /* Translate to unified attribute name. This is used in decode_format_type and
3851 decode_format_attr. In attr_name the user specified argument is passed. It
3852 returns the unified format name from TARGET_OVERRIDES_FORMAT_ATTRIBUTES
3853 or the attr_name passed to this function, if there is no matching entry. */
3855 convert_format_name_to_system_name (const char *attr_name
)
3859 if (attr_name
== NULL
|| *attr_name
== 0
3860 || strncmp (attr_name
, "gcc_", 4) == 0)
3862 #ifdef TARGET_OVERRIDES_FORMAT_INIT
3863 TARGET_OVERRIDES_FORMAT_INIT ();
3866 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
3867 /* Check if format attribute is overridden by target. */
3868 if (TARGET_OVERRIDES_FORMAT_ATTRIBUTES
!= NULL
3869 && TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT
> 0)
3871 for (i
= 0; i
< TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT
; ++i
)
3873 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[i
].named_attr_src
,
3876 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[i
].named_attr_dst
,
3878 return TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[i
].named_attr_src
;
3882 /* Otherwise default to gnu format. */
3884 gnu_target_overrides_format_attributes
[i
].named_attr_src
!= NULL
;
3887 if (cmp_attribs (gnu_target_overrides_format_attributes
[i
].named_attr_src
,
3890 if (cmp_attribs (gnu_target_overrides_format_attributes
[i
].named_attr_dst
,
3892 return gnu_target_overrides_format_attributes
[i
].named_attr_src
;
3898 /* Return true if TATTR_NAME and ATTR_NAME are the same format attribute,
3899 counting "name" and "__name__" as the same, false otherwise. */
3901 cmp_attribs (const char *tattr_name
, const char *attr_name
)
3903 int alen
= strlen (attr_name
);
3904 int slen
= (tattr_name
? strlen (tattr_name
) : 0);
3905 if (alen
> 4 && attr_name
[0] == '_' && attr_name
[1] == '_'
3906 && attr_name
[alen
- 1] == '_' && attr_name
[alen
- 2] == '_')
3911 if (alen
!= slen
|| strncmp (tattr_name
, attr_name
, alen
) != 0)
3916 /* Handle a "format" attribute; arguments as in
3917 struct attribute_spec.handler. */
3919 handle_format_attribute (tree
*node
, tree
ARG_UNUSED (name
), tree args
,
3920 int flags
, bool *no_add_attrs
)
3923 function_format_info info
;
3925 #ifdef TARGET_FORMAT_TYPES
3926 /* If the target provides additional format types, we need to
3927 add them to FORMAT_TYPES at first use. */
3928 if (TARGET_FORMAT_TYPES
!= NULL
&& !dynamic_format_types
)
3930 dynamic_format_types
= XNEWVEC (format_kind_info
,
3931 n_format_types
+ TARGET_N_FORMAT_TYPES
);
3932 memcpy (dynamic_format_types
, format_types_orig
,
3933 sizeof (format_types_orig
));
3934 memcpy (&dynamic_format_types
[n_format_types
], TARGET_FORMAT_TYPES
,
3935 TARGET_N_FORMAT_TYPES
* sizeof (dynamic_format_types
[0]));
3937 format_types
= dynamic_format_types
;
3938 /* Provide a reference for the first potential external type. */
3939 first_target_format_type
= n_format_types
;
3940 n_format_types
+= TARGET_N_FORMAT_TYPES
;
3944 if (!decode_format_attr (args
, &info
, 0))
3946 *no_add_attrs
= true;
3950 if (prototype_p (type
))
3952 if (!check_format_string (type
, info
.format_num
, flags
,
3953 no_add_attrs
, info
.format_type
))
3956 if (info
.first_arg_num
!= 0)
3958 unsigned HOST_WIDE_INT arg_num
= 1;
3959 function_args_iterator iter
;
3962 /* Verify that first_arg_num points to the last arg,
3964 FOREACH_FUNCTION_ARGS (type
, arg_type
, iter
)
3967 if (arg_num
!= info
.first_arg_num
)
3969 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
3970 error ("args to be formatted is not %<...%>");
3971 *no_add_attrs
= true;
3977 /* Check if this is a strftime variant. Just for this variant
3978 FMT_FLAG_ARG_CONVERT is not set. */
3979 if ((format_types
[info
.format_type
].flags
& (int) FMT_FLAG_ARG_CONVERT
) == 0
3980 && info
.first_arg_num
!= 0)
3982 error ("strftime formats cannot format arguments");
3983 *no_add_attrs
= true;
3987 /* If this is a custom GCC-internal format type, we have to
3988 initialize certain bits at runtime. */
3989 if (info
.format_type
== asm_fprintf_format_type
3990 || info
.format_type
== gcc_gfc_format_type
3991 || info
.format_type
== gcc_diag_format_type
3992 || info
.format_type
== gcc_tdiag_format_type
3993 || info
.format_type
== gcc_cdiag_format_type
3994 || info
.format_type
== gcc_cxxdiag_format_type
)
3996 /* Our first time through, we have to make sure that our
3997 format_type data is allocated dynamically and is modifiable. */
3998 if (!dynamic_format_types
)
3999 format_types
= dynamic_format_types
= (format_kind_info
*)
4000 xmemdup (format_types_orig
, sizeof (format_types_orig
),
4001 sizeof (format_types_orig
));
4003 /* If this is format __asm_fprintf__, we have to initialize
4004 GCC's notion of HOST_WIDE_INT for checking %wd. */
4005 if (info
.format_type
== asm_fprintf_format_type
)
4006 init_dynamic_asm_fprintf_info ();
4007 /* If this is format __gcc_gfc__, we have to initialize GCC's
4008 notion of 'locus' at runtime for %L. */
4009 else if (info
.format_type
== gcc_gfc_format_type
)
4010 init_dynamic_gfc_info ();
4011 /* If this is one of the diagnostic attributes, then we have to
4012 initialize 'location_t' and 'tree' at runtime. */
4013 else if (info
.format_type
== gcc_diag_format_type
4014 || info
.format_type
== gcc_tdiag_format_type
4015 || info
.format_type
== gcc_cdiag_format_type
4016 || info
.format_type
== gcc_cxxdiag_format_type
)
4017 init_dynamic_diag_info ();
4027 namespace selftest
{
4029 /* Selftests of location handling. */
4031 /* Get the format_kind_info with the given name. */
4033 static const format_kind_info
*
4034 get_info (const char *name
)
4036 int idx
= decode_format_type (name
);
4037 const format_kind_info
*fki
= &format_types
[idx
];
4038 ASSERT_STREQ (fki
->name
, name
);
4042 /* Verify that get_format_for_type (FKI, TYPE, CONVERSION_CHAR)
4043 is EXPECTED_FORMAT. */
4046 assert_format_for_type_streq (const location
&loc
, const format_kind_info
*fki
,
4047 const char *expected_format
, tree type
,
4048 char conversion_char
)
4051 gcc_assert (expected_format
);
4054 char *actual_format
= get_format_for_type (fki
, type
, conversion_char
);
4055 ASSERT_STREQ_AT (loc
, expected_format
, actual_format
);
4056 free (actual_format
);
4059 /* Selftests for get_format_for_type. */
4061 #define ASSERT_FORMAT_FOR_TYPE_STREQ(EXPECTED_FORMAT, TYPE, CONVERSION_CHAR) \
4062 assert_format_for_type_streq (SELFTEST_LOCATION, (fki), (EXPECTED_FORMAT), \
4063 (TYPE), (CONVERSION_CHAR))
4065 /* Selftest for get_format_for_type for "printf"-style functions. */
4068 test_get_format_for_type_printf ()
4070 const format_kind_info
*fki
= get_info ("gnu_printf");
4071 ASSERT_NE (fki
, NULL
);
4073 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'i');
4074 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'i');
4075 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'o');
4076 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'o');
4077 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'x');
4078 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'x');
4079 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'X');
4080 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'X');
4081 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", integer_type_node
, 'd');
4082 ASSERT_FORMAT_FOR_TYPE_STREQ ("i", integer_type_node
, 'i');
4083 ASSERT_FORMAT_FOR_TYPE_STREQ ("o", integer_type_node
, 'o');
4084 ASSERT_FORMAT_FOR_TYPE_STREQ ("x", integer_type_node
, 'x');
4085 ASSERT_FORMAT_FOR_TYPE_STREQ ("X", integer_type_node
, 'X');
4086 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", unsigned_type_node
, 'd');
4087 ASSERT_FORMAT_FOR_TYPE_STREQ ("i", unsigned_type_node
, 'i');
4088 ASSERT_FORMAT_FOR_TYPE_STREQ ("o", unsigned_type_node
, 'o');
4089 ASSERT_FORMAT_FOR_TYPE_STREQ ("x", unsigned_type_node
, 'x');
4090 ASSERT_FORMAT_FOR_TYPE_STREQ ("X", unsigned_type_node
, 'X');
4091 ASSERT_FORMAT_FOR_TYPE_STREQ ("ld", long_integer_type_node
, 'd');
4092 ASSERT_FORMAT_FOR_TYPE_STREQ ("li", long_integer_type_node
, 'i');
4093 ASSERT_FORMAT_FOR_TYPE_STREQ ("lx", long_integer_type_node
, 'x');
4094 ASSERT_FORMAT_FOR_TYPE_STREQ ("lo", long_unsigned_type_node
, 'o');
4095 ASSERT_FORMAT_FOR_TYPE_STREQ ("lx", long_unsigned_type_node
, 'x');
4096 ASSERT_FORMAT_FOR_TYPE_STREQ ("lld", long_long_integer_type_node
, 'd');
4097 ASSERT_FORMAT_FOR_TYPE_STREQ ("lli", long_long_integer_type_node
, 'i');
4098 ASSERT_FORMAT_FOR_TYPE_STREQ ("llo", long_long_unsigned_type_node
, 'o');
4099 ASSERT_FORMAT_FOR_TYPE_STREQ ("llx", long_long_unsigned_type_node
, 'x');
4100 ASSERT_FORMAT_FOR_TYPE_STREQ ("s", build_pointer_type (char_type_node
), 'i');
4103 /* Selftest for get_format_for_type for "scanf"-style functions. */
4106 test_get_format_for_type_scanf ()
4108 const format_kind_info
*fki
= get_info ("gnu_scanf");
4109 ASSERT_NE (fki
, NULL
);
4110 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", build_pointer_type (integer_type_node
), 'd');
4111 ASSERT_FORMAT_FOR_TYPE_STREQ ("u", build_pointer_type (unsigned_type_node
), 'u');
4112 ASSERT_FORMAT_FOR_TYPE_STREQ ("ld",
4113 build_pointer_type (long_integer_type_node
), 'd');
4114 ASSERT_FORMAT_FOR_TYPE_STREQ ("lu",
4115 build_pointer_type (long_unsigned_type_node
), 'u');
4116 ASSERT_FORMAT_FOR_TYPE_STREQ
4117 ("lld", build_pointer_type (long_long_integer_type_node
), 'd');
4118 ASSERT_FORMAT_FOR_TYPE_STREQ
4119 ("llu", build_pointer_type (long_long_unsigned_type_node
), 'u');
4120 ASSERT_FORMAT_FOR_TYPE_STREQ ("e", build_pointer_type (float_type_node
), 'e');
4121 ASSERT_FORMAT_FOR_TYPE_STREQ ("le", build_pointer_type (double_type_node
), 'e');
4124 #undef ASSERT_FORMAT_FOR_TYPE_STREQ
4126 /* Run all of the selftests within this file. */
4131 test_get_modifier_for_format_len ();
4132 test_get_format_for_type_printf ();
4133 test_get_format_for_type_scanf ();
4136 } // namespace selftest
4138 #endif /* CHECKING_P */