cocci.inria.fr archive mirror
 help / color / mirror / Atom feed
* [cocci] coccinelle: How to remove a return at the end of a void function?
@ 2022-12-24 11:56 Uwe Kleine-König
  2022-12-24 12:28 ` Julia Lawall
  0 siblings, 1 reply; 7+ messages in thread
From: Uwe Kleine-König @ 2022-12-24 11:56 UTC (permalink / raw)
  To: Julia Lawall, Nicolas Palix; +Cc: cocci, linux-kernel, kernel

[-- Attachment #1: Type: text/plain, Size: 2872 bytes --]

Hello,

I work on a patch set that eventually makes the function
rtsx_usb_sdmmc_drv_remove() return void:

A simplified spatch looks as follows:

-------->8--------
virtual patch

@p1@
identifier pdev;
@@
-int
+void
 rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev) {
 <...
-return 0;
+return;
 ...>
 }
-------->8--------

This results in:

-------->8--------
diff -u -p a/drivers/mmc/host/rtsx_usb_sdmmc.c b/drivers/mmc/host/rtsx_usb_sdmmc.c
--- a/drivers/mmc/host/rtsx_usb_sdmmc.c
+++ b/drivers/mmc/host/rtsx_usb_sdmmc.c
@@ -1379,13 +1379,13 @@ static int rtsx_usb_sdmmc_drv_probe(stru
        return 0;
 }

-static int rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev)
+static void rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev)
 {
        struct rtsx_usb_sdmmc *host = platform_get_drvdata(pdev);
        struct mmc_host *mmc;

        if (!host)
-               return 0;
+               return;

        mmc = host->mmc;
        host->host_removal = true;
@@ -1416,7 +1416,7 @@ static int rtsx_usb_sdmmc_drv_remove(str
        dev_dbg(&(pdev->dev),
                ": Realtek USB SD/MMC module has been removed\n");

-       return 0;
+       return;
 }

 #ifdef CONFIG_PM
-------->8--------

which is as intended. Now I want to remove the useless "return;" at the
end of the function, however adding

-------->8--------
@p2 depends on p1@
identifier pdev;
@@
 void rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev) {
 ...
-return;
 }
-------->8--------

to the spatch doesn't (only) do the intended:

-------->8--------
diff -u -p a/drivers/mmc/host/rtsx_usb_sdmmc.c b/drivers/mmc/host/rtsx_usb_sdmmc.c
--- a/drivers/mmc/host/rtsx_usb_sdmmc.c
+++ b/drivers/mmc/host/rtsx_usb_sdmmc.c
@@ -1379,13 +1379,13 @@ static int rtsx_usb_sdmmc_drv_probe(stru
        return 0;
 }

-static int rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev)
+static void rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev)
 {
        struct rtsx_usb_sdmmc *host = platform_get_drvdata(pdev);
        struct mmc_host *mmc;

        if (!host)
-               return 0;
+               {}

        mmc = host->mmc;
        host->host_removal = true;
@@ -1415,8 +1415,6 @@ static int rtsx_usb_sdmmc_drv_remove(str

        dev_dbg(&(pdev->dev),
                ": Realtek USB SD/MMC module has been removed\n");
-
-       return 0;
 }

 #ifdef CONFIG_PM
-------->8--------

It's obvious to me, why coccinelle also removes the first return, but
it's not obvious to me, how to prevent this and only drop the 2nd one.

Do you have a hint for me?

Thanks in advance and happy holidays,
Uwe


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

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [cocci] coccinelle: How to remove a return at the end of a void function?
  2022-12-24 11:56 [cocci] coccinelle: How to remove a return at the end of a void function? Uwe Kleine-König
@ 2022-12-24 12:28 ` Julia Lawall
  2022-12-25 21:20   ` Uwe Kleine-König
  0 siblings, 1 reply; 7+ messages in thread
From: Julia Lawall @ 2022-12-24 12:28 UTC (permalink / raw)
  To: Uwe Kleine-König
  Cc: Julia Lawall, Nicolas Palix, cocci, linux-kernel, kernel

[-- Attachment #1: Type: text/plain, Size: 3337 bytes --]



On Sat, 24 Dec 2022, Uwe Kleine-König wrote:

> Hello,
>
> I work on a patch set that eventually makes the function
> rtsx_usb_sdmmc_drv_remove() return void:
>
> A simplified spatch looks as follows:
>
> -------->8--------
> virtual patch
>
> @p1@
> identifier pdev;
> @@
> -int
> +void
>  rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev) {
>  <...
> -return 0;
> +return;
>  ...>
>  }
> -------->8--------
>
> This results in:
>
> -------->8--------
> diff -u -p a/drivers/mmc/host/rtsx_usb_sdmmc.c b/drivers/mmc/host/rtsx_usb_sdmmc.c
> --- a/drivers/mmc/host/rtsx_usb_sdmmc.c
> +++ b/drivers/mmc/host/rtsx_usb_sdmmc.c
> @@ -1379,13 +1379,13 @@ static int rtsx_usb_sdmmc_drv_probe(stru
>         return 0;
>  }
>
> -static int rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev)
> +static void rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev)
>  {
>         struct rtsx_usb_sdmmc *host = platform_get_drvdata(pdev);
>         struct mmc_host *mmc;
>
>         if (!host)
> -               return 0;
> +               return;
>
>         mmc = host->mmc;
>         host->host_removal = true;
> @@ -1416,7 +1416,7 @@ static int rtsx_usb_sdmmc_drv_remove(str
>         dev_dbg(&(pdev->dev),
>                 ": Realtek USB SD/MMC module has been removed\n");
>
> -       return 0;
> +       return;
>  }
>
>  #ifdef CONFIG_PM
> -------->8--------
>
> which is as intended. Now I want to remove the useless "return;" at the
> end of the function, however adding
>
> -------->8--------
> @p2 depends on p1@
> identifier pdev;
> @@
>  void rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev) {
>  ...
> -return;
>  }
> -------->8--------
>
> to the spatch doesn't (only) do the intended:

The problem is that Coccinelle is following the control-flow through the
function, and all of the returns are at the end of a control.flow path.
The simple, hacky solution is to change the return;s into some function
call Return();, then do like the above for Return(); and then change the
Return();s back to return;s

julia


>
> -------->8--------
> diff -u -p a/drivers/mmc/host/rtsx_usb_sdmmc.c b/drivers/mmc/host/rtsx_usb_sdmmc.c
> --- a/drivers/mmc/host/rtsx_usb_sdmmc.c
> +++ b/drivers/mmc/host/rtsx_usb_sdmmc.c
> @@ -1379,13 +1379,13 @@ static int rtsx_usb_sdmmc_drv_probe(stru
>         return 0;
>  }
>
> -static int rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev)
> +static void rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev)
>  {
>         struct rtsx_usb_sdmmc *host = platform_get_drvdata(pdev);
>         struct mmc_host *mmc;
>
>         if (!host)
> -               return 0;
> +               {}
>
>         mmc = host->mmc;
>         host->host_removal = true;
> @@ -1415,8 +1415,6 @@ static int rtsx_usb_sdmmc_drv_remove(str
>
>         dev_dbg(&(pdev->dev),
>                 ": Realtek USB SD/MMC module has been removed\n");
> -
> -       return 0;
>  }
>
>  #ifdef CONFIG_PM
> -------->8--------
>
> It's obvious to me, why coccinelle also removes the first return, but
> it's not obvious to me, how to prevent this and only drop the 2nd one.
>
> Do you have a hint for me?
>
> Thanks in advance and happy holidays,
> Uwe
>
>
> --
> Pengutronix e.K.                           | Uwe Kleine-König            |
> Industrial Linux Solutions                 | https://www.pengutronix.de/ |
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [cocci] coccinelle: How to remove a return at the end of a void function?
  2022-12-24 12:28 ` Julia Lawall
@ 2022-12-25 21:20   ` Uwe Kleine-König
  2022-12-26 16:02     ` Julia Lawall
  0 siblings, 1 reply; 7+ messages in thread
From: Uwe Kleine-König @ 2022-12-25 21:20 UTC (permalink / raw)
  To: Julia Lawall; +Cc: kernel, Nicolas Palix, linux-kernel, cocci

[-- Attachment #1: Type: text/plain, Size: 4234 bytes --]

Hello Julia,

first of all thanks for your quick answer.

On Sat, Dec 24, 2022 at 01:28:04PM +0100, Julia Lawall wrote:
> On Sat, 24 Dec 2022, Uwe Kleine-König wrote:
> > A simplified spatch looks as follows:
> >
> > -------->8--------
> > virtual patch
> >
> > @p1@
> > identifier pdev;
> > @@
> > -int
> > +void
> >  rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev) {
> >  <...
> > -return 0;
> > +return;
> >  ...>
> >  }
> > -------->8--------
> >
> > This results in:
> >
> > -------->8--------
> > diff -u -p a/drivers/mmc/host/rtsx_usb_sdmmc.c b/drivers/mmc/host/rtsx_usb_sdmmc.c
> > --- a/drivers/mmc/host/rtsx_usb_sdmmc.c
> > +++ b/drivers/mmc/host/rtsx_usb_sdmmc.c
> > @@ -1379,13 +1379,13 @@ static int rtsx_usb_sdmmc_drv_probe(stru
> >         return 0;
> >  }
> >
> > -static int rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev)
> > +static void rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev)
> >  {
> >         struct rtsx_usb_sdmmc *host = platform_get_drvdata(pdev);
> >         struct mmc_host *mmc;
> >
> >         if (!host)
> > -               return 0;
> > +               return;
> >
> >         mmc = host->mmc;
> >         host->host_removal = true;
> > @@ -1416,7 +1416,7 @@ static int rtsx_usb_sdmmc_drv_remove(str
> >         dev_dbg(&(pdev->dev),
> >                 ": Realtek USB SD/MMC module has been removed\n");
> >
> > -       return 0;
> > +       return;
> >  }
> >
> >  #ifdef CONFIG_PM
> > -------->8--------
> >
> > which is as intended. Now I want to remove the useless "return;" at the
> > end of the function, however adding
> >
> > -------->8--------
> > @p2 depends on p1@
> > identifier pdev;
> > @@
> >  void rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev) {
> >  ...
> > -return;
> >  }
> > -------->8--------
> >
> > to the spatch doesn't (only) do the intended:
> 
> The problem is that Coccinelle is following the control-flow through the
> function, and all of the returns are at the end of a control.flow path.
> The simple, hacky solution is to change the return;s into some function
> call Return();, then do like the above for Return(); and then change the
> Return();s back to return;s

