Skip to contentSkip to conversation

How to match switch cases and their absence with coccinelle?

Cocci Denis Efremov 2 participants 4 messages Jan 12, 2021

lore
Share & tools
lore
#0 lore raw

Hi,

Let's suppose I have this pattern:
@fix exists@
position p;
@@

binder_release_work(...)

{
	...
	switch (...) {
*		case BINDER_WORK_NODE: ... break;@p
	}
	...
}

and I want to match binder_release_work() function in drivers/android/binder.c file (linux kernel, master)

Seems like the rule is not enough, it gives nothing:

$ spatch --cocci-file binder.cocci drivers/android/binder.c
init_defs_builtins: /usr/lib64/coccinelle/standard.h
HANDLING: drivers/android/binder.c

1) What can I do to reliable check that there is a special case in a switch?
2) Is it possible to check that there is no case handling with something like:

	switch (...) {
		... when != case BINDER_WORK_NODE: ... break;
	}

Thanks,
Denis
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#1 lore raw
Quoted 7 lines · Denis Efremov

Hi,

Let's suppose I have this pattern:
@fix exists@
position p;
@@

Quoted 7 lines

binder_release_work(...)

{
	...
	switch (...) {
*		case BINDER_WORK_NODE: ... break;@p
	}
	...

Add when any to the outer ...s

Quoted 10 lines

}

and I want to match binder_release_work() function in drivers/android/binder.c file (linux kernel, master)

Seems like the rule is not enough, it gives nothing:

$ spatch --cocci-file binder.cocci drivers/android/binder.c
init_defs_builtins: /usr/lib64/coccinelle/standard.h
HANDLING: drivers/android/binder.c
Quoted 5 lines

1) What can I do to reliable check that there is a special case in a switch?
2) Is it possible to check that there is no case handling with something like:

	switch (...) {
		... when != case BINDER_WORK_NODE: ... break;
	}

I don't know if that will work. But you can do it with two rules. In the first rule, you could put a position variable on the switch, and then in the second rule, you could make a position variable that is required to be different than the first one, and that is also attached to a switch.

julia

Quoted 7 lines

Thanks,
Denis
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#2 lore raw
Quoted 19 lines · Julia Lawall
Quoted 7 lines · Denis Efremov

Hi,

Let's suppose I have this pattern:
@fix exists@
position p;
@@

Quoted 7 lines

binder_release_work(...)

{
	...
	switch (...) {
*		case BINDER_WORK_NODE: ... break;@p
	}
	...
Quoted 1 line

Add when any to the outer ...s

Thanks, this helped.

Quoted 17 lines
Quoted 11 lines

}

and I want to match binder_release_work() function in drivers/android/binder.c file (linux kernel, master)

Seems like the rule is not enough, it gives nothing:

$ spatch --cocci-file binder.cocci drivers/android/binder.c
init_defs_builtins: /usr/lib64/coccinelle/standard.h
HANDLING: drivers/android/binder.c
Quoted 5 lines

1) What can I do to reliable check that there is a special case in a switch?
2) Is it possible to check that there is no case handling with something like:

	switch (...) {
		... when != case BINDER_WORK_NODE: ... break;
	}
Quoted 4 lines

I don't know if that will work. But you can do it with two rules. In the first rule, you could put a position variable on the switch, and then in the second rule, you could make a position variable that is required to be different than the first one, and that is also attached to a switch.

Yes, I use this method currently.

Also I faced the problem when I can't use ... in the beginning of enum, i.e.:

struct binder_work {
        ...
        enum binder_work_type {
                ... // <== will not work
*               BINDER_WORK_NODE,
                ...
        } type;
	...
}

This works:

struct binder_work {
        ...
        enum binder_work_type {
		// BINDER_WORK_TRANSACTION = ..., // also will not work
                BINDER_WORK_TRANSACTION = 1,
                BINDER_WORK_TRANSACTION_COMPLETE,
                BINDER_WORK_RETURN_ERROR,
*               BINDER_WORK_NODE,
                ...
        } type;
        ...
}

Thanks,
Denis
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#3 lore raw
Quoted 48 lines · Denis Efremov
Quoted 19 lines · Julia Lawall
Quoted 7 lines · Denis Efremov

Hi,

Let's suppose I have this pattern:
@fix exists@
position p;
@@

Quoted 7 lines

binder_release_work(...)

{
	...
	switch (...) {
*		case BINDER_WORK_NODE: ... break;@p
	}
	...
Quoted 1 line

Add when any to the outer ...s

Thanks, this helped.

Quoted 17 lines
Quoted 11 lines

}

and I want to match binder_release_work() function in drivers/android/binder.c file (linux kernel, master)

Seems like the rule is not enough, it gives nothing:

$ spatch --cocci-file binder.cocci drivers/android/binder.c
init_defs_builtins: /usr/lib64/coccinelle/standard.h
HANDLING: drivers/android/binder.c
Quoted 5 lines

1) What can I do to reliable check that there is a special case in a switch?
2) Is it possible to check that there is no case handling with something like:

	switch (...) {
		... when != case BINDER_WORK_NODE: ... break;
	}
Quoted 4 lines

I don't know if that will work. But you can do it with two rules. In the first rule, you could put a position variable on the switch, and then in the second rule, you could make a position variable that is required to be different than the first one, and that is also attached to a switch.

Quoted 7 lines

Yes, I use this method currently.

Also I faced the problem when I can't use ... in the beginning of enum, i.e.:

struct binder_work {
        ...
        enum binder_work_type {
                ... // <== will not work

Probably the ... needs a comma.

julia

Quoted 20 lines

* BINDER_WORK_NODE,

                ...
        } type;
	...
}

This works:

struct binder_work {
        ...
        enum binder_work_type {
		// BINDER_WORK_TRANSACTION = ..., // also will not work
                BINDER_WORK_TRANSACTION = 1,
                BINDER_WORK_TRANSACTION_COMPLETE,
                BINDER_WORK_RETURN_ERROR,
*               BINDER_WORK_NODE,
                ...
        } type;
        ...
}
Quoted 3 lines

Thanks,
Denis

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci