[v3,1/4] cocci: add script for zero-length arrays in structs

Message ID 20220603101331.1030993-2-bruce.richardson@intel.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series clean up zero-length arrays |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Bruce Richardson June 3, 2022, 10:13 a.m. UTC
  Add script to replace [0] with [] when used at the end of a struct.
The script also includes an additional struct member to match against so
as to avoid issues with arrays with only a single zero-length element.

Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
Acked-by: Morten Brørup <mb@smartsharesystems.com>
Acked-by: Stephen Hemminger <stephen@networkplumber.org>
Acked-by: Hemant Agrawal <hemant.agrawal@nxp.com>
---
 devtools/cocci/zero_length_array.cocci | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
 create mode 100644 devtools/cocci/zero_length_array.cocci
  

Comments

Morten Brørup June 3, 2022, 10:30 a.m. UTC | #1
> From: Bruce Richardson [mailto:bruce.richardson@intel.com]
> Sent: Friday, 3 June 2022 12.13
> 
> Add script to replace [0] with [] when used at the end of a struct.
> The script also includes an additional struct member to match against
> so
> as to avoid issues with arrays with only a single zero-length element.
> 
> Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
> Acked-by: Morten Brørup <mb@smartsharesystems.com>
> Acked-by: Stephen Hemminger <stephen@networkplumber.org>
> Acked-by: Hemant Agrawal <hemant.agrawal@nxp.com>
> ---
>  devtools/cocci/zero_length_array.cocci | 21 +++++++++++++++++++++
>  1 file changed, 21 insertions(+)
>  create mode 100644 devtools/cocci/zero_length_array.cocci
> 
> diff --git a/devtools/cocci/zero_length_array.cocci
> b/devtools/cocci/zero_length_array.cocci
> new file mode 100644
> index 0000000000..de8783bc7a
> --- /dev/null
> +++ b/devtools/cocci/zero_length_array.cocci
> @@ -0,0 +1,21 @@
> +// Replace zero-length array members with []
> +@@
> +identifier st, member, arr;
> +type T1, T2;
> +@@
> +struct st {
> +	...
> +	T1 member;
> +-	T2 arr[0];
> ++	T2 arr[];
> +};
> +@@
> +identifier st, member, arr, id;
> +type T1, T2;
> +@@
> +struct st {
> +	...
> +	T1 member;
> +-	T2 arr[0];
> ++	T2 arr[];
> +} id;
> --
> 2.34.1
> 

Formally, arr[0] could be the only field in the structure, i.e. with no preceding fields. It would be silly, but consider checking for that too.

PS: No worries about the name. Scandinavian letters can cause all sorts of problems.
  
Bruce Richardson June 3, 2022, 10:38 a.m. UTC | #2
On Fri, Jun 03, 2022 at 12:30:25PM +0200, Morten Brørup wrote:
> > From: Bruce Richardson [mailto:bruce.richardson@intel.com]
> > Sent: Friday, 3 June 2022 12.13
> > 
> > Add script to replace [0] with [] when used at the end of a struct.
> > The script also includes an additional struct member to match against
> > so
> > as to avoid issues with arrays with only a single zero-length element.
> > 
> > Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
> > Acked-by: Morten Brørup <mb@smartsharesystems.com>
> > Acked-by: Stephen Hemminger <stephen@networkplumber.org>
> > Acked-by: Hemant Agrawal <hemant.agrawal@nxp.com>
> > ---
> >  devtools/cocci/zero_length_array.cocci | 21 +++++++++++++++++++++
> >  1 file changed, 21 insertions(+)
> >  create mode 100644 devtools/cocci/zero_length_array.cocci
> > 
> > diff --git a/devtools/cocci/zero_length_array.cocci
> > b/devtools/cocci/zero_length_array.cocci
> > new file mode 100644
> > index 0000000000..de8783bc7a
> > --- /dev/null
> > +++ b/devtools/cocci/zero_length_array.cocci
> > @@ -0,0 +1,21 @@
> > +// Replace zero-length array members with []
> > +@@
> > +identifier st, member, arr;
> > +type T1, T2;
> > +@@
> > +struct st {
> > +	...
> > +	T1 member;
> > +-	T2 arr[0];
> > ++	T2 arr[];
> > +};
> > +@@
> > +identifier st, member, arr, id;
> > +type T1, T2;
> > +@@
> > +struct st {
> > +	...
> > +	T1 member;
> > +-	T2 arr[0];
> > ++	T2 arr[];
> > +} id;
> > --
> > 2.34.1
> > 
> 
> Formally, arr[0] could be the only field in the structure, i.e. with no preceding fields. It would be silly, but consider checking for that too.
> 

I actually had that originally, but the compiler will complain on
structures where there is only an unsized element. Therefore, I made the
script only do replacements on structs which had at least one other
element.
  

Patch

diff --git a/devtools/cocci/zero_length_array.cocci b/devtools/cocci/zero_length_array.cocci
new file mode 100644
index 0000000000..de8783bc7a
--- /dev/null
+++ b/devtools/cocci/zero_length_array.cocci
@@ -0,0 +1,21 @@ 
+// Replace zero-length array members with []
+@@
+identifier st, member, arr;
+type T1, T2;
+@@
+struct st {
+	...
+	T1 member;
+-	T2 arr[0];
++	T2 arr[];
+};
+@@
+identifier st, member, arr, id;
+type T1, T2;
+@@
+struct st {
+	...
+	T1 member;
+-	T2 arr[0];
++	T2 arr[];
+} id;