On Mon, 27 Feb 2012, ron minnich wrote:

I have a use for coccinelle that I have not seen described. I need to
"filter" out a set of functions from a linux driver, so as to create a
standalone driver to be run from user mode. I'd like to be able to
point spatch at a driver directory and, to start, create output that
consists of only a single C function (as a test) and then, over time,
I want to extract more and more functions from the driver and apply
transformations to them to create the standalone code.

Here's an example, from drivers/gpu/drm/i915/intel_bios.c: I'd like to
run a coccinelle spatch that would 'extract' the function
intel_setup_bios
and apply a transformation to the dev_priv assignment to have it set
up from a function. I don't want anything else out of that file (to
start) and the includes need to be deleted as well.

Here is a quick hack that requires a little scripting on the side.

Use the following semantic patch:

@@
identifier virtual.picked;
@@

- picked(...) {...}

Run it as follows, to extract the function named two:

spatch -sp_file getfn.cocci getfn.c -D picked=two -U 0 > res

Now suppose you want to create the file res.c. Modify res to make the +++ line be res.c and create an empty res.c file.

Reverse apply the patch in res to res.c: patch -R < res

Now res.c contains your function.

If you want to make some changes to that function, then you may as well just make another semantic patch for that.


The other approach would be to try to remove everything else besides the function of interest in your file. But some includes and other preprocessor definitions, and some unparseable code, might get left over.

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

Reply via email to