使用 spatch 从结构实例化中删除字段

问题描述 投票:0回答:1

当我尝试在实例化中分配结构体的字段时,我不断收到错误:

@@
expression e; identifier i;
@@

 static struct genl_family i = {
 ...
- .id = e
 ...
 }

输出:

init_defs_builtins: /usr/lib/coccinelle/standard.h
75 76
Fatal error: exception Failure("minus: parse error: \n = File \"4-15to5-4.cocci\", line 8, column 2,  charpos = 75\n    around = '.', whole content = - .id = e\n")

我试图分发的代码:

static struct genl_family nl_ecnd_fam = {
    .id = GENL_ID_GENERATE,
    .name = ECNL_GENL_NAME,
    .hdrsize = 0,
    .version = 1,
    .maxattr = NL_ECNL_ATTR_MAX,
    .netnsok = true,
    .pre_doit = nl_ecnl_pre_doit,
    .post_doit = nl_ecnl_post_doit,
};

有什么建议吗?

c linux coccinelle
1个回答
0
投票

这是一个老问题,但这适用于 Coccinelle 版本 1.1.1。以下语义补丁:

@@
expression expr;
identifier i;
@@
 static struct genl_family i = {
 ...,
- .id = expr,
 ...
 };

用于此代码时:

static struct genl_family nl_ecnd_fam = {
    .id = GENL_ID_GENERATE,
    .name = ECNL_GENL_NAME,
    .hdrsize = 0,
    .version = 1,
    .maxattr = NL_ECNL_ATTR_MAX,
    .netnsok = true,
    .pre_doit = nl_ecnl_pre_doit,
    .post_doit = nl_ecnl_post_doit,
};

给出这个结果:

$ spatch --very-quiet --sp-file field.cocci code/field-example.c
--- code/field-example.c
+++ /tmp/cocci-output-403483-9f1fa0-field-example.c
@@ -1,5 +1,4 @@
 static struct genl_family nl_ecnd_fam = {
-    .id = GENL_ID_GENERATE,
     .name = ECNL_GENL_NAME,
     .hdrsize = 0,
     .version = 1,
© www.soinside.com 2019 - 2024. All rights reserved.