Skip to contentSkip to conversation

Adjusting SmPL script “ptr_ret.cocci”?

PATCH New Markus Elfring 2 participants 4 messages Sep 7, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hello,

I have taken another look at a known script for the semantic patch language.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/scripts/coccinelle/api/ptr_ret.cocci?id=1e3778cb223e861808ae0daccf353536e7573eed#n3

I got the impression that duplicate SmPL code can be reduced here. So I tried the following approach out.

…
@depends on patch@
expression ptr;
@@
(
(
- if (IS_ERR(ptr)) return PTR_ERR(ptr); else return 0; |
- if (IS_ERR(ptr)) return PTR_ERR(ptr); return 0; )
+ return PTR_ERR_OR_ZERO(ptr);
|
- (IS_ERR(ptr) ? PTR_ERR(ptr) : 0)
+ PTR_ERR_OR_ZERO(ptr)
)
…

Unfortunately, I got the following information then for a test transformation.

elfring@Sonne:~/Projekte/Linux/next-patched> spatch -D patch scripts/coccinelle/api/ptr_ret.cocci drivers/spi/spi-gpio.c …
29: no available token to attach to

It seems that the Coccinelle software “1.0.7-00218-gf284bf36” does not like the addition of the shown return statement after a nested SmPL disjunction. But the following SmPL code variant seems to work as expected.

…
@depends on patch@
expression ptr;
@@
(
- if (IS_ERR(ptr)) return PTR_ERR(ptr); else return 0;
+ return PTR_ERR_OR_ZERO(ptr);
|
- if (IS_ERR(ptr)) return PTR_ERR(ptr); return 0;
+ return PTR_ERR_OR_ZERO(ptr);
|
- (IS_ERR(ptr) ? PTR_ERR(ptr) : 0)
+ PTR_ERR_OR_ZERO(ptr)
)
…

How do you think about to reduce subsequent SmPL rules also according to a possible recombination of affected implementation details?

Regards,
Markus

#1 lore raw
Quoted 55 lines · Markus Elfring

Hello,

I have taken another look at a known script for the semantic patch language.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/scripts/coccinelle/api/ptr_ret.cocci?id=1e3778cb223e861808ae0daccf353536e7573eed#n3

I got the impression that duplicate SmPL code can be reduced here. So I tried the following approach out.

…
@depends on patch@
expression ptr;
@@
(
(
- if (IS_ERR(ptr)) return PTR_ERR(ptr); else return 0; |
- if (IS_ERR(ptr)) return PTR_ERR(ptr); return 0; )
+ return PTR_ERR_OR_ZERO(ptr);
|
- (IS_ERR(ptr) ? PTR_ERR(ptr) : 0)
+ PTR_ERR_OR_ZERO(ptr)
)
…

Unfortunately, I got the following information then for a test transformation.

elfring@Sonne:~/Projekte/Linux/next-patched> spatch -D patch scripts/coccinelle/api/ptr_ret.cocci drivers/spi/spi-gpio.c …
29: no available token to attach to

It seems that the Coccinelle software “1.0.7-00218-gf284bf36” does not like the addition of the shown return statement after a nested SmPL disjunction. But the following SmPL code variant seems to work as expected.

…
@depends on patch@
expression ptr;
@@
(
- if (IS_ERR(ptr)) return PTR_ERR(ptr); else return 0;
+ return PTR_ERR_OR_ZERO(ptr);
|
- if (IS_ERR(ptr)) return PTR_ERR(ptr); return 0;
+ return PTR_ERR_OR_ZERO(ptr);
|
- (IS_ERR(ptr) ? PTR_ERR(ptr) : 0)
+ PTR_ERR_OR_ZERO(ptr)
)
…

Quoted 2 lines

How do you think about to reduce subsequent SmPL rules also according to a possible recombination of affected implementation details?

There is not going to be any change with respect to this issue. It's fine when replacing one statement by another, but introduces complexity when removing something more complex. And there's not point to have something that works in only one special case.

julia

#2 lore raw
Quoted 4 lines
Quoted 2 lines

How do you think about to reduce subsequent SmPL rules also according to a possible recombination of affected implementation details?

There is not going to be any change with respect to this issue.

I am curious if the software situation will be reconsidered any further under other circumstances.

Quoted 2 lines

It's fine when replacing one statement by another, but introduces complexity when removing something more complex.

Thanks for such information.

Quoted 1 line

And there's not point to have something that works in only one special case.

Will corresponding software development concerns be eventually discussed a bit more?

Regards,
Markus

#3 lore raw

PATCH 1/1 Coccinelle: Reduce rules in SmPL script “ptr_ret.cocci”

In reply to Julia Lawall

From: Markus Elfring <[email protected]>
Date: Sat, 7 Sep 2019 18:30:32 +0200

This SmPL script contains duplicate code.
Thus reduce it to the required source code search functionality by the means of the semantic patch language.

---
 scripts/coccinelle/api/ptr_ret.cocci | 61 +++++-----------------------
 1 file changed, 10 insertions(+), 51 deletions(-)
scripts/coccinelle/api/ptr_ret.cocci +10 -51
index e76cd5d90a8a..f4b1ce4a06d0 100644
@@ -20,44 +20,28 @@ virtual report
 @depends on patch@
 expression ptr;
 @@
-
+(
 - if (IS_ERR(ptr)) return PTR_ERR(ptr); else return 0;
 + return PTR_ERR_OR_ZERO(ptr);
-
-@depends on patch@
-expression ptr;
-@@
-
+|
 - if (IS_ERR(ptr)) return PTR_ERR(ptr); return 0;
 + return PTR_ERR_OR_ZERO(ptr);
-
-@depends on patch@
-expression ptr;
-@@
-
+|
 - (IS_ERR(ptr) ? PTR_ERR(ptr) : 0)
 + PTR_ERR_OR_ZERO(ptr)
+)
 
 @r1 depends on !patch@
 expression ptr;
 position p1;
 @@
-
+(
 * if@p1 (IS_ERR(ptr)) return PTR_ERR(ptr); else return 0;
-
-@r2 depends on !patch@
-expression ptr;
-position p2;
-@@
-
-* if@p2 (IS_ERR(ptr)) return PTR_ERR(ptr); return 0;
-
-@r3 depends on !patch@
-expression ptr;
-position p3;
-@@
-
-* IS_ERR@p3(ptr) ? PTR_ERR(ptr) : 0
+|
+* if@p1 (IS_ERR(ptr)) return PTR_ERR(ptr); return 0;
+|
+* IS_ERR@p1(ptr) ? PTR_ERR(ptr) : 0
+)
 
 @script:python depends on org@
 p << r1.p1;
@@ -65,33 +49,8 @@ p << r1.p1;
 
 coccilib.org.print_todo(p[0], "WARNING: PTR_ERR_OR_ZERO can be used")
 
-
-@script:python depends on org@
-p << r2.p2;
-@@
-
-coccilib.org.print_todo(p[0], "WARNING: PTR_ERR_OR_ZERO can be used")
-
-@script:python depends on org@
-p << r3.p3;
-@@
-
-coccilib.org.print_todo(p[0], "WARNING: PTR_ERR_OR_ZERO can be used")
-
 @script:python depends on report@
 p << r1.p1;
 @@
 
 coccilib.report.print_report(p[0], "WARNING: PTR_ERR_OR_ZERO can be used")
-
-@script:python depends on report@
-p << r2.p2;
-@@
-
-coccilib.report.print_report(p[0], "WARNING: PTR_ERR_OR_ZERO can be used")
-
-@script:python depends on report@
-p << r3.p3;
-@@
-
-coccilib.report.print_report(p[0], "WARNING: PTR_ERR_OR_ZERO can be used")

--
2.23.0