On Wed, 12 Dec 2018, Max wrote:
> Hi.
>
> I've problem replacing parameterized #define using spatch.
>
> For example, in test.c I have following defines:
>
> ...
>
> #define old_junk(a, b, c) if (c) \
>
> printf(a); \
>
> else \
>
> { printf(b); }
>
> #define new_stuff(a, b, b) my_func(a, b, c)
>
> I would like to replace all the entries of old_junk(x, y, z); with
> new_stuff(x, y, z);
>
> I've tried following spatch:
>
> @@
> identifier old_junk, new_stuff, abort;
You have declared new_stuff and abort as metavariables, meaning that
Coccinelle has to figure out what they should be. But it has no way of
figuring that out in this case. I think that you can just drop the above
line completely, since you say you really want to replace a call to
old_junk by a call to new_stuff. If you wanted to replace all calls to
three argument functions by a call to new_stuff, then you would want
old_junk to be a metavariable, but that is probably not useful.
> identifier a, b, c;
Are you sure that a b and c will only match identifiers, like x. If they
may match expressions, like 2 + 3, then you would want expression in these
cases.
Actually, the fact that old_junk and new_stuff are defined as macros is
irrelevant for this rule. Coccinelle will just look at the calls. It
doesn't expland macros, so it doesn't know whether the call is to a macr
or to a function.
julia
> @@
> - old_junk(a, b, c);
> + if (new_stuff(a, b, c))
>
> + abort();
>
> Which used as "spatch --in-place --sp-file my.spatch tests/test.c"
>
> Unfortunately all I got is "metavariable new_stuff not used in the - or
> context code".
>
> I've tried using "function new_stuff" instead and got another failure:
> "MetaFunc, need more semantic info about id".
>
> Which is the right way to make this sort of replacements?
>
> I'd be happy to get points to the docs which clarify this part as I was
> unable to find anything relevant on
> http://coccinelle.lip6.fr/documentation.php
>
> I went over tutorial slides as well and it feels like I'm missing
> something obvious in here.
>
> I'm using spatch 1.0.4 in latest ubuntu.
>
> --
> - Max Suraev <[email protected]> http://www.sysmocom.de/
> =======================================================================
> * sysmocom - systems for mobile communications GmbH
> * Alt-Moabit 93
> * 10559 Berlin, Germany
> * Sitz / Registered office: Berlin, HRB 134158 B
> * Geschaeftsfuehrer / Managing Directors: Harald Welte
>
> _______________________________________________
> Cocci mailing list
> [email protected]
> https://systeme.lip6.fr/mailman/listinfo/cocci
>_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci