Skip to contentSkip to conversation

Replacing cast in nested initializers

cocci Christian A. Ehrhardt 2 participants 2 messages Jan 23, 2025

lore
Share & tools
lore
#0 lore raw

Hi,

I am trying to replace casts in nested struct initialisers. My patch attempt looks like this:

@@
identifier __type;
identifier __name;
expression __data;
@@

| struct __type __name[...] = {
| 	...,
| 	{
| 		...,
| -		(del_t)
| +		(add_t)
| 		&__data
| 		,...
| 	}
| 	,...
| };

Running this on this .c file

| struct foo foos[] = {
| 	{ (del_t)&x1 },
| 	{ (del_t)0 },
| 	{ (del_t)&x2 }
| };

I would expect the first and the third cast (in front of "&x1" and "&x2) to be replaced.

However, only the first cast (in front of "&x1") is replaced and I get a warning the del_t might have to be meta variable. If I add

| typedef del_t;

to the patch only the last cast (in front of "&x2") is replaced.

What am I doing wrong?

Best regards,
Christian

#1 lore raw
Quoted 21 lines

I am trying to replace casts in nested struct initialisers. My patch attempt looks like this:

@@
identifier __type;
identifier __name;
expression __data;
@@

| struct __type __name[...] = {
| 	...,
| 	{
| 		...,
| -		(del_t)
| +		(add_t)
| 		&__data
| 		,...
| 	}
| 	,...
| };
Quoted 7 lines

Running this on this .c file

| struct foo foos[] = {
| 	{ (del_t)&x1 },
| 	{ (del_t)0 },
| 	{ (del_t)&x2 }
| };

I find your usage of pipe characters confusing here.

Quoted 11 lines

I would expect the first and the third cast (in front of "&x1" and "&x2) to be replaced.

However, only the first cast (in front of "&x1") is replaced and I get a warning the del_t might have to be meta variable. If I add

| typedef del_t;

to the patch only the last cast (in front of "&x2") is replaced.

What am I doing wrong?

I suggest to take another look at applications of SmPL dot variants.
https://gitlab.inria.fr/coccinelle/coccinelle/-/blob/8aa44ca0ff45be70f124802696ea58c75843e2a9/docs/manual/cocci_syntax.tex#L793

Will you get further development ideas from additional information for the semantic patch language like the following?

@casting@
identifier my_name, my_type;
expression data;
@@
struct my_type my_name[...]
=
{
...,
<+...
{
...,
(
-del_t
+add_t
)
&data
, ...
}
,
...+>
};

Markus_Elfring@Sonne:…/Projekte/Coccinelle/Probe> spatch --parse-cocci suggestion_for_Christian_Ehrhardt.cocci …
minus: parse error:
File "suggestion_for_Christian_Ehrhardt.cocci", line 14, column 1, charpos = 291
around = '{',
whole content = {

Regards,
Markus