* [cocci] match arbitrary argument position
@ 2022-08-16 8:58 Jakob Koschel
0 siblings, 0 replies; 12+ messages in thread
From: Jakob Koschel @ 2022-08-16 8:58 UTC (permalink / raw)
To: cocci; +Cc: julia.lawall, Jakob Koschel
Hi,
(resending be cause I accidently included a private email address in cc,
can the duplicate be removed/rejected from the mailing list archive?)
sorry if this question is not suiting here.
I went through the documentation and most of the examples I could find, but wasn't
able to easily answer my question I'm wondering about.
Basically: is there a way to match to *any* argument? E.g. make something match both of
the 'x' uses in the function calls here:
int x = 5;
function_call1(x, 12, 12);
function_call2(0, x);
Based on that I was wondering if there is a way to say: match if the expression is
within an expression list. So if I for instance have something like this:
@main@
type T;
parameter list P;
expression list E;
expression E1;
identifier func, func_call;
@@
T func(P@E) {
...
func_call(E1 in E)
...
}
(above is not valid syntax of course)
Ideally I would like to make and get the parameters of 'func' that are used in any position
as arguments in 'func_call' if that makes sense?
Any help is very much appreciated!
Thanks,
Jakob
^ permalink raw reply [flat|nested] 12+ messages in thread* [cocci] match arbitrary argument position
@ 2022-08-16 8:54 Jakob Koschel
2022-08-16 11:37 ` Julia Lawall
0 siblings, 1 reply; 12+ messages in thread
From: Jakob Koschel @ 2022-08-16 8:54 UTC (permalink / raw)
To: cocci, jkl820.misc; +Cc: julia.lawall, Jakob Koschel
Hi,
sorry if this question is not suiting here.
I went through the documentation and most of the examples I could find, but wasn't
able to easily answer my question I'm wondering about.
Basically: is there a way to match to *any* argument? E.g. make something match both of
the 'x' uses in the function calls here:
int x = 5;
function_call1(x, 12, 12);
function_call2(0, x);
Based on that I was wondering if there is a way to say: match if the expression is
within an expression list. So if I for instance have something like this:
@main@
type T;
parameter list P;
expression list E;
expression E1;
identifier func, func_call;
@@
T func(P@E) {
...
func_call(E1 in E)
...
}
(above is not valid syntax of course)
Ideally I would like to make and get the parameters of 'func' that are used in any position
as arguments in 'func_call' if that makes sense?
Any help is very much appreciated!
Thanks,
Jakob
^ permalink raw reply [flat|nested] 12+ messages in thread* Re: [cocci] match arbitrary argument position 2022-08-16 8:54 Jakob Koschel @ 2022-08-16 11:37 ` Julia Lawall 2022-08-16 12:35 ` Jakob Koschel 0 siblings, 1 reply; 12+ messages in thread From: Julia Lawall @ 2022-08-16 11:37 UTC (permalink / raw) To: Jakob Koschel; +Cc: cocci, jkl820.misc On Tue, 16 Aug 2022, Jakob Koschel wrote: > Hi, > > sorry if this question is not suiting here. > > I went through the documentation and most of the examples I could find, but wasn't > able to easily answer my question I'm wondering about. > > Basically: is there a way to match to *any* argument? E.g. make something match both of > the 'x' uses in the function calls here: > > int x = 5; > > function_call1(x, 12, 12); > > function_call2(0, x); f(...,x,...) or f(...,<+...x...+>,...) In the second case, x would be a subexpression of the considered argument. julia > > > > Based on that I was wondering if there is a way to say: match if the expression is > within an expression list. So if I for instance have something like this: > > @main@ > type T; > parameter list P; > expression list E; > expression E1; > identifier func, func_call; > @@ > > T func(P@E) { > ... > func_call(E1 in E) > ... > } > > (above is not valid syntax of course) > > Ideally I would like to make and get the parameters of 'func' that are used in any position > as arguments in 'func_call' if that makes sense? > > > Any help is very much appreciated! > > Thanks, > Jakob > ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [cocci] match arbitrary argument position 2022-08-16 11:37 ` Julia Lawall @ 2022-08-16 12:35 ` Jakob Koschel 2022-08-16 17:55 ` Markus Elfring 2022-08-16 21:07 ` Julia Lawall 0 siblings, 2 replies; 12+ messages in thread From: Jakob Koschel @ 2022-08-16 12:35 UTC (permalink / raw) To: Julia Lawall; +Cc: cocci > On 16. Aug 2022, at 13:37, Julia Lawall <Julia.Lawall@inria.fr> wrote: > > > > On Tue, 16 Aug 2022, Jakob Koschel wrote: > >> Hi, >> >> sorry if this question is not suiting here. >> >> I went through the documentation and most of the examples I could find, but wasn't >> able to easily answer my question I'm wondering about. >> >> Basically: is there a way to match to *any* argument? E.g. make something match both of >> the 'x' uses in the function calls here: >> >> int x = 5; >> >> function_call1(x, 12, 12); >> >> function_call2(0, x); > > f(...,x,...) > > or > > f(...,<+...x...+>,...) > > In the second case, x would be a subexpression of the considered argument. > > julia > Thanks! I really thought I tried that earlier but now it works. > >> >> >> >> Based on that I was wondering if there is a way to say: match if the expression is >> within an expression list. So if I for instance have something like this: >> >> @main@ >> type T; >> parameter list P; >> expression list E; >> expression E1; >> identifier func, func_call; >> @@ >> >> T func(P@E) { >> ... >> func_call(E1 in E) >> ... >> } >> >> (above is not valid syntax of course) >> >> Ideally I would like to make and get the parameters of 'func' that are used in any position >> as arguments in 'func_call' if that makes sense? Do you have any idea regarding the expression lists? looking at the following code snippet: test.c: int func1(int argc, char *argv) { func2(argc, argv); func3(argc, argv); func3(argv); } what works is (only matches the first func3 call): @main4@ expression list Es; @@ func2(Es) @main5@ expression list main4.Es; identifier func; @@ func(Es); + // add comment what doesn't work (matching on the expression list from the parameter list): @main4@ expression list Es; @@ int func1(Ps@Es) { ... } @main5@ expression list main4.Es; identifier func; @@ func(Es); + // add comment Ideally I would like to 'find' all parameters used in a function call, I also tried this: @main4@ parameter P; expression E; @@ int func1(..., P@E, ...) { ... } @main5@ expression main4.E; identifier func; @@ func(..., E, ...); + // add comment But it throws: 'rule starting on line 41 contains unattached metavariables: main4.E'. Any idea, why e.g. the second one doesn't work? If I print it with python the expression list looks the same as for the first one. Also in the end this boils down to: is it possible to match to any of the expression list elements? Thanks, Jakob >> >> >> Any help is very much appreciated! >> >> Thanks, >> Jakob ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [cocci] match arbitrary argument position 2022-08-16 12:35 ` Jakob Koschel @ 2022-08-16 17:55 ` Markus Elfring 2022-08-17 14:26 ` Jakob Koschel 2022-08-16 21:07 ` Julia Lawall 1 sibling, 1 reply; 12+ messages in thread From: Markus Elfring @ 2022-08-16 17:55 UTC (permalink / raw) To: Jakob Koschel, cocci > looking at the following code snippet: > > test.c: … > func3(argc, argv); > > func3(argv); Would you like to handle function overloading here? > what doesn't work (matching on the expression list from the parameter list): > > @main4@ > expression list Es; > @@ > > int func1(Ps@Es) { > ... > } Which type would belong to the metavariable “Ps”? > I also tried this: > > @main4@ > parameter P; > expression E; > @@ > > int func1(..., P@E, ...) { > ... > } Does such SmPL code represent a contradiction? Can any other descriptions indicate your data processing imaginations better? > @main5@ > expression main4.E; > identifier func; > @@ > > func(..., E, ...); > + // add comment > > > But it throws: 'rule starting on line 41 contains unattached metavariables: main4.E'. Which software version did you try out here? I am curious how corresponding considerations will evolve further. Regards, Markus ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [cocci] match arbitrary argument position 2022-08-16 17:55 ` Markus Elfring @ 2022-08-17 14:26 ` Jakob Koschel 2022-08-17 19:21 ` Markus Elfring 0 siblings, 1 reply; 12+ messages in thread From: Jakob Koschel @ 2022-08-17 14:26 UTC (permalink / raw) To: Markus Elfring; +Cc: cocci Hey, > On 16. Aug 2022, at 19:55, Markus Elfring <Markus.Elfring@web.de> wrote: > >> looking at the following code snippet: >> >> test.c: > … >> func3(argc, argv); >> >> func3(argv); > > > Would you like to handle function overloading here? I'm not exactly sure I understand what you mean here. Finally I would like to archive something like this: void func(void) { int x = 0; int y = 0; func1(x, 0); func2(0, y); } void func1(int arg1, int arg2) { interesting_func(arg1); } void func2(int arg1, int arg2) { interesting_func(arg2); } In the end I want to, for example, turn it into: void func(void) { int x = 0; // 'x' was used as first argument to interesting_func int y = 0; // 'y' was used as first argument to interesting_func func1(x, 0); func2(0, y); } void func1(int arg1, int arg2) { interesting_func(arg1); } void func2(int arg1, int arg2) { interesting_func(arg2); } Basically I want to flag create the matching from 'x'/'y' to the argument to interesting_func. I was imagining if I could match the parameter list of any function calling 'interesting_func' then I can also create a matching from x to that function call and do the rest of the correlation in python somehow. However I'm still failing to detect if any argument (arg1 or arg2 in this case) is used as any argument in 'interesting_func'. I hope this makes more sense? > > > >> what doesn't work (matching on the expression list from the parameter list): >> >> @main4@ >> expression list Es; >> @@ >> >> int func1(Ps@Es) { >> ... >> } > > > Which type would belong to the metavariable “Ps”? Ps should be an arbitrary length parameter list with arbitrary types. > > > >> I also tried this: >> >> @main4@ >> parameter P; >> expression E; >> @@ >> >> int func1(..., P@E, ...) { >> ... >> } > > > Does such SmPL code represent a contradiction? > > > Can any other descriptions indicate your data processing imaginations better? hopefully the example above makes it a bit more clear. > > >> @main5@ >> expression main4.E; >> identifier func; >> @@ >> >> func(..., E, ...); >> + // add comment >> >> >> But it throws: 'rule starting on line 41 contains unattached metavariables: main4.E'. > > Which software version did you try out here? I'm guessing this will give you all the information you want: spatch version 1.1.1-00124-ge93f5513 compiled with OCaml version 4.13.1 > > I am curious how corresponding considerations will evolve further. > > > Regards, > Markus > Thanks! Jakob ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [cocci] match arbitrary argument position 2022-08-17 14:26 ` Jakob Koschel @ 2022-08-17 19:21 ` Markus Elfring 0 siblings, 0 replies; 12+ messages in thread From: Markus Elfring @ 2022-08-17 19:21 UTC (permalink / raw) To: Jakob Koschel; +Cc: cocci >>> looking at the following code snippet: >>> >>> test.c: >> … >>> func3(argc, argv); >>> >>> func3(argv); >> >> Would you like to handle function overloading here? > I'm not exactly sure I understand what you mean here. I suggest to take another look at the implementation detail that it was specified to call a function with different number of expressions. > Finally I would like to archive something like this: You would probably like to achieve special source code transformations instead of archiving items. > In the end I want to, for example, turn it into: > > void func(void) { > > int x = 0; > // 'x' was used as first argument to interesting_func > int y = 0; > // 'y' was used as first argument to interesting_func > > func1(x, 0); > func2(0, y); > } > > void func1(int arg1, int arg2) { > interesting_func(arg1); > } > > void func2(int arg1, int arg2) { > interesting_func(arg2); > } Will any filters (or related SmPL constraints) be relevant for such function names? > Basically I want to flag create the matching from 'x'/'y' to the argument to interesting_func. How do you think about further possibilities to choose which function argument should be passed as an expression for corresponding computations? > I was imagining if I could match the parameter list of any function calling 'interesting_func' > then I can also create a matching from x to that function call and do the rest of the correlation > in python somehow. I propose to reconsider also the need for extra scripting according to the discussed use case. > However I'm still failing to detect if any argument (arg1 or arg2 in this case) is used as > any argument in 'interesting_func'. Multiple SmPL rules can be applied on demand for desirable data processing results. > I hope this makes more sense? It can be challenging to express special imaginations by the means of the semantic patch language. > hopefully the example above makes it a bit more clear. It seems so. Regards, Markus ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [cocci] match arbitrary argument position 2022-08-16 12:35 ` Jakob Koschel 2022-08-16 17:55 ` Markus Elfring @ 2022-08-16 21:07 ` Julia Lawall 2022-08-17 14:18 ` Jakob Koschel 1 sibling, 1 reply; 12+ messages in thread From: Julia Lawall @ 2022-08-16 21:07 UTC (permalink / raw) To: Jakob Koschel; +Cc: cocci > >> Based on that I was wondering if there is a way to say: match if the expression is > >> within an expression list. So if I for instance have something like this: > >> > >> @main@ > >> type T; > >> parameter list P; > >> expression list E; > >> expression E1; > >> identifier func, func_call; > >> @@ > >> > >> T func(P@E) { > >> ... > >> func_call(E1 in E) > >> ... > >> } > >> > >> (above is not valid syntax of course) > >> > >> Ideally I would like to make and get the parameters of 'func' that are used in any position > >> as arguments in 'func_call' if that makes sense? > > Do you have any idea regarding the expression lists? > > looking at the following code snippet: > > test.c: > > int func1(int argc, char *argv) { > func2(argc, argv); > > func3(argc, argv); > > func3(argv); > } > > what works is (only matches the first func3 call): > > @main4@ > expression list Es; > @@ > > func2(Es) > > @main5@ > expression list main4.Es; > identifier func; > @@ > > func(Es); > + // add comment > > > what doesn't work (matching on the expression list from the parameter list): > > @main4@ > expression list Es; > @@ > > int func1(Ps@Es) { > ... > } > > @main5@ > expression list main4.Es; Could you try expression list main4.Ps; It may transform the parameters into a list of expressions. julia > identifier func; > @@ > > func(Es); > + // add comment > > > Ideally I would like to 'find' all parameters used in a function call, > I also tried this: > > @main4@ > parameter P; > expression E; > @@ > > int func1(..., P@E, ...) { > ... > } > > @main5@ > expression main4.E; > identifier func; > @@ > > func(..., E, ...); > + // add comment > > > But it throws: 'rule starting on line 41 contains unattached metavariables: main4.E'. > > Any idea, why e.g. the second one doesn't work? If I print it with python the expression list > looks the same as for the first one. > Also in the end this boils down to: is it possible to match to any of the expression list elements? > > Thanks, > Jakob > > >> > >> > >> Any help is very much appreciated! > >> > >> Thanks, > >> Jakob > > ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [cocci] match arbitrary argument position 2022-08-16 21:07 ` Julia Lawall @ 2022-08-17 14:18 ` Jakob Koschel 2022-08-17 14:36 ` Julia Lawall 0 siblings, 1 reply; 12+ messages in thread From: Jakob Koschel @ 2022-08-17 14:18 UTC (permalink / raw) To: Julia Lawall; +Cc: cocci > On 16. Aug 2022, at 23:07, Julia Lawall <Julia.Lawall@inria.fr> wrote: > >>>> Based on that I was wondering if there is a way to say: match if the expression is >>>> within an expression list. So if I for instance have something like this: >>>> >>>> @main@ >>>> type T; >>>> parameter list P; >>>> expression list E; >>>> expression E1; >>>> identifier func, func_call; >>>> @@ >>>> >>>> T func(P@E) { >>>> ... >>>> func_call(E1 in E) >>>> ... >>>> } >>>> >>>> (above is not valid syntax of course) >>>> >>>> Ideally I would like to make and get the parameters of 'func' that are used in any position >>>> as arguments in 'func_call' if that makes sense? >> >> Do you have any idea regarding the expression lists? >> >> looking at the following code snippet: >> >> test.c: >> >> int func1(int argc, char *argv) { >> func2(argc, argv); >> >> func3(argc, argv); >> >> func3(argv); >> } >> >> what works is (only matches the first func3 call): >> >> @main4@ >> expression list Es; >> @@ >> >> func2(Es) >> >> @main5@ >> expression list main4.Es; >> identifier func; >> @@ >> >> func(Es); >> + // add comment >> >> >> what doesn't work (matching on the expression list from the parameter list): >> >> @main4@ >> expression list Es; >> @@ >> >> int func1(Ps@Es) { >> ... >> } >> >> @main5@ >> expression list main4.Es; > > Could you try > > expression list main4.Ps; > > It may transform the parameters into a list of expressions. I just tried: @main4@ parameter list Ps; @@ int func1(Ps) { ... } @main5@ expression list main4.Ps; identifier func; @@ func(Es); + // add comment but that throws: meta: semantic error: incompatible inheritance declaration Ps File "test.cocci", line 45, column 24, charpos = 539 around = ';', whole content = expression list main4.Ps; > > julia > >> identifier func; >> @@ >> >> func(Es); >> + // add comment >> >> >> Ideally I would like to 'find' all parameters used in a function call, >> I also tried this: >> >> @main4@ >> parameter P; >> expression E; >> @@ >> >> int func1(..., P@E, ...) { >> ... >> } >> >> @main5@ >> expression main4.E; >> identifier func; >> @@ >> >> func(..., E, ...); >> + // add comment >> >> >> But it throws: 'rule starting on line 41 contains unattached metavariables: main4.E'. >> >> Any idea, why e.g. the second one doesn't work? If I print it with python the expression list >> looks the same as for the first one. >> Also in the end this boils down to: is it possible to match to any of the expression list elements? >> >> Thanks, >> Jakob >> >>>> >>>> >>>> Any help is very much appreciated! >>>> >>>> Thanks, >>>> Jakob ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [cocci] match arbitrary argument position 2022-08-17 14:18 ` Jakob Koschel @ 2022-08-17 14:36 ` Julia Lawall 2022-08-17 14:50 ` Jakob Koschel 0 siblings, 1 reply; 12+ messages in thread From: Julia Lawall @ 2022-08-17 14:36 UTC (permalink / raw) To: Jakob Koschel; +Cc: cocci On Wed, 17 Aug 2022, Jakob Koschel wrote: > > > > On 16. Aug 2022, at 23:07, Julia Lawall <Julia.Lawall@inria.fr> wrote: > > > >>>> Based on that I was wondering if there is a way to say: match if the expression is > >>>> within an expression list. So if I for instance have something like this: > >>>> > >>>> @main@ > >>>> type T; > >>>> parameter list P; > >>>> expression list E; > >>>> expression E1; > >>>> identifier func, func_call; > >>>> @@ > >>>> > >>>> T func(P@E) { > >>>> ... > >>>> func_call(E1 in E) > >>>> ... > >>>> } > >>>> > >>>> (above is not valid syntax of course) > >>>> > >>>> Ideally I would like to make and get the parameters of 'func' that are used in any position > >>>> as arguments in 'func_call' if that makes sense? > >> > >> Do you have any idea regarding the expression lists? > >> > >> looking at the following code snippet: > >> > >> test.c: > >> > >> int func1(int argc, char *argv) { > >> func2(argc, argv); > >> > >> func3(argc, argv); > >> > >> func3(argv); > >> } > >> > >> what works is (only matches the first func3 call): > >> > >> @main4@ > >> expression list Es; > >> @@ > >> > >> func2(Es) > >> > >> @main5@ > >> expression list main4.Es; > >> identifier func; > >> @@ > >> > >> func(Es); > >> + // add comment > >> > >> > >> what doesn't work (matching on the expression list from the parameter list): > >> > >> @main4@ > >> expression list Es; > >> @@ > >> > >> int func1(Ps@Es) { > >> ... > >> } > >> > >> @main5@ > >> expression list main4.Es; > > > > Could you try > > > > expression list main4.Ps; > > > > It may transform the parameters into a list of expressions. > > I just tried: > > @main4@ > parameter list Ps; > @@ > > int func1(Ps) { > ... > } > > @main5@ > expression list main4.Ps; > identifier func; > @@ > > func(Es); > + // add comment > > but that throws: > > meta: semantic error: incompatible inheritance declaration Ps > File "test.cocci", line 45, column 24, charpos = 539 > around = ';', > whole content = expression list main4.Ps; OK, there is a specific context in which this works, but it is only for generating semantic patch rules. You can do something like @r@ parameter list [n] ps; type t; identifier i,f; @@ f(ps,t i, ...) { ... } @@ expression list[r.n] es; identifier r.i, r.f; expression e; @@ ( f(es,i,...) | f(es, - e + 0 ,...) ) I'm not sure if that is exactly what you want to do, but perhaps it will give some ideas. julia > > > > > julia > > > >> identifier func; > >> @@ > >> > >> func(Es); > >> + // add comment > >> > >> > >> Ideally I would like to 'find' all parameters used in a function call, > >> I also tried this: > >> > >> @main4@ > >> parameter P; > >> expression E; > >> @@ > >> > >> int func1(..., P@E, ...) { > >> ... > >> } > >> > >> @main5@ > >> expression main4.E; > >> identifier func; > >> @@ > >> > >> func(..., E, ...); > >> + // add comment > >> > >> > >> But it throws: 'rule starting on line 41 contains unattached metavariables: main4.E'. > >> > >> Any idea, why e.g. the second one doesn't work? If I print it with python the expression list > >> looks the same as for the first one. > >> Also in the end this boils down to: is it possible to match to any of the expression list elements? > >> > >> Thanks, > >> Jakob > >> > >>>> > >>>> > >>>> Any help is very much appreciated! > >>>> > >>>> Thanks, > >>>> Jakob > > ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [cocci] match arbitrary argument position 2022-08-17 14:36 ` Julia Lawall @ 2022-08-17 14:50 ` Jakob Koschel 2022-08-17 15:26 ` Julia Lawall 0 siblings, 1 reply; 12+ messages in thread From: Jakob Koschel @ 2022-08-17 14:50 UTC (permalink / raw) To: Julia Lawall; +Cc: cocci > On 17. Aug 2022, at 16:36, Julia Lawall <julia.lawall@inria.fr> wrote: > > > > On Wed, 17 Aug 2022, Jakob Koschel wrote: > >> >> >>> On 16. Aug 2022, at 23:07, Julia Lawall <Julia.Lawall@inria.fr> wrote: >>> >>>>>> Based on that I was wondering if there is a way to say: match if the expression is >>>>>> within an expression list. So if I for instance have something like this: >>>>>> >>>>>> @main@ >>>>>> type T; >>>>>> parameter list P; >>>>>> expression list E; >>>>>> expression E1; >>>>>> identifier func, func_call; >>>>>> @@ >>>>>> >>>>>> T func(P@E) { >>>>>> ... >>>>>> func_call(E1 in E) >>>>>> ... >>>>>> } >>>>>> >>>>>> (above is not valid syntax of course) >>>>>> >>>>>> Ideally I would like to make and get the parameters of 'func' that are used in any position >>>>>> as arguments in 'func_call' if that makes sense? >>>> >>>> Do you have any idea regarding the expression lists? >>>> >>>> looking at the following code snippet: >>>> >>>> test.c: >>>> >>>> int func1(int argc, char *argv) { >>>> func2(argc, argv); >>>> >>>> func3(argc, argv); >>>> >>>> func3(argv); >>>> } >>>> >>>> what works is (only matches the first func3 call): >>>> >>>> @main4@ >>>> expression list Es; >>>> @@ >>>> >>>> func2(Es) >>>> >>>> @main5@ >>>> expression list main4.Es; >>>> identifier func; >>>> @@ >>>> >>>> func(Es); >>>> + // add comment >>>> >>>> >>>> what doesn't work (matching on the expression list from the parameter list): >>>> >>>> @main4@ >>>> expression list Es; >>>> @@ >>>> >>>> int func1(Ps@Es) { >>>> ... >>>> } >>>> >>>> @main5@ >>>> expression list main4.Es; >>> >>> Could you try >>> >>> expression list main4.Ps; >>> >>> It may transform the parameters into a list of expressions. >> >> I just tried: >> >> @main4@ >> parameter list Ps; >> @@ >> >> int func1(Ps) { >> ... >> } >> >> @main5@ >> expression list main4.Ps; >> identifier func; >> @@ >> >> func(Es); >> + // add comment >> >> but that throws: >> >> meta: semantic error: incompatible inheritance declaration Ps >> File "test.cocci", line 45, column 24, charpos = 539 >> around = ';', >> whole content = expression list main4.Ps; > > OK, there is a specific context in which this works, but it is only for > generating semantic patch rules. > > You can do something like > > @r@ > parameter list [n] ps; > type t; > identifier i,f; > @@ > > f(ps,t i, ...) { ... } > > @@ > expression list[r.n] es; > identifier r.i, r.f; > expression e; > @@ > > ( > f(es,i,...) > | > f(es, > - e > + 0 > ,...) > ) > > I'm not sure if that is exactly what you want to do, but perhaps it will > give some ideas. I just realized that you are no longer in the 'cc' in the email I send to Markus so I'm pasting what I'm trying to do here: Finally I would like to archive something like this: void func(void) { int x = 0; int y = 0; func1(x, 0); func2(0, y); } void func1(int arg1, int arg2) { interesting_func(arg1); } void func2(int arg1, int arg2) { interesting_func(arg2); } In the end I want to, for example, turn it into: void func(void) { int x = 0; // 'x' was used as first argument to interesting_func int y = 0; // 'y' was used as first argument to interesting_func func1(x, 0); func2(0, y); } void func1(int arg1, int arg2) { interesting_func(arg1); } void func2(int arg1, int arg2) { interesting_func(arg2); } Basically I want to flag create the matching from 'x'/'y' to the argument to interesting_func. I was imagining if I could match the parameter list of any function calling 'interesting_func' then I can also create a matching from x to that function call and do the rest of the correlation in python somehow. However I'm still failing to detect if any argument (arg1 or arg2 in this case) is used as any argument in 'interesting_func'. I hope this makes more sense? I think I understand your example, and you are basically matching on the length of 'n' on the parameter list. I'm not exactly sure if that would work with what I'm trying to accomplish. The more problematic case I *think* is matching the parameter list to the usage within that function. In your example it is matching from a function definition to a call to that function. so not matching: f(x, y); to void f(int arg1, int arg2) { ... } but matching the parameter to a function call within: void f(int arg1, int arg2) { insteresting_func(arg1); // match arg1 here } Thanks, Jakob > > julia > >> >>> >>> julia >>> >>>> identifier func; >>>> @@ >>>> >>>> func(Es); >>>> + // add comment >>>> >>>> >>>> Ideally I would like to 'find' all parameters used in a function call, >>>> I also tried this: >>>> >>>> @main4@ >>>> parameter P; >>>> expression E; >>>> @@ >>>> >>>> int func1(..., P@E, ...) { >>>> ... >>>> } >>>> >>>> @main5@ >>>> expression main4.E; >>>> identifier func; >>>> @@ >>>> >>>> func(..., E, ...); >>>> + // add comment >>>> >>>> >>>> But it throws: 'rule starting on line 41 contains unattached metavariables: main4.E'. >>>> >>>> Any idea, why e.g. the second one doesn't work? If I print it with python the expression list >>>> looks the same as for the first one. >>>> Also in the end this boils down to: is it possible to match to any of the expression list elements? >>>> >>>> Thanks, >>>> Jakob >>>> >>>>>> >>>>>> >>>>>> Any help is very much appreciated! >>>>>> >>>>>> Thanks, >>>>>> Jakob ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [cocci] match arbitrary argument position 2022-08-17 14:50 ` Jakob Koschel @ 2022-08-17 15:26 ` Julia Lawall 0 siblings, 0 replies; 12+ messages in thread From: Julia Lawall @ 2022-08-17 15:26 UTC (permalink / raw) To: Jakob Koschel; +Cc: cocci On Wed, 17 Aug 2022, Jakob Koschel wrote: > > > > On 17. Aug 2022, at 16:36, Julia Lawall <julia.lawall@inria.fr> wrote: > > > > > > > > On Wed, 17 Aug 2022, Jakob Koschel wrote: > > > >> > >> > >>> On 16. Aug 2022, at 23:07, Julia Lawall <Julia.Lawall@inria.fr> wrote: > >>> > >>>>>> Based on that I was wondering if there is a way to say: match if the expression is > >>>>>> within an expression list. So if I for instance have something like this: > >>>>>> > >>>>>> @main@ > >>>>>> type T; > >>>>>> parameter list P; > >>>>>> expression list E; > >>>>>> expression E1; > >>>>>> identifier func, func_call; > >>>>>> @@ > >>>>>> > >>>>>> T func(P@E) { > >>>>>> ... > >>>>>> func_call(E1 in E) > >>>>>> ... > >>>>>> } > >>>>>> > >>>>>> (above is not valid syntax of course) > >>>>>> > >>>>>> Ideally I would like to make and get the parameters of 'func' that are used in any position > >>>>>> as arguments in 'func_call' if that makes sense? > >>>> > >>>> Do you have any idea regarding the expression lists? > >>>> > >>>> looking at the following code snippet: > >>>> > >>>> test.c: > >>>> > >>>> int func1(int argc, char *argv) { > >>>> func2(argc, argv); > >>>> > >>>> func3(argc, argv); > >>>> > >>>> func3(argv); > >>>> } > >>>> > >>>> what works is (only matches the first func3 call): > >>>> > >>>> @main4@ > >>>> expression list Es; > >>>> @@ > >>>> > >>>> func2(Es) > >>>> > >>>> @main5@ > >>>> expression list main4.Es; > >>>> identifier func; > >>>> @@ > >>>> > >>>> func(Es); > >>>> + // add comment > >>>> > >>>> > >>>> what doesn't work (matching on the expression list from the parameter list): > >>>> > >>>> @main4@ > >>>> expression list Es; > >>>> @@ > >>>> > >>>> int func1(Ps@Es) { > >>>> ... > >>>> } > >>>> > >>>> @main5@ > >>>> expression list main4.Es; > >>> > >>> Could you try > >>> > >>> expression list main4.Ps; > >>> > >>> It may transform the parameters into a list of expressions. > >> > >> I just tried: > >> > >> @main4@ > >> parameter list Ps; > >> @@ > >> > >> int func1(Ps) { > >> ... > >> } > >> > >> @main5@ > >> expression list main4.Ps; > >> identifier func; > >> @@ > >> > >> func(Es); > >> + // add comment > >> > >> but that throws: > >> > >> meta: semantic error: incompatible inheritance declaration Ps > >> File "test.cocci", line 45, column 24, charpos = 539 > >> around = ';', > >> whole content = expression list main4.Ps; > > > > OK, there is a specific context in which this works, but it is only for > > generating semantic patch rules. > > > > You can do something like > > > > @r@ > > parameter list [n] ps; > > type t; > > identifier i,f; > > @@ > > > > f(ps,t i, ...) { ... } > > > > @@ > > expression list[r.n] es; > > identifier r.i, r.f; > > expression e; > > @@ > > > > ( > > f(es,i,...) > > | > > f(es, > > - e > > + 0 > > ,...) > > ) > > > > I'm not sure if that is exactly what you want to do, but perhaps it will > > give some ideas. > > I just realized that you are no longer in the 'cc' in the email I send to Markus so I'm pasting > what I'm trying to do here: > > Finally I would like to archive something like this: > > void func(void) { > > int x = 0; > int y = 0; > > func1(x, 0); > func2(0, y); > } > > void func1(int arg1, int arg2) { > interesting_func(arg1); > } > > void func2(int arg1, int arg2) { > interesting_func(arg2); > } > > In the end I want to, for example, turn it into: > > void func(void) { > > int x = 0; > // 'x' was used as first argument to interesting_func > int y = 0; > // 'y' was used as first argument to interesting_func > > func1(x, 0); > func2(0, y); > } > > void func1(int arg1, int arg2) { > interesting_func(arg1); > } > > void func2(int arg1, int arg2) { > interesting_func(arg2); > } > > Basically I want to flag create the matching from 'x'/'y' to the argument to interesting_func. > > I was imagining if I could match the parameter list of any function calling 'interesting_func' > then I can also create a matching from x to that function call and do the rest of the correlation > in python somehow. > > However I'm still failing to detect if any argument (arg1 or arg2 in this case) is used as > any argument in 'interesting_func'. > > I hope this makes more sense? > > I think I understand your example, and you are basically matching on the length of 'n' on the parameter list. I'm not exactly sure if that would work with what I'm trying to accomplish. > > The more problematic case I *think* is matching the parameter list to the usage within that function. > In your example it is matching from a function definition to a call to that function. > > so not matching: > > f(x, y); > > to > > void f(int arg1, int arg2) { ... } > > but matching the parameter to a function call within: > > void f(int arg1, int arg2) { > > insteresting_func(arg1); // match arg1 here > > } How many insteresting_funcs do you have? How about: @ok exists@ position p; // add other metavariables as needed @@ f(...,t i,...) { ... when any interesting_func@p(i) ... when any } @@ position p1 != ok.p; @@ *interesting_func@p1(...) If you know the maximum number of arguments of insteresting_func, then you can make a series of rules, for the first argument, for the second argument, etc. julia > > Thanks, > Jakob > > > > > julia > > > >> > >>> > >>> julia > >>> > >>>> identifier func; > >>>> @@ > >>>> > >>>> func(Es); > >>>> + // add comment > >>>> > >>>> > >>>> Ideally I would like to 'find' all parameters used in a function call, > >>>> I also tried this: > >>>> > >>>> @main4@ > >>>> parameter P; > >>>> expression E; > >>>> @@ > >>>> > >>>> int func1(..., P@E, ...) { > >>>> ... > >>>> } > >>>> > >>>> @main5@ > >>>> expression main4.E; > >>>> identifier func; > >>>> @@ > >>>> > >>>> func(..., E, ...); > >>>> + // add comment > >>>> > >>>> > >>>> But it throws: 'rule starting on line 41 contains unattached metavariables: main4.E'. > >>>> > >>>> Any idea, why e.g. the second one doesn't work? If I print it with python the expression list > >>>> looks the same as for the first one. > >>>> Also in the end this boils down to: is it possible to match to any of the expression list elements? > >>>> > >>>> Thanks, > >>>> Jakob > >>>> > >>>>>> > >>>>>> > >>>>>> Any help is very much appreciated! > >>>>>> > >>>>>> Thanks, > >>>>>> Jakob > > ^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2022-08-17 19:21 UTC | newest] Thread overview: 12+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2022-08-16 8:58 [cocci] match arbitrary argument position Jakob Koschel -- strict thread matches above, loose matches on Subject: below -- 2022-08-16 8:54 Jakob Koschel 2022-08-16 11:37 ` Julia Lawall 2022-08-16 12:35 ` Jakob Koschel 2022-08-16 17:55 ` Markus Elfring 2022-08-17 14:26 ` Jakob Koschel 2022-08-17 19:21 ` Markus Elfring 2022-08-16 21:07 ` Julia Lawall 2022-08-17 14:18 ` Jakob Koschel 2022-08-17 14:36 ` Julia Lawall 2022-08-17 14:50 ` Jakob Koschel 2022-08-17 15:26 ` Julia Lawall
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.