2018-10-04 17:12:28

by Gustavo A. R. Silva

[permalink] [raw]
Subject: [PATCH] powerpc/xmon/ppc-opc: Use ARRAY_SIZE macro

Use ARRAY_SIZE instead of dividing sizeof array with sizeof an element.

This code was detected with the help of Coccinelle.

Signed-off-by: Gustavo A. R. Silva <[email protected]>
---
arch/powerpc/xmon/ppc-opc.c | 12 ++++--------
1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/arch/powerpc/xmon/ppc-opc.c b/arch/powerpc/xmon/ppc-opc.c
index ac2b55b..f3f57a1 100644
--- a/arch/powerpc/xmon/ppc-opc.c
+++ b/arch/powerpc/xmon/ppc-opc.c
@@ -966,8 +966,7 @@ const struct powerpc_operand powerpc_operands[] =
{ 0xff, 11, NULL, NULL, PPC_OPERAND_SIGNOPT },
};

-const unsigned int num_powerpc_operands = (sizeof (powerpc_operands)
- / sizeof (powerpc_operands[0]));
+const unsigned int num_powerpc_operands = ARRAY_SIZE(powerpc_operands);

/* The functions used to insert and extract complicated operands. */

@@ -6980,8 +6979,7 @@ const struct powerpc_opcode powerpc_opcodes[] = {
{"fcfidu.", XRC(63,974,1), XRA_MASK, POWER7|PPCA2, PPCVLE, {FRT, FRB}},
};

-const int powerpc_num_opcodes =
- sizeof (powerpc_opcodes) / sizeof (powerpc_opcodes[0]);
+const int powerpc_num_opcodes = ARRAY_SIZE(powerpc_opcodes);

/* The VLE opcode table.

@@ -7219,8 +7217,7 @@ const struct powerpc_opcode vle_opcodes[] = {
{"se_bl", BD8(58,0,1), BD8_MASK, PPCVLE, 0, {B8}},
};

-const int vle_num_opcodes =
- sizeof (vle_opcodes) / sizeof (vle_opcodes[0]);
+const int vle_num_opcodes = ARRAY_SIZE(vle_opcodes);

/* The macro table. This is only used by the assembler. */

@@ -7288,5 +7285,4 @@ const struct powerpc_macro powerpc_macros[] = {
{"e_clrlslwi",4, PPCVLE, "e_rlwinm %0,%1,%3,(%2)-(%3),31-(%3)"},
};

-const int powerpc_num_macros =
- sizeof (powerpc_macros) / sizeof (powerpc_macros[0]);
+const int powerpc_num_macros = ARRAY_SIZE(powerpc_macros);
--
2.7.4



2018-10-04 22:03:33

by Tyrel Datwyler

[permalink] [raw]
Subject: Re: [PATCH] powerpc/xmon/ppc-opc: Use ARRAY_SIZE macro

On 10/04/2018 10:10 AM, Gustavo A. R. Silva wrote:
> Use ARRAY_SIZE instead of dividing sizeof array with sizeof an element.
>
> This code was detected with the help of Coccinelle.
>
> Signed-off-by: Gustavo A. R. Silva <[email protected]>

Reviewed-by: Tyrel Datwyler <[email protected]>


2018-10-09 02:28:09

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH] powerpc/xmon/ppc-opc: Use ARRAY_SIZE macro

On Thu, 2018-10-04 at 19:10 +0200, Gustavo A. R. Silva wrote:
> Use ARRAY_SIZE instead of dividing sizeof array with sizeof an element.
[]
> diff --git a/arch/powerpc/xmon/ppc-opc.c b/arch/powerpc/xmon/ppc-opc.c
[]
> @@ -966,8 +966,7 @@ const struct powerpc_operand powerpc_operands[] =
> { 0xff, 11, NULL, NULL, PPC_OPERAND_SIGNOPT },
> };
>
> -const unsigned int num_powerpc_operands = (sizeof (powerpc_operands)
> - / sizeof (powerpc_operands[0]));
> +const unsigned int num_powerpc_operands = ARRAY_SIZE(powerpc_operands);

It seems this is unused and could be deleted.

