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