From 1af202370e38a1bbdac5d8619bfa580188a0b802 Mon Sep 17 00:00:00 2001 From: Gediminas Jakutis Date: Tue, 8 Oct 2019 11:00:59 +0300 Subject: diagnostic: tag functions as printf-style. This allows the compiler to check the format string and give warnings accordingly. Signed-off-by: Gediminas Jakutis --- include/rin/definitions.h | 10 ++++++++++ include/rin/diagnostic.h | 9 +++++---- 2 files changed, 15 insertions(+), 4 deletions(-) (limited to 'include/rin') diff --git a/include/rin/definitions.h b/include/rin/definitions.h index 19dde14..b8b6da3 100644 --- a/include/rin/definitions.h +++ b/include/rin/definitions.h @@ -26,6 +26,12 @@ /* weak symbols */ # define WEAK_SYM __attribute__((weak)) + +/* allow the compiler to check varargs */ +/* +# define FORMAT_STRING(a, b) __attribute__((format (printf, a, b))) +*/ + /* optimizing likely / unlikely branches */ # define likely(a) (__builtin_expect(!!(a), 1)) # define unlikely(a) (__builtin_expect((a), 0)) @@ -35,6 +41,9 @@ /* weak symbols */ # define WEAK_SYM __declspec(selectany) + +# define FORMAT_STRING(a, b) + /* optimizing likely / unlikely branches */ # define likely(a) (a) # define unlikely(a) (a) @@ -42,6 +51,7 @@ /* the rest – NOOP */ #else # define WEAK_SYM +# define FORMAT_STRING(a, b) # define likely(a) (a) # define unlikely(a) (a) #endif diff --git a/include/rin/diagnostic.h b/include/rin/diagnostic.h index de925cc..128e059 100644 --- a/include/rin/diagnostic.h +++ b/include/rin/diagnostic.h @@ -21,6 +21,7 @@ #ifndef LIBRIN_DIAGNOSTIC_INCLUDED #define LIBRIN_DIAGNOSTIC_INCLUDED +#include "rin/definitions.h" #include #define RIN_DIAG_SET 1 @@ -42,9 +43,9 @@ enum rin_diag_outstream { int rin_diag_flags(int flag, int action); int rin_diag_set_outstream(enum rin_diag_outstream channel, FILE *stream); -void rin_err(const char *format, ...); -void rin_fixme(const char *format, ...); -void rin_warn(const char *format, ...); -void rin_info(const char *format, ...); +void rin_err(const char *format, ...) FORMAT_STRING(1, 2); +void rin_fixme(const char *format, ...) FORMAT_STRING(1, 2); +void rin_warn(const char *format, ...) FORMAT_STRING(1, 2); +void rin_info(const char *format, ...) FORMAT_STRING(1, 2); #endif /* LIBRIN_DIAGNOSTIC_INCLUDED */ -- cgit v1.2.3