Skip to contentSkip to conversation

handling C99 declarations after a case statement

DISCUSSION Dan Carpenter 1 participant 1 message Aug 12, 2022 Sparse checker

lore
Share & tools
lore
#0 lore raw

The kernel recently "moved to modern C99". https://lwn.net/Articles/885941/

We still have the -Wdeclaration-after-statement but one thing that we can do now which trips up Sparse is declare variables immediately after a case statement.

int x;
int test(void)

{
	switch (x) {
	case 4:
		int a;  <-- before you would have to add {} around this
		break;
	}
}

Sparse makes the "int a" into a STMT_EXPRESSION instead of a STMT_DECLARATION and test-inspect doesn't like it:

$ ./test-inspect  test.c
test.c:7:17: error: typename in expression
test.c:7:21: error: Expected ; at end of statement
test.c:7:21: error: got a
test.c:7:17: error: undefined identifier 'int'

This causes a Smatch warning as well about a statement without any effect. The first code to trigger this was introduced in linux-next yesterday.

regards,
dan carpenter