All of lore.kernel.org
 help / color / mirror / Atom feed
* [cocci] Can Coccinell concatinate or append to existing string literals?
@ 2022-03-13  0:53 Eric Wheeler
  2022-03-13  7:05 ` Julia Lawall
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Eric Wheeler @ 2022-03-13  0:53 UTC (permalink / raw)
  To: cocci

Hello,

I would like to do something like this:

	@ a @
	constant C;
	constant CN ## "_bar";
	@@
	-foo(C);
	+foo(CN);

to produce this:

	void f()
	{
	-	foo("foo");
	+	foo("foo_bar");
	}

It looks like `fresh identifier` can do something like this, but I can't 
get it to work with strings.  Is there a way?


--
Eric Wheeler

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

* Re: [cocci] Can Coccinell concatinate or append to existing string literals?
  2022-03-13  0:53 [cocci] Can Coccinell concatinate or append to existing string literals? Eric Wheeler
@ 2022-03-13  7:05 ` Julia Lawall
  2022-03-13  9:45 ` [cocci] Can Coccinelle concatenate " Markus Elfring
  2022-03-13 16:31 ` [cocci] Can Coccinell concatinate " Luis Chamberlain
  2 siblings, 0 replies; 10+ messages in thread
From: Julia Lawall @ 2022-03-13  7:05 UTC (permalink / raw)
  To: Eric Wheeler; +Cc: cocci



On Sat, 12 Mar 2022, Eric Wheeler wrote:

> Hello,
>
> I would like to do something like this:
>
> 	@ a @
> 	constant C;
> 	constant CN ## "_bar";
> 	@@
> 	-foo(C);
> 	+foo(CN);
>
> to produce this:
>
> 	void f()
> 	{
> 	-	foo("foo");
> 	+	foo("foo_bar");
> 	}
>
> It looks like `fresh identifier` can do something like this, but I can't
> get it to work with strings.  Is there a way?

Please take a look at demos/pythontococci.cocci
By using python scripting, you can do whatever you want.

julia

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

* Re: [cocci] Can Coccinelle concatenate or append to existing string literals?
  2022-03-13  0:53 [cocci] Can Coccinell concatinate or append to existing string literals? Eric Wheeler
  2022-03-13  7:05 ` Julia Lawall
@ 2022-03-13  9:45 ` Markus Elfring
  2022-03-13  9:57   ` Julia Lawall
  2022-03-13 16:31 ` [cocci] Can Coccinell concatinate " Luis Chamberlain
  2 siblings, 1 reply; 10+ messages in thread
From: Markus Elfring @ 2022-03-13  9:45 UTC (permalink / raw)
  To: Eric Wheeler, cocci


> to produce this:
>
> 	void f()
> 	{
> 	-	foo("foo");
> 	+	foo("foo_bar");
> 	}
>
> It looks like `fresh identifier` can do something like this,
> but I can't get it to work with strings.


I suggest to increase the distinction which data parts you would like to modify
for such a transformation attempt.



> Is there a way?


Will another test result for a SmPL script like the following trigger any further
collateral evolution?

@addition@
@@
 void f()
 {
 foo("foo"
+    "_bar"
    );
 }


Markus_Elfring@Sonne:…Projekte/Coccinelle/Probe> spatch --parse-cocci append_to_string_literal1.cocci
…
plus: parse error:
  File "append_to_string_literal1.cocci", line 6, column 10, charpos = 48
  around = '"',
  whole content = +    "_bar"


How will the chances evolve to support string literal concatenations better here
(by the means of the semantic patch language)?
https://en.cppreference.com/w/c/language/string_literal#Explanation


@initialize:python@
@@
import re

@find_update_candidate@
constant input_string;
position pos;
@@
 void f()
 {
 foo@pos(input_string);
 }

@script:python selection@
param << find_update_candidate.input_string;
text;
@@
if param == '"foo"':
   coccinelle.text = cocci.make_expr('"foo_bar"')
else:
   cocci.include_match(False)

@replacement@
constant find_update_candidate.input_string, selection.text;
position find_update_candidate.pos;
@@
 void f()
 {
 foo@pos(
-        input_string
+        text
        );
 }


