2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
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.
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.
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
22 #include "format_check.h"
25 #include "diagnostic.h"
31 typedef enum format_flag_t {
33 FMT_FLAG_HASH = 1U << 0,
34 FMT_FLAG_ZERO = 1U << 1,
35 FMT_FLAG_MINUS = 1U << 2,
36 FMT_FLAG_SPACE = 1U << 3,
37 FMT_FLAG_PLUS = 1U << 4,
38 FMT_FLAG_TICK = 1U << 5
41 typedef unsigned format_flags_t;
43 typedef enum format_length_modifier_t {
54 } format_length_modifier_t;
56 static const char* get_length_modifier_name(const format_length_modifier_t mod)
58 static const char* const names[] = {
70 assert(mod < sizeof(names) / sizeof(*names));
74 static void warn_invalid_length_modifier(const source_position_t pos,
75 const format_length_modifier_t mod,
76 const wchar_rep_t conversion)
79 "invalid length modifier '%s' for conversion specifier '%%%c'",
80 get_length_modifier_name(mod), conversion
84 static void check_format_arguments(const call_argument_t *const fmt_arg, const call_argument_t* arg)
86 const expression_t *fmt_expr = fmt_arg->expression;
87 if (fmt_expr->kind == EXPR_UNARY_CAST_IMPLICIT) {
88 fmt_expr = fmt_expr->unary.value;
91 if (fmt_expr->kind != EXPR_WIDE_STRING_LITERAL)
94 const source_position_t pos = fmt_expr->base.source_position;
95 const wide_string_t *const wstring = &fmt_expr->wide_string.value;
96 const wchar_rep_t *fmt = wstring->begin;
97 for (; *fmt != '\0'; ++fmt) {
105 format_flags_t fmt_flags = FMT_FLAG_NONE;
108 fmt_flags |= FMT_FLAG_ZERO;
111 /* argument selector or minimum field width */
112 if (iswdigit(*fmt)) {
115 } while (iswdigit(*fmt));
117 /* digit string was ... */
119 /* ... argument selector */
120 fmt_flags = FMT_FLAG_NONE; /* reset possibly set 0-flag */
124 /* ... minimum field width */
130 case '#': flag = FMT_FLAG_HASH; break;
131 case '0': flag = FMT_FLAG_ZERO; break;
132 case '-': flag = FMT_FLAG_MINUS; break;
133 case '\'': flag = FMT_FLAG_TICK; break;
136 if (fmt_flags & FMT_FLAG_PLUS) {
137 warningf(pos, "' ' is overridden by prior '+' in conversion specification");
139 flag = FMT_FLAG_SPACE;
143 if (fmt_flags & FMT_FLAG_SPACE) {
144 warningf(pos, "'+' overrides prior ' ' in conversion specification");
146 flag = FMT_FLAG_PLUS;
149 default: goto break_fmt_flags;
151 if (fmt_flags & flag) {
152 warningf(pos, "repeated flag '%c' in conversion specification", (char)*fmt);
159 /* minimum field width */
162 warningf(pos, "missing argument for '*' field width in conversion specification");
165 const type_t *const arg_type = arg->expression->base.type;
166 if (arg_type != type_int) {
167 warningf(pos, "argument for '*' field width in conversion specification is not an 'int', but an '%T'", arg_type);
171 while (iswdigit(*fmt)) {
182 warningf(pos, "missing argument for '*' precision in conversion specification");
185 const type_t *const arg_type = arg->expression->base.type;
186 if (arg_type != type_int) {
187 warningf(pos, "argument for '*' precision in conversion specification is not an 'int', but an '%T'", arg_type);
191 /* digit string may be omitted */
192 while (iswdigit(*fmt)) {
198 /* length modifier */
199 format_length_modifier_t fmt_mod;
205 fmt_mod = FMT_MOD_hh;
215 fmt_mod = FMT_MOD_ll;
221 case 'L': ++fmt; fmt_mod = FMT_MOD_L; break;
222 case 'j': ++fmt; fmt_mod = FMT_MOD_j; break;
223 case 't': ++fmt; fmt_mod = FMT_MOD_t; break;
224 case 'z': ++fmt; fmt_mod = FMT_MOD_z; break;
225 case 'q': ++fmt; fmt_mod = FMT_MOD_q; break;
226 default: fmt_mod = FMT_MOD_NONE; break;
230 warningf(pos, "dangling %% in format string");
234 const type_t *expected_type;
235 type_qualifiers_t expected_qual = TYPE_QUALIFIER_NONE;
236 format_flags_t allowed_flags;
241 case FMT_MOD_NONE: expected_type = type_int; break;
242 case FMT_MOD_hh: expected_type = type_int; break; /* TODO promoted signed char */
243 case FMT_MOD_h: expected_type = type_int; break; /* TODO promoted short */
244 case FMT_MOD_l: expected_type = type_long; break;
245 case FMT_MOD_ll: expected_type = type_long_long; break;
246 case FMT_MOD_j: expected_type = type_intmax_t; break;
247 case FMT_MOD_z: expected_type = type_ssize_t; break;
248 case FMT_MOD_t: expected_type = type_ptrdiff_t; break;
251 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
254 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_SPACE | FMT_FLAG_PLUS | FMT_FLAG_ZERO;
260 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_HASH | FMT_FLAG_ZERO;
261 goto eval_fmt_mod_unsigned;
265 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_ZERO;
266 eval_fmt_mod_unsigned:
268 case FMT_MOD_NONE: expected_type = type_unsigned_int; break;
269 case FMT_MOD_hh: expected_type = type_int; break; /* TODO promoted unsigned char */
270 case FMT_MOD_h: expected_type = type_int; break; /* TODO promoted unsigned short */
271 case FMT_MOD_l: expected_type = type_unsigned_long; break;
272 case FMT_MOD_ll: expected_type = type_unsigned_long_long; break;
273 case FMT_MOD_j: expected_type = type_uintmax_t; break;
274 case FMT_MOD_z: expected_type = type_size_t; break;
275 case FMT_MOD_t: expected_type = type_uptrdiff_t; break;
278 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
292 case FMT_MOD_l: /* l modifier is ignored */
293 case FMT_MOD_NONE: expected_type = type_double; break;
294 case FMT_MOD_L: expected_type = type_long_double; break;
297 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
300 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_SPACE | FMT_FLAG_PLUS | FMT_FLAG_HASH | FMT_FLAG_ZERO;
304 if (fmt_mod != FMT_MOD_NONE) {
305 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
308 expected_type = type_wchar_t;
309 allowed_flags = FMT_FLAG_NONE;
313 expected_type = type_int;
315 case FMT_MOD_NONE: expected_type = type_int; break; /* TODO promoted char */
316 case FMT_MOD_l: expected_type = type_wint_t; break;
319 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
322 allowed_flags = FMT_FLAG_NONE;
326 if (fmt_mod != FMT_MOD_NONE) {
327 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
330 expected_type = type_wchar_t_ptr;
331 expected_qual = TYPE_QUALIFIER_CONST;
332 allowed_flags = FMT_FLAG_NONE;
337 case FMT_MOD_NONE: expected_type = type_char_ptr; break;
338 case FMT_MOD_l: expected_type = type_wchar_t_ptr; break;
341 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
344 expected_qual = TYPE_QUALIFIER_CONST;
345 allowed_flags = FMT_FLAG_NONE;
349 if (fmt_mod != FMT_MOD_NONE) {
350 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
353 expected_type = type_void_ptr;
354 allowed_flags = FMT_FLAG_NONE;
359 case FMT_MOD_NONE: expected_type = type_int_ptr; break;
360 case FMT_MOD_hh: expected_type = type_signed_char_ptr; break;
361 case FMT_MOD_h: expected_type = type_short_ptr; break;
362 case FMT_MOD_l: expected_type = type_long_ptr; break;
363 case FMT_MOD_ll: expected_type = type_long_long_ptr; break;
364 case FMT_MOD_j: expected_type = type_intmax_t_ptr; break;
365 case FMT_MOD_z: expected_type = type_ssize_t_ptr; break;
366 case FMT_MOD_t: expected_type = type_ptrdiff_t_ptr; break;
369 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
372 allowed_flags = FMT_FLAG_NONE;
376 warningf(pos, "encountered unknown conversion specifier '%%%C'", (wint_t)*fmt);
380 if ((fmt_flags & ~allowed_flags) != 0) {
381 /* TODO better warning message text */
382 warningf(pos, "invalid format flags in conversion specification");
386 warningf(pos, "too few arguments for format string");
390 { /* create a scope here to prevent warning about the jump to next_arg */
391 type_t *const arg_type = arg->expression->base.type;
392 if (is_type_pointer(expected_type)) {
393 type_t *const arg_skip = skip_typeref(arg_type);
394 if (is_type_pointer(arg_skip)) {
395 type_t *const exp_to = skip_typeref(expected_type->pointer.points_to);
396 type_t *const arg_to = skip_typeref(arg_skip->pointer.points_to);
397 if ((arg_to->base.qualifiers & ~expected_qual) == 0 &&
398 get_unqualified_type(arg_to) == exp_to) {
403 if (get_unqualified_type(skip_typeref(arg_type)) == expected_type) {
407 if (is_type_valid(arg_type)) {
409 "argument type '%T' does not match conversion specifier '%%%s%c'",
410 arg_type, get_length_modifier_name(fmt_mod), (char)*fmt);
416 if (fmt + 1 != wstring->begin + wstring->size) {
417 warningf(pos, "format string contains NUL");
420 warningf(pos, "too many arguments for format string");
424 void check_format(const call_expression_t *const call)
426 if (!warning.check_format)
429 const expression_t *const func_expr = call->function;
430 if (func_expr->kind != EXPR_REFERENCE)
433 const char *const name = func_expr->reference.symbol->string;
434 const call_argument_t * arg = call->arguments;
435 if (strcmp(name, "wprintf") == 0) { /* TODO gammlig */
436 check_format_arguments(arg, arg->next);
437 } else if (strcmp(name, "swprintf") == 0) {
438 arg = arg->next->next; /* skip destination buffer and size */
439 check_format_arguments(arg, arg->next);