All of lore.kernel.org
 help / color / mirror / Atom feed
From: Julia Lawall <julia.lawall@inria.fr>
To: Thomas Adam <thomas@xteddy.org>
Cc: Coccinelle <cocci@systeme.lip6.fr>
Subject: Re: [Cocci] Removing the last return statement from a void function
Date: Thu, 18 Mar 2021 20:24:22 +0100 (CET)	[thread overview]
Message-ID: <alpine.DEB.2.22.394.2103182020110.2984@hadrien> (raw)
In-Reply-To: <CAOhcEPYTkAqYM4q4p6q=nvF3eS6QX_-ajRykn08YfnOK9+VHUg@mail.gmail.com>



On Thu, 18 Mar 2021, Thomas Adam wrote:

> Hello all,
>
> I've another Coccinelle question I'm hoping you can help me with.  The
> codebase I'm working on is old, and has some interesting styles which
> by themselves probably don't cause any problems, but newer C compilers
> are now starting to flag them.
>
> In particular, there seems to be a pattern in this code base of using
> explicit `return;` statements at the end of void functions.  Here's an
> example:
>
> static void broadcast_mini_icon(FvwmWindow *fw)
> {
>     if (!FMiniIconsSupported)
>     {
>         return;
>     }
>     if (fw->mini_pixmap_file && fw->mini_icon)
>     {
>         BroadcastFvwmPicture( M_MINI_ICON, FW_W(fw),
>             FW_W_FRAME(fw), (unsigned long)fw,
>             fw->mini_icon, fw->mini_pixmap_file);
>     }
>     return;
> }
>
> Here you can see the last return statement is not necessary.
>
> I'm trying to make coccinelle recognise this and remove such cases.
> Here's what I've tried:
>
> @@
> identifier f;
> @@
>
> void f(...) {
>   <...
> - return;
> ...>
>
> }
>
> ... which sort of works, but proceeds to remove *all* `return;`
> statements from void functions, rather than the last occurance in the
> function.
>
> Am I on the right track with this approach, or do I need to do
> something more creative?

The ... in Coccinelle is based on control flow, so it is a bit hard to
find the return at the bottom of the function.  Actually, from
Coccinelle's point of view, all returns are at the bottom of the function,
because one leaves the function after a return.

You can try the following:

@r@
position p;
identifier f;
}

f(...) {
<...
{ .. return@p; }
...>
}

@@
position p != r.p;
@@

- return@p;

Basically the first rule collects the position of all returns that are
inside a { }, and then the second rule removes the others.

However there is an isomorphism that makes a pattern with { ... S } match
just S, for any S, which you don't want.  So you can make an empty file
called empty.iso, and then run the rule with the command-line argument
--iso-file empty.iso

julia
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

  reply	other threads:[~2021-03-18 19:24 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-18 18:26 [Cocci] Removing the last return statement from a void function Thomas Adam
2021-03-18 19:24 ` Julia Lawall [this message]
2021-03-19 20:40   ` Thomas Adam
2021-03-19 21:10     ` Julia Lawall

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.DEB.2.22.394.2103182020110.2984@hadrien \
    --to=julia.lawall@inria.fr \
    --cc=cocci@systeme.lip6.fr \
    --cc=thomas@xteddy.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.