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