4 #include "diagnostic.h"
5 #include "format_check.h"
10 typedef enum format_flag_t {
12 FMT_FLAG_HASH = 1U << 0,
13 FMT_FLAG_ZERO = 1U << 1,
14 FMT_FLAG_MINUS = 1U << 2,
15 FMT_FLAG_SPACE = 1U << 3,
16 FMT_FLAG_PLUS = 1U << 4,
17 FMT_FLAG_TICK = 1U << 5
20 typedef unsigned format_flags_t;
22 typedef enum format_length_modifier_t {
33 } format_length_modifier_t;
35 static const char* get_length_modifier_name(const format_length_modifier_t mod)
37 static const char* const names[] = {
49 assert(mod < sizeof(names) / sizeof(*names));
53 static void warn_invalid_length_modifier(const source_position_t pos,
54 const format_length_modifier_t mod,
55 const wchar_rep_t conversion)
58 "invalid length modifier '%s' for conversion specifier '%%%c'",
59 get_length_modifier_name(mod), conversion
63 static void check_format_arguments(const call_argument_t *const fmt_arg, const call_argument_t* arg)
65 const expression_t *fmt_expr = fmt_arg->expression;
66 if (fmt_expr->kind == EXPR_UNARY_CAST_IMPLICIT) {
67 fmt_expr = fmt_expr->unary.value;
70 if (fmt_expr->kind != EXPR_WIDE_STRING_LITERAL)
73 const source_position_t pos = fmt_expr->base.source_position;
74 const wide_string_t *const wstring = &fmt_expr->wide_string.value;
75 const wchar_rep_t *fmt = wstring->begin;
76 for (; *fmt != '\0'; ++fmt) {
84 format_flags_t fmt_flags = FMT_FLAG_NONE;
87 fmt_flags |= FMT_FLAG_ZERO;
90 /* argument selector or minimum field width */
94 } while (iswdigit(*fmt));
96 /* digit string was ... */
98 /* ... argument selector */
99 fmt_flags = FMT_FLAG_NONE; /* reset possibly set 0-flag */
103 /* ... minimum field width */
109 case '#': flag = FMT_FLAG_HASH; break;
110 case '0': flag = FMT_FLAG_ZERO; break;
111 case '-': flag = FMT_FLAG_MINUS; break;
112 case '\'': flag = FMT_FLAG_TICK; break;
115 if (fmt_flags & FMT_FLAG_PLUS) {
116 warningf(pos, "' ' is overridden by prior '+' in conversion specification");
118 flag = FMT_FLAG_SPACE;
122 if (fmt_flags & FMT_FLAG_SPACE) {
123 warningf(pos, "'+' overrides prior ' ' in conversion specification");
125 flag = FMT_FLAG_PLUS;
128 default: goto break_fmt_flags;
130 if (fmt_flags & flag) {
131 warningf(pos, "repeated flag '%c' in conversion specification", (char)*fmt);
138 /* minimum field width */
141 warningf(pos, "missing argument for '*' field width in conversion specification");
144 const type_t *const arg_type = arg->expression->base.datatype;
145 if (arg_type != type_int) {
146 warningf(pos, "argument for '*' field width in conversion specification is not an 'int', but an '%T'", arg_type);
150 while (iswdigit(*fmt)) {
161 warningf(pos, "missing argument for '*' precision in conversion specification");
164 const type_t *const arg_type = arg->expression->base.datatype;
165 if (arg_type != type_int) {
166 warningf(pos, "argument for '*' precision in conversion specification is not an 'int', but an '%T'", arg_type);
170 /* digit string may be omitted */
171 while (iswdigit(*fmt)) {
177 /* length modifier */
178 format_length_modifier_t fmt_mod;
184 fmt_mod = FMT_MOD_hh;
194 fmt_mod = FMT_MOD_ll;
200 case 'L': ++fmt; fmt_mod = FMT_MOD_L; break;
201 case 'j': ++fmt; fmt_mod = FMT_MOD_j; break;
202 case 't': ++fmt; fmt_mod = FMT_MOD_t; break;
203 case 'z': ++fmt; fmt_mod = FMT_MOD_z; break;
204 case 'q': ++fmt; fmt_mod = FMT_MOD_q; break;
205 default: fmt_mod = FMT_MOD_NONE; break;
209 warningf(pos, "dangling %% in format string");
213 const type_t *expected_type = NULL;
214 format_flags_t allowed_flags;
219 case FMT_MOD_NONE: expected_type = type_int; break;
220 case FMT_MOD_hh: expected_type = type_int; break; /* TODO promoted signed char */
221 case FMT_MOD_h: expected_type = type_int; break; /* TODO promoted short */
222 case FMT_MOD_l: expected_type = type_long; break;
223 case FMT_MOD_ll: expected_type = type_long_long; break;
224 case FMT_MOD_j: expected_type = type_intmax_t; break;
225 case FMT_MOD_z: expected_type = type_ssize_t; break;
226 case FMT_MOD_t: expected_type = type_ptrdiff_t; break;
229 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
232 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_SPACE | FMT_FLAG_PLUS | FMT_FLAG_ZERO;
238 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_HASH | FMT_FLAG_ZERO;
239 goto eval_fmt_mod_unsigned;
243 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_ZERO;
244 eval_fmt_mod_unsigned:
246 case FMT_MOD_NONE: expected_type = type_unsigned_int; break;
247 case FMT_MOD_hh: expected_type = type_int; break; /* TODO promoted unsigned char */
248 case FMT_MOD_h: expected_type = type_int; break; /* TODO promoted unsigned short */
249 case FMT_MOD_l: expected_type = type_unsigned_long; break;
250 case FMT_MOD_ll: expected_type = type_unsigned_long_long; break;
251 case FMT_MOD_j: expected_type = type_uintmax_t; break;
252 case FMT_MOD_z: expected_type = type_size_t; break;
253 case FMT_MOD_t: expected_type = type_uptrdiff_t; break;
256 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
270 case FMT_MOD_l: /* l modifier is ignored */
271 case FMT_MOD_NONE: expected_type = type_double; break;
272 case FMT_MOD_L: expected_type = type_long_double; break;
275 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
278 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_SPACE | FMT_FLAG_PLUS | FMT_FLAG_HASH | FMT_FLAG_ZERO;
282 if (fmt_mod != FMT_MOD_NONE) {
283 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
285 expected_type = type_wchar_t;
286 allowed_flags = FMT_FLAG_NONE;
290 expected_type = type_int;
292 case FMT_MOD_NONE: expected_type = type_int; break; /* TODO promoted char */
293 case FMT_MOD_l: expected_type = type_wint_t; break;
296 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
299 allowed_flags = FMT_FLAG_NONE;
303 if (fmt_mod != FMT_MOD_NONE) {
304 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
306 expected_type = type_wchar_t_ptr;
307 allowed_flags = FMT_FLAG_NONE;
312 case FMT_MOD_NONE: expected_type = type_string; break;
313 case FMT_MOD_l: expected_type = type_wchar_t_ptr; break;
316 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
319 allowed_flags = FMT_FLAG_NONE;
323 if (fmt_mod != FMT_MOD_NONE) {
324 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
326 expected_type = type_void_ptr;
327 allowed_flags = FMT_FLAG_NONE;
332 case FMT_MOD_NONE: expected_type = type_int_ptr; break;
333 case FMT_MOD_hh: expected_type = type_signed_char_ptr; break;
334 case FMT_MOD_h: expected_type = type_short_ptr; break;
335 case FMT_MOD_l: expected_type = type_long_ptr; break;
336 case FMT_MOD_ll: expected_type = type_long_long_ptr; break;
337 case FMT_MOD_j: expected_type = type_intmax_t_ptr; break;
338 case FMT_MOD_z: expected_type = type_ssize_t_ptr; break;
339 case FMT_MOD_t: expected_type = type_ptrdiff_t_ptr; break;
342 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
345 allowed_flags = FMT_FLAG_NONE;
349 warningf(pos, "encountered unknown conversion specifier '%%%C'", (wint_t)*fmt);
354 if ((fmt_flags & ~allowed_flags) != 0) {
355 /* TODO better warning message text */
356 warningf(pos, "invalid format flags in conversion specification");
360 warningf(pos, "too few arguments for format string");
364 type_t *const arg_type = arg->expression->base.datatype;
365 if (is_type_pointer(expected_type)) {
366 type_t *const arg_skip = skip_typeref(arg_type);
367 if (is_type_pointer(arg_skip)) {
368 type_t *const exp_to = skip_typeref(expected_type->pointer.points_to);
369 type_t *const arg_to = skip_typeref(arg_skip->pointer.points_to);
370 if (arg_to == exp_to) {
375 if (get_unqualified_type(skip_typeref(arg_type)) == expected_type) {
380 "argument type '%T' does not match conversion specifier '%%%s%c'\n",
381 arg_type, get_length_modifier_name(fmt_mod), (char)*fmt);
386 if (fmt + 1 != wstring->begin + wstring->size) {
387 warningf(pos, "format string contains NUL");
390 warningf(pos, "too many arguments for format string");
394 void check_format(const call_expression_t *const call)
396 const expression_t *const func_expr = call->function;
397 if (func_expr->kind != EXPR_REFERENCE)
400 const char *const name = func_expr->reference.symbol->string;
401 const call_argument_t * arg = call->arguments;
402 if (strcmp(name, "wprintf") == 0) { /* TODO gammlig */
403 check_format_arguments(arg, arg->next);
404 } else if (strcmp(name, "swprintf") == 0) {
405 arg = arg->next->next; /* skip destination buffer and size */
406 check_format_arguments(arg, arg->next);