4 #include "diagnostic.h"
5 #include "format_check.h"
9 typedef enum format_flag_t {
11 FMT_FLAG_HASH = 1U << 0,
12 FMT_FLAG_ZERO = 1U << 1,
13 FMT_FLAG_MINUS = 1U << 2,
14 FMT_FLAG_SPACE = 1U << 3,
15 FMT_FLAG_PLUS = 1U << 4,
16 FMT_FLAG_TICK = 1U << 5
19 typedef unsigned format_flags_t;
21 typedef enum format_length_modifier_t {
32 } format_length_modifier_t;
34 static void warn_invalid_length_modifier(const source_position_t pos,
35 const format_length_modifier_t mod,
36 const char conversion)
38 static const char* const names[] = {
50 assert(mod < sizeof(names) / sizeof(*names));
52 parse_warning_posf(pos,
53 "invalid length modifier '%s' for conversion specifier '%%%c'",
54 names[mod], conversion
58 static void check_format_arguments(const call_argument_t *const fmt_arg, const call_argument_t* arg)
60 const expression_t *fmt_expr = fmt_arg->expression;
61 if (fmt_expr->kind == EXPR_UNARY_CAST_IMPLICIT) {
62 fmt_expr = fmt_expr->unary.value;
65 if (fmt_expr->kind != EXPR_WIDE_STRING_LITERAL)
68 const source_position_t pos = fmt_expr->base.source_position;
69 const wide_string_t *const wstring = &fmt_expr->wide_string.value;
70 const wchar_rep_t *fmt = wstring->begin;
71 for (; *fmt != '\0'; ++fmt) {
79 format_flags_t fmt_flags = FMT_FLAG_NONE;
82 fmt_flags |= FMT_FLAG_ZERO;
85 /* argument selector or minimum field width */
89 } while (iswdigit(*fmt));
91 /* digit string was ... */
93 /* ... argument selector */
94 fmt_flags = FMT_FLAG_NONE; /* reset possibly set 0-flag */
98 /* ... minimum field width */
104 case '#': flag = FMT_FLAG_HASH; break;
105 case '0': flag = FMT_FLAG_ZERO; break;
106 case '-': flag = FMT_FLAG_MINUS; break;
107 case '\'': flag = FMT_FLAG_TICK; break;
110 if (fmt_flags & FMT_FLAG_PLUS) {
111 parse_warning_pos(pos, "' ' is overridden by prior '+' in conversion specification");
113 flag = FMT_FLAG_SPACE;
117 if (fmt_flags & FMT_FLAG_SPACE) {
118 parse_warning_pos(pos, "'+' overrides prior ' ' in conversion specification");
120 flag = FMT_FLAG_PLUS;
123 default: goto break_fmt_flags;
125 if (fmt_flags & flag) {
126 parse_warning_posf(pos, "repeated flag '%c' in conversion specification", (char)*fmt);
133 /* minimum field width */
136 parse_warning_pos(pos, "missing argument for '*' field width in conversion specification");
139 const type_t *const arg_type = arg->expression->base.datatype;
140 if (arg_type != type_int) {
141 parse_warning_pos(pos, "argument for '*' field width in conversion specification is not an 'int', but an '");
142 print_type(arg_type);
147 while (iswdigit(*fmt)) {
158 parse_warning_pos(pos, "missing argument for '*' precision in conversion specification");
161 const type_t *const arg_type = arg->expression->base.datatype;
162 if (arg_type != type_int) {
163 parse_warning_pos(pos, "argument for '*' precision in conversion specification is not an 'int', but an '");
164 print_type(arg_type);
169 /* digit string may be omitted */
170 while (iswdigit(*fmt)) {
176 /* length modifier */
177 format_length_modifier_t fmt_mod;
183 fmt_mod = FMT_MOD_hh;
193 fmt_mod = FMT_MOD_ll;
199 case 'L': ++fmt; fmt_mod = FMT_MOD_L; break;
200 case 'j': ++fmt; fmt_mod = FMT_MOD_j; break;
201 case 't': ++fmt; fmt_mod = FMT_MOD_t; break;
202 case 'z': ++fmt; fmt_mod = FMT_MOD_z; break;
203 case 'q': ++fmt; fmt_mod = FMT_MOD_q; break;
204 default: fmt_mod = FMT_MOD_NONE; break;
208 parse_warning_pos(pos, "dangling % in format string");
212 const type_t *expected_type;
213 format_flags_t allowed_flags;
218 case FMT_MOD_NONE: expected_type = type_int; break;
219 case FMT_MOD_hh: expected_type = type_int; break; /* TODO promoted signed char */
220 case FMT_MOD_h: expected_type = type_int; break; /* TODO promoted short */
221 case FMT_MOD_l: expected_type = type_long; break;
222 case FMT_MOD_ll: expected_type = type_long_long; break;
223 case FMT_MOD_j: expected_type = type_intmax_t; break;
224 case FMT_MOD_z: expected_type = type_ssize_t; break;
225 case FMT_MOD_t: expected_type = type_ptrdiff_t; break;
228 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
231 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_PLUS | FMT_FLAG_ZERO;
237 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_HASH | FMT_FLAG_ZERO;
238 goto eval_fmt_mod_unsigned;
242 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_ZERO;
243 eval_fmt_mod_unsigned:
245 case FMT_MOD_NONE: expected_type = type_unsigned_int; break;
246 case FMT_MOD_hh: expected_type = type_int; break; /* TODO promoted unsigned char */
247 case FMT_MOD_h: expected_type = type_int; break; /* TODO promoted unsigned short */
248 case FMT_MOD_l: expected_type = type_unsigned_long; break;
249 case FMT_MOD_ll: expected_type = type_unsigned_long_long; break;
250 case FMT_MOD_j: expected_type = type_uintmax_t; break;
251 case FMT_MOD_z: expected_type = type_size_t; break;
252 case FMT_MOD_t: expected_type = type_uptrdiff_t; break;
255 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
269 case FMT_MOD_l: /* l modifier is ignored */
270 case FMT_MOD_NONE: expected_type = type_double; break;
271 case FMT_MOD_L: expected_type = type_long_double; break;
274 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
277 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_PLUS | FMT_FLAG_HASH | FMT_FLAG_ZERO;
281 if (fmt_mod != FMT_MOD_NONE) {
282 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
284 expected_type = type_wchar_t;
285 allowed_flags = FMT_FLAG_NONE;
289 expected_type = type_int;
291 case FMT_MOD_NONE: expected_type = type_int; break; /* TODO promoted char */
292 case FMT_MOD_l: expected_type = type_wint_t; break;
295 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
298 allowed_flags = FMT_FLAG_NONE;
302 if (fmt_mod != FMT_MOD_NONE) {
303 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
305 expected_type = type_wchar_t_ptr;
306 allowed_flags = FMT_FLAG_NONE;
311 case FMT_MOD_NONE: expected_type = type_string; break;
312 case FMT_MOD_l: expected_type = type_wchar_t_ptr; break;
315 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
318 allowed_flags = FMT_FLAG_NONE;
322 if (fmt_mod != FMT_MOD_NONE) {
323 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
325 expected_type = type_void_ptr;
326 allowed_flags = FMT_FLAG_NONE;
331 case FMT_MOD_NONE: expected_type = type_int_ptr; break;
332 case FMT_MOD_hh: expected_type = type_signed_char_ptr; break;
333 case FMT_MOD_h: expected_type = type_short_ptr; break;
334 case FMT_MOD_l: expected_type = type_long_ptr; break;
335 case FMT_MOD_ll: expected_type = type_long_long_ptr; break;
336 case FMT_MOD_j: expected_type = type_intmax_t_ptr; break;
337 case FMT_MOD_z: expected_type = type_ssize_t_ptr; break;
338 case FMT_MOD_t: expected_type = type_ptrdiff_t_ptr; break;
341 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
344 allowed_flags = FMT_FLAG_NONE;
348 parse_warning_posf(pos, "encountered unknown conversion specifier '%%%C'", (wint_t)*fmt);
353 if ((fmt_flags & ~allowed_flags) != 0) {
354 /* TODO better warning message text */
355 parse_warning_pos(pos, "invalid format flags in conversion specification");
359 parse_warning_pos(pos, "too few arguments for format string");
363 const type_t *const arg_type = arg->expression->base.datatype;
364 if (arg_type != expected_type) {
365 parser_print_warning_prefix_pos(pos);
366 fputs("argument type '", stderr);
367 print_type(arg_type);
368 fprintf(stderr, "' does not match conversion specifier '%%%c'\n", (char)*fmt);
373 if (fmt + 1 != wstring->begin + wstring->size) {
374 parse_warning_pos(pos, "format string contains NUL");
377 parse_warning_pos(pos, "too many arguments for format string");
381 void check_format(const call_expression_t *const call)
383 const expression_t *const func_expr = call->function;
384 if (func_expr->kind != EXPR_REFERENCE)
387 const char *const name = func_expr->reference.symbol->string;
388 const call_argument_t * arg = call->arguments;
389 if (strcmp(name, "wprintf") == 0) { /* TODO gammlig */
390 check_format_arguments(arg, arg->next);
391 } else if (strcmp(name, "swprintf") == 0) {
392 arg = arg->next->next; /* skip destination buffer and size */
393 check_format_arguments(arg, arg->next);