1 /* Check calls to formatted I/O functions (-Wformat).
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
24 #include "coretypes.h"
31 #include "diagnostic.h"
32 #include "langhooks.h"
35 /* Set format warning options according to a -Wformat=n option. */
38 set_Wformat (int setting)
40 warn_format = setting;
41 warn_format_extra_args = setting;
42 warn_format_zero_length = setting;
45 warn_format_nonliteral = setting;
46 warn_format_security = setting;
47 warn_format_y2k = setting;
49 /* Make sure not to disable -Wnonnull if -Wformat=0 is specified. */
51 warn_nonnull = setting;
55 /* Handle attributes associated with format checking. */
57 /* This must be in the same order as format_types, except for
58 format_type_error. Target-specific format types do not have
59 matching enum values. */
60 enum format_type { printf_format_type, asm_fprintf_format_type,
61 gcc_diag_format_type, gcc_tdiag_format_type,
62 gcc_cdiag_format_type,
63 gcc_cxxdiag_format_type,
64 scanf_format_type, strftime_format_type,
65 strfmon_format_type, format_type_error = -1};
67 typedef struct function_format_info
69 int format_type; /* type of format (printf, scanf, etc.) */
70 unsigned HOST_WIDE_INT format_num; /* number of format argument */
71 unsigned HOST_WIDE_INT first_arg_num; /* number of first arg (zero for varargs) */
72 } function_format_info;
74 static bool decode_format_attr (tree, function_format_info *, int);
75 static int decode_format_type (const char *);
77 static bool check_format_string (tree argument,
78 unsigned HOST_WIDE_INT format_num,
79 int flags, bool *no_add_attrs);
80 static bool get_constant (tree expr, unsigned HOST_WIDE_INT *value,
84 /* Handle a "format_arg" attribute; arguments as in
85 struct attribute_spec.handler. */
87 handle_format_arg_attribute (tree *node, tree ARG_UNUSED (name),
88 tree args, int flags, bool *no_add_attrs)
91 tree format_num_expr = TREE_VALUE (args);
92 unsigned HOST_WIDE_INT format_num = 0;
95 if (!get_constant (format_num_expr, &format_num, 0))
97 error ("format string has invalid operand number");
102 argument = TYPE_ARG_TYPES (type);
105 if (!check_format_string (argument, format_num, flags, no_add_attrs))
109 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
110 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
113 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
114 error ("function does not return string type");
115 *no_add_attrs = true;
122 /* Verify that the format_num argument is actually a string, in case
123 the format attribute is in error. */
125 check_format_string (tree argument, unsigned HOST_WIDE_INT format_num,
126 int flags, bool *no_add_attrs)
128 unsigned HOST_WIDE_INT i;
130 for (i = 1; i != format_num; i++)
134 argument = TREE_CHAIN (argument);
138 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
139 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
142 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
143 error ("format string argument not a string type");
144 *no_add_attrs = true;
151 /* Verify EXPR is a constant, and store its value.
152 If validated_p is true there should be no errors.
153 Returns true on success, false otherwise. */
155 get_constant (tree expr, unsigned HOST_WIDE_INT *value, int validated_p)
157 if (TREE_CODE (expr) != INTEGER_CST || TREE_INT_CST_HIGH (expr) != 0)
159 gcc_assert (!validated_p);
163 *value = TREE_INT_CST_LOW (expr);
168 /* Decode the arguments to a "format" attribute into a
169 function_format_info structure. It is already known that the list
170 is of the right length. If VALIDATED_P is true, then these
171 attributes have already been validated and must not be erroneous;
172 if false, it will give an error message. Returns true if the
173 attributes are successfully decoded, false otherwise. */
176 decode_format_attr (tree args, function_format_info *info, int validated_p)
178 tree format_type_id = TREE_VALUE (args);
179 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
180 tree first_arg_num_expr
181 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
183 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
185 gcc_assert (!validated_p);
186 error ("unrecognized format specifier");
191 const char *p = IDENTIFIER_POINTER (format_type_id);
193 info->format_type = decode_format_type (p);
195 if (info->format_type == format_type_error)
197 gcc_assert (!validated_p);
198 warning (OPT_Wformat, "%qE is an unrecognized format function type",
204 if (!get_constant (format_num_expr, &info->format_num, validated_p))
206 error ("format string has invalid operand number");
210 if (!get_constant (first_arg_num_expr, &info->first_arg_num, validated_p))
212 error ("%<...%> has invalid operand number");
216 if (info->first_arg_num != 0 && info->first_arg_num <= info->format_num)
218 gcc_assert (!validated_p);
219 error ("format string argument follows the args to be formatted");
226 /* Check a call to a format function against a parameter list. */
228 /* The C standard version C++ is treated as equivalent to
229 or inheriting from, for the purpose of format features supported. */
230 #define CPLUSPLUS_STD_VER STD_C94
231 /* The C standard version we are checking formats against when pedantic. */
232 #define C_STD_VER ((int) (c_dialect_cxx () \
233 ? CPLUSPLUS_STD_VER \
236 : (flag_isoc94 ? STD_C94 : STD_C89))))
237 /* The name to give to the standard version we are warning about when
238 pedantic. FEATURE_VER is the version in which the feature warned out
239 appeared, which is higher than C_STD_VER. */
240 #define C_STD_NAME(FEATURE_VER) (c_dialect_cxx () \
242 : ((FEATURE_VER) == STD_EXT \
245 /* Adjust a C standard version, which may be STD_C9L, to account for
246 -Wno-long-long. Returns other standard versions unchanged. */
247 #define ADJ_STD(VER) ((int) ((VER) == STD_C9L \
248 ? (warn_long_long ? STD_C99 : STD_C89) \
251 /* Structure describing details of a type expected in format checking,
252 and the type to check against it. */
253 typedef struct format_wanted_type
255 /* The type wanted. */
257 /* The name of this type to use in diagnostics. */
258 const char *wanted_type_name;
259 /* The level of indirection through pointers at which this type occurs. */
261 /* Whether, when pointer_count is 1, to allow any character type when
262 pedantic, rather than just the character or void type specified. */
263 int char_lenient_flag;
264 /* Whether the argument, dereferenced once, is written into and so the
265 argument must not be a pointer to a const-qualified type. */
267 /* Whether the argument, dereferenced once, is read from and so
268 must not be a NULL pointer. */
269 int reading_from_flag;
270 /* If warnings should be of the form "field precision should have
271 type 'int'", the name to use (in this case "field precision"),
272 otherwise NULL, for "format expects type 'long'" type
275 /* The actual parameter to check against the wanted type. */
277 /* The argument number of that parameter. */
279 /* The next type to check for this format conversion, or NULL if none. */
280 struct format_wanted_type *next;
281 } format_wanted_type;
284 static const format_length_info printf_length_specs[] =
286 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
287 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L },
288 { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
289 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
290 { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
291 { "Z", FMT_LEN_z, STD_EXT, NULL, 0, 0 },
292 { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
293 { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
294 { NULL, 0, 0, NULL, 0, 0 }
297 /* Length specifiers valid for asm_fprintf. */
298 static const format_length_info asm_fprintf_length_specs[] =
300 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89 },
301 { "w", FMT_LEN_none, STD_C89, NULL, 0, 0 },
302 { NULL, 0, 0, NULL, 0, 0 }
305 /* Length specifiers valid for GCC diagnostics. */
306 static const format_length_info gcc_diag_length_specs[] =
308 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89 },
309 { "w", FMT_LEN_none, STD_C89, NULL, 0, 0 },
310 { NULL, 0, 0, NULL, 0, 0 }
313 /* The custom diagnostics all accept the same length specifiers. */
314 #define gcc_tdiag_length_specs gcc_diag_length_specs
315 #define gcc_cdiag_length_specs gcc_diag_length_specs
316 #define gcc_cxxdiag_length_specs gcc_diag_length_specs
318 /* This differs from printf_length_specs only in that "Z" is not accepted. */
319 static const format_length_info scanf_length_specs[] =
321 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
322 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L },
323 { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
324 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
325 { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
326 { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
327 { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
328 { NULL, 0, 0, NULL, 0, 0 }
332 /* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings
333 make no sense for a format type not part of any C standard version. */
334 static const format_length_info strfmon_length_specs[] =
336 /* A GNU extension. */
337 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
338 { NULL, 0, 0, NULL, 0, 0 }
341 static const format_flag_spec printf_flag_specs[] =
343 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
344 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
345 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
346 { '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
347 { '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
348 { '\'', 0, 0, N_("''' flag"), N_("the ''' printf flag"), STD_EXT },
349 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' printf flag"), STD_EXT },
350 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
351 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
352 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
353 { 0, 0, 0, NULL, NULL, 0 }
357 static const format_flag_pair printf_flag_pairs[] =
361 { '0', 'p', 1, 'i' },
365 static const format_flag_spec asm_fprintf_flag_specs[] =
367 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
368 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
369 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
370 { '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
371 { '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
372 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
373 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
374 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
375 { 0, 0, 0, NULL, NULL, 0 }
378 static const format_flag_pair asm_fprintf_flag_pairs[] =
382 { '0', 'p', 1, 'i' },
386 static const format_flag_pair gcc_diag_flag_pairs[] =
391 #define gcc_tdiag_flag_pairs gcc_diag_flag_pairs
392 #define gcc_cdiag_flag_pairs gcc_diag_flag_pairs
393 #define gcc_cxxdiag_flag_pairs gcc_diag_flag_pairs
395 static const format_flag_spec gcc_diag_flag_specs[] =
397 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
398 { 'q', 0, 0, N_("'q' flag"), N_("the 'q' diagnostic flag"), STD_C89 },
399 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
400 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
401 { 0, 0, 0, NULL, NULL, 0 }
404 #define gcc_tdiag_flag_specs gcc_diag_flag_specs
405 #define gcc_cdiag_flag_specs gcc_diag_flag_specs
407 static const format_flag_spec gcc_cxxdiag_flag_specs[] =
409 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
410 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
411 { 'q', 0, 0, N_("'q' flag"), N_("the 'q' diagnostic flag"), STD_C89 },
412 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
413 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
414 { 0, 0, 0, NULL, NULL, 0 }
417 static const format_flag_spec scanf_flag_specs[] =
419 { '*', 0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89 },
420 { 'a', 0, 0, N_("'a' flag"), N_("the 'a' scanf flag"), STD_EXT },
421 { 'w', 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89 },
422 { 'L', 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89 },
423 { '\'', 0, 0, N_("''' flag"), N_("the ''' scanf flag"), STD_EXT },
424 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' scanf flag"), STD_EXT },
425 { 0, 0, 0, NULL, NULL, 0 }
429 static const format_flag_pair scanf_flag_pairs[] =
436 static const format_flag_spec strftime_flag_specs[] =
438 { '_', 0, 0, N_("'_' flag"), N_("the '_' strftime flag"), STD_EXT },
439 { '-', 0, 0, N_("'-' flag"), N_("the '-' strftime flag"), STD_EXT },
440 { '0', 0, 0, N_("'0' flag"), N_("the '0' strftime flag"), STD_EXT },
441 { '^', 0, 0, N_("'^' flag"), N_("the '^' strftime flag"), STD_EXT },
442 { '#', 0, 0, N_("'#' flag"), N_("the '#' strftime flag"), STD_EXT },
443 { 'w', 0, 0, N_("field width"), N_("field width in strftime format"), STD_EXT },
444 { 'E', 0, 0, N_("'E' modifier"), N_("the 'E' strftime modifier"), STD_C99 },
445 { 'O', 0, 0, N_("'O' modifier"), N_("the 'O' strftime modifier"), STD_C99 },
446 { 'O', 'o', 0, NULL, N_("the 'O' modifier"), STD_EXT },
447 { 0, 0, 0, NULL, NULL, 0 }
451 static const format_flag_pair strftime_flag_pairs[] =
462 static const format_flag_spec strfmon_flag_specs[] =
464 { '=', 0, 1, N_("fill character"), N_("fill character in strfmon format"), STD_C89 },
465 { '^', 0, 0, N_("'^' flag"), N_("the '^' strfmon flag"), STD_C89 },
466 { '+', 0, 0, N_("'+' flag"), N_("the '+' strfmon flag"), STD_C89 },
467 { '(', 0, 0, N_("'(' flag"), N_("the '(' strfmon flag"), STD_C89 },
468 { '!', 0, 0, N_("'!' flag"), N_("the '!' strfmon flag"), STD_C89 },
469 { '-', 0, 0, N_("'-' flag"), N_("the '-' strfmon flag"), STD_C89 },
470 { 'w', 0, 0, N_("field width"), N_("field width in strfmon format"), STD_C89 },
471 { '#', 0, 0, N_("left precision"), N_("left precision in strfmon format"), STD_C89 },
472 { 'p', 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89 },
473 { 'L', 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89 },
474 { 0, 0, 0, NULL, NULL, 0 }
477 static const format_flag_pair strfmon_flag_pairs[] =
484 static const format_char_info print_char_table[] =
486 /* C89 conversion specifiers. */
487 { "di", 0, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, TEX_LL, T99_SST, T99_PD, T99_IM }, "-wp0 +'I", "i", NULL },
488 { "oxX", 0, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "-wp0#", "i", NULL },
489 { "u", 0, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "-wp0'I", "i", NULL },
490 { "fgG", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#'I", "", NULL },
491 { "eE", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#I", "", NULL },
492 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, T94_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
493 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
494 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "c", NULL },
495 { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, BADLEN, T99_SST, T99_PD, T99_IM }, "", "W", NULL },
496 /* C99 conversion specifiers. */
497 { "F", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#'I", "", NULL },
498 { "aA", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "", NULL },
499 /* X/Open conversion specifiers. */
500 { "C", 0, STD_EXT, { TEX_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
501 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "R", NULL },
502 /* GNU conversion specifiers. */
503 { "m", 0, STD_EXT, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "", NULL },
504 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
507 static const format_char_info asm_fprintf_char_table[] =
509 /* C89 conversion specifiers. */
510 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +", "i", NULL },
511 { "oxX", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0#", "i", NULL },
512 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0", "i", NULL },
513 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
514 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
516 /* asm_fprintf conversion specifiers. */
517 { "O", 0, STD_C89, NOARGUMENTS, "", "", NULL },
518 { "R", 0, STD_C89, NOARGUMENTS, "", "", NULL },
519 { "I", 0, STD_C89, NOARGUMENTS, "", "", NULL },
520 { "L", 0, STD_C89, NOARGUMENTS, "", "", NULL },
521 { "U", 0, STD_C89, NOARGUMENTS, "", "", NULL },
522 { "r", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
523 { "@", 0, STD_C89, NOARGUMENTS, "", "", NULL },
524 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
527 static const format_char_info gcc_diag_char_table[] =
529 /* C89 conversion specifiers. */
530 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
531 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
532 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
533 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
534 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
535 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
537 /* Custom conversion specifiers. */
539 /* %H will require "location_t" at runtime. */
540 { "H", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
542 /* These will require a "tree" at runtime. */
543 { "J", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
545 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
546 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
547 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
550 static const format_char_info gcc_tdiag_char_table[] =
552 /* C89 conversion specifiers. */
553 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
554 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
555 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
556 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
557 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
558 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
560 /* Custom conversion specifiers. */
562 /* %H will require "location_t" at runtime. */
563 { "H", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
565 /* These will require a "tree" at runtime. */
566 { "DFJT", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
568 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
569 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
570 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
573 static const format_char_info gcc_cdiag_char_table[] =
575 /* C89 conversion specifiers. */
576 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
577 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
578 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
579 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
580 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
581 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
583 /* Custom conversion specifiers. */
585 /* %H will require "location_t" at runtime. */
586 { "H", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
588 /* These will require a "tree" at runtime. */
589 { "DEFJT", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
591 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
592 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
593 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
596 static const format_char_info gcc_cxxdiag_char_table[] =
598 /* C89 conversion specifiers. */
599 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
600 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
601 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
602 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
603 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
604 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
606 /* Custom conversion specifiers. */
608 /* %H will require "location_t" at runtime. */
609 { "H", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
611 /* These will require a "tree" at runtime. */
612 { "ADEFJTV",0,STD_C89,{ T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+#", "", NULL },
614 /* These accept either an 'int' or an 'enum tree_code' (which is handled as an 'int'.) */
615 { "CLOPQ",0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
617 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
618 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
619 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
622 static const format_char_info scan_char_table[] =
624 /* C89 conversion specifiers. */
625 { "di", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, TEX_LL, T99_SST, T99_PD, T99_IM }, "*w'I", "W", NULL },
626 { "u", 1, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "*w'I", "W", NULL },
627 { "oxX", 1, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "*w", "W", NULL },
628 { "efgEG", 1, STD_C89, { T89_F, BADLEN, BADLEN, T89_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "*w'", "W", NULL },
629 { "c", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "cW", NULL },
630 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "cW", NULL },
631 { "[", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "cW[", NULL },
632 { "p", 2, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
633 { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, BADLEN, T99_SST, T99_PD, T99_IM }, "", "W", NULL },
634 /* C99 conversion specifiers. */
635 { "FaA", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "*w'", "W", NULL },
636 /* X/Open conversion specifiers. */
637 { "C", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
638 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "W", NULL },
639 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
642 static const format_char_info time_char_table[] =
644 /* C89 conversion specifiers. */
645 { "ABZab", 0, STD_C89, NOLENGTHS, "^#", "", NULL },
646 { "cx", 0, STD_C89, NOLENGTHS, "E", "3", NULL },
647 { "HIMSUWdmw", 0, STD_C89, NOLENGTHS, "-_0Ow", "", NULL },
648 { "j", 0, STD_C89, NOLENGTHS, "-_0Ow", "o", NULL },
649 { "p", 0, STD_C89, NOLENGTHS, "#", "", NULL },
650 { "X", 0, STD_C89, NOLENGTHS, "E", "", NULL },
651 { "y", 0, STD_C89, NOLENGTHS, "EO-_0w", "4", NULL },
652 { "Y", 0, STD_C89, NOLENGTHS, "-_0EOw", "o", NULL },
653 { "%", 0, STD_C89, NOLENGTHS, "", "", NULL },
654 /* C99 conversion specifiers. */
655 { "C", 0, STD_C99, NOLENGTHS, "-_0EOw", "o", NULL },
656 { "D", 0, STD_C99, NOLENGTHS, "", "2", NULL },
657 { "eVu", 0, STD_C99, NOLENGTHS, "-_0Ow", "", NULL },
658 { "FRTnrt", 0, STD_C99, NOLENGTHS, "", "", NULL },
659 { "g", 0, STD_C99, NOLENGTHS, "O-_0w", "2o", NULL },
660 { "G", 0, STD_C99, NOLENGTHS, "-_0Ow", "o", NULL },
661 { "h", 0, STD_C99, NOLENGTHS, "^#", "", NULL },
662 { "z", 0, STD_C99, NOLENGTHS, "O", "o", NULL },
663 /* GNU conversion specifiers. */
664 { "kls", 0, STD_EXT, NOLENGTHS, "-_0Ow", "", NULL },
665 { "P", 0, STD_EXT, NOLENGTHS, "", "", NULL },
666 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
669 static const format_char_info monetary_char_table[] =
671 { "in", 0, STD_C89, { T89_D, BADLEN, BADLEN, BADLEN, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "=^+(!-w#p", "", NULL },
672 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
675 /* This must be in the same order as enum format_type. */
676 static const format_kind_info format_types_orig[] =
678 { "printf", printf_length_specs, print_char_table, " +#0-'I", NULL,
679 printf_flag_specs, printf_flag_pairs,
680 FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_EMPTY_PREC_OK,
682 &integer_type_node, &integer_type_node
684 { "asm_fprintf", asm_fprintf_length_specs, asm_fprintf_char_table, " +#0-", NULL,
685 asm_fprintf_flag_specs, asm_fprintf_flag_pairs,
686 FMT_FLAG_ARG_CONVERT|FMT_FLAG_EMPTY_PREC_OK,
690 { "gcc_diag", gcc_diag_length_specs, gcc_diag_char_table, "q+", NULL,
691 gcc_diag_flag_specs, gcc_diag_flag_pairs,
692 FMT_FLAG_ARG_CONVERT,
694 NULL, &integer_type_node
696 { "gcc_tdiag", gcc_tdiag_length_specs, gcc_tdiag_char_table, "q+", NULL,
697 gcc_tdiag_flag_specs, gcc_tdiag_flag_pairs,
698 FMT_FLAG_ARG_CONVERT,
700 NULL, &integer_type_node
702 { "gcc_cdiag", gcc_cdiag_length_specs, gcc_cdiag_char_table, "q+", NULL,
703 gcc_cdiag_flag_specs, gcc_cdiag_flag_pairs,
704 FMT_FLAG_ARG_CONVERT,
706 NULL, &integer_type_node
708 { "gcc_cxxdiag", gcc_cxxdiag_length_specs, gcc_cxxdiag_char_table, "q+#", NULL,
709 gcc_cxxdiag_flag_specs, gcc_cxxdiag_flag_pairs,
710 FMT_FLAG_ARG_CONVERT,
712 NULL, &integer_type_node
714 { "scanf", scanf_length_specs, scan_char_table, "*'I", NULL,
715 scanf_flag_specs, scanf_flag_pairs,
716 FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_ZERO_WIDTH_BAD|FMT_FLAG_DOLLAR_GAP_POINTER_OK,
720 { "strftime", NULL, time_char_table, "_-0^#", "EO",
721 strftime_flag_specs, strftime_flag_pairs,
722 FMT_FLAG_FANCY_PERCENT_OK, 'w', 0, 0, 0, 0,
725 { "strfmon", strfmon_length_specs, monetary_char_table, "=^+(!-", NULL,
726 strfmon_flag_specs, strfmon_flag_pairs,
727 FMT_FLAG_ARG_CONVERT, 'w', '#', 'p', 0, 'L',
732 /* This layer of indirection allows GCC to reassign format_types with
733 new data if necessary, while still allowing the original data to be
735 static const format_kind_info *format_types = format_types_orig;
736 /* We can modify this one. We also add target-specific format types
737 to the end of the array. */
738 static format_kind_info *dynamic_format_types;
740 static int n_format_types = ARRAY_SIZE (format_types_orig);
742 /* Structure detailing the results of checking a format function call
743 where the format expression may be a conditional expression with
744 many leaves resulting from nested conditional expressions. */
747 /* Number of leaves of the format argument that could not be checked
748 as they were not string literals. */
749 int number_non_literal;
750 /* Number of leaves of the format argument that were null pointers or
751 string literals, but had extra format arguments. */
752 int number_extra_args;
753 /* Number of leaves of the format argument that were null pointers or
754 string literals, but had extra format arguments and used $ operand
756 int number_dollar_extra_args;
757 /* Number of leaves of the format argument that were wide string
760 /* Number of leaves of the format argument that were empty strings. */
762 /* Number of leaves of the format argument that were unterminated
764 int number_unterminated;
765 /* Number of leaves of the format argument that were not counted above. */
767 } format_check_results;
771 format_check_results *res;
772 function_format_info *info;
774 } format_check_context;
776 static void check_format_info (function_format_info *, tree);
777 static void check_format_arg (void *, tree, unsigned HOST_WIDE_INT);
778 static void check_format_info_main (format_check_results *,
779 function_format_info *,
780 const char *, int, tree,
781 unsigned HOST_WIDE_INT);
783 static void init_dollar_format_checking (int, tree);
784 static int maybe_read_dollar_number (const char **, int,
785 tree, tree *, const format_kind_info *);
786 static bool avoid_dollar_number (const char *);
787 static void finish_dollar_format_checking (format_check_results *, int);
789 static const format_flag_spec *get_flag_spec (const format_flag_spec *,
792 static void check_format_types (format_wanted_type *, const char *, int);
793 static void format_type_warning (const char *, const char *, int, tree,
794 int, const char *, tree, int);
796 /* Decode a format type from a string, returning the type, or
797 format_type_error if not valid, in which case the caller should print an
800 decode_format_type (const char *s)
805 for (i = 0; i < n_format_types; i++)
808 if (!strcmp (s, format_types[i].name))
810 alen = strlen (format_types[i].name);
811 if (slen == alen + 4 && s[0] == '_' && s[1] == '_'
812 && s[slen - 1] == '_' && s[slen - 2] == '_'
813 && !strncmp (s + 2, format_types[i].name, alen))
816 return format_type_error;
820 /* Check the argument list of a call to printf, scanf, etc.
821 ATTRS are the attributes on the function type.
822 PARAMS is the list of argument values. Also, if -Wmissing-format-attribute,
823 warn for calls to vprintf or vscanf in functions with no such format
824 attribute themselves. */
827 check_function_format (tree attrs, tree params)
831 /* See if this function has any format attributes. */
832 for (a = attrs; a; a = TREE_CHAIN (a))
834 if (is_attribute_p ("format", TREE_PURPOSE (a)))
837 function_format_info info;
838 decode_format_attr (TREE_VALUE (a), &info, 1);
839 check_format_info (&info, params);
840 if (warn_missing_format_attribute && info.first_arg_num == 0
841 && (format_types[info.format_type].flags
842 & (int) FMT_FLAG_ARG_CONVERT))
845 for (c = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
848 if (is_attribute_p ("format", TREE_PURPOSE (c))
849 && (decode_format_type (IDENTIFIER_POINTER
850 (TREE_VALUE (TREE_VALUE (c))))
851 == info.format_type))
855 /* Check if the current function has a parameter to which
856 the format attribute could be attached; if not, it
857 can't be a candidate for a format attribute, despite
858 the vprintf-like or vscanf-like call. */
860 for (args = DECL_ARGUMENTS (current_function_decl);
862 args = TREE_CHAIN (args))
864 if (TREE_CODE (TREE_TYPE (args)) == POINTER_TYPE
865 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args)))
870 warning (OPT_Wattributes, "function might be possible "
871 "candidate for %qs format attribute",
872 format_types[info.format_type].name);
880 /* Variables used by the checking of $ operand number formats. */
881 static char *dollar_arguments_used = NULL;
882 static char *dollar_arguments_pointer_p = NULL;
883 static int dollar_arguments_alloc = 0;
884 static int dollar_arguments_count;
885 static int dollar_first_arg_num;
886 static int dollar_max_arg_used;
887 static int dollar_format_warned;
889 /* Initialize the checking for a format string that may contain $
890 parameter number specifications; we will need to keep track of whether
891 each parameter has been used. FIRST_ARG_NUM is the number of the first
892 argument that is a parameter to the format, or 0 for a vprintf-style
893 function; PARAMS is the list of arguments starting at this argument. */
896 init_dollar_format_checking (int first_arg_num, tree params)
898 tree oparams = params;
900 dollar_first_arg_num = first_arg_num;
901 dollar_arguments_count = 0;
902 dollar_max_arg_used = 0;
903 dollar_format_warned = 0;
904 if (first_arg_num > 0)
908 dollar_arguments_count++;
909 params = TREE_CHAIN (params);
912 if (dollar_arguments_alloc < dollar_arguments_count)
914 if (dollar_arguments_used)
915 free (dollar_arguments_used);
916 if (dollar_arguments_pointer_p)
917 free (dollar_arguments_pointer_p);
918 dollar_arguments_alloc = dollar_arguments_count;
919 dollar_arguments_used = XNEWVEC (char, dollar_arguments_alloc);
920 dollar_arguments_pointer_p = XNEWVEC (char, dollar_arguments_alloc);
922 if (dollar_arguments_alloc)
924 memset (dollar_arguments_used, 0, dollar_arguments_alloc);
925 if (first_arg_num > 0)
931 dollar_arguments_pointer_p[i] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params)))
933 params = TREE_CHAIN (params);
941 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
942 is set, it is an error if one is not found; otherwise, it is OK. If
943 such a number is found, check whether it is within range and mark that
944 numbered operand as being used for later checking. Returns the operand
945 number if found and within range, zero if no such number was found and
946 this is OK, or -1 on error. PARAMS points to the first operand of the
947 format; PARAM_PTR is made to point to the parameter referred to. If
948 a $ format is found, *FORMAT is updated to point just after it. */
951 maybe_read_dollar_number (const char **format,
952 int dollar_needed, tree params, tree *param_ptr,
953 const format_kind_info *fki)
957 const char *fcp = *format;
962 warning (OPT_Wformat, "missing $ operand number in format");
970 while (ISDIGIT (*fcp))
973 nargnum = 10 * argnum + (*fcp - '0');
974 if (nargnum < 0 || nargnum / 10 != argnum)
983 warning (OPT_Wformat, "missing $ operand number in format");
990 if (pedantic && !dollar_format_warned)
992 warning (OPT_Wformat, "%s does not support %%n$ operand number formats",
993 C_STD_NAME (STD_EXT));
994 dollar_format_warned = 1;
996 if (overflow_flag || argnum == 0
997 || (dollar_first_arg_num && argnum > dollar_arguments_count))
999 warning (OPT_Wformat, "operand number out of range in format");
1002 if (argnum > dollar_max_arg_used)
1003 dollar_max_arg_used = argnum;
1004 /* For vprintf-style functions we may need to allocate more memory to
1005 track which arguments are used. */
1006 while (dollar_arguments_alloc < dollar_max_arg_used)
1009 nalloc = 2 * dollar_arguments_alloc + 16;
1010 dollar_arguments_used = XRESIZEVEC (char, dollar_arguments_used,
1012 dollar_arguments_pointer_p = XRESIZEVEC (char, dollar_arguments_pointer_p,
1014 memset (dollar_arguments_used + dollar_arguments_alloc, 0,
1015 nalloc - dollar_arguments_alloc);
1016 dollar_arguments_alloc = nalloc;
1018 if (!(fki->flags & (int) FMT_FLAG_DOLLAR_MULTIPLE)
1019 && dollar_arguments_used[argnum - 1] == 1)
1021 dollar_arguments_used[argnum - 1] = 2;
1022 warning (OPT_Wformat, "format argument %d used more than once in %s format",
1026 dollar_arguments_used[argnum - 1] = 1;
1027 if (dollar_first_arg_num)
1030 *param_ptr = params;
1031 for (i = 1; i < argnum && *param_ptr != 0; i++)
1032 *param_ptr = TREE_CHAIN (*param_ptr);
1034 /* This case shouldn't be caught here. */
1035 gcc_assert (*param_ptr);
1042 /* Ensure that FORMAT does not start with a decimal number followed by
1043 a $; give a diagnostic and return true if it does, false otherwise. */
1046 avoid_dollar_number (const char *format)
1048 if (!ISDIGIT (*format))
1050 while (ISDIGIT (*format))
1054 warning (OPT_Wformat, "$ operand number used after format without operand number");
1061 /* Finish the checking for a format string that used $ operand number formats
1062 instead of non-$ formats. We check for unused operands before used ones
1063 (a serious error, since the implementation of the format function
1064 can't know what types to pass to va_arg to find the later arguments).
1065 and for unused operands at the end of the format (if we know how many
1066 arguments the format had, so not for vprintf). If there were operand
1067 numbers out of range on a non-vprintf-style format, we won't have reached
1068 here. If POINTER_GAP_OK, unused arguments are OK if all arguments are
1072 finish_dollar_format_checking (format_check_results *res, int pointer_gap_ok)
1075 bool found_pointer_gap = false;
1076 for (i = 0; i < dollar_max_arg_used; i++)
1078 if (!dollar_arguments_used[i])
1080 if (pointer_gap_ok && (dollar_first_arg_num == 0
1081 || dollar_arguments_pointer_p[i]))
1082 found_pointer_gap = true;
1084 warning (OPT_Wformat,
1085 "format argument %d unused before used argument %d in $-style format",
1086 i + 1, dollar_max_arg_used);
1089 if (found_pointer_gap
1090 || (dollar_first_arg_num
1091 && dollar_max_arg_used < dollar_arguments_count))
1093 res->number_other--;
1094 res->number_dollar_extra_args++;
1099 /* Retrieve the specification for a format flag. SPEC contains the
1100 specifications for format flags for the applicable kind of format.
1101 FLAG is the flag in question. If PREDICATES is NULL, the basic
1102 spec for that flag must be retrieved and must exist. If
1103 PREDICATES is not NULL, it is a string listing possible predicates
1104 for the spec entry; if an entry predicated on any of these is
1105 found, it is returned, otherwise NULL is returned. */
1107 static const format_flag_spec *
1108 get_flag_spec (const format_flag_spec *spec, int flag, const char *predicates)
1111 for (i = 0; spec[i].flag_char != 0; i++)
1113 if (spec[i].flag_char != flag)
1115 if (predicates != NULL)
1117 if (spec[i].predicate != 0
1118 && strchr (predicates, spec[i].predicate) != 0)
1121 else if (spec[i].predicate == 0)
1124 gcc_assert (predicates);
1129 /* Check the argument list of a call to printf, scanf, etc.
1130 INFO points to the function_format_info structure.
1131 PARAMS is the list of argument values. */
1134 check_format_info (function_format_info *info, tree params)
1136 format_check_context format_ctx;
1137 unsigned HOST_WIDE_INT arg_num;
1139 format_check_results res;
1140 /* Skip to format argument. If the argument isn't available, there's
1141 no work for us to do; prototype checking will catch the problem. */
1142 for (arg_num = 1; ; ++arg_num)
1146 if (arg_num == info->format_num)
1148 params = TREE_CHAIN (params);
1150 format_tree = TREE_VALUE (params);
1151 params = TREE_CHAIN (params);
1152 if (format_tree == 0)
1155 res.number_non_literal = 0;
1156 res.number_extra_args = 0;
1157 res.number_dollar_extra_args = 0;
1158 res.number_wide = 0;
1159 res.number_empty = 0;
1160 res.number_unterminated = 0;
1161 res.number_other = 0;
1163 format_ctx.res = &res;
1164 format_ctx.info = info;
1165 format_ctx.params = params;
1167 check_function_arguments_recurse (check_format_arg, &format_ctx,
1168 format_tree, arg_num);
1170 if (res.number_non_literal > 0)
1172 /* Functions taking a va_list normally pass a non-literal format
1173 string. These functions typically are declared with
1174 first_arg_num == 0, so avoid warning in those cases. */
1175 if (!(format_types[info->format_type].flags & (int) FMT_FLAG_ARG_CONVERT))
1177 /* For strftime-like formats, warn for not checking the format
1178 string; but there are no arguments to check. */
1179 warning (OPT_Wformat_nonliteral,
1180 "format not a string literal, format string not checked");
1182 else if (info->first_arg_num != 0)
1184 /* If there are no arguments for the format at all, we may have
1185 printf (foo) which is likely to be a security hole. */
1186 while (arg_num + 1 < info->first_arg_num)
1190 params = TREE_CHAIN (params);
1193 if (params == 0 && warn_format_security)
1194 warning (OPT_Wformat_security,
1195 "format not a string literal and no format arguments");
1196 else if (params == 0 && warn_format_nonliteral)
1197 warning (OPT_Wformat_nonliteral,
1198 "format not a string literal and no format arguments");
1200 warning (OPT_Wformat_nonliteral,
1201 "format not a string literal, argument types not checked");
1205 /* If there were extra arguments to the format, normally warn. However,
1206 the standard does say extra arguments are ignored, so in the specific
1207 case where we have multiple leaves (conditional expressions or
1208 ngettext) allow extra arguments if at least one leaf didn't have extra
1209 arguments, but was otherwise OK (either non-literal or checked OK).
1210 If the format is an empty string, this should be counted similarly to the
1211 case of extra format arguments. */
1212 if (res.number_extra_args > 0 && res.number_non_literal == 0
1213 && res.number_other == 0)
1214 warning (OPT_Wformat_extra_args, "too many arguments for format");
1215 if (res.number_dollar_extra_args > 0 && res.number_non_literal == 0
1216 && res.number_other == 0)
1217 warning (OPT_Wformat_extra_args, "unused arguments in $-style format");
1218 if (res.number_empty > 0 && res.number_non_literal == 0
1219 && res.number_other == 0)
1220 warning (OPT_Wformat_zero_length, "zero-length %s format string",
1221 format_types[info->format_type].name);
1223 if (res.number_wide > 0)
1224 warning (OPT_Wformat, "format is a wide character string");
1226 if (res.number_unterminated > 0)
1227 warning (OPT_Wformat, "unterminated format string");
1230 /* Callback from check_function_arguments_recurse to check a
1231 format string. FORMAT_TREE is the format parameter. ARG_NUM
1232 is the number of the format argument. CTX points to a
1233 format_check_context. */
1236 check_format_arg (void *ctx, tree format_tree,
1237 unsigned HOST_WIDE_INT arg_num)
1239 format_check_context *format_ctx = (format_check_context *) ctx;
1240 format_check_results *res = format_ctx->res;
1241 function_format_info *info = format_ctx->info;
1242 tree params = format_ctx->params;
1245 HOST_WIDE_INT offset;
1246 const char *format_chars;
1247 tree array_size = 0;
1250 if (integer_zerop (format_tree))
1252 /* Skip to first argument to check, so we can see if this format
1253 has any arguments (it shouldn't). */
1254 while (arg_num + 1 < info->first_arg_num)
1258 params = TREE_CHAIN (params);
1263 res->number_other++;
1265 res->number_extra_args++;
1271 if (TREE_CODE (format_tree) == PLUS_EXPR)
1275 arg0 = TREE_OPERAND (format_tree, 0);
1276 arg1 = TREE_OPERAND (format_tree, 1);
1279 if (TREE_CODE (arg1) == INTEGER_CST)
1281 else if (TREE_CODE (arg0) == INTEGER_CST)
1288 res->number_non_literal++;
1291 if (!host_integerp (arg1, 0)
1292 || (offset = tree_low_cst (arg1, 0)) < 0)
1294 res->number_non_literal++;
1298 if (TREE_CODE (format_tree) != ADDR_EXPR)
1300 res->number_non_literal++;
1303 format_tree = TREE_OPERAND (format_tree, 0);
1304 if (TREE_CODE (format_tree) == ARRAY_REF
1305 && host_integerp (TREE_OPERAND (format_tree, 1), 0)
1306 && (offset += tree_low_cst (TREE_OPERAND (format_tree, 1), 0)) >= 0)
1307 format_tree = TREE_OPERAND (format_tree, 0);
1308 if (TREE_CODE (format_tree) == VAR_DECL
1309 && TREE_CODE (TREE_TYPE (format_tree)) == ARRAY_TYPE
1310 && (array_init = decl_constant_value (format_tree)) != format_tree
1311 && TREE_CODE (array_init) == STRING_CST)
1313 /* Extract the string constant initializer. Note that this may include
1314 a trailing NUL character that is not in the array (e.g.
1315 const char a[3] = "foo";). */
1316 array_size = DECL_SIZE_UNIT (format_tree);
1317 format_tree = array_init;
1319 if (TREE_CODE (format_tree) != STRING_CST)
1321 res->number_non_literal++;
1324 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree))) != char_type_node)
1329 format_chars = TREE_STRING_POINTER (format_tree);
1330 format_length = TREE_STRING_LENGTH (format_tree);
1331 if (array_size != 0)
1333 /* Variable length arrays can't be initialized. */
1334 gcc_assert (TREE_CODE (array_size) == INTEGER_CST);
1336 if (host_integerp (array_size, 0))
1338 HOST_WIDE_INT array_size_value = TREE_INT_CST_LOW (array_size);
1339 if (array_size_value > 0
1340 && array_size_value == (int) array_size_value
1341 && format_length > array_size_value)
1342 format_length = array_size_value;
1347 if (offset >= format_length)
1349 res->number_non_literal++;
1352 format_chars += offset;
1353 format_length -= offset;
1355 if (format_length < 1)
1357 res->number_unterminated++;
1360 if (format_length == 1)
1362 res->number_empty++;
1365 if (format_chars[--format_length] != 0)
1367 res->number_unterminated++;
1371 /* Skip to first argument to check. */
1372 while (arg_num + 1 < info->first_arg_num)
1376 params = TREE_CHAIN (params);
1379 /* Provisionally increment res->number_other; check_format_info_main
1380 will decrement it if it finds there are extra arguments, but this way
1381 need not adjust it for every return. */
1382 res->number_other++;
1383 check_format_info_main (res, info, format_chars, format_length,
1388 /* Do the main part of checking a call to a format function. FORMAT_CHARS
1389 is the NUL-terminated format string (which at this point may contain
1390 internal NUL characters); FORMAT_LENGTH is its length (excluding the
1391 terminating NUL character). ARG_NUM is one less than the number of
1392 the first format argument to check; PARAMS points to that format
1393 argument in the list of arguments. */
1396 check_format_info_main (format_check_results *res,
1397 function_format_info *info, const char *format_chars,
1398 int format_length, tree params,
1399 unsigned HOST_WIDE_INT arg_num)
1401 const char *orig_format_chars = format_chars;
1402 tree first_fillin_param = params;
1404 const format_kind_info *fki = &format_types[info->format_type];
1405 const format_flag_spec *flag_specs = fki->flag_specs;
1406 const format_flag_pair *bad_flag_pairs = fki->bad_flag_pairs;
1408 /* -1 if no conversions taking an operand have been found; 0 if one has
1409 and it didn't use $; 1 if $ formats are in use. */
1410 int has_operand_number = -1;
1412 init_dollar_format_checking (info->first_arg_num, first_fillin_param);
1417 int suppressed = FALSE;
1418 const char *length_chars = NULL;
1419 enum format_lengths length_chars_val = FMT_LEN_none;
1420 enum format_std_version length_chars_std = STD_C89;
1424 int main_arg_num = 0;
1425 tree main_arg_params = 0;
1426 enum format_std_version wanted_type_std;
1427 const char *wanted_type_name;
1428 format_wanted_type width_wanted_type;
1429 format_wanted_type precision_wanted_type;
1430 format_wanted_type main_wanted_type;
1431 format_wanted_type *first_wanted_type = NULL;
1432 format_wanted_type *last_wanted_type = NULL;
1433 const format_length_info *fli = NULL;
1434 const format_char_info *fci = NULL;
1435 char flag_chars[256];
1437 const char *format_start = format_chars;
1438 if (*format_chars == 0)
1440 if (format_chars - orig_format_chars != format_length)
1441 warning (OPT_Wformat, "embedded %<\\0%> in format");
1442 if (info->first_arg_num != 0 && params != 0
1443 && has_operand_number <= 0)
1445 res->number_other--;
1446 res->number_extra_args++;
1448 if (has_operand_number > 0)
1449 finish_dollar_format_checking (res, fki->flags & (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK);
1452 if (*format_chars++ != '%')
1454 if (*format_chars == 0)
1456 warning (OPT_Wformat, "spurious trailing %<%%%> in format");
1459 if (*format_chars == '%')
1466 if ((fki->flags & (int) FMT_FLAG_USE_DOLLAR) && has_operand_number != 0)
1468 /* Possibly read a $ operand number at the start of the format.
1469 If one was previously used, one is required here. If one
1470 is not used here, we can't immediately conclude this is a
1471 format without them, since it could be printf %m or scanf %*. */
1473 opnum = maybe_read_dollar_number (&format_chars, 0,
1475 &main_arg_params, fki);
1480 has_operand_number = 1;
1481 main_arg_num = opnum + info->first_arg_num - 1;
1484 else if (fki->flags & FMT_FLAG_USE_DOLLAR)
1486 if (avoid_dollar_number (format_chars))
1490 /* Read any format flags, but do not yet validate them beyond removing
1491 duplicates, since in general validation depends on the rest of
1493 while (*format_chars != 0
1494 && strchr (fki->flag_chars, *format_chars) != 0)
1496 const format_flag_spec *s = get_flag_spec (flag_specs,
1497 *format_chars, NULL);
1498 if (strchr (flag_chars, *format_chars) != 0)
1500 warning (OPT_Wformat, "repeated %s in format", _(s->name));
1504 i = strlen (flag_chars);
1505 flag_chars[i++] = *format_chars;
1508 if (s->skip_next_char)
1511 if (*format_chars == 0)
1513 warning (OPT_Wformat, "missing fill character at end of strfmon format");
1520 /* Read any format width, possibly * or *m$. */
1521 if (fki->width_char != 0)
1523 if (fki->width_type != NULL && *format_chars == '*')
1525 i = strlen (flag_chars);
1526 flag_chars[i++] = fki->width_char;
1528 /* "...a field width...may be indicated by an asterisk.
1529 In this case, an int argument supplies the field width..." */
1531 if (has_operand_number != 0)
1534 opnum = maybe_read_dollar_number (&format_chars,
1535 has_operand_number == 1,
1542 has_operand_number = 1;
1543 arg_num = opnum + info->first_arg_num - 1;
1546 has_operand_number = 0;
1550 if (avoid_dollar_number (format_chars))
1553 if (info->first_arg_num != 0)
1557 warning (OPT_Wformat, "too few arguments for format");
1560 cur_param = TREE_VALUE (params);
1561 if (has_operand_number <= 0)
1563 params = TREE_CHAIN (params);
1566 width_wanted_type.wanted_type = *fki->width_type;
1567 width_wanted_type.wanted_type_name = NULL;
1568 width_wanted_type.pointer_count = 0;
1569 width_wanted_type.char_lenient_flag = 0;
1570 width_wanted_type.writing_in_flag = 0;
1571 width_wanted_type.reading_from_flag = 0;
1572 width_wanted_type.name = _("field width");
1573 width_wanted_type.param = cur_param;
1574 width_wanted_type.arg_num = arg_num;
1575 width_wanted_type.next = NULL;
1576 if (last_wanted_type != 0)
1577 last_wanted_type->next = &width_wanted_type;
1578 if (first_wanted_type == 0)
1579 first_wanted_type = &width_wanted_type;
1580 last_wanted_type = &width_wanted_type;
1585 /* Possibly read a numeric width. If the width is zero,
1586 we complain if appropriate. */
1587 int non_zero_width_char = FALSE;
1588 int found_width = FALSE;
1589 while (ISDIGIT (*format_chars))
1592 if (*format_chars != '0')
1593 non_zero_width_char = TRUE;
1596 if (found_width && !non_zero_width_char &&
1597 (fki->flags & (int) FMT_FLAG_ZERO_WIDTH_BAD))
1598 warning (OPT_Wformat, "zero width in %s format", fki->name);
1601 i = strlen (flag_chars);
1602 flag_chars[i++] = fki->width_char;
1608 /* Read any format left precision (must be a number, not *). */
1609 if (fki->left_precision_char != 0 && *format_chars == '#')
1612 i = strlen (flag_chars);
1613 flag_chars[i++] = fki->left_precision_char;
1615 if (!ISDIGIT (*format_chars))
1616 warning (OPT_Wformat, "empty left precision in %s format", fki->name);
1617 while (ISDIGIT (*format_chars))
1621 /* Read any format precision, possibly * or *m$. */
1622 if (fki->precision_char != 0 && *format_chars == '.')
1625 i = strlen (flag_chars);
1626 flag_chars[i++] = fki->precision_char;
1628 if (fki->precision_type != NULL && *format_chars == '*')
1630 /* "...a...precision...may be indicated by an asterisk.
1631 In this case, an int argument supplies the...precision." */
1633 if (has_operand_number != 0)
1636 opnum = maybe_read_dollar_number (&format_chars,
1637 has_operand_number == 1,
1644 has_operand_number = 1;
1645 arg_num = opnum + info->first_arg_num - 1;
1648 has_operand_number = 0;
1652 if (avoid_dollar_number (format_chars))
1655 if (info->first_arg_num != 0)
1659 warning (OPT_Wformat, "too few arguments for format");
1662 cur_param = TREE_VALUE (params);
1663 if (has_operand_number <= 0)
1665 params = TREE_CHAIN (params);
1668 precision_wanted_type.wanted_type = *fki->precision_type;
1669 precision_wanted_type.wanted_type_name = NULL;
1670 precision_wanted_type.pointer_count = 0;
1671 precision_wanted_type.char_lenient_flag = 0;
1672 precision_wanted_type.writing_in_flag = 0;
1673 precision_wanted_type.reading_from_flag = 0;
1674 precision_wanted_type.name = _("field precision");
1675 precision_wanted_type.param = cur_param;
1676 precision_wanted_type.arg_num = arg_num;
1677 precision_wanted_type.next = NULL;
1678 if (last_wanted_type != 0)
1679 last_wanted_type->next = &precision_wanted_type;
1680 if (first_wanted_type == 0)
1681 first_wanted_type = &precision_wanted_type;
1682 last_wanted_type = &precision_wanted_type;
1687 if (!(fki->flags & (int) FMT_FLAG_EMPTY_PREC_OK)
1688 && !ISDIGIT (*format_chars))
1689 warning (OPT_Wformat, "empty precision in %s format", fki->name);
1690 while (ISDIGIT (*format_chars))
1695 /* Read any length modifier, if this kind of format has them. */
1696 fli = fki->length_char_specs;
1697 length_chars = NULL;
1698 length_chars_val = FMT_LEN_none;
1699 length_chars_std = STD_C89;
1702 while (fli->name != 0 && fli->name[0] != *format_chars)
1707 if (fli->double_name != 0 && fli->name[0] == *format_chars)
1710 length_chars = fli->double_name;
1711 length_chars_val = fli->double_index;
1712 length_chars_std = fli->double_std;
1716 length_chars = fli->name;
1717 length_chars_val = fli->index;
1718 length_chars_std = fli->std;
1720 i = strlen (flag_chars);
1721 flag_chars[i++] = fki->length_code_char;
1726 /* Warn if the length modifier is non-standard. */
1727 if (ADJ_STD (length_chars_std) > C_STD_VER)
1728 warning (OPT_Wformat,
1729 "%s does not support the %qs %s length modifier",
1730 C_STD_NAME (length_chars_std), length_chars,
1735 /* Read any modifier (strftime E/O). */
1736 if (fki->modifier_chars != NULL)
1738 while (*format_chars != 0
1739 && strchr (fki->modifier_chars, *format_chars) != 0)
1741 if (strchr (flag_chars, *format_chars) != 0)
1743 const format_flag_spec *s = get_flag_spec (flag_specs,
1744 *format_chars, NULL);
1745 warning (OPT_Wformat, "repeated %s in format", _(s->name));
1749 i = strlen (flag_chars);
1750 flag_chars[i++] = *format_chars;
1757 /* Handle the scanf allocation kludge. */
1758 if (fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
1760 if (*format_chars == 'a' && !flag_isoc99)
1762 if (format_chars[1] == 's' || format_chars[1] == 'S'
1763 || format_chars[1] == '[')
1765 /* 'a' is used as a flag. */
1766 i = strlen (flag_chars);
1767 flag_chars[i++] = 'a';
1774 format_char = *format_chars;
1775 if (format_char == 0
1776 || (!(fki->flags & (int) FMT_FLAG_FANCY_PERCENT_OK)
1777 && format_char == '%'))
1779 warning (OPT_Wformat, "conversion lacks type at end of format");
1783 fci = fki->conversion_specs;
1784 while (fci->format_chars != 0
1785 && strchr (fci->format_chars, format_char) == 0)
1787 if (fci->format_chars == 0)
1789 if (ISGRAPH (format_char))
1790 warning (OPT_Wformat, "unknown conversion type character %qc in format",
1793 warning (OPT_Wformat, "unknown conversion type character 0x%x in format",
1799 if (ADJ_STD (fci->std) > C_STD_VER)
1800 warning (OPT_Wformat, "%s does not support the %<%%%c%> %s format",
1801 C_STD_NAME (fci->std), format_char, fki->name);
1804 /* Validate the individual flags used, removing any that are invalid. */
1807 for (i = 0; flag_chars[i] != 0; i++)
1809 const format_flag_spec *s = get_flag_spec (flag_specs,
1810 flag_chars[i], NULL);
1811 flag_chars[i - d] = flag_chars[i];
1812 if (flag_chars[i] == fki->length_code_char)
1814 if (strchr (fci->flag_chars, flag_chars[i]) == 0)
1816 warning (OPT_Wformat, "%s used with %<%%%c%> %s format",
1817 _(s->name), format_char, fki->name);
1823 const format_flag_spec *t;
1824 if (ADJ_STD (s->std) > C_STD_VER)
1825 warning (OPT_Wformat, "%s does not support %s",
1826 C_STD_NAME (s->std), _(s->long_name));
1827 t = get_flag_spec (flag_specs, flag_chars[i], fci->flags2);
1828 if (t != NULL && ADJ_STD (t->std) > ADJ_STD (s->std))
1830 const char *long_name = (t->long_name != NULL
1833 if (ADJ_STD (t->std) > C_STD_VER)
1834 warning (OPT_Wformat,
1835 "%s does not support %s with the %<%%%c%> %s format",
1836 C_STD_NAME (t->std), _(long_name),
1837 format_char, fki->name);
1841 flag_chars[i - d] = 0;
1844 if ((fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
1845 && strchr (flag_chars, 'a') != 0)
1848 if (fki->suppression_char
1849 && strchr (flag_chars, fki->suppression_char) != 0)
1852 /* Validate the pairs of flags used. */
1853 for (i = 0; bad_flag_pairs[i].flag_char1 != 0; i++)
1855 const format_flag_spec *s, *t;
1856 if (strchr (flag_chars, bad_flag_pairs[i].flag_char1) == 0)
1858 if (strchr (flag_chars, bad_flag_pairs[i].flag_char2) == 0)
1860 if (bad_flag_pairs[i].predicate != 0
1861 && strchr (fci->flags2, bad_flag_pairs[i].predicate) == 0)
1863 s = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char1, NULL);
1864 t = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char2, NULL);
1865 if (bad_flag_pairs[i].ignored)
1867 if (bad_flag_pairs[i].predicate != 0)
1868 warning (OPT_Wformat,
1869 "%s ignored with %s and %<%%%c%> %s format",
1870 _(s->name), _(t->name), format_char,
1873 warning (OPT_Wformat, "%s ignored with %s in %s format",
1874 _(s->name), _(t->name), fki->name);
1878 if (bad_flag_pairs[i].predicate != 0)
1879 warning (OPT_Wformat,
1880 "use of %s and %s together with %<%%%c%> %s format",
1881 _(s->name), _(t->name), format_char,
1884 warning (OPT_Wformat, "use of %s and %s together in %s format",
1885 _(s->name), _(t->name), fki->name);
1889 /* Give Y2K warnings. */
1890 if (warn_format_y2k)
1893 if (strchr (fci->flags2, '4') != 0)
1894 if (strchr (flag_chars, 'E') != 0)
1898 else if (strchr (fci->flags2, '3') != 0)
1900 else if (strchr (fci->flags2, '2') != 0)
1903 warning (OPT_Wformat_y2k, "%<%%%c%> yields only last 2 digits of "
1904 "year in some locales", format_char);
1905 else if (y2k_level == 2)
1906 warning (OPT_Wformat_y2k, "%<%%%c%> yields only last 2 digits of "
1907 "year", format_char);
1910 if (strchr (fci->flags2, '[') != 0)
1912 /* Skip over scan set, in case it happens to have '%' in it. */
1913 if (*format_chars == '^')
1915 /* Find closing bracket; if one is hit immediately, then
1916 it's part of the scan set rather than a terminator. */
1917 if (*format_chars == ']')
1919 while (*format_chars && *format_chars != ']')
1921 if (*format_chars != ']')
1922 /* The end of the format string was reached. */
1923 warning (OPT_Wformat, "no closing %<]%> for %<%%[%> format");
1927 wanted_type_name = 0;
1928 if (fki->flags & (int) FMT_FLAG_ARG_CONVERT)
1930 wanted_type = (fci->types[length_chars_val].type
1931 ? *fci->types[length_chars_val].type : 0);
1932 wanted_type_name = fci->types[length_chars_val].name;
1933 wanted_type_std = fci->types[length_chars_val].std;
1934 if (wanted_type == 0)
1936 warning (OPT_Wformat,
1937 "use of %qs length modifier with %qc type character",
1938 length_chars, format_char);
1939 /* Heuristic: skip one argument when an invalid length/type
1940 combination is encountered. */
1944 warning (OPT_Wformat, "too few arguments for format");
1947 params = TREE_CHAIN (params);
1951 /* Warn if non-standard, provided it is more non-standard
1952 than the length and type characters that may already
1953 have been warned for. */
1954 && ADJ_STD (wanted_type_std) > ADJ_STD (length_chars_std)
1955 && ADJ_STD (wanted_type_std) > ADJ_STD (fci->std))
1957 if (ADJ_STD (wanted_type_std) > C_STD_VER)
1958 warning (OPT_Wformat,
1959 "%s does not support the %<%%%s%c%> %s format",
1960 C_STD_NAME (wanted_type_std), length_chars,
1961 format_char, fki->name);
1965 main_wanted_type.next = NULL;
1967 /* Finally. . .check type of argument against desired type! */
1968 if (info->first_arg_num == 0)
1970 if ((fci->pointer_count == 0 && wanted_type == void_type_node)
1973 if (main_arg_num != 0)
1976 warning (OPT_Wformat, "operand number specified with "
1977 "suppressed assignment");
1979 warning (OPT_Wformat, "operand number specified for format "
1980 "taking no argument");
1985 format_wanted_type *wanted_type_ptr;
1987 if (main_arg_num != 0)
1989 arg_num = main_arg_num;
1990 params = main_arg_params;
1995 if (has_operand_number > 0)
1997 warning (OPT_Wformat, "missing $ operand number in format");
2001 has_operand_number = 0;
2004 wanted_type_ptr = &main_wanted_type;
2009 warning (OPT_Wformat, "too few arguments for format");
2013 cur_param = TREE_VALUE (params);
2014 params = TREE_CHAIN (params);
2016 wanted_type_ptr->wanted_type = wanted_type;
2017 wanted_type_ptr->wanted_type_name = wanted_type_name;
2018 wanted_type_ptr->pointer_count = fci->pointer_count + aflag;
2019 wanted_type_ptr->char_lenient_flag = 0;
2020 if (strchr (fci->flags2, 'c') != 0)
2021 wanted_type_ptr->char_lenient_flag = 1;
2022 wanted_type_ptr->writing_in_flag = 0;
2023 wanted_type_ptr->reading_from_flag = 0;
2025 wanted_type_ptr->writing_in_flag = 1;
2028 if (strchr (fci->flags2, 'W') != 0)
2029 wanted_type_ptr->writing_in_flag = 1;
2030 if (strchr (fci->flags2, 'R') != 0)
2031 wanted_type_ptr->reading_from_flag = 1;
2033 wanted_type_ptr->name = NULL;
2034 wanted_type_ptr->param = cur_param;
2035 wanted_type_ptr->arg_num = arg_num;
2036 wanted_type_ptr->next = NULL;
2037 if (last_wanted_type != 0)
2038 last_wanted_type->next = wanted_type_ptr;
2039 if (first_wanted_type == 0)
2040 first_wanted_type = wanted_type_ptr;
2041 last_wanted_type = wanted_type_ptr;
2046 wanted_type_ptr = ggc_alloc (sizeof (main_wanted_type));
2048 wanted_type = *fci->types[length_chars_val].type;
2049 wanted_type_name = fci->types[length_chars_val].name;
2054 if (first_wanted_type != 0)
2055 check_format_types (first_wanted_type, format_start,
2056 format_chars - format_start);
2058 if (main_wanted_type.next != NULL)
2060 format_wanted_type *wanted_type_ptr = main_wanted_type.next;
2061 while (wanted_type_ptr)
2063 format_wanted_type *next = wanted_type_ptr->next;
2064 ggc_free (wanted_type_ptr);
2065 wanted_type_ptr = next;
2072 /* Check the argument types from a single format conversion (possibly
2073 including width and precision arguments). */
2075 check_format_types (format_wanted_type *types, const char *format_start,
2078 for (; types != 0; types = types->next)
2087 cur_param = types->param;
2088 cur_type = TREE_TYPE (cur_param);
2089 if (cur_type == error_mark_node)
2091 orig_cur_type = cur_type;
2093 wanted_type = types->wanted_type;
2094 arg_num = types->arg_num;
2096 /* The following should not occur here. */
2097 gcc_assert (wanted_type);
2098 gcc_assert (wanted_type != void_type_node || types->pointer_count);
2100 if (types->pointer_count == 0)
2101 wanted_type = lang_hooks.types.type_promotes_to (wanted_type);
2103 wanted_type = TYPE_MAIN_VARIANT (wanted_type);
2105 STRIP_NOPS (cur_param);
2107 /* Check the types of any additional pointer arguments
2108 that precede the "real" argument. */
2109 for (i = 0; i < types->pointer_count; ++i)
2111 if (TREE_CODE (cur_type) == POINTER_TYPE)
2113 cur_type = TREE_TYPE (cur_type);
2114 if (cur_type == error_mark_node)
2117 /* Check for writing through a NULL pointer. */
2118 if (types->writing_in_flag
2121 && integer_zerop (cur_param))
2122 warning (OPT_Wformat, "writing through null pointer "
2123 "(argument %d)", arg_num);
2125 /* Check for reading through a NULL pointer. */
2126 if (types->reading_from_flag
2129 && integer_zerop (cur_param))
2130 warning (OPT_Wformat, "reading through null pointer "
2131 "(argument %d)", arg_num);
2133 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
2134 cur_param = TREE_OPERAND (cur_param, 0);
2138 /* See if this is an attempt to write into a const type with
2139 scanf or with printf "%n". Note: the writing in happens
2140 at the first indirection only, if for example
2141 void * const * is passed to scanf %p; passing
2142 const void ** is simply passing an incompatible type. */
2143 if (types->writing_in_flag
2145 && (TYPE_READONLY (cur_type)
2147 && (CONSTANT_CLASS_P (cur_param)
2148 || (DECL_P (cur_param)
2149 && TREE_READONLY (cur_param))))))
2150 warning (OPT_Wformat, "writing into constant object "
2151 "(argument %d)", arg_num);
2153 /* If there are extra type qualifiers beyond the first
2154 indirection, then this makes the types technically
2158 && (TYPE_READONLY (cur_type)
2159 || TYPE_VOLATILE (cur_type)
2160 || TYPE_RESTRICT (cur_type)))
2161 warning (OPT_Wformat, "extra type qualifiers in format "
2162 "argument (argument %d)",
2168 format_type_warning (types->name, format_start, format_length,
2169 wanted_type, types->pointer_count,
2170 types->wanted_type_name, orig_cur_type,
2176 if (i < types->pointer_count)
2179 cur_type = TYPE_MAIN_VARIANT (cur_type);
2181 /* Check whether the argument type is a character type. This leniency
2182 only applies to certain formats, flagged with 'c'.
2184 if (types->char_lenient_flag)
2185 char_type_flag = (cur_type == char_type_node
2186 || cur_type == signed_char_type_node
2187 || cur_type == unsigned_char_type_node);
2189 /* Check the type of the "real" argument, if there's a type we want. */
2190 if (wanted_type == cur_type)
2192 /* If we want 'void *', allow any pointer type.
2193 (Anything else would already have got a warning.)
2194 With -pedantic, only allow pointers to void and to character
2196 if (wanted_type == void_type_node
2197 && (!pedantic || (i == 1 && char_type_flag)))
2199 /* Don't warn about differences merely in signedness, unless
2200 -pedantic. With -pedantic, warn if the type is a pointer
2201 target and not a character type, and for character types at
2202 a second level of indirection. */
2203 if (TREE_CODE (wanted_type) == INTEGER_TYPE
2204 && TREE_CODE (cur_type) == INTEGER_TYPE
2205 && (!pedantic || i == 0 || (i == 1 && char_type_flag))
2206 && (TYPE_UNSIGNED (wanted_type)
2207 ? wanted_type == c_common_unsigned_type (cur_type)
2208 : wanted_type == c_common_signed_type (cur_type)))
2210 /* Likewise, "signed char", "unsigned char" and "char" are
2211 equivalent but the above test won't consider them equivalent. */
2212 if (wanted_type == char_type_node
2213 && (!pedantic || i < 2)
2216 /* Now we have a type mismatch. */
2217 format_type_warning (types->name, format_start, format_length,
2218 wanted_type, types->pointer_count,
2219 types->wanted_type_name, orig_cur_type, arg_num);
2224 /* Give a warning about a format argument of different type from that
2225 expected. DESCR is a description such as "field precision", or
2226 NULL for an ordinary format. For an ordinary format, FORMAT_START
2227 points to where the format starts in the format string and
2228 FORMAT_LENGTH is its length. WANTED_TYPE is the type the argument
2229 should have after POINTER_COUNT pointer dereferences.
2230 WANTED_NAME_NAME is a possibly more friendly name of WANTED_TYPE,
2231 or NULL if the ordinary name of the type should be used. ARG_TYPE
2232 is the type of the actual argument. ARG_NUM is the number of that
2235 format_type_warning (const char *descr, const char *format_start,
2236 int format_length, tree wanted_type, int pointer_count,
2237 const char *wanted_type_name, tree arg_type, int arg_num)
2240 /* If ARG_TYPE is a typedef with a misleading name (for example,
2241 size_t but not the standard size_t expected by printf %zu), avoid
2242 printing the typedef name. */
2243 if (wanted_type_name
2244 && TYPE_NAME (arg_type)
2245 && TREE_CODE (TYPE_NAME (arg_type)) == TYPE_DECL
2246 && DECL_NAME (TYPE_NAME (arg_type))
2247 && !strcmp (wanted_type_name,
2248 lang_hooks.decl_printable_name (TYPE_NAME (arg_type), 2)))
2249 arg_type = TYPE_MAIN_VARIANT (arg_type);
2250 /* The format type and name exclude any '*' for pointers, so those
2251 must be formatted manually. For all the types we currently have,
2252 this is adequate, but formats taking pointers to functions or
2253 arrays would require the full type to be built up in order to
2254 print it with %T. */
2255 p = alloca (pointer_count + 2);
2256 if (pointer_count == 0)
2258 else if (c_dialect_cxx ())
2260 memset (p, '*', pointer_count);
2261 p[pointer_count] = 0;
2266 memset (p + 1, '*', pointer_count);
2267 p[pointer_count + 1] = 0;
2269 if (wanted_type_name)
2272 warning (OPT_Wformat, "%s should have type %<%s%s%>, "
2273 "but argument %d has type %qT",
2274 descr, wanted_type_name, p, arg_num, arg_type);
2276 warning (OPT_Wformat, "format %q.*s expects type %<%s%s%>, "
2277 "but argument %d has type %qT",
2278 format_length, format_start, wanted_type_name, p,
2284 warning (OPT_Wformat, "%s should have type %<%T%s%>, "
2285 "but argument %d has type %qT",
2286 descr, wanted_type, p, arg_num, arg_type);
2288 warning (OPT_Wformat, "format %q.*s expects type %<%T%s%>, "
2289 "but argument %d has type %qT",
2290 format_length, format_start, wanted_type, p, arg_num, arg_type);
2295 /* Given a format_char_info array FCI, and a character C, this function
2296 returns the index into the conversion_specs where that specifier's
2297 data is located. The character must exist. */
2299 find_char_info_specifier_index (const format_char_info *fci, int c)
2303 for (i = 0; fci->format_chars; i++, fci++)
2304 if (strchr (fci->format_chars, c))
2307 /* We shouldn't be looking for a non-existent specifier. */
2311 /* Given a format_length_info array FLI, and a character C, this
2312 function returns the index into the conversion_specs where that
2313 modifier's data is located. The character must exist. */
2315 find_length_info_modifier_index (const format_length_info *fli, int c)
2319 for (i = 0; fli->name; i++, fli++)
2320 if (strchr (fli->name, c))
2323 /* We shouldn't be looking for a non-existent modifier. */
2327 /* Determine the type of HOST_WIDE_INT in the code being compiled for
2328 use in GCC's __asm_fprintf__ custom format attribute. You must
2329 have set dynamic_format_types before calling this function. */
2331 init_dynamic_asm_fprintf_info (void)
2337 format_length_info *new_asm_fprintf_length_specs;
2340 /* Find the underlying type for HOST_WIDE_INT. For the %w
2341 length modifier to work, one must have issued: "typedef
2342 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2343 prior to using that modifier. */
2344 hwi = maybe_get_identifier ("__gcc_host_wide_int__");
2347 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2350 hwi = identifier_global_value (hwi);
2351 if (!hwi || TREE_CODE (hwi) != TYPE_DECL)
2353 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2356 hwi = DECL_ORIGINAL_TYPE (hwi);
2358 if (hwi != long_integer_type_node && hwi != long_long_integer_type_node)
2360 error ("%<__gcc_host_wide_int__%> is not defined as %<long%>"
2361 " or %<long long%>");
2365 /* Create a new (writable) copy of asm_fprintf_length_specs. */
2366 new_asm_fprintf_length_specs = (format_length_info *)
2367 xmemdup (asm_fprintf_length_specs,
2368 sizeof (asm_fprintf_length_specs),
2369 sizeof (asm_fprintf_length_specs));
2371 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2372 i = find_length_info_modifier_index (new_asm_fprintf_length_specs, 'w');
2373 if (hwi == long_integer_type_node)
2374 new_asm_fprintf_length_specs[i].index = FMT_LEN_l;
2375 else if (hwi == long_long_integer_type_node)
2376 new_asm_fprintf_length_specs[i].index = FMT_LEN_ll;
2380 /* Assign the new data for use. */
2381 dynamic_format_types[asm_fprintf_format_type].length_char_specs =
2382 new_asm_fprintf_length_specs;
2386 /* Determine the types of "tree" and "location_t" in the code being
2387 compiled for use in GCC's diagnostic custom format attributes. You
2388 must have set dynamic_format_types before calling this function. */
2390 init_dynamic_diag_info (void)
2392 static tree t, loc, hwi;
2394 if (!loc || !t || !hwi)
2396 static format_char_info *diag_fci, *tdiag_fci, *cdiag_fci, *cxxdiag_fci;
2397 static format_length_info *diag_ls;
2400 /* For the GCC-diagnostics custom format specifiers to work, one
2401 must have declared 'tree' and/or 'location_t' prior to using
2402 those attributes. If we haven't seen these declarations then
2403 you shouldn't use the specifiers requiring these types.
2404 However we don't force a hard ICE because we may see only one
2405 or the other type. */
2406 if ((loc = maybe_get_identifier ("location_t")))
2408 loc = identifier_global_value (loc);
2411 if (TREE_CODE (loc) != TYPE_DECL)
2413 error ("%<location_t%> is not defined as a type");
2417 loc = TREE_TYPE (loc);
2421 /* We need to grab the underlying 'union tree_node' so peek into
2422 an extra type level. */
2423 if ((t = maybe_get_identifier ("tree")))
2425 t = identifier_global_value (t);
2428 if (TREE_CODE (t) != TYPE_DECL)
2430 error ("%<tree%> is not defined as a type");
2433 else if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
2435 error ("%<tree%> is not defined as a pointer type");
2439 t = TREE_TYPE (TREE_TYPE (t));
2443 /* Find the underlying type for HOST_WIDE_INT. For the %w
2444 length modifier to work, one must have issued: "typedef
2445 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2446 prior to using that modifier. */
2447 if ((hwi = maybe_get_identifier ("__gcc_host_wide_int__")))
2449 hwi = identifier_global_value (hwi);
2452 if (TREE_CODE (hwi) != TYPE_DECL)
2454 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2459 hwi = DECL_ORIGINAL_TYPE (hwi);
2461 if (hwi != long_integer_type_node
2462 && hwi != long_long_integer_type_node)
2464 error ("%<__gcc_host_wide_int__%> is not defined"
2465 " as %<long%> or %<long long%>");
2472 /* Assign the new data for use. */
2474 /* All the GCC diag formats use the same length specs. */
2476 dynamic_format_types[gcc_diag_format_type].length_char_specs =
2477 dynamic_format_types[gcc_tdiag_format_type].length_char_specs =
2478 dynamic_format_types[gcc_cdiag_format_type].length_char_specs =
2479 dynamic_format_types[gcc_cxxdiag_format_type].length_char_specs =
2480 diag_ls = (format_length_info *)
2481 xmemdup (gcc_diag_length_specs,
2482 sizeof (gcc_diag_length_specs),
2483 sizeof (gcc_diag_length_specs));
2486 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2487 i = find_length_info_modifier_index (diag_ls, 'w');
2488 if (hwi == long_integer_type_node)
2489 diag_ls[i].index = FMT_LEN_l;
2490 else if (hwi == long_long_integer_type_node)
2491 diag_ls[i].index = FMT_LEN_ll;
2496 /* Handle the __gcc_diag__ format specifics. */
2498 dynamic_format_types[gcc_diag_format_type].conversion_specs =
2499 diag_fci = (format_char_info *)
2500 xmemdup (gcc_diag_char_table,
2501 sizeof (gcc_diag_char_table),
2502 sizeof (gcc_diag_char_table));
2505 i = find_char_info_specifier_index (diag_fci, 'H');
2506 diag_fci[i].types[0].type = &loc;
2507 diag_fci[i].pointer_count = 1;
2511 i = find_char_info_specifier_index (diag_fci, 'J');
2512 diag_fci[i].types[0].type = &t;
2513 diag_fci[i].pointer_count = 1;
2516 /* Handle the __gcc_tdiag__ format specifics. */
2518 dynamic_format_types[gcc_tdiag_format_type].conversion_specs =
2519 tdiag_fci = (format_char_info *)
2520 xmemdup (gcc_tdiag_char_table,
2521 sizeof (gcc_tdiag_char_table),
2522 sizeof (gcc_tdiag_char_table));
2525 i = find_char_info_specifier_index (tdiag_fci, 'H');
2526 tdiag_fci[i].types[0].type = &loc;
2527 tdiag_fci[i].pointer_count = 1;
2531 /* All specifiers taking a tree share the same struct. */
2532 i = find_char_info_specifier_index (tdiag_fci, 'D');
2533 tdiag_fci[i].types[0].type = &t;
2534 tdiag_fci[i].pointer_count = 1;
2535 i = find_char_info_specifier_index (tdiag_fci, 'J');
2536 tdiag_fci[i].types[0].type = &t;
2537 tdiag_fci[i].pointer_count = 1;
2540 /* Handle the __gcc_cdiag__ format specifics. */
2542 dynamic_format_types[gcc_cdiag_format_type].conversion_specs =
2543 cdiag_fci = (format_char_info *)
2544 xmemdup (gcc_cdiag_char_table,
2545 sizeof (gcc_cdiag_char_table),
2546 sizeof (gcc_cdiag_char_table));
2549 i = find_char_info_specifier_index (cdiag_fci, 'H');
2550 cdiag_fci[i].types[0].type = &loc;
2551 cdiag_fci[i].pointer_count = 1;
2555 /* All specifiers taking a tree share the same struct. */
2556 i = find_char_info_specifier_index (cdiag_fci, 'D');
2557 cdiag_fci[i].types[0].type = &t;
2558 cdiag_fci[i].pointer_count = 1;
2559 i = find_char_info_specifier_index (cdiag_fci, 'J');
2560 cdiag_fci[i].types[0].type = &t;
2561 cdiag_fci[i].pointer_count = 1;
2564 /* Handle the __gcc_cxxdiag__ format specifics. */
2566 dynamic_format_types[gcc_cxxdiag_format_type].conversion_specs =
2567 cxxdiag_fci = (format_char_info *)
2568 xmemdup (gcc_cxxdiag_char_table,
2569 sizeof (gcc_cxxdiag_char_table),
2570 sizeof (gcc_cxxdiag_char_table));
2573 i = find_char_info_specifier_index (cxxdiag_fci, 'H');
2574 cxxdiag_fci[i].types[0].type = &loc;
2575 cxxdiag_fci[i].pointer_count = 1;
2579 /* All specifiers taking a tree share the same struct. */
2580 i = find_char_info_specifier_index (cxxdiag_fci, 'D');
2581 cxxdiag_fci[i].types[0].type = &t;
2582 cxxdiag_fci[i].pointer_count = 1;
2583 i = find_char_info_specifier_index (cxxdiag_fci, 'J');
2584 cxxdiag_fci[i].types[0].type = &t;
2585 cxxdiag_fci[i].pointer_count = 1;
2590 #ifdef TARGET_FORMAT_TYPES
2591 extern const format_kind_info TARGET_FORMAT_TYPES[];
2594 /* Handle a "format" attribute; arguments as in
2595 struct attribute_spec.handler. */
2597 handle_format_attribute (tree *node, tree ARG_UNUSED (name), tree args,
2598 int flags, bool *no_add_attrs)
2601 function_format_info info;
2604 #ifdef TARGET_FORMAT_TYPES
2605 /* If the target provides additional format types, we need to
2606 add them to FORMAT_TYPES at first use. */
2607 if (TARGET_FORMAT_TYPES != NULL && !dynamic_format_types)
2609 dynamic_format_types = xmalloc ((n_format_types + TARGET_N_FORMAT_TYPES)
2610 * sizeof (dynamic_format_types[0]));
2611 memcpy (dynamic_format_types, format_types_orig,
2612 sizeof (format_types_orig));
2613 memcpy (&dynamic_format_types[n_format_types], TARGET_FORMAT_TYPES,
2614 TARGET_N_FORMAT_TYPES * sizeof (dynamic_format_types[0]));
2616 format_types = dynamic_format_types;
2617 n_format_types += TARGET_N_FORMAT_TYPES;
2621 if (!decode_format_attr (args, &info, 0))
2623 *no_add_attrs = true;
2627 argument = TYPE_ARG_TYPES (type);
2630 if (!check_format_string (argument, info.format_num, flags,
2634 if (info.first_arg_num != 0)
2636 unsigned HOST_WIDE_INT arg_num = 1;
2638 /* Verify that first_arg_num points to the last arg,
2641 arg_num++, argument = TREE_CHAIN (argument);
2643 if (arg_num != info.first_arg_num)
2645 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
2646 error ("args to be formatted is not %<...%>");
2647 *no_add_attrs = true;
2653 if (info.format_type == strftime_format_type && info.first_arg_num != 0)
2655 error ("strftime formats cannot format arguments");
2656 *no_add_attrs = true;
2660 /* If this is a custom GCC-internal format type, we have to
2661 initialize certain bits a runtime. */
2662 if (info.format_type == asm_fprintf_format_type
2663 || info.format_type == gcc_diag_format_type
2664 || info.format_type == gcc_tdiag_format_type
2665 || info.format_type == gcc_cdiag_format_type
2666 || info.format_type == gcc_cxxdiag_format_type)
2668 /* Our first time through, we have to make sure that our
2669 format_type data is allocated dynamically and is modifiable. */
2670 if (!dynamic_format_types)
2671 format_types = dynamic_format_types = (format_kind_info *)
2672 xmemdup (format_types_orig, sizeof (format_types_orig),
2673 sizeof (format_types_orig));
2675 /* If this is format __asm_fprintf__, we have to initialize
2676 GCC's notion of HOST_WIDE_INT for checking %wd. */
2677 if (info.format_type == asm_fprintf_format_type)
2678 init_dynamic_asm_fprintf_info ();
2679 /* If this is one of the diagnostic attributes, then we have to
2680 initialize 'location_t' and 'tree' at runtime. */
2681 else if (info.format_type == gcc_diag_format_type
2682 || info.format_type == gcc_tdiag_format_type
2683 || info.format_type == gcc_cdiag_format_type
2684 || info.format_type == gcc_cxxdiag_format_type)
2685 init_dynamic_diag_info ();