Convert the format generator function to an annotated format function,

so that the generated formats can be checked by the compiler.
This commit is contained in:
zoulasc 2019-03-15 12:56:03 -04:00 committed by Qi Wang
parent 7ee3897740
commit 020b5dc7ac

View File

@ -86,10 +86,11 @@ emitter_printf(emitter_t *emitter, const char *format, ...) {
va_end(ap); va_end(ap);
} }
static inline void static inline const char * __attribute__((__format_arg__(3)))
emitter_gen_fmt(char *out_fmt, size_t out_size, const char *fmt_specifier, emitter_gen_fmt(char *out_fmt, size_t out_size, const char *fmt_specifier,
emitter_justify_t justify, int width) { emitter_justify_t justify, int width) {
size_t written; size_t written;
fmt_specifier++;
if (justify == emitter_justify_none) { if (justify == emitter_justify_none) {
written = malloc_snprintf(out_fmt, out_size, written = malloc_snprintf(out_fmt, out_size,
"%%%s", fmt_specifier); "%%%s", fmt_specifier);
@ -102,6 +103,7 @@ emitter_gen_fmt(char *out_fmt, size_t out_size, const char *fmt_specifier,
} }
/* Only happens in case of bad format string, which *we* choose. */ /* Only happens in case of bad format string, which *we* choose. */
assert(written < out_size); assert(written < out_size);
return out_fmt;
} }
/* /*
@ -127,26 +129,27 @@ emitter_print_value(emitter_t *emitter, emitter_justify_t justify, int width,
char buf[BUF_SIZE]; char buf[BUF_SIZE];
#define EMIT_SIMPLE(type, format) \ #define EMIT_SIMPLE(type, format) \
emitter_gen_fmt(fmt, FMT_SIZE, format, justify, width); \ emitter_printf(emitter, \
emitter_printf(emitter, fmt, *(const type *)value); \ emitter_gen_fmt(fmt, FMT_SIZE, format, justify, width), \
*(const type *)value);
switch (value_type) { switch (value_type) {
case emitter_type_bool: case emitter_type_bool:
emitter_gen_fmt(fmt, FMT_SIZE, "s", justify, width); emitter_printf(emitter,
emitter_printf(emitter, fmt, *(const bool *)value ? emitter_gen_fmt(fmt, FMT_SIZE, "%s", justify, width),
"true" : "false"); *(const bool *)value ? "true" : "false");
break; break;
case emitter_type_int: case emitter_type_int:
EMIT_SIMPLE(int, "d") EMIT_SIMPLE(int, "%d")
break; break;
case emitter_type_unsigned: case emitter_type_unsigned:
EMIT_SIMPLE(unsigned, "u") EMIT_SIMPLE(unsigned, "%u")
break; break;
case emitter_type_ssize: case emitter_type_ssize:
EMIT_SIMPLE(ssize_t, "zd") EMIT_SIMPLE(ssize_t, "%zd")
break; break;
case emitter_type_size: case emitter_type_size:
EMIT_SIMPLE(size_t, "zu") EMIT_SIMPLE(size_t, "%zu")
break; break;
case emitter_type_string: case emitter_type_string:
str_written = malloc_snprintf(buf, BUF_SIZE, "\"%s\"", str_written = malloc_snprintf(buf, BUF_SIZE, "\"%s\"",
@ -156,17 +159,17 @@ emitter_print_value(emitter_t *emitter, emitter_justify_t justify, int width,
* anywhere near the fmt size. * anywhere near the fmt size.
*/ */
assert(str_written < BUF_SIZE); assert(str_written < BUF_SIZE);
emitter_gen_fmt(fmt, FMT_SIZE, "s", justify, width); emitter_printf(emitter,
emitter_printf(emitter, fmt, buf); emitter_gen_fmt(fmt, FMT_SIZE, "%s", justify, width), buf);
break; break;
case emitter_type_uint32: case emitter_type_uint32:
EMIT_SIMPLE(uint32_t, FMTu32) EMIT_SIMPLE(uint32_t, "%" FMTu32)
break; break;
case emitter_type_uint64: case emitter_type_uint64:
EMIT_SIMPLE(uint64_t, FMTu64) EMIT_SIMPLE(uint64_t, "%" FMTu64)
break; break;
case emitter_type_title: case emitter_type_title:
EMIT_SIMPLE(char *const, "s"); EMIT_SIMPLE(char *const, "%s");
break; break;
default: default:
unreachable(); unreachable();