[Cocci] iterator and single line statement

Julia Lawall julia at diku.dk
Mon Apr 20 16:10:29 CEST 2009


On Mon, 20 Apr 2009, Corentin Chary wrote:

> Hi,
> I'm trying to do a patch to replace list_for_each_entry with
> ulist_for_each_entry (don't ask why, it's ugly).
> 
> Here is my patch:
> 
> @@
> iterator name list_for_each_entry;
> iterator name ulist_for_each_entry;
> statement S;
> type T;
> T *a;
> expression b, c;
> @@
> 
> - list_for_each_entry(a, b, c)
> + ulist_for_each_entry(a, b, c, T)
>   S
> 
> @@
> iterator name list_for_each_entry_safe;
> iterator name ulist_for_each_entry_safe;
> statement S;
> type T;
> T *a;
> expression b, c;
> @@
> 
> - list_for_each_entry_safe(a, b, c)
> + ulist_for_each_entry_safe(a, b, c, T)
>   S
> 
> With a single line statement it produce :
>         spin_lock(&c->buds_lock);
>         for (i = 0; i < c->jhead_cnt; i++)
> -               list_for_each_entry(bud, &c->jheads[i].buds_list, list)
> +               {ulist_for_each_entry(bud, &c->jheads[i].buds_list,
> list, struct ubifs_bud)
>                         bud_bytes += c->leb_size - bud->start;
> +               }
> 
> 
> Is it a bug in coccinelle, or my patch is not ok ?

Actually, it's more of a feature...

It doesn't realize that the new code is a single-line statement, and it 
sees the for loop beforehand, so it adds the { } around the iterator.

I will look into whether it can be made smarter in this case, but in the 
short term, if there are not too many of these cases, perhaps you can just 
delete the braces by hand?

Thanks for your interest in Coccinelle.

julia


More information about the Cocci mailing list