On Sun, 18 Dec 2011, Cyril Roelandt wrote:

Hello,

I would like to use Coccinelle to turn the following piece of code :

#define N 4

int array[N];

struct dummy_struct {
       int foo[N];
};

struct dummy_struct dummy = {
       .foo = {}
};

static int
bar(void)
{
       array[0] = BAR;
       array[1] = BAZ;
        /* some more code ... */
}


into something like this :


#define N 4

int array[N];

struct dummy_struct {
       int foo[N];
};

struct dummy_struct dummy = {
       .foo = { BAR, BAZ }
};

static int
bar(void)
{
        /* some more code ... */
}



I wrote the following Coccinelle script, that almost does the job :

@r@
identifier a;
expression E;
@@
- a[...] = E;


@depends on r@
identifier d;
expression r.E;
@@
struct dummy_struct d = {
       .foo = {
++ E,
...
}
};

But I don't get the expected result :

@@ -7,12 +7,12 @@ struct dummy_struct {
};

struct dummy_struct dummy = {
-       .foo = {}
+       .foo = {
+               BAZ,
+               BAR, }
};

static int
bar(void)
{
-       array[0] = BAR;
-       array[1] = BAZ;
}


The "foo" field is initialized to { BAZ, BAR, } instead of { BAR, BAZ }. How could I modify the second rule so that :
1) BAR comes before BAZ
2) No extra comma is added


Unfortuately, it is not possible. There is no way to control the order with ++. So you will have to do some editing by hand. I would suggest to put in your ++ rule BAR(0) and BAZ(1). Then you can easily see what changes to make afterwards.

It would be possible to cange coccinelle to drop an added comma followed by an existing }, if this is considered to be always a good idea.

julia
_______________________________________________
Cocci mailing list
[email protected]
http://lists.diku.dk/mailman/listinfo/cocci
(Web access from inside DIKUs LAN only)

Reply via email to