From 36c5884acb357871d089217bafcf3f7a2fb591ee Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 30 Aug 2020 22:26:34 +0200 Subject: [PATCH] Generalize WX_ATTRIBUTE_PRINTF to WX_ATTRIBUTE_FORMAT Allow applying gcc "format" attribute to other functions and do apply it to wxStrftime(). Also suppress -Wformat-nonliteral inside wxStrftime() itself, as it's now supposed to be checked when calling it. --- include/wx/defs.h | 12 +++++++----- include/wx/wxcrt.h | 9 ++++++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/include/wx/defs.h b/include/wx/defs.h index cb1d495b80..83bae9dca3 100644 --- a/include/wx/defs.h +++ b/include/wx/defs.h @@ -542,12 +542,14 @@ typedef short int WXTYPE; /* ---------------------------------------------------------------------------- */ /* Printf-like attribute definitions to obtain warnings with GNU C/C++ */ +#if defined(__GNUC__) && !wxUSE_UNICODE +# define WX_ATTRIBUTE_FORMAT(like, m, n) __attribute__ ((__format__ (like, m, n))) +#else +# define WX_ATTRIBUTE_FORMAT(like, m, n) +#endif + #ifndef WX_ATTRIBUTE_PRINTF -# if defined(__GNUC__) && !wxUSE_UNICODE -# define WX_ATTRIBUTE_PRINTF(m, n) __attribute__ ((__format__ (__printf__, m, n))) -# else -# define WX_ATTRIBUTE_PRINTF(m, n) -# endif +# define WX_ATTRIBUTE_PRINTF(m, n) WX_ATTRIBUTE_FORMAT(__printf__, m, n) # define WX_ATTRIBUTE_PRINTF_1 WX_ATTRIBUTE_PRINTF(1, 2) # define WX_ATTRIBUTE_PRINTF_2 WX_ATTRIBUTE_PRINTF(2, 3) diff --git a/include/wx/wxcrt.h b/include/wx/wxcrt.h index 6cf08f4fde..d4d6cd7df4 100644 --- a/include/wx/wxcrt.h +++ b/include/wx/wxcrt.h @@ -1052,9 +1052,16 @@ inline wchar_t* wxGetenv(const wxScopedWCharBuffer& name) { return wxCRT_GetenvW // ---------------------------------------------------------------------------- #ifndef wxNO_IMPLICIT_WXSTRING_ENCODING +WX_ATTRIBUTE_FORMAT(__strftime__, 3, 4) inline size_t wxStrftime(char *s, size_t max, const wxString& format, const struct tm *tm) - { return wxCRT_StrftimeA(s, max, format.mb_str(), tm); } + { + wxGCC_ONLY_WARNING_SUPPRESS(format-nonliteral) + + return wxCRT_StrftimeA(s, max, format.mb_str(), tm); + + wxGCC_ONLY_WARNING_RESTORE(format-nonliteral) + } #endif // wxNO_IMPLICIT_WXSTRING_ENCODING inline size_t wxStrftime(wchar_t *s, size_t max,