On Fri, 9 Jul 2010, Nicolas Palix wrote:

> Hi,
> 
> On Friday 09 July 2010 11:41:04 Michael Stefaniuc wrote:
> > Julia Lawall wrote:
> > > On Fri, 9 Jul 2010, Richard W.M. Jones wrote:
> > > 
> > >> I'd like to use coccinelle to change the namespace of all public
> > >> identifiers (functions in particular) in a program.  eg: foo_xxx ->
> > >> bar_xxx for all substrings 'xxx'.
> > >>
> > >> It seems this should be easy with coccinelle but I couldn't work out
> > >> how to do it.  This is about as far as I got:
> > >>
> > >> @@
> > >> identifier f ~= "foo_.*";
> > >> @@
> > >>
> > >> - f
> > >> + ## what to write here?? ##
> > > 
> > > There is the notion of a fresh identifier, which is described here:
> > > 
> > > http://cocci.ekstranet.diku.dk/wiki/doku.php?id=metavariables#fresh_identifier
> > > 
> > > The problem is that I don't think there is any way to reference what was 
> > > matched by the .* part of the regular expression.  So unfortunately I 
> > > think it is not currently possible.
> > Well he could do it in two steps:
> > 1.) Use the regexp and a python rule to generate a cocci file
> > 2.) Run the generated cocci file.
> > 
> 
> Indeed, that is currently the best way to go !
> 
> I will add group support for the regexp and fresh identifier,
> hopefully soon... It will help in that case.

One solution is to use virtual rules and identifiers.  It would be 
something like the following, named sp.cocci:

virtual have_new_name

@r depends on !have_new_name@
identifier x ~= "your regexp"
@@

x

@script:python@
x << r.x;
@@

new_name = ... some code to calculate the new name ...
print "spatch -sp_file sp.cocci -D have_new_name -D old_name=%s -D new_name=%s 
-dir directory_to_work_on" % (x,new_name)

@@
identifier virtual.old_name;
identifier virtual.new_name;
@@

- old_name
+ new_name

Running the semantic patch once will give a file containing lots of calls 
to spatch, and running that file will generate the required patches.

The only thing is that this only works if the only deciding factor about 
where to make the change is the name of the identifier.  If there is 
something about the context, eg the name of the containing function, that 
would also have to be collected and passed to the next call to spatch 
using virtual identifiers.

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

Reply via email to