public inbox for cocci@systeme.lip6.fr
 help / color / mirror / Atom feed
* [cocci] behavior change in semantic patches since c36b51ffc889 ("don't drop down on single statement")
@ 2025-03-07  0:43 Jacob Keller
  2025-03-07  0:55 ` Julia Lawall
  2025-03-07 10:47 ` [cocci] behavior change in semantic patches since c36b51ffc889 … Markus Elfring
  0 siblings, 2 replies; 14+ messages in thread
From: Jacob Keller @ 2025-03-07  0:43 UTC (permalink / raw)
  To: cocci, Julia Lawall; +Cc: Przemek Kitszel

Hi Julia,

We use semantic patches for code cleanup regularly, and recently noticed
a change in behavior some some semantic patches that are run as part of
a build process. I've provided the patch and an explanation of my
discoveries thus far. Hopefully help track down whats wrong and whether
we need to update these patches, there is some other workaround we can
do, or there is actually a bug in coccinelle/spatch.

I have the following semantic patch:

> @@
> void * void_ptr;
> type T;
> T * typed_ptr;
> @@
> - typed_ptr = (T *)void_ptr
> + typed_ptr = void_ptr

Its goal is to find and remove unnecessary type casts where a cast is on
a void pointer that to another type.

This is used for our build process as we have some code that is shared
between C and C++. The C++ compiler requires the casts to avoid issues,
but C warns about them.

With spatch 1.2.0, this patch works and finds and removes casts.

However, I updated to the master branch of the coccinelle project and
noticed that it no longer finds all of the casts. In particular, it
looks like casts which are part of type declarations don't always get
reliably caught.

I also noticed a behavior change in another patch:

> @@
> expression hw, ptr;
> @@
> (
> - ice_free(hw, ptr);
> + devm_kfree(ice_hw_to_dev(hw),ptr);
> )

This patch updates some code that used an internal wrapper function to
use the appropriate kernel function directly.

Since switching and testing on the master branch which looks like its
the 1.3.0 release, this now transforms code like this:

if (something)
  ice_free(...)

into

if (something) {
  devm_kfree(...)
}

When the function is inside a block like this, it is transformed to
include braces. This often violates our style guide as braces around
single line if or other scopes are not desired.

These changes break our ability to deploy the new version of coccinelle.

I ran a git bisect to determine when these changes broke, and both
changes appear to occur due to the following change:

c36b51ffc889 ("don't drop down on single statement")

I do not know why this commit causes such a behavior change, and am
reaching out for guidance and hope that a suitable workaround can be found.

I was able to partially work around the issue with the type cast:

I was using spatch --parse-cocci and saw some warnings about the patch
file, and refactored it to:

@disable drop_cast@
void * void_ptr;
type T;
T * typed_ptr;
@@
 typed_ptr =
-(T *)
 void_ptr


This fixed so that the patch does find all the typecasts, but has often
left us with undesirable white spacing, for example of there is code
like this:

struct something *a =
	(struct something *)data


Then before it translates to:

struct something *a = data

but now it translates to

struct something *a =
	data

The ability to automatically condense the lines was very nice, and
losing that is a bit annoying.

From the commit description, I doubt you can simply reverse this change.
I was hoping your expertise with semantic patches could help us identify
modified versions of the patches which will reliably work for us in both
the function transformation and cast removal patches.

Thanks,
Jake

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

end of thread, other threads:[~2025-03-18  9:11 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-07  0:43 [cocci] behavior change in semantic patches since c36b51ffc889 ("don't drop down on single statement") Jacob Keller
2025-03-07  0:55 ` Julia Lawall
2025-03-07 20:24   ` Jacob Keller
2025-03-07 10:47 ` [cocci] behavior change in semantic patches since c36b51ffc889 … Markus Elfring
2025-03-07 20:53   ` Jacob Keller
2025-03-07 22:47     ` Jacob Keller
2025-03-08  9:07       ` Markus Elfring
2025-03-10 18:34         ` Jacob Keller
2025-03-11  9:10           ` Markus Elfring
2025-03-08  8:39     ` Markus Elfring
2025-03-10 18:27       ` Jacob Keller
2025-03-11  9:26         ` Przemek Kitszel
2025-03-08  9:45     ` [cocci] Evolution of transformation processes? Markus Elfring
2025-03-10 18:23       ` Jacob Keller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox