Coccinelle Archive on lore.kernel.org
 help / color / Atom feed
* [Cocci] Problem with partial patch generation
@ 2021-02-15 11:24 Denis Efremov
  2021-02-15 11:45 ` Julia Lawall
  2021-02-15 11:47 ` Julia Lawall
  0 siblings, 2 replies; 5+ messages in thread
From: Denis Efremov @ 2021-02-15 11:24 UTC (permalink / raw)
  To: cocci

Hi,

I wrote a simple rule (swap.cocci):

virtual patch

@depends on patch@
identifier tmp;
expression a, b;
type T;
@@

(
- T tmp;
|
- T tmp = 0;
|
- T *tmp = NULL;
)
... when != tmp
- tmp = a;
- a = b;
- b = tmp;
+ swap(a, b);
... when != tmp

I would expect it to remove a local variable only if there is a match
with swap template.

However, it generates "partial" patch on 5.11 linux code:
$ spatch --version
spatch version 1.0.8-00201-g267f9cf8cc82 compiled with OCaml version 4.11.1
$ spatch -D patch --sp-file swap.cocci mm/filemap.c
--- mm/filemap.c
+++ /tmp/cocci-output-445786-88aa66-filemap.c
@@ -2348,7 +2348,7 @@ static int generic_file_buffered_read_ge
        struct file_ra_state *ra = &filp->f_ra;
        pgoff_t index = iocb->ki_pos >> PAGE_SHIFT;
        pgoff_t last_index = (iocb->ki_pos + iter->count + PAGE_SIZE-1) >> PAGE_SHIFT;
-       int i, j, nr_got, err = 0;
+       int i, nr_got, err = 0;
 
        nr = min_t(unsigned long, last_index - index, nr);
 find_page:

How can I improve the rule?

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

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

* Re: [Cocci] Problem with partial patch generation
  2021-02-15 11:24 [Cocci] Problem with partial patch generation Denis Efremov
@ 2021-02-15 11:45 ` Julia Lawall
  2021-02-15 11:47 ` Julia Lawall
  1 sibling, 0 replies; 5+ messages in thread
From: Julia Lawall @ 2021-02-15 11:45 UTC (permalink / raw)
  To: Denis Efremov; +Cc: cocci



On Mon, 15 Feb 2021, Denis Efremov wrote:

> Hi,
>
> I wrote a simple rule (swap.cocci):
>
> virtual patch
>
> @depends on patch@
> identifier tmp;
> expression a, b;
> type T;
> @@
>
> (
> - T tmp;
> |
> - T tmp = 0;
> |
> - T *tmp = NULL;
> )
> ... when != tmp
> - tmp = a;
> - a = b;
> - b = tmp;
> + swap(a, b);
> ... when != tmp
>
> I would expect it to remove a local variable only if there is a match
> with swap template.
>
> However, it generates "partial" patch on 5.11 linux code:
> $ spatch --version
> spatch version 1.0.8-00201-g267f9cf8cc82 compiled with OCaml version 4.11.1
> $ spatch -D patch --sp-file swap.cocci mm/filemap.c
> --- mm/filemap.c
> +++ /tmp/cocci-output-445786-88aa66-filemap.c
> @@ -2348,7 +2348,7 @@ static int generic_file_buffered_read_ge
>         struct file_ra_state *ra = &filp->f_ra;
>         pgoff_t index = iocb->ki_pos >> PAGE_SHIFT;
>         pgoff_t last_index = (iocb->ki_pos + iter->count + PAGE_SIZE-1) >> PAGE_SHIFT;
> -       int i, j, nr_got, err = 0;
> +       int i, nr_got, err = 0;
>
>         nr = min_t(unsigned long, last_index - index, nr);
>  find_page:
>
> How can I improve the rule?

Strange.  It shouldn't do that.

julia
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] Problem with partial patch generation
  2021-02-15 11:24 [Cocci] Problem with partial patch generation Denis Efremov
  2021-02-15 11:45 ` Julia Lawall
@ 2021-02-15 11:47 ` Julia Lawall
  2021-02-15 11:51   ` Denis Efremov
  1 sibling, 1 reply; 5+ messages in thread
From: Julia Lawall @ 2021-02-15 11:47 UTC (permalink / raw)
  To: Denis Efremov; +Cc: cocci



On Mon, 15 Feb 2021, Denis Efremov wrote:

> Hi,
>
> I wrote a simple rule (swap.cocci):
>
> virtual patch
>
> @depends on patch@
> identifier tmp;
> expression a, b;
> type T;
> @@
>
> (
> - T tmp;
> |
> - T tmp = 0;
> |
> - T *tmp = NULL;
> )
> ... when != tmp
> - tmp = a;
> - a = b;
> - b = tmp;
> + swap(a, b);
> ... when != tmp
>
> I would expect it to remove a local variable only if there is a match
> with swap template.
>
> However, it generates "partial" patch on 5.11 linux code:
> $ spatch --version
> spatch version 1.0.8-00201-g267f9cf8cc82 compiled with OCaml version 4.11.1
> $ spatch -D patch --sp-file swap.cocci mm/filemap.c
> --- mm/filemap.c
> +++ /tmp/cocci-output-445786-88aa66-filemap.c
> @@ -2348,7 +2348,7 @@ static int generic_file_buffered_read_ge
>         struct file_ra_state *ra = &filp->f_ra;
>         pgoff_t index = iocb->ki_pos >> PAGE_SHIFT;
>         pgoff_t last_index = (iocb->ki_pos + iter->count + PAGE_SIZE-1) >> PAGE_SHIFT;
> -       int i, j, nr_got, err = 0;
> +       int i, nr_got, err = 0;
>
>         nr = min_t(unsigned long, last_index - index, nr);
>  find_page:
>
> How can I improve the rule?

I don't get a match with the latest version of Coccinelle.

If the latest version of Coccinelle were to become a release, would that
be good enough for you?  Or do you need 1.0.8 to work as well?

julia
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] Problem with partial patch generation
  2021-02-15 11:47 ` Julia Lawall
@ 2021-02-15 11:51   ` Denis Efremov
  2021-02-15 12:03     ` Julia Lawall
  0 siblings, 1 reply; 5+ messages in thread
From: Denis Efremov @ 2021-02-15 11:51 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci



On 2/15/21 2:47 PM, Julia Lawall wrote:
> 
> 
> On Mon, 15 Feb 2021, Denis Efremov wrote:
> 
>> Hi,
>>
>> I wrote a simple rule (swap.cocci):
>>
>> virtual patch
>>
>> @depends on patch@
>> identifier tmp;
>> expression a, b;
>> type T;
>> @@
>>
>> (
>> - T tmp;
>> |
>> - T tmp = 0;
>> |
>> - T *tmp = NULL;
>> )
>> ... when != tmp
>> - tmp = a;
>> - a = b;
>> - b = tmp;
>> + swap(a, b);
>> ... when != tmp
>>
>> I would expect it to remove a local variable only if there is a match
>> with swap template.
>>
>> However, it generates "partial" patch on 5.11 linux code:
>> $ spatch --version
>> spatch version 1.0.8-00201-g267f9cf8cc82 compiled with OCaml version 4.11.1
>> $ spatch -D patch --sp-file swap.cocci mm/filemap.c
>> --- mm/filemap.c
>> +++ /tmp/cocci-output-445786-88aa66-filemap.c
>> @@ -2348,7 +2348,7 @@ static int generic_file_buffered_read_ge
>>         struct file_ra_state *ra = &filp->f_ra;
>>         pgoff_t index = iocb->ki_pos >> PAGE_SHIFT;
>>         pgoff_t last_index = (iocb->ki_pos + iter->count + PAGE_SIZE-1) >> PAGE_SHIFT;
>> -       int i, j, nr_got, err = 0;
>> +       int i, nr_got, err = 0;
>>
>>         nr = min_t(unsigned long, last_index - index, nr);
>>  find_page:
>>
>> How can I improve the rule?
> 
> I don't get a match with the latest version of Coccinelle.
> 
> If the latest version of Coccinelle were to become a release, would that
> be good enough for you?  Or do you need 1.0.8 to work as well?

I planned to submit the swap rule to scripts/coccinelle. However, I can
create more strict pattern for the patch mode. Maybe adding "when strict"
is suitable here?
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] Problem with partial patch generation
  2021-02-15 11:51   ` Denis Efremov
@ 2021-02-15 12:03     ` Julia Lawall
  0 siblings, 0 replies; 5+ messages in thread
From: Julia Lawall @ 2021-02-15 12:03 UTC (permalink / raw)
  To: Denis Efremov; +Cc: cocci



On Mon, 15 Feb 2021, Denis Efremov wrote:

>
>
> On 2/15/21 2:47 PM, Julia Lawall wrote:
> >
> >
> > On Mon, 15 Feb 2021, Denis Efremov wrote:
> >
> >> Hi,
> >>
> >> I wrote a simple rule (swap.cocci):
> >>
> >> virtual patch
> >>
> >> @depends on patch@
> >> identifier tmp;
> >> expression a, b;
> >> type T;
> >> @@
> >>
> >> (
> >> - T tmp;
> >> |
> >> - T tmp = 0;
> >> |
> >> - T *tmp = NULL;
> >> )
> >> ... when != tmp
> >> - tmp = a;
> >> - a = b;
> >> - b = tmp;
> >> + swap(a, b);
> >> ... when != tmp
> >>
> >> I would expect it to remove a local variable only if there is a match
> >> with swap template.
> >>
> >> However, it generates "partial" patch on 5.11 linux code:
> >> $ spatch --version
> >> spatch version 1.0.8-00201-g267f9cf8cc82 compiled with OCaml version 4.11.1
> >> $ spatch -D patch --sp-file swap.cocci mm/filemap.c
> >> --- mm/filemap.c
> >> +++ /tmp/cocci-output-445786-88aa66-filemap.c
> >> @@ -2348,7 +2348,7 @@ static int generic_file_buffered_read_ge
> >>         struct file_ra_state *ra = &filp->f_ra;
> >>         pgoff_t index = iocb->ki_pos >> PAGE_SHIFT;
> >>         pgoff_t last_index = (iocb->ki_pos + iter->count + PAGE_SIZE-1) >> PAGE_SHIFT;
> >> -       int i, j, nr_got, err = 0;
> >> +       int i, nr_got, err = 0;
> >>
> >>         nr = min_t(unsigned long, last_index - index, nr);
> >>  find_page:
> >>
> >> How can I improve the rule?
> >
> > I don't get a match with the latest version of Coccinelle.
> >
> > If the latest version of Coccinelle were to become a release, would that
> > be good enough for you?  Or do you need 1.0.8 to work as well?
>
> I planned to submit the swap rule to scripts/coccinelle. However, I can
> create more strict pattern for the patch mode. Maybe adding "when strict"
> is suitable here?

If there is a release, then the submitted rule can just have a constraint
about the release.

You can try when strict.  Normally, that is to ensure that error paths
aren't excused from having the pattern.  I don't see why it would help
here, but perhaps it's worth a try.

julia
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

end of thread, back to index

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-15 11:24 [Cocci] Problem with partial patch generation Denis Efremov
2021-02-15 11:45 ` Julia Lawall
2021-02-15 11:47 ` Julia Lawall
2021-02-15 11:51   ` Denis Efremov
2021-02-15 12:03     ` 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