2005-07-02 Zack Weinberg <zack@codesourcery.com>
[official-gcc.git] / gcc / c-format.c
blob5c4bb67dcde6cb9d1942aa622b08fa340a5aff4d
1 /* Check calls to formatted I/O functions (-Wformat).
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
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 COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "toplev.h"
29 #include "c-common.h"
30 #include "intl.h"
31 #include "diagnostic.h"
32 #include "langhooks.h"
33 #include "c-format.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 if (setting != 1)
45 warn_format_nonliteral = setting;
46 warn_format_security = setting;
47 warn_format_y2k = setting;
49 /* Make sure not to disable -Wnonnull if -Wformat=0 is specified. */
50 if (setting)
51 warn_nonnull = setting;
55 /* Handle attributes associated with format checking. */
57 /* This must be in the same order as format_types, except for
58 format_type_error. Target-specific format types do not have
59 matching enum values. */
60 enum format_type { printf_format_type, asm_fprintf_format_type,
61 gcc_diag_format_type, gcc_cdiag_format_type,
62 gcc_cxxdiag_format_type,
63 scanf_format_type, strftime_format_type,
64 strfmon_format_type, format_type_error = -1};
66 typedef struct function_format_info
68 int format_type; /* type of format (printf, scanf, etc.) */
69 unsigned HOST_WIDE_INT format_num; /* number of format argument */
70 unsigned HOST_WIDE_INT first_arg_num; /* number of first arg (zero for varargs) */
71 } function_format_info;
73 static bool decode_format_attr (tree, function_format_info *, int);
74 static int decode_format_type (const char *);
76 static bool check_format_string (tree argument,
77 unsigned HOST_WIDE_INT format_num,
78 int flags, bool *no_add_attrs);
79 static bool get_constant (tree expr, unsigned HOST_WIDE_INT *value,
80 int validated_p);
83 /* Handle a "format_arg" attribute; arguments as in
84 struct attribute_spec.handler. */
85 tree
86 handle_format_arg_attribute (tree *node, tree ARG_UNUSED (name),
87 tree args, int flags, bool *no_add_attrs)
89 tree type = *node;
90 tree format_num_expr = TREE_VALUE (args);
91 unsigned HOST_WIDE_INT format_num = 0;
92 tree argument;
94 if (!get_constant (format_num_expr, &format_num, 0))
96 error ("format string has invalid operand number");
97 *no_add_attrs = true;
98 return NULL_TREE;
101 argument = TYPE_ARG_TYPES (type);
102 if (argument)
104 if (!check_format_string (argument, format_num, flags, no_add_attrs))
105 return NULL_TREE;
108 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
109 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
110 != char_type_node))
112 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
113 error ("function does not return string type");
114 *no_add_attrs = true;
115 return NULL_TREE;
118 return NULL_TREE;
121 /* Verify that the format_num argument is actually a string, in case
122 the format attribute is in error. */
123 static bool
124 check_format_string (tree argument, unsigned HOST_WIDE_INT format_num,
125 int flags, bool *no_add_attrs)
127 unsigned HOST_WIDE_INT i;
129 for (i = 1; i != format_num; i++)
131 if (argument == 0)
132 break;
133 argument = TREE_CHAIN (argument);
136 if (!argument
137 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
138 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
139 != char_type_node))
141 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
142 error ("format string argument not a string type");
143 *no_add_attrs = true;
144 return false;
147 return true;
150 /* Verify EXPR is a constant, and store its value.
151 If validated_p is true there should be no errors.
152 Returns true on success, false otherwise. */
153 static bool
154 get_constant (tree expr, unsigned HOST_WIDE_INT *value, int validated_p)
156 if (TREE_CODE (expr) != INTEGER_CST || TREE_INT_CST_HIGH (expr) != 0)
158 gcc_assert (!validated_p);
159 return false;
162 *value = TREE_INT_CST_LOW (expr);
164 return true;
167 /* Decode the arguments to a "format" attribute into a
168 function_format_info structure. It is already known that the list
169 is of the right length. If VALIDATED_P is true, then these
170 attributes have already been validated and must not be erroneous;
171 if false, it will give an error message. Returns true if the
172 attributes are successfully decoded, false otherwise. */
174 static bool
175 decode_format_attr (tree args, function_format_info *info, int validated_p)
177 tree format_type_id = TREE_VALUE (args);
178 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
179 tree first_arg_num_expr
180 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
182 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
184 gcc_assert (!validated_p);
185 error ("unrecognized format specifier");
186 return false;
188 else
190 const char *p = IDENTIFIER_POINTER (format_type_id);
192 info->format_type = decode_format_type (p);
194 if (info->format_type == format_type_error)
196 gcc_assert (!validated_p);
197 warning (OPT_Wformat, "%qE is an unrecognized format function type",
198 format_type_id);
199 return false;
203 if (!get_constant (format_num_expr, &info->format_num, validated_p))
205 error ("format string has invalid operand number");
206 return false;
209 if (!get_constant (first_arg_num_expr, &info->first_arg_num, validated_p))
211 error ("%<...%> has invalid operand number");
212 return false;
215 if (info->first_arg_num != 0 && info->first_arg_num <= info->format_num)
217 gcc_assert (!validated_p);
218 error ("format string argument follows the args to be formatted");
219 return false;
222 return true;
225 /* Check a call to a format function against a parameter list. */
227 /* The C standard version C++ is treated as equivalent to
228 or inheriting from, for the purpose of format features supported. */
229 #define CPLUSPLUS_STD_VER STD_C94
230 /* The C standard version we are checking formats against when pedantic. */
231 #define C_STD_VER ((int) (c_dialect_cxx () \
232 ? CPLUSPLUS_STD_VER \
233 : (flag_isoc99 \
234 ? STD_C99 \
235 : (flag_isoc94 ? STD_C94 : STD_C89))))
236 /* The name to give to the standard version we are warning about when
237 pedantic. FEATURE_VER is the version in which the feature warned out
238 appeared, which is higher than C_STD_VER. */
239 #define C_STD_NAME(FEATURE_VER) (c_dialect_cxx () \
240 ? "ISO C++" \
241 : ((FEATURE_VER) == STD_EXT \
242 ? "ISO C" \
243 : "ISO C90"))
244 /* Adjust a C standard version, which may be STD_C9L, to account for
245 -Wno-long-long. Returns other standard versions unchanged. */
246 #define ADJ_STD(VER) ((int) ((VER) == STD_C9L \
247 ? (warn_long_long ? STD_C99 : STD_C89) \
248 : (VER)))
250 /* Structure describing details of a type expected in format checking,
251 and the type to check against it. */
252 typedef struct format_wanted_type
254 /* The type wanted. */
255 tree wanted_type;
256 /* The name of this type to use in diagnostics. */
257 const char *wanted_type_name;
258 /* The level of indirection through pointers at which this type occurs. */
259 int pointer_count;
260 /* Whether, when pointer_count is 1, to allow any character type when
261 pedantic, rather than just the character or void type specified. */
262 int char_lenient_flag;
263 /* Whether the argument, dereferenced once, is written into and so the
264 argument must not be a pointer to a const-qualified type. */
265 int writing_in_flag;
266 /* Whether the argument, dereferenced once, is read from and so
267 must not be a NULL pointer. */
268 int reading_from_flag;
269 /* If warnings should be of the form "field precision should have
270 type 'int'", the name to use (in this case "field precision"),
271 otherwise NULL, for "format expects type 'long'" type
272 messages. */
273 const char *name;
274 /* The actual parameter to check against the wanted type. */
275 tree param;
276 /* The argument number of that parameter. */
277 int arg_num;
278 /* The next type to check for this format conversion, or NULL if none. */
279 struct format_wanted_type *next;
280 } format_wanted_type;
283 static const format_length_info printf_length_specs[] =
285 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
286 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L },
287 { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
288 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
289 { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
290 { "Z", FMT_LEN_z, STD_EXT, NULL, 0, 0 },
291 { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
292 { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
293 { NULL, 0, 0, NULL, 0, 0 }
296 /* Length specifiers valid for asm_fprintf. */
297 static const format_length_info asm_fprintf_length_specs[] =
299 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89 },
300 { "w", FMT_LEN_none, STD_C89, NULL, 0, 0 },
301 { NULL, 0, 0, NULL, 0, 0 }
304 /* Length specifiers valid for GCC diagnostics. */
305 static const format_length_info gcc_diag_length_specs[] =
307 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89 },
308 { "w", FMT_LEN_none, STD_C89, NULL, 0, 0 },
309 { NULL, 0, 0, NULL, 0, 0 }
312 /* The custom diagnostics all accept the same length specifiers. */
313 #define gcc_cdiag_length_specs gcc_diag_length_specs
314 #define gcc_cxxdiag_length_specs gcc_diag_length_specs
316 /* This differs from printf_length_specs only in that "Z" is not accepted. */
317 static const format_length_info scanf_length_specs[] =
319 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
320 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L },
321 { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
322 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
323 { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
324 { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
325 { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
326 { NULL, 0, 0, NULL, 0, 0 }
330 /* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings
331 make no sense for a format type not part of any C standard version. */
332 static const format_length_info strfmon_length_specs[] =
334 /* A GNU extension. */
335 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
336 { NULL, 0, 0, NULL, 0, 0 }
339 static const format_flag_spec printf_flag_specs[] =
341 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
342 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
343 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
344 { '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
345 { '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
346 { '\'', 0, 0, N_("''' flag"), N_("the ''' printf flag"), STD_EXT },
347 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' printf flag"), STD_EXT },
348 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
349 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
350 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
351 { 0, 0, 0, NULL, NULL, 0 }
355 static const format_flag_pair printf_flag_pairs[] =
357 { ' ', '+', 1, 0 },
358 { '0', '-', 1, 0 },
359 { '0', 'p', 1, 'i' },
360 { 0, 0, 0, 0 }
363 static const format_flag_spec asm_fprintf_flag_specs[] =
365 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
366 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
367 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
368 { '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
369 { '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
370 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
371 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
372 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
373 { 0, 0, 0, NULL, NULL, 0 }
376 static const format_flag_pair asm_fprintf_flag_pairs[] =
378 { ' ', '+', 1, 0 },
379 { '0', '-', 1, 0 },
380 { '0', 'p', 1, 'i' },
381 { 0, 0, 0, 0 }
384 static const format_flag_pair gcc_diag_flag_pairs[] =
386 { 0, 0, 0, 0 }
389 #define gcc_cdiag_flag_pairs gcc_diag_flag_pairs
390 #define gcc_cxxdiag_flag_pairs gcc_diag_flag_pairs
392 static const format_flag_spec gcc_diag_flag_specs[] =
394 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
395 { 'q', 0, 0, N_("'q' flag"), N_("the 'q' diagnostic flag"), STD_C89 },
396 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
397 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
398 { 0, 0, 0, NULL, NULL, 0 }
401 #define gcc_cdiag_flag_specs gcc_diag_flag_specs
403 static const format_flag_spec gcc_cxxdiag_flag_specs[] =
405 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
406 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
407 { 'q', 0, 0, N_("'q' flag"), N_("the 'q' diagnostic flag"), STD_C89 },
408 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
409 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
410 { 0, 0, 0, NULL, NULL, 0 }
413 static const format_flag_spec scanf_flag_specs[] =
415 { '*', 0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89 },
416 { 'a', 0, 0, N_("'a' flag"), N_("the 'a' scanf flag"), STD_EXT },
417 { 'w', 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89 },
418 { 'L', 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89 },
419 { '\'', 0, 0, N_("''' flag"), N_("the ''' scanf flag"), STD_EXT },
420 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' scanf flag"), STD_EXT },
421 { 0, 0, 0, NULL, NULL, 0 }
425 static const format_flag_pair scanf_flag_pairs[] =
427 { '*', 'L', 0, 0 },
428 { 0, 0, 0, 0 }
432 static const format_flag_spec strftime_flag_specs[] =
434 { '_', 0, 0, N_("'_' flag"), N_("the '_' strftime flag"), STD_EXT },
435 { '-', 0, 0, N_("'-' flag"), N_("the '-' strftime flag"), STD_EXT },
436 { '0', 0, 0, N_("'0' flag"), N_("the '0' strftime flag"), STD_EXT },
437 { '^', 0, 0, N_("'^' flag"), N_("the '^' strftime flag"), STD_EXT },
438 { '#', 0, 0, N_("'#' flag"), N_("the '#' strftime flag"), STD_EXT },
439 { 'w', 0, 0, N_("field width"), N_("field width in strftime format"), STD_EXT },
440 { 'E', 0, 0, N_("'E' modifier"), N_("the 'E' strftime modifier"), STD_C99 },
441 { 'O', 0, 0, N_("'O' modifier"), N_("the 'O' strftime modifier"), STD_C99 },
442 { 'O', 'o', 0, NULL, N_("the 'O' modifier"), STD_EXT },
443 { 0, 0, 0, NULL, NULL, 0 }
447 static const format_flag_pair strftime_flag_pairs[] =
449 { 'E', 'O', 0, 0 },
450 { '_', '-', 0, 0 },
451 { '_', '0', 0, 0 },
452 { '-', '0', 0, 0 },
453 { '^', '#', 0, 0 },
454 { 0, 0, 0, 0 }
458 static const format_flag_spec strfmon_flag_specs[] =
460 { '=', 0, 1, N_("fill character"), N_("fill character in strfmon format"), STD_C89 },
461 { '^', 0, 0, N_("'^' flag"), N_("the '^' strfmon flag"), STD_C89 },
462 { '+', 0, 0, N_("'+' flag"), N_("the '+' strfmon flag"), STD_C89 },
463 { '(', 0, 0, N_("'(' flag"), N_("the '(' strfmon flag"), STD_C89 },
464 { '!', 0, 0, N_("'!' flag"), N_("the '!' strfmon flag"), STD_C89 },
465 { '-', 0, 0, N_("'-' flag"), N_("the '-' strfmon flag"), STD_C89 },
466 { 'w', 0, 0, N_("field width"), N_("field width in strfmon format"), STD_C89 },
467 { '#', 0, 0, N_("left precision"), N_("left precision in strfmon format"), STD_C89 },
468 { 'p', 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89 },
469 { 'L', 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89 },
470 { 0, 0, 0, NULL, NULL, 0 }
473 static const format_flag_pair strfmon_flag_pairs[] =
475 { '+', '(', 0, 0 },
476 { 0, 0, 0, 0 }
480 static const format_char_info print_char_table[] =
482 /* C89 conversion specifiers. */
483 { "di", 0, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, TEX_LL, T99_SST, T99_PD, T99_IM }, "-wp0 +'I", "i", NULL },
484 { "oxX", 0, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "-wp0#", "i", NULL },
485 { "u", 0, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "-wp0'I", "i", NULL },
486 { "fgG", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#'I", "", NULL },
487 { "eE", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#I", "", NULL },
488 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, T94_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
489 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
490 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "c", NULL },
491 { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, BADLEN, T99_SST, T99_PD, T99_IM }, "", "W", NULL },
492 /* C99 conversion specifiers. */
493 { "F", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#'I", "", NULL },
494 { "aA", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "", NULL },
495 /* X/Open conversion specifiers. */
496 { "C", 0, STD_EXT, { TEX_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
497 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "R", NULL },
498 /* GNU conversion specifiers. */
499 { "m", 0, STD_EXT, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "", NULL },
500 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
503 static const format_char_info asm_fprintf_char_table[] =
505 /* C89 conversion specifiers. */
506 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +", "i", NULL },
507 { "oxX", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0#", "i", NULL },
508 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0", "i", NULL },
509 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
510 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
512 /* asm_fprintf conversion specifiers. */
513 { "O", 0, STD_C89, NOARGUMENTS, "", "", NULL },
514 { "R", 0, STD_C89, NOARGUMENTS, "", "", NULL },
515 { "I", 0, STD_C89, NOARGUMENTS, "", "", NULL },
516 { "L", 0, STD_C89, NOARGUMENTS, "", "", NULL },
517 { "U", 0, STD_C89, NOARGUMENTS, "", "", NULL },
518 { "r", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
519 { "@", 0, STD_C89, NOARGUMENTS, "", "", NULL },
520 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
523 static const format_char_info gcc_diag_char_table[] =
525 /* C89 conversion specifiers. */
526 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
527 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
528 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
529 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
530 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
531 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
533 /* Custom conversion specifiers. */
535 /* %H will require "location_t" at runtime. */
536 { "H", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
538 /* These will require a "tree" at runtime. */
539 { "J", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
541 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
542 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
543 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
546 static const format_char_info gcc_cdiag_char_table[] =
548 /* C89 conversion specifiers. */
549 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
550 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
551 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
552 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
553 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
554 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
556 /* Custom conversion specifiers. */
558 /* %H will require "location_t" at runtime. */
559 { "H", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
561 /* These will require a "tree" at runtime. */
562 { "DEFJT", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
564 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
565 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
566 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
569 static const format_char_info gcc_cxxdiag_char_table[] =
571 /* C89 conversion specifiers. */
572 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
573 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
574 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
575 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
576 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
577 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
579 /* Custom conversion specifiers. */
581 /* %H will require "location_t" at runtime. */
582 { "H", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
584 /* These will require a "tree" at runtime. */
585 { "ADEFJTV",0,STD_C89,{ T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+#", "", NULL },
587 /* These accept either an 'int' or an 'enum tree_code' (which is handled as an 'int'.) */
588 { "CLOPQ",0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
590 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
591 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
592 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
595 static const format_char_info scan_char_table[] =
597 /* C89 conversion specifiers. */
598 { "di", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, TEX_LL, T99_SST, T99_PD, T99_IM }, "*w'I", "W", NULL },
599 { "u", 1, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "*w'I", "W", NULL },
600 { "oxX", 1, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "*w", "W", NULL },
601 { "efgEG", 1, STD_C89, { T89_F, BADLEN, BADLEN, T89_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "*w'", "W", NULL },
602 { "c", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "cW", NULL },
603 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "cW", NULL },
604 { "[", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "cW[", NULL },
605 { "p", 2, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
606 { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, BADLEN, T99_SST, T99_PD, T99_IM }, "", "W", NULL },
607 /* C99 conversion specifiers. */
608 { "FaA", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "*w'", "W", NULL },
609 /* X/Open conversion specifiers. */
610 { "C", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
611 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "W", NULL },
612 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
615 static const format_char_info time_char_table[] =
617 /* C89 conversion specifiers. */
618 { "ABZab", 0, STD_C89, NOLENGTHS, "^#", "", NULL },
619 { "cx", 0, STD_C89, NOLENGTHS, "E", "3", NULL },
620 { "HIMSUWdmw", 0, STD_C89, NOLENGTHS, "-_0Ow", "", NULL },
621 { "j", 0, STD_C89, NOLENGTHS, "-_0Ow", "o", NULL },
622 { "p", 0, STD_C89, NOLENGTHS, "#", "", NULL },
623 { "X", 0, STD_C89, NOLENGTHS, "E", "", NULL },
624 { "y", 0, STD_C89, NOLENGTHS, "EO-_0w", "4", NULL },
625 { "Y", 0, STD_C89, NOLENGTHS, "-_0EOw", "o", NULL },
626 { "%", 0, STD_C89, NOLENGTHS, "", "", NULL },
627 /* C99 conversion specifiers. */
628 { "C", 0, STD_C99, NOLENGTHS, "-_0EOw", "o", NULL },
629 { "D", 0, STD_C99, NOLENGTHS, "", "2", NULL },
630 { "eVu", 0, STD_C99, NOLENGTHS, "-_0Ow", "", NULL },
631 { "FRTnrt", 0, STD_C99, NOLENGTHS, "", "", NULL },
632 { "g", 0, STD_C99, NOLENGTHS, "O-_0w", "2o", NULL },
633 { "G", 0, STD_C99, NOLENGTHS, "-_0Ow", "o", NULL },
634 { "h", 0, STD_C99, NOLENGTHS, "^#", "", NULL },
635 { "z", 0, STD_C99, NOLENGTHS, "O", "o", NULL },
636 /* GNU conversion specifiers. */
637 { "kls", 0, STD_EXT, NOLENGTHS, "-_0Ow", "", NULL },
638 { "P", 0, STD_EXT, NOLENGTHS, "", "", NULL },
639 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
642 static const format_char_info monetary_char_table[] =
644 { "in", 0, STD_C89, { T89_D, BADLEN, BADLEN, BADLEN, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "=^+(!-w#p", "", NULL },
645 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
648 /* This must be in the same order as enum format_type. */
649 static const format_kind_info format_types_orig[] =
651 { "printf", printf_length_specs, print_char_table, " +#0-'I", NULL,
652 printf_flag_specs, printf_flag_pairs,
653 FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_EMPTY_PREC_OK,
654 'w', 0, 'p', 0, 'L',
655 &integer_type_node, &integer_type_node
657 { "asm_fprintf", asm_fprintf_length_specs, asm_fprintf_char_table, " +#0-", NULL,
658 asm_fprintf_flag_specs, asm_fprintf_flag_pairs,
659 FMT_FLAG_ARG_CONVERT|FMT_FLAG_EMPTY_PREC_OK,
660 'w', 0, 'p', 0, 'L',
661 NULL, NULL
663 { "gcc_diag", gcc_diag_length_specs, gcc_diag_char_table, "q+", NULL,
664 gcc_diag_flag_specs, gcc_diag_flag_pairs,
665 FMT_FLAG_ARG_CONVERT,
666 0, 0, 'p', 0, 'L',
667 NULL, &integer_type_node
669 { "gcc_cdiag", gcc_cdiag_length_specs, gcc_cdiag_char_table, "q+", NULL,
670 gcc_cdiag_flag_specs, gcc_cdiag_flag_pairs,
671 FMT_FLAG_ARG_CONVERT,
672 0, 0, 'p', 0, 'L',
673 NULL, &integer_type_node
675 { "gcc_cxxdiag", gcc_cxxdiag_length_specs, gcc_cxxdiag_char_table, "q+#", NULL,
676 gcc_cxxdiag_flag_specs, gcc_cxxdiag_flag_pairs,
677 FMT_FLAG_ARG_CONVERT,
678 0, 0, 'p', 0, 'L',
679 NULL, &integer_type_node
681 { "scanf", scanf_length_specs, scan_char_table, "*'I", NULL,
682 scanf_flag_specs, scanf_flag_pairs,
683 FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_ZERO_WIDTH_BAD|FMT_FLAG_DOLLAR_GAP_POINTER_OK,
684 'w', 0, 0, '*', 'L',
685 NULL, NULL
687 { "strftime", NULL, time_char_table, "_-0^#", "EO",
688 strftime_flag_specs, strftime_flag_pairs,
689 FMT_FLAG_FANCY_PERCENT_OK, 'w', 0, 0, 0, 0,
690 NULL, NULL
692 { "strfmon", strfmon_length_specs, monetary_char_table, "=^+(!-", NULL,
693 strfmon_flag_specs, strfmon_flag_pairs,
694 FMT_FLAG_ARG_CONVERT, 'w', '#', 'p', 0, 'L',
695 NULL, NULL
699 /* This layer of indirection allows GCC to reassign format_types with
700 new data if necessary, while still allowing the original data to be
701 const. */
702 static const format_kind_info *format_types = format_types_orig;
703 /* We can modify this one. We also add target-specific format types
704 to the end of the array. */
705 static format_kind_info *dynamic_format_types;
707 static int n_format_types = ARRAY_SIZE (format_types_orig);
709 /* Structure detailing the results of checking a format function call
710 where the format expression may be a conditional expression with
711 many leaves resulting from nested conditional expressions. */
712 typedef struct
714 /* Number of leaves of the format argument that could not be checked
715 as they were not string literals. */
716 int number_non_literal;
717 /* Number of leaves of the format argument that were null pointers or
718 string literals, but had extra format arguments. */
719 int number_extra_args;
720 /* Number of leaves of the format argument that were null pointers or
721 string literals, but had extra format arguments and used $ operand
722 numbers. */
723 int number_dollar_extra_args;
724 /* Number of leaves of the format argument that were wide string
725 literals. */
726 int number_wide;
727 /* Number of leaves of the format argument that were empty strings. */
728 int number_empty;
729 /* Number of leaves of the format argument that were unterminated
730 strings. */
731 int number_unterminated;
732 /* Number of leaves of the format argument that were not counted above. */
733 int number_other;
734 } format_check_results;
736 typedef struct
738 format_check_results *res;
739 function_format_info *info;
740 tree params;
741 } format_check_context;
743 static void check_format_info (function_format_info *, tree);
744 static void check_format_arg (void *, tree, unsigned HOST_WIDE_INT);
745 static void check_format_info_main (format_check_results *,
746 function_format_info *,
747 const char *, int, tree,
748 unsigned HOST_WIDE_INT);
750 static void init_dollar_format_checking (int, tree);
751 static int maybe_read_dollar_number (const char **, int,
752 tree, tree *, const format_kind_info *);
753 static bool avoid_dollar_number (const char *);
754 static void finish_dollar_format_checking (format_check_results *, int);
756 static const format_flag_spec *get_flag_spec (const format_flag_spec *,
757 int, const char *);
759 static void check_format_types (format_wanted_type *, const char *, int);
760 static void format_type_warning (const char *, const char *, int, tree,
761 int, const char *, tree, int);
763 /* Decode a format type from a string, returning the type, or
764 format_type_error if not valid, in which case the caller should print an
765 error message. */
766 static int
767 decode_format_type (const char *s)
769 int i;
770 int slen;
771 slen = strlen (s);
772 for (i = 0; i < n_format_types; i++)
774 int alen;
775 if (!strcmp (s, format_types[i].name))
776 return i;
777 alen = strlen (format_types[i].name);
778 if (slen == alen + 4 && s[0] == '_' && s[1] == '_'
779 && s[slen - 1] == '_' && s[slen - 2] == '_'
780 && !strncmp (s + 2, format_types[i].name, alen))
781 return i;
783 return format_type_error;
787 /* Check the argument list of a call to printf, scanf, etc.
788 ATTRS are the attributes on the function type.
789 PARAMS is the list of argument values. Also, if -Wmissing-format-attribute,
790 warn for calls to vprintf or vscanf in functions with no such format
791 attribute themselves. */
793 void
794 check_function_format (tree attrs, tree params)
796 tree a;
798 /* See if this function has any format attributes. */
799 for (a = attrs; a; a = TREE_CHAIN (a))
801 if (is_attribute_p ("format", TREE_PURPOSE (a)))
803 /* Yup; check it. */
804 function_format_info info;
805 decode_format_attr (TREE_VALUE (a), &info, 1);
806 check_format_info (&info, params);
807 if (warn_missing_format_attribute && info.first_arg_num == 0
808 && (format_types[info.format_type].flags
809 & (int) FMT_FLAG_ARG_CONVERT))
811 tree c;
812 for (c = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
814 c = TREE_CHAIN (c))
815 if (is_attribute_p ("format", TREE_PURPOSE (c))
816 && (decode_format_type (IDENTIFIER_POINTER
817 (TREE_VALUE (TREE_VALUE (c))))
818 == info.format_type))
819 break;
820 if (c == NULL_TREE)
822 /* Check if the current function has a parameter to which
823 the format attribute could be attached; if not, it
824 can't be a candidate for a format attribute, despite
825 the vprintf-like or vscanf-like call. */
826 tree args;
827 for (args = DECL_ARGUMENTS (current_function_decl);
828 args != 0;
829 args = TREE_CHAIN (args))
831 if (TREE_CODE (TREE_TYPE (args)) == POINTER_TYPE
832 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args)))
833 == char_type_node))
834 break;
836 if (args != 0)
837 warning (OPT_Wattributes, "function might be possible "
838 "candidate for %qs format attribute",
839 format_types[info.format_type].name);
847 /* Variables used by the checking of $ operand number formats. */
848 static char *dollar_arguments_used = NULL;
849 static char *dollar_arguments_pointer_p = NULL;
850 static int dollar_arguments_alloc = 0;
851 static int dollar_arguments_count;
852 static int dollar_first_arg_num;
853 static int dollar_max_arg_used;
854 static int dollar_format_warned;
856 /* Initialize the checking for a format string that may contain $
857 parameter number specifications; we will need to keep track of whether
858 each parameter has been used. FIRST_ARG_NUM is the number of the first
859 argument that is a parameter to the format, or 0 for a vprintf-style
860 function; PARAMS is the list of arguments starting at this argument. */
862 static void
863 init_dollar_format_checking (int first_arg_num, tree params)
865 tree oparams = params;
867 dollar_first_arg_num = first_arg_num;
868 dollar_arguments_count = 0;
869 dollar_max_arg_used = 0;
870 dollar_format_warned = 0;
871 if (first_arg_num > 0)
873 while (params)
875 dollar_arguments_count++;
876 params = TREE_CHAIN (params);
879 if (dollar_arguments_alloc < dollar_arguments_count)
881 if (dollar_arguments_used)
882 free (dollar_arguments_used);
883 if (dollar_arguments_pointer_p)
884 free (dollar_arguments_pointer_p);
885 dollar_arguments_alloc = dollar_arguments_count;
886 dollar_arguments_used = XNEWVEC (char, dollar_arguments_alloc);
887 dollar_arguments_pointer_p = XNEWVEC (char, dollar_arguments_alloc);
889 if (dollar_arguments_alloc)
891 memset (dollar_arguments_used, 0, dollar_arguments_alloc);
892 if (first_arg_num > 0)
894 int i = 0;
895 params = oparams;
896 while (params)
898 dollar_arguments_pointer_p[i] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params)))
899 == POINTER_TYPE);
900 params = TREE_CHAIN (params);
901 i++;
908 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
909 is set, it is an error if one is not found; otherwise, it is OK. If
910 such a number is found, check whether it is within range and mark that
911 numbered operand as being used for later checking. Returns the operand
912 number if found and within range, zero if no such number was found and
913 this is OK, or -1 on error. PARAMS points to the first operand of the
914 format; PARAM_PTR is made to point to the parameter referred to. If
915 a $ format is found, *FORMAT is updated to point just after it. */
917 static int
918 maybe_read_dollar_number (const char **format,
919 int dollar_needed, tree params, tree *param_ptr,
920 const format_kind_info *fki)
922 int argnum;
923 int overflow_flag;
924 const char *fcp = *format;
925 if (!ISDIGIT (*fcp))
927 if (dollar_needed)
929 warning (OPT_Wformat, "missing $ operand number in format");
930 return -1;
932 else
933 return 0;
935 argnum = 0;
936 overflow_flag = 0;
937 while (ISDIGIT (*fcp))
939 int nargnum;
940 nargnum = 10 * argnum + (*fcp - '0');
941 if (nargnum < 0 || nargnum / 10 != argnum)
942 overflow_flag = 1;
943 argnum = nargnum;
944 fcp++;
946 if (*fcp != '$')
948 if (dollar_needed)
950 warning (OPT_Wformat, "missing $ operand number in format");
951 return -1;
953 else
954 return 0;
956 *format = fcp + 1;
957 if (pedantic && !dollar_format_warned)
959 warning (OPT_Wformat, "%s does not support %%n$ operand number formats",
960 C_STD_NAME (STD_EXT));
961 dollar_format_warned = 1;
963 if (overflow_flag || argnum == 0
964 || (dollar_first_arg_num && argnum > dollar_arguments_count))
966 warning (OPT_Wformat, "operand number out of range in format");
967 return -1;
969 if (argnum > dollar_max_arg_used)
970 dollar_max_arg_used = argnum;
971 /* For vprintf-style functions we may need to allocate more memory to
972 track which arguments are used. */
973 while (dollar_arguments_alloc < dollar_max_arg_used)
975 int nalloc;
976 nalloc = 2 * dollar_arguments_alloc + 16;
977 dollar_arguments_used = XRESIZEVEC (char, dollar_arguments_used,
978 nalloc);
979 dollar_arguments_pointer_p = XRESIZEVEC (char, dollar_arguments_pointer_p,
980 nalloc);
981 memset (dollar_arguments_used + dollar_arguments_alloc, 0,
982 nalloc - dollar_arguments_alloc);
983 dollar_arguments_alloc = nalloc;
985 if (!(fki->flags & (int) FMT_FLAG_DOLLAR_MULTIPLE)
986 && dollar_arguments_used[argnum - 1] == 1)
988 dollar_arguments_used[argnum - 1] = 2;
989 warning (OPT_Wformat, "format argument %d used more than once in %s format",
990 argnum, fki->name);
992 else
993 dollar_arguments_used[argnum - 1] = 1;
994 if (dollar_first_arg_num)
996 int i;
997 *param_ptr = params;
998 for (i = 1; i < argnum && *param_ptr != 0; i++)
999 *param_ptr = TREE_CHAIN (*param_ptr);
1001 /* This case shouldn't be caught here. */
1002 gcc_assert (*param_ptr);
1004 else
1005 *param_ptr = 0;
1006 return argnum;
1009 /* Ensure that FORMAT does not start with a decimal number followed by
1010 a $; give a diagnostic and return true if it does, false otherwise. */
1012 static bool
1013 avoid_dollar_number (const char *format)
1015 if (!ISDIGIT (*format))
1016 return false;
1017 while (ISDIGIT (*format))
1018 format++;
1019 if (*format == '$')
1021 warning (OPT_Wformat, "$ operand number used after format without operand number");
1022 return true;
1024 return false;
1028 /* Finish the checking for a format string that used $ operand number formats
1029 instead of non-$ formats. We check for unused operands before used ones
1030 (a serious error, since the implementation of the format function
1031 can't know what types to pass to va_arg to find the later arguments).
1032 and for unused operands at the end of the format (if we know how many
1033 arguments the format had, so not for vprintf). If there were operand
1034 numbers out of range on a non-vprintf-style format, we won't have reached
1035 here. If POINTER_GAP_OK, unused arguments are OK if all arguments are
1036 pointers. */
1038 static void
1039 finish_dollar_format_checking (format_check_results *res, int pointer_gap_ok)
1041 int i;
1042 bool found_pointer_gap = false;
1043 for (i = 0; i < dollar_max_arg_used; i++)
1045 if (!dollar_arguments_used[i])
1047 if (pointer_gap_ok && (dollar_first_arg_num == 0
1048 || dollar_arguments_pointer_p[i]))
1049 found_pointer_gap = true;
1050 else
1051 warning (OPT_Wformat,
1052 "format argument %d unused before used argument %d in $-style format",
1053 i + 1, dollar_max_arg_used);
1056 if (found_pointer_gap
1057 || (dollar_first_arg_num
1058 && dollar_max_arg_used < dollar_arguments_count))
1060 res->number_other--;
1061 res->number_dollar_extra_args++;
1066 /* Retrieve the specification for a format flag. SPEC contains the
1067 specifications for format flags for the applicable kind of format.
1068 FLAG is the flag in question. If PREDICATES is NULL, the basic
1069 spec for that flag must be retrieved and must exist. If
1070 PREDICATES is not NULL, it is a string listing possible predicates
1071 for the spec entry; if an entry predicated on any of these is
1072 found, it is returned, otherwise NULL is returned. */
1074 static const format_flag_spec *
1075 get_flag_spec (const format_flag_spec *spec, int flag, const char *predicates)
1077 int i;
1078 for (i = 0; spec[i].flag_char != 0; i++)
1080 if (spec[i].flag_char != flag)
1081 continue;
1082 if (predicates != NULL)
1084 if (spec[i].predicate != 0
1085 && strchr (predicates, spec[i].predicate) != 0)
1086 return &spec[i];
1088 else if (spec[i].predicate == 0)
1089 return &spec[i];
1091 gcc_assert (predicates);
1092 return NULL;
1096 /* Check the argument list of a call to printf, scanf, etc.
1097 INFO points to the function_format_info structure.
1098 PARAMS is the list of argument values. */
1100 static void
1101 check_format_info (function_format_info *info, tree params)
1103 format_check_context format_ctx;
1104 unsigned HOST_WIDE_INT arg_num;
1105 tree format_tree;
1106 format_check_results res;
1107 /* Skip to format argument. If the argument isn't available, there's
1108 no work for us to do; prototype checking will catch the problem. */
1109 for (arg_num = 1; ; ++arg_num)
1111 if (params == 0)
1112 return;
1113 if (arg_num == info->format_num)
1114 break;
1115 params = TREE_CHAIN (params);
1117 format_tree = TREE_VALUE (params);
1118 params = TREE_CHAIN (params);
1119 if (format_tree == 0)
1120 return;
1122 res.number_non_literal = 0;
1123 res.number_extra_args = 0;
1124 res.number_dollar_extra_args = 0;
1125 res.number_wide = 0;
1126 res.number_empty = 0;
1127 res.number_unterminated = 0;
1128 res.number_other = 0;
1130 format_ctx.res = &res;
1131 format_ctx.info = info;
1132 format_ctx.params = params;
1134 check_function_arguments_recurse (check_format_arg, &format_ctx,
1135 format_tree, arg_num);
1137 if (res.number_non_literal > 0)
1139 /* Functions taking a va_list normally pass a non-literal format
1140 string. These functions typically are declared with
1141 first_arg_num == 0, so avoid warning in those cases. */
1142 if (!(format_types[info->format_type].flags & (int) FMT_FLAG_ARG_CONVERT))
1144 /* For strftime-like formats, warn for not checking the format
1145 string; but there are no arguments to check. */
1146 warning (OPT_Wformat_nonliteral,
1147 "format not a string literal, format string not checked");
1149 else if (info->first_arg_num != 0)
1151 /* If there are no arguments for the format at all, we may have
1152 printf (foo) which is likely to be a security hole. */
1153 while (arg_num + 1 < info->first_arg_num)
1155 if (params == 0)
1156 break;
1157 params = TREE_CHAIN (params);
1158 ++arg_num;
1160 if (params == 0 && warn_format_security)
1161 warning (OPT_Wformat_security,
1162 "format not a string literal and no format arguments");
1163 else if (params == 0 && warn_format_nonliteral)
1164 warning (OPT_Wformat_nonliteral,
1165 "format not a string literal and no format arguments");
1166 else
1167 warning (OPT_Wformat_nonliteral,
1168 "format not a string literal, argument types not checked");
1172 /* If there were extra arguments to the format, normally warn. However,
1173 the standard does say extra arguments are ignored, so in the specific
1174 case where we have multiple leaves (conditional expressions or
1175 ngettext) allow extra arguments if at least one leaf didn't have extra
1176 arguments, but was otherwise OK (either non-literal or checked OK).
1177 If the format is an empty string, this should be counted similarly to the
1178 case of extra format arguments. */
1179 if (res.number_extra_args > 0 && res.number_non_literal == 0
1180 && res.number_other == 0)
1181 warning (OPT_Wformat_extra_args, "too many arguments for format");
1182 if (res.number_dollar_extra_args > 0 && res.number_non_literal == 0
1183 && res.number_other == 0)
1184 warning (OPT_Wformat_extra_args, "unused arguments in $-style format");
1185 if (res.number_empty > 0 && res.number_non_literal == 0
1186 && res.number_other == 0)
1187 warning (OPT_Wformat_zero_length, "zero-length %s format string",
1188 format_types[info->format_type].name);
1190 if (res.number_wide > 0)
1191 warning (OPT_Wformat, "format is a wide character string");
1193 if (res.number_unterminated > 0)
1194 warning (OPT_Wformat, "unterminated format string");
1197 /* Callback from check_function_arguments_recurse to check a
1198 format string. FORMAT_TREE is the format parameter. ARG_NUM
1199 is the number of the format argument. CTX points to a
1200 format_check_context. */
1202 static void
1203 check_format_arg (void *ctx, tree format_tree,
1204 unsigned HOST_WIDE_INT arg_num)
1206 format_check_context *format_ctx = (format_check_context *) ctx;
1207 format_check_results *res = format_ctx->res;
1208 function_format_info *info = format_ctx->info;
1209 tree params = format_ctx->params;
1211 int format_length;
1212 HOST_WIDE_INT offset;
1213 const char *format_chars;
1214 tree array_size = 0;
1215 tree array_init;
1217 if (integer_zerop (format_tree))
1219 /* Skip to first argument to check, so we can see if this format
1220 has any arguments (it shouldn't). */
1221 while (arg_num + 1 < info->first_arg_num)
1223 if (params == 0)
1224 return;
1225 params = TREE_CHAIN (params);
1226 ++arg_num;
1229 if (params == 0)
1230 res->number_other++;
1231 else
1232 res->number_extra_args++;
1234 return;
1237 offset = 0;
1238 if (TREE_CODE (format_tree) == PLUS_EXPR)
1240 tree arg0, arg1;
1242 arg0 = TREE_OPERAND (format_tree, 0);
1243 arg1 = TREE_OPERAND (format_tree, 1);
1244 STRIP_NOPS (arg0);
1245 STRIP_NOPS (arg1);
1246 if (TREE_CODE (arg1) == INTEGER_CST)
1247 format_tree = arg0;
1248 else if (TREE_CODE (arg0) == INTEGER_CST)
1250 format_tree = arg1;
1251 arg1 = arg0;
1253 else
1255 res->number_non_literal++;
1256 return;
1258 if (!host_integerp (arg1, 0)
1259 || (offset = tree_low_cst (arg1, 0)) < 0)
1261 res->number_non_literal++;
1262 return;
1265 if (TREE_CODE (format_tree) != ADDR_EXPR)
1267 res->number_non_literal++;
1268 return;
1270 format_tree = TREE_OPERAND (format_tree, 0);
1271 if (TREE_CODE (format_tree) == ARRAY_REF
1272 && host_integerp (TREE_OPERAND (format_tree, 1), 0)
1273 && (offset += tree_low_cst (TREE_OPERAND (format_tree, 1), 0)) >= 0)
1274 format_tree = TREE_OPERAND (format_tree, 0);
1275 if (TREE_CODE (format_tree) == VAR_DECL
1276 && TREE_CODE (TREE_TYPE (format_tree)) == ARRAY_TYPE
1277 && (array_init = decl_constant_value (format_tree)) != format_tree
1278 && TREE_CODE (array_init) == STRING_CST)
1280 /* Extract the string constant initializer. Note that this may include
1281 a trailing NUL character that is not in the array (e.g.
1282 const char a[3] = "foo";). */
1283 array_size = DECL_SIZE_UNIT (format_tree);
1284 format_tree = array_init;
1286 if (TREE_CODE (format_tree) != STRING_CST)
1288 res->number_non_literal++;
1289 return;
1291 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree))) != char_type_node)
1293 res->number_wide++;
1294 return;
1296 format_chars = TREE_STRING_POINTER (format_tree);
1297 format_length = TREE_STRING_LENGTH (format_tree);
1298 if (array_size != 0)
1300 /* Variable length arrays can't be initialized. */
1301 gcc_assert (TREE_CODE (array_size) == INTEGER_CST);
1303 if (host_integerp (array_size, 0))
1305 HOST_WIDE_INT array_size_value = TREE_INT_CST_LOW (array_size);
1306 if (array_size_value > 0
1307 && array_size_value == (int) array_size_value
1308 && format_length > array_size_value)
1309 format_length = array_size_value;
1312 if (offset)
1314 if (offset >= format_length)
1316 res->number_non_literal++;
1317 return;
1319 format_chars += offset;
1320 format_length -= offset;
1322 if (format_length < 1)
1324 res->number_unterminated++;
1325 return;
1327 if (format_length == 1)
1329 res->number_empty++;
1330 return;
1332 if (format_chars[--format_length] != 0)
1334 res->number_unterminated++;
1335 return;
1338 /* Skip to first argument to check. */
1339 while (arg_num + 1 < info->first_arg_num)
1341 if (params == 0)
1342 return;
1343 params = TREE_CHAIN (params);
1344 ++arg_num;
1346 /* Provisionally increment res->number_other; check_format_info_main
1347 will decrement it if it finds there are extra arguments, but this way
1348 need not adjust it for every return. */
1349 res->number_other++;
1350 check_format_info_main (res, info, format_chars, format_length,
1351 params, arg_num);
1355 /* Do the main part of checking a call to a format function. FORMAT_CHARS
1356 is the NUL-terminated format string (which at this point may contain
1357 internal NUL characters); FORMAT_LENGTH is its length (excluding the
1358 terminating NUL character). ARG_NUM is one less than the number of
1359 the first format argument to check; PARAMS points to that format
1360 argument in the list of arguments. */
1362 static void
1363 check_format_info_main (format_check_results *res,
1364 function_format_info *info, const char *format_chars,
1365 int format_length, tree params,
1366 unsigned HOST_WIDE_INT arg_num)
1368 const char *orig_format_chars = format_chars;
1369 tree first_fillin_param = params;
1371 const format_kind_info *fki = &format_types[info->format_type];
1372 const format_flag_spec *flag_specs = fki->flag_specs;
1373 const format_flag_pair *bad_flag_pairs = fki->bad_flag_pairs;
1375 /* -1 if no conversions taking an operand have been found; 0 if one has
1376 and it didn't use $; 1 if $ formats are in use. */
1377 int has_operand_number = -1;
1379 init_dollar_format_checking (info->first_arg_num, first_fillin_param);
1381 while (1)
1383 int i;
1384 int suppressed = FALSE;
1385 const char *length_chars = NULL;
1386 enum format_lengths length_chars_val = FMT_LEN_none;
1387 enum format_std_version length_chars_std = STD_C89;
1388 int format_char;
1389 tree cur_param;
1390 tree wanted_type;
1391 int main_arg_num = 0;
1392 tree main_arg_params = 0;
1393 enum format_std_version wanted_type_std;
1394 const char *wanted_type_name;
1395 format_wanted_type width_wanted_type;
1396 format_wanted_type precision_wanted_type;
1397 format_wanted_type main_wanted_type;
1398 format_wanted_type *first_wanted_type = NULL;
1399 format_wanted_type *last_wanted_type = NULL;
1400 const format_length_info *fli = NULL;
1401 const format_char_info *fci = NULL;
1402 char flag_chars[256];
1403 int aflag = 0;
1404 const char *format_start = format_chars;
1405 if (*format_chars == 0)
1407 if (format_chars - orig_format_chars != format_length)
1408 warning (OPT_Wformat, "embedded %<\\0%> in format");
1409 if (info->first_arg_num != 0 && params != 0
1410 && has_operand_number <= 0)
1412 res->number_other--;
1413 res->number_extra_args++;
1415 if (has_operand_number > 0)
1416 finish_dollar_format_checking (res, fki->flags & (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK);
1417 return;
1419 if (*format_chars++ != '%')
1420 continue;
1421 if (*format_chars == 0)
1423 warning (OPT_Wformat, "spurious trailing %<%%%> in format");
1424 continue;
1426 if (*format_chars == '%')
1428 ++format_chars;
1429 continue;
1431 flag_chars[0] = 0;
1433 if ((fki->flags & (int) FMT_FLAG_USE_DOLLAR) && has_operand_number != 0)
1435 /* Possibly read a $ operand number at the start of the format.
1436 If one was previously used, one is required here. If one
1437 is not used here, we can't immediately conclude this is a
1438 format without them, since it could be printf %m or scanf %*. */
1439 int opnum;
1440 opnum = maybe_read_dollar_number (&format_chars, 0,
1441 first_fillin_param,
1442 &main_arg_params, fki);
1443 if (opnum == -1)
1444 return;
1445 else if (opnum > 0)
1447 has_operand_number = 1;
1448 main_arg_num = opnum + info->first_arg_num - 1;
1451 else if (fki->flags & FMT_FLAG_USE_DOLLAR)
1453 if (avoid_dollar_number (format_chars))
1454 return;
1457 /* Read any format flags, but do not yet validate them beyond removing
1458 duplicates, since in general validation depends on the rest of
1459 the format. */
1460 while (*format_chars != 0
1461 && strchr (fki->flag_chars, *format_chars) != 0)
1463 const format_flag_spec *s = get_flag_spec (flag_specs,
1464 *format_chars, NULL);
1465 if (strchr (flag_chars, *format_chars) != 0)
1467 warning (OPT_Wformat, "repeated %s in format", _(s->name));
1469 else
1471 i = strlen (flag_chars);
1472 flag_chars[i++] = *format_chars;
1473 flag_chars[i] = 0;
1475 if (s->skip_next_char)
1477 ++format_chars;
1478 if (*format_chars == 0)
1480 warning (OPT_Wformat, "missing fill character at end of strfmon format");
1481 return;
1484 ++format_chars;
1487 /* Read any format width, possibly * or *m$. */
1488 if (fki->width_char != 0)
1490 if (fki->width_type != NULL && *format_chars == '*')
1492 i = strlen (flag_chars);
1493 flag_chars[i++] = fki->width_char;
1494 flag_chars[i] = 0;
1495 /* "...a field width...may be indicated by an asterisk.
1496 In this case, an int argument supplies the field width..." */
1497 ++format_chars;
1498 if (has_operand_number != 0)
1500 int opnum;
1501 opnum = maybe_read_dollar_number (&format_chars,
1502 has_operand_number == 1,
1503 first_fillin_param,
1504 &params, fki);
1505 if (opnum == -1)
1506 return;
1507 else if (opnum > 0)
1509 has_operand_number = 1;
1510 arg_num = opnum + info->first_arg_num - 1;
1512 else
1513 has_operand_number = 0;
1515 else
1517 if (avoid_dollar_number (format_chars))
1518 return;
1520 if (info->first_arg_num != 0)
1522 if (params == 0)
1524 warning (OPT_Wformat, "too few arguments for format");
1525 return;
1527 cur_param = TREE_VALUE (params);
1528 if (has_operand_number <= 0)
1530 params = TREE_CHAIN (params);
1531 ++arg_num;
1533 width_wanted_type.wanted_type = *fki->width_type;
1534 width_wanted_type.wanted_type_name = NULL;
1535 width_wanted_type.pointer_count = 0;
1536 width_wanted_type.char_lenient_flag = 0;
1537 width_wanted_type.writing_in_flag = 0;
1538 width_wanted_type.reading_from_flag = 0;
1539 width_wanted_type.name = _("field width");
1540 width_wanted_type.param = cur_param;
1541 width_wanted_type.arg_num = arg_num;
1542 width_wanted_type.next = NULL;
1543 if (last_wanted_type != 0)
1544 last_wanted_type->next = &width_wanted_type;
1545 if (first_wanted_type == 0)
1546 first_wanted_type = &width_wanted_type;
1547 last_wanted_type = &width_wanted_type;
1550 else
1552 /* Possibly read a numeric width. If the width is zero,
1553 we complain if appropriate. */
1554 int non_zero_width_char = FALSE;
1555 int found_width = FALSE;
1556 while (ISDIGIT (*format_chars))
1558 found_width = TRUE;
1559 if (*format_chars != '0')
1560 non_zero_width_char = TRUE;
1561 ++format_chars;
1563 if (found_width && !non_zero_width_char &&
1564 (fki->flags & (int) FMT_FLAG_ZERO_WIDTH_BAD))
1565 warning (OPT_Wformat, "zero width in %s format", fki->name);
1566 if (found_width)
1568 i = strlen (flag_chars);
1569 flag_chars[i++] = fki->width_char;
1570 flag_chars[i] = 0;
1575 /* Read any format left precision (must be a number, not *). */
1576 if (fki->left_precision_char != 0 && *format_chars == '#')
1578 ++format_chars;
1579 i = strlen (flag_chars);
1580 flag_chars[i++] = fki->left_precision_char;
1581 flag_chars[i] = 0;
1582 if (!ISDIGIT (*format_chars))
1583 warning (OPT_Wformat, "empty left precision in %s format", fki->name);
1584 while (ISDIGIT (*format_chars))
1585 ++format_chars;
1588 /* Read any format precision, possibly * or *m$. */
1589 if (fki->precision_char != 0 && *format_chars == '.')
1591 ++format_chars;
1592 i = strlen (flag_chars);
1593 flag_chars[i++] = fki->precision_char;
1594 flag_chars[i] = 0;
1595 if (fki->precision_type != NULL && *format_chars == '*')
1597 /* "...a...precision...may be indicated by an asterisk.
1598 In this case, an int argument supplies the...precision." */
1599 ++format_chars;
1600 if (has_operand_number != 0)
1602 int opnum;
1603 opnum = maybe_read_dollar_number (&format_chars,
1604 has_operand_number == 1,
1605 first_fillin_param,
1606 &params, fki);
1607 if (opnum == -1)
1608 return;
1609 else if (opnum > 0)
1611 has_operand_number = 1;
1612 arg_num = opnum + info->first_arg_num - 1;
1614 else
1615 has_operand_number = 0;
1617 else
1619 if (avoid_dollar_number (format_chars))
1620 return;
1622 if (info->first_arg_num != 0)
1624 if (params == 0)
1626 warning (OPT_Wformat, "too few arguments for format");
1627 return;
1629 cur_param = TREE_VALUE (params);
1630 if (has_operand_number <= 0)
1632 params = TREE_CHAIN (params);
1633 ++arg_num;
1635 precision_wanted_type.wanted_type = *fki->precision_type;
1636 precision_wanted_type.wanted_type_name = NULL;
1637 precision_wanted_type.pointer_count = 0;
1638 precision_wanted_type.char_lenient_flag = 0;
1639 precision_wanted_type.writing_in_flag = 0;
1640 precision_wanted_type.reading_from_flag = 0;
1641 precision_wanted_type.name = _("field precision");
1642 precision_wanted_type.param = cur_param;
1643 precision_wanted_type.arg_num = arg_num;
1644 precision_wanted_type.next = NULL;
1645 if (last_wanted_type != 0)
1646 last_wanted_type->next = &precision_wanted_type;
1647 if (first_wanted_type == 0)
1648 first_wanted_type = &precision_wanted_type;
1649 last_wanted_type = &precision_wanted_type;
1652 else
1654 if (!(fki->flags & (int) FMT_FLAG_EMPTY_PREC_OK)
1655 && !ISDIGIT (*format_chars))
1656 warning (OPT_Wformat, "empty precision in %s format", fki->name);
1657 while (ISDIGIT (*format_chars))
1658 ++format_chars;
1662 /* Read any length modifier, if this kind of format has them. */
1663 fli = fki->length_char_specs;
1664 length_chars = NULL;
1665 length_chars_val = FMT_LEN_none;
1666 length_chars_std = STD_C89;
1667 if (fli)
1669 while (fli->name != 0 && fli->name[0] != *format_chars)
1670 fli++;
1671 if (fli->name != 0)
1673 format_chars++;
1674 if (fli->double_name != 0 && fli->name[0] == *format_chars)
1676 format_chars++;
1677 length_chars = fli->double_name;
1678 length_chars_val = fli->double_index;
1679 length_chars_std = fli->double_std;
1681 else
1683 length_chars = fli->name;
1684 length_chars_val = fli->index;
1685 length_chars_std = fli->std;
1687 i = strlen (flag_chars);
1688 flag_chars[i++] = fki->length_code_char;
1689 flag_chars[i] = 0;
1691 if (pedantic)
1693 /* Warn if the length modifier is non-standard. */
1694 if (ADJ_STD (length_chars_std) > C_STD_VER)
1695 warning (OPT_Wformat,
1696 "%s does not support the %qs %s length modifier",
1697 C_STD_NAME (length_chars_std), length_chars,
1698 fki->name);
1702 /* Read any modifier (strftime E/O). */
1703 if (fki->modifier_chars != NULL)
1705 while (*format_chars != 0
1706 && strchr (fki->modifier_chars, *format_chars) != 0)
1708 if (strchr (flag_chars, *format_chars) != 0)
1710 const format_flag_spec *s = get_flag_spec (flag_specs,
1711 *format_chars, NULL);
1712 warning (OPT_Wformat, "repeated %s in format", _(s->name));
1714 else
1716 i = strlen (flag_chars);
1717 flag_chars[i++] = *format_chars;
1718 flag_chars[i] = 0;
1720 ++format_chars;
1724 /* Handle the scanf allocation kludge. */
1725 if (fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
1727 if (*format_chars == 'a' && !flag_isoc99)
1729 if (format_chars[1] == 's' || format_chars[1] == 'S'
1730 || format_chars[1] == '[')
1732 /* 'a' is used as a flag. */
1733 i = strlen (flag_chars);
1734 flag_chars[i++] = 'a';
1735 flag_chars[i] = 0;
1736 format_chars++;
1741 format_char = *format_chars;
1742 if (format_char == 0
1743 || (!(fki->flags & (int) FMT_FLAG_FANCY_PERCENT_OK)
1744 && format_char == '%'))
1746 warning (OPT_Wformat, "conversion lacks type at end of format");
1747 continue;
1749 format_chars++;
1750 fci = fki->conversion_specs;
1751 while (fci->format_chars != 0
1752 && strchr (fci->format_chars, format_char) == 0)
1753 ++fci;
1754 if (fci->format_chars == 0)
1756 if (ISGRAPH (format_char))
1757 warning (OPT_Wformat, "unknown conversion type character %qc in format",
1758 format_char);
1759 else
1760 warning (OPT_Wformat, "unknown conversion type character 0x%x in format",
1761 format_char);
1762 continue;
1764 if (pedantic)
1766 if (ADJ_STD (fci->std) > C_STD_VER)
1767 warning (OPT_Wformat, "%s does not support the %<%%%c%> %s format",
1768 C_STD_NAME (fci->std), format_char, fki->name);
1771 /* Validate the individual flags used, removing any that are invalid. */
1773 int d = 0;
1774 for (i = 0; flag_chars[i] != 0; i++)
1776 const format_flag_spec *s = get_flag_spec (flag_specs,
1777 flag_chars[i], NULL);
1778 flag_chars[i - d] = flag_chars[i];
1779 if (flag_chars[i] == fki->length_code_char)
1780 continue;
1781 if (strchr (fci->flag_chars, flag_chars[i]) == 0)
1783 warning (OPT_Wformat, "%s used with %<%%%c%> %s format",
1784 _(s->name), format_char, fki->name);
1785 d++;
1786 continue;
1788 if (pedantic)
1790 const format_flag_spec *t;
1791 if (ADJ_STD (s->std) > C_STD_VER)
1792 warning (OPT_Wformat, "%s does not support %s",
1793 C_STD_NAME (s->std), _(s->long_name));
1794 t = get_flag_spec (flag_specs, flag_chars[i], fci->flags2);
1795 if (t != NULL && ADJ_STD (t->std) > ADJ_STD (s->std))
1797 const char *long_name = (t->long_name != NULL
1798 ? t->long_name
1799 : s->long_name);
1800 if (ADJ_STD (t->std) > C_STD_VER)
1801 warning (OPT_Wformat,
1802 "%s does not support %s with the %<%%%c%> %s format",
1803 C_STD_NAME (t->std), _(long_name),
1804 format_char, fki->name);
1808 flag_chars[i - d] = 0;
1811 if ((fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
1812 && strchr (flag_chars, 'a') != 0)
1813 aflag = 1;
1815 if (fki->suppression_char
1816 && strchr (flag_chars, fki->suppression_char) != 0)
1817 suppressed = 1;
1819 /* Validate the pairs of flags used. */
1820 for (i = 0; bad_flag_pairs[i].flag_char1 != 0; i++)
1822 const format_flag_spec *s, *t;
1823 if (strchr (flag_chars, bad_flag_pairs[i].flag_char1) == 0)
1824 continue;
1825 if (strchr (flag_chars, bad_flag_pairs[i].flag_char2) == 0)
1826 continue;
1827 if (bad_flag_pairs[i].predicate != 0
1828 && strchr (fci->flags2, bad_flag_pairs[i].predicate) == 0)
1829 continue;
1830 s = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char1, NULL);
1831 t = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char2, NULL);
1832 if (bad_flag_pairs[i].ignored)
1834 if (bad_flag_pairs[i].predicate != 0)
1835 warning (OPT_Wformat,
1836 "%s ignored with %s and %<%%%c%> %s format",
1837 _(s->name), _(t->name), format_char,
1838 fki->name);
1839 else
1840 warning (OPT_Wformat, "%s ignored with %s in %s format",
1841 _(s->name), _(t->name), fki->name);
1843 else
1845 if (bad_flag_pairs[i].predicate != 0)
1846 warning (OPT_Wformat,
1847 "use of %s and %s together with %<%%%c%> %s format",
1848 _(s->name), _(t->name), format_char,
1849 fki->name);
1850 else
1851 warning (OPT_Wformat, "use of %s and %s together in %s format",
1852 _(s->name), _(t->name), fki->name);
1856 /* Give Y2K warnings. */
1857 if (warn_format_y2k)
1859 int y2k_level = 0;
1860 if (strchr (fci->flags2, '4') != 0)
1861 if (strchr (flag_chars, 'E') != 0)
1862 y2k_level = 3;
1863 else
1864 y2k_level = 2;
1865 else if (strchr (fci->flags2, '3') != 0)
1866 y2k_level = 3;
1867 else if (strchr (fci->flags2, '2') != 0)
1868 y2k_level = 2;
1869 if (y2k_level == 3)
1870 warning (OPT_Wformat_y2k, "%<%%%c%> yields only last 2 digits of "
1871 "year in some locales", format_char);
1872 else if (y2k_level == 2)
1873 warning (OPT_Wformat_y2k, "%<%%%c%> yields only last 2 digits of "
1874 "year", format_char);
1877 if (strchr (fci->flags2, '[') != 0)
1879 /* Skip over scan set, in case it happens to have '%' in it. */
1880 if (*format_chars == '^')
1881 ++format_chars;
1882 /* Find closing bracket; if one is hit immediately, then
1883 it's part of the scan set rather than a terminator. */
1884 if (*format_chars == ']')
1885 ++format_chars;
1886 while (*format_chars && *format_chars != ']')
1887 ++format_chars;
1888 if (*format_chars != ']')
1889 /* The end of the format string was reached. */
1890 warning (OPT_Wformat, "no closing %<]%> for %<%%[%> format");
1893 wanted_type = 0;
1894 wanted_type_name = 0;
1895 if (fki->flags & (int) FMT_FLAG_ARG_CONVERT)
1897 wanted_type = (fci->types[length_chars_val].type
1898 ? *fci->types[length_chars_val].type : 0);
1899 wanted_type_name = fci->types[length_chars_val].name;
1900 wanted_type_std = fci->types[length_chars_val].std;
1901 if (wanted_type == 0)
1903 warning (OPT_Wformat,
1904 "use of %qs length modifier with %qc type character",
1905 length_chars, format_char);
1906 /* Heuristic: skip one argument when an invalid length/type
1907 combination is encountered. */
1908 arg_num++;
1909 if (params == 0)
1911 warning (OPT_Wformat, "too few arguments for format");
1912 return;
1914 params = TREE_CHAIN (params);
1915 continue;
1917 else if (pedantic
1918 /* Warn if non-standard, provided it is more non-standard
1919 than the length and type characters that may already
1920 have been warned for. */
1921 && ADJ_STD (wanted_type_std) > ADJ_STD (length_chars_std)
1922 && ADJ_STD (wanted_type_std) > ADJ_STD (fci->std))
1924 if (ADJ_STD (wanted_type_std) > C_STD_VER)
1925 warning (OPT_Wformat,
1926 "%s does not support the %<%%%s%c%> %s format",
1927 C_STD_NAME (wanted_type_std), length_chars,
1928 format_char, fki->name);
1932 main_wanted_type.next = NULL;
1934 /* Finally. . .check type of argument against desired type! */
1935 if (info->first_arg_num == 0)
1936 continue;
1937 if ((fci->pointer_count == 0 && wanted_type == void_type_node)
1938 || suppressed)
1940 if (main_arg_num != 0)
1942 if (suppressed)
1943 warning (OPT_Wformat, "operand number specified with "
1944 "suppressed assignment");
1945 else
1946 warning (OPT_Wformat, "operand number specified for format "
1947 "taking no argument");
1950 else
1952 format_wanted_type *wanted_type_ptr;
1954 if (main_arg_num != 0)
1956 arg_num = main_arg_num;
1957 params = main_arg_params;
1959 else
1961 ++arg_num;
1962 if (has_operand_number > 0)
1964 warning (OPT_Wformat, "missing $ operand number in format");
1965 return;
1967 else
1968 has_operand_number = 0;
1971 wanted_type_ptr = &main_wanted_type;
1972 while (fci)
1974 if (params == 0)
1976 warning (OPT_Wformat, "too few arguments for format");
1977 return;
1980 cur_param = TREE_VALUE (params);
1981 params = TREE_CHAIN (params);
1983 wanted_type_ptr->wanted_type = wanted_type;
1984 wanted_type_ptr->wanted_type_name = wanted_type_name;
1985 wanted_type_ptr->pointer_count = fci->pointer_count + aflag;
1986 wanted_type_ptr->char_lenient_flag = 0;
1987 if (strchr (fci->flags2, 'c') != 0)
1988 wanted_type_ptr->char_lenient_flag = 1;
1989 wanted_type_ptr->writing_in_flag = 0;
1990 wanted_type_ptr->reading_from_flag = 0;
1991 if (aflag)
1992 wanted_type_ptr->writing_in_flag = 1;
1993 else
1995 if (strchr (fci->flags2, 'W') != 0)
1996 wanted_type_ptr->writing_in_flag = 1;
1997 if (strchr (fci->flags2, 'R') != 0)
1998 wanted_type_ptr->reading_from_flag = 1;
2000 wanted_type_ptr->name = NULL;
2001 wanted_type_ptr->param = cur_param;
2002 wanted_type_ptr->arg_num = arg_num;
2003 wanted_type_ptr->next = NULL;
2004 if (last_wanted_type != 0)
2005 last_wanted_type->next = wanted_type_ptr;
2006 if (first_wanted_type == 0)
2007 first_wanted_type = wanted_type_ptr;
2008 last_wanted_type = wanted_type_ptr;
2010 fci = fci->chain;
2011 if (fci)
2013 wanted_type_ptr = ggc_alloc (sizeof (main_wanted_type));
2014 arg_num++;
2015 wanted_type = *fci->types[length_chars_val].type;
2016 wanted_type_name = fci->types[length_chars_val].name;
2021 if (first_wanted_type != 0)
2022 check_format_types (first_wanted_type, format_start,
2023 format_chars - format_start);
2025 if (main_wanted_type.next != NULL)
2027 format_wanted_type *wanted_type_ptr = main_wanted_type.next;
2028 while (wanted_type_ptr)
2030 format_wanted_type *next = wanted_type_ptr->next;
2031 ggc_free (wanted_type_ptr);
2032 wanted_type_ptr = next;
2039 /* Check the argument types from a single format conversion (possibly
2040 including width and precision arguments). */
2041 static void
2042 check_format_types (format_wanted_type *types, const char *format_start,
2043 int format_length)
2045 for (; types != 0; types = types->next)
2047 tree cur_param;
2048 tree cur_type;
2049 tree orig_cur_type;
2050 tree wanted_type;
2051 int arg_num;
2052 int i;
2053 int char_type_flag;
2054 cur_param = types->param;
2055 cur_type = TREE_TYPE (cur_param);
2056 if (cur_type == error_mark_node)
2057 continue;
2058 orig_cur_type = cur_type;
2059 char_type_flag = 0;
2060 wanted_type = types->wanted_type;
2061 arg_num = types->arg_num;
2063 /* The following should not occur here. */
2064 gcc_assert (wanted_type);
2065 gcc_assert (wanted_type != void_type_node || types->pointer_count);
2067 if (types->pointer_count == 0)
2068 wanted_type = lang_hooks.types.type_promotes_to (wanted_type);
2070 wanted_type = TYPE_MAIN_VARIANT (wanted_type);
2072 STRIP_NOPS (cur_param);
2074 /* Check the types of any additional pointer arguments
2075 that precede the "real" argument. */
2076 for (i = 0; i < types->pointer_count; ++i)
2078 if (TREE_CODE (cur_type) == POINTER_TYPE)
2080 cur_type = TREE_TYPE (cur_type);
2081 if (cur_type == error_mark_node)
2082 break;
2084 /* Check for writing through a NULL pointer. */
2085 if (types->writing_in_flag
2086 && i == 0
2087 && cur_param != 0
2088 && integer_zerop (cur_param))
2089 warning (OPT_Wformat, "writing through null pointer "
2090 "(argument %d)", arg_num);
2092 /* Check for reading through a NULL pointer. */
2093 if (types->reading_from_flag
2094 && i == 0
2095 && cur_param != 0
2096 && integer_zerop (cur_param))
2097 warning (OPT_Wformat, "reading through null pointer "
2098 "(argument %d)", arg_num);
2100 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
2101 cur_param = TREE_OPERAND (cur_param, 0);
2102 else
2103 cur_param = 0;
2105 /* See if this is an attempt to write into a const type with
2106 scanf or with printf "%n". Note: the writing in happens
2107 at the first indirection only, if for example
2108 void * const * is passed to scanf %p; passing
2109 const void ** is simply passing an incompatible type. */
2110 if (types->writing_in_flag
2111 && i == 0
2112 && (TYPE_READONLY (cur_type)
2113 || (cur_param != 0
2114 && (CONSTANT_CLASS_P (cur_param)
2115 || (DECL_P (cur_param)
2116 && TREE_READONLY (cur_param))))))
2117 warning (OPT_Wformat, "writing into constant object "
2118 "(argument %d)", arg_num);
2120 /* If there are extra type qualifiers beyond the first
2121 indirection, then this makes the types technically
2122 incompatible. */
2123 if (i > 0
2124 && pedantic
2125 && (TYPE_READONLY (cur_type)
2126 || TYPE_VOLATILE (cur_type)
2127 || TYPE_RESTRICT (cur_type)))
2128 warning (OPT_Wformat, "extra type qualifiers in format "
2129 "argument (argument %d)",
2130 arg_num);
2133 else
2135 format_type_warning (types->name, format_start, format_length,
2136 wanted_type, types->pointer_count,
2137 types->wanted_type_name, orig_cur_type,
2138 arg_num);
2139 break;
2143 if (i < types->pointer_count)
2144 continue;
2146 cur_type = TYPE_MAIN_VARIANT (cur_type);
2148 /* Check whether the argument type is a character type. This leniency
2149 only applies to certain formats, flagged with 'c'.
2151 if (types->char_lenient_flag)
2152 char_type_flag = (cur_type == char_type_node
2153 || cur_type == signed_char_type_node
2154 || cur_type == unsigned_char_type_node);
2156 /* Check the type of the "real" argument, if there's a type we want. */
2157 if (wanted_type == cur_type)
2158 continue;
2159 /* If we want 'void *', allow any pointer type.
2160 (Anything else would already have got a warning.)
2161 With -pedantic, only allow pointers to void and to character
2162 types. */
2163 if (wanted_type == void_type_node
2164 && (!pedantic || (i == 1 && char_type_flag)))
2165 continue;
2166 /* Don't warn about differences merely in signedness, unless
2167 -pedantic. With -pedantic, warn if the type is a pointer
2168 target and not a character type, and for character types at
2169 a second level of indirection. */
2170 if (TREE_CODE (wanted_type) == INTEGER_TYPE
2171 && TREE_CODE (cur_type) == INTEGER_TYPE
2172 && (!pedantic || i == 0 || (i == 1 && char_type_flag))
2173 && (TYPE_UNSIGNED (wanted_type)
2174 ? wanted_type == c_common_unsigned_type (cur_type)
2175 : wanted_type == c_common_signed_type (cur_type)))
2176 continue;
2177 /* Likewise, "signed char", "unsigned char" and "char" are
2178 equivalent but the above test won't consider them equivalent. */
2179 if (wanted_type == char_type_node
2180 && (!pedantic || i < 2)
2181 && char_type_flag)
2182 continue;
2183 /* Now we have a type mismatch. */
2184 format_type_warning (types->name, format_start, format_length,
2185 wanted_type, types->pointer_count,
2186 types->wanted_type_name, orig_cur_type, arg_num);
2191 /* Give a warning about a format argument of different type from that
2192 expected. DESCR is a description such as "field precision", or
2193 NULL for an ordinary format. For an ordinary format, FORMAT_START
2194 points to where the format starts in the format string and
2195 FORMAT_LENGTH is its length. WANTED_TYPE is the type the argument
2196 should have after POINTER_COUNT pointer dereferences.
2197 WANTED_NAME_NAME is a possibly more friendly name of WANTED_TYPE,
2198 or NULL if the ordinary name of the type should be used. ARG_TYPE
2199 is the type of the actual argument. ARG_NUM is the number of that
2200 argument. */
2201 static void
2202 format_type_warning (const char *descr, const char *format_start,
2203 int format_length, tree wanted_type, int pointer_count,
2204 const char *wanted_type_name, tree arg_type, int arg_num)
2206 char *p;
2207 /* If ARG_TYPE is a typedef with a misleading name (for example,
2208 size_t but not the standard size_t expected by printf %zu), avoid
2209 printing the typedef name. */
2210 if (wanted_type_name
2211 && TYPE_NAME (arg_type)
2212 && TREE_CODE (TYPE_NAME (arg_type)) == TYPE_DECL
2213 && DECL_NAME (TYPE_NAME (arg_type))
2214 && !strcmp (wanted_type_name,
2215 lang_hooks.decl_printable_name (TYPE_NAME (arg_type), 2)))
2216 arg_type = TYPE_MAIN_VARIANT (arg_type);
2217 /* The format type and name exclude any '*' for pointers, so those
2218 must be formatted manually. For all the types we currently have,
2219 this is adequate, but formats taking pointers to functions or
2220 arrays would require the full type to be built up in order to
2221 print it with %T. */
2222 p = alloca (pointer_count + 2);
2223 if (pointer_count == 0)
2224 p[0] = 0;
2225 else if (c_dialect_cxx ())
2227 memset (p, '*', pointer_count);
2228 p[pointer_count] = 0;
2230 else
2232 p[0] = ' ';
2233 memset (p + 1, '*', pointer_count);
2234 p[pointer_count + 1] = 0;
2236 if (wanted_type_name)
2238 if (descr)
2239 warning (OPT_Wformat, "%s should have type %<%s%s%>, "
2240 "but argument %d has type %qT",
2241 descr, wanted_type_name, p, arg_num, arg_type);
2242 else
2243 warning (OPT_Wformat, "format %q.*s expects type %<%s%s%>, "
2244 "but argument %d has type %qT",
2245 format_length, format_start, wanted_type_name, p,
2246 arg_num, arg_type);
2248 else
2250 if (descr)
2251 warning (OPT_Wformat, "%s should have type %<%T%s%>, "
2252 "but argument %d has type %qT",
2253 descr, wanted_type, p, arg_num, arg_type);
2254 else
2255 warning (OPT_Wformat, "format %q.*s expects type %<%T%s%>, "
2256 "but argument %d has type %qT",
2257 format_length, format_start, wanted_type, p, arg_num, arg_type);
2262 /* Given a format_char_info array FCI, and a character C, this function
2263 returns the index into the conversion_specs where that specifier's
2264 data is located. The character must exist. */
2265 static unsigned int
2266 find_char_info_specifier_index (const format_char_info *fci, int c)
2268 unsigned i;
2270 for (i = 0; fci->format_chars; i++, fci++)
2271 if (strchr (fci->format_chars, c))
2272 return i;
2274 /* We shouldn't be looking for a non-existent specifier. */
2275 gcc_unreachable ();
2278 /* Given a format_length_info array FLI, and a character C, this
2279 function returns the index into the conversion_specs where that
2280 modifier's data is located. The character must exist. */
2281 static unsigned int
2282 find_length_info_modifier_index (const format_length_info *fli, int c)
2284 unsigned i;
2286 for (i = 0; fli->name; i++, fli++)
2287 if (strchr (fli->name, c))
2288 return i;
2290 /* We shouldn't be looking for a non-existent modifier. */
2291 gcc_unreachable ();
2294 /* Determine the type of HOST_WIDE_INT in the code being compiled for
2295 use in GCC's __asm_fprintf__ custom format attribute. You must
2296 have set dynamic_format_types before calling this function. */
2297 static void
2298 init_dynamic_asm_fprintf_info (void)
2300 static tree hwi;
2302 if (!hwi)
2304 format_length_info *new_asm_fprintf_length_specs;
2305 unsigned int i;
2307 /* Find the underlying type for HOST_WIDE_INT. For the %w
2308 length modifier to work, one must have issued: "typedef
2309 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2310 prior to using that modifier. */
2311 hwi = maybe_get_identifier ("__gcc_host_wide_int__");
2312 if (!hwi)
2314 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2315 return;
2317 hwi = identifier_global_value (hwi);
2318 if (!hwi || TREE_CODE (hwi) != TYPE_DECL)
2320 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2321 return;
2323 hwi = DECL_ORIGINAL_TYPE (hwi);
2324 gcc_assert (hwi);
2325 if (hwi != long_integer_type_node && hwi != long_long_integer_type_node)
2327 error ("%<__gcc_host_wide_int__%> is not defined as %<long%>"
2328 " or %<long long%>");
2329 return;
2332 /* Create a new (writable) copy of asm_fprintf_length_specs. */
2333 new_asm_fprintf_length_specs = (format_length_info *)
2334 xmemdup (asm_fprintf_length_specs,
2335 sizeof (asm_fprintf_length_specs),
2336 sizeof (asm_fprintf_length_specs));
2338 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2339 i = find_length_info_modifier_index (new_asm_fprintf_length_specs, 'w');
2340 if (hwi == long_integer_type_node)
2341 new_asm_fprintf_length_specs[i].index = FMT_LEN_l;
2342 else if (hwi == long_long_integer_type_node)
2343 new_asm_fprintf_length_specs[i].index = FMT_LEN_ll;
2344 else
2345 gcc_unreachable ();
2347 /* Assign the new data for use. */
2348 dynamic_format_types[asm_fprintf_format_type].length_char_specs =
2349 new_asm_fprintf_length_specs;
2353 /* Determine the types of "tree" and "location_t" in the code being
2354 compiled for use in GCC's diagnostic custom format attributes. You
2355 must have set dynamic_format_types before calling this function. */
2356 static void
2357 init_dynamic_diag_info (void)
2359 static tree t, loc, hwi;
2361 if (!loc || !t || !hwi)
2363 static format_char_info *diag_fci, *cdiag_fci, *cxxdiag_fci;
2364 static format_length_info *diag_ls;
2365 unsigned int i;
2367 /* For the GCC-diagnostics custom format specifiers to work, one
2368 must have declared 'tree' and/or 'location_t' prior to using
2369 those attributes. If we haven't seen these declarations then
2370 you shouldn't use the specifiers requiring these types.
2371 However we don't force a hard ICE because we may see only one
2372 or the other type. */
2373 if ((loc = maybe_get_identifier ("location_t")))
2375 loc = identifier_global_value (loc);
2376 if (loc)
2378 if (TREE_CODE (loc) != TYPE_DECL)
2380 error ("%<location_t%> is not defined as a type");
2381 loc = 0;
2383 else
2384 loc = TREE_TYPE (loc);
2388 /* We need to grab the underlying 'union tree_node' so peek into
2389 an extra type level. */
2390 if ((t = maybe_get_identifier ("tree")))
2392 t = identifier_global_value (t);
2393 if (t)
2395 if (TREE_CODE (t) != TYPE_DECL)
2397 error ("%<tree%> is not defined as a type");
2398 t = 0;
2400 else if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
2402 error ("%<tree%> is not defined as a pointer type");
2403 t = 0;
2405 else
2406 t = TREE_TYPE (TREE_TYPE (t));
2410 /* Find the underlying type for HOST_WIDE_INT. For the %w
2411 length modifier to work, one must have issued: "typedef
2412 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2413 prior to using that modifier. */
2414 if ((hwi = maybe_get_identifier ("__gcc_host_wide_int__")))
2416 hwi = identifier_global_value (hwi);
2417 if (hwi)
2419 if (TREE_CODE (hwi) != TYPE_DECL)
2421 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2422 hwi = 0;
2424 else
2426 hwi = DECL_ORIGINAL_TYPE (hwi);
2427 gcc_assert (hwi);
2428 if (hwi != long_integer_type_node
2429 && hwi != long_long_integer_type_node)
2431 error ("%<__gcc_host_wide_int__%> is not defined"
2432 " as %<long%> or %<long long%>");
2433 hwi = 0;
2439 /* Assign the new data for use. */
2441 /* All the GCC diag formats use the same length specs. */
2442 if (!diag_ls)
2443 dynamic_format_types[gcc_diag_format_type].length_char_specs =
2444 dynamic_format_types[gcc_cdiag_format_type].length_char_specs =
2445 dynamic_format_types[gcc_cxxdiag_format_type].length_char_specs =
2446 diag_ls = (format_length_info *)
2447 xmemdup (gcc_diag_length_specs,
2448 sizeof (gcc_diag_length_specs),
2449 sizeof (gcc_diag_length_specs));
2450 if (hwi)
2452 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2453 i = find_length_info_modifier_index (diag_ls, 'w');
2454 if (hwi == long_integer_type_node)
2455 diag_ls[i].index = FMT_LEN_l;
2456 else if (hwi == long_long_integer_type_node)
2457 diag_ls[i].index = FMT_LEN_ll;
2458 else
2459 gcc_unreachable ();
2462 /* Handle the __gcc_diag__ format specifics. */
2463 if (!diag_fci)
2464 dynamic_format_types[gcc_diag_format_type].conversion_specs =
2465 diag_fci = (format_char_info *)
2466 xmemdup (gcc_diag_char_table,
2467 sizeof (gcc_diag_char_table),
2468 sizeof (gcc_diag_char_table));
2469 if (loc)
2471 i = find_char_info_specifier_index (diag_fci, 'H');
2472 diag_fci[i].types[0].type = &loc;
2473 diag_fci[i].pointer_count = 1;
2475 if (t)
2477 i = find_char_info_specifier_index (diag_fci, 'J');
2478 diag_fci[i].types[0].type = &t;
2479 diag_fci[i].pointer_count = 1;
2482 /* Handle the __gcc_cdiag__ format specifics. */
2483 if (!cdiag_fci)
2484 dynamic_format_types[gcc_cdiag_format_type].conversion_specs =
2485 cdiag_fci = (format_char_info *)
2486 xmemdup (gcc_cdiag_char_table,
2487 sizeof (gcc_cdiag_char_table),
2488 sizeof (gcc_cdiag_char_table));
2489 if (loc)
2491 i = find_char_info_specifier_index (cdiag_fci, 'H');
2492 cdiag_fci[i].types[0].type = &loc;
2493 cdiag_fci[i].pointer_count = 1;
2495 if (t)
2497 /* All specifiers taking a tree share the same struct. */
2498 i = find_char_info_specifier_index (cdiag_fci, 'D');
2499 cdiag_fci[i].types[0].type = &t;
2500 cdiag_fci[i].pointer_count = 1;
2501 i = find_char_info_specifier_index (cdiag_fci, 'J');
2502 cdiag_fci[i].types[0].type = &t;
2503 cdiag_fci[i].pointer_count = 1;
2506 /* Handle the __gcc_cxxdiag__ format specifics. */
2507 if (!cxxdiag_fci)
2508 dynamic_format_types[gcc_cxxdiag_format_type].conversion_specs =
2509 cxxdiag_fci = (format_char_info *)
2510 xmemdup (gcc_cxxdiag_char_table,
2511 sizeof (gcc_cxxdiag_char_table),
2512 sizeof (gcc_cxxdiag_char_table));
2513 if (loc)
2515 i = find_char_info_specifier_index (cxxdiag_fci, 'H');
2516 cxxdiag_fci[i].types[0].type = &loc;
2517 cxxdiag_fci[i].pointer_count = 1;
2519 if (t)
2521 /* All specifiers taking a tree share the same struct. */
2522 i = find_char_info_specifier_index (cxxdiag_fci, 'D');
2523 cxxdiag_fci[i].types[0].type = &t;
2524 cxxdiag_fci[i].pointer_count = 1;
2525 i = find_char_info_specifier_index (cxxdiag_fci, 'J');
2526 cxxdiag_fci[i].types[0].type = &t;
2527 cxxdiag_fci[i].pointer_count = 1;
2532 #ifdef TARGET_FORMAT_TYPES
2533 extern const format_kind_info TARGET_FORMAT_TYPES[];
2534 #endif
2536 /* Handle a "format" attribute; arguments as in
2537 struct attribute_spec.handler. */
2538 tree
2539 handle_format_attribute (tree *node, tree ARG_UNUSED (name), tree args,
2540 int flags, bool *no_add_attrs)
2542 tree type = *node;
2543 function_format_info info;
2544 tree argument;
2546 #ifdef TARGET_FORMAT_TYPES
2547 /* If the target provides additional format types, we need to
2548 add them to FORMAT_TYPES at first use. */
2549 if (TARGET_FORMAT_TYPES != NULL && !dynamic_format_types)
2551 dynamic_format_types = xmalloc ((n_format_types + TARGET_N_FORMAT_TYPES)
2552 * sizeof (dynamic_format_types[0]));
2553 memcpy (dynamic_format_types, format_types_orig,
2554 sizeof (format_types_orig));
2555 memcpy (&dynamic_format_types[n_format_types], TARGET_FORMAT_TYPES,
2556 TARGET_N_FORMAT_TYPES * sizeof (dynamic_format_types[0]));
2558 format_types = dynamic_format_types;
2559 n_format_types += TARGET_N_FORMAT_TYPES;
2561 #endif
2563 if (!decode_format_attr (args, &info, 0))
2565 *no_add_attrs = true;
2566 return NULL_TREE;
2569 argument = TYPE_ARG_TYPES (type);
2570 if (argument)
2572 if (!check_format_string (argument, info.format_num, flags,
2573 no_add_attrs))
2574 return NULL_TREE;
2576 if (info.first_arg_num != 0)
2578 unsigned HOST_WIDE_INT arg_num = 1;
2580 /* Verify that first_arg_num points to the last arg,
2581 the ... */
2582 while (argument)
2583 arg_num++, argument = TREE_CHAIN (argument);
2585 if (arg_num != info.first_arg_num)
2587 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
2588 error ("args to be formatted is not %<...%>");
2589 *no_add_attrs = true;
2590 return NULL_TREE;
2595 if (info.format_type == strftime_format_type && info.first_arg_num != 0)
2597 error ("strftime formats cannot format arguments");
2598 *no_add_attrs = true;
2599 return NULL_TREE;
2602 /* If this is a custom GCC-internal format type, we have to
2603 initialize certain bits a runtime. */
2604 if (info.format_type == asm_fprintf_format_type
2605 || info.format_type == gcc_diag_format_type
2606 || info.format_type == gcc_cdiag_format_type
2607 || info.format_type == gcc_cxxdiag_format_type)
2609 /* Our first time through, we have to make sure that our
2610 format_type data is allocated dynamically and is modifiable. */
2611 if (!dynamic_format_types)
2612 format_types = dynamic_format_types = (format_kind_info *)
2613 xmemdup (format_types_orig, sizeof (format_types_orig),
2614 sizeof (format_types_orig));
2616 /* If this is format __asm_fprintf__, we have to initialize
2617 GCC's notion of HOST_WIDE_INT for checking %wd. */
2618 if (info.format_type == asm_fprintf_format_type)
2619 init_dynamic_asm_fprintf_info ();
2620 /* If this is one of the diagnostic attributes, then we have to
2621 initialize 'location_t' and 'tree' at runtime. */
2622 else if (info.format_type == gcc_diag_format_type
2623 || info.format_type == gcc_cdiag_format_type
2624 || info.format_type == gcc_cxxdiag_format_type)
2625 init_dynamic_diag_info ();
2626 else
2627 gcc_unreachable ();
2630 return NULL_TREE;