Skip to contentSkip to conversation

Problem with partial patch generation

Cocci Denis Efremov 2 participants 5 messages Feb 15, 2021

lore
Share & tools
lore
#0 lore raw

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
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#1 lore raw
Quoted 45 lines · Denis Efremov

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
tmp/cocci-output-445786-88aa66-filemap.c renamed from filemap.c +1 -1
@@ -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:
Quoted 1 line

How can I improve the rule?

Strange. It shouldn't do that.

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

#2 lore raw
In reply to Denis Efremov
Quoted 45 lines · Denis Efremov

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
tmp/cocci-output-445786-88aa66-filemap.c renamed from filemap.c +1 -1
@@ -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:
Quoted 1 line

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
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#3 lore raw
Quoted 49 lines · Julia Lawall
Quoted 45 lines · Denis Efremov

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
tmp/cocci-output-445786-88aa66-filemap.c renamed from filemap.c +1 -1
@@ -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:
Quoted 6 lines
Quoted 1 line

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
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#4 lore raw
Quoted 56 lines · Denis Efremov
Quoted 53 lines · Julia Lawall
Quoted 45 lines · Denis Efremov

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
tmp/cocci-output-445786-88aa66-filemap.c renamed from filemap.c +1 -1
@@ -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:
Quoted 1 line

How can I improve the rule?

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

Quoted 6 lines
Quoted 2 lines

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
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci