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
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

Reply via email to