cocci.inria.fr archive mirror
 help / color / mirror / Atom feed
* [Cocci] Capturing all array initializers?
@ 2019-03-26 23:46 Michael Stefaniuc
  2019-03-27  7:26 ` Julia Lawall
  2019-03-27  7:28 ` Julia Lawall
  0 siblings, 2 replies; 5+ messages in thread
From: Michael Stefaniuc @ 2019-03-26 23:46 UTC (permalink / raw)
  To: Coccinelle

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

Hello,

I'm trying to do this transformation,
from:
    const WCHAR wstr[] = {'u','t','f','1','6','
','s','t','r','i','n','g','\0'};
to:
    const WCHAR wstr[] = u"utf16 string";

I had hoped to be able to use an expression list for the array
initializer, but that produces a parse error. I know that technically an
array initializer is not an expression list, but it looks like one.
Is there another metavariable that I can use instead?


A way to workaround that would be to use something like:
@r@
typedef WCHAR;
identifier wstr;
constant ch;
position p;
@@
 const WCHAR wstr[] = { ..., ch@p, ..., '\0' };


That would make the subsequent script:python rule run once for each
char. With some surprises though:
- The initializers ch get sorted before script:python runs. Thus the
position is needed to undo the sorting.
- More surprisingly, without @p the initializers get even deduplicated.

This workaround is doable but tedious. Before I go down that rabbit hole
I prefer to check if there's a better alternative.

thanks
bye
	michael

[-- Attachment #2: wchar.c --]
[-- Type: text/x-csrc, Size: 77 bytes --]

const WCHAR wstr[] = {'u','t','f','1','6',' ','s','t','r','i','n','g','\0'};

[-- Attachment #3: wstr.cocci --]
[-- Type: text/plain, Size: 187 bytes --]

@r@
typedef WCHAR;
identifier wstr;
constant ch;
position p;
@@
 const WCHAR wstr[] = { ..., ch@p, ..., '\0' };


@script:python@
wstr << r.wstr;
ch << r.ch;
p << r.p;
@@
print(wstr, ch)

[-- Attachment #4: 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] 5+ messages in thread

* Re: [Cocci] Capturing all array initializers?
  2019-03-26 23:46 [Cocci] Capturing all array initializers? Michael Stefaniuc
@ 2019-03-27  7:26 ` Julia Lawall
  2019-03-27 10:20   ` Michael Stefaniuc
  2019-03-27  7:28 ` Julia Lawall
  1 sibling, 1 reply; 5+ messages in thread
From: Julia Lawall @ 2019-03-27  7:26 UTC (permalink / raw)
  To: Michael Stefaniuc; +Cc: Coccinelle



On Wed, 27 Mar 2019, Michael Stefaniuc wrote:

> Hello,
>
> I'm trying to do this transformation,
> from:
>     const WCHAR wstr[] = {'u','t','f','1','6','
> ','s','t','r','i','n','g','\0'};
> to:
>     const WCHAR wstr[] = u"utf16 string";
>
> I had hoped to be able to use an expression list for the array
> initializer, but that produces a parse error. I know that technically an
> array initializer is not an expression list, but it looks like one.
> Is there another metavariable that I can use instead?

I think that there is initializer list?

julia

>
>
> A way to workaround that would be to use something like:
> @r@
> typedef WCHAR;
> identifier wstr;
> constant ch;
> position p;
> @@
>  const WCHAR wstr[] = { ..., ch@p, ..., '\0' };
>
>
> That would make the subsequent script:python rule run once for each
> char. With some surprises though:
> - The initializers ch get sorted before script:python runs. Thus the
> position is needed to undo the sorting.
> - More surprisingly, without @p the initializers get even deduplicated.
>
> This workaround is doable but tedious. Before I go down that rabbit hole
> I prefer to check if there's a better alternative.
>
> thanks
> bye
> 	michael
>
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] Capturing all array initializers?
  2019-03-26 23:46 [Cocci] Capturing all array initializers? Michael Stefaniuc
  2019-03-27  7:26 ` Julia Lawall
@ 2019-03-27  7:28 ` Julia Lawall
  1 sibling, 0 replies; 5+ messages in thread
From: Julia Lawall @ 2019-03-27  7:28 UTC (permalink / raw)
  To: Michael Stefaniuc; +Cc: Coccinelle



On Wed, 27 Mar 2019, Michael Stefaniuc wrote:

> Hello,
>
> I'm trying to do this transformation,
> from:
>     const WCHAR wstr[] = {'u','t','f','1','6','
> ','s','t','r','i','n','g','\0'};
> to:
>     const WCHAR wstr[] = u"utf16 string";
>
> I had hoped to be able to use an expression list for the array
> initializer, but that produces a parse error. I know that technically an
> array initializer is not an expression list, but it looks like one.
> Is there another metavariable that I can use instead?
>
>
> A way to workaround that would be to use something like:
> @r@
> typedef WCHAR;
> identifier wstr;
> constant ch;
> position p;
> @@
>  const WCHAR wstr[] = { ..., ch@p, ..., '\0' };
>
>
> That would make the subsequent script:python rule run once for each
> char. With some surprises though:
> - The initializers ch get sorted before script:python runs. Thus the
> position is needed to undo the sorting.
> - More surprisingly, without @p the initializers get even deduplicated.

These are both the expected behavior.  Script code runs once for each set
of arguments, not for each set of matches.  If you want the set of
matches, you need to put position variables.  There is no guarantee
offered on the order in which the script will run, although as you found
out metavariable bindings do get sorted somewhere along the line.

julia


> This workaround is doable but tedious. Before I go down that rabbit hole
> I prefer to check if there's a better alternative.
>
> thanks
> bye
> 	michael
>
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] Capturing all array initializers?
  2019-03-27  7:26 ` Julia Lawall
@ 2019-03-27 10:20   ` Michael Stefaniuc
  2019-03-27 10:30     ` Julia Lawall
  0 siblings, 1 reply; 5+ messages in thread
From: Michael Stefaniuc @ 2019-03-27 10:20 UTC (permalink / raw)
  To: Julia Lawall; +Cc: Coccinelle

On 2019-03-27 08:26, Julia Lawall wrote:
> On Wed, 27 Mar 2019, Michael Stefaniuc wrote:
> 
>> Hello,
>> 
>> I'm trying to do this transformation,
>> from:
>>     const WCHAR wstr[] = {'u','t','f','1','6','
>> ','s','t','r','i','n','g','\0'};
>> to:
>>     const WCHAR wstr[] = u"utf16 string";
>> 
>> I had hoped to be able to use an expression list for the array
>> initializer, but that produces a parse error. I know that technically 
>> an
>> array initializer is not an expression list, but it looks like one.
>> Is there another metavariable that I can use instead?
> 
> I think that there is initializer list?
Indeed there is, thanks!

But 'initializer' is totally missing from 
http://coccinelle.lip6.fr/docs/main_grammar002.html , there's where I 
have looked for it.

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

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

* Re: [Cocci] Capturing all array initializers?
  2019-03-27 10:20   ` Michael Stefaniuc
@ 2019-03-27 10:30     ` Julia Lawall
  0 siblings, 0 replies; 5+ messages in thread
From: Julia Lawall @ 2019-03-27 10:30 UTC (permalink / raw)
  To: Michael Stefaniuc; +Cc: Coccinelle



On Wed, 27 Mar 2019, Michael Stefaniuc wrote:

> On 2019-03-27 08:26, Julia Lawall wrote:
> > On Wed, 27 Mar 2019, Michael Stefaniuc wrote:
> >
> > > Hello,
> > >
> > > I'm trying to do this transformation,
> > > from:
> > >     const WCHAR wstr[] = {'u','t','f','1','6','
> > > ','s','t','r','i','n','g','\0'};
> > > to:
> > >     const WCHAR wstr[] = u"utf16 string";
> > >
> > > I had hoped to be able to use an expression list for the array
> > > initializer, but that produces a parse error. I know that technically an
> > > array initializer is not an expression list, but it looks like one.
> > > Is there another metavariable that I can use instead?
> >
> > I think that there is initializer list?
> Indeed there is, thanks!
>
> But 'initializer' is totally missing from
> http://coccinelle.lip6.fr/docs/main_grammar002.html , there's where I have
> looked for it.

OK, thanks for the feedback.  It was a later addition...  I will update
it.

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

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

end of thread, other threads:[~2019-03-27 10:30 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-26 23:46 [Cocci] Capturing all array initializers? Michael Stefaniuc
2019-03-27  7:26 ` Julia Lawall
2019-03-27 10:20   ` Michael Stefaniuc
2019-03-27 10:30     ` Julia Lawall
2019-03-27  7:28 ` 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).