Check for Altivec mode when returning altivec register.
[official-gcc.git] / gcc / c-format.c
blob019b91a733952305f5c1100b99b2487002fd6412
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 if (validated_p)
164 abort ();
165 return false;
168 *value = TREE_INT_CST_LOW (expr);
170 return true;
173 /* Decode the arguments to a "format" attribute into a function_format_info
174 structure. It is already known that the list is of the right length.
175 If VALIDATED_P is true, then these attributes have already been validated
176 and this function will abort if they are erroneous; if false, it
177 will give an error message. Returns true if the attributes are
178 successfully decoded, false otherwise. */
180 static bool
181 decode_format_attr (tree args, function_format_info *info, int validated_p)
183 tree format_type_id = TREE_VALUE (args);
184 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
185 tree first_arg_num_expr
186 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
188 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
190 if (validated_p)
191 abort ();
192 error ("unrecognized format specifier");
193 return false;
195 else
197 const char *p = IDENTIFIER_POINTER (format_type_id);
199 info->format_type = decode_format_type (p);
201 if (info->format_type == format_type_error)
203 if (validated_p)
204 abort ();
205 warning ("%qs is an unrecognized format function type", p);
206 return false;
210 if (!get_constant (format_num_expr, &info->format_num, validated_p))
212 error ("format string has invalid operand number");
213 return false;
216 if (!get_constant (first_arg_num_expr, &info->first_arg_num, validated_p))
218 error ("'...' has invalid operand number");
219 return false;
222 if (info->first_arg_num != 0 && info->first_arg_num <= info->format_num)
224 if (validated_p)
225 abort ();
226 error ("format string arg follows the args to be formatted");
227 return false;
230 return true;
233 /* Check a call to a format function against a parameter list. */
235 /* The C standard version C++ is treated as equivalent to
236 or inheriting from, for the purpose of format features supported. */
237 #define CPLUSPLUS_STD_VER STD_C94
238 /* The C standard version we are checking formats against when pedantic. */
239 #define C_STD_VER ((int)(c_dialect_cxx () \
240 ? CPLUSPLUS_STD_VER \
241 : (flag_isoc99 \
242 ? STD_C99 \
243 : (flag_isoc94 ? STD_C94 : STD_C89))))
244 /* The name to give to the standard version we are warning about when
245 pedantic. FEATURE_VER is the version in which the feature warned out
246 appeared, which is higher than C_STD_VER. */
247 #define C_STD_NAME(FEATURE_VER) (c_dialect_cxx () \
248 ? "ISO C++" \
249 : ((FEATURE_VER) == STD_EXT \
250 ? "ISO C" \
251 : "ISO C90"))
252 /* Adjust a C standard version, which may be STD_C9L, to account for
253 -Wno-long-long. Returns other standard versions unchanged. */
254 #define ADJ_STD(VER) ((int)((VER) == STD_C9L \
255 ? (warn_long_long ? STD_C99 : STD_C89) \
256 : (VER)))
258 /* Structure describing details of a type expected in format checking,
259 and the type to check against it. */
260 typedef struct format_wanted_type
262 /* The type wanted. */
263 tree wanted_type;
264 /* The name of this type to use in diagnostics. */
265 const char *wanted_type_name;
266 /* The level of indirection through pointers at which this type occurs. */
267 int pointer_count;
268 /* Whether, when pointer_count is 1, to allow any character type when
269 pedantic, rather than just the character or void type specified. */
270 int char_lenient_flag;
271 /* Whether the argument, dereferenced once, is written into and so the
272 argument must not be a pointer to a const-qualified type. */
273 int writing_in_flag;
274 /* Whether the argument, dereferenced once, is read from and so
275 must not be a NULL pointer. */
276 int reading_from_flag;
277 /* If warnings should be of the form "field precision should have
278 type 'int'", the name to use (in this case "field precision"),
279 otherwise NULL, for "format expects type 'long'" type
280 messages. */
281 const char *name;
282 /* The actual parameter to check against the wanted type. */
283 tree param;
284 /* The argument number of that parameter. */
285 int arg_num;
286 /* The next type to check for this format conversion, or NULL if none. */
287 struct format_wanted_type *next;
288 } format_wanted_type;
291 static const format_length_info printf_length_specs[] =
293 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
294 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L },
295 { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
296 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
297 { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
298 { "Z", FMT_LEN_z, STD_EXT, NULL, 0, 0 },
299 { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
300 { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
301 { NULL, 0, 0, NULL, 0, 0 }
304 /* Length specifiers valid for asm_fprintf. */
305 static const format_length_info asm_fprintf_length_specs[] =
307 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89 },
308 { "w", FMT_LEN_none, STD_C89, NULL, 0, 0 },
309 { NULL, 0, 0, NULL, 0, 0 }
312 /* Length specifiers valid for GCC diagnostics. */
313 static const format_length_info gcc_diag_length_specs[] =
315 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89 },
316 { "w", FMT_LEN_none, STD_C89, NULL, 0, 0 },
317 { NULL, 0, 0, NULL, 0, 0 }
320 /* The custom diagnostics all accept the same length specifiers. */
321 #define gcc_cdiag_length_specs gcc_diag_length_specs
322 #define gcc_cxxdiag_length_specs gcc_diag_length_specs
324 /* This differs from printf_length_specs only in that "Z" is not accepted. */
325 static const format_length_info scanf_length_specs[] =
327 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
328 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L },
329 { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
330 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
331 { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
332 { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
333 { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
334 { NULL, 0, 0, NULL, 0, 0 }
338 /* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings
339 make no sense for a format type not part of any C standard version. */
340 static const format_length_info strfmon_length_specs[] =
342 /* A GNU extension. */
343 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
344 { NULL, 0, 0, NULL, 0, 0 }
347 static const format_flag_spec printf_flag_specs[] =
349 { ' ', 0, 0, N_("` ' flag"), N_("the ` ' printf flag"), STD_C89 },
350 { '+', 0, 0, N_("`+' flag"), N_("the `+' printf flag"), STD_C89 },
351 { '#', 0, 0, N_("`#' flag"), N_("the `#' printf flag"), STD_C89 },
352 { '0', 0, 0, N_("`0' flag"), N_("the `0' printf flag"), STD_C89 },
353 { '-', 0, 0, N_("`-' flag"), N_("the `-' printf flag"), STD_C89 },
354 { '\'', 0, 0, N_("`'' flag"), N_("the `'' printf flag"), STD_EXT },
355 { 'I', 0, 0, N_("`I' flag"), N_("the `I' printf flag"), STD_EXT },
356 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
357 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
358 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
359 { 0, 0, 0, NULL, NULL, 0 }
363 static const format_flag_pair printf_flag_pairs[] =
365 { ' ', '+', 1, 0 },
366 { '0', '-', 1, 0 },
367 { '0', 'p', 1, 'i' },
368 { 0, 0, 0, 0 }
371 static const format_flag_spec asm_fprintf_flag_specs[] =
373 { ' ', 0, 0, N_("` ' flag"), N_("the ` ' printf flag"), STD_C89 },
374 { '+', 0, 0, N_("`+' flag"), N_("the `+' printf flag"), STD_C89 },
375 { '#', 0, 0, N_("`#' flag"), N_("the `#' printf flag"), STD_C89 },
376 { '0', 0, 0, N_("`0' flag"), N_("the `0' printf flag"), STD_C89 },
377 { '-', 0, 0, N_("`-' flag"), N_("the `-' printf flag"), STD_C89 },
378 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
379 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
380 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
381 { 0, 0, 0, NULL, NULL, 0 }
384 static const format_flag_pair asm_fprintf_flag_pairs[] =
386 { ' ', '+', 1, 0 },
387 { '0', '-', 1, 0 },
388 { '0', 'p', 1, 'i' },
389 { 0, 0, 0, 0 }
392 static const format_flag_pair gcc_diag_flag_pairs[] =
394 { 0, 0, 0, 0 }
397 #define gcc_cdiag_flag_pairs gcc_diag_flag_pairs
398 #define gcc_cxxdiag_flag_pairs gcc_diag_flag_pairs
400 static const format_flag_spec gcc_diag_flag_specs[] =
402 { 'q', 0, 0, N_("`q' flag"), N_("the `q' diagnostic flag"), STD_C89 },
403 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
404 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
405 { 0, 0, 0, NULL, NULL, 0 }
408 #define gcc_cdiag_flag_specs gcc_diag_flag_specs
410 static const format_flag_spec gcc_cxxdiag_flag_specs[] =
412 { '+', 0, 0, N_("`+' flag"), N_("the `+' printf flag"), STD_C89 },
413 { '#', 0, 0, N_("`#' flag"), N_("the `#' printf flag"), STD_C89 },
414 { 'q', 0, 0, N_("`q' flag"), N_("the `q' diagnostic flag"), STD_C89 },
415 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
416 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
417 { 0, 0, 0, NULL, NULL, 0 }
420 static const format_flag_spec scanf_flag_specs[] =
422 { '*', 0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89 },
423 { 'a', 0, 0, N_("`a' flag"), N_("the `a' scanf flag"), STD_EXT },
424 { 'w', 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89 },
425 { 'L', 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89 },
426 { '\'', 0, 0, N_("`'' flag"), N_("the `'' scanf flag"), STD_EXT },
427 { 'I', 0, 0, N_("`I' flag"), N_("the `I' scanf flag"), STD_EXT },
428 { 0, 0, 0, NULL, NULL, 0 }
432 static const format_flag_pair scanf_flag_pairs[] =
434 { '*', 'L', 0, 0 },
435 { 0, 0, 0, 0 }
439 static const format_flag_spec strftime_flag_specs[] =
441 { '_', 0, 0, N_("`_' flag"), N_("the `_' strftime flag"), STD_EXT },
442 { '-', 0, 0, N_("`-' flag"), N_("the `-' strftime flag"), STD_EXT },
443 { '0', 0, 0, N_("`0' flag"), N_("the `0' strftime flag"), STD_EXT },
444 { '^', 0, 0, N_("`^' flag"), N_("the `^' strftime flag"), STD_EXT },
445 { '#', 0, 0, N_("`#' flag"), N_("the `#' strftime flag"), STD_EXT },
446 { 'w', 0, 0, N_("field width"), N_("field width in strftime format"), STD_EXT },
447 { 'E', 0, 0, N_("`E' modifier"), N_("the `E' strftime modifier"), STD_C99 },
448 { 'O', 0, 0, N_("`O' modifier"), N_("the `O' strftime modifier"), STD_C99 },
449 { 'O', 'o', 0, NULL, N_("the `O' modifier"), STD_EXT },
450 { 0, 0, 0, NULL, NULL, 0 }
454 static const format_flag_pair strftime_flag_pairs[] =
456 { 'E', 'O', 0, 0 },
457 { '_', '-', 0, 0 },
458 { '_', '0', 0, 0 },
459 { '-', '0', 0, 0 },
460 { '^', '#', 0, 0 },
461 { 0, 0, 0, 0 }
465 static const format_flag_spec strfmon_flag_specs[] =
467 { '=', 0, 1, N_("fill character"), N_("fill character in strfmon format"), STD_C89 },
468 { '^', 0, 0, N_("`^' flag"), N_("the `^' strfmon flag"), STD_C89 },
469 { '+', 0, 0, N_("`+' flag"), N_("the `+' strfmon flag"), STD_C89 },
470 { '(', 0, 0, N_("`(' flag"), N_("the `(' strfmon flag"), STD_C89 },
471 { '!', 0, 0, N_("`!' flag"), N_("the `!' strfmon flag"), STD_C89 },
472 { '-', 0, 0, N_("`-' flag"), N_("the `-' strfmon flag"), STD_C89 },
473 { 'w', 0, 0, N_("field width"), N_("field width in strfmon format"), STD_C89 },
474 { '#', 0, 0, N_("left precision"), N_("left precision in strfmon format"), STD_C89 },
475 { 'p', 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89 },
476 { 'L', 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89 },
477 { 0, 0, 0, NULL, NULL, 0 }
480 static const format_flag_pair strfmon_flag_pairs[] =
482 { '+', '(', 0, 0 },
483 { 0, 0, 0, 0 }
487 static const format_char_info print_char_table[] =
489 /* C89 conversion specifiers. */
490 { "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 },
491 { "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 },
492 { "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 },
493 { "fgG", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#'I", "", NULL },
494 { "eE", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#I", "", NULL },
495 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, T94_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
496 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
497 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "c", NULL },
498 { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, BADLEN, T99_SST, T99_PD, T99_IM }, "", "W", NULL },
499 /* C99 conversion specifiers. */
500 { "F", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#'I", "", NULL },
501 { "aA", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "", NULL },
502 /* X/Open conversion specifiers. */
503 { "C", 0, STD_EXT, { TEX_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
504 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "R", NULL },
505 /* GNU conversion specifiers. */
506 { "m", 0, STD_EXT, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "", NULL },
507 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
510 static const format_char_info asm_fprintf_char_table[] =
512 /* C89 conversion specifiers. */
513 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +", "i", NULL },
514 { "oxX", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0#", "i", NULL },
515 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0", "i", NULL },
516 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
517 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
519 /* asm_fprintf conversion specifiers. */
520 { "O", 0, STD_C89, NOARGUMENTS, "", "", NULL },
521 { "R", 0, STD_C89, NOARGUMENTS, "", "", NULL },
522 { "I", 0, STD_C89, NOARGUMENTS, "", "", NULL },
523 { "L", 0, STD_C89, NOARGUMENTS, "", "", NULL },
524 { "U", 0, STD_C89, NOARGUMENTS, "", "", NULL },
525 { "r", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
526 { "@", 0, STD_C89, NOARGUMENTS, "", "", NULL },
527 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
530 static const format_char_info gcc_diag_char_table[] =
532 /* C89 conversion specifiers. */
533 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
534 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
535 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
536 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
537 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
538 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
540 /* Custom conversion specifiers. */
542 /* %H will require "location_t" at runtime. */
543 { "H", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
545 /* These will require a "tree" at runtime. */
546 { "J", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
548 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
549 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
550 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
553 static const format_char_info gcc_cdiag_char_table[] =
555 /* C89 conversion specifiers. */
556 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
557 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
558 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
559 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
560 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
561 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
563 /* Custom conversion specifiers. */
565 /* %H will require "location_t" at runtime. */
566 { "H", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
568 /* These will require a "tree" at runtime. */
569 { "DEFJT", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
571 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
572 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
573 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
576 static const format_char_info gcc_cxxdiag_char_table[] =
578 /* C89 conversion specifiers. */
579 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
580 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
581 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
582 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
583 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
584 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
586 /* Custom conversion specifiers. */
588 /* %H will require "location_t" at runtime. */
589 { "H", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
591 /* These will require a "tree" at runtime. */
592 { "ADEFJTV",0,STD_C89,{ T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+#", "", NULL },
594 /* These accept either an `int' or an `enum tree_code' (which is handled as an `int'.) */
595 { "CLOPQ",0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
597 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
598 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
599 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
602 static const format_char_info scan_char_table[] =
604 /* C89 conversion specifiers. */
605 { "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 },
606 { "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 },
607 { "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 },
608 { "efgEG", 1, STD_C89, { T89_F, BADLEN, BADLEN, T89_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "*w'", "W", NULL },
609 { "c", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "cW", NULL },
610 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "cW", NULL },
611 { "[", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "cW[", NULL },
612 { "p", 2, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
613 { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, BADLEN, T99_SST, T99_PD, T99_IM }, "", "W", NULL },
614 /* C99 conversion specifiers. */
615 { "FaA", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "*w'", "W", NULL },
616 /* X/Open conversion specifiers. */
617 { "C", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
618 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "W", NULL },
619 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
622 static const format_char_info time_char_table[] =
624 /* C89 conversion specifiers. */
625 { "ABZab", 0, STD_C89, NOLENGTHS, "^#", "", NULL },
626 { "cx", 0, STD_C89, NOLENGTHS, "E", "3", NULL },
627 { "HIMSUWdmw", 0, STD_C89, NOLENGTHS, "-_0Ow", "", NULL },
628 { "j", 0, STD_C89, NOLENGTHS, "-_0Ow", "o", NULL },
629 { "p", 0, STD_C89, NOLENGTHS, "#", "", NULL },
630 { "X", 0, STD_C89, NOLENGTHS, "E", "", NULL },
631 { "y", 0, STD_C89, NOLENGTHS, "EO-_0w", "4", NULL },
632 { "Y", 0, STD_C89, NOLENGTHS, "-_0EOw", "o", NULL },
633 { "%", 0, STD_C89, NOLENGTHS, "", "", NULL },
634 /* C99 conversion specifiers. */
635 { "C", 0, STD_C99, NOLENGTHS, "-_0EOw", "o", NULL },
636 { "D", 0, STD_C99, NOLENGTHS, "", "2", NULL },
637 { "eVu", 0, STD_C99, NOLENGTHS, "-_0Ow", "", NULL },
638 { "FRTnrt", 0, STD_C99, NOLENGTHS, "", "", NULL },
639 { "g", 0, STD_C99, NOLENGTHS, "O-_0w", "2o", NULL },
640 { "G", 0, STD_C99, NOLENGTHS, "-_0Ow", "o", NULL },
641 { "h", 0, STD_C99, NOLENGTHS, "^#", "", NULL },
642 { "z", 0, STD_C99, NOLENGTHS, "O", "o", NULL },
643 /* GNU conversion specifiers. */
644 { "kls", 0, STD_EXT, NOLENGTHS, "-_0Ow", "", NULL },
645 { "P", 0, STD_EXT, NOLENGTHS, "", "", NULL },
646 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
649 static const format_char_info monetary_char_table[] =
651 { "in", 0, STD_C89, { T89_D, BADLEN, BADLEN, BADLEN, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "=^+(!-w#p", "", NULL },
652 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
655 /* This must be in the same order as enum format_type. */
656 static const format_kind_info format_types_orig[] =
658 { "printf", printf_length_specs, print_char_table, " +#0-'I", NULL,
659 printf_flag_specs, printf_flag_pairs,
660 FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_EMPTY_PREC_OK,
661 'w', 0, 'p', 0, 'L',
662 &integer_type_node, &integer_type_node
664 { "asm_fprintf", asm_fprintf_length_specs, asm_fprintf_char_table, " +#0-", NULL,
665 asm_fprintf_flag_specs, asm_fprintf_flag_pairs,
666 FMT_FLAG_ARG_CONVERT|FMT_FLAG_EMPTY_PREC_OK,
667 'w', 0, 'p', 0, 'L',
668 NULL, NULL
670 { "gcc_diag", gcc_diag_length_specs, gcc_diag_char_table, "q", NULL,
671 gcc_diag_flag_specs, gcc_diag_flag_pairs,
672 FMT_FLAG_ARG_CONVERT,
673 0, 0, 'p', 0, 'L',
674 NULL, &integer_type_node
676 { "gcc_cdiag", gcc_cdiag_length_specs, gcc_cdiag_char_table, "q", NULL,
677 gcc_cdiag_flag_specs, gcc_cdiag_flag_pairs,
678 FMT_FLAG_ARG_CONVERT,
679 0, 0, 'p', 0, 'L',
680 NULL, &integer_type_node
682 { "gcc_cxxdiag", gcc_cxxdiag_length_specs, gcc_cxxdiag_char_table, "q+#", NULL,
683 gcc_cxxdiag_flag_specs, gcc_cxxdiag_flag_pairs,
684 FMT_FLAG_ARG_CONVERT,
685 0, 0, 'p', 0, 'L',
686 NULL, &integer_type_node
688 { "scanf", scanf_length_specs, scan_char_table, "*'I", NULL,
689 scanf_flag_specs, scanf_flag_pairs,
690 FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_ZERO_WIDTH_BAD|FMT_FLAG_DOLLAR_GAP_POINTER_OK,
691 'w', 0, 0, '*', 'L',
692 NULL, NULL
694 { "strftime", NULL, time_char_table, "_-0^#", "EO",
695 strftime_flag_specs, strftime_flag_pairs,
696 FMT_FLAG_FANCY_PERCENT_OK, 'w', 0, 0, 0, 0,
697 NULL, NULL
699 { "strfmon", strfmon_length_specs, monetary_char_table, "=^+(!-", NULL,
700 strfmon_flag_specs, strfmon_flag_pairs,
701 FMT_FLAG_ARG_CONVERT, 'w', '#', 'p', 0, 'L',
702 NULL, NULL
706 /* This layer of indirection allows GCC to reassign format_types with
707 new data if necessary, while still allowing the original data to be
708 const. */
709 static const format_kind_info *format_types = format_types_orig;
710 /* We can modify this one. We also add target-specific format types
711 to the end of the array. */
712 static format_kind_info *dynamic_format_types;
714 static int n_format_types = ARRAY_SIZE (format_types_orig);
716 /* Structure detailing the results of checking a format function call
717 where the format expression may be a conditional expression with
718 many leaves resulting from nested conditional expressions. */
719 typedef struct
721 /* Number of leaves of the format argument that could not be checked
722 as they were not string literals. */
723 int number_non_literal;
724 /* Number of leaves of the format argument that were null pointers or
725 string literals, but had extra format arguments. */
726 int number_extra_args;
727 /* Number of leaves of the format argument that were null pointers or
728 string literals, but had extra format arguments and used $ operand
729 numbers. */
730 int number_dollar_extra_args;
731 /* Number of leaves of the format argument that were wide string
732 literals. */
733 int number_wide;
734 /* Number of leaves of the format argument that were empty strings. */
735 int number_empty;
736 /* Number of leaves of the format argument that were unterminated
737 strings. */
738 int number_unterminated;
739 /* Number of leaves of the format argument that were not counted above. */
740 int number_other;
741 } format_check_results;
743 typedef struct
745 format_check_results *res;
746 function_format_info *info;
747 tree params;
748 } format_check_context;
750 static void check_format_info (function_format_info *, tree);
751 static void check_format_arg (void *, tree, unsigned HOST_WIDE_INT);
752 static void check_format_info_main (format_check_results *,
753 function_format_info *,
754 const char *, int, tree,
755 unsigned HOST_WIDE_INT);
757 static void init_dollar_format_checking (int, tree);
758 static int maybe_read_dollar_number (const char **, int,
759 tree, tree *, const format_kind_info *);
760 static bool avoid_dollar_number (const char *);
761 static void finish_dollar_format_checking (format_check_results *, int);
763 static const format_flag_spec *get_flag_spec (const format_flag_spec *,
764 int, const char *);
766 static void check_format_types (format_wanted_type *, const char *, int);
767 static void format_type_warning (const char *, const char *, int, tree,
768 int, const char *, tree, int);
770 /* Decode a format type from a string, returning the type, or
771 format_type_error if not valid, in which case the caller should print an
772 error message. */
773 static int
774 decode_format_type (const char *s)
776 int i;
777 int slen;
778 slen = strlen (s);
779 for (i = 0; i < n_format_types; i++)
781 int alen;
782 if (!strcmp (s, format_types[i].name))
783 return i;
784 alen = strlen (format_types[i].name);
785 if (slen == alen + 4 && s[0] == '_' && s[1] == '_'
786 && s[slen - 1] == '_' && s[slen - 2] == '_'
787 && !strncmp (s + 2, format_types[i].name, alen))
788 return i;
790 return format_type_error;
794 /* Check the argument list of a call to printf, scanf, etc.
795 ATTRS are the attributes on the function type.
796 PARAMS is the list of argument values. Also, if -Wmissing-format-attribute,
797 warn for calls to vprintf or vscanf in functions with no such format
798 attribute themselves. */
800 void
801 check_function_format (tree attrs, tree params)
803 tree a;
805 /* See if this function has any format attributes. */
806 for (a = attrs; a; a = TREE_CHAIN (a))
808 if (is_attribute_p ("format", TREE_PURPOSE (a)))
810 /* Yup; check it. */
811 function_format_info info;
812 decode_format_attr (TREE_VALUE (a), &info, 1);
813 check_format_info (&info, params);
814 if (warn_missing_format_attribute && info.first_arg_num == 0
815 && (format_types[info.format_type].flags
816 & (int) FMT_FLAG_ARG_CONVERT))
818 tree c;
819 for (c = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
821 c = TREE_CHAIN (c))
822 if (is_attribute_p ("format", TREE_PURPOSE (c))
823 && (decode_format_type (IDENTIFIER_POINTER
824 (TREE_VALUE (TREE_VALUE (c))))
825 == info.format_type))
826 break;
827 if (c == NULL_TREE)
829 /* Check if the current function has a parameter to which
830 the format attribute could be attached; if not, it
831 can't be a candidate for a format attribute, despite
832 the vprintf-like or vscanf-like call. */
833 tree args;
834 for (args = DECL_ARGUMENTS (current_function_decl);
835 args != 0;
836 args = TREE_CHAIN (args))
838 if (TREE_CODE (TREE_TYPE (args)) == POINTER_TYPE
839 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args)))
840 == char_type_node))
841 break;
843 if (args != 0)
844 warning ("function might be possible candidate for %qs format attribute",
845 format_types[info.format_type].name);
853 /* Variables used by the checking of $ operand number formats. */
854 static char *dollar_arguments_used = NULL;
855 static char *dollar_arguments_pointer_p = NULL;
856 static int dollar_arguments_alloc = 0;
857 static int dollar_arguments_count;
858 static int dollar_first_arg_num;
859 static int dollar_max_arg_used;
860 static int dollar_format_warned;
862 /* Initialize the checking for a format string that may contain $
863 parameter number specifications; we will need to keep track of whether
864 each parameter has been used. FIRST_ARG_NUM is the number of the first
865 argument that is a parameter to the format, or 0 for a vprintf-style
866 function; PARAMS is the list of arguments starting at this argument. */
868 static void
869 init_dollar_format_checking (int first_arg_num, tree params)
871 tree oparams = params;
873 dollar_first_arg_num = first_arg_num;
874 dollar_arguments_count = 0;
875 dollar_max_arg_used = 0;
876 dollar_format_warned = 0;
877 if (first_arg_num > 0)
879 while (params)
881 dollar_arguments_count++;
882 params = TREE_CHAIN (params);
885 if (dollar_arguments_alloc < dollar_arguments_count)
887 if (dollar_arguments_used)
888 free (dollar_arguments_used);
889 if (dollar_arguments_pointer_p)
890 free (dollar_arguments_pointer_p);
891 dollar_arguments_alloc = dollar_arguments_count;
892 dollar_arguments_used = XNEWVEC (char, dollar_arguments_alloc);
893 dollar_arguments_pointer_p = XNEWVEC (char, dollar_arguments_alloc);
895 if (dollar_arguments_alloc)
897 memset (dollar_arguments_used, 0, dollar_arguments_alloc);
898 if (first_arg_num > 0)
900 int i = 0;
901 params = oparams;
902 while (params)
904 dollar_arguments_pointer_p[i] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params)))
905 == POINTER_TYPE);
906 params = TREE_CHAIN (params);
907 i++;
914 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
915 is set, it is an error if one is not found; otherwise, it is OK. If
916 such a number is found, check whether it is within range and mark that
917 numbered operand as being used for later checking. Returns the operand
918 number if found and within range, zero if no such number was found and
919 this is OK, or -1 on error. PARAMS points to the first operand of the
920 format; PARAM_PTR is made to point to the parameter referred to. If
921 a $ format is found, *FORMAT is updated to point just after it. */
923 static int
924 maybe_read_dollar_number (const char **format,
925 int dollar_needed, tree params, tree *param_ptr,
926 const format_kind_info *fki)
928 int argnum;
929 int overflow_flag;
930 const char *fcp = *format;
931 if (! ISDIGIT (*fcp))
933 if (dollar_needed)
935 warning ("missing $ operand number in format");
936 return -1;
938 else
939 return 0;
941 argnum = 0;
942 overflow_flag = 0;
943 while (ISDIGIT (*fcp))
945 int nargnum;
946 nargnum = 10 * argnum + (*fcp - '0');
947 if (nargnum < 0 || nargnum / 10 != argnum)
948 overflow_flag = 1;
949 argnum = nargnum;
950 fcp++;
952 if (*fcp != '$')
954 if (dollar_needed)
956 warning ("missing $ operand number in format");
957 return -1;
959 else
960 return 0;
962 *format = fcp + 1;
963 if (pedantic && !dollar_format_warned)
965 warning ("%s does not support %%n$ operand number formats",
966 C_STD_NAME (STD_EXT));
967 dollar_format_warned = 1;
969 if (overflow_flag || argnum == 0
970 || (dollar_first_arg_num && argnum > dollar_arguments_count))
972 warning ("operand number out of range in format");
973 return -1;
975 if (argnum > dollar_max_arg_used)
976 dollar_max_arg_used = argnum;
977 /* For vprintf-style functions we may need to allocate more memory to
978 track which arguments are used. */
979 while (dollar_arguments_alloc < dollar_max_arg_used)
981 int nalloc;
982 nalloc = 2 * dollar_arguments_alloc + 16;
983 dollar_arguments_used = XRESIZEVEC (char, dollar_arguments_used,
984 nalloc);
985 dollar_arguments_pointer_p = XRESIZEVEC (char, dollar_arguments_pointer_p,
986 nalloc);
987 memset (dollar_arguments_used + dollar_arguments_alloc, 0,
988 nalloc - dollar_arguments_alloc);
989 dollar_arguments_alloc = nalloc;
991 if (!(fki->flags & (int) FMT_FLAG_DOLLAR_MULTIPLE)
992 && dollar_arguments_used[argnum - 1] == 1)
994 dollar_arguments_used[argnum - 1] = 2;
995 warning ("format argument %d used more than once in %s format",
996 argnum, fki->name);
998 else
999 dollar_arguments_used[argnum - 1] = 1;
1000 if (dollar_first_arg_num)
1002 int i;
1003 *param_ptr = params;
1004 for (i = 1; i < argnum && *param_ptr != 0; i++)
1005 *param_ptr = TREE_CHAIN (*param_ptr);
1007 if (*param_ptr == 0)
1009 /* This case shouldn't be caught here. */
1010 abort ();
1013 else
1014 *param_ptr = 0;
1015 return argnum;
1018 /* Ensure that FORMAT does not start with a decimal number followed by
1019 a $; give a diagnostic and return true if it does, false otherwise. */
1021 static bool
1022 avoid_dollar_number (const char *format)
1024 if (!ISDIGIT (*format))
1025 return false;
1026 while (ISDIGIT (*format))
1027 format++;
1028 if (*format == '$')
1030 warning ("$ operand number used after format without operand number");
1031 return true;
1033 return false;
1037 /* Finish the checking for a format string that used $ operand number formats
1038 instead of non-$ formats. We check for unused operands before used ones
1039 (a serious error, since the implementation of the format function
1040 can't know what types to pass to va_arg to find the later arguments).
1041 and for unused operands at the end of the format (if we know how many
1042 arguments the format had, so not for vprintf). If there were operand
1043 numbers out of range on a non-vprintf-style format, we won't have reached
1044 here. If POINTER_GAP_OK, unused arguments are OK if all arguments are
1045 pointers. */
1047 static void
1048 finish_dollar_format_checking (format_check_results *res, int pointer_gap_ok)
1050 int i;
1051 bool found_pointer_gap = false;
1052 for (i = 0; i < dollar_max_arg_used; i++)
1054 if (!dollar_arguments_used[i])
1056 if (pointer_gap_ok && (dollar_first_arg_num == 0
1057 || dollar_arguments_pointer_p[i]))
1058 found_pointer_gap = true;
1059 else
1060 warning ("format argument %d unused before used argument %d in $-style format",
1061 i + 1, dollar_max_arg_used);
1064 if (found_pointer_gap
1065 || (dollar_first_arg_num
1066 && dollar_max_arg_used < dollar_arguments_count))
1068 res->number_other--;
1069 res->number_dollar_extra_args++;
1074 /* Retrieve the specification for a format flag. SPEC contains the
1075 specifications for format flags for the applicable kind of format.
1076 FLAG is the flag in question. If PREDICATES is NULL, the basic
1077 spec for that flag must be retrieved and this function aborts if
1078 it cannot be found. If PREDICATES is not NULL, it is a string listing
1079 possible predicates for the spec entry; if an entry predicated on any
1080 of these is found, it is returned, otherwise NULL is returned. */
1082 static const format_flag_spec *
1083 get_flag_spec (const format_flag_spec *spec, int flag, const char *predicates)
1085 int i;
1086 for (i = 0; spec[i].flag_char != 0; i++)
1088 if (spec[i].flag_char != flag)
1089 continue;
1090 if (predicates != NULL)
1092 if (spec[i].predicate != 0
1093 && strchr (predicates, spec[i].predicate) != 0)
1094 return &spec[i];
1096 else if (spec[i].predicate == 0)
1097 return &spec[i];
1099 if (predicates == NULL)
1100 abort ();
1101 else
1102 return NULL;
1106 /* Check the argument list of a call to printf, scanf, etc.
1107 INFO points to the function_format_info structure.
1108 PARAMS is the list of argument values. */
1110 static void
1111 check_format_info (function_format_info *info, tree params)
1113 format_check_context format_ctx;
1114 unsigned HOST_WIDE_INT arg_num;
1115 tree format_tree;
1116 format_check_results res;
1117 /* Skip to format argument. If the argument isn't available, there's
1118 no work for us to do; prototype checking will catch the problem. */
1119 for (arg_num = 1; ; ++arg_num)
1121 if (params == 0)
1122 return;
1123 if (arg_num == info->format_num)
1124 break;
1125 params = TREE_CHAIN (params);
1127 format_tree = TREE_VALUE (params);
1128 params = TREE_CHAIN (params);
1129 if (format_tree == 0)
1130 return;
1132 res.number_non_literal = 0;
1133 res.number_extra_args = 0;
1134 res.number_dollar_extra_args = 0;
1135 res.number_wide = 0;
1136 res.number_empty = 0;
1137 res.number_unterminated = 0;
1138 res.number_other = 0;
1140 format_ctx.res = &res;
1141 format_ctx.info = info;
1142 format_ctx.params = params;
1144 check_function_arguments_recurse (check_format_arg, &format_ctx,
1145 format_tree, arg_num);
1147 if (res.number_non_literal > 0)
1149 /* Functions taking a va_list normally pass a non-literal format
1150 string. These functions typically are declared with
1151 first_arg_num == 0, so avoid warning in those cases. */
1152 if (!(format_types[info->format_type].flags & (int) FMT_FLAG_ARG_CONVERT))
1154 /* For strftime-like formats, warn for not checking the format
1155 string; but there are no arguments to check. */
1156 if (warn_format_nonliteral)
1157 warning ("format not a string literal, format string not checked");
1159 else if (info->first_arg_num != 0)
1161 /* If there are no arguments for the format at all, we may have
1162 printf (foo) which is likely to be a security hole. */
1163 while (arg_num + 1 < info->first_arg_num)
1165 if (params == 0)
1166 break;
1167 params = TREE_CHAIN (params);
1168 ++arg_num;
1170 if (params == 0 && (warn_format_nonliteral || warn_format_security))
1171 warning ("format not a string literal and no format arguments");
1172 else if (warn_format_nonliteral)
1173 warning ("format not a string literal, argument types not checked");
1177 /* If there were extra arguments to the format, normally warn. However,
1178 the standard does say extra arguments are ignored, so in the specific
1179 case where we have multiple leaves (conditional expressions or
1180 ngettext) allow extra arguments if at least one leaf didn't have extra
1181 arguments, but was otherwise OK (either non-literal or checked OK).
1182 If the format is an empty string, this should be counted similarly to the
1183 case of extra format arguments. */
1184 if (res.number_extra_args > 0 && res.number_non_literal == 0
1185 && res.number_other == 0 && warn_format_extra_args)
1186 warning ("too many arguments for format");
1187 if (res.number_dollar_extra_args > 0 && res.number_non_literal == 0
1188 && res.number_other == 0 && warn_format_extra_args)
1189 warning ("unused arguments in $-style format");
1190 if (res.number_empty > 0 && res.number_non_literal == 0
1191 && res.number_other == 0 && warn_format_zero_length)
1192 warning ("zero-length %s format string",
1193 format_types[info->format_type].name);
1195 if (res.number_wide > 0)
1196 warning ("format is a wide character string");
1198 if (res.number_unterminated > 0)
1199 warning ("unterminated format string");
1202 /* Callback from check_function_arguments_recurse to check a
1203 format string. FORMAT_TREE is the format parameter. ARG_NUM
1204 is the number of the format argument. CTX points to a
1205 format_check_context. */
1207 static void
1208 check_format_arg (void *ctx, tree format_tree,
1209 unsigned HOST_WIDE_INT arg_num)
1211 format_check_context *format_ctx = (format_check_context *) ctx;
1212 format_check_results *res = format_ctx->res;
1213 function_format_info *info = format_ctx->info;
1214 tree params = format_ctx->params;
1216 int format_length;
1217 HOST_WIDE_INT offset;
1218 const char *format_chars;
1219 tree array_size = 0;
1220 tree array_init;
1222 if (integer_zerop (format_tree))
1224 /* Skip to first argument to check, so we can see if this format
1225 has any arguments (it shouldn't). */
1226 while (arg_num + 1 < info->first_arg_num)
1228 if (params == 0)
1229 return;
1230 params = TREE_CHAIN (params);
1231 ++arg_num;
1234 if (params == 0)
1235 res->number_other++;
1236 else
1237 res->number_extra_args++;
1239 return;
1242 offset = 0;
1243 if (TREE_CODE (format_tree) == PLUS_EXPR)
1245 tree arg0, arg1;
1247 arg0 = TREE_OPERAND (format_tree, 0);
1248 arg1 = TREE_OPERAND (format_tree, 1);
1249 STRIP_NOPS (arg0);
1250 STRIP_NOPS (arg1);
1251 if (TREE_CODE (arg1) == INTEGER_CST)
1252 format_tree = arg0;
1253 else if (TREE_CODE (arg0) == INTEGER_CST)
1255 format_tree = arg1;
1256 arg1 = arg0;
1258 else
1260 res->number_non_literal++;
1261 return;
1263 if (!host_integerp (arg1, 0)
1264 || (offset = tree_low_cst (arg1, 0)) < 0)
1266 res->number_non_literal++;
1267 return;
1270 if (TREE_CODE (format_tree) != ADDR_EXPR)
1272 res->number_non_literal++;
1273 return;
1275 format_tree = TREE_OPERAND (format_tree, 0);
1276 if (TREE_CODE (format_tree) == VAR_DECL
1277 && TREE_CODE (TREE_TYPE (format_tree)) == ARRAY_TYPE
1278 && (array_init = decl_constant_value (format_tree)) != format_tree
1279 && TREE_CODE (array_init) == STRING_CST)
1281 /* Extract the string constant initializer. Note that this may include
1282 a trailing NUL character that is not in the array (e.g.
1283 const char a[3] = "foo";). */
1284 array_size = DECL_SIZE_UNIT (format_tree);
1285 format_tree = array_init;
1287 if (TREE_CODE (format_tree) != STRING_CST)
1289 res->number_non_literal++;
1290 return;
1292 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree))) != char_type_node)
1294 res->number_wide++;
1295 return;
1297 format_chars = TREE_STRING_POINTER (format_tree);
1298 format_length = TREE_STRING_LENGTH (format_tree);
1299 if (array_size != 0)
1301 /* Variable length arrays can't be initialized. */
1302 if (TREE_CODE (array_size) != INTEGER_CST)
1303 abort ();
1304 if (host_integerp (array_size, 0))
1306 HOST_WIDE_INT array_size_value = TREE_INT_CST_LOW (array_size);
1307 if (array_size_value > 0
1308 && array_size_value == (int) array_size_value
1309 && format_length > array_size_value)
1310 format_length = array_size_value;
1313 if (offset)
1315 if (offset >= format_length)
1317 res->number_non_literal++;
1318 return;
1320 format_chars += offset;
1321 format_length -= offset;
1323 if (format_length < 1)
1325 res->number_unterminated++;
1326 return;
1328 if (format_length == 1)
1330 res->number_empty++;
1331 return;
1333 if (format_chars[--format_length] != 0)
1335 res->number_unterminated++;
1336 return;
1339 /* Skip to first argument to check. */
1340 while (arg_num + 1 < info->first_arg_num)
1342 if (params == 0)
1343 return;
1344 params = TREE_CHAIN (params);
1345 ++arg_num;
1347 /* Provisionally increment res->number_other; check_format_info_main
1348 will decrement it if it finds there are extra arguments, but this way
1349 need not adjust it for every return. */
1350 res->number_other++;
1351 check_format_info_main (res, info, format_chars, format_length,
1352 params, arg_num);
1356 /* Do the main part of checking a call to a format function. FORMAT_CHARS
1357 is the NUL-terminated format string (which at this point may contain
1358 internal NUL characters); FORMAT_LENGTH is its length (excluding the
1359 terminating NUL character). ARG_NUM is one less than the number of
1360 the first format argument to check; PARAMS points to that format
1361 argument in the list of arguments. */
1363 static void
1364 check_format_info_main (format_check_results *res,
1365 function_format_info *info, const char *format_chars,
1366 int format_length, tree params,
1367 unsigned HOST_WIDE_INT arg_num)
1369 const char *orig_format_chars = format_chars;
1370 tree first_fillin_param = params;
1372 const format_kind_info *fki = &format_types[info->format_type];
1373 const format_flag_spec *flag_specs = fki->flag_specs;
1374 const format_flag_pair *bad_flag_pairs = fki->bad_flag_pairs;
1376 /* -1 if no conversions taking an operand have been found; 0 if one has
1377 and it didn't use $; 1 if $ formats are in use. */
1378 int has_operand_number = -1;
1380 init_dollar_format_checking (info->first_arg_num, first_fillin_param);
1382 while (1)
1384 int i;
1385 int suppressed = FALSE;
1386 const char *length_chars = NULL;
1387 enum format_lengths length_chars_val = FMT_LEN_none;
1388 enum format_std_version length_chars_std = STD_C89;
1389 int format_char;
1390 tree cur_param;
1391 tree wanted_type;
1392 int main_arg_num = 0;
1393 tree main_arg_params = 0;
1394 enum format_std_version wanted_type_std;
1395 const char *wanted_type_name;
1396 format_wanted_type width_wanted_type;
1397 format_wanted_type precision_wanted_type;
1398 format_wanted_type main_wanted_type;
1399 format_wanted_type *first_wanted_type = NULL;
1400 format_wanted_type *last_wanted_type = NULL;
1401 const format_length_info *fli = NULL;
1402 const format_char_info *fci = NULL;
1403 char flag_chars[256];
1404 int aflag = 0;
1405 const char *format_start = format_chars;
1406 if (*format_chars == 0)
1408 if (format_chars - orig_format_chars != format_length)
1409 warning ("embedded %<\\0%> in format");
1410 if (info->first_arg_num != 0 && params != 0
1411 && has_operand_number <= 0)
1413 res->number_other--;
1414 res->number_extra_args++;
1416 if (has_operand_number > 0)
1417 finish_dollar_format_checking (res, fki->flags & (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK);
1418 return;
1420 if (*format_chars++ != '%')
1421 continue;
1422 if (*format_chars == 0)
1424 warning ("spurious trailing %<%%%> in format");
1425 continue;
1427 if (*format_chars == '%')
1429 ++format_chars;
1430 continue;
1432 flag_chars[0] = 0;
1434 if ((fki->flags & (int) FMT_FLAG_USE_DOLLAR) && has_operand_number != 0)
1436 /* Possibly read a $ operand number at the start of the format.
1437 If one was previously used, one is required here. If one
1438 is not used here, we can't immediately conclude this is a
1439 format without them, since it could be printf %m or scanf %*. */
1440 int opnum;
1441 opnum = maybe_read_dollar_number (&format_chars, 0,
1442 first_fillin_param,
1443 &main_arg_params, fki);
1444 if (opnum == -1)
1445 return;
1446 else if (opnum > 0)
1448 has_operand_number = 1;
1449 main_arg_num = opnum + info->first_arg_num - 1;
1452 else if (fki->flags & FMT_FLAG_USE_DOLLAR)
1454 if (avoid_dollar_number (format_chars))
1455 return;
1458 /* Read any format flags, but do not yet validate them beyond removing
1459 duplicates, since in general validation depends on the rest of
1460 the format. */
1461 while (*format_chars != 0
1462 && strchr (fki->flag_chars, *format_chars) != 0)
1464 const format_flag_spec *s = get_flag_spec (flag_specs,
1465 *format_chars, NULL);
1466 if (strchr (flag_chars, *format_chars) != 0)
1468 warning ("repeated %s in format", _(s->name));
1470 else
1472 i = strlen (flag_chars);
1473 flag_chars[i++] = *format_chars;
1474 flag_chars[i] = 0;
1476 if (s->skip_next_char)
1478 ++format_chars;
1479 if (*format_chars == 0)
1481 warning ("missing fill character at end of strfmon format");
1482 return;
1485 ++format_chars;
1488 /* Read any format width, possibly * or *m$. */
1489 if (fki->width_char != 0)
1491 if (fki->width_type != NULL && *format_chars == '*')
1493 i = strlen (flag_chars);
1494 flag_chars[i++] = fki->width_char;
1495 flag_chars[i] = 0;
1496 /* "...a field width...may be indicated by an asterisk.
1497 In this case, an int argument supplies the field width..." */
1498 ++format_chars;
1499 if (has_operand_number != 0)
1501 int opnum;
1502 opnum = maybe_read_dollar_number (&format_chars,
1503 has_operand_number == 1,
1504 first_fillin_param,
1505 &params, fki);
1506 if (opnum == -1)
1507 return;
1508 else if (opnum > 0)
1510 has_operand_number = 1;
1511 arg_num = opnum + info->first_arg_num - 1;
1513 else
1514 has_operand_number = 0;
1516 else
1518 if (avoid_dollar_number (format_chars))
1519 return;
1521 if (info->first_arg_num != 0)
1523 if (params == 0)
1525 warning ("too few arguments for format");
1526 return;
1528 cur_param = TREE_VALUE (params);
1529 if (has_operand_number <= 0)
1531 params = TREE_CHAIN (params);
1532 ++arg_num;
1534 width_wanted_type.wanted_type = *fki->width_type;
1535 width_wanted_type.wanted_type_name = NULL;
1536 width_wanted_type.pointer_count = 0;
1537 width_wanted_type.char_lenient_flag = 0;
1538 width_wanted_type.writing_in_flag = 0;
1539 width_wanted_type.reading_from_flag = 0;
1540 width_wanted_type.name = _("field width");
1541 width_wanted_type.param = cur_param;
1542 width_wanted_type.arg_num = arg_num;
1543 width_wanted_type.next = NULL;
1544 if (last_wanted_type != 0)
1545 last_wanted_type->next = &width_wanted_type;
1546 if (first_wanted_type == 0)
1547 first_wanted_type = &width_wanted_type;
1548 last_wanted_type = &width_wanted_type;
1551 else
1553 /* Possibly read a numeric width. If the width is zero,
1554 we complain if appropriate. */
1555 int non_zero_width_char = FALSE;
1556 int found_width = FALSE;
1557 while (ISDIGIT (*format_chars))
1559 found_width = TRUE;
1560 if (*format_chars != '0')
1561 non_zero_width_char = TRUE;
1562 ++format_chars;
1564 if (found_width && !non_zero_width_char &&
1565 (fki->flags & (int) FMT_FLAG_ZERO_WIDTH_BAD))
1566 warning ("zero width in %s format", fki->name);
1567 if (found_width)
1569 i = strlen (flag_chars);
1570 flag_chars[i++] = fki->width_char;
1571 flag_chars[i] = 0;
1576 /* Read any format left precision (must be a number, not *). */
1577 if (fki->left_precision_char != 0 && *format_chars == '#')
1579 ++format_chars;
1580 i = strlen (flag_chars);
1581 flag_chars[i++] = fki->left_precision_char;
1582 flag_chars[i] = 0;
1583 if (!ISDIGIT (*format_chars))
1584 warning ("empty left precision in %s format", fki->name);
1585 while (ISDIGIT (*format_chars))
1586 ++format_chars;
1589 /* Read any format precision, possibly * or *m$. */
1590 if (fki->precision_char != 0 && *format_chars == '.')
1592 ++format_chars;
1593 i = strlen (flag_chars);
1594 flag_chars[i++] = fki->precision_char;
1595 flag_chars[i] = 0;
1596 if (fki->precision_type != NULL && *format_chars == '*')
1598 /* "...a...precision...may be indicated by an asterisk.
1599 In this case, an int argument supplies the...precision." */
1600 ++format_chars;
1601 if (has_operand_number != 0)
1603 int opnum;
1604 opnum = maybe_read_dollar_number (&format_chars,
1605 has_operand_number == 1,
1606 first_fillin_param,
1607 &params, fki);
1608 if (opnum == -1)
1609 return;
1610 else if (opnum > 0)
1612 has_operand_number = 1;
1613 arg_num = opnum + info->first_arg_num - 1;
1615 else
1616 has_operand_number = 0;
1618 else
1620 if (avoid_dollar_number (format_chars))
1621 return;
1623 if (info->first_arg_num != 0)
1625 if (params == 0)
1627 warning ("too few arguments for format");
1628 return;
1630 cur_param = TREE_VALUE (params);
1631 if (has_operand_number <= 0)
1633 params = TREE_CHAIN (params);
1634 ++arg_num;
1636 precision_wanted_type.wanted_type = *fki->precision_type;
1637 precision_wanted_type.wanted_type_name = NULL;
1638 precision_wanted_type.pointer_count = 0;
1639 precision_wanted_type.char_lenient_flag = 0;
1640 precision_wanted_type.writing_in_flag = 0;
1641 precision_wanted_type.reading_from_flag = 0;
1642 precision_wanted_type.name = _("field precision");
1643 precision_wanted_type.param = cur_param;
1644 precision_wanted_type.arg_num = arg_num;
1645 precision_wanted_type.next = NULL;
1646 if (last_wanted_type != 0)
1647 last_wanted_type->next = &precision_wanted_type;
1648 if (first_wanted_type == 0)
1649 first_wanted_type = &precision_wanted_type;
1650 last_wanted_type = &precision_wanted_type;
1653 else
1655 if (!(fki->flags & (int) FMT_FLAG_EMPTY_PREC_OK)
1656 && !ISDIGIT (*format_chars))
1657 warning ("empty precision in %s format", fki->name);
1658 while (ISDIGIT (*format_chars))
1659 ++format_chars;
1663 /* Read any length modifier, if this kind of format has them. */
1664 fli = fki->length_char_specs;
1665 length_chars = NULL;
1666 length_chars_val = FMT_LEN_none;
1667 length_chars_std = STD_C89;
1668 if (fli)
1670 while (fli->name != 0 && fli->name[0] != *format_chars)
1671 fli++;
1672 if (fli->name != 0)
1674 format_chars++;
1675 if (fli->double_name != 0 && fli->name[0] == *format_chars)
1677 format_chars++;
1678 length_chars = fli->double_name;
1679 length_chars_val = fli->double_index;
1680 length_chars_std = fli->double_std;
1682 else
1684 length_chars = fli->name;
1685 length_chars_val = fli->index;
1686 length_chars_std = fli->std;
1688 i = strlen (flag_chars);
1689 flag_chars[i++] = fki->length_code_char;
1690 flag_chars[i] = 0;
1692 if (pedantic)
1694 /* Warn if the length modifier is non-standard. */
1695 if (ADJ_STD (length_chars_std) > C_STD_VER)
1696 warning ("%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 ("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 ("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 ("unknown conversion type character %qc in format",
1758 format_char);
1759 else
1760 warning ("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 ("%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 ("%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 ("%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 ("%s does not support %s with the %<%%%c%> %s format",
1802 C_STD_NAME (t->std), _(long_name),
1803 format_char, fki->name);
1807 flag_chars[i - d] = 0;
1810 if ((fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
1811 && strchr (flag_chars, 'a') != 0)
1812 aflag = 1;
1814 if (fki->suppression_char
1815 && strchr (flag_chars, fki->suppression_char) != 0)
1816 suppressed = 1;
1818 /* Validate the pairs of flags used. */
1819 for (i = 0; bad_flag_pairs[i].flag_char1 != 0; i++)
1821 const format_flag_spec *s, *t;
1822 if (strchr (flag_chars, bad_flag_pairs[i].flag_char1) == 0)
1823 continue;
1824 if (strchr (flag_chars, bad_flag_pairs[i].flag_char2) == 0)
1825 continue;
1826 if (bad_flag_pairs[i].predicate != 0
1827 && strchr (fci->flags2, bad_flag_pairs[i].predicate) == 0)
1828 continue;
1829 s = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char1, NULL);
1830 t = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char2, NULL);
1831 if (bad_flag_pairs[i].ignored)
1833 if (bad_flag_pairs[i].predicate != 0)
1834 warning ("%s ignored with %s and %<%%%c%> %s format",
1835 _(s->name), _(t->name), format_char,
1836 fki->name);
1837 else
1838 warning ("%s ignored with %s in %s format",
1839 _(s->name), _(t->name), fki->name);
1841 else
1843 if (bad_flag_pairs[i].predicate != 0)
1844 warning ("use of %s and %s together with %<%%%c%> %s format",
1845 _(s->name), _(t->name), format_char,
1846 fki->name);
1847 else
1848 warning ("use of %s and %s together in %s format",
1849 _(s->name), _(t->name), fki->name);
1853 /* Give Y2K warnings. */
1854 if (warn_format_y2k)
1856 int y2k_level = 0;
1857 if (strchr (fci->flags2, '4') != 0)
1858 if (strchr (flag_chars, 'E') != 0)
1859 y2k_level = 3;
1860 else
1861 y2k_level = 2;
1862 else if (strchr (fci->flags2, '3') != 0)
1863 y2k_level = 3;
1864 else if (strchr (fci->flags2, '2') != 0)
1865 y2k_level = 2;
1866 if (y2k_level == 3)
1867 warning ("%<%%%c%> yields only last 2 digits of year in some locales",
1868 format_char);
1869 else if (y2k_level == 2)
1870 warning ("%<%%%c%> yields only last 2 digits of year", format_char);
1873 if (strchr (fci->flags2, '[') != 0)
1875 /* Skip over scan set, in case it happens to have '%' in it. */
1876 if (*format_chars == '^')
1877 ++format_chars;
1878 /* Find closing bracket; if one is hit immediately, then
1879 it's part of the scan set rather than a terminator. */
1880 if (*format_chars == ']')
1881 ++format_chars;
1882 while (*format_chars && *format_chars != ']')
1883 ++format_chars;
1884 if (*format_chars != ']')
1885 /* The end of the format string was reached. */
1886 warning ("no closing %<]%> for %<%%[%> format");
1889 wanted_type = 0;
1890 wanted_type_name = 0;
1891 if (fki->flags & (int) FMT_FLAG_ARG_CONVERT)
1893 wanted_type = (fci->types[length_chars_val].type
1894 ? *fci->types[length_chars_val].type : 0);
1895 wanted_type_name = fci->types[length_chars_val].name;
1896 wanted_type_std = fci->types[length_chars_val].std;
1897 if (wanted_type == 0)
1899 warning ("use of %qs length modifier with %qc type character",
1900 length_chars, format_char);
1901 /* Heuristic: skip one argument when an invalid length/type
1902 combination is encountered. */
1903 arg_num++;
1904 if (params == 0)
1906 warning ("too few arguments for format");
1907 return;
1909 params = TREE_CHAIN (params);
1910 continue;
1912 else if (pedantic
1913 /* Warn if non-standard, provided it is more non-standard
1914 than the length and type characters that may already
1915 have been warned for. */
1916 && ADJ_STD (wanted_type_std) > ADJ_STD (length_chars_std)
1917 && ADJ_STD (wanted_type_std) > ADJ_STD (fci->std))
1919 if (ADJ_STD (wanted_type_std) > C_STD_VER)
1920 warning ("%s does not support the %<%%%s%c%> %s format",
1921 C_STD_NAME (wanted_type_std), length_chars,
1922 format_char, fki->name);
1926 main_wanted_type.next = NULL;
1928 /* Finally. . .check type of argument against desired type! */
1929 if (info->first_arg_num == 0)
1930 continue;
1931 if ((fci->pointer_count == 0 && wanted_type == void_type_node)
1932 || suppressed)
1934 if (main_arg_num != 0)
1936 if (suppressed)
1937 warning ("operand number specified with suppressed assignment");
1938 else
1939 warning ("operand number specified for format taking no argument");
1942 else
1944 format_wanted_type *wanted_type_ptr;
1946 if (main_arg_num != 0)
1948 arg_num = main_arg_num;
1949 params = main_arg_params;
1951 else
1953 ++arg_num;
1954 if (has_operand_number > 0)
1956 warning ("missing $ operand number in format");
1957 return;
1959 else
1960 has_operand_number = 0;
1963 wanted_type_ptr = &main_wanted_type;
1964 while (fci)
1966 if (params == 0)
1968 warning ("too few arguments for format");
1969 return;
1972 cur_param = TREE_VALUE (params);
1973 params = TREE_CHAIN (params);
1975 wanted_type_ptr->wanted_type = wanted_type;
1976 wanted_type_ptr->wanted_type_name = wanted_type_name;
1977 wanted_type_ptr->pointer_count = fci->pointer_count + aflag;
1978 wanted_type_ptr->char_lenient_flag = 0;
1979 if (strchr (fci->flags2, 'c') != 0)
1980 wanted_type_ptr->char_lenient_flag = 1;
1981 wanted_type_ptr->writing_in_flag = 0;
1982 wanted_type_ptr->reading_from_flag = 0;
1983 if (aflag)
1984 wanted_type_ptr->writing_in_flag = 1;
1985 else
1987 if (strchr (fci->flags2, 'W') != 0)
1988 wanted_type_ptr->writing_in_flag = 1;
1989 if (strchr (fci->flags2, 'R') != 0)
1990 wanted_type_ptr->reading_from_flag = 1;
1992 wanted_type_ptr->name = NULL;
1993 wanted_type_ptr->param = cur_param;
1994 wanted_type_ptr->arg_num = arg_num;
1995 wanted_type_ptr->next = NULL;
1996 if (last_wanted_type != 0)
1997 last_wanted_type->next = wanted_type_ptr;
1998 if (first_wanted_type == 0)
1999 first_wanted_type = wanted_type_ptr;
2000 last_wanted_type = wanted_type_ptr;
2002 fci = fci->chain;
2003 if (fci)
2005 wanted_type_ptr = ggc_alloc (sizeof (main_wanted_type));
2006 arg_num++;
2007 wanted_type = *fci->types[length_chars_val].type;
2008 wanted_type_name = fci->types[length_chars_val].name;
2013 if (first_wanted_type != 0)
2014 check_format_types (first_wanted_type, format_start,
2015 format_chars - format_start);
2017 if (main_wanted_type.next != NULL)
2019 format_wanted_type *wanted_type_ptr = main_wanted_type.next;
2020 while (wanted_type_ptr)
2022 format_wanted_type *next = wanted_type_ptr->next;
2023 ggc_free (wanted_type_ptr);
2024 wanted_type_ptr = next;
2031 /* Check the argument types from a single format conversion (possibly
2032 including width and precision arguments). */
2033 static void
2034 check_format_types (format_wanted_type *types, const char *format_start,
2035 int format_length)
2037 for (; types != 0; types = types->next)
2039 tree cur_param;
2040 tree cur_type;
2041 tree orig_cur_type;
2042 tree wanted_type;
2043 int arg_num;
2044 int i;
2045 int char_type_flag;
2046 cur_param = types->param;
2047 cur_type = TREE_TYPE (cur_param);
2048 if (cur_type == error_mark_node)
2049 continue;
2050 orig_cur_type = cur_type;
2051 char_type_flag = 0;
2052 wanted_type = types->wanted_type;
2053 arg_num = types->arg_num;
2055 /* The following should not occur here. */
2056 if (wanted_type == 0)
2057 abort ();
2058 if (wanted_type == void_type_node && types->pointer_count == 0)
2059 abort ();
2061 if (types->pointer_count == 0)
2062 wanted_type = lang_hooks.types.type_promotes_to (wanted_type);
2064 wanted_type = TYPE_MAIN_VARIANT (wanted_type);
2066 STRIP_NOPS (cur_param);
2068 /* Check the types of any additional pointer arguments
2069 that precede the "real" argument. */
2070 for (i = 0; i < types->pointer_count; ++i)
2072 if (TREE_CODE (cur_type) == POINTER_TYPE)
2074 cur_type = TREE_TYPE (cur_type);
2075 if (cur_type == error_mark_node)
2076 break;
2078 /* Check for writing through a NULL pointer. */
2079 if (types->writing_in_flag
2080 && i == 0
2081 && cur_param != 0
2082 && integer_zerop (cur_param))
2083 warning ("writing through null pointer (arg %d)",
2084 arg_num);
2086 /* Check for reading through a NULL pointer. */
2087 if (types->reading_from_flag
2088 && i == 0
2089 && cur_param != 0
2090 && integer_zerop (cur_param))
2091 warning ("reading through null pointer (arg %d)",
2092 arg_num);
2094 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
2095 cur_param = TREE_OPERAND (cur_param, 0);
2096 else
2097 cur_param = 0;
2099 /* See if this is an attempt to write into a const type with
2100 scanf or with printf "%n". Note: the writing in happens
2101 at the first indirection only, if for example
2102 void * const * is passed to scanf %p; passing
2103 const void ** is simply passing an incompatible type. */
2104 if (types->writing_in_flag
2105 && i == 0
2106 && (TYPE_READONLY (cur_type)
2107 || (cur_param != 0
2108 && (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'c'
2109 || (DECL_P (cur_param)
2110 && TREE_READONLY (cur_param))))))
2111 warning ("writing into constant object (arg %d)", arg_num);
2113 /* If there are extra type qualifiers beyond the first
2114 indirection, then this makes the types technically
2115 incompatible. */
2116 if (i > 0
2117 && pedantic
2118 && (TYPE_READONLY (cur_type)
2119 || TYPE_VOLATILE (cur_type)
2120 || TYPE_RESTRICT (cur_type)))
2121 warning ("extra type qualifiers in format argument (arg %d)",
2122 arg_num);
2125 else
2127 format_type_warning (types->name, format_start, format_length,
2128 wanted_type, types->pointer_count,
2129 types->wanted_type_name, orig_cur_type,
2130 arg_num);
2131 break;
2135 if (i < types->pointer_count)
2136 continue;
2138 cur_type = TYPE_MAIN_VARIANT (cur_type);
2140 /* Check whether the argument type is a character type. This leniency
2141 only applies to certain formats, flagged with 'c'.
2143 if (types->char_lenient_flag)
2144 char_type_flag = (cur_type == char_type_node
2145 || cur_type == signed_char_type_node
2146 || cur_type == unsigned_char_type_node);
2148 /* Check the type of the "real" argument, if there's a type we want. */
2149 if (wanted_type == cur_type)
2150 continue;
2151 /* If we want `void *', allow any pointer type.
2152 (Anything else would already have got a warning.)
2153 With -pedantic, only allow pointers to void and to character
2154 types. */
2155 if (wanted_type == void_type_node
2156 && (!pedantic || (i == 1 && char_type_flag)))
2157 continue;
2158 /* Don't warn about differences merely in signedness, unless
2159 -pedantic. With -pedantic, warn if the type is a pointer
2160 target and not a character type, and for character types at
2161 a second level of indirection. */
2162 if (TREE_CODE (wanted_type) == INTEGER_TYPE
2163 && TREE_CODE (cur_type) == INTEGER_TYPE
2164 && (! pedantic || i == 0 || (i == 1 && char_type_flag))
2165 && (TYPE_UNSIGNED (wanted_type)
2166 ? wanted_type == c_common_unsigned_type (cur_type)
2167 : wanted_type == c_common_signed_type (cur_type)))
2168 continue;
2169 /* Likewise, "signed char", "unsigned char" and "char" are
2170 equivalent but the above test won't consider them equivalent. */
2171 if (wanted_type == char_type_node
2172 && (! pedantic || i < 2)
2173 && char_type_flag)
2174 continue;
2175 /* Now we have a type mismatch. */
2176 format_type_warning (types->name, format_start, format_length,
2177 wanted_type, types->pointer_count,
2178 types->wanted_type_name, orig_cur_type, arg_num);
2183 /* Give a warning about a format argument of different type from that
2184 expected. DESCR is a description such as "field precision", or
2185 NULL for an ordinary format. For an ordinary format, FORMAT_START
2186 points to where the format starts in the format string and
2187 FORMAT_LENGTH is its length. WANTED_TYPE is the type the argument
2188 should have after POINTER_COUNT pointer dereferences.
2189 WANTED_NAME_NAME is a possibly more friendly name of WANTED_TYPE,
2190 or NULL if the ordinary name of the type should be used. ARG_TYPE
2191 is the type of the actual argument. ARG_NUM is the number of that
2192 argument. */
2193 static void
2194 format_type_warning (const char *descr, const char *format_start,
2195 int format_length, tree wanted_type, int pointer_count,
2196 const char *wanted_type_name, tree arg_type, int arg_num)
2198 char *p;
2199 /* If ARG_TYPE is a typedef with a misleading name (for example,
2200 size_t but not the standard size_t expected by printf %zu), avoid
2201 printing the typedef name. */
2202 if (wanted_type_name
2203 && TYPE_NAME (arg_type)
2204 && TREE_CODE (TYPE_NAME (arg_type)) == TYPE_DECL
2205 && DECL_NAME (TYPE_NAME (arg_type))
2206 && !strcmp (wanted_type_name,
2207 lang_hooks.decl_printable_name (TYPE_NAME (arg_type), 2)))
2208 arg_type = TYPE_MAIN_VARIANT (arg_type);
2209 /* The format type and name exclude any '*' for pointers, so those
2210 must be formatted manually. For all the types we currently have,
2211 this is adequate, but formats taking pointers to functions or
2212 arrays would require the full type to be built up in order to
2213 print it with %T. */
2214 p = alloca (pointer_count + 2);
2215 if (pointer_count == 0)
2216 p[0] = 0;
2217 else if (c_dialect_cxx ())
2219 memset (p, '*', pointer_count);
2220 p[pointer_count] = 0;
2222 else
2224 p[0] = ' ';
2225 memset (p + 1, '*', pointer_count);
2226 p[pointer_count + 1] = 0;
2228 if (wanted_type_name)
2230 if (descr)
2231 warning ("%s should have type %<%s%s%>, but argument %d has type %qT",
2232 descr, wanted_type_name, p, arg_num, arg_type);
2233 else
2234 warning ("format %q.*s expects type %<%s%s%>, but argument %d has type %qT",
2235 format_length, format_start, wanted_type_name, p,
2236 arg_num, arg_type);
2238 else
2240 if (descr)
2241 warning ("%s should have type %<%T%s%>, but argument %d has type %qT",
2242 descr, wanted_type, p, arg_num, arg_type);
2243 else
2244 warning ("format %q.*s expects type %<%T%s%>, but argument %d has type %qT",
2245 format_length, format_start, wanted_type, p, arg_num, arg_type);
2250 /* Given a format_char_info array FCI, and a character C, this function
2251 returns the index into the conversion_specs where that specifier's
2252 data is located. If the character isn't found it aborts. */
2253 static unsigned int
2254 find_char_info_specifier_index (const format_char_info *fci, int c)
2256 unsigned int i = 0;
2258 while (fci->format_chars)
2260 if (strchr (fci->format_chars, c))
2261 return i;
2262 i++; fci++;
2265 /* We shouldn't be looking for a non-existent specifier. */
2266 abort ();
2269 /* Given a format_length_info array FLI, and a character C, this
2270 function returns the index into the conversion_specs where that
2271 modifier's data is located. If the character isn't found it
2272 aborts. */
2273 static unsigned int
2274 find_length_info_modifier_index (const format_length_info *fli, int c)
2276 unsigned int i = 0;
2278 while (fli->name)
2280 if (strchr (fli->name, c))
2281 return i;
2282 i++; fli++;
2285 /* We shouldn't be looking for a non-existent modifier. */
2286 abort ();
2289 /* Determine the type of HOST_WIDE_INT in the code being compiled for
2290 use in GCC's __asm_fprintf__ custom format attribute. You must
2291 have set dynamic_format_types before calling this function. */
2292 static void
2293 init_dynamic_asm_fprintf_info (void)
2295 static tree hwi;
2297 if (!hwi)
2299 format_length_info *new_asm_fprintf_length_specs;
2300 unsigned int i;
2302 /* Find the underlying type for HOST_WIDE_INT. For the %w
2303 length modifier to work, one must have issued: "typedef
2304 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2305 prior to using that modifier. */
2306 if (!(hwi = maybe_get_identifier ("__gcc_host_wide_int__"))
2307 || !(hwi = DECL_ORIGINAL_TYPE (identifier_global_value (hwi))))
2308 abort ();
2310 /* Create a new (writable) copy of asm_fprintf_length_specs. */
2311 new_asm_fprintf_length_specs = (format_length_info *)
2312 xmemdup (asm_fprintf_length_specs,
2313 sizeof (asm_fprintf_length_specs),
2314 sizeof (asm_fprintf_length_specs));
2316 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2317 i = find_length_info_modifier_index (new_asm_fprintf_length_specs, 'w');
2318 if (hwi == long_integer_type_node)
2319 new_asm_fprintf_length_specs[i].index = FMT_LEN_l;
2320 else if (hwi == long_long_integer_type_node)
2321 new_asm_fprintf_length_specs[i].index = FMT_LEN_ll;
2322 else
2323 abort ();
2325 /* Assign the new data for use. */
2326 dynamic_format_types[asm_fprintf_format_type].length_char_specs =
2327 new_asm_fprintf_length_specs;
2331 /* Determine the types of "tree" and "location_t" in the code being
2332 compiled for use in GCC's diagnostic custom format attributes. You
2333 must have set dynamic_format_types before calling this function. */
2334 static void
2335 init_dynamic_diag_info (void)
2337 static tree t, loc, hwi;
2339 if (!loc || !t || !hwi)
2341 static format_char_info *diag_fci, *cdiag_fci, *cxxdiag_fci;
2342 static format_length_info *diag_ls;
2343 unsigned int i;
2345 /* For the GCC-diagnostics custom format specifiers to work, one
2346 must have declared `tree' and/or `location_t' prior to using
2347 those attributes. If we haven't seen these declarations then
2348 you shouldn't use the specifiers requiring these types.
2349 However we don't force a hard ICE because we may see only one
2350 or the other type. */
2351 if ((loc = maybe_get_identifier ("location_t")))
2352 loc = TREE_TYPE (identifier_global_value (loc));
2354 /* We need to grab the underlying `union tree_node' so peek into
2355 an extra type level. */
2356 if ((t = maybe_get_identifier ("tree")))
2357 t = TREE_TYPE (TREE_TYPE (identifier_global_value (t)));
2359 /* Find the underlying type for HOST_WIDE_INT. For the %w
2360 length modifier to work, one must have issued: "typedef
2361 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2362 prior to using that modifier. */
2363 if ((hwi = maybe_get_identifier ("__gcc_host_wide_int__")))
2364 hwi = DECL_ORIGINAL_TYPE (identifier_global_value (hwi));
2366 /* Assign the new data for use. */
2368 /* All the GCC diag formats use the same length specs. */
2369 if (! diag_ls)
2370 dynamic_format_types[gcc_diag_format_type].length_char_specs =
2371 dynamic_format_types[gcc_cdiag_format_type].length_char_specs =
2372 dynamic_format_types[gcc_cxxdiag_format_type].length_char_specs =
2373 diag_ls = (format_length_info *)
2374 xmemdup (gcc_diag_length_specs,
2375 sizeof (gcc_diag_length_specs),
2376 sizeof (gcc_diag_length_specs));
2377 if (hwi)
2379 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2380 i = find_length_info_modifier_index (diag_ls, 'w');
2381 if (hwi == long_integer_type_node)
2382 diag_ls[i].index = FMT_LEN_l;
2383 else if (hwi == long_long_integer_type_node)
2384 diag_ls[i].index = FMT_LEN_ll;
2385 else
2386 abort ();
2389 /* Handle the __gcc_diag__ format specifics. */
2390 if (! diag_fci)
2391 dynamic_format_types[gcc_diag_format_type].conversion_specs =
2392 diag_fci = (format_char_info *)
2393 xmemdup (gcc_diag_char_table,
2394 sizeof(gcc_diag_char_table),
2395 sizeof(gcc_diag_char_table));
2396 if (loc)
2398 i = find_char_info_specifier_index (diag_fci, 'H');
2399 diag_fci[i].types[0].type = &loc;
2400 diag_fci[i].pointer_count = 1;
2402 if (t)
2404 i = find_char_info_specifier_index (diag_fci, 'J');
2405 diag_fci[i].types[0].type = &t;
2406 diag_fci[i].pointer_count = 1;
2409 /* Handle the __gcc_cdiag__ format specifics. */
2410 if (! cdiag_fci)
2411 dynamic_format_types[gcc_cdiag_format_type].conversion_specs =
2412 cdiag_fci = (format_char_info *)
2413 xmemdup (gcc_cdiag_char_table,
2414 sizeof(gcc_cdiag_char_table),
2415 sizeof(gcc_cdiag_char_table));
2416 if (loc)
2418 i = find_char_info_specifier_index (cdiag_fci, 'H');
2419 cdiag_fci[i].types[0].type = &loc;
2420 cdiag_fci[i].pointer_count = 1;
2422 if (t)
2424 /* All specifiers taking a tree share the same struct. */
2425 i = find_char_info_specifier_index (cdiag_fci, 'D');
2426 cdiag_fci[i].types[0].type = &t;
2427 cdiag_fci[i].pointer_count = 1;
2428 i = find_char_info_specifier_index (cdiag_fci, 'J');
2429 cdiag_fci[i].types[0].type = &t;
2430 cdiag_fci[i].pointer_count = 1;
2433 /* Handle the __gcc_cxxdiag__ format specifics. */
2434 if (! cxxdiag_fci)
2435 dynamic_format_types[gcc_cxxdiag_format_type].conversion_specs =
2436 cxxdiag_fci = (format_char_info *)
2437 xmemdup (gcc_cxxdiag_char_table,
2438 sizeof(gcc_cxxdiag_char_table),
2439 sizeof(gcc_cxxdiag_char_table));
2440 if (loc)
2442 i = find_char_info_specifier_index (cxxdiag_fci, 'H');
2443 cxxdiag_fci[i].types[0].type = &loc;
2444 cxxdiag_fci[i].pointer_count = 1;
2446 if (t)
2448 /* All specifiers taking a tree share the same struct. */
2449 i = find_char_info_specifier_index (cxxdiag_fci, 'D');
2450 cxxdiag_fci[i].types[0].type = &t;
2451 cxxdiag_fci[i].pointer_count = 1;
2452 i = find_char_info_specifier_index (cxxdiag_fci, 'J');
2453 cxxdiag_fci[i].types[0].type = &t;
2454 cxxdiag_fci[i].pointer_count = 1;
2459 #ifdef TARGET_FORMAT_TYPES
2460 extern const format_kind_info TARGET_FORMAT_TYPES[];
2461 #endif
2463 /* Handle a "format" attribute; arguments as in
2464 struct attribute_spec.handler. */
2465 tree
2466 handle_format_attribute (tree *node, tree ARG_UNUSED (name), tree args,
2467 int flags, bool *no_add_attrs)
2469 tree type = *node;
2470 function_format_info info;
2471 tree argument;
2473 #ifdef TARGET_FORMAT_TYPES
2474 /* If the target provides additional format types, we need to
2475 add them to FORMAT_TYPES at first use. */
2476 if (TARGET_FORMAT_TYPES != NULL && !dynamic_format_types)
2478 dynamic_format_types = xmalloc ((n_format_types + TARGET_N_FORMAT_TYPES)
2479 * sizeof (dynamic_format_types[0]));
2480 memcpy (dynamic_format_types, format_types_orig,
2481 sizeof (format_types_orig));
2482 memcpy (&dynamic_format_types[n_format_types], TARGET_FORMAT_TYPES,
2483 TARGET_N_FORMAT_TYPES * sizeof (dynamic_format_types[0]));
2485 format_types = dynamic_format_types;
2486 n_format_types += TARGET_N_FORMAT_TYPES;
2488 #endif
2490 if (!decode_format_attr (args, &info, 0))
2492 *no_add_attrs = true;
2493 return NULL_TREE;
2496 argument = TYPE_ARG_TYPES (type);
2497 if (argument)
2499 if (!check_format_string (argument, info.format_num, flags,
2500 no_add_attrs))
2501 return NULL_TREE;
2503 if (info.first_arg_num != 0)
2505 unsigned HOST_WIDE_INT arg_num = 1;
2507 /* Verify that first_arg_num points to the last arg,
2508 the ... */
2509 while (argument)
2510 arg_num++, argument = TREE_CHAIN (argument);
2512 if (arg_num != info.first_arg_num)
2514 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
2515 error ("args to be formatted is not '...'");
2516 *no_add_attrs = true;
2517 return NULL_TREE;
2522 if (info.format_type == strftime_format_type && info.first_arg_num != 0)
2524 error ("strftime formats cannot format arguments");
2525 *no_add_attrs = true;
2526 return NULL_TREE;
2529 /* If this is a custom GCC-internal format type, we have to
2530 initialize certain bits a runtime. */
2531 if (info.format_type == asm_fprintf_format_type
2532 || info.format_type == gcc_diag_format_type
2533 || info.format_type == gcc_cdiag_format_type
2534 || info.format_type == gcc_cxxdiag_format_type)
2536 /* Our first time through, we have to make sure that our
2537 format_type data is allocated dynamically and is modifiable. */
2538 if (!dynamic_format_types)
2539 format_types = dynamic_format_types = (format_kind_info *)
2540 xmemdup (format_types_orig, sizeof (format_types_orig),
2541 sizeof (format_types_orig));
2543 /* If this is format __asm_fprintf__, we have to initialize
2544 GCC's notion of HOST_WIDE_INT for checking %wd. */
2545 if (info.format_type == asm_fprintf_format_type)
2546 init_dynamic_asm_fprintf_info();
2547 /* If this is one of the diagnostic attributes, then we have to
2548 initialize `location_t' and `tree' at runtime. */
2549 else if (info.format_type == gcc_diag_format_type
2550 || info.format_type == gcc_cdiag_format_type
2551 || info.format_type == gcc_cxxdiag_format_type)
2552 init_dynamic_diag_info();
2553 else
2554 abort();
2557 return NULL_TREE;