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 wchar_rep_t conversion)
38 static const char* const names[] = {
50 assert(mod < sizeof(names) / sizeof(*names));
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 warningf(pos, "' ' is overridden by prior '+' in conversion specification");
113 flag = FMT_FLAG_SPACE;
117 if (fmt_flags & FMT_FLAG_SPACE) {
118 warningf(pos, "'+' overrides prior ' ' in conversion specification");
120 flag = FMT_FLAG_PLUS;
123 default: goto break_fmt_flags;
125 if (fmt_flags & flag) {
126 warningf(pos, "repeated flag '%c' in conversion specification", (char)*fmt);
133 /* minimum field width */
136 warningf(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 warningf(pos, "argument for '*' field width in conversion specification is not an 'int', but an '%T'", arg_type);
145 while (iswdigit(*fmt)) {
156 warningf(pos, "missing argument for '*' precision in conversion specification");
159 const type_t *const arg_type = arg->expression->base.datatype;
160 if (arg_type != type_int) {
161 warningf(pos, "argument for '*' precision in conversion specification is not an 'int', but an '%T'", arg_type);
165 /* digit string may be omitted */
166 while (iswdigit(*fmt)) {
172 /* length modifier */
173 format_length_modifier_t fmt_mod;
179 fmt_mod = FMT_MOD_hh;
189 fmt_mod = FMT_MOD_ll;
195 case 'L': ++fmt; fmt_mod = FMT_MOD_L; break;
196 case 'j': ++fmt; fmt_mod = FMT_MOD_j; break;
197 case 't': ++fmt; fmt_mod = FMT_MOD_t; break;
198 case 'z': ++fmt; fmt_mod = FMT_MOD_z; break;
199 case 'q': ++fmt; fmt_mod = FMT_MOD_q; break;
200 default: fmt_mod = FMT_MOD_NONE; break;
204 warningf(pos, "dangling %% in format string");
208 const type_t *expected_type = NULL;
209 format_flags_t allowed_flags;
214 case FMT_MOD_NONE: expected_type = type_int; break;
215 case FMT_MOD_hh: expected_type = type_int; break; /* TODO promoted signed char */
216 case FMT_MOD_h: expected_type = type_int; break; /* TODO promoted short */
217 case FMT_MOD_l: expected_type = type_long; break;
218 case FMT_MOD_ll: expected_type = type_long_long; break;
219 case FMT_MOD_j: expected_type = type_intmax_t; break;
220 case FMT_MOD_z: expected_type = type_ssize_t; break;
221 case FMT_MOD_t: expected_type = type_ptrdiff_t; break;
224 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
227 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_PLUS | FMT_FLAG_ZERO;
233 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_HASH | FMT_FLAG_ZERO;
234 goto eval_fmt_mod_unsigned;
238 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_ZERO;
239 eval_fmt_mod_unsigned:
241 case FMT_MOD_NONE: expected_type = type_unsigned_int; break;
242 case FMT_MOD_hh: expected_type = type_int; break; /* TODO promoted unsigned char */
243 case FMT_MOD_h: expected_type = type_int; break; /* TODO promoted unsigned short */
244 case FMT_MOD_l: expected_type = type_unsigned_long; break;
245 case FMT_MOD_ll: expected_type = type_unsigned_long_long; break;
246 case FMT_MOD_j: expected_type = type_uintmax_t; break;
247 case FMT_MOD_z: expected_type = type_size_t; break;
248 case FMT_MOD_t: expected_type = type_uptrdiff_t; break;
251 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
265 case FMT_MOD_l: /* l modifier is ignored */
266 case FMT_MOD_NONE: expected_type = type_double; break;
267 case FMT_MOD_L: expected_type = type_long_double; break;
270 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
273 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_PLUS | FMT_FLAG_HASH | FMT_FLAG_ZERO;
277 if (fmt_mod != FMT_MOD_NONE) {
278 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
280 expected_type = type_wchar_t;
281 allowed_flags = FMT_FLAG_NONE;
285 expected_type = type_int;
287 case FMT_MOD_NONE: expected_type = type_int; break; /* TODO promoted char */
288 case FMT_MOD_l: expected_type = type_wint_t; break;
291 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
294 allowed_flags = FMT_FLAG_NONE;
298 if (fmt_mod != FMT_MOD_NONE) {
299 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
301 expected_type = type_wchar_t_ptr;
302 allowed_flags = FMT_FLAG_NONE;
307 case FMT_MOD_NONE: expected_type = type_string; break;
308 case FMT_MOD_l: expected_type = type_wchar_t_ptr; break;
311 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
314 allowed_flags = FMT_FLAG_NONE;
318 if (fmt_mod != FMT_MOD_NONE) {
319 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
321 expected_type = type_void_ptr;
322 allowed_flags = FMT_FLAG_NONE;
327 case FMT_MOD_NONE: expected_type = type_int_ptr; break;
328 case FMT_MOD_hh: expected_type = type_signed_char_ptr; break;
329 case FMT_MOD_h: expected_type = type_short_ptr; break;
330 case FMT_MOD_l: expected_type = type_long_ptr; break;
331 case FMT_MOD_ll: expected_type = type_long_long_ptr; break;
332 case FMT_MOD_j: expected_type = type_intmax_t_ptr; break;
333 case FMT_MOD_z: expected_type = type_ssize_t_ptr; break;
334 case FMT_MOD_t: expected_type = type_ptrdiff_t_ptr; break;
337 warn_invalid_length_modifier(pos, fmt_mod, *fmt);
340 allowed_flags = FMT_FLAG_NONE;
344 warningf(pos, "encountered unknown conversion specifier '%%%C'", (wint_t)*fmt);
349 if ((fmt_flags & ~allowed_flags) != 0) {
350 /* TODO better warning message text */
351 warningf(pos, "invalid format flags in conversion specification");
355 warningf(pos, "too few arguments for format string");
359 const type_t *const arg_type = arg->expression->base.datatype;
360 if (arg_type != expected_type) {
361 warningf(pos, "argument type '%T' does not match conversion specifier '%%%c'\n", arg_type, (char)*fmt);
366 if (fmt + 1 != wstring->begin + wstring->size) {
367 warningf(pos, "format string contains NUL");
370 warningf(pos, "too many arguments for format string");
374 void check_format(const call_expression_t *const call)
376 const expression_t *const func_expr = call->function;
377 if (func_expr->kind != EXPR_REFERENCE)
380 const char *const name = func_expr->reference.symbol->string;
381 const call_argument_t * arg = call->arguments;
382 if (strcmp(name, "wprintf") == 0) { /* TODO gammlig */
383 check_format_arguments(arg, arg->next);
384 } else if (strcmp(name, "swprintf") == 0) {
385 arg = arg->next->next; /* skip destination buffer and size */
386 check_format_arguments(arg, arg->next);