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
23 #include "diagnostic.h"
24 #include "format_check.h"
30 typedef enum format_flag_t {
32 FMT_FLAG_HASH = 1U << 0,
33 FMT_FLAG_ZERO = 1U << 1,
34 FMT_FLAG_MINUS = 1U << 2,
35 FMT_FLAG_SPACE = 1U << 3,
36 FMT_FLAG_PLUS = 1U << 4,
37 FMT_FLAG_TICK = 1U << 5
40 typedef unsigned format_flags_t;
42 typedef enum format_length_modifier_t {
53 } format_length_modifier_t;
55 static const char* get_length_modifier_name(const format_length_modifier_t mod)
57 static const char* const names[] = {
69 assert(mod < sizeof(names) / sizeof(*names));
73 static void warn_invalid_length_modifier(const source_position_t pos,
74 const format_length_modifier_t mod,
75 const wchar_rep_t conversion)
78 "invalid length modifier '%s' for conversion specifier '%%%c'",
79 get_length_modifier_name(mod), conversion
83 static void check_format_arguments(const call_argument_t *const fmt_arg, const call_argument_t* arg)
85 const expression_t *fmt_expr = fmt_arg->expression;
86 if (fmt_expr->kind == EXPR_UNARY_CAST_IMPLICIT) {
87 fmt_expr = fmt_expr->unary.value;
90 if (fmt_expr->kind != EXPR_WIDE_STRING_LITERAL)
93 const source_position_t pos = fmt_expr->base.source_position;
94 const wide_string_t *const wstring = &fmt_expr->wide_string.value;
95 const wchar_rep_t *fmt = wstring->begin;
96 for (; *fmt != '\0'; ++fmt) {
104 format_flags_t fmt_flags = FMT_FLAG_NONE;
107 fmt_flags |= FMT_FLAG_ZERO;
110 /* argument selector or minimum field width */
111 if (iswdigit(*fmt)) {
114 } while (iswdigit(*fmt));
116 /* digit string was ... */
118 /* ... argument selector */
119 fmt_flags = FMT_FLAG_NONE; /* reset possibly set 0-flag */
123 /* ... minimum field width */
129 case '#': flag = FMT_FLAG_HASH; break;
130 case '0': flag = FMT_FLAG_ZERO; break;
131 case '-': flag = FMT_FLAG_MINUS; break;
132 case '\'': flag = FMT_FLAG_TICK; break;
135 if (fmt_flags & FMT_FLAG_PLUS) {
136 warningf(pos, "' ' is overridden by prior '+' in conversion specification");
138 flag = FMT_FLAG_SPACE;
142 if (fmt_flags & FMT_FLAG_SPACE) {
143 warningf(pos, "'+' overrides prior ' ' in conversion specification");
145 flag = FMT_FLAG_PLUS;
148 default: goto break_fmt_flags;
150 if (fmt_flags & flag) {
151 warningf(pos, "repeated flag '%c' in conversion specification", (char)*fmt);
158 /* minimum field width */
161 warningf(pos, "missing argument for '*' field width in conversion specification");
164 const type_t *const arg_type = arg->expression->base.type;
165 if (arg_type != type_int) {
166 warningf(pos, "argument for '*' field width in conversion specification is not an 'int', but an '%T'", arg_type);
170 while (iswdigit(*fmt)) {
181 warningf(pos, "missing argument for '*' precision in conversion specification");
184 const type_t *const arg_type = arg->expression->base.type;
185 if (arg_type != type_int) {
186 warningf(pos, "argument for '*' precision in conversion specification is not an 'int', but an '%T'", arg_type);
190 /* digit string may be omitted */
191 while (iswdigit(*fmt)) {
197 /* length modifier */
198 format_length_modifier_t fmt_mod;
204 fmt_mod = FMT_MOD_hh;
214 fmt_mod = FMT_MOD_ll;
220 case 'L': ++fmt; fmt_mod = FMT_MOD_L; break;
221 case 'j': ++fmt; fmt_mod = FMT_MOD_j; break;
222 case 't': ++fmt; fmt_mod = FMT_MOD_t; break;
223 case 'z': ++fmt; fmt_mod = FMT_MOD_z; break;
224 case 'q': ++fmt; fmt_mod = FMT_MOD_q; break;
225 default: fmt_mod = FMT_MOD_NONE; break;
229 warningf(pos, "dangling %% in format string");
233 const type_t *expected_type;
234 type_qualifiers_t expected_qual = TYPE_QUALIFIER_NONE;
235 format_flags_t allowed_flags;
240 case FMT_MOD_NONE: expected_type = type_int; break;
241 case FMT_MOD_hh: expected_type = type_int; break; /* TODO promoted signed char */
242 case FMT_MOD_h: expected_type = type_int; break; /* TODO promoted short */
243 case FMT_MOD_l: expected_type = type_long; break;
244 case FMT_MOD_ll: expected_type = type_long_long; break;
245 case FMT_MOD_j: expected_type = type_intmax_t; break;
246 case FMT_MOD_z: expected_type = type_ssize_t; break;
247 case FMT_MOD_t: expected_type = type_ptrdiff_t; break;
250 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
253 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_SPACE | FMT_FLAG_PLUS | FMT_FLAG_ZERO;
259 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_HASH | FMT_FLAG_ZERO;
260 goto eval_fmt_mod_unsigned;
264 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_ZERO;
265 eval_fmt_mod_unsigned:
267 case FMT_MOD_NONE: expected_type = type_unsigned_int; break;
268 case FMT_MOD_hh: expected_type = type_int; break; /* TODO promoted unsigned char */
269 case FMT_MOD_h: expected_type = type_int; break; /* TODO promoted unsigned short */
270 case FMT_MOD_l: expected_type = type_unsigned_long; break;
271 case FMT_MOD_ll: expected_type = type_unsigned_long_long; break;
272 case FMT_MOD_j: expected_type = type_uintmax_t; break;
273 case FMT_MOD_z: expected_type = type_size_t; break;
274 case FMT_MOD_t: expected_type = type_uptrdiff_t; break;
277 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
291 case FMT_MOD_l: /* l modifier is ignored */
292 case FMT_MOD_NONE: expected_type = type_double; break;
293 case FMT_MOD_L: expected_type = type_long_double; break;
296 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
299 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_SPACE | FMT_FLAG_PLUS | FMT_FLAG_HASH | FMT_FLAG_ZERO;
303 if (fmt_mod != FMT_MOD_NONE) {
304 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
307 expected_type = type_wchar_t;
308 allowed_flags = FMT_FLAG_NONE;
312 expected_type = type_int;
314 case FMT_MOD_NONE: expected_type = type_int; break; /* TODO promoted char */
315 case FMT_MOD_l: expected_type = type_wint_t; break;
318 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
321 allowed_flags = FMT_FLAG_NONE;
325 if (fmt_mod != FMT_MOD_NONE) {
326 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
329 expected_type = type_wchar_t_ptr;
330 expected_qual = TYPE_QUALIFIER_CONST;
331 allowed_flags = FMT_FLAG_NONE;
336 case FMT_MOD_NONE: expected_type = type_char_ptr; break;
337 case FMT_MOD_l: expected_type = type_wchar_t_ptr; break;
340 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
343 expected_qual = TYPE_QUALIFIER_CONST;
344 allowed_flags = FMT_FLAG_NONE;
348 if (fmt_mod != FMT_MOD_NONE) {
349 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
352 expected_type = type_void_ptr;
353 allowed_flags = FMT_FLAG_NONE;
358 case FMT_MOD_NONE: expected_type = type_int_ptr; break;
359 case FMT_MOD_hh: expected_type = type_signed_char_ptr; break;
360 case FMT_MOD_h: expected_type = type_short_ptr; break;
361 case FMT_MOD_l: expected_type = type_long_ptr; break;
362 case FMT_MOD_ll: expected_type = type_long_long_ptr; break;
363 case FMT_MOD_j: expected_type = type_intmax_t_ptr; break;
364 case FMT_MOD_z: expected_type = type_ssize_t_ptr; break;
365 case FMT_MOD_t: expected_type = type_ptrdiff_t_ptr; break;
368 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
371 allowed_flags = FMT_FLAG_NONE;
375 warningf(pos, "encountered unknown conversion specifier '%%%C'", (wint_t)*fmt);
379 if ((fmt_flags & ~allowed_flags) != 0) {
380 /* TODO better warning message text */
381 warningf(pos, "invalid format flags in conversion specification");
385 warningf(pos, "too few arguments for format string");
389 { /* create a scope here to prevent warning about the jump to next_arg */
390 type_t *const arg_type = arg->expression->base.type;
391 if (is_type_pointer(expected_type)) {
392 type_t *const arg_skip = skip_typeref(arg_type);
393 if (is_type_pointer(arg_skip)) {
394 type_t *const exp_to = skip_typeref(expected_type->pointer.points_to);
395 type_t *const arg_to = skip_typeref(arg_skip->pointer.points_to);
396 if ((arg_to->base.qualifiers & ~expected_qual) == 0 &&
397 get_unqualified_type(arg_to) == exp_to) {
402 if (get_unqualified_type(skip_typeref(arg_type)) == expected_type) {
406 if (is_type_valid(arg_type)) {
408 "argument type '%T' does not match conversion specifier '%%%s%c'",
409 arg_type, get_length_modifier_name(fmt_mod), (char)*fmt);
415 if (fmt + 1 != wstring->begin + wstring->size) {
416 warningf(pos, "format string contains NUL");
419 warningf(pos, "too many arguments for format string");
423 void check_format(const call_expression_t *const call)
425 if (!warning.check_format)
428 const expression_t *const func_expr = call->function;
429 if (func_expr->kind != EXPR_REFERENCE)
432 const char *const name = func_expr->reference.symbol->string;
433 const call_argument_t * arg = call->arguments;
434 if (strcmp(name, "wprintf") == 0) { /* TODO gammlig */
435 check_format_arguments(arg, arg->next);
436 } else if (strcmp(name, "swprintf") == 0) {
437 arg = arg->next->next; /* skip destination buffer and size */
438 check_format_arguments(arg, arg->next);