[Cocci] spatch defect: C90 initializers?

Julia Lawall julia at diku.dk
Thu Sep 9 09:46:46 CEST 2010


On Wed, 8 Sep 2010, Joe Perches wrote:

> On Thu, 2010-09-09 at 08:28 +0200, Julia Lawall wrote:
> > you could also just add a second rule to your semantic match:
> > 
> > @@
> > identifier I;
> > @@
> > 
> > * struct pci_driver I { .name = ..., };
> 
> That doesn't seem to work.

My sloppy C.  There should of course be an = before the {.  But also for 
some reason it doesn't like a * on the ...  This is probably a parsing 
problem that I can fix.  The following works:

@@
expression E;
@@

* struct pci_driver I = { .name = E, };


> $ cat find_pci_name.cocci
> @@
> identifier I;
> @@
> 
> * struct pci_driver I { .name = ..., }
> $ cat foo.c
> #include <linux/kernel.h>
> #include <linux/pci.h>
> 
> static struct pci_driver foo = {
> 	.name = "name",
> };
> 
> static struct pci_driver bar = {
> 	NULL, "name"
> };
> $ spatch -version
> spatch version 0.2.2 with Python support
> $ spatch -sp_file find_pci_name.cocci foo.c
> init_defs_builtins: /usr/local/share/coccinelle/standard.h
> 43 44
> Fatal error: exception Failure("minus: parse error: 
>  = File "find_pci_name.cocci", line 5, column 22,  charpos = 43
>     around = '{', whole content = * struct pci_driver I { .name = ..., };
> ")
> $
> 
> How would it find a non-c90 initializer like bar?

In principle, you would have to find the type definition, find the offset 
of the desired field in that type definition, and then find the value at 
that offset in the initializer.  Finding the type definition is only 
possible if it is directly included by the C file.  At the moment, there 
is unfortunately nothing to support either finding the offset of a field 
in a structure definition or finding the offset of a value in a 
declaration of the form { a, b, c }.  Howevere, I am currently working on 
improving the support for initializers of the form { a, b, c }, and I 
think it would be easy to add that at the same time.  With that in place, 
you could find the offset of the field you are interested in in pci_driver 
byhand, and specify that offset explicitly in the semantic patch.  It 
would be something like the following:

@@
expression list[5] E;
expression v;
@@

{ E,
* v,
  ... }

This gives the value for the 6th field.

julia


More information about the Cocci mailing list