Skip to content

Commit

Permalink
[Core] Update mpaland/printf to eyalroz/printf fork
Browse files Browse the repository at this point in the history
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
  • Loading branch information
KarlK90 committed Jul 7, 2022
1 parent 29a2bac commit 2ae8344
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 39 deletions.
6 changes: 3 additions & 3 deletions builddefs/build_test.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -64,14 +64,14 @@ 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
endif

$(TEST)_SRC += \
tests/test_common/main.c \
$(LIB_PATH)/printf/printf.c \
$(QUANTUM_PATH)/logging/print.c

$(TEST_OBJ)/$(TEST)_SRC := $($(TEST)_SRC)
Expand Down
2 changes: 1 addition & 1 deletion lib/printf
1 change: 1 addition & 0 deletions platforms/arm_atsam/platform.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions platforms/avr/platform.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion platforms/chibios/platform.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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)")

Expand Down
2 changes: 1 addition & 1 deletion quantum/logging/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ void print_set_sendchar(sendchar_func_t send) {
func = send;
}

void _putchar(char character) {
void putchar_(char character) {
func(character);
}
68 changes: 42 additions & 26 deletions quantum/logging/print.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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
Expand All @@ -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)))
17 changes: 10 additions & 7 deletions quantum/logging/print.mk
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2ae8344

Please sign in to comment.