3

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?

DarthNoodles
  • 370
  • 3
  • 11
  • What do you mean by "ordered"/"unordered"? If I get that right, you are looking for a toll. Sorry, that is off-topic for SO. – too honest for this site Jul 23 '15 at 20:42
  • Your question is well-posed and interesting, but unfortunately it is off-topic for SO. – John Bollinger Jul 23 '15 at 20:42
  • 2
    @Olaf The "unordered" are known as "designated" initializations. – Eugene Sh. Jul 23 '15 at 20:45
  • " Somebody added a member in the middle of the definition " git blame? – Daniel Jour Jul 23 '15 at 21:25
  • 1
    @JohnBollinger Where does it belong if not here? There are numerous questions, and a tag, for coccinelle. – DarthNoodles Jul 24 '15 at 04:08
  • @DanielJour I know who it is, that's not the issue. My choices are to manually add the new member initialization (NULL) to hundreds of declarations, and repeat next time somebody does something similar. Or, I can change all the declarations to ordered (designated) and next time we change the definition, we don't necessarily have to update the declarations. I was told coccinelle should be able to help do it with a script instead of doing it manually. – DarthNoodles Jul 24 '15 at 04:15

2 Answers2

1

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
 ,...};
0

Coccinelle tutorials: http://coccinelle.lip6.fr/papers.php

You can also try the mailing list for asking questions. Something like this may work:

@@
constant C1, C2;
identifier i;
@@
struct some_struct i = {
+       .blah1 =
        C1,
+       .blah2 =
        C2,
};
Peter
  • 1,753
  • 18
  • 19