Skip to contentSkip to conversation

match arbitrary argument position

cocci Jakob Koschel 3 participants 25 messages Aug 16, 2022 – Aug 21, 2022

lore
Share & tools
lore
#0 lore raw

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

#1 lore raw
Quoted 7 lines · 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.

Quoted 8 lines

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

Quoted 31 lines

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

#2 lore raw
Quoted 26 lines · Julia Lawall
Quoted 7 lines · 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.

Quoted 8 lines

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,...)

Quoted 8 lines

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.

Quoted 21 lines
Quoted 21 lines

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)
   ...

}

Quoted 4 lines
Quoted 4 lines

(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

Quoted 6 lines
Quoted 6 lines

Any help is very much appreciated!

Thanks,
Jakob

#3 lore raw
Quoted 3 lines

looking at the following code snippet:

test.c:

…

Quoted 3 lines
      func3(argc, argv);

      func3(argv);

Would you like to handle function overloading here?

Quoted 9 lines

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”?

Quoted 10 lines

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?

Quoted 10 lines
   @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

#4 lore raw

Hey,

Quoted 12 lines · Markus Elfring
Quoted 4 lines

looking at the following code snippet:

test.c:

…

Quoted 3 lines
     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?

Quoted 9 lines
Quoted 9 lines

what doesn't work (matching on the expression list from the parameter list):

@main4@
expression list Es;
@@

Quoted 6 lines
Quoted 3 lines
  int func1(Ps@Es) {
  ...
  }

Which type would belong to the metavariable “Ps”?

Ps should be an arbitrary length parameter list with arbitrary types.

Quoted 15 lines
Quoted 5 lines

I also tried this:

Quoted 8 lines

@main4@
parameter P;
expression E;
@@

  int func1(..., P@E, ...) {
  ...
  }
Quoted 4 lines

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.

Quoted 7 lines
Quoted 7 lines

@main5@
expression main4.E;
identifier func;
@@

Quoted 7 lines
Quoted 5 lines
    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

Quoted 7 lines

I am curious how corresponding considerations will evolve further.

Regards,
Markus

Thanks!
Jakob

#5 lore raw
Quoted 10 lines
Quoted 9 lines
Quoted 3 lines

looking at the following code snippet:

test.c:

…

Quoted 3 lines
     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.

Quoted 1 line

Finally I would like to archive something like this:

You would probably like to achieve special source code transformations instead of archiving items.

Quoted 13 lines

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);
}
Quoted 7 lines
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?

Quoted 1 line

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?

Quoted 3 lines

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.

Quoted 2 lines

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.

Quoted 1 line

I hope this makes more sense?

It can be challenging to express special imaginations by the means of the semantic patch language.

Quoted 1 line

hopefully the example above makes it a bit more clear.

It seems so.

Regards,
Markus

#6 lore raw
In reply to Jakob Koschel
Quoted 55 lines
Quoted 17 lines
Quoted 17 lines

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)
   ...

}

Quoted 4 lines
Quoted 4 lines

(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):

Quoted 10 lines
   @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

Quoted 45 lines
   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

Quoted 6 lines
Quoted 6 lines

Any help is very much appreciated!

Thanks,
Jakob

#7 lore raw
Quoted 67 lines · Julia Lawall
Quoted 64 lines
Quoted 18 lines
Quoted 18 lines

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)
...
}

Quoted 4 lines
Quoted 4 lines

(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) {
  ...
  }
Quoted 8 lines
Quoted 2 lines

@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;

Quoted 46 lines

julia

Quoted 43 lines

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

Quoted 6 lines
Quoted 6 lines

Any help is very much appreciated!

Thanks,
Jakob

#8 lore raw
Quoted 94 lines · Jakob Koschel
Quoted 67 lines · Julia Lawall
Quoted 64 lines
Quoted 18 lines
Quoted 18 lines

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)
...
}

Quoted 4 lines
Quoted 4 lines

(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) {
  ...
  }
Quoted 8 lines
Quoted 2 lines

@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

Quoted 6 lines

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

Quoted 49 lines
Quoted 47 lines

julia

Quoted 43 lines

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

Quoted 6 lines
Quoted 6 lines

Any help is very much appreciated!

Thanks,
Jakob

#9 lore raw
Quoted 137 lines · Julia Lawall
Quoted 94 lines · Jakob Koschel
Quoted 67 lines · Julia Lawall
Quoted 64 lines
Quoted 18 lines
Quoted 18 lines

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)
...
}

Quoted 4 lines
Quoted 4 lines

(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) {
...
}

Quoted 8 lines
Quoted 2 lines

@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

Quoted 6 lines

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
,...)
)

Quoted 2 lines

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

Quoted 50 lines

julia

Quoted 47 lines
Quoted 47 lines

julia

Quoted 43 lines

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

Quoted 6 lines
Quoted 6 lines

Any help is very much appreciated!

Thanks,
Jakob

#10 lore raw
Quoted 207 lines · Jakob Koschel
Quoted 137 lines · Julia Lawall
Quoted 94 lines · Jakob Koschel
Quoted 67 lines · Julia Lawall
Quoted 64 lines
Quoted 18 lines
Quoted 18 lines

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)
...
}

Quoted 4 lines
Quoted 4 lines

(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) {
...
}

Quoted 8 lines
Quoted 2 lines

@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

Quoted 6 lines

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
,...)
)

Quoted 2 lines

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) { ... }

Quoted 7 lines

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

Quoted 56 lines

Thanks,
Jakob

Quoted 50 lines

julia

Quoted 47 lines
Quoted 47 lines

julia

Quoted 43 lines

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

Quoted 6 lines
Quoted 6 lines

Any help is very much appreciated!

Thanks,
Jakob

#11 lore raw

Re: [cocci] Working with parameter/expression lists by SmPL

In reply to Julia Lawall
Quoted 18 lines
Quoted 18 lines

I just tried:

@main4@
parameter list Ps;
@@

int func1(Ps) {
...
}

@main5@
expression list main4.Ps;
identifier func;
@@

func(Es);
+ // add comment

Quoted 8 lines
Quoted 6 lines

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.

I would appreciate further explanations for such information.

Regards,
Markus

#12 lore raw

Re: [cocci] Working with parameter/expression lists by SmPL

Hey,

Quoted 26 lines · Markus Elfring
Quoted 25 lines
Quoted 25 lines

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;

Quoted 5 lines
Quoted 2 lines

OK, there is a specific context in which this works, but it is only for generating semantic patch rules.

I would appreciate further explanations for such information.

I've finally managed to get it working. Always when I struggle with coccinelle and get it working in the end it looks completely obvious.

Thanks a lot of for all the help Markus & Julia!

Let me show what I was trying to archive and how I solved it:

    @r1@
    parameter list [n] ps;
    type t;
    identifier i,f;
    position p;
    @@

    f(ps, t i, ...) {
    ... when any
    interesting_func@p(..., i, ...)
    ... when any
    }

    @r2@
    identifier r1.f;
    expression list [r1.n] es;
    expression e, e1;
    position p;
    @@

    e = e1
    + // here the argument to interesting_func was assigned
    ... when any
    f(es, e, ...)@p;

I'm running it with this sample code:

    int interesting_func(int argc, char *str) {

    }

    int main(int argc, char *argv[]) {
      int x = 0;

      func1(x, "asdf");
    }

    int func1(int number, char *str) {
      int y = 0;
      char *x = "x";

      interesting_func(number, str);

      interesting_func(number, x);

      interesting_func(y, str);

      interesting_func(y, x);

      return 0;
    }

with the desired result:

tmp/cocci-output-test1.c renamed from test1.c +2 -1
diff =
@@ -6,7 +6,8 @@ int interesting_func(int argc, char *str
 }
 
 int main(int argc, char *argv[]) {
-  int x = 0;
+  int x = 0
+    // test;
 
   func1(x, "asdf");
 }

Thanks,
Jakob

Quoted 5 lines

Regards,
Markus

#13 lore raw

Re: [cocci] Working with parameter/expression lists by SmPL

Quoted 1 line

Let me show what I was trying to archive and how I solved it:

How do you think about to achieve further software adjustments?

…

Quoted 1 line
    interesting_func@p(..., i, ...)

…

I interpret such SmPL code in the way that the specified position variable could be omitted because it was not used by another subsequent SmPL rule.

…

Quoted 2 lines

e = e1
+ // here the argument to interesting_func was assigned

…

tmp/cocci-output-test1.c int interesting_func(int argc, char *str · line 13

Quoted 11 lines

+++ /tmp/cocci-output-test1.c

tmp/cocci-output-test1.c renamed from test1.c +2 -1
@@ -6,7 +6,8 @@ int interesting_func(int argc, char *str
 }
 
 int main(int argc, char *argv[]) {
-  int x = 0;
+  int x = 0
+    // test;
 
   func1(x, "asdf");
 }

Do you vary comment additions for your test results?

Regards,
Markus

#14 lore raw

Re: [cocci] Working with parameter/expression lists by SmPL

Quoted 8 lines · Markus Elfring
Quoted 2 lines

Let me show what I was trying to archive and how I solved it:

How do you think about to achieve further software adjustments?

I'm trying to detect certain code patterns spanning across files with this. They will *probably* not easily be automatically patchable since I'm trying to find bugs with this in an ongoing research project.

Quoted 9 lines

…

Quoted 1 line
   interesting_func@p(..., i, ...)

…

I interpret such SmPL code in the way that the specified position variable could be omitted because it was not used by another subsequent SmPL rule.

You're right, the positions can be omitted here, but I'm using them with a simple python script afterwards to report the matches I found where the positions will be useful.

Quoted 16 lines

…

Quoted 2 lines

e = e1
+ // here the argument to interesting_func was assigned

…

Quoted 9 lines

+++ /tmp/cocci-output-test1.c

tmp/cocci-output-test1.c renamed from test1.c +2 -1
@@ -6,7 +6,8 @@ int interesting_func(int argc, char *str
 }
 
 int main(int argc, char *argv[]) {
-  int x = 0;
+  int x = 0
+    // test;
 
Quoted 5 lines

tmp/cocci-output-test1.c int interesting_func(int argc, char *str · line 13

Quoted 2 lines
tmp/cocci-output-test1.c renamed from test1.c +0 -0
  func1(x, "asdf");
 }

Do you vary comment additions for your test results?

yes exactly.

Only issue I'm having now is that it doesn't work across files.

I've found this [Link], suggesting to use '--file-groups' or '--kbuild-info' to
o analysis across multiple files. I've managed to get '--file-groups' working on a small example but it will get challenging to do on the entire kernel. The '--kbuild-info' option *looks* outdated at least I couldn't find an easy way to get it running on the kernel.

- jakob

Quoted 4 lines

Regards,
Markus

#15 lore raw

Re: [cocci] Working with parameter/expression lists by SmPL

Quoted 5 lines

I've found this [Link], suggesting to use '--file-groups' or '--kbuild-info' to
o analysis across multiple files. I've managed to get '--file-groups' working on a small example but it will get challenging to do on the entire kernel. The '--kbuild-info' option *looks* outdated at least I couldn't find an easy way to get it running on the kernel.

--file-groups is a better option.

The other option is to use iteration, and collect information on one iteration and use it on the next one. That would give a completely global analysis that might not scale. You can see an example in demos/iteration.cocci

julia

#16 lore raw

Re: [cocci] Working with parameter/expression lists by SmPL

Quoted 10 lines · Julia Lawall
Quoted 6 lines

I've found this [Link], suggesting to use '--file-groups' or '--kbuild-info' to
o analysis across multiple files. I've managed to get '--file-groups' working on a small example but it will get challenging to do on the entire kernel. The '--kbuild-info' option *looks* outdated at least I couldn't find an easy way to get it running on the kernel.

Quoted 6 lines

--file-groups is a better option.

The other option is to use iteration, and collect information on one iteration and use it on the next one. That would give a completely global analysis that might not scale. You can see an example in demos/iteration.cocci

The option to collection information and then do a separate run, is precisely what I was intending to do now. Since I don't see an easy way to use '--file-groups' on the entire kernel.

thanks for the pointer to the suitable example.

- jakob

Quoted 2 lines

julia

#17 lore raw

Re: [cocci] Working with parameter/expression lists by SmPL

In reply to Jakob Koschel
Quoted 3 lines

I'm trying to detect certain code patterns spanning across files with this. They will *probably* not easily be automatically patchable since I'm trying to find bugs with this in an ongoing research project.

Would you like to add a link for this evolving information source?

Quoted 3 lines

You're right, the positions can be omitted here, but I'm using them with a simple python script afterwards to report the matches I found where the positions will be useful.

Would you like to improve the distinction for the usages of mentioned variables any further by using operation modes (or variants) for SmPL scripts?

Would it become helpful to use the name ”interesting_func” as another metavariable?

Quoted 10 lines
Quoted 10 lines

…

Quoted 9 lines

+++ /tmp/cocci-output-test1.c

tmp/cocci-output-test1.c renamed from test1.c +2 -1
@@ -6,7 +6,8 @@ int interesting_func(int argc, char *str
 }
 
 int main(int argc, char *argv[]) {
-  int x = 0;
+  int x = 0
+    // test;
 
Quoted 4 lines
Quoted 3 lines

tmp/cocci-output-test1.c int interesting_func(int argc, char *str · line 13

Quoted 2 lines
tmp/cocci-output-test1.c renamed from test1.c +0 -0
  func1(x, "asdf");
 }

Do you vary comment additions for your test results?

yes exactly.

Do you stumble on any more development challenges for adding comments by the Coccinelle software?

How do you think about possibilities to add multi-line comments (instead of specifying desirable messages behind double slashes)?
https://en.cppreference.com/w/c/comment

Are you looking for better source code formatting according to semicolons?

Quoted 1 line

Only issue I'm having now is that it doesn't work across files.

Would you like to explain remaining development concerns any more?

Regards,
Markus

#18 lore raw

Re: [cocci] Working with parameter/expression lists by SmPL

Quoted 10 lines · Markus Elfring
Quoted 4 lines

I'm trying to detect certain code patterns spanning across files with this. They will *probably* not easily be automatically patchable since I'm trying to find bugs with this in an ongoing research project.

Would you like to add a link for this evolving information source?

I'm not sure what you mean "link for this evolving information source". There is no public information on this yet since it's an ongoing project.

Quoted 8 lines
Quoted 6 lines

You're right, the positions can be omitted here, but I'm using them with a simple python script afterwards to report the matches I found where the positions will be useful.

Quoted 4 lines

Would you like to improve the distinction for the usages of mentioned variables any further by using operation modes (or variants) for SmPL scripts?

Would it become helpful to use the name ”interesting_func” as another metavariable?

yes I've done that afterwards but for my use case it doesn't matter since it will only be one specific function regardless.

Quoted 19 lines
Quoted 13 lines
Quoted 13 lines

…

Quoted 9 lines

+++ /tmp/cocci-output-test1.c

tmp/cocci-output-test1.c renamed from test1.c +2 -1
@@ -6,7 +6,8 @@ int interesting_func(int argc, char *str
 }
 
 int main(int argc, char *argv[]) {
-  int x = 0;
+  int x = 0
+    // test;
 
Quoted 4 lines
Quoted 3 lines

tmp/cocci-output-test1.c int interesting_func(int argc, char *str · line 13

Quoted 2 lines
tmp/cocci-output-test1.c renamed from test1.c +0 -0
 func1(x, "asdf");
 }

Do you vary comment additions for your test results?

yes exactly.

Quoted 8 lines

Do you stumble on any more development challenges for adding comments by the Coccinelle software?

How do you think about possibilities to add multi-line comments (instead of specifying desirable messages behind double slashes)?
https://en.cppreference.com/w/c/comment

Are you looking for better source code formatting according to semicolons?

I'm not exactly sure what you mean with that? I've only used adding the comment to see if the matching works correctly, I'm not intending to do anything with comments in the final version.

Quoted 10 lines
Quoted 3 lines

Only issue I'm having now is that it doesn't work across files.

Would you like to explain remaining development concerns any more?

Regards,
Markus

#19 lore raw

Re: [cocci] Working with parameter/expression lists by SmPL

Quoted 2 lines

I'm not sure what you mean "link for this evolving information source". There is no public information on this yet since it's an ongoing project.

Are you looking for contributions from related research groups?

Quoted 2 lines
Quoted 1 line

Are you looking for better source code formatting according to semicolons?

I'm not exactly sure what you mean with that?

Do you care if a bit of code which is probably relevant for the programming language syntax would be commented out as it was indicated by the diff fragment “+    // test;”?

Quoted 1 line

I've only used adding the comment to see if the matching works correctly,

Would other code variants (than C++-style comments) be more helpful for the demonstration of desirable transformation effects?

Quoted 1 line

I'm not intending to do anything with comments in the final version.

My software application expectations evolved into additional directions according to mentioned change specifications (in SmPL code).

Regards,
Markus

#20 lore raw

Re: [cocci] Working with parameter/expression lists by SmPL

Jakob,

Despite the bizarre way in which Markus asks the question, the ; at the end of the comment could be a serious problem. Could you repost your semantic patch and test code?

thanks,
julia

Quoted 34 lines · Markus Elfring
Quoted 2 lines

I'm not sure what you mean "link for this evolving information source". There is no public information on this yet since it's an ongoing project.

Are you looking for contributions from related research groups?

Quoted 2 lines
Quoted 1 line

Are you looking for better source code formatting according to semicolons?

I'm not exactly sure what you mean with that?

Do you care if a bit of code which is probably relevant for the programming language syntax would be commented out as it was indicated by the diff fragment “+    // test;”?

Quoted 1 line

I've only used adding the comment to see if the matching works correctly,

Would other code variants (than C++-style comments) be more helpful for the demonstration of desirable transformation effects?

Quoted 1 line

I'm not intending to do anything with comments in the final version.

My software application expectations evolved into additional directions according to mentioned change specifications (in SmPL code).

Regards,
Markus

#21 lore raw

Re: [cocci] Working with parameter/expression lists by SmPL

Quoted 1 line

Despite the bizarre way in which Markus asks the question,

I find such a view also interesting.

Quoted 1 line

the ; at the end of the comment could be a serious problem.

Thanks for your wording variant about the usage of semicolons.

Quoted 1 line

Could you repost your semantic patch and test code?

I imagine that a semantic match (with the application of SmPL asterisks) could be also helpful for the desired understanding of the discussed transformation approach.

Regards,
Markus

#22 lore raw

Re: [cocci] Working with parameter/expression lists by SmPL

Quoted 14 lines · Markus Elfring
Quoted 1 line

Despite the bizarre way in which Markus asks the question,

I find such a view also interesting.

Quoted 1 line

the ; at the end of the comment could be a serious problem.

Thanks for your wording variant about the usage of semicolons.

Quoted 6 lines
Quoted 1 line

Could you repost your semantic patch and test code?

I imagine that a semantic match (with the application of SmPL asterisks) could be also helpful for the desired understanding of the discussed transformation approach.

I don't see why. I'm only concerned about why a // comment was added at the front of a line containing real code. Perhaps the pretty printer treats all comments the same...

julia

#23 lore raw

Re: [cocci] Working with parameter/expression lists by SmPL

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Could you repost your semantic patch and test code?

I imagine that a semantic match (with the application of SmPL asterisks) could be also helpful for the desired understanding of the discussed transformation approach.

I don't see why. I'm only concerned about why a // comment was added at the front of a line containing real code.

Jakob Koschel provided the following information yesterday.
https://sympa.inria.fr/sympa/arc/cocci/2022-08/msg00025.html
https://lore.kernel.org/cocci/[email protected]/

“…
I've only used adding the comment to see if the matching works correctly, I'm not intending to do anything with comments in the final version. …”

Quoted 1 line

Perhaps the pretty printer treats all comments the same...

How do you think about increasing case distinctions also for further improvements of the Coccinelle software?

* Under which circumstances may comment additions influence the control flow?

* Should the addition of a C++-style comment (before an SmPL ellipsis) eventually trigger the appending of a line break?

* Would you like to extend the test case collection accordingly?

Regards,
Markus

#24 lore raw

Re: [cocci] Checking a comment addition

In reply to Jakob Koschel

tmp/cocci-output-test1.c int interesting_func(int argc, char *str · line 13

Quoted 11 lines

+++ /tmp/cocci-output-test1.c

tmp/cocci-output-test1.c renamed from test1.c +2 -1
@@ -6,7 +6,8 @@ int interesting_func(int argc, char *str
 }
 
 int main(int argc, char *argv[]) {
-  int x = 0;
+  int x = 0
+    // test;
 
   func1(x, "asdf");
 }

I find such a comment addition questionable.

Do you expect that a semicolon should be used for the variable definition here?

Regards,
Markus