[Cocci] [PATCH linux-next RESEND] scripts/coccinelle/misc: Warn about NULL check on kmap()
This script removes NULL check on kmap() and all process involved (OOM message ...) Thanks to Jan Kara for explanations. Acked-by: Julia Lawall <julia.law...@lip6.fr> Signed-off-by: Fabian Frederick <f...@skynet.be> --- scripts/coccinelle/misc/kmap.cocci | 43 ++ 1 file changed, 43 insertions(+) create mode 100644 scripts/coccinelle/misc/kmap.cocci diff --git a/scripts/coccinelle/misc/kmap.cocci b/scripts/coccinelle/misc/kmap.cocci new file mode 100644 index 000..9ae4a6e --- /dev/null +++ b/scripts/coccinelle/misc/kmap.cocci @@ -0,0 +1,43 @@ +/// kmap never fails; remove NULL test case. +/// +// Copyright: (C) 2017 Fabian Frederick. GPLv2. +// Comments: - +// Options: --no-includes --include-headers + +virtual patch +virtual org +virtual report +virtual context + +@r2 depends on patch@ +expression E; +position p; +@@ + +E = kmap(...); +- if (!E) { +- ... +- } + +@r depends on context || report || org @ +expression E; +position p; +@@ + +* E = kmap(...); +* if (@p!E) { +* ... +* } + +@script:python depends on org@ +p << r.p; +@@ + +cocci.print_main("kmap() can't fail, NULL check and special process is not needed", p) + +@script:python depends on report@ +p << r.p; +@@ + +msg = "WARNING: NULL check on kmap() result is not needed." +coccilib.report.print_report(p[0], msg) -- 2.9.3 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 1/1 linux-next] scripts/coccinelle/misc: Warn about NULL check on kmap()
This script removes NULL check on kmap() and all process involved (OOM message ...) Thanks to Jan Kara for explanations. Signed-off-by: Fabian Frederick <f...@skynet.be> --- scripts/coccinelle/misc/kmap.cocci | 43 ++ 1 file changed, 43 insertions(+) create mode 100644 scripts/coccinelle/misc/kmap.cocci diff --git a/scripts/coccinelle/misc/kmap.cocci b/scripts/coccinelle/misc/kmap.cocci new file mode 100644 index 000..9ae4a6e --- /dev/null +++ b/scripts/coccinelle/misc/kmap.cocci @@ -0,0 +1,43 @@ +/// kmap never fails; remove NULL test case. +/// +// Copyright: (C) 2017 Fabian Frederick. GPLv2. +// Comments: - +// Options: --no-includes --include-headers + +virtual patch +virtual org +virtual report +virtual context + +@r2 depends on patch@ +expression E; +position p; +@@ + +E = kmap(...); +- if (!E) { +- ... +- } + +@r depends on context || report || org @ +expression E; +position p; +@@ + +* E = kmap(...); +* if (@p!E) { +* ... +* } + +@script:python depends on org@ +p << r.p; +@@ + +cocci.print_main("kmap() can't fail, NULL check and special process is not needed", p) + +@script:python depends on report@ +p << r.p; +@@ + +msg = "WARNING: NULL check on kmap() result is not needed." +coccilib.report.print_report(p[0], msg) -- 2.9.3 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] [PATCH 1/1] scripts/coccinelle/misc: add swap.cocci
On 31 May 2015 at 11:42 Julia Lawall julia.law...@lip6.fr wrote: I propose the extended version below (not currently coccicheck friendly). the extra features are: 1. The original version requires i1 and i2 to be identifiers, eg x and y. This doesn't address the case where they are terms line x-a or x[b]. The fact that the original code contains assignments with both i1 and i2 on the left hand side is enough to ensure that they are appropriate arguments for swap. So they can be changed to expression metavariables. 2. The original patch rule always removed the tmp variable. This is not valid if the tmp variable is used for something else. The new semantic patch separates the introduction of swap (rule r) from the removal of the variable declaration (rule ok and the one folowing). The rule ok checks that this is a function containing an introduced call to swap, and then the rule after removes the declaration if the variable is not used for anything else. Note that the name of the tmp variable is remembered in the invalid three-argument version of sawp. This is then cleaned up in the rule below. 3. The original patch always removed the initialization of the tmp variable. Actually, some code uses the tmp variable afterwards to refer to the old value. In the new semantic patch, the first set of rules considers the cases where the tmp variable is not used, and the last rule is for the case where the tmp variable is stll needed. No cleaning up of the declaration is needed in that case. There is one regression as compared to the original semantic patch: In the file lib/mpi/mpi-pow.c, the temporary variable is not needed after the change, but it is also not removed. It is declared within a loop, and Coccinelle does not realize that it is not needed afterwards, because it is needed on subsequent loop iterations. Trying to adjust the semantic patch to address this issue made it much slower and didn't fix the problem. Perhaps it is easier to rely on gcc to give an unused variable warning, and to clean it up then. Fabian, if you are o with this, do you want to sgenify it ans submit a new patch? Hello Julia, Interesting improvements :) Do we really need tmp variable in swap() ? I tried the version below which removes swap(x,y,tmp)-swap(x,y) rule and had the same result on drivers branch plus it solved a missing tmp. Maybe you want to avoid out of context swap() calls ? Regards, Fabian @r@ expression i1, i2, E; identifier tmp; @@ - tmp = i1; - i1 = i2; - i2 = tmp; + swap(i1, i2); ... when != tmp ? tmp = E @ok exists@ type t1; identifier r.tmp; expression i1,i2; position p; @@ t1@p tmp; ... swap(i1, i2); @@ expression i1,i2; identifier tmp; type t1; position ok.p; @@ -t1@p tmp; ... when strict when != tmp swap(i1, i2); ... // tmp variable still needed @@ expression i1, i2; identifier tmp; @@ tmp = i1; - i1 = i2; - i2 = tmp; + swap(i1, i2); thanks, julia // it may be possible to remove the tmp variable @r@ expression i1, i2, E; identifier tmp; @@ - tmp = i1; - i1 = i2; - i2 = tmp; + swap(i1, i2, tmp); ... when != tmp ? tmp = E @ok exists@ type t1; identifier r.tmp; expression i1,i2; position p; @@ t1@p tmp; ... swap(i1, i2, tmp); @@ expression i1,i2; identifier tmp; type t1; position ok.p; @@ -t1@p tmp; ... when strict when != tmp swap(i1, i2, tmp); ... @depends on r@ expression i1,i2; identifier tmp; @@ swap(i1,i2 - ,tmp ) // tmp variable still needed @@ expression i1, i2; identifier tmp; @@ tmp = i1; - i1 = i2; - i2 = tmp; + swap(i1, i2); ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 1/1] scripts/coccinelle/misc: add swap.cocci
Operations like int a, b, tmp; tmp = a; a = b; b = tmp; can be replaced by int a, b; swap(a, b); This uses kernel.h macro definition and simplifies the code. Thanks to Julia Lawall for suggestions about expressions and sgen Signed-off-by: Fabian Frederick f...@skynet.be --- scripts/coccinelle/misc/swap.cocci | 62 ++ 1 file changed, 62 insertions(+) create mode 100644 scripts/coccinelle/misc/swap.cocci diff --git a/scripts/coccinelle/misc/swap.cocci b/scripts/coccinelle/misc/swap.cocci new file mode 100644 index 000..0f0929c --- /dev/null +++ b/scripts/coccinelle/misc/swap.cocci @@ -0,0 +1,62 @@ +/// Use swap macro instead of complete operation to simplify the code +/// +// # Generated with sgen +// +// Confidence: Moderate +// Copyright: (C) 2015 Fabian Frederick. GPLv2. + +virtual patch +virtual context +virtual org +virtual report + +@r depends on patch !context !org !report@ +identifier i1, i2, tmp; +type t1; +@@ + +- t1 tmp; ++... when any +- tmp = i1; +- i1 = i2; +- i2 = tmp; ++ swap(i1, i2); +...+ + + +// + +@r_context depends on !patch (context || org || report)@ +type t1; +identifier i1, i2, tmp; +position j0, j1; +@@ + +* t1 tmp@j0; ++... when any +* tmp@j1 = i1; +* i1 = i2; +* i2 = tmp; +...+ + +// + +@script:python r_org depends on org@ +j0 r_context.j0; +j1 r_context.j1; +@@ + +msg = swap.cocci +coccilib.org.print_todo(j0[0], msg) +coccilib.org.print_link(j1[0], ) + +// + +@script:python r_report depends on report@ +j0 r_context.j0; +j1 r_context.j1; +@@ + +msg = WARNING: use swap() and remove temporary variable if it's not used elsewhere around line %s. % (j1[0].line) +coccilib.report.print_report(j0[0], msg) + -- 2.4.0 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 1/1 linux-next] scripts/coccinelle/misc/bugon.cocci: update bug_on conversion warning
if()/BUG conversion to BUG_ON must be avoided when there's side effect in condition. The reason being BUG_ON won't execute condition when CONFIG_BUG is not defined. Inspired-by: J. Bruce Fields bfie...@fieldses.org Suggested-by: Julia Lawall julia.law...@lip6.fr Signed-off-by: Fabian Frederick f...@skynet.be --- scripts/coccinelle/misc/bugon.cocci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/coccinelle/misc/bugon.cocci b/scripts/coccinelle/misc/bugon.cocci index 3b7eec2..0d18022 100644 --- a/scripts/coccinelle/misc/bugon.cocci +++ b/scripts/coccinelle/misc/bugon.cocci @@ -57,6 +57,6 @@ coccilib.org.print_todo(p[0], WARNING use BUG_ON) p r.p; @@ -msg=WARNING: Use BUG_ON +msg=WARNING: Use BUG_ON instead of if condition followed by BUG.\nPlease make sure condition has no side effects (see conditional BUG_ON definition in include/asm-generic/bug.h) coccilib.report.print_report(p[0], msg) -- 1.9.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 1/1 linux-next] scripts/coccinelle/misc/boolinit.cocci: fix assignment warnings
Replace Assignment of bool to 0/1 by Assignment of 0/1 to bool Suggested-by: Dan Mick dan.m...@inktank.com Signed-off-by: Fabian Frederick f...@skynet.be --- scripts/coccinelle/misc/boolinit.cocci | 8 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/coccinelle/misc/boolinit.cocci b/scripts/coccinelle/misc/boolinit.cocci index b9abed4..63c4562 100644 --- a/scripts/coccinelle/misc/boolinit.cocci +++ b/scripts/coccinelle/misc/boolinit.cocci @@ -145,13 +145,13 @@ cocci.print_main(WARNING: Comparison of bool to 0/1,p) p1 r3.p1; @@ -cocci.print_main(WARNING: Assignment of bool to 0/1,p1) +cocci.print_main(WARNING: Assignment of 0/1 to bool,p1) @script:python depends on org@ p2 r3.p2; @@ -cocci.print_main(ERROR: Assignment of bool to non-0/1 constant,p2) +cocci.print_main(ERROR: Assignment of non-0/1 constant to bool,p2) @script:python depends on report@ p r1.p; @@ -169,10 +169,10 @@ coccilib.report.print_report(p[0],WARNING: Comparison of bool to 0/1) p1 r3.p1; @@ -coccilib.report.print_report(p1[0],WARNING: Assignment of bool to 0/1) +coccilib.report.print_report(p1[0],WARNING: Assignment of 0/1 to bool) @script:python depends on report@ p2 r3.p2; @@ -coccilib.report.print_report(p2[0],ERROR: Assignment of bool to non-0/1 constant) +coccilib.report.print_report(p2[0],ERROR: Assignment of non-0/1 constant to bool) -- 1.9.3 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] [PATCH V3] scripts/coccinelle/free: Delete NULL test before freeing functions?
On Sat, 28 Jun 2014 11:08:35 +0200 SF Markus Elfring elfr...@users.sourceforge.net wrote: V3: -Update print_main message. Does the discussion topic need also an adjustment? How do you think about my previous update suggestion Deletion of unnecessary checks before specific function calls? https://systeme.lip6.fr/pipermail/cocci/2014-March/000675.html https://lkml.org/lkml/2014/3/5/344 Hello Markus, I didn't see you made the same kind of script. Ok, you can send yours and we forget this one :) Regards, Fabian Regards, Markus ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci