Hello,
  attached is a very rough piece of .cocci which might
    be an aproximation of your wish and julias hint.
  I assembled it as a sort of excersice for myself.
Regards,
  Robert

// For example, I’d like to transform:
// 
// #define BITMASK_A FOO(0x3F, 4) /* 0x3F0 */
// 
#define BITMASK_A FOO(0x3F, 4)  /* 0x3F0 --> 9,4 */
#define BITMASK_NOTSO FOO(0, 4) /* 0x000 */
#define BITMASK_B FOO(7, 8)     /* 0x700 --> 10,8 */

void bar() {
    FOO(0x3f, 4);               /* not converted */
}
// (compile "spatch --sp-file ex9.cocci ex9.c")

@initialize:python@
@@

def hweight(n):
    return bin(n).count("1")

def contiguos_ones(n):
#   print n
#   print (n > 0) and ((n & (n+1)) == 0)
    return (n > 0) and ((n & (n+1)) == 0)


@a@
constant x : script:python() { contiguos_ones(int(x, 0)) };
constant y;
identifier nam;
@@

#define nam FOO(x,y)

@script:python b@
x << a.x;
y << a.y;
xx;
yy;
@@

my_x = int(x, 0)
my_y = int(y, 0)
# print x
# print y
# print my_x
# print hweight(my_x)
coccinelle.xx = str(hweight(my_x) + my_y - 1)
coccinelle.yy = str(my_y)

@c@
constant a.x;
constant a.y;
identifier a.nam;
identifier b.xx;
identifier b.yy;
@@

- #define nam FOO(x,y)
+ #define nam GENMASK(xx,yy)
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

Reply via email to