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