I am trying to use Coccinelle to update a large number of files with C code to the Linux coding styles, but I can't manage to change the struct names. I am using the following script:
@rule1 disable all@
type t;
@@
typedef struct {
...
} t;
@ script:python foo @
t_fixed;
var;
t << rule1.t;
@@
import re
def split_uppercase(s):
return re.sub(r'([a-z-0-9])((?<![0-9]{4})[A-Z])',r'\1_\2',s).replace("_k_Hz", "_kHz").replace("_M_Hz", "_MHz").replace("d_B", "dB").lower()
print coccinelle.t
coccinelle.t_fixed = split_uppercase(t)
print coccinelle.t_fixed
coccinelle.var = cocci.make_type(t_fixed)
print var
@rule2 disable all@
type rule1.t;
type foo.var;
@@
+ var
- t
The desired result is to change:
typedef struct {
char me;
long you;
} strName;
int main (void) {
strName *DeV;
return 0;
}
To:
typedef struct {
char me;
long you;
} str_name;
int main (void) {
str_name *DeV;
return 0;
}
But I am getting the following error:
init_defs_builtins: /usr/lib/coccinelle/standard.h
----------------------------------------------------------------------- processing semantic patch file: structs_test.cocci with isos from:
/usr/lib/coccinelle/standard.iso
-----------------------------------------------------------------------
dependencies for script satisfied: HANDLING: test_basic.c dependencies
for rule rule1 satisfied:
transformation info is empty dependencies for script satisfied: Traceback (most recent call last): File "<string>", line 10, in
<module> AttributeError: Cocci instance has no attribute 'make_type'
while running simple python string: from coccinelle import *
import re def split_uppercase(s):
return re.sub(r'([a-z-0-9])((?<![0-9]{4})[A-Z])',r'\1_\2',s).replace("_k_Hz",
"_kHz").replace("_M_Hz", "_MHz").replace("d_B", "dB").lower()
print coccinelle.t coccinelle.t_fixed = split_uppercase(t) print
coccinelle.t_fixed coccinelle.var = cocci.make_type(t_fixed) print var
: Failure in foo Fatal error: exception Yes_pycocci.Pycocciexception
I am sorry if the question is wrong.