使用coccinelle将结构名称从大写更改为小写

时间:2018-01-03 12:53:56

标签: c coccinelle

我正在尝试使用Coccinelle将大量使用C代码的文件更新为Linux编码样式,但我无法更改结构名称。我使用以下脚本:

@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

期望的结果是改变:

typedef struct {
    char me;
    long you;
} strName;

int main (void) {
    strName *DeV;
    return 0;
}

要:

typedef struct {
    char me;
    long you;
} str_name;

int main (void) {
    str_name *DeV;
    return 0;
}

但是我收到以下错误:

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

如果问题不对,我很抱歉。

0 个答案:

没有答案