stb
stb copied to clipboard
Added fallthrough macro to suppress C++17 fallthrough warnings on MSVC and other compilers
This PR adds a STBI_FALLTHROUGH macro to fix warnings on Visual Studio about switch fallthrough.
The STBI_FALLTHROUGH macro is written to be defined if C++17 is enabled, and if it is enabled it will define STBI_FALLTHROUGH as [[fallthrough]], which stops the warnings.
Please keep comment lines (you may append macro being before comment, but don't remove comments), they are required to shut up the GCC 7+ and CLang warnings (even with older standards than C++17, I do have C++11 at my projects, and this warning always pops up if don't add the "// fallthrough
" or "/* fallthrough */
" comment line)
Thank you for catching this! I had no idea. I've added the comment to the end of the macro!
Lol, not that I actually meant, you just now made that both [[fallthrough]]
and comment will appear at C++17 and some special conditions. I meant, comments should be always, at all standards. So:
//C++17 fallthrough macro
#if (__cplusplus >= 201703L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L) && (_MSC_VER >= 1913))
#define STBI_FALLTHROUGH [[fallthrough]]; // <------ Keep this as-is
#else
#define STBI_FALLTHROUGH
#endif
....
switch(x)
{
case 1:
case 2: STBI_FALLTHROUGH /* fallthrough */ // <------------ Comment must be always at the here
case 3:
}
P.S. I doubt comments will be assed by macros, will need to run the preprocessor and see what it actually generated on output...
Oops, yeah I wasnt really thinking about that when I wrote it...
I'll go ahead and change that
I did an exact test:
With GCC:
With CLang:
So, adding that comment at macro has absolutely no sense
Now looks way better :fox_face: :+1:
It seems redundant to have both the comment and the explicit declaration. Why not this instead?
#elif defined(__GNUC__)
# define STBI_FALLTHROUGH __attribute__((fallthrough))
Thats definitely a much nicer solution.
@Wohlstand Does this fix the issue aswell on your end?
It seems redundant to have both the comment and the explicit declaration. Why not this instead?
Old compilers would get hurt (such as GCC 5 and below), gonna check some to make sure all clear.
On GCC 4.8 the next warning appears:
# gcc -c fallthrough.c -o fallthrough.o
fallthrough.c: В функции «main»:
fallthrough.c:11:9: предупреждение: пустая декларация [по умолчанию включена]
__attribute__((fallthrough));
^
"Empty declaration"
p.s. This code I tried to compile:
#include <stdio.h>
int main()
{
int cond = 42;
switch (cond)
{
case 42:
printf("Hello");
__attribute__((fallthrough));
case 43:
printf(" world!\n");
}
return 0;
}
on GCC 9 it doesn't gives any warnings
I think it is reasonable to give worse diagnostics on older compiler versions. But yeah there should be a check for minimum gcc version.
So, the best and most universal solution would be just keeping the /*fallthrough*/
comment which doesn't break old compilers and is approvable by modern compilers to not have the warning here. Speaking about MSVC compilers, they were the most painful compared to others (exclusively everything older than MSVC2015). There is still be the annoying case against friend templates in classes and the painful inability to take the pointer to the member of the sub-structure (everything works, but MSVC just fails).