2004-10-25 Benjamin Kosnik <bkoz@redhat.com>
[official-gcc.git] / gcc / c-format.c
blobe5009a32fea2584b4675e90f3ab25e9bebab6f9c
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 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, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, 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 arg not a string type");
143 *no_add_attrs = true;
144 return false;
147 return true;
150 /* Strip any conversions from the expression, verify it is a constant,
151 and store its value. If validated_p is true, abort on 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 while (TREE_CODE (expr) == NOP_EXPR
157 || TREE_CODE (expr) == CONVERT_EXPR
158 || TREE_CODE (expr) == NON_LVALUE_EXPR)
159 expr = TREE_OPERAND (expr, 0);
161 if (TREE_CODE (expr) != INTEGER_CST || TREE_INT_CST_HIGH (expr) != 0)
163 gcc_assert (!validated_p);
164 return false;
167 *value = TREE_INT_CST_LOW (expr);
169 return true;
172 /* Decode the arguments to a "format" attribute into a function_format_info
173 structure. It is already known that the list is of the right length.
174 If VALIDATED_P is true, then these attributes have already been validated
175 and this function will abort if they are erroneous; if false, it
176 will give an error message. Returns true if the attributes are
177 successfully decoded, false otherwise. */
179 static bool
180 decode_format_attr (tree args, function_format_info *info, int validated_p)
182 tree format_type_id = TREE_VALUE (args);
183 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
184 tree first_arg_num_expr
185 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
187 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
189 gcc_assert (!validated_p);
190 error ("unrecognized format specifier");
191 return false;
193 else
195 const char *p = IDENTIFIER_POINTER (format_type_id);
197 info->format_type = decode_format_type (p);
199 if (info->format_type == format_type_error)
201 gcc_assert (!validated_p);
202 warning ("%qs is an unrecognized format function type", p);
203 return false;
207 if (!get_constant (format_num_expr, &info->format_num, validated_p))
209 error ("format string has invalid operand number");
210 return false;
213 if (!get_constant (first_arg_num_expr, &info->first_arg_num, validated_p))
215 error ("'...' has invalid operand number");
216 return false;
219 if (info->first_arg_num != 0 && info->first_arg_num <= info->format_num)
221 gcc_assert (!validated_p);
222 error ("format string arg follows the args to be formatted");
223 return false;
226 return true;
229 /* Check a call to a format function against a parameter list. */
231 /* The C standard version C++ is treated as equivalent to
232 or inheriting from, for the purpose of format features supported. */
233 #define CPLUSPLUS_STD_VER STD_C94
234 /* The C standard version we are checking formats against when pedantic. */
235 #define C_STD_VER ((int) (c_dialect_cxx () \
236 ? CPLUSPLUS_STD_VER \
237 : (flag_isoc99 \
238 ? STD_C99 \
239 : (flag_isoc94 ? STD_C94 : STD_C89))))
240 /* The name to give to the standard version we are warning about when
241 pedantic. FEATURE_VER is the version in which the feature warned out
242 appeared, which is higher than C_STD_VER. */
243 #define C_STD_NAME(FEATURE_VER) (c_dialect_cxx () \
244 ? "ISO C++" \
245 : ((FEATURE_VER) == STD_EXT \
246 ? "ISO C" \
247 : "ISO C90"))
248 /* Adjust a C standard version, which may be STD_C9L, to account for
249 -Wno-long-long. Returns other standard versions unchanged. */
250 #define ADJ_STD(VER) ((int) ((VER) == STD_C9L \
251 ? (warn_long_long ? STD_C99 : STD_C89) \
252 : (VER)))
254 /* Structure describing details of a type expected in format checking,
255 and the type to check against it. */
256 typedef struct format_wanted_type
258 /* The type wanted. */
259 tree wanted_type;
260 /* The name of this type to use in diagnostics. */
261 const char *wanted_type_name;
262 /* The level of indirection through pointers at which this type occurs. */
263 int pointer_count;
264 /* Whether, when pointer_count is 1, to allow any character type when
265 pedantic, rather than just the character or void type specified. */
266 int char_lenient_flag;
267 /* Whether the argument, dereferenced once, is written into and so the
268 argument must not be a pointer to a const-qualified type. */
269 int writing_in_flag;
270 /* Whether the argument, dereferenced once, is read from and so
271 must not be a NULL pointer. */
272 int reading_from_flag;
273 /* If warnings should be of the form "field precision should have
274 type 'int'", the name to use (in this case "field precision"),
275 otherwise NULL, for "format expects type 'long'" type
276 messages. */
277 const char *name;
278 /* The actual parameter to check against the wanted type. */
279 tree param;
280 /* The argument number of that parameter. */
281 int arg_num;
282 /* The next type to check for this format conversion, or NULL if none. */
283 struct format_wanted_type *next;
284 } format_wanted_type;
287 static const format_length_info printf_length_specs[] =
289 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
290 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L },
291 { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
292 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
293 { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
294 { "Z", FMT_LEN_z, STD_EXT, NULL, 0, 0 },
295 { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
296 { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
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_cdiag_length_specs gcc_diag_length_specs
318 #define gcc_cxxdiag_length_specs gcc_diag_length_specs
320 /* This differs from printf_length_specs only in that "Z" is not accepted. */
321 static const format_length_info scanf_length_specs[] =
323 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
324 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L },
325 { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
326 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
327 { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
328 { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
329 { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
330 { NULL, 0, 0, NULL, 0, 0 }
334 /* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings
335 make no sense for a format type not part of any C standard version. */
336 static const format_length_info strfmon_length_specs[] =
338 /* A GNU extension. */
339 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
340 { NULL, 0, 0, NULL, 0, 0 }
343 static const format_flag_spec printf_flag_specs[] =
345 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
346 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
347 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
348 { '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
349 { '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
350 { '\'', 0, 0, N_("''' flag"), N_("the ''' printf flag"), STD_EXT },
351 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' printf flag"), STD_EXT },
352 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
353 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
354 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
355 { 0, 0, 0, NULL, NULL, 0 }
359 static const format_flag_pair printf_flag_pairs[] =
361 { ' ', '+', 1, 0 },
362 { '0', '-', 1, 0 },
363 { '0', 'p', 1, 'i' },
364 { 0, 0, 0, 0 }
367 static const format_flag_spec asm_fprintf_flag_specs[] =
369 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
370 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
371 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
372 { '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
373 { '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
374 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
375 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
376 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
377 { 0, 0, 0, NULL, NULL, 0 }
380 static const format_flag_pair asm_fprintf_flag_pairs[] =
382 { ' ', '+', 1, 0 },
383 { '0', '-', 1, 0 },
384 { '0', 'p', 1, 'i' },
385 { 0, 0, 0, 0 }
388 static const format_flag_pair gcc_diag_flag_pairs[] =
390 { 0, 0, 0, 0 }
393 #define gcc_cdiag_flag_pairs gcc_diag_flag_pairs
394 #define gcc_cxxdiag_flag_pairs gcc_diag_flag_pairs
396 static const format_flag_spec gcc_diag_flag_specs[] =
398 { 'q', 0, 0, N_("'q' flag"), N_("the 'q' diagnostic flag"), STD_C89 },
399 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
400 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
401 { 0, 0, 0, NULL, NULL, 0 }
404 #define gcc_cdiag_flag_specs gcc_diag_flag_specs
406 static const format_flag_spec gcc_cxxdiag_flag_specs[] =
408 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
409 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
410 { 'q', 0, 0, N_("'q' flag"), N_("the 'q' diagnostic flag"), STD_C89 },
411 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
412 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
413 { 0, 0, 0, NULL, NULL, 0 }
416 static const format_flag_spec scanf_flag_specs[] =
418 { '*', 0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89 },
419 { 'a', 0, 0, N_("'a' flag"), N_("the 'a' scanf flag"), STD_EXT },
420 { 'w', 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89 },
421 { 'L', 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89 },
422 { '\'', 0, 0, N_("''' flag"), N_("the ''' scanf flag"), STD_EXT },
423 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' scanf flag"), STD_EXT },
424 { 0, 0, 0, NULL, NULL, 0 }
428 static const format_flag_pair scanf_flag_pairs[] =
430 { '*', 'L', 0, 0 },
431 { 0, 0, 0, 0 }
435 static const format_flag_spec strftime_flag_specs[] =
437 { '_', 0, 0, N_("'_' flag"), N_("the '_' strftime flag"), STD_EXT },
438 { '-', 0, 0, N_("'-' flag"), N_("the '-' strftime flag"), STD_EXT },
439 { '0', 0, 0, N_("'0' flag"), N_("the '0' strftime flag"), STD_EXT },
440 { '^', 0, 0, N_("'^' flag"), N_("the '^' strftime flag"), STD_EXT },
441 { '#', 0, 0, N_("'#' flag"), N_("the '#' strftime flag"), STD_EXT },
442 { 'w', 0, 0, N_("field width"), N_("field width in strftime format"), STD_EXT },
443 { 'E', 0, 0, N_("'E' modifier"), N_("the 'E' strftime modifier"), STD_C99 },
444 { 'O', 0, 0, N_("'O' modifier"), N_("the 'O' strftime modifier"), STD_C99 },
445 { 'O', 'o', 0, NULL, N_("the 'O' modifier"), STD_EXT },
446 { 0, 0, 0, NULL, NULL, 0 }
450 static const format_flag_pair strftime_flag_pairs[] =
452 { 'E', 'O', 0, 0 },
453 { '_', '-', 0, 0 },
454 { '_', '0', 0, 0 },
455 { '-', '0', 0, 0 },
456 { '^', '#', 0, 0 },
457 { 0, 0, 0, 0 }
461 static const format_flag_spec strfmon_flag_specs[] =
463 { '=', 0, 1, N_("fill character"), N_("fill character in strfmon format"), 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 { '(', 0, 0, N_("'(' flag"), N_("the '(' strfmon flag"), STD_C89 },
467 { '!', 0, 0, N_("'!' flag"), N_("the '!' strfmon flag"), STD_C89 },
468 { '-', 0, 0, N_("'-' flag"), N_("the '-' strfmon flag"), STD_C89 },
469 { 'w', 0, 0, N_("field width"), N_("field width in strfmon format"), STD_C89 },
470 { '#', 0, 0, N_("left precision"), N_("left precision in strfmon format"), STD_C89 },
471 { 'p', 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89 },
472 { 'L', 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89 },
473 { 0, 0, 0, NULL, NULL, 0 }
476 static const format_flag_pair strfmon_flag_pairs[] =
478 { '+', '(', 0, 0 },
479 { 0, 0, 0, 0 }
483 static const format_char_info print_char_table[] =
485 /* C89 conversion specifiers. */
486 { "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 },
487 { "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 },
488 { "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 },
489 { "fgG", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#'I", "", NULL },
490 { "eE", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#I", "", NULL },
491 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, T94_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
492 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
493 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "c", NULL },
494 { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, BADLEN, T99_SST, T99_PD, T99_IM }, "", "W", NULL },
495 /* C99 conversion specifiers. */
496 { "F", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#'I", "", NULL },
497 { "aA", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "", NULL },
498 /* X/Open conversion specifiers. */
499 { "C", 0, STD_EXT, { TEX_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
500 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "R", NULL },
501 /* GNU conversion specifiers. */
502 { "m", 0, STD_EXT, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "", NULL },
503 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
506 static const format_char_info asm_fprintf_char_table[] =
508 /* C89 conversion specifiers. */
509 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +", "i", NULL },
510 { "oxX", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0#", "i", NULL },
511 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0", "i", NULL },
512 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
513 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
515 /* asm_fprintf conversion specifiers. */
516 { "O", 0, STD_C89, NOARGUMENTS, "", "", NULL },
517 { "R", 0, STD_C89, NOARGUMENTS, "", "", NULL },
518 { "I", 0, STD_C89, NOARGUMENTS, "", "", NULL },
519 { "L", 0, STD_C89, NOARGUMENTS, "", "", NULL },
520 { "U", 0, STD_C89, NOARGUMENTS, "", "", NULL },
521 { "r", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
522 { "@", 0, STD_C89, NOARGUMENTS, "", "", NULL },
523 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
526 static const format_char_info gcc_diag_char_table[] =
528 /* C89 conversion specifiers. */
529 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
530 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
531 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
532 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
533 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
534 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
536 /* Custom conversion specifiers. */
538 /* %H will require "location_t" at runtime. */
539 { "H", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
541 /* These will require a "tree" at runtime. */
542 { "J", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
544 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
545 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
546 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
549 static const format_char_info gcc_cdiag_char_table[] =
551 /* C89 conversion specifiers. */
552 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
553 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
554 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
555 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
556 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
557 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
559 /* Custom conversion specifiers. */
561 /* %H will require "location_t" at runtime. */
562 { "H", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
564 /* These will require a "tree" at runtime. */
565 { "DEFJT", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
567 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
568 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
569 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
572 static const format_char_info gcc_cxxdiag_char_table[] =
574 /* C89 conversion specifiers. */
575 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
576 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
577 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
578 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
579 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
580 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
582 /* Custom conversion specifiers. */
584 /* %H will require "location_t" at runtime. */
585 { "H", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
587 /* These will require a "tree" at runtime. */
588 { "ADEFJTV",0,STD_C89,{ T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+#", "", NULL },
590 /* These accept either an 'int' or an 'enum tree_code' (which is handled as an 'int'.) */
591 { "CLOPQ",0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
593 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
594 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
595 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
598 static const format_char_info scan_char_table[] =
600 /* C89 conversion specifiers. */
601 { "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 },
602 { "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 },
603 { "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 },
604 { "efgEG", 1, STD_C89, { T89_F, BADLEN, BADLEN, T89_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "*w'", "W", NULL },
605 { "c", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "cW", NULL },
606 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "cW", NULL },
607 { "[", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "cW[", NULL },
608 { "p", 2, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
609 { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, BADLEN, T99_SST, T99_PD, T99_IM }, "", "W", NULL },
610 /* C99 conversion specifiers. */
611 { "FaA", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "*w'", "W", NULL },
612 /* X/Open conversion specifiers. */
613 { "C", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
614 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "W", NULL },
615 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
618 static const format_char_info time_char_table[] =
620 /* C89 conversion specifiers. */
621 { "ABZab", 0, STD_C89, NOLENGTHS, "^#", "", NULL },
622 { "cx", 0, STD_C89, NOLENGTHS, "E", "3", NULL },
623 { "HIMSUWdmw", 0, STD_C89, NOLENGTHS, "-_0Ow", "", NULL },
624 { "j", 0, STD_C89, NOLENGTHS, "-_0Ow", "o", NULL },
625 { "p", 0, STD_C89, NOLENGTHS, "#", "", NULL },
626 { "X", 0, STD_C89, NOLENGTHS, "E", "", NULL },
627 { "y", 0, STD_C89, NOLENGTHS, "EO-_0w", "4", NULL },
628 { "Y", 0, STD_C89, NOLENGTHS, "-_0EOw", "o", NULL },
629 { "%", 0, STD_C89, NOLENGTHS, "", "", NULL },
630 /* C99 conversion specifiers. */
631 { "C", 0, STD_C99, NOLENGTHS, "-_0EOw", "o", NULL },
632 { "D", 0, STD_C99, NOLENGTHS, "", "2", NULL },
633 { "eVu", 0, STD_C99, NOLENGTHS, "-_0Ow", "", NULL },
634 { "FRTnrt", 0, STD_C99, NOLENGTHS, "", "", NULL },
635 { "g", 0, STD_C99, NOLENGTHS, "O-_0w", "2o", NULL },
636 { "G", 0, STD_C99, NOLENGTHS, "-_0Ow", "o", NULL },
637 { "h", 0, STD_C99, NOLENGTHS, "^#", "", NULL },
638 { "z", 0, STD_C99, NOLENGTHS, "O", "o", NULL },
639 /* GNU conversion specifiers. */
640 { "kls", 0, STD_EXT, NOLENGTHS, "-_0Ow", "", NULL },
641 { "P", 0, STD_EXT, NOLENGTHS, "", "", NULL },
642 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
645 static const format_char_info monetary_char_table[] =
647 { "in", 0, STD_C89, { T89_D, BADLEN, BADLEN, BADLEN, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "=^+(!-w#p", "", NULL },
648 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
651 /* This must be in the same order as enum format_type. */
652 static const format_kind_info format_types_orig[] =
654 { "printf", printf_length_specs, print_char_table, " +#0-'I", NULL,
655 printf_flag_specs, printf_flag_pairs,
656 FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_EMPTY_PREC_OK,
657 'w', 0, 'p', 0, 'L',
658 &integer_type_node, &integer_type_node
660 { "asm_fprintf", asm_fprintf_length_specs, asm_fprintf_char_table, " +#0-", NULL,
661 asm_fprintf_flag_specs, asm_fprintf_flag_pairs,
662 FMT_FLAG_ARG_CONVERT|FMT_FLAG_EMPTY_PREC_OK,
663 'w', 0, 'p', 0, 'L',
664 NULL, NULL
666 { "gcc_diag", gcc_diag_length_specs, gcc_diag_char_table, "q", NULL,
667 gcc_diag_flag_specs, gcc_diag_flag_pairs,
668 FMT_FLAG_ARG_CONVERT,
669 0, 0, 'p', 0, 'L',
670 NULL, &integer_type_node
672 { "gcc_cdiag", gcc_cdiag_length_specs, gcc_cdiag_char_table, "q", NULL,
673 gcc_cdiag_flag_specs, gcc_cdiag_flag_pairs,
674 FMT_FLAG_ARG_CONVERT,
675 0, 0, 'p', 0, 'L',
676 NULL, &integer_type_node
678 { "gcc_cxxdiag", gcc_cxxdiag_length_specs, gcc_cxxdiag_char_table, "q+#", NULL,
679 gcc_cxxdiag_flag_specs, gcc_cxxdiag_flag_pairs,
680 FMT_FLAG_ARG_CONVERT,
681 0, 0, 'p', 0, 'L',
682 NULL, &integer_type_node
684 { "scanf", scanf_length_specs, scan_char_table, "*'I", NULL,
685 scanf_flag_specs, scanf_flag_pairs,
686 FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_ZERO_WIDTH_BAD|FMT_FLAG_DOLLAR_GAP_POINTER_OK,
687 'w', 0, 0, '*', 'L',
688 NULL, NULL
690 { "strftime", NULL, time_char_table, "_-0^#", "EO",
691 strftime_flag_specs, strftime_flag_pairs,
692 FMT_FLAG_FANCY_PERCENT_OK, 'w', 0, 0, 0, 0,
693 NULL, NULL
695 { "strfmon", strfmon_length_specs, monetary_char_table, "=^+(!-", NULL,
696 strfmon_flag_specs, strfmon_flag_pairs,
697 FMT_FLAG_ARG_CONVERT, 'w', '#', 'p', 0, 'L',
698 NULL, NULL
702 /* This layer of indirection allows GCC to reassign format_types with
703 new data if necessary, while still allowing the original data to be
704 const. */
705 static const format_kind_info *format_types = format_types_orig;
706 /* We can modify this one. We also add target-specific format types
707 to the end of the array. */
708 static format_kind_info *dynamic_format_types;
710 static int n_format_types = ARRAY_SIZE (format_types_orig);
712 /* Structure detailing the results of checking a format function call
713 where the format expression may be a conditional expression with
714 many leaves resulting from nested conditional expressions. */
715 typedef struct
717 /* Number of leaves of the format argument that could not be checked
718 as they were not string literals. */
719 int number_non_literal;
720 /* Number of leaves of the format argument that were null pointers or
721 string literals, but had extra format arguments. */
722 int number_extra_args;
723 /* Number of leaves of the format argument that were null pointers or
724 string literals, but had extra format arguments and used $ operand
725 numbers. */
726 int number_dollar_extra_args;
727 /* Number of leaves of the format argument that were wide string
728 literals. */
729 int number_wide;
730 /* Number of leaves of the format argument that were empty strings. */
731 int number_empty;
732 /* Number of leaves of the format argument that were unterminated
733 strings. */
734 int number_unterminated;
735 /* Number of leaves of the format argument that were not counted above. */
736 int number_other;
737 } format_check_results;
739 typedef struct
741 format_check_results *res;
742 function_format_info *info;
743 tree params;
744 } format_check_context;
746 static void check_format_info (function_format_info *, tree);
747 static void check_format_arg (void *, tree, unsigned HOST_WIDE_INT);
748 static void check_format_info_main (format_check_results *,
749 function_format_info *,
750 const char *, int, tree,
751 unsigned HOST_WIDE_INT);
753 static void init_dollar_format_checking (int, tree);
754 static int maybe_read_dollar_number (const char **, int,
755 tree, tree *, const format_kind_info *);
756 static bool avoid_dollar_number (const char *);
757 static void finish_dollar_format_checking (format_check_results *, int);
759 static const format_flag_spec *get_flag_spec (const format_flag_spec *,
760 int, const char *);
762 static void check_format_types (format_wanted_type *, const char *, int);
763 static void format_type_warning (const char *, const char *, int, tree,
764 int, const char *, tree, int);
766 /* Decode a format type from a string, returning the type, or
767 format_type_error if not valid, in which case the caller should print an
768 error message. */
769 static int
770 decode_format_type (const char *s)
772 int i;
773 int slen;
774 slen = strlen (s);
775 for (i = 0; i < n_format_types; i++)
777 int alen;
778 if (!strcmp (s, format_types[i].name))
779 return i;
780 alen = strlen (format_types[i].name);
781 if (slen == alen + 4 && s[0] == '_' && s[1] == '_'
782 && s[slen - 1] == '_' && s[slen - 2] == '_'
783 && !strncmp (s + 2, format_types[i].name, alen))
784 return i;
786 return format_type_error;
790 /* Check the argument list of a call to printf, scanf, etc.
791 ATTRS are the attributes on the function type.
792 PARAMS is the list of argument values. Also, if -Wmissing-format-attribute,
793 warn for calls to vprintf or vscanf in functions with no such format
794 attribute themselves. */
796 void
797 check_function_format (tree attrs, tree params)
799 tree a;
801 /* See if this function has any format attributes. */
802 for (a = attrs; a; a = TREE_CHAIN (a))
804 if (is_attribute_p ("format", TREE_PURPOSE (a)))
806 /* Yup; check it. */
807 function_format_info info;
808 decode_format_attr (TREE_VALUE (a), &info, 1);
809 check_format_info (&info, params);
810 if (warn_missing_format_attribute && info.first_arg_num == 0
811 && (format_types[info.format_type].flags
812 & (int) FMT_FLAG_ARG_CONVERT))
814 tree c;
815 for (c = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
817 c = TREE_CHAIN (c))
818 if (is_attribute_p ("format", TREE_PURPOSE (c))
819 && (decode_format_type (IDENTIFIER_POINTER
820 (TREE_VALUE (TREE_VALUE (c))))
821 == info.format_type))
822 break;
823 if (c == NULL_TREE)
825 /* Check if the current function has a parameter to which
826 the format attribute could be attached; if not, it
827 can't be a candidate for a format attribute, despite
828 the vprintf-like or vscanf-like call. */
829 tree args;
830 for (args = DECL_ARGUMENTS (current_function_decl);
831 args != 0;
832 args = TREE_CHAIN (args))
834 if (TREE_CODE (TREE_TYPE (args)) == POINTER_TYPE
835 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args)))
836 == char_type_node))
837 break;
839 if (args != 0)
840 warning ("function might be possible candidate for %qs format attribute",
841 format_types[info.format_type].name);
849 /* Variables used by the checking of $ operand number formats. */
850 static char *dollar_arguments_used = NULL;
851 static char *dollar_arguments_pointer_p = NULL;
852 static int dollar_arguments_alloc = 0;
853 static int dollar_arguments_count;
854 static int dollar_first_arg_num;
855 static int dollar_max_arg_used;
856 static int dollar_format_warned;
858 /* Initialize the checking for a format string that may contain $
859 parameter number specifications; we will need to keep track of whether
860 each parameter has been used. FIRST_ARG_NUM is the number of the first
861 argument that is a parameter to the format, or 0 for a vprintf-style
862 function; PARAMS is the list of arguments starting at this argument. */
864 static void
865 init_dollar_format_checking (int first_arg_num, tree params)
867 tree oparams = params;
869 dollar_first_arg_num = first_arg_num;
870 dollar_arguments_count = 0;
871 dollar_max_arg_used = 0;
872 dollar_format_warned = 0;
873 if (first_arg_num > 0)
875 while (params)
877 dollar_arguments_count++;
878 params = TREE_CHAIN (params);
881 if (dollar_arguments_alloc < dollar_arguments_count)
883 if (dollar_arguments_used)
884 free (dollar_arguments_used);
885 if (dollar_arguments_pointer_p)
886 free (dollar_arguments_pointer_p);
887 dollar_arguments_alloc = dollar_arguments_count;
888 dollar_arguments_used = XNEWVEC (char, dollar_arguments_alloc);
889 dollar_arguments_pointer_p = XNEWVEC (char, dollar_arguments_alloc);
891 if (dollar_arguments_alloc)
893 memset (dollar_arguments_used, 0, dollar_arguments_alloc);
894 if (first_arg_num > 0)
896 int i = 0;
897 params = oparams;
898 while (params)
900 dollar_arguments_pointer_p[i] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params)))
901 == POINTER_TYPE);
902 params = TREE_CHAIN (params);
903 i++;
910 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
911 is set, it is an error if one is not found; otherwise, it is OK. If
912 such a number is found, check whether it is within range and mark that
913 numbered operand as being used for later checking. Returns the operand
914 number if found and within range, zero if no such number was found and
915 this is OK, or -1 on error. PARAMS points to the first operand of the
916 format; PARAM_PTR is made to point to the parameter referred to. If
917 a $ format is found, *FORMAT is updated to point just after it. */
919 static int
920 maybe_read_dollar_number (const char **format,
921 int dollar_needed, tree params, tree *param_ptr,
922 const format_kind_info *fki)
924 int argnum;
925 int overflow_flag;
926 const char *fcp = *format;
927 if (!ISDIGIT (*fcp))
929 if (dollar_needed)
931 warning ("missing $ operand number in format");
932 return -1;
934 else
935 return 0;
937 argnum = 0;
938 overflow_flag = 0;
939 while (ISDIGIT (*fcp))
941 int nargnum;
942 nargnum = 10 * argnum + (*fcp - '0');
943 if (nargnum < 0 || nargnum / 10 != argnum)
944 overflow_flag = 1;
945 argnum = nargnum;
946 fcp++;
948 if (*fcp != '$')
950 if (dollar_needed)
952 warning ("missing $ operand number in format");
953 return -1;
955 else
956 return 0;
958 *format = fcp + 1;
959 if (pedantic && !dollar_format_warned)
961 warning ("%s does not support %%n$ operand number formats",
962 C_STD_NAME (STD_EXT));
963 dollar_format_warned = 1;
965 if (overflow_flag || argnum == 0
966 || (dollar_first_arg_num && argnum > dollar_arguments_count))
968 warning ("operand number out of range in format");
969 return -1;
971 if (argnum > dollar_max_arg_used)
972 dollar_max_arg_used = argnum;
973 /* For vprintf-style functions we may need to allocate more memory to
974 track which arguments are used. */
975 while (dollar_arguments_alloc < dollar_max_arg_used)
977 int nalloc;
978 nalloc = 2 * dollar_arguments_alloc + 16;
979 dollar_arguments_used = XRESIZEVEC (char, dollar_arguments_used,
980 nalloc);
981 dollar_arguments_pointer_p = XRESIZEVEC (char, dollar_arguments_pointer_p,
982 nalloc);
983 memset (dollar_arguments_used + dollar_arguments_alloc, 0,
984 nalloc - dollar_arguments_alloc);
985 dollar_arguments_alloc = nalloc;
987 if (!(fki->flags & (int) FMT_FLAG_DOLLAR_MULTIPLE)
988 && dollar_arguments_used[argnum - 1] == 1)
990 dollar_arguments_used[argnum - 1] = 2;
991 warning ("format argument %d used more than once in %s format",
992 argnum, fki->name);
994 else
995 dollar_arguments_used[argnum - 1] = 1;
996 if (dollar_first_arg_num)
998 int i;
999 *param_ptr = params;
1000 for (i = 1; i < argnum && *param_ptr != 0; i++)
1001 *param_ptr = TREE_CHAIN (*param_ptr);
1003 /* This case shouldn't be caught here. */
1004 gcc_assert (*param_ptr);
1006 else
1007 *param_ptr = 0;
1008 return argnum;
1011 /* Ensure that FORMAT does not start with a decimal number followed by
1012 a $; give a diagnostic and return true if it does, false otherwise. */
1014 static bool
1015 avoid_dollar_number (const char *format)
1017 if (!ISDIGIT (*format))
1018 return false;
1019 while (ISDIGIT (*format))
1020 format++;
1021 if (*format == '$')
1023 warning ("$ operand number used after format without operand number");
1024 return true;
1026 return false;
1030 /* Finish the checking for a format string that used $ operand number formats
1031 instead of non-$ formats. We check for unused operands before used ones
1032 (a serious error, since the implementation of the format function
1033 can't know what types to pass to va_arg to find the later arguments).
1034 and for unused operands at the end of the format (if we know how many
1035 arguments the format had, so not for vprintf). If there were operand
1036 numbers out of range on a non-vprintf-style format, we won't have reached
1037 here. If POINTER_GAP_OK, unused arguments are OK if all arguments are
1038 pointers. */
1040 static void
1041 finish_dollar_format_checking (format_check_results *res, int pointer_gap_ok)
1043 int i;
1044 bool found_pointer_gap = false;
1045 for (i = 0; i < dollar_max_arg_used; i++)
1047 if (!dollar_arguments_used[i])
1049 if (pointer_gap_ok && (dollar_first_arg_num == 0
1050 || dollar_arguments_pointer_p[i]))
1051 found_pointer_gap = true;
1052 else
1053 warning ("format argument %d unused before used argument %d in $-style format",
1054 i + 1, dollar_max_arg_used);
1057 if (found_pointer_gap
1058 || (dollar_first_arg_num
1059 && dollar_max_arg_used < dollar_arguments_count))
1061 res->number_other--;
1062 res->number_dollar_extra_args++;
1067 /* Retrieve the specification for a format flag. SPEC contains the
1068 specifications for format flags for the applicable kind of format.
1069 FLAG is the flag in question. If PREDICATES is NULL, the basic
1070 spec for that flag must be retrieved and this function aborts if
1071 it cannot be found. If PREDICATES is not NULL, it is a string listing
1072 possible predicates for the spec entry; if an entry predicated on any
1073 of these is found, it is returned, otherwise NULL is returned. */
1075 static const format_flag_spec *
1076 get_flag_spec (const format_flag_spec *spec, int flag, const char *predicates)
1078 int i;
1079 for (i = 0; spec[i].flag_char != 0; i++)
1081 if (spec[i].flag_char != flag)
1082 continue;
1083 if (predicates != NULL)
1085 if (spec[i].predicate != 0
1086 && strchr (predicates, spec[i].predicate) != 0)
1087 return &spec[i];
1089 else if (spec[i].predicate == 0)
1090 return &spec[i];
1092 gcc_assert (predicates);
1093 return NULL;
1097 /* Check the argument list of a call to printf, scanf, etc.
1098 INFO points to the function_format_info structure.
1099 PARAMS is the list of argument values. */
1101 static void
1102 check_format_info (function_format_info *info, tree params)
1104 format_check_context format_ctx;
1105 unsigned HOST_WIDE_INT arg_num;
1106 tree format_tree;
1107 format_check_results res;
1108 /* Skip to format argument. If the argument isn't available, there's
1109 no work for us to do; prototype checking will catch the problem. */
1110 for (arg_num = 1; ; ++arg_num)
1112 if (params == 0)
1113 return;
1114 if (arg_num == info->format_num)
1115 break;
1116 params = TREE_CHAIN (params);
1118 format_tree = TREE_VALUE (params);
1119 params = TREE_CHAIN (params);
1120 if (format_tree == 0)
1121 return;
1123 res.number_non_literal = 0;
1124 res.number_extra_args = 0;
1125 res.number_dollar_extra_args = 0;
1126 res.number_wide = 0;
1127 res.number_empty = 0;
1128 res.number_unterminated = 0;
1129 res.number_other = 0;
1131 format_ctx.res = &res;
1132 format_ctx.info = info;
1133 format_ctx.params = params;
1135 check_function_arguments_recurse (check_format_arg, &format_ctx,
1136 format_tree, arg_num);
1138 if (res.number_non_literal > 0)
1140 /* Functions taking a va_list normally pass a non-literal format
1141 string. These functions typically are declared with
1142 first_arg_num == 0, so avoid warning in those cases. */
1143 if (!(format_types[info->format_type].flags & (int) FMT_FLAG_ARG_CONVERT))
1145 /* For strftime-like formats, warn for not checking the format
1146 string; but there are no arguments to check. */
1147 if (warn_format_nonliteral)
1148 warning ("format not a string literal, format string not checked");
1150 else if (info->first_arg_num != 0)
1152 /* If there are no arguments for the format at all, we may have
1153 printf (foo) which is likely to be a security hole. */
1154 while (arg_num + 1 < info->first_arg_num)
1156 if (params == 0)
1157 break;
1158 params = TREE_CHAIN (params);
1159 ++arg_num;
1161 if (params == 0 && (warn_format_nonliteral || warn_format_security))
1162 warning ("format not a string literal and no format arguments");
1163 else if (warn_format_nonliteral)
1164 warning ("format not a string literal, argument types not checked");
1168 /* If there were extra arguments to the format, normally warn. However,
1169 the standard does say extra arguments are ignored, so in the specific
1170 case where we have multiple leaves (conditional expressions or
1171 ngettext) allow extra arguments if at least one leaf didn't have extra
1172 arguments, but was otherwise OK (either non-literal or checked OK).
1173 If the format is an empty string, this should be counted similarly to the
1174 case of extra format arguments. */
1175 if (res.number_extra_args > 0 && res.number_non_literal == 0
1176 && res.number_other == 0 && warn_format_extra_args)
1177 warning ("too many arguments for format");
1178 if (res.number_dollar_extra_args > 0 && res.number_non_literal == 0
1179 && res.number_other == 0 && warn_format_extra_args)
1180 warning ("unused arguments in $-style format");
1181 if (res.number_empty > 0 && res.number_non_literal == 0
1182 && res.number_other == 0 && warn_format_zero_length)
1183 warning ("zero-length %s format string",
1184 format_types[info->format_type].name);
1186 if (res.number_wide > 0)
1187 warning ("format is a wide character string");
1189 if (res.number_unterminated > 0)
1190 warning ("unterminated format string");
1193 /* Callback from check_function_arguments_recurse to check a
1194 format string. FORMAT_TREE is the format parameter. ARG_NUM
1195 is the number of the format argument. CTX points to a
1196 format_check_context. */
1198 static void
1199 check_format_arg (void *ctx, tree format_tree,
1200 unsigned HOST_WIDE_INT arg_num)
1202 format_check_context *format_ctx = (format_check_context *) ctx;
1203 format_check_results *res = format_ctx->res;
1204 function_format_info *info = format_ctx->info;
1205 tree params = format_ctx->params;
1207 int format_length;
1208 HOST_WIDE_INT offset;
1209 const char *format_chars;
1210 tree array_size = 0;
1211 tree array_init;
1213 if (integer_zerop (format_tree))
1215 /* Skip to first argument to check, so we can see if this format
1216 has any arguments (it shouldn't). */
1217 while (arg_num + 1 < info->first_arg_num)
1219 if (params == 0)
1220 return;
1221 params = TREE_CHAIN (params);
1222 ++arg_num;
1225 if (params == 0)
1226 res->number_other++;
1227 else
1228 res->number_extra_args++;
1230 return;
1233 offset = 0;
1234 if (TREE_CODE (format_tree) == PLUS_EXPR)
1236 tree arg0, arg1;
1238 arg0 = TREE_OPERAND (format_tree, 0);
1239 arg1 = TREE_OPERAND (format_tree, 1);
1240 STRIP_NOPS (arg0);
1241 STRIP_NOPS (arg1);
1242 if (TREE_CODE (arg1) == INTEGER_CST)
1243 format_tree = arg0;
1244 else if (TREE_CODE (arg0) == INTEGER_CST)
1246 format_tree = arg1;
1247 arg1 = arg0;
1249 else
1251 res->number_non_literal++;
1252 return;
1254 if (!host_integerp (arg1, 0)
1255 || (offset = tree_low_cst (arg1, 0)) < 0)
1257 res->number_non_literal++;
1258 return;
1261 if (TREE_CODE (format_tree) != ADDR_EXPR)
1263 res->number_non_literal++;
1264 return;
1266 format_tree = TREE_OPERAND (format_tree, 0);
1267 if (TREE_CODE (format_tree) == VAR_DECL
1268 && TREE_CODE (TREE_TYPE (format_tree)) == ARRAY_TYPE
1269 && (array_init = decl_constant_value (format_tree)) != format_tree
1270 && TREE_CODE (array_init) == STRING_CST)
1272 /* Extract the string constant initializer. Note that this may include
1273 a trailing NUL character that is not in the array (e.g.
1274 const char a[3] = "foo";). */
1275 array_size = DECL_SIZE_UNIT (format_tree);
1276 format_tree = array_init;
1278 if (TREE_CODE (format_tree) != STRING_CST)
1280 res->number_non_literal++;
1281 return;
1283 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree))) != char_type_node)
1285 res->number_wide++;
1286 return;
1288 format_chars = TREE_STRING_POINTER (format_tree);
1289 format_length = TREE_STRING_LENGTH (format_tree);
1290 if (array_size != 0)
1292 /* Variable length arrays can't be initialized. */
1293 gcc_assert (TREE_CODE (array_size) == INTEGER_CST);
1295 if (host_integerp (array_size, 0))
1297 HOST_WIDE_INT array_size_value = TREE_INT_CST_LOW (array_size);
1298 if (array_size_value > 0
1299 && array_size_value == (int) array_size_value
1300 && format_length > array_size_value)
1301 format_length = array_size_value;
1304 if (offset)
1306 if (offset >= format_length)
1308 res->number_non_literal++;
1309 return;
1311 format_chars += offset;
1312 format_length -= offset;
1314 if (format_length < 1)
1316 res->number_unterminated++;
1317 return;
1319 if (format_length == 1)
1321 res->number_empty++;
1322 return;
1324 if (format_chars[--format_length] != 0)
1326 res->number_unterminated++;
1327 return;
1330 /* Skip to first argument to check. */
1331 while (arg_num + 1 < info->first_arg_num)
1333 if (params == 0)
1334 return;
1335 params = TREE_CHAIN (params);
1336 ++arg_num;
1338 /* Provisionally increment res->number_other; check_format_info_main
1339 will decrement it if it finds there are extra arguments, but this way
1340 need not adjust it for every return. */
1341 res->number_other++;
1342 check_format_info_main (res, info, format_chars, format_length,
1343 params, arg_num);
1347 /* Do the main part of checking a call to a format function. FORMAT_CHARS
1348 is the NUL-terminated format string (which at this point may contain
1349 internal NUL characters); FORMAT_LENGTH is its length (excluding the
1350 terminating NUL character). ARG_NUM is one less than the number of
1351 the first format argument to check; PARAMS points to that format
1352 argument in the list of arguments. */
1354 static void
1355 check_format_info_main (format_check_results *res,
1356 function_format_info *info, const char *format_chars,
1357 int format_length, tree params,
1358 unsigned HOST_WIDE_INT arg_num)
1360 const char *orig_format_chars = format_chars;
1361 tree first_fillin_param = params;
1363 const format_kind_info *fki = &format_types[info->format_type];
1364 const format_flag_spec *flag_specs = fki->flag_specs;
1365 const format_flag_pair *bad_flag_pairs = fki->bad_flag_pairs;
1367 /* -1 if no conversions taking an operand have been found; 0 if one has
1368 and it didn't use $; 1 if $ formats are in use. */
1369 int has_operand_number = -1;
1371 init_dollar_format_checking (info->first_arg_num, first_fillin_param);
1373 while (1)
1375 int i;
1376 int suppressed = FALSE;
1377 const char *length_chars = NULL;
1378 enum format_lengths length_chars_val = FMT_LEN_none;
1379 enum format_std_version length_chars_std = STD_C89;
1380 int format_char;
1381 tree cur_param;
1382 tree wanted_type;
1383 int main_arg_num = 0;
1384 tree main_arg_params = 0;
1385 enum format_std_version wanted_type_std;
1386 const char *wanted_type_name;
1387 format_wanted_type width_wanted_type;
1388 format_wanted_type precision_wanted_type;
1389 format_wanted_type main_wanted_type;
1390 format_wanted_type *first_wanted_type = NULL;
1391 format_wanted_type *last_wanted_type = NULL;
1392 const format_length_info *fli = NULL;
1393 const format_char_info *fci = NULL;
1394 char flag_chars[256];
1395 int aflag = 0;
1396 const char *format_start = format_chars;
1397 if (*format_chars == 0)
1399 if (format_chars - orig_format_chars != format_length)
1400 warning ("embedded %<\\0%> in format");
1401 if (info->first_arg_num != 0 && params != 0
1402 && has_operand_number <= 0)
1404 res->number_other--;
1405 res->number_extra_args++;
1407 if (has_operand_number > 0)
1408 finish_dollar_format_checking (res, fki->flags & (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK);
1409 return;
1411 if (*format_chars++ != '%')
1412 continue;
1413 if (*format_chars == 0)
1415 warning ("spurious trailing %<%%%> in format");
1416 continue;
1418 if (*format_chars == '%')
1420 ++format_chars;
1421 continue;
1423 flag_chars[0] = 0;
1425 if ((fki->flags & (int) FMT_FLAG_USE_DOLLAR) && has_operand_number != 0)
1427 /* Possibly read a $ operand number at the start of the format.
1428 If one was previously used, one is required here. If one
1429 is not used here, we can't immediately conclude this is a
1430 format without them, since it could be printf %m or scanf %*. */
1431 int opnum;
1432 opnum = maybe_read_dollar_number (&format_chars, 0,
1433 first_fillin_param,
1434 &main_arg_params, fki);
1435 if (opnum == -1)
1436 return;
1437 else if (opnum > 0)
1439 has_operand_number = 1;
1440 main_arg_num = opnum + info->first_arg_num - 1;
1443 else if (fki->flags & FMT_FLAG_USE_DOLLAR)
1445 if (avoid_dollar_number (format_chars))
1446 return;
1449 /* Read any format flags, but do not yet validate them beyond removing
1450 duplicates, since in general validation depends on the rest of
1451 the format. */
1452 while (*format_chars != 0
1453 && strchr (fki->flag_chars, *format_chars) != 0)
1455 const format_flag_spec *s = get_flag_spec (flag_specs,
1456 *format_chars, NULL);
1457 if (strchr (flag_chars, *format_chars) != 0)
1459 warning ("repeated %s in format", _(s->name));
1461 else
1463 i = strlen (flag_chars);
1464 flag_chars[i++] = *format_chars;
1465 flag_chars[i] = 0;
1467 if (s->skip_next_char)
1469 ++format_chars;
1470 if (*format_chars == 0)
1472 warning ("missing fill character at end of strfmon format");
1473 return;
1476 ++format_chars;
1479 /* Read any format width, possibly * or *m$. */
1480 if (fki->width_char != 0)
1482 if (fki->width_type != NULL && *format_chars == '*')
1484 i = strlen (flag_chars);
1485 flag_chars[i++] = fki->width_char;
1486 flag_chars[i] = 0;
1487 /* "...a field width...may be indicated by an asterisk.
1488 In this case, an int argument supplies the field width..." */
1489 ++format_chars;
1490 if (has_operand_number != 0)
1492 int opnum;
1493 opnum = maybe_read_dollar_number (&format_chars,
1494 has_operand_number == 1,
1495 first_fillin_param,
1496 &params, fki);
1497 if (opnum == -1)
1498 return;
1499 else if (opnum > 0)
1501 has_operand_number = 1;
1502 arg_num = opnum + info->first_arg_num - 1;
1504 else
1505 has_operand_number = 0;
1507 else
1509 if (avoid_dollar_number (format_chars))
1510 return;
1512 if (info->first_arg_num != 0)
1514 if (params == 0)
1516 warning ("too few arguments for format");
1517 return;
1519 cur_param = TREE_VALUE (params);
1520 if (has_operand_number <= 0)
1522 params = TREE_CHAIN (params);
1523 ++arg_num;
1525 width_wanted_type.wanted_type = *fki->width_type;
1526 width_wanted_type.wanted_type_name = NULL;
1527 width_wanted_type.pointer_count = 0;
1528 width_wanted_type.char_lenient_flag = 0;
1529 width_wanted_type.writing_in_flag = 0;
1530 width_wanted_type.reading_from_flag = 0;
1531 width_wanted_type.name = _("field width");
1532 width_wanted_type.param = cur_param;
1533 width_wanted_type.arg_num = arg_num;
1534 width_wanted_type.next = NULL;
1535 if (last_wanted_type != 0)
1536 last_wanted_type->next = &width_wanted_type;
1537 if (first_wanted_type == 0)
1538 first_wanted_type = &width_wanted_type;
1539 last_wanted_type = &width_wanted_type;
1542 else
1544 /* Possibly read a numeric width. If the width is zero,
1545 we complain if appropriate. */
1546 int non_zero_width_char = FALSE;
1547 int found_width = FALSE;
1548 while (ISDIGIT (*format_chars))
1550 found_width = TRUE;
1551 if (*format_chars != '0')
1552 non_zero_width_char = TRUE;
1553 ++format_chars;
1555 if (found_width && !non_zero_width_char &&
1556 (fki->flags & (int) FMT_FLAG_ZERO_WIDTH_BAD))
1557 warning ("zero width in %s format", fki->name);
1558 if (found_width)
1560 i = strlen (flag_chars);
1561 flag_chars[i++] = fki->width_char;
1562 flag_chars[i] = 0;
1567 /* Read any format left precision (must be a number, not *). */
1568 if (fki->left_precision_char != 0 && *format_chars == '#')
1570 ++format_chars;
1571 i = strlen (flag_chars);
1572 flag_chars[i++] = fki->left_precision_char;
1573 flag_chars[i] = 0;
1574 if (!ISDIGIT (*format_chars))
1575 warning ("empty left precision in %s format", fki->name);
1576 while (ISDIGIT (*format_chars))
1577 ++format_chars;
1580 /* Read any format precision, possibly * or *m$. */
1581 if (fki->precision_char != 0 && *format_chars == '.')
1583 ++format_chars;
1584 i = strlen (flag_chars);
1585 flag_chars[i++] = fki->precision_char;
1586 flag_chars[i] = 0;
1587 if (fki->precision_type != NULL && *format_chars == '*')
1589 /* "...a...precision...may be indicated by an asterisk.
1590 In this case, an int argument supplies the...precision." */
1591 ++format_chars;
1592 if (has_operand_number != 0)
1594 int opnum;
1595 opnum = maybe_read_dollar_number (&format_chars,
1596 has_operand_number == 1,
1597 first_fillin_param,
1598 &params, fki);
1599 if (opnum == -1)
1600 return;
1601 else if (opnum > 0)
1603 has_operand_number = 1;
1604 arg_num = opnum + info->first_arg_num - 1;
1606 else
1607 has_operand_number = 0;
1609 else
1611 if (avoid_dollar_number (format_chars))
1612 return;
1614 if (info->first_arg_num != 0)
1616 if (params == 0)
1618 warning ("too few arguments for format");
1619 return;
1621 cur_param = TREE_VALUE (params);
1622 if (has_operand_number <= 0)
1624 params = TREE_CHAIN (params);
1625 ++arg_num;
1627 precision_wanted_type.wanted_type = *fki->precision_type;
1628 precision_wanted_type.wanted_type_name = NULL;
1629 precision_wanted_type.pointer_count = 0;
1630 precision_wanted_type.char_lenient_flag = 0;
1631 precision_wanted_type.writing_in_flag = 0;
1632 precision_wanted_type.reading_from_flag = 0;
1633 precision_wanted_type.name = _("field precision");
1634 precision_wanted_type.param = cur_param;
1635 precision_wanted_type.arg_num = arg_num;
1636 precision_wanted_type.next = NULL;
1637 if (last_wanted_type != 0)
1638 last_wanted_type->next = &precision_wanted_type;
1639 if (first_wanted_type == 0)
1640 first_wanted_type = &precision_wanted_type;
1641 last_wanted_type = &precision_wanted_type;
1644 else
1646 if (!(fki->flags & (int) FMT_FLAG_EMPTY_PREC_OK)
1647 && !ISDIGIT (*format_chars))
1648 warning ("empty precision in %s format", fki->name);
1649 while (ISDIGIT (*format_chars))
1650 ++format_chars;
1654 /* Read any length modifier, if this kind of format has them. */
1655 fli = fki->length_char_specs;
1656 length_chars = NULL;
1657 length_chars_val = FMT_LEN_none;
1658 length_chars_std = STD_C89;
1659 if (fli)
1661 while (fli->name != 0 && fli->name[0] != *format_chars)
1662 fli++;
1663 if (fli->name != 0)
1665 format_chars++;
1666 if (fli->double_name != 0 && fli->name[0] == *format_chars)
1668 format_chars++;
1669 length_chars = fli->double_name;
1670 length_chars_val = fli->double_index;
1671 length_chars_std = fli->double_std;
1673 else
1675 length_chars = fli->name;
1676 length_chars_val = fli->index;
1677 length_chars_std = fli->std;
1679 i = strlen (flag_chars);
1680 flag_chars[i++] = fki->length_code_char;
1681 flag_chars[i] = 0;
1683 if (pedantic)
1685 /* Warn if the length modifier is non-standard. */
1686 if (ADJ_STD (length_chars_std) > C_STD_VER)
1687 warning ("%s does not support the %qs %s length modifier",
1688 C_STD_NAME (length_chars_std), length_chars,
1689 fki->name);
1693 /* Read any modifier (strftime E/O). */
1694 if (fki->modifier_chars != NULL)
1696 while (*format_chars != 0
1697 && strchr (fki->modifier_chars, *format_chars) != 0)
1699 if (strchr (flag_chars, *format_chars) != 0)
1701 const format_flag_spec *s = get_flag_spec (flag_specs,
1702 *format_chars, NULL);
1703 warning ("repeated %s in format", _(s->name));
1705 else
1707 i = strlen (flag_chars);
1708 flag_chars[i++] = *format_chars;
1709 flag_chars[i] = 0;
1711 ++format_chars;
1715 /* Handle the scanf allocation kludge. */
1716 if (fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
1718 if (*format_chars == 'a' && !flag_isoc99)
1720 if (format_chars[1] == 's' || format_chars[1] == 'S'
1721 || format_chars[1] == '[')
1723 /* 'a' is used as a flag. */
1724 i = strlen (flag_chars);
1725 flag_chars[i++] = 'a';
1726 flag_chars[i] = 0;
1727 format_chars++;
1732 format_char = *format_chars;
1733 if (format_char == 0
1734 || (!(fki->flags & (int) FMT_FLAG_FANCY_PERCENT_OK)
1735 && format_char == '%'))
1737 warning ("conversion lacks type at end of format");
1738 continue;
1740 format_chars++;
1741 fci = fki->conversion_specs;
1742 while (fci->format_chars != 0
1743 && strchr (fci->format_chars, format_char) == 0)
1744 ++fci;
1745 if (fci->format_chars == 0)
1747 if (ISGRAPH (format_char))
1748 warning ("unknown conversion type character %qc in format",
1749 format_char);
1750 else
1751 warning ("unknown conversion type character 0x%x in format",
1752 format_char);
1753 continue;
1755 if (pedantic)
1757 if (ADJ_STD (fci->std) > C_STD_VER)
1758 warning ("%s does not support the %<%%%c%> %s format",
1759 C_STD_NAME (fci->std), format_char, fki->name);
1762 /* Validate the individual flags used, removing any that are invalid. */
1764 int d = 0;
1765 for (i = 0; flag_chars[i] != 0; i++)
1767 const format_flag_spec *s = get_flag_spec (flag_specs,
1768 flag_chars[i], NULL);
1769 flag_chars[i - d] = flag_chars[i];
1770 if (flag_chars[i] == fki->length_code_char)
1771 continue;
1772 if (strchr (fci->flag_chars, flag_chars[i]) == 0)
1774 warning ("%s used with %<%%%c%> %s format",
1775 _(s->name), format_char, fki->name);
1776 d++;
1777 continue;
1779 if (pedantic)
1781 const format_flag_spec *t;
1782 if (ADJ_STD (s->std) > C_STD_VER)
1783 warning ("%s does not support %s",
1784 C_STD_NAME (s->std), _(s->long_name));
1785 t = get_flag_spec (flag_specs, flag_chars[i], fci->flags2);
1786 if (t != NULL && ADJ_STD (t->std) > ADJ_STD (s->std))
1788 const char *long_name = (t->long_name != NULL
1789 ? t->long_name
1790 : s->long_name);
1791 if (ADJ_STD (t->std) > C_STD_VER)
1792 warning ("%s does not support %s with the %<%%%c%> %s format",
1793 C_STD_NAME (t->std), _(long_name),
1794 format_char, fki->name);
1798 flag_chars[i - d] = 0;
1801 if ((fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
1802 && strchr (flag_chars, 'a') != 0)
1803 aflag = 1;
1805 if (fki->suppression_char
1806 && strchr (flag_chars, fki->suppression_char) != 0)
1807 suppressed = 1;
1809 /* Validate the pairs of flags used. */
1810 for (i = 0; bad_flag_pairs[i].flag_char1 != 0; i++)
1812 const format_flag_spec *s, *t;
1813 if (strchr (flag_chars, bad_flag_pairs[i].flag_char1) == 0)
1814 continue;
1815 if (strchr (flag_chars, bad_flag_pairs[i].flag_char2) == 0)
1816 continue;
1817 if (bad_flag_pairs[i].predicate != 0
1818 && strchr (fci->flags2, bad_flag_pairs[i].predicate) == 0)
1819 continue;
1820 s = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char1, NULL);
1821 t = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char2, NULL);
1822 if (bad_flag_pairs[i].ignored)
1824 if (bad_flag_pairs[i].predicate != 0)
1825 warning ("%s ignored with %s and %<%%%c%> %s format",
1826 _(s->name), _(t->name), format_char,
1827 fki->name);
1828 else
1829 warning ("%s ignored with %s in %s format",
1830 _(s->name), _(t->name), fki->name);
1832 else
1834 if (bad_flag_pairs[i].predicate != 0)
1835 warning ("use of %s and %s together with %<%%%c%> %s format",
1836 _(s->name), _(t->name), format_char,
1837 fki->name);
1838 else
1839 warning ("use of %s and %s together in %s format",
1840 _(s->name), _(t->name), fki->name);
1844 /* Give Y2K warnings. */
1845 if (warn_format_y2k)
1847 int y2k_level = 0;
1848 if (strchr (fci->flags2, '4') != 0)
1849 if (strchr (flag_chars, 'E') != 0)
1850 y2k_level = 3;
1851 else
1852 y2k_level = 2;
1853 else if (strchr (fci->flags2, '3') != 0)
1854 y2k_level = 3;
1855 else if (strchr (fci->flags2, '2') != 0)
1856 y2k_level = 2;
1857 if (y2k_level == 3)
1858 warning ("%<%%%c%> yields only last 2 digits of year in some locales",
1859 format_char);
1860 else if (y2k_level == 2)
1861 warning ("%<%%%c%> yields only last 2 digits of year", format_char);
1864 if (strchr (fci->flags2, '[') != 0)
1866 /* Skip over scan set, in case it happens to have '%' in it. */
1867 if (*format_chars == '^')
1868 ++format_chars;
1869 /* Find closing bracket; if one is hit immediately, then
1870 it's part of the scan set rather than a terminator. */
1871 if (*format_chars == ']')
1872 ++format_chars;
1873 while (*format_chars && *format_chars != ']')
1874 ++format_chars;
1875 if (*format_chars != ']')
1876 /* The end of the format string was reached. */
1877 warning ("no closing %<]%> for %<%%[%> format");
1880 wanted_type = 0;
1881 wanted_type_name = 0;
1882 if (fki->flags & (int) FMT_FLAG_ARG_CONVERT)
1884 wanted_type = (fci->types[length_chars_val].type
1885 ? *fci->types[length_chars_val].type : 0);
1886 wanted_type_name = fci->types[length_chars_val].name;
1887 wanted_type_std = fci->types[length_chars_val].std;
1888 if (wanted_type == 0)
1890 warning ("use of %qs length modifier with %qc type character",
1891 length_chars, format_char);
1892 /* Heuristic: skip one argument when an invalid length/type
1893 combination is encountered. */
1894 arg_num++;
1895 if (params == 0)
1897 warning ("too few arguments for format");
1898 return;
1900 params = TREE_CHAIN (params);
1901 continue;
1903 else if (pedantic
1904 /* Warn if non-standard, provided it is more non-standard
1905 than the length and type characters that may already
1906 have been warned for. */
1907 && ADJ_STD (wanted_type_std) > ADJ_STD (length_chars_std)
1908 && ADJ_STD (wanted_type_std) > ADJ_STD (fci->std))
1910 if (ADJ_STD (wanted_type_std) > C_STD_VER)
1911 warning ("%s does not support the %<%%%s%c%> %s format",
1912 C_STD_NAME (wanted_type_std), length_chars,
1913 format_char, fki->name);
1917 main_wanted_type.next = NULL;
1919 /* Finally. . .check type of argument against desired type! */
1920 if (info->first_arg_num == 0)
1921 continue;
1922 if ((fci->pointer_count == 0 && wanted_type == void_type_node)
1923 || suppressed)
1925 if (main_arg_num != 0)
1927 if (suppressed)
1928 warning ("operand number specified with suppressed assignment");
1929 else
1930 warning ("operand number specified for format taking no argument");
1933 else
1935 format_wanted_type *wanted_type_ptr;
1937 if (main_arg_num != 0)
1939 arg_num = main_arg_num;
1940 params = main_arg_params;
1942 else
1944 ++arg_num;
1945 if (has_operand_number > 0)
1947 warning ("missing $ operand number in format");
1948 return;
1950 else
1951 has_operand_number = 0;
1954 wanted_type_ptr = &main_wanted_type;
1955 while (fci)
1957 if (params == 0)
1959 warning ("too few arguments for format");
1960 return;
1963 cur_param = TREE_VALUE (params);
1964 params = TREE_CHAIN (params);
1966 wanted_type_ptr->wanted_type = wanted_type;
1967 wanted_type_ptr->wanted_type_name = wanted_type_name;
1968 wanted_type_ptr->pointer_count = fci->pointer_count + aflag;
1969 wanted_type_ptr->char_lenient_flag = 0;
1970 if (strchr (fci->flags2, 'c') != 0)
1971 wanted_type_ptr->char_lenient_flag = 1;
1972 wanted_type_ptr->writing_in_flag = 0;
1973 wanted_type_ptr->reading_from_flag = 0;
1974 if (aflag)
1975 wanted_type_ptr->writing_in_flag = 1;
1976 else
1978 if (strchr (fci->flags2, 'W') != 0)
1979 wanted_type_ptr->writing_in_flag = 1;
1980 if (strchr (fci->flags2, 'R') != 0)
1981 wanted_type_ptr->reading_from_flag = 1;
1983 wanted_type_ptr->name = NULL;
1984 wanted_type_ptr->param = cur_param;
1985 wanted_type_ptr->arg_num = arg_num;
1986 wanted_type_ptr->next = NULL;
1987 if (last_wanted_type != 0)
1988 last_wanted_type->next = wanted_type_ptr;
1989 if (first_wanted_type == 0)
1990 first_wanted_type = wanted_type_ptr;
1991 last_wanted_type = wanted_type_ptr;
1993 fci = fci->chain;
1994 if (fci)
1996 wanted_type_ptr = ggc_alloc (sizeof (main_wanted_type));
1997 arg_num++;
1998 wanted_type = *fci->types[length_chars_val].type;
1999 wanted_type_name = fci->types[length_chars_val].name;
2004 if (first_wanted_type != 0)
2005 check_format_types (first_wanted_type, format_start,
2006 format_chars - format_start);
2008 if (main_wanted_type.next != NULL)
2010 format_wanted_type *wanted_type_ptr = main_wanted_type.next;
2011 while (wanted_type_ptr)
2013 format_wanted_type *next = wanted_type_ptr->next;
2014 ggc_free (wanted_type_ptr);
2015 wanted_type_ptr = next;
2022 /* Check the argument types from a single format conversion (possibly
2023 including width and precision arguments). */
2024 static void
2025 check_format_types (format_wanted_type *types, const char *format_start,
2026 int format_length)
2028 for (; types != 0; types = types->next)
2030 tree cur_param;
2031 tree cur_type;
2032 tree orig_cur_type;
2033 tree wanted_type;
2034 int arg_num;
2035 int i;
2036 int char_type_flag;
2037 cur_param = types->param;
2038 cur_type = TREE_TYPE (cur_param);
2039 if (cur_type == error_mark_node)
2040 continue;
2041 orig_cur_type = cur_type;
2042 char_type_flag = 0;
2043 wanted_type = types->wanted_type;
2044 arg_num = types->arg_num;
2046 /* The following should not occur here. */
2047 gcc_assert (wanted_type);
2048 gcc_assert (wanted_type != void_type_node || types->pointer_count);
2050 if (types->pointer_count == 0)
2051 wanted_type = lang_hooks.types.type_promotes_to (wanted_type);
2053 wanted_type = TYPE_MAIN_VARIANT (wanted_type);
2055 STRIP_NOPS (cur_param);
2057 /* Check the types of any additional pointer arguments
2058 that precede the "real" argument. */
2059 for (i = 0; i < types->pointer_count; ++i)
2061 if (TREE_CODE (cur_type) == POINTER_TYPE)
2063 cur_type = TREE_TYPE (cur_type);
2064 if (cur_type == error_mark_node)
2065 break;
2067 /* Check for writing through a NULL pointer. */
2068 if (types->writing_in_flag
2069 && i == 0
2070 && cur_param != 0
2071 && integer_zerop (cur_param))
2072 warning ("writing through null pointer (arg %d)",
2073 arg_num);
2075 /* Check for reading through a NULL pointer. */
2076 if (types->reading_from_flag
2077 && i == 0
2078 && cur_param != 0
2079 && integer_zerop (cur_param))
2080 warning ("reading through null pointer (arg %d)",
2081 arg_num);
2083 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
2084 cur_param = TREE_OPERAND (cur_param, 0);
2085 else
2086 cur_param = 0;
2088 /* See if this is an attempt to write into a const type with
2089 scanf or with printf "%n". Note: the writing in happens
2090 at the first indirection only, if for example
2091 void * const * is passed to scanf %p; passing
2092 const void ** is simply passing an incompatible type. */
2093 if (types->writing_in_flag
2094 && i == 0
2095 && (TYPE_READONLY (cur_type)
2096 || (cur_param != 0
2097 && (CONSTANT_CLASS_P (cur_param)
2098 || (DECL_P (cur_param)
2099 && TREE_READONLY (cur_param))))))
2100 warning ("writing into constant object (arg %d)", arg_num);
2102 /* If there are extra type qualifiers beyond the first
2103 indirection, then this makes the types technically
2104 incompatible. */
2105 if (i > 0
2106 && pedantic
2107 && (TYPE_READONLY (cur_type)
2108 || TYPE_VOLATILE (cur_type)
2109 || TYPE_RESTRICT (cur_type)))
2110 warning ("extra type qualifiers in format argument (arg %d)",
2111 arg_num);
2114 else
2116 format_type_warning (types->name, format_start, format_length,
2117 wanted_type, types->pointer_count,
2118 types->wanted_type_name, orig_cur_type,
2119 arg_num);
2120 break;
2124 if (i < types->pointer_count)
2125 continue;
2127 cur_type = TYPE_MAIN_VARIANT (cur_type);
2129 /* Check whether the argument type is a character type. This leniency
2130 only applies to certain formats, flagged with 'c'.
2132 if (types->char_lenient_flag)
2133 char_type_flag = (cur_type == char_type_node
2134 || cur_type == signed_char_type_node
2135 || cur_type == unsigned_char_type_node);
2137 /* Check the type of the "real" argument, if there's a type we want. */
2138 if (wanted_type == cur_type)
2139 continue;
2140 /* If we want 'void *', allow any pointer type.
2141 (Anything else would already have got a warning.)
2142 With -pedantic, only allow pointers to void and to character
2143 types. */
2144 if (wanted_type == void_type_node
2145 && (!pedantic || (i == 1 && char_type_flag)))
2146 continue;
2147 /* Don't warn about differences merely in signedness, unless
2148 -pedantic. With -pedantic, warn if the type is a pointer
2149 target and not a character type, and for character types at
2150 a second level of indirection. */
2151 if (TREE_CODE (wanted_type) == INTEGER_TYPE
2152 && TREE_CODE (cur_type) == INTEGER_TYPE
2153 && (!pedantic || i == 0 || (i == 1 && char_type_flag))
2154 && (TYPE_UNSIGNED (wanted_type)
2155 ? wanted_type == c_common_unsigned_type (cur_type)
2156 : wanted_type == c_common_signed_type (cur_type)))
2157 continue;
2158 /* Likewise, "signed char", "unsigned char" and "char" are
2159 equivalent but the above test won't consider them equivalent. */
2160 if (wanted_type == char_type_node
2161 && (!pedantic || i < 2)
2162 && char_type_flag)
2163 continue;
2164 /* Now we have a type mismatch. */
2165 format_type_warning (types->name, format_start, format_length,
2166 wanted_type, types->pointer_count,
2167 types->wanted_type_name, orig_cur_type, arg_num);
2172 /* Give a warning about a format argument of different type from that
2173 expected. DESCR is a description such as "field precision", or
2174 NULL for an ordinary format. For an ordinary format, FORMAT_START
2175 points to where the format starts in the format string and
2176 FORMAT_LENGTH is its length. WANTED_TYPE is the type the argument
2177 should have after POINTER_COUNT pointer dereferences.
2178 WANTED_NAME_NAME is a possibly more friendly name of WANTED_TYPE,
2179 or NULL if the ordinary name of the type should be used. ARG_TYPE
2180 is the type of the actual argument. ARG_NUM is the number of that
2181 argument. */
2182 static void
2183 format_type_warning (const char *descr, const char *format_start,
2184 int format_length, tree wanted_type, int pointer_count,
2185 const char *wanted_type_name, tree arg_type, int arg_num)
2187 char *p;
2188 /* If ARG_TYPE is a typedef with a misleading name (for example,
2189 size_t but not the standard size_t expected by printf %zu), avoid
2190 printing the typedef name. */
2191 if (wanted_type_name
2192 && TYPE_NAME (arg_type)
2193 && TREE_CODE (TYPE_NAME (arg_type)) == TYPE_DECL
2194 && DECL_NAME (TYPE_NAME (arg_type))
2195 && !strcmp (wanted_type_name,
2196 lang_hooks.decl_printable_name (TYPE_NAME (arg_type), 2)))
2197 arg_type = TYPE_MAIN_VARIANT (arg_type);
2198 /* The format type and name exclude any '*' for pointers, so those
2199 must be formatted manually. For all the types we currently have,
2200 this is adequate, but formats taking pointers to functions or
2201 arrays would require the full type to be built up in order to
2202 print it with %T. */
2203 p = alloca (pointer_count + 2);
2204 if (pointer_count == 0)
2205 p[0] = 0;
2206 else if (c_dialect_cxx ())
2208 memset (p, '*', pointer_count);
2209 p[pointer_count] = 0;
2211 else
2213 p[0] = ' ';
2214 memset (p + 1, '*', pointer_count);
2215 p[pointer_count + 1] = 0;
2217 if (wanted_type_name)
2219 if (descr)
2220 warning ("%s should have type %<%s%s%>, but argument %d has type %qT",
2221 descr, wanted_type_name, p, arg_num, arg_type);
2222 else
2223 warning ("format %q.*s expects type %<%s%s%>, but argument %d has type %qT",
2224 format_length, format_start, wanted_type_name, p,
2225 arg_num, arg_type);
2227 else
2229 if (descr)
2230 warning ("%s should have type %<%T%s%>, but argument %d has type %qT",
2231 descr, wanted_type, p, arg_num, arg_type);
2232 else
2233 warning ("format %q.*s expects type %<%T%s%>, but argument %d has type %qT",
2234 format_length, format_start, wanted_type, p, arg_num, arg_type);
2239 /* Given a format_char_info array FCI, and a character C, this function
2240 returns the index into the conversion_specs where that specifier's
2241 data is located. If the character isn't found it aborts. */
2242 static unsigned int
2243 find_char_info_specifier_index (const format_char_info *fci, int c)
2245 unsigned i;
2247 for (i = 0; fci->format_chars; i++, fci++)
2248 if (strchr (fci->format_chars, c))
2249 return i;
2251 /* We shouldn't be looking for a non-existent specifier. */
2252 gcc_unreachable ();
2255 /* Given a format_length_info array FLI, and a character C, this
2256 function returns the index into the conversion_specs where that
2257 modifier's data is located. If the character isn't found it
2258 aborts. */
2259 static unsigned int
2260 find_length_info_modifier_index (const format_length_info *fli, int c)
2262 unsigned i;
2264 for (i = 0; fli->name; i++, fli++)
2265 if (strchr (fli->name, c))
2266 return i;
2268 /* We shouldn't be looking for a non-existent modifier. */
2269 gcc_unreachable ();
2272 /* Determine the type of HOST_WIDE_INT in the code being compiled for
2273 use in GCC's __asm_fprintf__ custom format attribute. You must
2274 have set dynamic_format_types before calling this function. */
2275 static void
2276 init_dynamic_asm_fprintf_info (void)
2278 static tree hwi;
2280 if (!hwi)
2282 format_length_info *new_asm_fprintf_length_specs;
2283 unsigned int i;
2285 /* Find the underlying type for HOST_WIDE_INT. For the %w
2286 length modifier to work, one must have issued: "typedef
2287 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2288 prior to using that modifier. */
2289 hwi = maybe_get_identifier ("__gcc_host_wide_int__");
2290 gcc_assert (hwi);
2291 hwi = DECL_ORIGINAL_TYPE (identifier_global_value (hwi));
2292 gcc_assert (hwi);
2294 /* Create a new (writable) copy of asm_fprintf_length_specs. */
2295 new_asm_fprintf_length_specs = (format_length_info *)
2296 xmemdup (asm_fprintf_length_specs,
2297 sizeof (asm_fprintf_length_specs),
2298 sizeof (asm_fprintf_length_specs));
2300 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2301 i = find_length_info_modifier_index (new_asm_fprintf_length_specs, 'w');
2302 if (hwi == long_integer_type_node)
2303 new_asm_fprintf_length_specs[i].index = FMT_LEN_l;
2304 else if (hwi == long_long_integer_type_node)
2305 new_asm_fprintf_length_specs[i].index = FMT_LEN_ll;
2306 else
2307 gcc_unreachable ();
2309 /* Assign the new data for use. */
2310 dynamic_format_types[asm_fprintf_format_type].length_char_specs =
2311 new_asm_fprintf_length_specs;
2315 /* Determine the types of "tree" and "location_t" in the code being
2316 compiled for use in GCC's diagnostic custom format attributes. You
2317 must have set dynamic_format_types before calling this function. */
2318 static void
2319 init_dynamic_diag_info (void)
2321 static tree t, loc, hwi;
2323 if (!loc || !t || !hwi)
2325 static format_char_info *diag_fci, *cdiag_fci, *cxxdiag_fci;
2326 static format_length_info *diag_ls;
2327 unsigned int i;
2329 /* For the GCC-diagnostics custom format specifiers to work, one
2330 must have declared 'tree' and/or 'location_t' prior to using
2331 those attributes. If we haven't seen these declarations then
2332 you shouldn't use the specifiers requiring these types.
2333 However we don't force a hard ICE because we may see only one
2334 or the other type. */
2335 if ((loc = maybe_get_identifier ("location_t")))
2336 loc = TREE_TYPE (identifier_global_value (loc));
2338 /* We need to grab the underlying 'union tree_node' so peek into
2339 an extra type level. */
2340 if ((t = maybe_get_identifier ("tree")))
2341 t = TREE_TYPE (TREE_TYPE (identifier_global_value (t)));
2343 /* Find the underlying type for HOST_WIDE_INT. For the %w
2344 length modifier to work, one must have issued: "typedef
2345 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2346 prior to using that modifier. */
2347 if ((hwi = maybe_get_identifier ("__gcc_host_wide_int__")))
2348 hwi = DECL_ORIGINAL_TYPE (identifier_global_value (hwi));
2350 /* Assign the new data for use. */
2352 /* All the GCC diag formats use the same length specs. */
2353 if (!diag_ls)
2354 dynamic_format_types[gcc_diag_format_type].length_char_specs =
2355 dynamic_format_types[gcc_cdiag_format_type].length_char_specs =
2356 dynamic_format_types[gcc_cxxdiag_format_type].length_char_specs =
2357 diag_ls = (format_length_info *)
2358 xmemdup (gcc_diag_length_specs,
2359 sizeof (gcc_diag_length_specs),
2360 sizeof (gcc_diag_length_specs));
2361 if (hwi)
2363 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2364 i = find_length_info_modifier_index (diag_ls, 'w');
2365 if (hwi == long_integer_type_node)
2366 diag_ls[i].index = FMT_LEN_l;
2367 else if (hwi == long_long_integer_type_node)
2368 diag_ls[i].index = FMT_LEN_ll;
2369 else
2370 gcc_unreachable ();
2373 /* Handle the __gcc_diag__ format specifics. */
2374 if (!diag_fci)
2375 dynamic_format_types[gcc_diag_format_type].conversion_specs =
2376 diag_fci = (format_char_info *)
2377 xmemdup (gcc_diag_char_table,
2378 sizeof (gcc_diag_char_table),
2379 sizeof (gcc_diag_char_table));
2380 if (loc)
2382 i = find_char_info_specifier_index (diag_fci, 'H');
2383 diag_fci[i].types[0].type = &loc;
2384 diag_fci[i].pointer_count = 1;
2386 if (t)
2388 i = find_char_info_specifier_index (diag_fci, 'J');
2389 diag_fci[i].types[0].type = &t;
2390 diag_fci[i].pointer_count = 1;
2393 /* Handle the __gcc_cdiag__ format specifics. */
2394 if (!cdiag_fci)
2395 dynamic_format_types[gcc_cdiag_format_type].conversion_specs =
2396 cdiag_fci = (format_char_info *)
2397 xmemdup (gcc_cdiag_char_table,
2398 sizeof (gcc_cdiag_char_table),
2399 sizeof (gcc_cdiag_char_table));
2400 if (loc)
2402 i = find_char_info_specifier_index (cdiag_fci, 'H');
2403 cdiag_fci[i].types[0].type = &loc;
2404 cdiag_fci[i].pointer_count = 1;
2406 if (t)
2408 /* All specifiers taking a tree share the same struct. */
2409 i = find_char_info_specifier_index (cdiag_fci, 'D');
2410 cdiag_fci[i].types[0].type = &t;
2411 cdiag_fci[i].pointer_count = 1;
2412 i = find_char_info_specifier_index (cdiag_fci, 'J');
2413 cdiag_fci[i].types[0].type = &t;
2414 cdiag_fci[i].pointer_count = 1;
2417 /* Handle the __gcc_cxxdiag__ format specifics. */
2418 if (!cxxdiag_fci)
2419 dynamic_format_types[gcc_cxxdiag_format_type].conversion_specs =
2420 cxxdiag_fci = (format_char_info *)
2421 xmemdup (gcc_cxxdiag_char_table,
2422 sizeof (gcc_cxxdiag_char_table),
2423 sizeof (gcc_cxxdiag_char_table));
2424 if (loc)
2426 i = find_char_info_specifier_index (cxxdiag_fci, 'H');
2427 cxxdiag_fci[i].types[0].type = &loc;
2428 cxxdiag_fci[i].pointer_count = 1;
2430 if (t)
2432 /* All specifiers taking a tree share the same struct. */
2433 i = find_char_info_specifier_index (cxxdiag_fci, 'D');
2434 cxxdiag_fci[i].types[0].type = &t;
2435 cxxdiag_fci[i].pointer_count = 1;
2436 i = find_char_info_specifier_index (cxxdiag_fci, 'J');
2437 cxxdiag_fci[i].types[0].type = &t;
2438 cxxdiag_fci[i].pointer_count = 1;
2443 #ifdef TARGET_FORMAT_TYPES
2444 extern const format_kind_info TARGET_FORMAT_TYPES[];
2445 #endif
2447 /* Handle a "format" attribute; arguments as in
2448 struct attribute_spec.handler. */
2449 tree
2450 handle_format_attribute (tree *node, tree ARG_UNUSED (name), tree args,
2451 int flags, bool *no_add_attrs)
2453 tree type = *node;
2454 function_format_info info;
2455 tree argument;
2457 #ifdef TARGET_FORMAT_TYPES
2458 /* If the target provides additional format types, we need to
2459 add them to FORMAT_TYPES at first use. */
2460 if (TARGET_FORMAT_TYPES != NULL && !dynamic_format_types)
2462 dynamic_format_types = xmalloc ((n_format_types + TARGET_N_FORMAT_TYPES)
2463 * sizeof (dynamic_format_types[0]));
2464 memcpy (dynamic_format_types, format_types_orig,
2465 sizeof (format_types_orig));
2466 memcpy (&dynamic_format_types[n_format_types], TARGET_FORMAT_TYPES,
2467 TARGET_N_FORMAT_TYPES * sizeof (dynamic_format_types[0]));
2469 format_types = dynamic_format_types;
2470 n_format_types += TARGET_N_FORMAT_TYPES;
2472 #endif
2474 if (!decode_format_attr (args, &info, 0))
2476 *no_add_attrs = true;
2477 return NULL_TREE;
2480 argument = TYPE_ARG_TYPES (type);
2481 if (argument)
2483 if (!check_format_string (argument, info.format_num, flags,
2484 no_add_attrs))
2485 return NULL_TREE;
2487 if (info.first_arg_num != 0)
2489 unsigned HOST_WIDE_INT arg_num = 1;
2491 /* Verify that first_arg_num points to the last arg,
2492 the ... */
2493 while (argument)
2494 arg_num++, argument = TREE_CHAIN (argument);
2496 if (arg_num != info.first_arg_num)
2498 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
2499 error ("args to be formatted is not '...'");
2500 *no_add_attrs = true;
2501 return NULL_TREE;
2506 if (info.format_type == strftime_format_type && info.first_arg_num != 0)
2508 error ("strftime formats cannot format arguments");
2509 *no_add_attrs = true;
2510 return NULL_TREE;
2513 /* If this is a custom GCC-internal format type, we have to
2514 initialize certain bits a runtime. */
2515 if (info.format_type == asm_fprintf_format_type
2516 || info.format_type == gcc_diag_format_type
2517 || info.format_type == gcc_cdiag_format_type
2518 || info.format_type == gcc_cxxdiag_format_type)
2520 /* Our first time through, we have to make sure that our
2521 format_type data is allocated dynamically and is modifiable. */
2522 if (!dynamic_format_types)
2523 format_types = dynamic_format_types = (format_kind_info *)
2524 xmemdup (format_types_orig, sizeof (format_types_orig),
2525 sizeof (format_types_orig));
2527 /* If this is format __asm_fprintf__, we have to initialize
2528 GCC's notion of HOST_WIDE_INT for checking %wd. */
2529 if (info.format_type == asm_fprintf_format_type)
2530 init_dynamic_asm_fprintf_info ();
2531 /* If this is one of the diagnostic attributes, then we have to
2532 initialize 'location_t' and 'tree' at runtime. */
2533 else if (info.format_type == gcc_diag_format_type
2534 || info.format_type == gcc_cdiag_format_type
2535 || info.format_type == gcc_cxxdiag_format_type)
2536 init_dynamic_diag_info ();
2537 else
2538 gcc_unreachable ();
2541 return NULL_TREE;