Skip to contentSkip to conversation

declarations after a case statement

DISCUSSION Dan Carpenter 2 participants 5 messages Jun 4 – Jun 5 Sparse checker

lore
Share & tools
lore
#0 lore raw

Sparse doesn't let you declare a variable immediately after a case statement but that's something we do in the kernel these days. The "b" declaration is fine, but "c" generates a warning.

regards,
dan carpenter

$ ./sparse test.c
test.c:10:17: error: typename in expression
test.c:10:21: error: Expected ; at end of statement
test.c:10:21: error: got c
test.c:10:17: error: undefined identifier 'int'
$

#include <stdio.h>

void func(int a)

{
	switch (a) {
	case 1:
		printf("hello\n");
		int b;
	case 2:
		int c;
	}
}
#1 lore raw

Same thing for declaration after after a label.

$ ./smatch test.c
test.c:5:9: error: typename in expression
test.c:5:14: error: Expected ; at end of statement
test.c:5:14: error: got b
test.c:5 func() warn: statement has no effect 'char'

void func(int a)

{
        goto out;
out:
        char b;
        b = 1;
}

regards,
dan carpenter

#2 lore raw

Dan

Quoted 1 line

Same thing for declaration after after a label.

Sparse is correct. The usage you describe is a syntax error.

A label may only appear on a statement, not a
declaration
https://c0x.shape-of-code.com/6.8.1.html

Quoted 13 lines
$ ./smatch test.c
test.c:5:9: error: typename in expression
test.c:5:14: error: Expected ; at end of statement
test.c:5:14: error: got b
test.c:5 func() warn: statement has no effect 'char'

void func(int a)

{
         goto out;
out:
         char b;
         b = 1;
}
Signature · 2 lines
Derek M. Jones           Evidence-based software engineering
blog:https://shape-of-code.com
#3 lore raw
Quoted 10 lines · Derek M Jones

Dan

Quoted 1 line

Same thing for declaration after after a label.

Sparse is correct. The usage you describe is a syntax error.

A label may only appear on a statement, not a
declaration
https://c0x.shape-of-code.com/6.8.1.html

That's an awkward thing because GCC allows it and the kernel is doing it.

net/mac80211/mesh_hwmp.c +373

        switch (action) {
        case MPATH_PREQ:
                struct ieee80211_mesh_hwmp_preq_top *preq_elem_top =
                        (void *)hwmp_ie;
                struct ieee80211_mesh_hwmp_preq_bottom *preq_elem_bottom =
                        ieee80211_mesh_hwmp_preq_get_bottom(hwmp_ie);

                orig_addr = preq_elem_top->orig_addr;

regards,
dan carpenter

#4 lore raw

Dan,

Quoted 9 lines
Quoted 6 lines

Sparse is correct. The usage you describe is a syntax error.

A label may only appear on a statement, not a
declaration
https://c0x.shape-of-code.com/6.8.1.html

That's an awkward thing because GCC allows it and the kernel is doing it.

Double checked. I'm out of date. C23 supports the labeling of declarations

6.8.2p4
Any statement or declaration in a compound statement can be preceded by a prefix that declares an identifier as a label name.

Sorry for confusion.

Signature · 2 lines
Derek M. Jones           Evidence-based software engineering
blog:https://shape-of-code.com