Coccinelle archive on lore.kernel.org
 help / Atom feed
* [Cocci] A complex set of rules: can it be done ?
@ 2019-03-30 22:24 Guenter Roeck
  2019-03-31  7:58 ` Julia Lawall
  0 siblings, 1 reply; 5+ messages in thread
From: Guenter Roeck @ 2019-03-30 22:24 UTC (permalink / raw)
  To: cocci

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.

Thanks,
Guenter

---
From 37f506439c184e930afda2ff82c3e207e0ac5d27 Mon Sep 17 00:00:00 2001
From: Guenter Roeck <linux@roeck-us.net>
Date: Fri, 29 Mar 2019 13:26:40 -0700
Subject: [PATCH] hwmon: (nct7904) Use new HWMON_CHANNEL_INFO() macro

The new macro simplifies the code, reduces the likelihood of errors,
and makes the code easier to read.

Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
 drivers/hwmon/nct7904.c | 128 +++++++++++++++++-------------------------------
 1 file changed, 46 insertions(+), 82 deletions(-)

diff --git a/drivers/hwmon/nct7904.c b/drivers/hwmon/nct7904.c
index 82c7de7b4639..04516789b070 100644
--- a/drivers/hwmon/nct7904.c
+++ b/drivers/hwmon/nct7904.c
@@ -400,89 +400,53 @@ static int nct7904_detect(struct i2c_client *client,
 	return 0;
 }
 
-static const u32 nct7904_in_config[] = {
-	HWMON_I_INPUT,                  /* dummy, skipped in is_visible */
-	HWMON_I_INPUT,
-	HWMON_I_INPUT,
-	HWMON_I_INPUT,
-	HWMON_I_INPUT,
-	HWMON_I_INPUT,
-	HWMON_I_INPUT,
-	HWMON_I_INPUT,
-	HWMON_I_INPUT,
-	HWMON_I_INPUT,
-	HWMON_I_INPUT,
-	HWMON_I_INPUT,
-	HWMON_I_INPUT,
-	HWMON_I_INPUT,
-	HWMON_I_INPUT,
-	HWMON_I_INPUT,
-	HWMON_I_INPUT,
-	HWMON_I_INPUT,
-	HWMON_I_INPUT,
-	HWMON_I_INPUT,
-	HWMON_I_INPUT,
-	0
-};
-
-static const struct hwmon_channel_info nct7904_in = {
-	.type = hwmon_in,
-	.config = nct7904_in_config,
-};
-
-static const u32 nct7904_fan_config[] = {
-	HWMON_F_INPUT,
-	HWMON_F_INPUT,
-	HWMON_F_INPUT,
-	HWMON_F_INPUT,
-	HWMON_F_INPUT,
-	HWMON_F_INPUT,
-	HWMON_F_INPUT,
-	HWMON_F_INPUT,
-	0
-};
-
-static const struct hwmon_channel_info nct7904_fan = {
-	.type = hwmon_fan,
-	.config = nct7904_fan_config,
-};
-
-static const u32 nct7904_pwm_config[] = {
-	HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
-	HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
-	HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
-	HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
-	0
-};
-
-static const struct hwmon_channel_info nct7904_pwm = {
-	.type = hwmon_pwm,
-	.config = nct7904_pwm_config,
-};
-
-static const u32 nct7904_temp_config[] = {
-	HWMON_T_INPUT,
-	HWMON_T_INPUT,
-	HWMON_T_INPUT,
-	HWMON_T_INPUT,
-	HWMON_T_INPUT,
-	HWMON_T_INPUT,
-	HWMON_T_INPUT,
-	HWMON_T_INPUT,
-	HWMON_T_INPUT,
-	0
-};
-
-static const struct hwmon_channel_info nct7904_temp = {
-	.type = hwmon_temp,
-	.config = nct7904_temp_config,
-};
-
 static const struct hwmon_channel_info *nct7904_info[] = {
-	&nct7904_in,
-	&nct7904_fan,
-	&nct7904_pwm,
-	&nct7904_temp,
+	HWMON_CHANNEL_INFO(in,
+			   HWMON_I_INPUT, /* dummy, skipped in is_visible */
+			   HWMON_I_INPUT,
+			   HWMON_I_INPUT,
+			   HWMON_I_INPUT,
+			   HWMON_I_INPUT,
+			   HWMON_I_INPUT,
+			   HWMON_I_INPUT,
+			   HWMON_I_INPUT,
+			   HWMON_I_INPUT,
+			   HWMON_I_INPUT,
+			   HWMON_I_INPUT,
+			   HWMON_I_INPUT,
+			   HWMON_I_INPUT,
+			   HWMON_I_INPUT,
+			   HWMON_I_INPUT,
+			   HWMON_I_INPUT,
+			   HWMON_I_INPUT,
+			   HWMON_I_INPUT,
+			   HWMON_I_INPUT,
+			   HWMON_I_INPUT,
+			   HWMON_I_INPUT),
+	HWMON_CHANNEL_INFO(fan,
+			   HWMON_F_INPUT,
+			   HWMON_F_INPUT,
+			   HWMON_F_INPUT,
+			   HWMON_F_INPUT,
+			   HWMON_F_INPUT,
+			   HWMON_F_INPUT,
+			   HWMON_F_INPUT,
+			   HWMON_F_INPUT),
+	HWMON_CHANNEL_INFO(pwm,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE),
+	HWMON_CHANNEL_INFO(temp,
+			   HWMON_T_INPUT,
+			   HWMON_T_INPUT,
+			   HWMON_T_INPUT,
+			   HWMON_T_INPUT,
+			   HWMON_T_INPUT,
+			   HWMON_T_INPUT,
+			   HWMON_T_INPUT,
+			   HWMON_T_INPUT,
+			   HWMON_T_INPUT),
 	NULL
 };
 
-- 
2.7.4



_______________________________________________
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] A complex set of rules: can it be done ?
  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
                     ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Julia Lawall @ 2019-03-31  7:58 UTC (permalink / raw)
  To: Guenter Roeck; +Cc: cocci



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
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] Introduction of HWMON_CHANNEL_INFO() macro with SmPL?
  2019-03-31  7:58 ` Julia Lawall
@ 2019-03-31 12:45   ` Markus Elfring
  2019-03-31 12:45   ` Markus Elfring
  2019-03-31 17:42   ` [Cocci] A complex set of rules: can it be done ? Guenter Roeck
  2 siblings, 0 replies; 5+ messages in thread
From: Markus Elfring @ 2019-03-31 12:45 UTC (permalink / raw)
  To: Julia Lawall, Günter Röck; +Cc: cocci

> @r@
> initializer list elements;

When will a description be added to the SmPL manual for this metavariable?


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

Are any more constraints needed for such a deletion?


> The elements of the array are considered to be an initializer list,
> while the arguments to the macro have to be an expression list.

How do you think about to improve any programming interfaces around
such a data structure conversion?

Regards,
Markus
_______________________________________________
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] Introduction of HWMON_CHANNEL_INFO() macro with SmPL?
  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   ` [Cocci] A complex set of rules: can it be done ? Guenter Roeck
  2 siblings, 0 replies; 5+ messages in thread
From: Markus Elfring @ 2019-03-31 12:45 UTC (permalink / raw)
  To: Julia Lawall, Günter Röck; +Cc: cocci

> @r@
> initializer list elements;

When will a description be added to the SmPL manual for this metavariable?


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

Are any more constraints needed for such a deletion?


> The elements of the array are considered to be an initializer list,
> while the arguments to the macro have to be an expression list.

How do you think about to improve any programming interfaces around
such a data structure conversion?

Regards,
Markus
_______________________________________________
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] A complex set of rules: can it be done ?
  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
  2 siblings, 0 replies; 5+ messages in thread
From: Guenter Roeck @ 2019-03-31 17:42 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci

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

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

end of thread, back to index

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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   ` [Cocci] A complex set of rules: can it be done ? Guenter Roeck

Coccinelle archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/cocci/0 cocci/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 cocci cocci/ https://lore.kernel.org/cocci \
		cocci@systeme.lip6.fr cocci@archiver.kernel.org
	public-inbox-index cocci


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/fr.lip6.systeme.cocci


AGPL code for this site: git clone https://public-inbox.org/ public-inbox