Skip to contentSkip to conversation

Matching an expression which use an identifier

Cocci Jerome Glisse 2 participants 2 messages May 24, 2020

lore
Share & tools
lore
#0 lore raw

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};
identifier V2;
expression E;
statement S1;
statement S;
type T1, T2;
@@
{ ... T1 V1; ...

-T2 V2 = E;
+T2 V2;

// Insert it before first statement that is not a declaration. ... when != S
+V2=E;
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

#1 lore raw
Quoted 30 lines · Jerome Glisse

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

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.

Quoted 7 lines

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

put when any after the second ... above

Quoted 1 line

-T2 V2 = E;

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

Quoted 4 lines

+T2 V2;

// Insert it before first statement that is not a declaration. ... when != S

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

Quoted 1 line

+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

Quoted 20 lines

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.

Quoted 8 lines

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