Coccinelle Archive on lore.kernel.org
 help / color / Atom feed
* [Cocci] How to match switch cases and their absence with coccinelle?
@ 2021-01-12 15:03 Denis Efremov
  2021-01-12 16:13 ` Julia Lawall
  0 siblings, 1 reply; 4+ messages in thread
From: Denis Efremov @ 2021-01-12 15:03 UTC (permalink / raw)
  To: Coccinelle

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
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] How to match switch cases and their absence with coccinelle?
  2021-01-12 15:03 [Cocci] How to match switch cases and their absence with coccinelle? Denis Efremov
@ 2021-01-12 16:13 ` Julia Lawall
  2021-01-12 17:03   ` Denis Efremov
  0 siblings, 1 reply; 4+ messages in thread
From: Julia Lawall @ 2021-01-12 16:13 UTC (permalink / raw)
  To: Denis Efremov; +Cc: Coccinelle



On Tue, 12 Jan 2021, Denis Efremov wrote:

> Hi,
>
> Let's suppose I have this pattern:
> @fix exists@
> position p;
> @@
>
> binder_release_work(...)
> {
> 	...
> 	switch (...) {
> *		case BINDER_WORK_NODE: ... break;@p
> 	}
> 	...

Add when any to the outer ...s

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

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

> Thanks,
> Denis
> _______________________________________________
> Cocci mailing list
> Cocci@systeme.lip6.fr
> https://systeme.lip6.fr/mailman/listinfo/cocci
>
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] How to match switch cases and their absence with coccinelle?
  2021-01-12 16:13 ` Julia Lawall
@ 2021-01-12 17:03   ` Denis Efremov
  2021-01-12 17:06     ` Julia Lawall
  0 siblings, 1 reply; 4+ messages in thread
From: Denis Efremov @ 2021-01-12 17:03 UTC (permalink / raw)
  To: Julia Lawall; +Cc: Coccinelle



On 1/12/21 7:13 PM, Julia Lawall wrote:
> 
> 
> On Tue, 12 Jan 2021, Denis Efremov wrote:
> 
>> Hi,
>>
>> Let's suppose I have this pattern:
>> @fix exists@
>> position p;
>> @@
>>
>> binder_release_work(...)
>> {
>> 	...
>> 	switch (...) {
>> *		case BINDER_WORK_NODE: ... break;@p
>> 	}
>> 	...
> 
> Add when any to the outer ...s

Thanks, this helped.

> 
>> }
>>
>> 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;
>> 	}
> 
> 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
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] How to match switch cases and their absence with coccinelle?
  2021-01-12 17:03   ` Denis Efremov
@ 2021-01-12 17:06     ` Julia Lawall
  0 siblings, 0 replies; 4+ messages in thread
From: Julia Lawall @ 2021-01-12 17:06 UTC (permalink / raw)
  To: Denis Efremov; +Cc: Coccinelle



On Tue, 12 Jan 2021, Denis Efremov wrote:

>
>
> On 1/12/21 7:13 PM, Julia Lawall wrote:
> >
> >
> > On Tue, 12 Jan 2021, Denis Efremov wrote:
> >
> >> Hi,
> >>
> >> Let's suppose I have this pattern:
> >> @fix exists@
> >> position p;
> >> @@
> >>
> >> binder_release_work(...)
> >> {
> >> 	...
> >> 	switch (...) {
> >> *		case BINDER_WORK_NODE: ... break;@p
> >> 	}
> >> 	...
> >
> > Add when any to the outer ...s
>
> Thanks, this helped.
>
> >
> >> }
> >>
> >> 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;
> >> 	}
> >
> > 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

Probably the ... needs a comma.

julia

> *               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
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-12 15:03 [Cocci] How to match switch cases and their absence with coccinelle? Denis Efremov
2021-01-12 16:13 ` Julia Lawall
2021-01-12 17:03   ` Denis Efremov
2021-01-12 17:06     ` Julia Lawall

Coccinelle Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/cocci/0 cocci/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 cocci cocci/ https://lore.kernel.org/cocci \
		cocci@systeme.lip6.fr
	public-inbox-index cocci

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/fr.lip6.systeme.cocci


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git