Skip to contentSkip to conversation

coccinelle: misc: add swap script

PATCH In review Denis Efremov 3 participants 16 messages Feb 16, 2021 – Apr 4, 2021 1 message not in the archive

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

PATCH 1/1 coccinelle: misc: add swap script

Check for opencoded swap() implementation.

---
 scripts/coccinelle/misc/swap.cocci | 77 ++++++++++++++++++++++++++++++
 1 file changed, 77 insertions(+)
 create mode 100644 scripts/coccinelle/misc/swap.cocci
scripts/coccinelle/misc/swap.cocci new file +77 -0
new file mode 100644
index 000000000000..38227a5d0855
@@ -0,0 +1,77 @@
+// SPDX-License-Identifier: GPL-2.0-only
+///
+/// Check for opencoded swap() implementation.
+///
+// Confidence: High
+// Copyright: (C) 2021 Denis Efremov ISPRAS
+// Options: --no-includes --include-headers
+//
+// Keywords: swap
+//
+
+virtual patch
+virtual org
+virtual report
+virtual context
+
+@r depends on !patch@
+identifier tmp;
+expression a, b;
+type T;
+position p;
+@@
+
+(
+* T tmp;
+|
+* T tmp = 0;
+|
+* T *tmp = NULL;
+)
+... when != tmp
+* tmp = a;
+* a = b;@p
+* b = tmp;
+... when != tmp
+
+@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
+
+@depends on patch@
+identifier tmp;
+expression a, b;
+@@
+
+- tmp = a;
+- a = b;
+- b = tmp;
++ swap(a, b);
+
+@script:python depends on report@
+p << r.p;
+@@
+
+coccilib.report.print_report(p[0], "WARNING opportunity for swap()")
+
+@script:python depends on org@
+p << r.p;
+@@
+
+coccilib.org.print_todo(p[0], "WARNING opportunity for swap()")
Signature · 6 lines
2.26.2

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci
#1 lore raw
Quoted 4 lines
+- tmp = a;
+- a = b;
+- b = tmp;

++ swap(a, b);

How do you think about to use the following SmPL code variant?
* Omission of a few leading space characters
* Keeping a semicolon unmodified in a line

+-tmp = a;
+-a = b;
+-b = tmp

++swap(a, b)
+;

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

maintainer
#2 lore raw
In reply to Denis Efremov
Quoted 9 lines

+@depends on patch@

scripts/coccinelle/misc/swap.cocci new file +8 -0
+identifier tmp;
+expression a, b;
+type T;
+@@
+
+(
+- T tmp;
+|

scripts/coccinelle/misc/swap.cocci line 55

Quoted 10 lines
scripts/coccinelle/misc/swap.cocci new file +10 -0
+- T tmp = 0;
+|
+- T *tmp = NULL;
+)
+... when != tmp
+- tmp = a;
+- a = b;
+- b = tmp;
++ swap(a, b);
+... when != tmp

In this rule and the next one, if you remove the final ; from the b = tmp line and from the swap line, and put it into context code afterwards, them the generated code looks better on cases like fs/xfs/xfs_inode.c in the function xfs_lock_two_inodes where two successive swap calls are generated.

There are also some cases such as drivers/net/wireless/ath/ath5k/phy.c in the function ath5k_hw_get_median_noise_floor where the swap code makes up a whole if branch. In this cases it would be good to remove the {}.

julia

Quoted 23 lines
scripts/coccinelle/misc/swap.cocci new file +22 -1
+
+@depends on patch@
+identifier tmp;
+expression a, b;
+@@
+
+- tmp = a;
+- a = b;
+- b = tmp;
++ swap(a, b);
+
+@script:python depends on report@
+p << r.p;
+@@
+
+coccilib.report.print_report(p[0], "WARNING opportunity for swap()")
+
+@script:python depends on org@
+p << r.p;
+@@
+
+coccilib.org.print_todo(p[0], "WARNING opportunity for swap()")
--
Quoted 3 lines

2.26.2

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

#3 lore raw
Quoted 26 lines · Julia Lawall
Quoted 9 lines

+@depends on patch@

scripts/coccinelle/misc/swap.cocci new file +8 -0
+identifier tmp;
+expression a, b;
+type T;
+@@
+
+(
+- T tmp;
+|

scripts/coccinelle/misc/swap.cocci line 55

Quoted 10 lines
scripts/coccinelle/misc/swap.cocci new file +10 -0
+- T tmp = 0;
+|
+- T *tmp = NULL;
+)
+... when != tmp
+- tmp = a;
+- a = b;
+- b = tmp;
++ swap(a, b);
+... when != tmp

In this rule and the next one, if you remove the final ; from the b = tmp line and from the swap line, and put it into context code afterwards, them the generated code looks better on cases like fs/xfs/xfs_inode.c in the function xfs_lock_two_inodes where two successive swap calls are generated.

Quoted 5 lines

There are also some cases such as drivers/net/wireless/ath/ath5k/phy.c in the function ath5k_hw_get_median_noise_floor where the swap code makes up a whole if branch.

In this cases it would be good to remove the {}.

How this can be handled?

If I use this pattern:
@depends on patch@
identifier tmp;
expression a, b;
@@

(
if (...)
- {

-       tmp = a;
-       a = b;
-       b = tmp
+       swap(a, b)
        ;
- }
|
-       tmp = a;
-       a = b;
-       b = tmp
+       swap(a, b)
        ;

)

The tool fails with error:

EXN: Failure("rule starting on line 58: already tagged token:\nC code context\nFile \"drivers/net/wireless/ath/ath5k/phy.c\", line 1574, column 4, charpos = 41650\n around = 'sort',\n whole content = \t\t\t\tsort[j - 1] = tmp;") in drivers/net/wireless/ath/ath5k/phy.c

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

maintainer
#4 lore raw
Quoted 60 lines · Denis Efremov
Quoted 26 lines · Julia Lawall
Quoted 9 lines

+@depends on patch@

scripts/coccinelle/misc/swap.cocci new file +8 -0
+identifier tmp;
+expression a, b;
+type T;
+@@
+
+(
+- T tmp;
+|

scripts/coccinelle/misc/swap.cocci line 55

Quoted 10 lines
scripts/coccinelle/misc/swap.cocci new file +10 -0
+- T tmp = 0;
+|
+- T *tmp = NULL;
+)
+... when != tmp
+- tmp = a;
+- a = b;
+- b = tmp;
++ swap(a, b);
+... when != tmp

In this rule and the next one, if you remove the final ; from the b = tmp line and from the swap line, and put it into context code afterwards, them the generated code looks better on cases like fs/xfs/xfs_inode.c in the function xfs_lock_two_inodes where two successive swap calls are generated.

Quoted 5 lines

There are also some cases such as drivers/net/wireless/ath/ath5k/phy.c in the function ath5k_hw_get_median_noise_floor where the swap code makes up a whole if branch.

In this cases it would be good to remove the {}.

How this can be handled?

If I use this pattern:
@depends on patch@
identifier tmp;
expression a, b;
@@

(
if (...)
- {

(quoted diff) +1 -4
-       tmp = a;
-       a = b;
-       b = tmp
+       swap(a, b)
        ;
- }

|

(quoted diff) +1 -3
-       tmp = a;
-       a = b;
-       b = tmp
+       swap(a, b)
        ;

)

Quoted 6 lines

The tool fails with error:

EXN: Failure("rule starting on line 58: already tagged token:\nC code context\nFile \"drivers/net/wireless/ath/ath5k/phy.c\", line 1574, column 4, charpos = 41650\n around = 'sort',\n whole content = \t\t\t\tsort[j - 1] = tmp;") in drivers/net/wireless/ath/ath5k/phy.c

A disjunction basically says "at this node in the cfg, can I match the first patter, or can I match the second pattern, etc." Unfortunately in this case the two branches start matching at different nodes, so the short circuit aspect of a disjunction isn't used, and it matches both patterns.

The solution is to just make two rules. The first for the if case and the second for everything else.

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

#5 lore raw
Quoted 71 lines · Julia Lawall
Quoted 60 lines · Denis Efremov
Quoted 26 lines · Julia Lawall
Quoted 9 lines

+@depends on patch@

(quoted diff) +8 -0
+identifier tmp;
+expression a, b;
+type T;
+@@
+
+(
+- T tmp;
+|

Review

Quoted 10 lines
(quoted diff) +10 -0
+- T tmp = 0;
+|
+- T *tmp = NULL;
+)
+... when != tmp
+- tmp = a;
+- a = b;
+- b = tmp;
++ swap(a, b);
+... when != tmp

In this rule and the next one, if you remove the final ; from the b = tmp line and from the swap line, and put it into context code afterwards, them the generated code looks better on cases like fs/xfs/xfs_inode.c in the function xfs_lock_two_inodes where two successive swap calls are generated.

Quoted 5 lines

There are also some cases such as drivers/net/wireless/ath/ath5k/phy.c in the function ath5k_hw_get_median_noise_floor where the swap code makes up a whole if branch.

In this cases it would be good to remove the {}.

How this can be handled?

If I use this pattern:
@depends on patch@
identifier tmp;
expression a, b;
@@

(
if (...)
- {

(quoted diff) +1 -4
-       tmp = a;
-       a = b;
-       b = tmp
+       swap(a, b)
        ;
- }

|

(quoted diff) +1 -3
-       tmp = a;
-       a = b;
-       b = tmp
+       swap(a, b)
        ;

)

Quoted 6 lines

The tool fails with error:

EXN: Failure("rule starting on line 58: already tagged token:\nC code context\nFile \"drivers/net/wireless/ath/ath5k/phy.c\", line 1574, column 4, charpos = 41650\n around = 'sort',\n whole content = \t\t\t\tsort[j - 1] = tmp;") in drivers/net/wireless/ath/ath5k/phy.c

Quoted 8 lines

A disjunction basically says "at this node in the cfg, can I match the first patter, or can I match the second pattern, etc." Unfortunately in this case the two branches start matching at different nodes, so the short circuit aspect of a disjunction isn't used, and it matches both patterns.

The solution is to just make two rules. The first for the if case and the second for everything else.

if (...)
- {

-       tmp = a;
-       a = b;
-       b = tmp
+       swap(a, b)
        ;

- }

This produces "single-line ifs".
Maybe generated patches can be improved somehow?
Moving -+; doesn't help in this case.

drivers/net/wireless/ath/ath9k/calib.c +1 -5
diff -u -p a/drivers/net/wireless/ath/ath9k/calib.c b/drivers/net/wireless/ath/ath9k/calib.c
@@ -32,11 +32,7 @@ static int16_t ath9k_hw_get_nf_hist_mid(
 
        for (i = 0; i < ATH9K_NF_CAL_HIST_MAX - 1; i++) {
                for (j = 1; j < ATH9K_NF_CAL_HIST_MAX - i; j++) {
-                       if (sort[j] > sort[j - 1]) {
-                               nfval = sort[j];
-                               sort[j] = sort[j - 1];
-                               sort[j - 1] = nfval;
-                       }
+                       if (sort[j] > sort[j - 1]) swap(sort[j], sort[j - 1]);
                }
        }
        nfval = sort[(ATH9K_NF_CAL_HIST_MAX - 1) >> 1];
drivers/net/wireless/ath/ath9k/ar9003_calib.c +4 -12
diff -u -p a/drivers/net/wireless/ath/ath9k/ar9003_calib.c b/drivers/net/wireless/ath/ath9k/ar9003_calib.c
@@ -1011,19 +1011,11 @@ static void __ar955x_tx_iq_cal_sort(stru
                for (ix = 0; ix < MAXIQCAL - 1; ix++) {
                        for (iy = ix + 1; iy <= MAXIQCAL - 1; iy++) {
                                if (coeff->mag_coeff[i][im][iy] <
-                                   coeff->mag_coeff[i][im][ix]) {
-                                       temp = coeff->mag_coeff[i][im][ix];
-                                       coeff->mag_coeff[i][im][ix] =
-                                               coeff->mag_coeff[i][im][iy];
-                                       coeff->mag_coeff[i][im][iy] = temp;
-                               }
+                                   coeff->mag_coeff[i][im][ix]) swap(coeff->mag_coeff[i][im][ix],
+                                                                     coeff->mag_coeff[i][im][iy]);
                                if (coeff->phs_coeff[i][im][iy] <
-                                   coeff->phs_coeff[i][im][ix]) {
-                                       temp = coeff->phs_coeff[i][im][ix];
-                                       coeff->phs_coeff[i][im][ix] =
-                                               coeff->phs_coeff[i][im][iy];
-                                       coeff->phs_coeff[i][im][iy] = temp;
-                               }
+                                   coeff->phs_coeff[i][im][ix]) swap(coeff->phs_coeff[i][im][ix],
+                                                                     coeff->phs_coeff[i][im][iy]);
 

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

maintainer
#6 lore raw
Quoted 93 lines · Denis Efremov
Quoted 71 lines · Julia Lawall
Quoted 60 lines · Denis Efremov
Quoted 26 lines · Julia Lawall
Quoted 9 lines

+@depends on patch@

(quoted diff) +8 -0
+identifier tmp;
+expression a, b;
+type T;
+@@
+
+(
+- T tmp;
+|

Review

Quoted 10 lines
(quoted diff) +10 -0
+- T tmp = 0;
+|
+- T *tmp = NULL;
+)
+... when != tmp
+- tmp = a;
+- a = b;
+- b = tmp;
++ swap(a, b);
+... when != tmp

In this rule and the next one, if you remove the final ; from the b = tmp line and from the swap line, and put it into context code afterwards, them the generated code looks better on cases like fs/xfs/xfs_inode.c in the function xfs_lock_two_inodes where two successive swap calls are generated.

Quoted 5 lines

There are also some cases such as drivers/net/wireless/ath/ath5k/phy.c in the function ath5k_hw_get_median_noise_floor where the swap code makes up a whole if branch.

In this cases it would be good to remove the {}.

How this can be handled?

If I use this pattern:
@depends on patch@
identifier tmp;
expression a, b;
@@

(
if (...)
- {

(quoted diff) +1 -4
-       tmp = a;
-       a = b;
-       b = tmp
+       swap(a, b)
        ;
- }

|

(quoted diff) +1 -3
-       tmp = a;
-       a = b;
-       b = tmp
+       swap(a, b)
        ;

)

Quoted 6 lines

The tool fails with error:

EXN: Failure("rule starting on line 58: already tagged token:\nC code context\nFile \"drivers/net/wireless/ath/ath5k/phy.c\", line 1574, column 4, charpos = 41650\n around = 'sort',\n whole content = \t\t\t\tsort[j - 1] = tmp;") in drivers/net/wireless/ath/ath5k/phy.c

Quoted 8 lines

A disjunction basically says "at this node in the cfg, can I match the first patter, or can I match the second pattern, etc." Unfortunately in this case the two branches start matching at different nodes, so the short circuit aspect of a disjunction isn't used, and it matches both patterns.

The solution is to just make two rules. The first for the if case and the second for everything else.

if (...)
- {

(quoted diff) +1 -4
-       tmp = a;
-       a = b;
-       b = tmp
+       swap(a, b)
        ;
- }
Quoted 3 lines

This produces "single-line ifs".
Maybe generated patches can be improved somehow?
Moving -+; doesn't help in this case.

There is clearly some problem with the management of newlines...

The other alternative is to just have one rule for introducing swap and another for removing the braces around a swap, ie

if (...)
- {
swap(...);
- }

I don't think it would be motivated to remove the newline in that case.

julia

Quoted 43 lines

diff -u -p a/drivers/net/wireless/ath/ath9k/calib.c b/drivers/net/wireless/ath/ath9k/calib.c

drivers/net/wireless/ath/ath9k/calib.c +1 -5
@@ -32,11 +32,7 @@ static int16_t ath9k_hw_get_nf_hist_mid(
 
        for (i = 0; i < ATH9K_NF_CAL_HIST_MAX - 1; i++) {
                for (j = 1; j < ATH9K_NF_CAL_HIST_MAX - i; j++) {
-                       if (sort[j] > sort[j - 1]) {
-                               nfval = sort[j];
-                               sort[j] = sort[j - 1];
-                               sort[j - 1] = nfval;
-                       }
+                       if (sort[j] > sort[j - 1]) swap(sort[j], sort[j - 1]);
                }
        }
        nfval = sort[(ATH9K_NF_CAL_HIST_MAX - 1) >> 1];

diff -u -p a/drivers/net/wireless/ath/ath9k/ar9003_calib.c b/drivers/net/wireless/ath/ath9k/ar9003_calib.c

drivers/net/wireless/ath/ath9k/ar9003_calib.c +4 -12
@@ -1011,19 +1011,11 @@ static void __ar955x_tx_iq_cal_sort(stru
                for (ix = 0; ix < MAXIQCAL - 1; ix++) {
                        for (iy = ix + 1; iy <= MAXIQCAL - 1; iy++) {
                                if (coeff->mag_coeff[i][im][iy] <
-                                   coeff->mag_coeff[i][im][ix]) {
-                                       temp = coeff->mag_coeff[i][im][ix];
-                                       coeff->mag_coeff[i][im][ix] =
-                                               coeff->mag_coeff[i][im][iy];
-                                       coeff->mag_coeff[i][im][iy] = temp;
-                               }
+                                   coeff->mag_coeff[i][im][ix]) swap(coeff->mag_coeff[i][im][ix],
+                                                                     coeff->mag_coeff[i][im][iy]);
                                if (coeff->phs_coeff[i][im][iy] <
-                                   coeff->phs_coeff[i][im][ix]) {
-                                       temp = coeff->phs_coeff[i][im][ix];
-                                       coeff->phs_coeff[i][im][ix] =
-                                               coeff->phs_coeff[i][im][iy];
-                                       coeff->phs_coeff[i][im][iy] = temp;
-                               }
+                                   coeff->phs_coeff[i][im][ix]) swap(coeff->phs_coeff[i][im][ix],
+                                                                     coeff->phs_coeff[i][im][iy]);
 
 Thanks,
Quoted 2 lines

Denis

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

#7 lore raw
Quoted 106 lines · Julia Lawall
Quoted 93 lines · Denis Efremov
Quoted 71 lines · Julia Lawall
Quoted 60 lines · Denis Efremov
Quoted 26 lines · Julia Lawall
Quoted 9 lines

+@depends on patch@

(quoted diff) +8 -0
+identifier tmp;
+expression a, b;
+type T;
+@@
+
+(
+- T tmp;
+|

Review

Quoted 10 lines
(quoted diff) +10 -0
+- T tmp = 0;
+|
+- T *tmp = NULL;
+)
+... when != tmp
+- tmp = a;
+- a = b;
+- b = tmp;
++ swap(a, b);
+... when != tmp

In this rule and the next one, if you remove the final ; from the b = tmp line and from the swap line, and put it into context code afterwards, them the generated code looks better on cases like fs/xfs/xfs_inode.c in the function xfs_lock_two_inodes where two successive swap calls are generated.

Quoted 5 lines

There are also some cases such as drivers/net/wireless/ath/ath5k/phy.c in the function ath5k_hw_get_median_noise_floor where the swap code makes up a whole if branch.

In this cases it would be good to remove the {}.

How this can be handled?

If I use this pattern:
@depends on patch@
identifier tmp;
expression a, b;
@@

(
if (...)
- {

(quoted diff) +1 -4
-       tmp = a;
-       a = b;
-       b = tmp
+       swap(a, b)
        ;
- }

|

(quoted diff) +1 -3
-       tmp = a;
-       a = b;
-       b = tmp
+       swap(a, b)
        ;

)

Quoted 6 lines

The tool fails with error:

EXN: Failure("rule starting on line 58: already tagged token:\nC code context\nFile \"drivers/net/wireless/ath/ath5k/phy.c\", line 1574, column 4, charpos = 41650\n around = 'sort',\n whole content = \t\t\t\tsort[j - 1] = tmp;") in drivers/net/wireless/ath/ath5k/phy.c

Quoted 8 lines

A disjunction basically says "at this node in the cfg, can I match the first patter, or can I match the second pattern, etc." Unfortunately in this case the two branches start matching at different nodes, so the short circuit aspect of a disjunction isn't used, and it matches both patterns.

The solution is to just make two rules. The first for the if case and the second for everything else.

if (...)
- {

(quoted diff) +1 -4
-       tmp = a;
-       a = b;
-       b = tmp
+       swap(a, b)
        ;
- }
Quoted 3 lines

This produces "single-line ifs".
Maybe generated patches can be improved somehow?
Moving -+; doesn't help in this case.

There is clearly some problem with the management of newlines...

The other alternative is to just have one rule for introducing swap and another for removing the braces around a swap, ie

Quoted 6 lines

if (...)
- {
swap(...);
- }

I don't think it would be motivated to remove the newline in that case.

Yes, this works. I'll send v2.

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

A message that is not in the archive (sent off-list or not yet received): [email protected]

maintainer
#9 lore raw
Quoted 9 lines · Markus Elfring
Quoted 7 lines

A disjunction basically says "at this node in the cfg, can I match the first patter, or can I match the second pattern, etc." Unfortunately in this case the two branches start matching at different nodes, so the short circuit aspect of a disjunction isn't used, and it matches both patterns.

The solution is to just make two rules. The first for the if case and the second for everything else.

Will such feedback trigger further software development considerations?

No. This is never going to change, until someone completely redesigns Coccinelle.

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

#10 lore raw

PATCH v2 1/1 coccinelle: misc: add minmax script

In reply to Denis Efremov

Check for opencoded min(), max() implementations.

---

Changes in v2:
- <... ...> instead of ... when any
- org mode reports fixed
- patch rule to drop excessive ()

 scripts/coccinelle/misc/minmax.cocci | 224 +++++++++++++++++++++++++++
 1 file changed, 224 insertions(+)
 create mode 100644 scripts/coccinelle/misc/minmax.cocci
scripts/coccinelle/misc/minmax.cocci new file +224 -0
new file mode 100644
index 000000000000..61d6b61fd82c
@@ -0,0 +1,224 @@
+// SPDX-License-Identifier: GPL-2.0-only
+///
+/// Check for opencoded min(), max() implementations.
+/// Generated patches sometimes require adding a cast to fix compile warning.
+/// Warnings/patches scope intentionally limited to a function body.
+///
+// Confidence: Medium
+// Copyright: (C) 2021 Denis Efremov ISPRAS
+// Options: --no-includes --include-headers
+//
+// Keywords: min, max
+//
+
+
+virtual report
+virtual org
+virtual context
+virtual patch
+
+@rmax depends on !patch@
+identifier func;
+expression x, y;
+binary operator cmp = {>, >=};
+position p;
+@@
+
+func(...)
+{
+	<...
+*	x cmp@p y ? x : y
+	...>
+}
+
+@rmaxif depends on !patch@
+identifier func;
+expression x, y;
+expression max_val;
+binary operator cmp = {>, >=};
+position p;
+@@
+
+func(...)
+{
+	<...
+*	if (x cmp@p y) {
+*		max_val = x;
+*	} else {
+*		max_val = y;
+*	}
+	...>
+}
+
+@rmin depends on !patch@
+identifier func;
+expression x, y;
+binary operator cmp = {<, <=};
+position p;
+@@
+
+func(...)
+{
+	<...
+*	x cmp@p y ? x : y
+	...>
+}
+
+@rminif depends on !patch@
+identifier func;
+expression x, y;
+expression min_val;
+binary operator cmp = {<, <=};
+position p;
+@@
+
+func(...)
+{
+	<...
+*	if (x cmp@p y) {
+*		min_val = x;
+*	} else {
+*		min_val = y;
+*	}
+	...>
+}
+
+@pmax depends on patch@
+identifier func;
+expression x, y;
+binary operator cmp = {>=, >};
+position p;
+@@
+
+func@p(...)
+{
+	<...
+-	x cmp y ? x : y
++	max(x, y)
+	...>
+}
+
+@pmaxif depends on patch@
+identifier func;
+expression x, y;
+expression max_val;
+binary operator cmp = {>=, >};
+position p;
+@@
+
+func@p(...)
+{
+	<...
+-	if (x cmp y) {
+-		max_val = x;
+-	} else {
+-		max_val = y;
+-	}
++	max_val = max(x, y);
+	...>
+}
+
+@pmin depends on patch@
+identifier func;
+expression x, y;
+binary operator cmp = {<=, <};
+position p;
+@@
+
+func@p(...)
+{
+	<...
+-	x cmp y ? x : y
++	min(x, y)
+	...>
+}
+
+@pminif depends on patch@
+identifier func;
+expression x, y;
+expression min_val;
+binary operator cmp = {<=, <};
+position p;
+@@
+
+func@p(...)
+{
+	<...
+-	if (x cmp y) {
+-		min_val = x;
+-	} else {
+-		min_val = y;
+-	}
++	min_val = min(x, y);
+	...>
+}
+
+@depends on (pmax || pmaxif || pmin || pminif)@
+identifier func;
+expression x, y;
+position p;
+// FIXME: Coccinelle consumes all available ram and
+// and timeouts on every file.
+// position p = { pmin.p, pminif.p, pmax.p, pmaxif.p };
+@@
+
+func@p(...)
+{
+	<...
+(
+-	(min((x), (y)))
++	min(x, y)
+|
+-	(max((x), (y)))
++	max(x, y)
+)
+	...>
+}
+
+@script:python depends on report@
+p << rmax.p;
+@@
+
+coccilib.report.print_report(p[0], "WARNING opportunity for max()")
+
+@script:python depends on org@
+p << rmax.p;
+@@
+
+coccilib.org.print_todo(p[0], "WARNING opportunity for max()")
+
+@script:python depends on report@
+p << rmaxif.p;
+@@
+
+coccilib.report.print_report(p[0], "WARNING opportunity for max()")
+
+@script:python depends on org@
+p << rmaxif.p;
+@@
+
+coccilib.org.print_todo(p[0], "WARNING opportunity for max()")
+
+@script:python depends on report@
+p << rmin.p;
+@@
+
+coccilib.report.print_report(p[0], "WARNING opportunity for min()")
+
+@script:python depends on org@
+p << rmin.p;
+@@
+
+coccilib.org.print_todo(p[0], "WARNING opportunity for min()")
+
+@script:python depends on report@
+p << rminif.p;
+@@
+
+coccilib.report.print_report(p[0], "WARNING opportunity for min()")
+
+@script:python depends on org@
+p << rminif.p;
+@@
+
+coccilib.org.print_todo(p[0], "WARNING opportunity for min()")
Signature · 6 lines
2.26.2

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci
#11 lore raw

Re: [PATCH v2] coccinelle: misc: add minmax script

Sorry for wrong thread, I'll resend v2 to the right one.

Denis

Quoted 235 lines · Denis Efremov

Check for opencoded min(), max() implementations.

---

Changes in v2:
- <... ...> instead of ... when any
- org mode reports fixed
- patch rule to drop excessive ()

 scripts/coccinelle/misc/minmax.cocci | 224 +++++++++++++++++++++++++++
 1 file changed, 224 insertions(+)
 create mode 100644 scripts/coccinelle/misc/minmax.cocci
scripts/coccinelle/misc/minmax.cocci new file +214 -0
new file mode 100644
index 000000000000..61d6b61fd82c
@@ -0,0 +1,224 @@
+// SPDX-License-Identifier: GPL-2.0-only
+///
+/// Check for opencoded min(), max() implementations.
+/// Generated patches sometimes require adding a cast to fix compile warning.
+/// Warnings/patches scope intentionally limited to a function body.
+///
+// Confidence: Medium
+// Copyright: (C) 2021 Denis Efremov ISPRAS
+// Options: --no-includes --include-headers
+//
+// Keywords: min, max
+//
+
+
+virtual report
+virtual org
+virtual context
+virtual patch
+
+@rmax depends on !patch@
+identifier func;
+expression x, y;
+binary operator cmp = {>, >=};
+position p;
+@@
+
+func(...)
+{
+	<...
+*	x cmp@p y ? x : y
+	...>
+}
+
+@rmaxif depends on !patch@
+identifier func;
+expression x, y;
+expression max_val;
+binary operator cmp = {>, >=};
+position p;
+@@
+
+func(...)
+{
+	<...
+*	if (x cmp@p y) {
+*		max_val = x;
+*	} else {
+*		max_val = y;
+*	}
+	...>
+}
+
+@rmin depends on !patch@
+identifier func;
+expression x, y;
+binary operator cmp = {<, <=};
+position p;
+@@
+
+func(...)
+{
+	<...
+*	x cmp@p y ? x : y
+	...>
+}
+
+@rminif depends on !patch@
+identifier func;
+expression x, y;
+expression min_val;
+binary operator cmp = {<, <=};
+position p;
+@@
+
+func(...)
+{
+	<...
+*	if (x cmp@p y) {
+*		min_val = x;
+*	} else {
+*		min_val = y;
+*	}
+	...>
+}
+
+@pmax depends on patch@
+identifier func;
+expression x, y;
+binary operator cmp = {>=, >};
+position p;
+@@
+
+func@p(...)
+{
+	<...
+-	x cmp y ? x : y
++	max(x, y)
+	...>
+}
+
+@pmaxif depends on patch@
+identifier func;
+expression x, y;
+expression max_val;
+binary operator cmp = {>=, >};
+position p;
+@@
+
+func@p(...)
+{
+	<...
+-	if (x cmp y) {
+-		max_val = x;
+-	} else {
+-		max_val = y;
+-	}
++	max_val = max(x, y);
+	...>
+}
+
+@pmin depends on patch@
+identifier func;
+expression x, y;
+binary operator cmp = {<=, <};
+position p;
+@@
+
+func@p(...)
+{
+	<...
+-	x cmp y ? x : y
++	min(x, y)
+	...>
+}
+
+@pminif depends on patch@
+identifier func;
+expression x, y;
+expression min_val;
+binary operator cmp = {<=, <};
+position p;
+@@
+
+func@p(...)
+{
+	<...
+-	if (x cmp y) {
+-		min_val = x;
+-	} else {
+-		min_val = y;
+-	}
++	min_val = min(x, y);
+	...>
+}
+
+@depends on (pmax || pmaxif || pmin || pminif)@
+identifier func;
+expression x, y;
+position p;
+// FIXME: Coccinelle consumes all available ram and
+// and timeouts on every file.
+// position p = { pmin.p, pminif.p, pmax.p, pmaxif.p };
+@@
+
+func@p(...)
+{
+	<...
+(
+-	(min((x), (y)))
++	min(x, y)
+|
+-	(max((x), (y)))
++	max(x, y)
+)
+	...>
+}
+
+@script:python depends on report@
+p << rmax.p;
+@@
+
+coccilib.report.print_report(p[0], "WARNING opportunity for max()")
+
+@script:python depends on org@
+p << rmax.p;
+@@
+
+coccilib.org.print_todo(p[0], "WARNING opportunity for max()")
+
+@script:python depends on report@
+p << rmaxif.p;
+@@
+
+coccilib.report.print_report(p[0], "WARNING opportunity for max()")
+
+@script:python depends on org@
+p << rmaxif.p;
+@@
+
+coccilib.org.print_todo(p[0], "WARNING opportunity for max()")
+
+@script:python depends on report@
+p << rmin.p;
+@@
+
+coccilib.report.print_report(p[0], "WARNING opportunity for min()")
+
+@script:python depends on org@
+p << rmin.p;
+@@
+
+coccilib.org.print_todo(p[0], "WARNING opportunity for min()")
+
+@script:python depends on report@

scripts/coccinelle/misc/minmax.cocci line 224

Quoted 10 lines
scripts/coccinelle/misc/minmax.cocci new file +10 -0
+p << rminif.p;
+@@
+
+coccilib.report.print_report(p[0], "WARNING opportunity for min()")
+
+@script:python depends on org@
+p << rminif.p;
+@@
+
+coccilib.org.print_todo(p[0], "WARNING opportunity for min()")

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

#12 lore raw

PATCH v2 1/1 coccinelle: misc: add swap script

In reply to Denis Efremov

Check for opencoded swap() implementation.

---
Changes in v2:
- additional patch rule to drop excessive {}
- fix indentation in patch mode by anchoring ;

 scripts/coccinelle/misc/swap.cocci | 101 +++++++++++++++++++++++++++++
 1 file changed, 101 insertions(+)
 create mode 100644 scripts/coccinelle/misc/swap.cocci
scripts/coccinelle/misc/swap.cocci new file +101 -0
new file mode 100644
index 000000000000..d5da9888c222
@@ -0,0 +1,101 @@
+// SPDX-License-Identifier: GPL-2.0-only
+///
+/// Check for opencoded swap() implementation.
+///
+// Confidence: High
+// Copyright: (C) 2021 Denis Efremov ISPRAS
+// Options: --no-includes --include-headers
+//
+// Keywords: swap
+//
+
+virtual patch
+virtual org
+virtual report
+virtual context
+
+@r depends on !patch@
+identifier tmp;
+expression a, b;
+type T;
+position p;
+@@
+
+(
+* T tmp;
+|
+* T tmp = 0;
+|
+* T *tmp = NULL;
+)
+... when != tmp
+* tmp = a;
+* a = b;@p
+* b = tmp;
+... when != tmp
+
+@rpvar 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
+
+
+@rp depends on patch@
+identifier tmp;
+expression a, b;
+@@
+
+- tmp = a;
+- a = b;
+- b = tmp
++ swap(a, b)
+  ;
+
+@depends on (rpvar || rp)@
+@@
+
+(
+  for (...;...;...)
+- {
+	swap(...);
+- }
+|
+  while (...)
+- {
+	swap(...);
+- }
+|
+  if (...)
+- {
+	swap(...);
+- }
+)
+
+
+@script:python depends on report@
+p << r.p;
+@@
+
+coccilib.report.print_report(p[0], "WARNING opportunity for swap()")
+
+@script:python depends on org@
+p << r.p;
+@@
+
+coccilib.org.print_todo(p[0], "WARNING opportunity for swap()")
Signature · 6 lines
2.26.2

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci
maintainer
#13 lore raw
Quoted 76 lines · Denis Efremov

Check for opencoded swap() implementation.

---
Changes in v2:
- additional patch rule to drop excessive {}
- fix indentation in patch mode by anchoring ;

 scripts/coccinelle/misc/swap.cocci | 101 +++++++++++++++++++++++++++++
 1 file changed, 101 insertions(+)
 create mode 100644 scripts/coccinelle/misc/swap.cocci
scripts/coccinelle/misc/swap.cocci new file +58 -0
new file mode 100644
index 000000000000..d5da9888c222
@@ -0,0 +1,101 @@
+// SPDX-License-Identifier: GPL-2.0-only
+///
+/// Check for opencoded swap() implementation.
+///
+// Confidence: High
+// Copyright: (C) 2021 Denis Efremov ISPRAS
+// Options: --no-includes --include-headers
+//
+// Keywords: swap
+//
+
+virtual patch
+virtual org
+virtual report
+virtual context
+
+@r depends on !patch@
+identifier tmp;
+expression a, b;
+type T;
+position p;
+@@
+
+(
+* T tmp;
+|
+* T tmp = 0;
+|
+* T *tmp = NULL;
+)
+... when != tmp
+* tmp = a;
+* a = b;@p
+* b = tmp;
+... when != tmp
+
+@rpvar 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
+
+

scripts/coccinelle/misc/swap.cocci line 68

Quoted 10 lines
scripts/coccinelle/misc/swap.cocci new file +10 -0
+@rp depends on patch@
+identifier tmp;
+expression a, b;
+@@
+
+- tmp = a;
+- a = b;
+- b = tmp
++ swap(a, b)
+  ;

A rule like the above should also appear in the non-patch case.

scripts/coccinelle/misc/swap.cocci line 70

Quoted 2 lines
scripts/coccinelle/misc/swap.cocci new file +2 -0
+
+@depends on (rpvar || rp)@

This needs to be depends on patch && (rpvar || rp). It doesn't make much sense, because rpvar and rp both depend on patch, but at the moment that information isn't propagate everywhere that it is needed.

thanks,
julia

Quoted 32 lines
scripts/coccinelle/misc/swap.cocci new file +31 -1
+@@
+
+(
+  for (...;...;...)
+- {
+	swap(...);
+- }
+|
+  while (...)
+- {
+	swap(...);
+- }
+|
+  if (...)
+- {
+	swap(...);
+- }
+)
+
+
+@script:python depends on report@
+p << r.p;
+@@
+
+coccilib.report.print_report(p[0], "WARNING opportunity for swap()")
+
+@script:python depends on org@
+p << r.p;
+@@
+
+coccilib.org.print_todo(p[0], "WARNING opportunity for swap()")
--
Quoted 3 lines

2.26.2

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

#14 lore raw

PATCH v3 1/1 coccinelle: misc: add swap script

In reply to Denis Efremov

Check for opencoded swap() implementation.

---
Changes in v2:
- additional patch rule to drop excessive {}
- fix indentation in patch mode by anchoring ;
Changes in v3:
- Rule added for simple (without var init) swap highlighting in !patch mode
- "depends on patch && (rpvar || rp)" fixed

 scripts/coccinelle/misc/swap.cocci | 122 +++++++++++++++++++++++++++++
 1 file changed, 122 insertions(+)
 create mode 100644 scripts/coccinelle/misc/swap.cocci
scripts/coccinelle/misc/swap.cocci new file +122 -0
new file mode 100644
index 000000000000..c5e71b7ef7f5
@@ -0,0 +1,122 @@
+// SPDX-License-Identifier: GPL-2.0-only
+///
+/// Check for opencoded swap() implementation.
+///
+// Confidence: High
+// Copyright: (C) 2021 Denis Efremov ISPRAS
+// Options: --no-includes --include-headers
+//
+// Keywords: swap
+//
+
+virtual patch
+virtual org
+virtual report
+virtual context
+
+@rvar depends on !patch@
+identifier tmp;
+expression a, b;
+type T;
+position p;
+@@
+
+(
+* T tmp;
+|
+* T tmp = 0;
+|
+* T *tmp = NULL;
+)
+... when != tmp
+* tmp = a;
+* a = b;@p
+* b = tmp;
+... when != tmp
+
+@r depends on !patch@
+identifier tmp;
+expression a, b;
+position p != rvar.p;
+@@
+
+* tmp = a;
+* a = b;@p
+* b = tmp;
+
+@rpvar 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
+
+@rp depends on patch@
+identifier tmp;
+expression a, b;
+@@
+
+- tmp = a;
+- a = b;
+- b = tmp
++ swap(a, b)
+  ;
+
+@depends on patch && (rpvar || rp)@
+@@
+
+(
+  for (...;...;...)
+- {
+	swap(...);
+- }
+|
+  while (...)
+- {
+	swap(...);
+- }
+|
+  if (...)
+- {
+	swap(...);
+- }
+)
+
+
+@script:python depends on report@
+p << r.p;
+@@
+
+coccilib.report.print_report(p[0], "WARNING opportunity for swap()")
+
+@script:python depends on org@
+p << r.p;
+@@
+
+coccilib.org.print_todo(p[0], "WARNING opportunity for swap()")
+
+@script:python depends on report@
+p << rvar.p;
+@@
+
+coccilib.report.print_report(p[0], "WARNING opportunity for swap()")
+
+@script:python depends on org@
+p << rvar.p;
+@@
+
+coccilib.org.print_todo(p[0], "WARNING opportunity for swap()")
Signature · 1 line
2.26.2
#15 lore raw

Ping?

Quoted 144 lines · Denis Efremov

Check for opencoded swap() implementation.

---
Changes in v2:
- additional patch rule to drop excessive {}
- fix indentation in patch mode by anchoring ;
Changes in v3:
- Rule added for simple (without var init) swap highlighting in !patch mode
- "depends on patch && (rpvar || rp)" fixed

  scripts/coccinelle/misc/swap.cocci | 122 +++++++++++++++++++++++++++++
  1 file changed, 122 insertions(+)

create mode 100644 scripts/coccinelle/misc/swap.cocci

scripts/coccinelle/misc/swap.cocci new file +122 -0
new file mode 100644
index 000000000000..c5e71b7ef7f5
@@ -0,0 +1,122 @@
+// SPDX-License-Identifier: GPL-2.0-only
+///
+/// Check for opencoded swap() implementation.
+///
+// Confidence: High
+// Copyright: (C) 2021 Denis Efremov ISPRAS
+// Options: --no-includes --include-headers
+//
+// Keywords: swap
+//
+
+virtual patch
+virtual org
+virtual report
+virtual context
+
+@rvar depends on !patch@
+identifier tmp;
+expression a, b;
+type T;
+position p;
+@@
+
+(
+* T tmp;
+|
+* T tmp = 0;
+|
+* T *tmp = NULL;
+)
+... when != tmp
+* tmp = a;
+* a = b;@p
+* b = tmp;
+... when != tmp
+
+@r depends on !patch@
+identifier tmp;
+expression a, b;
+position p != rvar.p;
+@@
+
+* tmp = a;
+* a = b;@p
+* b = tmp;
+
+@rpvar 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
+
+@rp depends on patch@
+identifier tmp;
+expression a, b;
+@@
+
+- tmp = a;
+- a = b;
+- b = tmp
++ swap(a, b)
+  ;
+
+@depends on patch && (rpvar || rp)@
+@@
+
+(
+  for (...;...;...)
+- {
+	swap(...);
+- }
+|
+  while (...)
+- {
+	swap(...);
+- }
+|
+  if (...)
+- {
+	swap(...);
+- }
+)
+
+
+@script:python depends on report@
+p << r.p;
+@@
+
+coccilib.report.print_report(p[0], "WARNING opportunity for swap()")
+
+@script:python depends on org@
+p << r.p;
+@@
+
+coccilib.org.print_todo(p[0], "WARNING opportunity for swap()")
+
+@script:python depends on report@
+p << rvar.p;
+@@
+
+coccilib.report.print_report(p[0], "WARNING opportunity for swap()")
+
+@script:python depends on org@
+p << rvar.p;
+@@
+
+coccilib.org.print_todo(p[0], "WARNING opportunity for swap()")
maintainer Applied
#16 lore raw
Quoted 1 line · Denis Efremov

Ping?

Applied. Thanks.

Quoted 140 lines · Denis Efremov
Quoted 138 lines

Check for opencoded swap() implementation.

---
Changes in v2:
- additional patch rule to drop excessive {}
- fix indentation in patch mode by anchoring ;
Changes in v3:
- Rule added for simple (without var init) swap highlighting in !patch
mode
- "depends on patch && (rpvar || rp)" fixed

  scripts/coccinelle/misc/swap.cocci | 122 +++++++++++++++++++++++++++++
  1 file changed, 122 insertions(+)

create mode 100644 scripts/coccinelle/misc/swap.cocci

scripts/coccinelle/misc/swap.cocci new file +115 -0
new file mode 100644
index 000000000000..c5e71b7ef7f5
@@ -0,0 +1,122 @@
+// SPDX-License-Identifier: GPL-2.0-only
+///
+/// Check for opencoded swap() implementation.
+///
+// Confidence: High
+// Copyright: (C) 2021 Denis Efremov ISPRAS
+// Options: --no-includes --include-headers
+//
+// Keywords: swap
+//
+
+virtual patch
+virtual org
+virtual report
+virtual context
+
+@rvar depends on !patch@
+identifier tmp;
+expression a, b;
+type T;
+position p;
+@@
+
+(
+* T tmp;
+|
+* T tmp = 0;
+|
+* T *tmp = NULL;
+)
+... when != tmp
+* tmp = a;
+* a = b;@p
+* b = tmp;
+... when != tmp
+
+@r depends on !patch@
+identifier tmp;
+expression a, b;
+position p != rvar.p;
+@@
+
+* tmp = a;
+* a = b;@p
+* b = tmp;
+
+@rpvar 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
+
+@rp depends on patch@
+identifier tmp;
+expression a, b;
+@@
+
+- tmp = a;
+- a = b;
+- b = tmp
++ swap(a, b)
+  ;
+
+@depends on patch && (rpvar || rp)@
+@@
+
+(
+  for (...;...;...)
+- {
+	swap(...);
+- }
+|
+  while (...)
+- {
+	swap(...);
+- }
+|
+  if (...)
+- {
+	swap(...);
+- }
+)
+
+
+@script:python depends on report@
+p << r.p;
+@@
+
+coccilib.report.print_report(p[0], "WARNING opportunity for swap()")
+
+@script:python depends on org@
+p << r.p;
+@@
+
+coccilib.org.print_todo(p[0], "WARNING opportunity for swap()")
+
+@script:python depends on report@
+p << rvar.p;
+@@
+
Quoted 9 lines

scripts/coccinelle/misc/swap.cocci line 122

Quoted 8 lines
scripts/coccinelle/misc/swap.cocci new file +7 -0
+coccilib.report.print_report(p[0], "WARNING opportunity for swap()")
+
+@script:python depends on org@
+p << rvar.p;
+@@
+
+coccilib.org.print_todo(p[0], "WARNING opportunity for swap()")

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