From 4cc4625a2b719e6dc5ea6fd2529101ab8d798d60 Mon Sep 17 00:00:00 2001 From: Huang Qi Date: Mon, 20 Dec 2021 15:51:36 +0800 Subject: [PATCH] Cleanup output format warnings (#904) Use `PRIxxx` related macros to format the output strings so as to clear compile warnings, e.g. PRIu32, PRId32, PRIX32, PRIX64 and so on. And add the related macro definitions in platform_common.h if they are not defined, as some compilers might not support them. --- core/iwasm/aot/arch/aot_reloc_riscv.c | 2 +- core/iwasm/common/wasm_application.c | 8 ++-- core/iwasm/compilation/aot_llvm.c | 4 +- core/iwasm/interpreter/wasm_interp_fast.c | 5 ++- .../libc-builtin/libc_builtin_wrapper.c | 8 ++-- core/shared/mem-alloc/ems/ems_alloc.c | 10 +++-- core/shared/mem-alloc/ems/ems_kfc.c | 10 ++--- .../shared/platform/include/platform_common.h | 42 +++++++++++++++++++ core/shared/utils/bh_log.c | 11 +++-- 9 files changed, 74 insertions(+), 26 deletions(-) diff --git a/core/iwasm/aot/arch/aot_reloc_riscv.c b/core/iwasm/aot/arch/aot_reloc_riscv.c index d695dc738..7d7002e65 100644 --- a/core/iwasm/aot/arch/aot_reloc_riscv.c +++ b/core/iwasm/aot/arch/aot_reloc_riscv.c @@ -357,7 +357,7 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr, if (error_buf != NULL) snprintf(error_buf, error_buf_size, "Load relocation section failed: " - "invalid relocation type %d.", + "invalid relocation type %" PRIu32 ".", reloc_type); return false; } diff --git a/core/iwasm/common/wasm_application.c b/core/iwasm/common/wasm_application.c index 03771b17e..3f4465fb7 100644 --- a/core/iwasm/common/wasm_application.c +++ b/core/iwasm/common/wasm_application.c @@ -417,7 +417,7 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst, char *endptr = NULL; bh_assert(argv[i] != NULL); if (argv[i][0] == '\0') { - snprintf(buf, sizeof(buf), "invalid input argument %d", i); + snprintf(buf, sizeof(buf), "invalid input argument %" PRId32, i); wasm_runtime_set_exception(module_inst, buf); goto fail; } @@ -554,8 +554,8 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst, break; } if (endptr && *endptr != '\0' && *endptr != '_') { - snprintf(buf, sizeof(buf), "invalid input argument %d: %s", i, - argv[i]); + snprintf(buf, sizeof(buf), "invalid input argument %" PRId32 ": %s", + i, argv[i]); wasm_runtime_set_exception(module_inst, buf); goto fail; } @@ -573,7 +573,7 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst, switch (type->types[type->param_count + j]) { case VALUE_TYPE_I32: { - os_printf("0x%x:i32", argv1[k]); + os_printf("0x%" PRIx32 ":i32", argv1[k]); k++; break; } diff --git a/core/iwasm/compilation/aot_llvm.c b/core/iwasm/compilation/aot_llvm.c index 4f8efc0ec..8fd6e8292 100644 --- a/core/iwasm/compilation/aot_llvm.c +++ b/core/iwasm/compilation/aot_llvm.c @@ -2490,12 +2490,12 @@ aot_load_const_from_table(AOTCompContext *comp_ctx, LLVMValueRef base, switch (value_type) { case VALUE_TYPE_F32: /* Store the raw int bits of f32 const as a hex string */ - snprintf(buf, sizeof(buf), "f32#%08X", value->i32); + snprintf(buf, sizeof(buf), "f32#%08" PRIX32, value->i32); const_ptr_type = F32_PTR_TYPE; break; case VALUE_TYPE_F64: /* Store the raw int bits of f64 const as a hex string */ - snprintf(buf, sizeof(buf), "f64#%016" PRIx64, value->i64); + snprintf(buf, sizeof(buf), "f64#%016" PRIX64, value->i64); const_ptr_type = F64_PTR_TYPE; break; default: diff --git a/core/iwasm/interpreter/wasm_interp_fast.c b/core/iwasm/interpreter/wasm_interp_fast.c index 424f09f86..9bb570219 100644 --- a/core/iwasm/interpreter/wasm_interp_fast.c +++ b/core/iwasm/interpreter/wasm_interp_fast.c @@ -3733,8 +3733,9 @@ wasm_interp_call_wasm(WASMModuleInstance *module_inst, WASMExecEnv *exec_env, if (argc < function->param_cell_num) { char buf[128]; snprintf(buf, sizeof(buf), - "invalid argument count %u, must be no smaller than %u", argc, - function->param_cell_num); + "invalid argument count %" PRIu32 + ", must be no smaller than %" PRIu32, + argc, (uint32)function->param_cell_num); wasm_set_exception(module_inst, buf); return; } diff --git a/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c b/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c index 3dd1d4720..a6e8693e5 100644 --- a/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c +++ b/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c @@ -769,7 +769,7 @@ exit_wrapper(wasm_exec_env_t exec_env, int32 status) { wasm_module_inst_t module_inst = get_module_inst(exec_env); char buf[32]; - snprintf(buf, sizeof(buf), "env.exit(%i)", status); + snprintf(buf, sizeof(buf), "env.exit(%" PRId32 ")", status); wasm_runtime_set_exception(module_inst, buf); } @@ -1012,7 +1012,7 @@ abort_wrapper(wasm_exec_env_t exec_env, int32 code) { wasm_module_inst_t module_inst = get_module_inst(exec_env); char buf[32]; - snprintf(buf, sizeof(buf), "env.abort(%i)", code); + snprintf(buf, sizeof(buf), "env.abort(%" PRId32 ")", code); wasm_runtime_set_exception(module_inst, buf); } @@ -1021,7 +1021,7 @@ abortStackOverflow_wrapper(wasm_exec_env_t exec_env, int32 code) { wasm_module_inst_t module_inst = get_module_inst(exec_env); char buf[32]; - snprintf(buf, sizeof(buf), "env.abortStackOverflow(%i)", code); + snprintf(buf, sizeof(buf), "env.abortStackOverflow(%" PRId32 ")", code); wasm_runtime_set_exception(module_inst, buf); } @@ -1030,7 +1030,7 @@ nullFunc_X_wrapper(wasm_exec_env_t exec_env, int32 code) { wasm_module_inst_t module_inst = get_module_inst(exec_env); char buf[32]; - snprintf(buf, sizeof(buf), "env.nullFunc_X(%i)", code); + snprintf(buf, sizeof(buf), "env.nullFunc_X(%" PRId32 ")", code); wasm_runtime_set_exception(module_inst, buf); } diff --git a/core/shared/mem-alloc/ems/ems_alloc.c b/core/shared/mem-alloc/ems/ems_alloc.c index 22f2d7f41..6f03fa58f 100644 --- a/core/shared/mem-alloc/ems/ems_alloc.c +++ b/core/shared/mem-alloc/ems/ems_alloc.c @@ -722,7 +722,8 @@ void gc_dump_heap_stats(gc_heap_t *heap) { os_printf("heap: %p, heap start: %p\n", heap, heap->base_addr); - os_printf("total free: %u, current: %u, highmark: %u\n", + os_printf("total free: %" PRIu32 ", current: %" PRIu32 + ", highmark: %" PRIu32 "\n", heap->total_free_size, heap->current_size, heap->highmark_size); os_printf("g_total_malloc=%lu, g_total_free=%lu, occupied=%lu\n", g_total_malloc, g_total_free, g_total_malloc - g_total_free); @@ -765,9 +766,10 @@ gci_dump(gc_heap_t *heap) return; } - os_printf("#%d %08x %x %x %d %c %d\n", i, - (int32)((char *)cur - (char *)heap->base_addr), ut, p, mark, - inuse, (int32)hmu_obj_size(size)); + os_printf("#%d %08" PRIx32 " %" PRIx32 " %d %d" + " %c %" PRId32 "\n", + i, (int32)((char *)cur - (char *)heap->base_addr), (int32)ut, + p, mark, inuse, (int32)hmu_obj_size(size)); #if BH_ENABLE_GC_VERIFY != 0 if (inuse == 'V') { gc_object_prefix_t *prefix = (gc_object_prefix_t *)(cur + 1); diff --git a/core/shared/mem-alloc/ems/ems_kfc.c b/core/shared/mem-alloc/ems/ems_kfc.c index f87872711..d2a1eb701 100644 --- a/core/shared/mem-alloc/ems/ems_kfc.c +++ b/core/shared/mem-alloc/ems/ems_kfc.c @@ -59,8 +59,8 @@ gc_init_with_pool(char *buf, gc_size_t buf_size) gc_size_t heap_max_size; if (buf_size < APP_HEAP_SIZE_MIN) { - os_printf("[GC_ERROR]heap init buf size (%u) < %u\n", buf_size, - APP_HEAP_SIZE_MIN); + os_printf("[GC_ERROR]heap init buf size (%" PRIu32 ") < %" PRIu32 "\n", + buf_size, (uint32)APP_HEAP_SIZE_MIN); return NULL; } @@ -93,7 +93,7 @@ gc_init_with_struct_and_pool(char *struct_buf, gc_size_t struct_buf_size, } if (struct_buf_size < sizeof(gc_handle_t)) { - os_printf("[GC_ERROR]heap init struct buf size (%u) < %zu\n", + os_printf("[GC_ERROR]heap init struct buf size (%" PRIu32 ") < %zu\n", struct_buf_size, sizeof(gc_handle_t)); return NULL; } @@ -104,8 +104,8 @@ gc_init_with_struct_and_pool(char *struct_buf, gc_size_t struct_buf_size, } if (pool_buf_size < APP_HEAP_SIZE_MIN) { - os_printf("[GC_ERROR]heap init buf size (%u) < %u\n", pool_buf_size, - APP_HEAP_SIZE_MIN); + os_printf("[GC_ERROR]heap init buf size (%" PRIu32 ") < %u\n", + pool_buf_size, APP_HEAP_SIZE_MIN); return NULL; } diff --git a/core/shared/platform/include/platform_common.h b/core/shared/platform/include/platform_common.h index 57629654f..2a5da79c0 100644 --- a/core/shared/platform/include/platform_common.h +++ b/core/shared/platform/include/platform_common.h @@ -104,6 +104,48 @@ typedef int64_t int64; typedef void *(*thread_start_routine_t)(void *); +#ifndef PRId32 +#define PRId32 "d" +#endif +#ifndef PRIi32 +#define PRIi32 "i" +#endif +#ifndef PRIu32 +#define PRIu32 "u" +#endif +#ifndef PRIx32 +#define PRIx32 "x" +#endif +#ifndef PRIX32 +#define PRIX32 "X" +#endif + +#ifndef __PRI64_PREFIX +#if UINTPTR_MAX == UINT64_MAX +#define __PRI64_PREFIX "l" +#define __PRIPTR_PREFIX "l" +#else +#define __PRI64_PREFIX "ll" +#define __PRIPTR_PREFIX +#endif +#endif + +#ifndef PRId64 +#define PRId64 __PRI64_PREFIX "d" +#endif +#ifndef PRIu64 +#define PRIu64 __PRI64_PREFIX "u" +#endif +#ifndef PRIx64 +#define PRIx64 __PRI64_PREFIX "x" +#endif +#ifndef PRIX64 +#define PRIX64 __PRI64_PREFIX "X" +#endif +#ifndef PRIXPTR +#define PRIXPTR __PRIPTR_PREFIX "X" +#endif + #ifdef __cplusplus } #endif diff --git a/core/shared/utils/bh_log.c b/core/shared/utils/bh_log.c index 04d3e98ae..3e5e95ea9 100644 --- a/core/shared/utils/bh_log.c +++ b/core/shared/utils/bh_log.c @@ -39,9 +39,11 @@ bh_log(LogLevel log_level, const char *file, int line, const char *fmt, ...) s = t % 60; mills = (uint32)(usec % 1000); - snprintf(buf, sizeof(buf), "%02u:%02u:%02u:%03u", h, m, s, mills); + snprintf(buf, sizeof(buf), + "%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%03" PRIu32, h, m, s, + mills); - os_printf("[%s - %X]: ", buf, (uint32)(uintptr_t)self); + os_printf("[%s - %" PRIXPTR "]: ", buf, (uintptr_t)self); if (file) os_printf("%s, line %d, ", file, line); @@ -71,8 +73,9 @@ bh_print_time(const char *prompt) total_time_ms += curr_time_ms - last_time_ms; - os_printf("%-48s time of last stage: %u ms, total time: %u ms\n", prompt, - curr_time_ms - last_time_ms, total_time_ms); + os_printf("%-48s time of last stage: %" PRIu32 " ms, total time: %" PRIu32 + " ms\n", + prompt, curr_time_ms - last_time_ms, total_time_ms); last_time_ms = curr_time_ms; }