Linux GPIO subsystem development
 help / color / mirror / Atom feed
* [PATCH v1 1/1] gpiolib: shared: Don't define variable in the middle of the code
@ 2026-10-13  9:44 Andy Shevchenko
  2026-10-13 10:04 ` Jiri Slaby
  0 siblings, 1 reply; 4+ messages in thread
From: Andy Shevchenko @ 2026-10-13  9:44 UTC (permalink / raw)
  To: Bartosz Golaszewski, linux-gpio, linux-kernel
  Cc: Linus Walleij, Bartosz Golaszewski, Jiri Slaby, Andy Shevchenko

While the C11 allows us to define variables in the code, we do not
do this style (see more at [1]). Move the declaration to the top of
the function.

Link: https://lore.kernel.org/lkml/58fd478f408a34b578ee8d949c5c4b4da4d4f41d.camel@HansenPartnership.com/ [1]
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/gpio/gpiolib-shared.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/gpio/gpiolib-shared.c b/drivers/gpio/gpiolib-shared.c
index 5f9623e40b0f..40c08f1ae5f0 100644
--- a/drivers/gpio/gpiolib-shared.c
+++ b/drivers/gpio/gpiolib-shared.c
@@ -196,6 +196,7 @@ static int gpio_shared_of_traverse(struct device_node *curr)
 		for (i = 0; i < count; i++) {
 			struct device_node *np __free(device_node) = NULL;
 			char *con_id __free(kfree) = NULL;
+			struct fwnode_handle *curr_fwnode;
 
 			ret = of_parse_phandle_with_args(curr, prop->name,
 							 "#gpio-cells", i,
@@ -261,8 +262,7 @@ static int gpio_shared_of_traverse(struct device_node *curr)
 				con_id[con_id_len - suffix_len] = '\0';
 			}
 
-			struct fwnode_handle *curr_fwnode =
-				fwnode_handle_get(of_fwnode_handle(curr));
+			curr_fwnode = fwnode_handle_get(of_fwnode_handle(curr));
 			ref = gpio_shared_make_ref(curr_fwnode, con_id, args.args[1]);
 			if (!ref) {
 				fwnode_handle_put(curr_fwnode);
-- 
2.50.1


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

* Re: [PATCH v1 1/1] gpiolib: shared: Don't define variable in the middle of the code
  2026-10-13  9:44 [PATCH v1 1/1] gpiolib: shared: Don't define variable in the middle of the code Andy Shevchenko
@ 2026-10-13 10:04 ` Jiri Slaby
  2026-10-13 11:42   ` Bartosz Golaszewski
  0 siblings, 1 reply; 4+ messages in thread
From: Jiri Slaby @ 2026-10-13 10:04 UTC (permalink / raw)
  To: Andy Shevchenko, Bartosz Golaszewski, linux-gpio, linux-kernel
  Cc: Linus Walleij, Bartosz Golaszewski

On 13. 10. 26, 11:44, Andy Shevchenko wrote:
> While the C11 allows us to define variables in the code, we do not
> do this style (see more at [1]). Move the declaration to the top of
> the function.
> 
> Link: https://lore.kernel.org/lkml/58fd478f408a34b578ee8d949c5c4b4da4d4f41d.camel@HansenPartnership.com/ [1]

 From the link, I have just learnt two things:
1) I am not alone preferring declarations in the middle of blocks (it 
has more pros than cons IMO). But again, I spend a lot of time in the 
c++ universe.
2) the kernel defines "auto" even for < c23. Therefore:

> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> ---
>   drivers/gpio/gpiolib-shared.c | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/gpio/gpiolib-shared.c b/drivers/gpio/gpiolib-shared.c
> index 5f9623e40b0f..40c08f1ae5f0 100644
> --- a/drivers/gpio/gpiolib-shared.c
> +++ b/drivers/gpio/gpiolib-shared.c
> @@ -196,6 +196,7 @@ static int gpio_shared_of_traverse(struct device_node *curr)
>   		for (i = 0; i < count; i++) {
>   			struct device_node *np __free(device_node) = NULL;
>   			char *con_id __free(kfree) = NULL;
> +			struct fwnode_handle *curr_fwnode;
>   
>   			ret = of_parse_phandle_with_args(curr, prop->name,
>   							 "#gpio-cells", i,
> @@ -261,8 +262,7 @@ static int gpio_shared_of_traverse(struct device_node *curr)
>   				con_id[con_id_len - suffix_len] = '\0';
>   			}
>   
> -			struct fwnode_handle *curr_fwnode =
> -				fwnode_handle_get(of_fwnode_handle(curr));
> +			curr_fwnode = fwnode_handle_get(of_fwnode_handle(curr));

For me, much cleaner would be the c++ way. Ie. doing:

   auto curr_fwnode = fwnode_handle_get(of_fwnode_handle(curr));

Everyone is supposed to use clangd, tree-sitter or alike nowadays. So 
inferring the type is the very same for start-of-block-declaration 
(which is miles away from the assignment now) and auto-declaration. 
While the latter obfuscates the code less (and eases local vars addition 
w/o jumping back and forth).

>   			ref = gpio_shared_make_ref(curr_fwnode, con_id, args.args[1]);
>   			if (!ref) {
>   				fwnode_handle_put(curr_fwnode);

thanks,
-- 
js
suse labs

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

* Re: [PATCH v1 1/1] gpiolib: shared: Don't define variable in the middle of the code
  2026-10-13 10:04 ` Jiri Slaby
