On Fri, 12 Nov 2010, Andriy Gapon wrote:

> 
> I wonder if coccinelle can help me with the following issue.
> There is a type, let's call it sometype_t, that is typedef-ed to int.
> There is an API which uses that type consistently to provide for possible 
> future
> extensions.  But that there are many lax users of that API which frequently 
> use
> int instead of sometype_t.
> For example, sometimes a sometype_t variable is assigned with int value, or
> conversely a sometype_t value is assigned to int variable, or int value is 
> passed
> in a function parameter where sometype_t is expected.
> 
> I wonder if I could use the power of coccinelle to easily find and perhaps 
> even
> fix such lax type handling.

Where is sometype_t defined?  If it is in a header file and your code is 
in a C file, it is easy.  You have to just ensure that Coccinelle does not 
see the definition in the header file, by giving it the option 
-no_includes.  You can then use a semantic match like the following to 
find the problem:

@@
int x;
@@

*x = foo()

You can also give the option -include_headers, to get the header files to 
be treated independently of the C files that include them.

If some of the bad code might be in files where the typedef is visible, 
then it will realize that sometype_t is the same as int, and sometype_t 
things will match the above rule.  To be safer, you can do the following.

@r@
typedef sometype_t;
sometype_t x;
int y;
@@

(
x = foo()
|
*y = foo()
)

Disjunctions are treated from top to bottom, so y will only be matched 
when it is an int, but not a sometype_t.  

In the case where the int expression is a local variable, you can also 
change the type:

@@
identifier f,x;
@@

f(...) {
  ... when any
- int x;
+ sometype_t x;
  <+... x = foo() ...+>
}

You would want to look very carefully to see if this is what is really 
wanted, though.  In this case, there is no danger with the typedef.  
Typedefs are only taken into account when considering the type of an 
expression, not the explicit type in a variable declaration.  This rule 
will also not do anything when x is declared with some other local 
variables.  There is no way to break such a declaration apart and only 
change the type of x.

I hope that helps.  If you have any further questions, please don't 
hesitate to ask.

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

Reply via email to