2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 #include "format_check.h"
28 #include "diagnostic.h"
32 #include "lang_features.h"
34 typedef enum format_flag_t {
36 FMT_FLAG_HASH = 1U << 0,
37 FMT_FLAG_ZERO = 1U << 1,
38 FMT_FLAG_MINUS = 1U << 2,
39 FMT_FLAG_SPACE = 1U << 3,
40 FMT_FLAG_PLUS = 1U << 4,
41 FMT_FLAG_TICK = 1U << 5
44 typedef unsigned format_flags_t;
46 typedef enum format_length_modifier_t {
57 /* only in microsoft mode */
62 } format_length_modifier_t;
64 typedef struct format_spec_t {
65 const char *name; /**< name of the function */
66 format_kind_t fmt_kind; /**< kind */
67 unsigned fmt_idx; /**< index of the format string */
68 unsigned arg_idx; /**< index of the first argument */
71 static const char* get_length_modifier_name(const format_length_modifier_t mod)
73 static const char* const names[] = {
84 /* only in microsoft mode */
87 [FMT_MOD_I32] = "I32",
90 assert(mod < lengthof(names));
94 static void warn_invalid_length_modifier(const source_position_t *pos,
95 const format_length_modifier_t mod,
96 const wchar_rep_t conversion)
99 "invalid length modifier '%s' for conversion specifier '%%%c'",
100 get_length_modifier_name(mod), conversion
104 typedef struct vchar_t vchar_t;
106 const void *string; /**< the string */
107 size_t position; /**< current position */
108 size_t size; /**< size of the string */
110 /** return the first character of the string and setthe position to 0. */
111 unsigned (*first)(vchar_t *self);
112 /** return the next character of the string */
113 unsigned (*next)(vchar_t *self);
114 /** return non_zero if the given character is a digit */
115 int (*is_digit)(unsigned vchar);
118 static unsigned string_first(vchar_t *self)
121 const string_t *string = self->string;
122 return string->begin[0];
125 static unsigned string_next(vchar_t *self)
128 const string_t *string = self->string;
129 return string->begin[self->position];
132 static int string_isdigit(unsigned vchar)
134 return isdigit(vchar);
137 static unsigned wstring_first(vchar_t *self)
140 const wide_string_t *wstring = self->string;
141 return wstring->begin[0];
144 static unsigned wstring_next(vchar_t *self)
147 const wide_string_t *wstring = self->string;
148 return wstring->begin[self->position];
151 static int wstring_isdigit(unsigned vchar)
153 return iswdigit(vchar);
156 static bool atend(vchar_t *self)
158 return self->position + 1 == self->size;
162 * Check printf-style format.
164 static void check_printf_format(const call_argument_t *arg, const format_spec_t *spec)
166 /* find format arg */
168 for (; idx < spec->fmt_idx; ++idx) {
174 const expression_t *fmt_expr = arg->expression;
175 if (fmt_expr->kind == EXPR_UNARY_CAST_IMPLICIT) {
176 fmt_expr = fmt_expr->unary.value;
180 if (fmt_expr->kind == EXPR_WIDE_STRING_LITERAL) {
181 vchar.string = &fmt_expr->wide_string.value;
182 vchar.size = fmt_expr->wide_string.value.size;
183 vchar.first = wstring_first;
184 vchar.next = wstring_next;
185 vchar.is_digit = wstring_isdigit;
186 } else if (fmt_expr->kind == EXPR_STRING_LITERAL) {
187 vchar.string = &fmt_expr->string.value;
188 vchar.size = fmt_expr->string.value.size;
189 vchar.first = string_first;
190 vchar.next = string_next;
191 vchar.is_digit = string_isdigit;
195 /* find the real args */
196 for(; idx < spec->arg_idx && arg != NULL; ++idx)
199 const source_position_t *pos = &fmt_expr->base.source_position;
200 unsigned fmt = vchar.first(&vchar);
201 unsigned num_fmt = 0;
202 for (; fmt != '\0'; fmt = vchar.next(&vchar)) {
205 fmt = vchar.next(&vchar);
212 format_flags_t fmt_flags = FMT_FLAG_NONE;
214 fmt = vchar.next(&vchar);
215 fmt_flags |= FMT_FLAG_ZERO;
218 /* argument selector or minimum field width */
219 if (vchar.is_digit(fmt)) {
221 fmt = vchar.next(&vchar);
222 } while (vchar.is_digit(fmt));
224 /* digit string was ... */
226 /* ... argument selector */
227 fmt_flags = FMT_FLAG_NONE; /* reset possibly set 0-flag */
231 /* ... minimum field width */
237 case '#': flag = FMT_FLAG_HASH; break;
238 case '0': flag = FMT_FLAG_ZERO; break;
239 case '-': flag = FMT_FLAG_MINUS; break;
240 case '\'': flag = FMT_FLAG_TICK; break;
243 if (fmt_flags & FMT_FLAG_PLUS) {
244 warningf(pos, "' ' is overridden by prior '+' in conversion specification %u", num_fmt);
246 flag = FMT_FLAG_SPACE;
250 if (fmt_flags & FMT_FLAG_SPACE) {
251 warningf(pos, "'+' overrides prior ' ' in conversion specification %u", num_fmt);
253 flag = FMT_FLAG_PLUS;
256 default: goto break_fmt_flags;
258 if (fmt_flags & flag) {
259 warningf(pos, "repeated flag '%c' in conversion specification %u", (char)fmt, num_fmt);
262 fmt = vchar.next(&vchar);
266 /* minimum field width */
268 fmt = vchar.next(&vchar);
270 warningf(pos, "missing argument for '*' field width in conversion specification %u", num_fmt);
273 const type_t *const arg_type = arg->expression->base.type;
274 if (arg_type != type_int) {
275 warningf(pos, "argument for '*' field width in conversion specification %u is not an 'int', but an '%T'", num_fmt, arg_type);
279 while (vchar.is_digit(fmt)) {
280 fmt = vchar.next(&vchar);
287 fmt = vchar.next(&vchar);
289 fmt = vchar.next(&vchar);
291 warningf(pos, "missing argument for '*' precision in conversion specification %u", num_fmt);
294 const type_t *const arg_type = arg->expression->base.type;
295 if (arg_type != type_int) {
296 warningf(pos, "argument for '*' precision in conversion specification %u is not an 'int', but an '%T'", num_fmt, arg_type);
300 /* digit string may be omitted */
301 while (vchar.is_digit(fmt)) {
302 fmt = vchar.next(&vchar);
307 /* length modifier */
308 format_length_modifier_t fmt_mod;
311 fmt = vchar.next(&vchar);
313 fmt = vchar.next(&vchar);
314 fmt_mod = FMT_MOD_hh;
321 fmt = vchar.next(&vchar);
323 fmt = vchar.next(&vchar);
324 fmt_mod = FMT_MOD_ll;
330 case 'L': fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_L; break;
331 case 'j': fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_j; break;
332 case 't': fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_t; break;
333 case 'z': fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_z; break;
334 case 'q': fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_q; break;
338 fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_w;
340 fmt_mod = FMT_MOD_NONE;
345 fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_I;
347 fmt = vchar.next(&vchar);
349 fmt = vchar.next(&vchar);
350 fmt_mod = FMT_MOD_I32;
355 } else if (fmt == '6') {
356 fmt = vchar.next(&vchar);
358 fmt = vchar.next(&vchar);
359 fmt_mod = FMT_MOD_I64;
366 fmt_mod = FMT_MOD_NONE;
370 fmt_mod = FMT_MOD_NONE;
375 warningf(pos, "dangling %% in format string");
379 type_t *expected_type;
380 type_qualifiers_t expected_qual = TYPE_QUALIFIER_NONE;
381 format_flags_t allowed_flags;
386 case FMT_MOD_NONE: expected_type = type_int; break;
387 case FMT_MOD_hh: expected_type = type_int; break; /* TODO promoted signed char */
388 case FMT_MOD_h: expected_type = type_int; break; /* TODO promoted short */
389 case FMT_MOD_l: expected_type = type_long; break;
390 case FMT_MOD_ll: expected_type = type_long_long; break;
391 case FMT_MOD_j: expected_type = type_intmax_t; break;
392 case FMT_MOD_z: expected_type = type_ssize_t; break;
393 case FMT_MOD_t: expected_type = type_ptrdiff_t; break;
394 case FMT_MOD_I: expected_type = type_ptrdiff_t; break;
395 case FMT_MOD_I32: expected_type = type_int32; break;
396 case FMT_MOD_I64: expected_type = type_int64; break;
399 warn_invalid_length_modifier(pos, fmt_mod, fmt);
402 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_SPACE | FMT_FLAG_PLUS | FMT_FLAG_ZERO;
408 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_HASH | FMT_FLAG_ZERO;
409 goto eval_fmt_mod_unsigned;
412 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_ZERO;
413 eval_fmt_mod_unsigned:
415 case FMT_MOD_NONE: expected_type = type_unsigned_int; break;
416 case FMT_MOD_hh: expected_type = type_int; break; /* TODO promoted unsigned char */
417 case FMT_MOD_h: expected_type = type_int; break; /* TODO promoted unsigned short */
418 case FMT_MOD_l: expected_type = type_unsigned_long; break;
419 case FMT_MOD_ll: expected_type = type_unsigned_long_long; break;
420 case FMT_MOD_j: expected_type = type_uintmax_t; break;
421 case FMT_MOD_z: expected_type = type_size_t; break;
422 case FMT_MOD_t: expected_type = type_uptrdiff_t; break;
423 case FMT_MOD_I: expected_type = type_size_t; break;
424 case FMT_MOD_I32: expected_type = type_unsigned_int32; break;
425 case FMT_MOD_I64: expected_type = type_unsigned_int64; break;
428 warn_invalid_length_modifier(pos, fmt_mod, fmt);
442 case FMT_MOD_l: /* l modifier is ignored */
443 case FMT_MOD_NONE: expected_type = type_double; break;
444 case FMT_MOD_L: expected_type = type_long_double; break;
447 warn_invalid_length_modifier(pos, fmt_mod, fmt);
450 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_SPACE | FMT_FLAG_PLUS | FMT_FLAG_HASH | FMT_FLAG_ZERO;
454 if (fmt_mod != FMT_MOD_NONE) {
455 warn_invalid_length_modifier(pos, fmt_mod, fmt);
458 expected_type = type_wchar_t;
459 allowed_flags = FMT_FLAG_NONE;
463 expected_type = type_int;
465 case FMT_MOD_NONE: expected_type = type_int; break; /* TODO promoted char */
466 case FMT_MOD_l: expected_type = type_wint_t; break;
467 case FMT_MOD_w: expected_type = type_wchar_t; break;
470 warn_invalid_length_modifier(pos, fmt_mod, fmt);
473 allowed_flags = FMT_FLAG_NONE;
477 if (fmt_mod != FMT_MOD_NONE) {
478 warn_invalid_length_modifier(pos, fmt_mod, fmt);
481 expected_type = type_wchar_t_ptr;
482 expected_qual = TYPE_QUALIFIER_CONST;
483 allowed_flags = FMT_FLAG_MINUS;
488 case FMT_MOD_NONE: expected_type = type_char_ptr; break;
489 case FMT_MOD_l: expected_type = type_wchar_t_ptr; break;
490 case FMT_MOD_w: expected_type = type_wchar_t_ptr; break;
493 warn_invalid_length_modifier(pos, fmt_mod, fmt);
496 expected_qual = TYPE_QUALIFIER_CONST;
497 allowed_flags = FMT_FLAG_MINUS;
501 if (fmt_mod != FMT_MOD_NONE) {
502 warn_invalid_length_modifier(pos, fmt_mod, fmt);
505 expected_type = type_void_ptr;
506 allowed_flags = FMT_FLAG_NONE;
511 case FMT_MOD_NONE: expected_type = type_int_ptr; break;
512 case FMT_MOD_hh: expected_type = type_signed_char_ptr; break;
513 case FMT_MOD_h: expected_type = type_short_ptr; break;
514 case FMT_MOD_l: expected_type = type_long_ptr; break;
515 case FMT_MOD_ll: expected_type = type_long_long_ptr; break;
516 case FMT_MOD_j: expected_type = type_intmax_t_ptr; break;
517 case FMT_MOD_z: expected_type = type_ssize_t_ptr; break;
518 case FMT_MOD_t: expected_type = type_ptrdiff_t_ptr; break;
521 warn_invalid_length_modifier(pos, fmt_mod, fmt);
524 allowed_flags = FMT_FLAG_NONE;
528 warningf(pos, "encountered unknown conversion specifier '%%%C' at position %u", (wint_t)fmt, num_fmt);
530 warningf(pos, "too few arguments for format string");
536 format_flags_t wrong_flags = fmt_flags & ~allowed_flags;
537 if (wrong_flags != 0) {
540 if (wrong_flags & FMT_FLAG_HASH) *p++ = '#';
541 if (wrong_flags & FMT_FLAG_ZERO) *p++ = '0';
542 if (wrong_flags & FMT_FLAG_MINUS) *p++ = '-';
543 if (wrong_flags & FMT_FLAG_SPACE) *p++ = ' ';
544 if (wrong_flags & FMT_FLAG_PLUS) *p++ = '+';
545 if (wrong_flags & FMT_FLAG_TICK) *p++ = '\'';
548 warningf(pos, "invalid format flags \"%s\" in conversion specification %%%c at position %u", wrong, fmt, num_fmt);
552 warningf(pos, "too few arguments for format string");
556 { /* create a scope here to prevent warning about the jump to next_arg */
557 type_t *const arg_type = arg->expression->base.type;
558 type_t *const arg_skip = skip_typeref(arg_type);
559 type_t *const expected_type_skip = skip_typeref(expected_type);
562 /* allow any pointer type for %p, not just void */
563 if (is_type_pointer(arg_skip))
567 if (is_type_pointer(expected_type_skip)) {
568 if (is_type_pointer(arg_skip)) {
569 type_t *const exp_to = skip_typeref(expected_type_skip->pointer.points_to);
570 type_t *const arg_to = skip_typeref(arg_skip->pointer.points_to);
571 if ((arg_to->base.qualifiers & ~expected_qual) == 0 &&
572 get_unqualified_type(arg_to) == exp_to) {
577 if (get_unqualified_type(arg_skip) == expected_type_skip) {
581 if (is_type_valid(arg_skip)) {
583 "argument type '%T' does not match conversion specifier '%%%s%c' at position %u",
584 arg_type, get_length_modifier_name(fmt_mod), (char)fmt, num_fmt);
590 if (!atend(&vchar)) {
591 warningf(pos, "format string contains '\\0'");
594 unsigned num_args = num_fmt;
595 while (arg != NULL) {
599 warningf(pos, "%u argument%s but only %u format specifier%s",
600 num_args, num_args != 1 ? "s" : "",
601 num_fmt, num_fmt != 1 ? "s" : "");
606 * Check scanf-style format.
608 static void check_scanf_format(const call_argument_t *arg, const format_spec_t *spec)
610 /* find format arg */
612 for (; idx < spec->fmt_idx; ++idx) {
618 const expression_t *fmt_expr = arg->expression;
619 if (fmt_expr->kind == EXPR_UNARY_CAST_IMPLICIT) {
620 fmt_expr = fmt_expr->unary.value;
624 if (fmt_expr->kind == EXPR_WIDE_STRING_LITERAL) {
625 vchar.string = &fmt_expr->wide_string.value;
626 vchar.size = fmt_expr->wide_string.value.size;
627 vchar.first = wstring_first;
628 vchar.next = wstring_next;
629 vchar.is_digit = wstring_isdigit;
630 } else if (fmt_expr->kind == EXPR_STRING_LITERAL) {
631 vchar.string = &fmt_expr->string.value;
632 vchar.size = fmt_expr->string.value.size;
633 vchar.first = string_first;
634 vchar.next = string_next;
635 vchar.is_digit = string_isdigit;
639 /* find the real args */
640 for (; idx < spec->arg_idx && arg != NULL; ++idx)
643 const source_position_t *pos = &fmt_expr->base.source_position;
644 unsigned fmt = vchar.first(&vchar);
645 unsigned num_fmt = 0;
646 for (; fmt != '\0'; fmt = vchar.next(&vchar)) {
649 fmt = vchar.next(&vchar);
656 /* length modifier */
657 format_length_modifier_t fmt_mod;
660 fmt = vchar.next(&vchar);
662 fmt = vchar.next(&vchar);
663 fmt_mod = FMT_MOD_hh;
670 fmt = vchar.next(&vchar);
672 fmt = vchar.next(&vchar);
673 fmt_mod = FMT_MOD_ll;
679 case 'L': fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_L; break;
680 case 'j': fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_j; break;
681 case 't': fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_t; break;
682 case 'z': fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_z; break;
686 fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_w;
688 fmt_mod = FMT_MOD_NONE;
693 fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_I;
695 fmt = vchar.next(&vchar);
697 fmt = vchar.next(&vchar);
698 fmt_mod = FMT_MOD_I32;
703 } else if (fmt == '6') {
704 fmt = vchar.next(&vchar);
706 fmt = vchar.next(&vchar);
707 fmt_mod = FMT_MOD_I64;
714 fmt_mod = FMT_MOD_NONE;
718 fmt_mod = FMT_MOD_NONE;
723 warningf(pos, "dangling %% in format string");
727 type_t *expected_type;
732 case FMT_MOD_NONE: expected_type = type_int; break;
733 case FMT_MOD_hh: expected_type = type_signed_char; break;
734 case FMT_MOD_h: expected_type = type_short; break;
735 case FMT_MOD_l: expected_type = type_long; break;
736 case FMT_MOD_ll: expected_type = type_long_long; break;
737 case FMT_MOD_j: expected_type = type_intmax_t; break;
738 case FMT_MOD_z: expected_type = type_ssize_t; break;
739 case FMT_MOD_t: expected_type = type_ptrdiff_t; break;
740 case FMT_MOD_I: expected_type = type_ptrdiff_t; break;
741 case FMT_MOD_I32: expected_type = type_int32; break;
742 case FMT_MOD_I64: expected_type = type_int64; break;
745 warn_invalid_length_modifier(pos, fmt_mod, fmt);
753 goto eval_fmt_mod_unsigned;
756 eval_fmt_mod_unsigned:
758 case FMT_MOD_NONE: expected_type = type_unsigned_int; break;
759 case FMT_MOD_hh: expected_type = type_unsigned_char; break;
760 case FMT_MOD_h: expected_type = type_unsigned_short; break;
761 case FMT_MOD_l: expected_type = type_unsigned_long; break;
762 case FMT_MOD_ll: expected_type = type_unsigned_long_long; break;
763 case FMT_MOD_j: expected_type = type_uintmax_t; break;
764 case FMT_MOD_z: expected_type = type_size_t; break;
765 case FMT_MOD_t: expected_type = type_uptrdiff_t; break;
766 case FMT_MOD_I: expected_type = type_size_t; break;
767 case FMT_MOD_I32: expected_type = type_unsigned_int32; break;
768 case FMT_MOD_I64: expected_type = type_unsigned_int64; break;
771 warn_invalid_length_modifier(pos, fmt_mod, fmt);
785 case FMT_MOD_l: /* l modifier is ignored */
786 case FMT_MOD_NONE: expected_type = type_double; break;
787 case FMT_MOD_L: expected_type = type_long_double; break;
790 warn_invalid_length_modifier(pos, fmt_mod, fmt);
796 if (fmt_mod != FMT_MOD_NONE) {
797 warn_invalid_length_modifier(pos, fmt_mod, fmt);
800 expected_type = type_wchar_t;
804 expected_type = type_int;
806 case FMT_MOD_NONE: expected_type = type_int; break; /* TODO promoted char */
807 case FMT_MOD_l: expected_type = type_wint_t; break;
808 case FMT_MOD_w: expected_type = type_wchar_t; break;
811 warn_invalid_length_modifier(pos, fmt_mod, fmt);
817 if (fmt_mod != FMT_MOD_NONE) {
818 warn_invalid_length_modifier(pos, fmt_mod, fmt);
821 expected_type = type_wchar_t;
827 case FMT_MOD_NONE: expected_type = type_char; break;
828 case FMT_MOD_l: expected_type = type_wchar_t; break;
829 case FMT_MOD_w: expected_type = type_wchar_t; break;
832 warn_invalid_length_modifier(pos, fmt_mod, fmt);
838 if (fmt_mod != FMT_MOD_NONE) {
839 warn_invalid_length_modifier(pos, fmt_mod, fmt);
842 expected_type = type_void_ptr;
847 case FMT_MOD_NONE: expected_type = type_int; break;
848 case FMT_MOD_hh: expected_type = type_signed_char; break;
849 case FMT_MOD_h: expected_type = type_short; break;
850 case FMT_MOD_l: expected_type = type_long; break;
851 case FMT_MOD_ll: expected_type = type_long_long; break;
852 case FMT_MOD_j: expected_type = type_intmax_t; break;
853 case FMT_MOD_z: expected_type = type_ssize_t; break;
854 case FMT_MOD_t: expected_type = type_ptrdiff_t; break;
857 warn_invalid_length_modifier(pos, fmt_mod, fmt);
863 warningf(pos, "encountered unknown conversion specifier '%%%C' at position %u", (wint_t)fmt, num_fmt);
865 warningf(pos, "too few arguments for format string");
872 warningf(pos, "too few arguments for format string");
876 { /* create a scope here to prevent warning about the jump to next_arg */
877 type_t *const arg_type = arg->expression->base.type;
878 type_t *const arg_skip = skip_typeref(arg_type);
879 type_t *const expected_type_skip = skip_typeref(expected_type);
881 if (! is_type_pointer(arg_skip))
883 type_t *const ptr_skip = skip_typeref(arg_skip->pointer.points_to);
886 /* allow any pointer type for %p, not just void */
887 if (is_type_pointer(ptr_skip))
891 /* do NOT allow const or restrict, all other should be ok */
892 if (ptr_skip->base.qualifiers & (TYPE_QUALIFIER_CONST | TYPE_QUALIFIER_VOLATILE))
894 type_t *const unqual_ptr = get_unqualified_type(ptr_skip);
895 if (unqual_ptr == expected_type_skip) {
897 } else if (expected_type_skip == type_char) {
898 /* char matches with unsigned char AND signed char */
899 if (unqual_ptr == type_signed_char || unqual_ptr == type_unsigned_char)
903 if (is_type_valid(arg_skip)) {
905 "argument type '%T' does not match conversion specifier '%%%s%c' at position %u",
906 arg_type, get_length_modifier_name(fmt_mod), (char)fmt, num_fmt);
912 if (!atend(&vchar)) {
913 warningf(pos, "format string contains '\\0'");
916 unsigned num_args = num_fmt;
917 while (arg != NULL) {
921 warningf(pos, "%u argument%s but only %u format specifier%s",
922 num_args, num_args != 1 ? "s" : "",
923 num_fmt, num_fmt != 1 ? "s" : "");
927 static const format_spec_t builtin_table[] = {
928 { "printf", FORMAT_PRINTF, 0, 1 },
929 { "wprintf", FORMAT_PRINTF, 0, 1 },
930 { "sprintf", FORMAT_PRINTF, 1, 2 },
931 { "swprintf", FORMAT_PRINTF, 1, 2 },
932 { "snprintf", FORMAT_PRINTF, 2, 3 },
933 { "snwprintf", FORMAT_PRINTF, 2, 3 },
934 { "fprintf", FORMAT_PRINTF, 1, 2 },
935 { "fwprintf", FORMAT_PRINTF, 1, 2 },
936 { "snwprintf", FORMAT_PRINTF, 2, 3 },
937 { "snwprintf", FORMAT_PRINTF, 2, 3 },
939 { "scanf", FORMAT_SCANF, 0, 1 },
940 { "wscanf", FORMAT_SCANF, 0, 1 },
941 { "sscanf", FORMAT_SCANF, 1, 2 },
942 { "swscanf", FORMAT_SCANF, 1, 2 },
943 { "fscanf", FORMAT_SCANF, 1, 2 },
944 { "fwscanf", FORMAT_SCANF, 1, 2 },
946 { "strftime", FORMAT_STRFTIME, 3, 4 },
947 { "wcstrftime", FORMAT_STRFTIME, 3, 4 },
949 { "strfmon", FORMAT_STRFMON, 3, 4 },
952 { "_snprintf", FORMAT_PRINTF, 2, 3 },
953 { "_snwprintf", FORMAT_PRINTF, 2, 3 },
954 { "_scrintf", FORMAT_PRINTF, 0, 1 },
955 { "_scwprintf", FORMAT_PRINTF, 0, 1 },
956 { "printf_s", FORMAT_PRINTF, 0, 1 },
957 { "wprintf_s", FORMAT_PRINTF, 0, 1 },
958 { "sprintf_s", FORMAT_PRINTF, 3, 4 },
959 { "swprintf_s", FORMAT_PRINTF, 3, 4 },
960 { "fprintf_s", FORMAT_PRINTF, 1, 2 },
961 { "fwprintf_s", FORMAT_PRINTF, 1, 2 },
962 { "_sprintf_l", FORMAT_PRINTF, 1, 3 },
963 { "_swprintf_l", FORMAT_PRINTF, 1, 3 },
964 { "_printf_l", FORMAT_PRINTF, 0, 2 },
965 { "_wprintf_l", FORMAT_PRINTF, 0, 2 },
966 { "_fprintf_l", FORMAT_PRINTF, 1, 3 },
967 { "_fwprintf_l", FORMAT_PRINTF, 1, 3 },
968 { "_printf_s_l", FORMAT_PRINTF, 0, 2 },
969 { "_wprintf_s_l", FORMAT_PRINTF, 0, 2 },
970 { "_sprintf_s_l", FORMAT_PRINTF, 3, 5 },
971 { "_swprintf_s_l", FORMAT_PRINTF, 3, 5 },
972 { "_fprintf_s_l", FORMAT_PRINTF, 1, 3 },
973 { "_fwprintf_s_l", FORMAT_PRINTF, 1, 3 },
976 void check_format(const call_expression_t *const call)
981 const expression_t *const func_expr = call->function;
982 if (func_expr->kind != EXPR_REFERENCE)
985 const entity_t *const entity = func_expr->reference.entity;
986 const call_argument_t * arg = call->arguments;
989 /* the declaration has a GNU format attribute, check it */
992 * For some functions we always check the format, even if it was not specified.
993 * This allows to check format even in MS mode or without header included.
995 const char *const name = entity->base.symbol->string;
996 for (size_t i = 0; i < lengthof(builtin_table); ++i) {
997 if (strcmp(name, builtin_table[i].name) == 0) {
998 switch (builtin_table[i].fmt_kind) {
1000 check_printf_format(arg, &builtin_table[i]);
1003 check_scanf_format(arg, &builtin_table[i]);
1005 case FORMAT_STRFTIME:
1006 case FORMAT_STRFMON:
1007 /* TODO: implement other cases */