Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coccinelle: Change ordered struct declaration to unordered

Tags:

c

coccinelle

I'm trying to find some resources/tutorials to help me create a coccinelle script to find structure declarations and change them from ordered to unordered.

In our code base we use several structs hundreds of times. Somebody added a member in the middle of the definition and now I need to update hundreds of declarations. A default value of 0 is good so if I switch all the declarations from order to unordered, all is good, and more future proof for next change.

Ex:

struct some_struct {
   int blah;
   int blah2;
}

// code is like this now.
struct some_struct ex1 = {
   0,
   1,
};

// Need script to change to this
struct some_struct ex2 = {
   .blah1 = 0,
   .blah2 = 1
}

Can anybody point me in the right direction?

like image 643
DarthNoodles Avatar asked Dec 10 '25 17:12

DarthNoodles


1 Answers

Lightly tested:

@ok1@
identifier i1,i2;
position p;
@@

struct i1 i2@p = { \(0\|NULL\) };

@ok2@
identifier i1,i2,i3;
position p;
expression e;
@@

struct i1 i2@p = { ..., .i3 = e, ... };

@ok3@
identifier i1,i2;
position p;
@@

struct i1 i2@p = { ..., { ... }, ... };

@decl@
identifier i1,fld;
type T;
field list[n] fs;
@@

struct i1 {
 fs
 T fld;
 ...};

@bad@
identifier decl.i1,i2;
expression e;
position p != {ok1.p,ok2.p,ok3.p};
constant nm;
initializer list[decl.n] is;
position fix;
@@

struct i1 i2@p = { is,
(
 nm(...)
|
 e@fix
)
 ,...};

@@
identifier decl.i1,i2,decl.fld;
expression e;
position bad.p, bad.fix;
@@

struct i1 i2@p = { ...,
+ .fld = e
- e@fix
 ,...};
like image 145
user2777045 Avatar answered Dec 12 '25 11:12

user2777045