On Sat, 27 Nov 2010, Eric Leblond wrote:

> Hi,
> 
> Le samedi 27 novembre 2010 à 15:56 +0100, Julia Lawall a écrit :
> > On Sat, 27 Nov 2010, Eric Leblond wrote:
> > 
> > > Hi,
> > > 
> > > Le jeudi 25 novembre 2010 à 18:55 +0100, Julia Lawall a écrit :
> > > > On Wed, 24 Nov 2010, Julia Lawall wrote:
> > > > 
> > > > > Thanks for your interest in Coccinelle.  I had thought that in the 
> > > > > special 
> > > > > case where the change is only on the variable that it would be able 
> > > > > to 
> > > > > make the transformation.  Ie, if the second rule were rather written 
> > > > > with:
> > > > > 
> > > > >   Packet
> > > > > - p
> > > > > + *p = SCMalloc(SIZE_OF_PACKET)
> > > > >   ;
> > > > > 
> > > > > But I see that this is not working either.  I will look into it and 
> > > > > hope 
> > > > > to find a solution today or tomorrow.
> > > > 
> > > > It turned out to be a little more subtle than expected.  I will hope to 
> > > > have a working solution soon.
> > > 
> > > Working on the same code modification, I did not found a way to treat
> > > the case where I've got multiple Packet variable.
> > > 
> > > If I use : 
> > >         @rule1@
> > >         identifier p;
> > >         identifier func;
> > >         identifier fdl;
> > >         @@
> > >         func(...) {
> > >         ...
> > >         Packet p;
> > >         <...
> > >         (
> > >         - p.fdl
> > >         + p->fdl
> > >         |
> > >         - &(p)
> > >         + p
> > >         )
> > >         ...>
> > >         }
> > > 
> > > on a file where there is :
> > >         Packet p1;
> > >         Packet p2;
> > > 
> > > then the modification is only done on p1.
> > > 
> > > Is there a way to treat all variables at once ?
> > 
> > Yes, it should work better if you move the <... up above the declaration 
> > of Packet.  You may also want to change it to <+... ...+> if you want to 
> > be sure there is at least one match.
> 
> Thanks ! It does the job for most of the patch but I've got the
> following problem. With the following cocci file
>         @rule2@
>         identifier p;
>         identifier func;
>         @@
>         func(...) {
>         <...
>         - Packet p;
>         + Packet *p = SCMalloc(SIZE_OF_PACKET);
>         + if (p == NULL)
>         +   return 0;
>         ...
>         + SCFree(p);
>         return ...;
>         ...>
>         }
> I encounter:
>         init_defs_builtins: /usr/share/coccinelle/standard.h
>         HANDLING: multi.c
>              
>         previous modification:
>         
>           <<< SCFree(rule2:p);
>         CONTEXT
>         According to environment 1:
>            rule2.p -> id p2
>         
>         current modification:
>         
>           <<< SCFree(rule2:p);
>         CONTEXT
>         According to environment 1:
>            rule2.p -> id p1
>         
>         Fatal error: exception Failure("rule2: already tagged token:
>         C code context
>         File "multi.c", line 94, column 4,  charpos = 2599
>             around = 'return', whole content =     return result;")
> 
> Is there a workaround ? I attach the related files to the mail for more
> convenience.

Oops, I forgot about that part.  Put ++ instead of + where you want an 
added line to be able to accumulate.

Is it OK with you for the NULL test to be mixed in with the declarations?  
If not, you can also use ++ to put it in front of the first statement.  
Concretely, I wrote the following, but I didn't get around to testing it, 
because I got stuck on the multidecl problem:

@rule2@
identifier p;
identifier func;
statement S;
@@
func(...) {
  <...
  Packet
- p                   // attempt to solve the multdecl problem
+ *p = SCMalloc(SIZE_OF_PACKET)
  ;
  ...                 // this will not match any statements
++if (p == NULL) return 0;
  S
  ...
++SCFree(p);
  return ...;
 ...>
}


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

Reply via email to