substring-locations: add class format_string_diagnostic_t
[official-gcc.git] / gcc / c-family / c-format.c
bloba7edfca4aa947216a736ff4b418bde1e9c59cacd
1 /* Check calls to formatted I/O functions (-Wformat).
2 Copyright (C) 1992-2018 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "c-target.h"
25 #include "c-common.h"
26 #include "alloc-pool.h"
27 #include "stringpool.h"
28 #include "c-objc.h"
29 #include "intl.h"
30 #include "langhooks.h"
31 #include "c-format.h"
32 #include "diagnostic.h"
33 #include "substring-locations.h"
34 #include "selftest.h"
35 #include "selftest-diagnostic.h"
36 #include "builtins.h"
37 #include "attribs.h"
38 #include "gcc-rich-location.h"
40 /* Handle attributes associated with format checking. */
42 /* This must be in the same order as format_types, except for
43 format_type_error. Target-specific format types do not have
44 matching enum values. */
45 enum format_type { printf_format_type, asm_fprintf_format_type,
46 gcc_diag_format_type, gcc_tdiag_format_type,
47 gcc_cdiag_format_type,
48 gcc_cxxdiag_format_type, gcc_gfc_format_type,
49 gcc_dump_printf_format_type,
50 gcc_objc_string_format_type,
51 format_type_error = -1};
53 struct function_format_info
55 int format_type; /* type of format (printf, scanf, etc.) */
56 unsigned HOST_WIDE_INT format_num; /* number of format argument */
57 unsigned HOST_WIDE_INT first_arg_num; /* number of first arg (zero for varargs) */
60 /* Initialized in init_dynamic_diag_info. */
61 static GTY(()) tree local_tree_type_node;
62 static GTY(()) tree local_gimple_ptr_node;
63 static GTY(()) tree locus;
65 static bool decode_format_attr (tree, function_format_info *, int);
66 static int decode_format_type (const char *);
68 static bool check_format_string (tree argument,
69 unsigned HOST_WIDE_INT format_num,
70 int flags, bool *no_add_attrs,
71 int expected_format_type);
72 static bool get_constant (tree expr, unsigned HOST_WIDE_INT *value,
73 int validated_p);
74 static const char *convert_format_name_to_system_name (const char *attr_name);
76 static int first_target_format_type;
77 static const char *format_name (int format_num);
78 static int format_flags (int format_num);
80 /* Emit a warning as per format_warning_va, but construct the substring_loc
81 for the character at offset (CHAR_IDX - 1) within a string constant
82 FORMAT_STRING_CST at FMT_STRING_LOC. */
84 ATTRIBUTE_GCC_DIAG (5,6)
85 static bool
86 format_warning_at_char (location_t fmt_string_loc, tree format_string_cst,
87 int char_idx, int opt, const char *gmsgid, ...)
89 va_list ap;
90 va_start (ap, gmsgid);
91 tree string_type = TREE_TYPE (format_string_cst);
93 /* The callers are of the form:
94 format_warning (format_string_loc, format_string_cst,
95 format_chars - orig_format_chars,
96 where format_chars has already been incremented, so that
97 CHAR_IDX is one character beyond where the warning should
98 be emitted. Fix it. */
99 char_idx -= 1;
101 substring_loc fmt_loc (fmt_string_loc, string_type, char_idx, char_idx,
102 char_idx);
103 format_string_diagnostic_t diag (fmt_loc, NULL, UNKNOWN_LOCATION, NULL,
104 NULL);
105 bool warned = diag.emit_warning_va (opt, gmsgid, &ap);
106 va_end (ap);
108 return warned;
111 /* Check that we have a pointer to a string suitable for use as a format.
112 The default is to check for a char type.
113 For objective-c dialects, this is extended to include references to string
114 objects validated by objc_string_ref_type_p ().
115 Targets may also provide a string object type that can be used within c and
116 c++ and shared with their respective objective-c dialects. In this case the
117 reference to a format string is checked for validity via a hook.
119 The function returns true if strref points to any string type valid for the
120 language dialect and target. */
122 static bool
123 valid_stringptr_type_p (tree strref)
125 return (strref != NULL
126 && TREE_CODE (strref) == POINTER_TYPE
127 && (TYPE_MAIN_VARIANT (TREE_TYPE (strref)) == char_type_node
128 || objc_string_ref_type_p (strref)
129 || (*targetcm.string_object_ref_type_p) ((const_tree) strref)));
132 /* Handle a "format_arg" attribute; arguments as in
133 struct attribute_spec.handler. */
134 tree
135 handle_format_arg_attribute (tree *node, tree ARG_UNUSED (name),
136 tree args, int flags, bool *no_add_attrs)
138 tree type = *node;
139 tree format_num_expr = TREE_VALUE (args);
140 unsigned HOST_WIDE_INT format_num = 0;
142 if (!get_constant (format_num_expr, &format_num, 0))
144 error ("format string has invalid operand number");
145 *no_add_attrs = true;
146 return NULL_TREE;
149 if (prototype_p (type))
151 /* The format arg can be any string reference valid for the language and
152 target. We cannot be more specific in this case. */
153 if (!check_format_string (type, format_num, flags, no_add_attrs, -1))
154 return NULL_TREE;
157 if (!valid_stringptr_type_p (TREE_TYPE (type)))
159 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
160 error ("function does not return string type");
161 *no_add_attrs = true;
162 return NULL_TREE;
165 return NULL_TREE;
168 /* Verify that the format_num argument is actually a string reference suitable,
169 for the language dialect and target (in case the format attribute is in
170 error). When we know the specific reference type expected, this is also
171 checked. */
172 static bool
173 check_format_string (tree fntype, unsigned HOST_WIDE_INT format_num,
174 int flags, bool *no_add_attrs, int expected_format_type)
176 unsigned HOST_WIDE_INT i;
177 bool is_objc_sref, is_target_sref, is_char_ref;
178 tree ref;
179 int fmt_flags;
180 function_args_iterator iter;
182 i = 1;
183 FOREACH_FUNCTION_ARGS (fntype, ref, iter)
185 if (i == format_num)
186 break;
187 i++;
190 if (!ref
191 || !valid_stringptr_type_p (ref))
193 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
194 error ("format string argument is not a string type");
195 *no_add_attrs = true;
196 return false;
199 /* We only know that we want a suitable string reference. */
200 if (expected_format_type < 0)
201 return true;
203 /* Now check that the arg matches the expected type. */
204 is_char_ref =
205 (TYPE_MAIN_VARIANT (TREE_TYPE (ref)) == char_type_node);
207 fmt_flags = format_flags (expected_format_type);
208 is_objc_sref = is_target_sref = false;
209 if (!is_char_ref)
210 is_objc_sref = objc_string_ref_type_p (ref);
212 if (!(fmt_flags & FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL))
214 if (is_char_ref)
215 return true; /* OK, we expected a char and found one. */
216 else
218 /* We expected a char but found an extended string type. */
219 if (is_objc_sref)
220 error ("found a %qs reference but the format argument should"
221 " be a string", format_name (gcc_objc_string_format_type));
222 else
223 error ("found a %qT but the format argument should be a string",
224 ref);
225 *no_add_attrs = true;
226 return false;
230 /* We expect a string object type as the format arg. */
231 if (is_char_ref)
233 error ("format argument should be a %qs reference but"
234 " a string was found", format_name (expected_format_type));
235 *no_add_attrs = true;
236 return false;
239 /* We will assert that objective-c will support either its own string type
240 or the target-supplied variant. */
241 if (!is_objc_sref)
242 is_target_sref = (*targetcm.string_object_ref_type_p) ((const_tree) ref);
244 if (expected_format_type == (int) gcc_objc_string_format_type
245 && (is_objc_sref || is_target_sref))
246 return true;
248 /* We will allow a target string ref to match only itself. */
249 if (first_target_format_type
250 && expected_format_type >= first_target_format_type
251 && is_target_sref)
252 return true;
253 else
255 error ("format argument should be a %qs reference",
256 format_name (expected_format_type));
257 *no_add_attrs = true;
258 return false;
261 gcc_unreachable ();
264 /* Verify EXPR is a constant, and store its value.
265 If validated_p is true there should be no errors.
266 Returns true on success, false otherwise. */
267 static bool
268 get_constant (tree expr, unsigned HOST_WIDE_INT *value, int validated_p)
270 if (!tree_fits_uhwi_p (expr))
272 gcc_assert (!validated_p);
273 return false;
276 *value = TREE_INT_CST_LOW (expr);
278 return true;
281 /* Decode the arguments to a "format" attribute into a
282 function_format_info structure. It is already known that the list
283 is of the right length. If VALIDATED_P is true, then these
284 attributes have already been validated and must not be erroneous;
285 if false, it will give an error message. Returns true if the
286 attributes are successfully decoded, false otherwise. */
288 static bool
289 decode_format_attr (tree args, function_format_info *info, int validated_p)
291 tree format_type_id = TREE_VALUE (args);
292 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
293 tree first_arg_num_expr
294 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
296 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
298 gcc_assert (!validated_p);
299 error ("unrecognized format specifier");
300 return false;
302 else
304 const char *p = IDENTIFIER_POINTER (format_type_id);
306 p = convert_format_name_to_system_name (p);
308 info->format_type = decode_format_type (p);
310 if (!c_dialect_objc ()
311 && info->format_type == gcc_objc_string_format_type)
313 gcc_assert (!validated_p);
314 warning (OPT_Wformat_, "%qE is only allowed in Objective-C dialects",
315 format_type_id);
316 info->format_type = format_type_error;
317 return false;
320 if (info->format_type == format_type_error)
322 gcc_assert (!validated_p);
323 warning (OPT_Wformat_, "%qE is an unrecognized format function type",
324 format_type_id);
325 return false;
329 if (!get_constant (format_num_expr, &info->format_num, validated_p))
331 error ("format string has invalid operand number");
332 return false;
335 if (!get_constant (first_arg_num_expr, &info->first_arg_num, validated_p))
337 error ("%<...%> has invalid operand number");
338 return false;
341 if (info->first_arg_num != 0 && info->first_arg_num <= info->format_num)
343 gcc_assert (!validated_p);
344 error ("format string argument follows the args to be formatted");
345 return false;
348 return true;
351 /* Check a call to a format function against a parameter list. */
353 /* The C standard version C++ is treated as equivalent to
354 or inheriting from, for the purpose of format features supported. */
355 #define CPLUSPLUS_STD_VER (cxx_dialect < cxx11 ? STD_C94 : STD_C99)
356 /* The C standard version we are checking formats against when pedantic. */
357 #define C_STD_VER ((int) (c_dialect_cxx () \
358 ? CPLUSPLUS_STD_VER \
359 : (flag_isoc99 \
360 ? STD_C99 \
361 : (flag_isoc94 ? STD_C94 : STD_C89))))
362 /* The name to give to the standard version we are warning about when
363 pedantic. FEATURE_VER is the version in which the feature warned out
364 appeared, which is higher than C_STD_VER. */
365 #define C_STD_NAME(FEATURE_VER) (c_dialect_cxx () \
366 ? (cxx_dialect < cxx11 ? "ISO C++98" \
367 : "ISO C++11") \
368 : ((FEATURE_VER) == STD_EXT \
369 ? "ISO C" \
370 : "ISO C90"))
371 /* Adjust a C standard version, which may be STD_C9L, to account for
372 -Wno-long-long. Returns other standard versions unchanged. */
373 #define ADJ_STD(VER) ((int) ((VER) == STD_C9L \
374 ? (warn_long_long ? STD_C99 : STD_C89) \
375 : (VER)))
377 /* Enum describing the kind of specifiers present in the format and
378 requiring an argument. */
379 enum format_specifier_kind {
380 CF_KIND_FORMAT,
381 CF_KIND_FIELD_WIDTH,
382 CF_KIND_FIELD_PRECISION
385 static const char *kind_descriptions[] = {
386 N_("format"),
387 N_("field width specifier"),
388 N_("field precision specifier")
391 /* Structure describing details of a type expected in format checking,
392 and the type to check against it. */
393 struct format_wanted_type
395 /* The type wanted. */
396 tree wanted_type;
397 /* The name of this type to use in diagnostics. */
398 const char *wanted_type_name;
399 /* Should be type checked just for scalar width identity. */
400 int scalar_identity_flag;
401 /* The level of indirection through pointers at which this type occurs. */
402 int pointer_count;
403 /* Whether, when pointer_count is 1, to allow any character type when
404 pedantic, rather than just the character or void type specified. */
405 int char_lenient_flag;
406 /* Whether the argument, dereferenced once, is written into and so the
407 argument must not be a pointer to a const-qualified type. */
408 int writing_in_flag;
409 /* Whether the argument, dereferenced once, is read from and so
410 must not be a NULL pointer. */
411 int reading_from_flag;
412 /* The kind of specifier that this type is used for. */
413 enum format_specifier_kind kind;
414 /* The starting character of the specifier. This never includes the
415 initial percent sign. */
416 const char *format_start;
417 /* The length of the specifier. */
418 int format_length;
419 /* The actual parameter to check against the wanted type. */
420 tree param;
421 /* The argument number of that parameter. */
422 int arg_num;
423 /* The offset location of this argument with respect to the format
424 string location. */
425 unsigned int offset_loc;
426 /* The next type to check for this format conversion, or NULL if none. */
427 struct format_wanted_type *next;
430 /* Convenience macro for format_length_info meaning unused. */
431 #define NO_FMT NULL, FMT_LEN_none, STD_C89
433 static const format_length_info printf_length_specs[] =
435 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99, 0 },
436 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L, 0 },
437 { "q", FMT_LEN_ll, STD_EXT, NO_FMT, 0 },
438 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
439 { "z", FMT_LEN_z, STD_C99, NO_FMT, 0 },
440 { "Z", FMT_LEN_z, STD_EXT, NO_FMT, 0 },
441 { "t", FMT_LEN_t, STD_C99, NO_FMT, 0 },
442 { "j", FMT_LEN_j, STD_C99, NO_FMT, 0 },
443 { "H", FMT_LEN_H, STD_EXT, NO_FMT, 0 },
444 { "D", FMT_LEN_D, STD_EXT, "DD", FMT_LEN_DD, STD_EXT, 0 },
445 { NO_FMT, NO_FMT, 0 }
448 /* Length specifiers valid for asm_fprintf. */
449 static const format_length_info asm_fprintf_length_specs[] =
451 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89, 0 },
452 { "w", FMT_LEN_none, STD_C89, NO_FMT, 0 },
453 { NO_FMT, NO_FMT, 0 }
456 /* Length specifiers valid for GCC diagnostics. */
457 static const format_length_info gcc_diag_length_specs[] =
459 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89, 0 },
460 { "w", FMT_LEN_none, STD_C89, NO_FMT, 0 },
461 { NO_FMT, NO_FMT, 0 }
464 /* The custom diagnostics all accept the same length specifiers. */
465 #define gcc_tdiag_length_specs gcc_diag_length_specs
466 #define gcc_cdiag_length_specs gcc_diag_length_specs
467 #define gcc_cxxdiag_length_specs gcc_diag_length_specs
468 #define gcc_dump_printf_length_specs gcc_diag_length_specs
470 /* This differs from printf_length_specs only in that "Z" is not accepted. */
471 static const format_length_info scanf_length_specs[] =
473 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99, 0 },
474 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L, 0 },
475 { "q", FMT_LEN_ll, STD_EXT, NO_FMT, 0 },
476 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
477 { "z", FMT_LEN_z, STD_C99, NO_FMT, 0 },
478 { "t", FMT_LEN_t, STD_C99, NO_FMT, 0 },
479 { "j", FMT_LEN_j, STD_C99, NO_FMT, 0 },
480 { "H", FMT_LEN_H, STD_EXT, NO_FMT, 0 },
481 { "D", FMT_LEN_D, STD_EXT, "DD", FMT_LEN_DD, STD_EXT, 0 },
482 { NO_FMT, NO_FMT, 0 }
486 /* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings
487 make no sense for a format type not part of any C standard version. */
488 static const format_length_info strfmon_length_specs[] =
490 /* A GNU extension. */
491 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
492 { NO_FMT, NO_FMT, 0 }
496 /* For now, the Fortran front-end routines only use l as length modifier. */
497 static const format_length_info gcc_gfc_length_specs[] =
499 { "l", FMT_LEN_l, STD_C89, NO_FMT, 0 },
500 { NO_FMT, NO_FMT, 0 }
504 static const format_flag_spec printf_flag_specs[] =
506 { ' ', 0, 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
507 { '+', 0, 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
508 { '#', 0, 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
509 { '0', 0, 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
510 { '-', 0, 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
511 { '\'', 0, 0, 0, N_("''' flag"), N_("the ''' printf flag"), STD_EXT },
512 { 'I', 0, 0, 0, N_("'I' flag"), N_("the 'I' printf flag"), STD_EXT },
513 { 'w', 0, 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
514 { 'p', 0, 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
515 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
516 { 0, 0, 0, 0, NULL, NULL, STD_C89 }
520 static const format_flag_pair printf_flag_pairs[] =
522 { ' ', '+', 1, 0 },
523 { '0', '-', 1, 0 },
524 { '0', 'p', 1, 'i' },
525 { 0, 0, 0, 0 }
528 static const format_flag_spec asm_fprintf_flag_specs[] =
530 { ' ', 0, 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
531 { '+', 0, 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
532 { '#', 0, 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
533 { '0', 0, 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
534 { '-', 0, 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
535 { 'w', 0, 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
536 { 'p', 0, 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
537 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
538 { 0, 0, 0, 0, NULL, NULL, STD_C89 }
541 static const format_flag_pair asm_fprintf_flag_pairs[] =
543 { ' ', '+', 1, 0 },
544 { '0', '-', 1, 0 },
545 { '0', 'p', 1, 'i' },
546 { 0, 0, 0, 0 }
549 static const format_flag_pair gcc_diag_flag_pairs[] =
551 { 0, 0, 0, 0 }
554 #define gcc_tdiag_flag_pairs gcc_diag_flag_pairs
555 #define gcc_cdiag_flag_pairs gcc_diag_flag_pairs
556 #define gcc_cxxdiag_flag_pairs gcc_diag_flag_pairs
557 #define gcc_gfc_flag_pairs gcc_diag_flag_pairs
558 #define gcc_dump_printf_flag_pairs gcc_diag_flag_pairs
560 static const format_flag_spec gcc_diag_flag_specs[] =
562 { '+', 0, 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
563 { '#', 0, 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
564 { 'q', 0, 0, 1, N_("'q' flag"), N_("the 'q' diagnostic flag"), STD_C89 },
565 { 'p', 0, 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
566 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
567 { 0, 0, 0, 0, NULL, NULL, STD_C89 }
570 #define gcc_tdiag_flag_specs gcc_diag_flag_specs
571 #define gcc_cdiag_flag_specs gcc_diag_flag_specs
572 #define gcc_cxxdiag_flag_specs gcc_diag_flag_specs
573 #define gcc_gfc_flag_specs gcc_diag_flag_specs
574 #define gcc_dump_printf_flag_specs gcc_diag_flag_specs
576 static const format_flag_spec scanf_flag_specs[] =
578 { '*', 0, 0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89 },
579 { 'a', 0, 0, 0, N_("'a' flag"), N_("the 'a' scanf flag"), STD_EXT },
580 { 'm', 0, 0, 0, N_("'m' flag"), N_("the 'm' scanf flag"), STD_EXT },
581 { 'w', 0, 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89 },
582 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89 },
583 { '\'', 0, 0, 0, N_("''' flag"), N_("the ''' scanf flag"), STD_EXT },
584 { 'I', 0, 0, 0, N_("'I' flag"), N_("the 'I' scanf flag"), STD_EXT },
585 { 0, 0, 0, 0, NULL, NULL, STD_C89 }
589 static const format_flag_pair scanf_flag_pairs[] =
591 { '*', 'L', 0, 0 },
592 { 'a', 'm', 0, 0 },
593 { 0, 0, 0, 0 }
597 static const format_flag_spec strftime_flag_specs[] =
599 { '_', 0, 0, 0, N_("'_' flag"), N_("the '_' strftime flag"), STD_EXT },
600 { '-', 0, 0, 0, N_("'-' flag"), N_("the '-' strftime flag"), STD_EXT },
601 { '0', 0, 0, 0, N_("'0' flag"), N_("the '0' strftime flag"), STD_EXT },
602 { '^', 0, 0, 0, N_("'^' flag"), N_("the '^' strftime flag"), STD_EXT },
603 { '#', 0, 0, 0, N_("'#' flag"), N_("the '#' strftime flag"), STD_EXT },
604 { 'w', 0, 0, 0, N_("field width"), N_("field width in strftime format"), STD_EXT },
605 { 'E', 0, 0, 0, N_("'E' modifier"), N_("the 'E' strftime modifier"), STD_C99 },
606 { 'O', 0, 0, 0, N_("'O' modifier"), N_("the 'O' strftime modifier"), STD_C99 },
607 { 'O', 'o', 0, 0, NULL, N_("the 'O' modifier"), STD_EXT },
608 { 0, 0, 0, 0, NULL, NULL, STD_C89 }
612 static const format_flag_pair strftime_flag_pairs[] =
614 { 'E', 'O', 0, 0 },
615 { '_', '-', 0, 0 },
616 { '_', '0', 0, 0 },
617 { '-', '0', 0, 0 },
618 { '^', '#', 0, 0 },
619 { 0, 0, 0, 0 }
623 static const format_flag_spec strfmon_flag_specs[] =
625 { '=', 0, 1, 0, N_("fill character"), N_("fill character in strfmon format"), STD_C89 },
626 { '^', 0, 0, 0, N_("'^' flag"), N_("the '^' strfmon flag"), STD_C89 },
627 { '+', 0, 0, 0, N_("'+' flag"), N_("the '+' strfmon flag"), STD_C89 },
628 { '(', 0, 0, 0, N_("'(' flag"), N_("the '(' strfmon flag"), STD_C89 },
629 { '!', 0, 0, 0, N_("'!' flag"), N_("the '!' strfmon flag"), STD_C89 },
630 { '-', 0, 0, 0, N_("'-' flag"), N_("the '-' strfmon flag"), STD_C89 },
631 { 'w', 0, 0, 0, N_("field width"), N_("field width in strfmon format"), STD_C89 },
632 { '#', 0, 0, 0, N_("left precision"), N_("left precision in strfmon format"), STD_C89 },
633 { 'p', 0, 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89 },
634 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89 },
635 { 0, 0, 0, 0, NULL, NULL, STD_C89 }
638 static const format_flag_pair strfmon_flag_pairs[] =
640 { '+', '(', 0, 0 },
641 { 0, 0, 0, 0 }
645 static const format_char_info print_char_table[] =
647 /* C89 conversion specifiers. */
648 { "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 },
649 { "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 },
650 { "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 },
651 { "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 },
652 { "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 },
653 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, T94_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
654 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
655 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "c", NULL },
656 { "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 },
657 /* C99 conversion specifiers. */
658 { "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 },
659 { "aA", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "", NULL },
660 /* X/Open conversion specifiers. */
661 { "C", 0, STD_EXT, { TEX_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
662 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "R", NULL },
663 /* GNU conversion specifiers. */
664 { "m", 0, STD_EXT, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "", NULL },
665 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
668 static const format_char_info asm_fprintf_char_table[] =
670 /* C89 conversion specifiers. */
671 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +", "i", NULL },
672 { "oxX", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0#", "i", NULL },
673 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0", "i", NULL },
674 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
675 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
677 /* asm_fprintf conversion specifiers. */
678 { "O", 0, STD_C89, NOARGUMENTS, "", "", NULL },
679 { "R", 0, STD_C89, NOARGUMENTS, "", "", NULL },
680 { "I", 0, STD_C89, NOARGUMENTS, "", "", NULL },
681 { "L", 0, STD_C89, NOARGUMENTS, "", "", NULL },
682 { "U", 0, STD_C89, NOARGUMENTS, "", "", NULL },
683 { "r", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
684 { "z", 0, STD_C89, NOARGUMENTS, "", "", NULL },
685 { "@", 0, STD_C89, NOARGUMENTS, "", "", NULL },
686 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
689 /* GCC-specific format_char_info arrays. */
691 /* The conversion specifiers implemented within pp_format, and thus supported
692 by all pretty_printer instances within GCC. */
694 #define PP_FORMAT_CHAR_TABLE \
695 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL }, \
696 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL }, \
697 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL }, \
698 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL }, \
699 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL }, \
700 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL }, \
701 { "r", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "//cR", NULL }, \
702 { "<", 0, STD_C89, NOARGUMENTS, "", "<", NULL }, \
703 { ">", 0, STD_C89, NOARGUMENTS, "", ">", NULL }, \
704 { "'" , 0, STD_C89, NOARGUMENTS, "", "", NULL }, \
705 { "R", 0, STD_C89, NOARGUMENTS, "", "\\", NULL }, \
706 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL }, \
707 { "Z", 1, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", &gcc_diag_char_table[0] }
709 static const format_char_info gcc_diag_char_table[] =
711 /* The conversion specifiers implemented within pp_format. */
712 PP_FORMAT_CHAR_TABLE,
714 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
717 static const format_char_info gcc_tdiag_char_table[] =
719 /* The conversion specifiers implemented within pp_format. */
720 PP_FORMAT_CHAR_TABLE,
722 /* Custom conversion specifiers implemented by default_tree_printer. */
724 /* These will require a "tree" at runtime. */
725 { "DFTV", 1, STD_C89, { T89_T, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "'", NULL },
726 { "E", 1, STD_C89, { T89_T, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
727 { "K", 1, STD_C89, { T89_T, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "\"", NULL },
729 /* G requires a "gimple*" argument at runtime. */
730 { "G", 1, STD_C89, { T89_G, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "\"", NULL },
732 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
735 static const format_char_info gcc_cdiag_char_table[] =
737 /* The conversion specifiers implemented within pp_format. */
738 PP_FORMAT_CHAR_TABLE,
740 /* Custom conversion specifiers implemented by c_tree_printer. */
742 /* These will require a "tree" at runtime. */
743 { "DFTV", 1, STD_C89, { T89_T, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "'", NULL },
744 { "E", 1, STD_C89, { T89_T, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
745 { "K", 1, STD_C89, { T89_T, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "\"", NULL },
747 /* G requires a "gimple*" argument at runtime. */
748 { "G", 1, STD_C89, { T89_G, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "\"", NULL },
750 { "v", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
752 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
755 static const format_char_info gcc_cxxdiag_char_table[] =
757 /* The conversion specifiers implemented within pp_format. */
758 PP_FORMAT_CHAR_TABLE,
760 /* Custom conversion specifiers implemented by cp_printer. */
762 /* These will require a "tree" at runtime. */
763 { "ADFHISTVX",1,STD_C89,{ T89_T, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+#", "'", NULL },
764 { "E", 1,STD_C89,{ T89_T, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+#", "", NULL },
765 { "K", 1, STD_C89,{ T89_T, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "\"", NULL },
767 /* G requires a "gimple*" argument at runtime. */
768 { "G", 1, STD_C89,{ T89_G, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "\"", NULL },
770 /* These accept either an 'int' or an 'enum tree_code' (which is handled as an 'int'.) */
771 { "CLOPQ",0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
773 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
776 static const format_char_info gcc_gfc_char_table[] =
778 /* C89 conversion specifiers. */
779 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
780 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
781 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
782 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "cR", NULL },
784 /* gfc conversion specifiers. */
786 { "C", 0, STD_C89, NOARGUMENTS, "", "", NULL },
788 /* This will require a "locus" at runtime. */
789 { "L", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "R", NULL },
791 /* These will require nothing. */
792 { "<>",0, STD_C89, NOARGUMENTS, "", "", NULL },
793 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
796 static const format_char_info gcc_dump_printf_char_table[] =
798 /* The conversion specifiers implemented within pp_format. */
799 PP_FORMAT_CHAR_TABLE,
801 /* Custom conversion specifiers implemented by dump_pretty_printer. */
803 /* E and G require a "gimple *" argument at runtime. */
804 { "EG", 1, STD_C89, { T89_G, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "\"", NULL },
806 /* T requires a "tree" at runtime. */
807 { "T", 1, STD_C89, { T89_T, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "\"", NULL },
809 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
812 static const format_char_info scan_char_table[] =
814 /* C89 conversion specifiers. */
815 { "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 },
816 { "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 },
817 { "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 },
818 { "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 },
819 { "c", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*mw", "cW", NULL },
820 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "cW", NULL },
821 { "[", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "cW[", NULL },
822 { "p", 2, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
823 { "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 },
824 /* C99 conversion specifiers. */
825 { "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 },
826 { "aA", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w'", "W", NULL },
827 /* X/Open conversion specifiers. */
828 { "C", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*mw", "W", NULL },
829 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "W", NULL },
830 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
833 static const format_char_info time_char_table[] =
835 /* C89 conversion specifiers. */
836 { "ABZab", 0, STD_C89, NOLENGTHS, "^#", "", NULL },
837 { "cx", 0, STD_C89, NOLENGTHS, "E", "3", NULL },
838 { "HIMSUWdmw", 0, STD_C89, NOLENGTHS, "-_0Ow", "", NULL },
839 { "j", 0, STD_C89, NOLENGTHS, "-_0Ow", "o", NULL },
840 { "p", 0, STD_C89, NOLENGTHS, "#", "", NULL },
841 { "X", 0, STD_C89, NOLENGTHS, "E", "", NULL },
842 { "y", 0, STD_C89, NOLENGTHS, "EO-_0w", "4", NULL },
843 { "Y", 0, STD_C89, NOLENGTHS, "-_0EOw", "o", NULL },
844 { "%", 0, STD_C89, NOLENGTHS, "", "", NULL },
845 /* C99 conversion specifiers. */
846 { "C", 0, STD_C99, NOLENGTHS, "-_0EOw", "o", NULL },
847 { "D", 0, STD_C99, NOLENGTHS, "", "2", NULL },
848 { "eVu", 0, STD_C99, NOLENGTHS, "-_0Ow", "", NULL },
849 { "FRTnrt", 0, STD_C99, NOLENGTHS, "", "", NULL },
850 { "g", 0, STD_C99, NOLENGTHS, "O-_0w", "2o", NULL },
851 { "G", 0, STD_C99, NOLENGTHS, "-_0Ow", "o", NULL },
852 { "h", 0, STD_C99, NOLENGTHS, "^#", "", NULL },
853 { "z", 0, STD_C99, NOLENGTHS, "O", "o", NULL },
854 /* GNU conversion specifiers. */
855 { "kls", 0, STD_EXT, NOLENGTHS, "-_0Ow", "", NULL },
856 { "P", 0, STD_EXT, NOLENGTHS, "", "", NULL },
857 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
860 static const format_char_info monetary_char_table[] =
862 { "in", 0, STD_C89, { T89_D, BADLEN, BADLEN, BADLEN, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "=^+(!-w#p", "", NULL },
863 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
866 /* This must be in the same order as enum format_type. */
867 static const format_kind_info format_types_orig[] =
869 { "gnu_printf", printf_length_specs, print_char_table, " +#0-'I", NULL,
870 printf_flag_specs, printf_flag_pairs,
871 FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_EMPTY_PREC_OK,
872 'w', 0, 'p', 0, 'L', 0,
873 &integer_type_node, &integer_type_node
875 { "asm_fprintf", asm_fprintf_length_specs, asm_fprintf_char_table, " +#0-", NULL,
876 asm_fprintf_flag_specs, asm_fprintf_flag_pairs,
877 FMT_FLAG_ARG_CONVERT|FMT_FLAG_EMPTY_PREC_OK,
878 'w', 0, 'p', 0, 'L', 0,
879 NULL, NULL
881 { "gcc_diag", gcc_diag_length_specs, gcc_diag_char_table, "q+#", NULL,
882 gcc_diag_flag_specs, gcc_diag_flag_pairs,
883 FMT_FLAG_ARG_CONVERT,
884 0, 0, 'p', 0, 'L', 0,
885 NULL, &integer_type_node
887 { "gcc_tdiag", gcc_tdiag_length_specs, gcc_tdiag_char_table, "q+#", NULL,
888 gcc_tdiag_flag_specs, gcc_tdiag_flag_pairs,
889 FMT_FLAG_ARG_CONVERT,
890 0, 0, 'p', 0, 'L', 0,
891 NULL, &integer_type_node
893 { "gcc_cdiag", gcc_cdiag_length_specs, gcc_cdiag_char_table, "q+#", NULL,
894 gcc_cdiag_flag_specs, gcc_cdiag_flag_pairs,
895 FMT_FLAG_ARG_CONVERT,
896 0, 0, 'p', 0, 'L', 0,
897 NULL, &integer_type_node
899 { "gcc_cxxdiag", gcc_cxxdiag_length_specs, gcc_cxxdiag_char_table, "q+#", NULL,
900 gcc_cxxdiag_flag_specs, gcc_cxxdiag_flag_pairs,
901 FMT_FLAG_ARG_CONVERT,
902 0, 0, 'p', 0, 'L', 0,
903 NULL, &integer_type_node
905 { "gcc_gfc", gcc_gfc_length_specs, gcc_gfc_char_table, "q+#", NULL,
906 gcc_gfc_flag_specs, gcc_gfc_flag_pairs,
907 FMT_FLAG_ARG_CONVERT,
908 0, 0, 0, 0, 0, 0,
909 NULL, NULL
911 { "gcc_dump_printf", gcc_dump_printf_length_specs,
912 gcc_dump_printf_char_table, "q+#", NULL,
913 gcc_dump_printf_flag_specs, gcc_dump_printf_flag_pairs,
914 FMT_FLAG_ARG_CONVERT,
915 0, 0, 'p', 0, 'L', 0,
916 NULL, &integer_type_node
918 { "NSString", NULL, NULL, NULL, NULL,
919 NULL, NULL,
920 FMT_FLAG_ARG_CONVERT|FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL, 0, 0, 0, 0, 0, 0,
921 NULL, NULL
923 { "gnu_scanf", scanf_length_specs, scan_char_table, "*'I", NULL,
924 scanf_flag_specs, scanf_flag_pairs,
925 FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_ZERO_WIDTH_BAD|FMT_FLAG_DOLLAR_GAP_POINTER_OK,
926 'w', 0, 0, '*', 'L', 'm',
927 NULL, NULL
929 { "gnu_strftime", NULL, time_char_table, "_-0^#", "EO",
930 strftime_flag_specs, strftime_flag_pairs,
931 FMT_FLAG_FANCY_PERCENT_OK, 'w', 0, 0, 0, 0, 0,
932 NULL, NULL
934 { "gnu_strfmon", strfmon_length_specs, monetary_char_table, "=^+(!-", NULL,
935 strfmon_flag_specs, strfmon_flag_pairs,
936 FMT_FLAG_ARG_CONVERT, 'w', '#', 'p', 0, 'L', 0,
937 NULL, NULL
941 /* This layer of indirection allows GCC to reassign format_types with
942 new data if necessary, while still allowing the original data to be
943 const. */
944 static const format_kind_info *format_types = format_types_orig;
945 /* We can modify this one. We also add target-specific format types
946 to the end of the array. */
947 static format_kind_info *dynamic_format_types;
949 static int n_format_types = ARRAY_SIZE (format_types_orig);
951 /* Structure detailing the results of checking a format function call
952 where the format expression may be a conditional expression with
953 many leaves resulting from nested conditional expressions. */
954 struct format_check_results
956 /* Number of leaves of the format argument that could not be checked
957 as they were not string literals. */
958 int number_non_literal;
959 /* Number of leaves of the format argument that were null pointers or
960 string literals, but had extra format arguments. */
961 int number_extra_args;
962 location_t extra_arg_loc;
963 /* Number of leaves of the format argument that were null pointers or
964 string literals, but had extra format arguments and used $ operand
965 numbers. */
966 int number_dollar_extra_args;
967 /* Number of leaves of the format argument that were wide string
968 literals. */
969 int number_wide;
970 /* Number of leaves of the format argument that are not array of "char". */
971 int number_non_char;
972 /* Number of leaves of the format argument that were empty strings. */
973 int number_empty;
974 /* Number of leaves of the format argument that were unterminated
975 strings. */
976 int number_unterminated;
977 /* Number of leaves of the format argument that were not counted above. */
978 int number_other;
979 /* Location of the format string. */
980 location_t format_string_loc;
983 struct format_check_context
985 format_check_results *res;
986 function_format_info *info;
987 tree params;
988 vec<location_t> *arglocs;
991 /* Return the format name (as specified in the original table) for the format
992 type indicated by format_num. */
993 static const char *
994 format_name (int format_num)
996 if (format_num >= 0 && format_num < n_format_types)
997 return format_types[format_num].name;
998 gcc_unreachable ();
1001 /* Return the format flags (as specified in the original table) for the format
1002 type indicated by format_num. */
1003 static int
1004 format_flags (int format_num)
1006 if (format_num >= 0 && format_num < n_format_types)
1007 return format_types[format_num].flags;
1008 gcc_unreachable ();
1011 static void check_format_info (function_format_info *, tree,
1012 vec<location_t> *);
1013 static void check_format_arg (void *, tree, unsigned HOST_WIDE_INT);
1014 static void check_format_info_main (format_check_results *,
1015 function_format_info *, const char *,
1016 location_t, tree,
1017 int, tree,
1018 unsigned HOST_WIDE_INT,
1019 object_allocator<format_wanted_type> &,
1020 vec<location_t> *);
1022 static void init_dollar_format_checking (int, tree);
1023 static int maybe_read_dollar_number (const char **, int,
1024 tree, tree *, const format_kind_info *);
1025 static bool avoid_dollar_number (const char *);
1026 static void finish_dollar_format_checking (format_check_results *, int);
1028 static const format_flag_spec *get_flag_spec (const format_flag_spec *,
1029 int, const char *);
1031 static void check_format_types (const substring_loc &fmt_loc,
1032 format_wanted_type *,
1033 const format_kind_info *fki,
1034 int offset_to_type_start,
1035 char conversion_char,
1036 vec<location_t> *arglocs);
1037 static void format_type_warning (const substring_loc &fmt_loc,
1038 location_t param_loc,
1039 format_wanted_type *, tree,
1040 tree,
1041 const format_kind_info *fki,
1042 int offset_to_type_start,
1043 char conversion_char);
1045 /* Decode a format type from a string, returning the type, or
1046 format_type_error if not valid, in which case the caller should print an
1047 error message. */
1048 static int
1049 decode_format_type (const char *s)
1051 int i;
1052 int slen;
1054 s = convert_format_name_to_system_name (s);
1055 slen = strlen (s);
1056 for (i = 0; i < n_format_types; i++)
1058 int alen;
1059 if (!strcmp (s, format_types[i].name))
1060 return i;
1061 alen = strlen (format_types[i].name);
1062 if (slen == alen + 4 && s[0] == '_' && s[1] == '_'
1063 && s[slen - 1] == '_' && s[slen - 2] == '_'
1064 && !strncmp (s + 2, format_types[i].name, alen))
1065 return i;
1067 return format_type_error;
1071 /* Check the argument list of a call to printf, scanf, etc.
1072 ATTRS are the attributes on the function type. There are NARGS argument
1073 values in the array ARGARRAY.
1074 Also, if -Wsuggest-attribute=format,
1075 warn for calls to vprintf or vscanf in functions with no such format
1076 attribute themselves. */
1078 void
1079 check_function_format (tree attrs, int nargs, tree *argarray,
1080 vec<location_t> *arglocs)
1082 tree a;
1084 /* See if this function has any format attributes. */
1085 for (a = attrs; a; a = TREE_CHAIN (a))
1087 if (is_attribute_p ("format", TREE_PURPOSE (a)))
1089 /* Yup; check it. */
1090 function_format_info info;
1091 decode_format_attr (TREE_VALUE (a), &info, /*validated=*/true);
1092 if (warn_format)
1094 /* FIXME: Rewrite all the internal functions in this file
1095 to use the ARGARRAY directly instead of constructing this
1096 temporary list. */
1097 tree params = NULL_TREE;
1098 int i;
1099 for (i = nargs - 1; i >= 0; i--)
1100 params = tree_cons (NULL_TREE, argarray[i], params);
1101 check_format_info (&info, params, arglocs);
1104 /* Attempt to detect whether the current function might benefit
1105 from the format attribute if the called function is decorated
1106 with it. Avoid using calls with string literal formats for
1107 guidance since those are unlikely to be viable candidates. */
1108 if (warn_suggest_attribute_format
1109 && current_function_decl != NULL_TREE
1110 && info.first_arg_num == 0
1111 && (format_types[info.format_type].flags
1112 & (int) FMT_FLAG_ARG_CONVERT)
1113 /* c_strlen will fail for a function parameter but succeed
1114 for a literal or constant array. */
1115 && !c_strlen (argarray[info.format_num - 1], 1))
1117 tree c;
1118 for (c = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
1120 c = TREE_CHAIN (c))
1121 if (is_attribute_p ("format", TREE_PURPOSE (c))
1122 && (decode_format_type (IDENTIFIER_POINTER
1123 (TREE_VALUE (TREE_VALUE (c))))
1124 == info.format_type))
1125 break;
1126 if (c == NULL_TREE)
1128 /* Check if the current function has a parameter to which
1129 the format attribute could be attached; if not, it
1130 can't be a candidate for a format attribute, despite
1131 the vprintf-like or vscanf-like call. */
1132 tree args;
1133 for (args = DECL_ARGUMENTS (current_function_decl);
1134 args != 0;
1135 args = DECL_CHAIN (args))
1137 if (TREE_CODE (TREE_TYPE (args)) == POINTER_TYPE
1138 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args)))
1139 == char_type_node))
1140 break;
1142 if (args != 0)
1143 warning (OPT_Wsuggest_attribute_format, "function %qD "
1144 "might be a candidate for %qs format attribute",
1145 current_function_decl,
1146 format_types[info.format_type].name);
1154 /* Variables used by the checking of $ operand number formats. */
1155 static char *dollar_arguments_used = NULL;
1156 static char *dollar_arguments_pointer_p = NULL;
1157 static int dollar_arguments_alloc = 0;
1158 static int dollar_arguments_count;
1159 static int dollar_first_arg_num;
1160 static int dollar_max_arg_used;
1161 static int dollar_format_warned;
1163 /* Initialize the checking for a format string that may contain $
1164 parameter number specifications; we will need to keep track of whether
1165 each parameter has been used. FIRST_ARG_NUM is the number of the first
1166 argument that is a parameter to the format, or 0 for a vprintf-style
1167 function; PARAMS is the list of arguments starting at this argument. */
1169 static void
1170 init_dollar_format_checking (int first_arg_num, tree params)
1172 tree oparams = params;
1174 dollar_first_arg_num = first_arg_num;
1175 dollar_arguments_count = 0;
1176 dollar_max_arg_used = 0;
1177 dollar_format_warned = 0;
1178 if (first_arg_num > 0)
1180 while (params)
1182 dollar_arguments_count++;
1183 params = TREE_CHAIN (params);
1186 if (dollar_arguments_alloc < dollar_arguments_count)
1188 free (dollar_arguments_used);
1189 free (dollar_arguments_pointer_p);
1190 dollar_arguments_alloc = dollar_arguments_count;
1191 dollar_arguments_used = XNEWVEC (char, dollar_arguments_alloc);
1192 dollar_arguments_pointer_p = XNEWVEC (char, dollar_arguments_alloc);
1194 if (dollar_arguments_alloc)
1196 memset (dollar_arguments_used, 0, dollar_arguments_alloc);
1197 if (first_arg_num > 0)
1199 int i = 0;
1200 params = oparams;
1201 while (params)
1203 dollar_arguments_pointer_p[i] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params)))
1204 == POINTER_TYPE);
1205 params = TREE_CHAIN (params);
1206 i++;
1213 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
1214 is set, it is an error if one is not found; otherwise, it is OK. If
1215 such a number is found, check whether it is within range and mark that
1216 numbered operand as being used for later checking. Returns the operand
1217 number if found and within range, zero if no such number was found and
1218 this is OK, or -1 on error. PARAMS points to the first operand of the
1219 format; PARAM_PTR is made to point to the parameter referred to. If
1220 a $ format is found, *FORMAT is updated to point just after it. */
1222 static int
1223 maybe_read_dollar_number (const char **format,
1224 int dollar_needed, tree params, tree *param_ptr,
1225 const format_kind_info *fki)
1227 int argnum;
1228 int overflow_flag;
1229 const char *fcp = *format;
1230 if (!ISDIGIT (*fcp))
1232 if (dollar_needed)
1234 warning (OPT_Wformat_, "missing $ operand number in format");
1235 return -1;
1237 else
1238 return 0;
1240 argnum = 0;
1241 overflow_flag = 0;
1242 while (ISDIGIT (*fcp))
1244 int nargnum;
1245 nargnum = 10 * argnum + (*fcp - '0');
1246 if (nargnum < 0 || nargnum / 10 != argnum)
1247 overflow_flag = 1;
1248 argnum = nargnum;
1249 fcp++;
1251 if (*fcp != '$')
1253 if (dollar_needed)
1255 warning (OPT_Wformat_, "missing $ operand number in format");
1256 return -1;
1258 else
1259 return 0;
1261 *format = fcp + 1;
1262 if (pedantic && !dollar_format_warned)
1264 warning (OPT_Wformat_, "%s does not support %%n$ operand number formats",
1265 C_STD_NAME (STD_EXT));
1266 dollar_format_warned = 1;
1268 if (overflow_flag || argnum == 0
1269 || (dollar_first_arg_num && argnum > dollar_arguments_count))
1271 warning (OPT_Wformat_, "operand number out of range in format");
1272 return -1;
1274 if (argnum > dollar_max_arg_used)
1275 dollar_max_arg_used = argnum;
1276 /* For vprintf-style functions we may need to allocate more memory to
1277 track which arguments are used. */
1278 while (dollar_arguments_alloc < dollar_max_arg_used)
1280 int nalloc;
1281 nalloc = 2 * dollar_arguments_alloc + 16;
1282 dollar_arguments_used = XRESIZEVEC (char, dollar_arguments_used,
1283 nalloc);
1284 dollar_arguments_pointer_p = XRESIZEVEC (char, dollar_arguments_pointer_p,
1285 nalloc);
1286 memset (dollar_arguments_used + dollar_arguments_alloc, 0,
1287 nalloc - dollar_arguments_alloc);
1288 dollar_arguments_alloc = nalloc;
1290 if (!(fki->flags & (int) FMT_FLAG_DOLLAR_MULTIPLE)
1291 && dollar_arguments_used[argnum - 1] == 1)
1293 dollar_arguments_used[argnum - 1] = 2;
1294 warning (OPT_Wformat_, "format argument %d used more than once in %s format",
1295 argnum, fki->name);
1297 else
1298 dollar_arguments_used[argnum - 1] = 1;
1299 if (dollar_first_arg_num)
1301 int i;
1302 *param_ptr = params;
1303 for (i = 1; i < argnum && *param_ptr != 0; i++)
1304 *param_ptr = TREE_CHAIN (*param_ptr);
1306 /* This case shouldn't be caught here. */
1307 gcc_assert (*param_ptr);
1309 else
1310 *param_ptr = 0;
1311 return argnum;
1314 /* Ensure that FORMAT does not start with a decimal number followed by
1315 a $; give a diagnostic and return true if it does, false otherwise. */
1317 static bool
1318 avoid_dollar_number (const char *format)
1320 if (!ISDIGIT (*format))
1321 return false;
1322 while (ISDIGIT (*format))
1323 format++;
1324 if (*format == '$')
1326 warning (OPT_Wformat_, "$ operand number used after format without operand number");
1327 return true;
1329 return false;
1333 /* Finish the checking for a format string that used $ operand number formats
1334 instead of non-$ formats. We check for unused operands before used ones
1335 (a serious error, since the implementation of the format function
1336 can't know what types to pass to va_arg to find the later arguments).
1337 and for unused operands at the end of the format (if we know how many
1338 arguments the format had, so not for vprintf). If there were operand
1339 numbers out of range on a non-vprintf-style format, we won't have reached
1340 here. If POINTER_GAP_OK, unused arguments are OK if all arguments are
1341 pointers. */
1343 static void
1344 finish_dollar_format_checking (format_check_results *res, int pointer_gap_ok)
1346 int i;
1347 bool found_pointer_gap = false;
1348 for (i = 0; i < dollar_max_arg_used; i++)
1350 if (!dollar_arguments_used[i])
1352 if (pointer_gap_ok && (dollar_first_arg_num == 0
1353 || dollar_arguments_pointer_p[i]))
1354 found_pointer_gap = true;
1355 else
1356 warning_at (res->format_string_loc, OPT_Wformat_,
1357 "format argument %d unused before used argument %d in $-style format",
1358 i + 1, dollar_max_arg_used);
1361 if (found_pointer_gap
1362 || (dollar_first_arg_num
1363 && dollar_max_arg_used < dollar_arguments_count))
1365 res->number_other--;
1366 res->number_dollar_extra_args++;
1371 /* Retrieve the specification for a format flag. SPEC contains the
1372 specifications for format flags for the applicable kind of format.
1373 FLAG is the flag in question. If PREDICATES is NULL, the basic
1374 spec for that flag must be retrieved and must exist. If
1375 PREDICATES is not NULL, it is a string listing possible predicates
1376 for the spec entry; if an entry predicated on any of these is
1377 found, it is returned, otherwise NULL is returned. */
1379 static const format_flag_spec *
1380 get_flag_spec (const format_flag_spec *spec, int flag, const char *predicates)
1382 int i;
1383 for (i = 0; spec[i].flag_char != 0; i++)
1385 if (spec[i].flag_char != flag)
1386 continue;
1387 if (predicates != NULL)
1389 if (spec[i].predicate != 0
1390 && strchr (predicates, spec[i].predicate) != 0)
1391 return &spec[i];
1393 else if (spec[i].predicate == 0)
1394 return &spec[i];
1396 gcc_assert (predicates);
1397 return NULL;
1401 /* Check the argument list of a call to printf, scanf, etc.
1402 INFO points to the function_format_info structure.
1403 PARAMS is the list of argument values. */
1405 static void
1406 check_format_info (function_format_info *info, tree params,
1407 vec<location_t> *arglocs)
1409 format_check_context format_ctx;
1410 unsigned HOST_WIDE_INT arg_num;
1411 tree format_tree;
1412 format_check_results res;
1413 /* Skip to format argument. If the argument isn't available, there's
1414 no work for us to do; prototype checking will catch the problem. */
1415 for (arg_num = 1; ; ++arg_num)
1417 if (params == 0)
1418 return;
1419 if (arg_num == info->format_num)
1420 break;
1421 params = TREE_CHAIN (params);
1423 format_tree = TREE_VALUE (params);
1424 params = TREE_CHAIN (params);
1425 if (format_tree == 0)
1426 return;
1428 res.number_non_literal = 0;
1429 res.number_extra_args = 0;
1430 res.extra_arg_loc = UNKNOWN_LOCATION;
1431 res.number_dollar_extra_args = 0;
1432 res.number_wide = 0;
1433 res.number_non_char = 0;
1434 res.number_empty = 0;
1435 res.number_unterminated = 0;
1436 res.number_other = 0;
1437 res.format_string_loc = input_location;
1439 format_ctx.res = &res;
1440 format_ctx.info = info;
1441 format_ctx.params = params;
1442 format_ctx.arglocs = arglocs;
1444 check_function_arguments_recurse (check_format_arg, &format_ctx,
1445 format_tree, arg_num);
1447 location_t loc = format_ctx.res->format_string_loc;
1449 if (res.number_non_literal > 0)
1451 /* Functions taking a va_list normally pass a non-literal format
1452 string. These functions typically are declared with
1453 first_arg_num == 0, so avoid warning in those cases. */
1454 if (!(format_types[info->format_type].flags & (int) FMT_FLAG_ARG_CONVERT))
1456 /* For strftime-like formats, warn for not checking the format
1457 string; but there are no arguments to check. */
1458 warning_at (loc, OPT_Wformat_nonliteral,
1459 "format not a string literal, format string not checked");
1461 else if (info->first_arg_num != 0)
1463 /* If there are no arguments for the format at all, we may have
1464 printf (foo) which is likely to be a security hole. */
1465 while (arg_num + 1 < info->first_arg_num)
1467 if (params == 0)
1468 break;
1469 params = TREE_CHAIN (params);
1470 ++arg_num;
1472 if (params == 0 && warn_format_security)
1473 warning_at (loc, OPT_Wformat_security,
1474 "format not a string literal and no format arguments");
1475 else if (params == 0 && warn_format_nonliteral)
1476 warning_at (loc, OPT_Wformat_nonliteral,
1477 "format not a string literal and no format arguments");
1478 else
1479 warning_at (loc, OPT_Wformat_nonliteral,
1480 "format not a string literal, argument types not checked");
1484 /* If there were extra arguments to the format, normally warn. However,
1485 the standard does say extra arguments are ignored, so in the specific
1486 case where we have multiple leaves (conditional expressions or
1487 ngettext) allow extra arguments if at least one leaf didn't have extra
1488 arguments, but was otherwise OK (either non-literal or checked OK).
1489 If the format is an empty string, this should be counted similarly to the
1490 case of extra format arguments. */
1491 if (res.number_extra_args > 0 && res.number_non_literal == 0
1492 && res.number_other == 0)
1494 if (res.extra_arg_loc == UNKNOWN_LOCATION)
1495 res.extra_arg_loc = loc;
1496 warning_at (res.extra_arg_loc, OPT_Wformat_extra_args,
1497 "too many arguments for format");
1499 if (res.number_dollar_extra_args > 0 && res.number_non_literal == 0
1500 && res.number_other == 0)
1501 warning_at (loc, OPT_Wformat_extra_args, "unused arguments in $-style format");
1502 if (res.number_empty > 0 && res.number_non_literal == 0
1503 && res.number_other == 0)
1504 warning_at (loc, OPT_Wformat_zero_length, "zero-length %s format string",
1505 format_types[info->format_type].name);
1507 if (res.number_wide > 0)
1508 warning_at (loc, OPT_Wformat_, "format is a wide character string");
1510 if (res.number_non_char > 0)
1511 warning_at (loc, OPT_Wformat_,
1512 "format string is not an array of type %qs", "char");
1514 if (res.number_unterminated > 0)
1515 warning_at (loc, OPT_Wformat_, "unterminated format string");
1518 /* Callback from check_function_arguments_recurse to check a
1519 format string. FORMAT_TREE is the format parameter. ARG_NUM
1520 is the number of the format argument. CTX points to a
1521 format_check_context. */
1523 static void
1524 check_format_arg (void *ctx, tree format_tree,
1525 unsigned HOST_WIDE_INT arg_num)
1527 format_check_context *format_ctx = (format_check_context *) ctx;
1528 format_check_results *res = format_ctx->res;
1529 function_format_info *info = format_ctx->info;
1530 tree params = format_ctx->params;
1531 vec<location_t> *arglocs = format_ctx->arglocs;
1533 int format_length;
1534 HOST_WIDE_INT offset;
1535 const char *format_chars;
1536 tree array_size = 0;
1537 tree array_init;
1539 location_t fmt_param_loc = EXPR_LOC_OR_LOC (format_tree, input_location);
1541 /* Pull out a constant value if the front end didn't, and handle location
1542 wrappers. */
1543 format_tree = fold_for_warn (format_tree);
1544 STRIP_NOPS (format_tree);
1546 if (integer_zerop (format_tree))
1548 /* Skip to first argument to check, so we can see if this format
1549 has any arguments (it shouldn't). */
1550 while (arg_num + 1 < info->first_arg_num)
1552 if (params == 0)
1553 return;
1554 params = TREE_CHAIN (params);
1555 ++arg_num;
1558 if (params == 0)
1559 res->number_other++;
1560 else
1562 if (res->number_extra_args == 0)
1563 res->extra_arg_loc = EXPR_LOC_OR_LOC (TREE_VALUE (params),
1564 input_location);
1565 res->number_extra_args++;
1567 return;
1570 offset = 0;
1571 if (TREE_CODE (format_tree) == POINTER_PLUS_EXPR)
1573 tree arg0, arg1;
1575 arg0 = TREE_OPERAND (format_tree, 0);
1576 arg1 = TREE_OPERAND (format_tree, 1);
1577 STRIP_NOPS (arg0);
1578 STRIP_NOPS (arg1);
1579 if (TREE_CODE (arg1) == INTEGER_CST)
1580 format_tree = arg0;
1581 else
1583 res->number_non_literal++;
1584 return;
1586 /* POINTER_PLUS_EXPR offsets are to be interpreted signed. */
1587 if (!cst_and_fits_in_hwi (arg1))
1589 res->number_non_literal++;
1590 return;
1592 offset = int_cst_value (arg1);
1594 if (TREE_CODE (format_tree) != ADDR_EXPR)
1596 res->number_non_literal++;
1597 return;
1599 res->format_string_loc = EXPR_LOC_OR_LOC (format_tree, input_location);
1600 format_tree = TREE_OPERAND (format_tree, 0);
1601 if (format_types[info->format_type].flags
1602 & (int) FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL)
1604 bool objc_str = (info->format_type == gcc_objc_string_format_type);
1605 /* We cannot examine this string here - but we can check that it is
1606 a valid type. */
1607 if (TREE_CODE (format_tree) != CONST_DECL
1608 || !((objc_str && objc_string_ref_type_p (TREE_TYPE (format_tree)))
1609 || (*targetcm.string_object_ref_type_p)
1610 ((const_tree) TREE_TYPE (format_tree))))
1612 res->number_non_literal++;
1613 return;
1615 /* Skip to first argument to check. */
1616 while (arg_num + 1 < info->first_arg_num)
1618 if (params == 0)
1619 return;
1620 params = TREE_CHAIN (params);
1621 ++arg_num;
1623 /* So, we have a valid literal string object and one or more params.
1624 We need to use an external helper to parse the string into format
1625 info. For Objective-C variants we provide the resource within the
1626 objc tree, for target variants, via a hook. */
1627 if (objc_str)
1628 objc_check_format_arg (format_tree, params);
1629 else if (targetcm.check_string_object_format_arg)
1630 (*targetcm.check_string_object_format_arg) (format_tree, params);
1631 /* Else we can't handle it and retire quietly. */
1632 return;
1634 if (TREE_CODE (format_tree) == ARRAY_REF
1635 && tree_fits_shwi_p (TREE_OPERAND (format_tree, 1))
1636 && (offset += tree_to_shwi (TREE_OPERAND (format_tree, 1))) >= 0)
1637 format_tree = TREE_OPERAND (format_tree, 0);
1638 if (offset < 0)
1640 res->number_non_literal++;
1641 return;
1643 if (VAR_P (format_tree)
1644 && TREE_CODE (TREE_TYPE (format_tree)) == ARRAY_TYPE
1645 && (array_init = decl_constant_value (format_tree)) != format_tree
1646 && TREE_CODE (array_init) == STRING_CST)
1648 /* Extract the string constant initializer. Note that this may include
1649 a trailing NUL character that is not in the array (e.g.
1650 const char a[3] = "foo";). */
1651 array_size = DECL_SIZE_UNIT (format_tree);
1652 format_tree = array_init;
1654 if (TREE_CODE (format_tree) != STRING_CST)
1656 res->number_non_literal++;
1657 return;
1659 tree underlying_type
1660 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree)));
1661 if (underlying_type != char_type_node)
1663 if (underlying_type == char16_type_node
1664 || underlying_type == char32_type_node
1665 || underlying_type == wchar_type_node)
1666 res->number_wide++;
1667 else
1668 res->number_non_char++;
1669 return;
1671 format_chars = TREE_STRING_POINTER (format_tree);
1672 format_length = TREE_STRING_LENGTH (format_tree);
1673 if (array_size != 0)
1675 /* Variable length arrays can't be initialized. */
1676 gcc_assert (TREE_CODE (array_size) == INTEGER_CST);
1678 if (tree_fits_shwi_p (array_size))
1680 HOST_WIDE_INT array_size_value = tree_to_shwi (array_size);
1681 if (array_size_value > 0
1682 && array_size_value == (int) array_size_value
1683 && format_length > array_size_value)
1684 format_length = array_size_value;
1687 if (offset)
1689 if (offset >= format_length)
1691 res->number_non_literal++;
1692 return;
1694 format_chars += offset;
1695 format_length -= offset;
1697 if (format_length < 1 || format_chars[--format_length] != 0)
1699 res->number_unterminated++;
1700 return;
1702 if (format_length == 0)
1704 res->number_empty++;
1705 return;
1708 /* Skip to first argument to check. */
1709 while (arg_num + 1 < info->first_arg_num)
1711 if (params == 0)
1712 return;
1713 params = TREE_CHAIN (params);
1714 ++arg_num;
1716 /* Provisionally increment res->number_other; check_format_info_main
1717 will decrement it if it finds there are extra arguments, but this way
1718 need not adjust it for every return. */
1719 res->number_other++;
1720 object_allocator <format_wanted_type> fwt_pool ("format_wanted_type pool");
1721 check_format_info_main (res, info, format_chars, fmt_param_loc, format_tree,
1722 format_length, params, arg_num, fwt_pool, arglocs);
1725 /* Support class for argument_parser and check_format_info_main.
1726 Tracks any flag characters that have been applied to the
1727 current argument. */
1729 class flag_chars_t
1731 public:
1732 flag_chars_t ();
1733 bool has_char_p (char ch) const;
1734 void add_char (char ch);
1735 void validate (const format_kind_info *fki,
1736 const format_char_info *fci,
1737 const format_flag_spec *flag_specs,
1738 const char * const format_chars,
1739 tree format_string_cst,
1740 location_t format_string_loc,
1741 const char * const orig_format_chars,
1742 char format_char,
1743 bool quoted);
1744 int get_alloc_flag (const format_kind_info *fki);
1745 int assignment_suppression_p (const format_kind_info *fki);
1747 private:
1748 char m_flag_chars[256];
1751 /* Support struct for argument_parser and check_format_info_main.
1752 Encapsulates any length modifier applied to the current argument. */
1754 struct length_modifier
1756 length_modifier ()
1757 : chars (NULL), val (FMT_LEN_none), std (STD_C89),
1758 scalar_identity_flag (0)
1762 length_modifier (const char *chars_,
1763 enum format_lengths val_,
1764 enum format_std_version std_,
1765 int scalar_identity_flag_)
1766 : chars (chars_), val (val_), std (std_),
1767 scalar_identity_flag (scalar_identity_flag_)
1771 const char *chars;
1772 enum format_lengths val;
1773 enum format_std_version std;
1774 int scalar_identity_flag;
1777 /* Parsing one argument within a format string. */
1779 class argument_parser
1781 public:
1782 argument_parser (function_format_info *info, const char *&format_chars,
1783 tree format_string_cst,
1784 const char * const orig_format_chars,
1785 location_t format_string_loc, flag_chars_t &flag_chars,
1786 int &has_operand_number, tree first_fillin_param,
1787 object_allocator <format_wanted_type> &fwt_pool_,
1788 vec<location_t> *arglocs);
1790 bool read_any_dollar ();
1792 bool read_format_flags ();
1794 bool
1795 read_any_format_width (tree &params,
1796 unsigned HOST_WIDE_INT &arg_num);
1798 void
1799 read_any_format_left_precision ();
1801 bool
1802 read_any_format_precision (tree &params,
1803 unsigned HOST_WIDE_INT &arg_num);
1805 void handle_alloc_chars ();
1807 length_modifier read_any_length_modifier ();
1809 void read_any_other_modifier ();
1811 const format_char_info *find_format_char_info (char format_char);
1813 void
1814 validate_flag_pairs (const format_char_info *fci,
1815 char format_char);
1817 void
1818 give_y2k_warnings (const format_char_info *fci,
1819 char format_char);
1821 void parse_any_scan_set (const format_char_info *fci);
1823 bool handle_conversions (const format_char_info *fci,
1824 const length_modifier &len_modifier,
1825 tree &wanted_type,
1826 const char *&wanted_type_name,
1827 unsigned HOST_WIDE_INT &arg_num,
1828 tree &params,
1829 char format_char);
1831 bool
1832 check_argument_type (const format_char_info *fci,
1833 const length_modifier &len_modifier,
1834 tree &wanted_type,
1835 const char *&wanted_type_name,
1836 const bool suppressed,
1837 unsigned HOST_WIDE_INT &arg_num,
1838 tree &params,
1839 const int alloc_flag,
1840 const char * const format_start,
1841 const char * const type_start,
1842 location_t fmt_param_loc,
1843 char conversion_char);
1845 private:
1846 const function_format_info *const info;
1847 const format_kind_info * const fki;
1848 const format_flag_spec * const flag_specs;
1849 const char *start_of_this_format;
1850 const char *&format_chars;
1851 const tree format_string_cst;
1852 const char * const orig_format_chars;
1853 const location_t format_string_loc;
1854 object_allocator <format_wanted_type> &fwt_pool;
1855 flag_chars_t &flag_chars;
1856 int main_arg_num;
1857 tree main_arg_params;
1858 int &has_operand_number;
1859 const tree first_fillin_param;
1860 format_wanted_type width_wanted_type;
1861 format_wanted_type precision_wanted_type;
1862 public:
1863 format_wanted_type main_wanted_type;
1864 private:
1865 format_wanted_type *first_wanted_type;
1866 format_wanted_type *last_wanted_type;
1867 vec<location_t> *arglocs;
1870 /* flag_chars_t's constructor. */
1872 flag_chars_t::flag_chars_t ()
1874 m_flag_chars[0] = 0;
1877 /* Has CH been seen as a flag within the current argument? */
1879 bool
1880 flag_chars_t::has_char_p (char ch) const
1882 return strchr (m_flag_chars, ch) != 0;
1885 /* Add CH to the flags seen within the current argument. */
1887 void
1888 flag_chars_t::add_char (char ch)
1890 int i = strlen (m_flag_chars);
1891 m_flag_chars[i++] = ch;
1892 m_flag_chars[i] = 0;
1895 /* Validate the individual flags used, removing any that are invalid. */
1897 void
1898 flag_chars_t::validate (const format_kind_info *fki,
1899 const format_char_info *fci,
1900 const format_flag_spec *flag_specs,
1901 const char * const format_chars,
1902 tree format_string_cst,
1903 location_t format_string_loc,
1904 const char * const orig_format_chars,
1905 char format_char,
1906 bool quoted)
1908 int i;
1909 int d = 0;
1910 bool quotflag = false;
1912 for (i = 0; m_flag_chars[i] != 0; i++)
1914 const format_flag_spec *s = get_flag_spec (flag_specs,
1915 m_flag_chars[i], NULL);
1916 m_flag_chars[i - d] = m_flag_chars[i];
1917 if (m_flag_chars[i] == fki->length_code_char)
1918 continue;
1920 /* Remember if a quoting flag is seen. */
1921 quotflag |= s->quoting;
1923 if (strchr (fci->flag_chars, m_flag_chars[i]) == 0)
1925 format_warning_at_char (format_string_loc, format_string_cst,
1926 format_chars - orig_format_chars,
1927 OPT_Wformat_,
1928 "%s used with %<%%%c%> %s format",
1929 _(s->name), format_char, fki->name);
1930 d++;
1931 continue;
1933 if (pedantic)
1935 const format_flag_spec *t;
1936 if (ADJ_STD (s->std) > C_STD_VER)
1937 warning_at (format_string_loc, OPT_Wformat_,
1938 "%s does not support %s",
1939 C_STD_NAME (s->std), _(s->long_name));
1940 t = get_flag_spec (flag_specs, m_flag_chars[i], fci->flags2);
1941 if (t != NULL && ADJ_STD (t->std) > ADJ_STD (s->std))
1943 const char *long_name = (t->long_name != NULL
1944 ? t->long_name
1945 : s->long_name);
1946 if (ADJ_STD (t->std) > C_STD_VER)
1947 warning_at (format_string_loc, OPT_Wformat_,
1948 "%s does not support %s with"
1949 " the %<%%%c%> %s format",
1950 C_STD_NAME (t->std), _(long_name),
1951 format_char, fki->name);
1955 /* Detect quoting directives used within a quoted sequence, such
1956 as GCC's "%<...%qE". */
1957 if (quoted && s->quoting)
1959 format_warning_at_char (format_string_loc, format_string_cst,
1960 format_chars - orig_format_chars - 1,
1961 OPT_Wformat_,
1962 "%s used within a quoted sequence",
1963 _(s->name));
1966 m_flag_chars[i - d] = 0;
1968 if (!quoted
1969 && !quotflag
1970 && strchr (fci->flags2, '\''))
1972 format_warning_at_char (format_string_loc, format_string_cst,
1973 format_chars - orig_format_chars,
1974 OPT_Wformat_,
1975 "%qc conversion used unquoted",
1976 format_char);
1980 /* Determine if an assignment-allocation has been set, requiring
1981 an extra char ** for writing back a dynamically-allocated char *.
1982 This is for handling the optional 'm' character in scanf. */
1985 flag_chars_t::get_alloc_flag (const format_kind_info *fki)
1987 if ((fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
1988 && has_char_p ('a'))
1989 return 1;
1990 if (fki->alloc_char && has_char_p (fki->alloc_char))
1991 return 1;
1992 return 0;
1995 /* Determine if an assignment-suppression character was seen.
1996 ('*' in scanf, for discarding the converted input). */
1999 flag_chars_t::assignment_suppression_p (const format_kind_info *fki)
2001 if (fki->suppression_char
2002 && has_char_p (fki->suppression_char))
2003 return 1;
2004 return 0;
2007 /* Constructor for argument_parser. Initialize for parsing one
2008 argument within a format string. */
2010 argument_parser::
2011 argument_parser (function_format_info *info_, const char *&format_chars_,
2012 tree format_string_cst_,
2013 const char * const orig_format_chars_,
2014 location_t format_string_loc_,
2015 flag_chars_t &flag_chars_,
2016 int &has_operand_number_,
2017 tree first_fillin_param_,
2018 object_allocator <format_wanted_type> &fwt_pool_,
2019 vec<location_t> *arglocs_)
2020 : info (info_),
2021 fki (&format_types[info->format_type]),
2022 flag_specs (fki->flag_specs),
2023 start_of_this_format (format_chars_),
2024 format_chars (format_chars_),
2025 format_string_cst (format_string_cst_),
2026 orig_format_chars (orig_format_chars_),
2027 format_string_loc (format_string_loc_),
2028 fwt_pool (fwt_pool_),
2029 flag_chars (flag_chars_),
2030 main_arg_num (0),
2031 main_arg_params (NULL),
2032 has_operand_number (has_operand_number_),
2033 first_fillin_param (first_fillin_param_),
2034 first_wanted_type (NULL),
2035 last_wanted_type (NULL),
2036 arglocs (arglocs_)
2040 /* Handle dollars at the start of format arguments, setting up main_arg_params
2041 and main_arg_num.
2043 Return true if format parsing is to continue, false otherwise. */
2045 bool
2046 argument_parser::read_any_dollar ()
2048 if ((fki->flags & (int) FMT_FLAG_USE_DOLLAR) && has_operand_number != 0)
2050 /* Possibly read a $ operand number at the start of the format.
2051 If one was previously used, one is required here. If one
2052 is not used here, we can't immediately conclude this is a
2053 format without them, since it could be printf %m or scanf %*. */
2054 int opnum;
2055 opnum = maybe_read_dollar_number (&format_chars, 0,
2056 first_fillin_param,
2057 &main_arg_params, fki);
2058 if (opnum == -1)
2059 return false;
2060 else if (opnum > 0)
2062 has_operand_number = 1;
2063 main_arg_num = opnum + info->first_arg_num - 1;
2066 else if (fki->flags & FMT_FLAG_USE_DOLLAR)
2068 if (avoid_dollar_number (format_chars))
2069 return false;
2071 return true;
2074 /* Read any format flags, but do not yet validate them beyond removing
2075 duplicates, since in general validation depends on the rest of
2076 the format.
2078 Return true if format parsing is to continue, false otherwise. */
2080 bool
2081 argument_parser::read_format_flags ()
2083 while (*format_chars != 0
2084 && strchr (fki->flag_chars, *format_chars) != 0)
2086 const format_flag_spec *s = get_flag_spec (flag_specs,
2087 *format_chars, NULL);
2088 if (flag_chars.has_char_p (*format_chars))
2090 format_warning_at_char (format_string_loc, format_string_cst,
2091 format_chars + 1 - orig_format_chars,
2092 OPT_Wformat_,
2093 "repeated %s in format", _(s->name));
2095 else
2096 flag_chars.add_char (*format_chars);
2098 if (s->skip_next_char)
2100 ++format_chars;
2101 if (*format_chars == 0)
2103 warning_at (format_string_loc, OPT_Wformat_,
2104 "missing fill character at end of strfmon format");
2105 return false;
2108 ++format_chars;
2111 return true;
2114 /* Read any format width, possibly * or *m$.
2116 Return true if format parsing is to continue, false otherwise. */
2118 bool
2119 argument_parser::
2120 read_any_format_width (tree &params,
2121 unsigned HOST_WIDE_INT &arg_num)
2123 if (!fki->width_char)
2124 return true;
2126 if (fki->width_type != NULL && *format_chars == '*')
2128 flag_chars.add_char (fki->width_char);
2129 /* "...a field width...may be indicated by an asterisk.
2130 In this case, an int argument supplies the field width..." */
2131 ++format_chars;
2132 if (has_operand_number != 0)
2134 int opnum;
2135 opnum = maybe_read_dollar_number (&format_chars,
2136 has_operand_number == 1,
2137 first_fillin_param,
2138 &params, fki);
2139 if (opnum == -1)
2140 return false;
2141 else if (opnum > 0)
2143 has_operand_number = 1;
2144 arg_num = opnum + info->first_arg_num - 1;
2146 else
2147 has_operand_number = 0;
2149 else
2151 if (avoid_dollar_number (format_chars))
2152 return false;
2154 if (info->first_arg_num != 0)
2156 tree cur_param;
2157 if (params == 0)
2158 cur_param = NULL;
2159 else
2161 cur_param = TREE_VALUE (params);
2162 if (has_operand_number <= 0)
2164 params = TREE_CHAIN (params);
2165 ++arg_num;
2168 width_wanted_type.wanted_type = *fki->width_type;
2169 width_wanted_type.wanted_type_name = NULL;
2170 width_wanted_type.pointer_count = 0;
2171 width_wanted_type.char_lenient_flag = 0;
2172 width_wanted_type.scalar_identity_flag = 0;
2173 width_wanted_type.writing_in_flag = 0;
2174 width_wanted_type.reading_from_flag = 0;
2175 width_wanted_type.kind = CF_KIND_FIELD_WIDTH;
2176 width_wanted_type.format_start = format_chars - 1;
2177 width_wanted_type.format_length = 1;
2178 width_wanted_type.param = cur_param;
2179 width_wanted_type.arg_num = arg_num;
2180 width_wanted_type.offset_loc =
2181 format_chars - orig_format_chars;
2182 width_wanted_type.next = NULL;
2183 if (last_wanted_type != 0)
2184 last_wanted_type->next = &width_wanted_type;
2185 if (first_wanted_type == 0)
2186 first_wanted_type = &width_wanted_type;
2187 last_wanted_type = &width_wanted_type;
2190 else
2192 /* Possibly read a numeric width. If the width is zero,
2193 we complain if appropriate. */
2194 int non_zero_width_char = FALSE;
2195 int found_width = FALSE;
2196 while (ISDIGIT (*format_chars))
2198 found_width = TRUE;
2199 if (*format_chars != '0')
2200 non_zero_width_char = TRUE;
2201 ++format_chars;
2203 if (found_width && !non_zero_width_char &&
2204 (fki->flags & (int) FMT_FLAG_ZERO_WIDTH_BAD))
2205 warning_at (format_string_loc, OPT_Wformat_,
2206 "zero width in %s format", fki->name);
2207 if (found_width)
2208 flag_chars.add_char (fki->width_char);
2211 return true;
2214 /* Read any format left precision (must be a number, not *). */
2215 void
2216 argument_parser::read_any_format_left_precision ()
2218 if (fki->left_precision_char == 0)
2219 return;
2220 if (*format_chars != '#')
2221 return;
2223 ++format_chars;
2224 flag_chars.add_char (fki->left_precision_char);
2225 if (!ISDIGIT (*format_chars))
2226 format_warning_at_char (format_string_loc, format_string_cst,
2227 format_chars - orig_format_chars,
2228 OPT_Wformat_,
2229 "empty left precision in %s format", fki->name);
2230 while (ISDIGIT (*format_chars))
2231 ++format_chars;
2234 /* Read any format precision, possibly * or *m$.
2236 Return true if format parsing is to continue, false otherwise. */
2238 bool
2239 argument_parser::
2240 read_any_format_precision (tree &params,
2241 unsigned HOST_WIDE_INT &arg_num)
2243 if (fki->precision_char == 0)
2244 return true;
2245 if (*format_chars != '.')
2246 return true;
2248 ++format_chars;
2249 flag_chars.add_char (fki->precision_char);
2250 if (fki->precision_type != NULL && *format_chars == '*')
2252 /* "...a...precision...may be indicated by an asterisk.
2253 In this case, an int argument supplies the...precision." */
2254 ++format_chars;
2255 if (has_operand_number != 0)
2257 int opnum;
2258 opnum = maybe_read_dollar_number (&format_chars,
2259 has_operand_number == 1,
2260 first_fillin_param,
2261 &params, fki);
2262 if (opnum == -1)
2263 return false;
2264 else if (opnum > 0)
2266 has_operand_number = 1;
2267 arg_num = opnum + info->first_arg_num - 1;
2269 else
2270 has_operand_number = 0;
2272 else
2274 if (avoid_dollar_number (format_chars))
2275 return false;
2277 if (info->first_arg_num != 0)
2279 tree cur_param;
2280 if (params == 0)
2281 cur_param = NULL;
2282 else
2284 cur_param = TREE_VALUE (params);
2285 if (has_operand_number <= 0)
2287 params = TREE_CHAIN (params);
2288 ++arg_num;
2291 precision_wanted_type.wanted_type = *fki->precision_type;
2292 precision_wanted_type.wanted_type_name = NULL;
2293 precision_wanted_type.pointer_count = 0;
2294 precision_wanted_type.char_lenient_flag = 0;
2295 precision_wanted_type.scalar_identity_flag = 0;
2296 precision_wanted_type.writing_in_flag = 0;
2297 precision_wanted_type.reading_from_flag = 0;
2298 precision_wanted_type.kind = CF_KIND_FIELD_PRECISION;
2299 precision_wanted_type.param = cur_param;
2300 precision_wanted_type.format_start = format_chars - 2;
2301 precision_wanted_type.format_length = 2;
2302 precision_wanted_type.arg_num = arg_num;
2303 precision_wanted_type.offset_loc =
2304 format_chars - orig_format_chars;
2305 precision_wanted_type.next = NULL;
2306 if (last_wanted_type != 0)
2307 last_wanted_type->next = &precision_wanted_type;
2308 if (first_wanted_type == 0)
2309 first_wanted_type = &precision_wanted_type;
2310 last_wanted_type = &precision_wanted_type;
2313 else
2315 if (!(fki->flags & (int) FMT_FLAG_EMPTY_PREC_OK)
2316 && !ISDIGIT (*format_chars))
2317 format_warning_at_char (format_string_loc, format_string_cst,
2318 format_chars - orig_format_chars,
2319 OPT_Wformat_,
2320 "empty precision in %s format", fki->name);
2321 while (ISDIGIT (*format_chars))
2322 ++format_chars;
2325 return true;
2328 /* Parse any assignment-allocation flags, which request an extra
2329 char ** for writing back a dynamically-allocated char *.
2330 This is for handling the optional 'm' character in scanf,
2331 and, before C99, 'a' (for compatibility with a non-standard
2332 GNU libc extension). */
2334 void
2335 argument_parser::handle_alloc_chars ()
2337 if (fki->alloc_char && fki->alloc_char == *format_chars)
2339 flag_chars.add_char (fki->alloc_char);
2340 format_chars++;
2343 /* Handle the scanf allocation kludge. */
2344 if (fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
2346 if (*format_chars == 'a' && !flag_isoc99)
2348 if (format_chars[1] == 's' || format_chars[1] == 'S'
2349 || format_chars[1] == '[')
2351 /* 'a' is used as a flag. */
2352 flag_chars.add_char ('a');
2353 format_chars++;
2359 /* Look for length modifiers within the current format argument,
2360 returning a length_modifier instance describing it (or the
2361 default if one is not found).
2363 Issue warnings about non-standard modifiers. */
2365 length_modifier
2366 argument_parser::read_any_length_modifier ()
2368 length_modifier result;
2370 const format_length_info *fli = fki->length_char_specs;
2371 if (!fli)
2372 return result;
2374 while (fli->name != 0
2375 && strncmp (fli->name, format_chars, strlen (fli->name)))
2376 fli++;
2377 if (fli->name != 0)
2379 format_chars += strlen (fli->name);
2380 if (fli->double_name != 0 && fli->name[0] == *format_chars)
2382 format_chars++;
2383 result = length_modifier (fli->double_name, fli->double_index,
2384 fli->double_std, 0);
2386 else
2388 result = length_modifier (fli->name, fli->index, fli->std,
2389 fli->scalar_identity_flag);
2391 flag_chars.add_char (fki->length_code_char);
2393 if (pedantic)
2395 /* Warn if the length modifier is non-standard. */
2396 if (ADJ_STD (result.std) > C_STD_VER)
2397 warning_at (format_string_loc, OPT_Wformat_,
2398 "%s does not support the %qs %s length modifier",
2399 C_STD_NAME (result.std), result.chars,
2400 fki->name);
2403 return result;
2406 /* Read any other modifier (strftime E/O). */
2408 void
2409 argument_parser::read_any_other_modifier ()
2411 if (fki->modifier_chars == NULL)
2412 return;
2414 while (*format_chars != 0
2415 && strchr (fki->modifier_chars, *format_chars) != 0)
2417 if (flag_chars.has_char_p (*format_chars))
2419 const format_flag_spec *s = get_flag_spec (flag_specs,
2420 *format_chars, NULL);
2421 format_warning_at_char (format_string_loc, format_string_cst,
2422 format_chars - orig_format_chars,
2423 OPT_Wformat_,
2424 "repeated %s in format", _(s->name));
2426 else
2427 flag_chars.add_char (*format_chars);
2428 ++format_chars;
2432 /* Return the format_char_info corresponding to FORMAT_CHAR,
2433 potentially issuing a warning if the format char is
2434 not supported in the C standard version we are checking
2435 against.
2437 Issue a warning and return NULL if it is not found.
2439 Issue warnings about non-standard modifiers. */
2441 const format_char_info *
2442 argument_parser::find_format_char_info (char format_char)
2444 const format_char_info *fci = fki->conversion_specs;
2446 while (fci->format_chars != 0
2447 && strchr (fci->format_chars, format_char) == 0)
2448 ++fci;
2449 if (fci->format_chars == 0)
2451 format_warning_at_char (format_string_loc, format_string_cst,
2452 format_chars - orig_format_chars,
2453 OPT_Wformat_,
2454 "unknown conversion type character"
2455 " %qc in format",
2456 format_char);
2457 return NULL;
2460 if (pedantic)
2462 if (ADJ_STD (fci->std) > C_STD_VER)
2463 format_warning_at_char (format_string_loc, format_string_cst,
2464 format_chars - orig_format_chars,
2465 OPT_Wformat_,
2466 "%s does not support the %<%%%c%> %s format",
2467 C_STD_NAME (fci->std), format_char, fki->name);
2470 return fci;
2473 /* Validate the pairs of flags used.
2474 Issue warnings about incompatible combinations of flags. */
2476 void
2477 argument_parser::validate_flag_pairs (const format_char_info *fci,
2478 char format_char)
2480 const format_flag_pair * const bad_flag_pairs = fki->bad_flag_pairs;
2482 for (int i = 0; bad_flag_pairs[i].flag_char1 != 0; i++)
2484 const format_flag_spec *s, *t;
2485 if (!flag_chars.has_char_p (bad_flag_pairs[i].flag_char1))
2486 continue;
2487 if (!flag_chars.has_char_p (bad_flag_pairs[i].flag_char2))
2488 continue;
2489 if (bad_flag_pairs[i].predicate != 0
2490 && strchr (fci->flags2, bad_flag_pairs[i].predicate) == 0)
2491 continue;
2492 s = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char1, NULL);
2493 t = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char2, NULL);
2494 if (bad_flag_pairs[i].ignored)
2496 if (bad_flag_pairs[i].predicate != 0)
2497 warning_at (format_string_loc, OPT_Wformat_,
2498 "%s ignored with %s and %<%%%c%> %s format",
2499 _(s->name), _(t->name), format_char,
2500 fki->name);
2501 else
2502 warning_at (format_string_loc, OPT_Wformat_,
2503 "%s ignored with %s in %s format",
2504 _(s->name), _(t->name), fki->name);
2506 else
2508 if (bad_flag_pairs[i].predicate != 0)
2509 warning_at (format_string_loc, OPT_Wformat_,
2510 "use of %s and %s together with %<%%%c%> %s format",
2511 _(s->name), _(t->name), format_char,
2512 fki->name);
2513 else
2514 warning_at (format_string_loc, OPT_Wformat_,
2515 "use of %s and %s together in %s format",
2516 _(s->name), _(t->name), fki->name);
2521 /* Give Y2K warnings. */
2523 void
2524 argument_parser::give_y2k_warnings (const format_char_info *fci,
2525 char format_char)
2527 if (!warn_format_y2k)
2528 return;
2530 int y2k_level = 0;
2531 if (strchr (fci->flags2, '4') != 0)
2532 if (flag_chars.has_char_p ('E'))
2533 y2k_level = 3;
2534 else
2535 y2k_level = 2;
2536 else if (strchr (fci->flags2, '3') != 0)
2537 y2k_level = 3;
2538 else if (strchr (fci->flags2, '2') != 0)
2539 y2k_level = 2;
2540 if (y2k_level == 3)
2541 warning_at (format_string_loc, OPT_Wformat_y2k,
2542 "%<%%%c%> yields only last 2 digits of "
2543 "year in some locales", format_char);
2544 else if (y2k_level == 2)
2545 warning_at (format_string_loc, OPT_Wformat_y2k,
2546 "%<%%%c%> yields only last 2 digits of year",
2547 format_char);
2550 /* Parse any "scan sets" enclosed in square brackets, e.g.
2551 for scanf-style calls. */
2553 void
2554 argument_parser::parse_any_scan_set (const format_char_info *fci)
2556 if (strchr (fci->flags2, '[') == NULL)
2557 return;
2559 /* Skip over scan set, in case it happens to have '%' in it. */
2560 if (*format_chars == '^')
2561 ++format_chars;
2562 /* Find closing bracket; if one is hit immediately, then
2563 it's part of the scan set rather than a terminator. */
2564 if (*format_chars == ']')
2565 ++format_chars;
2566 while (*format_chars && *format_chars != ']')
2567 ++format_chars;
2568 if (*format_chars != ']')
2569 /* The end of the format string was reached. */
2570 format_warning_at_char (format_string_loc, format_string_cst,
2571 format_chars - orig_format_chars,
2572 OPT_Wformat_,
2573 "no closing %<]%> for %<%%[%> format");
2576 /* Return true if this argument is to be continued to be parsed,
2577 false to skip to next argument. */
2579 bool
2580 argument_parser::handle_conversions (const format_char_info *fci,
2581 const length_modifier &len_modifier,
2582 tree &wanted_type,
2583 const char *&wanted_type_name,
2584 unsigned HOST_WIDE_INT &arg_num,
2585 tree &params,
2586 char format_char)
2588 enum format_std_version wanted_type_std;
2590 if (!(fki->flags & (int) FMT_FLAG_ARG_CONVERT))
2591 return true;
2593 wanted_type = (fci->types[len_modifier.val].type
2594 ? *fci->types[len_modifier.val].type : 0);
2595 wanted_type_name = fci->types[len_modifier.val].name;
2596 wanted_type_std = fci->types[len_modifier.val].std;
2597 if (wanted_type == 0)
2599 format_warning_at_char (format_string_loc, format_string_cst,
2600 format_chars - orig_format_chars,
2601 OPT_Wformat_,
2602 "use of %qs length modifier with %qc type"
2603 " character has either no effect"
2604 " or undefined behavior",
2605 len_modifier.chars, format_char);
2606 /* Heuristic: skip one argument when an invalid length/type
2607 combination is encountered. */
2608 arg_num++;
2609 if (params != 0)
2610 params = TREE_CHAIN (params);
2611 return false;
2613 else if (pedantic
2614 /* Warn if non-standard, provided it is more non-standard
2615 than the length and type characters that may already
2616 have been warned for. */
2617 && ADJ_STD (wanted_type_std) > ADJ_STD (len_modifier.std)
2618 && ADJ_STD (wanted_type_std) > ADJ_STD (fci->std))
2620 if (ADJ_STD (wanted_type_std) > C_STD_VER)
2621 format_warning_at_char (format_string_loc, format_string_cst,
2622 format_chars - orig_format_chars,
2623 OPT_Wformat_,
2624 "%s does not support the %<%%%s%c%> %s format",
2625 C_STD_NAME (wanted_type_std),
2626 len_modifier.chars,
2627 format_char, fki->name);
2630 return true;
2633 /* Check type of argument against desired type.
2635 Return true if format parsing is to continue, false otherwise. */
2637 bool
2638 argument_parser::
2639 check_argument_type (const format_char_info *fci,
2640 const length_modifier &len_modifier,
2641 tree &wanted_type,
2642 const char *&wanted_type_name,
2643 const bool suppressed,
2644 unsigned HOST_WIDE_INT &arg_num,
2645 tree &params,
2646 const int alloc_flag,
2647 const char * const format_start,
2648 const char * const type_start,
2649 location_t fmt_param_loc,
2650 char conversion_char)
2652 if (info->first_arg_num == 0)
2653 return true;
2655 if ((fci->pointer_count == 0 && wanted_type == void_type_node)
2656 || suppressed)
2658 if (main_arg_num != 0)
2660 if (suppressed)
2661 warning_at (format_string_loc, OPT_Wformat_,
2662 "operand number specified with "
2663 "suppressed assignment");
2664 else
2665 warning_at (format_string_loc, OPT_Wformat_,
2666 "operand number specified for format "
2667 "taking no argument");
2670 else
2672 format_wanted_type *wanted_type_ptr;
2674 if (main_arg_num != 0)
2676 arg_num = main_arg_num;
2677 params = main_arg_params;
2679 else
2681 ++arg_num;
2682 if (has_operand_number > 0)
2684 warning_at (format_string_loc, OPT_Wformat_,
2685 "missing $ operand number in format");
2686 return false;
2688 else
2689 has_operand_number = 0;
2692 wanted_type_ptr = &main_wanted_type;
2693 while (fci)
2695 tree cur_param;
2696 if (params == 0)
2697 cur_param = NULL;
2698 else
2700 cur_param = TREE_VALUE (params);
2701 params = TREE_CHAIN (params);
2704 wanted_type_ptr->wanted_type = wanted_type;
2705 wanted_type_ptr->wanted_type_name = wanted_type_name;
2706 wanted_type_ptr->pointer_count = fci->pointer_count + alloc_flag;
2707 wanted_type_ptr->char_lenient_flag = 0;
2708 if (strchr (fci->flags2, 'c') != 0)
2709 wanted_type_ptr->char_lenient_flag = 1;
2710 wanted_type_ptr->scalar_identity_flag = 0;
2711 if (len_modifier.scalar_identity_flag)
2712 wanted_type_ptr->scalar_identity_flag = 1;
2713 wanted_type_ptr->writing_in_flag = 0;
2714 wanted_type_ptr->reading_from_flag = 0;
2715 if (alloc_flag)
2716 wanted_type_ptr->writing_in_flag = 1;
2717 else
2719 if (strchr (fci->flags2, 'W') != 0)
2720 wanted_type_ptr->writing_in_flag = 1;
2721 if (strchr (fci->flags2, 'R') != 0)
2722 wanted_type_ptr->reading_from_flag = 1;
2724 wanted_type_ptr->kind = CF_KIND_FORMAT;
2725 wanted_type_ptr->param = cur_param;
2726 wanted_type_ptr->arg_num = arg_num;
2727 wanted_type_ptr->format_start = format_start;
2728 wanted_type_ptr->format_length = format_chars - format_start;
2729 wanted_type_ptr->offset_loc = format_chars - orig_format_chars;
2730 wanted_type_ptr->next = NULL;
2731 if (last_wanted_type != 0)
2732 last_wanted_type->next = wanted_type_ptr;
2733 if (first_wanted_type == 0)
2734 first_wanted_type = wanted_type_ptr;
2735 last_wanted_type = wanted_type_ptr;
2737 fci = fci->chain;
2738 if (fci)
2740 wanted_type_ptr = fwt_pool.allocate ();
2741 arg_num++;
2742 wanted_type = *fci->types[len_modifier.val].type;
2743 wanted_type_name = fci->types[len_modifier.val].name;
2748 if (first_wanted_type != 0)
2750 ptrdiff_t offset_to_format_start = (start_of_this_format - 1) - orig_format_chars;
2751 ptrdiff_t offset_to_format_end = (format_chars - 1) - orig_format_chars;
2752 /* By default, use the end of the range for the caret location. */
2753 substring_loc fmt_loc (fmt_param_loc, TREE_TYPE (format_string_cst),
2754 offset_to_format_end,
2755 offset_to_format_start, offset_to_format_end);
2756 ptrdiff_t offset_to_type_start = type_start - orig_format_chars;
2757 check_format_types (fmt_loc, first_wanted_type, fki,
2758 offset_to_type_start,
2759 conversion_char, arglocs);
2762 return true;
2765 /* Do the main part of checking a call to a format function. FORMAT_CHARS
2766 is the NUL-terminated format string (which at this point may contain
2767 internal NUL characters); FORMAT_LENGTH is its length (excluding the
2768 terminating NUL character). ARG_NUM is one less than the number of
2769 the first format argument to check; PARAMS points to that format
2770 argument in the list of arguments. */
2772 static void
2773 check_format_info_main (format_check_results *res,
2774 function_format_info *info, const char *format_chars,
2775 location_t fmt_param_loc, tree format_string_cst,
2776 int format_length, tree params,
2777 unsigned HOST_WIDE_INT arg_num,
2778 object_allocator <format_wanted_type> &fwt_pool,
2779 vec<location_t> *arglocs)
2781 const char * const orig_format_chars = format_chars;
2782 const tree first_fillin_param = params;
2784 const format_kind_info * const fki = &format_types[info->format_type];
2785 const format_flag_spec * const flag_specs = fki->flag_specs;
2786 const location_t format_string_loc = res->format_string_loc;
2788 /* -1 if no conversions taking an operand have been found; 0 if one has
2789 and it didn't use $; 1 if $ formats are in use. */
2790 int has_operand_number = -1;
2792 /* Vector of pointers to opening quoting directives (like GCC "%<"). */
2793 auto_vec<const char*> quotdirs;
2795 /* Pointers to the most recent color directives (like GCC's "%r or %R").
2796 A starting color directive much be terminated before the end of
2797 the format string. A terminating directive makes no sense without
2798 a prior starting directive. */
2799 const char *color_begin = NULL;
2800 const char *color_end = NULL;
2802 init_dollar_format_checking (info->first_arg_num, first_fillin_param);
2804 while (*format_chars != 0)
2806 if (*format_chars++ != '%')
2807 continue;
2808 if (*format_chars == 0)
2810 format_warning_at_char (format_string_loc, format_string_cst,
2811 format_chars - orig_format_chars,
2812 OPT_Wformat_,
2813 "spurious trailing %<%%%> in format");
2814 continue;
2816 if (*format_chars == '%')
2818 ++format_chars;
2819 continue;
2822 flag_chars_t flag_chars;
2823 argument_parser arg_parser (info, format_chars, format_string_cst,
2824 orig_format_chars, format_string_loc,
2825 flag_chars, has_operand_number,
2826 first_fillin_param, fwt_pool, arglocs);
2828 if (!arg_parser.read_any_dollar ())
2829 return;
2831 if (!arg_parser.read_format_flags ())
2832 return;
2834 /* Read any format width, possibly * or *m$. */
2835 if (!arg_parser.read_any_format_width (params, arg_num))
2836 return;
2838 /* Read any format left precision (must be a number, not *). */
2839 arg_parser.read_any_format_left_precision ();
2841 /* Read any format precision, possibly * or *m$. */
2842 if (!arg_parser.read_any_format_precision (params, arg_num))
2843 return;
2845 const char *format_start = format_chars;
2847 arg_parser.handle_alloc_chars ();
2849 /* The rest of the conversion specification is the length modifier
2850 (if any), and the conversion specifier, so this is where the
2851 type information starts. If we need to issue a suggestion
2852 about a type mismatch, then we should preserve everything up
2853 to here. */
2854 const char *type_start = format_chars;
2856 /* Read any length modifier, if this kind of format has them. */
2857 const length_modifier len_modifier
2858 = arg_parser.read_any_length_modifier ();
2860 /* Read any modifier (strftime E/O). */
2861 arg_parser.read_any_other_modifier ();
2863 char format_char = *format_chars;
2864 if (format_char == 0
2865 || (!(fki->flags & (int) FMT_FLAG_FANCY_PERCENT_OK)
2866 && format_char == '%'))
2868 format_warning_at_char (format_string_loc, format_string_cst,
2869 format_chars - orig_format_chars,
2870 OPT_Wformat_,
2871 "conversion lacks type at end of format");
2872 continue;
2874 format_chars++;
2876 const format_char_info * const fci
2877 = arg_parser.find_format_char_info (format_char);
2878 if (!fci)
2879 continue;
2881 flag_chars.validate (fki, fci, flag_specs, format_chars,
2882 format_string_cst,
2883 format_string_loc, orig_format_chars, format_char,
2884 quotdirs.length () > 0);
2886 const int alloc_flag = flag_chars.get_alloc_flag (fki);
2887 const bool suppressed = flag_chars.assignment_suppression_p (fki);
2889 /* Diagnose nested or unmatched quoting directives such as GCC's
2890 "%<...%<" and "%>...%>". */
2891 bool quot_begin_p = strchr (fci->flags2, '<');
2892 bool quot_end_p = strchr (fci->flags2, '>');
2894 if (quot_begin_p && !quot_end_p)
2896 if (quotdirs.length ())
2897 format_warning_at_char (format_string_loc, format_string_cst,
2898 format_chars - orig_format_chars,
2899 OPT_Wformat_,
2900 "nested quoting directive");
2901 quotdirs.safe_push (format_chars);
2903 else if (!quot_begin_p && quot_end_p)
2905 if (quotdirs.length ())
2906 quotdirs.pop ();
2907 else
2908 format_warning_at_char (format_string_loc, format_string_cst,
2909 format_chars - orig_format_chars,
2910 OPT_Wformat_,
2911 "unmatched quoting directive");
2914 bool color_begin_p = strchr (fci->flags2, '/');
2915 if (color_begin_p)
2917 color_begin = format_chars;
2918 color_end = NULL;
2920 else if (strchr (fci->flags2, '\\'))
2922 if (color_end)
2923 format_warning_at_char (format_string_loc, format_string_cst,
2924 format_chars - orig_format_chars,
2925 OPT_Wformat_,
2926 "%qc directive redundant after prior "
2927 "occurence of the same", format_char);
2928 else if (!color_begin)
2929 format_warning_at_char (format_string_loc, format_string_cst,
2930 format_chars - orig_format_chars,
2931 OPT_Wformat_,
2932 "unmatched color reset directive");
2933 color_end = format_chars;
2936 /* Diagnose directives that shouldn't appear in a quoted sequence.
2937 (They are denoted by a double quote in FLAGS2.) */
2938 if (quotdirs.length ())
2940 if (strchr (fci->flags2, '"'))
2941 format_warning_at_char (format_string_loc, format_string_cst,
2942 format_chars - orig_format_chars,
2943 OPT_Wformat_,
2944 "%qc conversion used within a quoted "
2945 "sequence",
2946 format_char);
2949 /* Validate the pairs of flags used. */
2950 arg_parser.validate_flag_pairs (fci, format_char);
2952 arg_parser.give_y2k_warnings (fci, format_char);
2954 arg_parser.parse_any_scan_set (fci);
2956 tree wanted_type = NULL;
2957 const char *wanted_type_name = NULL;
2959 if (!arg_parser.handle_conversions (fci, len_modifier,
2960 wanted_type, wanted_type_name,
2961 arg_num,
2962 params,
2963 format_char))
2964 continue;
2966 arg_parser.main_wanted_type.next = NULL;
2968 /* Finally. . .check type of argument against desired type! */
2969 if (!arg_parser.check_argument_type (fci, len_modifier,
2970 wanted_type, wanted_type_name,
2971 suppressed,
2972 arg_num, params,
2973 alloc_flag,
2974 format_start, type_start,
2975 fmt_param_loc,
2976 format_char))
2977 return;
2980 if (format_chars - orig_format_chars != format_length)
2981 format_warning_at_char (format_string_loc, format_string_cst,
2982 format_chars + 1 - orig_format_chars,
2983 OPT_Wformat_contains_nul,
2984 "embedded %<\\0%> in format");
2985 if (info->first_arg_num != 0 && params != 0
2986 && has_operand_number <= 0)
2988 res->number_other--;
2989 res->number_extra_args++;
2991 if (has_operand_number > 0)
2992 finish_dollar_format_checking (res, fki->flags & (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK);
2994 if (quotdirs.length ())
2995 format_warning_at_char (format_string_loc, format_string_cst,
2996 quotdirs.pop () - orig_format_chars,
2997 OPT_Wformat_, "unterminated quoting directive");
2998 if (color_begin && !color_end)
2999 format_warning_at_char (format_string_loc, format_string_cst,
3000 color_begin - orig_format_chars,
3001 OPT_Wformat_, "unterminated color directive");
3004 /* Check the argument types from a single format conversion (possibly
3005 including width and precision arguments).
3007 FMT_LOC is the location of the format conversion.
3009 TYPES is a singly-linked list expressing the parts of the format
3010 conversion that expect argument types, and the arguments they
3011 correspond to.
3013 OFFSET_TO_TYPE_START is the offset within the execution-charset encoded
3014 format string to where type information begins for the conversion
3015 (the length modifier and conversion specifier).
3017 CONVERSION_CHAR is the user-provided conversion specifier.
3019 For example, given:
3021 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3023 then FMT_LOC covers this range:
3025 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3026 ^^^^^^^^^
3028 and TYPES in this case is a three-entry singly-linked list consisting of:
3029 (1) the check for the field width here:
3030 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3031 ^ ^^^^
3032 against arg3, and
3033 (2) the check for the field precision here:
3034 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3035 ^^ ^^^^
3036 against arg4, and
3037 (3) the check for the length modifier and conversion char here:
3038 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3039 ^^^ ^^^^
3040 against arg5.
3042 OFFSET_TO_TYPE_START is 13, the offset to the "lld" within the
3043 STRING_CST:
3045 0000000000111111111122
3046 0123456789012345678901
3047 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3049 | ` CONVERSION_CHAR: 'd'
3050 type starts here. */
3052 static void
3053 check_format_types (const substring_loc &fmt_loc,
3054 format_wanted_type *types, const format_kind_info *fki,
3055 int offset_to_type_start,
3056 char conversion_char,
3057 vec<location_t> *arglocs)
3059 for (; types != 0; types = types->next)
3061 tree cur_param;
3062 tree cur_type;
3063 tree orig_cur_type;
3064 tree wanted_type;
3065 int arg_num;
3066 int i;
3067 int char_type_flag;
3069 wanted_type = types->wanted_type;
3070 arg_num = types->arg_num;
3072 /* The following should not occur here. */
3073 gcc_assert (wanted_type);
3074 gcc_assert (wanted_type != void_type_node || types->pointer_count);
3076 if (types->pointer_count == 0)
3077 wanted_type = lang_hooks.types.type_promotes_to (wanted_type);
3079 wanted_type = TYPE_MAIN_VARIANT (wanted_type);
3081 cur_param = types->param;
3082 if (!cur_param)
3084 format_type_warning (fmt_loc, UNKNOWN_LOCATION, types, wanted_type,
3085 NULL, fki, offset_to_type_start,
3086 conversion_char);
3087 continue;
3090 cur_type = TREE_TYPE (cur_param);
3091 if (cur_type == error_mark_node)
3092 continue;
3093 orig_cur_type = cur_type;
3094 char_type_flag = 0;
3096 location_t param_loc = UNKNOWN_LOCATION;
3097 if (EXPR_HAS_LOCATION (cur_param))
3098 param_loc = EXPR_LOCATION (cur_param);
3099 else if (arglocs)
3101 /* arg_num is 1-based. */
3102 gcc_assert (types->arg_num > 0);
3103 param_loc = (*arglocs)[types->arg_num - 1];
3106 STRIP_NOPS (cur_param);
3108 /* Check the types of any additional pointer arguments
3109 that precede the "real" argument. */
3110 for (i = 0; i < types->pointer_count; ++i)
3112 if (TREE_CODE (cur_type) == POINTER_TYPE)
3114 cur_type = TREE_TYPE (cur_type);
3115 if (cur_type == error_mark_node)
3116 break;
3118 /* Check for writing through a NULL pointer. */
3119 if (types->writing_in_flag
3120 && i == 0
3121 && cur_param != 0
3122 && integer_zerop (cur_param))
3123 warning (OPT_Wformat_, "writing through null pointer "
3124 "(argument %d)", arg_num);
3126 /* Check for reading through a NULL pointer. */
3127 if (types->reading_from_flag
3128 && i == 0
3129 && cur_param != 0
3130 && integer_zerop (cur_param))
3131 warning (OPT_Wformat_, "reading through null pointer "
3132 "(argument %d)", arg_num);
3134 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
3135 cur_param = TREE_OPERAND (cur_param, 0);
3136 else
3137 cur_param = 0;
3139 /* See if this is an attempt to write into a const type with
3140 scanf or with printf "%n". Note: the writing in happens
3141 at the first indirection only, if for example
3142 void * const * is passed to scanf %p; passing
3143 const void ** is simply passing an incompatible type. */
3144 if (types->writing_in_flag
3145 && i == 0
3146 && (TYPE_READONLY (cur_type)
3147 || (cur_param != 0
3148 && (CONSTANT_CLASS_P (cur_param)
3149 || (DECL_P (cur_param)
3150 && TREE_READONLY (cur_param))))))
3151 warning (OPT_Wformat_, "writing into constant object "
3152 "(argument %d)", arg_num);
3154 /* If there are extra type qualifiers beyond the first
3155 indirection, then this makes the types technically
3156 incompatible. */
3157 if (i > 0
3158 && pedantic
3159 && (TYPE_READONLY (cur_type)
3160 || TYPE_VOLATILE (cur_type)
3161 || TYPE_ATOMIC (cur_type)
3162 || TYPE_RESTRICT (cur_type)))
3163 warning (OPT_Wformat_, "extra type qualifiers in format "
3164 "argument (argument %d)",
3165 arg_num);
3168 else
3170 format_type_warning (fmt_loc, param_loc,
3171 types, wanted_type, orig_cur_type, fki,
3172 offset_to_type_start, conversion_char);
3173 break;
3177 if (i < types->pointer_count)
3178 continue;
3180 cur_type = TYPE_MAIN_VARIANT (cur_type);
3182 /* Check whether the argument type is a character type. This leniency
3183 only applies to certain formats, flagged with 'c'. */
3184 if (types->char_lenient_flag)
3185 char_type_flag = (cur_type == char_type_node
3186 || cur_type == signed_char_type_node
3187 || cur_type == unsigned_char_type_node);
3189 /* Check the type of the "real" argument, if there's a type we want. */
3190 if (lang_hooks.types_compatible_p (wanted_type, cur_type))
3191 continue;
3192 /* If we want 'void *', allow any pointer type.
3193 (Anything else would already have got a warning.)
3194 With -Wpedantic, only allow pointers to void and to character
3195 types. */
3196 if (wanted_type == void_type_node
3197 && (!pedantic || (i == 1 && char_type_flag)))
3198 continue;
3199 /* Don't warn about differences merely in signedness, unless
3200 -Wpedantic. With -Wpedantic, warn if the type is a pointer
3201 target and not a character type, and for character types at
3202 a second level of indirection. */
3203 if (TREE_CODE (wanted_type) == INTEGER_TYPE
3204 && TREE_CODE (cur_type) == INTEGER_TYPE
3205 && ((!pedantic && !warn_format_signedness)
3206 || (i == 0 && !warn_format_signedness)
3207 || (i == 1 && char_type_flag))
3208 && (TYPE_UNSIGNED (wanted_type)
3209 ? wanted_type == c_common_unsigned_type (cur_type)
3210 : wanted_type == c_common_signed_type (cur_type)))
3211 continue;
3212 /* Don't warn about differences merely in signedness if we know
3213 that the current type is integer-promoted and its original type
3214 was unsigned such as that it is in the range of WANTED_TYPE. */
3215 if (TREE_CODE (wanted_type) == INTEGER_TYPE
3216 && TREE_CODE (cur_type) == INTEGER_TYPE
3217 && warn_format_signedness
3218 && TYPE_UNSIGNED (wanted_type)
3219 && cur_param != NULL_TREE
3220 && TREE_CODE (cur_param) == NOP_EXPR)
3222 tree t = TREE_TYPE (TREE_OPERAND (cur_param, 0));
3223 if (TYPE_UNSIGNED (t)
3224 && cur_type == lang_hooks.types.type_promotes_to (t))
3225 continue;
3227 /* Likewise, "signed char", "unsigned char" and "char" are
3228 equivalent but the above test won't consider them equivalent. */
3229 if (wanted_type == char_type_node
3230 && (!pedantic || i < 2)
3231 && char_type_flag)
3232 continue;
3233 if (types->scalar_identity_flag
3234 && (TREE_CODE (cur_type) == TREE_CODE (wanted_type)
3235 || (INTEGRAL_TYPE_P (cur_type)
3236 && INTEGRAL_TYPE_P (wanted_type)))
3237 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type))
3238 continue;
3239 /* Now we have a type mismatch. */
3240 format_type_warning (fmt_loc, param_loc, types,
3241 wanted_type, orig_cur_type, fki,
3242 offset_to_type_start, conversion_char);
3246 /* Given type TYPE, attempt to dereference the type N times
3247 (e.g. from ("int ***", 2) to "int *")
3249 Return the derefenced type, with any qualifiers
3250 such as "const" stripped from the result, or
3251 NULL if unsuccessful (e.g. TYPE is not a pointer type). */
3253 static tree
3254 deref_n_times (tree type, int n)
3256 gcc_assert (type);
3258 for (int i = n; i > 0; i--)
3260 if (TREE_CODE (type) != POINTER_TYPE)
3261 return NULL_TREE;
3262 type = TREE_TYPE (type);
3264 /* Strip off any "const" etc. */
3265 return build_qualified_type (type, 0);
3268 /* Lookup the format code for FORMAT_LEN within FLI,
3269 returning the string code for expressing it, or NULL
3270 if it is not found. */
3272 static const char *
3273 get_modifier_for_format_len (const format_length_info *fli,
3274 enum format_lengths format_len)
3276 for (; fli->name; fli++)
3278 if (fli->index == format_len)
3279 return fli->name;
3280 if (fli->double_index == format_len)
3281 return fli->double_name;
3283 return NULL;
3286 #if CHECKING_P
3288 namespace selftest {
3290 static void
3291 test_get_modifier_for_format_len ()
3293 ASSERT_STREQ ("h",
3294 get_modifier_for_format_len (printf_length_specs, FMT_LEN_h));
3295 ASSERT_STREQ ("hh",
3296 get_modifier_for_format_len (printf_length_specs, FMT_LEN_hh));
3297 ASSERT_STREQ ("L",
3298 get_modifier_for_format_len (printf_length_specs, FMT_LEN_L));
3299 ASSERT_EQ (NULL,
3300 get_modifier_for_format_len (printf_length_specs, FMT_LEN_none));
3303 } // namespace selftest
3305 #endif /* CHECKING_P */
3307 /* Determine if SPEC_TYPE and ARG_TYPE are sufficiently similar for a
3308 format_type_detail using SPEC_TYPE to be offered as a suggestion for
3309 Wformat type errors where the argument has type ARG_TYPE. */
3311 static bool
3312 matching_type_p (tree spec_type, tree arg_type)
3314 gcc_assert (spec_type);
3315 gcc_assert (arg_type);
3317 /* If any of the types requires structural equality, we can't compare
3318 their canonical types. */
3319 if (TYPE_STRUCTURAL_EQUALITY_P (spec_type)
3320 || TYPE_STRUCTURAL_EQUALITY_P (arg_type))
3321 return false;
3323 spec_type = TYPE_CANONICAL (spec_type);
3324 arg_type = TYPE_CANONICAL (arg_type);
3326 if (TREE_CODE (spec_type) == INTEGER_TYPE
3327 && TREE_CODE (arg_type) == INTEGER_TYPE
3328 && (TYPE_UNSIGNED (spec_type)
3329 ? spec_type == c_common_unsigned_type (arg_type)
3330 : spec_type == c_common_signed_type (arg_type)))
3331 return true;
3333 return spec_type == arg_type;
3336 /* Subroutine of get_format_for_type.
3338 Generate a string containing the length modifier and conversion specifier
3339 that should be used to format arguments of type ARG_TYPE within FKI
3340 (effectively the inverse of the checking code).
3342 If CONVERSION_CHAR is not zero (the first pass), the resulting suggestion
3343 is required to use it, for correcting bogus length modifiers.
3344 If CONVERSION_CHAR is zero (the second pass), then allow any suggestion
3345 that matches ARG_TYPE.
3347 If successful, returns a non-NULL string which should be freed
3348 by the caller.
3349 Otherwise, returns NULL. */
3351 static char *
3352 get_format_for_type_1 (const format_kind_info *fki, tree arg_type,
3353 char conversion_char)
3355 gcc_assert (arg_type);
3357 const format_char_info *spec;
3358 for (spec = &fki->conversion_specs[0];
3359 spec->format_chars;
3360 spec++)
3362 if (conversion_char)
3363 if (!strchr (spec->format_chars, conversion_char))
3364 continue;
3366 tree effective_arg_type = deref_n_times (arg_type,
3367 spec->pointer_count);
3368 if (!effective_arg_type)
3369 continue;
3370 for (int i = 0; i < FMT_LEN_MAX; i++)
3372 const format_type_detail *ftd = &spec->types[i];
3373 if (!ftd->type)
3374 continue;
3375 if (matching_type_p (*ftd->type, effective_arg_type))
3377 const char *len_modifier
3378 = get_modifier_for_format_len (fki->length_char_specs,
3379 (enum format_lengths)i);
3380 if (!len_modifier)
3381 len_modifier = "";
3383 if (conversion_char)
3384 /* We found a match, using the given conversion char - the
3385 length modifier was incorrect (or absent).
3386 Provide a suggestion using the conversion char with the
3387 correct length modifier for the type. */
3388 return xasprintf ("%s%c", len_modifier, conversion_char);
3389 else
3390 /* 2nd pass: no match was possible using the user-provided
3391 conversion char, but we do have a match without using it.
3392 Provide a suggestion using the first conversion char
3393 listed for the given type. */
3394 return xasprintf ("%s%c", len_modifier, spec->format_chars[0]);
3399 return NULL;
3402 /* Generate a string containing the length modifier and conversion specifier
3403 that should be used to format arguments of type ARG_TYPE within FKI
3404 (effectively the inverse of the checking code).
3406 If successful, returns a non-NULL string which should be freed
3407 by the caller.
3408 Otherwise, returns NULL. */
3410 static char *
3411 get_format_for_type (const format_kind_info *fki, tree arg_type,
3412 char conversion_char)
3414 gcc_assert (arg_type);
3415 gcc_assert (conversion_char);
3417 /* First pass: look for a format_char_info containing CONVERSION_CHAR
3418 If we find one, then presumably the length modifier was incorrect
3419 (or absent). */
3420 char *result = get_format_for_type_1 (fki, arg_type, conversion_char);
3421 if (result)
3422 return result;
3424 /* Second pass: we didn't find a match for CONVERSION_CHAR, so try
3425 matching just on the type. */
3426 return get_format_for_type_1 (fki, arg_type, '\0');
3429 /* Attempt to get a string for use as a replacement fix-it hint for the
3430 source range in FMT_LOC.
3432 Preserve all of the text within the range of FMT_LOC up to
3433 OFFSET_TO_TYPE_START, replacing the rest with an appropriate
3434 length modifier and conversion specifier for ARG_TYPE, attempting
3435 to keep the user-provided CONVERSION_CHAR if possible.
3437 For example, given a long vs long long mismatch for arg5 here:
3439 000000000111111111122222222223333333333|
3440 123456789012345678901234567890123456789` column numbers
3441 0000000000111111111122|
3442 0123456789012345678901` string offsets
3443 V~~~~~~~~ : range of FMT_LOC, from cols 23-31
3444 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3446 | ` CONVERSION_CHAR: 'd'
3447 type starts here
3449 where OFFSET_TO_TYPE_START is 13 (the offset to the "lld" within the
3450 STRING_CST), where the user provided:
3451 %-+*.*lld
3452 the result (assuming "long" argument 5) should be:
3453 %-+*.*ld
3455 If successful, returns a non-NULL string which should be freed
3456 by the caller.
3457 Otherwise, returns NULL. */
3459 static char *
3460 get_corrected_substring (const substring_loc &fmt_loc,
3461 format_wanted_type *type, tree arg_type,
3462 const format_kind_info *fki,
3463 int offset_to_type_start, char conversion_char)
3465 /* Attempt to provide hints for argument types, but not for field widths
3466 and precisions. */
3467 if (!arg_type)
3468 return NULL;
3469 if (type->kind != CF_KIND_FORMAT)
3470 return NULL;
3472 /* Locate the current code within the source range, rejecting
3473 any awkward cases where the format string occupies more than
3474 one line.
3475 Lookup the place where the type starts (including any length
3476 modifiers), getting it as the caret location. */
3477 substring_loc type_loc (fmt_loc);
3478 type_loc.set_caret_index (offset_to_type_start);
3480 location_t fmt_substring_loc;
3481 const char *err = type_loc.get_location (&fmt_substring_loc);
3482 if (err)
3483 return NULL;
3485 source_range fmt_substring_range
3486 = get_range_from_loc (line_table, fmt_substring_loc);
3488 expanded_location caret
3489 = expand_location_to_spelling_point (fmt_substring_loc);
3490 expanded_location start
3491 = expand_location_to_spelling_point (fmt_substring_range.m_start);
3492 expanded_location finish
3493 = expand_location_to_spelling_point (fmt_substring_range.m_finish);
3494 if (caret.file != start.file)
3495 return NULL;
3496 if (start.file != finish.file)
3497 return NULL;
3498 if (caret.line != start.line)
3499 return NULL;
3500 if (start.line != finish.line)
3501 return NULL;
3502 if (start.column > caret.column)
3503 return NULL;
3504 if (start.column > finish.column)
3505 return NULL;
3506 if (caret.column > finish.column)
3507 return NULL;
3509 char_span line = location_get_source_line (start.file, start.line);
3510 if (!line)
3511 return NULL;
3513 /* If we got this far, then we have the line containing the
3514 existing conversion specification.
3516 Generate a trimmed copy, containing the prefix part of the conversion
3517 specification, up to the (but not including) the length modifier.
3518 In the above example, this would be "%-+*.*". */
3519 int length_up_to_type = caret.column - start.column;
3520 char_span prefix_span = line.subspan (start.column - 1, length_up_to_type);
3521 char *prefix = prefix_span.xstrdup ();
3523 /* Now attempt to generate a suggestion for the rest of the specification
3524 (length modifier and conversion char), based on ARG_TYPE and
3525 CONVERSION_CHAR.
3526 In the above example, this would be "ld". */
3527 char *format_for_type = get_format_for_type (fki, arg_type, conversion_char);
3528 if (!format_for_type)
3530 free (prefix);
3531 return NULL;
3534 /* Success. Generate the resulting suggestion for the whole range of
3535 FMT_LOC by concatenating the two strings.
3536 In the above example, this would be "%-+*.*ld". */
3537 char *result = concat (prefix, format_for_type, NULL);
3538 free (format_for_type);
3539 free (prefix);
3540 return result;
3543 /* Helper class for adding zero or more trailing '*' to types.
3545 The format type and name exclude any '*' for pointers, so those
3546 must be formatted manually. For all the types we currently have,
3547 this is adequate, but formats taking pointers to functions or
3548 arrays would require the full type to be built up in order to
3549 print it with %T. */
3551 class indirection_suffix
3553 public:
3554 indirection_suffix (int pointer_count) : m_pointer_count (pointer_count) {}
3556 /* Determine the size of the buffer (including NUL-terminator). */
3558 size_t get_buffer_size () const
3560 return m_pointer_count + 2;
3563 /* Write the '*' to DST and add a NUL-terminator. */
3565 void fill_buffer (char *dst) const
3567 if (m_pointer_count == 0)
3568 dst[0] = 0;
3569 else if (c_dialect_cxx ())
3571 memset (dst, '*', m_pointer_count);
3572 dst[m_pointer_count] = 0;
3574 else
3576 dst[0] = ' ';
3577 memset (dst + 1, '*', m_pointer_count);
3578 dst[m_pointer_count + 1] = 0;
3582 private:
3583 int m_pointer_count;
3586 /* Subclass of range_label for labelling the range in the format string
3587 with the type in question, adding trailing '*' for pointer_count. */
3589 class range_label_for_format_type_mismatch
3590 : public range_label_for_type_mismatch
3592 public:
3593 range_label_for_format_type_mismatch (tree labelled_type, tree other_type,
3594 int pointer_count)
3595 : range_label_for_type_mismatch (labelled_type, other_type),
3596 m_pointer_count (pointer_count)
3600 label_text get_text () const FINAL OVERRIDE
3602 label_text text = range_label_for_type_mismatch::get_text ();
3603 if (text.m_buffer == NULL)
3604 return text;
3606 indirection_suffix suffix (m_pointer_count);
3607 char *p = (char *) alloca (suffix.get_buffer_size ());
3608 suffix.fill_buffer (p);
3610 char *result = concat (text.m_buffer, p, NULL);
3611 text.maybe_free ();
3612 return label_text (result, true);
3615 private:
3616 int m_pointer_count;
3619 /* Give a warning about a format argument of different type from that expected.
3620 The range of the diagnostic is taken from WHOLE_FMT_LOC; the caret location
3621 is based on the location of the char at TYPE->offset_loc.
3622 PARAM_LOC is the location of the relevant argument, or UNKNOWN_LOCATION
3623 if this is unavailable.
3624 WANTED_TYPE is the type the argument should have,
3625 possibly stripped of pointer dereferences. The description (such as "field
3626 precision"), the placement in the format string, a possibly more
3627 friendly name of WANTED_TYPE, and the number of pointer dereferences
3628 are taken from TYPE. ARG_TYPE is the type of the actual argument,
3629 or NULL if it is missing.
3631 OFFSET_TO_TYPE_START is the offset within the execution-charset encoded
3632 format string to where type information begins for the conversion
3633 (the length modifier and conversion specifier).
3634 CONVERSION_CHAR is the user-provided conversion specifier.
3636 For example, given a type mismatch for argument 5 here:
3638 00000000011111111112222222222333333333344444444445555555555|
3639 12345678901234567890123456789012345678901234567890123456789` column numbers
3640 0000000000111111111122|
3641 0123456789012345678901` offsets within STRING_CST
3642 V~~~~~~~~ : range of WHOLE_FMT_LOC, from cols 23-31
3643 sprintf (d, "before %-+*.*lld after", int_expr, int_expr, long_expr);
3644 ^ ^ ^~~~~~~~~
3645 | ` CONVERSION_CHAR: 'd' PARAM_LOC
3646 type starts here
3648 OFFSET_TO_TYPE_START is 13, the offset to the "lld" within the
3649 STRING_CST. */
3651 static void
3652 format_type_warning (const substring_loc &whole_fmt_loc,
3653 location_t param_loc,
3654 format_wanted_type *type,
3655 tree wanted_type, tree arg_type,
3656 const format_kind_info *fki,
3657 int offset_to_type_start,
3658 char conversion_char)
3660 enum format_specifier_kind kind = type->kind;
3661 const char *wanted_type_name = type->wanted_type_name;
3662 const char *format_start = type->format_start;
3663 int format_length = type->format_length;
3664 int pointer_count = type->pointer_count;
3665 int arg_num = type->arg_num;
3667 /* If ARG_TYPE is a typedef with a misleading name (for example,
3668 size_t but not the standard size_t expected by printf %zu), avoid
3669 printing the typedef name. */
3670 if (wanted_type_name
3671 && arg_type
3672 && TYPE_NAME (arg_type)
3673 && TREE_CODE (TYPE_NAME (arg_type)) == TYPE_DECL
3674 && DECL_NAME (TYPE_NAME (arg_type))
3675 && !strcmp (wanted_type_name,
3676 lang_hooks.decl_printable_name (TYPE_NAME (arg_type), 2)))
3677 arg_type = TYPE_MAIN_VARIANT (arg_type);
3679 indirection_suffix suffix (pointer_count);
3680 char *p = (char *) alloca (suffix.get_buffer_size ());
3681 suffix.fill_buffer (p);
3683 /* WHOLE_FMT_LOC has the caret at the end of the range.
3684 Set the caret to be at the offset from TYPE. Subtract one
3685 from the offset for the same reason as in format_warning_at_char. */
3686 substring_loc fmt_loc (whole_fmt_loc);
3687 fmt_loc.set_caret_index (type->offset_loc - 1);
3689 range_label_for_format_type_mismatch fmt_label (wanted_type, arg_type,
3690 pointer_count);
3691 range_label_for_type_mismatch param_label (arg_type, wanted_type);
3693 /* Get a string for use as a replacement fix-it hint for the range in
3694 fmt_loc, or NULL. */
3695 char *corrected_substring
3696 = get_corrected_substring (fmt_loc, type, arg_type, fki,
3697 offset_to_type_start, conversion_char);
3698 format_string_diagnostic_t diag (fmt_loc, &fmt_label, param_loc, &param_label,
3699 corrected_substring);
3700 if (wanted_type_name)
3702 if (arg_type)
3703 diag.emit_warning
3704 (OPT_Wformat_,
3705 "%s %<%s%.*s%> expects argument of type %<%s%s%>, "
3706 "but argument %d has type %qT",
3707 gettext (kind_descriptions[kind]),
3708 (kind == CF_KIND_FORMAT ? "%" : ""),
3709 format_length, format_start,
3710 wanted_type_name, p, arg_num, arg_type);
3711 else
3712 diag.emit_warning
3713 (OPT_Wformat_,
3714 "%s %<%s%.*s%> expects a matching %<%s%s%> argument",
3715 gettext (kind_descriptions[kind]),
3716 (kind == CF_KIND_FORMAT ? "%" : ""),
3717 format_length, format_start, wanted_type_name, p);
3719 else
3721 if (arg_type)
3722 diag.emit_warning
3723 (OPT_Wformat_,
3724 "%s %<%s%.*s%> expects argument of type %<%T%s%>, "
3725 "but argument %d has type %qT",
3726 gettext (kind_descriptions[kind]),
3727 (kind == CF_KIND_FORMAT ? "%" : ""),
3728 format_length, format_start,
3729 wanted_type, p, arg_num, arg_type);
3730 else
3731 diag.emit_warning
3732 (OPT_Wformat_,
3733 "%s %<%s%.*s%> expects a matching %<%T%s%> argument",
3734 gettext (kind_descriptions[kind]),
3735 (kind == CF_KIND_FORMAT ? "%" : ""),
3736 format_length, format_start, wanted_type, p);
3739 free (corrected_substring);
3743 /* Given a format_char_info array FCI, and a character C, this function
3744 returns the index into the conversion_specs where that specifier's
3745 data is located. The character must exist. */
3746 static unsigned int
3747 find_char_info_specifier_index (const format_char_info *fci, int c)
3749 unsigned i;
3751 for (i = 0; fci->format_chars; i++, fci++)
3752 if (strchr (fci->format_chars, c))
3753 return i;
3755 /* We shouldn't be looking for a non-existent specifier. */
3756 gcc_unreachable ();
3759 /* Given a format_length_info array FLI, and a character C, this
3760 function returns the index into the conversion_specs where that
3761 modifier's data is located. The character must exist. */
3762 static unsigned int
3763 find_length_info_modifier_index (const format_length_info *fli, int c)
3765 unsigned i;
3767 for (i = 0; fli->name; i++, fli++)
3768 if (strchr (fli->name, c))
3769 return i;
3771 /* We shouldn't be looking for a non-existent modifier. */
3772 gcc_unreachable ();
3775 /* Determine the type of HOST_WIDE_INT in the code being compiled for
3776 use in GCC's __asm_fprintf__ custom format attribute. You must
3777 have set dynamic_format_types before calling this function. */
3778 static void
3779 init_dynamic_asm_fprintf_info (void)
3781 static tree hwi;
3783 if (!hwi)
3785 format_length_info *new_asm_fprintf_length_specs;
3786 unsigned int i;
3788 /* Find the underlying type for HOST_WIDE_INT. For the %w
3789 length modifier to work, one must have issued: "typedef
3790 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
3791 prior to using that modifier. */
3792 hwi = maybe_get_identifier ("__gcc_host_wide_int__");
3793 if (!hwi)
3795 error ("%<__gcc_host_wide_int__%> is not defined as a type");
3796 return;
3798 hwi = identifier_global_value (hwi);
3799 if (!hwi || TREE_CODE (hwi) != TYPE_DECL)
3801 error ("%<__gcc_host_wide_int__%> is not defined as a type");
3802 return;
3804 hwi = DECL_ORIGINAL_TYPE (hwi);
3805 gcc_assert (hwi);
3806 if (hwi != long_integer_type_node && hwi != long_long_integer_type_node)
3808 error ("%<__gcc_host_wide_int__%> is not defined as %<long%>"
3809 " or %<long long%>");
3810 return;
3813 /* Create a new (writable) copy of asm_fprintf_length_specs. */
3814 new_asm_fprintf_length_specs = (format_length_info *)
3815 xmemdup (asm_fprintf_length_specs,
3816 sizeof (asm_fprintf_length_specs),
3817 sizeof (asm_fprintf_length_specs));
3819 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
3820 i = find_length_info_modifier_index (new_asm_fprintf_length_specs, 'w');
3821 if (hwi == long_integer_type_node)
3822 new_asm_fprintf_length_specs[i].index = FMT_LEN_l;
3823 else if (hwi == long_long_integer_type_node)
3824 new_asm_fprintf_length_specs[i].index = FMT_LEN_ll;
3825 else
3826 gcc_unreachable ();
3828 /* Assign the new data for use. */
3829 dynamic_format_types[asm_fprintf_format_type].length_char_specs =
3830 new_asm_fprintf_length_specs;
3834 /* Determine the type of a "locus" in the code being compiled for use
3835 in GCC's __gcc_gfc__ custom format attribute. You must have set
3836 dynamic_format_types before calling this function. */
3837 static void
3838 init_dynamic_gfc_info (void)
3840 if (!locus)
3842 static format_char_info *gfc_fci;
3844 /* For the GCC __gcc_gfc__ custom format specifier to work, one
3845 must have declared 'locus' prior to using this attribute. If
3846 we haven't seen this declarations then you shouldn't use the
3847 specifier requiring that type. */
3848 if ((locus = maybe_get_identifier ("locus")))
3850 locus = identifier_global_value (locus);
3851 if (locus)
3853 if (TREE_CODE (locus) != TYPE_DECL
3854 || TREE_TYPE (locus) == error_mark_node)
3856 error ("%<locus%> is not defined as a type");
3857 locus = 0;
3859 else
3860 locus = TREE_TYPE (locus);
3864 /* Assign the new data for use. */
3866 /* Handle the __gcc_gfc__ format specifics. */
3867 if (!gfc_fci)
3868 dynamic_format_types[gcc_gfc_format_type].conversion_specs =
3869 gfc_fci = (format_char_info *)
3870 xmemdup (gcc_gfc_char_table,
3871 sizeof (gcc_gfc_char_table),
3872 sizeof (gcc_gfc_char_table));
3873 if (locus)
3875 const unsigned i = find_char_info_specifier_index (gfc_fci, 'L');
3876 gfc_fci[i].types[0].type = &locus;
3877 gfc_fci[i].pointer_count = 1;
3882 /* Determine the types of "tree" and "location_t" in the code being
3883 compiled for use in GCC's diagnostic custom format attributes. You
3884 must have set dynamic_format_types before calling this function. */
3885 static void
3886 init_dynamic_diag_info (void)
3888 /* For the GCC-diagnostics custom format specifiers to work, one
3889 must have declared 'tree' and 'location_t' prior to using those
3890 attributes. If we haven't seen these declarations then
3891 the specifiers requiring these types shouldn't be used.
3892 However we don't force a hard ICE because we may see only one
3893 or the other type. */
3894 if (tree loc = maybe_get_identifier ("location_t"))
3896 loc = identifier_global_value (loc);
3897 if (loc && TREE_CODE (loc) != TYPE_DECL)
3898 error ("%<location_t%> is not defined as a type");
3901 /* Initialize the global tree node type local to this file. */
3902 if (!local_tree_type_node
3903 || local_tree_type_node == void_type_node)
3905 /* We need to grab the underlying 'union tree_node' so peek into
3906 an extra type level. */
3907 if ((local_tree_type_node = maybe_get_identifier ("tree")))
3909 local_tree_type_node = identifier_global_value (local_tree_type_node);
3910 if (local_tree_type_node)
3912 if (TREE_CODE (local_tree_type_node) != TYPE_DECL)
3914 error ("%<tree%> is not defined as a type");
3915 local_tree_type_node = 0;
3917 else if (TREE_CODE (TREE_TYPE (local_tree_type_node))
3918 != POINTER_TYPE)
3920 error ("%<tree%> is not defined as a pointer type");
3921 local_tree_type_node = 0;
3923 else
3924 local_tree_type_node =
3925 TREE_TYPE (TREE_TYPE (local_tree_type_node));
3928 else
3929 local_tree_type_node = void_type_node;
3932 /* Similar to the above but for gimple*. */
3933 if (!local_gimple_ptr_node
3934 || local_gimple_ptr_node == void_type_node)
3936 if ((local_gimple_ptr_node = maybe_get_identifier ("gimple")))
3938 local_gimple_ptr_node
3939 = identifier_global_value (local_gimple_ptr_node);
3940 if (local_gimple_ptr_node)
3942 if (TREE_CODE (local_gimple_ptr_node) != TYPE_DECL)
3944 error ("%<gimple%> is not defined as a type");
3945 local_gimple_ptr_node = 0;
3947 else
3948 local_gimple_ptr_node = TREE_TYPE (local_gimple_ptr_node);
3951 else
3952 local_gimple_ptr_node = void_type_node;
3955 static tree hwi;
3957 if (!hwi)
3959 static format_length_info *diag_ls;
3960 unsigned int i;
3962 /* Find the underlying type for HOST_WIDE_INT. For the 'w'
3963 length modifier to work, one must have issued: "typedef
3964 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
3965 prior to using that modifier. */
3966 if ((hwi = maybe_get_identifier ("__gcc_host_wide_int__")))
3968 hwi = identifier_global_value (hwi);
3969 if (hwi)
3971 if (TREE_CODE (hwi) != TYPE_DECL)
3973 error ("%<__gcc_host_wide_int__%> is not defined as a type");
3974 hwi = 0;
3976 else
3978 hwi = DECL_ORIGINAL_TYPE (hwi);
3979 gcc_assert (hwi);
3980 if (hwi != long_integer_type_node
3981 && hwi != long_long_integer_type_node)
3983 error ("%<__gcc_host_wide_int__%> is not defined"
3984 " as %<long%> or %<long long%>");
3985 hwi = 0;
3991 /* Assign the new data for use. */
3993 /* All the GCC diag formats use the same length specs. */
3994 if (!diag_ls)
3995 dynamic_format_types[gcc_diag_format_type].length_char_specs =
3996 dynamic_format_types[gcc_tdiag_format_type].length_char_specs =
3997 dynamic_format_types[gcc_cdiag_format_type].length_char_specs =
3998 dynamic_format_types[gcc_cxxdiag_format_type].length_char_specs =
3999 dynamic_format_types[gcc_dump_printf_format_type].length_char_specs =
4000 diag_ls = (format_length_info *)
4001 xmemdup (gcc_diag_length_specs,
4002 sizeof (gcc_diag_length_specs),
4003 sizeof (gcc_diag_length_specs));
4004 if (hwi)
4006 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
4007 i = find_length_info_modifier_index (diag_ls, 'w');
4008 if (hwi == long_integer_type_node)
4009 diag_ls[i].index = FMT_LEN_l;
4010 else if (hwi == long_long_integer_type_node)
4011 diag_ls[i].index = FMT_LEN_ll;
4012 else
4013 gcc_unreachable ();
4017 /* It's safe to "re-initialize these to the same values. */
4018 dynamic_format_types[gcc_diag_format_type].conversion_specs =
4019 gcc_diag_char_table;
4020 dynamic_format_types[gcc_tdiag_format_type].conversion_specs =
4021 gcc_tdiag_char_table;
4022 dynamic_format_types[gcc_cdiag_format_type].conversion_specs =
4023 gcc_cdiag_char_table;
4024 dynamic_format_types[gcc_cxxdiag_format_type].conversion_specs =
4025 gcc_cxxdiag_char_table;
4026 dynamic_format_types[gcc_dump_printf_format_type].conversion_specs =
4027 gcc_dump_printf_char_table;
4030 #ifdef TARGET_FORMAT_TYPES
4031 extern const format_kind_info TARGET_FORMAT_TYPES[];
4032 #endif
4034 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
4035 extern const target_ovr_attr TARGET_OVERRIDES_FORMAT_ATTRIBUTES[];
4036 #endif
4037 #ifdef TARGET_OVERRIDES_FORMAT_INIT
4038 extern void TARGET_OVERRIDES_FORMAT_INIT (void);
4039 #endif
4041 /* Attributes such as "printf" are equivalent to those such as
4042 "gnu_printf" unless this is overridden by a target. */
4043 static const target_ovr_attr gnu_target_overrides_format_attributes[] =
4045 { "gnu_printf", "printf" },
4046 { "gnu_scanf", "scanf" },
4047 { "gnu_strftime", "strftime" },
4048 { "gnu_strfmon", "strfmon" },
4049 { NULL, NULL }
4052 /* Translate to unified attribute name. This is used in decode_format_type and
4053 decode_format_attr. In attr_name the user specified argument is passed. It
4054 returns the unified format name from TARGET_OVERRIDES_FORMAT_ATTRIBUTES
4055 or the attr_name passed to this function, if there is no matching entry. */
4056 static const char *
4057 convert_format_name_to_system_name (const char *attr_name)
4059 int i;
4061 if (attr_name == NULL || *attr_name == 0
4062 || strncmp (attr_name, "gcc_", 4) == 0)
4063 return attr_name;
4064 #ifdef TARGET_OVERRIDES_FORMAT_INIT
4065 TARGET_OVERRIDES_FORMAT_INIT ();
4066 #endif
4068 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
4069 /* Check if format attribute is overridden by target. */
4070 if (TARGET_OVERRIDES_FORMAT_ATTRIBUTES != NULL
4071 && TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT > 0)
4073 for (i = 0; i < TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT; ++i)
4075 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_src,
4076 attr_name))
4077 return attr_name;
4078 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_dst,
4079 attr_name))
4080 return TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_src;
4083 #endif
4084 /* Otherwise default to gnu format. */
4085 for (i = 0;
4086 gnu_target_overrides_format_attributes[i].named_attr_src != NULL;
4087 ++i)
4089 if (cmp_attribs (gnu_target_overrides_format_attributes[i].named_attr_src,
4090 attr_name))
4091 return attr_name;
4092 if (cmp_attribs (gnu_target_overrides_format_attributes[i].named_attr_dst,
4093 attr_name))
4094 return gnu_target_overrides_format_attributes[i].named_attr_src;
4097 return attr_name;
4100 /* Handle a "format" attribute; arguments as in
4101 struct attribute_spec.handler. */
4102 tree
4103 handle_format_attribute (tree *node, tree ARG_UNUSED (name), tree args,
4104 int flags, bool *no_add_attrs)
4106 tree type = *node;
4107 function_format_info info;
4109 #ifdef TARGET_FORMAT_TYPES
4110 /* If the target provides additional format types, we need to
4111 add them to FORMAT_TYPES at first use. */
4112 if (TARGET_FORMAT_TYPES != NULL && !dynamic_format_types)
4114 dynamic_format_types = XNEWVEC (format_kind_info,
4115 n_format_types + TARGET_N_FORMAT_TYPES);
4116 memcpy (dynamic_format_types, format_types_orig,
4117 sizeof (format_types_orig));
4118 memcpy (&dynamic_format_types[n_format_types], TARGET_FORMAT_TYPES,
4119 TARGET_N_FORMAT_TYPES * sizeof (dynamic_format_types[0]));
4121 format_types = dynamic_format_types;
4122 /* Provide a reference for the first potential external type. */
4123 first_target_format_type = n_format_types;
4124 n_format_types += TARGET_N_FORMAT_TYPES;
4126 #endif
4128 /* Canonicalize name of format function. */
4129 if (TREE_CODE (TREE_VALUE (args)) == IDENTIFIER_NODE)
4130 TREE_VALUE (args) = canonicalize_attr_name (TREE_VALUE (args));
4132 if (!decode_format_attr (args, &info, 0))
4134 *no_add_attrs = true;
4135 return NULL_TREE;
4138 if (prototype_p (type))
4140 if (!check_format_string (type, info.format_num, flags,
4141 no_add_attrs, info.format_type))
4142 return NULL_TREE;
4144 if (info.first_arg_num != 0)
4146 unsigned HOST_WIDE_INT arg_num = 1;
4147 function_args_iterator iter;
4148 tree arg_type;
4150 /* Verify that first_arg_num points to the last arg,
4151 the ... */
4152 FOREACH_FUNCTION_ARGS (type, arg_type, iter)
4153 arg_num++;
4155 if (arg_num != info.first_arg_num)
4157 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
4158 error ("args to be formatted is not %<...%>");
4159 *no_add_attrs = true;
4160 return NULL_TREE;
4165 /* Check if this is a strftime variant. Just for this variant
4166 FMT_FLAG_ARG_CONVERT is not set. */
4167 if ((format_types[info.format_type].flags & (int) FMT_FLAG_ARG_CONVERT) == 0
4168 && info.first_arg_num != 0)
4170 error ("strftime formats cannot format arguments");
4171 *no_add_attrs = true;
4172 return NULL_TREE;
4175 /* If this is a custom GCC-internal format type, we have to
4176 initialize certain bits at runtime. */
4177 if (info.format_type == asm_fprintf_format_type
4178 || info.format_type == gcc_gfc_format_type
4179 || info.format_type == gcc_diag_format_type
4180 || info.format_type == gcc_tdiag_format_type
4181 || info.format_type == gcc_cdiag_format_type
4182 || info.format_type == gcc_cxxdiag_format_type
4183 || info.format_type == gcc_dump_printf_format_type)
4185 /* Our first time through, we have to make sure that our
4186 format_type data is allocated dynamically and is modifiable. */
4187 if (!dynamic_format_types)
4188 format_types = dynamic_format_types = (format_kind_info *)
4189 xmemdup (format_types_orig, sizeof (format_types_orig),
4190 sizeof (format_types_orig));
4192 /* If this is format __asm_fprintf__, we have to initialize
4193 GCC's notion of HOST_WIDE_INT for checking %wd. */
4194 if (info.format_type == asm_fprintf_format_type)
4195 init_dynamic_asm_fprintf_info ();
4196 /* If this is format __gcc_gfc__, we have to initialize GCC's
4197 notion of 'locus' at runtime for %L. */
4198 else if (info.format_type == gcc_gfc_format_type)
4199 init_dynamic_gfc_info ();
4200 /* If this is one of the diagnostic attributes, then we have to
4201 initialize 'location_t' and 'tree' at runtime. */
4202 else if (info.format_type == gcc_diag_format_type
4203 || info.format_type == gcc_tdiag_format_type
4204 || info.format_type == gcc_cdiag_format_type
4205 || info.format_type == gcc_cxxdiag_format_type
4206 || info.format_type == gcc_dump_printf_format_type)
4207 init_dynamic_diag_info ();
4208 else
4209 gcc_unreachable ();
4212 return NULL_TREE;
4215 #if CHECKING_P
4217 namespace selftest {
4219 /* Selftests of location handling. */
4221 /* Get the format_kind_info with the given name. */
4223 static const format_kind_info *
4224 get_info (const char *name)
4226 int idx = decode_format_type (name);
4227 const format_kind_info *fki = &format_types[idx];
4228 ASSERT_STREQ (fki->name, name);
4229 return fki;
4232 /* Verify that get_format_for_type (FKI, TYPE, CONVERSION_CHAR)
4233 is EXPECTED_FORMAT. */
4235 static void
4236 assert_format_for_type_streq (const location &loc, const format_kind_info *fki,
4237 const char *expected_format, tree type,
4238 char conversion_char)
4240 gcc_assert (fki);
4241 gcc_assert (expected_format);
4242 gcc_assert (type);
4244 char *actual_format = get_format_for_type (fki, type, conversion_char);
4245 ASSERT_STREQ_AT (loc, expected_format, actual_format);
4246 free (actual_format);
4249 /* Selftests for get_format_for_type. */
4251 #define ASSERT_FORMAT_FOR_TYPE_STREQ(EXPECTED_FORMAT, TYPE, CONVERSION_CHAR) \
4252 assert_format_for_type_streq (SELFTEST_LOCATION, (fki), (EXPECTED_FORMAT), \
4253 (TYPE), (CONVERSION_CHAR))
4255 /* Selftest for get_format_for_type for "printf"-style functions. */
4257 static void
4258 test_get_format_for_type_printf ()
4260 const format_kind_info *fki = get_info ("gnu_printf");
4261 ASSERT_NE (fki, NULL);
4263 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node, 'i');
4264 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node, 'i');
4265 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node, 'o');
4266 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node, 'o');
4267 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node, 'x');
4268 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node, 'x');
4269 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node, 'X');
4270 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node, 'X');
4271 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", integer_type_node, 'd');
4272 ASSERT_FORMAT_FOR_TYPE_STREQ ("i", integer_type_node, 'i');
4273 ASSERT_FORMAT_FOR_TYPE_STREQ ("o", integer_type_node, 'o');
4274 ASSERT_FORMAT_FOR_TYPE_STREQ ("x", integer_type_node, 'x');
4275 ASSERT_FORMAT_FOR_TYPE_STREQ ("X", integer_type_node, 'X');
4276 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", unsigned_type_node, 'd');
4277 ASSERT_FORMAT_FOR_TYPE_STREQ ("i", unsigned_type_node, 'i');
4278 ASSERT_FORMAT_FOR_TYPE_STREQ ("o", unsigned_type_node, 'o');
4279 ASSERT_FORMAT_FOR_TYPE_STREQ ("x", unsigned_type_node, 'x');
4280 ASSERT_FORMAT_FOR_TYPE_STREQ ("X", unsigned_type_node, 'X');
4281 ASSERT_FORMAT_FOR_TYPE_STREQ ("ld", long_integer_type_node, 'd');
4282 ASSERT_FORMAT_FOR_TYPE_STREQ ("li", long_integer_type_node, 'i');
4283 ASSERT_FORMAT_FOR_TYPE_STREQ ("lx", long_integer_type_node, 'x');
4284 ASSERT_FORMAT_FOR_TYPE_STREQ ("lo", long_unsigned_type_node, 'o');
4285 ASSERT_FORMAT_FOR_TYPE_STREQ ("lx", long_unsigned_type_node, 'x');
4286 ASSERT_FORMAT_FOR_TYPE_STREQ ("lld", long_long_integer_type_node, 'd');
4287 ASSERT_FORMAT_FOR_TYPE_STREQ ("lli", long_long_integer_type_node, 'i');
4288 ASSERT_FORMAT_FOR_TYPE_STREQ ("llo", long_long_unsigned_type_node, 'o');
4289 ASSERT_FORMAT_FOR_TYPE_STREQ ("llx", long_long_unsigned_type_node, 'x');
4290 ASSERT_FORMAT_FOR_TYPE_STREQ ("s", build_pointer_type (char_type_node), 'i');
4293 /* Selftest for get_format_for_type for "scanf"-style functions. */
4295 static void
4296 test_get_format_for_type_scanf ()
4298 const format_kind_info *fki = get_info ("gnu_scanf");
4299 ASSERT_NE (fki, NULL);
4300 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", build_pointer_type (integer_type_node), 'd');
4301 ASSERT_FORMAT_FOR_TYPE_STREQ ("u", build_pointer_type (unsigned_type_node), 'u');
4302 ASSERT_FORMAT_FOR_TYPE_STREQ ("ld",
4303 build_pointer_type (long_integer_type_node), 'd');
4304 ASSERT_FORMAT_FOR_TYPE_STREQ ("lu",
4305 build_pointer_type (long_unsigned_type_node), 'u');
4306 ASSERT_FORMAT_FOR_TYPE_STREQ
4307 ("lld", build_pointer_type (long_long_integer_type_node), 'd');
4308 ASSERT_FORMAT_FOR_TYPE_STREQ
4309 ("llu", build_pointer_type (long_long_unsigned_type_node), 'u');
4310 ASSERT_FORMAT_FOR_TYPE_STREQ ("e", build_pointer_type (float_type_node), 'e');
4311 ASSERT_FORMAT_FOR_TYPE_STREQ ("le", build_pointer_type (double_type_node), 'e');
4314 #undef ASSERT_FORMAT_FOR_TYPE_STREQ
4316 /* Exercise the type-printing label code, to give some coverage
4317 under "make selftest-valgrind" (in particular, to ensure that
4318 the label-printing machinery doesn't leak). */
4320 static void
4321 test_type_mismatch_range_labels ()
4323 /* Create a tempfile and write some text to it.
4324 ....................0000000001 11111111 12 22222222
4325 ....................1234567890 12345678 90 12345678. */
4326 const char *content = " printf (\"msg: %i\\n\", msg);\n";
4327 temp_source_file tmp (SELFTEST_LOCATION, ".c", content);
4328 line_table_test ltt;
4330 linemap_add (line_table, LC_ENTER, false, tmp.get_filename (), 1);
4332 location_t c17 = linemap_position_for_column (line_table, 17);
4333 ASSERT_EQ (LOCATION_COLUMN (c17), 17);
4334 location_t c18 = linemap_position_for_column (line_table, 18);
4335 location_t c24 = linemap_position_for_column (line_table, 24);
4336 location_t c26 = linemap_position_for_column (line_table, 26);
4338 /* Don't attempt to run the tests if column data might be unavailable. */
4339 if (c26 > LINE_MAP_MAX_LOCATION_WITH_COLS)
4340 return;
4342 location_t fmt = make_location (c18, c17, c18);
4343 ASSERT_EQ (LOCATION_COLUMN (fmt), 18);
4345 location_t param = make_location (c24, c24, c26);
4346 ASSERT_EQ (LOCATION_COLUMN (param), 24);
4348 range_label_for_format_type_mismatch fmt_label (char_type_node,
4349 integer_type_node, 1);
4350 range_label_for_type_mismatch param_label (integer_type_node,
4351 char_type_node);
4352 gcc_rich_location richloc (fmt, &fmt_label);
4353 richloc.add_range (param, SHOW_RANGE_WITHOUT_CARET, &param_label);
4355 test_diagnostic_context dc;
4356 diagnostic_show_locus (&dc, &richloc, DK_ERROR);
4357 if (c_dialect_cxx ())
4358 /* "char*", without a space. */
4359 ASSERT_STREQ ("\n"
4360 " printf (\"msg: %i\\n\", msg);\n"
4361 " ~^ ~~~\n"
4362 " | |\n"
4363 " char* int\n",
4364 pp_formatted_text (dc.printer));
4365 else
4366 /* "char *", with a space. */
4367 ASSERT_STREQ ("\n"
4368 " printf (\"msg: %i\\n\", msg);\n"
4369 " ~^ ~~~\n"
4370 " | |\n"
4371 " | int\n"
4372 " char *\n",
4373 pp_formatted_text (dc.printer));
4376 /* Run all of the selftests within this file. */
4378 void
4379 c_format_c_tests ()
4381 test_get_modifier_for_format_len ();
4382 test_get_format_for_type_printf ();
4383 test_get_format_for_type_scanf ();
4384 test_type_mismatch_range_labels ();
4387 } // namespace selftest
4389 #endif /* CHECKING_P */
4391 #include "gt-c-family-c-format.h"