Re: [Cocci] Removing the last return statement from a void function

2021-03-18 Thread Julia Lawall



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


[Cocci] Removing the last return statement from a void function

2021-03-18 Thread Thomas Adam
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?

Thanks once more for your help.

Thomas
___
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci


Re: [Cocci] Replacing a struct member with a function call

2021-03-18 Thread Thomas Adam
Hi all,

Thanks for your help.  This is now resolved!

Kindly,
Thomas

On Tue, 16 Mar 2021 at 07:20, Julia Lawall  wrote:
>
>
>
> On Mon, 15 Mar 2021, Mansour Moufid wrote:
>
> > On Sun, Mar 14, 2021, 20:43 Thomas Adam  wrote:
> >   Hello,
> >
> >   I can see I was as clear as mud with my explanation -- apologies
> >   for
> >   that, so let me try again.
> >
> >   In my original example:
> >
> >   struct monitor {
> >   struct {
> >   int width;
> >   int height
> >   } virtual;
> >   };
> >
> >   ... the members width and height aren't required any more, as
> >   they're
> >   actually computable generically, and don't belong in that
> >   struct.
> >   Instead, I have separate functions which can provide those
> >   values.
> >
> >   So where I have in code, statements such as:
> >
> >   struct monitor *m = this_monitor();
> >   int foo = m->virutal.width;
> >
> >   I want to be able to substitute "m->virtual.width" with a
> >   function
> >   call "get_width()" -- which does not involve "struct monitor" at
> >   all.
> >   Indeed, the semantic patch I'm trying to apply now looks like
> >   this:
> >
> >   @@
> >   struct monitor *m;
> >   @@
> >
> >   - m->virtual.width;
> >   + get_width();
> >
> >   ... and although spatch doesn't tell me of any errors, when I
> >   run it
> >   over my codebase, no modifications are made.  So clearly I'm
> >   still
> >   doing something wrong.
> >
> >
> > Remove the semi-colons. ;)
>
> Good catch :)
>
> julia
___
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci