On Sat, 23 May 2020, Jerome Glisse wrote:

> Hi,
>
> I am trying to move local variable initialization of some variable to
> first statement. For instance in example below i want to move any local
> initialization that use local variable "a" to before the printf.
>
> From:
> void foo()
> {
>     int k = 8/2;
>     int a = 45*4;
>     int c = 16*2;
>     int b = a*4+2;
>     int d;
>
>     printf("%d %d %d", a, c, b);
> }
>
> To:
> void foo()
> {
>     int a = 45*4;
>     int c = 16*2;
>     int b;
>     int d;
>
>     b = a*4+2;
>     printf("%d %d %d", a, c, b);
> }
>
> Here is a rule that will move "c = 16*2"
>
> @@
> identifier V1={a};

The above line is not needed.  Just use a in your rule.

> identifier V2;
> expression E;
> statement S1;
> statement S;
> type T1, T2;
> @@
> { ... T1 V1; ...

put when any after the second ... above

> -T2 V2 = E;

put \(<+...a...+>\&E\) in place of E

> +T2 V2;
>
> // Insert it before first statement that is not a declaration.
> ... when != S

Put another when any here, below the when != S

> +V2=E;

Put a ++ instead of +, since there may be several things to add.  There is
no guarantee on the order in which the added code appears.

julia

> S1}
>
>
> This match the first declaration after "a" declaration. I have try using
> V1@E or E@V1 but it does not seem to match b declaration.
>
> Note that i do not know the form of the expression in which "a" appears,
> nor do i know the type of "b". My objective is to move the initialization
> of some local variable of some type to first statement (role played by "a"
> in above example) and then move all dependent initialization after it
> (in above example "b" is dependent on "a").
>
> I am not sure what kind of filter on E i can use to make it float to
> only expression containing "a" ...
>
> Hope this is something that can be done, i would hate having to do it
> in python :)
>
> Thank you in advance for any pointers.
>
> Cheers,
> Jérôme
>
> _______________________________________________
> Cocci mailing list
> [email protected]
> https://systeme.lip6.fr/mailman/listinfo/cocci
>
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

Reply via email to