* c-parser.c (c_parser_lex_all): Don't enforce location constraint
[official-gcc.git] / gcc / c-format.c
blobc1bb9235d8f9c3ed48427b6b243202cb6622ab8f
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 scanf_format_type, strftime_format_type,
66 strfmon_format_type, format_type_error = -1};
68 typedef struct function_format_info
70 int format_type; /* type of format (printf, scanf, etc.) */
71 unsigned HOST_WIDE_INT format_num; /* number of format argument */
72 unsigned HOST_WIDE_INT first_arg_num; /* number of first arg (zero for varargs) */
73 } function_format_info;
75 static bool decode_format_attr (tree, function_format_info *, int);
76 static int decode_format_type (const char *);
78 static bool check_format_string (tree argument,
79 unsigned HOST_WIDE_INT format_num,
80 int flags, bool *no_add_attrs);
81 static bool get_constant (tree expr, unsigned HOST_WIDE_INT *value,
82 int validated_p);
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 info->format_type = decode_format_type (p);
196 if (info->format_type == format_type_error)
198 gcc_assert (!validated_p);
199 warning (OPT_Wformat, "%qE is an unrecognized format function type",
200 format_type_id);
201 return false;
205 if (!get_constant (format_num_expr, &info->format_num, validated_p))
207 error ("format string has invalid operand number");
208 return false;
211 if (!get_constant (first_arg_num_expr, &info->first_arg_num, validated_p))
213 error ("%<...%> has invalid operand number");
214 return false;
217 if (info->first_arg_num != 0 && info->first_arg_num <= info->format_num)
219 gcc_assert (!validated_p);
220 error ("format string argument follows the args to be formatted");
221 return false;
224 return true;
227 /* Check a call to a format function against a parameter list. */
229 /* The C standard version C++ is treated as equivalent to
230 or inheriting from, for the purpose of format features supported. */
231 #define CPLUSPLUS_STD_VER STD_C94
232 /* The C standard version we are checking formats against when pedantic. */
233 #define C_STD_VER ((int) (c_dialect_cxx () \
234 ? CPLUSPLUS_STD_VER \
235 : (flag_isoc99 \
236 ? STD_C99 \
237 : (flag_isoc94 ? STD_C94 : STD_C89))))
238 /* The name to give to the standard version we are warning about when
239 pedantic. FEATURE_VER is the version in which the feature warned out
240 appeared, which is higher than C_STD_VER. */
241 #define C_STD_NAME(FEATURE_VER) (c_dialect_cxx () \
242 ? "ISO C++" \
243 : ((FEATURE_VER) == STD_EXT \
244 ? "ISO C" \
245 : "ISO C90"))
246 /* Adjust a C standard version, which may be STD_C9L, to account for
247 -Wno-long-long. Returns other standard versions unchanged. */
248 #define ADJ_STD(VER) ((int) ((VER) == STD_C9L \
249 ? (warn_long_long ? STD_C99 : STD_C89) \
250 : (VER)))
252 /* Structure describing details of a type expected in format checking,
253 and the type to check against it. */
254 typedef struct format_wanted_type
256 /* The type wanted. */
257 tree wanted_type;
258 /* The name of this type to use in diagnostics. */
259 const char *wanted_type_name;
260 /* The level of indirection through pointers at which this type occurs. */
261 int pointer_count;
262 /* Whether, when pointer_count is 1, to allow any character type when
263 pedantic, rather than just the character or void type specified. */
264 int char_lenient_flag;
265 /* Whether the argument, dereferenced once, is written into and so the
266 argument must not be a pointer to a const-qualified type. */
267 int writing_in_flag;
268 /* Whether the argument, dereferenced once, is read from and so
269 must not be a NULL pointer. */
270 int reading_from_flag;
271 /* If warnings should be of the form "field precision should have
272 type 'int'", the name to use (in this case "field precision"),
273 otherwise NULL, for "format expects type 'long'" type
274 messages. */
275 const char *name;
276 /* The actual parameter to check against the wanted type. */
277 tree param;
278 /* The argument number of that parameter. */
279 int arg_num;
280 /* The next type to check for this format conversion, or NULL if none. */
281 struct format_wanted_type *next;
282 } format_wanted_type;
285 static const format_length_info printf_length_specs[] =
287 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
288 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L },
289 { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
290 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
291 { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
292 { "Z", FMT_LEN_z, STD_EXT, NULL, 0, 0 },
293 { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
294 { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
295 { "H", FMT_LEN_H, STD_EXT, NULL, 0, 0 },
296 { "D", FMT_LEN_D, STD_EXT, "DD", FMT_LEN_DD, STD_EXT },
297 { NULL, 0, 0, NULL, 0, 0 }
300 /* Length specifiers valid for asm_fprintf. */
301 static const format_length_info asm_fprintf_length_specs[] =
303 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89 },
304 { "w", FMT_LEN_none, STD_C89, NULL, 0, 0 },
305 { NULL, 0, 0, NULL, 0, 0 }
308 /* Length specifiers valid for GCC diagnostics. */
309 static const format_length_info gcc_diag_length_specs[] =
311 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89 },
312 { "w", FMT_LEN_none, STD_C89, NULL, 0, 0 },
313 { NULL, 0, 0, NULL, 0, 0 }
316 /* The custom diagnostics all accept the same length specifiers. */
317 #define gcc_tdiag_length_specs gcc_diag_length_specs
318 #define gcc_cdiag_length_specs gcc_diag_length_specs
319 #define gcc_cxxdiag_length_specs gcc_diag_length_specs
321 /* This differs from printf_length_specs only in that "Z" is not accepted. */
322 static const format_length_info scanf_length_specs[] =
324 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
325 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L },
326 { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
327 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
328 { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
329 { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
330 { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
331 { "H", FMT_LEN_H, STD_EXT, NULL, 0, 0 },
332 { "D", FMT_LEN_D, STD_EXT, "DD", FMT_LEN_DD, STD_EXT },
333 { NULL, 0, 0, NULL, 0, 0 }
337 /* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings
338 make no sense for a format type not part of any C standard version. */
339 static const format_length_info strfmon_length_specs[] =
341 /* A GNU extension. */
342 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
343 { NULL, 0, 0, NULL, 0, 0 }
347 /* For now, the Fortran front-end routines only use l as length modifier. */
348 static const format_length_info gcc_gfc_length_specs[] =
350 { "l", FMT_LEN_l, STD_C89, NULL, 0, 0 },
351 { NULL, 0, 0, NULL, 0, 0 }
355 static const format_flag_spec printf_flag_specs[] =
357 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
358 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
359 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
360 { '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
361 { '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
362 { '\'', 0, 0, N_("''' flag"), N_("the ''' printf flag"), STD_EXT },
363 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' printf flag"), STD_EXT },
364 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
365 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
366 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
367 { 0, 0, 0, NULL, NULL, 0 }
371 static const format_flag_pair printf_flag_pairs[] =
373 { ' ', '+', 1, 0 },
374 { '0', '-', 1, 0 },
375 { '0', 'p', 1, 'i' },
376 { 0, 0, 0, 0 }
379 static const format_flag_spec asm_fprintf_flag_specs[] =
381 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
382 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
383 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
384 { '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
385 { '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
386 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
387 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
388 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
389 { 0, 0, 0, NULL, NULL, 0 }
392 static const format_flag_pair asm_fprintf_flag_pairs[] =
394 { ' ', '+', 1, 0 },
395 { '0', '-', 1, 0 },
396 { '0', 'p', 1, 'i' },
397 { 0, 0, 0, 0 }
400 static const format_flag_pair gcc_diag_flag_pairs[] =
402 { 0, 0, 0, 0 }
405 #define gcc_tdiag_flag_pairs gcc_diag_flag_pairs
406 #define gcc_cdiag_flag_pairs gcc_diag_flag_pairs
407 #define gcc_cxxdiag_flag_pairs gcc_diag_flag_pairs
409 static const format_flag_pair gcc_gfc_flag_pairs[] =
411 { 0, 0, 0, 0 }
414 static const format_flag_spec gcc_diag_flag_specs[] =
416 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
417 { 'q', 0, 0, N_("'q' flag"), N_("the 'q' diagnostic flag"), STD_C89 },
418 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
419 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
420 { 0, 0, 0, NULL, NULL, 0 }
423 #define gcc_tdiag_flag_specs gcc_diag_flag_specs
424 #define gcc_cdiag_flag_specs gcc_diag_flag_specs
426 static const format_flag_spec gcc_cxxdiag_flag_specs[] =
428 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
429 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
430 { 'q', 0, 0, N_("'q' flag"), N_("the 'q' diagnostic flag"), STD_C89 },
431 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
432 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
433 { 0, 0, 0, NULL, NULL, 0 }
436 static const format_flag_spec scanf_flag_specs[] =
438 { '*', 0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89 },
439 { 'a', 0, 0, N_("'a' flag"), N_("the 'a' scanf flag"), STD_EXT },
440 { 'm', 0, 0, N_("'m' flag"), N_("the 'm' scanf flag"), STD_EXT },
441 { 'w', 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89 },
442 { 'L', 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89 },
443 { '\'', 0, 0, N_("''' flag"), N_("the ''' scanf flag"), STD_EXT },
444 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' scanf flag"), STD_EXT },
445 { 0, 0, 0, NULL, NULL, 0 }
449 static const format_flag_pair scanf_flag_pairs[] =
451 { '*', 'L', 0, 0 },
452 { 'a', 'm', 0, 0 },
453 { 0, 0, 0, 0 }
457 static const format_flag_spec strftime_flag_specs[] =
459 { '_', 0, 0, N_("'_' flag"), N_("the '_' strftime flag"), STD_EXT },
460 { '-', 0, 0, N_("'-' flag"), N_("the '-' strftime flag"), STD_EXT },
461 { '0', 0, 0, N_("'0' flag"), N_("the '0' strftime flag"), STD_EXT },
462 { '^', 0, 0, N_("'^' flag"), N_("the '^' strftime flag"), STD_EXT },
463 { '#', 0, 0, N_("'#' flag"), N_("the '#' strftime flag"), STD_EXT },
464 { 'w', 0, 0, N_("field width"), N_("field width in strftime format"), STD_EXT },
465 { 'E', 0, 0, N_("'E' modifier"), N_("the 'E' strftime modifier"), STD_C99 },
466 { 'O', 0, 0, N_("'O' modifier"), N_("the 'O' strftime modifier"), STD_C99 },
467 { 'O', 'o', 0, NULL, N_("the 'O' modifier"), STD_EXT },
468 { 0, 0, 0, NULL, NULL, 0 }
472 static const format_flag_pair strftime_flag_pairs[] =
474 { 'E', 'O', 0, 0 },
475 { '_', '-', 0, 0 },
476 { '_', '0', 0, 0 },
477 { '-', '0', 0, 0 },
478 { '^', '#', 0, 0 },
479 { 0, 0, 0, 0 }
483 static const format_flag_spec strfmon_flag_specs[] =
485 { '=', 0, 1, N_("fill character"), N_("fill character in strfmon format"), STD_C89 },
486 { '^', 0, 0, N_("'^' flag"), N_("the '^' strfmon flag"), STD_C89 },
487 { '+', 0, 0, N_("'+' flag"), N_("the '+' strfmon flag"), STD_C89 },
488 { '(', 0, 0, N_("'(' flag"), N_("the '(' strfmon flag"), STD_C89 },
489 { '!', 0, 0, N_("'!' flag"), N_("the '!' strfmon flag"), STD_C89 },
490 { '-', 0, 0, N_("'-' flag"), N_("the '-' strfmon flag"), STD_C89 },
491 { 'w', 0, 0, N_("field width"), N_("field width in strfmon format"), STD_C89 },
492 { '#', 0, 0, N_("left precision"), N_("left precision in strfmon format"), STD_C89 },
493 { 'p', 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89 },
494 { 'L', 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89 },
495 { 0, 0, 0, NULL, NULL, 0 }
498 static const format_flag_pair strfmon_flag_pairs[] =
500 { '+', '(', 0, 0 },
501 { 0, 0, 0, 0 }
505 static const format_char_info print_char_table[] =
507 /* C89 conversion specifiers. */
508 { "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 },
509 { "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 },
510 { "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 },
511 { "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 },
512 { "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 },
513 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, T94_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
514 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
515 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "c", NULL },
516 { "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 },
517 /* C99 conversion specifiers. */
518 { "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 },
519 { "aA", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "", NULL },
520 /* X/Open conversion specifiers. */
521 { "C", 0, STD_EXT, { TEX_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
522 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "R", NULL },
523 /* GNU conversion specifiers. */
524 { "m", 0, STD_EXT, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "", NULL },
525 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
528 static const format_char_info asm_fprintf_char_table[] =
530 /* C89 conversion specifiers. */
531 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +", "i", NULL },
532 { "oxX", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0#", "i", NULL },
533 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0", "i", NULL },
534 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
535 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
537 /* asm_fprintf conversion specifiers. */
538 { "O", 0, STD_C89, NOARGUMENTS, "", "", NULL },
539 { "R", 0, STD_C89, NOARGUMENTS, "", "", NULL },
540 { "I", 0, STD_C89, NOARGUMENTS, "", "", NULL },
541 { "L", 0, STD_C89, NOARGUMENTS, "", "", NULL },
542 { "U", 0, STD_C89, NOARGUMENTS, "", "", NULL },
543 { "r", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
544 { "@", 0, STD_C89, NOARGUMENTS, "", "", NULL },
545 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
548 static const format_char_info gcc_diag_char_table[] =
550 /* C89 conversion specifiers. */
551 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
552 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
553 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
554 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
555 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
556 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
558 /* Custom conversion specifiers. */
560 /* %H will require "location_t" at runtime. */
561 { "H", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
563 /* These will require a "tree" at runtime. */
564 { "JK", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
566 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
567 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
568 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
571 static const format_char_info gcc_tdiag_char_table[] =
573 /* C89 conversion specifiers. */
574 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
575 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
576 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
577 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
578 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
579 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
581 /* Custom conversion specifiers. */
583 /* %H will require "location_t" at runtime. */
584 { "H", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
586 /* These will require a "tree" at runtime. */
587 { "DFJKT", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
589 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
590 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
591 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
594 static const format_char_info gcc_cdiag_char_table[] =
596 /* C89 conversion specifiers. */
597 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
598 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
599 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
600 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
601 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
602 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
604 /* Custom conversion specifiers. */
606 /* %H will require "location_t" at runtime. */
607 { "H", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
609 /* These will require a "tree" at runtime. */
610 { "DEFJKT", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
612 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
613 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
614 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
617 static const format_char_info gcc_cxxdiag_char_table[] =
619 /* C89 conversion specifiers. */
620 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
621 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
622 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
623 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
624 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
625 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
627 /* Custom conversion specifiers. */
629 /* %H will require "location_t" at runtime. */
630 { "H", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
632 /* These will require a "tree" at runtime. */
633 { "ADEFJKTV",0,STD_C89,{ T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+#", "", NULL },
635 /* These accept either an 'int' or an 'enum tree_code' (which is handled as an 'int'.) */
636 { "CLOPQ",0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
638 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
639 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
640 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
643 static const format_char_info gcc_gfc_char_table[] =
645 /* C89 conversion specifiers. */
646 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
647 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
648 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
649 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL },
651 /* gfc conversion specifiers. */
653 { "C", 0, STD_C89, NOARGUMENTS, "", "", NULL },
655 /* This will require a "locus" at runtime. */
656 { "L", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "R", NULL },
658 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
661 static const format_char_info scan_char_table[] =
663 /* C89 conversion specifiers. */
664 { "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 },
665 { "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 },
666 { "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 },
667 { "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 },
668 { "c", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*mw", "cW", NULL },
669 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "cW", NULL },
670 { "[", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "cW[", NULL },
671 { "p", 2, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
672 { "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 },
673 /* C99 conversion specifiers. */
674 { "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 },
675 { "aA", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w'", "W", NULL },
676 /* X/Open conversion specifiers. */
677 { "C", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*mw", "W", NULL },
678 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "W", NULL },
679 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
682 static const format_char_info time_char_table[] =
684 /* C89 conversion specifiers. */
685 { "ABZab", 0, STD_C89, NOLENGTHS, "^#", "", NULL },
686 { "cx", 0, STD_C89, NOLENGTHS, "E", "3", NULL },
687 { "HIMSUWdmw", 0, STD_C89, NOLENGTHS, "-_0Ow", "", NULL },
688 { "j", 0, STD_C89, NOLENGTHS, "-_0Ow", "o", NULL },
689 { "p", 0, STD_C89, NOLENGTHS, "#", "", NULL },
690 { "X", 0, STD_C89, NOLENGTHS, "E", "", NULL },
691 { "y", 0, STD_C89, NOLENGTHS, "EO-_0w", "4", NULL },
692 { "Y", 0, STD_C89, NOLENGTHS, "-_0EOw", "o", NULL },
693 { "%", 0, STD_C89, NOLENGTHS, "", "", NULL },
694 /* C99 conversion specifiers. */
695 { "C", 0, STD_C99, NOLENGTHS, "-_0EOw", "o", NULL },
696 { "D", 0, STD_C99, NOLENGTHS, "", "2", NULL },
697 { "eVu", 0, STD_C99, NOLENGTHS, "-_0Ow", "", NULL },
698 { "FRTnrt", 0, STD_C99, NOLENGTHS, "", "", NULL },
699 { "g", 0, STD_C99, NOLENGTHS, "O-_0w", "2o", NULL },
700 { "G", 0, STD_C99, NOLENGTHS, "-_0Ow", "o", NULL },
701 { "h", 0, STD_C99, NOLENGTHS, "^#", "", NULL },
702 { "z", 0, STD_C99, NOLENGTHS, "O", "o", NULL },
703 /* GNU conversion specifiers. */
704 { "kls", 0, STD_EXT, NOLENGTHS, "-_0Ow", "", NULL },
705 { "P", 0, STD_EXT, NOLENGTHS, "", "", NULL },
706 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
709 static const format_char_info monetary_char_table[] =
711 { "in", 0, STD_C89, { T89_D, BADLEN, BADLEN, BADLEN, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "=^+(!-w#p", "", NULL },
712 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
715 /* This must be in the same order as enum format_type. */
716 static const format_kind_info format_types_orig[] =
718 { "printf", printf_length_specs, print_char_table, " +#0-'I", NULL,
719 printf_flag_specs, printf_flag_pairs,
720 FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_EMPTY_PREC_OK,
721 'w', 0, 'p', 0, 'L', 0,
722 &integer_type_node, &integer_type_node
724 { "asm_fprintf", asm_fprintf_length_specs, asm_fprintf_char_table, " +#0-", NULL,
725 asm_fprintf_flag_specs, asm_fprintf_flag_pairs,
726 FMT_FLAG_ARG_CONVERT|FMT_FLAG_EMPTY_PREC_OK,
727 'w', 0, 'p', 0, 'L', 0,
728 NULL, NULL
730 { "gcc_diag", gcc_diag_length_specs, gcc_diag_char_table, "q+", NULL,
731 gcc_diag_flag_specs, gcc_diag_flag_pairs,
732 FMT_FLAG_ARG_CONVERT,
733 0, 0, 'p', 0, 'L', 0,
734 NULL, &integer_type_node
736 { "gcc_tdiag", gcc_tdiag_length_specs, gcc_tdiag_char_table, "q+", NULL,
737 gcc_tdiag_flag_specs, gcc_tdiag_flag_pairs,
738 FMT_FLAG_ARG_CONVERT,
739 0, 0, 'p', 0, 'L', 0,
740 NULL, &integer_type_node
742 { "gcc_cdiag", gcc_cdiag_length_specs, gcc_cdiag_char_table, "q+", NULL,
743 gcc_cdiag_flag_specs, gcc_cdiag_flag_pairs,
744 FMT_FLAG_ARG_CONVERT,
745 0, 0, 'p', 0, 'L', 0,
746 NULL, &integer_type_node
748 { "gcc_cxxdiag", gcc_cxxdiag_length_specs, gcc_cxxdiag_char_table, "q+#", NULL,
749 gcc_cxxdiag_flag_specs, gcc_cxxdiag_flag_pairs,
750 FMT_FLAG_ARG_CONVERT,
751 0, 0, 'p', 0, 'L', 0,
752 NULL, &integer_type_node
754 { "gcc_gfc", gcc_gfc_length_specs, gcc_gfc_char_table, "", NULL,
755 NULL, gcc_gfc_flag_pairs,
756 FMT_FLAG_ARG_CONVERT,
757 0, 0, 0, 0, 0, 0,
758 NULL, NULL
760 { "scanf", scanf_length_specs, scan_char_table, "*'I", NULL,
761 scanf_flag_specs, scanf_flag_pairs,
762 FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_ZERO_WIDTH_BAD|FMT_FLAG_DOLLAR_GAP_POINTER_OK,
763 'w', 0, 0, '*', 'L', 'm',
764 NULL, NULL
766 { "strftime", NULL, time_char_table, "_-0^#", "EO",
767 strftime_flag_specs, strftime_flag_pairs,
768 FMT_FLAG_FANCY_PERCENT_OK, 'w', 0, 0, 0, 0, 0,
769 NULL, NULL
771 { "strfmon", strfmon_length_specs, monetary_char_table, "=^+(!-", NULL,
772 strfmon_flag_specs, strfmon_flag_pairs,
773 FMT_FLAG_ARG_CONVERT, 'w', '#', 'p', 0, 'L', 0,
774 NULL, NULL
778 /* This layer of indirection allows GCC to reassign format_types with
779 new data if necessary, while still allowing the original data to be
780 const. */
781 static const format_kind_info *format_types = format_types_orig;
782 /* We can modify this one. We also add target-specific format types
783 to the end of the array. */
784 static format_kind_info *dynamic_format_types;
786 static int n_format_types = ARRAY_SIZE (format_types_orig);
788 /* An ad-hoc structure holding any global state needed by functions in
789 this module. */
790 struct format_state GTY (())
792 /* Memory can be allocated on this obstack by functions in this
793 module. The obstack is deleted at module cleanup time. */
794 struct obstack GTY ((skip)) obstack;
796 /* State needed for init_dynamic_asm_fprintf_info. */
797 tree fprintf_hwi;
799 /* State needed for init_dynamic_gfc_info. */
800 tree locus;
801 format_char_info * GTY ((skip)) gfc_fci;
803 /* State needed for init_dynamic_diag_info. */
804 tree diag_t, diag_loc, diag_hwi;
805 format_char_info * GTY ((skip)) diag_fci;
806 format_char_info * GTY ((skip)) tdiag_fci;
807 format_char_info * GTY ((skip)) cdiag_fci;
808 format_char_info * GTY ((skip)) cxxdiag_fci;
809 format_length_info * GTY ((skip)) diag_ls;
812 /* The global format_state object. */
813 static GTY (()) struct format_state *global_format_state;
815 /* Structure detailing the results of checking a format function call
816 where the format expression may be a conditional expression with
817 many leaves resulting from nested conditional expressions. */
818 typedef struct
820 /* Number of leaves of the format argument that could not be checked
821 as they were not string literals. */
822 int number_non_literal;
823 /* Number of leaves of the format argument that were null pointers or
824 string literals, but had extra format arguments. */
825 int number_extra_args;
826 /* Number of leaves of the format argument that were null pointers or
827 string literals, but had extra format arguments and used $ operand
828 numbers. */
829 int number_dollar_extra_args;
830 /* Number of leaves of the format argument that were wide string
831 literals. */
832 int number_wide;
833 /* Number of leaves of the format argument that were empty strings. */
834 int number_empty;
835 /* Number of leaves of the format argument that were unterminated
836 strings. */
837 int number_unterminated;
838 /* Number of leaves of the format argument that were not counted above. */
839 int number_other;
840 } format_check_results;
842 typedef struct
844 format_check_results *res;
845 function_format_info *info;
846 tree params;
847 } format_check_context;
849 /* State used by the checking of $ operand number formats. */
850 typedef struct dollar_argument_info
852 char *dollar_arguments_used;
853 char *dollar_arguments_pointer_p;
854 int dollar_arguments_alloc;
855 int dollar_arguments_count;
856 int dollar_first_arg_num;
857 int dollar_max_arg_used;
858 int dollar_format_warned;
859 } dollar_argument_info;
862 static void check_format_info (function_format_info *, tree);
863 static void check_format_arg (void *, tree, unsigned HOST_WIDE_INT);
864 static void check_format_info_inner (dollar_argument_info *,
865 format_check_results *,
866 function_format_info *,
867 const char *, int, tree,
868 unsigned HOST_WIDE_INT,
869 alloc_pool);
870 static void check_format_info_main (format_check_results *,
871 function_format_info *,
872 const char *, int, tree,
873 unsigned HOST_WIDE_INT, alloc_pool);
875 static void init_dollar_format_checking (dollar_argument_info *, int, tree);
876 static int maybe_read_dollar_number (dollar_argument_info *,
877 const char **, int,
878 tree, tree *, const format_kind_info *);
879 static bool avoid_dollar_number (const char *);
880 static void finish_dollar_format_checking (dollar_argument_info *,
881 format_check_results *, int);
883 static const format_flag_spec *get_flag_spec (const format_flag_spec *,
884 int, const char *);
886 static void check_format_types (format_wanted_type *, const char *, int);
887 static void format_type_warning (const char *, const char *, int, tree,
888 int, const char *, tree, int);
892 /* Create an instance of this module. */
893 static void
894 construct_format_state (void)
896 gcc_assert (!global_format_state);
897 /* For the time being this is a singleton module. */
898 global_format_state = GGC_CNEW (struct format_state);
899 gcc_obstack_init (&global_format_state->obstack);
902 /* Destroy the module instance. */
903 void
904 c_format_finalize (void)
906 if (global_format_state)
908 obstack_free (&global_format_state->obstack, NULL);
909 global_format_state = NULL;
910 /* dynamic_format_types was allocated on the obstack. */
911 dynamic_format_types = NULL;
912 format_types = format_types_orig;
913 n_format_types = ARRAY_SIZE (format_types_orig);
917 /* Like xmemdup but use our obstack. */
918 static void *
919 obdup_mem (const void *mem, size_t size)
921 void *result = obstack_alloc (&global_format_state->obstack, size);
922 memcpy (result, mem, size);
923 return result;
928 /* Decode a format type from a string, returning the type, or
929 format_type_error if not valid, in which case the caller should print an
930 error message. */
931 static int
932 decode_format_type (const char *s)
934 int i;
935 int slen;
936 slen = strlen (s);
937 for (i = 0; i < n_format_types; i++)
939 int alen;
940 if (!strcmp (s, format_types[i].name))
941 return i;
942 alen = strlen (format_types[i].name);
943 if (slen == alen + 4 && s[0] == '_' && s[1] == '_'
944 && s[slen - 1] == '_' && s[slen - 2] == '_'
945 && !strncmp (s + 2, format_types[i].name, alen))
946 return i;
948 return format_type_error;
952 /* Check the argument list of a call to printf, scanf, etc.
953 ATTRS are the attributes on the function type. There are NARGS argument
954 values in the array ARGARRAY.
955 Also, if -Wmissing-format-attribute,
956 warn for calls to vprintf or vscanf in functions with no such format
957 attribute themselves. */
959 void
960 check_function_format (tree attrs, int nargs, tree *argarray)
962 tree a;
964 /* See if this function has any format attributes. */
965 for (a = attrs; a; a = TREE_CHAIN (a))
967 if (is_attribute_p ("format", TREE_PURPOSE (a)))
969 /* Yup; check it. */
970 function_format_info info;
971 decode_format_attr (TREE_VALUE (a), &info, 1);
972 if (warn_format)
974 /* FIXME: Rewrite all the internal functions in this file
975 to use the ARGARRAY directly instead of constructing this
976 temporary list. */
977 tree params = NULL_TREE;
978 int i;
979 for (i = nargs - 1; i >= 0; i--)
980 params = tree_cons (NULL_TREE, argarray[i], params);
981 check_format_info (&info, params);
983 if (warn_missing_format_attribute && info.first_arg_num == 0
984 && (format_types[info.format_type].flags
985 & (int) FMT_FLAG_ARG_CONVERT))
987 tree c;
988 for (c = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
990 c = TREE_CHAIN (c))
991 if (is_attribute_p ("format", TREE_PURPOSE (c))
992 && (decode_format_type (IDENTIFIER_POINTER
993 (TREE_VALUE (TREE_VALUE (c))))
994 == info.format_type))
995 break;
996 if (c == NULL_TREE)
998 /* Check if the current function has a parameter to which
999 the format attribute could be attached; if not, it
1000 can't be a candidate for a format attribute, despite
1001 the vprintf-like or vscanf-like call. */
1002 tree args;
1003 for (args = DECL_ARGUMENTS (current_function_decl);
1004 args != 0;
1005 args = TREE_CHAIN (args))
1007 if (TREE_CODE (TREE_TYPE (args)) == POINTER_TYPE
1008 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args)))
1009 == char_type_node))
1010 break;
1012 if (args != 0)
1013 warning (OPT_Wmissing_format_attribute, "function might "
1014 "be possible candidate for %qs format attribute",
1015 format_types[info.format_type].name);
1023 /* Initialize the checking for a format string that may contain $
1024 parameter number specifications; we will need to keep track of whether
1025 each parameter has been used. FIRST_ARG_NUM is the number of the first
1026 argument that is a parameter to the format, or 0 for a vprintf-style
1027 function; PARAMS is the list of arguments starting at this argument. */
1029 static void
1030 init_dollar_format_checking (dollar_argument_info *state,
1031 int first_arg_num, tree params)
1033 tree oparams = params;
1035 state->dollar_first_arg_num = first_arg_num;
1036 state->dollar_arguments_count = 0;
1037 state->dollar_max_arg_used = 0;
1038 state->dollar_format_warned = 0;
1039 if (first_arg_num > 0)
1041 while (params)
1043 state->dollar_arguments_count++;
1044 params = TREE_CHAIN (params);
1048 state->dollar_arguments_alloc = state->dollar_arguments_count;
1049 if (state->dollar_arguments_count)
1051 state->dollar_arguments_used = XNEWVEC (char,
1052 state->dollar_arguments_alloc);
1053 state->dollar_arguments_pointer_p = XNEWVEC (char,
1054 state->dollar_arguments_alloc);
1056 else
1058 state->dollar_arguments_used = NULL;
1059 state->dollar_arguments_pointer_p = NULL;
1062 if (state->dollar_arguments_alloc)
1064 memset (state->dollar_arguments_used, 0, state->dollar_arguments_alloc);
1065 if (first_arg_num > 0)
1067 int i = 0;
1068 params = oparams;
1069 while (params)
1071 state->dollar_arguments_pointer_p[i] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params)))
1072 == POINTER_TYPE);
1073 params = TREE_CHAIN (params);
1074 i++;
1081 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
1082 is set, it is an error if one is not found; otherwise, it is OK. If
1083 such a number is found, check whether it is within range and mark that
1084 numbered operand as being used for later checking. Returns the operand
1085 number if found and within range, zero if no such number was found and
1086 this is OK, or -1 on error. PARAMS points to the first operand of the
1087 format; PARAM_PTR is made to point to the parameter referred to. If
1088 a $ format is found, *FORMAT is updated to point just after it. */
1090 static int
1091 maybe_read_dollar_number (dollar_argument_info *state,
1092 const char **format,
1093 int dollar_needed, tree params, tree *param_ptr,
1094 const format_kind_info *fki)
1096 int argnum;
1097 int overflow_flag;
1098 const char *fcp = *format;
1099 if (!ISDIGIT (*fcp))
1101 if (dollar_needed)
1103 warning (OPT_Wformat, "missing $ operand number in format");
1104 return -1;
1106 else
1107 return 0;
1109 argnum = 0;
1110 overflow_flag = 0;
1111 while (ISDIGIT (*fcp))
1113 int nargnum;
1114 nargnum = 10 * argnum + (*fcp - '0');
1115 if (nargnum < 0 || nargnum / 10 != argnum)
1116 overflow_flag = 1;
1117 argnum = nargnum;
1118 fcp++;
1120 if (*fcp != '$')
1122 if (dollar_needed)
1124 warning (OPT_Wformat, "missing $ operand number in format");
1125 return -1;
1127 else
1128 return 0;
1130 *format = fcp + 1;
1131 if (pedantic && !state->dollar_format_warned)
1133 warning (OPT_Wformat, "%s does not support %%n$ operand number formats",
1134 C_STD_NAME (STD_EXT));
1135 state->dollar_format_warned = 1;
1137 if (overflow_flag || argnum == 0
1138 || (state->dollar_first_arg_num
1139 && argnum > state->dollar_arguments_count))
1141 warning (OPT_Wformat, "operand number out of range in format");
1142 return -1;
1144 if (argnum > state->dollar_max_arg_used)
1145 state->dollar_max_arg_used = argnum;
1146 /* For vprintf-style functions we may need to allocate more memory to
1147 track which arguments are used. */
1148 while (state->dollar_arguments_alloc < state->dollar_max_arg_used)
1150 int nalloc;
1151 nalloc = 2 * state->dollar_arguments_alloc + 16;
1152 state->dollar_arguments_used = XRESIZEVEC (char,
1153 state->dollar_arguments_used,
1154 nalloc);
1155 state->dollar_arguments_pointer_p = XRESIZEVEC (char,
1156 state->dollar_arguments_pointer_p,
1157 nalloc);
1158 memset (state->dollar_arguments_used + state->dollar_arguments_alloc, 0,
1159 nalloc - state->dollar_arguments_alloc);
1160 state->dollar_arguments_alloc = nalloc;
1162 if (!(fki->flags & (int) FMT_FLAG_DOLLAR_MULTIPLE)
1163 && state->dollar_arguments_used[argnum - 1] == 1)
1165 state->dollar_arguments_used[argnum - 1] = 2;
1166 warning (OPT_Wformat, "format argument %d used more than once in %s format",
1167 argnum, fki->name);
1169 else
1170 state->dollar_arguments_used[argnum - 1] = 1;
1171 if (state->dollar_first_arg_num)
1173 int i;
1174 *param_ptr = params;
1175 for (i = 1; i < argnum && *param_ptr != 0; i++)
1176 *param_ptr = TREE_CHAIN (*param_ptr);
1178 /* This case shouldn't be caught here. */
1179 gcc_assert (*param_ptr);
1181 else
1182 *param_ptr = 0;
1183 return argnum;
1186 /* Ensure that FORMAT does not start with a decimal number followed by
1187 a $; give a diagnostic and return true if it does, false otherwise. */
1189 static bool
1190 avoid_dollar_number (const char *format)
1192 if (!ISDIGIT (*format))
1193 return false;
1194 while (ISDIGIT (*format))
1195 format++;
1196 if (*format == '$')
1198 warning (OPT_Wformat, "$ operand number used after format without operand number");
1199 return true;
1201 return false;
1205 /* Finish the checking for a format string that used $ operand number formats
1206 instead of non-$ formats. We check for unused operands before used ones
1207 (a serious error, since the implementation of the format function
1208 can't know what types to pass to va_arg to find the later arguments).
1209 and for unused operands at the end of the format (if we know how many
1210 arguments the format had, so not for vprintf). If there were operand
1211 numbers out of range on a non-vprintf-style format, we won't have reached
1212 here. If POINTER_GAP_OK, unused arguments are OK if all arguments are
1213 pointers. */
1215 static void
1216 finish_dollar_format_checking (dollar_argument_info *state,
1217 format_check_results *res, int pointer_gap_ok)
1219 int i;
1220 bool found_pointer_gap = false;
1221 for (i = 0; i < state->dollar_max_arg_used; i++)
1223 if (!state->dollar_arguments_used[i])
1225 if (pointer_gap_ok && (state->dollar_first_arg_num == 0
1226 || state->dollar_arguments_pointer_p[i]))
1227 found_pointer_gap = true;
1228 else
1229 warning (OPT_Wformat,
1230 "format argument %d unused before used argument %d in $-style format",
1231 i + 1, state->dollar_max_arg_used);
1234 if (found_pointer_gap
1235 || (state->dollar_first_arg_num
1236 && state->dollar_max_arg_used < state->dollar_arguments_count))
1238 res->number_other--;
1239 res->number_dollar_extra_args++;
1244 /* Retrieve the specification for a format flag. SPEC contains the
1245 specifications for format flags for the applicable kind of format.
1246 FLAG is the flag in question. If PREDICATES is NULL, the basic
1247 spec for that flag must be retrieved and must exist. If
1248 PREDICATES is not NULL, it is a string listing possible predicates
1249 for the spec entry; if an entry predicated on any of these is
1250 found, it is returned, otherwise NULL is returned. */
1252 static const format_flag_spec *
1253 get_flag_spec (const format_flag_spec *spec, int flag, const char *predicates)
1255 int i;
1256 for (i = 0; spec[i].flag_char != 0; i++)
1258 if (spec[i].flag_char != flag)
1259 continue;
1260 if (predicates != NULL)
1262 if (spec[i].predicate != 0
1263 && strchr (predicates, spec[i].predicate) != 0)
1264 return &spec[i];
1266 else if (spec[i].predicate == 0)
1267 return &spec[i];
1269 gcc_assert (predicates);
1270 return NULL;
1274 /* Check the argument list of a call to printf, scanf, etc.
1275 INFO points to the function_format_info structure.
1276 PARAMS is the list of argument values. */
1278 static void
1279 check_format_info (function_format_info *info, tree params)
1281 format_check_context format_ctx;
1282 unsigned HOST_WIDE_INT arg_num;
1283 tree format_tree;
1284 format_check_results res;
1285 /* Skip to format argument. If the argument isn't available, there's
1286 no work for us to do; prototype checking will catch the problem. */
1287 for (arg_num = 1; ; ++arg_num)
1289 if (params == 0)
1290 return;
1291 if (arg_num == info->format_num)
1292 break;
1293 params = TREE_CHAIN (params);
1295 format_tree = TREE_VALUE (params);
1296 params = TREE_CHAIN (params);
1297 if (format_tree == 0)
1298 return;
1300 res.number_non_literal = 0;
1301 res.number_extra_args = 0;
1302 res.number_dollar_extra_args = 0;
1303 res.number_wide = 0;
1304 res.number_empty = 0;
1305 res.number_unterminated = 0;
1306 res.number_other = 0;
1308 format_ctx.res = &res;
1309 format_ctx.info = info;
1310 format_ctx.params = params;
1312 check_function_arguments_recurse (check_format_arg, &format_ctx,
1313 format_tree, arg_num);
1315 if (res.number_non_literal > 0)
1317 /* Functions taking a va_list normally pass a non-literal format
1318 string. These functions typically are declared with
1319 first_arg_num == 0, so avoid warning in those cases. */
1320 if (!(format_types[info->format_type].flags & (int) FMT_FLAG_ARG_CONVERT))
1322 /* For strftime-like formats, warn for not checking the format
1323 string; but there are no arguments to check. */
1324 warning (OPT_Wformat_nonliteral,
1325 "format not a string literal, format string not checked");
1327 else if (info->first_arg_num != 0)
1329 /* If there are no arguments for the format at all, we may have
1330 printf (foo) which is likely to be a security hole. */
1331 while (arg_num + 1 < info->first_arg_num)
1333 if (params == 0)
1334 break;
1335 params = TREE_CHAIN (params);
1336 ++arg_num;
1338 if (params == 0 && warn_format_security)
1339 warning (OPT_Wformat_security,
1340 "format not a string literal and no format arguments");
1341 else if (params == 0 && warn_format_nonliteral)
1342 warning (OPT_Wformat_nonliteral,
1343 "format not a string literal and no format arguments");
1344 else
1345 warning (OPT_Wformat_nonliteral,
1346 "format not a string literal, argument types not checked");
1350 /* If there were extra arguments to the format, normally warn. However,
1351 the standard does say extra arguments are ignored, so in the specific
1352 case where we have multiple leaves (conditional expressions or
1353 ngettext) allow extra arguments if at least one leaf didn't have extra
1354 arguments, but was otherwise OK (either non-literal or checked OK).
1355 If the format is an empty string, this should be counted similarly to the
1356 case of extra format arguments. */
1357 if (res.number_extra_args > 0 && res.number_non_literal == 0
1358 && res.number_other == 0)
1359 warning (OPT_Wformat_extra_args, "too many arguments for format");
1360 if (res.number_dollar_extra_args > 0 && res.number_non_literal == 0
1361 && res.number_other == 0)
1362 warning (OPT_Wformat_extra_args, "unused arguments in $-style format");
1363 if (res.number_empty > 0 && res.number_non_literal == 0
1364 && res.number_other == 0)
1365 warning (OPT_Wformat_zero_length, "zero-length %s format string",
1366 format_types[info->format_type].name);
1368 if (res.number_wide > 0)
1369 warning (OPT_Wformat, "format is a wide character string");
1371 if (res.number_unterminated > 0)
1372 warning (OPT_Wformat, "unterminated format string");
1375 /* Callback from check_function_arguments_recurse to check a
1376 format string. FORMAT_TREE is the format parameter. ARG_NUM
1377 is the number of the format argument. CTX points to a
1378 format_check_context. */
1380 static void
1381 check_format_arg (void *ctx, tree format_tree,
1382 unsigned HOST_WIDE_INT arg_num)
1384 format_check_context *format_ctx = (format_check_context *) ctx;
1385 format_check_results *res = format_ctx->res;
1386 function_format_info *info = format_ctx->info;
1387 tree params = format_ctx->params;
1389 int format_length;
1390 HOST_WIDE_INT offset;
1391 const char *format_chars;
1392 tree array_size = 0;
1393 tree array_init;
1394 alloc_pool fwt_pool;
1396 if (integer_zerop (format_tree))
1398 /* Skip to first argument to check, so we can see if this format
1399 has any arguments (it shouldn't). */
1400 while (arg_num + 1 < info->first_arg_num)
1402 if (params == 0)
1403 return;
1404 params = TREE_CHAIN (params);
1405 ++arg_num;
1408 if (params == 0)
1409 res->number_other++;
1410 else
1411 res->number_extra_args++;
1413 return;
1416 offset = 0;
1417 if (TREE_CODE (format_tree) == POINTER_PLUS_EXPR)
1419 tree arg0, arg1;
1421 arg0 = TREE_OPERAND (format_tree, 0);
1422 arg1 = TREE_OPERAND (format_tree, 1);
1423 STRIP_NOPS (arg0);
1424 STRIP_NOPS (arg1);
1425 if (TREE_CODE (arg1) == INTEGER_CST)
1426 format_tree = arg0;
1427 else
1429 res->number_non_literal++;
1430 return;
1432 if (!host_integerp (arg1, 0)
1433 || (offset = tree_low_cst (arg1, 0)) < 0)
1435 res->number_non_literal++;
1436 return;
1439 if (TREE_CODE (format_tree) != ADDR_EXPR)
1441 res->number_non_literal++;
1442 return;
1444 format_tree = TREE_OPERAND (format_tree, 0);
1445 if (TREE_CODE (format_tree) == ARRAY_REF
1446 && host_integerp (TREE_OPERAND (format_tree, 1), 0)
1447 && (offset += tree_low_cst (TREE_OPERAND (format_tree, 1), 0)) >= 0)
1448 format_tree = TREE_OPERAND (format_tree, 0);
1449 if (TREE_CODE (format_tree) == VAR_DECL
1450 && TREE_CODE (TREE_TYPE (format_tree)) == ARRAY_TYPE
1451 && (array_init = decl_constant_value (format_tree)) != format_tree
1452 && TREE_CODE (array_init) == STRING_CST)
1454 /* Extract the string constant initializer. Note that this may include
1455 a trailing NUL character that is not in the array (e.g.
1456 const char a[3] = "foo";). */
1457 array_size = DECL_SIZE_UNIT (format_tree);
1458 format_tree = array_init;
1460 if (TREE_CODE (format_tree) != STRING_CST)
1462 res->number_non_literal++;
1463 return;
1465 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree))) != char_type_node)
1467 res->number_wide++;
1468 return;
1470 format_chars = TREE_STRING_POINTER (format_tree);
1471 format_length = TREE_STRING_LENGTH (format_tree);
1472 if (array_size != 0)
1474 /* Variable length arrays can't be initialized. */
1475 gcc_assert (TREE_CODE (array_size) == INTEGER_CST);
1477 if (host_integerp (array_size, 0))
1479 HOST_WIDE_INT array_size_value = TREE_INT_CST_LOW (array_size);
1480 if (array_size_value > 0
1481 && array_size_value == (int) array_size_value
1482 && format_length > array_size_value)
1483 format_length = array_size_value;
1486 if (offset)
1488 if (offset >= format_length)
1490 res->number_non_literal++;
1491 return;
1493 format_chars += offset;
1494 format_length -= offset;
1496 if (format_length < 1 || format_chars[--format_length] != 0)
1498 res->number_unterminated++;
1499 return;
1501 if (format_length == 0)
1503 res->number_empty++;
1504 return;
1507 /* Skip to first argument to check. */
1508 while (arg_num + 1 < info->first_arg_num)
1510 if (params == 0)
1511 return;
1512 params = TREE_CHAIN (params);
1513 ++arg_num;
1515 /* Provisionally increment res->number_other; check_format_info_main
1516 will decrement it if it finds there are extra arguments, but this way
1517 need not adjust it for every return. */
1518 res->number_other++;
1519 fwt_pool = create_alloc_pool ("format_wanted_type pool",
1520 sizeof (format_wanted_type), 10);
1521 check_format_info_main (res, info, format_chars, format_length,
1522 params, arg_num, fwt_pool);
1523 free_alloc_pool (fwt_pool);
1527 /* Helper for check_format_info_main which does all the actual work. */
1529 static void
1530 check_format_info_inner (dollar_argument_info *state,
1531 format_check_results *res,
1532 function_format_info *info, const char *format_chars,
1533 int format_length, tree params,
1534 unsigned HOST_WIDE_INT arg_num, alloc_pool fwt_pool)
1536 const char *orig_format_chars = format_chars;
1537 tree first_fillin_param = params;
1539 const format_kind_info *fki = &format_types[info->format_type];
1540 const format_flag_spec *flag_specs = fki->flag_specs;
1541 const format_flag_pair *bad_flag_pairs = fki->bad_flag_pairs;
1543 /* -1 if no conversions taking an operand have been found; 0 if one has
1544 and it didn't use $; 1 if $ formats are in use. */
1545 int has_operand_number = -1;
1547 while (1)
1549 int i;
1550 int suppressed = FALSE;
1551 const char *length_chars = NULL;
1552 enum format_lengths length_chars_val = FMT_LEN_none;
1553 enum format_std_version length_chars_std = STD_C89;
1554 int format_char;
1555 tree cur_param;
1556 tree wanted_type;
1557 int main_arg_num = 0;
1558 tree main_arg_params = 0;
1559 enum format_std_version wanted_type_std;
1560 const char *wanted_type_name;
1561 format_wanted_type width_wanted_type;
1562 format_wanted_type precision_wanted_type;
1563 format_wanted_type main_wanted_type;
1564 format_wanted_type *first_wanted_type = NULL;
1565 format_wanted_type *last_wanted_type = NULL;
1566 const format_length_info *fli = NULL;
1567 const format_char_info *fci = NULL;
1568 char flag_chars[256];
1569 int alloc_flag = 0;
1570 const char *format_start = format_chars;
1571 if (*format_chars == 0)
1573 if (format_chars - orig_format_chars != format_length)
1574 warning (OPT_Wformat_contains_nul, "embedded %<\\0%> in format");
1575 if (info->first_arg_num != 0 && params != 0
1576 && has_operand_number <= 0)
1578 res->number_other--;
1579 res->number_extra_args++;
1581 if (has_operand_number > 0)
1582 finish_dollar_format_checking (state, res,
1583 fki->flags & (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK);
1584 return;
1586 if (*format_chars++ != '%')
1587 continue;
1588 if (*format_chars == 0)
1590 warning (OPT_Wformat, "spurious trailing %<%%%> in format");
1591 continue;
1593 if (*format_chars == '%')
1595 ++format_chars;
1596 continue;
1598 flag_chars[0] = 0;
1600 if ((fki->flags & (int) FMT_FLAG_USE_DOLLAR) && has_operand_number != 0)
1602 /* Possibly read a $ operand number at the start of the format.
1603 If one was previously used, one is required here. If one
1604 is not used here, we can't immediately conclude this is a
1605 format without them, since it could be printf %m or scanf %*. */
1606 int opnum;
1607 opnum = maybe_read_dollar_number (state, &format_chars, 0,
1608 first_fillin_param,
1609 &main_arg_params, fki);
1610 if (opnum == -1)
1611 return;
1612 else if (opnum > 0)
1614 has_operand_number = 1;
1615 main_arg_num = opnum + info->first_arg_num - 1;
1618 else if (fki->flags & FMT_FLAG_USE_DOLLAR)
1620 if (avoid_dollar_number (format_chars))
1621 return;
1624 /* Read any format flags, but do not yet validate them beyond removing
1625 duplicates, since in general validation depends on the rest of
1626 the format. */
1627 while (*format_chars != 0
1628 && strchr (fki->flag_chars, *format_chars) != 0)
1630 const format_flag_spec *s = get_flag_spec (flag_specs,
1631 *format_chars, NULL);
1632 if (strchr (flag_chars, *format_chars) != 0)
1634 warning (OPT_Wformat, "repeated %s in format", _(s->name));
1636 else
1638 i = strlen (flag_chars);
1639 flag_chars[i++] = *format_chars;
1640 flag_chars[i] = 0;
1642 if (s->skip_next_char)
1644 ++format_chars;
1645 if (*format_chars == 0)
1647 warning (OPT_Wformat, "missing fill character at end of strfmon format");
1648 return;
1651 ++format_chars;
1654 /* Read any format width, possibly * or *m$. */
1655 if (fki->width_char != 0)
1657 if (fki->width_type != NULL && *format_chars == '*')
1659 i = strlen (flag_chars);
1660 flag_chars[i++] = fki->width_char;
1661 flag_chars[i] = 0;
1662 /* "...a field width...may be indicated by an asterisk.
1663 In this case, an int argument supplies the field width..." */
1664 ++format_chars;
1665 if (has_operand_number != 0)
1667 int opnum;
1668 opnum = maybe_read_dollar_number (state,
1669 &format_chars,
1670 has_operand_number == 1,
1671 first_fillin_param,
1672 &params, fki);
1673 if (opnum == -1)
1674 return;
1675 else if (opnum > 0)
1677 has_operand_number = 1;
1678 arg_num = opnum + info->first_arg_num - 1;
1680 else
1681 has_operand_number = 0;
1683 else
1685 if (avoid_dollar_number (format_chars))
1686 return;
1688 if (info->first_arg_num != 0)
1690 if (params == 0)
1692 warning (OPT_Wformat, "too few arguments for format");
1693 return;
1695 cur_param = TREE_VALUE (params);
1696 if (has_operand_number <= 0)
1698 params = TREE_CHAIN (params);
1699 ++arg_num;
1701 width_wanted_type.wanted_type = *fki->width_type;
1702 width_wanted_type.wanted_type_name = NULL;
1703 width_wanted_type.pointer_count = 0;
1704 width_wanted_type.char_lenient_flag = 0;
1705 width_wanted_type.writing_in_flag = 0;
1706 width_wanted_type.reading_from_flag = 0;
1707 width_wanted_type.name = _("field width");
1708 width_wanted_type.param = cur_param;
1709 width_wanted_type.arg_num = arg_num;
1710 width_wanted_type.next = NULL;
1711 if (last_wanted_type != 0)
1712 last_wanted_type->next = &width_wanted_type;
1713 if (first_wanted_type == 0)
1714 first_wanted_type = &width_wanted_type;
1715 last_wanted_type = &width_wanted_type;
1718 else
1720 /* Possibly read a numeric width. If the width is zero,
1721 we complain if appropriate. */
1722 int non_zero_width_char = FALSE;
1723 int found_width = FALSE;
1724 while (ISDIGIT (*format_chars))
1726 found_width = TRUE;
1727 if (*format_chars != '0')
1728 non_zero_width_char = TRUE;
1729 ++format_chars;
1731 if (found_width && !non_zero_width_char &&
1732 (fki->flags & (int) FMT_FLAG_ZERO_WIDTH_BAD))
1733 warning (OPT_Wformat, "zero width in %s format", fki->name);
1734 if (found_width)
1736 i = strlen (flag_chars);
1737 flag_chars[i++] = fki->width_char;
1738 flag_chars[i] = 0;
1743 /* Read any format left precision (must be a number, not *). */
1744 if (fki->left_precision_char != 0 && *format_chars == '#')
1746 ++format_chars;
1747 i = strlen (flag_chars);
1748 flag_chars[i++] = fki->left_precision_char;
1749 flag_chars[i] = 0;
1750 if (!ISDIGIT (*format_chars))
1751 warning (OPT_Wformat, "empty left precision in %s format", fki->name);
1752 while (ISDIGIT (*format_chars))
1753 ++format_chars;
1756 /* Read any format precision, possibly * or *m$. */
1757 if (fki->precision_char != 0 && *format_chars == '.')
1759 ++format_chars;
1760 i = strlen (flag_chars);
1761 flag_chars[i++] = fki->precision_char;
1762 flag_chars[i] = 0;
1763 if (fki->precision_type != NULL && *format_chars == '*')
1765 /* "...a...precision...may be indicated by an asterisk.
1766 In this case, an int argument supplies the...precision." */
1767 ++format_chars;
1768 if (has_operand_number != 0)
1770 int opnum;
1771 opnum = maybe_read_dollar_number (state,
1772 &format_chars,
1773 has_operand_number == 1,
1774 first_fillin_param,
1775 &params, fki);
1776 if (opnum == -1)
1777 return;
1778 else if (opnum > 0)
1780 has_operand_number = 1;
1781 arg_num = opnum + info->first_arg_num - 1;
1783 else
1784 has_operand_number = 0;
1786 else
1788 if (avoid_dollar_number (format_chars))
1789 return;
1791 if (info->first_arg_num != 0)
1793 if (params == 0)
1795 warning (OPT_Wformat, "too few arguments for format");
1796 return;
1798 cur_param = TREE_VALUE (params);
1799 if (has_operand_number <= 0)
1801 params = TREE_CHAIN (params);
1802 ++arg_num;
1804 precision_wanted_type.wanted_type = *fki->precision_type;
1805 precision_wanted_type.wanted_type_name = NULL;
1806 precision_wanted_type.pointer_count = 0;
1807 precision_wanted_type.char_lenient_flag = 0;
1808 precision_wanted_type.writing_in_flag = 0;
1809 precision_wanted_type.reading_from_flag = 0;
1810 precision_wanted_type.name = _("field precision");
1811 precision_wanted_type.param = cur_param;
1812 precision_wanted_type.arg_num = arg_num;
1813 precision_wanted_type.next = NULL;
1814 if (last_wanted_type != 0)
1815 last_wanted_type->next = &precision_wanted_type;
1816 if (first_wanted_type == 0)
1817 first_wanted_type = &precision_wanted_type;
1818 last_wanted_type = &precision_wanted_type;
1821 else
1823 if (!(fki->flags & (int) FMT_FLAG_EMPTY_PREC_OK)
1824 && !ISDIGIT (*format_chars))
1825 warning (OPT_Wformat, "empty precision in %s format", fki->name);
1826 while (ISDIGIT (*format_chars))
1827 ++format_chars;
1831 if (fki->alloc_char && fki->alloc_char == *format_chars)
1833 i = strlen (flag_chars);
1834 flag_chars[i++] = fki->alloc_char;
1835 flag_chars[i] = 0;
1836 format_chars++;
1839 /* Handle the scanf allocation kludge. */
1840 if (fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
1842 if (*format_chars == 'a' && !flag_isoc99)
1844 if (format_chars[1] == 's' || format_chars[1] == 'S'
1845 || format_chars[1] == '[')
1847 /* 'a' is used as a flag. */
1848 i = strlen (flag_chars);
1849 flag_chars[i++] = 'a';
1850 flag_chars[i] = 0;
1851 format_chars++;
1856 /* Read any length modifier, if this kind of format has them. */
1857 fli = fki->length_char_specs;
1858 length_chars = NULL;
1859 length_chars_val = FMT_LEN_none;
1860 length_chars_std = STD_C89;
1861 if (fli)
1863 while (fli->name != 0 && fli->name[0] != *format_chars)
1864 fli++;
1865 if (fli->name != 0)
1867 format_chars++;
1868 if (fli->double_name != 0 && fli->name[0] == *format_chars)
1870 format_chars++;
1871 length_chars = fli->double_name;
1872 length_chars_val = fli->double_index;
1873 length_chars_std = fli->double_std;
1875 else
1877 length_chars = fli->name;
1878 length_chars_val = fli->index;
1879 length_chars_std = fli->std;
1881 i = strlen (flag_chars);
1882 flag_chars[i++] = fki->length_code_char;
1883 flag_chars[i] = 0;
1885 if (pedantic)
1887 /* Warn if the length modifier is non-standard. */
1888 if (ADJ_STD (length_chars_std) > C_STD_VER)
1889 warning (OPT_Wformat,
1890 "%s does not support the %qs %s length modifier",
1891 C_STD_NAME (length_chars_std), length_chars,
1892 fki->name);
1896 /* Read any modifier (strftime E/O). */
1897 if (fki->modifier_chars != NULL)
1899 while (*format_chars != 0
1900 && strchr (fki->modifier_chars, *format_chars) != 0)
1902 if (strchr (flag_chars, *format_chars) != 0)
1904 const format_flag_spec *s = get_flag_spec (flag_specs,
1905 *format_chars, NULL);
1906 warning (OPT_Wformat, "repeated %s in format", _(s->name));
1908 else
1910 i = strlen (flag_chars);
1911 flag_chars[i++] = *format_chars;
1912 flag_chars[i] = 0;
1914 ++format_chars;
1918 format_char = *format_chars;
1919 if (format_char == 0
1920 || (!(fki->flags & (int) FMT_FLAG_FANCY_PERCENT_OK)
1921 && format_char == '%'))
1923 warning (OPT_Wformat, "conversion lacks type at end of format");
1924 continue;
1926 format_chars++;
1927 fci = fki->conversion_specs;
1928 while (fci->format_chars != 0
1929 && strchr (fci->format_chars, format_char) == 0)
1930 ++fci;
1931 if (fci->format_chars == 0)
1933 if (ISGRAPH (format_char))
1934 warning (OPT_Wformat, "unknown conversion type character %qc in format",
1935 format_char);
1936 else
1937 warning (OPT_Wformat, "unknown conversion type character 0x%x in format",
1938 format_char);
1939 continue;
1941 if (pedantic)
1943 if (ADJ_STD (fci->std) > C_STD_VER)
1944 warning (OPT_Wformat, "%s does not support the %<%%%c%> %s format",
1945 C_STD_NAME (fci->std), format_char, fki->name);
1948 /* Validate the individual flags used, removing any that are invalid. */
1950 int d = 0;
1951 for (i = 0; flag_chars[i] != 0; i++)
1953 const format_flag_spec *s = get_flag_spec (flag_specs,
1954 flag_chars[i], NULL);
1955 flag_chars[i - d] = flag_chars[i];
1956 if (flag_chars[i] == fki->length_code_char)
1957 continue;
1958 if (strchr (fci->flag_chars, flag_chars[i]) == 0)
1960 warning (OPT_Wformat, "%s used with %<%%%c%> %s format",
1961 _(s->name), format_char, fki->name);
1962 d++;
1963 continue;
1965 if (pedantic)
1967 const format_flag_spec *t;
1968 if (ADJ_STD (s->std) > C_STD_VER)
1969 warning (OPT_Wformat, "%s does not support %s",
1970 C_STD_NAME (s->std), _(s->long_name));
1971 t = get_flag_spec (flag_specs, flag_chars[i], fci->flags2);
1972 if (t != NULL && ADJ_STD (t->std) > ADJ_STD (s->std))
1974 const char *long_name = (t->long_name != NULL
1975 ? t->long_name
1976 : s->long_name);
1977 if (ADJ_STD (t->std) > C_STD_VER)
1978 warning (OPT_Wformat,
1979 "%s does not support %s with the %<%%%c%> %s format",
1980 C_STD_NAME (t->std), _(long_name),
1981 format_char, fki->name);
1985 flag_chars[i - d] = 0;
1988 if ((fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
1989 && strchr (flag_chars, 'a') != 0)
1990 alloc_flag = 1;
1991 if (fki->alloc_char && strchr (flag_chars, fki->alloc_char) != 0)
1992 alloc_flag = 1;
1994 if (fki->suppression_char
1995 && strchr (flag_chars, fki->suppression_char) != 0)
1996 suppressed = 1;
1998 /* Validate the pairs of flags used. */
1999 for (i = 0; bad_flag_pairs[i].flag_char1 != 0; i++)
2001 const format_flag_spec *s, *t;
2002 if (strchr (flag_chars, bad_flag_pairs[i].flag_char1) == 0)
2003 continue;
2004 if (strchr (flag_chars, bad_flag_pairs[i].flag_char2) == 0)
2005 continue;
2006 if (bad_flag_pairs[i].predicate != 0
2007 && strchr (fci->flags2, bad_flag_pairs[i].predicate) == 0)
2008 continue;
2009 s = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char1, NULL);
2010 t = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char2, NULL);
2011 if (bad_flag_pairs[i].ignored)
2013 if (bad_flag_pairs[i].predicate != 0)
2014 warning (OPT_Wformat,
2015 "%s ignored with %s and %<%%%c%> %s format",
2016 _(s->name), _(t->name), format_char,
2017 fki->name);
2018 else
2019 warning (OPT_Wformat, "%s ignored with %s in %s format",
2020 _(s->name), _(t->name), fki->name);
2022 else
2024 if (bad_flag_pairs[i].predicate != 0)
2025 warning (OPT_Wformat,
2026 "use of %s and %s together with %<%%%c%> %s format",
2027 _(s->name), _(t->name), format_char,
2028 fki->name);
2029 else
2030 warning (OPT_Wformat, "use of %s and %s together in %s format",
2031 _(s->name), _(t->name), fki->name);
2035 /* Give Y2K warnings. */
2036 if (warn_format_y2k)
2038 int y2k_level = 0;
2039 if (strchr (fci->flags2, '4') != 0)
2040 if (strchr (flag_chars, 'E') != 0)
2041 y2k_level = 3;
2042 else
2043 y2k_level = 2;
2044 else if (strchr (fci->flags2, '3') != 0)
2045 y2k_level = 3;
2046 else if (strchr (fci->flags2, '2') != 0)
2047 y2k_level = 2;
2048 if (y2k_level == 3)
2049 warning (OPT_Wformat_y2k, "%<%%%c%> yields only last 2 digits of "
2050 "year in some locales", format_char);
2051 else if (y2k_level == 2)
2052 warning (OPT_Wformat_y2k, "%<%%%c%> yields only last 2 digits of "
2053 "year", format_char);
2056 if (strchr (fci->flags2, '[') != 0)
2058 /* Skip over scan set, in case it happens to have '%' in it. */
2059 if (*format_chars == '^')
2060 ++format_chars;
2061 /* Find closing bracket; if one is hit immediately, then
2062 it's part of the scan set rather than a terminator. */
2063 if (*format_chars == ']')
2064 ++format_chars;
2065 while (*format_chars && *format_chars != ']')
2066 ++format_chars;
2067 if (*format_chars != ']')
2068 /* The end of the format string was reached. */
2069 warning (OPT_Wformat, "no closing %<]%> for %<%%[%> format");
2072 wanted_type = 0;
2073 wanted_type_name = 0;
2074 if (fki->flags & (int) FMT_FLAG_ARG_CONVERT)
2076 wanted_type = (fci->types[length_chars_val].type
2077 ? *fci->types[length_chars_val].type : 0);
2078 wanted_type_name = fci->types[length_chars_val].name;
2079 wanted_type_std = fci->types[length_chars_val].std;
2080 if (wanted_type == 0)
2082 warning (OPT_Wformat,
2083 "use of %qs length modifier with %qc type character",
2084 length_chars, format_char);
2085 /* Heuristic: skip one argument when an invalid length/type
2086 combination is encountered. */
2087 arg_num++;
2088 if (params == 0)
2090 warning (OPT_Wformat, "too few arguments for format");
2091 return;
2093 params = TREE_CHAIN (params);
2094 continue;
2096 else if (pedantic
2097 /* Warn if non-standard, provided it is more non-standard
2098 than the length and type characters that may already
2099 have been warned for. */
2100 && ADJ_STD (wanted_type_std) > ADJ_STD (length_chars_std)
2101 && ADJ_STD (wanted_type_std) > ADJ_STD (fci->std))
2103 if (ADJ_STD (wanted_type_std) > C_STD_VER)
2104 warning (OPT_Wformat,
2105 "%s does not support the %<%%%s%c%> %s format",
2106 C_STD_NAME (wanted_type_std), length_chars,
2107 format_char, fki->name);
2111 main_wanted_type.next = NULL;
2113 /* Finally. . .check type of argument against desired type! */
2114 if (info->first_arg_num == 0)
2115 continue;
2116 if ((fci->pointer_count == 0 && wanted_type == void_type_node)
2117 || suppressed)
2119 if (main_arg_num != 0)
2121 if (suppressed)
2122 warning (OPT_Wformat, "operand number specified with "
2123 "suppressed assignment");
2124 else
2125 warning (OPT_Wformat, "operand number specified for format "
2126 "taking no argument");
2129 else
2131 format_wanted_type *wanted_type_ptr;
2133 if (main_arg_num != 0)
2135 arg_num = main_arg_num;
2136 params = main_arg_params;
2138 else
2140 ++arg_num;
2141 if (has_operand_number > 0)
2143 warning (OPT_Wformat, "missing $ operand number in format");
2144 return;
2146 else
2147 has_operand_number = 0;
2150 wanted_type_ptr = &main_wanted_type;
2151 while (fci)
2153 if (params == 0)
2155 warning (OPT_Wformat, "too few arguments for format");
2156 return;
2159 cur_param = TREE_VALUE (params);
2160 params = TREE_CHAIN (params);
2162 wanted_type_ptr->wanted_type = wanted_type;
2163 wanted_type_ptr->wanted_type_name = wanted_type_name;
2164 wanted_type_ptr->pointer_count = fci->pointer_count + alloc_flag;
2165 wanted_type_ptr->char_lenient_flag = 0;
2166 if (strchr (fci->flags2, 'c') != 0)
2167 wanted_type_ptr->char_lenient_flag = 1;
2168 wanted_type_ptr->writing_in_flag = 0;
2169 wanted_type_ptr->reading_from_flag = 0;
2170 if (alloc_flag)
2171 wanted_type_ptr->writing_in_flag = 1;
2172 else
2174 if (strchr (fci->flags2, 'W') != 0)
2175 wanted_type_ptr->writing_in_flag = 1;
2176 if (strchr (fci->flags2, 'R') != 0)
2177 wanted_type_ptr->reading_from_flag = 1;
2179 wanted_type_ptr->name = NULL;
2180 wanted_type_ptr->param = cur_param;
2181 wanted_type_ptr->arg_num = arg_num;
2182 wanted_type_ptr->next = NULL;
2183 if (last_wanted_type != 0)
2184 last_wanted_type->next = wanted_type_ptr;
2185 if (first_wanted_type == 0)
2186 first_wanted_type = wanted_type_ptr;
2187 last_wanted_type = wanted_type_ptr;
2189 fci = fci->chain;
2190 if (fci)
2192 wanted_type_ptr = (format_wanted_type *)
2193 pool_alloc (fwt_pool);
2194 arg_num++;
2195 wanted_type = *fci->types[length_chars_val].type;
2196 wanted_type_name = fci->types[length_chars_val].name;
2201 if (first_wanted_type != 0)
2202 check_format_types (first_wanted_type, format_start,
2203 format_chars - format_start);
2207 /* Do the main part of checking a call to a format function. FORMAT_CHARS
2208 is the NUL-terminated format string (which at this point may contain
2209 internal NUL characters); FORMAT_LENGTH is its length (excluding the
2210 terminating NUL character). ARG_NUM is one less than the number of
2211 the first format argument to check; PARAMS points to that format
2212 argument in the list of arguments. */
2214 static void
2215 check_format_info_main (format_check_results *res,
2216 function_format_info *info, const char *format_chars,
2217 int format_length, tree params,
2218 unsigned HOST_WIDE_INT arg_num, alloc_pool fwt_pool)
2220 dollar_argument_info dollar_state;
2222 init_dollar_format_checking (&dollar_state, info->first_arg_num, params);
2223 check_format_info_inner (&dollar_state, res, info, format_chars,
2224 format_length, params, arg_num, fwt_pool);
2225 if (dollar_state.dollar_arguments_used)
2226 free (dollar_state.dollar_arguments_used);
2227 if (dollar_state.dollar_arguments_pointer_p)
2228 free (dollar_state.dollar_arguments_pointer_p);
2232 /* Check the argument types from a single format conversion (possibly
2233 including width and precision arguments). */
2234 static void
2235 check_format_types (format_wanted_type *types, const char *format_start,
2236 int format_length)
2238 for (; types != 0; types = types->next)
2240 tree cur_param;
2241 tree cur_type;
2242 tree orig_cur_type;
2243 tree wanted_type;
2244 int arg_num;
2245 int i;
2246 int char_type_flag;
2247 cur_param = types->param;
2248 cur_type = TREE_TYPE (cur_param);
2249 if (cur_type == error_mark_node)
2250 continue;
2251 orig_cur_type = cur_type;
2252 char_type_flag = 0;
2253 wanted_type = types->wanted_type;
2254 arg_num = types->arg_num;
2256 /* The following should not occur here. */
2257 gcc_assert (wanted_type);
2258 gcc_assert (wanted_type != void_type_node || types->pointer_count);
2260 if (types->pointer_count == 0)
2261 wanted_type = lang_hooks.types.type_promotes_to (wanted_type);
2263 wanted_type = TYPE_MAIN_VARIANT (wanted_type);
2265 STRIP_NOPS (cur_param);
2267 /* Check the types of any additional pointer arguments
2268 that precede the "real" argument. */
2269 for (i = 0; i < types->pointer_count; ++i)
2271 if (TREE_CODE (cur_type) == POINTER_TYPE)
2273 cur_type = TREE_TYPE (cur_type);
2274 if (cur_type == error_mark_node)
2275 break;
2277 /* Check for writing through a NULL pointer. */
2278 if (types->writing_in_flag
2279 && i == 0
2280 && cur_param != 0
2281 && integer_zerop (cur_param))
2282 warning (OPT_Wformat, "writing through null pointer "
2283 "(argument %d)", arg_num);
2285 /* Check for reading through a NULL pointer. */
2286 if (types->reading_from_flag
2287 && i == 0
2288 && cur_param != 0
2289 && integer_zerop (cur_param))
2290 warning (OPT_Wformat, "reading through null pointer "
2291 "(argument %d)", arg_num);
2293 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
2294 cur_param = TREE_OPERAND (cur_param, 0);
2295 else
2296 cur_param = 0;
2298 /* See if this is an attempt to write into a const type with
2299 scanf or with printf "%n". Note: the writing in happens
2300 at the first indirection only, if for example
2301 void * const * is passed to scanf %p; passing
2302 const void ** is simply passing an incompatible type. */
2303 if (types->writing_in_flag
2304 && i == 0
2305 && (TYPE_READONLY (cur_type)
2306 || (cur_param != 0
2307 && (CONSTANT_CLASS_P (cur_param)
2308 || (DECL_P (cur_param)
2309 && TREE_READONLY (cur_param))))))
2310 warning (OPT_Wformat, "writing into constant object "
2311 "(argument %d)", arg_num);
2313 /* If there are extra type qualifiers beyond the first
2314 indirection, then this makes the types technically
2315 incompatible. */
2316 if (i > 0
2317 && pedantic
2318 && (TYPE_READONLY (cur_type)
2319 || TYPE_VOLATILE (cur_type)
2320 || TYPE_RESTRICT (cur_type)))
2321 warning (OPT_Wformat, "extra type qualifiers in format "
2322 "argument (argument %d)",
2323 arg_num);
2326 else
2328 format_type_warning (types->name, format_start, format_length,
2329 wanted_type, types->pointer_count,
2330 types->wanted_type_name, orig_cur_type,
2331 arg_num);
2332 break;
2336 if (i < types->pointer_count)
2337 continue;
2339 cur_type = TYPE_MAIN_VARIANT (cur_type);
2341 /* Check whether the argument type is a character type. This leniency
2342 only applies to certain formats, flagged with 'c'.
2344 if (types->char_lenient_flag)
2345 char_type_flag = (cur_type == char_type_node
2346 || cur_type == signed_char_type_node
2347 || cur_type == unsigned_char_type_node);
2349 /* Check the type of the "real" argument, if there's a type we want. */
2350 if (lang_hooks.types_compatible_p (wanted_type, cur_type))
2351 continue;
2352 /* If we want 'void *', allow any pointer type.
2353 (Anything else would already have got a warning.)
2354 With -pedantic, only allow pointers to void and to character
2355 types. */
2356 if (wanted_type == void_type_node
2357 && (!pedantic || (i == 1 && char_type_flag)))
2358 continue;
2359 /* Don't warn about differences merely in signedness, unless
2360 -pedantic. With -pedantic, warn if the type is a pointer
2361 target and not a character type, and for character types at
2362 a second level of indirection. */
2363 if (TREE_CODE (wanted_type) == INTEGER_TYPE
2364 && TREE_CODE (cur_type) == INTEGER_TYPE
2365 && (!pedantic || i == 0 || (i == 1 && char_type_flag))
2366 && (TYPE_UNSIGNED (wanted_type)
2367 ? wanted_type == c_common_unsigned_type (cur_type)
2368 : wanted_type == c_common_signed_type (cur_type)))
2369 continue;
2370 /* Likewise, "signed char", "unsigned char" and "char" are
2371 equivalent but the above test won't consider them equivalent. */
2372 if (wanted_type == char_type_node
2373 && (!pedantic || i < 2)
2374 && char_type_flag)
2375 continue;
2376 /* Now we have a type mismatch. */
2377 format_type_warning (types->name, format_start, format_length,
2378 wanted_type, types->pointer_count,
2379 types->wanted_type_name, orig_cur_type, arg_num);
2384 /* Give a warning about a format argument of different type from that
2385 expected. DESCR is a description such as "field precision", or
2386 NULL for an ordinary format. For an ordinary format, FORMAT_START
2387 points to where the format starts in the format string and
2388 FORMAT_LENGTH is its length. WANTED_TYPE is the type the argument
2389 should have after POINTER_COUNT pointer dereferences.
2390 WANTED_NAME_NAME is a possibly more friendly name of WANTED_TYPE,
2391 or NULL if the ordinary name of the type should be used. ARG_TYPE
2392 is the type of the actual argument. ARG_NUM is the number of that
2393 argument. */
2394 static void
2395 format_type_warning (const char *descr, const char *format_start,
2396 int format_length, tree wanted_type, int pointer_count,
2397 const char *wanted_type_name, tree arg_type, int arg_num)
2399 char *p;
2400 /* If ARG_TYPE is a typedef with a misleading name (for example,
2401 size_t but not the standard size_t expected by printf %zu), avoid
2402 printing the typedef name. */
2403 if (wanted_type_name
2404 && TYPE_NAME (arg_type)
2405 && TREE_CODE (TYPE_NAME (arg_type)) == TYPE_DECL
2406 && DECL_NAME (TYPE_NAME (arg_type))
2407 && !strcmp (wanted_type_name,
2408 lang_hooks.decl_printable_name (TYPE_NAME (arg_type), 2)))
2409 arg_type = TYPE_MAIN_VARIANT (arg_type);
2410 /* The format type and name exclude any '*' for pointers, so those
2411 must be formatted manually. For all the types we currently have,
2412 this is adequate, but formats taking pointers to functions or
2413 arrays would require the full type to be built up in order to
2414 print it with %T. */
2415 p = (char *) alloca (pointer_count + 2);
2416 if (pointer_count == 0)
2417 p[0] = 0;
2418 else if (c_dialect_cxx ())
2420 memset (p, '*', pointer_count);
2421 p[pointer_count] = 0;
2423 else
2425 p[0] = ' ';
2426 memset (p + 1, '*', pointer_count);
2427 p[pointer_count + 1] = 0;
2429 if (wanted_type_name)
2431 if (descr)
2432 warning (OPT_Wformat, "%s should have type %<%s%s%>, "
2433 "but argument %d has type %qT",
2434 descr, wanted_type_name, p, arg_num, arg_type);
2435 else
2436 warning (OPT_Wformat, "format %q.*s expects type %<%s%s%>, "
2437 "but argument %d has type %qT",
2438 format_length, format_start, wanted_type_name, p,
2439 arg_num, arg_type);
2441 else
2443 if (descr)
2444 warning (OPT_Wformat, "%s should have type %<%T%s%>, "
2445 "but argument %d has type %qT",
2446 descr, wanted_type, p, arg_num, arg_type);
2447 else
2448 warning (OPT_Wformat, "format %q.*s expects type %<%T%s%>, "
2449 "but argument %d has type %qT",
2450 format_length, format_start, wanted_type, p, arg_num, arg_type);
2455 /* Given a format_char_info array FCI, and a character C, this function
2456 returns the index into the conversion_specs where that specifier's
2457 data is located. The character must exist. */
2458 static unsigned int
2459 find_char_info_specifier_index (const format_char_info *fci, int c)
2461 unsigned i;
2463 for (i = 0; fci->format_chars; i++, fci++)
2464 if (strchr (fci->format_chars, c))
2465 return i;
2467 /* We shouldn't be looking for a non-existent specifier. */
2468 gcc_unreachable ();
2471 /* Given a format_length_info array FLI, and a character C, this
2472 function returns the index into the conversion_specs where that
2473 modifier's data is located. The character must exist. */
2474 static unsigned int
2475 find_length_info_modifier_index (const format_length_info *fli, int c)
2477 unsigned i;
2479 for (i = 0; fli->name; i++, fli++)
2480 if (strchr (fli->name, c))
2481 return i;
2483 /* We shouldn't be looking for a non-existent modifier. */
2484 gcc_unreachable ();
2487 /* Determine the type of HOST_WIDE_INT in the code being compiled for
2488 use in GCC's __asm_fprintf__ custom format attribute. You must
2489 have set dynamic_format_types before calling this function. */
2490 static void
2491 init_dynamic_asm_fprintf_info (void)
2493 if (!global_format_state->fprintf_hwi)
2495 format_length_info *new_asm_fprintf_length_specs;
2496 unsigned int i;
2497 tree hwi;
2499 /* Find the underlying type for HOST_WIDE_INT. For the %w
2500 length modifier to work, one must have issued: "typedef
2501 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2502 prior to using that modifier. */
2503 hwi = maybe_get_identifier ("__gcc_host_wide_int__");
2504 if (!hwi)
2506 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2507 return;
2509 hwi = identifier_global_value (hwi);
2510 if (!hwi || TREE_CODE (hwi) != TYPE_DECL)
2512 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2513 return;
2515 hwi = DECL_ORIGINAL_TYPE (hwi);
2516 gcc_assert (hwi);
2517 if (hwi != long_integer_type_node && hwi != long_long_integer_type_node)
2519 error ("%<__gcc_host_wide_int__%> is not defined as %<long%>"
2520 " or %<long long%>");
2521 return;
2524 global_format_state->fprintf_hwi = hwi;
2526 /* Create a new (writable) copy of asm_fprintf_length_specs. */
2527 new_asm_fprintf_length_specs
2528 = (format_length_info *) obdup_mem (asm_fprintf_length_specs,
2529 sizeof (asm_fprintf_length_specs));
2531 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2532 i = find_length_info_modifier_index (new_asm_fprintf_length_specs, 'w');
2533 if (hwi == long_integer_type_node)
2534 new_asm_fprintf_length_specs[i].index = FMT_LEN_l;
2535 else if (hwi == long_long_integer_type_node)
2536 new_asm_fprintf_length_specs[i].index = FMT_LEN_ll;
2537 else
2538 gcc_unreachable ();
2540 /* Assign the new data for use. */
2541 dynamic_format_types[asm_fprintf_format_type].length_char_specs =
2542 new_asm_fprintf_length_specs;
2546 /* Determine the type of a "locus" in the code being compiled for use
2547 in GCC's __gcc_gfc__ custom format attribute. You must have set
2548 dynamic_format_types before calling this function. */
2549 static void
2550 init_dynamic_gfc_info (void)
2552 if (!global_format_state->locus)
2554 tree locus;
2556 /* For the GCC __gcc_gfc__ custom format specifier to work, one
2557 must have declared 'locus' prior to using this attribute. If
2558 we haven't seen this declarations then you shouldn't use the
2559 specifier requiring that type. */
2560 if ((locus = maybe_get_identifier ("locus")))
2562 locus = identifier_global_value (locus);
2563 if (locus)
2565 if (TREE_CODE (locus) != TYPE_DECL)
2567 error ("%<locus%> is not defined as a type");
2568 locus = 0;
2570 else
2571 locus = TREE_TYPE (locus);
2574 global_format_state->locus = locus;
2576 /* Assign the new data for use. */
2578 /* Handle the __gcc_gfc__ format specifics. */
2579 if (!global_format_state->gfc_fci)
2580 dynamic_format_types[gcc_gfc_format_type].conversion_specs
2581 = global_format_state->gfc_fci
2582 = (format_char_info *) obdup_mem (gcc_gfc_char_table,
2583 sizeof (gcc_gfc_char_table));
2584 if (locus)
2586 const unsigned i
2587 = find_char_info_specifier_index (global_format_state->gfc_fci,
2588 'L');
2589 global_format_state->gfc_fci[i].types[0].type
2590 = &global_format_state->locus;
2591 global_format_state->gfc_fci[i].pointer_count = 1;
2596 /* Determine the types of "tree" and "location_t" in the code being
2597 compiled for use in GCC's diagnostic custom format attributes. You
2598 must have set dynamic_format_types before calling this function. */
2599 static void
2600 init_dynamic_diag_info (void)
2602 if (!global_format_state->diag_loc
2603 || !global_format_state->diag_t
2604 || !global_format_state->diag_hwi)
2606 tree t, loc, hwi;
2607 unsigned int i;
2609 /* For the GCC-diagnostics custom format specifiers to work, one
2610 must have declared 'tree' and/or 'location_t' prior to using
2611 those attributes. If we haven't seen these declarations then
2612 you shouldn't use the specifiers requiring these types.
2613 However we don't force a hard ICE because we may see only one
2614 or the other type. */
2615 if ((loc = maybe_get_identifier ("location_t")))
2617 loc = identifier_global_value (loc);
2618 if (loc)
2620 if (TREE_CODE (loc) != TYPE_DECL)
2622 error ("%<location_t%> is not defined as a type");
2623 loc = 0;
2625 else
2626 loc = TREE_TYPE (loc);
2629 global_format_state->diag_loc = loc;
2631 /* We need to grab the underlying 'union tree_node' so peek into
2632 an extra type level. */
2633 if ((t = maybe_get_identifier ("tree")))
2635 t = identifier_global_value (t);
2636 if (t)
2638 if (TREE_CODE (t) != TYPE_DECL)
2640 error ("%<tree%> is not defined as a type");
2641 t = 0;
2643 else if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
2645 error ("%<tree%> is not defined as a pointer type");
2646 t = 0;
2648 else
2649 t = TREE_TYPE (TREE_TYPE (t));
2652 global_format_state->diag_t = t;
2654 /* Find the underlying type for HOST_WIDE_INT. For the %w
2655 length modifier to work, one must have issued: "typedef
2656 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2657 prior to using that modifier. */
2658 if ((hwi = maybe_get_identifier ("__gcc_host_wide_int__")))
2660 hwi = identifier_global_value (hwi);
2661 if (hwi)
2663 if (TREE_CODE (hwi) != TYPE_DECL)
2665 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2666 hwi = 0;
2668 else
2670 hwi = DECL_ORIGINAL_TYPE (hwi);
2671 gcc_assert (hwi);
2672 if (hwi != long_integer_type_node
2673 && hwi != long_long_integer_type_node)
2675 error ("%<__gcc_host_wide_int__%> is not defined"
2676 " as %<long%> or %<long long%>");
2677 hwi = 0;
2682 global_format_state->diag_hwi = hwi;
2684 /* Assign the new data for use. */
2686 /* All the GCC diag formats use the same length specs. */
2687 if (!global_format_state->diag_ls)
2688 dynamic_format_types[gcc_diag_format_type].length_char_specs =
2689 dynamic_format_types[gcc_tdiag_format_type].length_char_specs =
2690 dynamic_format_types[gcc_cdiag_format_type].length_char_specs =
2691 dynamic_format_types[gcc_cxxdiag_format_type].length_char_specs =
2692 global_format_state->diag_ls = (format_length_info *)
2693 obdup_mem (gcc_diag_length_specs,
2694 sizeof (gcc_diag_length_specs));
2695 if (hwi)
2697 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2698 i = find_length_info_modifier_index (global_format_state->diag_ls,
2699 'w');
2700 if (hwi == long_integer_type_node)
2701 global_format_state->diag_ls[i].index = FMT_LEN_l;
2702 else if (hwi == long_long_integer_type_node)
2703 global_format_state->diag_ls[i].index = FMT_LEN_ll;
2704 else
2705 gcc_unreachable ();
2708 /* Handle the __gcc_diag__ format specifics. */
2709 if (!global_format_state->diag_fci)
2710 dynamic_format_types[gcc_diag_format_type].conversion_specs =
2711 global_format_state->diag_fci = (format_char_info *)
2712 obdup_mem (gcc_diag_char_table,
2713 sizeof (gcc_diag_char_table));
2714 if (loc)
2716 i = find_char_info_specifier_index (global_format_state->diag_fci,
2717 'H');
2718 global_format_state->diag_fci[i].types[0].type
2719 = &global_format_state->diag_loc;
2720 global_format_state->diag_fci[i].pointer_count = 1;
2722 if (t)
2724 i = find_char_info_specifier_index (global_format_state->diag_fci,
2725 'J');
2726 global_format_state->diag_fci[i].types[0].type
2727 = &global_format_state->diag_t;
2728 global_format_state->diag_fci[i].pointer_count = 1;
2729 i = find_char_info_specifier_index (global_format_state->diag_fci,
2730 'K');
2731 global_format_state->diag_fci[i].types[0].type
2732 = &global_format_state->diag_t;
2733 global_format_state->diag_fci[i].pointer_count = 1;
2736 /* Handle the __gcc_tdiag__ format specifics. */
2737 if (!global_format_state->tdiag_fci)
2738 dynamic_format_types[gcc_tdiag_format_type].conversion_specs =
2739 global_format_state->tdiag_fci = (format_char_info *)
2740 obdup_mem (gcc_tdiag_char_table,
2741 sizeof (gcc_tdiag_char_table));
2742 if (loc)
2744 i = find_char_info_specifier_index (global_format_state->tdiag_fci,
2745 'H');
2746 global_format_state->tdiag_fci[i].types[0].type
2747 = &global_format_state->diag_loc;
2748 global_format_state->tdiag_fci[i].pointer_count = 1;
2750 if (t)
2752 /* All specifiers taking a tree share the same struct. */
2753 i = find_char_info_specifier_index (global_format_state->tdiag_fci,
2754 'D');
2755 global_format_state->tdiag_fci[i].types[0].type
2756 = &global_format_state->diag_t;
2757 global_format_state->tdiag_fci[i].pointer_count = 1;
2758 i = find_char_info_specifier_index (global_format_state->tdiag_fci,
2759 'J');
2760 global_format_state->tdiag_fci[i].types[0].type
2761 = &global_format_state->diag_t;
2762 global_format_state->tdiag_fci[i].pointer_count = 1;
2763 i = find_char_info_specifier_index (global_format_state->tdiag_fci,
2764 'K');
2765 global_format_state->tdiag_fci[i].types[0].type
2766 = &global_format_state->diag_t;
2767 global_format_state->tdiag_fci[i].pointer_count = 1;
2770 /* Handle the __gcc_cdiag__ format specifics. */
2771 if (!global_format_state->cdiag_fci)
2772 dynamic_format_types[gcc_cdiag_format_type].conversion_specs =
2773 global_format_state->cdiag_fci = (format_char_info *)
2774 obdup_mem (gcc_cdiag_char_table,
2775 sizeof (gcc_cdiag_char_table));
2776 if (loc)
2778 i = find_char_info_specifier_index (global_format_state->cdiag_fci,
2779 'H');
2780 global_format_state->cdiag_fci[i].types[0].type
2781 = &global_format_state->diag_loc;
2782 global_format_state->cdiag_fci[i].pointer_count = 1;
2784 if (t)
2786 /* All specifiers taking a tree share the same struct. */
2787 i = find_char_info_specifier_index (global_format_state->cdiag_fci,
2788 'D');
2789 global_format_state->cdiag_fci[i].types[0].type
2790 = &global_format_state->diag_t;
2791 global_format_state->cdiag_fci[i].pointer_count = 1;
2792 i = find_char_info_specifier_index (global_format_state->cdiag_fci,
2793 'J');
2794 global_format_state->cdiag_fci[i].types[0].type
2795 = &global_format_state->diag_t;
2796 global_format_state->cdiag_fci[i].pointer_count = 1;
2797 i = find_char_info_specifier_index (global_format_state->cdiag_fci,
2798 'K');
2799 global_format_state->cdiag_fci[i].types[0].type
2800 = &global_format_state->diag_t;
2801 global_format_state->cdiag_fci[i].pointer_count = 1;
2804 /* Handle the __gcc_cxxdiag__ format specifics. */
2805 if (!global_format_state->cxxdiag_fci)
2806 dynamic_format_types[gcc_cxxdiag_format_type].conversion_specs =
2807 global_format_state->cxxdiag_fci = (format_char_info *)
2808 obdup_mem (gcc_cxxdiag_char_table,
2809 sizeof (gcc_cxxdiag_char_table));
2810 if (loc)
2812 i = find_char_info_specifier_index (global_format_state->cxxdiag_fci,
2813 'H');
2814 global_format_state->cxxdiag_fci[i].types[0].type
2815 = &global_format_state->diag_loc;
2816 global_format_state->cxxdiag_fci[i].pointer_count = 1;
2818 if (t)
2820 /* All specifiers taking a tree share the same struct. */
2821 i = find_char_info_specifier_index (global_format_state->cxxdiag_fci,
2822 'D');
2823 global_format_state->cxxdiag_fci[i].types[0].type
2824 = &global_format_state->diag_t;
2825 global_format_state->cxxdiag_fci[i].pointer_count = 1;
2826 i = find_char_info_specifier_index (global_format_state->cxxdiag_fci,
2827 'J');
2828 global_format_state->cxxdiag_fci[i].types[0].type
2829 = &global_format_state->diag_t;
2830 global_format_state->cxxdiag_fci[i].pointer_count = 1;
2831 i = find_char_info_specifier_index (global_format_state->cxxdiag_fci,
2832 'K');
2833 global_format_state->cxxdiag_fci[i].types[0].type
2834 = &global_format_state->diag_t;
2835 global_format_state->cxxdiag_fci[i].pointer_count = 1;
2840 #ifdef TARGET_FORMAT_TYPES
2841 extern const format_kind_info TARGET_FORMAT_TYPES[];
2842 #endif
2844 /* Handle a "format" attribute; arguments as in
2845 struct attribute_spec.handler. */
2846 tree
2847 handle_format_attribute (tree *node, tree ARG_UNUSED (name), tree args,
2848 int flags, bool *no_add_attrs)
2850 tree type = *node;
2851 function_format_info info;
2852 tree argument;
2854 if (!global_format_state)
2855 construct_format_state ();
2857 #ifdef TARGET_FORMAT_TYPES
2858 /* If the target provides additional format types, we need to
2859 add them to FORMAT_TYPES at first use. */
2860 if (TARGET_FORMAT_TYPES != NULL && !dynamic_format_types)
2862 dynamic_format_types
2863 = obstack_alloc (&global_format_state->obstack,
2864 (n_format_types + TARGET_N_FORMAT_TYPES)
2865 * sizeof (dynamic_format_types[0]));
2866 memcpy (dynamic_format_types, format_types_orig,
2867 sizeof (format_types_orig));
2868 memcpy (&dynamic_format_types[n_format_types], TARGET_FORMAT_TYPES,
2869 TARGET_N_FORMAT_TYPES * sizeof (dynamic_format_types[0]));
2871 format_types = dynamic_format_types;
2872 n_format_types += TARGET_N_FORMAT_TYPES;
2874 #endif
2876 if (!decode_format_attr (args, &info, 0))
2878 *no_add_attrs = true;
2879 return NULL_TREE;
2882 argument = TYPE_ARG_TYPES (type);
2883 if (argument)
2885 if (!check_format_string (argument, info.format_num, flags,
2886 no_add_attrs))
2887 return NULL_TREE;
2889 if (info.first_arg_num != 0)
2891 unsigned HOST_WIDE_INT arg_num = 1;
2893 /* Verify that first_arg_num points to the last arg,
2894 the ... */
2895 while (argument)
2896 arg_num++, argument = TREE_CHAIN (argument);
2898 if (arg_num != info.first_arg_num)
2900 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
2901 error ("args to be formatted is not %<...%>");
2902 *no_add_attrs = true;
2903 return NULL_TREE;
2908 if (info.format_type == strftime_format_type && info.first_arg_num != 0)
2910 error ("strftime formats cannot format arguments");
2911 *no_add_attrs = true;
2912 return NULL_TREE;
2915 /* If this is a custom GCC-internal format type, we have to
2916 initialize certain bits a runtime. */
2917 if (info.format_type == asm_fprintf_format_type
2918 || info.format_type == gcc_gfc_format_type
2919 || info.format_type == gcc_diag_format_type
2920 || info.format_type == gcc_tdiag_format_type
2921 || info.format_type == gcc_cdiag_format_type
2922 || info.format_type == gcc_cxxdiag_format_type)
2924 /* Our first time through, we have to make sure that our
2925 format_type data is allocated dynamically and is modifiable. */
2926 if (!dynamic_format_types)
2927 format_types = dynamic_format_types = (format_kind_info *)
2928 obdup_mem (format_types_orig, sizeof (format_types_orig));
2930 /* If this is format __asm_fprintf__, we have to initialize
2931 GCC's notion of HOST_WIDE_INT for checking %wd. */
2932 if (info.format_type == asm_fprintf_format_type)
2933 init_dynamic_asm_fprintf_info ();
2934 /* If this is format __gcc_gfc__, we have to initialize GCC's
2935 notion of 'locus' at runtime for %L. */
2936 else if (info.format_type == gcc_gfc_format_type)
2937 init_dynamic_gfc_info ();
2938 /* If this is one of the diagnostic attributes, then we have to
2939 initialize 'location_t' and 'tree' at runtime. */
2940 else if (info.format_type == gcc_diag_format_type
2941 || info.format_type == gcc_tdiag_format_type
2942 || info.format_type == gcc_cdiag_format_type
2943 || info.format_type == gcc_cxxdiag_format_type)
2944 init_dynamic_diag_info ();
2945 else
2946 gcc_unreachable ();
2949 return NULL_TREE;
2952 #include "gt-c-format.h"