cocci.inria.fr archive mirror
 help / color / mirror / Atom feed
From: Guenter Roeck <linux@roeck-us.net>
To: Julia Lawall <julia.lawall@lip6.fr>
Cc: cocci@systeme.lip6.fr
Subject: Re: [Cocci] A complex set of rules: can it be done ?
Date: Sun, 31 Mar 2019 10:42:27 -0700	[thread overview]
Message-ID: <20190331174227.GA27479@roeck-us.net> (raw)
In-Reply-To: <alpine.DEB.2.21.1903310950160.2578@hadrien>

Hi Julia,

On Sun, Mar 31, 2019 at 09:58:15AM +0200, Julia Lawall wrote:
> 
> 
> 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.
> 
I dropped the "static const". With that, the above catches all instances
in the upstream kernel.

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

And I thought this might be super-complicated or just impossible.
You are just amazing. Thanks a lot for your help!

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

      parent reply	other threads:[~2019-03-31 17:42 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-30 22:24 [Cocci] A complex set of rules: can it be done ? Guenter Roeck
2019-03-31  7:58 ` Julia Lawall
2019-03-31 12:45   ` [Cocci] Introduction of HWMON_CHANNEL_INFO() macro with SmPL? Markus Elfring
2019-03-31 12:45   ` Markus Elfring
2019-03-31 17:42   ` Guenter Roeck [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190331174227.GA27479@roeck-us.net \
    --to=linux@roeck-us.net \
    --cc=cocci@systeme.lip6.fr \
    --cc=julia.lawall@lip6.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).