GCC 7, transparent warnings and portable way to clean them?

We catch warnings from GCC 7 for an implicit fall in the switch statement. We used to clean them according to the Clan (which is the reason for the comment below):

g++ -DNDEBUG -g2 -O3 -std=c++17 -Wall -Wextra -fPIC -c authenc.cpp
asn.cpp: In member function โ€˜void EncodedObjectFilter::Put(const byte*, size_t)โ€™:
asn.cpp:359:18: warning: this statement may fall through [-Wimplicit-fallthrough=]
    m_state = BODY;  // fall through
                  ^
asn.cpp:361:3: note: here
   case BODY:
   ^~~~

The GCC Guide allows use __attribute__ ((fallthrough))but is not portable. The manual also says: "... you can also add a missing comment to turn off the warning," but it only offers FALLTHRU(is this really the only choice?):

switch (cond)
  {
  case 1:
    bar (0);
    /* FALLTHRU */
  default:
    โ€ฆ
  }

Is there a portable way to clear a drop through a warning for Clang and GCC? If so, what is it?

+30
source share
4 answers

GCC , :

  m_state = BODY;
  // fall through
case BODY:

case; }.

fall through , GCC. FALLTHRU. . -Wimplicit-fallthrough. Red Hat Developer.

( Clang, (r308163) , .)

, , . , , -C GCC). , , -C , , , keep_comments_cpp .

+33

++ 17 [[fallthrough]]

:

int main(int argc, char **argv) {
    switch (argc) {
        case 0:
            argc = 1;
            [[fallthrough]];
        case 1:
            argc = 2;
    };
}

:

g++ -std=c++17 -Wimplicit-fallthrough main.cpp

[[fallthrough]];, GCC :

main.cpp: In function โ€˜int main():
main.cpp:5:15: warning: this statement may fall through [-Wimplicit-fallthrough=]
             argc = 1;
             ~~^~~
main.cpp:6:9: note: here
         case 1:
         ^~~~

, , : case ( case 1) , break.

:

#include <cstdlib>

[[noreturn]] void my_noreturn_func() {
    exit(1);
}

int main(int argc, char **argv) {
    // Erm, an actual break
    switch (argc) {
        case 0:
            argc = 1;
            break;
        case 1:
            argc = 2;
    }

    // Return also works.
    switch (argc) {
        case 0:
            argc = 1;
            return 0;
        case 1:
            argc = 2;
    }

    // noreturn functions are also work.
    // https://stackoverflow.com/questions/10538291/what-is-the-point-of-noreturn/47444782#47444782
    switch (argc) {
        case 0:
            argc = 1;
            my_noreturn_func();
        case 1:
            argc = 2;
    }

    // Empty case synonyms are fine.
    switch (argc) {
        case 0:
        case 1:
            argc = 2;
    }

    // Magic comment mentioned at:
    // https://stackoverflow.com/a/45137452/895245
    switch (argc) {
        case 0:
            argc = 1;
            // fall through
        case 1:
            argc = 2;
    }

    switch (argc) {
        // GCC extension for pre C++17.
        case 0:
            argc = 1;
            __attribute__ ((fallthrough));
        case 1:
            argc = 2;
    }

    switch (argc) {
        // GCC examines all braches.
        case 0:
            if (argv[0][0] == 'm') {
                [[fallthrough]];
            } else {
                return 0;
            }
        case 1:
            argc = 2;
    }
}

, GCC , - [[fallthrough]]; break return.

, , GEM5 :

#if defined __has_cpp_attribute
    #if __has_cpp_attribute(fallthrough)
        #define MY_FALLTHROUGH [[fallthrough]]
    #else
        #define MY_FALLTHROUGH
    #endif
#else
    #define MY_FALLTHROUGH
#endif

: https://en.cppreference.com/w/cpp/language/attributes/fallthrough

GCC 7.4.0, Ubuntu 18.04.

C : GCC 7, -Wimplicit-fallthrough , ?

+6

, ++ 17 [[fallthrough]]. g++ ( , 7.3.0). -Wextra break; , [[fallthrough]], . , , ++ 17.

, . :

... -std=c++17 -Wextra ...

.

:

... -std=c++17 -Wextra -Wimplicit-fallthrough ...

It only works with the C ++ 17 attribute. I have not tried, but I assume that the order may be important. However, the implementation -Wextramay be smart enough not to modify a warning that is already configured.

case 1:
  blah = 5;
  [[fallthrough]];  // works only if "... -Wimplicit-fallthrough ..." is used

The old attribute was __attribute__((fallthrough)). g ++ may also recognize some comments.

0
source

Pure solution C:

int r(int a) {
    switch(a) {
    case 0:
        a += 3;
    case 1:
        a += 2;
    default:
        a += a;
    }
    return a;
}

becomes:

int h(int a) {
    switch(a) {
    case 0:
        a += 3;
        goto one;
    one:
    case 1:
        a += 2;
        goto others;
    others:
    default:
        a += a;
    }
    return a;
}
0
source

All Articles