From bbebd199987581ee6f344c89bfb02237dc0ab0bd Mon Sep 17 00:00:00 2001 From: Antonio Ospite Date: Tue, 27 Feb 2018 17:03:36 +0100 Subject: [PATCH 1/1] am7xxx: fix C99 conformance for printf & co. when compiling with MinGW MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit C99 format specifiers like "%hhd" are used with log_message() but MinGW (and Windows) does not really supports them, so the compiler suggests to use the "gnu_printf" attribute for the function. However "gnu_printf"is not available on clang so it's not an option, a better alternative is the solution suggested by MinGW at https://sourceforge.net/p/mingw-w64/wiki2/gnu%20printf/ The change fixes the following warnings when compiling with MinGW: .../libam7xxx/src/am7xxx.c: In function ‘log_message’: .../libam7xxx/src/am7xxx.c:647:3: warning: function ‘log_message’ might be a candidate for ‘gnu_printf’ format attribute [-Wsuggest-attribute=format] vfprintf(stderr, fmt, ap); ^~~~~~~~ .../libam7xxx/src/am7xxx.c: In function ‘open_device’: .../libam7xxx/src/am7xxx.c:772:15: warning: unknown conversion type character ‘h’ in format [-Wformat=] debug(ctx, "Cannot set configuration %hhu\n", ^ .../libam7xxx/src/am7xxx.c:65:85: note: in definition of macro ‘debug’ #define debug(ctx, ...) log_message(ctx, AM7XXX_LOG_DEBUG, __func__, 0, __VA_ARGS__) ^~~~~~~~~~~ .../libam7xxx/src/am7xxx.c:772:15: warning: too many arguments for format [-Wformat-extra-args] debug(ctx, "Cannot set configuration %hhu\n", ^ .../libam7xxx/src/am7xxx.c:65:85: note: in definition of macro ‘debug’ #define debug(ctx, ...) log_message(ctx, AM7XXX_LOG_DEBUG, __func__, 0, __VA_ARGS__) ^~~~~~~~~~~ .../libam7xxx/src/am7xxx.c:785:14: warning: unknown conversion type character ‘h’ in format [-Wformat=] debug(ctx, "Cannot claim interface %hhu\n", ^ .../libam7xxx/src/am7xxx.c:65:85: note: in definition of macro ‘debug’ #define debug(ctx, ...) log_message(ctx, AM7XXX_LOG_DEBUG, __func__, 0, __VA_ARGS__) ^~~~~~~~~~~ .../libam7xxx/src/am7xxx.c:785:14: warning: too many arguments for format [-Wformat-extra-args] debug(ctx, "Cannot claim interface %hhu\n", ^ .../libam7xxx/src/am7xxx.c:65:85: note: in definition of macro ‘debug’ #define debug(ctx, ...) log_message(ctx, AM7XXX_LOG_DEBUG, __func__, 0, __VA_ARGS__) ^~~~~~~~~~~ .../libam7xxx/src/am7xxx.c:803:14: warning: unknown conversion type character ‘h’ in format [-Wformat=] debug(ctx, "libusb configuration changed (expected: %hhu, current: %d)\n", ^ .../libam7xxx/src/am7xxx.c:65:85: note: in definition of macro ‘debug’ #define debug(ctx, ...) log_message(ctx, AM7XXX_LOG_DEBUG, __func__, 0, __VA_ARGS__) ^~~~~~~~~~~ .../libam7xxx/src/am7xxx.c:803:14: warning: too many arguments for format [-Wformat-extra-args] debug(ctx, "libusb configuration changed (expected: %hhu, current: %d)\n", ^ .../libam7xxx/src/am7xxx.c:65:85: note: in definition of macro ‘debug’ #define debug(ctx, ...) log_message(ctx, AM7XXX_LOG_DEBUG, __func__, 0, __VA_ARGS__) ^~~~~~~~~~~ --- src/CMakeLists.txt | 4 ++++ src/am7xxx.c | 12 +++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 15c1f6f..d6cdaad 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,10 @@ add_definitions("-D_DEFAULT_SOURCE") # for htole32() add_definitions("-D_POSIX_C_SOURCE=200112L") # for nanosleep() and lroundf() +# Express a preference for C99 format strings when using MinGW, see: +# https://sourceforge.net/p/mingw-w64/wiki2/gnu%20printf/ +add_definitions("-D__USE_MINGW_ANSI_STDIO=1") + # Find packages needed to build library find_package(libusb-1.0 REQUIRED) include_directories(${LIBUSB_1_INCLUDE_DIRS}) diff --git a/src/am7xxx.c b/src/am7xxx.c index 51bce40..4bf9abb 100644 --- a/src/am7xxx.c +++ b/src/am7xxx.c @@ -38,6 +38,16 @@ #define __attribute__(x) /* NOTHING */ #endif +/* + * Fix printf format when compiling for Windows with MinGW, see: + * https://sourceforge.net/p/mingw-w64/wiki2/gnu%20printf/ + */ +#ifdef __MINGW_PRINTF_FORMAT + #define AM7XXX_PRINTF_FORMAT __MINGW_PRINTF_FORMAT +#else + #define AM7XXX_PRINTF_FORMAT printf +#endif + /* Control shared library symbols visibility */ #if defined _WIN32 || defined __CYGWIN__ #define AM7XXX_PUBLIC __declspec(dllexport) @@ -57,7 +67,7 @@ static void log_message(am7xxx_context *ctx, const char *function_name, int line, const char *fmt, - ...) __attribute__ ((format (printf, 5, 6))); + ...) __attribute__ ((format (AM7XXX_PRINTF_FORMAT, 5, 6))); #define fatal(...) log_message(NULL, AM7XXX_LOG_FATAL, __func__, __LINE__, __VA_ARGS__) #define error(ctx, ...) log_message(ctx, AM7XXX_LOG_ERROR, __func__, __LINE__, __VA_ARGS__) -- 2.1.4