diff --git a/libsrc/stdio/vfprintf.c b/libsrc/stdio/vfprintf.c index 9d361ae23..c429fe884 100644 --- a/libsrc/stdio/vfprintf.c +++ b/libsrc/stdio/vfprintf.c @@ -224,6 +224,7 @@ int vfprintf(FILE *fp, const char *fmt_, va_list ap) { int order = 0, suborder = 0; bool sign = false; bool leftalign = false; + bool sizez = false; int nlong = 0; c = fmt[++i]; @@ -265,16 +266,23 @@ int vfprintf(FILE *fp, const char *fmt_, va_list ap) { nlong = 2; c = fmt[++i]; } + } else if (c == 'z') { + sizez = true; + c = fmt[++i]; } switch (c) { case 'd': { long long x; - switch (nlong) { - case 0: x = va_arg(ap, int); break; - case 1: x = va_arg(ap, long); break; - default: x = va_arg(ap, long long); break; // case 2: + if (sizez) { + x = va_arg(ap, ssize_t); + } else { + switch (nlong) { + case 0: x = va_arg(ap, int); break; + case 1: x = va_arg(ap, long); break; + default: x = va_arg(ap, long long); break; // case 2: + } } bool negative = x < 0; unsigned long long ux = negative ? -x : x; @@ -287,10 +295,14 @@ int vfprintf(FILE *fp, const char *fmt_, va_list ap) { case 'u': { unsigned long long x; - switch (nlong) { - case 0: x = va_arg(ap, unsigned int); break; - case 1: x = va_arg(ap, unsigned long); break; - default: x = va_arg(ap, unsigned long long); break; // case 2: + if (sizez) { + x = va_arg(ap, size_t); + } else { + switch (nlong) { + case 0: x = va_arg(ap, unsigned int); break; + case 1: x = va_arg(ap, unsigned long); break; + default: x = va_arg(ap, unsigned long long); break; // case 2: + } } char *p = snprintullong2(bufend, x, 10, kHexDigits); if (sign) @@ -298,40 +310,18 @@ int vfprintf(FILE *fp, const char *fmt_, va_list ap) { o += snprintstr(fp, p, order, suborder, leftalign, padding); } break; - case 'z': - { - switch (fmt[i + 1]) { - case 'u': - { - ++i; - size_t x = va_arg(ap, size_t); - o += snprintullong(fp, x, 10, kHexDigits, order, padding); - } - break; - case 'd': - { - ++i; - ssize_t x = va_arg(ap, ssize_t); - bool negative = x < 0; - size_t ux = negative ? -x : x; - if (negative || order > 0 || sign) - o += sprintsign(fp, negative, sign, &order); - char *p = snprintullong2(bufend, ux, 10, kHexDigits); - o += snprintstr(fp, p, order, suborder, leftalign, padding); - } - break; - default: break; // TODO: error - } - } - break; case 'x': case 'X': { const char *digits = c == 'x' ? kHexDigits : kUpperHexDigits; unsigned long long x; - switch (nlong) { - case 0: x = va_arg(ap, unsigned int); break; - case 1: x = va_arg(ap, unsigned long); break; - default: x = va_arg(ap, unsigned long long); break; // case 2: + if (sizez) { + x = va_arg(ap, size_t); + } else { + switch (nlong) { + case 0: x = va_arg(ap, unsigned int); break; + case 1: x = va_arg(ap, unsigned long); break; + default: x = va_arg(ap, unsigned long long); break; // case 2: + } } o += snprintullong(fp, x, 16, digits, order, padding); } diff --git a/libsrc/tests/printf_test.c b/libsrc/tests/printf_test.c index d376d0c75..fe8637cf7 100644 --- a/libsrc/tests/printf_test.c +++ b/libsrc/tests/printf_test.c @@ -104,6 +104,7 @@ TEST(vsnprintf) { EXPECT("PRIuMAX:18446744073709551615", "PRIuMAX:%" PRIuMAX, UINTMAX_MAX); } EXPECT("size_t:54321, ssize_t:-54321", "size_t:%zu, ssize_t:%zd", (size_t)54321, (ssize_t)-54321); + EXPECT("size_t:0x54321", "size_t:0x%zx", (size_t)0x54321); EXPECT("PRId64:1311768467463790320", "PRId64:%" PRId64, (int64_t)0x123456789abcdef0); EXPECT("PRIu64:18364758544493064720", "PRIu64:%" PRIu64, (uint64_t)0xfedcba9876543210); diff --git a/src/wcc/emit_wasm.c b/src/wcc/emit_wasm.c index 31d59c7f9..60b4735c8 100644 --- a/src/wcc/emit_wasm.c +++ b/src/wcc/emit_wasm.c @@ -505,7 +505,7 @@ static Vector *emit_data_section(EmitWasm *ew) { data_push(&datasec, 0); // flags // Init (address). uint32_t address = segment->gvarinfo->non_prim.address; - VERBOSE("%04x: %.*s (size=%zu)\n", address, NAMES(segment->gvarinfo->varinfo->name), + VERBOSE("%04x: %.*s (size=0x%zx)\n", address, NAMES(segment->gvarinfo->varinfo->name), type_size(segment->gvarinfo->varinfo->type)); data_push(&datasec, OP_I32_CONST); data_leb128(&datasec, -1, address); diff --git a/src/wcc/traverse.c b/src/wcc/traverse.c index 4347d015c..67c54fde5 100644 --- a/src/wcc/traverse.c +++ b/src/wcc/traverse.c @@ -858,7 +858,7 @@ void traverse_ast(Vector *decls) { info->non_prim.address = address; size_t size = type_size(varinfo->type); address += size; - VERBOSE("%04x: %.*s (size=0x%lx)\n", info->non_prim.address, NAMES(varinfo->name), size); + VERBOSE("%04x: %.*s (size=0x%zx)\n", info->non_prim.address, NAMES(varinfo->name), size); } } }