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