> /* The functions used to insert and extract complicated operands. */
>
> @@ -6980,8 +6979,7 @@ const struct powerpc_opcode powerpc_opcodes[] = {
> {"fcfidu.", XRC(63,974,1), XRA_MASK, POWER7|PPCA2, PPCVLE, {FRT, FRB}},
> };
>
> -const int powerpc_num_opcodes =
> - sizeof (powerpc_opcodes) / sizeof (powerpc_opcodes[0]);
> +const int powerpc_num_opcodes = ARRAY_SIZE(powerpc_opcodes);

This is used once and should probably be replaced where
it is used with ARRAY_SIZE

> /* The VLE opcode table.
>
> @@ -7219,8 +7217,7 @@ const struct powerpc_opcode vle_opcodes[] = {
> {"se_bl", BD8(58,0,1), BD8_MASK, PPCVLE, 0, {B8}},
> };
>
> -const int vle_num_opcodes =
> - sizeof (vle_opcodes) / sizeof (vle_opcodes[0]);
> +const int vle_num_opcodes = ARRAY_SIZE(vle_opcodes);

Also apparently unused and could be deleted.

>
> /* The macro table. This is only used by the assembler. */
>
> @@ -7288,5 +7285,4 @@ const struct powerpc_macro powerpc_macros[] = {
> {"e_clrlslwi",4, PPCVLE, "e_rlwinm %0,%1,%3,(%2)-(%3),31-(%3)"},
> };
> ld
> -const int powerpc_num_macros =
> - sizeof (powerpc_macros) / sizeof (powerpc_macros[0]);
> +const int powerpc_num_macros = ARRAY_SIZE(powerpc_macros);

Also apparently unused and could be deleted.



2018-10-09 03:44:38

by Michael Ellerman

[permalink] [raw]
Subject: Re: [PATCH] powerpc/xmon/ppc-opc: Use ARRAY_SIZE macro

Joe Perches <[email protected]> writes:

> On Thu, 2018-10-04 at 19:10 +0200, Gustavo A. R. Silva wrote:
>> Use ARRAY_SIZE instead of dividing sizeof array with sizeof an element.
> []
>> diff --git a/arch/powerpc/xmon/ppc-opc.c b/arch/powerpc/xmon/ppc-opc.c
> []
>> @@ -966,8 +966,7 @@ const struct powerpc_operand powerpc_operands[] =
>> { 0xff, 11, NULL, NULL, PPC_OPERAND_SIGNOPT },
>> };
>>
>> -const unsigned int num_powerpc_operands = (sizeof (powerpc_operands)
>> - / sizeof (powerpc_operands[0]));
>> +const unsigned int num_powerpc_operands = ARRAY_SIZE(powerpc_operands);
>
> It seems this is unused and could be deleted.

The code in this file is copied from binutils.

We don't want to needlessly diverge it.

I've said this before:

https://lore.kernel.org/linuxppc-dev/[email protected]/

Is there some way we can blacklist this file from checkpatch, Coccinelle
etc?

cheers

2018-10-09 04:39:50

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH] powerpc/xmon/ppc-opc: Use ARRAY_SIZE macro

On Tue, 2018-10-09 at 14:43 +1100, Michael Ellerman wrote:
> Joe Perches <[email protected]> writes:
>
> > On Thu, 2018-10-04 at 19:10 +0200, Gustavo A. R. Silva wrote:
> > > Use ARRAY_SIZE instead of dividing sizeof array with sizeof an element.
> > []
> > > diff --git a/arch/powerpc/xmon/ppc-opc.c b/arch/powerpc/xmon/ppc-opc.c
> > []
> > > @@ -966,8 +966,7 @@ const struct powerpc_operand powerpc_operands[] =
> > > { 0xff, 11, NULL, NULL, PPC_OPERAND_SIGNOPT },
> > > };
> > >
> > > -const unsigned int num_powerpc_operands = (sizeof (powerpc_operands)
> > > - / sizeof (powerpc_operands[0]));
> > > +const unsigned int num_powerpc_operands = ARRAY_SIZE(powerpc_operands);
> >
> > It seems this is unused and could be deleted.
>
> The code in this file is copied from binutils.
>
> We don't want to needlessly diverge it.
>
> I've said this before:
>
> https://lore.kernel.org/linuxppc-dev/[email protected]/

Don't expect people to remember this.

> Is there some way we can blacklist this file from checkpatch, Coccinelle
> etc?

Modify both to look for some specific tag
in a file and then update the scripts to
read the file when looking at patches too.

Otherwise, no.