All of lore.kernel.org
 help / color / mirror / Atom feed
* [cocci] "not supported"
@ 2022-10-20  4:56 Jason A. Donenfeld
  2022-10-20  5:11 ` Julia Lawall
  0 siblings, 1 reply; 19+ messages in thread
From: Jason A. Donenfeld @ 2022-10-20  4:56 UTC (permalink / raw)
  To: cocci

Hi,

I'm trying to generically replace constructions that look like this:

@@
expression E;
type T;
identifier I;
@@
  {
-   T I;
-   do {
-     I = get_random_u32();
-   } while (I > E);
-   return I;
+   return get_random_u32_below(E + 1);
  }

But trying to run it results in "not supported". Any idea what's up?

Also, it'd be nice if there was a way for Coccinelle to figure out
variants of that, such as I >= E in a succinct way, and maybe other
different loop constructions that amount to the same thing.

Is there any way to express what I'm after?

Thanks,
Jason

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

* Re: [cocci] "not supported"
  2022-10-20  4:56 [cocci] "not supported" Jason A. Donenfeld
@ 2022-10-20  5:11 ` Julia Lawall
  2022-10-22  1:59   ` Jason A. Donenfeld
  0 siblings, 1 reply; 19+ messages in thread
From: Julia Lawall @ 2022-10-20  5:11 UTC (permalink / raw)
  To: Jason A. Donenfeld; +Cc: cocci



On Wed, 19 Oct 2022, Jason A. Donenfeld wrote:

> Hi,
>
> I'm trying to generically replace constructions that look like this:
>
> @@
> expression E;
> type T;
> identifier I;
> @@
>   {
> -   T I;
> -   do {
> -     I = get_random_u32();
> -   } while (I > E);
> -   return I;
> +   return get_random_u32_below(E + 1);
>   }
>
> But trying to run it results in "not supported". Any idea what's up?

Maybe you have an old version of Coccinelle?  For a long time do while was
not supported.  But it's supported now, and your rule seemed to work fine
for me.

>
> Also, it'd be nice if there was a way for Coccinelle to figure out
> variants of that, such as I >= E in a succinct way, and maybe other
> different loop constructions that amount to the same thing.
>
> Is there any way to express what I'm after?

The only way Coccinelle figures out variants is with its isomorphisms, and
allowing I > E to match I >= E seems strange as a general rule.  There are
the following isomoprhisms:

Expression
@ gtr_lss @
expression X, Y;
@@
X < Y <=> Y > X

Expression
@ gtr_lss_eq @
expression X, Y;
@@
X <= Y <=> Y >= X

You may want to at least consider putting a ... when != I just under
the T I.

You may want to write a rule like:

do { ...
*  I = get_random_u32();
  ...
} while (...);

To see what the possible options are.

julia

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

* Re: [cocci] "not supported"
  2022-10-20  5:11 ` Julia Lawall
@ 2022-10-22  1:59   ` Jason A. Donenfeld
  2022-10-22  8:03     ` Julia Lawall
                       ` (3 more replies)
  0 siblings, 4 replies; 19+ messages in thread
From: Jason A. Donenfeld @ 2022-10-22  1:59 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci

Hi Julia,

On Thu, Oct 20, 2022 at 1:11 AM Julia Lawall <julia.lawall@inria.fr> wrote:
> Maybe you have an old version of Coccinelle?  For a long time do while was
> not supported.  But it's supported now, and your rule seemed to work fine
> for me.

Bingo, that worked. Thanks. Might consider cutting a new release soon
so that that's available on OPAM. I built it myself for the latest.

I just sent these in:
https://lore.kernel.org/lkml/20221022014403.3881893-5-Jason@zx2c4.com/

I wound up just having a bunch of different instances, rather than
trying to teach Coccinelle about strange equivalences.

There's another one in that series that's kind of interesting:

https://lore.kernel.org/lkml/20221022014403.3881893-6-Jason@zx2c4.com/

This one has a very simple rule:

@@
expression H;
expression L;
@@
- (get_random_u32_below(H) + L)
+ get_random_u32_between(L, H + L)

This worked as expected, but I had to do a lot of hand simplification.
For example, a lot of code had a pattern of:

get_random_u32_below(H - L + 1) + L

which then got converted into

get_random_u32_between(L, H - L + 1 + L)

which I then had to clean up manually into

get_random_u32_between(L, H + 1)

It's not like that's all that hard to do. But it would be cool if
Cocinnelle had some sort of algebraic simplification possibility.

Jason

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

* Re: [cocci] "not supported"
  2022-10-22  1:59   ` Jason A. Donenfeld
