cocci.inria.fr archive mirror
 help / color / mirror / Atom feed
* [Cocci] Can the current function name be passed to a Python snippet?
@ 2018-11-15 20:58 Timur Tabi
  2018-11-15 21:29 ` Julia Lawall
  0 siblings, 1 reply; 17+ messages in thread
From: Timur Tabi @ 2018-11-15 20:58 UTC (permalink / raw)
  To: cocci

Is it possible for a Python code snippet to be passed the name of the
current function?  I already have Python code in my .cocci file that
cleans up the text of some string literals.  I want to add more code
that removes the current function name from the beginning of a string
literal.  So for instance, If I have

void myfunc(void)
{
...
    NV_PRINTF("myfunc: xxx\n");

I want to replace the NV_PRINTF line with:

    NV_PRINTF("xxx\n");

To do that, the Python code would need to given "myfunc" as a
parameter of some kind.
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] Can the current function name be passed to a Python snippet?
  2018-11-15 20:58 [Cocci] Can the current function name be passed to a Python snippet? Timur Tabi
@ 2018-11-15 21:29 ` Julia Lawall
  2018-12-04 20:17   ` Timur Tabi
  0 siblings, 1 reply; 17+ messages in thread
From: Julia Lawall @ 2018-11-15 21:29 UTC (permalink / raw)
  To: Timur Tabi; +Cc: cocci



On Thu, 15 Nov 2018, Timur Tabi wrote:

> Is it possible for a Python code snippet to be passed the name of the
> current function?  I already have Python code in my .cocci file that
> cleans up the text of some string literals.  I want to add more code
> that removes the current function name from the beginning of a string
> literal.  So for instance, If I have
>
> void myfunc(void)
> {
> ...
>     NV_PRINTF("myfunc: xxx\n");
>
> I want to replace the NV_PRINTF line with:
>
>     NV_PRINTF("xxx\n");
>
> To do that, the Python code would need to given "myfunc" as a
> parameter of some kind.

The name of the current function at a given match is available in a
position variable bound as part of that match.  A position variable should
have a current_element field.

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

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

* Re: [Cocci] Can the current function name be passed to a Python snippet?
  2018-11-15 21:29 ` Julia Lawall
@ 2018-12-04 20:17   ` Timur Tabi
  2018-12-04 20:25     ` Julia Lawall
  0 siblings, 1 reply; 17+ messages in thread
From: Timur Tabi @ 2018-12-04 20:17 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci

On Thu, Nov 15, 2018 at 3:29 PM Julia Lawall <julia.lawall@lip6.fr> wrote:

> The name of the current function at a given match is available in a
> position variable bound as part of that match.  A position variable should
> have a current_element field.

Unfortunately, I don't understand what you're saying.  I see some
examples referencing the "position" in a Python script, but they don't
make any sense to me, e.g.

http://coccinelle.lip6.fr/docs/main_grammar016.html#sec27

The example shows:

@ r exists @
local idexpression struct device_node *n;
position p1, p2;
statement S1,S2;
expression E,E1;

I assume when you say "position parameter", you're talking about the
"position p1, p2;" above.  If so, I'm completely confused.
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] Can the current function name be passed to a Python snippet?
  2018-12-04 20:17   ` Timur Tabi
@ 2018-12-04 20:25     ` Julia Lawall
  2018-12-04 22:02       ` Timur Tabi
  0 siblings, 1 reply; 17+ messages in thread
From: Julia Lawall @ 2018-12-04 20:25 UTC (permalink / raw)
  To: Timur Tabi; +Cc: cocci



On Tue, 4 Dec 2018, Timur Tabi wrote:

> On Thu, Nov 15, 2018 at 3:29 PM Julia Lawall <julia.lawall@lip6.fr> wrote:
>
> > The name of the current function at a given match is available in a
> > position variable bound as part of that match.  A position variable should
> > have a current_element field.
>
> Unfortunately, I don't understand what you're saying.  I see some
> examples referencing the "position" in a Python script, but they don't
> make any sense to me, e.g.
>
> http://coccinelle.lip6.fr/docs/main_grammar016.html#sec27
>
> The example shows:
>
> @ r exists @
> local idexpression struct device_node *n;
> position p1, p2;
> statement S1,S2;
> expression E,E1;
>
> I assume when you say "position parameter", you're talking about the
> "position p1, p2;" above.  If so, I'm completely confused.

In the example, p1 will store the position of n.  In the python rule, you
can then access p1[0].current_element to get the name of the function in
which n occurs.

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

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

* Re: [Cocci] Can the current function name be passed to a Python snippet?
  2018-12-04 20:25     ` Julia Lawall
@ 2018-12-04 22:02       ` Timur Tabi
  2018-12-04 22:19         ` Timur Tabi
  2018-12-05  6:34         ` Julia Lawall
  0 siblings, 2 replies; 17+ messages in thread
From: Timur Tabi @ 2018-12-04 22:02 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci, Timur Tabi

On Tue, Dec 4, 2018 at 2:25 PM Julia Lawall <julia.lawall@lip6.fr> wrote:
>
>
>
> On Tue, 4 Dec 2018, Timur Tabi wrote:
>
> > On Thu, Nov 15, 2018 at 3:29 PM Julia Lawall <julia.lawall@lip6.fr> wrote:
> >
> > > The name of the current function at a given match is available in a
> > > position variable bound as part of that match.  A position variable should
> > > have a current_element field.
> >
> > Unfortunately, I don't understand what you're saying.  I see some
> > examples referencing the "position" in a Python script, but they don't
> > make any sense to me, e.g.
> >
> > http://coccinelle.lip6.fr/docs/main_grammar016.html#sec27
> >
> > The example shows:
> >
> > @ r exists @
> > local idexpression struct device_node *n;
> > position p1, p2;
> > statement S1,S2;
> > expression E,E1;
> >
> > I assume when you say "position parameter", you're talking about the
> > "position p1, p2;" above.  If so, I'm completely confused.
>
> In the example, p1 will store the position of n.  In the python rule, you
> can then access p1[0].current_element to get the name of the function in
> which n occurs.

What do you mean by "position of n"?

I managed to figure it out without using positional parameters.  It
almost works in that it only does the replacement once:

// Look for NV_PRINTF2 calls that have the function name in the string
@r4 depends on rules@
identifier func;
expression x;
constant char[] c;
@@
func(...) {
...
NV_PRINTF2(x, c, ...)
...
}

// Get rid of the function name at the beginning of the string
@script:python s4@
c << r4.c;
c2;
f << r4.func;
@@
import re
coccinelle.c2 = re.sub('"%s[: ]*' % f, '"', c, 1)

@depends on rules@
expression x;
constant char[] r4.c;
identifier s4.c2;
@@
NV_PRINTF2(x,
-c
+c2
,...);

Based on what I've read, I need to add < > in order for spatch to run
the rule multiple times within a function.  But if I change r4 to
this:

@r4 depends on rules@
identifier func;
expression x;
constant char[] c;
@@
func(...) {
<...
NV_PRINTF2(x, c, ...)
...>
}

It doesn't work at all.
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] Can the current function name be passed to a Python snippet?
  2018-12-04 22:02       ` Timur Tabi
@ 2018-12-04 22:19         ` Timur Tabi
  2018-12-05  6:35           ` Julia Lawall
  2018-12-05  6:34         ` Julia Lawall
  1 sibling, 1 reply; 17+ messages in thread
From: Timur Tabi @ 2018-12-04 22:19 UTC (permalink / raw)
  To: Timur Tabi; +Cc: cocci

On Tue, Dec 4, 2018 at 4:02 PM Timur Tabi <timur@kernel.org> wrote:
> Based on what I've read, I need to add < > in order for spatch to run
> the rule multiple times within a function.  But if I change r4 to
> this:
>
> @r4 depends on rules@
> identifier func;
> expression x;
> constant char[] c;
> @@
> func(...) {
> <...
> NV_PRINTF2(x, c, ...)
> ...>
> }
>
> It doesn't work at all.

Ok, I changed it to

func(...) {
<+...
NV_PRINTF2(x, c, ...)
...+>
}

and now it works.  I don't know why I needed the +'s, but at least it
works now.  Thanks.
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] Can the current function name be passed to a Python snippet?
  2018-12-04 22:02       ` Timur Tabi
  2018-12-04 22:19         ` Timur Tabi
@ 2018-12-05  6:34         ` Julia Lawall
  2018-12-05 17:57           ` Timur Tabi
  1 sibling, 1 reply; 17+ messages in thread
From: Julia Lawall @ 2018-12-05  6:34 UTC (permalink / raw)
  To: Timur Tabi; +Cc: cocci



On Tue, 4 Dec 2018, Timur Tabi wrote:

> On Tue, Dec 4, 2018 at 2:25 PM Julia Lawall <julia.lawall@lip6.fr> wrote:
> >
> >
> >
> > On Tue, 4 Dec 2018, Timur Tabi wrote:
> >
> > > On Thu, Nov 15, 2018 at 3:29 PM Julia Lawall <julia.lawall@lip6.fr> wrote:
> > >
> > > > The name of the current function at a given match is available in a
> > > > position variable bound as part of that match.  A position variable should
> > > > have a current_element field.
> > >
> > > Unfortunately, I don't understand what you're saying.  I see some
> > > examples referencing the "position" in a Python script, but they don't
> > > make any sense to me, e.g.
> > >
> > > http://coccinelle.lip6.fr/docs/main_grammar016.html#sec27
> > >
> > > The example shows:
> > >
> > > @ r exists @
> > > local idexpression struct device_node *n;
> > > position p1, p2;
> > > statement S1,S2;
> > > expression E,E1;
> > >
> > > I assume when you say "position parameter", you're talking about the
> > > "position p1, p2;" above.  If so, I'm completely confused.
> >
> > In the example, p1 will store the position of n.  In the python rule, you
> > can then access p1[0].current_element to get the name of the function in
> > which n occurs.
>
> What do you mean by "position of n"?

The code that n matches is in some file, within some function, at some
line number, and at some column offset.  All of that information is
collected in p1.

>
> I managed to figure it out without using positional parameters.  It
> almost works in that it only does the replacement once:
>
> // Look for NV_PRINTF2 calls that have the function name in the string
> @r4 depends on rules@
> identifier func;
> expression x;
> constant char[] c;
> @@
> func(...) {
> ...
> NV_PRINTF2(x, c, ...)
> ...
> }

This can work, but as written it requires exactly one call to NV_PRINTF2
on every control-flow path through the function.  It also has to trace
through the entire function, which will be expensive.

> // Get rid of the function name at the beginning of the string
> @script:python s4@
> c << r4.c;
> c2;
> f << r4.func;
> @@
> import re
> coccinelle.c2 = re.sub('"%s[: ]*' % f, '"', c, 1)
>
> @depends on rules@
> expression x;
> constant char[] r4.c;
> identifier s4.c2;
> @@
> NV_PRINTF2(x,
> -c
> +c2
> ,...);
>
> Based on what I've read, I need to add < > in order for spatch to run
> the rule multiple times within a function.  But if I change r4 to
> this:
>
> @r4 depends on rules@
> identifier func;
> expression x;
> constant char[] c;
> @@
> func(...) {
> <...
> NV_PRINTF2(x, c, ...)
> ...>
> }
>
> It doesn't work at all.

What do you mean by doesn't work at all?

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

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

* Re: [Cocci] Can the current function name be passed to a Python snippet?
  2018-12-04 22:19         ` Timur Tabi
@ 2018-12-05  6:35           ` Julia Lawall
  2018-12-05 17:49             ` Timur Tabi
  0 siblings, 1 reply; 17+ messages in thread
From: Julia Lawall @ 2018-12-05  6:35 UTC (permalink / raw)
  To: Timur Tabi; +Cc: cocci



