Skip to contentSkip to conversation

Does coccinelle support non-standard C?

Cocci Christoph Böhmwalder 2 participants 5 messages May 22, 2019

lore
Share & tools
lore
#0 lore raw

Hi,

Consider the following snippet of C:

#define f() ({ puts("hello"); 0; })
int main()

{
    printf("%d\n", f());
}

It uses an expression statement in the definition of f, which is a gcc extension.

Now let's try to write a semantic patch to generate something like this:

@@
@@
- cocci_replace_this()
+ ({ puts("hello"); 0; })

With the C code as:

#define f() cocci_replace_this()
int main()

{
    printf("%d\n", f());
}

This prompts the following error message from spatch:

$ spatch --sp-file test.cocci test.c
init_defs_builtins: /usr/lib/coccinelle/standard.h
32 33
Fatal error: exception Failure("plus: parse error: \n = File \"test.cocci\", line 4, column 3,  charpos = 32\n    around = '{', whole content = + ({ puts(\"hello\"); 0; })\n")

Now here's my question: this obviously implies that coccinelle doesn't support expression statements (or any other non-standard C for that matter). Can I still somehow tell spatch to just replace it with this string of text instead of trying to parse it as C?

Thanks.

--
Regards,
Christoph
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#1 lore raw
Quoted 45 lines · Christoph Böhmwalder

Hi,

Consider the following snippet of C:

#define f() ({ puts("hello"); 0; })
int main()

{
    printf("%d\n", f());
}

It uses an expression statement in the definition of f, which is a gcc extension.

Now let's try to write a semantic patch to generate something like this:

@@
@@
- cocci_replace_this()
+ ({ puts("hello"); 0; })

With the C code as:

#define f() cocci_replace_this()
int main()

{
    printf("%d\n", f());
}

This prompts the following error message from spatch:

$ spatch --sp-file test.cocci test.c
init_defs_builtins: /usr/lib/coccinelle/standard.h
32 33
Fatal error: exception Failure("plus: parse error: \n = File \"test.cocci\",
line 4, column 3,  charpos = 32\n    around = '{', whole content = + ({
puts(\"hello\"); 0; })\n")
Quoted 2 lines

Now here's my question: this obviously implies that coccinelle doesn't support expression statements (or any other non-standard C for that matter).

The C parser should parse such statements. I think it is indeed likely that the semantic patch parser doesn't support them. I wouldn't go as far as saying that the semantic patch parser doesn't support any non-standard C. It pretty much supports what people have asked for, when there didn't seem to be a workaround.

Quoted 3 lines

Can I
still somehow tell spatch to just replace it with this string of text instead of trying to parse it as C?

Is it always exactly this string that you want to add?

julia

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#3 lore raw
Quoted 5 lines · Christoph Böhmwalder
Quoted 1 line · Julia Lawall

Is it always exactly this string that you want to add?

In my case, yes. I don't have any metavariable substitutions, etc; it's always the same constant code.

You can do the following

@script:python b@
x;
@@

coccinelle.x = "({...})" <--- The code you want

@@
identifier b.i;
@@
- cocci_replace_this()
+ i

julia

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#4 lore raw
Quoted 18 lines · Julia Lawall
Quoted 5 lines · Christoph Böhmwalder
Quoted 1 line · Julia Lawall

Is it always exactly this string that you want to add?

In my case, yes. I don't have any metavariable substitutions, etc; it's always the same constant code.

You can do the following

@script:python b@
x;
@@

coccinelle.x = "({...})" <--- The code you want

Quoted 8 lines

@@
identifier b.i;
@@
- cocci_replace_this()
+ i

julia

Cool, I guess that works. Thanks for the prompt reply and your help!

--
Regards,
Christoph
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci