printf format specifiers are invalid for MinGW which causes misleading warnings #3592

Closed
opened 2020-11-12 00:27:13 +00:00 by Xeverous · 7 comments
Xeverous commented 2020-11-12 00:27:13 +00:00 (Migrated from github.com)

Version/Branch of Dear ImGui:

acb8ef2006 (1.80 WIP (17905))

Back-end/Renderer/Compiler/OS

Back-ends: https://github.com/mosra/magnum-integration/tree/master/src/Magnum/ImGuiIntegration
Compiler: MinGW GCC 10.2.1
Operating System: Windows

My Issue/Question:

Dear ImGui creates misleading warnings that do not match compiler's behavior. ImGui is using incorrect format specifiers. The current ImGui macro (#define IM_FMTARGS(FMT) __attribute__((format(printf, FMT, FMT+1)))) does not match MinGW's implementation.

Standalone, minimal, complete and verifiable example: (see https://github.com/ocornut/imgui/issues/2261)

#define __USE_MINGW_ANSI_STDIO 1

#include <stdio.h>
#include <cstdio>

void func_printf(const char*, ...) __attribute__((format(printf, 1, 2)));
void func_ms_printf(const char*, ...) __attribute__((format(ms_printf, 1, 2)));
void func_gnu_printf(const char*, ...) __attribute__((format(gnu_printf, 1, 2)));

void func_printf(const char*, ...) {}
void func_ms_printf(const char*, ...) {}
void func_gnu_printf(const char*, ...) {}

int main()
{
	size_t x = 1;
	printf         ("%zu\n", x);
	std::printf    ("%zu\n", x);
	func_printf    ("%zu\n", x);
	func_ms_printf ("%zu\n", x);
	func_gnu_printf("%zu\n", x);
}

The code above (compiled with -Wall -Wextra -Wpedantic -std=c++17) generates following warnings:

main.cpp: In function 'int main()':
main.cpp:19:20: warning: unknown conversion type character 'z' in format [-Wformat=]
   19 |  func_printf    ("%zu\n", x);
      |                    ^
main.cpp:19:18: warning: too many arguments for format [-Wformat-extra-args]
   19 |  func_printf    ("%zu\n", x);
      |                  ^~~~~~~
main.cpp:20:20: warning: unknown conversion type character 'z' in format [-Wformat=]
   20 |  func_ms_printf ("%zu\n", x);
      |                    ^
main.cpp:20:18: warning: too many arguments for format [-Wformat-extra-args]
   20 |  func_ms_printf ("%zu\n", x);
      |                  ^~~~~~~

The macro (whether commented or not) in the first line has no impact on warnings (at least in MinGW distribution I use). Warnings appear only for printf and ms_printf format specifiers but MinGW's actual implementation is:

__mingw_ovr
__attribute__((__format__ (gnu_printf, 1, 2))) __MINGW_ATTRIB_NONNULL(1)
int printf (const char *__format, ...)

Thus, if I call printf-style functions from Dear ImGui in my project, I'm getting warnings that %zu is not supported while it actually is. I never get warnings for standard printf functions and they are used in ImGui's implementation. So in short, ImGui's functions are just incorrectly labeled.

**Version/Branch of Dear ImGui:** acb8ef200610e0e3618182c8407681090eaa75ec (1.80 WIP (17905)) **Back-end/Renderer/Compiler/OS** Back-ends: https://github.com/mosra/magnum-integration/tree/master/src/Magnum/ImGuiIntegration Compiler: MinGW GCC 10.2.1 Operating System: Windows **My Issue/Question:** Dear ImGui creates misleading warnings that do not match compiler's behavior. ImGui is using incorrect format specifiers. The current ImGui macro (`#define IM_FMTARGS(FMT) __attribute__((format(printf, FMT, FMT+1)))`) does not match MinGW's implementation. **Standalone, minimal, complete and verifiable example:** _(see https://github.com/ocornut/imgui/issues/2261)_ ```cpp #define __USE_MINGW_ANSI_STDIO 1 #include <stdio.h> #include <cstdio> void func_printf(const char*, ...) __attribute__((format(printf, 1, 2))); void func_ms_printf(const char*, ...) __attribute__((format(ms_printf, 1, 2))); void func_gnu_printf(const char*, ...) __attribute__((format(gnu_printf, 1, 2))); void func_printf(const char*, ...) {} void func_ms_printf(const char*, ...) {} void func_gnu_printf(const char*, ...) {} int main() { size_t x = 1; printf ("%zu\n", x); std::printf ("%zu\n", x); func_printf ("%zu\n", x); func_ms_printf ("%zu\n", x); func_gnu_printf("%zu\n", x); } ``` The code above (compiled with ` -Wall -Wextra -Wpedantic -std=c++17`) generates following warnings: ``` main.cpp: In function 'int main()': main.cpp:19:20: warning: unknown conversion type character 'z' in format [-Wformat=] 19 | func_printf ("%zu\n", x); | ^ main.cpp:19:18: warning: too many arguments for format [-Wformat-extra-args] 19 | func_printf ("%zu\n", x); | ^~~~~~~ main.cpp:20:20: warning: unknown conversion type character 'z' in format [-Wformat=] 20 | func_ms_printf ("%zu\n", x); | ^ main.cpp:20:18: warning: too many arguments for format [-Wformat-extra-args] 20 | func_ms_printf ("%zu\n", x); | ^~~~~~~ ``` The macro (whether commented or not) in the first line has no impact on warnings (at least in MinGW distribution I use). Warnings appear only for `printf` and `ms_printf` format specifiers but MinGW's actual implementation is: ```cpp __mingw_ovr __attribute__((__format__ (gnu_printf, 1, 2))) __MINGW_ATTRIB_NONNULL(1) int printf (const char *__format, ...) ``` Thus, if I call printf-style functions from Dear ImGui in my project, I'm getting warnings that `%zu` is not supported while it actually is. I never get warnings for standard `printf` functions and they are used in ImGui's implementation. So in short, ImGui's functions are just incorrectly labeled.
ocornut commented 2020-11-12 10:38:44 +00:00 (Migrated from github.com)

Hello,
Thanks for your report.
I am not sure I fully understand.
Do you suggest that if we changed the IM_FMTARGS/IM_FMTLIST to use gnu_printf when __GNUC__ is defined it would fix the issue for you?

Hello, Thanks for your report. I am not sure I fully understand. Do you suggest that if we changed the `IM_FMTARGS`/`IM_FMTLIST` to use gnu_printf when `__GNUC__` is defined it would fix the issue for you?
ocornut commented 2020-11-12 10:40:59 +00:00 (Migrated from github.com)
#if !defined(IMGUI_USE_STB_SPRINTF) && (defined(__clang__) || defined(__GNUC__))
#if !defined(IM_PRINTF_TYPE) && defined(__GNUC__)
#define IM_PRINTF_TYPE          gnu_printf
#elif !defined(IM_PRINTF_TYPE)
#define IM_PRINTF_TYPE          printf
#endif
#define IM_FMTARGS(FMT)         __attribute__((format(IM_PRINTF_TYPE, FMT, FMT+1))) // To apply printf-style warnings to our functions.
#define IM_FMTLIST(FMT)         __attribute__((format(IM_PRINTF_TYPE, FMT, 0)))
#else
#define IM_FMTARGS(FMT)
#define IM_FMTLIST(FMT)
#endif
``` #if !defined(IMGUI_USE_STB_SPRINTF) && (defined(__clang__) || defined(__GNUC__)) #if !defined(IM_PRINTF_TYPE) && defined(__GNUC__) #define IM_PRINTF_TYPE gnu_printf #elif !defined(IM_PRINTF_TYPE) #define IM_PRINTF_TYPE printf #endif #define IM_FMTARGS(FMT) __attribute__((format(IM_PRINTF_TYPE, FMT, FMT+1))) // To apply printf-style warnings to our functions. #define IM_FMTLIST(FMT) __attribute__((format(IM_PRINTF_TYPE, FMT, 0))) #else #define IM_FMTARGS(FMT) #define IM_FMTLIST(FMT) #endif ```
ocornut commented 2020-11-12 11:03:06 +00:00 (Migrated from github.com)

Done some tests on my side and pushed a fix for it : 7a135a763c
Thank you!

Done some tests on my side and pushed a fix for it : 7a135a763c66e91cdf78d38ff343ffd06e39dec8 Thank you!
Xeverous commented 2020-11-12 12:19:36 +00:00 (Migrated from github.com)

I would not use just __GNUC__ - it is a MinGW-specific issue, not for all GCCs. More like #if defined(__GNUG__) && defined(__MINGW32__).

I would not use just `__GNUC__` - it is a MinGW-specific issue, not for all GCCs. More like `#if defined(__GNUG__) && defined(__MINGW32__)`.
Xeverous commented 2020-11-12 12:25:25 +00:00 (Migrated from github.com)

Note: __MINGW32__ is also present in 64-bit builds.

Note: `__MINGW32__` is also present in 64-bit builds.
Xeverous commented 2020-11-13 20:01:40 +00:00 (Migrated from github.com)

@ocornut are you sure that the linked commit is the right solution? IMO if there were no problems with __attribute__((format(printf, FMT, FMT+1))); it should only be changed to gnu_printf under MinGW toolchain, not any GCC.

@ocornut are you sure that the linked commit is the right solution? IMO if there were no problems with `__attribute__((format(printf, FMT, FMT+1)));` it should only be changed to `gnu_printf` under MinGW toolchain, not any GCC.
ocornut commented 2020-11-13 20:31:11 +00:00 (Migrated from github.com)
Tested with https://github.com/ocornut/imgui/runs/1397751653?check_suite_focus=true And removed with: https://github.com/ocornut/imgui/runs/1397757816?check_suite_focus=true Amended, you are right. Thank you!
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ocornut/imgui#3592
No description provided.