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