Discussion:
[Cocci] Matching function pointer typedef
Jerome Glisse
2018-05-15 20:36:07 UTC
Permalink
Hello,

I am trying to modify an function pointer typedef something like:

@@
@@
- typedef void (*toto_t)(int a, int b);
+ typedef void (*toto_t)(int a, int b, int c);

But it seems spatch or the semantic does not handle function pointer.
Or simply that typedef is not well handled in the first place. Thing
like:

@@
@@
- typedef int nombre;
+ typedef unsigned nombre;

also fails to work. But if typedef is use with struct then it works.
For instance:

@@
@@
- typedef struct {int a;} nombre;
+ typedef struct {unsigned a;} nombre;

Do work. Looking at declaration grammar i do not see why the former
does not work. I am using fedora 27 coccinelle 1.0.6 if that matters.

Is this a known limitation or am i writting it wrong ?

Thank you for any input on this,
Jérôme
Håkon Løvdal
2018-05-15 20:50:46 UTC
Permalink
It's been a while since I used coccinelle, but I think to remember that you must
(and in any case should) keep the non-changing parts outside of the
+/- lines, e.g.

@@
@@
typedef void (*toto_t)(int a, int b
+ , int c
);

etc

BR Håkon Løvdal
Post by Jerome Glisse
Hello,
@@
@@
- typedef void (*toto_t)(int a, int b);
+ typedef void (*toto_t)(int a, int b, int c);
But it seems spatch or the semantic does not handle function pointer.
Or simply that typedef is not well handled in the first place. Thing
@@
@@
- typedef int nombre;
+ typedef unsigned nombre;
also fails to work. But if typedef is use with struct then it works.
@@
@@
- typedef struct {int a;} nombre;
+ typedef struct {unsigned a;} nombre;
Do work. Looking at declaration grammar i do not see why the former
does not work. I am using fedora 27 coccinelle 1.0.6 if that matters.
Is this a known limitation or am i writting it wrong ?
Thank you for any input on this,
Jérôme
_______________________________________________
Cocci mailing list
https://systeme.lip6.fr/mailman/listinfo/cocci
Julia Lawall
2018-05-15 20:53:36 UTC
Permalink
Post by Håkon Løvdal
It's been a while since I used coccinelle, but I think to remember that you must
(and in any case should) keep the non-changing parts outside of the
+/- lines, e.g.
@@
@@
typedef void (*toto_t)(int a, int b
+ , int c
);
I think that typedefs of function pointers just don't work. It is looking
for typedef type name;. I can try to fix this.

julia
Post by Håkon Løvdal
etc
BR Håkon LÞvdal
Post by Jerome Glisse
Hello,
@@
@@
- typedef void (*toto_t)(int a, int b);
+ typedef void (*toto_t)(int a, int b, int c);
But it seems spatch or the semantic does not handle function pointer.
Or simply that typedef is not well handled in the first place. Thing
@@
@@
- typedef int nombre;
+ typedef unsigned nombre;
also fails to work. But if typedef is use with struct then it works.
@@
@@
- typedef struct {int a;} nombre;
+ typedef struct {unsigned a;} nombre;
Do work. Looking at declaration grammar i do not see why the former
does not work. I am using fedora 27 coccinelle 1.0.6 if that matters.
Is this a known limitation or am i writting it wrong ?
Thank you for any input on this,
JérÎme
_______________________________________________
Cocci mailing list
https://systeme.lip6.fr/mailman/listinfo/cocci
_______________________________________________
Cocci mailing list
https://systeme.lip6.fr/mailman/listinfo/cocci
Jerome Glisse
2018-05-15 21:26:59 UTC
Permalink
Post by Julia Lawall
Post by Håkon Løvdal
It's been a while since I used coccinelle, but I think to remember that you must
(and in any case should) keep the non-changing parts outside of the
+/- lines, e.g.
@@
@@
typedef void (*toto_t)(int a, int b
+ , int c
);
I think that typedefs of function pointers just don't work. It is looking
for typedef type name;. I can try to fix this.
Above does not work either. The error is same roughly spatch complains
that it matches whole content ... I have a workaround, namely abusing
gcc which accept:

typedef void toto_t(int a, int b);

For function pointer typedef and then coccinelle on function declaration
do work. Still it would be nice if coccinelle can understand function
pointer typedef.

Jérôme
Jerome Glisse
2018-05-16 15:13:46 UTC
Permalink
Post by Jerome Glisse
Post by Julia Lawall
Post by Håkon Løvdal
It's been a while since I used coccinelle, but I think to remember that you must
(and in any case should) keep the non-changing parts outside of the
+/- lines, e.g.
@@
@@
typedef void (*toto_t)(int a, int b
+ , int c
);
I think that typedefs of function pointers just don't work. It is looking
for typedef type name;. I can try to fix this.
Above does not work either. The error is same roughly spatch complains
that it matches whole content ... I have a workaround, namely abusing
typedef void toto_t(int a, int b);
For function pointer typedef and then coccinelle on function declaration
do work. Still it would be nice if coccinelle can understand function
pointer typedef.
Ok my work around does work that well, it seems gcc allow this abuse
only for function parameter type, it can not be use as a type inside
a structure and so it doesn't cover all cases i am dealing with.

Is there a way to modify code using python inside coccinelle ? I have
seen example that print thing inside coccinelle but i haven't not seen
an example that use coccinelle to match something and then do changes
to the code using python.

I am guessing i can open the file and use the position information to
do that but was wondering if there is already helper provided for that.

Cheers,
Jérôme
Julia Lawall
2018-05-16 18:35:45 UTC
Permalink
Post by Jerome Glisse
Post by Jerome Glisse
Post by Julia Lawall
Post by Håkon Løvdal
It's been a while since I used coccinelle, but I think to remember that you must
(and in any case should) keep the non-changing parts outside of the
+/- lines, e.g.
@@
@@
typedef void (*toto_t)(int a, int b
+ , int c
);
I think that typedefs of function pointers just don't work. It is looking
for typedef type name;. I can try to fix this.
Above does not work either. The error is same roughly spatch complains
that it matches whole content ... I have a workaround, namely abusing
typedef void toto_t(int a, int b);
For function pointer typedef and then coccinelle on function declaration
do work. Still it would be nice if coccinelle can understand function
pointer typedef.
Ok my work around does work that well, it seems gcc allow this abuse
only for function parameter type, it can not be use as a type inside
a structure and so it doesn't cover all cases i am dealing with.
Is there a way to modify code using python inside coccinelle ? I have
seen example that print thing inside coccinelle but i haven't not seen
an example that use coccinelle to match something and then do changes
to the code using python.
I am guessing i can open the file and use the position information to
do that but was wondering if there is already helper provided for that.
No, python code doesn't have access to the ast, which is an ocaml
structure.

julia
Post by Jerome Glisse
Cheers,
Jérôme
Jerome Glisse
2018-05-17 19:48:52 UTC
Permalink
Post by Jerome Glisse
Hello,
@@
@@
- typedef void (*toto_t)(int a, int b);
+ typedef void (*toto_t)(int a, int b, int c);
This now works. Currently, you need to remove the whole typedef and add
it back, not just add the third argument as Håkon suggested.
Awesome ! :) Thank you for working on that.
Post by Jerome Glisse
But it seems spatch or the semantic does not handle function pointer.
Or simply that typedef is not well handled in the first place. Thing
@@
@@
- typedef int nombre;
+ typedef unsigned nombre;
Was this just an experiment, or is it something you need? I would imagine
that the problem is that "unsigned" as a type by itself is not well
supported.
This was just an experiment i did while trying to understand why my
function pointer typedef changes did not work. If i use long or any
basic ctype then it works.

Cheers,
Jérôme

Continue reading on narkive:
Loading...