[PATCH v1] coccinelle: api: Don't use devm_platform_get_and_ioremap_resource with res==NULL

Uwe Kleine-König posted 1 patch 3 years, 5 months ago
There is a newer version of this series
.../api/devm_platform_ioremap_resource.cocci  | 44 +++++++++++++++++++
1 file changed, 44 insertions(+)
create mode 100644 scripts/coccinelle/api/devm_platform_ioremap_resource.cocci
[PATCH v1] coccinelle: api: Don't use devm_platform_get_and_ioremap_resource with res==NULL
Posted by Uwe Kleine-König 3 years, 5 months ago
devm_platform_get_and_ioremap_resource(pdev, index, NULL) is equivalent to
the shorter devm_platform_ioremap_resource(pdev, index).

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
Hello,

a potential improvement is to check for invocations of
devm_platform_get_and_ioremap_resource() where the res parameter isn't
used afterwards, but my coccinelle foo isn't strong enough for that.

Best regards
Uwe

 .../api/devm_platform_ioremap_resource.cocci  | 44 +++++++++++++++++++
 1 file changed, 44 insertions(+)
 create mode 100644 scripts/coccinelle/api/devm_platform_ioremap_resource.cocci

diff --git a/scripts/coccinelle/api/devm_platform_ioremap_resource.cocci b/scripts/coccinelle/api/devm_platform_ioremap_resource.cocci
new file mode 100644
index 000000000000..401610b9a17d
--- /dev/null
+++ b/scripts/coccinelle/api/devm_platform_ioremap_resource.cocci
@@ -0,0 +1,44 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/// Don't use devm_platform_get_and_ioremap_resource with NULL as third parameter
+// Confidence: High
+
+virtual patch
+virtual context
+virtual org
+virtual report
+
+@r1@
+position p;
+@@
+ devm_platform_ioremap_resource(...) {
+	...
+	devm_platform_get_and_ioremap_resource@p(...)
+	...
+ }
+
+@depends on patch@
+expression pdev,index;
+position p != r1.p;
+@@
+
+-  devm_platform_get_and_ioremap_resource@p(pdev, index, NULL)
++  devm_platform_ioremap_resource(pdev, index)
+
+@r2 depends on !patch exists@
+expression pdev,index;
+position p;
+@@
+
+*  devm_platform_get_and_ioremap_resource@p(pdev, index, NULL)
+
+@script:python depends on org@
+p << r2.p;
+@@
+
+cocci.print_main("WARNING opportunity for devm_platform_ioremap_resource", p)
+
+@script:python depends on report@
+p << r2.p;
+@@
+
+coccilib.report.print_report(p[0], "WARNING opportunity for devm_platform_ioremap_resource")
-- 
2.38.1

Re: [PATCH v1] coccinelle: api: Don't use devm_platform_get_and_ioremap_resource with res==NULL
Posted by Julia Lawall 3 years, 5 months ago

On Mon, 7 Nov 2022, Uwe Kleine-König wrote:

> devm_platform_get_and_ioremap_resource(pdev, index, NULL) is equivalent to
> the shorter devm_platform_ioremap_resource(pdev, index).
>
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> ---
> Hello,
>
> a potential improvement is to check for invocations of
> devm_platform_get_and_ioremap_resource() where the res parameter isn't
> used afterwards, but my coccinelle foo isn't strong enough for that.

... when != res

I'm not sure where you wanted to put it though.

julia

>
> Best regards
> Uwe
>
>  .../api/devm_platform_ioremap_resource.cocci  | 44 +++++++++++++++++++
>  1 file changed, 44 insertions(+)
>  create mode 100644 scripts/coccinelle/api/devm_platform_ioremap_resource.cocci
>
> diff --git a/scripts/coccinelle/api/devm_platform_ioremap_resource.cocci b/scripts/coccinelle/api/devm_platform_ioremap_resource.cocci
> new file mode 100644
> index 000000000000..401610b9a17d
> --- /dev/null
> +++ b/scripts/coccinelle/api/devm_platform_ioremap_resource.cocci
> @@ -0,0 +1,44 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/// Don't use devm_platform_get_and_ioremap_resource with NULL as third parameter
> +// Confidence: High
> +
> +virtual patch
> +virtual context
> +virtual org
> +virtual report
> +
> +@r1@
> +position p;
> +@@
> + devm_platform_ioremap_resource(...) {
> +	...
> +	devm_platform_get_and_ioremap_resource@p(...)
> +	...
> + }
> +
> +@depends on patch@
> +expression pdev,index;
> +position p != r1.p;
> +@@
> +
> +-  devm_platform_get_and_ioremap_resource@p(pdev, index, NULL)
> ++  devm_platform_ioremap_resource(pdev, index)
> +
> +@r2 depends on !patch exists@
> +expression pdev,index;
> +position p;
> +@@
> +
> +*  devm_platform_get_and_ioremap_resource@p(pdev, index, NULL)
> +
> +@script:python depends on org@
> +p << r2.p;
> +@@
> +
> +cocci.print_main("WARNING opportunity for devm_platform_ioremap_resource", p)
> +
> +@script:python depends on report@
> +p << r2.p;
> +@@
> +
> +coccilib.report.print_report(p[0], "WARNING opportunity for devm_platform_ioremap_resource")
> --
> 2.38.1
>
>
[PATCH v2] coccinelle: api: Don't use devm_platform_get_and_ioremap_resource with res==NULL
Posted by Uwe Kleine-König 3 years, 5 months ago
devm_platform_get_and_ioremap_resource(pdev, index, NULL) is equivalent to
the shorter devm_platform_ioremap_resource(pdev, index).

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
On Mon, Nov 07, 2022 at 08:45:38PM +0800, Julia Lawall wrote:
> On Mon, 7 Nov 2022, Uwe Kleine-König wrote:
> 
> > devm_platform_get_and_ioremap_resource(pdev, index, NULL) is equivalent to
> > the shorter devm_platform_ioremap_resource(pdev, index).
> >
> > Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> > ---
> > Hello,
> >
> > a potential improvement is to check for invocations of
> > devm_platform_get_and_ioremap_resource() where the res parameter isn't
> > used afterwards, but my coccinelle foo isn't strong enough for that.
> 
> ... when != res
> 
> I'm not sure where you wanted to put it though.

