Coccinelle archive on lore.kernel.org
 help / Atom feed
* [Cocci] Need help with Python again
@ 2018-10-11 22:27 timur
  2018-10-11 22:33 ` timur
  2018-10-12  1:05 ` julia.lawall
  0 siblings, 2 replies; 4+ messages in thread
From: timur @ 2018-10-11 22:27 UTC (permalink / raw)
  To: cocci

I'm trying to write a rule that will remove __FUNCTION__ from
printf-like statements.  That is:

NV_PRINTF(x, "%s: ...", __FUNCTION__, ...)

into

NV_PRINTF(x, "...", ...)

I have this, which is based on existing Python code that works, but I
can't even get it to compile:

@script:python s@
c << r.c;
c2;
@@
import re

print c, c.find('%s')
coccinelle.c2 = c

// Get rid of __FUNCTION__@the beginning of the string
@@
expression list[r.n] r.es;
constant char[] r.c;
identifier s.c2;
@@
NV_PRINTF(es,
-c, __FUNCTION__
+c2
,...);

This produces:

23 24
Fatal error: exception Failure("scriptmeta: parse error: \n = File
\"/home/ttabi/nv_printf2.cocci\", line 2, column 5,  charpos = 23\n
around = 'r', whole content = c << r.c;\n")

Besides whatever is wrong with the script, I'm confused as to how
spatch knows to invoke the Python script in the first place.  What is
it about my unnamed rule that tells spatch to invoke the script?

Also, why is the first parameter (es) an expression list?  Can't I
just use "expression x;" instead?

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

* [Cocci] Need help with Python again
  2018-10-11 22:27 [Cocci] Need help with Python again timur
@ 2018-10-11 22:33 ` timur
  2018-10-12  1:03   ` julia.lawall
  2018-10-12  1:05 ` julia.lawall
  1 sibling, 1 reply; 4+ messages in thread
From: timur @ 2018-10-11 22:33 UTC (permalink / raw)
  To: cocci

Ok, I just noticed something in my original Python that I don't
understand.  There are two clauses:

// Use Python to clean up the string literals.
// Comments are still C-style though
@r depends on rules@
constant char[] c;
expression list[n] es;
@@

NV_PRINTF(es,c,...)

@script:python s@
c << r.c;
c2;
@@
[snip]
coccinelle.c2 = c

@@
expression list[r.n] r.es;
constant char[] r.c;
identifier s.c2;
@@
NV_PRINTF(es,
-c
+c2
,...)

What is the third rule for?

On Thu, Oct 11, 2018 at 5:27 PM Timur Tabi <timur@kernel.org> wrote:
>
> I'm trying to write a rule that will remove __FUNCTION__ from
> printf-like statements.  That is:
>
> NV_PRINTF(x, "%s: ...", __FUNCTION__, ...)
>
> into
>
> NV_PRINTF(x, "...", ...)
>
> I have this, which is based on existing Python code that works, but I
> can't even get it to compile:
>
> @script:python s@
> c << r.c;
> c2;
> @@
> import re
>
> print c, c.find('%s')
> coccinelle.c2 = c
>
> // Get rid of __FUNCTION__ at the beginning of the string
> @@
> expression list[r.n] r.es;
> constant char[] r.c;
> identifier s.c2;
> @@
> NV_PRINTF(es,
> -c, __FUNCTION__
> +c2
> ,...);
>
> This produces:
>
> 23 24
> Fatal error: exception Failure("scriptmeta: parse error: \n = File
> \"/home/ttabi/nv_printf2.cocci\", line 2, column 5,  charpos = 23\n
> around = 'r', whole content = c << r.c;\n")
>
> Besides whatever is wrong with the script, I'm confused as to how
> spatch knows to invoke the Python script in the first place.  What is
> it about my unnamed rule that tells spatch to invoke the script?
>
> Also, why is the first parameter (es) an expression list?  Can't I
> just use "expression x;" instead?

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

* [Cocci] Need help with Python again
  2018-10-11 22:33 ` timur
@ 2018-10-12  1:03   ` julia.lawall
  0 siblings, 0 replies; 4+ messages in thread
From: julia.lawall @ 2018-10-12  1:03 UTC (permalink / raw)
  To: cocci



On Thu, 11 Oct 2018, Timur Tabi wrote:

> Ok, I just noticed something in my original Python that I don't
> understand.  There are two clauses:
>
> // Use Python to clean up the string literals.
> // Comments are still C-style though
> @r depends on rules@
> constant char[] c;
> expression list[n] es;
> @@
>
> NV_PRINTF(es,c,...)
>
> @script:python s@
> c << r.c;
> c2;
> @@
> [snip]
> coccinelle.c2 = c
>
> @@
> expression list[r.n] r.es;
> constant char[] r.c;
> identifier s.c2;
> @@
> NV_PRINTF(es,
> -c
> +c2
> ,...)
>
> What is the third rule for?

The first rule finds the format string in the call to NV_PRINTF, the
second rule changes that, and the third rule replaces the old format
string by the new one.

If you know that there is always only one argument before the format
string, then you can use expression x instead of the expression list.

julia

>
> On Thu, Oct 11, 2018 at 5:27 PM Timur Tabi <timur@kernel.org> wrote:
> >
> > I'm trying to write a rule that will remove __FUNCTION__ from
> > printf-like statements.  That is:
> >
> > NV_PRINTF(x, "%s: ...", __FUNCTION__, ...)
> >
> > into
> >
> > NV_PRINTF(x, "...", ...)
> >
> > I have this, which is based on existing Python code that works, but I
> > can't even get it to compile:
> >
> > @script:python s@
> > c << r.c;
> > c2;
> > @@
> > import re
> >
> > print c, c.find('%s')
> > coccinelle.c2 = c
> >
> > // Get rid of __FUNCTION__ at the beginning of the string
> > @@
> > expression list[r.n] r.es;
> > constant char[] r.c;
> > identifier s.c2;
> > @@
> > NV_PRINTF(es,
> > -c, __FUNCTION__
> > +c2
> > ,...);
> >
> > This produces:
> >
> > 23 24
> > Fatal error: exception Failure("scriptmeta: parse error: \n = File
> > \"/home/ttabi/nv_printf2.cocci\", line 2, column 5,  charpos = 23\n
> > around = 'r', whole content = c << r.c;\n")
> >
> > Besides whatever is wrong with the script, I'm confused as to how
> > spatch knows to invoke the Python script in the first place.  What is
> > it about my unnamed rule that tells spatch to invoke the script?
> >
> > Also, why is the first parameter (es) an expression list?  Can't I
> > just use "expression x;" instead?
> _______________________________________________
> Cocci mailing list
> Cocci at systeme.lip6.fr
> https://systeme.lip6.fr/mailman/listinfo/cocci
>

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

* [Cocci] Need help with Python again
  2018-10-11 22:27 [Cocci] Need help with Python again timur
  2018-10-11 22:33 ` timur
@ 2018-10-12  1:05 ` julia.lawall
  1 sibling, 0 replies; 4+ messages in thread
From: julia.lawall @ 2018-10-12  1:05 UTC (permalink / raw)
  To: cocci



On Thu, 11 Oct 2018, Timur Tabi wrote:

> I'm trying to write a rule that will remove __FUNCTION__ from
> printf-like statements.  That is:
>
> NV_PRINTF(x, "%s: ...", __FUNCTION__, ...)
>
> into
>
> NV_PRINTF(x, "...", ...)
>
> I have this, which is based on existing Python code that works, but I
> can't even get it to compile:
>
> @script:python s@
> c << r.c;
> c2;
> @@
> import re
>
> print c, c.find('%s')
> coccinelle.c2 = c
>
> // Get rid of __FUNCTION__ at the beginning of the string
> @@
> expression list[r.n] r.es;
> constant char[] r.c;
> identifier s.c2;
> @@
> NV_PRINTF(es,
> -c, __FUNCTION__
> +c2
> ,...);
>
> This produces:
>
> 23 24
> Fatal error: exception Failure("scriptmeta: parse error: \n = File
> \"/home/ttabi/nv_printf2.cocci\", line 2, column 5,  charpos = 23\n
> around = 'r', whole content = c << r.c;\n")

It always checks the metavariables when parsing the semantic patch.  That
doesn't involve invoking the python code.  It looks like you are missing
the rule r, although I don't know if you have given the complete semantic
patch above.

julia

>
> Besides whatever is wrong with the script, I'm confused as to how
> spatch knows to invoke the Python script in the first place.  What is
> it about my unnamed rule that tells spatch to invoke the script?
>
> Also, why is the first parameter (es) an expression list?  Can't I
> just use "expression x;" instead?
> _______________________________________________
> Cocci mailing list
> Cocci at systeme.lip6.fr
> https://systeme.lip6.fr/mailman/listinfo/cocci
>

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

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-10-11 22:27 [Cocci] Need help with Python again timur
2018-10-11 22:33 ` timur
2018-10-12  1:03   ` julia.lawall
2018-10-12  1:05 ` 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