All of lore.kernel.org
 help / color / mirror / Atom feed
* [cocci] Creating function definitions from function pointers in a struct with coccinelle
@ 2025-02-10 11:08 Fonyuy-Asheri Caleb
  2025-02-10 11:16 ` Julia Lawall
  0 siblings, 1 reply; 11+ messages in thread
From: Fonyuy-Asheri Caleb @ 2025-02-10 11:08 UTC (permalink / raw)
  To: cocci

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

Hello, 

I am new to coccinelle and am interested in creating a patch which can define functions based on the function pointers 
available in a struct declaration. Since the names of the pointers don't exactly match the 
function definitions I want, I used a python script to modify the names. Below is my script: 

@m_rule@ 
field list f_list; 
identifier I; 
@@ 
struct I { 
f_list 
}; 

@script:python p_rule@ 
nf << m_rule.f_list; 
n_list; 
@@ 
new_nf = [] 
for f in nf: 
parts = f.split("(") 
r_type = parts[0] 
if '*' in r_type: 
r_type=r_type.strip() 
else: 
r_type = r_type.lstrip() 
f_name = parts[1].split(")")[0].replace("*", "").strip() 
params = parts[2].split(")")[0] 
new_nf.append(r_type + "pv_" + f_name + "(" + params + ")") 
new_nf.append(r_type + "hvm_" + f_name + "(" + params + ")") 


Please how can I write a new rule that defines several lines of function definitions from 
the p_rule new_nf list available in my python structure? 

I tried adding the list in a plus(+) entry but that does not work. 


Thank you in advance for your help. 

Caleb 

[-- Attachment #2: Type: text/html, Size: 2018 bytes --]

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

* Re: [cocci] Creating function definitions from function pointers in a struct with coccinelle
  2025-02-10 11:08 [cocci] Creating function definitions from function pointers in a struct with coccinelle Fonyuy-Asheri Caleb
@ 2025-02-10 11:16 ` Julia Lawall
  2025-02-10 11:23   ` Fonyuy-Asheri Caleb
  0 siblings, 1 reply; 11+ messages in thread
From: Julia Lawall @ 2025-02-10 11:16 UTC (permalink / raw)
  To: Fonyuy-Asheri Caleb; +Cc: cocci

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



On Mon, 10 Feb 2025, Fonyuy-Asheri Caleb wrote:

> Hello,
>
> I am new to coccinelle and am interested in creating a patch which can define functions based on the function pointers 
> available in a struct declaration. Since the names of the pointers don't exactly match the 
> function definitions I want, I used a python script to modify the names. Below is my script: 
>
> @m_rule@
> field list f_list;
> identifier I;
> @@
> struct I {
>     f_list
> };

I'm not 100% sure if this work, but it could be nicer to match the
declaration of a function pointer, ie

struct I {
  ...
  T (*I1)(...);
  ...
}

Also, it is better to omit the ; at the end, because a structure type
could be declared in a variable declaration, where there is no ;
immediately after it.

>
> @script:python p_rule@
> nf << m_rule.f_list;
> n_list;
> @@
> new_nf = []
> for f in nf:
>     parts = f.split("(")
>     r_type = parts[0]
>     if '*' in r_type:
>         r_type=r_type.strip()
>     else:
>         r_type = r_type.lstrip()
>     f_name = parts[1].split(")")[0].replace("*", "").strip()
>     params = parts[2].split(")")[0]
>     new_nf.append(r_type + "pv_" + f_name + "(" + params + ")")
>     new_nf.append(r_type + "hvm_" + f_name + "(" + params + ")")
>
>
> Please how can I write a new rule that defines several lines of function definitions from 
> the p_rule new_nf list available in my python structure? 
>
> I tried adding the list in a plus(+) entry but that does not work. 

I'm not really sure what you are trying to do here.  Maybe if you could
give an example of source code an the results you want to achieve that
would be more understandable.

An alternative is not to bother with the type declarations, but instead to
see how some fields are used.  A function pointer field is a field that
has a function name stored in it.  So you could do:

@fname@
identifier fn;
@@

fn(...) { ... }

@@
type T;
T x;
identifier id;
identifier fname.fn;
@@

x.id = fn

This might be less complete than what you propose.

julia

>
>
> Thank you in advance for your help. 
>
> Caleb
>
>

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

* Re: [cocci] Creating function definitions from function pointers in a struct with coccinelle
  2025-02-10 11:16 ` Julia Lawall
@ 2025-02-10 11:23   ` Fonyuy-Asheri Caleb
  2025-02-10 11:32     ` Julia Lawall
  0 siblings, 1 reply; 11+ messages in thread
From: Fonyuy-Asheri Caleb @ 2025-02-10 11:23 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci

Thank you for your prompt response

> I'm not really sure what you are trying to do here.  Maybe if you could
> give an example of source code an the results you want to achieve that
> would be more understandable.
> 

I have structures defined as such in the code base but this is a problem for 
the work I want to do. 

struct save_ops {
    void (*start_copy)(void *data, const char *filename);
    void *(*copy_finished)(const char *filename);
    void (*check_copied)(void *data);
};

The aim of my patch is to define the following functions from the above struct

 
void hvm_start_copy(void *data, const char *filename);
void *hvm_copy_finished(const char *filename);
void hvm_check_copied(void *data);

void pv_start_copy(void *data, const char *filename);
void *pv_copy_finished(const char *filename);
void pv_check_copied(void *data);

The python m_rule I have permits me to match the list of function pointers
and the python script permits me to rename the pointers to the function names
as I have them here. Having at least the pv functions will be good for me. 

The issue now is to be able to write these to the resulting patch. 


Caleb

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

* Re: [cocci] Creating function definitions from function pointers in a struct with coccinelle
  2025-02-10 11:23   ` Fonyuy-Asheri Caleb
@ 2025-02-10 11:32     ` Julia Lawall
  2025-02-10 11:42       ` Fonyuy-Asheri Caleb
  0 siblings, 1 reply; 11+ messages in thread
From: Julia Lawall @ 2025-02-10 11:32 UTC (permalink / raw)
  To: Fonyuy-Asheri Caleb; +Cc: Julia Lawall, cocci



On Mon, 10 Feb 2025, Fonyuy-Asheri Caleb wrote:

> Thank you for your prompt response
>
> > I'm not really sure what you are trying to do here.  Maybe if you could
> > give an example of source code an the results you want to achieve that
> > would be more understandable.
> >
>
> I have structures defined as such in the code base but this is a problem for
> the work I want to do.
>
> struct save_ops {
>     void (*start_copy)(void *data, const char *filename);
>     void *(*copy_finished)(const char *filename);
>     void (*check_copied)(void *data);
> };
>
> The aim of my patch is to define the following functions from the above struct
>
>
> void hvm_start_copy(void *data, const char *filename);
> void *hvm_copy_finished(const char *filename);
> void hvm_check_copied(void *data);
>
> void pv_start_copy(void *data, const char *filename);
> void *pv_copy_finished(const char *filename);
> void pv_check_copied(void *data);
>
> The python m_rule I have permits me to match the list of function pointers
> and the python script permits me to rename the pointers to the function names
> as I have them here. Having at least the pv functions will be good for me.
>
> The issue now is to be able to write these to the resulting patch.

OK, maybe:

@@
identifier I, fn;
parameter list ps;
fresh identifier fnhvm = "hvm_" ## fn;
fresh identifier fnpv = "pv_" ## fn;
@@

struct I {
  ...
  T (*fn)(ps);
  ...
};
++ void fnhvm(ps);
++ void fnpv(ps);

If that doesn't work, maybe have one rule to add the hvm functions and
another to add the pv functions.  That would have the side benefit that
the hvm functions would come out together and the pv functions would come
out together.  Both groups would be attached right after the struct
declaration, so you would want to do the pv functions first and then the
hvm functions.

julia

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

* Re: [cocci] Creating function definitions from function pointers in a struct with coccinelle
  2025-02-10 11:32     ` Julia Lawall
