From 078eaa009d7bea0303e09293ed291c1f7eb69a5a Mon Sep 17 00:00:00 2001 From: Avamander Date: Sun, 11 Jul 2021 22:19:51 +0300 Subject: [PATCH] refactor(printf) add printf-like function attribute to _lv_txt_set_text_vfmt and lv_label_set_text_fmt (#2332) This improves static analysis and compiler warnings of incorrect format usage. --- src/misc/lv_txt.c | 2 +- src/misc/lv_txt.h | 3 ++- src/misc/lv_types.h | 6 ++++++ src/widgets/lv_label.c | 4 ++-- src/widgets/lv_label.h | 2 +- 5 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/misc/lv_txt.c b/src/misc/lv_txt.c index 10dcd1a0c2..0b524ea23d 100644 --- a/src/misc/lv_txt.c +++ b/src/misc/lv_txt.c @@ -483,7 +483,7 @@ void _lv_txt_cut(char * txt, uint32_t pos, uint32_t len) * @param fmt `printf`-like format * @return pointer to the allocated text string. */ -char * _lv_txt_set_text_vfmt(const char * fmt, va_list ap) +LV_FORMAT_ATTRIBUTE(1, 0) char * _lv_txt_set_text_vfmt(const char * fmt, va_list ap) { /*Allocate space for the new text by using trick from C99 standard section 7.19.6.12*/ va_list ap_copy; diff --git a/src/misc/lv_txt.h b/src/misc/lv_txt.h index 0bfaf58beb..f44c27d750 100644 --- a/src/misc/lv_txt.h +++ b/src/misc/lv_txt.h @@ -20,6 +20,7 @@ extern "C" { #include "lv_area.h" #include "../font/lv_font.h" #include "lv_printf.h" +#include "lv_types.h" /********************* * DEFINES @@ -141,7 +142,7 @@ void _lv_txt_cut(char * txt, uint32_t pos, uint32_t len); * @param fmt `printf`-like format * @return pointer to the allocated text string. */ -char * _lv_txt_set_text_vfmt(const char * fmt, va_list ap); +char * _lv_txt_set_text_vfmt(const char * fmt, va_list ap) LV_FORMAT_ATTRIBUTE(1, 0); /** * Decode two encoded character from a string. diff --git a/src/misc/lv_types.h b/src/misc/lv_types.h index e445b72db2..be08b115bb 100644 --- a/src/misc/lv_types.h +++ b/src/misc/lv_types.h @@ -80,6 +80,12 @@ typedef uint32_t lv_uintptr_t; #define _LV_CONCAT3(x, y, z) x ## y ## z #define LV_CONCAT3(x, y, z) _LV_CONCAT3(x, y, z) +#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) +#define LV_FORMAT_ATTRIBUTE(fmtstr, vararg) __attribute__ ((format(printf, fmtstr, vararg))) +#else +#define LV_FORMAT_ATTRIBUTE(fmtstr, vararg) +#endif + #ifdef __cplusplus } /*extern "C"*/ #endif diff --git a/src/widgets/lv_label.c b/src/widgets/lv_label.c index 6d5f657bb7..c71b74ada6 100644 --- a/src/widgets/lv_label.c +++ b/src/widgets/lv_label.c @@ -1,4 +1,4 @@ -/** +/** * @file lv_label.c * */ @@ -144,7 +144,7 @@ void lv_label_set_text(lv_obj_t * obj, const char * text) lv_label_refr_text(obj); } -void lv_label_set_text_fmt(lv_obj_t * obj, const char * fmt, ...) +LV_FORMAT_ATTRIBUTE(2, 3) void lv_label_set_text_fmt(lv_obj_t * obj, const char * fmt, ...) { LV_ASSERT_OBJ(obj, MY_CLASS); LV_ASSERT_NULL(fmt); diff --git a/src/widgets/lv_label.h b/src/widgets/lv_label.h index 2bbfc86f39..aac7d0b8ca 100644 --- a/src/widgets/lv_label.h +++ b/src/widgets/lv_label.h @@ -105,7 +105,7 @@ void lv_label_set_text(lv_obj_t * obj, const char * text); * @param fmt `printf`-like format * @example lv_label_set_text_fmt(label1, "%d user", user_num); */ -void lv_label_set_text_fmt(lv_obj_t * obj, const char * fmt, ...); +void lv_label_set_text_fmt(lv_obj_t * obj, const char * fmt, ...) LV_FORMAT_ATTRIBUTE(2, 3); /** * Set a static text. It will not be saved by the label so the 'text' variable