Community
Participate
Working Groups
Consider the following code: int x = 0; switch (....) { case 3: x += 4; case 2: x += 3; case 1: x += 2; case 0: x += 1; } With -Wextra, gcc will report warnings since the fall-through could be an accidental error. To suppress that warning, gcc has a fall-through attribute, which should be used like this: #define FALLTHROUGH __attribute__ (( __fallthrough__ )) int x = 0; switch (....) { case 3: x += 4; FALLTHROUGH; case 2: x += 3; FALLTHROUGH; case 1: x += 2; FALLTHROUGH; case 0: x += 1; } However, CDT reports syntax errors. GCC does not report any errors or warnings anymore. Documentation is available here: https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
Which error exactly are you seeing? I don't see a syntax error but I see a Code Analysis warning "No break at the end of case"
I tested again with CDT 9.11.1. I get two yellow markers saying "Syntax error" and one yellow marker with "No break at the end of case".
Could you post a complete, self-contained file which reproduces the syntax error? The fragment you posted is not complete (a switch statement cannot appear at global scope).
Here you go: #define FALLTHROUGH __attribute__(( __fallthrough__ )) void test(void) { int x = 0; switch (1) { case 1: x+=1; FALLTHROUGH; case 2: x+=2; FALLTHROUGH; case 3: x+=3; FALLTHROUGH; case 4: x+=4; } }
Are you using a C file or C++ file? I see the *syntax* error with a .c file but not a with a .cpp (only Code analysis).
I am using a *.c file. I have observed, that there are two markers for each FALLTHROUGH line: both a "syntax error" and the "no break at the end of case". The syntax error is only visible if you hover the mouse over the icons on the left, not if you hover the mouse over the underlined source.