1 /* Check calls to formatted I/O functions (-Wformat).
2 Copyright (C) 1992-2013 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "flags.h"
26 #include "c-common.h"
27 #include "c-objc.h"
28 #include "intl.h"
29 #include "diagnostic-core.h"
30 #include "langhooks.h"
31 #include "c-format.h"
32 #include "alloc-pool.h"
33 #include "c-target.h"
34
35 /* Handle attributes associated with format checking. */
36
37 /* This must be in the same order as format_types, except for
38 format_type_error. Target-specific format types do not have
39 matching enum values. */
40 enum format_type { printf_format_type, asm_fprintf_format_type,
41 gcc_diag_format_type, gcc_tdiag_format_type,
42 gcc_cdiag_format_type,
43 gcc_cxxdiag_format_type, gcc_gfc_format_type,
44 gcc_objc_string_format_type,
45 format_type_error = -1};
46
47 typedef struct function_format_info
48 {
49 int format_type; /* type of format (printf, scanf, etc.) */
50 unsigned HOST_WIDE_INT format_num; /* number of format argument */
51 unsigned HOST_WIDE_INT first_arg_num; /* number of first arg (zero for varargs) */
52 } function_format_info;
53
54 static bool decode_format_attr (tree, function_format_info *, int);
55 static int decode_format_type (const char *);
56
57 static bool check_format_string (tree argument,
58 unsigned HOST_WIDE_INT format_num,
59 int flags, bool *no_add_attrs,
60 int expected_format_type);
61 static bool get_constant (tree expr, unsigned HOST_WIDE_INT *value,
62 int validated_p);
63 static const char *convert_format_name_to_system_name (const char *attr_name);
64 static bool cmp_attribs (const char *tattr_name, const char *attr_name);
65
66 static int first_target_format_type;
67 static const char *format_name (int format_num);
68 static int format_flags (int format_num);
69
70 /* Check that we have a pointer to a string suitable for use as a format.
71 The default is to check for a char type.
72 For objective-c dialects, this is extended to include references to string
73 objects validated by objc_string_ref_type_p ().
74 Targets may also provide a string object type that can be used within c and
75 c++ and shared with their respective objective-c dialects. In this case the
76 reference to a format string is checked for validity via a hook.
77
78 The function returns true if strref points to any string type valid for the
79 language dialect and target. */
80
81 static bool
valid_stringptr_type_p(tree strref)82 valid_stringptr_type_p (tree strref)
83 {
84 return (strref != NULL
85 && TREE_CODE (strref) == POINTER_TYPE
86 && (TYPE_MAIN_VARIANT (TREE_TYPE (strref)) == char_type_node
87 || objc_string_ref_type_p (strref)
88 || (*targetcm.string_object_ref_type_p) ((const_tree) strref)));
89 }
90
91 /* Handle a "format_arg" attribute; arguments as in
92 struct attribute_spec.handler. */
93 tree
handle_format_arg_attribute(tree * node,tree ARG_UNUSED (name),tree args,int flags,bool * no_add_attrs)94 handle_format_arg_attribute (tree *node, tree ARG_UNUSED (name),
95 tree args, int flags, bool *no_add_attrs)
96 {
97 tree type = *node;
98 tree format_num_expr = TREE_VALUE (args);
99 unsigned HOST_WIDE_INT format_num = 0;
100
101 if (!get_constant (format_num_expr, &format_num, 0))
102 {
103 error ("format string has invalid operand number");
104 *no_add_attrs = true;
105 return NULL_TREE;
106 }
107
108 if (prototype_p (type))
109 {
110 /* The format arg can be any string reference valid for the language and
111 target. We cannot be more specific in this case. */
112 if (!check_format_string (type, format_num, flags, no_add_attrs, -1))
113 return NULL_TREE;
114 }
115
116 if (!valid_stringptr_type_p (TREE_TYPE (type)))
117 {
118 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
119 error ("function does not return string type");
120 *no_add_attrs = true;
121 return NULL_TREE;
122 }
123
124 return NULL_TREE;
125 }
126
127 /* Verify that the format_num argument is actually a string reference suitable,
128 for the language dialect and target (in case the format attribute is in
129 error). When we know the specific reference type expected, this is also
130 checked. */
131 static bool
check_format_string(tree fntype,unsigned HOST_WIDE_INT format_num,int flags,bool * no_add_attrs,int expected_format_type)132 check_format_string (tree fntype, unsigned HOST_WIDE_INT format_num,
133 int flags, bool *no_add_attrs, int expected_format_type)
134 {
135 unsigned HOST_WIDE_INT i;
136 bool is_objc_sref, is_target_sref, is_char_ref;
137 tree ref;
138 int fmt_flags;
139 function_args_iterator iter;
140
141 i = 1;
142 FOREACH_FUNCTION_ARGS (fntype, ref, iter)
143 {
144 if (i == format_num)
145 break;
146 i++;
147 }
148
149 if (!ref
150 || !valid_stringptr_type_p (ref))
151 {
152 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
153 error ("format string argument is not a string type");
154 *no_add_attrs = true;
155 return false;
156 }
157
158 /* We only know that we want a suitable string reference. */
159 if (expected_format_type < 0)
160 return true;
161
162 /* Now check that the arg matches the expected type. */
163 is_char_ref =
164 (TYPE_MAIN_VARIANT (TREE_TYPE (ref)) == char_type_node);
165
166 fmt_flags = format_flags (expected_format_type);
167 is_objc_sref = is_target_sref = false;
168 if (!is_char_ref)
169 is_objc_sref = objc_string_ref_type_p (ref);
170
171 if (!(fmt_flags & FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL))
172 {
173 if (is_char_ref)
174 return true; /* OK, we expected a char and found one. */
175 else
176 {
177 /* We expected a char but found an extended string type. */
178 if (is_objc_sref)
179 error ("found a %<%s%> reference but the format argument should"
180 " be a string", format_name (gcc_objc_string_format_type));
181 else
182 error ("found a %qT but the format argument should be a string",
183 ref);
184 *no_add_attrs = true;
185 return false;
186 }
187 }
188
189 /* We expect a string object type as the format arg. */
190 if (is_char_ref)
191 {
192 error ("format argument should be a %<%s%> reference but"
193 " a string was found", format_name (expected_format_type));
194 *no_add_attrs = true;
195 return false;
196 }
197
198 /* We will assert that objective-c will support either its own string type
199 or the target-supplied variant. */
200 if (!is_objc_sref)
201 is_target_sref = (*targetcm.string_object_ref_type_p) ((const_tree) ref);
202
203 if (expected_format_type == (int) gcc_objc_string_format_type
204 && (is_objc_sref || is_target_sref))
205 return true;
206
207 /* We will allow a target string ref to match only itself. */
208 if (first_target_format_type
209 && expected_format_type >= first_target_format_type
210 && is_target_sref)
211 return true;
212 else
213 {
214 error ("format argument should be a %<%s%> reference",
215 format_name (expected_format_type));
216 *no_add_attrs = true;
217 return false;
218 }
219
220 gcc_unreachable ();
221 }
222
223 /* Verify EXPR is a constant, and store its value.
224 If validated_p is true there should be no errors.
225 Returns true on success, false otherwise. */
226 static bool
get_constant(tree expr,unsigned HOST_WIDE_INT * value,int validated_p)227 get_constant (tree expr, unsigned HOST_WIDE_INT *value, int validated_p)
228 {
229 if (TREE_CODE (expr) != INTEGER_CST || TREE_INT_CST_HIGH (expr) != 0)
230 {
231 gcc_assert (!validated_p);
232 return false;
233 }
234
235 *value = TREE_INT_CST_LOW (expr);
236
237 return true;
238 }
239
240 /* Decode the arguments to a "format" attribute into a
241 function_format_info structure. It is already known that the list
242 is of the right length. If VALIDATED_P is true, then these
243 attributes have already been validated and must not be erroneous;
244 if false, it will give an error message. Returns true if the
245 attributes are successfully decoded, false otherwise. */
246
247 static bool
decode_format_attr(tree args,function_format_info * info,int validated_p)248 decode_format_attr (tree args, function_format_info *info, int validated_p)
249 {
250 tree format_type_id = TREE_VALUE (args);
251 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
252 tree first_arg_num_expr
253 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
254
255 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
256 {
257 gcc_assert (!validated_p);
258 error ("unrecognized format specifier");
259 return false;
260 }
261 else
262 {
263 const char *p = IDENTIFIER_POINTER (format_type_id);
264
265 p = convert_format_name_to_system_name (p);
266
267 info->format_type = decode_format_type (p);
268
269 if (!c_dialect_objc ()
270 && info->format_type == gcc_objc_string_format_type)
271 {
272 gcc_assert (!validated_p);
273 warning (OPT_Wformat_, "%qE is only allowed in Objective-C dialects",
274 format_type_id);
275 info->format_type = format_type_error;
276 return false;
277 }
278
279 if (info->format_type == format_type_error)
280 {
281 gcc_assert (!validated_p);
282 warning (OPT_Wformat_, "%qE is an unrecognized format function type",
283 format_type_id);
284 return false;
285 }
286 }
287
288 if (!get_constant (format_num_expr, &info->format_num, validated_p))
289 {
290 error ("format string has invalid operand number");
291 return false;
292 }
293
294 if (!get_constant (first_arg_num_expr, &info->first_arg_num, validated_p))
295 {
296 error ("%<...%> has invalid operand number");
297 return false;
298 }
299
300 if (info->first_arg_num != 0 && info->first_arg_num <= info->format_num)
301 {
302 gcc_assert (!validated_p);
303 error ("format string argument follows the args to be formatted");
304 return false;
305 }
306
307 return true;
308 }
309
310 /* Check a call to a format function against a parameter list. */
311
312 /* The C standard version C++ is treated as equivalent to
313 or inheriting from, for the purpose of format features supported. */
314 #define CPLUSPLUS_STD_VER (cxx_dialect < cxx11 ? STD_C94 : STD_C99)
315 /* The C standard version we are checking formats against when pedantic. */
316 #define C_STD_VER ((int) (c_dialect_cxx () \
317 ? CPLUSPLUS_STD_VER \
318 : (flag_isoc99 \
319 ? STD_C99 \
320 : (flag_isoc94 ? STD_C94 : STD_C89))))
321 /* The name to give to the standard version we are warning about when
322 pedantic. FEATURE_VER is the version in which the feature warned out
323 appeared, which is higher than C_STD_VER. */
324 #define C_STD_NAME(FEATURE_VER) (c_dialect_cxx () \
325 ? (cxx_dialect < cxx11 ? "ISO C++98" \
326 : "ISO C++11") \
327 : ((FEATURE_VER) == STD_EXT \
328 ? "ISO C" \
329 : "ISO C90"))
330 /* Adjust a C standard version, which may be STD_C9L, to account for
331 -Wno-long-long. Returns other standard versions unchanged. */
332 #define ADJ_STD(VER) ((int) ((VER) == STD_C9L \
333 ? (warn_long_long ? STD_C99 : STD_C89) \
334 : (VER)))
335
336 /* Enum describing the kind of specifiers present in the format and
337 requiring an argument. */
338 enum format_specifier_kind {
339 CF_KIND_FORMAT,
340 CF_KIND_FIELD_WIDTH,
341 CF_KIND_FIELD_PRECISION
342 };
343
344 static const char *kind_descriptions[] = {
345 N_("format"),
346 N_("field width specifier"),
347 N_("field precision specifier")
348 };
349
350 /* Structure describing details of a type expected in format checking,
351 and the type to check against it. */
352 typedef struct format_wanted_type
353 {
354 /* The type wanted. */
355 tree wanted_type;
356 /* The name of this type to use in diagnostics. */
357 const char *wanted_type_name;
358 /* Should be type checked just for scalar width identity. */
359 int scalar_identity_flag;
360 /* The level of indirection through pointers at which this type occurs. */
361 int pointer_count;
362 /* Whether, when pointer_count is 1, to allow any character type when
363 pedantic, rather than just the character or void type specified. */
364 int char_lenient_flag;
365 /* Whether the argument, dereferenced once, is written into and so the
366 argument must not be a pointer to a const-qualified type. */
367 int writing_in_flag;
368 /* Whether the argument, dereferenced once, is read from and so
369 must not be a NULL pointer. */
370 int reading_from_flag;
371 /* The kind of specifier that this type is used for. */
372 enum format_specifier_kind kind;
373 /* The starting character of the specifier. This never includes the
374 initial percent sign. */
375 const char *format_start;
376 /* The length of the specifier. */
377 int format_length;
378 /* The actual parameter to check against the wanted type. */
379 tree param;
380 /* The argument number of that parameter. */
381 int arg_num;
382 /* The next type to check for this format conversion, or NULL if none. */
383 struct format_wanted_type *next;
384 } format_wanted_type;
385
386 /* Convenience macro for format_length_info meaning unused. */
387 #define NO_FMT NULL, FMT_LEN_none, STD_C89
388
389 static const format_length_info printf_length_specs[] =
390 {
391 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99, 0 },
392 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L, 0 },
393 { "q", FMT_LEN_ll, STD_EXT, NO_FMT, 0 },
394 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
395 { "z", FMT_LEN_z, STD_C99, NO_FMT, 0 },
396 { "Z", FMT_LEN_z, STD_EXT, NO_FMT, 0 },
397 { "t", FMT_LEN_t, STD_C99, NO_FMT, 0 },
398 { "j", FMT_LEN_j, STD_C99, NO_FMT, 0 },
399 { "H", FMT_LEN_H, STD_EXT, NO_FMT, 0 },
400 { "D", FMT_LEN_D, STD_EXT, "DD", FMT_LEN_DD, STD_EXT, 0 },
401 { NO_FMT, NO_FMT, 0 }
402 };
403
404 /* Length specifiers valid for asm_fprintf. */
405 static const format_length_info asm_fprintf_length_specs[] =
406 {
407 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89, 0 },
408 { "w", FMT_LEN_none, STD_C89, NO_FMT, 0 },
409 { NO_FMT, NO_FMT, 0 }
410 };
411
412 /* Length specifiers valid for GCC diagnostics. */
413 static const format_length_info gcc_diag_length_specs[] =
414 {
415 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89, 0 },
416 { "w", FMT_LEN_none, STD_C89, NO_FMT, 0 },
417 { NO_FMT, NO_FMT, 0 }
418 };
419
420 /* The custom diagnostics all accept the same length specifiers. */
421 #define gcc_tdiag_length_specs gcc_diag_length_specs
422 #define gcc_cdiag_length_specs gcc_diag_length_specs
423 #define gcc_cxxdiag_length_specs gcc_diag_length_specs
424
425 /* This differs from printf_length_specs only in that "Z" is not accepted. */
426 static const format_length_info scanf_length_specs[] =
427 {
428 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99, 0 },
429 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L, 0 },
430 { "q", FMT_LEN_ll, STD_EXT, NO_FMT, 0 },
431 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
432 { "z", FMT_LEN_z, STD_C99, NO_FMT, 0 },
433 { "t", FMT_LEN_t, STD_C99, NO_FMT, 0 },
434 { "j", FMT_LEN_j, STD_C99, NO_FMT, 0 },
435 { "H", FMT_LEN_H, STD_EXT, NO_FMT, 0 },
436 { "D", FMT_LEN_D, STD_EXT, "DD", FMT_LEN_DD, STD_EXT, 0 },
437 { NO_FMT, NO_FMT, 0 }
438 };
439
440
441 /* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings
442 make no sense for a format type not part of any C standard version. */
443 static const format_length_info strfmon_length_specs[] =
444 {
445 /* A GNU extension. */
446 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
447 { NO_FMT, NO_FMT, 0 }
448 };
449
450
451 /* For now, the Fortran front-end routines only use l as length modifier. */
452 static const format_length_info gcc_gfc_length_specs[] =
453 {
454 { "l", FMT_LEN_l, STD_C89, NO_FMT, 0 },
455 { NO_FMT, NO_FMT, 0 }
456 };
457
458
459 static const format_flag_spec printf_flag_specs[] =
460 {
461 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
462 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
463 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
464 { '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
465 { '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
466 { '\'', 0, 0, N_("''' flag"), N_("the ''' printf flag"), STD_EXT },
467 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' printf flag"), STD_EXT },
468 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
469 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
470 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
471 { 0, 0, 0, NULL, NULL, STD_C89 }
472 };
473
474
475 static const format_flag_pair printf_flag_pairs[] =
476 {
477 { ' ', '+', 1, 0 },
478 { '0', '-', 1, 0 },
479 { '0', 'p', 1, 'i' },
480 { 0, 0, 0, 0 }
481 };
482
483 static const format_flag_spec asm_fprintf_flag_specs[] =
484 {
485 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
486 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
487 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
488 { '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
489 { '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
490 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
491 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
492 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
493 { 0, 0, 0, NULL, NULL, STD_C89 }
494 };
495
496 static const format_flag_pair asm_fprintf_flag_pairs[] =
497 {
498 { ' ', '+', 1, 0 },
499 { '0', '-', 1, 0 },
500 { '0', 'p', 1, 'i' },
501 { 0, 0, 0, 0 }
502 };
503
504 static const format_flag_pair gcc_diag_flag_pairs[] =
505 {
506 { 0, 0, 0, 0 }
507 };
508
509 #define gcc_tdiag_flag_pairs gcc_diag_flag_pairs
510 #define gcc_cdiag_flag_pairs gcc_diag_flag_pairs
511 #define gcc_cxxdiag_flag_pairs gcc_diag_flag_pairs
512
513 static const format_flag_pair gcc_gfc_flag_pairs[] =
514 {
515 { 0, 0, 0, 0 }
516 };
517
518 static const format_flag_spec gcc_diag_flag_specs[] =
519 {
520 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
521 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
522 { 'q', 0, 0, N_("'q' flag"), N_("the 'q' diagnostic flag"), STD_C89 },
523 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
524 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
525 { 0, 0, 0, NULL, NULL, STD_C89 }
526 };
527
528 #define gcc_tdiag_flag_specs gcc_diag_flag_specs
529 #define gcc_cdiag_flag_specs gcc_diag_flag_specs
530 #define gcc_cxxdiag_flag_specs gcc_diag_flag_specs
531
532 static const format_flag_spec scanf_flag_specs[] =
533 {
534 { '*', 0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89 },
535 { 'a', 0, 0, N_("'a' flag"), N_("the 'a' scanf flag"), STD_EXT },
536 { 'm', 0, 0, N_("'m' flag"), N_("the 'm' scanf flag"), STD_EXT },
537 { 'w', 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89 },
538 { 'L', 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89 },
539 { '\'', 0, 0, N_("''' flag"), N_("the ''' scanf flag"), STD_EXT },
540 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' scanf flag"), STD_EXT },
541 { 0, 0, 0, NULL, NULL, STD_C89 }
542 };
543
544
545 static const format_flag_pair scanf_flag_pairs[] =
546 {
547 { '*', 'L', 0, 0 },
548 { 'a', 'm', 0, 0 },
549 { 0, 0, 0, 0 }
550 };
551
552
553 static const format_flag_spec strftime_flag_specs[] =
554 {
555 { '_', 0, 0, N_("'_' flag"), N_("the '_' strftime flag"), STD_EXT },
556 { '-', 0, 0, N_("'-' flag"), N_("the '-' strftime flag"), STD_EXT },
557 { '0', 0, 0, N_("'0' flag"), N_("the '0' strftime flag"), STD_EXT },
558 { '^', 0, 0, N_("'^' flag"), N_("the '^' strftime flag"), STD_EXT },
559 { '#', 0, 0, N_("'#' flag"), N_("the '#' strftime flag"), STD_EXT },
560 { 'w', 0, 0, N_("field width"), N_("field width in strftime format"), STD_EXT },
561 { 'E', 0, 0, N_("'E' modifier"), N_("the 'E' strftime modifier"), STD_C99 },
562 { 'O', 0, 0, N_("'O' modifier"), N_("the 'O' strftime modifier"), STD_C99 },
563 { 'O', 'o', 0, NULL, N_("the 'O' modifier"), STD_EXT },
564 { 0, 0, 0, NULL, NULL, STD_C89 }
565 };
566
567
568 static const format_flag_pair strftime_flag_pairs[] =
569 {
570 { 'E', 'O', 0, 0 },
571 { '_', '-', 0, 0 },
572 { '_', '0', 0, 0 },
573 { '-', '0', 0, 0 },
574 { '^', '#', 0, 0 },
575 { 0, 0, 0, 0 }
576 };
577
578
579 static const format_flag_spec strfmon_flag_specs[] =
580 {
581 { '=', 0, 1, N_("fill character"), N_("fill character in strfmon format"), STD_C89 },
582 { '^', 0, 0, N_("'^' flag"), N_("the '^' strfmon flag"), STD_C89 },
583 { '+', 0, 0, N_("'+' flag"), N_("the '+' strfmon flag"), STD_C89 },
584 { '(', 0, 0, N_("'(' flag"), N_("the '(' strfmon flag"), STD_C89 },
585 { '!', 0, 0, N_("'!' flag"), N_("the '!' strfmon flag"), STD_C89 },
586 { '-', 0, 0, N_("'-' flag"), N_("the '-' strfmon flag"), STD_C89 },
587 { 'w', 0, 0, N_("field width"), N_("field width in strfmon format"), STD_C89 },
588 { '#', 0, 0, N_("left precision"), N_("left precision in strfmon format"), STD_C89 },
589 { 'p', 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89 },
590 { 'L', 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89 },
591 { 0, 0, 0, NULL, NULL, STD_C89 }
592 };
593
594 static const format_flag_pair strfmon_flag_pairs[] =
595 {
596 { '+', '(', 0, 0 },
597 { 0, 0, 0, 0 }
598 };
599
600
601 static const format_char_info print_char_table[] =
602 {
603 /* C89 conversion specifiers. */
604 { "di", 0, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, TEX_LL, T99_SST, T99_PD, T99_IM, BADLEN, BADLEN, BADLEN }, "-wp0 +'I", "i", NULL },
605 { "oxX", 0, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM, BADLEN, BADLEN, BADLEN }, "-wp0#", "i", NULL },
606 { "u", 0, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM, BADLEN, BADLEN, BADLEN }, "-wp0'I", "i", NULL },
607 { "fgG", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, TEX_D32, TEX_D64, TEX_D128 }, "-wp0 +#'I", "", NULL },
608 { "eE", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, TEX_D32, TEX_D64, TEX_D128 }, "-wp0 +#I", "", NULL },
609 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, T94_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
610 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
611 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "c", NULL },
612 { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, BADLEN, T99_SST, T99_PD, T99_IM, BADLEN, BADLEN, BADLEN }, "", "W", NULL },
613 /* C99 conversion specifiers. */
614 { "F", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, TEX_D32, TEX_D64, TEX_D128 }, "-wp0 +#'I", "", NULL },
615 { "aA", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "", NULL },
616 /* X/Open conversion specifiers. */
617 { "C", 0, STD_EXT, { TEX_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
618 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "R", NULL },
619 /* GNU conversion specifiers. */
620 { "m", 0, STD_EXT, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "", NULL },
621 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
622 };
623
624 static const format_char_info asm_fprintf_char_table[] =
625 {
626 /* C89 conversion specifiers. */
627 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +", "i", NULL },
628 { "oxX", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0#", "i", NULL },
629 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0", "i", NULL },
630 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
631 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
632
633 /* asm_fprintf conversion specifiers. */
634 { "O", 0, STD_C89, NOARGUMENTS, "", "", NULL },
635 { "R", 0, STD_C89, NOARGUMENTS, "", "", NULL },
636 { "I", 0, STD_C89, NOARGUMENTS, "", "", NULL },
637 { "L", 0, STD_C89, NOARGUMENTS, "", "", NULL },
638 { "U", 0, STD_C89, NOARGUMENTS, "", "", NULL },
639 { "r", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
640 { "@", 0, STD_C89, NOARGUMENTS, "", "", NULL },
641 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
642 };
643
644 static const format_char_info gcc_diag_char_table[] =
645 {
646 /* C89 conversion specifiers. */
647 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
648 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
649 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
650 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
651 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
652 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
653
654 /* Custom conversion specifiers. */
655
656 /* These will require a "tree" at runtime. */
657 { "K", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
658
659 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
660 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
661 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
662 };
663
664 static const format_char_info gcc_tdiag_char_table[] =
665 {
666 /* C89 conversion specifiers. */
667 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
668 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
669 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
670 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
671 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
672 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
673
674 /* Custom conversion specifiers. */
675
676 /* These will require a "tree" at runtime. */
677 { "DFKTEV", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
678
679 { "v", 0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
680
681 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
682 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
683 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
684 };
685
686 static const format_char_info gcc_cdiag_char_table[] =
687 {
688 /* C89 conversion specifiers. */
689 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
690 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
691 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
692 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
693 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
694 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
695
696 /* Custom conversion specifiers. */
697
698 /* These will require a "tree" at runtime. */
699 { "DEFKTV", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
700
701 { "v", 0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
702
703 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
704 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
705 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
706 };
707
708 static const format_char_info gcc_cxxdiag_char_table[] =
709 {
710 /* C89 conversion specifiers. */
711 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
712 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
713 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
714 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
715 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
716 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
717
718 /* Custom conversion specifiers. */
719
720 /* These will require a "tree" at runtime. */
721 { "ADEFKSTV",0,STD_C89,{ T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+#", "", NULL },
722
723 { "v", 0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
724
725 /* These accept either an 'int' or an 'enum tree_code' (which is handled as an 'int'.) */
726 { "CLOPQ",0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
727
728 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
729 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
730 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
731 };
732
733 static const format_char_info gcc_gfc_char_table[] =
734 {
735 /* C89 conversion specifiers. */
736 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
737 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
738 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
739 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL },
740
741 /* gfc conversion specifiers. */
742
743 { "C", 0, STD_C89, NOARGUMENTS, "", "", NULL },
744
745 /* This will require a "locus" at runtime. */
746 { "L", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "R", NULL },
747
748 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
749 };
750
751 static const format_char_info scan_char_table[] =
752 {
753 /* C89 conversion specifiers. */
754 { "di", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, TEX_LL, T99_SST, T99_PD, T99_IM, BADLEN, BADLEN, BADLEN }, "*w'I", "W", NULL },
755 { "u", 1, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM, BADLEN, BADLEN, BADLEN }, "*w'I", "W", NULL },
756 { "oxX", 1, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
757 { "efgEG", 1, STD_C89, { T89_F, BADLEN, BADLEN, T89_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, TEX_D32, TEX_D64, TEX_D128 }, "*w'", "W", NULL },
758 { "c", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*mw", "cW", NULL },
759 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "cW", NULL },
760 { "[", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "cW[", NULL },
761 { "p", 2, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
762 { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, BADLEN, T99_SST, T99_PD, T99_IM, BADLEN, BADLEN, BADLEN }, "", "W", NULL },
763 /* C99 conversion specifiers. */
764 { "F", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, TEX_D32, TEX_D64, TEX_D128 }, "*w'", "W", NULL },
765 { "aA", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w'", "W", NULL },
766 /* X/Open conversion specifiers. */
767 { "C", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*mw", "W", NULL },
768 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "W", NULL },
769 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
770 };
771
772 static const format_char_info time_char_table[] =
773 {
774 /* C89 conversion specifiers. */
775 { "ABZab", 0, STD_C89, NOLENGTHS, "^#", "", NULL },
776 { "cx", 0, STD_C89, NOLENGTHS, "E", "3", NULL },
777 { "HIMSUWdmw", 0, STD_C89, NOLENGTHS, "-_0Ow", "", NULL },
778 { "j", 0, STD_C89, NOLENGTHS, "-_0Ow", "o", NULL },
779 { "p", 0, STD_C89, NOLENGTHS, "#", "", NULL },
780 { "X", 0, STD_C89, NOLENGTHS, "E", "", NULL },
781 { "y", 0, STD_C89, NOLENGTHS, "EO-_0w", "4", NULL },
782 { "Y", 0, STD_C89, NOLENGTHS, "-_0EOw", "o", NULL },
783 { "%", 0, STD_C89, NOLENGTHS, "", "", NULL },
784 /* C99 conversion specifiers. */
785 { "C", 0, STD_C99, NOLENGTHS, "-_0EOw", "o", NULL },
786 { "D", 0, STD_C99, NOLENGTHS, "", "2", NULL },
787 { "eVu", 0, STD_C99, NOLENGTHS, "-_0Ow", "", NULL },
788 { "FRTnrt", 0, STD_C99, NOLENGTHS, "", "", NULL },
789 { "g", 0, STD_C99, NOLENGTHS, "O-_0w", "2o", NULL },
790 { "G", 0, STD_C99, NOLENGTHS, "-_0Ow", "o", NULL },
791 { "h", 0, STD_C99, NOLENGTHS, "^#", "", NULL },
792 { "z", 0, STD_C99, NOLENGTHS, "O", "o", NULL },
793 /* GNU conversion specifiers. */
794 { "kls", 0, STD_EXT, NOLENGTHS, "-_0Ow", "", NULL },
795 { "P", 0, STD_EXT, NOLENGTHS, "", "", NULL },
796 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
797 };
798
799 static const format_char_info monetary_char_table[] =
800 {
801 { "in", 0, STD_C89, { T89_D, BADLEN, BADLEN, BADLEN, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "=^+(!-w#p", "", NULL },
802 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
803 };
804
805 /* This must be in the same order as enum format_type. */
806 static const format_kind_info format_types_orig[] =
807 {
808 { "gnu_printf", printf_length_specs, print_char_table, " +#0-'I", NULL,
809 printf_flag_specs, printf_flag_pairs,
810 FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_EMPTY_PREC_OK,
811 'w', 0, 'p', 0, 'L', 0,
812 &integer_type_node, &integer_type_node
813 },
814 { "asm_fprintf", asm_fprintf_length_specs, asm_fprintf_char_table, " +#0-", NULL,
815 asm_fprintf_flag_specs, asm_fprintf_flag_pairs,
816 FMT_FLAG_ARG_CONVERT|FMT_FLAG_EMPTY_PREC_OK,
817 'w', 0, 'p', 0, 'L', 0,
818 NULL, NULL
819 },
820 { "gcc_diag", gcc_diag_length_specs, gcc_diag_char_table, "q+#", NULL,
821 gcc_diag_flag_specs, gcc_diag_flag_pairs,
822 FMT_FLAG_ARG_CONVERT,
823 0, 0, 'p', 0, 'L', 0,
824 NULL, &integer_type_node
825 },
826 { "gcc_tdiag", gcc_tdiag_length_specs, gcc_tdiag_char_table, "q+#", NULL,
827 gcc_tdiag_flag_specs, gcc_tdiag_flag_pairs,
828 FMT_FLAG_ARG_CONVERT,
829 0, 0, 'p', 0, 'L', 0,
830 NULL, &integer_type_node
831 },
832 { "gcc_cdiag", gcc_cdiag_length_specs, gcc_cdiag_char_table, "q+#", NULL,
833 gcc_cdiag_flag_specs, gcc_cdiag_flag_pairs,
834 FMT_FLAG_ARG_CONVERT,
835 0, 0, 'p', 0, 'L', 0,
836 NULL, &integer_type_node
837 },
838 { "gcc_cxxdiag", gcc_cxxdiag_length_specs, gcc_cxxdiag_char_table, "q+#", NULL,
839 gcc_cxxdiag_flag_specs, gcc_cxxdiag_flag_pairs,
840 FMT_FLAG_ARG_CONVERT,
841 0, 0, 'p', 0, 'L', 0,
842 NULL, &integer_type_node
843 },
844 { "gcc_gfc", gcc_gfc_length_specs, gcc_gfc_char_table, "", NULL,
845 NULL, gcc_gfc_flag_pairs,
846 FMT_FLAG_ARG_CONVERT,
847 0, 0, 0, 0, 0, 0,
848 NULL, NULL
849 },
850 { "NSString", NULL, NULL, NULL, NULL,
851 NULL, NULL,
852 FMT_FLAG_ARG_CONVERT|FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL, 0, 0, 0, 0, 0, 0,
853 NULL, NULL
854 },
855 { "gnu_scanf", scanf_length_specs, scan_char_table, "*'I", NULL,
856 scanf_flag_specs, scanf_flag_pairs,
857 FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_ZERO_WIDTH_BAD|FMT_FLAG_DOLLAR_GAP_POINTER_OK,
858 'w', 0, 0, '*', 'L', 'm',
859 NULL, NULL
860 },
861 { "gnu_strftime", NULL, time_char_table, "_-0^#", "EO",
862 strftime_flag_specs, strftime_flag_pairs,
863 FMT_FLAG_FANCY_PERCENT_OK, 'w', 0, 0, 0, 0, 0,
864 NULL, NULL
865 },
866 { "gnu_strfmon", strfmon_length_specs, monetary_char_table, "=^+(!-", NULL,
867 strfmon_flag_specs, strfmon_flag_pairs,
868 FMT_FLAG_ARG_CONVERT, 'w', '#', 'p', 0, 'L', 0,
869 NULL, NULL
870 }
871 };
872
873 /* This layer of indirection allows GCC to reassign format_types with
874 new data if necessary, while still allowing the original data to be
875 const. */
876 static const format_kind_info *format_types = format_types_orig;
877 /* We can modify this one. We also add target-specific format types
878 to the end of the array. */
879 static format_kind_info *dynamic_format_types;
880
881 static int n_format_types = ARRAY_SIZE (format_types_orig);
882
883 /* Structure detailing the results of checking a format function call
884 where the format expression may be a conditional expression with
885 many leaves resulting from nested conditional expressions. */
886 typedef struct
887 {
888 /* Number of leaves of the format argument that could not be checked
889 as they were not string literals. */
890 int number_non_literal;
891 /* Number of leaves of the format argument that were null pointers or
892 string literals, but had extra format arguments. */
893 int number_extra_args;
894 /* Number of leaves of the format argument that were null pointers or
895 string literals, but had extra format arguments and used $ operand
896 numbers. */
897 int number_dollar_extra_args;
898 /* Number of leaves of the format argument that were wide string
899 literals. */
900 int number_wide;
901 /* Number of leaves of the format argument that were empty strings. */
902 int number_empty;
903 /* Number of leaves of the format argument that were unterminated
904 strings. */
905 int number_unterminated;
906 /* Number of leaves of the format argument that were not counted above. */
907 int number_other;
908 } format_check_results;
909
910 typedef struct
911 {
912 format_check_results *res;
913 function_format_info *info;
914 tree params;
915 } format_check_context;
916
917 /* Return the format name (as specified in the original table) for the format
918 type indicated by format_num. */
919 static const char *
format_name(int format_num)920 format_name (int format_num)
921 {
922 if (format_num >= 0 && format_num < n_format_types)
923 return format_types[format_num].name;
924 gcc_unreachable ();
925 }
926
927 /* Return the format flags (as specified in the original table) for the format
928 type indicated by format_num. */
929 static int
format_flags(int format_num)930 format_flags (int format_num)
931 {
932 if (format_num >= 0 && format_num < n_format_types)
933 return format_types[format_num].flags;
934 gcc_unreachable ();
935 }
936
937 static void check_format_info (function_format_info *, tree);
938 static void check_format_arg (void *, tree, unsigned HOST_WIDE_INT);
939 static void check_format_info_main (format_check_results *,
940 function_format_info *,
941 const char *, int, tree,
942 unsigned HOST_WIDE_INT, alloc_pool);
943
944 static void init_dollar_format_checking (int, tree);
945 static int maybe_read_dollar_number (const char **, int,
946 tree, tree *, const format_kind_info *);
947 static bool avoid_dollar_number (const char *);
948 static void finish_dollar_format_checking (format_check_results *, int);
949
950 static const format_flag_spec *get_flag_spec (const format_flag_spec *,
951 int, const char *);
952
953 static void check_format_types (format_wanted_type *);
954 static void format_type_warning (format_wanted_type *, tree, tree);
955
956 /* Decode a format type from a string, returning the type, or
957 format_type_error if not valid, in which case the caller should print an
958 error message. */
959 static int
decode_format_type(const char * s)960 decode_format_type (const char *s)
961 {
962 int i;
963 int slen;
964
965 s = convert_format_name_to_system_name (s);
966 slen = strlen (s);
967 for (i = 0; i < n_format_types; i++)
968 {
969 int alen;
970 if (!strcmp (s, format_types[i].name))
971 return i;
972 alen = strlen (format_types[i].name);
973 if (slen == alen + 4 && s[0] == '_' && s[1] == '_'
974 && s[slen - 1] == '_' && s[slen - 2] == '_'
975 && !strncmp (s + 2, format_types[i].name, alen))
976 return i;
977 }
978 return format_type_error;
979 }
980
981
982 /* Check the argument list of a call to printf, scanf, etc.
983 ATTRS are the attributes on the function type. There are NARGS argument
984 values in the array ARGARRAY.
985 Also, if -Wsuggest-attribute=format,
986 warn for calls to vprintf or vscanf in functions with no such format
987 attribute themselves. */
988
989 void
check_function_format(tree attrs,int nargs,tree * argarray)990 check_function_format (tree attrs, int nargs, tree *argarray)
991 {
992 tree a;
993
994 /* See if this function has any format attributes. */
995 for (a = attrs; a; a = TREE_CHAIN (a))
996 {
997 if (is_attribute_p ("format", TREE_PURPOSE (a)))
998 {
999 /* Yup; check it. */
1000 function_format_info info;
1001 decode_format_attr (TREE_VALUE (a), &info, 1);
1002 if (warn_format)
1003 {
1004 /* FIXME: Rewrite all the internal functions in this file
1005 to use the ARGARRAY directly instead of constructing this
1006 temporary list. */
1007 tree params = NULL_TREE;
1008 int i;
1009 for (i = nargs - 1; i >= 0; i--)
1010 params = tree_cons (NULL_TREE, argarray[i], params);
1011 check_format_info (&info, params);
1012 }
1013 if (warn_suggest_attribute_format && info.first_arg_num == 0
1014 && (format_types[info.format_type].flags
1015 & (int) FMT_FLAG_ARG_CONVERT))
1016 {
1017 tree c;
1018 for (c = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
1019 c;
1020 c = TREE_CHAIN (c))
1021 if (is_attribute_p ("format", TREE_PURPOSE (c))
1022 && (decode_format_type (IDENTIFIER_POINTER
1023 (TREE_VALUE (TREE_VALUE (c))))
1024 == info.format_type))
1025 break;
1026 if (c == NULL_TREE)
1027 {
1028 /* Check if the current function has a parameter to which
1029 the format attribute could be attached; if not, it
1030 can't be a candidate for a format attribute, despite
1031 the vprintf-like or vscanf-like call. */
1032 tree args;
1033 for (args = DECL_ARGUMENTS (current_function_decl);
1034 args != 0;
1035 args = DECL_CHAIN (args))
1036 {
1037 if (TREE_CODE (TREE_TYPE (args)) == POINTER_TYPE
1038 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args)))
1039 == char_type_node))
1040 break;
1041 }
1042 if (args != 0)
1043 warning (OPT_Wsuggest_attribute_format, "function might "
1044 "be possible candidate for %qs format attribute",
1045 format_types[info.format_type].name);
1046 }
1047 }
1048 }
1049 }
1050 }
1051
1052
1053 /* Variables used by the checking of $ operand number formats. */
1054 static char *dollar_arguments_used = NULL;
1055 static char *dollar_arguments_pointer_p = NULL;
1056 static int dollar_arguments_alloc = 0;
1057 static int dollar_arguments_count;
1058 static int dollar_first_arg_num;
1059 static int dollar_max_arg_used;
1060 static int dollar_format_warned;
1061
1062 /* Initialize the checking for a format string that may contain $
1063 parameter number specifications; we will need to keep track of whether
1064 each parameter has been used. FIRST_ARG_NUM is the number of the first
1065 argument that is a parameter to the format, or 0 for a vprintf-style
1066 function; PARAMS is the list of arguments starting at this argument. */
1067
1068 static void
init_dollar_format_checking(int first_arg_num,tree params)1069 init_dollar_format_checking (int first_arg_num, tree params)
1070 {
1071 tree oparams = params;
1072
1073 dollar_first_arg_num = first_arg_num;
1074 dollar_arguments_count = 0;
1075 dollar_max_arg_used = 0;
1076 dollar_format_warned = 0;
1077 if (first_arg_num > 0)
1078 {
1079 while (params)
1080 {
1081 dollar_arguments_count++;
1082 params = TREE_CHAIN (params);
1083 }
1084 }
1085 if (dollar_arguments_alloc < dollar_arguments_count)
1086 {
1087 free (dollar_arguments_used);
1088 free (dollar_arguments_pointer_p);
1089 dollar_arguments_alloc = dollar_arguments_count;
1090 dollar_arguments_used = XNEWVEC (char, dollar_arguments_alloc);
1091 dollar_arguments_pointer_p = XNEWVEC (char, dollar_arguments_alloc);
1092 }
1093 if (dollar_arguments_alloc)
1094 {
1095 memset (dollar_arguments_used, 0, dollar_arguments_alloc);
1096 if (first_arg_num > 0)
1097 {
1098 int i = 0;
1099 params = oparams;
1100 while (params)
1101 {
1102 dollar_arguments_pointer_p[i] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params)))
1103 == POINTER_TYPE);
1104 params = TREE_CHAIN (params);
1105 i++;
1106 }
1107 }
1108 }
1109 }
1110
1111
1112 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
1113 is set, it is an error if one is not found; otherwise, it is OK. If
1114 such a number is found, check whether it is within range and mark that
1115 numbered operand as being used for later checking. Returns the operand
1116 number if found and within range, zero if no such number was found and
1117 this is OK, or -1 on error. PARAMS points to the first operand of the
1118 format; PARAM_PTR is made to point to the parameter referred to. If
1119 a $ format is found, *FORMAT is updated to point just after it. */
1120
1121 static int
maybe_read_dollar_number(const char ** format,int dollar_needed,tree params,tree * param_ptr,const format_kind_info * fki)1122 maybe_read_dollar_number (const char **format,
1123 int dollar_needed, tree params, tree *param_ptr,
1124 const format_kind_info *fki)
1125 {
1126 int argnum;
1127 int overflow_flag;
1128 const char *fcp = *format;
1129 if (!ISDIGIT (*fcp))
1130 {
1131 if (dollar_needed)
1132 {
1133 warning (OPT_Wformat_, "missing $ operand number in format");
1134 return -1;
1135 }
1136 else
1137 return 0;
1138 }
1139 argnum = 0;
1140 overflow_flag = 0;
1141 while (ISDIGIT (*fcp))
1142 {
1143 int nargnum;
1144 nargnum = 10 * argnum + (*fcp - '0');
1145 if (nargnum < 0 || nargnum / 10 != argnum)
1146 overflow_flag = 1;
1147 argnum = nargnum;
1148 fcp++;
1149 }
1150 if (*fcp != '$')
1151 {
1152 if (dollar_needed)
1153 {
1154 warning (OPT_Wformat_, "missing $ operand number in format");
1155 return -1;
1156 }
1157 else
1158 return 0;
1159 }
1160 *format = fcp + 1;
1161 if (pedantic && !dollar_format_warned)
1162 {
1163 warning (OPT_Wformat_, "%s does not support %%n$ operand number formats",
1164 C_STD_NAME (STD_EXT));
1165 dollar_format_warned = 1;
1166 }
1167 if (overflow_flag || argnum == 0
1168 || (dollar_first_arg_num && argnum > dollar_arguments_count))
1169 {
1170 warning (OPT_Wformat_, "operand number out of range in format");
1171 return -1;
1172 }
1173 if (argnum > dollar_max_arg_used)
1174 dollar_max_arg_used = argnum;
1175 /* For vprintf-style functions we may need to allocate more memory to
1176 track which arguments are used. */
1177 while (dollar_arguments_alloc < dollar_max_arg_used)
1178 {
1179 int nalloc;
1180 nalloc = 2 * dollar_arguments_alloc + 16;
1181 dollar_arguments_used = XRESIZEVEC (char, dollar_arguments_used,
1182 nalloc);
1183 dollar_arguments_pointer_p = XRESIZEVEC (char, dollar_arguments_pointer_p,
1184 nalloc);
1185 memset (dollar_arguments_used + dollar_arguments_alloc, 0,
1186 nalloc - dollar_arguments_alloc);
1187 dollar_arguments_alloc = nalloc;
1188 }
1189 if (!(fki->flags & (int) FMT_FLAG_DOLLAR_MULTIPLE)
1190 && dollar_arguments_used[argnum - 1] == 1)
1191 {
1192 dollar_arguments_used[argnum - 1] = 2;
1193 warning (OPT_Wformat_, "format argument %d used more than once in %s format",
1194 argnum, fki->name);
1195 }
1196 else
1197 dollar_arguments_used[argnum - 1] = 1;
1198 if (dollar_first_arg_num)
1199 {
1200 int i;
1201 *param_ptr = params;
1202 for (i = 1; i < argnum && *param_ptr != 0; i++)
1203 *param_ptr = TREE_CHAIN (*param_ptr);
1204
1205 /* This case shouldn't be caught here. */
1206 gcc_assert (*param_ptr);
1207 }
1208 else
1209 *param_ptr = 0;
1210 return argnum;
1211 }
1212
1213 /* Ensure that FORMAT does not start with a decimal number followed by
1214 a $; give a diagnostic and return true if it does, false otherwise. */
1215
1216 static bool
avoid_dollar_number(const char * format)1217 avoid_dollar_number (const char *format)
1218 {
1219 if (!ISDIGIT (*format))
1220 return false;
1221 while (ISDIGIT (*format))
1222 format++;
1223 if (*format == '$')
1224 {
1225 warning (OPT_Wformat_, "$ operand number used after format without operand number");
1226 return true;
1227 }
1228 return false;
1229 }
1230
1231
1232 /* Finish the checking for a format string that used $ operand number formats
1233 instead of non-$ formats. We check for unused operands before used ones
1234 (a serious error, since the implementation of the format function
1235 can't know what types to pass to va_arg to find the later arguments).
1236 and for unused operands at the end of the format (if we know how many
1237 arguments the format had, so not for vprintf). If there were operand
1238 numbers out of range on a non-vprintf-style format, we won't have reached
1239 here. If POINTER_GAP_OK, unused arguments are OK if all arguments are
1240 pointers. */
1241
1242 static void
finish_dollar_format_checking(format_check_results * res,int pointer_gap_ok)1243 finish_dollar_format_checking (format_check_results *res, int pointer_gap_ok)
1244 {
1245 int i;
1246 bool found_pointer_gap = false;
1247 for (i = 0; i < dollar_max_arg_used; i++)
1248 {
1249 if (!dollar_arguments_used[i])
1250 {
1251 if (pointer_gap_ok && (dollar_first_arg_num == 0
1252 || dollar_arguments_pointer_p[i]))
1253 found_pointer_gap = true;
1254 else
1255 warning (OPT_Wformat_,
1256 "format argument %d unused before used argument %d in $-style format",
1257 i + 1, dollar_max_arg_used);
1258 }
1259 }
1260 if (found_pointer_gap
1261 || (dollar_first_arg_num
1262 && dollar_max_arg_used < dollar_arguments_count))
1263 {
1264 res->number_other--;
1265 res->number_dollar_extra_args++;
1266 }
1267 }
1268
1269
1270 /* Retrieve the specification for a format flag. SPEC contains the
1271 specifications for format flags for the applicable kind of format.
1272 FLAG is the flag in question. If PREDICATES is NULL, the basic
1273 spec for that flag must be retrieved and must exist. If
1274 PREDICATES is not NULL, it is a string listing possible predicates
1275 for the spec entry; if an entry predicated on any of these is
1276 found, it is returned, otherwise NULL is returned. */
1277
1278 static const format_flag_spec *
get_flag_spec(const format_flag_spec * spec,int flag,const char * predicates)1279 get_flag_spec (const format_flag_spec *spec, int flag, const char *predicates)
1280 {
1281 int i;
1282 for (i = 0; spec[i].flag_char != 0; i++)
1283 {
1284 if (spec[i].flag_char != flag)
1285 continue;
1286 if (predicates != NULL)
1287 {
1288 if (spec[i].predicate != 0
1289 && strchr (predicates, spec[i].predicate) != 0)
1290 return &spec[i];
1291 }
1292 else if (spec[i].predicate == 0)
1293 return &spec[i];
1294 }
1295 gcc_assert (predicates);
1296 return NULL;
1297 }
1298
1299
1300 /* Check the argument list of a call to printf, scanf, etc.
1301 INFO points to the function_format_info structure.
1302 PARAMS is the list of argument values. */
1303
1304 static void
check_format_info(function_format_info * info,tree params)1305 check_format_info (function_format_info *info, tree params)
1306 {
1307 format_check_context format_ctx;
1308 unsigned HOST_WIDE_INT arg_num;
1309 tree format_tree;
1310 format_check_results res;
1311 /* Skip to format argument. If the argument isn't available, there's
1312 no work for us to do; prototype checking will catch the problem. */
1313 for (arg_num = 1; ; ++arg_num)
1314 {
1315 if (params == 0)
1316 return;
1317 if (arg_num == info->format_num)
1318 break;
1319 params = TREE_CHAIN (params);
1320 }
1321 format_tree = TREE_VALUE (params);
1322 params = TREE_CHAIN (params);
1323 if (format_tree == 0)
1324 return;
1325
1326 res.number_non_literal = 0;
1327 res.number_extra_args = 0;
1328 res.number_dollar_extra_args = 0;
1329 res.number_wide = 0;
1330 res.number_empty = 0;
1331 res.number_unterminated = 0;
1332 res.number_other = 0;
1333
1334 format_ctx.res = &res;
1335 format_ctx.info = info;
1336 format_ctx.params = params;
1337
1338 check_function_arguments_recurse (check_format_arg, &format_ctx,
1339 format_tree, arg_num);
1340
1341 if (res.number_non_literal > 0)
1342 {
1343 /* Functions taking a va_list normally pass a non-literal format
1344 string. These functions typically are declared with
1345 first_arg_num == 0, so avoid warning in those cases. */
1346 if (!(format_types[info->format_type].flags & (int) FMT_FLAG_ARG_CONVERT))
1347 {
1348 /* For strftime-like formats, warn for not checking the format
1349 string; but there are no arguments to check. */
1350 warning (OPT_Wformat_nonliteral,
1351 "format not a string literal, format string not checked");
1352 }
1353 else if (info->first_arg_num != 0)
1354 {
1355 /* If there are no arguments for the format at all, we may have
1356 printf (foo) which is likely to be a security hole. */
1357 while (arg_num + 1 < info->first_arg_num)
1358 {
1359 if (params == 0)
1360 break;
1361 params = TREE_CHAIN (params);
1362 ++arg_num;
1363 }
1364 if (params == 0 && warn_format_security)
1365 warning (OPT_Wformat_security,
1366 "format not a string literal and no format arguments");
1367 else if (params == 0 && warn_format_nonliteral)
1368 warning (OPT_Wformat_nonliteral,
1369 "format not a string literal and no format arguments");
1370 else
1371 warning (OPT_Wformat_nonliteral,
1372 "format not a string literal, argument types not checked");
1373 }
1374 }
1375
1376 /* If there were extra arguments to the format, normally warn. However,
1377 the standard does say extra arguments are ignored, so in the specific
1378 case where we have multiple leaves (conditional expressions or
1379 ngettext) allow extra arguments if at least one leaf didn't have extra
1380 arguments, but was otherwise OK (either non-literal or checked OK).
1381 If the format is an empty string, this should be counted similarly to the
1382 case of extra format arguments. */
1383 if (res.number_extra_args > 0 && res.number_non_literal == 0
1384 && res.number_other == 0)
1385 warning (OPT_Wformat_extra_args, "too many arguments for format");
1386 if (res.number_dollar_extra_args > 0 && res.number_non_literal == 0
1387 && res.number_other == 0)
1388 warning (OPT_Wformat_extra_args, "unused arguments in $-style format");
1389 if (res.number_empty > 0 && res.number_non_literal == 0
1390 && res.number_other == 0)
1391 warning (OPT_Wformat_zero_length, "zero-length %s format string",
1392 format_types[info->format_type].name);
1393
1394 if (res.number_wide > 0)
1395 warning (OPT_Wformat_, "format is a wide character string");
1396
1397 if (res.number_unterminated > 0)
1398 warning (OPT_Wformat_, "unterminated format string");
1399 }
1400
1401 /* Callback from check_function_arguments_recurse to check a
1402 format string. FORMAT_TREE is the format parameter. ARG_NUM
1403 is the number of the format argument. CTX points to a
1404 format_check_context. */
1405
1406 static void
check_format_arg(void * ctx,tree format_tree,unsigned HOST_WIDE_INT arg_num)1407 check_format_arg (void *ctx, tree format_tree,
1408 unsigned HOST_WIDE_INT arg_num)
1409 {
1410 format_check_context *format_ctx = (format_check_context *) ctx;
1411 format_check_results *res = format_ctx->res;
1412 function_format_info *info = format_ctx->info;
1413 tree params = format_ctx->params;
1414
1415 int format_length;
1416 HOST_WIDE_INT offset;
1417 const char *format_chars;
1418 tree array_size = 0;
1419 tree array_init;
1420 alloc_pool fwt_pool;
1421
1422 if (integer_zerop (format_tree))
1423 {
1424 /* Skip to first argument to check, so we can see if this format
1425 has any arguments (it shouldn't). */
1426 while (arg_num + 1 < info->first_arg_num)
1427 {
1428 if (params == 0)
1429 return;
1430 params = TREE_CHAIN (params);
1431 ++arg_num;
1432 }
1433
1434 if (params == 0)
1435 res->number_other++;
1436 else
1437 res->number_extra_args++;
1438
1439 return;
1440 }
1441
1442 offset = 0;
1443 if (TREE_CODE (format_tree) == POINTER_PLUS_EXPR)
1444 {
1445 tree arg0, arg1;
1446
1447 arg0 = TREE_OPERAND (format_tree, 0);
1448 arg1 = TREE_OPERAND (format_tree, 1);
1449 STRIP_NOPS (arg0);
1450 STRIP_NOPS (arg1);
1451 if (TREE_CODE (arg1) == INTEGER_CST)
1452 format_tree = arg0;
1453 else
1454 {
1455 res->number_non_literal++;
1456 return;
1457 }
1458 if (!host_integerp (arg1, 0)
1459 || (offset = tree_low_cst (arg1, 0)) < 0)
1460 {
1461 res->number_non_literal++;
1462 return;
1463 }
1464 }
1465 if (TREE_CODE (format_tree) != ADDR_EXPR)
1466 {
1467 res->number_non_literal++;
1468 return;
1469 }
1470 format_tree = TREE_OPERAND (format_tree, 0);
1471 if (format_types[info->format_type].flags
1472 & (int) FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL)
1473 {
1474 bool objc_str = (info->format_type == gcc_objc_string_format_type);
1475 /* We cannot examine this string here - but we can check that it is
1476 a valid type. */
1477 if (TREE_CODE (format_tree) != CONST_DECL
1478 || !((objc_str && objc_string_ref_type_p (TREE_TYPE (format_tree)))
1479 || (*targetcm.string_object_ref_type_p)
1480 ((const_tree) TREE_TYPE (format_tree))))
1481 {
1482 res->number_non_literal++;
1483 return;
1484 }
1485 /* Skip to first argument to check. */
1486 while (arg_num + 1 < info->first_arg_num)
1487 {
1488 if (params == 0)
1489 return;
1490 params = TREE_CHAIN (params);
1491 ++arg_num;
1492 }
1493 /* So, we have a valid literal string object and one or more params.
1494 We need to use an external helper to parse the string into format
1495 info. For Objective-C variants we provide the resource within the
1496 objc tree, for target variants, via a hook. */
1497 if (objc_str)
1498 objc_check_format_arg (format_tree, params);
1499 else if (targetcm.check_string_object_format_arg)
1500 (*targetcm.check_string_object_format_arg) (format_tree, params);
1501 /* Else we can't handle it and retire quietly. */
1502 return;
1503 }
1504 if (TREE_CODE (format_tree) == ARRAY_REF
1505 && host_integerp (TREE_OPERAND (format_tree, 1), 0)
1506 && (offset += tree_low_cst (TREE_OPERAND (format_tree, 1), 0)) >= 0)
1507 format_tree = TREE_OPERAND (format_tree, 0);
1508 if (TREE_CODE (format_tree) == VAR_DECL
1509 && TREE_CODE (TREE_TYPE (format_tree)) == ARRAY_TYPE
1510 && (array_init = decl_constant_value (format_tree)) != format_tree
1511 && TREE_CODE (array_init) == STRING_CST)
1512 {
1513 /* Extract the string constant initializer. Note that this may include
1514 a trailing NUL character that is not in the array (e.g.
1515 const char a[3] = "foo";). */
1516 array_size = DECL_SIZE_UNIT (format_tree);
1517 format_tree = array_init;
1518 }
1519 if (TREE_CODE (format_tree) != STRING_CST)
1520 {
1521 res->number_non_literal++;
1522 return;
1523 }
1524 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree))) != char_type_node)
1525 {
1526 res->number_wide++;
1527 return;
1528 }
1529 format_chars = TREE_STRING_POINTER (format_tree);
1530 format_length = TREE_STRING_LENGTH (format_tree);
1531 if (array_size != 0)
1532 {
1533 /* Variable length arrays can't be initialized. */
1534 gcc_assert (TREE_CODE (array_size) == INTEGER_CST);
1535
1536 if (host_integerp (array_size, 0))
1537 {
1538 HOST_WIDE_INT array_size_value = TREE_INT_CST_LOW (array_size);
1539 if (array_size_value > 0
1540 && array_size_value == (int) array_size_value
1541 && format_length > array_size_value)
1542 format_length = array_size_value;
1543 }
1544 }
1545 if (offset)
1546 {
1547 if (offset >= format_length)
1548 {
1549 res->number_non_literal++;
1550 return;
1551 }
1552 format_chars += offset;
1553 format_length -= offset;
1554 }
1555 if (format_length < 1 || format_chars[--format_length] != 0)
1556 {
1557 res->number_unterminated++;
1558 return;
1559 }
1560 if (format_length == 0)
1561 {
1562 res->number_empty++;
1563 return;
1564 }
1565
1566 /* Skip to first argument to check. */
1567 while (arg_num + 1 < info->first_arg_num)
1568 {
1569 if (params == 0)
1570 return;
1571 params = TREE_CHAIN (params);
1572 ++arg_num;
1573 }
1574 /* Provisionally increment res->number_other; check_format_info_main
1575 will decrement it if it finds there are extra arguments, but this way
1576 need not adjust it for every return. */
1577 res->number_other++;
1578 fwt_pool = create_alloc_pool ("format_wanted_type pool",
1579 sizeof (format_wanted_type), 10);
1580 check_format_info_main (res, info, format_chars, format_length,
1581 params, arg_num, fwt_pool);
1582 free_alloc_pool (fwt_pool);
1583 }
1584
1585
1586 /* Do the main part of checking a call to a format function. FORMAT_CHARS
1587 is the NUL-terminated format string (which at this point may contain
1588 internal NUL characters); FORMAT_LENGTH is its length (excluding the
1589 terminating NUL character). ARG_NUM is one less than the number of
1590 the first format argument to check; PARAMS points to that format
1591 argument in the list of arguments. */
1592
1593 static void
check_format_info_main(format_check_results * res,function_format_info * info,const char * format_chars,int format_length,tree params,unsigned HOST_WIDE_INT arg_num,alloc_pool fwt_pool)1594 check_format_info_main (format_check_results *res,
1595 function_format_info *info, const char *format_chars,
1596 int format_length, tree params,
1597 unsigned HOST_WIDE_INT arg_num, alloc_pool fwt_pool)
1598 {
1599 const char *orig_format_chars = format_chars;
1600 tree first_fillin_param = params;
1601
1602 const format_kind_info *fki = &format_types[info->format_type];
1603 const format_flag_spec *flag_specs = fki->flag_specs;
1604 const format_flag_pair *bad_flag_pairs = fki->bad_flag_pairs;
1605
1606 /* -1 if no conversions taking an operand have been found; 0 if one has
1607 and it didn't use $; 1 if $ formats are in use. */
1608 int has_operand_number = -1;
1609
1610 init_dollar_format_checking (info->first_arg_num, first_fillin_param);
1611
1612 while (*format_chars != 0)
1613 {
1614 int i;
1615 int suppressed = FALSE;
1616 const char *length_chars = NULL;
1617 enum format_lengths length_chars_val = FMT_LEN_none;
1618 enum format_std_version length_chars_std = STD_C89;
1619 int format_char;
1620 tree cur_param;
1621 tree wanted_type;
1622 int main_arg_num = 0;
1623 tree main_arg_params = 0;
1624 enum format_std_version wanted_type_std;
1625 const char *wanted_type_name;
1626 format_wanted_type width_wanted_type;
1627 format_wanted_type precision_wanted_type;
1628 format_wanted_type main_wanted_type;
1629 format_wanted_type *first_wanted_type = NULL;
1630 format_wanted_type *last_wanted_type = NULL;
1631 const format_length_info *fli = NULL;
1632 const format_char_info *fci = NULL;
1633 char flag_chars[256];
1634 int alloc_flag = 0;
1635 int scalar_identity_flag = 0;
1636 const char *format_start;
1637
1638 if (*format_chars++ != '%')
1639 continue;
1640 if (*format_chars == 0)
1641 {
1642 warning (OPT_Wformat_, "spurious trailing %<%%%> in format");
1643 continue;
1644 }
1645 if (*format_chars == '%')
1646 {
1647 ++format_chars;
1648 continue;
1649 }
1650 flag_chars[0] = 0;
1651
1652 if ((fki->flags & (int) FMT_FLAG_USE_DOLLAR) && has_operand_number != 0)
1653 {
1654 /* Possibly read a $ operand number at the start of the format.
1655 If one was previously used, one is required here. If one
1656 is not used here, we can't immediately conclude this is a
1657 format without them, since it could be printf %m or scanf %*. */
1658 int opnum;
1659 opnum = maybe_read_dollar_number (&format_chars, 0,
1660 first_fillin_param,
1661 &main_arg_params, fki);
1662 if (opnum == -1)
1663 return;
1664 else if (opnum > 0)
1665 {
1666 has_operand_number = 1;
1667 main_arg_num = opnum + info->first_arg_num - 1;
1668 }
1669 }
1670 else if (fki->flags & FMT_FLAG_USE_DOLLAR)
1671 {
1672 if (avoid_dollar_number (format_chars))
1673 return;
1674 }
1675
1676 /* Read any format flags, but do not yet validate them beyond removing
1677 duplicates, since in general validation depends on the rest of
1678 the format. */
1679 while (*format_chars != 0
1680 && strchr (fki->flag_chars, *format_chars) != 0)
1681 {
1682 const format_flag_spec *s = get_flag_spec (flag_specs,
1683 *format_chars, NULL);
1684 if (strchr (flag_chars, *format_chars) != 0)
1685 {
1686 warning (OPT_Wformat_, "repeated %s in format", _(s->name));
1687 }
1688 else
1689 {
1690 i = strlen (flag_chars);
1691 flag_chars[i++] = *format_chars;
1692 flag_chars[i] = 0;
1693 }
1694 if (s->skip_next_char)
1695 {
1696 ++format_chars;
1697 if (*format_chars == 0)
1698 {
1699 warning (OPT_Wformat_, "missing fill character at end of strfmon format");
1700 return;
1701 }
1702 }
1703 ++format_chars;
1704 }
1705
1706 /* Read any format width, possibly * or *m$. */
1707 if (fki->width_char != 0)
1708 {
1709 if (fki->width_type != NULL && *format_chars == '*')
1710 {
1711 i = strlen (flag_chars);
1712 flag_chars[i++] = fki->width_char;
1713 flag_chars[i] = 0;
1714 /* "...a field width...may be indicated by an asterisk.
1715 In this case, an int argument supplies the field width..." */
1716 ++format_chars;
1717 if (has_operand_number != 0)
1718 {
1719 int opnum;
1720 opnum = maybe_read_dollar_number (&format_chars,
1721 has_operand_number == 1,
1722 first_fillin_param,
1723 ¶ms, fki);
1724 if (opnum == -1)
1725 return;
1726 else if (opnum > 0)
1727 {
1728 has_operand_number = 1;
1729 arg_num = opnum + info->first_arg_num - 1;
1730 }
1731 else
1732 has_operand_number = 0;
1733 }
1734 else
1735 {
1736 if (avoid_dollar_number (format_chars))
1737 return;
1738 }
1739 if (info->first_arg_num != 0)
1740 {
1741 if (params == 0)
1742 cur_param = NULL;
1743 else
1744 {
1745 cur_param = TREE_VALUE (params);
1746 if (has_operand_number <= 0)
1747 {
1748 params = TREE_CHAIN (params);
1749 ++arg_num;
1750 }
1751 }
1752 width_wanted_type.wanted_type = *fki->width_type;
1753 width_wanted_type.wanted_type_name = NULL;
1754 width_wanted_type.pointer_count = 0;
1755 width_wanted_type.char_lenient_flag = 0;
1756 width_wanted_type.scalar_identity_flag = 0;
1757 width_wanted_type.writing_in_flag = 0;
1758 width_wanted_type.reading_from_flag = 0;
1759 width_wanted_type.kind = CF_KIND_FIELD_WIDTH;
1760 width_wanted_type.format_start = format_chars - 1;
1761 width_wanted_type.format_length = 1;
1762 width_wanted_type.param = cur_param;
1763 width_wanted_type.arg_num = arg_num;
1764 width_wanted_type.next = NULL;
1765 if (last_wanted_type != 0)
1766 last_wanted_type->next = &width_wanted_type;
1767 if (first_wanted_type == 0)
1768 first_wanted_type = &width_wanted_type;
1769 last_wanted_type = &width_wanted_type;
1770 }
1771 }
1772 else
1773 {
1774 /* Possibly read a numeric width. If the width is zero,
1775 we complain if appropriate. */
1776 int non_zero_width_char = FALSE;
1777 int found_width = FALSE;
1778 while (ISDIGIT (*format_chars))
1779 {
1780 found_width = TRUE;
1781 if (*format_chars != '0')
1782 non_zero_width_char = TRUE;
1783 ++format_chars;
1784 }
1785 if (found_width && !non_zero_width_char &&
1786 (fki->flags & (int) FMT_FLAG_ZERO_WIDTH_BAD))
1787 warning (OPT_Wformat_, "zero width in %s format", fki->name);
1788 if (found_width)
1789 {
1790 i = strlen (flag_chars);
1791 flag_chars[i++] = fki->width_char;
1792 flag_chars[i] = 0;
1793 }
1794 }
1795 }
1796
1797 /* Read any format left precision (must be a number, not *). */
1798 if (fki->left_precision_char != 0 && *format_chars == '#')
1799 {
1800 ++format_chars;
1801 i = strlen (flag_chars);
1802 flag_chars[i++] = fki->left_precision_char;
1803 flag_chars[i] = 0;
1804 if (!ISDIGIT (*format_chars))
1805 warning (OPT_Wformat_, "empty left precision in %s format", fki->name);
1806 while (ISDIGIT (*format_chars))
1807 ++format_chars;
1808 }
1809
1810 /* Read any format precision, possibly * or *m$. */
1811 if (fki->precision_char != 0 && *format_chars == '.')
1812 {
1813 ++format_chars;
1814 i = strlen (flag_chars);
1815 flag_chars[i++] = fki->precision_char;
1816 flag_chars[i] = 0;
1817 if (fki->precision_type != NULL && *format_chars == '*')
1818 {
1819 /* "...a...precision...may be indicated by an asterisk.
1820 In this case, an int argument supplies the...precision." */
1821 ++format_chars;
1822 if (has_operand_number != 0)
1823 {
1824 int opnum;
1825 opnum = maybe_read_dollar_number (&format_chars,
1826 has_operand_number == 1,
1827 first_fillin_param,
1828 ¶ms, fki);
1829 if (opnum == -1)
1830 return;
1831 else if (opnum > 0)
1832 {
1833 has_operand_number = 1;
1834 arg_num = opnum + info->first_arg_num - 1;
1835 }
1836 else
1837 has_operand_number = 0;
1838 }
1839 else
1840 {
1841 if (avoid_dollar_number (format_chars))
1842 return;
1843 }
1844 if (info->first_arg_num != 0)
1845 {
1846 if (params == 0)
1847 cur_param = NULL;
1848 else
1849 {
1850 cur_param = TREE_VALUE (params);
1851 if (has_operand_number <= 0)
1852 {
1853 params = TREE_CHAIN (params);
1854 ++arg_num;
1855 }
1856 }
1857 precision_wanted_type.wanted_type = *fki->precision_type;
1858 precision_wanted_type.wanted_type_name = NULL;
1859 precision_wanted_type.pointer_count = 0;
1860 precision_wanted_type.char_lenient_flag = 0;
1861 precision_wanted_type.scalar_identity_flag = 0;
1862 precision_wanted_type.writing_in_flag = 0;
1863 precision_wanted_type.reading_from_flag = 0;
1864 precision_wanted_type.kind = CF_KIND_FIELD_PRECISION;
1865 precision_wanted_type.param = cur_param;
1866 precision_wanted_type.format_start = format_chars - 2;
1867 precision_wanted_type.format_length = 2;
1868 precision_wanted_type.arg_num = arg_num;
1869 precision_wanted_type.next = NULL;
1870 if (last_wanted_type != 0)
1871 last_wanted_type->next = &precision_wanted_type;
1872 if (first_wanted_type == 0)
1873 first_wanted_type = &precision_wanted_type;
1874 last_wanted_type = &precision_wanted_type;
1875 }
1876 }
1877 else
1878 {
1879 if (!(fki->flags & (int) FMT_FLAG_EMPTY_PREC_OK)
1880 && !ISDIGIT (*format_chars))
1881 warning (OPT_Wformat_, "empty precision in %s format", fki->name);
1882 while (ISDIGIT (*format_chars))
1883 ++format_chars;
1884 }
1885 }
1886
1887 format_start = format_chars;
1888 if (fki->alloc_char && fki->alloc_char == *format_chars)
1889 {
1890 i = strlen (flag_chars);
1891 flag_chars[i++] = fki->alloc_char;
1892 flag_chars[i] = 0;
1893 format_chars++;
1894 }
1895
1896 /* Handle the scanf allocation kludge. */
1897 if (fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
1898 {
1899 if (*format_chars == 'a' && !flag_isoc99)
1900 {
1901 if (format_chars[1] == 's' || format_chars[1] == 'S'
1902 || format_chars[1] == '[')
1903 {
1904 /* 'a' is used as a flag. */
1905 i = strlen (flag_chars);
1906 flag_chars[i++] = 'a';
1907 flag_chars[i] = 0;
1908 format_chars++;
1909 }
1910 }
1911 }
1912
1913 /* Read any length modifier, if this kind of format has them. */
1914 fli = fki->length_char_specs;
1915 length_chars = NULL;
1916 length_chars_val = FMT_LEN_none;
1917 length_chars_std = STD_C89;
1918 scalar_identity_flag = 0;
1919 if (fli)
1920 {
1921 while (fli->name != 0
1922 && strncmp (fli->name, format_chars, strlen (fli->name)))
1923 fli++;
1924 if (fli->name != 0)
1925 {
1926 format_chars += strlen (fli->name);
1927 if (fli->double_name != 0 && fli->name[0] == *format_chars)
1928 {
1929 format_chars++;
1930 length_chars = fli->double_name;
1931 length_chars_val = fli->double_index;
1932 length_chars_std = fli->double_std;
1933 }
1934 else
1935 {
1936 length_chars = fli->name;
1937 length_chars_val = fli->index;
1938 length_chars_std = fli->std;
1939 scalar_identity_flag = fli->scalar_identity_flag;
1940 }
1941 i = strlen (flag_chars);
1942 flag_chars[i++] = fki->length_code_char;
1943 flag_chars[i] = 0;
1944 }
1945 if (pedantic)
1946 {
1947 /* Warn if the length modifier is non-standard. */
1948 if (ADJ_STD (length_chars_std) > C_STD_VER)
1949 warning (OPT_Wformat_,
1950 "%s does not support the %qs %s length modifier",
1951 C_STD_NAME (length_chars_std), length_chars,
1952 fki->name);
1953 }
1954 }
1955
1956 /* Read any modifier (strftime E/O). */
1957 if (fki->modifier_chars != NULL)
1958 {
1959 while (*format_chars != 0
1960 && strchr (fki->modifier_chars, *format_chars) != 0)
1961 {
1962 if (strchr (flag_chars, *format_chars) != 0)
1963 {
1964 const format_flag_spec *s = get_flag_spec (flag_specs,
1965 *format_chars, NULL);
1966 warning (OPT_Wformat_, "repeated %s in format", _(s->name));
1967 }
1968 else
1969 {
1970 i = strlen (flag_chars);
1971 flag_chars[i++] = *format_chars;
1972 flag_chars[i] = 0;
1973 }
1974 ++format_chars;
1975 }
1976 }
1977
1978 format_char = *format_chars;
1979 if (format_char == 0
1980 || (!(fki->flags & (int) FMT_FLAG_FANCY_PERCENT_OK)
1981 && format_char == '%'))
1982 {
1983 warning (OPT_Wformat_, "conversion lacks type at end of format");
1984 continue;
1985 }
1986 format_chars++;
1987 fci = fki->conversion_specs;
1988 while (fci->format_chars != 0
1989 && strchr (fci->format_chars, format_char) == 0)
1990 ++fci;
1991 if (fci->format_chars == 0)
1992 {
1993 if (ISGRAPH (format_char))
1994 warning (OPT_Wformat_, "unknown conversion type character %qc in format",
1995 format_char);
1996 else
1997 warning (OPT_Wformat_, "unknown conversion type character 0x%x in format",
1998 format_char);
1999 continue;
2000 }
2001 if (pedantic)
2002 {
2003 if (ADJ_STD (fci->std) > C_STD_VER)
2004 warning (OPT_Wformat_, "%s does not support the %<%%%c%> %s format",
2005 C_STD_NAME (fci->std), format_char, fki->name);
2006 }
2007
2008 /* Validate the individual flags used, removing any that are invalid. */
2009 {
2010 int d = 0;
2011 for (i = 0; flag_chars[i] != 0; i++)
2012 {
2013 const format_flag_spec *s = get_flag_spec (flag_specs,
2014 flag_chars[i], NULL);
2015 flag_chars[i - d] = flag_chars[i];
2016 if (flag_chars[i] == fki->length_code_char)
2017 continue;
2018 if (strchr (fci->flag_chars, flag_chars[i]) == 0)
2019 {
2020 warning (OPT_Wformat_, "%s used with %<%%%c%> %s format",
2021 _(s->name), format_char, fki->name);
2022 d++;
2023 continue;
2024 }
2025 if (pedantic)
2026 {
2027 const format_flag_spec *t;
2028 if (ADJ_STD (s->std) > C_STD_VER)
2029 warning (OPT_Wformat_, "%s does not support %s",
2030 C_STD_NAME (s->std), _(s->long_name));
2031 t = get_flag_spec (flag_specs, flag_chars[i], fci->flags2);
2032 if (t != NULL && ADJ_STD (t->std) > ADJ_STD (s->std))
2033 {
2034 const char *long_name = (t->long_name != NULL
2035 ? t->long_name
2036 : s->long_name);
2037 if (ADJ_STD (t->std) > C_STD_VER)
2038 warning (OPT_Wformat_,
2039 "%s does not support %s with the %<%%%c%> %s format",
2040 C_STD_NAME (t->std), _(long_name),
2041 format_char, fki->name);
2042 }
2043 }
2044 }
2045 flag_chars[i - d] = 0;
2046 }
2047
2048 if ((fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
2049 && strchr (flag_chars, 'a') != 0)
2050 alloc_flag = 1;
2051 if (fki->alloc_char && strchr (flag_chars, fki->alloc_char) != 0)
2052 alloc_flag = 1;
2053
2054 if (fki->suppression_char
2055 && strchr (flag_chars, fki->suppression_char) != 0)
2056 suppressed = 1;
2057
2058 /* Validate the pairs of flags used. */
2059 for (i = 0; bad_flag_pairs[i].flag_char1 != 0; i++)
2060 {
2061 const format_flag_spec *s, *t;
2062 if (strchr (flag_chars, bad_flag_pairs[i].flag_char1) == 0)
2063 continue;
2064 if (strchr (flag_chars, bad_flag_pairs[i].flag_char2) == 0)
2065 continue;
2066 if (bad_flag_pairs[i].predicate != 0
2067 && strchr (fci->flags2, bad_flag_pairs[i].predicate) == 0)
2068 continue;
2069 s = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char1, NULL);
2070 t = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char2, NULL);
2071 if (bad_flag_pairs[i].ignored)
2072 {
2073 if (bad_flag_pairs[i].predicate != 0)
2074 warning (OPT_Wformat_,
2075 "%s ignored with %s and %<%%%c%> %s format",
2076 _(s->name), _(t->name), format_char,
2077 fki->name);
2078 else
2079 warning (OPT_Wformat_, "%s ignored with %s in %s format",
2080 _(s->name), _(t->name), fki->name);
2081 }
2082 else
2083 {
2084 if (bad_flag_pairs[i].predicate != 0)
2085 warning (OPT_Wformat_,
2086 "use of %s and %s together with %<%%%c%> %s format",
2087 _(s->name), _(t->name), format_char,
2088 fki->name);
2089 else
2090 warning (OPT_Wformat_, "use of %s and %s together in %s format",
2091 _(s->name), _(t->name), fki->name);
2092 }
2093 }
2094
2095 /* Give Y2K warnings. */
2096 if (warn_format_y2k)
2097 {
2098 int y2k_level = 0;
2099 if (strchr (fci->flags2, '4') != 0)
2100 if (strchr (flag_chars, 'E') != 0)
2101 y2k_level = 3;
2102 else
2103 y2k_level = 2;
2104 else if (strchr (fci->flags2, '3') != 0)
2105 y2k_level = 3;
2106 else if (strchr (fci->flags2, '2') != 0)
2107 y2k_level = 2;
2108 if (y2k_level == 3)
2109 warning (OPT_Wformat_y2k, "%<%%%c%> yields only last 2 digits of "
2110 "year in some locales", format_char);
2111 else if (y2k_level == 2)
2112 warning (OPT_Wformat_y2k, "%<%%%c%> yields only last 2 digits of "
2113 "year", format_char);
2114 }
2115
2116 if (strchr (fci->flags2, '[') != 0)
2117 {
2118 /* Skip over scan set, in case it happens to have '%' in it. */
2119 if (*format_chars == '^')
2120 ++format_chars;
2121 /* Find closing bracket; if one is hit immediately, then
2122 it's part of the scan set rather than a terminator. */
2123 if (*format_chars == ']')
2124 ++format_chars;
2125 while (*format_chars && *format_chars != ']')
2126 ++format_chars;
2127 if (*format_chars != ']')
2128 /* The end of the format string was reached. */
2129 warning (OPT_Wformat_, "no closing %<]%> for %<%%[%> format");
2130 }
2131
2132 wanted_type = 0;
2133 wanted_type_name = 0;
2134 if (fki->flags & (int) FMT_FLAG_ARG_CONVERT)
2135 {
2136 wanted_type = (fci->types[length_chars_val].type
2137 ? *fci->types[length_chars_val].type : 0);
2138 wanted_type_name = fci->types[length_chars_val].name;
2139 wanted_type_std = fci->types[length_chars_val].std;
2140 if (wanted_type == 0)
2141 {
2142 warning (OPT_Wformat_,
2143 "use of %qs length modifier with %qc type character",
2144 length_chars, format_char);
2145 /* Heuristic: skip one argument when an invalid length/type
2146 combination is encountered. */
2147 arg_num++;
2148 if (params != 0)
2149 params = TREE_CHAIN (params);
2150 continue;
2151 }
2152 else if (pedantic
2153 /* Warn if non-standard, provided it is more non-standard
2154 than the length and type characters that may already
2155 have been warned for. */
2156 && ADJ_STD (wanted_type_std) > ADJ_STD (length_chars_std)
2157 && ADJ_STD (wanted_type_std) > ADJ_STD (fci->std))
2158 {
2159 if (ADJ_STD (wanted_type_std) > C_STD_VER)
2160 warning (OPT_Wformat_,
2161 "%s does not support the %<%%%s%c%> %s format",
2162 C_STD_NAME (wanted_type_std), length_chars,
2163 format_char, fki->name);
2164 }
2165 }
2166
2167 main_wanted_type.next = NULL;
2168
2169 /* Finally. . .check type of argument against desired type! */
2170 if (info->first_arg_num == 0)
2171 continue;
2172 if ((fci->pointer_count == 0 && wanted_type == void_type_node)
2173 || suppressed)
2174 {
2175 if (main_arg_num != 0)
2176 {
2177 if (suppressed)
2178 warning (OPT_Wformat_, "operand number specified with "
2179 "suppressed assignment");
2180 else
2181 warning (OPT_Wformat_, "operand number specified for format "
2182 "taking no argument");
2183 }
2184 }
2185 else
2186 {
2187 format_wanted_type *wanted_type_ptr;
2188
2189 if (main_arg_num != 0)
2190 {
2191 arg_num = main_arg_num;
2192 params = main_arg_params;
2193 }
2194 else
2195 {
2196 ++arg_num;
2197 if (has_operand_number > 0)
2198 {
2199 warning (OPT_Wformat_, "missing $ operand number in format");
2200 return;
2201 }
2202 else
2203 has_operand_number = 0;
2204 }
2205
2206 wanted_type_ptr = &main_wanted_type;
2207 while (fci)
2208 {
2209 if (params == 0)
2210 cur_param = NULL;
2211 else
2212 {
2213 cur_param = TREE_VALUE (params);
2214 params = TREE_CHAIN (params);
2215 }
2216
2217 wanted_type_ptr->wanted_type = wanted_type;
2218 wanted_type_ptr->wanted_type_name = wanted_type_name;
2219 wanted_type_ptr->pointer_count = fci->pointer_count + alloc_flag;
2220 wanted_type_ptr->char_lenient_flag = 0;
2221 if (strchr (fci->flags2, 'c') != 0)
2222 wanted_type_ptr->char_lenient_flag = 1;
2223 wanted_type_ptr->scalar_identity_flag = 0;
2224 if (scalar_identity_flag)
2225 wanted_type_ptr->scalar_identity_flag = 1;
2226 wanted_type_ptr->writing_in_flag = 0;
2227 wanted_type_ptr->reading_from_flag = 0;
2228 if (alloc_flag)
2229 wanted_type_ptr->writing_in_flag = 1;
2230 else
2231 {
2232 if (strchr (fci->flags2, 'W') != 0)
2233 wanted_type_ptr->writing_in_flag = 1;
2234 if (strchr (fci->flags2, 'R') != 0)
2235 wanted_type_ptr->reading_from_flag = 1;
2236 }
2237 wanted_type_ptr->kind = CF_KIND_FORMAT;
2238 wanted_type_ptr->param = cur_param;
2239 wanted_type_ptr->arg_num = arg_num;
2240 wanted_type_ptr->format_start = format_start;
2241 wanted_type_ptr->format_length = format_chars - format_start;
2242 wanted_type_ptr->next = NULL;
2243 if (last_wanted_type != 0)
2244 last_wanted_type->next = wanted_type_ptr;
2245 if (first_wanted_type == 0)
2246 first_wanted_type = wanted_type_ptr;
2247 last_wanted_type = wanted_type_ptr;
2248
2249 fci = fci->chain;
2250 if (fci)
2251 {
2252 wanted_type_ptr = (format_wanted_type *)
2253 pool_alloc (fwt_pool);
2254 arg_num++;
2255 wanted_type = *fci->types[length_chars_val].type;
2256 wanted_type_name = fci->types[length_chars_val].name;
2257 }
2258 }
2259 }
2260
2261 if (first_wanted_type != 0)
2262 check_format_types (first_wanted_type);
2263 }
2264
2265 if (format_chars - orig_format_chars != format_length)
2266 warning (OPT_Wformat_contains_nul, "embedded %<\\0%> in format");
2267 if (info->first_arg_num != 0 && params != 0
2268 && has_operand_number <= 0)
2269 {
2270 res->number_other--;
2271 res->number_extra_args++;
2272 }
2273 if (has_operand_number > 0)
2274 finish_dollar_format_checking (res, fki->flags & (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK);
2275 }
2276
2277
2278 /* Check the argument types from a single format conversion (possibly
2279 including width and precision arguments). */
2280 static void
check_format_types(format_wanted_type * types)2281 check_format_types (format_wanted_type *types)
2282 {
2283 for (; types != 0; types = types->next)
2284 {
2285 tree cur_param;
2286 tree cur_type;
2287 tree orig_cur_type;
2288 tree wanted_type;
2289 int arg_num;
2290 int i;
2291 int char_type_flag;
2292
2293 wanted_type = types->wanted_type;
2294 arg_num = types->arg_num;
2295
2296 /* The following should not occur here. */
2297 gcc_assert (wanted_type);
2298 gcc_assert (wanted_type != void_type_node || types->pointer_count);
2299
2300 if (types->pointer_count == 0)
2301 wanted_type = lang_hooks.types.type_promotes_to (wanted_type);
2302
2303 wanted_type = TYPE_MAIN_VARIANT (wanted_type);
2304
2305 cur_param = types->param;
2306 if (!cur_param)
2307 {
2308 format_type_warning (types, wanted_type, NULL);
2309 continue;
2310 }
2311
2312 cur_type = TREE_TYPE (cur_param);
2313 if (cur_type == error_mark_node)
2314 continue;
2315 orig_cur_type = cur_type;
2316 char_type_flag = 0;
2317
2318 STRIP_NOPS (cur_param);
2319
2320 /* Check the types of any additional pointer arguments
2321 that precede the "real" argument. */
2322 for (i = 0; i < types->pointer_count; ++i)
2323 {
2324 if (TREE_CODE (cur_type) == POINTER_TYPE)
2325 {
2326 cur_type = TREE_TYPE (cur_type);
2327 if (cur_type == error_mark_node)
2328 break;
2329
2330 /* Check for writing through a NULL pointer. */
2331 if (types->writing_in_flag
2332 && i == 0
2333 && cur_param != 0
2334 && integer_zerop (cur_param))
2335 warning (OPT_Wformat_, "writing through null pointer "
2336 "(argument %d)", arg_num);
2337
2338 /* Check for reading through a NULL pointer. */
2339 if (types->reading_from_flag
2340 && i == 0
2341 && cur_param != 0
2342 && integer_zerop (cur_param))
2343 warning (OPT_Wformat_, "reading through null pointer "
2344 "(argument %d)", arg_num);
2345
2346 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
2347 cur_param = TREE_OPERAND (cur_param, 0);
2348 else
2349 cur_param = 0;
2350
2351 /* See if this is an attempt to write into a const type with
2352 scanf or with printf "%n". Note: the writing in happens
2353 at the first indirection only, if for example
2354 void * const * is passed to scanf %p; passing
2355 const void ** is simply passing an incompatible type. */
2356 if (types->writing_in_flag
2357 && i == 0
2358 && (TYPE_READONLY (cur_type)
2359 || (cur_param != 0
2360 && (CONSTANT_CLASS_P (cur_param)
2361 || (DECL_P (cur_param)
2362 && TREE_READONLY (cur_param))))))
2363 warning (OPT_Wformat_, "writing into constant object "
2364 "(argument %d)", arg_num);
2365
2366 /* If there are extra type qualifiers beyond the first
2367 indirection, then this makes the types technically
2368 incompatible. */
2369 if (i > 0
2370 && pedantic
2371 && (TYPE_READONLY (cur_type)
2372 || TYPE_VOLATILE (cur_type)
2373 || TYPE_RESTRICT (cur_type)))
2374 warning (OPT_Wformat_, "extra type qualifiers in format "
2375 "argument (argument %d)",
2376 arg_num);
2377
2378 }
2379 else
2380 {
2381 format_type_warning (types, wanted_type, orig_cur_type);
2382 break;
2383 }
2384 }
2385
2386 if (i < types->pointer_count)
2387 continue;
2388
2389 cur_type = TYPE_MAIN_VARIANT (cur_type);
2390
2391 /* Check whether the argument type is a character type. This leniency
2392 only applies to certain formats, flagged with 'c'.
2393 */
2394 if (types->char_lenient_flag)
2395 char_type_flag = (cur_type == char_type_node
2396 || cur_type == signed_char_type_node
2397 || cur_type == unsigned_char_type_node);
2398
2399 /* Check the type of the "real" argument, if there's a type we want. */
2400 if (lang_hooks.types_compatible_p (wanted_type, cur_type))
2401 continue;
2402 /* If we want 'void *', allow any pointer type.
2403 (Anything else would already have got a warning.)
2404 With -Wpedantic, only allow pointers to void and to character
2405 types. */
2406 if (wanted_type == void_type_node
2407 && (!pedantic || (i == 1 && char_type_flag)))
2408 continue;
2409 /* Don't warn about differences merely in signedness, unless
2410 -Wpedantic. With -Wpedantic, warn if the type is a pointer
2411 target and not a character type, and for character types at
2412 a second level of indirection. */
2413 if (TREE_CODE (wanted_type) == INTEGER_TYPE
2414 && TREE_CODE (cur_type) == INTEGER_TYPE
2415 && (!pedantic || i == 0 || (i == 1 && char_type_flag))
2416 && (TYPE_UNSIGNED (wanted_type)
2417 ? wanted_type == c_common_unsigned_type (cur_type)
2418 : wanted_type == c_common_signed_type (cur_type)))
2419 continue;
2420 /* Likewise, "signed char", "unsigned char" and "char" are
2421 equivalent but the above test won't consider them equivalent. */
2422 if (wanted_type == char_type_node
2423 && (!pedantic || i < 2)
2424 && char_type_flag)
2425 continue;
2426 if (types->scalar_identity_flag
2427 && (TREE_CODE (cur_type) == TREE_CODE (wanted_type)
2428 || (INTEGRAL_TYPE_P (cur_type)
2429 && INTEGRAL_TYPE_P (wanted_type)))
2430 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type))
2431 continue;
2432 /* Now we have a type mismatch. */
2433 format_type_warning (types, wanted_type, orig_cur_type);
2434 }
2435 }
2436
2437
2438 /* Give a warning about a format argument of different type from that
2439 expected. WANTED_TYPE is the type the argument should have, possibly
2440 stripped of pointer dereferences. The description (such as "field
2441 precision"), the placement in the format string, a possibly more
2442 friendly name of WANTED_TYPE, and the number of pointer dereferences
2443 are taken from TYPE. ARG_TYPE is the type of the actual argument,
2444 or NULL if it is missing. */
2445 static void
format_type_warning(format_wanted_type * type,tree wanted_type,tree arg_type)2446 format_type_warning (format_wanted_type *type, tree wanted_type, tree arg_type)
2447 {
2448 int kind = type->kind;
2449 const char *wanted_type_name = type->wanted_type_name;
2450 const char *format_start = type->format_start;
2451 int format_length = type->format_length;
2452 int pointer_count = type->pointer_count;
2453 int arg_num = type->arg_num;
2454
2455 char *p;
2456 /* If ARG_TYPE is a typedef with a misleading name (for example,
2457 size_t but not the standard size_t expected by printf %zu), avoid
2458 printing the typedef name. */
2459 if (wanted_type_name
2460 && arg_type
2461 && TYPE_NAME (arg_type)
2462 && TREE_CODE (TYPE_NAME (arg_type)) == TYPE_DECL
2463 && DECL_NAME (TYPE_NAME (arg_type))
2464 && !strcmp (wanted_type_name,
2465 lang_hooks.decl_printable_name (TYPE_NAME (arg_type), 2)))
2466 arg_type = TYPE_MAIN_VARIANT (arg_type);
2467 /* The format type and name exclude any '*' for pointers, so those
2468 must be formatted manually. For all the types we currently have,
2469 this is adequate, but formats taking pointers to functions or
2470 arrays would require the full type to be built up in order to
2471 print it with %T. */
2472 p = (char *) alloca (pointer_count + 2);
2473 if (pointer_count == 0)
2474 p[0] = 0;
2475 else if (c_dialect_cxx ())
2476 {
2477 memset (p, '*', pointer_count);
2478 p[pointer_count] = 0;
2479 }
2480 else
2481 {
2482 p[0] = ' ';
2483 memset (p + 1, '*', pointer_count);
2484 p[pointer_count + 1] = 0;
2485 }
2486
2487 if (wanted_type_name)
2488 {
2489 if (arg_type)
2490 warning (OPT_Wformat_, "%s %<%s%.*s%> expects argument of type %<%s%s%>, "
2491 "but argument %d has type %qT",
2492 gettext (kind_descriptions[kind]),
2493 (kind == CF_KIND_FORMAT ? "%" : ""),
2494 format_length, format_start,
2495 wanted_type_name, p, arg_num, arg_type);
2496 else
2497 warning (OPT_Wformat_, "%s %<%s%.*s%> expects a matching %<%s%s%> argument",
2498 gettext (kind_descriptions[kind]),
2499 (kind == CF_KIND_FORMAT ? "%" : ""),
2500 format_length, format_start, wanted_type_name, p);
2501 }
2502 else
2503 {
2504 if (arg_type)
2505 warning (OPT_Wformat_, "%s %<%s%.*s%> expects argument of type %<%T%s%>, "
2506 "but argument %d has type %qT",
2507 gettext (kind_descriptions[kind]),
2508 (kind == CF_KIND_FORMAT ? "%" : ""),
2509 format_length, format_start,
2510 wanted_type, p, arg_num, arg_type);
2511 else
2512 warning (OPT_Wformat_, "%s %<%s%.*s%> expects a matching %<%T%s%> argument",
2513 gettext (kind_descriptions[kind]),
2514 (kind == CF_KIND_FORMAT ? "%" : ""),
2515 format_length, format_start, wanted_type, p);
2516 }
2517 }
2518
2519
2520 /* Given a format_char_info array FCI, and a character C, this function
2521 returns the index into the conversion_specs where that specifier's
2522 data is located. The character must exist. */
2523 static unsigned int
find_char_info_specifier_index(const format_char_info * fci,int c)2524 find_char_info_specifier_index (const format_char_info *fci, int c)
2525 {
2526 unsigned i;
2527
2528 for (i = 0; fci->format_chars; i++, fci++)
2529 if (strchr (fci->format_chars, c))
2530 return i;
2531
2532 /* We shouldn't be looking for a non-existent specifier. */
2533 gcc_unreachable ();
2534 }
2535
2536 /* Given a format_length_info array FLI, and a character C, this
2537 function returns the index into the conversion_specs where that
2538 modifier's data is located. The character must exist. */
2539 static unsigned int
find_length_info_modifier_index(const format_length_info * fli,int c)2540 find_length_info_modifier_index (const format_length_info *fli, int c)
2541 {
2542 unsigned i;
2543
2544 for (i = 0; fli->name; i++, fli++)
2545 if (strchr (fli->name, c))
2546 return i;
2547
2548 /* We shouldn't be looking for a non-existent modifier. */
2549 gcc_unreachable ();
2550 }
2551
2552 /* Determine the type of HOST_WIDE_INT in the code being compiled for
2553 use in GCC's __asm_fprintf__ custom format attribute. You must
2554 have set dynamic_format_types before calling this function. */
2555 static void
init_dynamic_asm_fprintf_info(void)2556 init_dynamic_asm_fprintf_info (void)
2557 {
2558 static tree hwi;
2559
2560 if (!hwi)
2561 {
2562 format_length_info *new_asm_fprintf_length_specs;
2563 unsigned int i;
2564
2565 /* Find the underlying type for HOST_WIDE_INT. For the %w
2566 length modifier to work, one must have issued: "typedef
2567 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2568 prior to using that modifier. */
2569 hwi = maybe_get_identifier ("__gcc_host_wide_int__");
2570 if (!hwi)
2571 {
2572 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2573 return;
2574 }
2575 hwi = identifier_global_value (hwi);
2576 if (!hwi || TREE_CODE (hwi) != TYPE_DECL)
2577 {
2578 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2579 return;
2580 }
2581 hwi = DECL_ORIGINAL_TYPE (hwi);
2582 gcc_assert (hwi);
2583 if (hwi != long_integer_type_node && hwi != long_long_integer_type_node)
2584 {
2585 error ("%<__gcc_host_wide_int__%> is not defined as %<long%>"
2586 " or %<long long%>");
2587 return;
2588 }
2589
2590 /* Create a new (writable) copy of asm_fprintf_length_specs. */
2591 new_asm_fprintf_length_specs = (format_length_info *)
2592 xmemdup (asm_fprintf_length_specs,
2593 sizeof (asm_fprintf_length_specs),
2594 sizeof (asm_fprintf_length_specs));
2595
2596 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2597 i = find_length_info_modifier_index (new_asm_fprintf_length_specs, 'w');
2598 if (hwi == long_integer_type_node)
2599 new_asm_fprintf_length_specs[i].index = FMT_LEN_l;
2600 else if (hwi == long_long_integer_type_node)
2601 new_asm_fprintf_length_specs[i].index = FMT_LEN_ll;
2602 else
2603 gcc_unreachable ();
2604
2605 /* Assign the new data for use. */
2606 dynamic_format_types[asm_fprintf_format_type].length_char_specs =
2607 new_asm_fprintf_length_specs;
2608 }
2609 }
2610
2611 /* Determine the type of a "locus" in the code being compiled for use
2612 in GCC's __gcc_gfc__ custom format attribute. You must have set
2613 dynamic_format_types before calling this function. */
2614 static void
init_dynamic_gfc_info(void)2615 init_dynamic_gfc_info (void)
2616 {
2617 static tree locus;
2618
2619 if (!locus)
2620 {
2621 static format_char_info *gfc_fci;
2622
2623 /* For the GCC __gcc_gfc__ custom format specifier to work, one
2624 must have declared 'locus' prior to using this attribute. If
2625 we haven't seen this declarations then you shouldn't use the
2626 specifier requiring that type. */
2627 if ((locus = maybe_get_identifier ("locus")))
2628 {
2629 locus = identifier_global_value (locus);
2630 if (locus)
2631 {
2632 if (TREE_CODE (locus) != TYPE_DECL
2633 || TREE_TYPE (locus) == error_mark_node)
2634 {
2635 error ("%<locus%> is not defined as a type");
2636 locus = 0;
2637 }
2638 else
2639 locus = TREE_TYPE (locus);
2640 }
2641 }
2642
2643 /* Assign the new data for use. */
2644
2645 /* Handle the __gcc_gfc__ format specifics. */
2646 if (!gfc_fci)
2647 dynamic_format_types[gcc_gfc_format_type].conversion_specs =
2648 gfc_fci = (format_char_info *)
2649 xmemdup (gcc_gfc_char_table,
2650 sizeof (gcc_gfc_char_table),
2651 sizeof (gcc_gfc_char_table));
2652 if (locus)
2653 {
2654 const unsigned i = find_char_info_specifier_index (gfc_fci, 'L');
2655 gfc_fci[i].types[0].type = &locus;
2656 gfc_fci[i].pointer_count = 1;
2657 }
2658 }
2659 }
2660
2661 /* Determine the types of "tree" and "location_t" in the code being
2662 compiled for use in GCC's diagnostic custom format attributes. You
2663 must have set dynamic_format_types before calling this function. */
2664 static void
init_dynamic_diag_info(void)2665 init_dynamic_diag_info (void)
2666 {
2667 static tree t, loc, hwi;
2668
2669 if (!loc || !t || !hwi)
2670 {
2671 static format_char_info *diag_fci, *tdiag_fci, *cdiag_fci, *cxxdiag_fci;
2672 static format_length_info *diag_ls;
2673 unsigned int i;
2674
2675 /* For the GCC-diagnostics custom format specifiers to work, one
2676 must have declared 'tree' and/or 'location_t' prior to using
2677 those attributes. If we haven't seen these declarations then
2678 you shouldn't use the specifiers requiring these types.
2679 However we don't force a hard ICE because we may see only one
2680 or the other type. */
2681 if ((loc = maybe_get_identifier ("location_t")))
2682 {
2683 loc = identifier_global_value (loc);
2684 if (loc)
2685 {
2686 if (TREE_CODE (loc) != TYPE_DECL)
2687 {
2688 error ("%<location_t%> is not defined as a type");
2689 loc = 0;
2690 }
2691 else
2692 loc = TREE_TYPE (loc);
2693 }
2694 }
2695
2696 /* We need to grab the underlying 'union tree_node' so peek into
2697 an extra type level. */
2698 if ((t = maybe_get_identifier ("tree")))
2699 {
2700 t = identifier_global_value (t);
2701 if (t)
2702 {
2703 if (TREE_CODE (t) != TYPE_DECL)
2704 {
2705 error ("%<tree%> is not defined as a type");
2706 t = 0;
2707 }
2708 else if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
2709 {
2710 error ("%<tree%> is not defined as a pointer type");
2711 t = 0;
2712 }
2713 else
2714 t = TREE_TYPE (TREE_TYPE (t));
2715 }
2716 }
2717
2718 /* Find the underlying type for HOST_WIDE_INT. For the %w
2719 length modifier to work, one must have issued: "typedef
2720 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2721 prior to using that modifier. */
2722 if ((hwi = maybe_get_identifier ("__gcc_host_wide_int__")))
2723 {
2724 hwi = identifier_global_value (hwi);
2725 if (hwi)
2726 {
2727 if (TREE_CODE (hwi) != TYPE_DECL)
2728 {
2729 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2730 hwi = 0;
2731 }
2732 else
2733 {
2734 hwi = DECL_ORIGINAL_TYPE (hwi);
2735 gcc_assert (hwi);
2736 if (hwi != long_integer_type_node
2737 && hwi != long_long_integer_type_node)
2738 {
2739 error ("%<__gcc_host_wide_int__%> is not defined"
2740 " as %<long%> or %<long long%>");
2741 hwi = 0;
2742 }
2743 }
2744 }
2745 }
2746
2747 /* Assign the new data for use. */
2748
2749 /* All the GCC diag formats use the same length specs. */
2750 if (!diag_ls)
2751 dynamic_format_types[gcc_diag_format_type].length_char_specs =
2752 dynamic_format_types[gcc_tdiag_format_type].length_char_specs =
2753 dynamic_format_types[gcc_cdiag_format_type].length_char_specs =
2754 dynamic_format_types[gcc_cxxdiag_format_type].length_char_specs =
2755 diag_ls = (format_length_info *)
2756 xmemdup (gcc_diag_length_specs,
2757 sizeof (gcc_diag_length_specs),
2758 sizeof (gcc_diag_length_specs));
2759 if (hwi)
2760 {
2761 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2762 i = find_length_info_modifier_index (diag_ls, 'w');
2763 if (hwi == long_integer_type_node)
2764 diag_ls[i].index = FMT_LEN_l;
2765 else if (hwi == long_long_integer_type_node)
2766 diag_ls[i].index = FMT_LEN_ll;
2767 else
2768 gcc_unreachable ();
2769 }
2770
2771 /* Handle the __gcc_diag__ format specifics. */
2772 if (!diag_fci)
2773 dynamic_format_types[gcc_diag_format_type].conversion_specs =
2774 diag_fci = (format_char_info *)
2775 xmemdup (gcc_diag_char_table,
2776 sizeof (gcc_diag_char_table),
2777 sizeof (gcc_diag_char_table));
2778 if (t)
2779 {
2780 i = find_char_info_specifier_index (diag_fci, 'K');
2781 diag_fci[i].types[0].type = &t;
2782 diag_fci[i].pointer_count = 1;
2783 }
2784
2785 /* Handle the __gcc_tdiag__ format specifics. */
2786 if (!tdiag_fci)
2787 dynamic_format_types[gcc_tdiag_format_type].conversion_specs =
2788 tdiag_fci = (format_char_info *)
2789 xmemdup (gcc_tdiag_char_table,
2790 sizeof (gcc_tdiag_char_table),
2791 sizeof (gcc_tdiag_char_table));
2792 if (t)
2793 {
2794 /* All specifiers taking a tree share the same struct. */
2795 i = find_char_info_specifier_index (tdiag_fci, 'D');
2796 tdiag_fci[i].types[0].type = &t;
2797 tdiag_fci[i].pointer_count = 1;
2798 i = find_char_info_specifier_index (tdiag_fci, 'K');
2799 tdiag_fci[i].types[0].type = &t;
2800 tdiag_fci[i].pointer_count = 1;
2801 }
2802
2803 /* Handle the __gcc_cdiag__ format specifics. */
2804 if (!cdiag_fci)
2805 dynamic_format_types[gcc_cdiag_format_type].conversion_specs =
2806 cdiag_fci = (format_char_info *)
2807 xmemdup (gcc_cdiag_char_table,
2808 sizeof (gcc_cdiag_char_table),
2809 sizeof (gcc_cdiag_char_table));
2810 if (t)
2811 {
2812 /* All specifiers taking a tree share the same struct. */
2813 i = find_char_info_specifier_index (cdiag_fci, 'D');
2814 cdiag_fci[i].types[0].type = &t;
2815 cdiag_fci[i].pointer_count = 1;
2816 i = find_char_info_specifier_index (cdiag_fci, 'K');
2817 cdiag_fci[i].types[0].type = &t;
2818 cdiag_fci[i].pointer_count = 1;
2819 }
2820
2821 /* Handle the __gcc_cxxdiag__ format specifics. */
2822 if (!cxxdiag_fci)
2823 dynamic_format_types[gcc_cxxdiag_format_type].conversion_specs =
2824 cxxdiag_fci = (format_char_info *)
2825 xmemdup (gcc_cxxdiag_char_table,
2826 sizeof (gcc_cxxdiag_char_table),
2827 sizeof (gcc_cxxdiag_char_table));
2828 if (t)
2829 {
2830 /* All specifiers taking a tree share the same struct. */
2831 i = find_char_info_specifier_index (cxxdiag_fci, 'D');
2832 cxxdiag_fci[i].types[0].type = &t;
2833 cxxdiag_fci[i].pointer_count = 1;
2834 i = find_char_info_specifier_index (cxxdiag_fci, 'K');
2835 cxxdiag_fci[i].types[0].type = &t;
2836 cxxdiag_fci[i].pointer_count = 1;
2837 }
2838 }
2839 }
2840
2841 #ifdef TARGET_FORMAT_TYPES
2842 extern const format_kind_info TARGET_FORMAT_TYPES[];
2843 #endif
2844
2845 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
2846 extern const target_ovr_attr TARGET_OVERRIDES_FORMAT_ATTRIBUTES[];
2847 #endif
2848 #ifdef TARGET_OVERRIDES_FORMAT_INIT
2849 extern void TARGET_OVERRIDES_FORMAT_INIT (void);
2850 #endif
2851
2852 /* Attributes such as "printf" are equivalent to those such as
2853 "gnu_printf" unless this is overridden by a target. */
2854 static const target_ovr_attr gnu_target_overrides_format_attributes[] =
2855 {
2856 { "gnu_printf", "printf" },
2857 { "gnu_scanf", "scanf" },
2858 { "gnu_strftime", "strftime" },
2859 { "gnu_strfmon", "strfmon" },
2860 { NULL, NULL }
2861 };
2862
2863 /* Translate to unified attribute name. This is used in decode_format_type and
2864 decode_format_attr. In attr_name the user specified argument is passed. It
2865 returns the unified format name from TARGET_OVERRIDES_FORMAT_ATTRIBUTES
2866 or the attr_name passed to this function, if there is no matching entry. */
2867 static const char *
convert_format_name_to_system_name(const char * attr_name)2868 convert_format_name_to_system_name (const char *attr_name)
2869 {
2870 int i;
2871
2872 if (attr_name == NULL || *attr_name == 0
2873 || strncmp (attr_name, "gcc_", 4) == 0)
2874 return attr_name;
2875 #ifdef TARGET_OVERRIDES_FORMAT_INIT
2876 TARGET_OVERRIDES_FORMAT_INIT ();
2877 #endif
2878
2879 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
2880 /* Check if format attribute is overridden by target. */
2881 if (TARGET_OVERRIDES_FORMAT_ATTRIBUTES != NULL
2882 && TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT > 0)
2883 {
2884 for (i = 0; i < TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT; ++i)
2885 {
2886 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_src,
2887 attr_name))
2888 return attr_name;
2889 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_dst,
2890 attr_name))
2891 return TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_src;
2892 }
2893 }
2894 #endif
2895 /* Otherwise default to gnu format. */
2896 for (i = 0;
2897 gnu_target_overrides_format_attributes[i].named_attr_src != NULL;
2898 ++i)
2899 {
2900 if (cmp_attribs (gnu_target_overrides_format_attributes[i].named_attr_src,
2901 attr_name))
2902 return attr_name;
2903 if (cmp_attribs (gnu_target_overrides_format_attributes[i].named_attr_dst,
2904 attr_name))
2905 return gnu_target_overrides_format_attributes[i].named_attr_src;
2906 }
2907
2908 return attr_name;
2909 }
2910
2911 /* Return true if TATTR_NAME and ATTR_NAME are the same format attribute,
2912 counting "name" and "__name__" as the same, false otherwise. */
2913 static bool
cmp_attribs(const char * tattr_name,const char * attr_name)2914 cmp_attribs (const char *tattr_name, const char *attr_name)
2915 {
2916 int alen = strlen (attr_name);
2917 int slen = (tattr_name ? strlen (tattr_name) : 0);
2918 if (alen > 4 && attr_name[0] == '_' && attr_name[1] == '_'
2919 && attr_name[alen - 1] == '_' && attr_name[alen - 2] == '_')
2920 {
2921 attr_name += 2;
2922 alen -= 4;
2923 }
2924 if (alen != slen || strncmp (tattr_name, attr_name, alen) != 0)
2925 return false;
2926 return true;
2927 }
2928
2929 /* Handle a "format" attribute; arguments as in
2930 struct attribute_spec.handler. */
2931 tree
handle_format_attribute(tree * node,tree ARG_UNUSED (name),tree args,int flags,bool * no_add_attrs)2932 handle_format_attribute (tree *node, tree ARG_UNUSED (name), tree args,
2933 int flags, bool *no_add_attrs)
2934 {
2935 tree type = *node;
2936 function_format_info info;
2937
2938 #ifdef TARGET_FORMAT_TYPES
2939 /* If the target provides additional format types, we need to
2940 add them to FORMAT_TYPES at first use. */
2941 if (TARGET_FORMAT_TYPES != NULL && !dynamic_format_types)
2942 {
2943 dynamic_format_types = XNEWVEC (format_kind_info,
2944 n_format_types + TARGET_N_FORMAT_TYPES);
2945 memcpy (dynamic_format_types, format_types_orig,
2946 sizeof (format_types_orig));
2947 memcpy (&dynamic_format_types[n_format_types], TARGET_FORMAT_TYPES,
2948 TARGET_N_FORMAT_TYPES * sizeof (dynamic_format_types[0]));
2949
2950 format_types = dynamic_format_types;
2951 /* Provide a reference for the first potential external type. */
2952 first_target_format_type = n_format_types;
2953 n_format_types += TARGET_N_FORMAT_TYPES;
2954 }
2955 #endif
2956
2957 if (!decode_format_attr (args, &info, 0))
2958 {
2959 *no_add_attrs = true;
2960 return NULL_TREE;
2961 }
2962
2963 if (prototype_p (type))
2964 {
2965 if (!check_format_string (type, info.format_num, flags,
2966 no_add_attrs, info.format_type))
2967 return NULL_TREE;
2968
2969 if (info.first_arg_num != 0)
2970 {
2971 unsigned HOST_WIDE_INT arg_num = 1;
2972 function_args_iterator iter;
2973 tree arg_type;
2974
2975 /* Verify that first_arg_num points to the last arg,
2976 the ... */
2977 FOREACH_FUNCTION_ARGS (type, arg_type, iter)
2978 arg_num++;
2979
2980 if (arg_num != info.first_arg_num)
2981 {
2982 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
2983 error ("args to be formatted is not %<...%>");
2984 *no_add_attrs = true;
2985 return NULL_TREE;
2986 }
2987 }
2988 }
2989
2990 /* Check if this is a strftime variant. Just for this variant
2991 FMT_FLAG_ARG_CONVERT is not set. */
2992 if ((format_types[info.format_type].flags & (int) FMT_FLAG_ARG_CONVERT) == 0
2993 && info.first_arg_num != 0)
2994 {
2995 error ("strftime formats cannot format arguments");
2996 *no_add_attrs = true;
2997 return NULL_TREE;
2998 }
2999
3000 /* If this is a custom GCC-internal format type, we have to
3001 initialize certain bits at runtime. */
3002 if (info.format_type == asm_fprintf_format_type
3003 || info.format_type == gcc_gfc_format_type
3004 || info.format_type == gcc_diag_format_type
3005 || info.format_type == gcc_tdiag_format_type
3006 || info.format_type == gcc_cdiag_format_type
3007 || info.format_type == gcc_cxxdiag_format_type)
3008 {
3009 /* Our first time through, we have to make sure that our
3010 format_type data is allocated dynamically and is modifiable. */
3011 if (!dynamic_format_types)
3012 format_types = dynamic_format_types = (format_kind_info *)
3013 xmemdup (format_types_orig, sizeof (format_types_orig),
3014 sizeof (format_types_orig));
3015
3016 /* If this is format __asm_fprintf__, we have to initialize
3017 GCC's notion of HOST_WIDE_INT for checking %wd. */
3018 if (info.format_type == asm_fprintf_format_type)
3019 init_dynamic_asm_fprintf_info ();
3020 /* If this is format __gcc_gfc__, we have to initialize GCC's
3021 notion of 'locus' at runtime for %L. */
3022 else if (info.format_type == gcc_gfc_format_type)
3023 init_dynamic_gfc_info ();
3024 /* If this is one of the diagnostic attributes, then we have to
3025 initialize 'location_t' and 'tree' at runtime. */
3026 else if (info.format_type == gcc_diag_format_type
3027 || info.format_type == gcc_tdiag_format_type
3028 || info.format_type == gcc_cdiag_format_type
3029 || info.format_type == gcc_cxxdiag_format_type)
3030 init_dynamic_diag_info ();
3031 else
3032 gcc_unreachable ();
3033 }
3034
3035 return NULL_TREE;
3036 }
3037