@ 2022-10-22  8:03     ` Julia Lawall
  2022-10-22 19:45     ` [cocci] Replacing special do { … } while loops by selected function calls Markus Elfring
                       ` (2 subsequent siblings)
  3 siblings, 0 replies; 19+ messages in thread
From: Julia Lawall @ 2022-10-22  8:03 UTC (permalink / raw)
  To: Jason A. Donenfeld; +Cc: cocci



On Fri, 21 Oct 2022, Jason A. Donenfeld wrote:

> Hi Julia,
>
> On Thu, Oct 20, 2022 at 1:11 AM Julia Lawall <julia.lawall@inria.fr> wrote:
> > Maybe you have an old version of Coccinelle?  For a long time do while was
> > not supported.  But it's supported now, and your rule seemed to work fine
> > for me.
>
> Bingo, that worked. Thanks. Might consider cutting a new release soon
> so that that's available on OPAM. I built it myself for the latest.

Yes, I'll try to do that soon.

>
> I just sent these in:
> https://lore.kernel.org/lkml/20221022014403.3881893-5-Jason@zx2c4.com/
>
> I wound up just having a bunch of different instances, rather than
> trying to teach Coccinelle about strange equivalences.
>
> There's another one in that series that's kind of interesting:
>
> https://lore.kernel.org/lkml/20221022014403.3881893-6-Jason@zx2c4.com/
>
> This one has a very simple rule:
>
> @@
> expression H;
> expression L;
> @@
> - (get_random_u32_below(H) + L)
> + get_random_u32_between(L, H + L)
>
> This worked as expected, but I had to do a lot of hand simplification.
> For example, a lot of code had a pattern of:
>
> get_random_u32_below(H - L + 1) + L
>
> which then got converted into
>
> get_random_u32_between(L, H - L + 1 + L)
>
> which I then had to clean up manually into
>
> get_random_u32_between(L, H + 1)
>
> It's not like that's all that hard to do. But it would be cool if
> Cocinnelle had some sort of algebraic simplification possibility.

Maybe it's a bit special case for Coccinelle.  If there were many
recurring cases you could make extra rules.

julia

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

* Re: [cocci] Replacing special do { … } while loops by selected function calls
  2022-10-22  1:59   ` Jason A. Donenfeld
  2022-10-22  8:03     ` Julia Lawall
@ 2022-10-22 19:45     ` Markus Elfring
  2022-10-23 19:36       ` Jason A. Donenfeld
  2022-10-23 11:18     ` [cocci] Checking the replacement “get_random_u32_above(0)” Markus Elfring
  2022-10-23 13:55     ` [cocci] algebraic simplification possibilities with SmPL Markus Elfring
  3 siblings, 1 reply; 19+ messages in thread
From: Markus Elfring @ 2022-10-22 19:45 UTC (permalink / raw)
  To: Jason A. Donenfeld; +Cc: Julia Lawall, cocci

> I just sent these in:
> https://lore.kernel.org/lkml/20221022014403.3881893-5-Jason@zx2c4.com/

I suggest to reconsider the usage of SmPL ellipses in more detail
according to your SmPL code demonstration in the description for the change
suggestion “[PATCH v1 4/5] treewide: use get_random_u32_{above,below}()
instead of manual loop”.

Regards,
Markus

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

* Re: [cocci] Checking the replacement “get_random_u32_above(0)”
  2022-10-22  1:59   ` Jason A. Donenfeld
  2022-10-22  8:03     ` Julia Lawall
  2022-10-22 19:45     ` [cocci] Replacing special do { … } while loops by selected function calls Markus Elfring
@ 2022-10-23 11:18     ` Markus Elfring
  2022-10-23 19:37       ` Jason A. Donenfeld
  2022-10-23 13:55     ` [cocci] algebraic simplification possibilities with SmPL Markus Elfring
  3 siblings, 1 reply; 19+ messages in thread
From: Markus Elfring @ 2022-10-23 11:18 UTC (permalink / raw)
  To: Jason A. Donenfeld; +Cc: Julia Lawall, cocci

> I just sent these in:
> https://lore.kernel.org/lkml/20221022014403.3881893-5-Jason@zx2c4.com/

I find the following code details also suspicious from two SmPL rules.

…
-   } while (!I);
+   I = get_random_u32_above(0);
…
-   } while (I == 0);
+   I = get_random_u32_above(0);
…


* Should such case distinctions be reconsidered?

* Can different function parameters become relevant here?


Regards,
Markus

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

* Re: [cocci] algebraic simplification possibilities with SmPL
  2022-10-22  1:59   ` Jason A. Donenfeld
                       ` (2 preceding siblings ...)
  2022-10-23 11:18     ` [cocci] Checking the replacement “get_random_u32_above(0)” Markus Elfring
