On Thu, 30 Aug 2012, Dmitry Antipov wrote:

Hello,

Is it possible to use coccinelle to find the structure/union
members which aren't accessed explicitly, via '.' or '->'?
For example, if I have:

struct foo
{
int x;
int y;
int z;
};

and neither X->z nor Y.z (where X is of type 'struct foo *'
and Y is of type 'struct foo') is used, I would like to
get a message about 'z' from type 'struct foo' is never used.

If this isn't possible just now, consider this as the feature
request for the future versions :-).

An implementation is below. It is a rather standard pattern: use position variables to mark the things that have a property, and then report errors for everything that is not marked.

The rule ty selects each field in the structure, one by one.

The rules r and rr then take care of the . case and the rules s and ss take care of the -> case. The rule r finds some object x from which the field is referenced. The rule rr then puts a position variable on that field, in the case that some such reference was found (depends on r). The rules s and ss are similar. Now the bad fields are the ones that are not marked by either a rr or ss position variable. These are found in the last rule.

The limitation is that the structure has to be in the same file as the references, or in a header file that Coccinelle finds when it is treating the C file. You could use the argument --all-includes and appropriate -I arguments to get all directly mentioned #include files to be included. The argument --recursive-includes includes files that are included by other files, but it may make things extremely slow, if there are many such files.

julia


@ty@
identifier i,fld;
type T;
position p;
@@

struct i@p {
...
T fld;
...
};

@r@
identifier ty.i;
struct i x;
identifier ty.fld;
@@

x.fld

@rr depends on r@
identifier ty.i,ty.fld;
type ty.T;
position p1,ty.p;
@@

struct i@p {
...
T fld@p1;
...
};

@s@
identifier ty.i;
struct i *x;
identifier ty.fld;
@@

x->fld

@ss depends on s@
identifier ty.i,ty.fld;
type ty.T;
position p2,ty.p;
@@

struct i@p {
...
T fld@p2;
...
};

@@
identifier ty.i,ty.fld;
type ty.T;
position bad!={rr.p1,ss.p2},ty.p;
@@

struct i@p {
...
*T fld@bad;
...
};

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

Reply via email to