31

The newer versions of gcc offer the Wimplicit-fallthrough, which is great to have for most switch statements. However, I have one switch statement where I want to allow fall throughs from all case-statements.

Is there a way to do an explicit fall through? I'd prefer to avoid having to compile with Wno-implicit-fallthrough for this file.

EDIT: I'm looking for a way to make the fall through explicit (if it's possible), not to turn off the warning via a compiler switch or pragma.

dlasalle
  • 1,615
  • 3
  • 19
  • 25
  • Perhaps [this](https://stackoverflow.com/a/3394268/1553090) is what you want. – paddy Jun 13 '17 at 02:29
  • I found the same one. https://stackoverflow.com/questions/3378560/how-to-disable-gcc-warnings-for-a-few-lines-of-code – Retired Ninja Jun 13 '17 at 02:31
  • 1
    Check out the description at [`-Wimplicit-fallthrough`](https://gcc.gnu.org/onlinedocs/gcc-7.1.0/gcc/Warning-Options.html#index-Wimplicit-fallthrough) in the GCC documentation? There won't be any non-extension mechanism — the whole issue is all extension. – Jonathan Leffler Jun 13 '17 at 04:54

3 Answers3

26

Use __attribute__ ((fallthrough))

switch (condition) {
    case 1:
        printf("1 ");
        __attribute__ ((fallthrough));
    case 2:
        printf("2 ");
        __attribute__ ((fallthrough));
    case 3:
        printf("3\n");
        break;
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 14
    Also note that the above is gcc-specific. For C++ there is also a standardized way to write it since C++17: [[fallthrough]]; – lwho Jul 10 '18 at 09:01
8

GCC fallghrough magic comments

You should not use this if you can help it, it is insane, but good to know about:

int main(int argc, char **argv) {
    (void)argv;
    switch (argc) {
        case 0:
            argc = 1;
            // fall through
        case 1:
            argc = 2;
    };
}

prevents the warning on GCC 7.4.0 with:

gcc -Wall -Wextra main.c

man gcc describes how different comments may or not be recognized depending on the value of:

-Wimplicit-fallthrough=n

C++17 [[fallthrough]] attribute

C++17 got a standardized syntax for this: GCC 7, -Wimplicit-fallthrough warnings, and portable way to clear them?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
  • 3
    Thanks for the magic comments. It actually worked.My code needs to be backward compatible with c++03 also and \_\_attribute__ ((fallthrough)) didnt work. The comments worked like magic :) – Rahul Sreeram May 07 '20 at 12:01
4

You should be able to use GCC diagnostic pragmas to disable that particular warning for your source file or some portion of a source file. Try putting this at the top of your file:

#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
David Grayson
  • 84,103
  • 24
  • 152
  • 189
  • Thanks!! Only this answer works on android jni. – Zhou Hongbo Jun 15 '22 at 09:35
  • 1
    To keep it clean, you should use the diagnostic push at the start of the section, then your "ignored" statement, and at the end of the section add the diagnostic pop to restore the old settings. – RufusVS May 27 '23 at 04:02