Coccinelle archive on lore.kernel.org
 help / color / Atom feed
* [Cocci] Problem writing simple patch
@ 2019-11-25 19:57 David Frey
  2019-11-25 21:10 ` Julia Lawall
  0 siblings, 1 reply; 6+ messages in thread
From: David Frey @ 2019-11-25 19:57 UTC (permalink / raw)
  To: cocci

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
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Cocci] Problem writing simple patch
  2019-11-25 19:57 [Cocci] Problem writing simple patch David Frey
@ 2019-11-25 21:10 ` Julia Lawall
  2019-11-26  7:24   ` [Cocci] Specifying conditional compilation with SmPL Markus Elfring
  2019-11-26 23:46   ` [Cocci] Problem writing simple patch David Frey
  0 siblings, 2 replies; 6+ messages in thread
From: Julia Lawall @ 2019-11-25 21:10 UTC (permalink / raw)
  To: David Frey; +Cc: cocci

On Mon, 25 Nov 2019, David Frey wrote:

> 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?

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
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Cocci] Specifying conditional compilation with SmPL
  2019-11-25 21:10 ` Julia Lawall
@ 2019-11-26  7:24   ` Markus Elfring
  2019-11-26  7:47     ` Julia Lawall
  2019-11-26 23:46   ` [Cocci] Problem writing simple patch David Frey
  1 sibling, 1 reply; 6+ messages in thread
From: Markus Elfring @ 2019-11-26  7:24 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci

> 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
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Cocci] Specifying conditional compilation with SmPL
  2019-11-26  7:24   ` [Cocci] Specifying conditional compilation with SmPL Markus Elfring
@ 2019-11-26  7:47     ` Julia Lawall
  0 siblings, 0 replies; 6+ messages in thread
From: Julia Lawall @ 2019-11-26  7:47 UTC (permalink / raw)
  To: Markus Elfring; +Cc: cocci



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


On Tue, 26 Nov 2019, Markus Elfring wrote:

> > 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);

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

>
> 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
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Cocci] Problem writing simple patch
  2019-11-25 21:10 ` Julia Lawall
  2019-11-26  7:24   ` [Cocci] Specifying conditional compilation with SmPL Markus Elfring
@ 2019-11-26 23:46   ` David Frey
  2019-11-27  6:00     ` Julia Lawall
  1 sibling, 1 reply; 6+ messages in thread
From: David Frey @ 2019-11-26 23:46 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci

On 11/25/2019 1:10 PM, Julia Lawall wrote:
> On Mon, 25 Nov 2019, David Frey wrote:
> 
>> 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?
> 
> 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
> 


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
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Cocci] Problem writing simple patch
  2019-11-26 23:46   ` [Cocci] Problem writing simple patch David Frey
@ 2019-11-27  6:00     ` Julia Lawall
  0 siblings, 0 replies; 6+ messages in thread
From: Julia Lawall @ 2019-11-27  6:00 UTC (permalink / raw)
  To: David Frey; +Cc: cocci



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


On Tue, 26 Nov 2019, David Frey wrote:

> On 11/25/2019 1:10 PM, Julia Lawall wrote:
> > On Mon, 25 Nov 2019, David Frey wrote:
> >
> >> 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?
> >
> > 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
> >
>
>
> 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.

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
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, back to index

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-25 19:57 [Cocci] Problem writing simple patch David Frey
2019-11-25 21:10 ` Julia Lawall
2019-11-26  7:24   ` [Cocci] Specifying conditional compilation with SmPL Markus Elfring
2019-11-26  7:47     ` Julia Lawall
2019-11-26 23:46   ` [Cocci] Problem writing simple patch David Frey
2019-11-27  6:00     ` Julia Lawall

Coccinelle archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/cocci/0 cocci/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 cocci cocci/ https://lore.kernel.org/cocci \
		cocci@systeme.lip6.fr
	public-inbox-index cocci

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/fr.lip6.systeme.cocci


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git