@ 2025-02-10 11:42       ` Fonyuy-Asheri Caleb
  2025-02-10 11:56         ` Julia Lawall
  0 siblings, 1 reply; 11+ messages in thread
From: Fonyuy-Asheri Caleb @ 2025-02-10 11:42 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci

Thanks a lot Julia. 

It works now.


Caleb

----- Original Message -----
> From: "Julia Lawall" <julia.lawall@inria.fr>
> To: "Fonyuy-Asheri Caleb" <fonyuy-asheri.caleb@inria.fr>
> Cc: "Julia Lawall" <julia.lawall@inria.fr>, "cocci" <cocci@inria.fr>
> Sent: Monday, February 10, 2025 12:32:15 PM
> Subject: Re: [cocci] Creating function definitions from function pointers in a struct with coccinelle

> On Mon, 10 Feb 2025, Fonyuy-Asheri Caleb wrote:
> 
>> Thank you for your prompt response
>>
>> > I'm not really sure what you are trying to do here.  Maybe if you could
>> > give an example of source code an the results you want to achieve that
>> > would be more understandable.
>> >
>>
>> I have structures defined as such in the code base but this is a problem for
>> the work I want to do.
>>
>> struct save_ops {
>>     void (*start_copy)(void *data, const char *filename);
>>     void *(*copy_finished)(const char *filename);
>>     void (*check_copied)(void *data);
>> };
>>
>> The aim of my patch is to define the following functions from the above struct
>>
>>
>> void hvm_start_copy(void *data, const char *filename);
>> void *hvm_copy_finished(const char *filename);
>> void hvm_check_copied(void *data);
>>
>> void pv_start_copy(void *data, const char *filename);
>> void *pv_copy_finished(const char *filename);
>> void pv_check_copied(void *data);
>>
>> The python m_rule I have permits me to match the list of function pointers
>> and the python script permits me to rename the pointers to the function names
>> as I have them here. Having at least the pv functions will be good for me.
>>
>> The issue now is to be able to write these to the resulting patch.
> 
> OK, maybe:
> 
> @@
> identifier I, fn;
> parameter list ps;
> fresh identifier fnhvm = "hvm_" ## fn;
> fresh identifier fnpv = "pv_" ## fn;
> @@
> 
> struct I {
>  ...
>  T (*fn)(ps);
>  ...
> };
> ++ void fnhvm(ps);
> ++ void fnpv(ps);
> 
> If that doesn't work, maybe have one rule to add the hvm functions and
> another to add the pv functions.  That would have the side benefit that
> the hvm functions would come out together and the pv functions would come
> out together.  Both groups would be attached right after the struct
> declaration, so you would want to do the pv functions first and then the
> hvm functions.
> 
> julia

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

* Re: [cocci] Creating function definitions from function pointers in a struct with coccinelle
  2025-02-10 11:42       ` Fonyuy-Asheri Caleb
@ 2025-02-10 11:56         ` Julia Lawall
  2025-02-10 14:48           ` Fonyuy-Asheri Caleb
  0 siblings, 1 reply; 11+ messages in thread
From: Julia Lawall @ 2025-02-10 11:56 UTC (permalink / raw)
  To: Fonyuy-Asheri Caleb; +Cc: Julia Lawall, cocci



On Mon, 10 Feb 2025, Fonyuy-Asheri Caleb wrote:

> Thanks a lot Julia.
>
> It works now.

Excellent :)

julia

>
>
> Caleb
>
> ----- Original Message -----
> > From: "Julia Lawall" <julia.lawall@inria.fr>
> > To: "Fonyuy-Asheri Caleb" <fonyuy-asheri.caleb@inria.fr>
> > Cc: "Julia Lawall" <julia.lawall@inria.fr>, "cocci" <cocci@inria.fr>
> > Sent: Monday, February 10, 2025 12:32:15 PM
> > Subject: Re: [cocci] Creating function definitions from function pointers in a struct with coccinelle
>
> > On Mon, 10 Feb 2025, Fonyuy-Asheri Caleb wrote:
> >
> >> Thank you for your prompt response
> >>
> >> > I'm not really sure what you are trying to do here.  Maybe if you could
> >> > give an example of source code an the results you want to achieve that
> >> > would be more understandable.
> >> >
> >>
> >> I have structures defined as such in the code base but this is a problem for
> >> the work I want to do.
> >>
> >> struct save_ops {
> >>     void (*start_copy)(void *data, const char *filename);
> >>     void *(*copy_finished)(const char *filename);
> >>     void (*check_copied)(void *data);
> >> };
> >>
> >> The aim of my patch is to define the following functions from the above struct
> >>
> >>
> >> void hvm_start_copy(void *data, const char *filename);
> >> void *hvm_copy_finished(const char *filename);
> >> void hvm_check_copied(void *data);
> >>
> >> void pv_start_copy(void *data, const char *filename);
> >> void *pv_copy_finished(const char *filename);
> >> void pv_check_copied(void *data);
> >>
> >> The python m_rule I have permits me to match the list of function pointers
> >> and the python script permits me to rename the pointers to the function names
> >> as I have them here. Having at least the pv functions will be good for me.
> >>
> >> The issue now is to be able to write these to the resulting patch.
> >
> > OK, maybe:
> >
> > @@
> > identifier I, fn;
> > parameter list ps;
> > fresh identifier fnhvm = "hvm_" ## fn;
> > fresh identifier fnpv = "pv_" ## fn;
> > @@
> >
> > struct I {
> >  ...
> >  T (*fn)(ps);
> >  ...
> > };
> > ++ void fnhvm(ps);
> > ++ void fnpv(ps);
> >
> > If that doesn't work, maybe have one rule to add the hvm functions and
> > another to add the pv functions.  That would have the side benefit that
> > the hvm functions would come out together and the pv functions would come
> > out together.  Both groups would be attached right after the struct
> > declaration, so you would want to do the pv functions first and then the
> > hvm functions.
> >
> > julia
>

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

* Re: [cocci] Creating function definitions from function pointers in a struct with coccinelle
  2025-02-10 11:56         ` Julia Lawall
@ 2025-02-10 14:48           ` Fonyuy-Asheri Caleb
  2025-02-10 14:52             ` Julia Lawall
  0 siblings, 1 reply; 11+ messages in thread
From: Fonyuy-Asheri Caleb @ 2025-02-10 14:48 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci

Follow up question please: 

How can i match function pointer calls such as 

ctx->ops.my_function(int id, struct data_struct data);

or 

ctx->my_function(int id, struct data_struct data);

I can't seem to get this working. 


Caleb

----- Original Message -----
> From: "Julia Lawall" <julia.lawall@inria.fr>
> To: "Fonyuy-Asheri Caleb" <fonyuy-asheri.caleb@inria.fr>
> Cc: "Julia Lawall" <julia.lawall@inria.fr>, "cocci" <cocci@inria.fr>
> Sent: Monday, February 10, 2025 12:56:20 PM
> Subject: Re: [cocci] Creating function definitions from function pointers in a struct with coccinelle

> On Mon, 10 Feb 2025, Fonyuy-Asheri Caleb wrote:
> 
>> Thanks a lot Julia.
>>
>> It works now.
> 
> Excellent :)
> 
> julia
> 

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

* Re: [cocci] Creating function definitions from function pointers in a struct with coccinelle
  2025-02-10 14:48           ` Fonyuy-Asheri Caleb
@ 2025-02-10 14:52             ` Julia Lawall
  2025-02-10 14:54               ` Fonyuy-Asheri Caleb
  0 siblings, 1 reply; 11+ messages in thread
From: Julia Lawall @ 2025-02-10 14:52 UTC (permalink / raw)
  To: Fonyuy-Asheri Caleb; +Cc: cocci



On Mon, 10 Feb 2025, Fonyuy-Asheri Caleb wrote:

> Follow up question please:
>
> How can i match function pointer calls such as
>
> ctx->ops.my_function(int id, struct data_struct data);

When you call a function, you don't put the types on the arguments.  If
you want to specify the types, you can make metavariables of a certain
type like:

@@
int id;
struct data_struct data;
@@

julia
>
> or
>
> ctx->my_function(int id, struct data_struct data);
>
> I can't seem to get this working.
>
>
> Caleb
>
> ----- Original Message -----
> > From: "Julia Lawall" <julia.lawall@inria.fr>
> > To: "Fonyuy-Asheri Caleb" <fonyuy-asheri.caleb@inria.fr>
> > Cc: "Julia Lawall" <julia.lawall@inria.fr>, "cocci" <cocci@inria.fr>
> > Sent: Monday, February 10, 2025 12:56:20 PM
> > Subject: Re: [cocci] Creating function definitions from function pointers in a struct with coccinelle
>
> > On Mon, 10 Feb 2025, Fonyuy-Asheri Caleb wrote:
> >
> >> Thanks a lot Julia.
> >>
> >> It works now.
> >
> > Excellent :)
> >
> > julia
> >
>

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

* Re: [cocci] Creating function definitions from function pointers in a struct with coccinelle
  2025-02-10 14:52             ` Julia Lawall
@ 2025-02-10 14:54               ` Fonyuy-Asheri Caleb
  2025-02-10 15:00                 ` Julia Lawall
  2025-02-10 15:54                 ` Markus Elfring
  0 siblings, 2 replies; 11+ messages in thread
From: Fonyuy-Asheri Caleb @ 2025-02-10 14:54 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci

That was a mistake. Sorry about that. 

I actually meant this
ctx->ops.my_function(id, data);

ctx->my_function(id, data);

with the intention of picking up my_function and the parameter list. 

Caleb


> On Mon, 10 Feb 2025, Fonyuy-Asheri Caleb wrote:
> 
>> Follow up question please:
>>
>> How can i match function pointer calls such as
>>
>> ctx->ops.my_function(int id, struct data_struct data);
> 
> When you call a function, you don't put the types on the arguments.  If
> you want to specify the types, you can make metavariables of a certain
> type like:
> 
> @@
> int id;
> struct data_struct data;
> @@
> 
> julia
>>
>> or
>>
>> ctx->my_function(int id, struct data_struct data);
>>
>> I can't seem to get this working.
>>
>>
>> Caleb
>>
>> ----- Original Message -----
>> > From: "Julia Lawall" <julia.lawall@inria.fr>
>> > To: "Fonyuy-Asheri Caleb" <fonyuy-asheri.caleb@inria.fr>
>> > Cc: "Julia Lawall" <julia.lawall@inria.fr>, "cocci" <cocci@inria.fr>
>> > Sent: Monday, February 10, 2025 12:56:20 PM
>> > Subject: Re: [cocci] Creating function definitions from function pointers in a
>> > struct with coccinelle
>>
>> > On Mon, 10 Feb 2025, Fonyuy-Asheri Caleb wrote:
>> >
>> >> Thanks a lot Julia.
>> >>
>> >> It works now.
>> >
>> > Excellent :)
>> >
>> > julia
>> >

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

* Re: [cocci] Creating function definitions from function pointers in a struct with coccinelle
  2025-02-10 14:54               ` Fonyuy-Asheri Caleb
@ 2025-02-10 15:00                 ` Julia Lawall
  2025-02-10 15:54                 ` Markus Elfring
  1 sibling, 0 replies; 11+ messages in thread
From: Julia Lawall @ 2025-02-10 15:00 UTC (permalink / raw)
  To: Fonyuy-Asheri Caleb; +Cc: cocci



On Mon, 10 Feb 2025, Fonyuy-Asheri Caleb wrote:

> That was a mistake. Sorry about that.
>
> I actually meant this
> ctx->ops.my_function(id, data);
>
> ctx->my_function(id, data);
>
> with the intention of picking up my_function and the parameter list.

Sorry, it's not clear to me what you want.  If you want the complete list
of arguments and don't know how many there are, you may want to use:

expression list es;

julia

>
> Caleb
>
>
> > On Mon, 10 Feb 2025, Fonyuy-Asheri Caleb wrote:
> >
> >> Follow up question please:
> >>
> >> How can i match function pointer calls such as
> >>
> >> ctx->ops.my_function(int id, struct data_struct data);
> >
> > When you call a function, you don't put the types on the arguments.  If
> > you want to specify the types, you can make metavariables of a certain
> > type like:
> >
> > @@
> > int id;
> > struct data_struct data;
> > @@
> >
> > julia
> >>
> >> or
> >>
> >> ctx->my_function(int id, struct data_struct data);
> >>
> >> I can't seem to get this working.
> >>
> >>
> >> Caleb
> >>
> >> ----- Original Message -----
> >> > From: "Julia Lawall" <julia.lawall@inria.fr>
> >> > To: "Fonyuy-Asheri Caleb" <fonyuy-asheri.caleb@inria.fr>
> >> > Cc: "Julia Lawall" <julia.lawall@inria.fr>, "cocci" <cocci@inria.fr>
> >> > Sent: Monday, February 10, 2025 12:56:20 PM
> >> > Subject: Re: [cocci] Creating function definitions from function pointers in a
> >> > struct with coccinelle
> >>
> >> > On Mon, 10 Feb 2025, Fonyuy-Asheri Caleb wrote:
> >> >
> >> >> Thanks a lot Julia.
> >> >>
> >> >> It works now.
> >> >
> >> > Excellent :)
> >> >
> >> > julia
> >> >
>

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

* Re: [cocci] Creating function definitions from function pointers in a struct with coccinelle
  2025-02-10 14:54               ` Fonyuy-Asheri Caleb
  2025-02-10 15:00                 ` Julia Lawall
@ 2025-02-10 15:54                 ` Markus Elfring
  1 sibling, 0 replies; 11+ messages in thread
From: Markus Elfring @ 2025-02-10 15:54 UTC (permalink / raw)
  To: Fonyuy-Asheri Caleb; +Cc: cocci

> I actually meant this
> ctx->ops.my_function(id, data);
>
> ctx->my_function(id, data);
>
> with the intention of picking up my_function and the parameter list.

You can choose metavariables as needed for function calls.

Examples:
expression ctx;
expression list el;
identifier ops, my_function;
long id;
struct my_info * data;

Regards,
Markus

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

end of thread, other threads:[~2025-02-10 15:54 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-10 11:08 [cocci] Creating function definitions from function pointers in a struct with coccinelle Fonyuy-Asheri Caleb
2025-02-10 11:16 ` Julia Lawall
2025-02-10 11:23   ` Fonyuy-Asheri Caleb
2025-02-10 11:32     ` Julia Lawall
2025-02-10 11:42       ` Fonyuy-Asheri Caleb
2025-02-10 11:56         ` Julia Lawall
2025-02-10 14:48           ` Fonyuy-Asheri Caleb
2025-02-10 14:52             ` Julia Lawall
2025-02-10 14:54               ` Fonyuy-Asheri Caleb
2025-02-10 15:00                 ` Julia Lawall
2025-02-10 15:54                 ` Markus Elfring

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.