On Tue, 4 Dec 2018, Timur Tabi wrote:

> On Tue, Dec 4, 2018 at 4:02 PM Timur Tabi <timur@kernel.org> wrote:
> > Based on what I've read, I need to add < > in order for spatch to run
> > the rule multiple times within a function.  But if I change r4 to
> > this:
> >
> > @r4 depends on rules@
> > identifier func;
> > expression x;
> > constant char[] c;
> > @@
> > func(...) {
> > <...
> > NV_PRINTF2(x, c, ...)
> > ...>
> > }
> >
> > It doesn't work at all.
>
> Ok, I changed it to
>
> func(...) {
> <+...
> NV_PRINTF2(x, c, ...)
> ...+>
> }
>
> and now it works.  I don't know why I needed the +'s, but at least it
> works now.  Thanks.

This means that the rule only applies when there is at least one
occurrence of the pattern in between the <+... ...+>.

The previous rule matched all functions.

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

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

* Re: [Cocci] Can the current function name be passed to a Python snippet?
  2018-12-05  6:35           ` Julia Lawall
@ 2018-12-05 17:49             ` Timur Tabi
  2018-12-05 19:33               ` Julia Lawall
  0 siblings, 1 reply; 17+ messages in thread
From: Timur Tabi @ 2018-12-05 17:49 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci

On Wed, Dec 5, 2018 at 12:47 AM Julia Lawall <julia.lawall@lip6.fr> wrote:
>
> > and now it works.  I don't know why I needed the +'s, but at least it
> > works now.  Thanks.
>
> This means that the rule only applies when there is at least one
> occurrence of the pattern in between the <+... ...+>.

Unfortunately, on some files, adding the + significantly slows down
the script.  It stalls for about 15-20 seconds a few times during
parsing.  Normally the while script takes about 1-2 seconds to run.  I
believe coccinelle is having trouble finding the functions because
some my source files abuse C macros.
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] Can the current function name be passed to a Python snippet?
  2018-12-05  6:34         ` Julia Lawall
@ 2018-12-05 17:57           ` Timur Tabi
  2018-12-05 18:32             ` Timur Tabi
                               ` (2 more replies)
  0 siblings, 3 replies; 17+ messages in thread
From: Timur Tabi @ 2018-12-05 17:57 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci, Timur Tabi

On Wed, Dec 5, 2018 at 12:44 AM Julia Lawall <julia.lawall@lip6.fr> wrote:
>
> > > In the example, p1 will store the position of n.  In the python rule, you
> > > can then access p1[0].current_element to get the name of the function in
> > > which n occurs.
> >
> > What do you mean by "position of n"?
>
> The code that n matches is in some file, within some function, at some
> line number, and at some column offset.  All of that information is
> collected in p1.

I'm having trouble figuring out how to access p1[0].  I tried this

// Use Python to clean up the string literals.
// Comments are still C-style though
@r depends on rules@
expression x;
position p1;
constant char[] c;
@@
NV_PRINTF2(x, c, ...)

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

print p1

But that gave me this error:

warning: r: metavariable p1 not used in the - or
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] Can the current function name be passed to a Python snippet?
  2018-12-05 17:57           ` Timur Tabi
@ 2018-12-05 18:32             ` Timur Tabi
  2018-12-05 19:45               ` Julia Lawall
  2018-12-05 19:11             ` Timur Tabi
  2018-12-05 19:32             ` Julia Lawall
  2 siblings, 1 reply; 17+ messages in thread
From: Timur Tabi @ 2018-12-05 18:32 UTC (permalink / raw)
  To: Timur Tabi; +Cc: cocci

