[Cocci] Coccinelle errors during parsing of C-macros
ext-yingtong.li at nokia.com
ext-yingtong.li at nokia.com
Tue Apr 28 06:25:51 CEST 2009
As per the tests I made, you are right, coccinelle is able to recognize the type
defined by C typedef. However, if there are more than 1 typedef to define a same
type(they are wrapped by #ifdef, so only one typedef is alive when compile), coccinelle
will miss the type.
I think to only recognize the last typedef will actually cause some unexpected parsing
result. In our case, we are only interested in error detection, hence, to analyze the preprocess
file is ok.
Regarding the macros, seems coccinelle can not generate the control flow no matter
if the macro is defined in standard.h or not.
A simple example is:
//this macro is defined in standard.h
typedef int INT;
typedef long INT;
INT a, b, c;
a = b + c;
Coccinelle can not parse this code and recognize INT as int or long.
We are wondering if it is possible to generate the control flow according to the defined macro?
And if it's possible, a good idea is to define/undefine the macro, which is used for the control flow,
in the command line.
Such as: >spatch -sp_file xxx.cocci -D <macros needed> -UD <macros unneeded> xxx.c
S30 Application SW, Nokia, Beijing
>From: ext Yoann Padioleau [mailto:padator at wanadoo.fr]
>Sent: Friday, April 24, 2009 9:35 AM
>To: Li Yingtong (EXT-Elektrobit/Beijing)
>Cc: cocci at diku.dk
>Subject: Re: [Cocci] Coccinelle errors during parsing of C-macros
>Yoann Padioleau <padator at wanadoo.fr> writes:
>>> 2. If the macro is defined to represent data type, e.g. int
>>> and then use the macro to declare variables, coccinelle can not
>>> recognize them, and an error msg is shown as well:
>>> "Unsupported form of #define: File 'xxx.h', line xxx "
>>> For instance, we defined INT16 to indicate int, and define all the
>>> int variables as INT16, but the .cocci file can not recognize the
>>> INT16 type as a int.
>>> C file, test.c:
>>> #define INT16 int
>>> INT16 a, b, c;
>>> c = a + b;
>>> Cocci file, test.cocci:
>>> int E1, E2, E3;
>>> - E1 = E2 + E3;
>>> +E1 = E2 - E3;
>>> Execute "spatch -sp_file test.cocci test.c", nothing
>changed but an
>>> error reported. But if change type of a,b and c to int, then the
>>> replacement happens.
>> Yes, this is because as julia said coccinelle tries to parse
>> as is and even if we try to handle most macros definitions, parts of
>> coccinelle don't. For instance here our type checker does
>> the information in the macro.
>> We will try to handle your case by extending the typechecker to be
>> aware also of certain kind of macro definitions involving type alias.
>I've done that. Your specific example should now work in the
>next release of coccinelle. Nevertheless the typechecker now
>takes into account only the last definitions of the macro so
>if your file contains:
> #ifdef XXX
> #define INT16 int
> #define INT16 short
> INT16 a, b, c;
> c = a + b;
>then your semantic patch will fail to apply as 'short' will be
>considered the last definitions of INT16.
More information about the Cocci