Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use __attribute__((fallthrough)) correctly in gcc

Code sample:

int main(int argc, char **argv)
{
    switch(argc)
    {
    case 0:
        argc = 5;
        __attribute__((fallthrough));

    case 1:
        break;
    }
}

Using gcc 6.3.0, with -std=c11 only, this code gives a warning:

<source>: In function 'main':
7 : <source>:7:3: warning: empty declaration
   __attribute__((fallthrough));
   ^~~~~~~~~~~~~

What is the correct way to use this without eliciting a warning?

like image 752
M.M Avatar asked Jul 27 '17 11:07

M.M


People also ask

What is __ attribute __ (( Fallthrough ))?

The fallthrough attribute with a null statement serves as a fallthrough statement. It hints to the compiler that a statement that falls through to another case label, or user-defined label in a switch statement is intentional and thus the -Wimplicit-fallthrough warning must not trigger.

Where is [[ Fallthrough ]] used?

A fallthrough statement may only be used in a switch statement, where the next statement to be executed is a statement with a case or default label for that switch statement. If the fallthrough statement is inside a loop, the next (labeled) statement must be part of the same iteration of that loop.

What is fallthrough in C?

Fallthrough in C++ Fall through is a type of error that occurs in various programming languages like C, C++, Java, Dart …etc. It occurs in switch-case statements where when we forget to add a break statement and in that case flow of control jumps to the next line.

What is Wimplicit fallthrough?

The GCC compiler has, for some time, offered a warning option (-Wimplicit-fallthrough) intended to catch missing break statements. It will trigger for any case that falls through into a succeeding case unless an explicit marker has been placed to indicate that the behavior is correct.


2 Answers

As previously answered, __attribute__ ((fallthrough)) was introduced in GCC 7. To maintain backward compatibility and clear the fall through warning for both Clang and GCC, you can use the /* fall through */ marker comment.

Applied to your code sample:

int main(int argc, char **argv)
{
    switch(argc)
    {
    case 0:
        argc = 5;
        /* fall through */

    case 1:
        break;
    }

    return 0;
}
like image 135
jyvet Avatar answered Sep 28 '22 10:09

jyvet


Tried to comment previous, but did not have 50 reputation.

So, my experiences:

1) the feature is since gcc 7, so using attribute on older compilers will give warning. therefore I currently use:

#if defined(__GNUC__) && __GNUC__ >= 7
 #define FALL_THROUGH __attribute__ ((fallthrough))
#else
 #define FALL_THROUGH ((void)0)
#endif /* __GNUC__ >= 7 */

and then I use FALL_THROUGH; in code

(Some day I figure out what is needed for clang, but not today)

2) I spent considerable time to try to get the gcc marker comment to work, but nothing I tried worked! Some comment somewere suggested that in order for that to work one has to add -C to gcc arguments (meaning comments will be passed to cc1). Sure gcc 7 documentation doesn't mention anything about this requirement...

like image 20
Tomi Ollila Avatar answered Sep 28 '22 11:09

Tomi Ollila