* loop-iv.c (replace_single_def_regs, replace_in_expr): New static
[official-gcc/alias-decl.git] / gcc / c-format.c
blob24a292fe33acc320b536b816e4ef4bc61a2c5c42
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 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "c-common.h"
28 #include "toplev.h"
29 #include "intl.h"
30 #include "diagnostic.h"
31 #include "langhooks.h"
32 #include "c-format.h"
33 #include "alloc-pool.h"
35 /* Set format warning options according to a -Wformat=n option. */
37 void
38 set_Wformat (int setting)
40 warn_format = setting;
41 warn_format_extra_args = setting;
42 warn_format_zero_length = setting;
43 warn_format_contains_nul = setting;
44 if (setting != 1)
46 warn_format_nonliteral = setting;
47 warn_format_security = setting;
48 warn_format_y2k = setting;
50 /* Make sure not to disable -Wnonnull if -Wformat=0 is specified. */
51 if (setting)
52 warn_nonnull = setting;
56 /* Handle attributes associated with format checking. */
58 /* This must be in the same order as format_types, except for
59 format_type_error. Target-specific format types do not have
60 matching enum values. */
61 enum format_type { printf_format_type, asm_fprintf_format_type,
62 gcc_diag_format_type, gcc_tdiag_format_type,
63 gcc_cdiag_format_type,
64 gcc_cxxdiag_format_type, gcc_gfc_format_type,
65 format_type_error = -1};
67 typedef struct function_format_info
69 int format_type; /* type of format (printf, scanf, etc.) */
70 unsigned HOST_WIDE_INT format_num; /* number of format argument */
71 unsigned HOST_WIDE_INT first_arg_num; /* number of first arg (zero for varargs) */
72 } function_format_info;
74 static bool decode_format_attr (tree, function_format_info *, int);
75 static int decode_format_type (const char *);
77 static bool check_format_string (tree argument,
78 unsigned HOST_WIDE_INT format_num,
79 int flags, bool *no_add_attrs);
80 static bool get_constant (tree expr, unsigned HOST_WIDE_INT *value,
81 int validated_p);
82 static const char *convert_format_name_to_system_name (const char *attr_name);
83 static bool cmp_attribs (const char *tattr_name, const char *attr_name);
85 /* Handle a "format_arg" attribute; arguments as in
86 struct attribute_spec.handler. */
87 tree
88 handle_format_arg_attribute (tree *node, tree ARG_UNUSED (name),
89 tree args, int flags, bool *no_add_attrs)
91 tree type = *node;
92 tree format_num_expr = TREE_VALUE (args);
93 unsigned HOST_WIDE_INT format_num = 0;
94 tree argument;
96 if (!get_constant (format_num_expr, &format_num, 0))
98 error ("format string has invalid operand number");
99 *no_add_attrs = true;
100 return NULL_TREE;
103 argument = TYPE_ARG_TYPES (type);
104 if (argument)
106 if (!check_format_string (argument, format_num, flags, no_add_attrs))
107 return NULL_TREE;
110 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
111 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
112 != char_type_node))
114 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
115 error ("function does not return string type");
116 *no_add_attrs = true;
117 return NULL_TREE;
120 return NULL_TREE;
123 /* Verify that the format_num argument is actually a string, in case
124 the format attribute is in error. */
125 static bool
126 check_format_string (tree argument, unsigned HOST_WIDE_INT format_num,
127 int flags, bool *no_add_attrs)
129 unsigned HOST_WIDE_INT i;
131 for (i = 1; i != format_num; i++)
133 if (argument == 0)
134 break;
135 argument = TREE_CHAIN (argument);
138 if (!argument
139 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
140 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
141 != char_type_node))
143 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
144 error ("format string argument not a string type");
145 *no_add_attrs = true;
146 return false;
149 return true;
152 /* Verify EXPR is a constant, and store its value.
153 If validated_p is true there should be no errors.
154 Returns true on success, false otherwise. */
155 static bool
156 get_constant (tree expr, unsigned HOST_WIDE_INT *value, int validated_p)
158 if (TREE_CODE (expr) != INTEGER_CST || TREE_INT_CST_HIGH (expr) != 0)
160 gcc_assert (!validated_p);
161 return false;
164 *value = TREE_INT_CST_LOW (expr);
166 return true;
169 /* Decode the arguments to a "format" attribute into a
170 function_format_info structure. It is already known that the list
171 is of the right length. If VALIDATED_P is true, then these
172 attributes have already been validated and must not be erroneous;
173 if false, it will give an error message. Returns true if the
174 attributes are successfully decoded, false otherwise. */
176 static bool
177 decode_format_attr (tree args, function_format_info *info, int validated_p)
179 tree format_type_id = TREE_VALUE (args);
180 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
181 tree first_arg_num_expr
182 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
184 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
186 gcc_assert (!validated_p);
187 error ("unrecognized format specifier");
188 return false;
190 else
192 const char *p = IDENTIFIER_POINTER (format_type_id);
194 p = convert_format_name_to_system_name (p);
196 info->format_type = decode_format_type (p);
198 if (info->format_type == format_type_error)
200 gcc_assert (!validated_p);
201 warning (OPT_Wformat, "%qE is an unrecognized format function type",
202 format_type_id);
203 return false;
207 if (!get_constant (format_num_expr, &info->format_num, validated_p))
209 error ("format string has invalid operand number");
210 return false;
213 if (!get_constant (first_arg_num_expr, &info->first_arg_num, validated_p))
215 error ("%<...%> has invalid operand number");
216 return false;
219 if (info->first_arg_num != 0 && info->first_arg_num <= info->format_num)
221 gcc_assert (!validated_p);
222 error ("format string argument follows the args to be formatted");
223 return false;
226 return true;
229 /* Check a call to a format function against a parameter list. */
231 /* The C standard version C++ is treated as equivalent to
232 or inheriting from, for the purpose of format features supported. */
233 #define CPLUSPLUS_STD_VER STD_C94
234 /* The C standard version we are checking formats against when pedantic. */
235 #define C_STD_VER ((int) (c_dialect_cxx () \
236 ? CPLUSPLUS_STD_VER \
237 : (flag_isoc99 \
238 ? STD_C99 \
239 : (flag_isoc94 ? STD_C94 : STD_C89))))
240 /* The name to give to the standard version we are warning about when
241 pedantic. FEATURE_VER is the version in which the feature warned out
242 appeared, which is higher than C_STD_VER. */
243 #define C_STD_NAME(FEATURE_VER) (c_dialect_cxx () \
244 ? "ISO C++" \
245 : ((FEATURE_VER) == STD_EXT \
246 ? "ISO C" \
247 : "ISO C90"))
248 /* Adjust a C standard version, which may be STD_C9L, to account for
249 -Wno-long-long. Returns other standard versions unchanged. */
250 #define ADJ_STD(VER) ((int) ((VER) == STD_C9L \
251 ? (warn_long_long ? STD_C99 : STD_C89) \
252 : (VER)))
254 /* Structure describing details of a type expected in format checking,
255 and the type to check against it. */
256 typedef struct format_wanted_type
258 /* The type wanted. */
259 tree wanted_type;
260 /* The name of this type to use in diagnostics. */
261 const char *wanted_type_name;
262 /* The level of indirection through pointers at which this type occurs. */
263 int pointer_count;
264 /* Whether, when pointer_count is 1, to allow any character type when
265 pedantic, rather than just the character or void type specified. */
266 int char_lenient_flag;
267 /* Whether the argument, dereferenced once, is written into and so the
268 argument must not be a pointer to a const-qualified type. */
269 int writing_in_flag;
270 /* Whether the argument, dereferenced once, is read from and so
271 must not be a NULL pointer. */
272 int reading_from_flag;
273 /* If warnings should be of the form "field precision should have
274 type 'int'", the name to use (in this case "field precision"),
275 otherwise NULL, for "format expects type 'long'" type
276 messages. */
277 const char *name;
278 /* The actual parameter to check against the wanted type. */
279 tree param;
280 /* The argument number of that parameter. */
281 int arg_num;
282 /* The next type to check for this format conversion, or NULL if none. */
283 struct format_wanted_type *next;
284 } format_wanted_type;
286 /* Convenience macro for format_length_info meaning unused. */
287 #define NO_FMT NULL, FMT_LEN_none, STD_C89
289 static const format_length_info printf_length_specs[] =
291 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
292 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L },
293 { "q", FMT_LEN_ll, STD_EXT, NO_FMT },
294 { "L", FMT_LEN_L, STD_C89, NO_FMT },
295 { "z", FMT_LEN_z, STD_C99, NO_FMT },
296 { "Z", FMT_LEN_z, STD_EXT, NO_FMT },
297 { "t", FMT_LEN_t, STD_C99, NO_FMT },
298 { "j", FMT_LEN_j, STD_C99, NO_FMT },
299 { "H", FMT_LEN_H, STD_EXT, NO_FMT },
300 { "D", FMT_LEN_D, STD_EXT, "DD", FMT_LEN_DD, STD_EXT },
301 { NO_FMT, NO_FMT }
304 /* Length specifiers valid for asm_fprintf. */
305 static const format_length_info asm_fprintf_length_specs[] =
307 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89 },
308 { "w", FMT_LEN_none, STD_C89, NO_FMT },
309 { NO_FMT, NO_FMT }
312 /* Length specifiers valid for GCC diagnostics. */
313 static const format_length_info gcc_diag_length_specs[] =
315 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89 },
316 { "w", FMT_LEN_none, STD_C89, NO_FMT },
317 { NO_FMT, NO_FMT }
320 /* The custom diagnostics all accept the same length specifiers. */
321 #define gcc_tdiag_length_specs gcc_diag_length_specs
322 #define gcc_cdiag_length_specs gcc_diag_length_specs
323 #define gcc_cxxdiag_length_specs gcc_diag_length_specs
325 /* This differs from printf_length_specs only in that "Z" is not accepted. */
326 static const format_length_info scanf_length_specs[] =
328 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
329 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L },
330 { "q", FMT_LEN_ll, STD_EXT, NO_FMT },
331 { "L", FMT_LEN_L, STD_C89, NO_FMT },
332 { "z", FMT_LEN_z, STD_C99, NO_FMT },
333 { "t", FMT_LEN_t, STD_C99, NO_FMT },
334 { "j", FMT_LEN_j, STD_C99, NO_FMT },
335 { "H", FMT_LEN_H, STD_EXT, NO_FMT },
336 { "D", FMT_LEN_D, STD_EXT, "DD", FMT_LEN_DD, STD_EXT },
337 { NO_FMT, NO_FMT }
341 /* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings
342 make no sense for a format type not part of any C standard version. */
343 static const format_length_info strfmon_length_specs[] =
345 /* A GNU extension. */
346 { "L", FMT_LEN_L, STD_C89, NO_FMT },
347 { NO_FMT, NO_FMT }
351 /* For now, the Fortran front-end routines only use l as length modifier. */
352 static const format_length_info gcc_gfc_length_specs[] =
354 { "l", FMT_LEN_l, STD_C89, NO_FMT },
355 { NO_FMT, NO_FMT }
359 static const format_flag_spec printf_flag_specs[] =
361 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
362 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
363 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
364 { '0', 0, 0, N_("'0' flag"), N_("the '0' 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_EXT },
367 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' printf flag"), STD_EXT },
368 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
369 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
370 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
371 { 0, 0, 0, NULL, NULL, STD_C89 }
375 static const format_flag_pair printf_flag_pairs[] =
377 { ' ', '+', 1, 0 },
378 { '0', '-', 1, 0 },
379 { '0', 'p', 1, 'i' },
380 { 0, 0, 0, 0 }
383 static const format_flag_spec asm_fprintf_flag_specs[] =
385 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
386 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
387 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
388 { '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
389 { '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
390 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
391 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
392 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
393 { 0, 0, 0, NULL, NULL, STD_C89 }
396 static const format_flag_pair asm_fprintf_flag_pairs[] =
398 { ' ', '+', 1, 0 },
399 { '0', '-', 1, 0 },
400 { '0', 'p', 1, 'i' },
401 { 0, 0, 0, 0 }
404 static const format_flag_pair gcc_diag_flag_pairs[] =
406 { 0, 0, 0, 0 }
409 #define gcc_tdiag_flag_pairs gcc_diag_flag_pairs
410 #define gcc_cdiag_flag_pairs gcc_diag_flag_pairs
411 #define gcc_cxxdiag_flag_pairs gcc_diag_flag_pairs
413 static const format_flag_pair gcc_gfc_flag_pairs[] =
415 { 0, 0, 0, 0 }
418 static const format_flag_spec gcc_diag_flag_specs[] =
420 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
421 { 'q', 0, 0, N_("'q' flag"), N_("the 'q' diagnostic flag"), STD_C89 },
422 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
423 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
424 { 0, 0, 0, NULL, NULL, STD_C89 }
427 #define gcc_tdiag_flag_specs gcc_diag_flag_specs
428 #define gcc_cdiag_flag_specs gcc_diag_flag_specs
430 static const format_flag_spec gcc_cxxdiag_flag_specs[] =
432 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
433 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
434 { 'q', 0, 0, N_("'q' flag"), N_("the 'q' diagnostic flag"), STD_C89 },
435 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
436 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
437 { 0, 0, 0, NULL, NULL, STD_C89 }
440 static const format_flag_spec scanf_flag_specs[] =
442 { '*', 0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89 },
443 { 'a', 0, 0, N_("'a' flag"), N_("the 'a' scanf flag"), STD_EXT },
444 { 'm', 0, 0, N_("'m' flag"), N_("the 'm' scanf flag"), STD_EXT },
445 { 'w', 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89 },
446 { 'L', 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89 },
447 { '\'', 0, 0, N_("''' flag"), N_("the ''' scanf flag"), STD_EXT },
448 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' scanf flag"), STD_EXT },
449 { 0, 0, 0, NULL, NULL, STD_C89 }
453 static const format_flag_pair scanf_flag_pairs[] =
455 { '*', 'L', 0, 0 },
456 { 'a', 'm', 0, 0 },
457 { 0, 0, 0, 0 }
461 static const format_flag_spec strftime_flag_specs[] =
463 { '_', 0, 0, N_("'_' flag"), N_("the '_' strftime flag"), STD_EXT },
464 { '-', 0, 0, N_("'-' flag"), N_("the '-' strftime flag"), STD_EXT },
465 { '0', 0, 0, N_("'0' flag"), N_("the '0' strftime flag"), STD_EXT },
466 { '^', 0, 0, N_("'^' flag"), N_("the '^' strftime flag"), STD_EXT },
467 { '#', 0, 0, N_("'#' flag"), N_("the '#' strftime flag"), STD_EXT },
468 { 'w', 0, 0, N_("field width"), N_("field width in strftime format"), STD_EXT },
469 { 'E', 0, 0, N_("'E' modifier"), N_("the 'E' strftime modifier"), STD_C99 },
470 { 'O', 0, 0, N_("'O' modifier"), N_("the 'O' strftime modifier"), STD_C99 },
471 { 'O', 'o', 0, NULL, N_("the 'O' modifier"), STD_EXT },
472 { 0, 0, 0, NULL, NULL, STD_C89 }
476 static const format_flag_pair strftime_flag_pairs[] =
478 { 'E', 'O', 0, 0 },
479 { '_', '-', 0, 0 },
480 { '_', '0', 0, 0 },
481 { '-', '0', 0, 0 },
482 { '^', '#', 0, 0 },
483 { 0, 0, 0, 0 }
487 static const format_flag_spec strfmon_flag_specs[] =
489 { '=', 0, 1, N_("fill character"), N_("fill character in strfmon format"), STD_C89 },
490 { '^', 0, 0, N_("'^' flag"), N_("the '^' strfmon flag"), STD_C89 },
491 { '+', 0, 0, N_("'+' flag"), N_("the '+' strfmon flag"), STD_C89 },
492 { '(', 0, 0, N_("'(' flag"), N_("the '(' strfmon flag"), STD_C89 },
493 { '!', 0, 0, N_("'!' flag"), N_("the '!' strfmon flag"), STD_C89 },
494 { '-', 0, 0, N_("'-' flag"), N_("the '-' strfmon flag"), STD_C89 },
495 { 'w', 0, 0, N_("field width"), N_("field width in strfmon format"), STD_C89 },
496 { '#', 0, 0, N_("left precision"), N_("left precision in strfmon format"), STD_C89 },
497 { 'p', 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89 },
498 { 'L', 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89 },
499 { 0, 0, 0, NULL, NULL, STD_C89 }
502 static const format_flag_pair strfmon_flag_pairs[] =
504 { '+', '(', 0, 0 },
505 { 0, 0, 0, 0 }
509 static const format_char_info print_char_table[] =
511 /* C89 conversion specifiers. */
512 { "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 },
513 { "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 },
514 { "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 },
515 { "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 },
516 { "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 },
517 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, T94_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
518 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
519 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "c", NULL },
520 { "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 },
521 /* C99 conversion specifiers. */
522 { "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 },
523 { "aA", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "", NULL },
524 /* X/Open conversion specifiers. */
525 { "C", 0, STD_EXT, { TEX_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
526 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "R", NULL },
527 /* GNU conversion specifiers. */
528 { "m", 0, STD_EXT, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "", NULL },
529 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
532 static const format_char_info asm_fprintf_char_table[] =
534 /* C89 conversion specifiers. */
535 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +", "i", NULL },
536 { "oxX", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0#", "i", NULL },
537 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0", "i", NULL },
538 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
539 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
541 /* asm_fprintf conversion specifiers. */
542 { "O", 0, STD_C89, NOARGUMENTS, "", "", NULL },
543 { "R", 0, STD_C89, NOARGUMENTS, "", "", NULL },
544 { "I", 0, STD_C89, NOARGUMENTS, "", "", NULL },
545 { "L", 0, STD_C89, NOARGUMENTS, "", "", NULL },
546 { "U", 0, STD_C89, NOARGUMENTS, "", "", NULL },
547 { "r", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
548 { "@", 0, STD_C89, NOARGUMENTS, "", "", NULL },
549 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
552 static const format_char_info gcc_diag_char_table[] =
554 /* C89 conversion specifiers. */
555 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
556 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
557 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
558 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
559 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
560 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
562 /* Custom conversion specifiers. */
564 /* %H will require "location_t" at runtime. */
565 { "H", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
567 /* These will require a "tree" at runtime. */
568 { "JK", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
570 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
571 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
572 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
575 static const format_char_info gcc_tdiag_char_table[] =
577 /* C89 conversion specifiers. */
578 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
579 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
580 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
581 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
582 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
583 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
585 /* Custom conversion specifiers. */
587 /* %H will require "location_t" at runtime. */
588 { "H", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
590 /* These will require a "tree" at runtime. */
591 { "DFJKT", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
593 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
594 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
595 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
598 static const format_char_info gcc_cdiag_char_table[] =
600 /* C89 conversion specifiers. */
601 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
602 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
603 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
604 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
605 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
606 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
608 /* Custom conversion specifiers. */
610 /* %H will require "location_t" at runtime. */
611 { "H", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
613 /* These will require a "tree" at runtime. */
614 { "DEFJKT", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
616 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
617 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
618 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
621 static const format_char_info gcc_cxxdiag_char_table[] =
623 /* C89 conversion specifiers. */
624 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
625 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
626 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
627 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
628 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
629 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
631 /* Custom conversion specifiers. */
633 /* %H will require "location_t" at runtime. */
634 { "H", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
636 /* These will require a "tree" at runtime. */
637 { "ADEFJKTV",0,STD_C89,{ T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+#", "", NULL },
639 /* These accept either an 'int' or an 'enum tree_code' (which is handled as an 'int'.) */
640 { "CLOPQ",0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
642 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
643 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
644 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
647 static const format_char_info gcc_gfc_char_table[] =
649 /* C89 conversion specifiers. */
650 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
651 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
652 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
653 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL },
655 /* gfc conversion specifiers. */
657 { "C", 0, STD_C89, NOARGUMENTS, "", "", NULL },
659 /* This will require a "locus" at runtime. */
660 { "L", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "R", NULL },
662 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
665 static const format_char_info scan_char_table[] =
667 /* C89 conversion specifiers. */
668 { "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 },
669 { "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 },
670 { "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 },
671 { "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 },
672 { "c", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*mw", "cW", NULL },
673 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "cW", NULL },
674 { "[", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "cW[", NULL },
675 { "p", 2, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
676 { "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 },
677 /* C99 conversion specifiers. */
678 { "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 },
679 { "aA", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w'", "W", NULL },
680 /* X/Open conversion specifiers. */
681 { "C", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*mw", "W", NULL },
682 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "W", NULL },
683 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
686 static const format_char_info time_char_table[] =
688 /* C89 conversion specifiers. */
689 { "ABZab", 0, STD_C89, NOLENGTHS, "^#", "", NULL },
690 { "cx", 0, STD_C89, NOLENGTHS, "E", "3", NULL },
691 { "HIMSUWdmw", 0, STD_C89, NOLENGTHS, "-_0Ow", "", NULL },
692 { "j", 0, STD_C89, NOLENGTHS, "-_0Ow", "o", NULL },
693 { "p", 0, STD_C89, NOLENGTHS, "#", "", NULL },
694 { "X", 0, STD_C89, NOLENGTHS, "E", "", NULL },
695 { "y", 0, STD_C89, NOLENGTHS, "EO-_0w", "4", NULL },
696 { "Y", 0, STD_C89, NOLENGTHS, "-_0EOw", "o", NULL },
697 { "%", 0, STD_C89, NOLENGTHS, "", "", NULL },
698 /* C99 conversion specifiers. */
699 { "C", 0, STD_C99, NOLENGTHS, "-_0EOw", "o", NULL },
700 { "D", 0, STD_C99, NOLENGTHS, "", "2", NULL },
701 { "eVu", 0, STD_C99, NOLENGTHS, "-_0Ow", "", NULL },
702 { "FRTnrt", 0, STD_C99, NOLENGTHS, "", "", NULL },
703 { "g", 0, STD_C99, NOLENGTHS, "O-_0w", "2o", NULL },
704 { "G", 0, STD_C99, NOLENGTHS, "-_0Ow", "o", NULL },
705 { "h", 0, STD_C99, NOLENGTHS, "^#", "", NULL },
706 { "z", 0, STD_C99, NOLENGTHS, "O", "o", NULL },
707 /* GNU conversion specifiers. */
708 { "kls", 0, STD_EXT, NOLENGTHS, "-_0Ow", "", NULL },
709 { "P", 0, STD_EXT, NOLENGTHS, "", "", NULL },
710 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
713 static const format_char_info monetary_char_table[] =
715 { "in", 0, STD_C89, { T89_D, BADLEN, BADLEN, BADLEN, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "=^+(!-w#p", "", NULL },
716 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
719 /* This must be in the same order as enum format_type. */
720 static const format_kind_info format_types_orig[] =
722 { "gnu_printf", printf_length_specs, print_char_table, " +#0-'I", NULL,
723 printf_flag_specs, printf_flag_pairs,
724 FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_EMPTY_PREC_OK,
725 'w', 0, 'p', 0, 'L', 0,
726 &integer_type_node, &integer_type_node
728 { "asm_fprintf", asm_fprintf_length_specs, asm_fprintf_char_table, " +#0-", NULL,
729 asm_fprintf_flag_specs, asm_fprintf_flag_pairs,
730 FMT_FLAG_ARG_CONVERT|FMT_FLAG_EMPTY_PREC_OK,
731 'w', 0, 'p', 0, 'L', 0,
732 NULL, NULL
734 { "gcc_diag", gcc_diag_length_specs, gcc_diag_char_table, "q+", NULL,
735 gcc_diag_flag_specs, gcc_diag_flag_pairs,
736 FMT_FLAG_ARG_CONVERT,
737 0, 0, 'p', 0, 'L', 0,
738 NULL, &integer_type_node
740 { "gcc_tdiag", gcc_tdiag_length_specs, gcc_tdiag_char_table, "q+", NULL,
741 gcc_tdiag_flag_specs, gcc_tdiag_flag_pairs,
742 FMT_FLAG_ARG_CONVERT,
743 0, 0, 'p', 0, 'L', 0,
744 NULL, &integer_type_node
746 { "gcc_cdiag", gcc_cdiag_length_specs, gcc_cdiag_char_table, "q+", NULL,
747 gcc_cdiag_flag_specs, gcc_cdiag_flag_pairs,
748 FMT_FLAG_ARG_CONVERT,
749 0, 0, 'p', 0, 'L', 0,
750 NULL, &integer_type_node
752 { "gcc_cxxdiag", gcc_cxxdiag_length_specs, gcc_cxxdiag_char_table, "q+#", NULL,
753 gcc_cxxdiag_flag_specs, gcc_cxxdiag_flag_pairs,
754 FMT_FLAG_ARG_CONVERT,
755 0, 0, 'p', 0, 'L', 0,
756 NULL, &integer_type_node
758 { "gcc_gfc", gcc_gfc_length_specs, gcc_gfc_char_table, "", NULL,
759 NULL, gcc_gfc_flag_pairs,
760 FMT_FLAG_ARG_CONVERT,
761 0, 0, 0, 0, 0, 0,
762 NULL, NULL
764 { "gnu_scanf", scanf_length_specs, scan_char_table, "*'I", NULL,
765 scanf_flag_specs, scanf_flag_pairs,
766 FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_ZERO_WIDTH_BAD|FMT_FLAG_DOLLAR_GAP_POINTER_OK,
767 'w', 0, 0, '*', 'L', 'm',
768 NULL, NULL
770 { "gnu_strftime", NULL, time_char_table, "_-0^#", "EO",
771 strftime_flag_specs, strftime_flag_pairs,
772 FMT_FLAG_FANCY_PERCENT_OK, 'w', 0, 0, 0, 0, 0,
773 NULL, NULL
775 { "gnu_strfmon", strfmon_length_specs, monetary_char_table, "=^+(!-", NULL,
776 strfmon_flag_specs, strfmon_flag_pairs,
777 FMT_FLAG_ARG_CONVERT, 'w', '#', 'p', 0, 'L', 0,
778 NULL, NULL
782 /* This layer of indirection allows GCC to reassign format_types with
783 new data if necessary, while still allowing the original data to be
784 const. */
785 static const format_kind_info *format_types = format_types_orig;
786 /* We can modify this one. We also add target-specific format types
787 to the end of the array. */
788 static format_kind_info *dynamic_format_types;
790 static int n_format_types = ARRAY_SIZE (format_types_orig);
792 /* Structure detailing the results of checking a format function call
793 where the format expression may be a conditional expression with
794 many leaves resulting from nested conditional expressions. */
795 typedef struct
797 /* Number of leaves of the format argument that could not be checked
798 as they were not string literals. */
799 int number_non_literal;
800 /* Number of leaves of the format argument that were null pointers or
801 string literals, but had extra format arguments. */
802 int number_extra_args;
803 /* Number of leaves of the format argument that were null pointers or
804 string literals, but had extra format arguments and used $ operand
805 numbers. */
806 int number_dollar_extra_args;
807 /* Number of leaves of the format argument that were wide string
808 literals. */
809 int number_wide;
810 /* Number of leaves of the format argument that were empty strings. */
811 int number_empty;
812 /* Number of leaves of the format argument that were unterminated
813 strings. */
814 int number_unterminated;
815 /* Number of leaves of the format argument that were not counted above. */
816 int number_other;
817 } format_check_results;
819 typedef struct
821 format_check_results *res;
822 function_format_info *info;
823 tree params;
824 } format_check_context;
826 static void check_format_info (function_format_info *, tree);
827 static void check_format_arg (void *, tree, unsigned HOST_WIDE_INT);
828 static void check_format_info_main (format_check_results *,
829 function_format_info *,
830 const char *, int, tree,
831 unsigned HOST_WIDE_INT, alloc_pool);
833 static void init_dollar_format_checking (int, tree);
834 static int maybe_read_dollar_number (const char **, int,
835 tree, tree *, const format_kind_info *);
836 static bool avoid_dollar_number (const char *);
837 static void finish_dollar_format_checking (format_check_results *, int);
839 static const format_flag_spec *get_flag_spec (const format_flag_spec *,
840 int, const char *);
842 static void check_format_types (format_wanted_type *, const char *, int);
843 static void format_type_warning (const char *, const char *, int, tree,
844 int, const char *, tree, int);
846 /* Decode a format type from a string, returning the type, or
847 format_type_error if not valid, in which case the caller should print an
848 error message. */
849 static int
850 decode_format_type (const char *s)
852 int i;
853 int slen;
855 s = convert_format_name_to_system_name (s);
856 slen = strlen (s);
857 for (i = 0; i < n_format_types; i++)
859 int alen;
860 if (!strcmp (s, format_types[i].name))
861 return i;
862 alen = strlen (format_types[i].name);
863 if (slen == alen + 4 && s[0] == '_' && s[1] == '_'
864 && s[slen - 1] == '_' && s[slen - 2] == '_'
865 && !strncmp (s + 2, format_types[i].name, alen))
866 return i;
868 return format_type_error;
872 /* Check the argument list of a call to printf, scanf, etc.
873 ATTRS are the attributes on the function type. There are NARGS argument
874 values in the array ARGARRAY.
875 Also, if -Wmissing-format-attribute,
876 warn for calls to vprintf or vscanf in functions with no such format
877 attribute themselves. */
879 void
880 check_function_format (tree attrs, int nargs, tree *argarray)
882 tree a;
884 /* See if this function has any format attributes. */
885 for (a = attrs; a; a = TREE_CHAIN (a))
887 if (is_attribute_p ("format", TREE_PURPOSE (a)))
889 /* Yup; check it. */
890 function_format_info info;
891 decode_format_attr (TREE_VALUE (a), &info, 1);
892 if (warn_format)
894 /* FIXME: Rewrite all the internal functions in this file
895 to use the ARGARRAY directly instead of constructing this
896 temporary list. */
897 tree params = NULL_TREE;
898 int i;
899 for (i = nargs - 1; i >= 0; i--)
900 params = tree_cons (NULL_TREE, argarray[i], params);
901 check_format_info (&info, params);
903 if (warn_missing_format_attribute && info.first_arg_num == 0
904 && (format_types[info.format_type].flags
905 & (int) FMT_FLAG_ARG_CONVERT))
907 tree c;
908 for (c = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
910 c = TREE_CHAIN (c))
911 if (is_attribute_p ("format", TREE_PURPOSE (c))
912 && (decode_format_type (IDENTIFIER_POINTER
913 (TREE_VALUE (TREE_VALUE (c))))
914 == info.format_type))
915 break;
916 if (c == NULL_TREE)
918 /* Check if the current function has a parameter to which
919 the format attribute could be attached; if not, it
920 can't be a candidate for a format attribute, despite
921 the vprintf-like or vscanf-like call. */
922 tree args;
923 for (args = DECL_ARGUMENTS (current_function_decl);
924 args != 0;
925 args = TREE_CHAIN (args))
927 if (TREE_CODE (TREE_TYPE (args)) == POINTER_TYPE
928 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args)))
929 == char_type_node))
930 break;
932 if (args != 0)
933 warning (OPT_Wmissing_format_attribute, "function might "
934 "be possible candidate for %qs format attribute",
935 format_types[info.format_type].name);
943 /* Variables used by the checking of $ operand number formats. */
944 static char *dollar_arguments_used = NULL;
945 static char *dollar_arguments_pointer_p = NULL;
946 static int dollar_arguments_alloc = 0;
947 static int dollar_arguments_count;
948 static int dollar_first_arg_num;
949 static int dollar_max_arg_used;
950 static int dollar_format_warned;
952 /* Initialize the checking for a format string that may contain $
953 parameter number specifications; we will need to keep track of whether
954 each parameter has been used. FIRST_ARG_NUM is the number of the first
955 argument that is a parameter to the format, or 0 for a vprintf-style
956 function; PARAMS is the list of arguments starting at this argument. */
958 static void
959 init_dollar_format_checking (int first_arg_num, tree params)
961 tree oparams = params;
963 dollar_first_arg_num = first_arg_num;
964 dollar_arguments_count = 0;
965 dollar_max_arg_used = 0;
966 dollar_format_warned = 0;
967 if (first_arg_num > 0)
969 while (params)
971 dollar_arguments_count++;
972 params = TREE_CHAIN (params);
975 if (dollar_arguments_alloc < dollar_arguments_count)
977 if (dollar_arguments_used)
978 free (dollar_arguments_used);
979 if (dollar_arguments_pointer_p)
980 free (dollar_arguments_pointer_p);
981 dollar_arguments_alloc = dollar_arguments_count;
982 dollar_arguments_used = XNEWVEC (char, dollar_arguments_alloc);
983 dollar_arguments_pointer_p = XNEWVEC (char, dollar_arguments_alloc);
985 if (dollar_arguments_alloc)
987 memset (dollar_arguments_used, 0, dollar_arguments_alloc);
988 if (first_arg_num > 0)
990 int i = 0;
991 params = oparams;
992 while (params)
994 dollar_arguments_pointer_p[i] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params)))
995 == POINTER_TYPE);
996 params = TREE_CHAIN (params);
997 i++;
1004 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
1005 is set, it is an error if one is not found; otherwise, it is OK. If
1006 such a number is found, check whether it is within range and mark that
1007 numbered operand as being used for later checking. Returns the operand
1008 number if found and within range, zero if no such number was found and
1009 this is OK, or -1 on error. PARAMS points to the first operand of the
1010 format; PARAM_PTR is made to point to the parameter referred to. If
1011 a $ format is found, *FORMAT is updated to point just after it. */
1013 static int
1014 maybe_read_dollar_number (const char **format,
1015 int dollar_needed, tree params, tree *param_ptr,
1016 const format_kind_info *fki)
1018 int argnum;
1019 int overflow_flag;
1020 const char *fcp = *format;
1021 if (!ISDIGIT (*fcp))
1023 if (dollar_needed)
1025 warning (OPT_Wformat, "missing $ operand number in format");
1026 return -1;
1028 else
1029 return 0;
1031 argnum = 0;
1032 overflow_flag = 0;
1033 while (ISDIGIT (*fcp))
1035 int nargnum;
1036 nargnum = 10 * argnum + (*fcp - '0');
1037 if (nargnum < 0 || nargnum / 10 != argnum)
1038 overflow_flag = 1;
1039 argnum = nargnum;
1040 fcp++;
1042 if (*fcp != '$')
1044 if (dollar_needed)
1046 warning (OPT_Wformat, "missing $ operand number in format");
1047 return -1;
1049 else
1050 return 0;
1052 *format = fcp + 1;
1053 if (pedantic && !dollar_format_warned)
1055 warning (OPT_Wformat, "%s does not support %%n$ operand number formats",
1056 C_STD_NAME (STD_EXT));
1057 dollar_format_warned = 1;
1059 if (overflow_flag || argnum == 0
1060 || (dollar_first_arg_num && argnum > dollar_arguments_count))
1062 warning (OPT_Wformat, "operand number out of range in format");
1063 return -1;
1065 if (argnum > dollar_max_arg_used)
1066 dollar_max_arg_used = argnum;
1067 /* For vprintf-style functions we may need to allocate more memory to
1068 track which arguments are used. */
1069 while (dollar_arguments_alloc < dollar_max_arg_used)
1071 int nalloc;
1072 nalloc = 2 * dollar_arguments_alloc + 16;
1073 dollar_arguments_used = XRESIZEVEC (char, dollar_arguments_used,
1074 nalloc);
1075 dollar_arguments_pointer_p = XRESIZEVEC (char, dollar_arguments_pointer_p,
1076 nalloc);
1077 memset (dollar_arguments_used + dollar_arguments_alloc, 0,
1078 nalloc - dollar_arguments_alloc);
1079 dollar_arguments_alloc = nalloc;
1081 if (!(fki->flags & (int) FMT_FLAG_DOLLAR_MULTIPLE)
1082 && dollar_arguments_used[argnum - 1] == 1)
1084 dollar_arguments_used[argnum - 1] = 2;
1085 warning (OPT_Wformat, "format argument %d used more than once in %s format",
1086 argnum, fki->name);
1088 else
1089 dollar_arguments_used[argnum - 1] = 1;
1090 if (dollar_first_arg_num)
1092 int i;
1093 *param_ptr = params;
1094 for (i = 1; i < argnum && *param_ptr != 0; i++)
1095 *param_ptr = TREE_CHAIN (*param_ptr);
1097 /* This case shouldn't be caught here. */
1098 gcc_assert (*param_ptr);
1100 else
1101 *param_ptr = 0;
1102 return argnum;
1105 /* Ensure that FORMAT does not start with a decimal number followed by
1106 a $; give a diagnostic and return true if it does, false otherwise. */
1108 static bool
1109 avoid_dollar_number (const char *format)
1111 if (!ISDIGIT (*format))
1112 return false;
1113 while (ISDIGIT (*format))
1114 format++;
1115 if (*format == '$')
1117 warning (OPT_Wformat, "$ operand number used after format without operand number");
1118 return true;
1120 return false;
1124 /* Finish the checking for a format string that used $ operand number formats
1125 instead of non-$ formats. We check for unused operands before used ones
1126 (a serious error, since the implementation of the format function
1127 can't know what types to pass to va_arg to find the later arguments).
1128 and for unused operands at the end of the format (if we know how many
1129 arguments the format had, so not for vprintf). If there were operand
1130 numbers out of range on a non-vprintf-style format, we won't have reached
1131 here. If POINTER_GAP_OK, unused arguments are OK if all arguments are
1132 pointers. */
1134 static void
1135 finish_dollar_format_checking (format_check_results *res, int pointer_gap_ok)
1137 int i;
1138 bool found_pointer_gap = false;
1139 for (i = 0; i < dollar_max_arg_used; i++)
1141 if (!dollar_arguments_used[i])
1143 if (pointer_gap_ok && (dollar_first_arg_num == 0
1144 || dollar_arguments_pointer_p[i]))
1145 found_pointer_gap = true;
1146 else
1147 warning (OPT_Wformat,
1148 "format argument %d unused before used argument %d in $-style format",
1149 i + 1, dollar_max_arg_used);
1152 if (found_pointer_gap
1153 || (dollar_first_arg_num
1154 && dollar_max_arg_used < dollar_arguments_count))
1156 res->number_other--;
1157 res->number_dollar_extra_args++;
1162 /* Retrieve the specification for a format flag. SPEC contains the
1163 specifications for format flags for the applicable kind of format.
1164 FLAG is the flag in question. If PREDICATES is NULL, the basic
1165 spec for that flag must be retrieved and must exist. If
1166 PREDICATES is not NULL, it is a string listing possible predicates
1167 for the spec entry; if an entry predicated on any of these is
1168 found, it is returned, otherwise NULL is returned. */
1170 static const format_flag_spec *
1171 get_flag_spec (const format_flag_spec *spec, int flag, const char *predicates)
1173 int i;
1174 for (i = 0; spec[i].flag_char != 0; i++)
1176 if (spec[i].flag_char != flag)
1177 continue;
1178 if (predicates != NULL)
1180 if (spec[i].predicate != 0
1181 && strchr (predicates, spec[i].predicate) != 0)
1182 return &spec[i];
1184 else if (spec[i].predicate == 0)
1185 return &spec[i];
1187 gcc_assert (predicates);
1188 return NULL;
1192 /* Check the argument list of a call to printf, scanf, etc.
1193 INFO points to the function_format_info structure.
1194 PARAMS is the list of argument values. */
1196 static void
1197 check_format_info (function_format_info *info, tree params)
1199 format_check_context format_ctx;
1200 unsigned HOST_WIDE_INT arg_num;
1201 tree format_tree;
1202 format_check_results res;
1203 /* Skip to format argument. If the argument isn't available, there's
1204 no work for us to do; prototype checking will catch the problem. */
1205 for (arg_num = 1; ; ++arg_num)
1207 if (params == 0)
1208 return;
1209 if (arg_num == info->format_num)
1210 break;
1211 params = TREE_CHAIN (params);
1213 format_tree = TREE_VALUE (params);
1214 params = TREE_CHAIN (params);
1215 if (format_tree == 0)
1216 return;
1218 res.number_non_literal = 0;
1219 res.number_extra_args = 0;
1220 res.number_dollar_extra_args = 0;
1221 res.number_wide = 0;
1222 res.number_empty = 0;
1223 res.number_unterminated = 0;
1224 res.number_other = 0;
1226 format_ctx.res = &res;
1227 format_ctx.info = info;
1228 format_ctx.params = params;
1230 check_function_arguments_recurse (check_format_arg, &format_ctx,
1231 format_tree, arg_num);
1233 if (res.number_non_literal > 0)
1235 /* Functions taking a va_list normally pass a non-literal format
1236 string. These functions typically are declared with
1237 first_arg_num == 0, so avoid warning in those cases. */
1238 if (!(format_types[info->format_type].flags & (int) FMT_FLAG_ARG_CONVERT))
1240 /* For strftime-like formats, warn for not checking the format
1241 string; but there are no arguments to check. */
1242 warning (OPT_Wformat_nonliteral,
1243 "format not a string literal, format string not checked");
1245 else if (info->first_arg_num != 0)
1247 /* If there are no arguments for the format at all, we may have
1248 printf (foo) which is likely to be a security hole. */
1249 while (arg_num + 1 < info->first_arg_num)
1251 if (params == 0)
1252 break;
1253 params = TREE_CHAIN (params);
1254 ++arg_num;
1256 if (params == 0 && warn_format_security)
1257 warning (OPT_Wformat_security,
1258 "format not a string literal and no format arguments");
1259 else if (params == 0 && warn_format_nonliteral)
1260 warning (OPT_Wformat_nonliteral,
1261 "format not a string literal and no format arguments");
1262 else
1263 warning (OPT_Wformat_nonliteral,
1264 "format not a string literal, argument types not checked");
1268 /* If there were extra arguments to the format, normally warn. However,
1269 the standard does say extra arguments are ignored, so in the specific
1270 case where we have multiple leaves (conditional expressions or
1271 ngettext) allow extra arguments if at least one leaf didn't have extra
1272 arguments, but was otherwise OK (either non-literal or checked OK).
1273 If the format is an empty string, this should be counted similarly to the
1274 case of extra format arguments. */
1275 if (res.number_extra_args > 0 && res.number_non_literal == 0
1276 && res.number_other == 0)
1277 warning (OPT_Wformat_extra_args, "too many arguments for format");
1278 if (res.number_dollar_extra_args > 0 && res.number_non_literal == 0
1279 && res.number_other == 0)
1280 warning (OPT_Wformat_extra_args, "unused arguments in $-style format");
1281 if (res.number_empty > 0 && res.number_non_literal == 0
1282 && res.number_other == 0)
1283 warning (OPT_Wformat_zero_length, "zero-length %s format string",
1284 format_types[info->format_type].name);
1286 if (res.number_wide > 0)
1287 warning (OPT_Wformat, "format is a wide character string");
1289 if (res.number_unterminated > 0)
1290 warning (OPT_Wformat, "unterminated format string");
1293 /* Callback from check_function_arguments_recurse to check a
1294 format string. FORMAT_TREE is the format parameter. ARG_NUM
1295 is the number of the format argument. CTX points to a
1296 format_check_context. */
1298 static void
1299 check_format_arg (void *ctx, tree format_tree,
1300 unsigned HOST_WIDE_INT arg_num)
1302 format_check_context *format_ctx = (format_check_context *) ctx;
1303 format_check_results *res = format_ctx->res;
1304 function_format_info *info = format_ctx->info;
1305 tree params = format_ctx->params;
1307 int format_length;
1308 HOST_WIDE_INT offset;
1309 const char *format_chars;
1310 tree array_size = 0;
1311 tree array_init;
1312 alloc_pool fwt_pool;
1314 if (integer_zerop (format_tree))
1316 /* Skip to first argument to check, so we can see if this format
1317 has any arguments (it shouldn't). */
1318 while (arg_num + 1 < info->first_arg_num)
1320 if (params == 0)
1321 return;
1322 params = TREE_CHAIN (params);
1323 ++arg_num;
1326 if (params == 0)
1327 res->number_other++;
1328 else
1329 res->number_extra_args++;
1331 return;
1334 offset = 0;
1335 if (TREE_CODE (format_tree) == POINTER_PLUS_EXPR)
1337 tree arg0, arg1;
1339 arg0 = TREE_OPERAND (format_tree, 0);
1340 arg1 = TREE_OPERAND (format_tree, 1);
1341 STRIP_NOPS (arg0);
1342 STRIP_NOPS (arg1);
1343 if (TREE_CODE (arg1) == INTEGER_CST)
1344 format_tree = arg0;
1345 else
1347 res->number_non_literal++;
1348 return;
1350 if (!host_integerp (arg1, 0)
1351 || (offset = tree_low_cst (arg1, 0)) < 0)
1353 res->number_non_literal++;
1354 return;
1357 if (TREE_CODE (format_tree) != ADDR_EXPR)
1359 res->number_non_literal++;
1360 return;
1362 format_tree = TREE_OPERAND (format_tree, 0);
1363 if (TREE_CODE (format_tree) == ARRAY_REF
1364 && host_integerp (TREE_OPERAND (format_tree, 1), 0)
1365 && (offset += tree_low_cst (TREE_OPERAND (format_tree, 1), 0)) >= 0)
1366 format_tree = TREE_OPERAND (format_tree, 0);
1367 if (TREE_CODE (format_tree) == VAR_DECL
1368 && TREE_CODE (TREE_TYPE (format_tree)) == ARRAY_TYPE
1369 && (array_init = decl_constant_value (format_tree)) != format_tree
1370 && TREE_CODE (array_init) == STRING_CST)
1372 /* Extract the string constant initializer. Note that this may include
1373 a trailing NUL character that is not in the array (e.g.
1374 const char a[3] = "foo";). */
1375 array_size = DECL_SIZE_UNIT (format_tree);
1376 format_tree = array_init;
1378 if (TREE_CODE (format_tree) != STRING_CST)
1380 res->number_non_literal++;
1381 return;
1383 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree))) != char_type_node)
1385 res->number_wide++;
1386 return;
1388 format_chars = TREE_STRING_POINTER (format_tree);
1389 format_length = TREE_STRING_LENGTH (format_tree);
1390 if (array_size != 0)
1392 /* Variable length arrays can't be initialized. */
1393 gcc_assert (TREE_CODE (array_size) == INTEGER_CST);
1395 if (host_integerp (array_size, 0))
1397 HOST_WIDE_INT array_size_value = TREE_INT_CST_LOW (array_size);
1398 if (array_size_value > 0
1399 && array_size_value == (int) array_size_value
1400 && format_length > array_size_value)
1401 format_length = array_size_value;
1404 if (offset)
1406 if (offset >= format_length)
1408 res->number_non_literal++;
1409 return;
1411 format_chars += offset;
1412 format_length -= offset;
1414 if (format_length < 1 || format_chars[--format_length] != 0)
1416 res->number_unterminated++;
1417 return;
1419 if (format_length == 0)
1421 res->number_empty++;
1422 return;
1425 /* Skip to first argument to check. */
1426 while (arg_num + 1 < info->first_arg_num)
1428 if (params == 0)
1429 return;
1430 params = TREE_CHAIN (params);
1431 ++arg_num;
1433 /* Provisionally increment res->number_other; check_format_info_main
1434 will decrement it if it finds there are extra arguments, but this way
1435 need not adjust it for every return. */
1436 res->number_other++;
1437 fwt_pool = create_alloc_pool ("format_wanted_type pool",
1438 sizeof (format_wanted_type), 10);
1439 check_format_info_main (res, info, format_chars, format_length,
1440 params, arg_num, fwt_pool);
1441 free_alloc_pool (fwt_pool);
1445 /* Do the main part of checking a call to a format function. FORMAT_CHARS
1446 is the NUL-terminated format string (which at this point may contain
1447 internal NUL characters); FORMAT_LENGTH is its length (excluding the
1448 terminating NUL character). ARG_NUM is one less than the number of
1449 the first format argument to check; PARAMS points to that format
1450 argument in the list of arguments. */
1452 static void
1453 check_format_info_main (format_check_results *res,
1454 function_format_info *info, const char *format_chars,
1455 int format_length, tree params,
1456 unsigned HOST_WIDE_INT arg_num, alloc_pool fwt_pool)
1458 const char *orig_format_chars = format_chars;
1459 tree first_fillin_param = params;
1461 const format_kind_info *fki = &format_types[info->format_type];
1462 const format_flag_spec *flag_specs = fki->flag_specs;
1463 const format_flag_pair *bad_flag_pairs = fki->bad_flag_pairs;
1465 /* -1 if no conversions taking an operand have been found; 0 if one has
1466 and it didn't use $; 1 if $ formats are in use. */
1467 int has_operand_number = -1;
1469 init_dollar_format_checking (info->first_arg_num, first_fillin_param);
1471 while (1)
1473 int i;
1474 int suppressed = FALSE;
1475 const char *length_chars = NULL;
1476 enum format_lengths length_chars_val = FMT_LEN_none;
1477 enum format_std_version length_chars_std = STD_C89;
1478 int format_char;
1479 tree cur_param;
1480 tree wanted_type;
1481 int main_arg_num = 0;
1482 tree main_arg_params = 0;
1483 enum format_std_version wanted_type_std;
1484 const char *wanted_type_name;
1485 format_wanted_type width_wanted_type;
1486 format_wanted_type precision_wanted_type;
1487 format_wanted_type main_wanted_type;
1488 format_wanted_type *first_wanted_type = NULL;
1489 format_wanted_type *last_wanted_type = NULL;
1490 const format_length_info *fli = NULL;
1491 const format_char_info *fci = NULL;
1492 char flag_chars[256];
1493 int alloc_flag = 0;
1494 const char *format_start = format_chars;
1495 if (*format_chars == 0)
1497 if (format_chars - orig_format_chars != format_length)
1498 warning (OPT_Wformat_contains_nul, "embedded %<\\0%> in format");
1499 if (info->first_arg_num != 0 && params != 0
1500 && has_operand_number <= 0)
1502 res->number_other--;
1503 res->number_extra_args++;
1505 if (has_operand_number > 0)
1506 finish_dollar_format_checking (res, fki->flags & (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK);
1507 return;
1509 if (*format_chars++ != '%')
1510 continue;
1511 if (*format_chars == 0)
1513 warning (OPT_Wformat, "spurious trailing %<%%%> in format");
1514 continue;
1516 if (*format_chars == '%')
1518 ++format_chars;
1519 continue;
1521 flag_chars[0] = 0;
1523 if ((fki->flags & (int) FMT_FLAG_USE_DOLLAR) && has_operand_number != 0)
1525 /* Possibly read a $ operand number at the start of the format.
1526 If one was previously used, one is required here. If one
1527 is not used here, we can't immediately conclude this is a
1528 format without them, since it could be printf %m or scanf %*. */
1529 int opnum;
1530 opnum = maybe_read_dollar_number (&format_chars, 0,
1531 first_fillin_param,
1532 &main_arg_params, fki);
1533 if (opnum == -1)
1534 return;
1535 else if (opnum > 0)
1537 has_operand_number = 1;
1538 main_arg_num = opnum + info->first_arg_num - 1;
1541 else if (fki->flags & FMT_FLAG_USE_DOLLAR)
1543 if (avoid_dollar_number (format_chars))
1544 return;
1547 /* Read any format flags, but do not yet validate them beyond removing
1548 duplicates, since in general validation depends on the rest of
1549 the format. */
1550 while (*format_chars != 0
1551 && strchr (fki->flag_chars, *format_chars) != 0)
1553 const format_flag_spec *s = get_flag_spec (flag_specs,
1554 *format_chars, NULL);
1555 if (strchr (flag_chars, *format_chars) != 0)
1557 warning (OPT_Wformat, "repeated %s in format", _(s->name));
1559 else
1561 i = strlen (flag_chars);
1562 flag_chars[i++] = *format_chars;
1563 flag_chars[i] = 0;
1565 if (s->skip_next_char)
1567 ++format_chars;
1568 if (*format_chars == 0)
1570 warning (OPT_Wformat, "missing fill character at end of strfmon format");
1571 return;
1574 ++format_chars;
1577 /* Read any format width, possibly * or *m$. */
1578 if (fki->width_char != 0)
1580 if (fki->width_type != NULL && *format_chars == '*')
1582 i = strlen (flag_chars);
1583 flag_chars[i++] = fki->width_char;
1584 flag_chars[i] = 0;
1585 /* "...a field width...may be indicated by an asterisk.
1586 In this case, an int argument supplies the field width..." */
1587 ++format_chars;
1588 if (has_operand_number != 0)
1590 int opnum;
1591 opnum = maybe_read_dollar_number (&format_chars,
1592 has_operand_number == 1,
1593 first_fillin_param,
1594 &params, fki);
1595 if (opnum == -1)
1596 return;
1597 else if (opnum > 0)
1599 has_operand_number = 1;
1600 arg_num = opnum + info->first_arg_num - 1;
1602 else
1603 has_operand_number = 0;
1605 else
1607 if (avoid_dollar_number (format_chars))
1608 return;
1610 if (info->first_arg_num != 0)
1612 if (params == 0)
1614 warning (OPT_Wformat, "too few arguments for format");
1615 return;
1617 cur_param = TREE_VALUE (params);
1618 if (has_operand_number <= 0)
1620 params = TREE_CHAIN (params);
1621 ++arg_num;
1623 width_wanted_type.wanted_type = *fki->width_type;
1624 width_wanted_type.wanted_type_name = NULL;
1625 width_wanted_type.pointer_count = 0;
1626 width_wanted_type.char_lenient_flag = 0;
1627 width_wanted_type.writing_in_flag = 0;
1628 width_wanted_type.reading_from_flag = 0;
1629 width_wanted_type.name = _("field width");
1630 width_wanted_type.param = cur_param;
1631 width_wanted_type.arg_num = arg_num;
1632 width_wanted_type.next = NULL;
1633 if (last_wanted_type != 0)
1634 last_wanted_type->next = &width_wanted_type;
1635 if (first_wanted_type == 0)
1636 first_wanted_type = &width_wanted_type;
1637 last_wanted_type = &width_wanted_type;
1640 else
1642 /* Possibly read a numeric width. If the width is zero,
1643 we complain if appropriate. */
1644 int non_zero_width_char = FALSE;
1645 int found_width = FALSE;
1646 while (ISDIGIT (*format_chars))
1648 found_width = TRUE;
1649 if (*format_chars != '0')
1650 non_zero_width_char = TRUE;
1651 ++format_chars;
1653 if (found_width && !non_zero_width_char &&
1654 (fki->flags & (int) FMT_FLAG_ZERO_WIDTH_BAD))
1655 warning (OPT_Wformat, "zero width in %s format", fki->name);
1656 if (found_width)
1658 i = strlen (flag_chars);
1659 flag_chars[i++] = fki->width_char;
1660 flag_chars[i] = 0;
1665 /* Read any format left precision (must be a number, not *). */
1666 if (fki->left_precision_char != 0 && *format_chars == '#')
1668 ++format_chars;
1669 i = strlen (flag_chars);
1670 flag_chars[i++] = fki->left_precision_char;
1671 flag_chars[i] = 0;
1672 if (!ISDIGIT (*format_chars))
1673 warning (OPT_Wformat, "empty left precision in %s format", fki->name);
1674 while (ISDIGIT (*format_chars))
1675 ++format_chars;
1678 /* Read any format precision, possibly * or *m$. */
1679 if (fki->precision_char != 0 && *format_chars == '.')
1681 ++format_chars;
1682 i = strlen (flag_chars);
1683 flag_chars[i++] = fki->precision_char;
1684 flag_chars[i] = 0;
1685 if (fki->precision_type != NULL && *format_chars == '*')
1687 /* "...a...precision...may be indicated by an asterisk.
1688 In this case, an int argument supplies the...precision." */
1689 ++format_chars;
1690 if (has_operand_number != 0)
1692 int opnum;
1693 opnum = maybe_read_dollar_number (&format_chars,
1694 has_operand_number == 1,
1695 first_fillin_param,
1696 &params, fki);
1697 if (opnum == -1)
1698 return;
1699 else if (opnum > 0)
1701 has_operand_number = 1;
1702 arg_num = opnum + info->first_arg_num - 1;
1704 else
1705 has_operand_number = 0;
1707 else
1709 if (avoid_dollar_number (format_chars))
1710 return;
1712 if (info->first_arg_num != 0)
1714 if (params == 0)
1716 warning (OPT_Wformat, "too few arguments for format");
1717 return;
1719 cur_param = TREE_VALUE (params);
1720 if (has_operand_number <= 0)
1722 params = TREE_CHAIN (params);
1723 ++arg_num;
1725 precision_wanted_type.wanted_type = *fki->precision_type;
1726 precision_wanted_type.wanted_type_name = NULL;
1727 precision_wanted_type.pointer_count = 0;
1728 precision_wanted_type.char_lenient_flag = 0;
1729 precision_wanted_type.writing_in_flag = 0;
1730 precision_wanted_type.reading_from_flag = 0;
1731 precision_wanted_type.name = _("field precision");
1732 precision_wanted_type.param = cur_param;
1733 precision_wanted_type.arg_num = arg_num;
1734 precision_wanted_type.next = NULL;
1735 if (last_wanted_type != 0)
1736 last_wanted_type->next = &precision_wanted_type;
1737 if (first_wanted_type == 0)
1738 first_wanted_type = &precision_wanted_type;
1739 last_wanted_type = &precision_wanted_type;
1742 else
1744 if (!(fki->flags & (int) FMT_FLAG_EMPTY_PREC_OK)
1745 && !ISDIGIT (*format_chars))
1746 warning (OPT_Wformat, "empty precision in %s format", fki->name);
1747 while (ISDIGIT (*format_chars))
1748 ++format_chars;
1752 if (fki->alloc_char && fki->alloc_char == *format_chars)
1754 i = strlen (flag_chars);
1755 flag_chars[i++] = fki->alloc_char;
1756 flag_chars[i] = 0;
1757 format_chars++;
1760 /* Handle the scanf allocation kludge. */
1761 if (fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
1763 if (*format_chars == 'a' && !flag_isoc99)
1765 if (format_chars[1] == 's' || format_chars[1] == 'S'
1766 || format_chars[1] == '[')
1768 /* 'a' is used as a flag. */
1769 i = strlen (flag_chars);
1770 flag_chars[i++] = 'a';
1771 flag_chars[i] = 0;
1772 format_chars++;
1777 /* Read any length modifier, if this kind of format has them. */
1778 fli = fki->length_char_specs;
1779 length_chars = NULL;
1780 length_chars_val = FMT_LEN_none;
1781 length_chars_std = STD_C89;
1782 if (fli)
1784 while (fli->name != 0
1785 && strncmp (fli->name, format_chars, strlen (fli->name)))
1786 fli++;
1787 if (fli->name != 0)
1789 format_chars += strlen (fli->name);
1790 if (fli->double_name != 0 && fli->name[0] == *format_chars)
1792 format_chars++;
1793 length_chars = fli->double_name;
1794 length_chars_val = fli->double_index;
1795 length_chars_std = fli->double_std;
1797 else
1799 length_chars = fli->name;
1800 length_chars_val = fli->index;
1801 length_chars_std = fli->std;
1803 i = strlen (flag_chars);
1804 flag_chars[i++] = fki->length_code_char;
1805 flag_chars[i] = 0;
1807 if (pedantic)
1809 /* Warn if the length modifier is non-standard. */
1810 if (ADJ_STD (length_chars_std) > C_STD_VER)
1811 warning (OPT_Wformat,
1812 "%s does not support the %qs %s length modifier",
1813 C_STD_NAME (length_chars_std), length_chars,
1814 fki->name);
1818 /* Read any modifier (strftime E/O). */
1819 if (fki->modifier_chars != NULL)
1821 while (*format_chars != 0
1822 && strchr (fki->modifier_chars, *format_chars) != 0)
1824 if (strchr (flag_chars, *format_chars) != 0)
1826 const format_flag_spec *s = get_flag_spec (flag_specs,
1827 *format_chars, NULL);
1828 warning (OPT_Wformat, "repeated %s in format", _(s->name));
1830 else
1832 i = strlen (flag_chars);
1833 flag_chars[i++] = *format_chars;
1834 flag_chars[i] = 0;
1836 ++format_chars;
1840 format_char = *format_chars;
1841 if (format_char == 0
1842 || (!(fki->flags & (int) FMT_FLAG_FANCY_PERCENT_OK)
1843 && format_char == '%'))
1845 warning (OPT_Wformat, "conversion lacks type at end of format");
1846 continue;
1848 format_chars++;
1849 fci = fki->conversion_specs;
1850 while (fci->format_chars != 0
1851 && strchr (fci->format_chars, format_char) == 0)
1852 ++fci;
1853 if (fci->format_chars == 0)
1855 if (ISGRAPH (format_char))
1856 warning (OPT_Wformat, "unknown conversion type character %qc in format",
1857 format_char);
1858 else
1859 warning (OPT_Wformat, "unknown conversion type character 0x%x in format",
1860 format_char);
1861 continue;
1863 if (pedantic)
1865 if (ADJ_STD (fci->std) > C_STD_VER)
1866 warning (OPT_Wformat, "%s does not support the %<%%%c%> %s format",
1867 C_STD_NAME (fci->std), format_char, fki->name);
1870 /* Validate the individual flags used, removing any that are invalid. */
1872 int d = 0;
1873 for (i = 0; flag_chars[i] != 0; i++)
1875 const format_flag_spec *s = get_flag_spec (flag_specs,
1876 flag_chars[i], NULL);
1877 flag_chars[i - d] = flag_chars[i];
1878 if (flag_chars[i] == fki->length_code_char)
1879 continue;
1880 if (strchr (fci->flag_chars, flag_chars[i]) == 0)
1882 warning (OPT_Wformat, "%s used with %<%%%c%> %s format",
1883 _(s->name), format_char, fki->name);
1884 d++;
1885 continue;
1887 if (pedantic)
1889 const format_flag_spec *t;
1890 if (ADJ_STD (s->std) > C_STD_VER)
1891 warning (OPT_Wformat, "%s does not support %s",
1892 C_STD_NAME (s->std), _(s->long_name));
1893 t = get_flag_spec (flag_specs, flag_chars[i], fci->flags2);
1894 if (t != NULL && ADJ_STD (t->std) > ADJ_STD (s->std))
1896 const char *long_name = (t->long_name != NULL
1897 ? t->long_name
1898 : s->long_name);
1899 if (ADJ_STD (t->std) > C_STD_VER)
1900 warning (OPT_Wformat,
1901 "%s does not support %s with the %<%%%c%> %s format",
1902 C_STD_NAME (t->std), _(long_name),
1903 format_char, fki->name);
1907 flag_chars[i - d] = 0;
1910 if ((fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
1911 && strchr (flag_chars, 'a') != 0)
1912 alloc_flag = 1;
1913 if (fki->alloc_char && strchr (flag_chars, fki->alloc_char) != 0)
1914 alloc_flag = 1;
1916 if (fki->suppression_char
1917 && strchr (flag_chars, fki->suppression_char) != 0)
1918 suppressed = 1;
1920 /* Validate the pairs of flags used. */
1921 for (i = 0; bad_flag_pairs[i].flag_char1 != 0; i++)
1923 const format_flag_spec *s, *t;
1924 if (strchr (flag_chars, bad_flag_pairs[i].flag_char1) == 0)
1925 continue;
1926 if (strchr (flag_chars, bad_flag_pairs[i].flag_char2) == 0)
1927 continue;
1928 if (bad_flag_pairs[i].predicate != 0
1929 && strchr (fci->flags2, bad_flag_pairs[i].predicate) == 0)
1930 continue;
1931 s = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char1, NULL);
1932 t = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char2, NULL);
1933 if (bad_flag_pairs[i].ignored)
1935 if (bad_flag_pairs[i].predicate != 0)
1936 warning (OPT_Wformat,
1937 "%s ignored with %s and %<%%%c%> %s format",
1938 _(s->name), _(t->name), format_char,
1939 fki->name);
1940 else
1941 warning (OPT_Wformat, "%s ignored with %s in %s format",
1942 _(s->name), _(t->name), fki->name);
1944 else
1946 if (bad_flag_pairs[i].predicate != 0)
1947 warning (OPT_Wformat,
1948 "use of %s and %s together with %<%%%c%> %s format",
1949 _(s->name), _(t->name), format_char,
1950 fki->name);
1951 else
1952 warning (OPT_Wformat, "use of %s and %s together in %s format",
1953 _(s->name), _(t->name), fki->name);
1957 /* Give Y2K warnings. */
1958 if (warn_format_y2k)
1960 int y2k_level = 0;
1961 if (strchr (fci->flags2, '4') != 0)
1962 if (strchr (flag_chars, 'E') != 0)
1963 y2k_level = 3;
1964 else
1965 y2k_level = 2;
1966 else if (strchr (fci->flags2, '3') != 0)
1967 y2k_level = 3;
1968 else if (strchr (fci->flags2, '2') != 0)
1969 y2k_level = 2;
1970 if (y2k_level == 3)
1971 warning (OPT_Wformat_y2k, "%<%%%c%> yields only last 2 digits of "
1972 "year in some locales", format_char);
1973 else if (y2k_level == 2)
1974 warning (OPT_Wformat_y2k, "%<%%%c%> yields only last 2 digits of "
1975 "year", format_char);
1978 if (strchr (fci->flags2, '[') != 0)
1980 /* Skip over scan set, in case it happens to have '%' in it. */
1981 if (*format_chars == '^')
1982 ++format_chars;
1983 /* Find closing bracket; if one is hit immediately, then
1984 it's part of the scan set rather than a terminator. */
1985 if (*format_chars == ']')
1986 ++format_chars;
1987 while (*format_chars && *format_chars != ']')
1988 ++format_chars;
1989 if (*format_chars != ']')
1990 /* The end of the format string was reached. */
1991 warning (OPT_Wformat, "no closing %<]%> for %<%%[%> format");
1994 wanted_type = 0;
1995 wanted_type_name = 0;
1996 if (fki->flags & (int) FMT_FLAG_ARG_CONVERT)
1998 wanted_type = (fci->types[length_chars_val].type
1999 ? *fci->types[length_chars_val].type : 0);
2000 wanted_type_name = fci->types[length_chars_val].name;
2001 wanted_type_std = fci->types[length_chars_val].std;
2002 if (wanted_type == 0)
2004 warning (OPT_Wformat,
2005 "use of %qs length modifier with %qc type character",
2006 length_chars, format_char);
2007 /* Heuristic: skip one argument when an invalid length/type
2008 combination is encountered. */
2009 arg_num++;
2010 if (params == 0)
2012 warning (OPT_Wformat, "too few arguments for format");
2013 return;
2015 params = TREE_CHAIN (params);
2016 continue;
2018 else if (pedantic
2019 /* Warn if non-standard, provided it is more non-standard
2020 than the length and type characters that may already
2021 have been warned for. */
2022 && ADJ_STD (wanted_type_std) > ADJ_STD (length_chars_std)
2023 && ADJ_STD (wanted_type_std) > ADJ_STD (fci->std))
2025 if (ADJ_STD (wanted_type_std) > C_STD_VER)
2026 warning (OPT_Wformat,
2027 "%s does not support the %<%%%s%c%> %s format",
2028 C_STD_NAME (wanted_type_std), length_chars,
2029 format_char, fki->name);
2033 main_wanted_type.next = NULL;
2035 /* Finally. . .check type of argument against desired type! */
2036 if (info->first_arg_num == 0)
2037 continue;
2038 if ((fci->pointer_count == 0 && wanted_type == void_type_node)
2039 || suppressed)
2041 if (main_arg_num != 0)
2043 if (suppressed)
2044 warning (OPT_Wformat, "operand number specified with "
2045 "suppressed assignment");
2046 else
2047 warning (OPT_Wformat, "operand number specified for format "
2048 "taking no argument");
2051 else
2053 format_wanted_type *wanted_type_ptr;
2055 if (main_arg_num != 0)
2057 arg_num = main_arg_num;
2058 params = main_arg_params;
2060 else
2062 ++arg_num;
2063 if (has_operand_number > 0)
2065 warning (OPT_Wformat, "missing $ operand number in format");
2066 return;
2068 else
2069 has_operand_number = 0;
2072 wanted_type_ptr = &main_wanted_type;
2073 while (fci)
2075 if (params == 0)
2077 warning (OPT_Wformat, "too few arguments for format");
2078 return;
2081 cur_param = TREE_VALUE (params);
2082 params = TREE_CHAIN (params);
2084 wanted_type_ptr->wanted_type = wanted_type;
2085 wanted_type_ptr->wanted_type_name = wanted_type_name;
2086 wanted_type_ptr->pointer_count = fci->pointer_count + alloc_flag;
2087 wanted_type_ptr->char_lenient_flag = 0;
2088 if (strchr (fci->flags2, 'c') != 0)
2089 wanted_type_ptr->char_lenient_flag = 1;
2090 wanted_type_ptr->writing_in_flag = 0;
2091 wanted_type_ptr->reading_from_flag = 0;
2092 if (alloc_flag)
2093 wanted_type_ptr->writing_in_flag = 1;
2094 else
2096 if (strchr (fci->flags2, 'W') != 0)
2097 wanted_type_ptr->writing_in_flag = 1;
2098 if (strchr (fci->flags2, 'R') != 0)
2099 wanted_type_ptr->reading_from_flag = 1;
2101 wanted_type_ptr->name = NULL;
2102 wanted_type_ptr->param = cur_param;
2103 wanted_type_ptr->arg_num = arg_num;
2104 wanted_type_ptr->next = NULL;
2105 if (last_wanted_type != 0)
2106 last_wanted_type->next = wanted_type_ptr;
2107 if (first_wanted_type == 0)
2108 first_wanted_type = wanted_type_ptr;
2109 last_wanted_type = wanted_type_ptr;
2111 fci = fci->chain;
2112 if (fci)
2114 wanted_type_ptr = (format_wanted_type *)
2115 pool_alloc (fwt_pool);
2116 arg_num++;
2117 wanted_type = *fci->types[length_chars_val].type;
2118 wanted_type_name = fci->types[length_chars_val].name;
2123 if (first_wanted_type != 0)
2124 check_format_types (first_wanted_type, format_start,
2125 format_chars - format_start);
2130 /* Check the argument types from a single format conversion (possibly
2131 including width and precision arguments). */
2132 static void
2133 check_format_types (format_wanted_type *types, const char *format_start,
2134 int format_length)
2136 for (; types != 0; types = types->next)
2138 tree cur_param;
2139 tree cur_type;
2140 tree orig_cur_type;
2141 tree wanted_type;
2142 int arg_num;
2143 int i;
2144 int char_type_flag;
2145 cur_param = types->param;
2146 cur_type = TREE_TYPE (cur_param);
2147 if (cur_type == error_mark_node)
2148 continue;
2149 orig_cur_type = cur_type;
2150 char_type_flag = 0;
2151 wanted_type = types->wanted_type;
2152 arg_num = types->arg_num;
2154 /* The following should not occur here. */
2155 gcc_assert (wanted_type);
2156 gcc_assert (wanted_type != void_type_node || types->pointer_count);
2158 if (types->pointer_count == 0)
2159 wanted_type = lang_hooks.types.type_promotes_to (wanted_type);
2161 wanted_type = TYPE_MAIN_VARIANT (wanted_type);
2163 STRIP_NOPS (cur_param);
2165 /* Check the types of any additional pointer arguments
2166 that precede the "real" argument. */
2167 for (i = 0; i < types->pointer_count; ++i)
2169 if (TREE_CODE (cur_type) == POINTER_TYPE)
2171 cur_type = TREE_TYPE (cur_type);
2172 if (cur_type == error_mark_node)
2173 break;
2175 /* Check for writing through a NULL pointer. */
2176 if (types->writing_in_flag
2177 && i == 0
2178 && cur_param != 0
2179 && integer_zerop (cur_param))
2180 warning (OPT_Wformat, "writing through null pointer "
2181 "(argument %d)", arg_num);
2183 /* Check for reading through a NULL pointer. */
2184 if (types->reading_from_flag
2185 && i == 0
2186 && cur_param != 0
2187 && integer_zerop (cur_param))
2188 warning (OPT_Wformat, "reading through null pointer "
2189 "(argument %d)", arg_num);
2191 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
2192 cur_param = TREE_OPERAND (cur_param, 0);
2193 else
2194 cur_param = 0;
2196 /* See if this is an attempt to write into a const type with
2197 scanf or with printf "%n". Note: the writing in happens
2198 at the first indirection only, if for example
2199 void * const * is passed to scanf %p; passing
2200 const void ** is simply passing an incompatible type. */
2201 if (types->writing_in_flag
2202 && i == 0
2203 && (TYPE_READONLY (cur_type)
2204 || (cur_param != 0
2205 && (CONSTANT_CLASS_P (cur_param)
2206 || (DECL_P (cur_param)
2207 && TREE_READONLY (cur_param))))))
2208 warning (OPT_Wformat, "writing into constant object "
2209 "(argument %d)", arg_num);
2211 /* If there are extra type qualifiers beyond the first
2212 indirection, then this makes the types technically
2213 incompatible. */
2214 if (i > 0
2215 && pedantic
2216 && (TYPE_READONLY (cur_type)
2217 || TYPE_VOLATILE (cur_type)
2218 || TYPE_RESTRICT (cur_type)))
2219 warning (OPT_Wformat, "extra type qualifiers in format "
2220 "argument (argument %d)",
2221 arg_num);
2224 else
2226 format_type_warning (types->name, format_start, format_length,
2227 wanted_type, types->pointer_count,
2228 types->wanted_type_name, orig_cur_type,
2229 arg_num);
2230 break;
2234 if (i < types->pointer_count)
2235 continue;
2237 cur_type = TYPE_MAIN_VARIANT (cur_type);
2239 /* Check whether the argument type is a character type. This leniency
2240 only applies to certain formats, flagged with 'c'.
2242 if (types->char_lenient_flag)
2243 char_type_flag = (cur_type == char_type_node
2244 || cur_type == signed_char_type_node
2245 || cur_type == unsigned_char_type_node);
2247 /* Check the type of the "real" argument, if there's a type we want. */
2248 if (lang_hooks.types_compatible_p (wanted_type, cur_type))
2249 continue;
2250 /* If we want 'void *', allow any pointer type.
2251 (Anything else would already have got a warning.)
2252 With -pedantic, only allow pointers to void and to character
2253 types. */
2254 if (wanted_type == void_type_node
2255 && (!pedantic || (i == 1 && char_type_flag)))
2256 continue;
2257 /* Don't warn about differences merely in signedness, unless
2258 -pedantic. With -pedantic, warn if the type is a pointer
2259 target and not a character type, and for character types at
2260 a second level of indirection. */
2261 if (TREE_CODE (wanted_type) == INTEGER_TYPE
2262 && TREE_CODE (cur_type) == INTEGER_TYPE
2263 && (!pedantic || i == 0 || (i == 1 && char_type_flag))
2264 && (TYPE_UNSIGNED (wanted_type)
2265 ? wanted_type == c_common_unsigned_type (cur_type)
2266 : wanted_type == c_common_signed_type (cur_type)))
2267 continue;
2268 /* Likewise, "signed char", "unsigned char" and "char" are
2269 equivalent but the above test won't consider them equivalent. */
2270 if (wanted_type == char_type_node
2271 && (!pedantic || i < 2)
2272 && char_type_flag)
2273 continue;
2274 /* Now we have a type mismatch. */
2275 format_type_warning (types->name, format_start, format_length,
2276 wanted_type, types->pointer_count,
2277 types->wanted_type_name, orig_cur_type, arg_num);
2282 /* Give a warning about a format argument of different type from that
2283 expected. DESCR is a description such as "field precision", or
2284 NULL for an ordinary format. For an ordinary format, FORMAT_START
2285 points to where the format starts in the format string and
2286 FORMAT_LENGTH is its length. WANTED_TYPE is the type the argument
2287 should have after POINTER_COUNT pointer dereferences.
2288 WANTED_NAME_NAME is a possibly more friendly name of WANTED_TYPE,
2289 or NULL if the ordinary name of the type should be used. ARG_TYPE
2290 is the type of the actual argument. ARG_NUM is the number of that
2291 argument. */
2292 static void
2293 format_type_warning (const char *descr, const char *format_start,
2294 int format_length, tree wanted_type, int pointer_count,
2295 const char *wanted_type_name, tree arg_type, int arg_num)
2297 char *p;
2298 /* If ARG_TYPE is a typedef with a misleading name (for example,
2299 size_t but not the standard size_t expected by printf %zu), avoid
2300 printing the typedef name. */
2301 if (wanted_type_name
2302 && TYPE_NAME (arg_type)
2303 && TREE_CODE (TYPE_NAME (arg_type)) == TYPE_DECL
2304 && DECL_NAME (TYPE_NAME (arg_type))
2305 && !strcmp (wanted_type_name,
2306 lang_hooks.decl_printable_name (TYPE_NAME (arg_type), 2)))
2307 arg_type = TYPE_MAIN_VARIANT (arg_type);
2308 /* The format type and name exclude any '*' for pointers, so those
2309 must be formatted manually. For all the types we currently have,
2310 this is adequate, but formats taking pointers to functions or
2311 arrays would require the full type to be built up in order to
2312 print it with %T. */
2313 p = (char *) alloca (pointer_count + 2);
2314 if (pointer_count == 0)
2315 p[0] = 0;
2316 else if (c_dialect_cxx ())
2318 memset (p, '*', pointer_count);
2319 p[pointer_count] = 0;
2321 else
2323 p[0] = ' ';
2324 memset (p + 1, '*', pointer_count);
2325 p[pointer_count + 1] = 0;
2327 if (wanted_type_name)
2329 if (descr)
2330 warning (OPT_Wformat, "%s should have type %<%s%s%>, "
2331 "but argument %d has type %qT",
2332 descr, wanted_type_name, p, arg_num, arg_type);
2333 else
2334 warning (OPT_Wformat, "format %q.*s expects type %<%s%s%>, "
2335 "but argument %d has type %qT",
2336 format_length, format_start, wanted_type_name, p,
2337 arg_num, arg_type);
2339 else
2341 if (descr)
2342 warning (OPT_Wformat, "%s should have type %<%T%s%>, "
2343 "but argument %d has type %qT",
2344 descr, wanted_type, p, arg_num, arg_type);
2345 else
2346 warning (OPT_Wformat, "format %q.*s expects type %<%T%s%>, "
2347 "but argument %d has type %qT",
2348 format_length, format_start, wanted_type, p, arg_num, arg_type);
2353 /* Given a format_char_info array FCI, and a character C, this function
2354 returns the index into the conversion_specs where that specifier's
2355 data is located. The character must exist. */
2356 static unsigned int
2357 find_char_info_specifier_index (const format_char_info *fci, int c)
2359 unsigned i;
2361 for (i = 0; fci->format_chars; i++, fci++)
2362 if (strchr (fci->format_chars, c))
2363 return i;
2365 /* We shouldn't be looking for a non-existent specifier. */
2366 gcc_unreachable ();
2369 /* Given a format_length_info array FLI, and a character C, this
2370 function returns the index into the conversion_specs where that
2371 modifier's data is located. The character must exist. */
2372 static unsigned int
2373 find_length_info_modifier_index (const format_length_info *fli, int c)
2375 unsigned i;
2377 for (i = 0; fli->name; i++, fli++)
2378 if (strchr (fli->name, c))
2379 return i;
2381 /* We shouldn't be looking for a non-existent modifier. */
2382 gcc_unreachable ();
2385 /* Determine the type of HOST_WIDE_INT in the code being compiled for
2386 use in GCC's __asm_fprintf__ custom format attribute. You must
2387 have set dynamic_format_types before calling this function. */
2388 static void
2389 init_dynamic_asm_fprintf_info (void)
2391 static tree hwi;
2393 if (!hwi)
2395 format_length_info *new_asm_fprintf_length_specs;
2396 unsigned int i;
2398 /* Find the underlying type for HOST_WIDE_INT. For the %w
2399 length modifier to work, one must have issued: "typedef
2400 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2401 prior to using that modifier. */
2402 hwi = maybe_get_identifier ("__gcc_host_wide_int__");
2403 if (!hwi)
2405 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2406 return;
2408 hwi = identifier_global_value (hwi);
2409 if (!hwi || TREE_CODE (hwi) != TYPE_DECL)
2411 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2412 return;
2414 hwi = DECL_ORIGINAL_TYPE (hwi);
2415 gcc_assert (hwi);
2416 if (hwi != long_integer_type_node && hwi != long_long_integer_type_node)
2418 error ("%<__gcc_host_wide_int__%> is not defined as %<long%>"
2419 " or %<long long%>");
2420 return;
2423 /* Create a new (writable) copy of asm_fprintf_length_specs. */
2424 new_asm_fprintf_length_specs = (format_length_info *)
2425 xmemdup (asm_fprintf_length_specs,
2426 sizeof (asm_fprintf_length_specs),
2427 sizeof (asm_fprintf_length_specs));
2429 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2430 i = find_length_info_modifier_index (new_asm_fprintf_length_specs, 'w');
2431 if (hwi == long_integer_type_node)
2432 new_asm_fprintf_length_specs[i].index = FMT_LEN_l;
2433 else if (hwi == long_long_integer_type_node)
2434 new_asm_fprintf_length_specs[i].index = FMT_LEN_ll;
2435 else
2436 gcc_unreachable ();
2438 /* Assign the new data for use. */
2439 dynamic_format_types[asm_fprintf_format_type].length_char_specs =
2440 new_asm_fprintf_length_specs;
2444 /* Determine the type of a "locus" in the code being compiled for use
2445 in GCC's __gcc_gfc__ custom format attribute. You must have set
2446 dynamic_format_types before calling this function. */
2447 static void
2448 init_dynamic_gfc_info (void)
2450 static tree locus;
2452 if (!locus)
2454 static format_char_info *gfc_fci;
2456 /* For the GCC __gcc_gfc__ custom format specifier to work, one
2457 must have declared 'locus' prior to using this attribute. If
2458 we haven't seen this declarations then you shouldn't use the
2459 specifier requiring that type. */
2460 if ((locus = maybe_get_identifier ("locus")))
2462 locus = identifier_global_value (locus);
2463 if (locus)
2465 if (TREE_CODE (locus) != TYPE_DECL
2466 || TREE_TYPE (locus) == error_mark_node)
2468 error ("%<locus%> is not defined as a type");
2469 locus = 0;
2471 else
2472 locus = TREE_TYPE (locus);
2476 /* Assign the new data for use. */
2478 /* Handle the __gcc_gfc__ format specifics. */
2479 if (!gfc_fci)
2480 dynamic_format_types[gcc_gfc_format_type].conversion_specs =
2481 gfc_fci = (format_char_info *)
2482 xmemdup (gcc_gfc_char_table,
2483 sizeof (gcc_gfc_char_table),
2484 sizeof (gcc_gfc_char_table));
2485 if (locus)
2487 const unsigned i = find_char_info_specifier_index (gfc_fci, 'L');
2488 gfc_fci[i].types[0].type = &locus;
2489 gfc_fci[i].pointer_count = 1;
2494 /* Determine the types of "tree" and "location_t" in the code being
2495 compiled for use in GCC's diagnostic custom format attributes. You
2496 must have set dynamic_format_types before calling this function. */
2497 static void
2498 init_dynamic_diag_info (void)
2500 static tree t, loc, hwi;
2502 if (!loc || !t || !hwi)
2504 static format_char_info *diag_fci, *tdiag_fci, *cdiag_fci, *cxxdiag_fci;
2505 static format_length_info *diag_ls;
2506 unsigned int i;
2508 /* For the GCC-diagnostics custom format specifiers to work, one
2509 must have declared 'tree' and/or 'location_t' prior to using
2510 those attributes. If we haven't seen these declarations then
2511 you shouldn't use the specifiers requiring these types.
2512 However we don't force a hard ICE because we may see only one
2513 or the other type. */
2514 if ((loc = maybe_get_identifier ("location_t")))
2516 loc = identifier_global_value (loc);
2517 if (loc)
2519 if (TREE_CODE (loc) != TYPE_DECL)
2521 error ("%<location_t%> is not defined as a type");
2522 loc = 0;
2524 else
2525 loc = TREE_TYPE (loc);
2529 /* We need to grab the underlying 'union tree_node' so peek into
2530 an extra type level. */
2531 if ((t = maybe_get_identifier ("tree")))
2533 t = identifier_global_value (t);
2534 if (t)
2536 if (TREE_CODE (t) != TYPE_DECL)
2538 error ("%<tree%> is not defined as a type");
2539 t = 0;
2541 else if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
2543 error ("%<tree%> is not defined as a pointer type");
2544 t = 0;
2546 else
2547 t = TREE_TYPE (TREE_TYPE (t));
2551 /* Find the underlying type for HOST_WIDE_INT. For the %w
2552 length modifier to work, one must have issued: "typedef
2553 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2554 prior to using that modifier. */
2555 if ((hwi = maybe_get_identifier ("__gcc_host_wide_int__")))
2557 hwi = identifier_global_value (hwi);
2558 if (hwi)
2560 if (TREE_CODE (hwi) != TYPE_DECL)
2562 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2563 hwi = 0;
2565 else
2567 hwi = DECL_ORIGINAL_TYPE (hwi);
2568 gcc_assert (hwi);
2569 if (hwi != long_integer_type_node
2570 && hwi != long_long_integer_type_node)
2572 error ("%<__gcc_host_wide_int__%> is not defined"
2573 " as %<long%> or %<long long%>");
2574 hwi = 0;
2580 /* Assign the new data for use. */
2582 /* All the GCC diag formats use the same length specs. */
2583 if (!diag_ls)
2584 dynamic_format_types[gcc_diag_format_type].length_char_specs =
2585 dynamic_format_types[gcc_tdiag_format_type].length_char_specs =
2586 dynamic_format_types[gcc_cdiag_format_type].length_char_specs =
2587 dynamic_format_types[gcc_cxxdiag_format_type].length_char_specs =
2588 diag_ls = (format_length_info *)
2589 xmemdup (gcc_diag_length_specs,
2590 sizeof (gcc_diag_length_specs),
2591 sizeof (gcc_diag_length_specs));
2592 if (hwi)
2594 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2595 i = find_length_info_modifier_index (diag_ls, 'w');
2596 if (hwi == long_integer_type_node)
2597 diag_ls[i].index = FMT_LEN_l;
2598 else if (hwi == long_long_integer_type_node)
2599 diag_ls[i].index = FMT_LEN_ll;
2600 else
2601 gcc_unreachable ();
2604 /* Handle the __gcc_diag__ format specifics. */
2605 if (!diag_fci)
2606 dynamic_format_types[gcc_diag_format_type].conversion_specs =
2607 diag_fci = (format_char_info *)
2608 xmemdup (gcc_diag_char_table,
2609 sizeof (gcc_diag_char_table),
2610 sizeof (gcc_diag_char_table));
2611 if (loc)
2613 i = find_char_info_specifier_index (diag_fci, 'H');
2614 diag_fci[i].types[0].type = &loc;
2615 diag_fci[i].pointer_count = 1;
2617 if (t)
2619 i = find_char_info_specifier_index (diag_fci, 'J');
2620 diag_fci[i].types[0].type = &t;
2621 diag_fci[i].pointer_count = 1;
2622 i = find_char_info_specifier_index (diag_fci, 'K');
2623 diag_fci[i].types[0].type = &t;
2624 diag_fci[i].pointer_count = 1;
2627 /* Handle the __gcc_tdiag__ format specifics. */
2628 if (!tdiag_fci)
2629 dynamic_format_types[gcc_tdiag_format_type].conversion_specs =
2630 tdiag_fci = (format_char_info *)
2631 xmemdup (gcc_tdiag_char_table,
2632 sizeof (gcc_tdiag_char_table),
2633 sizeof (gcc_tdiag_char_table));
2634 if (loc)
2636 i = find_char_info_specifier_index (tdiag_fci, 'H');
2637 tdiag_fci[i].types[0].type = &loc;
2638 tdiag_fci[i].pointer_count = 1;
2640 if (t)
2642 /* All specifiers taking a tree share the same struct. */
2643 i = find_char_info_specifier_index (tdiag_fci, 'D');
2644 tdiag_fci[i].types[0].type = &t;
2645 tdiag_fci[i].pointer_count = 1;
2646 i = find_char_info_specifier_index (tdiag_fci, 'J');
2647 tdiag_fci[i].types[0].type = &t;
2648 tdiag_fci[i].pointer_count = 1;
2649 i = find_char_info_specifier_index (tdiag_fci, 'K');
2650 tdiag_fci[i].types[0].type = &t;
2651 tdiag_fci[i].pointer_count = 1;
2654 /* Handle the __gcc_cdiag__ format specifics. */
2655 if (!cdiag_fci)
2656 dynamic_format_types[gcc_cdiag_format_type].conversion_specs =
2657 cdiag_fci = (format_char_info *)
2658 xmemdup (gcc_cdiag_char_table,
2659 sizeof (gcc_cdiag_char_table),
2660 sizeof (gcc_cdiag_char_table));
2661 if (loc)
2663 i = find_char_info_specifier_index (cdiag_fci, 'H');
2664 cdiag_fci[i].types[0].type = &loc;
2665 cdiag_fci[i].pointer_count = 1;
2667 if (t)
2669 /* All specifiers taking a tree share the same struct. */
2670 i = find_char_info_specifier_index (cdiag_fci, 'D');
2671 cdiag_fci[i].types[0].type = &t;
2672 cdiag_fci[i].pointer_count = 1;
2673 i = find_char_info_specifier_index (cdiag_fci, 'J');
2674 cdiag_fci[i].types[0].type = &t;
2675 cdiag_fci[i].pointer_count = 1;
2676 i = find_char_info_specifier_index (cdiag_fci, 'K');
2677 cdiag_fci[i].types[0].type = &t;
2678 cdiag_fci[i].pointer_count = 1;
2681 /* Handle the __gcc_cxxdiag__ format specifics. */
2682 if (!cxxdiag_fci)
2683 dynamic_format_types[gcc_cxxdiag_format_type].conversion_specs =
2684 cxxdiag_fci = (format_char_info *)
2685 xmemdup (gcc_cxxdiag_char_table,
2686 sizeof (gcc_cxxdiag_char_table),
2687 sizeof (gcc_cxxdiag_char_table));
2688 if (loc)
2690 i = find_char_info_specifier_index (cxxdiag_fci, 'H');
2691 cxxdiag_fci[i].types[0].type = &loc;
2692 cxxdiag_fci[i].pointer_count = 1;
2694 if (t)
2696 /* All specifiers taking a tree share the same struct. */
2697 i = find_char_info_specifier_index (cxxdiag_fci, 'D');
2698 cxxdiag_fci[i].types[0].type = &t;
2699 cxxdiag_fci[i].pointer_count = 1;
2700 i = find_char_info_specifier_index (cxxdiag_fci, 'J');
2701 cxxdiag_fci[i].types[0].type = &t;
2702 cxxdiag_fci[i].pointer_count = 1;
2703 i = find_char_info_specifier_index (cxxdiag_fci, 'K');
2704 cxxdiag_fci[i].types[0].type = &t;
2705 cxxdiag_fci[i].pointer_count = 1;
2710 #ifdef TARGET_FORMAT_TYPES
2711 extern const format_kind_info TARGET_FORMAT_TYPES[];
2712 #endif
2714 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
2715 extern const target_ovr_attr TARGET_OVERRIDES_FORMAT_ATTRIBUTES[];
2716 #endif
2717 #ifdef TARGET_OVERRIDES_FORMAT_INIT
2718 extern void TARGET_OVERRIDES_FORMAT_INIT (void);
2719 #endif
2721 /* Attributes such as "printf" are equivalent to those such as
2722 "gnu_printf" unless this is overridden by a target. */
2723 static const target_ovr_attr gnu_target_overrides_format_attributes[] =
2725 { "gnu_printf", "printf" },
2726 { "gnu_scanf", "scanf" },
2727 { "gnu_strftime", "strftime" },
2728 { "gnu_strfmon", "strfmon" },
2729 { NULL, NULL }
2732 /* Translate to unified attribute name. This is used in decode_format_type and
2733 decode_format_attr. In attr_name the user specified argument is passed. It
2734 returns the unified format name from TARGET_OVERRIDES_FORMAT_ATTRIBUTES
2735 or the attr_name passed to this function, if there is no matching entry. */
2736 static const char *
2737 convert_format_name_to_system_name (const char *attr_name)
2739 int i;
2741 if (attr_name == NULL || *attr_name == 0
2742 || strncmp (attr_name, "gcc_", 4) == 0)
2743 return attr_name;
2744 #ifdef TARGET_OVERRIDES_FORMAT_INIT
2745 TARGET_OVERRIDES_FORMAT_INIT ();
2746 #endif
2748 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
2749 /* Check if format attribute is overridden by target. */
2750 if (TARGET_OVERRIDES_FORMAT_ATTRIBUTES != NULL
2751 && TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT > 0)
2753 for (i = 0; i < TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT; ++i)
2755 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_src,
2756 attr_name))
2757 return attr_name;
2758 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_dst,
2759 attr_name))
2760 return TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_src;
2763 #endif
2764 /* Otherwise default to gnu format. */
2765 for (i = 0;
2766 gnu_target_overrides_format_attributes[i].named_attr_src != NULL;
2767 ++i)
2769 if (cmp_attribs (gnu_target_overrides_format_attributes[i].named_attr_src,
2770 attr_name))
2771 return attr_name;
2772 if (cmp_attribs (gnu_target_overrides_format_attributes[i].named_attr_dst,
2773 attr_name))
2774 return gnu_target_overrides_format_attributes[i].named_attr_src;
2777 return attr_name;
2780 /* Return true if TATTR_NAME and ATTR_NAME are the same format attribute,
2781 counting "name" and "__name__" as the same, false otherwise. */
2782 static bool
2783 cmp_attribs (const char *tattr_name, const char *attr_name)
2785 int alen = strlen (attr_name);
2786 int slen = (tattr_name ? strlen (tattr_name) : 0);
2787 if (alen > 4 && attr_name[0] == '_' && attr_name[1] == '_'
2788 && attr_name[alen - 1] == '_' && attr_name[alen - 2] == '_')
2790 attr_name += 2;
2791 alen -= 4;
2793 if (alen != slen || strncmp (tattr_name, attr_name, alen) != 0)
2794 return false;
2795 return true;
2798 /* Handle a "format" attribute; arguments as in
2799 struct attribute_spec.handler. */
2800 tree
2801 handle_format_attribute (tree *node, tree ARG_UNUSED (name), tree args,
2802 int flags, bool *no_add_attrs)
2804 tree type = *node;
2805 function_format_info info;
2806 tree argument;
2808 #ifdef TARGET_FORMAT_TYPES
2809 /* If the target provides additional format types, we need to
2810 add them to FORMAT_TYPES at first use. */
2811 if (TARGET_FORMAT_TYPES != NULL && !dynamic_format_types)
2813 dynamic_format_types = XNEWVEC (format_kind_info,
2814 n_format_types + TARGET_N_FORMAT_TYPES);
2815 memcpy (dynamic_format_types, format_types_orig,
2816 sizeof (format_types_orig));
2817 memcpy (&dynamic_format_types[n_format_types], TARGET_FORMAT_TYPES,
2818 TARGET_N_FORMAT_TYPES * sizeof (dynamic_format_types[0]));
2820 format_types = dynamic_format_types;
2821 n_format_types += TARGET_N_FORMAT_TYPES;
2823 #endif
2825 if (!decode_format_attr (args, &info, 0))
2827 *no_add_attrs = true;
2828 return NULL_TREE;
2831 argument = TYPE_ARG_TYPES (type);
2832 if (argument)
2834 if (!check_format_string (argument, info.format_num, flags,
2835 no_add_attrs))
2836 return NULL_TREE;
2838 if (info.first_arg_num != 0)
2840 unsigned HOST_WIDE_INT arg_num = 1;
2842 /* Verify that first_arg_num points to the last arg,
2843 the ... */
2844 while (argument)
2845 arg_num++, argument = TREE_CHAIN (argument);
2847 if (arg_num != info.first_arg_num)
2849 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
2850 error ("args to be formatted is not %<...%>");
2851 *no_add_attrs = true;
2852 return NULL_TREE;
2857 /* Check if this is a strftime variant. Just for this variant
2858 FMT_FLAG_ARG_CONVERT is not set. */
2859 if ((format_types[info.format_type].flags & (int) FMT_FLAG_ARG_CONVERT) == 0
2860 && info.first_arg_num != 0)
2862 error ("strftime formats cannot format arguments");
2863 *no_add_attrs = true;
2864 return NULL_TREE;
2867 /* If this is a custom GCC-internal format type, we have to
2868 initialize certain bits a runtime. */
2869 if (info.format_type == asm_fprintf_format_type
2870 || info.format_type == gcc_gfc_format_type
2871 || info.format_type == gcc_diag_format_type
2872 || info.format_type == gcc_tdiag_format_type
2873 || info.format_type == gcc_cdiag_format_type
2874 || info.format_type == gcc_cxxdiag_format_type)
2876 /* Our first time through, we have to make sure that our
2877 format_type data is allocated dynamically and is modifiable. */
2878 if (!dynamic_format_types)
2879 format_types = dynamic_format_types = (format_kind_info *)
2880 xmemdup (format_types_orig, sizeof (format_types_orig),
2881 sizeof (format_types_orig));
2883 /* If this is format __asm_fprintf__, we have to initialize
2884 GCC's notion of HOST_WIDE_INT for checking %wd. */
2885 if (info.format_type == asm_fprintf_format_type)
2886 init_dynamic_asm_fprintf_info ();
2887 /* If this is format __gcc_gfc__, we have to initialize GCC's
2888 notion of 'locus' at runtime for %L. */
2889 else if (info.format_type == gcc_gfc_format_type)
2890 init_dynamic_gfc_info ();
2891 /* If this is one of the diagnostic attributes, then we have to
2892 initialize 'location_t' and 'tree' at runtime. */
2893 else if (info.format_type == gcc_diag_format_type
2894 || info.format_type == gcc_tdiag_format_type
2895 || info.format_type == gcc_cdiag_format_type
2896 || info.format_type == gcc_cxxdiag_format_type)
2897 init_dynamic_diag_info ();
2898 else
2899 gcc_unreachable ();
2902 return NULL_TREE;