[-- Attachment #1: Type: text/plain, Size: 402 bytes --]

Another thing that I don't understand is why do I need to have two
rules for every Python script?  I've attached my .cocci file.  Each
Python script is in between a pair of //---- comments.  They all have
the same format:

1. One rule to specify the pattern to match
2. A Python script
3. Another rule to specify how to process the output of the Python script

Can't I combine 1) and 3) into one rule?

[-- Attachment #2: nv_printf.cocci --]
[-- Type: application/octet-stream, Size: 4989 bytes --]

/*
 * Coccinelle file to replace DBG_PRINTF() calls with NV_PRINTF() 
 *
 * To work around an odd behavior of Coccinelle, we temporarily change
 * the DBG_PRINTx statements into NV_PRINT2, and do all our work on that
 * macro name.  Only at the very end do we change NV_PRINT2 to NV_PRINTF.
 * We do this so that Coccinelle won't touch existing NV_PRINTF statements.
 *
 * Python rules come in three parts:
 *  1. A rule to determine if the Python code should be run
 *  2. The Python code itself, inside a rule
 *  3. A final rule that applies the output of the Python script
 */

// Remove the double-parentheses in DBG_PRINTF statements
@rule0@
@@
DBG_PRINTF(
-(
...
-)
 );

// Replace DBG_PRINTF with NV_PRINTF2
@rule1 depends on rule0@
expression x;
expression list y;
@@
-DBG_PRINTF(x, y);
+NV_PRINTF2(y);

// Replace DBG_PRINT_STRING with NV_PRINTF2
@rule2@
expression list x;
@@
-DBG_PRINT_STRING(x);
+NV_PRINTF2(x);

// Replace DBG_PRINT_STRING_VALUE with NV_PRINTF2
@rule3@
expression x;
expression list y;
@@
-DBG_PRINT_STRING_VALUE(x, y);
+NV_PRINTF2(x, "%s 0x%x\n", y);

// Replace DBG_PRINT_STRING_PTR with NV_PRINTF2
@rule4@
expression x;
expression list y;
@@
-DBG_PRINT_STRING_PTR(x, y);
+NV_PRINTF2(x, "%s %p\n", y);

// Replace DBG_PRINT_STRING_VAL64 with NV_PRINTF2
@rule5@
expression x;
expression list y;
@@
-DBG_PRINT_STRING_VAL64(x, y);
+NV_PRINTF2(x, "%s 0x%llx\n", y);

// Collect all the previous rules as a dependecy, so that the rules below
// can depend only on "rules" instead of "rule1 || rule2 || ..."
@rules depends on rule1 || rule2 || rule3 || rule4 || rule5@
@@
NV_PRINTF2

// Replace the DBG_xxx or DEBUGLEVEL_xxx with LEVEL_xxx
@depends on rules@
@@
NV_PRINTF2(...,
-\(DBG_LEVEL_INFO \| DEBUGLEVEL_TRACEINFO \| DBG_LEVEL_SETUPINFO \| DEBUGLEVEL_SETUPINFO\)
+LEVEL_INFO
, ...);

@depends on rules@
@@
NV_PRINTF2(...,
-\(DBG_LEVEL_USERERRORS \| DEBUGLEVEL_USERERRORS\)
+LEVEL_NOTICE
, ...);

@depends on rules@
@@
NV_PRINTF2(...,
-\(DBG_LEVEL_WARNINGS \| DEBUGLEVEL_WARNINGS\)
+LEVEL_WARNING
, ...);

@depends on rules@
@@
NV_PRINTF2(...,
-\(DBG_LEVEL_ERRORS \| DEBUGLEVEL_ERRORS\)
+LEVEL_ERROR
, ...);

// -------------------------------

// Use Python to clean up the string literals.
// Comments are still C-style though
@r depends on rules@
expression x;
constant char[] c;
position p1, p2;
@@
NV_PRINTF2(x, c, ...)

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

print 'p1=', p1
print 'p2=', p2
// Convert a malformed multi-line string literal into one line
// Coccinelle sometimes takes care of this for us, but sometimes it doesn't
c = re.sub(r'\\\n[ ]*', '', c, re.DOTALL)

// Combine "xxx" "yyy" into "xxxyyy"
while True:
    m = re.match(r'(.*)"([^"]+)" "(.*)', c)
    if not m:
	break
    c = '%s"%s%s' % (m.group(1), m.group(2), m.group(3))

// Finally, remove the "NXXX: " prefix
c = re.sub(r'"N[\-A-Z]+[\: ]*', '"', c, 1)
coccinelle.c2 = c

@depends on rules@
expression x;
constant char[] r.c;
identifier s.c2;
@@
NV_PRINTF2(x,
-c
+c2
,...)

// -------------------------------

// Look for NV_PRINTF2 calls that have __FUNCTION__ or __func__ as the first parameter
@r2 depends on rules@
constant char[] c;
expression x;
symbol __func__;
@@
NV_PRINTF2(x, c, __FUNCTION__, ...)

// Get rid of __FUNCTION__ at the beginning of the string
@script:python s2@
c << r2.c;
c2;
@@
import re
coccinelle.c2 = re.sub('%s[: ]*', '', c, 1)

@depends on rules@
expression x;
constant char[] r2.c;
identifier s2.c2;
@@
NV_PRINTF2(x,
-c, __FUNCTION__
+c2
,...);

// -------------------------------

// Look for NV_PRINTF2 calls that have __FUNCTION__ or __func__ as the last parameter
@r3 depends on rules@
constant char[] c;
expression x;
symbol __func__;
@@
NV_PRINTF2(x, c, ..., __FUNCTION__)

// Get rid of __FUNCTION__ at the end of the string
@script:python s3@
c << r3.c;
c2;
@@
import re
print 'Start'
// Find the last [: ]*%s, and remove it
pos = [i.span() for i in re.finditer('[: ]*%s', c)][-1]
coccinelle.c2 = c[:pos[0]] + c[pos[1]:]
print 'End'

@depends on rules@
expression x;
constant char[] r3.c;
identifier s3.c2;
@@
NV_PRINTF2(x,
-c
+c2
,...);

// -------------------------------

// Look for NV_PRINTF2 calls that have the function name in the string
@r4 depends on rules@
identifier func;
expression x;
constant char[] c;
@@
func(...) {
<+...
NV_PRINTF2(x, c, ...)
...+>
}

// Get rid of the function name at the beginning of the string
@script:python s4@
c << r4.c;
c2;
f << r4.func;
@@
import re
coccinelle.c2 = re.sub('"%s[: ]*' % f, '"', c, 1)

@depends on rules@
expression x;
constant char[] r4.c;
identifier s4.c2;
@@
NV_PRINTF2(x,
-c
+c2
,...);

// -------------------------------

// Remove any unnecessary braces
//@depends on rules@
//expression list x;
//@@
//-{
//NV_PRINTF2(x);
//-}

// Finally, change NV_PRINT2 to NV_PRINTF.  This has the side-effect of
// also compressing all the parameters into as few lines as possible.
@depends on rules@
expression list x;
@@
-NV_PRINTF2(x)
+NV_PRINTF(x)

[-- Attachment #3: Type: text/plain, Size: 136 bytes --]

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

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

* Re: [Cocci] Can the current function name be passed to a Python snippet?
  2018-12-05 17:57           ` Timur Tabi
  2018-12-05 18:32             ` Timur Tabi
@ 2018-12-05 19:11             ` Timur Tabi
  2018-12-05 23:27               ` Timur Tabi
  2018-12-05 19:32             ` Julia Lawall
  2 siblings, 1 reply; 17+ messages in thread
From: Timur Tabi @ 2018-12-05 19:11 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci

On Wed, Dec 5, 2018 at 11:57 AM Timur Tabi <timur@kernel.org> wrote:
>
> @script:python s@
> c << r.c;
> c2;
> p1 << r.p1;
> @@
> import re
>
> print p1
>
> But that gave me this error:
>
> warning: r: metavariable p1 not used in the - or

Never mind, I now understand how the @p1 is supposed to be used.  I
have it working now.
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] Can the current function name be passed to a Python snippet?
  2018-12-05 17:57           ` Timur Tabi
  2018-12-05 18:32             ` Timur Tabi
  2018-12-05 19:11             ` Timur Tabi
@ 2018-12-05 19:32             ` Julia Lawall
  2 siblings, 0 replies; 17+ messages in thread
From: Julia Lawall @ 2018-12-05 19:32 UTC (permalink / raw)
  To: Timur Tabi; +Cc: cocci



On Wed, 5 Dec 2018, Timur Tabi wrote:

> On Wed, Dec 5, 2018 at 12:44 AM Julia Lawall <julia.lawall@lip6.fr> wrote:
> >
> > > > In the example, p1 will store the position of n.  In the python rule, you
> > > > can then access p1[0].current_element to get the name of the function in
> > > > which n occurs.
> > >
> > > What do you mean by "position of n"?
> >
> > The code that n matches is in some file, within some function, at some
> > line number, and at some column offset.  All of that information is
> > collected in p1.
>
> I'm having trouble figuring out how to access p1[0].  I tried this
>
> // Use Python to clean up the string literals.
> // Comments are still C-style though
> @r depends on rules@
> expression x;
> position p1;
> constant char[] c;
> @@
> NV_PRINTF2(x, c, ...)

You need to attach p1 to something.  For example, you could say c@p1 or
x@p1 or )@p1

julia

>
> @script:python s@
> c << r.c;
> c2;
> p1 << r.p1;
> @@
> import re
>
> print p1
>
> But that gave me this error:
>
> warning: r: metavariable p1 not used in the - or
>
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] Can the current function name be passed to a Python snippet?
  2018-12-05 17:49             ` Timur Tabi
@ 2018-12-05 19:33               ` Julia Lawall
  0 siblings, 0 replies; 17+ messages in thread
From: Julia Lawall @ 2018-12-05 19:33 UTC (permalink / raw)
  To: Timur Tabi; +Cc: cocci



On Wed, 5 Dec 2018, Timur Tabi wrote:

> On Wed, Dec 5, 2018 at 12:47 AM Julia Lawall <julia.lawall@lip6.fr> wrote:
> >
> > > and now it works.  I don't know why I needed the +'s, but at least it
> > > works now.  Thanks.
> >
> > This means that the rule only applies when there is at least one
> > occurrence of the pattern in between the <+... ...+>.
>
> Unfortunately, on some files, adding the + significantly slows down
> the script.  It stalls for about 15-20 seconds a few times during
> parsing.  Normally the while script takes about 1-2 seconds to run.  I
> believe coccinelle is having trouble finding the functions because
> some my source files abuse C macros.

That wouldn't change by using <+... ...+>

Could you send the rule again?  I could make another suggestion.

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

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

* Re: [Cocci] Can the current function name be passed to a Python snippet?
  2018-12-05 18:32             ` Timur Tabi
@ 2018-12-05 19:45               ` Julia Lawall
  0 siblings, 0 replies; 17+ messages in thread
From: Julia Lawall @ 2018-12-05 19:45 UTC (permalink / raw)
  To: Timur Tabi; +Cc: cocci



On Wed, 5 Dec 2018, Timur Tabi wrote:

> Another thing that I don't understand is why do I need to have two
> rules for every Python script?  I've attached my .cocci file.  Each
> Python script is in between a pair of //---- comments.  They all have
> the same format:
>
> 1. One rule to specify the pattern to match
> 2. A Python script
> 3. Another rule to specify how to process the output of the Python script
>
> Can't I combine 1) and 3) into one rule?

No.  It is possible to attach script code to metavariable declarations,
but only to express predicates.  For example:

constant c : script:ocaml() { is_less_than_6 c };

is_less_than_6 would be defined in the @initialize:ocaml@ rule, and
takes a string as an argument, which is the potential term to bind to c,
and returns true or false.

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

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

* Re: [Cocci] Can the current function name be passed to a Python snippet?
  2018-12-05 19:11             ` Timur Tabi
@ 2018-12-05 23:27               ` Timur Tabi
  2018-12-06  6:33                 ` Julia Lawall
  0 siblings, 1 reply; 17+ messages in thread
From: Timur Tabi @ 2018-12-05 23:27 UTC (permalink / raw)
  To: Timur Tabi; +Cc: cocci

On Wed, Dec 5, 2018 at 1:11 PM Timur Tabi <timur@kernel.org> wrote:
>
> Never mind, I now understand how the @p1 is supposed to be used.  I
> have it working now.

I spoke too soon, I'm getting a weird error:

rule starting on line 232: already tagged token:
C code context
File "/tmp/cocci_small_output-22474-8398df.c", line 43, column 23,
charpos = 1216
  around = '"init_PllId: Error programming clock.  Stopping script.\n"',
  whole content =                        "init_PllId: Error
programming clock.  Stopping script.\n");

Line 232 is the "depends on rule" in this snippet:

// Get rid of the function name at the beginning of the string
@script:python s4@
c << r4.c;
c2;
p1 << r4.p1;
@@
import re

f = p1[0].current_element
coccinelle.c2 = re.sub('"%s[: ]*' % f, '"', c, 1)

@depends on rules@
expression x;
constant char[] r4.c;
identifier s4.c2;
@@
NV_PRINTF2(x,
-c
+c2
,...);
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] Can the current function name be passed to a Python snippet?
  2018-12-05 23:27               ` Timur Tabi
@ 2018-12-06  6:33                 ` Julia Lawall
  0 siblings, 0 replies; 17+ messages in thread
From: Julia Lawall @ 2018-12-06  6:33 UTC (permalink / raw)
  To: Timur Tabi; +Cc: cocci



On Wed, 5 Dec 2018, Timur Tabi wrote:

> On Wed, Dec 5, 2018 at 1:11 PM Timur Tabi <timur@kernel.org> wrote:
> >
> > Never mind, I now understand how the @p1 is supposed to be used.  I
> > have it working now.
>
> I spoke too soon, I'm getting a weird error:
>
> rule starting on line 232: already tagged token:
> C code context
> File "/tmp/cocci_small_output-22474-8398df.c", line 43, column 23,
> charpos = 1216
>   around = '"init_PllId: Error programming clock.  Stopping script.\n"',
>   whole content =                        "init_PllId: Error
> programming clock.  Stopping script.\n");
>
> Line 232 is the "depends on rule" in this snippet:
>
> // Get rid of the function name at the beginning of the string
> @script:python s4@
> c << r4.c;
> c2;
> p1 << r4.p1;
> @@
> import re
>
> f = p1[0].current_element
> coccinelle.c2 = re.sub('"%s[: ]*' % f, '"', c, 1)
>
> @depends on rules@
> expression x;
> constant char[] r4.c;
> identifier s4.c2;
> @@
> NV_PRINTF2(x,
> -c
> +c2
> ,...);

Probably the same string occurs in multiple functions, so you get multiple
new propositions for it.  To guard against this, you should reuse the
position variable:

@depends on rules@
expression x;
constant char[] r4.c;
position r4.p1;
identifier s4.c2;
@@
NV_PRINTF2(x,
-c@p1
+c2
,...);

You may need to move the @p1.  It should be in the same place that it
appears in r4.

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

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

end of thread, other threads:[~2018-12-06  6:34 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-11-15 20:58 [Cocci] Can the current function name be passed to a Python snippet? Timur Tabi
2018-11-15 21:29 ` Julia Lawall
2018-12-04 20:17   ` Timur Tabi
2018-12-04 20:25     ` Julia Lawall
2018-12-04 22:02       ` Timur Tabi
2018-12-04 22:19         ` Timur Tabi
2018-12-05  6:35           ` Julia Lawall
2018-12-05 17:49             ` Timur Tabi
2018-12-05 19:33               ` Julia Lawall
2018-12-05  6:34         ` Julia Lawall
2018-12-05 17:57           ` Timur Tabi
2018-12-05 18:32             ` Timur Tabi
2018-12-05 19:45               ` Julia Lawall
2018-12-05 19:11             ` Timur Tabi
2018-12-05 23:27               ` Timur Tabi
2018-12-06  6:33                 ` Julia Lawall
2018-12-05 19:32             ` Julia Lawall

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).