SV: SV: [Cocci] adding #include's

Nicolas Palix npalix at diku.dk
Thu Nov 5 12:26:23 CET 2009


> 
> So what missing now is a rule to insert #include <foo.h> in a file without any #include statements. For my real world tasks I can live without this.

I am not sure there is a simple solution. A workaround is to use the Python API and
the cocci.include_match(False) feature.

In the first rule, you match on a function/variable declaration and keep its position.

@first_fct@
type T;
identifier f;
position p;
@@

T f at p (...) {...}

@first_var@
type T;
identifier v;
position p;
@@

T v at p;

These rules will match any fct/var declaration but you could filter with python to select
only the first one, using p[0].line, and discard the other ones with include_match(False).

To select only the first one, you will need a global variable in Python. It can be declared and initial to 0 in the @initialize:python@ section at the very beginning of the cocci file.

Finally, write two cocci rules to add the #include either before the variable declaration
or the function declaration and ensure that the position is the same.

You are welcome to post your complete solution in our Wiki.
http://cocci.ekstranet.diku.dk/wiki/doku.php

Don't hesitate if something is not clear.

Regards.

> 
> Regards,
> Andreas Bach Aaen


-- 
Nicolas Palix


More information about the Cocci mailing list