cocci.inria.fr archive mirror
 help / color / mirror / Atom feed
* [Cocci] Matching an expression which use an identifier
@ 2020-05-24  2:38 Jerome Glisse
  2020-05-24  6:08 ` Julia Lawall
  0 siblings, 1 reply; 2+ messages in thread
From: Jerome Glisse @ 2020-05-24  2:38 UTC (permalink / raw)
  To: cocci, Julia Lawall

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
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Cocci] Matching an expression which use an identifier
  2020-05-24  2:38 [Cocci] Matching an expression which use an identifier Jerome Glisse
@ 2020-05-24  6:08 ` Julia Lawall
  0 siblings, 0 replies; 2+ messages in thread
From: Julia Lawall @ 2020-05-24  6:08 UTC (permalink / raw)
  To: Jerome Glisse; +Cc: cocci

[-- Attachment #1: Type: text/plain, Size: 2104 bytes --]



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
> Cocci@systeme.lip6.fr
> https://systeme.lip6.fr/mailman/listinfo/cocci
>

[-- Attachment #2: Type: text/plain, Size: 136 bytes --]

_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-05-24  6:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-24  2:38 [Cocci] Matching an expression which use an identifier Jerome Glisse
2020-05-24  6:08 ` Julia Lawall

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