clean up and renames beginigs of a testsuite
[official-gcc.git] / gcc / c-family / c-format.c
blobe7fd2295d52903b1cdb40b92acd433f6e505294d
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, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "c-common.h"
29 #include "toplev.h"
30 #include "intl.h"
31 #include "diagnostic-core.h"
32 #include "langhooks.h"
33 #include "c-format.h"
34 #include "alloc-pool.h"
36 /* Set format warning options according to a -Wformat=n option. */
38 void
39 set_Wformat (int setting)
41 warn_format = setting;
42 warn_format_extra_args = setting;
43 warn_format_zero_length = setting;
44 warn_format_contains_nul = setting;
45 if (setting != 1)
47 warn_format_nonliteral = setting;
48 warn_format_security = setting;
49 warn_format_y2k = setting;
51 /* Make sure not to disable -Wnonnull if -Wformat=0 is specified. */
52 if (setting)
53 warn_nonnull = setting;
57 /* Handle attributes associated with format checking. */
59 /* This must be in the same order as format_types, except for
60 format_type_error. Target-specific format types do not have
61 matching enum values. */
62 enum format_type { printf_format_type, asm_fprintf_format_type,
63 gcc_diag_format_type, gcc_tdiag_format_type,
64 gcc_cdiag_format_type,
65 gcc_cxxdiag_format_type, gcc_gfc_format_type,
66 format_type_error = -1};
68 typedef struct function_format_info
70 int format_type; /* type of format (printf, scanf, etc.) */
71 unsigned HOST_WIDE_INT format_num; /* number of format argument */
72 unsigned HOST_WIDE_INT first_arg_num; /* number of first arg (zero for varargs) */
73 } function_format_info;
75 static bool decode_format_attr (tree, function_format_info *, int);
76 static int decode_format_type (const char *);
78 static bool check_format_string (tree argument,
79 unsigned HOST_WIDE_INT format_num,
80 int flags, bool *no_add_attrs);
81 static bool get_constant (tree expr, unsigned HOST_WIDE_INT *value,
82 int validated_p);
83 static const char *convert_format_name_to_system_name (const char *attr_name);
84 static bool cmp_attribs (const char *tattr_name, const char *attr_name);
86 /* Handle a "format_arg" attribute; arguments as in
87 struct attribute_spec.handler. */
88 tree
89 handle_format_arg_attribute (tree *node, tree ARG_UNUSED (name),
90 tree args, int flags, bool *no_add_attrs)
92 tree type = *node;
93 tree format_num_expr = TREE_VALUE (args);
94 unsigned HOST_WIDE_INT format_num = 0;
95 tree argument;
97 if (!get_constant (format_num_expr, &format_num, 0))
99 error ("format string has invalid operand number");
100 *no_add_attrs = true;
101 return NULL_TREE;
104 argument = TYPE_ARG_TYPES (type);
105 if (argument)
107 if (!check_format_string (argument, format_num, flags, no_add_attrs))
108 return NULL_TREE;
111 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
112 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
113 != char_type_node))
115 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
116 error ("function does not return string type");
117 *no_add_attrs = true;
118 return NULL_TREE;
121 return NULL_TREE;
124 /* Verify that the format_num argument is actually a string, in case
125 the format attribute is in error. */
126 static bool
127 check_format_string (tree argument, unsigned HOST_WIDE_INT format_num,
128 int flags, bool *no_add_attrs)
130 unsigned HOST_WIDE_INT i;
132 for (i = 1; i != format_num; i++)
134 if (argument == 0)
135 break;
136 argument = TREE_CHAIN (argument);
139 if (!argument
140 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
141 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
142 != char_type_node))
144 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
145 error ("format string argument not a string type");
146 *no_add_attrs = true;
147 return false;
150 return true;
153 /* Verify EXPR is a constant, and store its value.
154 If validated_p is true there should be no errors.
155 Returns true on success, false otherwise. */
156 static bool
157 get_constant (tree expr, unsigned HOST_WIDE_INT *value, int validated_p)
159 if (TREE_CODE (expr) != INTEGER_CST || TREE_INT_CST_HIGH (expr) != 0)
161 gcc_assert (!validated_p);
162 return false;
165 *value = TREE_INT_CST_LOW (expr);
167 return true;
170 /* Decode the arguments to a "format" attribute into a
171 function_format_info structure. It is already known that the list
172 is of the right length. If VALIDATED_P is true, then these
173 attributes have already been validated and must not be erroneous;
174 if false, it will give an error message. Returns true if the
175 attributes are successfully decoded, false otherwise. */
177 static bool
178 decode_format_attr (tree args, function_format_info *info, int validated_p)
180 tree format_type_id = TREE_VALUE (args);
181 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
182 tree first_arg_num_expr
183 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
185 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
187 gcc_assert (!validated_p);
188 error ("unrecognized format specifier");
189 return false;
191 else
193 const char *p = IDENTIFIER_POINTER (format_type_id);
195 p = convert_format_name_to_system_name (p);
197 info->format_type = decode_format_type (p);
199 if (info->format_type == format_type_error)
201 gcc_assert (!validated_p);
202 warning (OPT_Wformat, "%qE is an unrecognized format function type",
203 format_type_id);
204 return false;
208 if (!get_constant (format_num_expr, &info->format_num, validated_p))
210 error ("format string has invalid operand number");
211 return false;
214 if (!get_constant (first_arg_num_expr, &info->first_arg_num, validated_p))
216 error ("%<...%> has invalid operand number");
217 return false;
220 if (info->first_arg_num != 0 && info->first_arg_num <= info->format_num)
222 gcc_assert (!validated_p);
223 error ("format string argument follows the args to be formatted");
224 return false;
227 return true;
230 /* Check a call to a format function against a parameter list. */
232 /* The C standard version C++ is treated as equivalent to
233 or inheriting from, for the purpose of format features supported. */
234 #define CPLUSPLUS_STD_VER STD_C94
235 /* The C standard version we are checking formats against when pedantic. */
236 #define C_STD_VER ((int) (c_dialect_cxx () \
237 ? CPLUSPLUS_STD_VER \
238 : (flag_isoc99 \
239 ? STD_C99 \
240 : (flag_isoc94 ? STD_C94 : STD_C89))))
241 /* The name to give to the standard version we are warning about when
242 pedantic. FEATURE_VER is the version in which the feature warned out
243 appeared, which is higher than C_STD_VER. */
244 #define C_STD_NAME(FEATURE_VER) (c_dialect_cxx () \
245 ? "ISO C++" \
246 : ((FEATURE_VER) == STD_EXT \
247 ? "ISO C" \
248 : "ISO C90"))
249 /* Adjust a C standard version, which may be STD_C9L, to account for
250 -Wno-long-long. Returns other standard versions unchanged. */
251 #define ADJ_STD(VER) ((int) ((VER) == STD_C9L \
252 ? (warn_long_long ? STD_C99 : STD_C89) \
253 : (VER)))
255 /* Structure describing details of a type expected in format checking,
256 and the type to check against it. */
257 typedef struct format_wanted_type
259 /* The type wanted. */
260 tree wanted_type;
261 /* The name of this type to use in diagnostics. */
262 const char *wanted_type_name;
263 /* Should be type checked just for scalar width identity. */
264 int scalar_identity_flag;
265 /* The level of indirection through pointers at which this type occurs. */
266 int pointer_count;
267 /* Whether, when pointer_count is 1, to allow any character type when
268 pedantic, rather than just the character or void type specified. */
269 int char_lenient_flag;
270 /* Whether the argument, dereferenced once, is written into and so the
271 argument must not be a pointer to a const-qualified type. */
272 int writing_in_flag;
273 /* Whether the argument, dereferenced once, is read from and so
274 must not be a NULL pointer. */
275 int reading_from_flag;
276 /* If warnings should be of the form "field precision should have
277 type 'int'", the name to use (in this case "field precision"),
278 otherwise NULL, for "format expects type 'long'" type
279 messages. */
280 const char *name;
281 /* The actual parameter to check against the wanted type. */
282 tree param;
283 /* The argument number of that parameter. */
284 int arg_num;
285 /* The next type to check for this format conversion, or NULL if none. */
286 struct format_wanted_type *next;
287 } format_wanted_type;
289 /* Convenience macro for format_length_info meaning unused. */
290 #define NO_FMT NULL, FMT_LEN_none, STD_C89
292 static const format_length_info printf_length_specs[] =
294 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99, 0 },
295 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L, 0 },
296 { "q", FMT_LEN_ll, STD_EXT, NO_FMT, 0 },
297 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
298 { "z", FMT_LEN_z, STD_C99, NO_FMT, 0 },
299 { "Z", FMT_LEN_z, STD_EXT, NO_FMT, 0 },
300 { "t", FMT_LEN_t, STD_C99, NO_FMT, 0 },
301 { "j", FMT_LEN_j, STD_C99, NO_FMT, 0 },
302 { "H", FMT_LEN_H, STD_EXT, NO_FMT, 0 },
303 { "D", FMT_LEN_D, STD_EXT, "DD", FMT_LEN_DD, STD_EXT, 0 },
304 { NO_FMT, NO_FMT, 0 }
307 /* Length specifiers valid for asm_fprintf. */
308 static const format_length_info asm_fprintf_length_specs[] =
310 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89, 0 },
311 { "w", FMT_LEN_none, STD_C89, NO_FMT, 0 },
312 { NO_FMT, NO_FMT, 0 }
315 /* Length specifiers valid for GCC diagnostics. */
316 static const format_length_info gcc_diag_length_specs[] =
318 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89, 0 },
319 { "w", FMT_LEN_none, STD_C89, NO_FMT, 0 },
320 { NO_FMT, NO_FMT, 0 }
323 /* The custom diagnostics all accept the same length specifiers. */
324 #define gcc_tdiag_length_specs gcc_diag_length_specs
325 #define gcc_cdiag_length_specs gcc_diag_length_specs
326 #define gcc_cxxdiag_length_specs gcc_diag_length_specs
328 /* This differs from printf_length_specs only in that "Z" is not accepted. */
329 static const format_length_info scanf_length_specs[] =
331 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99, 0 },
332 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L, 0 },
333 { "q", FMT_LEN_ll, STD_EXT, NO_FMT, 0 },
334 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
335 { "z", FMT_LEN_z, STD_C99, NO_FMT, 0 },
336 { "t", FMT_LEN_t, STD_C99, NO_FMT, 0 },
337 { "j", FMT_LEN_j, STD_C99, NO_FMT, 0 },
338 { "H", FMT_LEN_H, STD_EXT, NO_FMT, 0 },
339 { "D", FMT_LEN_D, STD_EXT, "DD", FMT_LEN_DD, STD_EXT, 0 },
340 { NO_FMT, NO_FMT, 0 }
344 /* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings
345 make no sense for a format type not part of any C standard version. */
346 static const format_length_info strfmon_length_specs[] =
348 /* A GNU extension. */
349 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
350 { NO_FMT, NO_FMT, 0 }
354 /* For now, the Fortran front-end routines only use l as length modifier. */
355 static const format_length_info gcc_gfc_length_specs[] =
357 { "l", FMT_LEN_l, STD_C89, NO_FMT, 0 },
358 { NO_FMT, NO_FMT, 0 }
362 static const format_flag_spec printf_flag_specs[] =
364 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
365 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
366 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
367 { '0', 0, 0, N_("'0' flag"), N_("the '0' 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_EXT },
370 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' printf flag"), STD_EXT },
371 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
372 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
373 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
374 { 0, 0, 0, NULL, NULL, STD_C89 }
378 static const format_flag_pair printf_flag_pairs[] =
380 { ' ', '+', 1, 0 },
381 { '0', '-', 1, 0 },
382 { '0', 'p', 1, 'i' },
383 { 0, 0, 0, 0 }
386 static const format_flag_spec asm_fprintf_flag_specs[] =
388 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
389 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
390 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
391 { '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
392 { '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
393 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
394 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
395 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
396 { 0, 0, 0, NULL, NULL, STD_C89 }
399 static const format_flag_pair asm_fprintf_flag_pairs[] =
401 { ' ', '+', 1, 0 },
402 { '0', '-', 1, 0 },
403 { '0', 'p', 1, 'i' },
404 { 0, 0, 0, 0 }
407 static const format_flag_pair gcc_diag_flag_pairs[] =
409 { 0, 0, 0, 0 }
412 #define gcc_tdiag_flag_pairs gcc_diag_flag_pairs
413 #define gcc_cdiag_flag_pairs gcc_diag_flag_pairs
414 #define gcc_cxxdiag_flag_pairs gcc_diag_flag_pairs
416 static const format_flag_pair gcc_gfc_flag_pairs[] =
418 { 0, 0, 0, 0 }
421 static const format_flag_spec gcc_diag_flag_specs[] =
423 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
424 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
425 { 'q', 0, 0, N_("'q' flag"), N_("the 'q' diagnostic flag"), STD_C89 },
426 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
427 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
428 { 0, 0, 0, NULL, NULL, STD_C89 }
431 #define gcc_tdiag_flag_specs gcc_diag_flag_specs
432 #define gcc_cdiag_flag_specs gcc_diag_flag_specs
433 #define gcc_cxxdiag_flag_specs gcc_diag_flag_specs
435 static const format_flag_spec scanf_flag_specs[] =
437 { '*', 0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89 },
438 { 'a', 0, 0, N_("'a' flag"), N_("the 'a' scanf flag"), STD_EXT },
439 { 'm', 0, 0, N_("'m' flag"), N_("the 'm' scanf flag"), STD_EXT },
440 { 'w', 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89 },
441 { 'L', 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89 },
442 { '\'', 0, 0, N_("''' flag"), N_("the ''' scanf flag"), STD_EXT },
443 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' scanf flag"), STD_EXT },
444 { 0, 0, 0, NULL, NULL, STD_C89 }
448 static const format_flag_pair scanf_flag_pairs[] =
450 { '*', 'L', 0, 0 },
451 { 'a', 'm', 0, 0 },
452 { 0, 0, 0, 0 }
456 static const format_flag_spec strftime_flag_specs[] =
458 { '_', 0, 0, N_("'_' flag"), N_("the '_' strftime flag"), STD_EXT },
459 { '-', 0, 0, N_("'-' flag"), N_("the '-' strftime flag"), STD_EXT },
460 { '0', 0, 0, N_("'0' flag"), N_("the '0' strftime flag"), STD_EXT },
461 { '^', 0, 0, N_("'^' flag"), N_("the '^' strftime flag"), STD_EXT },
462 { '#', 0, 0, N_("'#' flag"), N_("the '#' strftime flag"), STD_EXT },
463 { 'w', 0, 0, N_("field width"), N_("field width in strftime format"), STD_EXT },
464 { 'E', 0, 0, N_("'E' modifier"), N_("the 'E' strftime modifier"), STD_C99 },
465 { 'O', 0, 0, N_("'O' modifier"), N_("the 'O' strftime modifier"), STD_C99 },
466 { 'O', 'o', 0, NULL, N_("the 'O' modifier"), STD_EXT },
467 { 0, 0, 0, NULL, NULL, STD_C89 }
471 static const format_flag_pair strftime_flag_pairs[] =
473 { 'E', 'O', 0, 0 },
474 { '_', '-', 0, 0 },
475 { '_', '0', 0, 0 },
476 { '-', '0', 0, 0 },
477 { '^', '#', 0, 0 },
478 { 0, 0, 0, 0 }
482 static const format_flag_spec strfmon_flag_specs[] =
484 { '=', 0, 1, N_("fill character"), N_("fill character in strfmon format"), STD_C89 },
485 { '^', 0, 0, N_("'^' flag"), N_("the '^' strfmon flag"), STD_C89 },
486 { '+', 0, 0, N_("'+' flag"), N_("the '+' strfmon flag"), STD_C89 },
487 { '(', 0, 0, N_("'(' flag"), N_("the '(' strfmon flag"), STD_C89 },
488 { '!', 0, 0, N_("'!' flag"), N_("the '!' strfmon flag"), STD_C89 },
489 { '-', 0, 0, N_("'-' flag"), N_("the '-' strfmon flag"), STD_C89 },
490 { 'w', 0, 0, N_("field width"), N_("field width in strfmon format"), STD_C89 },
491 { '#', 0, 0, N_("left precision"), N_("left precision in strfmon format"), STD_C89 },
492 { 'p', 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89 },
493 { 'L', 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89 },
494 { 0, 0, 0, NULL, NULL, STD_C89 }
497 static const format_flag_pair strfmon_flag_pairs[] =
499 { '+', '(', 0, 0 },
500 { 0, 0, 0, 0 }
504 static const format_char_info print_char_table[] =
506 /* C89 conversion specifiers. */
507 { "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 },
508 { "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 },
509 { "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 },
510 { "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 },
511 { "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 },
512 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, T94_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
513 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
514 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "c", NULL },
515 { "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 },
516 /* C99 conversion specifiers. */
517 { "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 },
518 { "aA", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "", NULL },
519 /* X/Open conversion specifiers. */
520 { "C", 0, STD_EXT, { TEX_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
521 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "R", NULL },
522 /* GNU conversion specifiers. */
523 { "m", 0, STD_EXT, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "", NULL },
524 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
527 static const format_char_info asm_fprintf_char_table[] =
529 /* C89 conversion specifiers. */
530 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +", "i", NULL },
531 { "oxX", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0#", "i", NULL },
532 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0", "i", NULL },
533 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
534 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
536 /* asm_fprintf conversion specifiers. */
537 { "O", 0, STD_C89, NOARGUMENTS, "", "", NULL },
538 { "R", 0, STD_C89, NOARGUMENTS, "", "", NULL },
539 { "I", 0, STD_C89, NOARGUMENTS, "", "", NULL },
540 { "L", 0, STD_C89, NOARGUMENTS, "", "", NULL },
541 { "U", 0, STD_C89, NOARGUMENTS, "", "", NULL },
542 { "r", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
543 { "@", 0, STD_C89, NOARGUMENTS, "", "", NULL },
544 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
547 static const format_char_info gcc_diag_char_table[] =
549 /* C89 conversion specifiers. */
550 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
551 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
552 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
553 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
554 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
555 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
557 /* Custom conversion specifiers. */
559 /* These will require a "tree" at runtime. */
560 { "K", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
562 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
563 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
564 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
567 static const format_char_info gcc_tdiag_char_table[] =
569 /* C89 conversion specifiers. */
570 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
571 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
572 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
573 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
574 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
575 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
577 /* Custom conversion specifiers. */
579 /* These will require a "tree" at runtime. */
580 { "DFKTEV", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
582 { "v", 0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
584 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
585 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
586 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
589 static const format_char_info gcc_cdiag_char_table[] =
591 /* C89 conversion specifiers. */
592 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
593 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
594 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
595 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
596 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
597 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
599 /* Custom conversion specifiers. */
601 /* These will require a "tree" at runtime. */
602 { "DEFKTV", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
604 { "v", 0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
606 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
607 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
608 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
611 static const format_char_info gcc_cxxdiag_char_table[] =
613 /* C89 conversion specifiers. */
614 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
615 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
616 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
617 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
618 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
619 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
621 /* Custom conversion specifiers. */
623 /* These will require a "tree" at runtime. */
624 { "ADEFKTV",0,STD_C89,{ T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+#", "", NULL },
626 { "v", 0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
628 /* These accept either an 'int' or an 'enum tree_code' (which is handled as an 'int'.) */
629 { "CLOPQ",0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
631 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
632 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
633 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
636 static const format_char_info gcc_gfc_char_table[] =
638 /* C89 conversion specifiers. */
639 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
640 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
641 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
642 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL },
644 /* gfc conversion specifiers. */
646 { "C", 0, STD_C89, NOARGUMENTS, "", "", NULL },
648 /* This will require a "locus" at runtime. */
649 { "L", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "R", NULL },
651 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
654 static const format_char_info scan_char_table[] =
656 /* C89 conversion specifiers. */
657 { "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 },
658 { "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 },
659 { "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 },
660 { "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 },
661 { "c", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*mw", "cW", NULL },
662 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "cW", NULL },
663 { "[", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "cW[", NULL },
664 { "p", 2, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
665 { "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 },
666 /* C99 conversion specifiers. */
667 { "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 },
668 { "aA", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w'", "W", NULL },
669 /* X/Open conversion specifiers. */
670 { "C", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*mw", "W", NULL },
671 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "W", NULL },
672 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
675 static const format_char_info time_char_table[] =
677 /* C89 conversion specifiers. */
678 { "ABZab", 0, STD_C89, NOLENGTHS, "^#", "", NULL },
679 { "cx", 0, STD_C89, NOLENGTHS, "E", "3", NULL },
680 { "HIMSUWdmw", 0, STD_C89, NOLENGTHS, "-_0Ow", "", NULL },
681 { "j", 0, STD_C89, NOLENGTHS, "-_0Ow", "o", NULL },
682 { "p", 0, STD_C89, NOLENGTHS, "#", "", NULL },
683 { "X", 0, STD_C89, NOLENGTHS, "E", "", NULL },
684 { "y", 0, STD_C89, NOLENGTHS, "EO-_0w", "4", NULL },
685 { "Y", 0, STD_C89, NOLENGTHS, "-_0EOw", "o", NULL },
686 { "%", 0, STD_C89, NOLENGTHS, "", "", NULL },
687 /* C99 conversion specifiers. */
688 { "C", 0, STD_C99, NOLENGTHS, "-_0EOw", "o", NULL },
689 { "D", 0, STD_C99, NOLENGTHS, "", "2", NULL },
690 { "eVu", 0, STD_C99, NOLENGTHS, "-_0Ow", "", NULL },
691 { "FRTnrt", 0, STD_C99, NOLENGTHS, "", "", NULL },
692 { "g", 0, STD_C99, NOLENGTHS, "O-_0w", "2o", NULL },
693 { "G", 0, STD_C99, NOLENGTHS, "-_0Ow", "o", NULL },
694 { "h", 0, STD_C99, NOLENGTHS, "^#", "", NULL },
695 { "z", 0, STD_C99, NOLENGTHS, "O", "o", NULL },
696 /* GNU conversion specifiers. */
697 { "kls", 0, STD_EXT, NOLENGTHS, "-_0Ow", "", NULL },
698 { "P", 0, STD_EXT, NOLENGTHS, "", "", NULL },
699 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
702 static const format_char_info monetary_char_table[] =
704 { "in", 0, STD_C89, { T89_D, BADLEN, BADLEN, BADLEN, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "=^+(!-w#p", "", NULL },
705 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
708 /* This must be in the same order as enum format_type. */
709 static const format_kind_info format_types_orig[] =
711 { "gnu_printf", printf_length_specs, print_char_table, " +#0-'I", NULL,
712 printf_flag_specs, printf_flag_pairs,
713 FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_EMPTY_PREC_OK,
714 'w', 0, 'p', 0, 'L', 0,
715 &integer_type_node, &integer_type_node
717 { "asm_fprintf", asm_fprintf_length_specs, asm_fprintf_char_table, " +#0-", NULL,
718 asm_fprintf_flag_specs, asm_fprintf_flag_pairs,
719 FMT_FLAG_ARG_CONVERT|FMT_FLAG_EMPTY_PREC_OK,
720 'w', 0, 'p', 0, 'L', 0,
721 NULL, NULL
723 { "gcc_diag", gcc_diag_length_specs, gcc_diag_char_table, "q+#", NULL,
724 gcc_diag_flag_specs, gcc_diag_flag_pairs,
725 FMT_FLAG_ARG_CONVERT,
726 0, 0, 'p', 0, 'L', 0,
727 NULL, &integer_type_node
729 { "gcc_tdiag", gcc_tdiag_length_specs, gcc_tdiag_char_table, "q+#", NULL,
730 gcc_tdiag_flag_specs, gcc_tdiag_flag_pairs,
731 FMT_FLAG_ARG_CONVERT,
732 0, 0, 'p', 0, 'L', 0,
733 NULL, &integer_type_node
735 { "gcc_cdiag", gcc_cdiag_length_specs, gcc_cdiag_char_table, "q+#", NULL,
736 gcc_cdiag_flag_specs, gcc_cdiag_flag_pairs,
737 FMT_FLAG_ARG_CONVERT,
738 0, 0, 'p', 0, 'L', 0,
739 NULL, &integer_type_node
741 { "gcc_cxxdiag", gcc_cxxdiag_length_specs, gcc_cxxdiag_char_table, "q+#", NULL,
742 gcc_cxxdiag_flag_specs, gcc_cxxdiag_flag_pairs,
743 FMT_FLAG_ARG_CONVERT,
744 0, 0, 'p', 0, 'L', 0,
745 NULL, &integer_type_node
747 { "gcc_gfc", gcc_gfc_length_specs, gcc_gfc_char_table, "", NULL,
748 NULL, gcc_gfc_flag_pairs,
749 FMT_FLAG_ARG_CONVERT,
750 0, 0, 0, 0, 0, 0,
751 NULL, NULL
753 { "gnu_scanf", scanf_length_specs, scan_char_table, "*'I", NULL,
754 scanf_flag_specs, scanf_flag_pairs,
755 FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_ZERO_WIDTH_BAD|FMT_FLAG_DOLLAR_GAP_POINTER_OK,
756 'w', 0, 0, '*', 'L', 'm',
757 NULL, NULL
759 { "gnu_strftime", NULL, time_char_table, "_-0^#", "EO",
760 strftime_flag_specs, strftime_flag_pairs,
761 FMT_FLAG_FANCY_PERCENT_OK, 'w', 0, 0, 0, 0, 0,
762 NULL, NULL
764 { "gnu_strfmon", strfmon_length_specs, monetary_char_table, "=^+(!-", NULL,
765 strfmon_flag_specs, strfmon_flag_pairs,
766 FMT_FLAG_ARG_CONVERT, 'w', '#', 'p', 0, 'L', 0,
767 NULL, NULL
771 /* This layer of indirection allows GCC to reassign format_types with
772 new data if necessary, while still allowing the original data to be
773 const. */
774 static const format_kind_info *format_types = format_types_orig;
775 /* We can modify this one. We also add target-specific format types
776 to the end of the array. */
777 static format_kind_info *dynamic_format_types;
779 static int n_format_types = ARRAY_SIZE (format_types_orig);
781 /* Structure detailing the results of checking a format function call
782 where the format expression may be a conditional expression with
783 many leaves resulting from nested conditional expressions. */
784 typedef struct
786 /* Number of leaves of the format argument that could not be checked
787 as they were not string literals. */
788 int number_non_literal;
789 /* Number of leaves of the format argument that were null pointers or
790 string literals, but had extra format arguments. */
791 int number_extra_args;
792 /* Number of leaves of the format argument that were null pointers or
793 string literals, but had extra format arguments and used $ operand
794 numbers. */
795 int number_dollar_extra_args;
796 /* Number of leaves of the format argument that were wide string
797 literals. */
798 int number_wide;
799 /* Number of leaves of the format argument that were empty strings. */
800 int number_empty;
801 /* Number of leaves of the format argument that were unterminated
802 strings. */
803 int number_unterminated;
804 /* Number of leaves of the format argument that were not counted above. */
805 int number_other;
806 } format_check_results;
808 typedef struct
810 format_check_results *res;
811 function_format_info *info;
812 tree params;
813 } format_check_context;
815 static void check_format_info (function_format_info *, tree);
816 static void check_format_arg (void *, tree, unsigned HOST_WIDE_INT);
817 static void check_format_info_main (format_check_results *,
818 function_format_info *,
819 const char *, int, tree,
820 unsigned HOST_WIDE_INT, alloc_pool);
822 static void init_dollar_format_checking (int, tree);
823 static int maybe_read_dollar_number (const char **, int,
824 tree, tree *, const format_kind_info *);
825 static bool avoid_dollar_number (const char *);
826 static void finish_dollar_format_checking (format_check_results *, int);
828 static const format_flag_spec *get_flag_spec (const format_flag_spec *,
829 int, const char *);
831 static void check_format_types (format_wanted_type *, const char *, int);
832 static void format_type_warning (const char *, const char *, int, tree,
833 int, const char *, tree, int);
835 /* Decode a format type from a string, returning the type, or
836 format_type_error if not valid, in which case the caller should print an
837 error message. */
838 static int
839 decode_format_type (const char *s)
841 int i;
842 int slen;
844 s = convert_format_name_to_system_name (s);
845 slen = strlen (s);
846 for (i = 0; i < n_format_types; i++)
848 int alen;
849 if (!strcmp (s, format_types[i].name))
850 return i;
851 alen = strlen (format_types[i].name);
852 if (slen == alen + 4 && s[0] == '_' && s[1] == '_'
853 && s[slen - 1] == '_' && s[slen - 2] == '_'
854 && !strncmp (s + 2, format_types[i].name, alen))
855 return i;
857 return format_type_error;
861 /* Check the argument list of a call to printf, scanf, etc.
862 ATTRS are the attributes on the function type. There are NARGS argument
863 values in the array ARGARRAY.
864 Also, if -Wmissing-format-attribute,
865 warn for calls to vprintf or vscanf in functions with no such format
866 attribute themselves. */
868 void
869 check_function_format (tree attrs, int nargs, tree *argarray)
871 tree a;
873 /* See if this function has any format attributes. */
874 for (a = attrs; a; a = TREE_CHAIN (a))
876 if (is_attribute_p ("format", TREE_PURPOSE (a)))
878 /* Yup; check it. */
879 function_format_info info;
880 decode_format_attr (TREE_VALUE (a), &info, 1);
881 if (warn_format)
883 /* FIXME: Rewrite all the internal functions in this file
884 to use the ARGARRAY directly instead of constructing this
885 temporary list. */
886 tree params = NULL_TREE;
887 int i;
888 for (i = nargs - 1; i >= 0; i--)
889 params = tree_cons (NULL_TREE, argarray[i], params);
890 check_format_info (&info, params);
892 if (warn_missing_format_attribute && info.first_arg_num == 0
893 && (format_types[info.format_type].flags
894 & (int) FMT_FLAG_ARG_CONVERT))
896 tree c;
897 for (c = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
899 c = TREE_CHAIN (c))
900 if (is_attribute_p ("format", TREE_PURPOSE (c))
901 && (decode_format_type (IDENTIFIER_POINTER
902 (TREE_VALUE (TREE_VALUE (c))))
903 == info.format_type))
904 break;
905 if (c == NULL_TREE)
907 /* Check if the current function has a parameter to which
908 the format attribute could be attached; if not, it
909 can't be a candidate for a format attribute, despite
910 the vprintf-like or vscanf-like call. */
911 tree args;
912 for (args = DECL_ARGUMENTS (current_function_decl);
913 args != 0;
914 args = DECL_CHAIN (args))
916 if (TREE_CODE (TREE_TYPE (args)) == POINTER_TYPE
917 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args)))
918 == char_type_node))
919 break;
921 if (args != 0)
922 warning (OPT_Wmissing_format_attribute, "function might "
923 "be possible candidate for %qs format attribute",
924 format_types[info.format_type].name);
932 /* Variables used by the checking of $ operand number formats. */
933 static char *dollar_arguments_used = NULL;
934 static char *dollar_arguments_pointer_p = NULL;
935 static int dollar_arguments_alloc = 0;
936 static int dollar_arguments_count;
937 static int dollar_first_arg_num;
938 static int dollar_max_arg_used;
939 static int dollar_format_warned;
941 /* Initialize the checking for a format string that may contain $
942 parameter number specifications; we will need to keep track of whether
943 each parameter has been used. FIRST_ARG_NUM is the number of the first
944 argument that is a parameter to the format, or 0 for a vprintf-style
945 function; PARAMS is the list of arguments starting at this argument. */
947 static void
948 init_dollar_format_checking (int first_arg_num, tree params)
950 tree oparams = params;
952 dollar_first_arg_num = first_arg_num;
953 dollar_arguments_count = 0;
954 dollar_max_arg_used = 0;
955 dollar_format_warned = 0;
956 if (first_arg_num > 0)
958 while (params)
960 dollar_arguments_count++;
961 params = TREE_CHAIN (params);
964 if (dollar_arguments_alloc < dollar_arguments_count)
966 if (dollar_arguments_used)
967 free (dollar_arguments_used);
968 if (dollar_arguments_pointer_p)
969 free (dollar_arguments_pointer_p);
970 dollar_arguments_alloc = dollar_arguments_count;
971 dollar_arguments_used = XNEWVEC (char, dollar_arguments_alloc);
972 dollar_arguments_pointer_p = XNEWVEC (char, dollar_arguments_alloc);
974 if (dollar_arguments_alloc)
976 memset (dollar_arguments_used, 0, dollar_arguments_alloc);
977 if (first_arg_num > 0)
979 int i = 0;
980 params = oparams;
981 while (params)
983 dollar_arguments_pointer_p[i] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params)))
984 == POINTER_TYPE);
985 params = TREE_CHAIN (params);
986 i++;
993 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
994 is set, it is an error if one is not found; otherwise, it is OK. If
995 such a number is found, check whether it is within range and mark that
996 numbered operand as being used for later checking. Returns the operand
997 number if found and within range, zero if no such number was found and
998 this is OK, or -1 on error. PARAMS points to the first operand of the
999 format; PARAM_PTR is made to point to the parameter referred to. If
1000 a $ format is found, *FORMAT is updated to point just after it. */
1002 static int
1003 maybe_read_dollar_number (const char **format,
1004 int dollar_needed, tree params, tree *param_ptr,
1005 const format_kind_info *fki)
1007 int argnum;
1008 int overflow_flag;
1009 const char *fcp = *format;
1010 if (!ISDIGIT (*fcp))
1012 if (dollar_needed)
1014 warning (OPT_Wformat, "missing $ operand number in format");
1015 return -1;
1017 else
1018 return 0;
1020 argnum = 0;
1021 overflow_flag = 0;
1022 while (ISDIGIT (*fcp))
1024 int nargnum;
1025 nargnum = 10 * argnum + (*fcp - '0');
1026 if (nargnum < 0 || nargnum / 10 != argnum)
1027 overflow_flag = 1;
1028 argnum = nargnum;
1029 fcp++;
1031 if (*fcp != '$')
1033 if (dollar_needed)
1035 warning (OPT_Wformat, "missing $ operand number in format");
1036 return -1;
1038 else
1039 return 0;
1041 *format = fcp + 1;
1042 if (pedantic && !dollar_format_warned)
1044 warning (OPT_Wformat, "%s does not support %%n$ operand number formats",
1045 C_STD_NAME (STD_EXT));
1046 dollar_format_warned = 1;
1048 if (overflow_flag || argnum == 0
1049 || (dollar_first_arg_num && argnum > dollar_arguments_count))
1051 warning (OPT_Wformat, "operand number out of range in format");
1052 return -1;
1054 if (argnum > dollar_max_arg_used)
1055 dollar_max_arg_used = argnum;
1056 /* For vprintf-style functions we may need to allocate more memory to
1057 track which arguments are used. */
1058 while (dollar_arguments_alloc < dollar_max_arg_used)
1060 int nalloc;
1061 nalloc = 2 * dollar_arguments_alloc + 16;
1062 dollar_arguments_used = XRESIZEVEC (char, dollar_arguments_used,
1063 nalloc);
1064 dollar_arguments_pointer_p = XRESIZEVEC (char, dollar_arguments_pointer_p,
1065 nalloc);
1066 memset (dollar_arguments_used + dollar_arguments_alloc, 0,
1067 nalloc - dollar_arguments_alloc);
1068 dollar_arguments_alloc = nalloc;
1070 if (!(fki->flags & (int) FMT_FLAG_DOLLAR_MULTIPLE)
1071 && dollar_arguments_used[argnum - 1] == 1)
1073 dollar_arguments_used[argnum - 1] = 2;
1074 warning (OPT_Wformat, "format argument %d used more than once in %s format",
1075 argnum, fki->name);
1077 else
1078 dollar_arguments_used[argnum - 1] = 1;
1079 if (dollar_first_arg_num)
1081 int i;
1082 *param_ptr = params;
1083 for (i = 1; i < argnum && *param_ptr != 0; i++)
1084 *param_ptr = TREE_CHAIN (*param_ptr);
1086 /* This case shouldn't be caught here. */
1087 gcc_assert (*param_ptr);
1089 else
1090 *param_ptr = 0;
1091 return argnum;
1094 /* Ensure that FORMAT does not start with a decimal number followed by
1095 a $; give a diagnostic and return true if it does, false otherwise. */
1097 static bool
1098 avoid_dollar_number (const char *format)
1100 if (!ISDIGIT (*format))
1101 return false;
1102 while (ISDIGIT (*format))
1103 format++;
1104 if (*format == '$')
1106 warning (OPT_Wformat, "$ operand number used after format without operand number");
1107 return true;
1109 return false;
1113 /* Finish the checking for a format string that used $ operand number formats
1114 instead of non-$ formats. We check for unused operands before used ones
1115 (a serious error, since the implementation of the format function
1116 can't know what types to pass to va_arg to find the later arguments).
1117 and for unused operands at the end of the format (if we know how many
1118 arguments the format had, so not for vprintf). If there were operand
1119 numbers out of range on a non-vprintf-style format, we won't have reached
1120 here. If POINTER_GAP_OK, unused arguments are OK if all arguments are
1121 pointers. */
1123 static void
1124 finish_dollar_format_checking (format_check_results *res, int pointer_gap_ok)
1126 int i;
1127 bool found_pointer_gap = false;
1128 for (i = 0; i < dollar_max_arg_used; i++)
1130 if (!dollar_arguments_used[i])
1132 if (pointer_gap_ok && (dollar_first_arg_num == 0
1133 || dollar_arguments_pointer_p[i]))
1134 found_pointer_gap = true;
1135 else
1136 warning (OPT_Wformat,
1137 "format argument %d unused before used argument %d in $-style format",
1138 i + 1, dollar_max_arg_used);
1141 if (found_pointer_gap
1142 || (dollar_first_arg_num
1143 && dollar_max_arg_used < dollar_arguments_count))
1145 res->number_other--;
1146 res->number_dollar_extra_args++;
1151 /* Retrieve the specification for a format flag. SPEC contains the
1152 specifications for format flags for the applicable kind of format.
1153 FLAG is the flag in question. If PREDICATES is NULL, the basic
1154 spec for that flag must be retrieved and must exist. If
1155 PREDICATES is not NULL, it is a string listing possible predicates
1156 for the spec entry; if an entry predicated on any of these is
1157 found, it is returned, otherwise NULL is returned. */
1159 static const format_flag_spec *
1160 get_flag_spec (const format_flag_spec *spec, int flag, const char *predicates)
1162 int i;
1163 for (i = 0; spec[i].flag_char != 0; i++)
1165 if (spec[i].flag_char != flag)
1166 continue;
1167 if (predicates != NULL)
1169 if (spec[i].predicate != 0
1170 && strchr (predicates, spec[i].predicate) != 0)
1171 return &spec[i];
1173 else if (spec[i].predicate == 0)
1174 return &spec[i];
1176 gcc_assert (predicates);
1177 return NULL;
1181 /* Check the argument list of a call to printf, scanf, etc.
1182 INFO points to the function_format_info structure.
1183 PARAMS is the list of argument values. */
1185 static void
1186 check_format_info (function_format_info *info, tree params)
1188 format_check_context format_ctx;
1189 unsigned HOST_WIDE_INT arg_num;
1190 tree format_tree;
1191 format_check_results res;
1192 /* Skip to format argument. If the argument isn't available, there's
1193 no work for us to do; prototype checking will catch the problem. */
1194 for (arg_num = 1; ; ++arg_num)
1196 if (params == 0)
1197 return;
1198 if (arg_num == info->format_num)
1199 break;
1200 params = TREE_CHAIN (params);
1202 format_tree = TREE_VALUE (params);
1203 params = TREE_CHAIN (params);
1204 if (format_tree == 0)
1205 return;
1207 res.number_non_literal = 0;
1208 res.number_extra_args = 0;
1209 res.number_dollar_extra_args = 0;
1210 res.number_wide = 0;
1211 res.number_empty = 0;
1212 res.number_unterminated = 0;
1213 res.number_other = 0;
1215 format_ctx.res = &res;
1216 format_ctx.info = info;
1217 format_ctx.params = params;
1219 check_function_arguments_recurse (check_format_arg, &format_ctx,
1220 format_tree, arg_num);
1222 if (res.number_non_literal > 0)
1224 /* Functions taking a va_list normally pass a non-literal format
1225 string. These functions typically are declared with
1226 first_arg_num == 0, so avoid warning in those cases. */
1227 if (!(format_types[info->format_type].flags & (int) FMT_FLAG_ARG_CONVERT))
1229 /* For strftime-like formats, warn for not checking the format
1230 string; but there are no arguments to check. */
1231 warning (OPT_Wformat_nonliteral,
1232 "format not a string literal, format string not checked");
1234 else if (info->first_arg_num != 0)
1236 /* If there are no arguments for the format at all, we may have
1237 printf (foo) which is likely to be a security hole. */
1238 while (arg_num + 1 < info->first_arg_num)
1240 if (params == 0)
1241 break;
1242 params = TREE_CHAIN (params);
1243 ++arg_num;
1245 if (params == 0 && warn_format_security)
1246 warning (OPT_Wformat_security,
1247 "format not a string literal and no format arguments");
1248 else if (params == 0 && warn_format_nonliteral)
1249 warning (OPT_Wformat_nonliteral,
1250 "format not a string literal and no format arguments");
1251 else
1252 warning (OPT_Wformat_nonliteral,
1253 "format not a string literal, argument types not checked");
1257 /* If there were extra arguments to the format, normally warn. However,
1258 the standard does say extra arguments are ignored, so in the specific
1259 case where we have multiple leaves (conditional expressions or
1260 ngettext) allow extra arguments if at least one leaf didn't have extra
1261 arguments, but was otherwise OK (either non-literal or checked OK).
1262 If the format is an empty string, this should be counted similarly to the
1263 case of extra format arguments. */
1264 if (res.number_extra_args > 0 && res.number_non_literal == 0
1265 && res.number_other == 0)
1266 warning (OPT_Wformat_extra_args, "too many arguments for format");
1267 if (res.number_dollar_extra_args > 0 && res.number_non_literal == 0
1268 && res.number_other == 0)
1269 warning (OPT_Wformat_extra_args, "unused arguments in $-style format");
1270 if (res.number_empty > 0 && res.number_non_literal == 0
1271 && res.number_other == 0)
1272 warning (OPT_Wformat_zero_length, "zero-length %s format string",
1273 format_types[info->format_type].name);
1275 if (res.number_wide > 0)
1276 warning (OPT_Wformat, "format is a wide character string");
1278 if (res.number_unterminated > 0)
1279 warning (OPT_Wformat, "unterminated format string");
1282 /* Callback from check_function_arguments_recurse to check a
1283 format string. FORMAT_TREE is the format parameter. ARG_NUM
1284 is the number of the format argument. CTX points to a
1285 format_check_context. */
1287 static void
1288 check_format_arg (void *ctx, tree format_tree,
1289 unsigned HOST_WIDE_INT arg_num)
1291 format_check_context *format_ctx = (format_check_context *) ctx;
1292 format_check_results *res = format_ctx->res;
1293 function_format_info *info = format_ctx->info;
1294 tree params = format_ctx->params;
1296 int format_length;
1297 HOST_WIDE_INT offset;
1298 const char *format_chars;
1299 tree array_size = 0;
1300 tree array_init;
1301 alloc_pool fwt_pool;
1303 if (integer_zerop (format_tree))
1305 /* Skip to first argument to check, so we can see if this format
1306 has any arguments (it shouldn't). */
1307 while (arg_num + 1 < info->first_arg_num)
1309 if (params == 0)
1310 return;
1311 params = TREE_CHAIN (params);
1312 ++arg_num;
1315 if (params == 0)
1316 res->number_other++;
1317 else
1318 res->number_extra_args++;
1320 return;
1323 offset = 0;
1324 if (TREE_CODE (format_tree) == POINTER_PLUS_EXPR)
1326 tree arg0, arg1;
1328 arg0 = TREE_OPERAND (format_tree, 0);
1329 arg1 = TREE_OPERAND (format_tree, 1);
1330 STRIP_NOPS (arg0);
1331 STRIP_NOPS (arg1);
1332 if (TREE_CODE (arg1) == INTEGER_CST)
1333 format_tree = arg0;
1334 else
1336 res->number_non_literal++;
1337 return;
1339 if (!host_integerp (arg1, 0)
1340 || (offset = tree_low_cst (arg1, 0)) < 0)
1342 res->number_non_literal++;
1343 return;
1346 if (TREE_CODE (format_tree) != ADDR_EXPR)
1348 res->number_non_literal++;
1349 return;
1351 format_tree = TREE_OPERAND (format_tree, 0);
1352 if (TREE_CODE (format_tree) == ARRAY_REF
1353 && host_integerp (TREE_OPERAND (format_tree, 1), 0)
1354 && (offset += tree_low_cst (TREE_OPERAND (format_tree, 1), 0)) >= 0)
1355 format_tree = TREE_OPERAND (format_tree, 0);
1356 if (TREE_CODE (format_tree) == VAR_DECL
1357 && TREE_CODE (TREE_TYPE (format_tree)) == ARRAY_TYPE
1358 && (array_init = decl_constant_value (format_tree)) != format_tree
1359 && TREE_CODE (array_init) == STRING_CST)
1361 /* Extract the string constant initializer. Note that this may include
1362 a trailing NUL character that is not in the array (e.g.
1363 const char a[3] = "foo";). */
1364 array_size = DECL_SIZE_UNIT (format_tree);
1365 format_tree = array_init;
1367 if (TREE_CODE (format_tree) != STRING_CST)
1369 res->number_non_literal++;
1370 return;
1372 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree))) != char_type_node)
1374 res->number_wide++;
1375 return;
1377 format_chars = TREE_STRING_POINTER (format_tree);
1378 format_length = TREE_STRING_LENGTH (format_tree);
1379 if (array_size != 0)
1381 /* Variable length arrays can't be initialized. */
1382 gcc_assert (TREE_CODE (array_size) == INTEGER_CST);
1384 if (host_integerp (array_size, 0))
1386 HOST_WIDE_INT array_size_value = TREE_INT_CST_LOW (array_size);
1387 if (array_size_value > 0
1388 && array_size_value == (int) array_size_value
1389 && format_length > array_size_value)
1390 format_length = array_size_value;
1393 if (offset)
1395 if (offset >= format_length)
1397 res->number_non_literal++;
1398 return;
1400 format_chars += offset;
1401 format_length -= offset;
1403 if (format_length < 1 || format_chars[--format_length] != 0)
1405 res->number_unterminated++;
1406 return;
1408 if (format_length == 0)
1410 res->number_empty++;
1411 return;
1414 /* Skip to first argument to check. */
1415 while (arg_num + 1 < info->first_arg_num)
1417 if (params == 0)
1418 return;
1419 params = TREE_CHAIN (params);
1420 ++arg_num;
1422 /* Provisionally increment res->number_other; check_format_info_main
1423 will decrement it if it finds there are extra arguments, but this way
1424 need not adjust it for every return. */
1425 res->number_other++;
1426 fwt_pool = create_alloc_pool ("format_wanted_type pool",
1427 sizeof (format_wanted_type), 10);
1428 check_format_info_main (res, info, format_chars, format_length,
1429 params, arg_num, fwt_pool);
1430 free_alloc_pool (fwt_pool);
1434 /* Do the main part of checking a call to a format function. FORMAT_CHARS
1435 is the NUL-terminated format string (which at this point may contain
1436 internal NUL characters); FORMAT_LENGTH is its length (excluding the
1437 terminating NUL character). ARG_NUM is one less than the number of
1438 the first format argument to check; PARAMS points to that format
1439 argument in the list of arguments. */
1441 static void
1442 check_format_info_main (format_check_results *res,
1443 function_format_info *info, const char *format_chars,
1444 int format_length, tree params,
1445 unsigned HOST_WIDE_INT arg_num, alloc_pool fwt_pool)
1447 const char *orig_format_chars = format_chars;
1448 tree first_fillin_param = params;
1450 const format_kind_info *fki = &format_types[info->format_type];
1451 const format_flag_spec *flag_specs = fki->flag_specs;
1452 const format_flag_pair *bad_flag_pairs = fki->bad_flag_pairs;
1454 /* -1 if no conversions taking an operand have been found; 0 if one has
1455 and it didn't use $; 1 if $ formats are in use. */
1456 int has_operand_number = -1;
1458 init_dollar_format_checking (info->first_arg_num, first_fillin_param);
1460 while (1)
1462 int i;
1463 int suppressed = FALSE;
1464 const char *length_chars = NULL;
1465 enum format_lengths length_chars_val = FMT_LEN_none;
1466 enum format_std_version length_chars_std = STD_C89;
1467 int format_char;
1468 tree cur_param;
1469 tree wanted_type;
1470 int main_arg_num = 0;
1471 tree main_arg_params = 0;
1472 enum format_std_version wanted_type_std;
1473 const char *wanted_type_name;
1474 format_wanted_type width_wanted_type;
1475 format_wanted_type precision_wanted_type;
1476 format_wanted_type main_wanted_type;
1477 format_wanted_type *first_wanted_type = NULL;
1478 format_wanted_type *last_wanted_type = NULL;
1479 const format_length_info *fli = NULL;
1480 const format_char_info *fci = NULL;
1481 char flag_chars[256];
1482 int alloc_flag = 0;
1483 int scalar_identity_flag = 0;
1484 const char *format_start = format_chars;
1485 if (*format_chars == 0)
1487 if (format_chars - orig_format_chars != format_length)
1488 warning (OPT_Wformat_contains_nul, "embedded %<\\0%> in format");
1489 if (info->first_arg_num != 0 && params != 0
1490 && has_operand_number <= 0)
1492 res->number_other--;
1493 res->number_extra_args++;
1495 if (has_operand_number > 0)
1496 finish_dollar_format_checking (res, fki->flags & (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK);
1497 return;
1499 if (*format_chars++ != '%')
1500 continue;
1501 if (*format_chars == 0)
1503 warning (OPT_Wformat, "spurious trailing %<%%%> in format");
1504 continue;
1506 if (*format_chars == '%')
1508 ++format_chars;
1509 continue;
1511 flag_chars[0] = 0;
1513 if ((fki->flags & (int) FMT_FLAG_USE_DOLLAR) && has_operand_number != 0)
1515 /* Possibly read a $ operand number at the start of the format.
1516 If one was previously used, one is required here. If one
1517 is not used here, we can't immediately conclude this is a
1518 format without them, since it could be printf %m or scanf %*. */
1519 int opnum;
1520 opnum = maybe_read_dollar_number (&format_chars, 0,
1521 first_fillin_param,
1522 &main_arg_params, fki);
1523 if (opnum == -1)
1524 return;
1525 else if (opnum > 0)
1527 has_operand_number = 1;
1528 main_arg_num = opnum + info->first_arg_num - 1;
1531 else if (fki->flags & FMT_FLAG_USE_DOLLAR)
1533 if (avoid_dollar_number (format_chars))
1534 return;
1537 /* Read any format flags, but do not yet validate them beyond removing
1538 duplicates, since in general validation depends on the rest of
1539 the format. */
1540 while (*format_chars != 0
1541 && strchr (fki->flag_chars, *format_chars) != 0)
1543 const format_flag_spec *s = get_flag_spec (flag_specs,
1544 *format_chars, NULL);
1545 if (strchr (flag_chars, *format_chars) != 0)
1547 warning (OPT_Wformat, "repeated %s in format", _(s->name));
1549 else
1551 i = strlen (flag_chars);
1552 flag_chars[i++] = *format_chars;
1553 flag_chars[i] = 0;
1555 if (s->skip_next_char)
1557 ++format_chars;
1558 if (*format_chars == 0)
1560 warning (OPT_Wformat, "missing fill character at end of strfmon format");
1561 return;
1564 ++format_chars;
1567 /* Read any format width, possibly * or *m$. */
1568 if (fki->width_char != 0)
1570 if (fki->width_type != NULL && *format_chars == '*')
1572 i = strlen (flag_chars);
1573 flag_chars[i++] = fki->width_char;
1574 flag_chars[i] = 0;
1575 /* "...a field width...may be indicated by an asterisk.
1576 In this case, an int argument supplies the field width..." */
1577 ++format_chars;
1578 if (has_operand_number != 0)
1580 int opnum;
1581 opnum = maybe_read_dollar_number (&format_chars,
1582 has_operand_number == 1,
1583 first_fillin_param,
1584 &params, fki);
1585 if (opnum == -1)
1586 return;
1587 else if (opnum > 0)
1589 has_operand_number = 1;
1590 arg_num = opnum + info->first_arg_num - 1;
1592 else
1593 has_operand_number = 0;
1595 else
1597 if (avoid_dollar_number (format_chars))
1598 return;
1600 if (info->first_arg_num != 0)
1602 if (params == 0)
1604 warning (OPT_Wformat, "too few arguments for format");
1605 return;
1607 cur_param = TREE_VALUE (params);
1608 if (has_operand_number <= 0)
1610 params = TREE_CHAIN (params);
1611 ++arg_num;
1613 width_wanted_type.wanted_type = *fki->width_type;
1614 width_wanted_type.wanted_type_name = NULL;
1615 width_wanted_type.pointer_count = 0;
1616 width_wanted_type.char_lenient_flag = 0;
1617 width_wanted_type.scalar_identity_flag = 0;
1618 width_wanted_type.writing_in_flag = 0;
1619 width_wanted_type.reading_from_flag = 0;
1620 width_wanted_type.name = _("field width");
1621 width_wanted_type.param = cur_param;
1622 width_wanted_type.arg_num = arg_num;
1623 width_wanted_type.next = NULL;
1624 if (last_wanted_type != 0)
1625 last_wanted_type->next = &width_wanted_type;
1626 if (first_wanted_type == 0)
1627 first_wanted_type = &width_wanted_type;
1628 last_wanted_type = &width_wanted_type;
1631 else
1633 /* Possibly read a numeric width. If the width is zero,
1634 we complain if appropriate. */
1635 int non_zero_width_char = FALSE;
1636 int found_width = FALSE;
1637 while (ISDIGIT (*format_chars))
1639 found_width = TRUE;
1640 if (*format_chars != '0')
1641 non_zero_width_char = TRUE;
1642 ++format_chars;
1644 if (found_width && !non_zero_width_char &&
1645 (fki->flags & (int) FMT_FLAG_ZERO_WIDTH_BAD))
1646 warning (OPT_Wformat, "zero width in %s format", fki->name);
1647 if (found_width)
1649 i = strlen (flag_chars);
1650 flag_chars[i++] = fki->width_char;
1651 flag_chars[i] = 0;
1656 /* Read any format left precision (must be a number, not *). */
1657 if (fki->left_precision_char != 0 && *format_chars == '#')
1659 ++format_chars;
1660 i = strlen (flag_chars);
1661 flag_chars[i++] = fki->left_precision_char;
1662 flag_chars[i] = 0;
1663 if (!ISDIGIT (*format_chars))
1664 warning (OPT_Wformat, "empty left precision in %s format", fki->name);
1665 while (ISDIGIT (*format_chars))
1666 ++format_chars;
1669 /* Read any format precision, possibly * or *m$. */
1670 if (fki->precision_char != 0 && *format_chars == '.')
1672 ++format_chars;
1673 i = strlen (flag_chars);
1674 flag_chars[i++] = fki->precision_char;
1675 flag_chars[i] = 0;
1676 if (fki->precision_type != NULL && *format_chars == '*')
1678 /* "...a...precision...may be indicated by an asterisk.
1679 In this case, an int argument supplies the...precision." */
1680 ++format_chars;
1681 if (has_operand_number != 0)
1683 int opnum;
1684 opnum = maybe_read_dollar_number (&format_chars,
1685 has_operand_number == 1,
1686 first_fillin_param,
1687 &params, fki);
1688 if (opnum == -1)
1689 return;
1690 else if (opnum > 0)
1692 has_operand_number = 1;
1693 arg_num = opnum + info->first_arg_num - 1;
1695 else
1696 has_operand_number = 0;
1698 else
1700 if (avoid_dollar_number (format_chars))
1701 return;
1703 if (info->first_arg_num != 0)
1705 if (params == 0)
1707 warning (OPT_Wformat, "too few arguments for format");
1708 return;
1710 cur_param = TREE_VALUE (params);
1711 if (has_operand_number <= 0)
1713 params = TREE_CHAIN (params);
1714 ++arg_num;
1716 precision_wanted_type.wanted_type = *fki->precision_type;
1717 precision_wanted_type.wanted_type_name = NULL;
1718 precision_wanted_type.pointer_count = 0;
1719 precision_wanted_type.char_lenient_flag = 0;
1720 precision_wanted_type.scalar_identity_flag = 0;
1721 precision_wanted_type.writing_in_flag = 0;
1722 precision_wanted_type.reading_from_flag = 0;
1723 precision_wanted_type.name = _("field precision");
1724 precision_wanted_type.param = cur_param;
1725 precision_wanted_type.arg_num = arg_num;
1726 precision_wanted_type.next = NULL;
1727 if (last_wanted_type != 0)
1728 last_wanted_type->next = &precision_wanted_type;
1729 if (first_wanted_type == 0)
1730 first_wanted_type = &precision_wanted_type;
1731 last_wanted_type = &precision_wanted_type;
1734 else
1736 if (!(fki->flags & (int) FMT_FLAG_EMPTY_PREC_OK)
1737 && !ISDIGIT (*format_chars))
1738 warning (OPT_Wformat, "empty precision in %s format", fki->name);
1739 while (ISDIGIT (*format_chars))
1740 ++format_chars;
1744 if (fki->alloc_char && fki->alloc_char == *format_chars)
1746 i = strlen (flag_chars);
1747 flag_chars[i++] = fki->alloc_char;
1748 flag_chars[i] = 0;
1749 format_chars++;
1752 /* Handle the scanf allocation kludge. */
1753 if (fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
1755 if (*format_chars == 'a' && !flag_isoc99)
1757 if (format_chars[1] == 's' || format_chars[1] == 'S'
1758 || format_chars[1] == '[')
1760 /* 'a' is used as a flag. */
1761 i = strlen (flag_chars);
1762 flag_chars[i++] = 'a';
1763 flag_chars[i] = 0;
1764 format_chars++;
1769 /* Read any length modifier, if this kind of format has them. */
1770 fli = fki->length_char_specs;
1771 length_chars = NULL;
1772 length_chars_val = FMT_LEN_none;
1773 length_chars_std = STD_C89;
1774 scalar_identity_flag = 0;
1775 if (fli)
1777 while (fli->name != 0
1778 && strncmp (fli->name, format_chars, strlen (fli->name)))
1779 fli++;
1780 if (fli->name != 0)
1782 format_chars += strlen (fli->name);
1783 if (fli->double_name != 0 && fli->name[0] == *format_chars)
1785 format_chars++;
1786 length_chars = fli->double_name;
1787 length_chars_val = fli->double_index;
1788 length_chars_std = fli->double_std;
1790 else
1792 length_chars = fli->name;
1793 length_chars_val = fli->index;
1794 length_chars_std = fli->std;
1795 scalar_identity_flag = fli->scalar_identity_flag;
1797 i = strlen (flag_chars);
1798 flag_chars[i++] = fki->length_code_char;
1799 flag_chars[i] = 0;
1801 if (pedantic)
1803 /* Warn if the length modifier is non-standard. */
1804 if (ADJ_STD (length_chars_std) > C_STD_VER)
1805 warning (OPT_Wformat,
1806 "%s does not support the %qs %s length modifier",
1807 C_STD_NAME (length_chars_std), length_chars,
1808 fki->name);
1812 /* Read any modifier (strftime E/O). */
1813 if (fki->modifier_chars != NULL)
1815 while (*format_chars != 0
1816 && strchr (fki->modifier_chars, *format_chars) != 0)
1818 if (strchr (flag_chars, *format_chars) != 0)
1820 const format_flag_spec *s = get_flag_spec (flag_specs,
1821 *format_chars, NULL);
1822 warning (OPT_Wformat, "repeated %s in format", _(s->name));
1824 else
1826 i = strlen (flag_chars);
1827 flag_chars[i++] = *format_chars;
1828 flag_chars[i] = 0;
1830 ++format_chars;
1834 format_char = *format_chars;
1835 if (format_char == 0
1836 || (!(fki->flags & (int) FMT_FLAG_FANCY_PERCENT_OK)
1837 && format_char == '%'))
1839 warning (OPT_Wformat, "conversion lacks type at end of format");
1840 continue;
1842 format_chars++;
1843 fci = fki->conversion_specs;
1844 while (fci->format_chars != 0
1845 && strchr (fci->format_chars, format_char) == 0)
1846 ++fci;
1847 if (fci->format_chars == 0)
1849 if (ISGRAPH (format_char))
1850 warning (OPT_Wformat, "unknown conversion type character %qc in format",
1851 format_char);
1852 else
1853 warning (OPT_Wformat, "unknown conversion type character 0x%x in format",
1854 format_char);
1855 continue;
1857 if (pedantic)
1859 if (ADJ_STD (fci->std) > C_STD_VER)
1860 warning (OPT_Wformat, "%s does not support the %<%%%c%> %s format",
1861 C_STD_NAME (fci->std), format_char, fki->name);
1864 /* Validate the individual flags used, removing any that are invalid. */
1866 int d = 0;
1867 for (i = 0; flag_chars[i] != 0; i++)
1869 const format_flag_spec *s = get_flag_spec (flag_specs,
1870 flag_chars[i], NULL);
1871 flag_chars[i - d] = flag_chars[i];
1872 if (flag_chars[i] == fki->length_code_char)
1873 continue;
1874 if (strchr (fci->flag_chars, flag_chars[i]) == 0)
1876 warning (OPT_Wformat, "%s used with %<%%%c%> %s format",
1877 _(s->name), format_char, fki->name);
1878 d++;
1879 continue;
1881 if (pedantic)
1883 const format_flag_spec *t;
1884 if (ADJ_STD (s->std) > C_STD_VER)
1885 warning (OPT_Wformat, "%s does not support %s",
1886 C_STD_NAME (s->std), _(s->long_name));
1887 t = get_flag_spec (flag_specs, flag_chars[i], fci->flags2);
1888 if (t != NULL && ADJ_STD (t->std) > ADJ_STD (s->std))
1890 const char *long_name = (t->long_name != NULL
1891 ? t->long_name
1892 : s->long_name);
1893 if (ADJ_STD (t->std) > C_STD_VER)
1894 warning (OPT_Wformat,
1895 "%s does not support %s with the %<%%%c%> %s format",
1896 C_STD_NAME (t->std), _(long_name),
1897 format_char, fki->name);
1901 flag_chars[i - d] = 0;
1904 if ((fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
1905 && strchr (flag_chars, 'a') != 0)
1906 alloc_flag = 1;
1907 if (fki->alloc_char && strchr (flag_chars, fki->alloc_char) != 0)
1908 alloc_flag = 1;
1910 if (fki->suppression_char
1911 && strchr (flag_chars, fki->suppression_char) != 0)
1912 suppressed = 1;
1914 /* Validate the pairs of flags used. */
1915 for (i = 0; bad_flag_pairs[i].flag_char1 != 0; i++)
1917 const format_flag_spec *s, *t;
1918 if (strchr (flag_chars, bad_flag_pairs[i].flag_char1) == 0)
1919 continue;
1920 if (strchr (flag_chars, bad_flag_pairs[i].flag_char2) == 0)
1921 continue;
1922 if (bad_flag_pairs[i].predicate != 0
1923 && strchr (fci->flags2, bad_flag_pairs[i].predicate) == 0)
1924 continue;
1925 s = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char1, NULL);
1926 t = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char2, NULL);
1927 if (bad_flag_pairs[i].ignored)
1929 if (bad_flag_pairs[i].predicate != 0)
1930 warning (OPT_Wformat,
1931 "%s ignored with %s and %<%%%c%> %s format",
1932 _(s->name), _(t->name), format_char,
1933 fki->name);
1934 else
1935 warning (OPT_Wformat, "%s ignored with %s in %s format",
1936 _(s->name), _(t->name), fki->name);
1938 else
1940 if (bad_flag_pairs[i].predicate != 0)
1941 warning (OPT_Wformat,
1942 "use of %s and %s together with %<%%%c%> %s format",
1943 _(s->name), _(t->name), format_char,
1944 fki->name);
1945 else
1946 warning (OPT_Wformat, "use of %s and %s together in %s format",
1947 _(s->name), _(t->name), fki->name);
1951 /* Give Y2K warnings. */
1952 if (warn_format_y2k)
1954 int y2k_level = 0;
1955 if (strchr (fci->flags2, '4') != 0)
1956 if (strchr (flag_chars, 'E') != 0)
1957 y2k_level = 3;
1958 else
1959 y2k_level = 2;
1960 else if (strchr (fci->flags2, '3') != 0)
1961 y2k_level = 3;
1962 else if (strchr (fci->flags2, '2') != 0)
1963 y2k_level = 2;
1964 if (y2k_level == 3)
1965 warning (OPT_Wformat_y2k, "%<%%%c%> yields only last 2 digits of "
1966 "year in some locales", format_char);
1967 else if (y2k_level == 2)
1968 warning (OPT_Wformat_y2k, "%<%%%c%> yields only last 2 digits of "
1969 "year", format_char);
1972 if (strchr (fci->flags2, '[') != 0)
1974 /* Skip over scan set, in case it happens to have '%' in it. */
1975 if (*format_chars == '^')
1976 ++format_chars;
1977 /* Find closing bracket; if one is hit immediately, then
1978 it's part of the scan set rather than a terminator. */
1979 if (*format_chars == ']')
1980 ++format_chars;
1981 while (*format_chars && *format_chars != ']')
1982 ++format_chars;
1983 if (*format_chars != ']')
1984 /* The end of the format string was reached. */
1985 warning (OPT_Wformat, "no closing %<]%> for %<%%[%> format");
1988 wanted_type = 0;
1989 wanted_type_name = 0;
1990 if (fki->flags & (int) FMT_FLAG_ARG_CONVERT)
1992 wanted_type = (fci->types[length_chars_val].type
1993 ? *fci->types[length_chars_val].type : 0);
1994 wanted_type_name = fci->types[length_chars_val].name;
1995 wanted_type_std = fci->types[length_chars_val].std;
1996 if (wanted_type == 0)
1998 warning (OPT_Wformat,
1999 "use of %qs length modifier with %qc type character",
2000 length_chars, format_char);
2001 /* Heuristic: skip one argument when an invalid length/type
2002 combination is encountered. */
2003 arg_num++;
2004 if (params == 0)
2006 warning (OPT_Wformat, "too few arguments for format");
2007 return;
2009 params = TREE_CHAIN (params);
2010 continue;
2012 else if (pedantic
2013 /* Warn if non-standard, provided it is more non-standard
2014 than the length and type characters that may already
2015 have been warned for. */
2016 && ADJ_STD (wanted_type_std) > ADJ_STD (length_chars_std)
2017 && ADJ_STD (wanted_type_std) > ADJ_STD (fci->std))
2019 if (ADJ_STD (wanted_type_std) > C_STD_VER)
2020 warning (OPT_Wformat,
2021 "%s does not support the %<%%%s%c%> %s format",
2022 C_STD_NAME (wanted_type_std), length_chars,
2023 format_char, fki->name);
2027 main_wanted_type.next = NULL;
2029 /* Finally. . .check type of argument against desired type! */
2030 if (info->first_arg_num == 0)
2031 continue;
2032 if ((fci->pointer_count == 0 && wanted_type == void_type_node)
2033 || suppressed)
2035 if (main_arg_num != 0)
2037 if (suppressed)
2038 warning (OPT_Wformat, "operand number specified with "
2039 "suppressed assignment");
2040 else
2041 warning (OPT_Wformat, "operand number specified for format "
2042 "taking no argument");
2045 else
2047 format_wanted_type *wanted_type_ptr;
2049 if (main_arg_num != 0)
2051 arg_num = main_arg_num;
2052 params = main_arg_params;
2054 else
2056 ++arg_num;
2057 if (has_operand_number > 0)
2059 warning (OPT_Wformat, "missing $ operand number in format");
2060 return;
2062 else
2063 has_operand_number = 0;
2066 wanted_type_ptr = &main_wanted_type;
2067 while (fci)
2069 if (params == 0)
2071 warning (OPT_Wformat, "too few arguments for format");
2072 return;
2075 cur_param = TREE_VALUE (params);
2076 params = TREE_CHAIN (params);
2078 wanted_type_ptr->wanted_type = wanted_type;
2079 wanted_type_ptr->wanted_type_name = wanted_type_name;
2080 wanted_type_ptr->pointer_count = fci->pointer_count + alloc_flag;
2081 wanted_type_ptr->char_lenient_flag = 0;
2082 if (strchr (fci->flags2, 'c') != 0)
2083 wanted_type_ptr->char_lenient_flag = 1;
2084 wanted_type_ptr->scalar_identity_flag = 0;
2085 if (scalar_identity_flag)
2086 wanted_type_ptr->scalar_identity_flag = 1;
2087 wanted_type_ptr->writing_in_flag = 0;
2088 wanted_type_ptr->reading_from_flag = 0;
2089 if (alloc_flag)
2090 wanted_type_ptr->writing_in_flag = 1;
2091 else
2093 if (strchr (fci->flags2, 'W') != 0)
2094 wanted_type_ptr->writing_in_flag = 1;
2095 if (strchr (fci->flags2, 'R') != 0)
2096 wanted_type_ptr->reading_from_flag = 1;
2098 wanted_type_ptr->name = NULL;
2099 wanted_type_ptr->param = cur_param;
2100 wanted_type_ptr->arg_num = arg_num;
2101 wanted_type_ptr->next = NULL;
2102 if (last_wanted_type != 0)
2103 last_wanted_type->next = wanted_type_ptr;
2104 if (first_wanted_type == 0)
2105 first_wanted_type = wanted_type_ptr;
2106 last_wanted_type = wanted_type_ptr;
2108 fci = fci->chain;
2109 if (fci)
2111 wanted_type_ptr = (format_wanted_type *)
2112 pool_alloc (fwt_pool);
2113 arg_num++;
2114 wanted_type = *fci->types[length_chars_val].type;
2115 wanted_type_name = fci->types[length_chars_val].name;
2120 if (first_wanted_type != 0)
2121 check_format_types (first_wanted_type, format_start,
2122 format_chars - format_start);
2127 /* Check the argument types from a single format conversion (possibly
2128 including width and precision arguments). */
2129 static void
2130 check_format_types (format_wanted_type *types, const char *format_start,
2131 int format_length)
2133 for (; types != 0; types = types->next)
2135 tree cur_param;
2136 tree cur_type;
2137 tree orig_cur_type;
2138 tree wanted_type;
2139 int arg_num;
2140 int i;
2141 int char_type_flag;
2142 cur_param = types->param;
2143 cur_type = TREE_TYPE (cur_param);
2144 if (cur_type == error_mark_node)
2145 continue;
2146 orig_cur_type = cur_type;
2147 char_type_flag = 0;
2148 wanted_type = types->wanted_type;
2149 arg_num = types->arg_num;
2151 /* The following should not occur here. */
2152 gcc_assert (wanted_type);
2153 gcc_assert (wanted_type != void_type_node || types->pointer_count);
2155 if (types->pointer_count == 0)
2156 wanted_type = lang_hooks.types.type_promotes_to (wanted_type);
2158 wanted_type = TYPE_MAIN_VARIANT (wanted_type);
2160 STRIP_NOPS (cur_param);
2162 /* Check the types of any additional pointer arguments
2163 that precede the "real" argument. */
2164 for (i = 0; i < types->pointer_count; ++i)
2166 if (TREE_CODE (cur_type) == POINTER_TYPE)
2168 cur_type = TREE_TYPE (cur_type);
2169 if (cur_type == error_mark_node)
2170 break;
2172 /* Check for writing through a NULL pointer. */
2173 if (types->writing_in_flag
2174 && i == 0
2175 && cur_param != 0
2176 && integer_zerop (cur_param))
2177 warning (OPT_Wformat, "writing through null pointer "
2178 "(argument %d)", arg_num);
2180 /* Check for reading through a NULL pointer. */
2181 if (types->reading_from_flag
2182 && i == 0
2183 && cur_param != 0
2184 && integer_zerop (cur_param))
2185 warning (OPT_Wformat, "reading through null pointer "
2186 "(argument %d)", arg_num);
2188 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
2189 cur_param = TREE_OPERAND (cur_param, 0);
2190 else
2191 cur_param = 0;
2193 /* See if this is an attempt to write into a const type with
2194 scanf or with printf "%n". Note: the writing in happens
2195 at the first indirection only, if for example
2196 void * const * is passed to scanf %p; passing
2197 const void ** is simply passing an incompatible type. */
2198 if (types->writing_in_flag
2199 && i == 0
2200 && (TYPE_READONLY (cur_type)
2201 || (cur_param != 0
2202 && (CONSTANT_CLASS_P (cur_param)
2203 || (DECL_P (cur_param)
2204 && TREE_READONLY (cur_param))))))
2205 warning (OPT_Wformat, "writing into constant object "
2206 "(argument %d)", arg_num);
2208 /* If there are extra type qualifiers beyond the first
2209 indirection, then this makes the types technically
2210 incompatible. */
2211 if (i > 0
2212 && pedantic
2213 && (TYPE_READONLY (cur_type)
2214 || TYPE_VOLATILE (cur_type)
2215 || TYPE_RESTRICT (cur_type)))
2216 warning (OPT_Wformat, "extra type qualifiers in format "
2217 "argument (argument %d)",
2218 arg_num);
2221 else
2223 format_type_warning (types->name, format_start, format_length,
2224 wanted_type, types->pointer_count,
2225 types->wanted_type_name, orig_cur_type,
2226 arg_num);
2227 break;
2231 if (i < types->pointer_count)
2232 continue;
2234 cur_type = TYPE_MAIN_VARIANT (cur_type);
2236 /* Check whether the argument type is a character type. This leniency
2237 only applies to certain formats, flagged with 'c'.
2239 if (types->char_lenient_flag)
2240 char_type_flag = (cur_type == char_type_node
2241 || cur_type == signed_char_type_node
2242 || cur_type == unsigned_char_type_node);
2244 /* Check the type of the "real" argument, if there's a type we want. */
2245 if (lang_hooks.types_compatible_p (wanted_type, cur_type))
2246 continue;
2247 /* If we want 'void *', allow any pointer type.
2248 (Anything else would already have got a warning.)
2249 With -pedantic, only allow pointers to void and to character
2250 types. */
2251 if (wanted_type == void_type_node
2252 && (!pedantic || (i == 1 && char_type_flag)))
2253 continue;
2254 /* Don't warn about differences merely in signedness, unless
2255 -pedantic. With -pedantic, warn if the type is a pointer
2256 target and not a character type, and for character types at
2257 a second level of indirection. */
2258 if (TREE_CODE (wanted_type) == INTEGER_TYPE
2259 && TREE_CODE (cur_type) == INTEGER_TYPE
2260 && (!pedantic || i == 0 || (i == 1 && char_type_flag))
2261 && (TYPE_UNSIGNED (wanted_type)
2262 ? wanted_type == c_common_unsigned_type (cur_type)
2263 : wanted_type == c_common_signed_type (cur_type)))
2264 continue;
2265 /* Likewise, "signed char", "unsigned char" and "char" are
2266 equivalent but the above test won't consider them equivalent. */
2267 if (wanted_type == char_type_node
2268 && (!pedantic || i < 2)
2269 && char_type_flag)
2270 continue;
2271 if (types->scalar_identity_flag
2272 && (TREE_CODE (cur_type) == TREE_CODE (wanted_type)
2273 || (INTEGRAL_TYPE_P (cur_type)
2274 && INTEGRAL_TYPE_P (wanted_type)))
2275 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type))
2276 continue;
2277 /* Now we have a type mismatch. */
2278 format_type_warning (types->name, format_start, format_length,
2279 wanted_type, types->pointer_count,
2280 types->wanted_type_name, orig_cur_type, arg_num);
2285 /* Give a warning about a format argument of different type from that
2286 expected. DESCR is a description such as "field precision", or
2287 NULL for an ordinary format. For an ordinary format, FORMAT_START
2288 points to where the format starts in the format string and
2289 FORMAT_LENGTH is its length. WANTED_TYPE is the type the argument
2290 should have after POINTER_COUNT pointer dereferences.
2291 WANTED_NAME_NAME is a possibly more friendly name of WANTED_TYPE,
2292 or NULL if the ordinary name of the type should be used. ARG_TYPE
2293 is the type of the actual argument. ARG_NUM is the number of that
2294 argument. */
2295 static void
2296 format_type_warning (const char *descr, const char *format_start,
2297 int format_length, tree wanted_type, int pointer_count,
2298 const char *wanted_type_name, tree arg_type, int arg_num)
2300 char *p;
2301 /* If ARG_TYPE is a typedef with a misleading name (for example,
2302 size_t but not the standard size_t expected by printf %zu), avoid
2303 printing the typedef name. */
2304 if (wanted_type_name
2305 && TYPE_NAME (arg_type)
2306 && TREE_CODE (TYPE_NAME (arg_type)) == TYPE_DECL
2307 && DECL_NAME (TYPE_NAME (arg_type))
2308 && !strcmp (wanted_type_name,
2309 lang_hooks.decl_printable_name (TYPE_NAME (arg_type), 2)))
2310 arg_type = TYPE_MAIN_VARIANT (arg_type);
2311 /* The format type and name exclude any '*' for pointers, so those
2312 must be formatted manually. For all the types we currently have,
2313 this is adequate, but formats taking pointers to functions or
2314 arrays would require the full type to be built up in order to
2315 print it with %T. */
2316 p = (char *) alloca (pointer_count + 2);
2317 if (pointer_count == 0)
2318 p[0] = 0;
2319 else if (c_dialect_cxx ())
2321 memset (p, '*', pointer_count);
2322 p[pointer_count] = 0;
2324 else
2326 p[0] = ' ';
2327 memset (p + 1, '*', pointer_count);
2328 p[pointer_count + 1] = 0;
2330 if (wanted_type_name)
2332 if (descr)
2333 warning (OPT_Wformat, "%s should have type %<%s%s%>, "
2334 "but argument %d has type %qT",
2335 descr, wanted_type_name, p, arg_num, arg_type);
2336 else
2337 warning (OPT_Wformat, "format %q.*s expects type %<%s%s%>, "
2338 "but argument %d has type %qT",
2339 format_length, format_start, wanted_type_name, p,
2340 arg_num, arg_type);
2342 else
2344 if (descr)
2345 warning (OPT_Wformat, "%s should have type %<%T%s%>, "
2346 "but argument %d has type %qT",
2347 descr, wanted_type, p, arg_num, arg_type);
2348 else
2349 warning (OPT_Wformat, "format %q.*s expects type %<%T%s%>, "
2350 "but argument %d has type %qT",
2351 format_length, format_start, wanted_type, p, arg_num, arg_type);
2356 /* Given a format_char_info array FCI, and a character C, this function
2357 returns the index into the conversion_specs where that specifier's
2358 data is located. The character must exist. */
2359 static unsigned int
2360 find_char_info_specifier_index (const format_char_info *fci, int c)
2362 unsigned i;
2364 for (i = 0; fci->format_chars; i++, fci++)
2365 if (strchr (fci->format_chars, c))
2366 return i;
2368 /* We shouldn't be looking for a non-existent specifier. */
2369 gcc_unreachable ();
2372 /* Given a format_length_info array FLI, and a character C, this
2373 function returns the index into the conversion_specs where that
2374 modifier's data is located. The character must exist. */
2375 static unsigned int
2376 find_length_info_modifier_index (const format_length_info *fli, int c)
2378 unsigned i;
2380 for (i = 0; fli->name; i++, fli++)
2381 if (strchr (fli->name, c))
2382 return i;
2384 /* We shouldn't be looking for a non-existent modifier. */
2385 gcc_unreachable ();
2388 /* Determine the type of HOST_WIDE_INT in the code being compiled for
2389 use in GCC's __asm_fprintf__ custom format attribute. You must
2390 have set dynamic_format_types before calling this function. */
2391 static void
2392 init_dynamic_asm_fprintf_info (void)
2394 static tree hwi;
2396 if (!hwi)
2398 format_length_info *new_asm_fprintf_length_specs;
2399 unsigned int i;
2401 /* Find the underlying type for HOST_WIDE_INT. For the %w
2402 length modifier to work, one must have issued: "typedef
2403 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2404 prior to using that modifier. */
2405 hwi = maybe_get_identifier ("__gcc_host_wide_int__");
2406 if (!hwi)
2408 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2409 return;
2411 hwi = identifier_global_value (hwi);
2412 if (!hwi || TREE_CODE (hwi) != TYPE_DECL)
2414 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2415 return;
2417 hwi = DECL_ORIGINAL_TYPE (hwi);
2418 gcc_assert (hwi);
2419 if (hwi != long_integer_type_node && hwi != long_long_integer_type_node)
2421 error ("%<__gcc_host_wide_int__%> is not defined as %<long%>"
2422 " or %<long long%>");
2423 return;
2426 /* Create a new (writable) copy of asm_fprintf_length_specs. */
2427 new_asm_fprintf_length_specs = (format_length_info *)
2428 xmemdup (asm_fprintf_length_specs,
2429 sizeof (asm_fprintf_length_specs),
2430 sizeof (asm_fprintf_length_specs));
2432 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2433 i = find_length_info_modifier_index (new_asm_fprintf_length_specs, 'w');
2434 if (hwi == long_integer_type_node)
2435 new_asm_fprintf_length_specs[i].index = FMT_LEN_l;
2436 else if (hwi == long_long_integer_type_node)
2437 new_asm_fprintf_length_specs[i].index = FMT_LEN_ll;
2438 else
2439 gcc_unreachable ();
2441 /* Assign the new data for use. */
2442 dynamic_format_types[asm_fprintf_format_type].length_char_specs =
2443 new_asm_fprintf_length_specs;
2447 /* Determine the type of a "locus" in the code being compiled for use
2448 in GCC's __gcc_gfc__ custom format attribute. You must have set
2449 dynamic_format_types before calling this function. */
2450 static void
2451 init_dynamic_gfc_info (void)
2453 static tree locus;
2455 if (!locus)
2457 static format_char_info *gfc_fci;
2459 /* For the GCC __gcc_gfc__ custom format specifier to work, one
2460 must have declared 'locus' prior to using this attribute. If
2461 we haven't seen this declarations then you shouldn't use the
2462 specifier requiring that type. */
2463 if ((locus = maybe_get_identifier ("locus")))
2465 locus = identifier_global_value (locus);
2466 if (locus)
2468 if (TREE_CODE (locus) != TYPE_DECL
2469 || TREE_TYPE (locus) == error_mark_node)
2471 error ("%<locus%> is not defined as a type");
2472 locus = 0;
2474 else
2475 locus = TREE_TYPE (locus);
2479 /* Assign the new data for use. */
2481 /* Handle the __gcc_gfc__ format specifics. */
2482 if (!gfc_fci)
2483 dynamic_format_types[gcc_gfc_format_type].conversion_specs =
2484 gfc_fci = (format_char_info *)
2485 xmemdup (gcc_gfc_char_table,
2486 sizeof (gcc_gfc_char_table),
2487 sizeof (gcc_gfc_char_table));
2488 if (locus)
2490 const unsigned i = find_char_info_specifier_index (gfc_fci, 'L');
2491 gfc_fci[i].types[0].type = &locus;
2492 gfc_fci[i].pointer_count = 1;
2497 /* Determine the types of "tree" and "location_t" in the code being
2498 compiled for use in GCC's diagnostic custom format attributes. You
2499 must have set dynamic_format_types before calling this function. */
2500 static void
2501 init_dynamic_diag_info (void)
2503 static tree t, loc, hwi;
2505 if (!loc || !t || !hwi)
2507 static format_char_info *diag_fci, *tdiag_fci, *cdiag_fci, *cxxdiag_fci;
2508 static format_length_info *diag_ls;
2509 unsigned int i;
2511 /* For the GCC-diagnostics custom format specifiers to work, one
2512 must have declared 'tree' and/or 'location_t' prior to using
2513 those attributes. If we haven't seen these declarations then
2514 you shouldn't use the specifiers requiring these types.
2515 However we don't force a hard ICE because we may see only one
2516 or the other type. */
2517 if ((loc = maybe_get_identifier ("location_t")))
2519 loc = identifier_global_value (loc);
2520 if (loc)
2522 if (TREE_CODE (loc) != TYPE_DECL)
2524 error ("%<location_t%> is not defined as a type");
2525 loc = 0;
2527 else
2528 loc = TREE_TYPE (loc);
2532 /* We need to grab the underlying 'union tree_node' so peek into
2533 an extra type level. */
2534 if ((t = maybe_get_identifier ("tree")))
2536 t = identifier_global_value (t);
2537 if (t)
2539 if (TREE_CODE (t) != TYPE_DECL)
2541 error ("%<tree%> is not defined as a type");
2542 t = 0;
2544 else if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
2546 error ("%<tree%> is not defined as a pointer type");
2547 t = 0;
2549 else
2550 t = TREE_TYPE (TREE_TYPE (t));
2554 /* Find the underlying type for HOST_WIDE_INT. For the %w
2555 length modifier to work, one must have issued: "typedef
2556 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2557 prior to using that modifier. */
2558 if ((hwi = maybe_get_identifier ("__gcc_host_wide_int__")))
2560 hwi = identifier_global_value (hwi);
2561 if (hwi)
2563 if (TREE_CODE (hwi) != TYPE_DECL)
2565 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2566 hwi = 0;
2568 else
2570 hwi = DECL_ORIGINAL_TYPE (hwi);
2571 gcc_assert (hwi);
2572 if (hwi != long_integer_type_node
2573 && hwi != long_long_integer_type_node)
2575 error ("%<__gcc_host_wide_int__%> is not defined"
2576 " as %<long%> or %<long long%>");
2577 hwi = 0;
2583 /* Assign the new data for use. */
2585 /* All the GCC diag formats use the same length specs. */
2586 if (!diag_ls)
2587 dynamic_format_types[gcc_diag_format_type].length_char_specs =
2588 dynamic_format_types[gcc_tdiag_format_type].length_char_specs =
2589 dynamic_format_types[gcc_cdiag_format_type].length_char_specs =
2590 dynamic_format_types[gcc_cxxdiag_format_type].length_char_specs =
2591 diag_ls = (format_length_info *)
2592 xmemdup (gcc_diag_length_specs,
2593 sizeof (gcc_diag_length_specs),
2594 sizeof (gcc_diag_length_specs));
2595 if (hwi)
2597 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2598 i = find_length_info_modifier_index (diag_ls, 'w');
2599 if (hwi == long_integer_type_node)
2600 diag_ls[i].index = FMT_LEN_l;
2601 else if (hwi == long_long_integer_type_node)
2602 diag_ls[i].index = FMT_LEN_ll;
2603 else
2604 gcc_unreachable ();
2607 /* Handle the __gcc_diag__ format specifics. */
2608 if (!diag_fci)
2609 dynamic_format_types[gcc_diag_format_type].conversion_specs =
2610 diag_fci = (format_char_info *)
2611 xmemdup (gcc_diag_char_table,
2612 sizeof (gcc_diag_char_table),
2613 sizeof (gcc_diag_char_table));
2614 if (t)
2616 i = find_char_info_specifier_index (diag_fci, 'K');
2617 diag_fci[i].types[0].type = &t;
2618 diag_fci[i].pointer_count = 1;
2621 /* Handle the __gcc_tdiag__ format specifics. */
2622 if (!tdiag_fci)
2623 dynamic_format_types[gcc_tdiag_format_type].conversion_specs =
2624 tdiag_fci = (format_char_info *)
2625 xmemdup (gcc_tdiag_char_table,
2626 sizeof (gcc_tdiag_char_table),
2627 sizeof (gcc_tdiag_char_table));
2628 if (t)
2630 /* All specifiers taking a tree share the same struct. */
2631 i = find_char_info_specifier_index (tdiag_fci, 'D');
2632 tdiag_fci[i].types[0].type = &t;
2633 tdiag_fci[i].pointer_count = 1;
2634 i = find_char_info_specifier_index (tdiag_fci, 'K');
2635 tdiag_fci[i].types[0].type = &t;
2636 tdiag_fci[i].pointer_count = 1;
2639 /* Handle the __gcc_cdiag__ format specifics. */
2640 if (!cdiag_fci)
2641 dynamic_format_types[gcc_cdiag_format_type].conversion_specs =
2642 cdiag_fci = (format_char_info *)
2643 xmemdup (gcc_cdiag_char_table,
2644 sizeof (gcc_cdiag_char_table),
2645 sizeof (gcc_cdiag_char_table));
2646 if (t)
2648 /* All specifiers taking a tree share the same struct. */
2649 i = find_char_info_specifier_index (cdiag_fci, 'D');
2650 cdiag_fci[i].types[0].type = &t;
2651 cdiag_fci[i].pointer_count = 1;
2652 i = find_char_info_specifier_index (cdiag_fci, 'K');
2653 cdiag_fci[i].types[0].type = &t;
2654 cdiag_fci[i].pointer_count = 1;
2657 /* Handle the __gcc_cxxdiag__ format specifics. */
2658 if (!cxxdiag_fci)
2659 dynamic_format_types[gcc_cxxdiag_format_type].conversion_specs =
2660 cxxdiag_fci = (format_char_info *)
2661 xmemdup (gcc_cxxdiag_char_table,
2662 sizeof (gcc_cxxdiag_char_table),
2663 sizeof (gcc_cxxdiag_char_table));
2664 if (t)
2666 /* All specifiers taking a tree share the same struct. */
2667 i = find_char_info_specifier_index (cxxdiag_fci, 'D');
2668 cxxdiag_fci[i].types[0].type = &t;
2669 cxxdiag_fci[i].pointer_count = 1;
2670 i = find_char_info_specifier_index (cxxdiag_fci, 'K');
2671 cxxdiag_fci[i].types[0].type = &t;
2672 cxxdiag_fci[i].pointer_count = 1;
2677 #ifdef TARGET_FORMAT_TYPES
2678 extern const format_kind_info TARGET_FORMAT_TYPES[];
2679 #endif
2681 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
2682 extern const target_ovr_attr TARGET_OVERRIDES_FORMAT_ATTRIBUTES[];
2683 #endif
2684 #ifdef TARGET_OVERRIDES_FORMAT_INIT
2685 extern void TARGET_OVERRIDES_FORMAT_INIT (void);
2686 #endif
2688 /* Attributes such as "printf" are equivalent to those such as
2689 "gnu_printf" unless this is overridden by a target. */
2690 static const target_ovr_attr gnu_target_overrides_format_attributes[] =
2692 { "gnu_printf", "printf" },
2693 { "gnu_scanf", "scanf" },
2694 { "gnu_strftime", "strftime" },
2695 { "gnu_strfmon", "strfmon" },
2696 { NULL, NULL }
2699 /* Translate to unified attribute name. This is used in decode_format_type and
2700 decode_format_attr. In attr_name the user specified argument is passed. It
2701 returns the unified format name from TARGET_OVERRIDES_FORMAT_ATTRIBUTES
2702 or the attr_name passed to this function, if there is no matching entry. */
2703 static const char *
2704 convert_format_name_to_system_name (const char *attr_name)
2706 int i;
2708 if (attr_name == NULL || *attr_name == 0
2709 || strncmp (attr_name, "gcc_", 4) == 0)
2710 return attr_name;
2711 #ifdef TARGET_OVERRIDES_FORMAT_INIT
2712 TARGET_OVERRIDES_FORMAT_INIT ();
2713 #endif
2715 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
2716 /* Check if format attribute is overridden by target. */
2717 if (TARGET_OVERRIDES_FORMAT_ATTRIBUTES != NULL
2718 && TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT > 0)
2720 for (i = 0; i < TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT; ++i)
2722 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_src,
2723 attr_name))
2724 return attr_name;
2725 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_dst,
2726 attr_name))
2727 return TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_src;
2730 #endif
2731 /* Otherwise default to gnu format. */
2732 for (i = 0;
2733 gnu_target_overrides_format_attributes[i].named_attr_src != NULL;
2734 ++i)
2736 if (cmp_attribs (gnu_target_overrides_format_attributes[i].named_attr_src,
2737 attr_name))
2738 return attr_name;
2739 if (cmp_attribs (gnu_target_overrides_format_attributes[i].named_attr_dst,
2740 attr_name))
2741 return gnu_target_overrides_format_attributes[i].named_attr_src;
2744 return attr_name;
2747 /* Return true if TATTR_NAME and ATTR_NAME are the same format attribute,
2748 counting "name" and "__name__" as the same, false otherwise. */
2749 static bool
2750 cmp_attribs (const char *tattr_name, const char *attr_name)
2752 int alen = strlen (attr_name);
2753 int slen = (tattr_name ? strlen (tattr_name) : 0);
2754 if (alen > 4 && attr_name[0] == '_' && attr_name[1] == '_'
2755 && attr_name[alen - 1] == '_' && attr_name[alen - 2] == '_')
2757 attr_name += 2;
2758 alen -= 4;
2760 if (alen != slen || strncmp (tattr_name, attr_name, alen) != 0)
2761 return false;
2762 return true;
2765 /* Handle a "format" attribute; arguments as in
2766 struct attribute_spec.handler. */
2767 tree
2768 handle_format_attribute (tree *node, tree ARG_UNUSED (name), tree args,
2769 int flags, bool *no_add_attrs)
2771 tree type = *node;
2772 function_format_info info;
2773 tree argument;
2775 #ifdef TARGET_FORMAT_TYPES
2776 /* If the target provides additional format types, we need to
2777 add them to FORMAT_TYPES at first use. */
2778 if (TARGET_FORMAT_TYPES != NULL && !dynamic_format_types)
2780 dynamic_format_types = XNEWVEC (format_kind_info,
2781 n_format_types + TARGET_N_FORMAT_TYPES);
2782 memcpy (dynamic_format_types, format_types_orig,
2783 sizeof (format_types_orig));
2784 memcpy (&dynamic_format_types[n_format_types], TARGET_FORMAT_TYPES,
2785 TARGET_N_FORMAT_TYPES * sizeof (dynamic_format_types[0]));
2787 format_types = dynamic_format_types;
2788 n_format_types += TARGET_N_FORMAT_TYPES;
2790 #endif
2792 if (!decode_format_attr (args, &info, 0))
2794 *no_add_attrs = true;
2795 return NULL_TREE;
2798 argument = TYPE_ARG_TYPES (type);
2799 if (argument)
2801 if (!check_format_string (argument, info.format_num, flags,
2802 no_add_attrs))
2803 return NULL_TREE;
2805 if (info.first_arg_num != 0)
2807 unsigned HOST_WIDE_INT arg_num = 1;
2809 /* Verify that first_arg_num points to the last arg,
2810 the ... */
2811 while (argument)
2812 arg_num++, argument = TREE_CHAIN (argument);
2814 if (arg_num != info.first_arg_num)
2816 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
2817 error ("args to be formatted is not %<...%>");
2818 *no_add_attrs = true;
2819 return NULL_TREE;
2824 /* Check if this is a strftime variant. Just for this variant
2825 FMT_FLAG_ARG_CONVERT is not set. */
2826 if ((format_types[info.format_type].flags & (int) FMT_FLAG_ARG_CONVERT) == 0
2827 && info.first_arg_num != 0)
2829 error ("strftime formats cannot format arguments");
2830 *no_add_attrs = true;
2831 return NULL_TREE;
2834 /* If this is a custom GCC-internal format type, we have to
2835 initialize certain bits at runtime. */
2836 if (info.format_type == asm_fprintf_format_type
2837 || info.format_type == gcc_gfc_format_type
2838 || info.format_type == gcc_diag_format_type
2839 || info.format_type == gcc_tdiag_format_type
2840 || info.format_type == gcc_cdiag_format_type
2841 || info.format_type == gcc_cxxdiag_format_type)
2843 /* Our first time through, we have to make sure that our
2844 format_type data is allocated dynamically and is modifiable. */
2845 if (!dynamic_format_types)
2846 format_types = dynamic_format_types = (format_kind_info *)
2847 xmemdup (format_types_orig, sizeof (format_types_orig),
2848 sizeof (format_types_orig));
2850 /* If this is format __asm_fprintf__, we have to initialize
2851 GCC's notion of HOST_WIDE_INT for checking %wd. */
2852 if (info.format_type == asm_fprintf_format_type)
2853 init_dynamic_asm_fprintf_info ();
2854 /* If this is format __gcc_gfc__, we have to initialize GCC's
2855 notion of 'locus' at runtime for %L. */
2856 else if (info.format_type == gcc_gfc_format_type)
2857 init_dynamic_gfc_info ();
2858 /* If this is one of the diagnostic attributes, then we have to
2859 initialize 'location_t' and 'tree' at runtime. */
2860 else if (info.format_type == gcc_diag_format_type
2861 || info.format_type == gcc_tdiag_format_type
2862 || info.format_type == gcc_cdiag_format_type
2863 || info.format_type == gcc_cxxdiag_format_type)
2864 init_dynamic_diag_info ();
2865 else
2866 gcc_unreachable ();
2869 return NULL_TREE;