Skip to contentSkip to conversation

Problem writing simple patch

Cocci David Frey 3 participants 6 messages Nov 25, 2019 – Nov 27, 2019

lore
Share & tools
lore

David Frey

author
#0 lore raw

Hi,

I'm trying to write a .cocci file to transform all calls to a function "f(ex)" to something like this:

#ifdef USE_F
f(ex)
#else
g(ex)
#endif

The function has this signature:
bool f(int x);

This is the patch that I tried to use:
@@
expression ex;
@@
+#ifdef USE_F
f(ex)
+#else
+g(ex)
+#endif

This is the result of running it:

$ spatch --show-c --sp-file test.cocci test.c
init_defs_builtins: /usr/bin/../lib/coccinelle/standard.h
plus: parse error:
  File "test.cocci", line 7, column 1, charpos = 50
  around = 'g',
  whole content = +g(ex)

What is wrong with the patch above?

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

#1 lore raw
Quoted 26 lines · David Frey

Hi,

I'm trying to write a .cocci file to transform all calls to a function "f(ex)" to something like this:

#ifdef USE_F
f(ex)
#else
g(ex)
#endif

The function has this signature:
bool f(int x);

This is the patch that I tried to use:
@@
expression ex;
@@

(quoted diff) +4 -0
+#ifdef USE_F
 f(ex)
+#else
+g(ex)
+#endif

This is the result of running it:

Quoted 8 lines
$ spatch --show-c --sp-file test.cocci test.c
init_defs_builtins: /usr/bin/../lib/coccinelle/standard.h
plus: parse error:
  File "test.cocci", line 7, column 1, charpos = 50
  around = 'g',
  whole content = +g(ex)

What is wrong with the patch above?

Coccinelle doesn't currently support adding ifdefs on expressions, only on statements.

You could try for some typical usage contexts, like

+ifdef...
x = f(ex);
+#else
+x = g(ex);
+#endif

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

#2 lore raw

Re: [Cocci] Specifying conditional compilation with SmPL

Quoted 2 lines

Coccinelle doesn't currently support adding ifdefs on expressions, only on statements.

Can the following transformation approach ever work

@adjustment2@
expression x;
@@
+#ifdef USE_F
f
+#else
+g
+#endif
(x);

in addition to this code variant for the semantic patch language?

@adjustment1@
expression x;
@@
+#ifdef USE_F
f(x);
+#else
+g(x);
+#endif

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

#3 lore raw

Re: [Cocci] Specifying conditional compilation with SmPL

--- Please note the new email address ---

Quoted 8 lines · Markus Elfring
Quoted 2 lines

Coccinelle doesn't currently support adding ifdefs on expressions, only on statements.

Can the following transformation approach ever work

@adjustment2@
expression x;
@@

Review

Quoted 6 lines
(quoted diff) +4 -0
+#ifdef USE_F
 f
+#else
+g
+#endif
 (x);

Obviously, it what was asked for would work, this would work too. We're not going to make a special case to exclude expressions that are identifiers. But the generated code would look horrible.

The reason for putting an ifdef on an expression would be that the expression could appear in other contexts, such as conditional tests, not to minimize the number of characters in the file.

julia

Quoted 13 lines

in addition to this code variant for the semantic patch language?

@adjustment1@
expression x;
@@

(quoted diff) +4 -0
+#ifdef USE_F
 f(x);
+#else
+g(x);
+#endif
Quoted 3 lines

Regards,
Markus

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

author
#4 lore raw
In reply to Julia Lawall
Quoted 48 lines · Julia Lawall
Quoted 26 lines · David Frey

Hi,

I'm trying to write a .cocci file to transform all calls to a function "f(ex)" to something like this:

#ifdef USE_F
f(ex)
#else
g(ex)
#endif

The function has this signature:
bool f(int x);

This is the patch that I tried to use:
@@
expression ex;
@@

(quoted diff) +4 -0
+#ifdef USE_F
 f(ex)
+#else
+g(ex)
+#endif

This is the result of running it:

Quoted 8 lines
$ spatch --show-c --sp-file test.cocci test.c
init_defs_builtins: /usr/bin/../lib/coccinelle/standard.h
plus: parse error:
  File "test.cocci", line 7, column 1, charpos = 50
  around = 'g',
  whole content = +g(ex)

What is wrong with the patch above?

Coccinelle doesn't currently support adding ifdefs on expressions, only on statements.

You could try for some typical usage contexts, like

+ifdef...
x = f(ex);

(quoted diff) +3 -0
+#else
+x = g(ex);
+#endif
Quoted 2 lines

julia

Hi Julia,

Thanks for your explanation and your suggestion. I ended up creating a new header that was like this:

#ifdef SOMETHING
#define foo_backport_x(_arg) bar_x(_arg)
#define foo_backport_y(_arg) bar_y(_arg)
#else
#define foo_backport_x(_arg) foo_x(_arg)
#define foo_backport_y(_arg) foo_y(_arg)
#endif

and then I defined coccinelle rules to change:
foo_x -> foo_backport_x
foo_y -> foo_backport_y

It's not the most elegant solution, but it works.

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

#5 lore raw

--- Please note the new email address ---

Quoted 66 lines · David Frey
Quoted 48 lines · Julia Lawall
Quoted 26 lines · David Frey

Hi,

I'm trying to write a .cocci file to transform all calls to a function "f(ex)" to something like this:

#ifdef USE_F
f(ex)
#else
g(ex)
#endif

The function has this signature:
bool f(int x);

This is the patch that I tried to use:
@@
expression ex;
@@

(quoted diff) +4 -0
+#ifdef USE_F
 f(ex)
+#else
+g(ex)
+#endif

This is the result of running it:

Quoted 8 lines
$ spatch --show-c --sp-file test.cocci test.c
init_defs_builtins: /usr/bin/../lib/coccinelle/standard.h
plus: parse error:
  File "test.cocci", line 7, column 1, charpos = 50
  around = 'g',
  whole content = +g(ex)

What is wrong with the patch above?

Coccinelle doesn't currently support adding ifdefs on expressions, only on statements.

You could try for some typical usage contexts, like

+ifdef...
x = f(ex);

(quoted diff) +3 -0
+#else
+x = g(ex);
+#endif
Quoted 2 lines

julia

Hi Julia,

Thanks for your explanation and your suggestion. I ended up creating a new header that was like this:

#ifdef SOMETHING
#define foo_backport_x(_arg) bar_x(_arg)
#define foo_backport_y(_arg) bar_y(_arg)
#else
#define foo_backport_x(_arg) foo_x(_arg)
#define foo_backport_y(_arg) foo_y(_arg)
#endif

Quoted 5 lines

and then I defined coccinelle rules to change:
foo_x -> foo_backport_x
foo_y -> foo_backport_y

It's not the most elegant solution, but it works.

Persoally, I would find lots of ifdefs around expressions in the code rather ugly too, so I'm not sure that it is such a bad solution, although the name is not pretty...

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