cocci.inria.fr archive mirror
 help / color / mirror / Atom feed
* [cocci] How do you concatinate string constants in SmPL with python?
@ 2022-04-02 21:18 Eric Wheeler
  2022-04-02 21:26 ` Julia Lawall
  0 siblings, 1 reply; 8+ messages in thread
From: Eric Wheeler @ 2022-04-02 21:18 UTC (permalink / raw)
  To: cocci

Hello all,

I would like to do something like this:

@ r @
constant C;
@@
-perror(C);
+pr_err(C ": %s\n", strerror(errno));

Thus in python I need something like C = C + C ": %s\n".

I've tried a few things, but I don't think I'm anywhere close to getting 
it right.  Can someone help with how this should be setup?

Also is there a reference for the `coccinelle.` and `cocci.` python calls 
that seem to be available in a script:python section?

Thanks for your help!


--
Eric Wheeler

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

* Re: [cocci] How do you concatinate string constants in SmPL with python?
  2022-04-02 21:18 [cocci] How do you concatinate string constants in SmPL with python? Eric Wheeler
@ 2022-04-02 21:26 ` Julia Lawall
  2022-04-02 22:51   ` Eric Wheeler
  2022-04-03 14:55   ` [cocci] Completing documentation for SmPL scripting interfaces Markus Elfring
  0 siblings, 2 replies; 8+ messages in thread
From: Julia Lawall @ 2022-04-02 21:26 UTC (permalink / raw)
  To: Eric Wheeler; +Cc: cocci



On Sat, 2 Apr 2022, Eric Wheeler wrote:

> Hello all,
>
> I would like to do something like this:
>
> @ r @
> constant C;
> @@
> -perror(C);
> +pr_err(C ": %s\n", strerror(errno));
>
> Thus in python I need something like C = C + C ": %s\n".

I don't understand this.

>
> I've tried a few things, but I don't think I'm anywhere close to getting
> it right.  Can someone help with how this should be setup?
>
> Also is there a reference for the `coccinelle.` and `cocci.` python calls
> that seem to be available in a script:python section?

There is Coccilib.3cocci, but that is for ocaml.

My understanding is that there are reflective features in python that let
you find out what functions are available.  python/python_documentation.md
in the source tree also has some information.  Or look at the code in
python/coccilib.

julia

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

* Re: [cocci] How do you concatinate string constants in SmPL with python?
  2022-04-02 21:26 ` Julia Lawall
@ 2022-04-02 22:51   ` Eric Wheeler
  2022-04-03  8:06     ` Julia Lawall
  2022-04-03  9:50     ` [cocci] How do you concatenate string constants with SmPL? Markus Elfring
  2022-04-03 14:55   ` [cocci] Completing documentation for SmPL scripting interfaces Markus Elfring
  1 sibling, 2 replies; 8+ messages in thread
From: Eric Wheeler @ 2022-04-02 22:51 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci

On Sat, 2 Apr 2022, Julia Lawall wrote:
> On Sat, 2 Apr 2022, Eric Wheeler wrote:
> > I would like to do something like this:
> >
> > @ r @
> > constant C;
> > @@
> > -perror(C);
> > +pr_err(C ": %s\n", strerror(errno));
> >
> > Thus in python I need something like C = C + C ": %s\n".
> 
> I don't understand this.

For example:

-perror("unable to open file");
+pr_err("unable to open file: %s\n", strerror(errno));

I'm trying to append ": %s\n" to the original string (C) so we can process 
the error using pr_err() instead of perror().  Since SmPL does not support 
string concatination (so far as I am aware), I would need to use python.

But I'm not sure how to structure the python bits so they interact with 
cocci.  I've looked at some examples but I don't see how to change the 
value of C to something else, such as C = C + ": %s\n" where + is the 
append operator in python.

--
Eric Wheeler


> 
> >
> > I've tried a few things, but I don't think I'm anywhere close to getting
> > it right.  Can someone help with how this should be setup?
> >
> > Also is there a reference for the `coccinelle.` and `cocci.` python calls
> > that seem to be available in a script:python section?
> 
> There is Coccilib.3cocci, but that is for ocaml.
> 
> My understanding is that there are reflective features in python that let
> you find out what functions are available.  python/python_documentation.md
> in the source tree also has some information.  Or look at the code in
> python/coccilib.
> 
> julia
> 

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

* Re: [cocci] How do you concatinate string constants in SmPL with python?
  2022-04-02 22:51   ` Eric Wheeler
