Some times people write code that screams "this should be a for statement" by
using while instead. Coccinelle is of course the perfect tool to
correct this, but I
have some trouble defining a rule where the loop counter is
initialized on declaration.
Tests input:
void f1(void)
{
int i;
i = 0;
while (i<10) {
printf("%d\n", i);
i++;
}
}
void f2(void)
{
int i = 0;
while (i<10) {
printf("%d\n", i);
i++;
}
}
Script:
@rule1@
expression i; // using expression instead of just identifier,
since that will miss out on array elements for instance
expression E1, E2;
@@
- i = E1;
...
- while( E2 )
+ for (i=E1; E2; i++)
{
...
- i++;
}
@rule2@
identifier i;
expression E1, E2;
Type T;
@@
- T i = E1;
+ T i;
...
- while( E2 )
+ for (i=E1; E2; i++)
{
...
- i++;
}
Running with only rule1 works fine on f1. With rule2 added I get an error:
Fatal error: exception Failure("minus: parse error:
= File "while2for.cocci", line 26, column 4, charpos = 283
around = 'i', whole content = - T i = E1;
")
and I do not understand why coccinelle complains about syntax here.
BR Håkon Løvdal
_______________________________________________
Cocci mailing list
[email protected]
http://lists.diku.dk/mailman/listinfo/cocci
(Web access from inside DIKUs LAN only)