Hi,

I am trying to clean-up the MTD linux kernel subsystem and I have
some troubles with writing a semantic patch. I've created a small
example.c file:

$ cat example.c 
static int mtd_read(int *retlen)
{
        *retlen = 0;
        return 0;
}

static void mtdchar_read1(void)
{
        int retlen;

        retlen = 0;
        mtd_read(&retlen);
}

static void mtdchar_read2(void)
{
        int retlen = 0;

        mtd_read(&retlen);
}

static void mtdchar_read3(void)
{
        int retlen;
        int mode = 1;

        retlen = 0;

        switch (mode) {
        case 1:
                mtd_read(&retlen);
                break;
        case 2:
                break;
        }
}

I know that mtd_read initialized the retlen to 0, so I do not want the
callers to do this. I want to find all the places. Here is the semantic
patch I've tried:

$ cat retlen.cocci 
@@
identifier retlen;
@@
- retlen = 0
...
mtd_read(&retlen)

And here is the result:

$ spatch -sp_file retlen.cocci example.c
init_defs_builtins: /usr/share/coccinelle/standard.h
the simple assignment expression on line 4 contains transformations
that prevent it from matching a declaration (multiple replacements)

HANDLING: example.c
diff = 
--- example.c
+++ /tmp/cocci-output-16957-5edc2c-example.c
@@ -8,7 +8,7 @@ static void mtdchar_read1(void)
 {
        int retlen;
 
-       retlen = 0;
+       ;
        mtd_read(&retlen);
 }

I have several questions:

1. Warning about "preventing from matching a declaration" worries me. As
   I understand - a declaration is a construct like "int a" or
   "static long x, y=0, z", etc (C99 6.7).

   But I do want to match "int retlen = 0"... What should I do?
   IOW, how to match 'mtdchar_read2()' ?

2. I do not need an empty ";" ... Should I create a second rule
   which removes extra ";" ? Or there is a better way?

3. Why "mtdchar_read3()" does not match?

Thanks for help!

-- 
Best Regards,
Artem Bityutskiy

Attachment: signature.asc
Description: This is a digitally signed message part

_______________________________________________
Cocci mailing list
[email protected]
http://lists.diku.dk/mailman/listinfo/cocci
(Web access from inside DIKUs LAN only)

Reply via email to