From d9bb189e25f14578ca74d6a3aa9f9a467f6f6595 Mon Sep 17 00:00:00 2001 From: Stefan Kerkmann Date: Thu, 7 Jul 2022 09:27:50 +0200 Subject: [PATCH] [Core] Update mpaland/printf to eyalroz/printf fork (#16163) mpaland printf implementation was abandoned in ~2019 and the fork by eyalroz is now regarded to be the goto replacement of it. So this commit incoporates the changes needed to use this fork in QMK. Note that pointer ptrdiff_t is always supported since commit 51c90f93a97fdaef895783ecbe24569be0db7cb8 --- builddefs/build_test.mk | 6 +-- lib/printf | 2 +- platforms/arm_atsam/platform.mk | 1 + platforms/avr/platform.mk | 1 + platforms/chibios/platform.mk | 3 +- quantum/logging/print.c | 2 +- quantum/logging/print.h | 68 ++++++++++++++++++++------------- quantum/logging/print.mk | 17 +++++---- 8 files changed, 61 insertions(+), 39 deletions(-) diff --git a/builddefs/build_test.mk b/builddefs/build_test.mk index bd9b372c332..64db99fed91 100644 --- a/builddefs/build_test.mk +++ b/builddefs/build_test.mk @@ -38,11 +38,11 @@ CREATE_MAP := no VPATH += \ $(LIB_PATH)/googletest \ $(LIB_PATH)/googlemock \ - $(LIB_PATH)/printf + $(COMMON_VPATH) \ + $(TEST_PATH) all: elf -VPATH += $(TEST_PATH) $(COMMON_VPATH) PLATFORM:=TEST PLATFORM_KEY:=test BOOTLOADER_TYPE:=none @@ -64,6 +64,7 @@ include $(QUANTUM_PATH)/debounce/tests/rules.mk include $(QUANTUM_PATH)/encoder/tests/rules.mk include $(QUANTUM_PATH)/sequencer/tests/rules.mk include $(QUANTUM_PATH)/wear_leveling/tests/rules.mk +include $(QUANTUM_PATH)/logging/print.mk include $(PLATFORM_PATH)/test/rules.mk ifneq ($(filter $(FULL_TESTS),$(TEST)),) include $(BUILDDEFS_PATH)/build_full_test.mk @@ -71,7 +72,6 @@ endif $(TEST)_SRC += \ tests/test_common/main.c \ - $(LIB_PATH)/printf/printf.c \ $(QUANTUM_PATH)/logging/print.c $(TEST_OBJ)/$(TEST)_SRC := $($(TEST)_SRC) diff --git a/lib/printf b/lib/printf index d3b984684bb..c2e3b4e10d2 160000 --- a/lib/printf +++ b/lib/printf @@ -1 +1 @@ -Subproject commit d3b984684bb8a8bdc48cc7a1abecb93ce59bbe3e +Subproject commit c2e3b4e10d281e7f0f694d3ecbd9f320977288cc diff --git a/platforms/arm_atsam/platform.mk b/platforms/arm_atsam/platform.mk index 9618838dc3d..9462f517ae7 100644 --- a/platforms/arm_atsam/platform.mk +++ b/platforms/arm_atsam/platform.mk @@ -24,6 +24,7 @@ COMPILEFLAGS += -fno-strict-aliasing COMPILEFLAGS += -mfloat-abi=hard COMPILEFLAGS += -mfpu=fpv4-sp-d16 COMPILEFLAGS += -mthumb +COMPILEFLAGS += -fno-builtin-printf #ALLOW_WARNINGS = yes diff --git a/platforms/avr/platform.mk b/platforms/avr/platform.mk index b51a94c93af..39a11b28e47 100644 --- a/platforms/avr/platform.mk +++ b/platforms/avr/platform.mk @@ -24,6 +24,7 @@ COMPILEFLAGS += -fdata-sections COMPILEFLAGS += -fpack-struct COMPILEFLAGS += -fshort-enums COMPILEFLAGS += -mcall-prologues +COMPILEFLAGS += -fno-builtin-printf # Linker relaxation is only possible if # link time optimizations are not enabled. diff --git a/platforms/chibios/platform.mk b/platforms/chibios/platform.mk index 63cc1a4b335..b2a8ec89e13 100644 --- a/platforms/chibios/platform.mk +++ b/platforms/chibios/platform.mk @@ -349,7 +349,8 @@ SHARED_CFLAGS = -fomit-frame-pointer \ -ffunction-sections \ -fdata-sections \ -fno-common \ - -fshort-wchar + -fshort-wchar \ + -fno-builtin-printf LDSCRIPT_PATH := $(shell dirname "$(LDSCRIPT)") diff --git a/quantum/logging/print.c b/quantum/logging/print.c index 50a6b826eec..17e6737ac44 100644 --- a/quantum/logging/print.c +++ b/quantum/logging/print.c @@ -28,6 +28,6 @@ void print_set_sendchar(sendchar_func_t send) { func = send; } -void _putchar(char character) { +void putchar_(char character) { func(character); } diff --git a/quantum/logging/print.h b/quantum/logging/print.h index aa72fc70745..5fbf189505d 100644 --- a/quantum/logging/print.h +++ b/quantum/logging/print.h @@ -32,6 +32,22 @@ void print_set_sendchar(sendchar_func_t func); +/** + * @brief This macro suppress format warnings for the function that is passed + * in. The main use-case is that `b` format specifier for printing binary + * numbers is not in the official C standard. Inclusion is planned for the + * upcoming C2X C standard, but until then GCC will always output a warning for + * a unknown format specifier. + */ +#define IGNORE_FORMAT_WARNING(func) \ + do { \ + _Pragma("GCC diagnostic push"); \ + _Pragma("GCC diagnostic ignored \"-Wformat\""); \ + _Pragma("GCC diagnostic ignored \"-Wformat-extra-args\""); \ + func; \ + _Pragma("GCC diagnostic pop"); \ + } while (0) + #ifndef NO_PRINT # if __has_include_next("_print.h") # include_next "_print.h" /* Include the platforms print.h */ @@ -78,25 +94,25 @@ void print_set_sendchar(sendchar_func_t func); #define print_hex16(i) xprintf("%04X", i) #define print_hex32(i) xprintf("%08lX", i) /* binary */ -#define print_bin4(i) xprintf("%04b", i) -#define print_bin8(i) xprintf("%08b", i) -#define print_bin16(i) xprintf("%016b", i) -#define print_bin32(i) xprintf("%032lb", i) -#define print_bin_reverse8(i) xprintf("%08b", bitrev(i)) -#define print_bin_reverse16(i) xprintf("%016b", bitrev16(i)) -#define print_bin_reverse32(i) xprintf("%032lb", bitrev32(i)) +#define print_bin4(i) IGNORE_FORMAT_WARNING(xprintf("%04b", i)) +#define print_bin8(i) IGNORE_FORMAT_WARNING(xprintf("%08b", i)) +#define print_bin16(i) IGNORE_FORMAT_WARNING(xprintf("%016b", i)) +#define print_bin32(i) IGNORE_FORMAT_WARNING(xprintf("%032lb", i)) +#define print_bin_reverse8(i) IGNORE_FORMAT_WARNING(xprintf("%08b", bitrev(i))) +#define print_bin_reverse16(i) IGNORE_FORMAT_WARNING(xprintf("%016b", bitrev16(i))) +#define print_bin_reverse32(i) IGNORE_FORMAT_WARNINGxprintf("%032lb", bitrev32(i))) /* print value utility */ #define print_val_dec(v) xprintf(#v ": %u\n", v) #define print_val_decs(v) xprintf(#v ": %d\n", v) #define print_val_hex8(v) xprintf(#v ": %X\n", v) #define print_val_hex16(v) xprintf(#v ": %02X\n", v) #define print_val_hex32(v) xprintf(#v ": %04lX\n", v) -#define print_val_bin8(v) xprintf(#v ": %08b\n", v) -#define print_val_bin16(v) xprintf(#v ": %016b\n", v) -#define print_val_bin32(v) xprintf(#v ": %032lb\n", v) -#define print_val_bin_reverse8(v) xprintf(#v ": %08b\n", bitrev(v)) -#define print_val_bin_reverse16(v) xprintf(#v ": %016b\n", bitrev16(v)) -#define print_val_bin_reverse32(v) xprintf(#v ": %032lb\n", bitrev32(v)) +#define print_val_bin8(v) IGNORE_FORMAT_WARNING(xprintf(#v ": %08b\n", v)) +#define print_val_bin16(v) IGNORE_FORMAT_WARNING(xprintf(#v ": %016b\n", v)) +#define print_val_bin32(v) IGNORE_FORMAT_WARNING(xprintf(#v ": %032lb\n", v)) +#define print_val_bin_reverse8(v) IGNORE_FORMAT_WARNING(xprintf(#v ": %08b\n", bitrev(v))) +#define print_val_bin_reverse16(v) IGNORE_FORMAT_WARNING(xprintf(#v ": %016b\n", bitrev16(v))) +#define print_val_bin_reverse32(v) IGNORE_FORMAT_WARNING(xprintf(#v ": %032lb\n", bitrev32(v))) // User print disables the normal print messages in the body of QMK/TMK code and // is meant as a lightweight alternative to NOPRINT. Use it when you only want to do @@ -114,22 +130,22 @@ void print_set_sendchar(sendchar_func_t func); #define uprint_hex16(i) uprintf("%04X", i) #define uprint_hex32(i) uprintf("%08lX", i) /* binary */ -#define uprint_bin4(i) uprintf("%04b", i) -#define uprint_bin8(i) uprintf("%08b", i) -#define uprint_bin16(i) uprintf("%016b", i) -#define uprint_bin32(i) uprintf("%032lb", i) -#define uprint_bin_reverse8(i) uprintf("%08b", bitrev(i)) -#define uprint_bin_reverse16(i) uprintf("%016b", bitrev16(i)) -#define uprint_bin_reverse32(i) uprintf("%032lb", bitrev32(i)) +#define uprint_bin4(i) IGNORE_FORMAT_WARNING(uprintf("%04b", i)) +#define uprint_bin8(i) IGNORE_FORMAT_WARNING(uprintf("%08b", i)) +#define uprint_bin16(i) IGNORE_FORMAT_WARNING(uprintf("%016b", i)) +#define uprint_bin32(i) IGNORE_FORMAT_WARNING(uprintf("%032lb", i)) +#define uprint_bin_reverse8(i) IGNORE_FORMAT_WARNING(uprintf("%08b", bitrev(i))) +#define uprint_bin_reverse16(i) IGNORE_FORMAT_WARNING(uprintf("%016b", bitrev16(i))) +#define uprint_bin_reverse32(i) IGNORE_FORMAT_WARNING(uprintf("%032lb", bitrev32(i))) /* print value utility */ #define uprint_val_dec(v) uprintf(#v ": %u\n", v) #define uprint_val_decs(v) uprintf(#v ": %d\n", v) #define uprint_val_hex8(v) uprintf(#v ": %X\n", v) #define uprint_val_hex16(v) uprintf(#v ": %02X\n", v) #define uprint_val_hex32(v) uprintf(#v ": %04lX\n", v) -#define uprint_val_bin8(v) uprintf(#v ": %08b\n", v) -#define uprint_val_bin16(v) uprintf(#v ": %016b\n", v) -#define uprint_val_bin32(v) uprintf(#v ": %032lb\n", v) -#define uprint_val_bin_reverse8(v) uprintf(#v ": %08b\n", bitrev(v)) -#define uprint_val_bin_reverse16(v) uprintf(#v ": %016b\n", bitrev16(v)) -#define uprint_val_bin_reverse32(v) uprintf(#v ": %032lb\n", bitrev32(v)) +#define uprint_val_bin8(v) IGNORE_FORMAT_WARNING(uprintf(#v ": %08b\n", v)) +#define uprint_val_bin16(v) IGNORE_FORMAT_WARNING(uprintf(#v ": %016b\n", v)) +#define uprint_val_bin32(v) IGNORE_FORMAT_WARNING(uprintf(#v ": %032lb\n", v)) +#define uprint_val_bin_reverse8(v) IGNORE_FORMAT_WARNING(uprintf(#v ": %08b\n", bitrev(v))) +#define uprint_val_bin_reverse16(v) IGNORE_FORMAT_WARNING(uprintf(#v ": %016b\n", bitrev16(v))) +#define uprint_val_bin_reverse32(v) IGNORE_FORMAT_WARNING(uprintf(#v ": %032lb\n", bitrev32(v))) diff --git a/quantum/logging/print.mk b/quantum/logging/print.mk index 67c004192d8..658c533dadf 100644 --- a/quantum/logging/print.mk +++ b/quantum/logging/print.mk @@ -1,9 +1,12 @@ -PRINTF_PATH = $(LIB_PATH)/printf +PRINTF_PATH = $(LIB_PATH)/printf/src -VPATH += $(PRINTF_PATH) -SRC += $(PRINTF_PATH)/printf.c +VPATH += $(PRINTF_PATH) $(PRINTF_PATH)/printf +SRC += printf.c QUANTUM_SRC +=$(QUANTUM_DIR)/logging/print.c -OPT_DEFS += -DPRINTF_DISABLE_SUPPORT_FLOAT -OPT_DEFS += -DPRINTF_DISABLE_SUPPORT_EXPONENTIAL -OPT_DEFS += -DPRINTF_DISABLE_SUPPORT_LONG_LONG -OPT_DEFS += -DPRINTF_DISABLE_SUPPORT_PTRDIFF_T + +OPT_DEFS += -DPRINTF_SUPPORT_DECIMAL_SPECIFIERS=0 +OPT_DEFS += -DPRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS=0 +OPT_DEFS += -DPRINTF_SUPPORT_LONG_LONG=0 +OPT_DEFS += -DPRINTF_SUPPORT_WRITEBACK_SPECIFIER=0 +OPT_DEFS += -DSUPPORT_MSVC_STYLE_INTEGER_SPECIFIERS=0 +OPT_DEFS += -DPRINTF_ALIAS_STANDARD_FUNCTION_NAMES=1