GNOME Bugzilla – Bug 793074
g_message() does not get -Wformat warnings when compiling with G_LOG_USE_STRUCTURED
Last modified: 2018-02-02 09:11:34 UTC
Because g_message() (and the other macros) pass format parameters as part of a larger set of varargs to g_log_structured(), there is (and can be) no G_GNUC_PRINTF annotation for g_log_structured(), and hence they don’t get checked. This is a regression on the previous version of g_message(). The fix for this is to replace #define g_message(...) g_log_structured (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, \ "CODE_FILE", __FILE__, \ "CODE_LINE", G_STRINGIFY (__LINE__), \ "CODE_FUNC", G_STRFUNC, \ "MESSAGE", __VA_ARGS__) with #define g_message(...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, __FILE__, G_STRINGIFY (__LINE__), G_STRFUNC, __VA_ARGS__) g_log_structured_standard() (better name suggestions welcome) would be a new semi-private helper function which has the appropriate G_GNUC_PRINTF annotation.
Created attachment 367757 [details] [review] gmessages: Fix -Wformat warnings for g_message() and friends When compiling with G_LOG_USE_STRUCTURED, g_message(), g_debug(), etc. use g_log_structured(). The message format string and its format arguments are passed as the final set of arguments in a longer varargs list, which includes the log domain and level (and other) fields. Passing the message format in this way means it’s not possible for the compiler to know to check its format placeholders when compiling with -Wformat. Fix support for this by adding a new semi-private helper function, _g_log_structured_standard(), which only uses varargs for the message format and its arguments, and uses fixed arguments for the other fields. This is then converted to a set of GLogFields and passed to g_log_structured() as normal. Support for -Wformat when compiling *without* G_LOG_USE_STRUCTURED was never broken. Signed-off-by: Philip Withnall <withnall@endlessm.com>
Created attachment 367758 [details] [review] gmessages: Fix -Wformat warnings for g_message() and friends When compiling with G_LOG_USE_STRUCTURED, g_message(), g_debug(), etc. use g_log_structured(). The message format string and its format arguments are passed as the final set of arguments in a longer varargs list, which includes the log domain and level (and other) fields. Passing the message format in this way means it’s not possible for the compiler to know to check its format placeholders when compiling with -Wformat. Fix support for this by adding a new semi-private helper function, _g_log_structured_standard(), which only uses varargs for the message format and its arguments, and uses fixed arguments for the other fields. This is then converted to a set of GLogFields and passed to g_log_structured() as normal. Support for -Wformat when compiling *without* G_LOG_USE_STRUCTURED was never broken. Signed-off-by: Philip Withnall <withnall@endlessm.com>
Review of attachment 367758 [details] [review]: Looks good to me. Will gtk-doc complain about this function being undocumented ?
(In reply to Matthias Clasen from comment #3) > Review of attachment 367758 [details] [review] [review]: > > Will gtk-doc complain about this function being > undocumented ? Shouldn’t do; the function’s not listed in glib-sections.txt.
Merged to master, thanks. Attachment 367758 [details] pushed as 32cc60d - gmessages: Fix -Wformat warnings for g_message() and friends