@ 2022-10-23 13:55     ` Markus Elfring
  2022-10-23 19:39       ` Jason A. Donenfeld
  3 siblings, 1 reply; 19+ messages in thread
From: Markus Elfring @ 2022-10-23 13:55 UTC (permalink / raw)
  To: Jason A. Donenfeld; +Cc: Julia Lawall, cocci

> There's another one in that series that's kind of interesting:
>
> https://lore.kernel.org/lkml/20221022014403.3881893-6-Jason@zx2c4.com/
>
> This one has a very simple rule:
>
> @@
> expression H;
> expression L;
> @@
> - (get_random_u32_below(H) + L)
> + get_random_u32_between(L, H + L)
>
> This worked as expected, but I had to do a lot of hand simplification.
> For example, a lot of code had a pattern of:
>
> get_random_u32_below(H - L + 1) + L
>
> which then got converted into
>
> get_random_u32_between(L, H - L + 1 + L)
>
> which I then had to clean up manually into
>
> get_random_u32_between(L, H + 1)
>
> It's not like that's all that hard to do. But it would be cool if
> Cocinnelle had some sort of algebraic simplification possibility.

I imagine also that it will occasionally be nicer if further optimisation
rules would belong to the standard functionality of the Coccinelle software.
But I guess that you can achieve desirable transformations also by using
additional rules for the semantic patch language.

How do you think about a change approach like the following?

@cleanup@
identifier H, L;
@@
 get_random_u32_between(L,
                        H
-                         - L
                          + 1
-                         + L
                       )


Regards,
Markus

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

* Re: [cocci] Replacing special do { … } while loops by selected function calls
  2022-10-22 19:45     ` [cocci] Replacing special do { … } while loops by selected function calls Markus Elfring
@ 2022-10-23 19:36       ` Jason A. Donenfeld
  2022-10-24  8:14         ` Markus Elfring
  0 siblings, 1 reply; 19+ messages in thread
From: Jason A. Donenfeld @ 2022-10-23 19:36 UTC (permalink / raw)
  To: Markus Elfring; +Cc: Julia Lawall, cocci

On Sat, Oct 22, 2022 at 09:45:43PM +0200, Markus Elfring wrote:
> > I just sent these in:
> > https://lore.kernel.org/lkml/20221022014403.3881893-5-Jason@zx2c4.com/
> 
> I suggest to reconsider the usage of SmPL ellipses in more detail
> according to your SmPL code demonstration in the description for the change
> suggestion “[PATCH v1 4/5] treewide: use get_random_u32_{above,below}()
> instead of manual loop”.

I'll do `... when != I`.

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

* Re: [cocci] Checking the replacement “get_random_u32_above(0)”
  2022-10-23 11:18     ` [cocci] Checking the replacement “get_random_u32_above(0)” Markus Elfring
@ 2022-10-23 19:37       ` Jason A. Donenfeld
  2022-10-24  7:18         ` Markus Elfring
  0 siblings, 1 reply; 19+ messages in thread
From: Jason A. Donenfeld @ 2022-10-23 19:37 UTC (permalink / raw)
  To: Markus Elfring; +Cc: Julia Lawall, cocci

On Sun, Oct 23, 2022 at 01:18:32PM +0200, Markus Elfring wrote:
> > I just sent these in:
> > https://lore.kernel.org/lkml/20221022014403.3881893-5-Jason@zx2c4.com/
> 
> I find the following code details also suspicious from two SmPL rules.
> 
> …
> -   } while (!I);
> +   I = get_random_u32_above(0);
> …
> -   } while (I == 0);
> +   I = get_random_u32_above(0);
> …
> 
> 
> * Should such case distinctions be reconsidered?
> 
> * Can different function parameters become relevant here?

* Can you be more specific?

I have no idea what you're getting at.

Jason

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

* Re: [cocci] algebraic simplification possibilities with SmPL
  2022-10-23 13:55     ` [cocci] algebraic simplification possibilities with SmPL Markus Elfring
@ 2022-10-23 19:39       ` Jason A. Donenfeld
  2022-10-23 19:45         ` Julia Lawall
                           ` (2 more replies)
  0 siblings, 3 replies; 19+ messages in thread
From: Jason A. Donenfeld @ 2022-10-23 19:39 UTC (permalink / raw)
  To: Markus Elfring; +Cc: Julia Lawall, cocci

On Sun, Oct 23, 2022 at 03:55:23PM +0200, Markus Elfring wrote:
> > There's another one in that series that's kind of interesting:
> >
> > https://lore.kernel.org/lkml/20221022014403.3881893-6-Jason@zx2c4.com/
> >
> > This one has a very simple rule:
> >
> > @@
> > expression H;
> > expression L;
> > @@
> > - (get_random_u32_below(H) + L)
> > + get_random_u32_between(L, H + L)
> >
> > This worked as expected, but I had to do a lot of hand simplification.
> > For example, a lot of code had a pattern of:
> >
> > get_random_u32_below(H - L + 1) + L
> >
> > which then got converted into
> >
> > get_random_u32_between(L, H - L + 1 + L)
> >
> > which I then had to clean up manually into
> >
> > get_random_u32_between(L, H + 1)
> >
> > It's not like that's all that hard to do. But it would be cool if
> > Cocinnelle had some sort of algebraic simplification possibility.
> 
> I imagine also that it will occasionally be nicer if further optimisation
> rules would belong to the standard functionality of the Coccinelle software.
> But I guess that you can achieve desirable transformations also by using
> additional rules for the semantic patch language.
> 
> How do you think about a change approach like the following?
> 
> @cleanup@
> identifier H, L;
> @@
>  get_random_u32_between(L,
>                         H
> -                         - L
>                           + 1
> -                         + L
>                        )
> 

I can do something like that. These seem to work:

@@
expression H;
expression L;
@@
- (get_random_u32_below(H) + L)
+ get_random_u32_between(L, H + L)

@@
expression H;
expression L;
expression E;
@@
  get_random_u32_between(L,
  H
- + E
- - E
  )

@@
expression H;
expression L;
expression E;
@@
  get_random_u32_between(L,
  H
- - E
- + E
  )

@@
expression H;
expression L;
expression E;
expression F;
@@
  get_random_u32_between(L,
  H
- - E
  + F
- + E
  )

@@
expression H;
expression L;
expression E;
expression F;
@@
  get_random_u32_between(L,
  H
- + E
  + F
- - E
  )


Kind of annoying that Coccinelle doesn't do commutativity itself, but
that's fine.

Jason

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

* Re: [cocci] algebraic simplification possibilities with SmPL
  2022-10-23 19:39       ` Jason A. Donenfeld
@ 2022-10-23 19:45         ` Julia Lawall
  2022-10-23 19:59           ` Jason A. Donenfeld
  2022-10-24 13:12         ` Markus Elfring
  2022-10-24 16:30         ` Markus Elfring
  2 siblings, 1 reply; 19+ messages in thread
From: Julia Lawall @ 2022-10-23 19:45 UTC (permalink / raw)
  To: Jason A. Donenfeld; +Cc: Markus Elfring, cocci



On Sun, 23 Oct 2022, Jason A. Donenfeld wrote:

> On Sun, Oct 23, 2022 at 03:55:23PM +0200, Markus Elfring wrote:
> > > There's another one in that series that's kind of interesting:
> > >
> > > https://lore.kernel.org/lkml/20221022014403.3881893-6-Jason@zx2c4.com/
> > >
> > > This one has a very simple rule:
> > >
> > > @@
> > > expression H;
> > > expression L;
> > > @@
> > > - (get_random_u32_below(H) + L)
> > > + get_random_u32_between(L, H + L)
> > >
> > > This worked as expected, but I had to do a lot of hand simplification.
> > > For example, a lot of code had a pattern of:
> > >
> > > get_random_u32_below(H - L + 1) + L
> > >
> > > which then got converted into
> > >
> > > get_random_u32_between(L, H - L + 1 + L)
> > >
> > > which I then had to clean up manually into
> > >
> > > get_random_u32_between(L, H + 1)
> > >
> > > It's not like that's all that hard to do. But it would be cool if
> > > Cocinnelle had some sort of algebraic simplification possibility.
> >
> > I imagine also that it will occasionally be nicer if further optimisation
> > rules would belong to the standard functionality of the Coccinelle software.
> > But I guess that you can achieve desirable transformations also by using
> > additional rules for the semantic patch language.
> >
> > How do you think about a change approach like the following?
> >
> > @cleanup@
> > identifier H, L;
> > @@
> >  get_random_u32_between(L,
> >                         H
> > -                         - L
> >                           + 1
> > -                         + L
> >                        )
> >
>
> I can do something like that. These seem to work:
>
> @@
> expression H;
> expression L;
> @@
> - (get_random_u32_below(H) + L)
> + get_random_u32_between(L, H + L)
>
> @@
> expression H;
> expression L;
> expression E;
> @@
>   get_random_u32_between(L,
>   H
> - + E
> - - E
>   )
>
> @@
> expression H;
> expression L;
> expression E;
> @@
>   get_random_u32_between(L,
>   H
> - - E
> - + E
>   )
>
> @@
> expression H;
> expression L;
> expression E;
> expression F;
> @@
>   get_random_u32_between(L,
>   H
> - - E
>   + F
> - + E
>   )
>
> @@
> expression H;
> expression L;
> expression E;
> expression F;
> @@
>   get_random_u32_between(L,
>   H
> - + E
>   + F
> - - E
>   )
>
>
> Kind of annoying that Coccinelle doesn't do commutativity itself, but
> that's fine.

The parse tree is probably not what you would hope it to be.  It's
probably, eg:

(a + b) + c

so it's not straightforward to exchange b and c, without considering lots
of special cases.

julia

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

* Re: [cocci] algebraic simplification possibilities with SmPL
  2022-10-23 19:45         ` Julia Lawall
@ 2022-10-23 19:59           ` Jason A. Donenfeld
  2022-10-23 20:09             ` Julia Lawall
  0 siblings, 1 reply; 19+ messages in thread
From: Jason A. Donenfeld @ 2022-10-23 19:59 UTC (permalink / raw)
  To: Julia Lawall; +Cc: Markus Elfring, cocci

On Sun, Oct 23, 2022 at 09:45:42PM +0200, Julia Lawall wrote:
> 
> 
> On Sun, 23 Oct 2022, Jason A. Donenfeld wrote:
> 
> > On Sun, Oct 23, 2022 at 03:55:23PM +0200, Markus Elfring wrote:
> > > > There's another one in that series that's kind of interesting:
> > > >
> > > > https://lore.kernel.org/lkml/20221022014403.3881893-6-Jason@zx2c4.com/
> > > >
> > > > This one has a very simple rule:
> > > >
> > > > @@
> > > > expression H;
> > > > expression L;
> > > > @@
> > > > - (get_random_u32_below(H) + L)
> > > > + get_random_u32_between(L, H + L)
> > > >
> > > > This worked as expected, but I had to do a lot of hand simplification.
> > > > For example, a lot of code had a pattern of:
> > > >
> > > > get_random_u32_below(H - L + 1) + L
> > > >
> > > > which then got converted into
> > > >
> > > > get_random_u32_between(L, H - L + 1 + L)
> > > >
> > > > which I then had to clean up manually into
> > > >
> > > > get_random_u32_between(L, H + 1)
> > > >
> > > > It's not like that's all that hard to do. But it would be cool if
> > > > Cocinnelle had some sort of algebraic simplification possibility.
> > >
> > > I imagine also that it will occasionally be nicer if further optimisation
> > > rules would belong to the standard functionality of the Coccinelle software.
> > > But I guess that you can achieve desirable transformations also by using
> > > additional rules for the semantic patch language.
> > >
> > > How do you think about a change approach like the following?
> > >
> > > @cleanup@
> > > identifier H, L;
> > > @@
> > >  get_random_u32_between(L,
> > >                         H
> > > -                         - L
> > >                           + 1
> > > -                         + L
> > >                        )
> > >
> >
> > I can do something like that. These seem to work:
> >
> > @@
> > expression H;
> > expression L;
> > @@
> > - (get_random_u32_below(H) + L)
> > + get_random_u32_between(L, H + L)
> >
> > @@
> > expression H;
> > expression L;
> > expression E;
> > @@
> >   get_random_u32_between(L,
> >   H
> > - + E
> > - - E
> >   )
> >
> > @@
> > expression H;
> > expression L;
> > expression E;
> > @@
> >   get_random_u32_between(L,
> >   H
> > - - E
> > - + E
> >   )
> >
> > @@
> > expression H;
> > expression L;
> > expression E;
> > expression F;
> > @@
> >   get_random_u32_between(L,
> >   H
> > - - E
> >   + F
> > - + E
> >   )
> >
> > @@
> > expression H;
> > expression L;
> > expression E;
> > expression F;
> > @@
> >   get_random_u32_between(L,
> >   H
> > - + E
> >   + F
> > - - E
> >   )
> >
> >
> > Kind of annoying that Coccinelle doesn't do commutativity itself, but
> > that's fine.
> 
> The parse tree is probably not what you would hope it to be.  It's
> probably, eg:
> 
> (a + b) + c
> 
> so it's not straightforward to exchange b and c, without considering lots
> of special cases.

The general solution might be a pass that removes parens when they don't
influence the order of operations?

Jason

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

* Re: [cocci] algebraic simplification possibilities with SmPL
  2022-10-23 19:59           ` Jason A. Donenfeld
@ 2022-10-23 20:09             ` Julia Lawall
  0 siblings, 0 replies; 19+ messages in thread
From: Julia Lawall @ 2022-10-23 20:09 UTC (permalink / raw)
  To: Jason A. Donenfeld; +Cc: Markus Elfring, cocci



On Sun, 23 Oct 2022, Jason A. Donenfeld wrote:

> On Sun, Oct 23, 2022 at 09:45:42PM +0200, Julia Lawall wrote:
> >
> >
> > On Sun, 23 Oct 2022, Jason A. Donenfeld wrote:
> >
> > > On Sun, Oct 23, 2022 at 03:55:23PM +0200, Markus Elfring wrote:
> > > > > There's another one in that series that's kind of interesting:
> > > > >
> > > > > https://lore.kernel.org/lkml/20221022014403.3881893-6-Jason@zx2c4.com/
> > > > >
> > > > > This one has a very simple rule:
> > > > >
> > > > > @@
> > > > > expression H;
> > > > > expression L;
> > > > > @@
> > > > > - (get_random_u32_below(H) + L)
> > > > > + get_random_u32_between(L, H + L)
> > > > >
> > > > > This worked as expected, but I had to do a lot of hand simplification.
> > > > > For example, a lot of code had a pattern of:
> > > > >
> > > > > get_random_u32_below(H - L + 1) + L
> > > > >
> > > > > which then got converted into
> > > > >
> > > > > get_random_u32_between(L, H - L + 1 + L)
> > > > >
> > > > > which I then had to clean up manually into
> > > > >
> > > > > get_random_u32_between(L, H + 1)
> > > > >
> > > > > It's not like that's all that hard to do. But it would be cool if
> > > > > Cocinnelle had some sort of algebraic simplification possibility.
> > > >
> > > > I imagine also that it will occasionally be nicer if further optimisation
> > > > rules would belong to the standard functionality of the Coccinelle software.
> > > > But I guess that you can achieve desirable transformations also by using
> > > > additional rules for the semantic patch language.
> > > >
> > > > How do you think about a change approach like the following?
> > > >
> > > > @cleanup@
> > > > identifier H, L;
> > > > @@
> > > >  get_random_u32_between(L,
> > > >                         H
> > > > -                         - L
> > > >                           + 1
> > > > -                         + L
> > > >                        )
> > > >
> > >
> > > I can do something like that. These seem to work:
> > >
> > > @@
> > > expression H;
> > > expression L;
> > > @@
> > > - (get_random_u32_below(H) + L)
> > > + get_random_u32_between(L, H + L)
> > >
> > > @@
> > > expression H;
> > > expression L;
> > > expression E;
> > > @@
> > >   get_random_u32_between(L,
> > >   H
> > > - + E
> > > - - E
> > >   )
> > >
> > > @@
> > > expression H;
> > > expression L;
> > > expression E;
> > > @@
> > >   get_random_u32_between(L,
> > >   H
> > > - - E
> > > - + E
> > >   )
> > >
> > > @@
> > > expression H;
> > > expression L;
> > > expression E;
> > > expression F;
> > > @@
> > >   get_random_u32_between(L,
> > >   H
> > > - - E
> > >   + F
> > > - + E
> > >   )
> > >
> > > @@
> > > expression H;
> > > expression L;
> > > expression E;
> > > expression F;
> > > @@
> > >   get_random_u32_between(L,
> > >   H
> > > - + E
> > >   + F
> > > - - E
> > >   )
> > >
> > >
> > > Kind of annoying that Coccinelle doesn't do commutativity itself, but
> > > that's fine.
> >
> > The parse tree is probably not what you would hope it to be.  It's
> > probably, eg:
> >
> > (a + b) + c
> >
> > so it's not straightforward to exchange b and c, without considering lots
> > of special cases.
>
> The general solution might be a pass that removes parens when they don't
> influence the order of operations?

Sorry, they weren't real parentheses.  They were showing how the AST is
organized.  b and c aren't at the same level.

julia

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

* Re: [cocci] Checking the replacement “get_random_u32_above(0)”
  2022-10-23 19:37       ` Jason A. Donenfeld
@ 2022-10-24  7:18         ` Markus Elfring
  0 siblings, 0 replies; 19+ messages in thread
From: Markus Elfring @ 2022-10-24  7:18 UTC (permalink / raw)
  To: Jason A. Donenfeld; +Cc: Julia Lawall, cocci

> > > I just sent these in:
> > > https://lore.kernel.org/lkml/20221022014403.3881893-5-Jason@zx2c4.com/
> > 
> > I find the following code details also suspicious from two SmPL rules.
> > 
> > …
> > -   } while (!I);
> > +   I = get_random_u32_above(0);
> > …
> > -   } while (I == 0);
> > +   I = get_random_u32_above(0);
> > …
> > 
> > 
> > * Should such case distinctions be reconsidered?
> > 
> > * Can different function parameters become relevant here?
> 
> * Can you be more specific?
> 
> I have no idea what you're getting at.

I doubt that you would like to map the condition checks “!I” and “I == 0”
to the same function call.

If you would insist to cobble them together, I imagine that it would be nicer
to combine discussed SmPL rules into one by using SmPL disjunctions accordingly.

Regards,
Markus

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

* Re: [cocci] Replacing special do { … } while loops by selected function calls
  2022-10-23 19:36       ` Jason A. Donenfeld
@ 2022-10-24  8:14         ` Markus Elfring
  0 siblings, 0 replies; 19+ messages in thread
From: Markus Elfring @ 2022-10-24  8:14 UTC (permalink / raw)
  To: Jason A. Donenfeld; +Cc: Julia Lawall, cocci

> > > I just sent these in:
> > > https://lore.kernel.org/lkml/20221022014403.3881893-5-Jason@zx2c4.com/
> > 
> > I suggest to reconsider the usage of SmPL ellipses in more detail
> > according to your SmPL code demonstration in the description for the change
> > suggestion “[PATCH v1 4/5] treewide: use get_random_u32_{above,below}()
> > instead of manual loop”.
> 
> I'll do `... when != I`.

I find this feedback too terse.

Code exclusion specifications can occasionally be helpful.
I imagine that further case distinctions will point a need out to consider
clarification for additional SmPL code design variants.

Regards,
Markus

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

* Re: [cocci] algebraic simplification possibilities with SmPL
  2022-10-23 19:39       ` Jason A. Donenfeld
  2022-10-23 19:45         ` Julia Lawall
@ 2022-10-24 13:12         ` Markus Elfring
  2022-10-24 16:14           ` Julia Lawall
  2022-10-24 16:30         ` Markus Elfring
  2 siblings, 1 reply; 19+ messages in thread
From: Markus Elfring @ 2022-10-24 13:12 UTC (permalink / raw)
  To: Julia Lawall, cocci; +Cc: Jason A. Donenfeld

> I can do something like that. These seem to work:

I wonder about another error response from the software “Coccinelle 1.1.1”
for an SmPL code variant like the following.


@replacement@
expression H, L;
@@
-(get_random_u32_below(H) + L)
+get_random_u32_between(L, H + L)

@cleanup@
expression H, L, E, F;
@@
 get_random_u32_between(L,
  H
(
- - E
  + F
- + E
|
- + E
  + F
- - E
|
- - E
- + E
|
- + E
- - E
)
                       )


linux@linux:…/Projekte/Coccinelle/Probe> spatch --parse-cocci suggestion3_for_Jason_Donenfeld-20221024.cocci
…
minus: parse error: 
  File "suggestion3_for_Jason_Donenfeld-20221024.cocci", line 12, column 0, charpos = 167
  around = '(',
  whole content = (


Can such a test case trigger any further improvements for the advanced application
of SmPL disjunctions?

Regards,
Markus

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

* Re: [cocci] algebraic simplification possibilities with SmPL
  2022-10-24 13:12         ` Markus Elfring
@ 2022-10-24 16:14           ` Julia Lawall
  0 siblings, 0 replies; 19+ messages in thread
From: Julia Lawall @ 2022-10-24 16:14 UTC (permalink / raw)
  To: Markus Elfring; +Cc: cocci, Jason A. Donenfeld

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



On Mon, 24 Oct 2022, Markus Elfring wrote:

> > I can do something like that. These seem to work:
>
> I wonder about another error response from the software “Coccinelle 1.1.1”
> for an SmPL code variant like the following.

You can't make a disjunction like that.  The branches of a disjunction has
to be complete terms, not random strings of tokens.

julia


>
>
> @replacement@
> expression H, L;
> @@
> -(get_random_u32_below(H) + L)
> +get_random_u32_between(L, H + L)
>
> @cleanup@
> expression H, L, E, F;
> @@
>  get_random_u32_between(L,
>   H
> (
> - - E
>   + F
> - + E
> |
> - + E
>   + F
> - - E
> |
> - - E
> - + E
> |
> - + E
> - - E
> )
>                        )
>
>
> linux@linux:…/Projekte/Coccinelle/Probe> spatch --parse-cocci suggestion3_for_Jason_Donenfeld-20221024.cocci
> …
> minus: parse error:
>   File "suggestion3_for_Jason_Donenfeld-20221024.cocci", line 12, column 0, charpos = 167
>   around = '(',
>   whole content = (
>
>
> Can such a test case trigger any further improvements for the advanced application
> of SmPL disjunctions?
>
> Regards,
> Markus
>

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

* Re: [cocci] algebraic simplification possibilities with SmPL
  2022-10-23 19:39       ` Jason A. Donenfeld
  2022-10-23 19:45         ` Julia Lawall
  2022-10-24 13:12         ` Markus Elfring
@ 2022-10-24 16:30         ` Markus Elfring
  2022-10-24 16:39           ` Julia Lawall
  2 siblings, 1 reply; 19+ messages in thread
From: Markus Elfring @ 2022-10-24 16:30 UTC (permalink / raw)
  To: Jason A. Donenfeld; +Cc: Julia Lawall, cocci

> I can do something like that. These seem to work:

The software “Coccinelle 1.1.1” gave me the impression that the following
SmPL code variant can be acceptable.


@replacement@
expression H, L;
@@
-(get_random_u32_below(H) + L)
+get_random_u32_between(L, H + L)

@cleanup@
expression H, L, E, F;
@@
(
 get_random_u32_between
 (L,
  H
- - E
  + F
- + E
 )
|
 get_random_u32_between
 (L,
  H
- + E
  + F
- - E
 )
|
 get_random_u32_between
 (L,
  H
- - E
- + E
 )
|
 get_random_u32_between
 (L,
  H
- + E
- - E
 )
)


Regards,
Markus

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

* Re: [cocci] algebraic simplification possibilities with SmPL
  2022-10-24 16:30         ` Markus Elfring
@ 2022-10-24 16:39           ` Julia Lawall
  0 siblings, 0 replies; 19+ messages in thread
From: Julia Lawall @ 2022-10-24 16:39 UTC (permalink / raw)
  To: Markus Elfring; +Cc: Jason A. Donenfeld, cocci

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



On Mon, 24 Oct 2022, Markus Elfring wrote:

> > I can do something like that. These seem to work:
>
> The software “Coccinelle 1.1.1” gave me the impression that the following
> SmPL code variant can be acceptable.
>
>
> @replacement@
> expression H, L;
> @@
> -(get_random_u32_below(H) + L)
> +get_random_u32_between(L, H + L)
>
> @cleanup@
> expression H, L, E, F;
> @@
> (
>  get_random_u32_between
>  (L,
>   H
> - - E
>   + F
> - + E
>  )
> |
>  get_random_u32_between
>  (L,
>   H
> - + E
>   + F
> - - E
>  )
> |
>  get_random_u32_between
>  (L,
>   H
> - - E
> - + E
>  )
> |
>  get_random_u32_between
>  (L,
>   H
> - + E
> - - E
>  )
> )

That is fine.

julia

>
>
> Regards,
> Markus
>

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

end of thread, other threads:[~2022-10-24 16:39 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-20  4:56 [cocci] "not supported" Jason A. Donenfeld
2022-10-20  5:11 ` Julia Lawall
2022-10-22  1:59   ` Jason A. Donenfeld
2022-10-22  8:03     ` Julia Lawall
2022-10-22 19:45     ` [cocci] Replacing special do { … } while loops by selected function calls Markus Elfring
2022-10-23 19:36       ` Jason A. Donenfeld
2022-10-24  8:14         ` Markus Elfring
2022-10-23 11:18     ` [cocci] Checking the replacement “get_random_u32_above(0)” Markus Elfring
2022-10-23 19:37       ` Jason A. Donenfeld
2022-10-24  7:18         ` Markus Elfring
2022-10-23 13:55     ` [cocci] algebraic simplification possibilities with SmPL Markus Elfring
2022-10-23 19:39       ` Jason A. Donenfeld
2022-10-23 19:45         ` Julia Lawall
2022-10-23 19:59           ` Jason A. Donenfeld
2022-10-23 20:09             ` Julia Lawall
2022-10-24 13:12         ` Markus Elfring
2022-10-24 16:14           ` Julia Lawall
2022-10-24 16:30         ` Markus Elfring
2022-10-24 16:39           ` Julia Lawall

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.