Coccinelle archive on
 help / color / Atom feed
From: Jerome Glisse <>
To:, Julia Lawall <>
Subject: [Cocci] Matching an expression which use an identifier
Date: Sat, 23 May 2020 22:38:24 -0400
Message-ID: <> (raw)


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.

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

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

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.


Cocci mailing list

             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:

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

  git send-email \ \ \ \ \

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

Coccinelle archive on

Archives are clonable:
	git clone --mirror 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/ \
	public-inbox-index cocci

Example config snippet for mirrors

Newsgroup available over NNTP:

AGPL code for this site: git clone