Add the macro lengthof().
[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 "adt/util.h"
24 #include "format_check.h"
25 #include "symbol_t.h"
26 #include "ast_t.h"
27 #include "entity_t.h"
28 #include "diagnostic.h"
29 #include "types.h"
30 #include "type_t.h"
31 #include "warning.h"
32 #include "lang_features.h"
33
34 typedef enum format_flag_t {
35         FMT_FLAG_NONE  = 0,
36         FMT_FLAG_HASH  = 1U << 0,
37         FMT_FLAG_ZERO  = 1U << 1,
38         FMT_FLAG_MINUS = 1U << 2,
39         FMT_FLAG_SPACE = 1U << 3,
40         FMT_FLAG_PLUS  = 1U << 4,
41         FMT_FLAG_TICK  = 1U << 5
42 } format_flag_t;
43
44 typedef unsigned format_flags_t;
45
46 typedef enum format_length_modifier_t {
47         FMT_MOD_NONE,
48         FMT_MOD_L,
49         FMT_MOD_hh,
50         FMT_MOD_h,
51         FMT_MOD_l,
52         FMT_MOD_ll,
53         FMT_MOD_j,
54         FMT_MOD_t,
55         FMT_MOD_z,
56         FMT_MOD_q,
57         /* only in microsoft mode */
58         FMT_MOD_w,
59         FMT_MOD_I,
60         FMT_MOD_I32,
61         FMT_MOD_I64
62 } format_length_modifier_t;
63
64 typedef struct format_spec_t {
65         const char    *name;     /**< name of the function */
66         format_kind_t  fmt_kind; /**< kind */
67         unsigned       fmt_idx;  /**< index of the format string */
68         unsigned       arg_idx;  /**< index of the first argument */
69 } format_spec_t;
70
71 static const char* get_length_modifier_name(const format_length_modifier_t mod)
72 {
73         static const char* const names[] = {
74                 [FMT_MOD_NONE] = "",
75                 [FMT_MOD_L]    = "L",
76                 [FMT_MOD_hh]   = "hh",
77                 [FMT_MOD_h]    = "h",
78                 [FMT_MOD_l]    = "l",
79                 [FMT_MOD_ll]   = "ll",
80                 [FMT_MOD_j]    = "j",
81                 [FMT_MOD_t]    = "t",
82                 [FMT_MOD_z]    = "z",
83                 [FMT_MOD_q]    = "q",
84                 /* only in microsoft mode */
85                 [FMT_MOD_w]    = "w",
86                 [FMT_MOD_I]    = "I",
87                 [FMT_MOD_I32]  = "I32",
88                 [FMT_MOD_I64]  = "I64"
89         };
90         assert(mod < lengthof(names));
91         return names[mod];
92 }
93
94 static void warn_invalid_length_modifier(const source_position_t *pos,
95                                          const format_length_modifier_t mod,
96                                          const wchar_rep_t conversion)
97 {
98         warningf(pos,
99                 "invalid length modifier '%s' for conversion specifier '%%%c'",
100                 get_length_modifier_name(mod), conversion
101         );
102 }
103
104 typedef struct vchar_t vchar_t;
105 struct vchar_t {
106         const void *string;   /**< the string */
107         size_t     position;  /**< current position */
108         size_t     size;      /**< size of the string */
109
110         /** return the first character of the string and setthe position to 0. */
111         unsigned (*first)(vchar_t *self);
112         /** return the next character of the string */
113         unsigned (*next)(vchar_t *self);
114         /** return non_zero if the given character is a digit */
115         int (*is_digit)(unsigned vchar);
116 };
117
118 static unsigned string_first(vchar_t *self) {
119         self->position = 0;
120         const string_t *string = self->string;
121         return string->begin[0];
122 }
123
124 static unsigned string_next(vchar_t *self) {
125         ++self->position;
126         const string_t *string = self->string;
127         return string->begin[self->position];
128 }
129
130 static int string_isdigit(unsigned vchar) {
131         return isdigit(vchar);
132 }
133
134 static unsigned wstring_first(vchar_t *self) {
135         self->position = 0;
136         const wide_string_t *wstring = self->string;
137         return wstring->begin[0];
138 }
139
140 static unsigned wstring_next(vchar_t *self) {
141         ++self->position;
142         const wide_string_t *wstring = self->string;
143         return wstring->begin[self->position];
144 }
145
146 static int wstring_isdigit(unsigned vchar) {
147         return iswdigit(vchar);
148 }
149
150 static bool atend(vchar_t *self) {
151         return self->position + 1 == self->size;
152 }
153
154 /**
155  * Check printf-style format.
156  */
157 static void check_printf_format(const call_argument_t *arg, const format_spec_t *spec)
158 {
159         /* find format arg */
160         unsigned idx = 0;
161         for (; idx < spec->fmt_idx; ++idx) {
162                 if (arg == NULL)
163                         return;
164                 arg = arg->next;
165         }
166
167         const expression_t *fmt_expr = arg->expression;
168         if (fmt_expr->kind == EXPR_UNARY_CAST_IMPLICIT) {
169                 fmt_expr = fmt_expr->unary.value;
170         }
171
172         vchar_t vchar;
173         if (fmt_expr->kind == EXPR_WIDE_STRING_LITERAL) {
174                 vchar.string   = &fmt_expr->wide_string.value;
175                 vchar.size     = fmt_expr->wide_string.value.size;
176                 vchar.first    = wstring_first;
177                 vchar.next     = wstring_next;
178                 vchar.is_digit = wstring_isdigit;
179         } else if (fmt_expr->kind == EXPR_STRING_LITERAL) {
180                 vchar.string   = &fmt_expr->string.value;
181                 vchar.size     = fmt_expr->string.value.size;
182                 vchar.first    = string_first;
183                 vchar.next     = string_next;
184                 vchar.is_digit = string_isdigit;
185         } else {
186                 return;
187         }
188         /* find the real args */
189         for(; idx < spec->arg_idx && arg != NULL; ++idx)
190                 arg = arg->next;
191
192         const source_position_t *pos = &fmt_expr->base.source_position;
193         unsigned fmt     = vchar.first(&vchar);
194         unsigned num_fmt = 0;
195         for (; fmt != '\0'; fmt = vchar.next(&vchar)) {
196                 if (fmt != '%')
197                         continue;
198                 fmt = vchar.next(&vchar);
199
200                 if (fmt == '%')
201                         continue;
202
203                 ++num_fmt;
204
205                 format_flags_t fmt_flags = FMT_FLAG_NONE;
206                 if (fmt == '0') {
207                         fmt = vchar.next(&vchar);
208                         fmt_flags |= FMT_FLAG_ZERO;
209                 }
210
211                 /* argument selector or minimum field width */
212                 if (vchar.is_digit(fmt)) {
213                         do {
214                                 fmt = vchar.next(&vchar);
215                         } while (vchar.is_digit(fmt));
216
217                         /* digit string was ... */
218                         if (fmt == '$') {
219                                 /* ... argument selector */
220                                 fmt_flags = FMT_FLAG_NONE; /* reset possibly set 0-flag */
221                                 /* TODO implement */
222                                 return;
223                         }
224                         /* ... minimum field width */
225                 } else {
226                         /* flags */
227                         for (;;) {
228                                 format_flags_t flag;
229                                 switch (fmt) {
230                                         case '#':  flag = FMT_FLAG_HASH;  break;
231                                         case '0':  flag = FMT_FLAG_ZERO;  break;
232                                         case '-':  flag = FMT_FLAG_MINUS; break;
233                                         case '\'': flag = FMT_FLAG_TICK;  break;
234
235                                         case ' ':
236                                                 if (fmt_flags & FMT_FLAG_PLUS) {
237                                                         warningf(pos, "' ' is overridden by prior '+' in conversion specification %u", num_fmt);
238                                                 }
239                                                 flag = FMT_FLAG_SPACE;
240                                                 break;
241
242                                         case '+':
243                                                 if (fmt_flags & FMT_FLAG_SPACE) {
244                                                         warningf(pos, "'+' overrides prior ' ' in conversion specification %u", num_fmt);
245                                                 }
246                                                 flag = FMT_FLAG_PLUS;
247                                                 break;
248
249                                         default: goto break_fmt_flags;
250                                 }
251                                 if (fmt_flags & flag) {
252                                         warningf(pos, "repeated flag '%c' in conversion specification %u", (char)fmt, num_fmt);
253                                 }
254                                 fmt_flags |= flag;
255                                 fmt = vchar.next(&vchar);
256                         }
257 break_fmt_flags:
258
259                         /* minimum field width */
260                         if (fmt == '*') {
261                                 fmt = vchar.next(&vchar);
262                                 if (arg == NULL) {
263                                         warningf(pos, "missing argument for '*' field width in conversion specification %u", num_fmt);
264                                         return;
265                                 }
266                                 const type_t *const arg_type = arg->expression->base.type;
267                                 if (arg_type != type_int) {
268                                         warningf(pos, "argument for '*' field width in conversion specification %u is not an 'int', but an '%T'", num_fmt, arg_type);
269                                 }
270                                 arg = arg->next;
271                         } else {
272                                 while (vchar.is_digit(fmt)) {
273                                         fmt = vchar.next(&vchar);
274                                 }
275                         }
276                 }
277
278                 /* precision */
279                 if (fmt == '.') {
280                         fmt = vchar.next(&vchar);
281                         if (fmt == '*') {
282                                 fmt = vchar.next(&vchar);
283                                 if (arg == NULL) {
284                                         warningf(pos, "missing argument for '*' precision in conversion specification %u", num_fmt);
285                                         return;
286                                 }
287                                 const type_t *const arg_type = arg->expression->base.type;
288                                 if (arg_type != type_int) {
289                                         warningf(pos, "argument for '*' precision in conversion specification %u is not an 'int', but an '%T'", num_fmt, arg_type);
290                                 }
291                                 arg = arg->next;
292                         } else {
293                                 /* digit string may be omitted */
294                                 while (vchar.is_digit(fmt)) {
295                                         fmt = vchar.next(&vchar);
296                                 }
297                         }
298                 }
299
300                 /* length modifier */
301                 format_length_modifier_t fmt_mod;
302                 switch (fmt) {
303                         case 'h':
304                                 fmt = vchar.next(&vchar);
305                                 if (fmt == 'h') {
306                                         fmt = vchar.next(&vchar);
307                                         fmt_mod = FMT_MOD_hh;
308                                 } else {
309                                         fmt_mod = FMT_MOD_h;
310                                 }
311                                 break;
312
313                         case 'l':
314                                 fmt = vchar.next(&vchar);
315                                 if (fmt == 'l') {
316                                         fmt = vchar.next(&vchar);
317                                         fmt_mod = FMT_MOD_ll;
318                                 } else {
319                                         fmt_mod = FMT_MOD_l;
320                                 }
321                                 break;
322
323                         case 'L': fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_L;    break;
324                         case 'j': fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_j;    break;
325                         case 't': fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_t;    break;
326                         case 'z': fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_z;    break;
327                         case 'q': fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_q;    break;
328                         /* microsoft mode */
329                         case 'w':
330                                 if (c_mode & _MS) {
331                                         fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_w;
332                                 } else {
333                                         fmt_mod = FMT_MOD_NONE;
334                                 }
335                                 break;
336                         case 'I':
337                                 if (c_mode & _MS) {
338                                         fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_I;
339                                         if (fmt == '3') {
340                                                 fmt = vchar.next(&vchar);
341                                                 if (fmt == '2') {
342                                                         fmt = vchar.next(&vchar);
343                                                         fmt_mod = FMT_MOD_I32;
344                                                 } else {
345                                                         /* rewind */
346                                                         --vchar.position;
347                                                 }
348                                         } else if (fmt == '6') {
349                                                 fmt = vchar.next(&vchar);
350                                                 if (fmt == '4') {
351                                                         fmt = vchar.next(&vchar);
352                                                         fmt_mod = FMT_MOD_I64;
353                                                 } else {
354                                                         /* rewind */
355                                                         --vchar.position;
356                                                 }
357                                         }
358                                 } else {
359                                         fmt_mod = FMT_MOD_NONE;
360                                 }
361                                 break;
362                         default:
363                                 fmt_mod = FMT_MOD_NONE;
364                                 break;
365                 }
366
367                 if (fmt == '\0') {
368                         warningf(pos, "dangling %% in format string");
369                         break;
370                 }
371
372                 type_t            *expected_type;
373                 type_qualifiers_t  expected_qual = TYPE_QUALIFIER_NONE;
374                 format_flags_t     allowed_flags;
375                 switch (fmt) {
376                         case 'd':
377                         case 'i':
378                                 switch (fmt_mod) {
379                                         case FMT_MOD_NONE: expected_type = type_int;       break;
380                                         case FMT_MOD_hh:   expected_type = type_int;       break; /* TODO promoted signed char */
381                                         case FMT_MOD_h:    expected_type = type_int;       break; /* TODO promoted short */
382                                         case FMT_MOD_l:    expected_type = type_long;      break;
383                                         case FMT_MOD_ll:   expected_type = type_long_long; break;
384                                         case FMT_MOD_j:    expected_type = type_intmax_t;  break;
385                                         case FMT_MOD_z:    expected_type = type_ssize_t;   break;
386                                         case FMT_MOD_t:    expected_type = type_ptrdiff_t; break;
387                                         case FMT_MOD_I:    expected_type = type_ptrdiff_t; break;
388                                         case FMT_MOD_I32:  expected_type = type_int32;     break;
389                                         case FMT_MOD_I64:  expected_type = type_int64;     break;
390
391                                         default:
392                                                 warn_invalid_length_modifier(pos, fmt_mod, fmt);
393                                                 goto next_arg;
394                                 }
395                                 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_SPACE | FMT_FLAG_PLUS | FMT_FLAG_ZERO;
396                                 break;
397
398                         case 'o':
399                         case 'X':
400                         case 'x':
401                                 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_HASH | FMT_FLAG_ZERO;
402                                 goto eval_fmt_mod_unsigned;
403
404                         case 'u':
405                                 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_ZERO;
406 eval_fmt_mod_unsigned:
407                                 switch (fmt_mod) {
408                                         case FMT_MOD_NONE: expected_type = type_unsigned_int;       break;
409                                         case FMT_MOD_hh:   expected_type = type_int;                break; /* TODO promoted unsigned char */
410                                         case FMT_MOD_h:    expected_type = type_int;                break; /* TODO promoted unsigned short */
411                                         case FMT_MOD_l:    expected_type = type_unsigned_long;      break;
412                                         case FMT_MOD_ll:   expected_type = type_unsigned_long_long; break;
413                                         case FMT_MOD_j:    expected_type = type_uintmax_t;          break;
414                                         case FMT_MOD_z:    expected_type = type_size_t;             break;
415                                         case FMT_MOD_t:    expected_type = type_uptrdiff_t;         break;
416                                         case FMT_MOD_I:    expected_type = type_size_t;             break;
417                                         case FMT_MOD_I32:  expected_type = type_unsigned_int32;     break;
418                                         case FMT_MOD_I64:  expected_type = type_unsigned_int64;     break;
419
420                                         default:
421                                                 warn_invalid_length_modifier(pos, fmt_mod, fmt);
422                                                 goto next_arg;
423                                 }
424                                 break;
425
426                         case 'A':
427                         case 'a':
428                         case 'E':
429                         case 'e':
430                         case 'F':
431                         case 'f':
432                         case 'G':
433                         case 'g':
434                                 switch (fmt_mod) {
435                                         case FMT_MOD_l:    /* l modifier is ignored */
436                                         case FMT_MOD_NONE: expected_type = type_double;      break;
437                                         case FMT_MOD_L:    expected_type = type_long_double; break;
438
439                                         default:
440                                                 warn_invalid_length_modifier(pos, fmt_mod, fmt);
441                                                 goto next_arg;
442                                 }
443                                 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_SPACE | FMT_FLAG_PLUS | FMT_FLAG_HASH | FMT_FLAG_ZERO;
444                                 break;
445
446                         case 'C':
447                                 if (fmt_mod != FMT_MOD_NONE) {
448                                         warn_invalid_length_modifier(pos, fmt_mod, fmt);
449                                         goto next_arg;
450                                 }
451                                 expected_type = type_wchar_t;
452                                 allowed_flags = FMT_FLAG_NONE;
453                                 break;
454
455                         case 'c':
456                                 expected_type = type_int;
457                                 switch (fmt_mod) {
458                                         case FMT_MOD_NONE: expected_type = type_int;     break; /* TODO promoted char */
459                                         case FMT_MOD_l:    expected_type = type_wint_t;  break;
460                                         case FMT_MOD_w:    expected_type = type_wchar_t; break;
461
462                                         default:
463                                                 warn_invalid_length_modifier(pos, fmt_mod, fmt);
464                                                 goto next_arg;
465                                 }
466                                 allowed_flags = FMT_FLAG_NONE;
467                                 break;
468
469                         case 'S':
470                                 if (fmt_mod != FMT_MOD_NONE) {
471                                         warn_invalid_length_modifier(pos, fmt_mod, fmt);
472                                         goto next_arg;
473                                 }
474                                 expected_type = type_wchar_t_ptr;
475                                 expected_qual = TYPE_QUALIFIER_CONST;
476                                 allowed_flags = FMT_FLAG_MINUS;
477                                 break;
478
479                         case 's':
480                                 switch (fmt_mod) {
481                                         case FMT_MOD_NONE: expected_type = type_char_ptr;    break;
482                                         case FMT_MOD_l:    expected_type = type_wchar_t_ptr; break;
483                                         case FMT_MOD_w:    expected_type = type_wchar_t_ptr; break;
484
485                                         default:
486                                                 warn_invalid_length_modifier(pos, fmt_mod, fmt);
487                                                 goto next_arg;
488                                 }
489                                 expected_qual = TYPE_QUALIFIER_CONST;
490                                 allowed_flags = FMT_FLAG_MINUS;
491                                 break;
492
493                         case 'p':
494                                 if (fmt_mod != FMT_MOD_NONE) {
495                                         warn_invalid_length_modifier(pos, fmt_mod, fmt);
496                                         goto next_arg;
497                                 }
498                                 expected_type = type_void_ptr;
499                                 allowed_flags = FMT_FLAG_NONE;
500                                 break;
501
502                         case 'n':
503                                 switch (fmt_mod) {
504                                         case FMT_MOD_NONE: expected_type = type_int_ptr;         break;
505                                         case FMT_MOD_hh:   expected_type = type_signed_char_ptr; break;
506                                         case FMT_MOD_h:    expected_type = type_short_ptr;       break;
507                                         case FMT_MOD_l:    expected_type = type_long_ptr;        break;
508                                         case FMT_MOD_ll:   expected_type = type_long_long_ptr;   break;
509                                         case FMT_MOD_j:    expected_type = type_intmax_t_ptr;    break;
510                                         case FMT_MOD_z:    expected_type = type_ssize_t_ptr;     break;
511                                         case FMT_MOD_t:    expected_type = type_ptrdiff_t_ptr;   break;
512
513                                         default:
514                                                 warn_invalid_length_modifier(pos, fmt_mod, fmt);
515                                                 goto next_arg;
516                                 }
517                                 allowed_flags = FMT_FLAG_NONE;
518                                 break;
519
520                         default:
521                                 warningf(pos, "encountered unknown conversion specifier '%%%C' at position %u", (wint_t)fmt, num_fmt);
522                                 if (arg == NULL) {
523                                         warningf(pos, "too few arguments for format string");
524                                         return;
525                                 }
526                                 goto next_arg;
527                 }
528
529                 format_flags_t wrong_flags = fmt_flags & ~allowed_flags;
530                 if (wrong_flags != 0) {
531                         char  wrong[8];
532                         char *p = wrong;
533                         if (wrong_flags & FMT_FLAG_HASH)  *p++ = '#';
534                         if (wrong_flags & FMT_FLAG_ZERO)  *p++ = '0';
535                         if (wrong_flags & FMT_FLAG_MINUS) *p++ = '-';
536                         if (wrong_flags & FMT_FLAG_SPACE) *p++ = ' ';
537                         if (wrong_flags & FMT_FLAG_PLUS)  *p++ = '+';
538                         if (wrong_flags & FMT_FLAG_TICK)  *p++ = '\'';
539                         *p = '\0';
540
541                         warningf(pos, "invalid format flags \"%s\" in conversion specification %%%c at position %u", wrong, fmt, num_fmt);
542                 }
543
544                 if (arg == NULL) {
545                         warningf(pos, "too few arguments for format string");
546                         return;
547                 }
548
549                 {       /* create a scope here to prevent warning about the jump to next_arg */
550                         type_t *const arg_type           = arg->expression->base.type;
551                         type_t *const arg_skip           = skip_typeref(arg_type);
552                         type_t *const expected_type_skip = skip_typeref(expected_type);
553
554                         if (fmt == 'p') {
555                                 /* allow any pointer type for %p, not just void */
556                                 if (is_type_pointer(arg_skip))
557                                         goto next_arg;
558                         }
559
560                         if (is_type_pointer(expected_type_skip)) {
561                                 if (is_type_pointer(arg_skip)) {
562                                         type_t *const exp_to = skip_typeref(expected_type_skip->pointer.points_to);
563                                         type_t *const arg_to = skip_typeref(arg_skip->pointer.points_to);
564                                         if ((arg_to->base.qualifiers & ~expected_qual) == 0 &&
565                                                 get_unqualified_type(arg_to) == exp_to) {
566                                                 goto next_arg;
567                                         }
568                                 }
569                         } else {
570                                 if (get_unqualified_type(arg_skip) == expected_type_skip) {
571                                         goto next_arg;
572                                 }
573                         }
574                         if (is_type_valid(arg_skip)) {
575                                 warningf(pos,
576                                         "argument type '%T' does not match conversion specifier '%%%s%c' at position %u",
577                                         arg_type, get_length_modifier_name(fmt_mod), (char)fmt, num_fmt);
578                         }
579                 }
580 next_arg:
581                 arg = arg->next;
582         }
583         if (!atend(&vchar)) {
584                 warningf(pos, "format string contains '\\0'");
585         }
586         if (arg != NULL) {
587                 unsigned num_args = num_fmt;
588                 while (arg != NULL) {
589                         ++num_args;
590                         arg = arg->next;
591                 }
592                 warningf(pos, "%u argument%s but only %u format specifier%s",
593                         num_args, num_args != 1 ? "s" : "",
594                         num_fmt, num_fmt != 1 ? "s" : "");
595         }
596 }
597
598 /**
599  * Check scanf-style format.
600  */
601 static void check_scanf_format(const call_argument_t *arg, const format_spec_t *spec)
602 {
603         /* find format arg */
604         unsigned idx = 0;
605         for (; idx < spec->fmt_idx; ++idx) {
606                 if (arg == NULL)
607                         return;
608                 arg = arg->next;
609         }
610
611         const expression_t *fmt_expr = arg->expression;
612         if (fmt_expr->kind == EXPR_UNARY_CAST_IMPLICIT) {
613                 fmt_expr = fmt_expr->unary.value;
614         }
615
616         vchar_t vchar;
617         if (fmt_expr->kind == EXPR_WIDE_STRING_LITERAL) {
618                 vchar.string   = &fmt_expr->wide_string.value;
619                 vchar.size     = fmt_expr->wide_string.value.size;
620                 vchar.first    = wstring_first;
621                 vchar.next     = wstring_next;
622                 vchar.is_digit = wstring_isdigit;
623         } else if (fmt_expr->kind == EXPR_STRING_LITERAL) {
624                 vchar.string   = &fmt_expr->string.value;
625                 vchar.size     = fmt_expr->string.value.size;
626                 vchar.first    = string_first;
627                 vchar.next     = string_next;
628                 vchar.is_digit = string_isdigit;
629         } else {
630                 return;
631         }
632         /* find the real args */
633         for (; idx < spec->arg_idx && arg != NULL; ++idx)
634                 arg = arg->next;
635
636         const source_position_t *pos = &fmt_expr->base.source_position;
637         unsigned fmt     = vchar.first(&vchar);
638         unsigned num_fmt = 0;
639         for (; fmt != '\0'; fmt = vchar.next(&vchar)) {
640                 if (fmt != '%')
641                         continue;
642                 fmt = vchar.next(&vchar);
643
644                 if (fmt == '%')
645                         continue;
646
647                 ++num_fmt;
648
649                 /* length modifier */
650                 format_length_modifier_t fmt_mod;
651                 switch (fmt) {
652                         case 'h':
653                                 fmt = vchar.next(&vchar);
654                                 if (fmt == 'h') {
655                                         fmt = vchar.next(&vchar);
656                                         fmt_mod = FMT_MOD_hh;
657                                 } else {
658                                         fmt_mod = FMT_MOD_h;
659                                 }
660                                 break;
661
662                         case 'l':
663                                 fmt = vchar.next(&vchar);
664                                 if (fmt == 'l') {
665                                         fmt = vchar.next(&vchar);
666                                         fmt_mod = FMT_MOD_ll;
667                                 } else {
668                                         fmt_mod = FMT_MOD_l;
669                                 }
670                                 break;
671
672                         case 'L': fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_L;    break;
673                         case 'j': fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_j;    break;
674                         case 't': fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_t;    break;
675                         case 'z': fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_z;    break;
676                         /* microsoft mode */
677                         case 'w':
678                                 if (c_mode & _MS) {
679                                         fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_w;
680                                 } else {
681                                         fmt_mod = FMT_MOD_NONE;
682                                 }
683                                 break;
684                         case 'I':
685                                 if (c_mode & _MS) {
686                                         fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_I;
687                                         if (fmt == '3') {
688                                                 fmt = vchar.next(&vchar);
689                                                 if (fmt == '2') {
690                                                         fmt = vchar.next(&vchar);
691                                                         fmt_mod = FMT_MOD_I32;
692                                                 } else {
693                                                         /* rewind */
694                                                         --vchar.position;
695                                                 }
696                                         } else if (fmt == '6') {
697                                                 fmt = vchar.next(&vchar);
698                                                 if (fmt == '4') {
699                                                         fmt = vchar.next(&vchar);
700                                                         fmt_mod = FMT_MOD_I64;
701                                                 } else {
702                                                         /* rewind */
703                                                         --vchar.position;
704                                                 }
705                                         }
706                                 } else {
707                                         fmt_mod = FMT_MOD_NONE;
708                                 }
709                                 break;
710                         default:
711                                 fmt_mod = FMT_MOD_NONE;
712                                 break;
713                 }
714
715                 if (fmt == '\0') {
716                         warningf(pos, "dangling %% in format string");
717                         break;
718                 }
719
720                 type_t            *expected_type;
721                 switch (fmt) {
722                         case 'd':
723                         case 'i':
724                                 switch (fmt_mod) {
725                                         case FMT_MOD_NONE: expected_type = type_int;         break;
726                                         case FMT_MOD_hh:   expected_type = type_signed_char; break;
727                                         case FMT_MOD_h:    expected_type = type_short;       break;
728                                         case FMT_MOD_l:    expected_type = type_long;        break;
729                                         case FMT_MOD_ll:   expected_type = type_long_long;   break;
730                                         case FMT_MOD_j:    expected_type = type_intmax_t;    break;
731                                         case FMT_MOD_z:    expected_type = type_ssize_t;     break;
732                                         case FMT_MOD_t:    expected_type = type_ptrdiff_t;   break;
733                                         case FMT_MOD_I:    expected_type = type_ptrdiff_t;   break;
734                                         case FMT_MOD_I32:  expected_type = type_int32;       break;
735                                         case FMT_MOD_I64:  expected_type = type_int64;       break;
736
737                                         default:
738                                                 warn_invalid_length_modifier(pos, fmt_mod, fmt);
739                                                 goto next_arg;
740                                 }
741                                 break;
742
743                         case 'o':
744                         case 'X':
745                         case 'x':
746                                 goto eval_fmt_mod_unsigned;
747
748                         case 'u':
749 eval_fmt_mod_unsigned:
750                                 switch (fmt_mod) {
751                                         case FMT_MOD_NONE: expected_type = type_unsigned_int;       break;
752                                         case FMT_MOD_hh:   expected_type = type_unsigned_char;      break;
753                                         case FMT_MOD_h:    expected_type = type_unsigned_short;     break;
754                                         case FMT_MOD_l:    expected_type = type_unsigned_long;      break;
755                                         case FMT_MOD_ll:   expected_type = type_unsigned_long_long; break;
756                                         case FMT_MOD_j:    expected_type = type_uintmax_t;          break;
757                                         case FMT_MOD_z:    expected_type = type_size_t;             break;
758                                         case FMT_MOD_t:    expected_type = type_uptrdiff_t;         break;
759                                         case FMT_MOD_I:    expected_type = type_size_t;             break;
760                                         case FMT_MOD_I32:  expected_type = type_unsigned_int32;     break;
761                                         case FMT_MOD_I64:  expected_type = type_unsigned_int64;     break;
762
763                                         default:
764                                                 warn_invalid_length_modifier(pos, fmt_mod, fmt);
765                                                 goto next_arg;
766                                 }
767                                 break;
768
769                         case 'A':
770                         case 'a':
771                         case 'E':
772                         case 'e':
773                         case 'F':
774                         case 'f':
775                         case 'G':
776                         case 'g':
777                                 switch (fmt_mod) {
778                                         case FMT_MOD_l:    /* l modifier is ignored */
779                                         case FMT_MOD_NONE: expected_type = type_double;      break;
780                                         case FMT_MOD_L:    expected_type = type_long_double; break;
781
782                                         default:
783                                                 warn_invalid_length_modifier(pos, fmt_mod, fmt);
784                                                 goto next_arg;
785                                 }
786                                 break;
787
788                         case 'C':
789                                 if (fmt_mod != FMT_MOD_NONE) {
790                                         warn_invalid_length_modifier(pos, fmt_mod, fmt);
791                                         goto next_arg;
792                                 }
793                                 expected_type = type_wchar_t;
794                                 break;
795
796                         case 'c':
797                                 expected_type = type_int;
798                                 switch (fmt_mod) {
799                                         case FMT_MOD_NONE: expected_type = type_int;     break; /* TODO promoted char */
800                                         case FMT_MOD_l:    expected_type = type_wint_t;  break;
801                                         case FMT_MOD_w:    expected_type = type_wchar_t; break;
802
803                                         default:
804                                                 warn_invalid_length_modifier(pos, fmt_mod, fmt);
805                                                 goto next_arg;
806                                 }
807                                 break;
808
809                         case 'S':
810                                 if (fmt_mod != FMT_MOD_NONE) {
811                                         warn_invalid_length_modifier(pos, fmt_mod, fmt);
812                                         goto next_arg;
813                                 }
814                                 expected_type = type_wchar_t;
815                                 break;
816
817                         case 's':
818                         case '[':
819                                 switch (fmt_mod) {
820                                         case FMT_MOD_NONE: expected_type = type_char;    break;
821                                         case FMT_MOD_l:    expected_type = type_wchar_t; break;
822                                         case FMT_MOD_w:    expected_type = type_wchar_t; break;
823
824                                         default:
825                                                 warn_invalid_length_modifier(pos, fmt_mod, fmt);
826                                                 goto next_arg;
827                                 }
828                                 break;
829
830                         case 'p':
831                                 if (fmt_mod != FMT_MOD_NONE) {
832                                         warn_invalid_length_modifier(pos, fmt_mod, fmt);
833                                         goto next_arg;
834                                 }
835                                 expected_type = type_void_ptr;
836                                 break;
837
838                         case 'n':
839                                 switch (fmt_mod) {
840                                         case FMT_MOD_NONE: expected_type = type_int;         break;
841                                         case FMT_MOD_hh:   expected_type = type_signed_char; break;
842                                         case FMT_MOD_h:    expected_type = type_short;       break;
843                                         case FMT_MOD_l:    expected_type = type_long;        break;
844                                         case FMT_MOD_ll:   expected_type = type_long_long;   break;
845                                         case FMT_MOD_j:    expected_type = type_intmax_t;    break;
846                                         case FMT_MOD_z:    expected_type = type_ssize_t;     break;
847                                         case FMT_MOD_t:    expected_type = type_ptrdiff_t;   break;
848
849                                         default:
850                                                 warn_invalid_length_modifier(pos, fmt_mod, fmt);
851                                                 goto next_arg;
852                                 }
853                                 break;
854
855                         default:
856                                 warningf(pos, "encountered unknown conversion specifier '%%%C' at position %u", (wint_t)fmt, num_fmt);
857                                 if (arg == NULL) {
858                                         warningf(pos, "too few arguments for format string");
859                                         return;
860                                 }
861                                 goto next_arg;
862                 }
863
864                 if (arg == NULL) {
865                         warningf(pos, "too few arguments for format string");
866                         return;
867                 }
868
869                 {       /* create a scope here to prevent warning about the jump to next_arg */
870                         type_t *const arg_type           = arg->expression->base.type;
871                         type_t *const arg_skip           = skip_typeref(arg_type);
872                         type_t *const expected_type_skip = skip_typeref(expected_type);
873
874                         if (! is_type_pointer(arg_skip))
875                                 goto error_arg_type;
876                         type_t *const ptr_skip = skip_typeref(arg_skip->pointer.points_to);
877
878                         if (fmt == 'p') {
879                                 /* allow any pointer type for %p, not just void */
880                                 if (is_type_pointer(ptr_skip))
881                                         goto next_arg;
882                         }
883
884                         /* do NOT allow const or restrict, all other should be ok */
885                         if (ptr_skip->base.qualifiers & (TYPE_QUALIFIER_CONST | TYPE_QUALIFIER_VOLATILE))
886                                 goto error_arg_type;
887                         type_t *const unqual_ptr = get_unqualified_type(ptr_skip);
888                         if (unqual_ptr == expected_type_skip) {
889                                 goto next_arg;
890                         } else if (expected_type_skip == type_char) {
891                                 /* char matches with unsigned char AND signed char */
892                                 if (unqual_ptr == type_signed_char || unqual_ptr == type_unsigned_char)
893                                         goto next_arg;
894                         }
895 error_arg_type:
896                         if (is_type_valid(arg_skip)) {
897                                 warningf(pos,
898                                         "argument type '%T' does not match conversion specifier '%%%s%c' at position %u",
899                                         arg_type, get_length_modifier_name(fmt_mod), (char)fmt, num_fmt);
900                         }
901                 }
902 next_arg:
903                 arg = arg->next;
904         }
905         if (!atend(&vchar)) {
906                 warningf(pos, "format string contains '\\0'");
907         }
908         if (arg != NULL) {
909                 unsigned num_args = num_fmt;
910                 while (arg != NULL) {
911                         ++num_args;
912                         arg = arg->next;
913                 }
914                 warningf(pos, "%u argument%s but only %u format specifier%s",
915                         num_args, num_args != 1 ? "s" : "",
916                         num_fmt, num_fmt != 1 ? "s" : "");
917         }
918 }
919
920 static const format_spec_t builtin_table[] = {
921         { "printf",        FORMAT_PRINTF,   0, 1 },
922         { "wprintf",       FORMAT_PRINTF,   0, 1 },
923         { "sprintf",       FORMAT_PRINTF,   1, 2 },
924         { "swprintf",      FORMAT_PRINTF,   1, 2 },
925         { "snprintf",      FORMAT_PRINTF,   2, 3 },
926         { "snwprintf",     FORMAT_PRINTF,   2, 3 },
927         { "fprintf",       FORMAT_PRINTF,   1, 2 },
928         { "fwprintf",      FORMAT_PRINTF,   1, 2 },
929         { "snwprintf",     FORMAT_PRINTF,   2, 3 },
930         { "snwprintf",     FORMAT_PRINTF,   2, 3 },
931
932         { "scanf",         FORMAT_SCANF,    0, 1 },
933         { "wscanf",        FORMAT_SCANF,    0, 1 },
934         { "sscanf",        FORMAT_SCANF,    1, 2 },
935         { "swscanf",       FORMAT_SCANF,    1, 2 },
936         { "fscanf",        FORMAT_SCANF,    1, 2 },
937         { "fwscanf",       FORMAT_SCANF,    1, 2 },
938
939         { "strftime",      FORMAT_STRFTIME, 3, 4 },
940         { "wcstrftime",    FORMAT_STRFTIME, 3, 4 },
941
942         { "strfmon",       FORMAT_STRFMON,  3, 4 },
943
944         /* MS extensions */
945         { "_snprintf",     FORMAT_PRINTF,   2, 3 },
946         { "_snwprintf",    FORMAT_PRINTF,   2, 3 },
947         { "_scrintf",      FORMAT_PRINTF,   0, 1 },
948         { "_scwprintf",    FORMAT_PRINTF,   0, 1 },
949         { "printf_s",      FORMAT_PRINTF,   0, 1 },
950         { "wprintf_s",     FORMAT_PRINTF,   0, 1 },
951         { "sprintf_s",     FORMAT_PRINTF,   3, 4 },
952         { "swprintf_s",    FORMAT_PRINTF,   3, 4 },
953         { "fprintf_s",     FORMAT_PRINTF,   1, 2 },
954         { "fwprintf_s",    FORMAT_PRINTF,   1, 2 },
955         { "_sprintf_l",    FORMAT_PRINTF,   1, 3 },
956         { "_swprintf_l",   FORMAT_PRINTF,   1, 3 },
957         { "_printf_l",     FORMAT_PRINTF,   0, 2 },
958         { "_wprintf_l",    FORMAT_PRINTF,   0, 2 },
959         { "_fprintf_l",    FORMAT_PRINTF,   1, 3 },
960         { "_fwprintf_l",   FORMAT_PRINTF,   1, 3 },
961         { "_printf_s_l",   FORMAT_PRINTF,   0, 2 },
962         { "_wprintf_s_l",  FORMAT_PRINTF,   0, 2 },
963         { "_sprintf_s_l",  FORMAT_PRINTF,   3, 5 },
964         { "_swprintf_s_l", FORMAT_PRINTF,   3, 5 },
965         { "_fprintf_s_l",  FORMAT_PRINTF,   1, 3 },
966         { "_fwprintf_s_l", FORMAT_PRINTF,   1, 3 },
967 };
968
969 void check_format(const call_expression_t *const call)
970 {
971         if (!warning.format)
972                 return;
973
974         const expression_t *const func_expr = call->function;
975         if (func_expr->kind != EXPR_REFERENCE)
976                 return;
977
978         const entity_t        *const entity = func_expr->reference.entity;
979         const call_argument_t *      arg    = call->arguments;
980
981         if (false) {
982                 /* the declaration has a GNU format attribute, check it */
983         } else {
984                 /*
985                  * For some functions we always check the format, even if it was not specified.
986                  * This allows to check format even in MS mode or without header included.
987                  */
988                 const char *const name = entity->base.symbol->string;
989                 for (size_t i = 0; i < lengthof(builtin_table); ++i) {
990                         if (strcmp(name, builtin_table[i].name) == 0) {
991                                 switch (builtin_table[i].fmt_kind) {
992                                 case FORMAT_PRINTF:
993                                         check_printf_format(arg, &builtin_table[i]);
994                                         break;
995                                 case FORMAT_SCANF:
996                                         check_scanf_format(arg, &builtin_table[i]);
997                                         break;
998                                 case FORMAT_STRFTIME:
999                                 case FORMAT_STRFMON:
1000                                         /* TODO: implement other cases */
1001                                         break;
1002                                 }
1003                                 break;
1004                         }
1005                 }
1006         }
1007 }