Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Xeverous opened this issue Nov 12, 2020 · 7 comments

Comments

@Xeverous
Copy link

Xeverous commented Nov 12, 2020

Version/Branch of Dear ImGui:

acb8ef2 (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 #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.

@ocornut
Copy link
Owner

ocornut commented Nov 12, 2020

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
Copy link
Owner

ocornut commented Nov 12, 2020

#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 added a commit that referenced this issue Nov 12, 2020
@ocornut
Copy link
Owner

ocornut commented Nov 12, 2020

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

@ocornut ocornut closed this as completed Nov 12, 2020
@Xeverous
Copy link
Author

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

@Xeverous
Copy link
Author

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

@Xeverous
Copy link
Author

@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 added a commit that referenced this issue Nov 13, 2020
ocornut added a commit that referenced this issue Nov 13, 2020
…Rectangle. Fix Clang OSX warnings. Amend #3592 for Mingw only.
@ocornut
Copy link
Owner

ocornut commented Nov 13, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants