cocci.inria.fr archive mirror
 help / color / mirror / Atom feed
From: Jakob Koschel <jkl820.git@gmail.com>
To: Julia Lawall <julia.lawall@inria.fr>
Cc: cocci@inria.fr
Subject: Re: [cocci] match arbitrary argument position
Date: Wed, 17 Aug 2022 16:50:42 +0200	[thread overview]
Message-ID: <4DFE05B0-AD73-42C5-87C3-F6ED651D8439@gmail.com> (raw)
In-Reply-To: <alpine.DEB.2.22.394.2208171631050.2482@hadrien>



> 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


  reply	other threads:[~2022-08-17 15:27 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-16  8:54 [cocci] match arbitrary argument position 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 [this message]
2022-08-17 15:26             ` Julia Lawall
2022-08-17 19:48           ` [cocci] Working with parameter/expression lists by SmPL Markus Elfring
2022-08-18 12:51             ` Jakob Koschel
2022-08-18 17:42               ` Markus Elfring
2022-08-19  9:12                 ` Jakob Koschel
2022-08-19  9:57                   ` Julia Lawall
2022-08-19 10:00                     ` Jakob Koschel
2022-08-19 17:00                   ` Markus Elfring
2022-08-20 12:57                     ` Jakob Koschel
2022-08-21  8:10                       ` Markus Elfring
2022-08-21  9:09                         ` Julia Lawall
2022-08-21  9:46                           ` Markus Elfring
2022-08-21 10:01                             ` Julia Lawall
2022-08-21 11:33                               ` Markus Elfring
2022-08-18 18:00               ` [cocci] Checking a comment addition Markus Elfring
2022-08-16  8:58 [cocci] match arbitrary argument position Jakob Koschel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4DFE05B0-AD73-42C5-87C3-F6ED651D8439@gmail.com \
    --to=jkl820.git@gmail.com \
    --cc=cocci@inria.fr \
    --cc=julia.lawall@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).