Skip to contentSkip to conversation

Are types re-evaluated between subsequent rules?

Cocci Christoph Böhmwalder 3 participants 8 messages May 27, 2019

lore
Share & tools
lore
#0 lore raw

Hi,

I'm having trouble understanding coccinelles behaviour here. Consider the following C code and cocci rules:

#include <stdio.h>

int x;

int main(int argc, char **argv)

{
    f(x);
}

@@
identifier x;
@@
- int x;
+ int *x;

@@
int *x;
@@
- f(x)
+ g(x)

Since I read on some slides[0] that "Later rules see the results of earlier rules", I would assume that this would pick up the type change introduced by the first rule and replace f by g because it now has an "int *" parameter. However, spatch merely outputs the patch to change the type of x. If I change the type of the "expected" x in rule 2 to "int", cocci picks it up correctly.

Am I missing something?

[0]: http://events17.linuxfoundation.org/sites/events/files/slides/part1.pdf, page 43

--
Regards,
Christoph
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#1 lore raw

Re: [Cocci] Checking change scope for a data type replacement

Quoted 1 line

Am I missing something?

It depends on details.

Your initial transformation approach can be written also as follows.

@replacement@
identifier x;
@@
-int
+int*
x;

In which scopes would you like to add the asterisk for the usage of a pointer data type?

Do you expect that function parameters should be also modified here?

Regards,
Markus
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#2 lore raw

Re: [Cocci] Checking change scope for a data type replacement

Quoted 13 lines · Markus Elfring
Quoted 1 line

Am I missing something?

It depends on details.

Your initial transformation approach can be written also as follows.

@replacement@
identifier x;
@@

(quoted diff) +1 -1
-int
+int*
  x; >
Quoted 2 lines

In which scopes would you like to add the asterisk for the usage of a pointer data type?

I'm trying to replace a particular function call with a one-liner if the function is not defined, so I control the scope myself. In other words, in my original code "int x" is passed to "void f(int)" as a paramter, and I would like to apply the following transformations:
1) "x" has a type of "int *"
2) the new "int *x" gets passed to a function "void g(int *)" instead.

Quoted 6 lines

Do you expect that function parameters should be also modified here?

Regards,
Markus

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#3 lore raw

Re: [Cocci] Checking change scope for a data type replacement

Quoted 9 lines
Quoted 9 lines

@replacement@
identifier x;
@@

(quoted diff) +1 -1
-int
+int*

  x; >

In which scopes would you like to add the asterisk for the usage of a pointer data type?

…

Quoted 1 line

1) "x" has a type of "int *"

The asterisk addition seems to work for (local) variables.

Quoted 1 line

2) the new "int *x" gets passed to a function "void g(int *)" instead.

The succinct SmPL change specification looks like it could express data type adjustments also at other places.

But an additional SmPL rule seems to be needed at the moment.

@replacement2@
identifier f, x;
type t;
@@
t
f(...,
- int
+ int*
x, ...)
{ ... }

Do you distinguish any references to global variables here?

Regards,
Markus
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#4 lore raw

Re: [Cocci] Checking change scope for a data type replacement

In reply to Christoph Böhmwalder
Quoted 2 lines

In other words, in my original code "int x" is passed to "void f(int)" as a paramter, and I would like to apply the following transformations:

How do you think about to try a SmPL change specification out like the following?

@replacement3@
identifier x;
@@
-int
+int*
x;
<+...
-f
+g
(x);
...+>

Regards,
Markus
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#5 lore raw

Re: [Cocci] Checking change scope for a data type replacement

Quoted 8 lines · Markus Elfring
Quoted 2 lines

In other words, in my original code "int x" is passed to "void f(int)" as a paramter, and I would like to apply the following transformations:

How do you think about to try a SmPL change specification out like the following?

@replacement3@
identifier x;
@@

Review

Quoted 8 lines
(quoted diff) +2 -2
-int
+int*
 x;
 <+...
-f
+g
  (x);
 ...+>

His example shows that he wants to change a parameter type, not a local variable.

julia

Quoted 9 lines

Regards,
Markus
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#6 lore raw

Re: [Cocci] Checking change scope for a data type replacement

Quoted 5 lines
Quoted 5 lines

@replacement3@
identifier x;
@@

(quoted diff) +1 -1
-int
+int*
Quoted 8 lines

Review

Quoted 6 lines

x;
<+...

(quoted diff) +1 -1
-f
+g
  (x);
 ...+>

His example shows that he wants to change a parameter type,

He would like to call a function which gets a single pointer passed instead of an integer by possibly varying variables.

Quoted 1 line

not a local variable.

The support for source code transformations together with global variables might need further software development considerations.

Regards,
Markus
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#7 lore raw
In reply to Christoph Böhmwalder
Quoted 30 lines · Christoph Böhmwalder

Hi,

I'm having trouble understanding coccinelles behaviour here. Consider the following C code and cocci rules:

#include <stdio.h>

int x;

int main(int argc, char **argv)

{
    f(x);
}

@@
identifier x;
@@
- int x;
+ int *x;

@@
int *x;
@@
- f(x)
+ g(x)

Quoted 7 lines

Since I read on some slides[0] that "Later rules see the results of earlier rules", I would assume that this would pick up the type change introduced by the first rule and replace f by g because it now has an "int *" parameter. However, spatch merely outputs the patch to change the type of x. If I change the type of the "expected" x in rule 2 to "int", cocci picks it up correctly.

Am I missing something?

Your first rule changes only variable declarations, which consist of a type, a variable and a semicolon. If you want to change all int types to int *, then you can change the first rule to remove the x and the ;. If you want to change int x in the parameter list of main, or in the parameter list of all functions, then you could do

@@
identifier f; // all functions
identifier x;
@@

f(...,
- int x
+ int *x
,...) { ... }

You can factor out the common parts, ie int and x from the transformation, as Markus suggests, but I doubt this would be useful.

julia

Quoted 5 lines

[0]: http://events17.linuxfoundation.org/sites/events/files/slides/part1.pdf,
page 43

Quoted 8 lines

--
Regards,
Christoph
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci