Skip to contentSkip to conversation

Replacement of the form `x |= function()`

cocci Alyssa Rosenzweig 3 participants 10 messages Feb 25, 2025 – Mar 2, 2025

lore
Share & tools
lore
#0 lore raw

Hi, I've hit an awkward corner case combining operators-with-assignment with a function call in the replacement of a semantic patch.

These patches both work as expected (@@..@@ sections omitted):

    -lhs = rhs;
    +lhs |= rhs;

and

    -lhs = rhs;
    +lhs = myfunc(rhs);

However, putting it together...

    -lhs = rhs;
    +lhs |= myfunc(rhs);

...fails with a parse error

plus: parse error:
File "s.cocci", line 7, column 8, charpos = 59 around = 'myfunc',
whole content = +lhs |= myfunc(rhs);

Is this expected? Is this a Coccinelle bug, or an invalid semantic patch? Is there a correct way to specify this replacement?

---

As a workaround, this patch does work

    -lhs = rhs;
    +lhs |= (myfunc(rhs));

..but results in superfluous parantheses in the output... which I guess can be removed with

    -lhs = (rhs);
    +lhs = rhs;

...but this is surely not the right fix :-)

---

I use Coccinelle for collateral evolutions in Mesa3D's compiler stack. This particular case came up when working on
https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/33722 for the "real-world" motivation.

Thanks,

Alyssa

#1 lore raw
Quoted 35 lines · Alyssa Rosenzweig

Hi, I've hit an awkward corner case combining operators-with-assignment with a function call in the replacement of a semantic patch.

These patches both work as expected (@@..@@ sections omitted):

    -lhs = rhs;
    +lhs |= rhs;

and

    -lhs = rhs;
    +lhs = myfunc(rhs);

However, putting it together...

    -lhs = rhs;
    +lhs |= myfunc(rhs);

...fails with a parse error

plus: parse error:
File "s.cocci", line 7, column 8, charpos = 59 around = 'myfunc',
whole content = +lhs |= myfunc(rhs);

Is this expected? Is this a Coccinelle bug, or an invalid semantic patch? Is there a correct way to specify this replacement?

---

As a workaround, this patch does work

    -lhs = rhs;
    +lhs |= (myfunc(rhs));
Quoted 7 lines

..but results in superfluous parantheses in the output... which I guess can be removed with

    -lhs = (rhs);
    +lhs = rhs;

...but this is surely not the right fix :-)

What version of Coccinelle do you have? For me it works fine...

julia

#2 lore raw
Quoted 1 line

What version of Coccinelle do you have? For me it works fine...

1.1.1, apparently. I didn't realize Fedora 40 was so out-of-date, sorry!

[BTW, I'm a huge fan of Coccinelle. Thank you for writing this phenomenal piece of software!]

#3 lore raw
Quoted 6 lines · Alyssa Rosenzweig
Quoted 1 line

What version of Coccinelle do you have? For me it works fine...

1.1.1, apparently. I didn't realize Fedora 40 was so out-of-date, sorry!

[BTW, I'm a huge fan of Coccinelle. Thank you for writing this phenomenal piece of software!]

Thanks!

You can ty the version on github.

julia

#4 lore raw
In reply to Julia Lawall
Quoted 1 line

What version of Coccinelle do you have? For me it works fine...

Under which circumstances will a transformation approach (like the following) become better supported?

@adjustment@
expression lhs, rhs;
@@
lhs
-=
+|= myfunc(
rhs
+ )
;

Markus_Elfring@Sonne:…/Projekte/Coccinelle/Probe> spatch --version && spatch --parse-cocci suggestion1_for_Alyssa_Rosenzweig-20250301.cocci spatch version 1.3-00043-gcca22217d-dirty compiled with OCaml version 5.1.1
…
plus: parse error:
File "suggestion1_for_Alyssa_Rosenzweig-20250301.cocci", line 10, column 1, charpos = 227
around = '|=',
whole content = +|= myfunc(

Regards,
Markus

#5 lore raw

Probably you are missing a space after + ?
Sent from my iPhone

Quoted 31 lines · Markus Elfring



Quoted 2 lines

What version of Coccinelle do you have? For me it works fine...

Under which circumstances will a transformation approach (like the following) become better supported?

@adjustment@
expression lhs, rhs;
@@
lhs
-=
+|= myfunc(
rhs
+ )
;

Markus_Elfring@Sonne:…/Projekte/Coccinelle/Probe> spatch --version && spatch --parse-cocci suggestion1_for_Alyssa_Rosenzweig-20250301.cocci spatch version 1.3-00043-gcca22217d-dirty compiled with OCaml version 5.1.1
…
plus: parse error:
File "suggestion1_for_Alyssa_Rosenzweig-20250301.cocci", line 10, column 1, charpos = 227
around = '|=',
whole content = +|= myfunc(

Regards,
Markus

#6 lore raw
Quoted 1 line

Probably you are missing a space after + ?

Do you really insist on the usage of extra whitespace characters at the shown positions of SmPL code?

Regards,
Markus

#7 lore raw
In reply to Markus Elfring
Quoted 10 lines · Markus Elfring
Quoted 1 line

What version of Coccinelle do you have? For me it works fine...

Under which circumstances will a transformation approach (like the following) become better supported?

@adjustment@
expression lhs, rhs;
@@
lhs
-=

Actually, it is the above line that doesn't parse, because -= is an operator in C.

julia

Quoted 18 lines

+|= myfunc(
rhs
+ )
;

Markus_Elfring@Sonne:…/Projekte/Coccinelle/Probe> spatch --version && spatch --parse-cocci suggestion1_for_Alyssa_Rosenzweig-20250301.cocci spatch version 1.3-00043-gcca22217d-dirty compiled with OCaml version 5.1.1
…
plus: parse error:
File "suggestion1_for_Alyssa_Rosenzweig-20250301.cocci", line 10, column 1, charpos = 227
around = '|=',
whole content = +|= myfunc(

Regards,
Markus

#8 lore raw

Re: [cocci] Data processing challenges with “compound assignment operators”?

Quoted 8 lines
Quoted 5 lines

@adjustment@
expression lhs, rhs;
@@
lhs
-=

Actually, it is the above line that doesn't parse, because -= is an operator in C.

Does such an SmPL script example provide opportunities to resolve the handling of such a “compound assignment operator” in other ways?
https://en.cppreference.com/w/c/language/operator_assignment#Compound_assignment

May I expect that the minus character in the first text column gets usually a special meaning according to the semantic patch language?
https://gitlab.inria.fr/coccinelle/coccinelle/-/blob/1c46296c6aa9415c3e33388c83f8529ee7b049da/docs/manual/cocci_syntax.tex#L1028-1032

Regards,
Markus

#9 lore raw
In reply to Alyssa Rosenzweig

…

Quoted 6 lines

However, putting it together...

    -lhs = rhs;
    +lhs |= myfunc(rhs);

...fails with a parse error

…

Does another transformation approach (like the following) work better together also with the Coccinelle software versions which you tried out so far?

@adjustment@
expression lhs, rhs;
@@
lhs
- =
+ |= myfunc(
rhs
+ )
;

Regards,
Markus