Coccinelle archive on lore.kernel.org
 help / color / Atom feed
From: Jerome Glisse <jglisse@redhat.com>
To: cocci@systeme.lip6.fr, Julia Lawall <julia.lawall@lip6.fr>
Subject: [Cocci] Matching an expression which use an identifier
Date: Sat, 23 May 2020 22:38:24 -0400
Message-ID: <20200524023824.GA2598074@redhat.com> (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
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

             reply index

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-24  2:38 Jerome Glisse [this message]
2020-05-24  6:08 ` Julia Lawall

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200524023824.GA2598074@redhat.com \
    --to=jglisse@redhat.com \
    --cc=cocci@systeme.lip6.fr \
    --cc=julia.lawall@lip6.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Coccinelle archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/cocci/0 cocci/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 cocci cocci/ https://lore.kernel.org/cocci \
		cocci@systeme.lip6.fr
	public-inbox-index cocci

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/fr.lip6.systeme.cocci


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git