@ 2022-04-03  8:06     ` Julia Lawall
  2022-04-03  9:50     ` [cocci] How do you concatenate string constants with SmPL? Markus Elfring
  1 sibling, 0 replies; 8+ messages in thread
From: Julia Lawall @ 2022-04-03  8:06 UTC (permalink / raw)
  To: Eric Wheeler; +Cc: cocci



On Sat, 2 Apr 2022, Eric Wheeler wrote:

> On Sat, 2 Apr 2022, Julia Lawall wrote:
> > On Sat, 2 Apr 2022, Eric Wheeler wrote:
> > > I would like to do something like this:
> > >
> > > @ r @
> > > constant C;
> > > @@
> > > -perror(C);
> > > +pr_err(C ": %s\n", strerror(errno));
> > >
> > > Thus in python I need something like C = C + C ": %s\n".
> >
> > I don't understand this.
>
> For example:
>
> -perror("unable to open file");
> +pr_err("unable to open file: %s\n", strerror(errno));
>
> I'm trying to append ": %s\n" to the original string (C) so we can process
> the error using pr_err() instead of perror().  Since SmPL does not support
> string concatination (so far as I am aware), I would need to use python.
>
> But I'm not sure how to structure the python bits so they interact with
> cocci.  I've looked at some examples but I don't see how to change the
> value of C to something else, such as C = C + ": %s\n" where + is the
> append operator in python.

You should check demos/pythontococci.  You need to make three rules, one
to match, one to use python to create the new string you want (in the
place of "something" in the demo), and one to rematch and generate the
code.

julia



>
> --
> Eric Wheeler
>
>
> >
> > >
> > > I've tried a few things, but I don't think I'm anywhere close to getting
> > > it right.  Can someone help with how this should be setup?
> > >
> > > Also is there a reference for the `coccinelle.` and `cocci.` python calls
> > > that seem to be available in a script:python section?
> >
> > There is Coccilib.3cocci, but that is for ocaml.
> >
> > My understanding is that there are reflective features in python that let
> > you find out what functions are available.  python/python_documentation.md
> > in the source tree also has some information.  Or look at the code in
> > python/coccilib.
> >
> > julia
> >
>

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

* Re: [cocci] How do you concatenate string constants with SmPL?
  2022-04-02 22:51   ` Eric Wheeler
  2022-04-03  8:06     ` Julia Lawall
@ 2022-04-03  9:50     ` Markus Elfring
  1 sibling, 0 replies; 8+ messages in thread
From: Markus Elfring @ 2022-04-03  9:50 UTC (permalink / raw)
  To: Eric Wheeler; +Cc: cocci

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


> For example:
>
> -perror("unable to open file");
> +pr_err("unable to open file: %s\n", strerror(errno));
>
> I'm trying to append ": %s\n" to the original string (C) so we can process
> the error using pr_err() instead of perror().


It is a pity that the following transformation approach failed with the message
“plus: parse error: …”.

@replacement@
@@
-perror
+pr_err
       ("unable to open file"
+       ": %s\n", strerror(errno)
       );


> Since SmPL does not support string concatination (so far as I am aware),


It would be nice if adjustments will become more convenient also for string literals.



> I would need to use python.


You can choose from the supported scripting languages.



> But I'm not sure how to structure the python bits so they interact with cocci.


Something can be achieved by using three SmPL rules for the mentioned situation.

1. Search for your update candidates.
2. Specify SmPL variables in a script rule so that data generation will be performed.
3. Integrate data into the desired source code adjustment.


Can you get further development ideas from my SmPL script example according to
the discussion topic “Can Coccinelle concatenate or append to existing string literals?”?
https://lore.kernel.org/cocci/caf9ec38-c627-711d-885d-b975cc914fca@web.de/
https://sympa.inria.fr/sympa/arc/cocci/2022-03/msg00058.html

Regards,
Markus

[-- Attachment #2: Type: text/html, Size: 3075 bytes --]

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

* Re: [cocci] Completing documentation for SmPL scripting interfaces
  2022-04-02 21:26 ` Julia Lawall
  2022-04-02 22:51   ` Eric Wheeler
@ 2022-04-03 14:55   ` Markus Elfring
  2022-04-03 16:44     ` Nicolas Palix
  1 sibling, 1 reply; 8+ messages in thread
From: Markus Elfring @ 2022-04-03 14:55 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci, Eric Wheeler

>> Also is there a reference for the `coccinelle.` and `cocci.` python calls
>> that seem to be available in a script:python section?
> There is Coccilib.3cocci, but that is for ocaml.
>
> My understanding is that there are reflective features in python that let
> you find out what functions are available.  python/python_documentation.md
> in the source tree also has some information.  Or look at the code in
> python/coccilib.


Would you like to improve the documentation for these programming interfaces?
https://gitlab.inria.fr/coccinelle/coccinelle/-/blob/5069eaeadd731ecdd99e7a6f4465c286a2792354/python/python_documentation.md#fields-and-methods-exposed-via-cocci-object
https://github.com/coccinelle/coccinelle/blob/31bf3aefce245e43cc58f6b6ff49fc9b8e9084ff/python/python_documentation.md#fields-and-methods-exposed-via-cocci-object


Should the method “cocci.make_expr()” be explained also?

Regards,
Markus


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

* Re: [cocci] Completing documentation for SmPL scripting interfaces
  2022-04-03 14:55   ` [cocci] Completing documentation for SmPL scripting interfaces Markus Elfring
@ 2022-04-03 16:44     ` Nicolas Palix
  2022-04-03 17:36       ` [cocci] Evolution according to SmPL scripting interfaces and regular expressions Markus Elfring
  0 siblings, 1 reply; 8+ messages in thread
From: Nicolas Palix @ 2022-04-03 16:44 UTC (permalink / raw)
  To: Eric Wheeler; +Cc: cocci

On 03/04/2022 16:55, Markus Elfring wrote:
>>> Also is there a reference for the `coccinelle.` and `cocci.` python calls
>>> that seem to be available in a script:python section?
>> There is Coccilib.3cocci, but that is for ocaml.
>>
>> My understanding is that there are reflective features in python that let
>> you find out what functions are available.  python/python_documentation.md
>> in the source tree also has some information.  Or look at the code in
>> python/coccilib.
> 
> 
> Would you like to improve the documentation for these programming interfaces?
> https://gitlab.inria.fr/coccinelle/coccinelle/-/blob/5069eaeadd731ecdd99e7a6f4465c286a2792354/python/python_documentation.md#fields-and-methods-exposed-via-cocci-object
> https://github.com/coccinelle/coccinelle/blob/31bf3aefce245e43cc58f6b6ff49fc9b8e9084ff/python/python_documentation.md#fields-and-methods-exposed-via-cocci-object
> 
> 
> Should the method “cocci.make_expr()” be explained also?

An example:

https://gitlab.inria.fr/coccinelle/coccinelle/-/commit/d0666cf3f6faa4516bd42bac1f319d6bc894e32d
> 
> Regards,
> Markus
> 


-- 
Nicolas Palix

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

* Re: [cocci] Evolution according to SmPL scripting interfaces and regular expressions
  2022-04-03 16:44     ` Nicolas Palix
@ 2022-04-03 17:36       ` Markus Elfring
  0 siblings, 0 replies; 8+ messages in thread
From: Markus Elfring @ 2022-04-03 17:36 UTC (permalink / raw)
  To: Nicolas Palix; +Cc: cocci, Eric Wheeler, Thierry Martinez


>> Should the method “cocci.make_expr()” be explained also?
>
> An example:
>
> https://gitlab.inria.fr/coccinelle/coccinelle/-/commit/d0666cf3f6faa4516bd42bac1f319d6bc894e32d


Thanks for your contribution “Scripting: Add example of make_expr”.


I noticed also your contribution “Regexp: Better error report with PCRE”
because of this reference.

Will your development branch “npalix-regexp” trigger any more software evolution
also according to improved handling of regular expressions?

Regards,
Markus


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

end of thread, other threads:[~2022-04-03 17:36 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-02 21:18 [cocci] How do you concatinate string constants in SmPL with python? Eric Wheeler
2022-04-02 21:26 ` Julia Lawall
2022-04-02 22:51   ` Eric Wheeler
2022-04-03  8:06     ` Julia Lawall
2022-04-03  9:50     ` [cocci] How do you concatenate string constants with SmPL? Markus Elfring
2022-04-03 14:55   ` [cocci] Completing documentation for SmPL scripting interfaces Markus Elfring
2022-04-03 16:44     ` Nicolas Palix
2022-04-03 17:36       ` [cocci] Evolution according to SmPL scripting interfaces and regular expressions Markus Elfring

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