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