Skip to contentSkip to conversation

A complex set of rules: can it be done ?

Cocci Guenter Roeck 3 participants 5 messages Mar 30, 2019 – Mar 31, 2019 Hardware monitoring

lore
Share & tools
lore

Guenter Roeck

author maintainer
#0 lore raw

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 <[email protected]>
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.

---
 drivers/hwmon/nct7904.c | 128 +++++++++++++++++-------------------------------
 1 file changed, 46 insertions(+), 82 deletions(-)
drivers/hwmon/nct7904.c +46 -82
index 82c7de7b4639..04516789b070 100644
@@ -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
 };
 
Signature · 8 lines
2.7.4



_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci
#1 lore raw
Quoted 4 lines · Guenter Roeck

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

#2 lore raw

Re: [Cocci] Introduction of HWMON_CHANNEL_INFO() macro with SmPL?

Quoted 2 lines

@r@
initializer list elements;

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

drivers/hwmon/nct7904.c static int nct7904_detect(struct i2c_client *client, · line 426

Quoted 7 lines

identifier i;
@@

drivers/hwmon/nct7904.c +0 -4
-static const u32 i[] = {
-  elements,
-  0
-};

Are any more constraints needed for such a deletion?

Quoted 2 lines

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

#3 lore raw

Re: [Cocci] Introduction of HWMON_CHANNEL_INFO() macro with SmPL?

In reply to Julia Lawall
Quoted 2 lines

@r@
initializer list elements;

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

drivers/hwmon/nct7904.c static int nct7904_detect(struct i2c_client *client, · line 426

Quoted 7 lines

identifier i;
@@

drivers/hwmon/nct7904.c +0 -4
-static const u32 i[] = {
-  elements,
-  0
-};

Are any more constraints needed for such a deletion?

Quoted 2 lines

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

author maintainer
#4 lore raw
In reply to Julia Lawall

Hi Julia,

Quoted 58 lines · Julia Lawall
Quoted 4 lines · Guenter Roeck

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;
@@

drivers/hwmon/nct7904.c +0 -4
-static const u32 i[] = {
-  elements,
-  0
-};

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

(quoted diff) +0 -4
-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.

Quoted 7 lines

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.

Quoted 6 lines

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