On Sat, 30 Mar 2019, Guenter Roeck wrote:

> Hi Coccinelle experts,
>
> is it possible to define a set of rules to automate the conversion done
> in the attached patch ? I tried doing it myself, but did'nt get anywhere.

@r@
initializer list elements;
identifier i;
@@

-static const u32 i[] = {
-  elements,
-  0
-};

@s@
identifier r.i,j,ty;
@@

-static const struct hwmon_channel_info j = {
-       .type = ty,
-       .config = i,
-};

@script:ocaml t@
ty << s.ty;
elements << r.elements;
shorter;
elems;
@@

shorter :=
   make_ident (List.hd(List.rev (Str.split (Str.regexp "_") ty)));
elems :=
   make_ident
    (String.concat ","
     (List.map (fun x -> Printf.sprintf "\n\t\t\t   %s" x)
       (Str.split (Str.regexp " , ") elements)))

@@
identifier s.j,t.shorter;
identifier t.elems;
@@

- &j
+ HWMON_CHANNEL_INFO(shorter,elems)

--------------------------------------

Theer is some ugliness here.  The elements of the array are considered to
be an initializer list, while the arguments to the macro have to be an
expression list.  The script code related to elems and elements is to deal
with that.

There is also script code to constuct the first argument of
HWMON_CHANNEL_INFO from gthe name of the hwmon_channel_info structure.

You should be able to drop the static const stuff on the structures, if it
is not appropriate in general.  It should still be removed when the whole
structure is removed.

If you want to write some other script code that makes metavariables in
this way and prefer to use python, there are examples in

demos/pythontococci.cocci (only for making identifiers)
tests/python_mdecl.cocci  (for making identifiers and others)

julia
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

Reply via email to