Coccinelle archive on lore.kernel.org
 help / color / Atom feed
* [Cocci] How to concatenate identifier with text?
@ 2019-05-24 19:13 Timur Tabi
  2019-05-24 19:36 ` Timur Tabi
  2019-05-24 19:38 ` Julia Lawall
  0 siblings, 2 replies; 8+ messages in thread
From: Timur Tabi @ 2019-05-24 19:13 UTC (permalink / raw)
  To: cocci

@@
identifier func;
@@
func(...) {
<+...
     .... stuff
+   goto func_exit;
+}
...+>
}

So if I have this code:

void myfunc(int x)
{
}

I want it to look like this:

void myfunc(int x)
{
    goto myfunc_exit;
}

My problem is that cocci takes "func_exit" literally.  I tried
func##_exit, but that didn't work.  How do I make this work?
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] How to concatenate identifier with text?
  2019-05-24 19:13 [Cocci] How to concatenate identifier with text? Timur Tabi
@ 2019-05-24 19:36 ` Timur Tabi
  2019-05-24 19:38   ` Julia Lawall
  2019-05-24 19:38 ` Julia Lawall
  1 sibling, 1 reply; 8+ messages in thread
From: Timur Tabi @ 2019-05-24 19:36 UTC (permalink / raw)
  To: Timur Tabi; +Cc: cocci

On Fri, May 24, 2019 at 2:13 PM Timur Tabi <timur@kernel.org> wrote:

> void myfunc(int x)
> {
>     goto myfunc_exit;
> }
>
> My problem is that cocci takes "func_exit" literally.  I tried
> func##_exit, but that didn't work.  How do I make this work?

I figured it out:

@@
identifier func;
fresh identifier label = func ## "_exit";
@@
func(...) {
<+...
+    goto label;
...+>
}
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] How to concatenate identifier with text?
  2019-05-24 19:13 [Cocci] How to concatenate identifier with text? Timur Tabi
  2019-05-24 19:36 ` Timur Tabi
@ 2019-05-24 19:38 ` Julia Lawall
  1 sibling, 0 replies; 8+ messages in thread
From: Julia Lawall @ 2019-05-24 19:38 UTC (permalink / raw)
  To: Timur Tabi; +Cc: cocci



On Fri, 24 May 2019, Timur Tabi wrote:

> @@
> identifier func;
> @@
> func(...) {
> <+...
>      .... stuff
> +   goto func_exit;
> +}
> ...+>
> }
>
> So if I have this code:
>
> void myfunc(int x)
> {
> }
>
> I want it to look like this:
>
> void myfunc(int x)
> {
>     goto myfunc_exit;
> }
>
> My problem is that cocci takes "func_exit" literally.  I tried
> func##_exit, but that didn't work.  How do I make this work?

Python.  You need one rule to collect the func name, then a python rule to
create the new identifier you want, and then a third rule to do the
transformation.  See coccinelle/demos/pythontococci.cocci

julia
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] How to concatenate identifier with text?
  2019-05-24 19:36 ` Timur Tabi
@ 2019-05-24 19:38   ` Julia Lawall
  2019-06-11 21:51     ` Timur Tabi
  0 siblings, 1 reply; 8+ messages in thread
From: Julia Lawall @ 2019-05-24 19:38 UTC (permalink / raw)
  To: Timur Tabi; +Cc: cocci



On Fri, 24 May 2019, Timur Tabi wrote:

> On Fri, May 24, 2019 at 2:13 PM Timur Tabi <timur@kernel.org> wrote:
>
> > void myfunc(int x)
> > {
> >     goto myfunc_exit;
> > }
> >
> > My problem is that cocci takes "func_exit" literally.  I tried
> > func##_exit, but that didn't work.  How do I make this work?
>
> I figured it out:
>
> @@
> identifier func;
> fresh identifier label = func ## "_exit";

Yes, that works too.  Simpler than my suggestion.

julia

> @@
> func(...) {
> <+...
> +    goto label;
> ...+>
> }
> _______________________________________________
> Cocci mailing list
> Cocci@systeme.lip6.fr
> https://systeme.lip6.fr/mailman/listinfo/cocci
>
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] How to concatenate identifier with text?
  2019-05-24 19:38   ` Julia Lawall
@ 2019-06-11 21:51     ` Timur Tabi
  2019-06-11 21:58       ` Timur Tabi
  2019-06-12  5:25       ` [Cocci] How to concatenate identifier with text? Julia Lawall
  0 siblings, 2 replies; 8+ messages in thread
From: Timur Tabi @ 2019-06-11 21:51 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci, Timur Tabi

On Fri, May 24, 2019 at 2:38 PM Julia Lawall <julia.lawall@lip6.fr> wrote:
> On Fri, 24 May 2019, Timur Tabi wrote:
> > I figured it out:
> >
> > @@
> > identifier func;
> > fresh identifier label = func ## "_exit";
>
> Yes, that works too.  Simpler than my suggestion.

I managed to break it somehow.  I suspect I ran into some other quirk
of coccinelle.  I'm trying to convert that looks like this:

x = MACRO1(stuff);
if (x == NULL)
{
  BREAKPOINT();
  status = ERROR;
  goto myfunc_exit;
}

into:
x = MACRO1(stuff);
CHECK_OR_BP_AND_GOTO(x == NULL, status, ERROR, _exit);

I thought this would work:

@@
type T;
expression y, z;
identifier x, s;
identifier func;
fresh identifier label = func ## "_exit";
@@
func(...) {
<+...
 x =
 (T)
 \(MACRO1\|MACRO2\)
 (...);
-if (y)
-{
-      BREAKPOINT();
-      s = z;
-      goto label;
-}
+CHECK_OR_BP_AND_GOTO(y, s, z, _exit);
...
 label:
...+>
}

but I get this error:

18: unexpected use of a fresh identifier label

What am I missing?
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] How to concatenate identifier with text?
  2019-06-11 21:51     ` Timur Tabi
@ 2019-06-11 21:58       ` Timur Tabi
  2019-06-12  7:12         ` [Cocci] Wider usage of “fresh identifiers”? Markus Elfring
  2019-06-12  5:25       ` [Cocci] How to concatenate identifier with text? Julia Lawall
  1 sibling, 1 reply; 8+ messages in thread
From: Timur Tabi @ 2019-06-11 21:58 UTC (permalink / raw)
  To: Timur Tabi; +Cc: cocci

On Tue, Jun 11, 2019 at 4:51 PM Timur Tabi <timur@kernel.org> wrote:
> but I get this error:
>
> 18: unexpected use of a fresh identifier label
>
> What am I missing?

Apparently I'm missing this obscure line in the documentation:

"Fresh identifier metavariables must only be used in + code."

Any suggestions on how to work around this, other than use Python?
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] How to concatenate identifier with text?
  2019-06-11 21:51     ` Timur Tabi
  2019-06-11 21:58       ` Timur Tabi
@ 2019-06-12  5:25       ` Julia Lawall
  1 sibling, 0 replies; 8+ messages in thread
From: Julia Lawall @ 2019-06-12  5:25 UTC (permalink / raw)
  To: Timur Tabi; +Cc: cocci



On Tue, 11 Jun 2019, Timur Tabi wrote:

> On Fri, May 24, 2019 at 2:38 PM Julia Lawall <julia.lawall@lip6.fr> wrote:
> > On Fri, 24 May 2019, Timur Tabi wrote:
> > > I figured it out:
> > >
> > > @@
> > > identifier func;
> > > fresh identifier label = func ## "_exit";
> >
> > Yes, that works too.  Simpler than my suggestion.
>
> I managed to break it somehow.  I suspect I ran into some other quirk
> of coccinelle.  I'm trying to convert that looks like this:
>
> x = MACRO1(stuff);
> if (x == NULL)
> {
>   BREAKPOINT();
>   status = ERROR;
>   goto myfunc_exit;
> }
>
> into:
> x = MACRO1(stuff);
> CHECK_OR_BP_AND_GOTO(x == NULL, status, ERROR, _exit);
>
> I thought this would work:
>
> @@
> type T;
> expression y, z;
> identifier x, s;
> identifier func;
> fresh identifier label = func ## "_exit";
> @@
> func(...) {
> <+...
>  x =
>  (T)
>  \(MACRO1\|MACRO2\)
>  (...);
> -if (y)
> -{
> -      BREAKPOINT();
> -      s = z;
> -      goto label;
> -}
> +CHECK_OR_BP_AND_GOTO(y, s, z, _exit);
> ...
>  label:
> ...+>
> }
>
> but I get this error:
>
> 18: unexpected use of a fresh identifier label
>
> What am I missing?

Fresh identifiers are only for + code.

You need to construct the identifier you want to match using python code,
as illustrated in demos/pythontococci.cocci

julia
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] Wider usage of “fresh identifiers”?
  2019-06-11 21:58       ` Timur Tabi
@ 2019-06-12  7:12         ` Markus Elfring
  0 siblings, 0 replies; 8+ messages in thread
From: Markus Elfring @ 2019-06-12  7:12 UTC (permalink / raw)
  To: Timur Tabi; +Cc: cocci

> Apparently I'm missing this obscure line in the documentation:
>
> "Fresh identifier metavariables must only be used in + code."
>
> Any suggestions on how to work around this, other than use Python?

Would you get into the mood to adjust this software situation any more?
(Increase OCaml development?)

How do you think about to check the mentioned restriction in more detail?

Regards,
Markus
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

end of thread, back to index

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-05-24 19:13 [Cocci] How to concatenate identifier with text? Timur Tabi
2019-05-24 19:36 ` Timur Tabi
2019-05-24 19:38   ` Julia Lawall
2019-06-11 21:51     ` Timur Tabi
2019-06-11 21:58       ` Timur Tabi
2019-06-12  7:12         ` [Cocci] Wider usage of “fresh identifiers”? Markus Elfring
2019-06-12  5:25       ` [Cocci] How to concatenate identifier with text? Julia Lawall
2019-05-24 19:38 ` 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 cocci@archiver.kernel.org
	public-inbox-index cocci


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