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

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