OK, I tried, but somehow coccinelle refuse to work after I introduced
Return(), even replacing them by return; doesn't work:

-------->8--------
virtual patch

@p1@
identifier pdev;
@@
-int
+void
 rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev) {
 ...
-return 0;
+Return();
 ...
 }

@p2@
identifier pdev;
@@
 void rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev) {
 ...
-Return();
+return;
 ...
 }
-------->8--------

results in

-------->8--------
$ /usr/bin/spatch --debug -D patch --very-quiet --cocci-file scripts/coccinelle/api/test.cocci --patch . --dir drivers/mmc/host/rtsx_usb_sdmmc.c -I ./arch/x86/include -I ./arch/x86/include/generated -I ./include -I ./arch/x86/include/uapi -I ./arch/x86/include/generated/uapi -I ./include/uapi -I ./include/generated/uapi --include ./include/linux/compiler-version.h --include ./include/linux/kconfig.h --jobs 4 --chunksize 1
diff -u -p a/drivers/mmc/host/rtsx_usb_sdmmc.c b/drivers/mmc/host/rtsx_usb_sdmmc.c
--- a/drivers/mmc/host/rtsx_usb_sdmmc.c
+++ b/drivers/mmc/host/rtsx_usb_sdmmc.c
@@ -1385,7 +1385,7 @@ static int rtsx_usb_sdmmc_drv_remove(str
 	struct mmc_host *mmc;

 	if (!host)
-		return 0;
+		Return();

 	mmc = host->mmc;
 	host->host_removal = true;
@@ -1416,7 +1416,7 @@ static int rtsx_usb_sdmmc_drv_remove(str
 	dev_dbg(&(pdev->dev),
 		": Realtek USB SD/MMC module has been removed\n");

-	return 0;
+	Return();
 }

 #ifdef CONFIG_PM
-------->8--------

Adding --debug doesn't give any hints.

(And if I add another hunk inbeetween removing Return at the end of the
function there is no effect either.)

Do I need to split that in two spatches to make coccinelle cooperate?

(If it matters, this is coccinelle as shipped by Debian, Version
1.1.1.deb-2)

Best regards
Uwe

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

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [cocci] coccinelle: How to remove a return at the end of a void function?
  2022-12-25 21:20   ` Uwe Kleine-König
@ 2022-12-26 16:02     ` Julia Lawall
  2022-12-27  9:55       ` Uwe Kleine-König
  0 siblings, 1 reply; 7+ messages in thread
From: Julia Lawall @ 2022-12-26 16:02 UTC (permalink / raw)
  To: Uwe Kleine-König; +Cc: kernel, Nicolas Palix, linux-kernel, cocci

[-- Attachment #1: Type: text/plain, Size: 4961 bytes --]



On Sun, 25 Dec 2022, Uwe Kleine-König wrote:

> Hello Julia,
>
> first of all thanks for your quick answer.
>
> On Sat, Dec 24, 2022 at 01:28:04PM +0100, Julia Lawall wrote:
> > On Sat, 24 Dec 2022, Uwe Kleine-König wrote:
> > > A simplified spatch looks as follows:
> > >
> > > -------->8--------
> > > virtual patch
> > >
> > > @p1@
> > > identifier pdev;
> > > @@
> > > -int
> > > +void
> > >  rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev) {
> > >  <...
> > > -return 0;
> > > +return;
> > >  ...>
> > >  }
> > > -------->8--------
> > >
> > > This results in:
> > >
> > > -------->8--------
> > > diff -u -p a/drivers/mmc/host/rtsx_usb_sdmmc.c b/drivers/mmc/host/rtsx_usb_sdmmc.c
> > > --- a/drivers/mmc/host/rtsx_usb_sdmmc.c
> > > +++ b/drivers/mmc/host/rtsx_usb_sdmmc.c
> > > @@ -1379,13 +1379,13 @@ static int rtsx_usb_sdmmc_drv_probe(stru
> > >         return 0;
> > >  }
> > >
> > > -static int rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev)
> > > +static void rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev)
> > >  {
> > >         struct rtsx_usb_sdmmc *host = platform_get_drvdata(pdev);
> > >         struct mmc_host *mmc;
> > >
> > >         if (!host)
> > > -               return 0;
> > > +               return;
> > >
> > >         mmc = host->mmc;
> > >         host->host_removal = true;
> > > @@ -1416,7 +1416,7 @@ static int rtsx_usb_sdmmc_drv_remove(str
> > >         dev_dbg(&(pdev->dev),
> > >                 ": Realtek USB SD/MMC module has been removed\n");
> > >
> > > -       return 0;
> > > +       return;
> > >  }
> > >
> > >  #ifdef CONFIG_PM
> > > -------->8--------
> > >
> > > which is as intended. Now I want to remove the useless "return;" at the
> > > end of the function, however adding
> > >
> > > -------->8--------
> > > @p2 depends on p1@
> > > identifier pdev;
> > > @@
> > >  void rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev) {
> > >  ...
> > > -return;
> > >  }
> > > -------->8--------
> > >
> > > to the spatch doesn't (only) do the intended:
> >
> > The problem is that Coccinelle is following the control-flow through the
> > function, and all of the returns are at the end of a control.flow path.
> > The simple, hacky solution is to change the return;s into some function
> > call Return();, then do like the above for Return(); and then change the
> > Return();s back to return;s
>
> OK, I tried, but somehow coccinelle refuse to work after I introduced
> Return(), even replacing them by return; doesn't work:
>
> -------->8--------
> virtual patch
>
> @p1@
> identifier pdev;
> @@
> -int
> +void
>  rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev) {
>  ...
> -return 0;
> +Return();
>  ...
>  }
>
> @p2@
> identifier pdev;
> @@
>  void rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev) {
>  ...
> -Return();
> +return;
>  ...
>  }

The problem is that a control-flow path at this point can have multiple
calls to Return();  You pattern only matches when every control-flow path
through the code has exactly one Return().

You should have one rule that removes the final Return();

 @p2@
 identifier pdev;
 @@
  void rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev) {
  ...
 -Return();
  }

Then another rule to remove the others:

 @p2@
 identifier pdev;
 @@
  void rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev) {
  <...
 -Return();
 +return;
  ...>
  }

julia

> -------->8--------
>
> results in
>
> -------->8--------
> $ /usr/bin/spatch --debug -D patch --very-quiet --cocci-file scripts/coccinelle/api/test.cocci --patch . --dir drivers/mmc/host/rtsx_usb_sdmmc.c -I ./arch/x86/include -I ./arch/x86/include/generated -I ./include -I ./arch/x86/include/uapi -I ./arch/x86/include/generated/uapi -I ./include/uapi -I ./include/generated/uapi --include ./include/linux/compiler-version.h --include ./include/linux/kconfig.h --jobs 4 --chunksize 1
> diff -u -p a/drivers/mmc/host/rtsx_usb_sdmmc.c b/drivers/mmc/host/rtsx_usb_sdmmc.c
> --- a/drivers/mmc/host/rtsx_usb_sdmmc.c
> +++ b/drivers/mmc/host/rtsx_usb_sdmmc.c
> @@ -1385,7 +1385,7 @@ static int rtsx_usb_sdmmc_drv_remove(str
>  	struct mmc_host *mmc;
>
>  	if (!host)
> -		return 0;
> +		Return();
>
>  	mmc = host->mmc;
>  	host->host_removal = true;
> @@ -1416,7 +1416,7 @@ static int rtsx_usb_sdmmc_drv_remove(str
>  	dev_dbg(&(pdev->dev),
>  		": Realtek USB SD/MMC module has been removed\n");
>
> -	return 0;
> +	Return();
>  }
>
>  #ifdef CONFIG_PM
> -------->8--------
>
> Adding --debug doesn't give any hints.
>
> (And if I add another hunk inbeetween removing Return at the end of the
> function there is no effect either.)
>
> Do I need to split that in two spatches to make coccinelle cooperate?
>
> (If it matters, this is coccinelle as shipped by Debian, Version
> 1.1.1.deb-2)
>
> Best regards
> Uwe
>
> --
> Pengutronix e.K.                           | Uwe Kleine-König            |
> Industrial Linux Solutions                 | https://www.pengutronix.de/ |
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [cocci] coccinelle: How to remove a return at the end of a void function?
  2022-12-26 16:02     ` Julia Lawall
@ 2022-12-27  9:55       ` Uwe Kleine-König
  2022-12-27 11:26         ` Julia Lawall
  0 siblings, 1 reply; 7+ messages in thread
From: Uwe Kleine-König @ 2022-12-27  9:55 UTC (permalink / raw)
  To: Julia Lawall; +Cc: Nicolas Palix, linux-kernel, kernel, cocci

[-- Attachment #1: Type: text/plain, Size: 6859 bytes --]

Hello Julia,

On Mon, Dec 26, 2022 at 05:02:35PM +0100, Julia Lawall wrote:
> On Sun, 25 Dec 2022, Uwe Kleine-König wrote:
> 
> > Hello Julia,
> >
> > first of all thanks for your quick answer.
> >
> > On Sat, Dec 24, 2022 at 01:28:04PM +0100, Julia Lawall wrote:
> > > On Sat, 24 Dec 2022, Uwe Kleine-König wrote:
> > > > A simplified spatch looks as follows:
> > > >
> > > > -------->8--------
> > > > virtual patch
> > > >
> > > > @p1@
> > > > identifier pdev;
> > > > @@
> > > > -int
> > > > +void
> > > >  rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev) {
> > > >  <...
> > > > -return 0;
> > > > +return;
> > > >  ...>
> > > >  }
> > > > -------->8--------
> > > >
> > > > This results in:
> > > >
> > > > -------->8--------
> > > > diff -u -p a/drivers/mmc/host/rtsx_usb_sdmmc.c b/drivers/mmc/host/rtsx_usb_sdmmc.c
> > > > --- a/drivers/mmc/host/rtsx_usb_sdmmc.c
> > > > +++ b/drivers/mmc/host/rtsx_usb_sdmmc.c
> > > > @@ -1379,13 +1379,13 @@ static int rtsx_usb_sdmmc_drv_probe(stru
> > > >         return 0;
> > > >  }
> > > >
> > > > -static int rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev)
> > > > +static void rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev)
> > > >  {
> > > >         struct rtsx_usb_sdmmc *host = platform_get_drvdata(pdev);
> > > >         struct mmc_host *mmc;
> > > >
> > > >         if (!host)
> > > > -               return 0;
> > > > +               return;
> > > >
> > > >         mmc = host->mmc;
> > > >         host->host_removal = true;
> > > > @@ -1416,7 +1416,7 @@ static int rtsx_usb_sdmmc_drv_remove(str
> > > >         dev_dbg(&(pdev->dev),
> > > >                 ": Realtek USB SD/MMC module has been removed\n");
> > > >
> > > > -       return 0;
> > > > +       return;
> > > >  }
> > > >
> > > >  #ifdef CONFIG_PM
> > > > -------->8--------
> > > >
> > > > which is as intended. Now I want to remove the useless "return;" at the
> > > > end of the function, however adding
> > > >
> > > > -------->8--------
> > > > @p2 depends on p1@
> > > > identifier pdev;
> > > > @@
> > > >  void rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev) {
> > > >  ...
> > > > -return;
> > > >  }
> > > > -------->8--------
> > > >
> > > > to the spatch doesn't (only) do the intended:
> > >
> > > The problem is that Coccinelle is following the control-flow through the
> > > function, and all of the returns are at the end of a control.flow path.
> > > The simple, hacky solution is to change the return;s into some function
> > > call Return();, then do like the above for Return(); and then change the
> > > Return();s back to return;s
> >
> > OK, I tried, but somehow coccinelle refuse to work after I introduced
> > Return(), even replacing them by return; doesn't work:
> >
> > -------->8--------
> > virtual patch
> >
> > @p1@
> > identifier pdev;
> > @@
> > -int
> > +void
> >  rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev) {
> >  ...
> > -return 0;
> > +Return();
> >  ...
> >  }
> >
> > @p2@
> > identifier pdev;
> > @@
> >  void rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev) {
> >  ...
> > -Return();
> > +return;
> >  ...
> >  }
> 
> The problem is that a control-flow path at this point can have multiple
> calls to Return();  You pattern only matches when every control-flow path
> through the code has exactly one Return().

