Bug 567227 - CDT reports syntax error when using GCC's fallthrough attribute
Summary: CDT reports syntax error when using GCC's fallthrough attribute
Status: NEW
Alias: None
Product: CDT
Classification: Tools
Component: cdt-parser (show other bugs)
Version: 9.11.1   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact: Jonah Graham CLA
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-09-21 21:30 EDT by Sven Köhler CLA
Modified: 2020-09-21 22:43 EDT (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sven Köhler CLA 2020-09-21 21:30:29 EDT
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
Comment 1 Marc-André Laperle CLA 2020-09-21 22:04:19 EDT
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"
Comment 2 Sven Köhler CLA 2020-09-21 22:32:45 EDT
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".
Comment 3 Nathan Ridge CLA 2020-09-21 22:37:45 EDT
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).
Comment 4 Sven Köhler CLA 2020-09-21 22:38:51 EDT
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;
	}
}
Comment 5 Marc-André Laperle CLA 2020-09-21 22:39:19 EDT
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).
Comment 6 Sven Köhler CLA 2020-09-21 22:43:55 EDT
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.