[Cocci] Macro parsing without ;

Julia Lawall julia at diku.dk
Fri Nov 27 16:18:58 CET 2009


I think you should put:

#define HEADER(x) MACROSTATEMENT

in your standard.h file.

julia


On Fri, 27 Nov 2009, Andreas.Aaen at tieto.com wrote:

> Hi,
> I have a huge code base with code like this:
> 
> -------------
> #define HEADER(head) int head ;
> struct  mes {
>   HEADER(head)
>     int        a ;
>     int        b ;
> };
> typedef struct
> {
>   HEADER(head)
>   int  c;
>   int d;
> } Umes;
> 
> int main() {
>   struct mes g;
>   g.a = 5;
> }
> ------------
> 
> this c code compiles file with gcc -ansi -pedantic, but cocci -parse_c doesn't like the HEADER(head) macro.
> 
> I get output like this:
> ----------
> spatch -parse_c  test/testhead.c
> init_defs_builtins: /usr/share/coccinelle/standard.h
> PARSING: test/testhead.c
> (ONCE) CPP-MACRO: found macro with param noptvirg: HEADER
> ERROR-RECOV: found sync '}' at line 7
> ERROR-RECOV: found sync bis, eating } and ;
> parsing pass2: try again
> ERROR-RECOV: found sync '}' at line 7
> ERROR-RECOV: found sync bis, eating } and ;
> parsing pass3: try again
> (ONCE) CPP-MACRO: found known macro = HEADER
> ERROR-RECOV: found sync '}' at line 14
> ERROR-RECOV: found sync bis, eating ident, }, and ;
> parsing pass2: try again
> ERROR-RECOV: found sync '}' at line 14
> ERROR-RECOV: found sync bis, eating ident, }, and ;
> parse error
>  = File "test/testhead.c", line 11, column 2,  charpos = 124
>     around = 'HEADER', whole content =   HEADER(head)
> badcount: 7
> bad: };
> bad:
> bad: typedef struct
> bad: {
> BAD:!!!!!   HEADER(head)
> bad:   int  c;
> bad:   int d;
> bad: } Umes;
> passed:HEADER ( head )
> passed:( head )
> -----------------------------------------------------------------------
> maybe 10 most problematic tokens
> -----------------------------------------------------------------------
> HEADER: present in 1 parsing errors
> example:
>        typedef struct
>        {
>          HEADER(head)
>          int  c;
> -----------------------------------------------------------------------
> NB total files = 1; perfect = 0; pbs = 1; timeout = 0; =========> 0%
> nb good = 12,  nb passed = 2 =========> 16.666667% passed
> nb good = 12,  nb bad = 7 =========> 63.157895% good
> ---------------------
> 
> The idea in the code base is of cause that sometimes you wan't the #define HEADER(head) macro to be empty and don't left over a ";". If use the macro as:
> HEADER(head);
> instead of
> HEADER(head)
> 
> Then gcc will warn you:
> test/testhead.c:4: warning: extra semicolon in struct or union specified
> test/testhead.c:11: warning: extra semicolon in struct or union specified
> 
> So how do I work arround the limitation in the cocci c parser?
> 


More information about the Cocci mailing list