Markus_Elfring@Sonne:…Projekte/Coccinelle/Probe> spatch append_to_string_literal2.cocci string_literal_usage1.c
…
@@ -1,4 +1,4 @@
 void f()
 {
-foo("foo");
+foo("foo_bar");
 }


Regards,
Markus


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

* Re: [cocci] Can Coccinelle concatenate or append to existing string literals?
  2022-03-13  9:45 ` [cocci] Can Coccinelle concatenate " Markus Elfring
@ 2022-03-13  9:57   ` Julia Lawall
  2022-03-13 13:00     ` Markus Elfring
  0 siblings, 1 reply; 10+ messages in thread
From: Julia Lawall @ 2022-03-13  9:57 UTC (permalink / raw)
  To: Markus Elfring; +Cc: Eric Wheeler, cocci

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



On Sun, 13 Mar 2022, Markus Elfring wrote:

>
> > to produce this:
> >
> > 	void f()
> > 	{
> > 	-	foo("foo");
> > 	+	foo("foo_bar");
> > 	}
> >
> > It looks like `fresh identifier` can do something like this,
> > but I can't get it to work with strings.
>
>
> I suggest to increase the distinction which data parts you would like to modify
> for such a transformation attempt.
>
>
>
> > Is there a way?
>
>
> Will another test result for a SmPL script like the following trigger any further
> collateral evolution?
>
> @addition@
> @@
>  void f()
>  {
>  foo("foo"
> +    "_bar"
>     );
>  }

Maybe this would be a good thing in principle, but I don't think it would
be the desirable solution in Eric's case.  He should just use python to
construct the string he wants.  In that way, the user will better be able
to connect the output to the string that is found in the code.

julia


>
>
> Markus_Elfring@Sonne:…Projekte/Coccinelle/Probe> spatch --parse-cocci append_to_string_literal1.cocci
> …
> plus: parse error:
>   File "append_to_string_literal1.cocci", line 6, column 10, charpos = 48
>   around = '"',
>   whole content = +    "_bar"
>
>
> How will the chances evolve to support string literal concatenations better here
> (by the means of the semantic patch language)?
> https://en.cppreference.com/w/c/language/string_literal#Explanation
>
>
> @initialize:python@
> @@
> import re
>
> @find_update_candidate@
> constant input_string;
> position pos;
> @@
>  void f()
>  {
>  foo@pos(input_string);
>  }
>
> @script:python selection@
> param << find_update_candidate.input_string;
> text;
> @@
> if param == '"foo"':
>    coccinelle.text = cocci.make_expr('"foo_bar"')
> else:
>    cocci.include_match(False)
>
> @replacement@
> constant find_update_candidate.input_string, selection.text;
> position find_update_candidate.pos;
> @@
>  void f()
>  {
>  foo@pos(
> -        input_string
> +        text
>         );
>  }
>
>
> Markus_Elfring@Sonne:…Projekte/Coccinelle/Probe> spatch append_to_string_literal2.cocci string_literal_usage1.c
> …
> @@ -1,4 +1,4 @@
>  void f()
>  {
> -foo("foo");
> +foo("foo_bar");
>  }
>
>
> Regards,
> Markus
>
>

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

* Re: [cocci] Can Coccinelle concatenate or append to existing string literals?
  2022-03-13  9:57   ` Julia Lawall
@ 2022-03-13 13:00     ` Markus Elfring
  0 siblings, 0 replies; 10+ messages in thread
From: Markus Elfring @ 2022-03-13 13:00 UTC (permalink / raw)
  To: Julia Lawall; +Cc: Eric Wheeler, cocci

>> Will another test result for a SmPL script like the following trigger any further
>> collateral evolution?
>>
>> @addition@
>> @@
>>  void f()
>>  {
>>  foo("foo"
>> +    "_bar"
>>     );
>>  }
> Maybe this would be a good thing in principle,


Will the chances grow to improve Coccinelle software components
so that similarly simple source code adjustments will become directly supported?



> but I don't think it would be the desirable solution in Eric's case.


I am curious how the extended transformation approach will look like.



> He should just use python to construct the string he wants.


Other scripting languages (OCaml for example) can provide further design options.



> In that way, the user will better be able to connect the output to the string
> that is found in the code.


How many development efforts would you like to invest for expressing desirable changes
in a succinct way?

Regards,
Markus


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

* Re: [cocci] Can Coccinell concatinate or append to existing string literals?
  2022-03-13  0:53 [cocci] Can Coccinell concatinate or append to existing string literals? Eric Wheeler
  2022-03-13  7:05 ` Julia Lawall
  2022-03-13  9:45 ` [cocci] Can Coccinelle concatenate " Markus Elfring
@ 2022-03-13 16:31 ` Luis Chamberlain
  2022-03-13 17:24   ` [cocci] Can Coccinelle concatenate " Markus Elfring
                     ` (2 more replies)
  2 siblings, 3 replies; 10+ messages in thread
From: Luis Chamberlain @ 2022-03-13 16:31 UTC (permalink / raw)
  To: Eric Wheeler; +Cc: cocci

On Sat, Mar 12, 2022 at 04:53:22PM -0800, Eric Wheeler wrote:
> Hello,
> 
> I would like to do something like this:
> 
> 	@ a @
> 	constant C;
> 	constant CN ## "_bar";
> 	@@
> 	-foo(C);
> 	+foo(CN);
> 
> to produce this:
> 
> 	void f()
> 	{
> 	-	foo("foo");
> 	+	foo("foo_bar");
> 	}
> 
> It looks like `fresh identifier` can do something like this, but I can't 
> get it to work with strings.  Is there a way?

Yes sure below is an example I used recently in Linux:

@c1@
expression E1;
identifier subdir, sysctls;
@@

static struct ctl_table subdir[] = {
    {
	    .procname = E1,
	    .maxlen = 0,
	    .mode = 0555,
	    .child = sysctls,
    },
    { }
};

@c2@
identifier c1.subdir;

expression E2;
identifier base;
@@

static struct ctl_table base[] = {
    {
	    .procname = E2,
	    .maxlen = 0,
	    .mode = 0555,
	    .child = subdir,
    },
    { }
};

@c3@
identifier c2.base;
identifier header;
@@

header = register_sysctl_table(base);

@r1 depends on c1 && c2 && c3@
expression c1.E1;
identifier c1.subdir, c1.sysctls;
@@

-static struct ctl_table subdir[] = {
-       {
-               .procname = E1,
-               .maxlen = 0,
-               .mode = 0555,
-               .child = sysctls,
-       },
-       { }
-};

@r2 depends on c1 && c2 && c3@
identifier c1.subdir;

expression c2.E2;
identifier c2.base;
@@
-static struct ctl_table base[] = {
-       {
-               .procname = E2,
-               .maxlen = 0,
-               .mode = 0555,
-               .child = subdir,
-       },
-       { }
-};

@initialize:python@
@@

def make_my_fresh_expression(s1, s2):
return '"' + s1.strip('"') + "/" + s2.strip('"') + '"'

@r3 depends on c1 && c2 && c3@
expression c1.E1;
identifier c1.sysctls;
expression c2.E2;
identifier c2.base;
identifier c3.header;
fresh identifier E3 = script:python(E2, E1) { make_my_fresh_expression(E2, E1) };
@@

header =
-register_sysctl_table(base);
+register_sysctl(E3, sysctls);

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

* Re: [cocci] Can Coccinelle concatenate or append to existing string literals?
  2022-03-13 16:31 ` [cocci] Can Coccinell concatinate " Luis Chamberlain
@ 2022-03-13 17:24   ` Markus Elfring
  2022-03-13 17:33     ` Julia Lawall
  2022-03-13 18:30   ` [cocci] Checking the construction of a fresh identifier Markus Elfring
  2022-03-15  2:40   ` [cocci] Can Coccinell concatinate or append to existing string literals? Eric Wheeler
  2 siblings, 1 reply; 10+ messages in thread
From: Markus Elfring @ 2022-03-13 17:24 UTC (permalink / raw)
  To: Luis Chamberlain; +Cc: Eric Wheeler, cocci

…

> @initialize:python@
> @@
>
> def make_my_fresh_expression(s1, s2):
> return '"' + s1.strip('"') + "/" + s2.strip('"') + '"'
>
> @r3 depends on c1 && c2 && c3@
> expression c1.E1;
> identifier c1.sysctls;
> expression c2.E2;
> identifier c2.base;
> identifier c3.header;
> fresh identifier E3 = script:python(E2, E1) { make_my_fresh_expression(E2, E1) };
> @@
>
> header =
> -register_sysctl_table(base);
> +register_sysctl(E3, sysctls);


How relevant is such a SmPL script example (which seems to demonstrate the selection
of a name pattern) when string literals should be constructed?

Regards,
Markus


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

* Re: [cocci] Can Coccinelle concatenate or append to existing string literals?
  2022-03-13 17:24   ` [cocci] Can Coccinelle concatenate " Markus Elfring
@ 2022-03-13 17:33     ` Julia Lawall
  0 siblings, 0 replies; 10+ messages in thread
From: Julia Lawall @ 2022-03-13 17:33 UTC (permalink / raw)
  To: Markus Elfring; +Cc: Luis Chamberlain, Eric Wheeler, cocci

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



On Sun, 13 Mar 2022, Markus Elfring wrote:

> …
>
> > @initialize:python@
> > @@
> >
> > def make_my_fresh_expression(s1, s2):
> > return '"' + s1.strip('"') + "/" + s2.strip('"') + '"'
> >
> > @r3 depends on c1 && c2 && c3@
> > expression c1.E1;
> > identifier c1.sysctls;
> > expression c2.E2;
> > identifier c2.base;
> > identifier c3.header;
> > fresh identifier E3 = script:python(E2, E1) { make_my_fresh_expression(E2, E1) };
> > @@
> >
> > header =
> > -register_sysctl_table(base);
> > +register_sysctl(E3, sysctls);
>
>
> How relevant is such a SmPL script example (which seems to demonstrate the selection
> of a name pattern) when string literals should be constructed?

It shows how to generate noew code from some matched information, as was
asked for.

julia

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

* Re: [cocci] Checking the construction of a fresh identifier
  2022-03-13 16:31 ` [cocci] Can Coccinell concatinate " Luis Chamberlain
  2022-03-13 17:24   ` [cocci] Can Coccinelle concatenate " Markus Elfring
@ 2022-03-13 18:30   ` Markus Elfring
  2022-03-15  2:40   ` [cocci] Can Coccinell concatinate or append to existing string literals? Eric Wheeler
  2 siblings, 0 replies; 10+ messages in thread
From: Markus Elfring @ 2022-03-13 18:30 UTC (permalink / raw)
  To: Luis Chamberlain; +Cc: Eric Wheeler, cocci

…
> @initialize:python@
> @@
>
> def make_my_fresh_expression(s1, s2):
> return '"' + s1.strip('"') + "/" + s2.strip('"') + '"'
>
> @r3 depends on c1 && c2 && c3@
> expression c1.E1;
> identifier c1.sysctls;
> expression c2.E2;
> identifier c2.base;
> identifier c3.header;
> fresh identifier E3 = script:python(E2, E1) { make_my_fresh_expression(E2, E1) };
> @@
>
> header =
> -register_sysctl_table(base);
> +register_sysctl(E3, sysctls);


I find it questionable that an “identifier” for the C programming language may contain
quotation characters and a slash.

Do you depend on any special software behaviour here?
https://gitlab.inria.fr/coccinelle/coccinelle/-/blob/20fdb67f4b20a242f222337e13091115884cf6bb/docs/manual/cocci_syntax.tex#L280
https://github.com/coccinelle/coccinelle/blob/ae337fce1512ff15aabc3ad5b6d2e537f97ab62a/docs/manual/cocci_syntax.tex#L280


Regards,
Markus


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

* Re: [cocci] Can Coccinell concatinate or append to existing string literals?
  2022-03-13 16:31 ` [cocci] Can Coccinell concatinate " Luis Chamberlain
  2022-03-13 17:24   ` [cocci] Can Coccinelle concatenate " Markus Elfring
  2022-03-13 18:30   ` [cocci] Checking the construction of a fresh identifier Markus Elfring
@ 2022-03-15  2:40   ` Eric Wheeler
  2 siblings, 0 replies; 10+ messages in thread
From: Eric Wheeler @ 2022-03-15  2:40 UTC (permalink / raw)
  To: Luis Chamberlain; +Cc: cocci

On Sun, 13 Mar 2022, Luis Chamberlain wrote:
> On Sat, Mar 12, 2022 at 04:53:22PM -0800, Eric Wheeler wrote:
> > Hello,
> > 
> > I would like to do something like this:
> > 
> > 	@ a @
> > 	constant C;
> > 	constant CN ## "_bar";
> > 	@@
> > 	-foo(C);
> > 	+foo(CN);
> > 
> > to produce this:
> > 
> > 	void f()
> > 	{
> > 	-	foo("foo");
> > 	+	foo("foo_bar");
> > 	}
> > 
> > It looks like `fresh identifier` can do something like this, but I can't 
> > get it to work with strings.  Is there a way?
> 
> Yes sure below is an example I used recently in Linux:
> 
> @c1@
> expression E1;
> identifier subdir, sysctls;
> @@
> 
> static struct ctl_table subdir[] = {
>     {
> 	    .procname = E1,
> 	    .maxlen = 0,
> 	    .mode = 0555,
> 	    .child = sysctls,
>     },
>     { }
> };
> 
> @c2@
> identifier c1.subdir;
> 
> expression E2;
> identifier base;
> @@
> 
> static struct ctl_table base[] = {
>     {
> 	    .procname = E2,
> 	    .maxlen = 0,
> 	    .mode = 0555,
> 	    .child = subdir,
>     },
>     { }
> };
> 
> @c3@
> identifier c2.base;
> identifier header;
> @@
> 
> header = register_sysctl_table(base);
> 
> @r1 depends on c1 && c2 && c3@
> expression c1.E1;
> identifier c1.subdir, c1.sysctls;
> @@
> 
> -static struct ctl_table subdir[] = {
> -       {
> -               .procname = E1,
> -               .maxlen = 0,
> -               .mode = 0555,
> -               .child = sysctls,
> -       },
> -       { }
> -};
> 
> @r2 depends on c1 && c2 && c3@
> identifier c1.subdir;
> 
> expression c2.E2;
> identifier c2.base;
> @@
> -static struct ctl_table base[] = {
> -       {
> -               .procname = E2,
> -               .maxlen = 0,
> -               .mode = 0555,
> -               .child = subdir,
> -       },
> -       { }
> -};
> 
> @initialize:python@
> @@
> 
> def make_my_fresh_expression(s1, s2):
> return '"' + s1.strip('"') + "/" + s2.strip('"') + '"'
> 
> @r3 depends on c1 && c2 && c3@
> expression c1.E1;
> identifier c1.sysctls;
> expression c2.E2;
> identifier c2.base;
> identifier c3.header;
> fresh identifier E3 = script:python(E2, E1) { make_my_fresh_expression(E2, E1) };
> @@
> 
> header =
> -register_sysctl_table(base);
> +register_sysctl(E3, sysctls);
> 


Thanks everyone for answering this thread and providing these examples and 
explanations to reference!

--
Eric Wheeler



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

end of thread, other threads:[~2022-03-15  2:40 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-03-13  0:53 [cocci] Can Coccinell concatinate or append to existing string literals? Eric Wheeler
2022-03-13  7:05 ` Julia Lawall
2022-03-13  9:45 ` [cocci] Can Coccinelle concatenate " Markus Elfring
2022-03-13  9:57   ` Julia Lawall
2022-03-13 13:00     ` Markus Elfring
2022-03-13 16:31 ` [cocci] Can Coccinell concatinate " Luis Chamberlain
2022-03-13 17:24   ` [cocci] Can Coccinelle concatenate " Markus Elfring
2022-03-13 17:33     ` Julia Lawall
2022-03-13 18:30   ` [cocci] Checking the construction of a fresh identifier Markus Elfring
2022-03-15  2:40   ` [cocci] Can Coccinell concatinate or append to existing string literals? Eric Wheeler

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.