I tinkered a bit further and even succeeded to remove the declaration if
it's otherwise unused.

I failed to test the report mode, my spatch tells me some error about
python2?!

Best regards
Uwe

 .../api/devm_platform_ioremap_resource.cocci  | 85 +++++++++++++++++++
 1 file changed, 85 insertions(+)
 create mode 100644 scripts/coccinelle/api/devm_platform_ioremap_resource.cocci

diff --git a/scripts/coccinelle/api/devm_platform_ioremap_resource.cocci b/scripts/coccinelle/api/devm_platform_ioremap_resource.cocci
new file mode 100644
index 000000000000..485dfa6ea0aa
--- /dev/null
+++ b/scripts/coccinelle/api/devm_platform_ioremap_resource.cocci
@@ -0,0 +1,85 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/// Don't use devm_platform_get_and_ioremap_resource with NULL as third parameter
+// Confidence: High
+
+virtual patch
+virtual context
+virtual org
+virtual report
+
+@r1@
+position p;
+@@
+ devm_platform_ioremap_resource(...) {
+	...
+	devm_platform_get_and_ioremap_resource@p(...)
+	...
+ }
+
+@depends on patch@
+expression pdev,index;
+position p != r1.p;
+@@
+
+-  devm_platform_get_and_ioremap_resource@p(pdev, index, NULL)
++  devm_platform_ioremap_resource(pdev, index)
+
+@depends on patch@
+expression pdev,index;
+identifier res;
+type T;
+@@
+-  T res;
+   ... when != res
+-  devm_platform_get_and_ioremap_resource(pdev, index, &res)
++  devm_platform_ioremap_resource(pdev, index)
+   ... when != res
+
+@depends on patch@
+expression pdev,index;
+identifier res;
+@@
+
+-  devm_platform_get_and_ioremap_resource(pdev, index, &res)
++  devm_platform_ioremap_resource(pdev, index)
+   ... when != res
+
+@r2 depends on !patch exists@
+expression pdev,index;
+position p;
+@@
+
+*  devm_platform_get_and_ioremap_resource@p(pdev, index, NULL)
+
+@script:python depends on org@
+p << r2.p;
+@@
+
+cocci.print_main("WARNING opportunity for devm_platform_ioremap_resource", p)
+
+@script:python depends on report@
+p << r2.p;
+@@
+
+coccilib.report.print_report(p[0], "WARNING opportunity for devm_platform_ioremap_resource")
+
+@r3 depends on !patch exists@
+expression pdev,index;
+identifier res;
+position p;
+@@
+
+*  devm_platform_get_and_ioremap_resource@p(pdev, index, &res)
+   ... when != res
+
+@script:python depends on org@
+p << r3.p;
+@@
+
+cocci.print_main("WARNING opportunity for devm_platform_ioremap_resource", p)
+
+@script:python depends on report@
+p << r3.p;
+@@
+
+coccilib.report.print_report(p[0], "WARNING opportunity for devm_platform_ioremap_resource")
-- 
2.38.1


-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
Re: [PATCH v2] coccinelle: api: Don't use devm_platform_get_and_ioremap_resource with res==NULL
Posted by Uwe Kleine-König 3 years, 4 months ago
Hello,

On Mon, Nov 07, 2022 at 07:11:35PM +0100, Uwe Kleine-König wrote:
> devm_platform_get_and_ioremap_resource(pdev, index, NULL) is equivalent to
> the shorter devm_platform_ioremap_resource(pdev, index).
> 
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> ---
> On Mon, Nov 07, 2022 at 08:45:38PM +0800, Julia Lawall wrote:
> > On Mon, 7 Nov 2022, Uwe Kleine-König wrote:
> > 
> > > devm_platform_get_and_ioremap_resource(pdev, index, NULL) is equivalent to
> > > the shorter devm_platform_ioremap_resource(pdev, index).
> > >
> > > Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> > > ---
> > > Hello,
> > >
> > > a potential improvement is to check for invocations of
> > > devm_platform_get_and_ioremap_resource() where the res parameter isn't
> > > used afterwards, but my coccinelle foo isn't strong enough for that.
> > 
> > ... when != res
> > 
> > I'm not sure where you wanted to put it though.
> 
> I tinkered a bit further and even succeeded to remove the declaration if
> it's otherwise unused.
> 
> I failed to test the report mode, my spatch tells me some error about
> python2?!

For the record, that is a problem with Debian's coccinelle, bugreport 
at https://bugs.debian.org/1023653

Best regards
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |