fix wrong usage of idx_fmt (detected by a compiler warning)
[cparser] / format_check.c
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
4  *
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.
9  *
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.
14  *
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
18  * 02111-1307, USA.
19  */
20 #include <ctype.h>
21 #include <wctype.h>
22
23 #include "format_check.h"
24 #include "symbol_t.h"
25 #include "ast_t.h"
26 #include "diagnostic.h"
27 #include "types.h"
28 #include "type_t.h"
29 #include "warning.h"
30 #include "lang_features.h"
31
32 typedef enum format_flag_t {
33         FMT_FLAG_NONE  = 0,
34         FMT_FLAG_HASH  = 1U << 0,
35         FMT_FLAG_ZERO  = 1U << 1,
36         FMT_FLAG_MINUS = 1U << 2,
37         FMT_FLAG_SPACE = 1U << 3,
38         FMT_FLAG_PLUS  = 1U << 4,
39         FMT_FLAG_TICK  = 1U << 5
40 } format_flag_t;
41
42 typedef unsigned format_flags_t;
43
44 typedef enum format_length_modifier_t {
45         FMT_MOD_NONE,
46         FMT_MOD_L,
47         FMT_MOD_hh,
48         FMT_MOD_h,
49         FMT_MOD_l,
50         FMT_MOD_ll,
51         FMT_MOD_j,
52         FMT_MOD_t,
53         FMT_MOD_z,
54         FMT_MOD_q,
55         /* only in microsoft mode */
56         FMT_MOD_w,
57         FMT_MOD_I,
58         FMT_MOD_I32,
59         FMT_MOD_I64
60 } format_length_modifier_t;
61
62 static const char* get_length_modifier_name(const format_length_modifier_t mod)
63 {
64         static const char* const names[] = {
65                 [FMT_MOD_NONE] = "",
66                 [FMT_MOD_L]    = "L",
67                 [FMT_MOD_hh]   = "hh",
68                 [FMT_MOD_h]    = "h",
69                 [FMT_MOD_l]    = "l",
70                 [FMT_MOD_ll]   = "ll",
71                 [FMT_MOD_j]    = "j",
72                 [FMT_MOD_t]    = "t",
73                 [FMT_MOD_z]    = "z",
74                 [FMT_MOD_q]    = "q",
75                 /* only in microsoft mode */
76                 [FMT_MOD_w]    = "w",
77                 [FMT_MOD_I]    = "I",
78                 [FMT_MOD_I32]  = "I32",
79                 [FMT_MOD_I64]  = "I64"
80         };
81         assert(mod < sizeof(names) / sizeof(*names));
82         return names[mod];
83 }
84
85 static void warn_invalid_length_modifier(const source_position_t *pos,
86                                          const format_length_modifier_t mod,
87                                          const wchar_rep_t conversion)
88 {
89         warningf(pos,
90                 "invalid length modifier '%s' for conversion specifier '%%%c'",
91                 get_length_modifier_name(mod), conversion
92         );
93 }
94
95 typedef struct vchar_t vchar_t;
96 struct vchar_t {
97         const void *string;   /**< the string */
98         size_t     position;  /**< current position */
99         size_t     size;      /**< size of the string */
100
101         /** return the first character of the string and setthe position to 0. */
102         unsigned (*first)(vchar_t *self);
103         /** return the next character of the string */
104         unsigned (*next)(vchar_t *self);
105         /** return non_zero if the given character is a digit */
106         int (*is_digit)(unsigned vchar);
107 };
108
109 static unsigned string_first(vchar_t *self) {
110         self->position = 0;
111         const string_t *string = self->string;
112         return string->begin[0];
113 }
114
115 static unsigned string_next(vchar_t *self) {
116         ++self->position;
117         const string_t *string = self->string;
118         return string->begin[self->position];
119 }
120
121 static int string_isdigit(unsigned vchar) {
122         return isdigit(vchar);
123 }
124
125 static unsigned wstring_first(vchar_t *self) {
126         self->position = 0;
127         const wide_string_t *wstring = self->string;
128         return wstring->begin[0];
129 }
130
131 static unsigned wstring_next(vchar_t *self) {
132         ++self->position;
133         const wide_string_t *wstring = self->string;
134         return wstring->begin[self->position];
135 }
136
137 static int wstring_isdigit(unsigned vchar) {
138         return iswdigit(vchar);
139 }
140
141 static bool atend(vchar_t *self) {
142         return self->position + 1 == self->size;
143 }
144
145 /**
146  * Check printf-style format.
147  */
148 static void check_format_arguments(const call_argument_t *arg, unsigned idx_fmt,
149                 unsigned idx_param)
150 {
151         const call_argument_t *fmt_arg;
152         unsigned idx = 0;
153
154         /* find format arg */
155         for(idx = 0; idx < idx_fmt; ++idx)
156                 arg = arg->next;
157         fmt_arg = arg;
158
159         const expression_t *fmt_expr = fmt_arg->expression;
160         if (fmt_expr->kind == EXPR_UNARY_CAST_IMPLICIT) {
161                 fmt_expr = fmt_expr->unary.value;
162         }
163
164         vchar_t vchar;
165         if (fmt_expr->kind == EXPR_WIDE_STRING_LITERAL) {
166                 vchar.string   = &fmt_expr->wide_string.value;
167                 vchar.size     = fmt_expr->wide_string.value.size;
168                 vchar.first    = wstring_first;
169                 vchar.next     = wstring_next;
170                 vchar.is_digit = wstring_isdigit;
171         } else if (fmt_expr->kind == EXPR_STRING_LITERAL) {
172                 vchar.string   = &fmt_expr->string.value;
173                 vchar.size     = fmt_expr->string.value.size;
174                 vchar.first    = string_first;
175                 vchar.next     = string_next;
176                 vchar.is_digit = string_isdigit;
177         } else {
178                 return;
179         }
180         /* find the real args */
181         for(; idx < idx_param; ++idx)
182                 arg = arg->next;
183
184         const source_position_t *pos = &fmt_expr->base.source_position;
185         unsigned fmt = vchar.first(&vchar);
186         for (; fmt != '\0'; fmt = vchar.next(&vchar)) {
187                 if (fmt != '%')
188                         continue;
189                 fmt = vchar.next(&vchar);
190
191                 if (fmt == '%')
192                         continue;
193
194                 format_flags_t fmt_flags = FMT_FLAG_NONE;
195                 if (fmt == '0') {
196                         fmt = vchar.next(&vchar);
197                         fmt_flags |= FMT_FLAG_ZERO;
198                 }
199
200                 /* argument selector or minimum field width */
201                 if (vchar.is_digit(fmt)) {
202                         do {
203                                 fmt = vchar.next(&vchar);
204                         } while (vchar.is_digit(fmt));
205
206                         /* digit string was ... */
207                         if (fmt == '$') {
208                                 /* ... argument selector */
209                                 fmt_flags = FMT_FLAG_NONE; /* reset possibly set 0-flag */
210                                 /* TODO implement */
211                                 return;
212                         }
213                         /* ... minimum field width */
214                 } else {
215                         /* flags */
216                         for (;;) {
217                                 format_flags_t flag;
218                                 switch (fmt) {
219                                         case '#':  flag = FMT_FLAG_HASH;  break;
220                                         case '0':  flag = FMT_FLAG_ZERO;  break;
221                                         case '-':  flag = FMT_FLAG_MINUS; break;
222                                         case '\'': flag = FMT_FLAG_TICK;  break;
223
224                                         case ' ':
225                                                 if (fmt_flags & FMT_FLAG_PLUS) {
226                                                         warningf(pos, "' ' is overridden by prior '+' in conversion specification");
227                                                 }
228                                                 flag = FMT_FLAG_SPACE;
229                                                 break;
230
231                                         case '+':
232                                                 if (fmt_flags & FMT_FLAG_SPACE) {
233                                                         warningf(pos, "'+' overrides prior ' ' in conversion specification");
234                                                 }
235                                                 flag = FMT_FLAG_PLUS;
236                                                 break;
237
238                                         default: goto break_fmt_flags;
239                                 }
240                                 if (fmt_flags & flag) {
241                                         warningf(pos, "repeated flag '%c' in conversion specification", (char)fmt);
242                                 }
243                                 fmt_flags |= flag;
244                                 fmt = vchar.next(&vchar);
245                         }
246 break_fmt_flags:
247
248                         /* minimum field width */
249                         if (fmt == '*') {
250                                 if (arg == NULL) {
251                                         warningf(pos, "missing argument for '*' field width in conversion specification");
252                                         return;
253                                 }
254                                 const type_t *const arg_type = arg->expression->base.type;
255                                 if (arg_type != type_int) {
256                                         warningf(pos, "argument for '*' field width in conversion specification is not an 'int', but an '%T'", arg_type);
257                                 }
258                                 arg = arg->next;
259                         } else {
260                                 while (vchar.is_digit(fmt)) {
261                                         fmt = vchar.next(&vchar);
262                                 }
263                         }
264                 }
265
266                 /* precision */
267                 if (fmt == '.') {
268                         fmt = vchar.next(&vchar);
269                         if (fmt == '*') {
270                                 if (arg == NULL) {
271                                         warningf(pos, "missing argument for '*' precision in conversion specification");
272                                         return;
273                                 }
274                                 const type_t *const arg_type = arg->expression->base.type;
275                                 if (arg_type != type_int) {
276                                         warningf(pos, "argument for '*' precision in conversion specification is not an 'int', but an '%T'", arg_type);
277                                 }
278                                 arg = arg->next;
279                         } else {
280                                 /* digit string may be omitted */
281                                 while (vchar.is_digit(fmt)) {
282                                         fmt = vchar.next(&vchar);
283                                 }
284                         }
285                 }
286
287                 /* length modifier */
288                 format_length_modifier_t fmt_mod;
289                 switch (fmt) {
290                         case 'h':
291                                 fmt = vchar.next(&vchar);
292                                 if (fmt == 'h') {
293                                         fmt = vchar.next(&vchar);
294                                         fmt_mod = FMT_MOD_hh;
295                                 } else {
296                                         fmt_mod = FMT_MOD_h;
297                                 }
298                                 break;
299
300                         case 'l':
301                                 fmt = vchar.next(&vchar);
302                                 if (fmt == 'l') {
303                                         fmt = vchar.next(&vchar);
304                                         fmt_mod = FMT_MOD_ll;
305                                 } else {
306                                         fmt_mod = FMT_MOD_l;
307                                 }
308                                 break;
309
310                         case 'L': fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_L;    break;
311                         case 'j': fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_j;    break;
312                         case 't': fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_t;    break;
313                         case 'z': fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_z;    break;
314                         case 'q': fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_q;    break;
315                         /* microsoft mode */
316                         case 'w':
317                                 if (c_mode & _MS) {
318                                         fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_w;
319                                 } else {
320                                         fmt_mod = FMT_MOD_NONE;
321                                 }
322                                 break;
323                         case 'I':
324                                 if (c_mode & _MS) {
325                                         fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_I;
326                                         if (fmt == '3') {
327                                                 fmt = vchar.next(&vchar);
328                                                 if (fmt == '2') {
329                                                         fmt = vchar.next(&vchar);
330                                                         fmt_mod = FMT_MOD_I32;
331                                                 } else {
332                                                         /* rewind */
333                                                         --vchar.position;
334                                                 }
335                                         } else if (fmt == '6') {
336                                                 fmt = vchar.next(&vchar);
337                                                 if (fmt == '4') {
338                                                         fmt = vchar.next(&vchar);
339                                                         fmt_mod = FMT_MOD_I64;
340                                                 } else {
341                                                         /* rewind */
342                                                         --vchar.position;
343                                                 }
344                                         }
345                                 } else {
346                                         fmt_mod = FMT_MOD_NONE;
347                                 }
348                                 break;
349                         default:
350                                 fmt_mod = FMT_MOD_NONE;
351                                 break;
352                 }
353
354                 if (fmt == '\0') {
355                         warningf(pos, "dangling %% in format string");
356                         break;
357                 }
358
359                 const type_t      *expected_type;
360                 type_qualifiers_t  expected_qual = TYPE_QUALIFIER_NONE;
361                 format_flags_t     allowed_flags;
362                 switch (fmt) {
363                         case 'd':
364                         case 'i':
365                                 switch (fmt_mod) {
366                                         case FMT_MOD_NONE: expected_type = type_int;       break;
367                                         case FMT_MOD_hh:   expected_type = type_int;       break; /* TODO promoted signed char */
368                                         case FMT_MOD_h:    expected_type = type_int;       break; /* TODO promoted short */
369                                         case FMT_MOD_l:    expected_type = type_long;      break;
370                                         case FMT_MOD_ll:   expected_type = type_long_long; break;
371                                         case FMT_MOD_j:    expected_type = type_intmax_t;  break;
372                                         case FMT_MOD_z:    expected_type = type_ssize_t;   break;
373                                         case FMT_MOD_t:    expected_type = type_ptrdiff_t; break;
374                                         case FMT_MOD_I:    expected_type = type_ptrdiff_t; break;
375                                         case FMT_MOD_I32:  expected_type = type_int32;     break;
376                                         case FMT_MOD_I64:  expected_type = type_int64;     break;
377
378                                         default:
379                                                 warn_invalid_length_modifier(pos, fmt_mod, fmt);
380                                                 goto next_arg;
381                                 }
382                                 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_SPACE | FMT_FLAG_PLUS | FMT_FLAG_ZERO;
383                                 break;
384
385                         case 'o':
386                         case 'X':
387                         case 'x':
388                                 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_HASH | FMT_FLAG_ZERO;
389                                 goto eval_fmt_mod_unsigned;
390                                 break;
391
392                         case 'u':
393                                 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_ZERO;
394 eval_fmt_mod_unsigned:
395                                 switch (fmt_mod) {
396                                         case FMT_MOD_NONE: expected_type = type_unsigned_int;       break;
397                                         case FMT_MOD_hh:   expected_type = type_int;                break; /* TODO promoted unsigned char */
398                                         case FMT_MOD_h:    expected_type = type_int;                break; /* TODO promoted unsigned short */
399                                         case FMT_MOD_l:    expected_type = type_unsigned_long;      break;
400                                         case FMT_MOD_ll:   expected_type = type_unsigned_long_long; break;
401                                         case FMT_MOD_j:    expected_type = type_uintmax_t;          break;
402                                         case FMT_MOD_z:    expected_type = type_size_t;             break;
403                                         case FMT_MOD_t:    expected_type = type_uptrdiff_t;         break;
404                                         case FMT_MOD_I:    expected_type = type_size_t;             break;
405                                         case FMT_MOD_I32:  expected_type = type_unsigned_int32;     break;
406                                         case FMT_MOD_I64:  expected_type = type_unsigned_int64;     break;
407
408                                         default:
409                                                 warn_invalid_length_modifier(pos, fmt_mod, fmt);
410                                                 goto next_arg;
411                                 }
412                                 break;
413
414                         case 'A':
415                         case 'a':
416                         case 'E':
417                         case 'e':
418                         case 'F':
419                         case 'f':
420                         case 'G':
421                         case 'g':
422                                 switch (fmt_mod) {
423                                         case FMT_MOD_l:    /* l modifier is ignored */
424                                         case FMT_MOD_NONE: expected_type = type_double;      break;
425                                         case FMT_MOD_L:    expected_type = type_long_double; break;
426
427                                         default:
428                                                 warn_invalid_length_modifier(pos, fmt_mod, fmt);
429                                                 goto next_arg;
430                                 }
431                                 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_SPACE | FMT_FLAG_PLUS | FMT_FLAG_HASH | FMT_FLAG_ZERO;
432                                 break;
433
434                         case 'C':
435                                 if (fmt_mod != FMT_MOD_NONE) {
436                                         warn_invalid_length_modifier(pos, fmt_mod, fmt);
437                                         goto next_arg;
438                                 }
439                                 expected_type = type_wchar_t;
440                                 allowed_flags = FMT_FLAG_NONE;
441                                 break;
442
443                         case 'c':
444                                 expected_type = type_int;
445                                 switch (fmt_mod) {
446                                         case FMT_MOD_NONE: expected_type = type_int;     break; /* TODO promoted char */
447                                         case FMT_MOD_l:    expected_type = type_wint_t;  break;
448                                         case FMT_MOD_w:    expected_type = type_wchar_t; break;
449
450                                         default:
451                                                 warn_invalid_length_modifier(pos, fmt_mod, fmt);
452                                                 goto next_arg;
453                                 }
454                                 allowed_flags = FMT_FLAG_NONE;
455                                 break;
456
457                         case 'S':
458                                 if (fmt_mod != FMT_MOD_NONE) {
459                                         warn_invalid_length_modifier(pos, fmt_mod, fmt);
460                                         goto next_arg;
461                                 }
462                                 expected_type = type_wchar_t_ptr;
463                                 expected_qual = TYPE_QUALIFIER_CONST;
464                                 allowed_flags = FMT_FLAG_NONE;
465                                 break;
466
467                         case 's':
468                                 switch (fmt_mod) {
469                                         case FMT_MOD_NONE: expected_type = type_char_ptr;    break;
470                                         case FMT_MOD_l:    expected_type = type_wchar_t_ptr; break;
471                                         case FMT_MOD_w:    expected_type = type_wchar_t_ptr; break;
472
473                                         default:
474                                                 warn_invalid_length_modifier(pos, fmt_mod, fmt);
475                                                 goto next_arg;
476                                 }
477                                 expected_qual = TYPE_QUALIFIER_CONST;
478                                 allowed_flags = FMT_FLAG_NONE;
479                                 break;
480
481                         case 'p':
482                                 if (fmt_mod != FMT_MOD_NONE) {
483                                         warn_invalid_length_modifier(pos, fmt_mod, fmt);
484                                         goto next_arg;
485                                 }
486                                 expected_type = type_void_ptr;
487                                 allowed_flags = FMT_FLAG_NONE;
488                                 break;
489
490                         case 'n':
491                                 switch (fmt_mod) {
492                                         case FMT_MOD_NONE: expected_type = type_int_ptr;         break;
493                                         case FMT_MOD_hh:   expected_type = type_signed_char_ptr; break;
494                                         case FMT_MOD_h:    expected_type = type_short_ptr;       break;
495                                         case FMT_MOD_l:    expected_type = type_long_ptr;        break;
496                                         case FMT_MOD_ll:   expected_type = type_long_long_ptr;   break;
497                                         case FMT_MOD_j:    expected_type = type_intmax_t_ptr;    break;
498                                         case FMT_MOD_z:    expected_type = type_ssize_t_ptr;     break;
499                                         case FMT_MOD_t:    expected_type = type_ptrdiff_t_ptr;   break;
500
501                                         default:
502                                                 warn_invalid_length_modifier(pos, fmt_mod, fmt);
503                                                 goto next_arg;
504                                 }
505                                 allowed_flags = FMT_FLAG_NONE;
506                                 break;
507
508                         default:
509                                 warningf(pos, "encountered unknown conversion specifier '%%%C'", (wint_t)fmt);
510                                 goto next_arg;
511                 }
512
513                 if ((fmt_flags & ~allowed_flags) != 0) {
514                         /* TODO better warning message text */
515                         warningf(pos, "invalid format flags in conversion specification");
516                 }
517
518                 if (arg == NULL) {
519                         warningf(pos, "too few arguments for format string");
520                         return;
521                 }
522
523                 {       /* create a scope here to prevent warning about the jump to next_arg */
524                         type_t *const arg_type = arg->expression->base.type;
525                         if (is_type_pointer(expected_type)) {
526                                 type_t *const arg_skip = skip_typeref(arg_type);
527                                 if (is_type_pointer(arg_skip)) {
528                                         type_t *const exp_to = skip_typeref(expected_type->pointer.points_to);
529                                         type_t *const arg_to = skip_typeref(arg_skip->pointer.points_to);
530                                         if ((arg_to->base.qualifiers & ~expected_qual) == 0 &&
531                                                 get_unqualified_type(arg_to) == exp_to) {
532                                                 goto next_arg;
533                                         }
534                                 }
535                         } else {
536                                 if (get_unqualified_type(skip_typeref(arg_type)) == expected_type) {
537                                         goto next_arg;
538                                 }
539                         }
540                         if (is_type_valid(arg_type)) {
541                                 warningf(pos,
542                                         "argument type '%T' does not match conversion specifier '%%%s%c'",
543                                         arg_type, get_length_modifier_name(fmt_mod), (char)fmt);
544                         }
545                 }
546 next_arg:
547                 arg = arg->next;
548         }
549         if (!atend(&vchar)) {
550                 warningf(pos, "format string contains NUL");
551         }
552         if (arg != NULL) {
553                 warningf(pos, "too many arguments for format string");
554         }
555 }
556
557 static const struct {
558         const char    *name;
559         format_kind_t  fmt_kind;
560         unsigned       fmt_idx;
561         unsigned       arg_idx;
562 } builtin_table[] = {
563         { "printf",        FORMAT_PRINTF,   0, 1 },
564         { "wprintf",       FORMAT_PRINTF,   0, 1 },
565         { "sprintf",       FORMAT_PRINTF,   1, 2 },
566         { "swprintf",      FORMAT_PRINTF,   1, 2 },
567         { "snprintf",      FORMAT_PRINTF,   2, 3 },
568         { "snwprintf",     FORMAT_PRINTF,   2, 3 },
569         { "fprintf",       FORMAT_PRINTF,   1, 2 },
570         { "fwprintf",      FORMAT_PRINTF,   1, 2 },
571         { "snwprintf",     FORMAT_PRINTF,   2, 3 },
572         { "snwprintf",     FORMAT_PRINTF,   2, 3 },
573
574         { "scanf",         FORMAT_SCANF,    0, 1 },
575         { "wscanf",        FORMAT_SCANF,    0, 1 },
576         { "sscanf",        FORMAT_SCANF,    1, 2 },
577         { "swscanf",       FORMAT_SCANF,    1, 2 },
578         { "fscanf",        FORMAT_SCANF,    1, 2 },
579         { "fwscanf",       FORMAT_SCANF,    1, 2 },
580
581         { "strftime",      FORMAT_STRFTIME, 3, 4 },
582         { "wcstrftime",    FORMAT_STRFTIME, 3, 4 },
583
584         { "strfmon",       FORMAT_STRFMON,  3, 4 },
585
586         /* MS extensions */
587         { "_snprintf",     FORMAT_PRINTF,   2, 3 },
588         { "_snwprintf",    FORMAT_PRINTF,   2, 3 },
589         { "_scrintf",      FORMAT_PRINTF,   0, 1 },
590         { "_scwprintf",    FORMAT_PRINTF,   0, 1 },
591         { "printf_s",      FORMAT_PRINTF,   0, 1 },
592         { "wprintf_s",     FORMAT_PRINTF,   0, 1 },
593         { "sprintf_s",     FORMAT_PRINTF,   3, 4 },
594         { "swprintf_s",    FORMAT_PRINTF,   3, 4 },
595         { "fprintf_s",     FORMAT_PRINTF,   1, 2 },
596         { "fwprintf_s",    FORMAT_PRINTF,   1, 2 },
597         { "_sprintf_l",    FORMAT_PRINTF,   1, 3 },
598         { "_swprintf_l",   FORMAT_PRINTF,   1, 3 },
599         { "_printf_l",     FORMAT_PRINTF,   0, 2 },
600         { "_wprintf_l",    FORMAT_PRINTF,   0, 2 },
601         { "_fprintf_l",    FORMAT_PRINTF,   1, 3 },
602         { "_fwprintf_l",   FORMAT_PRINTF,   1, 3 },
603         { "_printf_s_l",   FORMAT_PRINTF,   0, 2 },
604         { "_wprintf_s_l",  FORMAT_PRINTF,   0, 2 },
605         { "_sprintf_s_l",  FORMAT_PRINTF,   3, 5 },
606         { "_swprintf_s_l", FORMAT_PRINTF,   3, 5 },
607         { "_fprintf_s_l",  FORMAT_PRINTF,   1, 3 },
608         { "_fwprintf_s_l", FORMAT_PRINTF,   1, 3 },
609 };
610
611 void check_format(const call_expression_t *const call)
612 {
613         if (!warning.format)
614                 return;
615
616         const expression_t *const func_expr = call->function;
617         if (func_expr->kind != EXPR_REFERENCE)
618                 return;
619
620         const declaration_t   *const decl = func_expr->reference.declaration;
621         const call_argument_t *      arg  = call->arguments;
622
623         if(false) {
624                 /* the declaration has a GNU format attribute, check it */
625         } else {
626                 /*
627                  * For some functions we always check the format, even if it was not specified.
628                  * This allows to check format even in MS mode or without header included.
629                  */
630                 const char            *const name = decl->symbol->string;
631                 for(size_t i = 0; i < sizeof(builtin_table) / sizeof(builtin_table[0]); ++i) {
632                         if(strcmp(name, builtin_table[i].name) == 0) {
633                                 if(builtin_table[i].fmt_kind == FORMAT_PRINTF) {
634                                         check_format_arguments(arg,
635                                                                builtin_table[i].fmt_idx,
636                                                                builtin_table[i].arg_idx);
637                                 }
638                                 break;
639                         }
640                 }
641         }
642 }