Linux SPARSE checker discussions
 help / color / mirror / Atom feed
* declarations after a case statement
@ 2026-06-04 13:40 Dan Carpenter
  2026-06-04 13:48 ` Dan Carpenter
  0 siblings, 1 reply; 5+ messages in thread
From: Dan Carpenter @ 2026-06-04 13:40 UTC (permalink / raw)
  To: linux-sparse

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;
	}
}


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: declarations after a case statement
  2026-06-04 13:40 declarations after a case statement Dan Carpenter
@ 2026-06-04 13:48 ` Dan Carpenter
  2026-06-04 14:21   ` Derek M Jones
  0 siblings, 1 reply; 5+ messages in thread
From: Dan Carpenter @ 2026-06-04 13:48 UTC (permalink / raw)
  To: linux-sparse

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: declarations after a case statement
  2026-06-04 13:48 ` Dan Carpenter
@ 2026-06-04 14:21   ` Derek M Jones
  2026-06-05  5:50     ` Dan Carpenter
  0 siblings, 1 reply; 5+ messages in thread
From: Derek M Jones @ 2026-06-04 14:21 UTC (permalink / raw)
  To: linux-sparse

Dan

> 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

> $ ./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;
> }

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: declarations after a case statement
  2026-06-04 14:21   ` Derek M Jones
@ 2026-06-05  5:50     ` Dan Carpenter
  2026-06-05 10:43       ` Derek M Jones
  0 siblings, 1 reply; 5+ messages in thread
From: Dan Carpenter @ 2026-06-05  5:50 UTC (permalink / raw)
  To: Derek M Jones; +Cc: linux-sparse

On Thu, Jun 04, 2026 at 03:21:00PM +0100, Derek M Jones wrote:
> Dan
> 
> > 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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: declarations after a case statement
  2026-06-05  5:50     ` Dan Carpenter
@ 2026-06-05 10:43       ` Derek M Jones
  0 siblings, 0 replies; 5+ messages in thread
From: Derek M Jones @ 2026-06-05 10:43 UTC (permalink / raw)
  To: Dan Carpenter; +Cc: linux-sparse

Dan,

>> 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.

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-06-05 12:11 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-04 13:40 declarations after a case statement Dan Carpenter
2026-06-04 13:48 ` Dan Carpenter
2026-06-04 14:21   ` Derek M Jones
2026-06-05  5:50     ` Dan Carpenter
2026-06-05 10:43       ` Derek M Jones

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox