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

From: Julia Lawall
Date: Wed Feb 17 2021 - 16:19:34 EST




On Tue, 16 Feb 2021, Denis Efremov wrote:

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

Some cases that could be improved:

diff -u -p a/drivers/platform/x86/asus-laptop.c
b/drivers/platform/x86/asus-laptop.c
--- a/drivers/platform/x86/asus-laptop.c
+++ b/drivers/platform/x86/asus-laptop.c
@@ -1195,7 +1195,7 @@ static ssize_t ls_level_store(struct dev
if (rv < 0)
return rv;

- value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
+ value = (0 < value) ? (min(15, value)) : 0;
/* 0 <= value <= 15 */
asus_als_level(asus, value);


diff -u -p a/sound/pci/ctxfi/ctatc.c b/sound/pci/ctxfi/ctatc.c
--- a/sound/pci/ctxfi/ctatc.c
+++ b/sound/pci/ctxfi/ctatc.c
@@ -382,7 +382,7 @@ static int atc_pcm_playback_start(struct
apcm->started = 1;

max_cisz = src->multi * src->rsc.msr;
- max_cisz = 0x80 * (max_cisz < 8 ? max_cisz : 8);
+ max_cisz = 0x80 * (min(max_cisz, 8));


> +func(...)
> +{
> + ... when any
> +* (x cmp y) ?@p x : y
> + ... when any
> +}

In all cases, this would be more efficient as:

func(...)
{
<...
* (x cmp y) ?@p x : y
...>
}

There is an optimization that causes this to be just a search through the
nodes of the control-flow graph, rather than following the actual control
flow from the beginning of the function to the end.

> +@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.report.print_todo(p[0], "WARNING opportunity for max()")

All of the org cases should be coccilib.org, not coccilib.report.

julia