Ah, ok. This wasn't clear to me from reading the documentation (e.g.
https://coccinelle.gitlabpages.inria.fr/website/docs/main_grammar.html)
 
> You should have one rule that removes the final Return();
> 
>  @p2@
>  identifier pdev;
>  @@
>   void rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev) {
>   ...
>  -Return();
>   }
> 
> Then another rule to remove the others:
> 
>  @p2@
>  identifier pdev;
>  @@
>   void rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev) {
>   <...
>  -Return();
>  +return;
>   ...>
>   }

I now have

-------->8--------
virtual patch

@p1@
identifier pdev;
@@
-int
+void
 rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev) {
 ...
-return 0;
+Return();
 ...
 }

@p2@
identifier pdev;
@@
 void rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev) {
 ...
-Return();
 }

@p3@
identifier pdev;
@@
 void rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev) {
 <...
-Return();
+return;
 ...>
 }
-------->8--------

But there p2 suffers from the same problem and only matches
code paths with exactly 1 Return(). So the above results in

-------->8--------
diff -u -p a/drivers/mmc/host/rtsx_usb_sdmmc.c b/drivers/mmc/host/rtsx_usb_sdmmc.c
--- a/drivers/mmc/host/rtsx_usb_sdmmc.c
+++ b/drivers/mmc/host/rtsx_usb_sdmmc.c
@@ -1379,13 +1379,13 @@ static int rtsx_usb_sdmmc_drv_probe(stru
        return 0;
 }

-static int rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev)
+static void rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev)
 {
        struct rtsx_usb_sdmmc *host = platform_get_drvdata(pdev);
        struct mmc_host *mmc;

        if (!host)
-               return 0;
+               return;

        mmc = host->mmc;
        host->host_removal = true;
@@ -1416,7 +1416,7 @@ static int rtsx_usb_sdmmc_drv_remove(str
        dev_dbg(&(pdev->dev),
                ": Realtek USB SD/MMC module has been removed\n");

-       return 0;
+       return;
 }

 #ifdef CONFIG_PM
-------->8--------

and only if I remove the "if (!host) return 0;" block before patch
generation, the final return is also dropped. 

I think this is good enough for me, as there are not too many cases like
the above. If there is spatch that does the desired change (i.e.

-------->8--------
diff -u -p a/drivers/mmc/host/rtsx_usb_sdmmc.c b/drivers/mmc/host/rtsx_usb_sdmmc.c
--- a/drivers/mmc/host/rtsx_usb_sdmmc.c
+++ b/drivers/mmc/host/rtsx_usb_sdmmc.c
@@ -1379,13 +1379,11 @@ static int rtsx_usb_sdmmc_drv_probe(stru
        return 0;
 }

-static int rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev)
+static void rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev)
 {
        struct rtsx_usb_sdmmc *host = platform_get_drvdata(pdev);
        struct mmc_host *mmc;

        if (!host)
-               return 0;
+               return;

        mmc = host->mmc;
        host->host_removal = true;
@@ -1416,7 +1416,7 @@ static int rtsx_usb_sdmmc_drv_remove(str
        dev_dbg(&(pdev->dev),
                ": Realtek USB SD/MMC module has been removed\n");
-
-       return 0;
 }

 #ifdef CONFIG_PM
-------->8--------
) I'd be happy to hear and learn about it.

