Skip to contentSkip to conversation

gpiolib: shared: Don't define variable in the middle of the code

PATCH In review Andy Shevchenko 3 participants 5 messages Oct 13 – Oct 14 GPIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpiolib: shared: Don't define variable in the middle of the code

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.

---
 drivers/gpio/gpiolib-shared.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpio/gpiolib-shared.c +2 -2
index 5f9623e40b0f..40c08f1ae5f0 100644
@@ -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);
Signature · 1 line
2.50.1
#1 lore raw
Quoted 5 lines · 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.

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:

Quoted 18 lines
---
  drivers/gpio/gpiolib-shared.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpio/gpiolib-shared.c +1 -0
index 5f9623e40b0f..40c08f1ae5f0 100644
@@ -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,

drivers/gpio/gpiolib-shared.c static int gpio_shared_of_traverse(struct device_node *curr) · line 265

Quoted 6 lines
drivers/gpio/gpiolib-shared.c +1 -2
@@ -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).

drivers/gpio/gpiolib-shared.c static int gpio_shared_of_traverse(struct device_node *curr) · line 268

Quoted 3 lines
drivers/gpio/gpiolib-shared.c +0 -0
  			ref = gpio_shared_make_ref(curr_fwnode, con_id, args.args[1]);
  			if (!ref) {
  				fwnode_handle_put(curr_fwnode);

thanks,

Signature · 2 lines
js
suse labs
#2 lore raw
Quoted 11 lines · Jiri Slaby
Quoted 5 lines · 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.

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().

Quoted 2 lines

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

#3 lore raw
Quoted 6 lines · Bartosz Golaszewski
Quoted 5 lines · Jiri Slaby
Quoted 4 lines · 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.

Quoted 6 lines
Quoted 6 lines
Quoted 1 line

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.

Quoted 4 lines

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

Quoted 1 line

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.

Quoted 4 lines

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.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw
In reply to Andy Shevchenko
Quoted 7 lines · 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.

---

Doesn't apply on top of gpio/for-next.

While at it: the subject should be "gpio: shared: ..." for consistency.

Bart