stb icon indicating copy to clipboard operation
stb copied to clipboard

Added fallthrough macro to suppress C++17 fallthrough warnings on MSVC and other compilers

Open wmcnamara opened this issue 2 years ago • 13 comments

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.

wmcnamara avatar Feb 17 '22 15:02 wmcnamara

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)

Wohlstand avatar Feb 18 '22 20:02 Wohlstand

Thank you for catching this! I had no idea. I've added the comment to the end of the macro!

wmcnamara avatar Feb 18 '22 20:02 wmcnamara

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:
}

Wohlstand avatar Feb 18 '22 20:02 Wohlstand

P.S. I doubt comments will be assed by macros, will need to run the preprocessor and see what it actually generated on output...

Wohlstand avatar Feb 18 '22 20:02 Wohlstand

Oops, yeah I wasnt really thinking about that when I wrote it...

I'll go ahead and change that

wmcnamara avatar Feb 18 '22 20:02 wmcnamara

I did an exact test:

With GCC: изображение

With CLang: изображение

So, adding that comment at macro has absolutely no sense

Wohlstand avatar Feb 18 '22 20:02 Wohlstand

Now looks way better :fox_face: :+1:

Wohlstand avatar Feb 18 '22 20:02 Wohlstand

It seems redundant to have both the comment and the explicit declaration. Why not this instead?

#elif defined(__GNUC__)
# define STBI_FALLTHROUGH __attribute__((fallthrough))

moon-chilled avatar Mar 09 '22 05:03 moon-chilled

Thats definitely a much nicer solution.

@Wohlstand Does this fix the issue aswell on your end?

wmcnamara avatar Mar 11 '22 20:03 wmcnamara

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.

Wohlstand avatar Mar 11 '22 20:03 Wohlstand

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

Wohlstand avatar Mar 11 '22 20:03 Wohlstand

I think it is reasonable to give worse diagnostics on older compiler versions. But yeah there should be a check for minimum gcc version.

moon-chilled avatar Mar 11 '22 22:03 moon-chilled

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).

Wohlstand avatar Mar 11 '22 22:03 Wohlstand