Thanks
Uwe

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

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [cocci] coccinelle: How to remove a return at the end of a void function?
  2022-12-27  9:55       ` Uwe Kleine-König
@ 2022-12-27 11:26         ` Julia Lawall
  2022-12-27 11:54           ` Uwe Kleine-König
  0 siblings, 1 reply; 7+ messages in thread
From: Julia Lawall @ 2022-12-27 11:26 UTC (permalink / raw)
  To: Uwe Kleine-König; +Cc: Nicolas Palix, linux-kernel, kernel, cocci

> @p2@
> identifier pdev;
> @@
>  void rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev) {
>  ...
> -Return();
>  }

You need when any on the ... to allow there to be Returns();s along the
way.

julia

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [cocci] coccinelle: How to remove a return at the end of a void function?
  2022-12-27 11:26         ` Julia Lawall
@ 2022-12-27 11:54           ` Uwe Kleine-König
  0 siblings, 0 replies; 7+ messages in thread
From: Uwe Kleine-König @ 2022-12-27 11:54 UTC (permalink / raw)
  To: Julia Lawall; +Cc: Nicolas Palix, linux-kernel, kernel, cocci

[-- Attachment #1: Type: text/plain, Size: 565 bytes --]

Hello Julia,

On Tue, Dec 27, 2022 at 12:26:50PM +0100, Julia Lawall wrote:
> > @p2@
> > identifier pdev;
> > @@
> >  void rtsx_usb_sdmmc_drv_remove(struct platform_device *pdev) {
> >  ...
> > -Return();
> >  }
> 
> You need when any on the ... to allow there to be Returns();s along the
> way.

\o/ that works fine, thanks

(i.e.

	... when any

instead of ...)

Best regards
Uwe

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

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-12-27 12:51 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-24 11:56 [cocci] coccinelle: How to remove a return at the end of a void function? Uwe Kleine-König
2022-12-24 12:28 ` Julia Lawall
2022-12-25 21:20   ` Uwe Kleine-König
2022-12-26 16:02     ` Julia Lawall
2022-12-27  9:55       ` Uwe Kleine-König
2022-12-27 11:26         ` Julia Lawall
2022-12-27 11:54           ` Uwe Kleine-König

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).