@ 2026-10-13 11:42   ` Bartosz Golaszewski
  2026-10-13 13:31     ` Andy Shevchenko
  0 siblings, 1 reply; 4+ messages in thread
From: Bartosz Golaszewski @ 2026-10-13 11:42 UTC (permalink / raw)
  To: Jiri Slaby
  Cc: Linus Walleij, Bartosz Golaszewski, Andy Shevchenko,
	Bartosz Golaszewski, linux-gpio, linux-kernel

On Tue, 13 Oct 2026 12:04:41 +0200, Jiri Slaby <jirislaby@kernel.org> said:
> On 13. 10. 26, 11:44, Andy Shevchenko wrote:
>> While the C11 allows us to define variables in the code, we do not
>> do this style (see more at [1]). Move the declaration to the top of
>> the function.
>>
>> Link: https://lore.kernel.org/lkml/58fd478f408a34b578ee8d949c5c4b4da4d4f41d.camel@HansenPartnership.com/ [1]
>
>  From the link, I have just learnt two things:
> 1) I am not alone preferring declarations in the middle of blocks (it
> has more pros than cons IMO). But again, I spend a lot of time in the
> c++ universe.

I actually do prefer declaring variables at the top of the function. This is
a leftover from when this variable was managed with __free().

> 2) the kernel defines "auto" even for < c23. Therefore:
>

Excuse me, what?! :O

That's the first time I'm learning that we can use C++-like auto in the kernel
C code. Thanks for bringing this to my attention!

Bart

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

* Re: [PATCH v1 1/1] gpiolib: shared: Don't define variable in the middle of the code
  2026-10-13 11:42   ` Bartosz Golaszewski
@ 2026-10-13 13:31     ` Andy Shevchenko
  0 siblings, 0 replies; 4+ messages in thread
From: Andy Shevchenko @ 2026-10-13 13:31 UTC (permalink / raw)
  To: Bartosz Golaszewski
  Cc: Jiri Slaby, Linus Walleij, Bartosz Golaszewski, linux-gpio,
	linux-kernel

On Tue, Oct 13, 2026 at 02:42:59PM +0300, Bartosz Golaszewski wrote:
> On Tue, 13 Oct 2026 12:04:41 +0200, Jiri Slaby <jirislaby@kernel.org> said:
> > On 13. 10. 26, 11:44, Andy Shevchenko wrote:
> >> While the C11 allows us to define variables in the code, we do not
> >> do this style (see more at [1]). Move the declaration to the top of
> >> the function.
> >>
> >> Link: https://lore.kernel.org/lkml/58fd478f408a34b578ee8d949c5c4b4da4d4f41d.camel@HansenPartnership.com/ [1]
> >
> >  From the link, I have just learnt two things:
> > 1) I am not alone preferring declarations in the middle of blocks (it
> > has more pros than cons IMO). But again, I spend a lot of time in the
> > c++ universe.

True, but there is also old style and it's kept like this.

> I actually do prefer declaring variables at the top of the function. This is
> a leftover from when this variable was managed with __free().
> 
> > 2) the kernel defines "auto" even for < c23. Therefore:

True, but using auto in the code will make it really hard to follow. I find it
nice in the macros, though.

> Excuse me, what?! :O
> 
> That's the first time I'm learning that we can use C++-like auto in the kernel
> C code. Thanks for bringing this to my attention!

Yeah, with some version we eventually get it standardized.

-- 
With Best Regards,
Andy Shevchenko



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

end of thread, other threads:[~2026-10-13 13:32 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-13  9:44 [PATCH v1 1/1] gpiolib: shared: Don't define variable in the middle of the code Andy Shevchenko
2026-10-13 10:04 ` Jiri Slaby
2026-10-13 11:42   ` Bartosz Golaszewski
2026-10-13 13:31     ` Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox