Re: [PATCH 0/10] Use ARRAY_SIZE macro - v4.13-rc7

From: Joe Perches
Date: Sun Sep 03 2017 - 11:36:13 EST


On Sun, 2017-09-03 at 14:19 +0200, Thomas Meyer wrote:
> Use ARRAY_SIZE macro, rather than explicitly coding some variant of it
> yourself.
>
> Found with: find -type f -name "*.c" -o -name "*.h" | xargs perl -p -i -e
> 's/\bsizeof\s*\(\s*(\w+)\s*\)\s*\ /\s*sizeof\s*\(\s*\1\s*\[\s*0\s*\]\s*\)
> /ARRAY_SIZE(\1)/g' and manual check/verification.

Hey Thomas.

There are some instances that span multiple lines that
the regex above misses.

For instance:

diff --git a/drivers/infiniband/hw/mlx5/odp.c b/drivers/infiniband/hw/mlx5/odp.c
index 3d701c7a4c91..26a825bd7581 100644
--- a/drivers/infiniband/hw/mlx5/odp.c
+++ b/drivers/infiniband/hw/mlx5/odp.c
@@ -929,8 +929,7 @@ static int mlx5_ib_mr_initiator_pfault_handler(
                return -EFAULT;
        }
 
-       if (unlikely(opcode >= sizeof(mlx5_ib_odp_opcode_cap) /
-           sizeof(mlx5_ib_odp_opcode_cap[0]) ||
+       if (unlikely(opcode >= ARRAY_SIZE(mlx5_ib_odp_opcode_cap) ||

Here is another perl command regex that fixes a few more:

$ perl -i -e 'local $/; while (<>) { s/\bsizeof\s*\(\s*(\w+)\s*\)\s*\/\s*sizeof\s*\(\s*\1\s*\[\s*0\s*\]\s*\)/ARRAY_SIZE(\1)/g; print; }' $file

This regex could still miss variants that
have a comment or that don't use parentheses
around the sizeof.

It seems none of those styles exist though.