linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 3/5] Creates macro to avoid variable shadowing
@ 2018-10-23  1:10 Leonardo Brás
  2018-10-23 21:14 ` Helen Koike
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Leonardo Brás @ 2018-10-23  1:10 UTC (permalink / raw)
  To: lkcamp
  Cc: Borislav Petkov, David.Laight, Andy Lutomirski, Ingo Molnar,
	helen, Masahiro Yamada, Michal Marek, linux-kbuild, linux-kernel

Creates DEF_FIELD_ADDR_VAR as a more generic version of the DEF_FIELD_ADD
macro, allowing usage of a variable name other than the struct element name.
Also, sets DEF_FIELD_ADDR as a specific usage of DEF_FILD_ADDR_VAR in which
the var name is the same as the struct element name.

Signed-off-by: Leonardo Brás <leobras.c@gmail.com>
---
 scripts/mod/file2alias.c | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/scripts/mod/file2alias.c b/scripts/mod/file2alias.c
index 7be43697ff84..3015c0bdecb2 100644
--- a/scripts/mod/file2alias.c
+++ b/scripts/mod/file2alias.c
@@ -95,12 +95,20 @@ extern struct devtable *__start___devtable[], *__stop___devtable[];
  */
 #define DEF_FIELD(m, devid, f) \
 	typeof(((struct devid *)0)->f) f = TO_NATIVE(*(typeof(f) *)((m) + OFF_##devid##_##f))
+
+/* Define a variable v that holds the address of field f of struct devid
+ * based at address m.  Due to the way typeof works, for a field of type
+ * T[N] the variable has type T(*)[N], _not_ T*.
+ */
+#define DEF_FIELD_ADDR_VAR(m, devid, f, v) \
+	typeof(((struct devid *)0)->f) *v = ((m) + OFF_##devid##_##f)
+
 /* Define a variable f that holds the address of field f of struct devid
  * based at address m.  Due to the way typeof works, for a field of type
  * T[N] the variable has type T(*)[N], _not_ T*.
  */
 #define DEF_FIELD_ADDR(m, devid, f) \
-	typeof(((struct devid *)0)->f) *f = ((m) + OFF_##devid##_##f)
+	DEF_FIELD_ADDR_VAR(m, devid, f, f)
 
 /* Add a table entry.  We test function type matches while we're here. */
 #define ADD_TO_DEVTABLE(device_id, type, function) \
@@ -641,25 +649,27 @@ static void do_pnp_card_entries(void *symval, unsigned long size,
 	unsigned int i;
 
 	device_id_check(mod->name, "pnp", size, id_size, symval);
+	DEF_FIELD_ADDR(symval, pnp_card_device_id, devs);
+	typeof(devs) devs_last;
 
 	for (i = 0; i < count; i++) {
 		unsigned int j;
-		DEF_FIELD_ADDR(symval + i*id_size, pnp_card_device_id, devs);
+		devs_last = devs + i * id_size;
 
 		for (j = 0; j < PNP_MAX_DEVICES; j++) {
-			const char *id = (char *)(*devs)[j].id;
-			int i2, j2;
+			const char *id = (char *)(*devs_last)[j].id;
+			int j2;
 			int dup = 0;
 
 			if (!id[0])
 				break;
 
 			/* find duplicate, already added value */
-			for (i2 = 0; i2 < i && !dup; i2++) {
-				DEF_FIELD_ADDR(symval + i2*id_size, pnp_card_device_id, devs);
+			while ((devs_last -= id_size) >= devs && !dup) {
 
 				for (j2 = 0; j2 < PNP_MAX_DEVICES; j2++) {
-					const char *id2 = (char *)(*devs)[j2].id;
+					const char *id2 =
+						(char *)(*devs_last)[j2].id;
 
 					if (!id2[0])
 						break;
-- 
2.19.1


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

* Re: [PATCH v2 3/5] Creates macro to avoid variable shadowing
  2018-10-23  1:10 [PATCH v2 3/5] Creates macro to avoid variable shadowing Leonardo Brás
@ 2018-10-23 21:14 ` Helen Koike
  2018-10-23 21:35 ` Helen Koike
  2018-10-28 16:37 ` Masahiro Yamada
  2 siblings, 0 replies; 6+ messages in thread
From: Helen Koike @ 2018-10-23 21:14 UTC (permalink / raw)
  To: Leonardo Brás, lkcamp
  Cc: Borislav Petkov, David.Laight, Andy Lutomirski, Ingo Molnar,
	Masahiro Yamada, Michal Marek, linux-kbuild, linux-kernel

Hi Leonardo,

Thanks for your patch.

On 10/22/18 11:10 PM, Leonardo Brás wrote:
> Creates DEF_FIELD_ADDR_VAR as a more generic version of the DEF_FIELD_ADD
> macro, allowing usage of a variable name other than the struct element name.
> Also, sets DEF_FIELD_ADDR as a specific usage of DEF_FILD_ADDR_VAR in which
> the var name is the same as the struct element name.
> 
> Signed-off-by: Leonardo Brás <leobras.c@gmail.com>
> ---
>  scripts/mod/file2alias.c | 24 +++++++++++++++++-------
>  1 file changed, 17 insertions(+), 7 deletions(-)
> 
> diff --git a/scripts/mod/file2alias.c b/scripts/mod/file2alias.c
> index 7be43697ff84..3015c0bdecb2 100644
> --- a/scripts/mod/file2alias.c
> +++ b/scripts/mod/file2alias.c
> @@ -95,12 +95,20 @@ extern struct devtable *__start___devtable[], *__stop___devtable[];
>   */
>  #define DEF_FIELD(m, devid, f) \
>  	typeof(((struct devid *)0)->f) f = TO_NATIVE(*(typeof(f) *)((m) + OFF_##devid##_##f))
> +
> +/* Define a variable v that holds the address of field f of struct devid
> + * based at address m.  Due to the way typeof works, for a field of type
> + * T[N] the variable has type T(*)[N], _not_ T*.
> + */
> +#define DEF_FIELD_ADDR_VAR(m, devid, f, v) \
> +	typeof(((struct devid *)0)->f) *v = ((m) + OFF_##devid##_##f)

You created this macro but you don't use it below, maybe you forgot to
update the patch? Please check.

Helen

> +
>  /* Define a variable f that holds the address of field f of struct devid
>   * based at address m.  Due to the way typeof works, for a field of type
>   * T[N] the variable has type T(*)[N], _not_ T*.
>   */
>  #define DEF_FIELD_ADDR(m, devid, f) \
> -	typeof(((struct devid *)0)->f) *f = ((m) + OFF_##devid##_##f)
> +	DEF_FIELD_ADDR_VAR(m, devid, f, f)
>  
>  /* Add a table entry.  We test function type matches while we're here. */
>  #define ADD_TO_DEVTABLE(device_id, type, function) \
> @@ -641,25 +649,27 @@ static void do_pnp_card_entries(void *symval, unsigned long size,
>  	unsigned int i;
>  
>  	device_id_check(mod->name, "pnp", size, id_size, symval);
> +	DEF_FIELD_ADDR(symval, pnp_card_device_id, devs);
> +	typeof(devs) devs_last;
>  
>  	for (i = 0; i < count; i++) {
>  		unsigned int j;
> -		DEF_FIELD_ADDR(symval + i*id_size, pnp_card_device_id, devs);
> +		devs_last = devs + i * id_size;
>  
>  		for (j = 0; j < PNP_MAX_DEVICES; j++) {
> -			const char *id = (char *)(*devs)[j].id;
> -			int i2, j2;
> +			const char *id = (char *)(*devs_last)[j].id;
> +			int j2;
>  			int dup = 0;
>  
>  			if (!id[0])
>  				break;
>  
>  			/* find duplicate, already added value */
> -			for (i2 = 0; i2 < i && !dup; i2++) {
> -				DEF_FIELD_ADDR(symval + i2*id_size, pnp_card_device_id, devs);
> +			while ((devs_last -= id_size) >= devs && !dup) {
>  
>  				for (j2 = 0; j2 < PNP_MAX_DEVICES; j2++) {
> -					const char *id2 = (char *)(*devs)[j2].id;
> +					const char *id2 =
> +						(char *)(*devs_last)[j2].id;
>  
>  					if (!id2[0])
>  						break;
> 

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

* Re: [PATCH v2 3/5] Creates macro to avoid variable shadowing
  2018-10-23  1:10 [PATCH v2 3/5] Creates macro to avoid variable shadowing Leonardo Brás
  2018-10-23 21:14 ` Helen Koike
@ 2018-10-23 21:35 ` Helen Koike
  2018-10-24  1:48   ` Leonardo Bras
  2018-10-28 16:37 ` Masahiro Yamada
  2 siblings, 1 reply; 6+ messages in thread
From: Helen Koike @ 2018-10-23 21:35 UTC (permalink / raw)
  To: Leonardo Brás, lkcamp
  Cc: Borislav Petkov, David.Laight, Andy Lutomirski, Ingo Molnar,
	Masahiro Yamada, Michal Marek, linux-kbuild, linux-kernel

Hi Leonardo,

On 10/22/18 11:10 PM, Leonardo Brás wrote:
> Creates DEF_FIELD_ADDR_VAR as a more generic version of the DEF_FIELD_ADD
> macro, allowing usage of a variable name other than the struct element name.
> Also, sets DEF_FIELD_ADDR as a specific usage of DEF_FILD_ADDR_VAR in which
> the var name is the same as the struct element name.
> 
> Signed-off-by: Leonardo Brás <leobras.c@gmail.com>
> ---
>  scripts/mod/file2alias.c | 24 +++++++++++++++++-------
>  1 file changed, 17 insertions(+), 7 deletions(-)
> 
> diff --git a/scripts/mod/file2alias.c b/scripts/mod/file2alias.c
> index 7be43697ff84..3015c0bdecb2 100644
> --- a/scripts/mod/file2alias.c
> +++ b/scripts/mod/file2alias.c
> @@ -95,12 +95,20 @@ extern struct devtable *__start___devtable[], *__stop___devtable[];
>   */
>  #define DEF_FIELD(m, devid, f) \
>  	typeof(((struct devid *)0)->f) f = TO_NATIVE(*(typeof(f) *)((m) + OFF_##devid##_##f))
> +
> +/* Define a variable v that holds the address of field f of struct devid
> + * based at address m.  Due to the way typeof works, for a field of type
> + * T[N] the variable has type T(*)[N], _not_ T*.
> + */
> +#define DEF_FIELD_ADDR_VAR(m, devid, f, v) \
> +	typeof(((struct devid *)0)->f) *v = ((m) + OFF_##devid##_##f)
> +
>  /* Define a variable f that holds the address of field f of struct devid
>   * based at address m.  Due to the way typeof works, for a field of type
>   * T[N] the variable has type T(*)[N], _not_ T*.
>   */
>  #define DEF_FIELD_ADDR(m, devid, f) \
> -	typeof(((struct devid *)0)->f) *f = ((m) + OFF_##devid##_##f)
> +	DEF_FIELD_ADDR_VAR(m, devid, f, f)
>  
>  /* Add a table entry.  We test function type matches while we're here. */
>  #define ADD_TO_DEVTABLE(device_id, type, function) \
> @@ -641,25 +649,27 @@ static void do_pnp_card_entries(void *symval, unsigned long size,
>  	unsigned int i;
>  
>  	device_id_check(mod->name, "pnp", size, id_size, symval);
> +	DEF_FIELD_ADDR(symval, pnp_card_device_id, devs);
> +	typeof(devs) devs_last;
>  
>  	for (i = 0; i < count; i++) {
>  		unsigned int j;
> -		DEF_FIELD_ADDR(symval + i*id_size, pnp_card_device_id, devs);
> +		devs_last = devs + i * id_size;

Instead of doing all these changes, you could just modify here with
		DEF_FIELD_ADDR_VAR(symval + i*id_size, pnp_card_device_id, devs,
devs_aux);

Like this you are not changing the logic in this file, you are just
renaming the variable which is more then enough to solve the -Wshadow
warning.
I don't it is worthy changing the logic.

Regards,
Helen.

>  
>  		for (j = 0; j < PNP_MAX_DEVICES; j++) {
> -			const char *id = (char *)(*devs)[j].id;
> -			int i2, j2;
> +			const char *id = (char *)(*devs_last)[j].id;
> +			int j2;
>  			int dup = 0;
>  
>  			if (!id[0])
>  				break;
>  
>  			/* find duplicate, already added value */
> -			for (i2 = 0; i2 < i && !dup; i2++) {
> -				DEF_FIELD_ADDR(symval + i2*id_size, pnp_card_device_id, devs);
> +			while ((devs_last -= id_size) >= devs && !dup) {
>  
>  				for (j2 = 0; j2 < PNP_MAX_DEVICES; j2++) {
> -					const char *id2 = (char *)(*devs)[j2].id;
> +					const char *id2 =
> +						(char *)(*devs_last)[j2].id;
>  
>  					if (!id2[0])
>  						break;
> 

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

* Re: [PATCH v2 3/5] Creates macro to avoid variable shadowing
  2018-10-23 21:35 ` Helen Koike
@ 2018-10-24  1:48   ` Leonardo Bras
  0 siblings, 0 replies; 6+ messages in thread
From: Leonardo Bras @ 2018-10-24  1:48 UTC (permalink / raw)
  To: helen
  Cc: lkcamp, Borislav Petkov, David.Laight, Andy Lutomirski,
	Ingo Molnar, Masahiro Yamada, Michal Marek, linux-kbuild,
	linux-kernel

Hello Helen,
On Tue, Oct 23, 2018 at 6:35 PM Helen Koike <helen@koikeco.de> wrote:
> Like this you are not changing the logic in this file, you are just
> renaming the variable which is more then enough to solve the -Wshadow
> warning.
> I don't it is worthy changing the logic.

You are right, that was what the patch was supposed to do. For some
crazy reason I must have submitted the wrong commit as a patch.

I will fix it and send you all a v3 ASAP.

Sorry for the inconvenience.
Thanks!

Leonardo Brás

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

* Re: [PATCH v2 3/5] Creates macro to avoid variable shadowing
  2018-10-23  1:10 [PATCH v2 3/5] Creates macro to avoid variable shadowing Leonardo Brás
  2018-10-23 21:14 ` Helen Koike
  2018-10-23 21:35 ` Helen Koike
@ 2018-10-28 16:37 ` Masahiro Yamada
  2018-10-30  0:32   ` Leonardo Bras
  2 siblings, 1 reply; 6+ messages in thread
From: Masahiro Yamada @ 2018-10-28 16:37 UTC (permalink / raw)
  To: Leonardo Brás
  Cc: lkcamp, Borislav Petkov, David.Laight, Andy Lutomirski,
	Ingo Molnar, helen, Michal Marek, Linux Kbuild mailing list,
	Linux Kernel Mailing List

On Tue, Oct 23, 2018 at 10:11 AM Leonardo Brás <leobras.c@gmail.com> wrote:
>
> Creates DEF_FIELD_ADDR_VAR as a more generic version of the DEF_FIELD_ADD
> macro, allowing usage of a variable name other than the struct element name.
> Also, sets DEF_FIELD_ADDR as a specific usage of DEF_FILD_ADDR_VAR in which
> the var name is the same as the struct element name.
>
> Signed-off-by: Leonardo Brás <leobras.c@gmail.com>
> ---


Applied to linux-kbuild.



>  scripts/mod/file2alias.c | 24 +++++++++++++++++-------
>  1 file changed, 17 insertions(+), 7 deletions(-)
>
> diff --git a/scripts/mod/file2alias.c b/scripts/mod/file2alias.c
> index 7be43697ff84..3015c0bdecb2 100644
> --- a/scripts/mod/file2alias.c
> +++ b/scripts/mod/file2alias.c
> @@ -95,12 +95,20 @@ extern struct devtable *__start___devtable[], *__stop___devtable[];
>   */
>  #define DEF_FIELD(m, devid, f) \
>         typeof(((struct devid *)0)->f) f = TO_NATIVE(*(typeof(f) *)((m) + OFF_##devid##_##f))
> +
> +/* Define a variable v that holds the address of field f of struct devid
> + * based at address m.  Due to the way typeof works, for a field of type
> + * T[N] the variable has type T(*)[N], _not_ T*.
> + */
> +#define DEF_FIELD_ADDR_VAR(m, devid, f, v) \
> +       typeof(((struct devid *)0)->f) *v = ((m) + OFF_##devid##_##f)
> +
>  /* Define a variable f that holds the address of field f of struct devid
>   * based at address m.  Due to the way typeof works, for a field of type
>   * T[N] the variable has type T(*)[N], _not_ T*.
>   */
>  #define DEF_FIELD_ADDR(m, devid, f) \
> -       typeof(((struct devid *)0)->f) *f = ((m) + OFF_##devid##_##f)
> +       DEF_FIELD_ADDR_VAR(m, devid, f, f)
>
>  /* Add a table entry.  We test function type matches while we're here. */
>  #define ADD_TO_DEVTABLE(device_id, type, function) \
> @@ -641,25 +649,27 @@ static void do_pnp_card_entries(void *symval, unsigned long size,
>         unsigned int i;
>
>         device_id_check(mod->name, "pnp", size, id_size, symval);
> +       DEF_FIELD_ADDR(symval, pnp_card_device_id, devs);
> +       typeof(devs) devs_last;
>
>         for (i = 0; i < count; i++) {
>                 unsigned int j;
> -               DEF_FIELD_ADDR(symval + i*id_size, pnp_card_device_id, devs);
> +               devs_last = devs + i * id_size;
>
>                 for (j = 0; j < PNP_MAX_DEVICES; j++) {
> -                       const char *id = (char *)(*devs)[j].id;
> -                       int i2, j2;
> +                       const char *id = (char *)(*devs_last)[j].id;
> +                       int j2;
>                         int dup = 0;
>
>                         if (!id[0])
>                                 break;
>
>                         /* find duplicate, already added value */
> -                       for (i2 = 0; i2 < i && !dup; i2++) {
> -                               DEF_FIELD_ADDR(symval + i2*id_size, pnp_card_device_id, devs);
> +                       while ((devs_last -= id_size) >= devs && !dup) {
>
>                                 for (j2 = 0; j2 < PNP_MAX_DEVICES; j2++) {
> -                                       const char *id2 = (char *)(*devs)[j2].id;
> +                                       const char *id2 =
> +                                               (char *)(*devs_last)[j2].id;
>
>                                         if (!id2[0])
>                                                 break;
> --
> 2.19.1
>


-- 
Best Regards
Masahiro Yamada

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

* Re: [PATCH v2 3/5] Creates macro to avoid variable shadowing
  2018-10-28 16:37 ` Masahiro Yamada
@ 2018-10-30  0:32   ` Leonardo Bras
  0 siblings, 0 replies; 6+ messages in thread
From: Leonardo Bras @ 2018-10-30  0:32 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: lkcamp, Borislav Petkov, David.Laight, Andy Lutomirski,
	Ingo Molnar, Helen Koike, Michal Marek, linux-kbuild,
	linux-kernel

Thank you!
On Sun, Oct 28, 2018 at 1:38 PM Masahiro Yamada
<yamada.masahiro@socionext.com> wrote:
>
> On Tue, Oct 23, 2018 at 10:11 AM Leonardo Brás <leobras.c@gmail.com> wrote:
> >
> > Creates DEF_FIELD_ADDR_VAR as a more generic version of the DEF_FIELD_ADD
> > macro, allowing usage of a variable name other than the struct element name.
> > Also, sets DEF_FIELD_ADDR as a specific usage of DEF_FILD_ADDR_VAR in which
> > the var name is the same as the struct element name.
> >
> > Signed-off-by: Leonardo Brás <leobras.c@gmail.com>
> > ---
>
>
> Applied to linux-kbuild.
>
>
>
> >  scripts/mod/file2alias.c | 24 +++++++++++++++++-------
> >  1 file changed, 17 insertions(+), 7 deletions(-)
> >
> > diff --git a/scripts/mod/file2alias.c b/scripts/mod/file2alias.c
> > index 7be43697ff84..3015c0bdecb2 100644
> > --- a/scripts/mod/file2alias.c
> > +++ b/scripts/mod/file2alias.c
> > @@ -95,12 +95,20 @@ extern struct devtable *__start___devtable[], *__stop___devtable[];
> >   */
> >  #define DEF_FIELD(m, devid, f) \
> >         typeof(((struct devid *)0)->f) f = TO_NATIVE(*(typeof(f) *)((m) + OFF_##devid##_##f))
> > +
> > +/* Define a variable v that holds the address of field f of struct devid
> > + * based at address m.  Due to the way typeof works, for a field of type
> > + * T[N] the variable has type T(*)[N], _not_ T*.
> > + */
> > +#define DEF_FIELD_ADDR_VAR(m, devid, f, v) \
> > +       typeof(((struct devid *)0)->f) *v = ((m) + OFF_##devid##_##f)
> > +
> >  /* Define a variable f that holds the address of field f of struct devid
> >   * based at address m.  Due to the way typeof works, for a field of type
> >   * T[N] the variable has type T(*)[N], _not_ T*.
> >   */
> >  #define DEF_FIELD_ADDR(m, devid, f) \
> > -       typeof(((struct devid *)0)->f) *f = ((m) + OFF_##devid##_##f)
> > +       DEF_FIELD_ADDR_VAR(m, devid, f, f)
> >
> >  /* Add a table entry.  We test function type matches while we're here. */
> >  #define ADD_TO_DEVTABLE(device_id, type, function) \
> > @@ -641,25 +649,27 @@ static void do_pnp_card_entries(void *symval, unsigned long size,
> >         unsigned int i;
> >
> >         device_id_check(mod->name, "pnp", size, id_size, symval);
> > +       DEF_FIELD_ADDR(symval, pnp_card_device_id, devs);
> > +       typeof(devs) devs_last;
> >
> >         for (i = 0; i < count; i++) {
> >                 unsigned int j;
> > -               DEF_FIELD_ADDR(symval + i*id_size, pnp_card_device_id, devs);
> > +               devs_last = devs + i * id_size;
> >
> >                 for (j = 0; j < PNP_MAX_DEVICES; j++) {
> > -                       const char *id = (char *)(*devs)[j].id;
> > -                       int i2, j2;
> > +                       const char *id = (char *)(*devs_last)[j].id;
> > +                       int j2;
> >                         int dup = 0;
> >
> >                         if (!id[0])
> >                                 break;
> >
> >                         /* find duplicate, already added value */
> > -                       for (i2 = 0; i2 < i && !dup; i2++) {
> > -                               DEF_FIELD_ADDR(symval + i2*id_size, pnp_card_device_id, devs);
> > +                       while ((devs_last -= id_size) >= devs && !dup) {
> >
> >                                 for (j2 = 0; j2 < PNP_MAX_DEVICES; j2++) {
> > -                                       const char *id2 = (char *)(*devs)[j2].id;
> > +                                       const char *id2 =
> > +                                               (char *)(*devs_last)[j2].id;
> >
> >                                         if (!id2[0])
> >                                                 break;
> > --
> > 2.19.1
> >
>
>
> --
> Best Regards
> Masahiro Yamada

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

end of thread, other threads:[~2018-10-30  0:32 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-10-23  1:10 [PATCH v2 3/5] Creates macro to avoid variable shadowing Leonardo Brás
2018-10-23 21:14 ` Helen Koike
2018-10-23 21:35 ` Helen Koike
2018-10-24  1:48   ` Leonardo Bras
2018-10-28 16:37 ` Masahiro Yamada
2018-10-30  0:32   ` Leonardo Bras

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).