Skip to contentSkip to conversation

iio: light: bh1750: Move static keyword to the front of declaration

PATCH In review Krzysztof Wilczynski 5 participants 11 messages Sep 2, 2019 – Sep 15, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: light: bh1750: Move static keyword to the front of declaration

Move the static keyword to the front of declaration of bh1750_chip_info_tbl, and resolve the following compiler warning that can be seen when building with warnings enabled (W=1):

drivers/iio/light/bh1750.c:64:1: warning:
‘static’ is not at beginning of declaration [-Wold-style-declaration]

---
Related: https://lore.kernel.org/r/[email protected]

 drivers/iio/light/bh1750.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/iio/light/bh1750.c +2 -4
index 28347df78cff..460c0013f1a9 100644
@@ -42,7 +42,7 @@ struct bh1750_data {
 	u16 mtreg;
 };
 
-struct bh1750_chip_info {
+static const struct bh1750_chip_info {
 	u16 mtreg_min;
 	u16 mtreg_max;
 	u16 mtreg_default;
@@ -59,9 +59,7 @@ struct bh1750_chip_info {
 
 	u16 int_time_low_mask;
 	u16 int_time_high_mask;
-}
-
-static const bh1750_chip_info_tbl[] = {
+} bh1750_chip_info_tbl[] = {
 	[BH1710] = { 140, 1022, 300, 400,  250000000, 2, 0x001F, 0x03E0 },
 	[BH1721] = { 140, 1020, 300, 400,  250000000, 2, 0x0010, 0x03E0 },
 	[BH1750] = { 31,  254,  69,  1740, 57500000,  1, 0x001F, 0x00E0 },
Signature · 1 line
2.22.1
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 4 lines · Krzysztof Wilczynski

Move the static keyword to the front of declaration of bh1750_chip_info_tbl, and resolve the following compiler warning that can be seen when building with warnings enabled (W=1):

Looks okay.

Quoted 38 lines

drivers/iio/light/bh1750.c:64:1: warning:
‘static’ is not at beginning of declaration [-Wold-style-declaration]

---
Related: https://lore.kernel.org/r/[email protected]

 drivers/iio/light/bh1750.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/iio/light/bh1750.c +2 -5
index 28347df78cff..460c0013f1a9 100644
@@ -42,7 +42,7 @@ struct bh1750_data {
 	u16 mtreg;
 };
 
-struct bh1750_chip_info {
+static const struct bh1750_chip_info {
 	u16 mtreg_min;
 	u16 mtreg_max;
 	u16 mtreg_default;
@@ -59,9 +59,7 @@ struct bh1750_chip_info {
 
 	u16 int_time_low_mask;
 	u16 int_time_high_mask;
-}
-
-static const bh1750_chip_info_tbl[] = {
+} bh1750_chip_info_tbl[] = {
 	[BH1710] = { 140, 1022, 300, 400,  250000000, 2, 0x001F, 0x03E0 },
 	[BH1721] = { 140, 1020, 300, 400,  250000000, 2, 0x0010, 0x03E0 },
 	[BH1750] = { 31,  254,  69,  1740, 57500000,  1, 0x001F, 0x00E0 },
--

2.22.1

maintainer
#2 lore raw
In reply to Krzysztof Wilczynski
Quoted 9 lines · On Mon, 2 Sep 2019 13:31:32 +0200 Krzysztof Wilczynski <[email protected]> wrote:

Move the static keyword to the front of declaration of bh1750_chip_info_tbl, and resolve the following compiler warning that can be seen when building with warnings enabled (W=1):

drivers/iio/light/bh1750.c:64:1: warning:
‘static’ is not at beginning of declaration [-Wold-style-declaration]

This one has me confused. The warning seems to be false as static is at the beginning of the declaration....

Sure we "could" combine the declaration with the definition as you have done here, but that has nothing much to do with the warning.

What am I missing?

Jonathan

Quoted 20 lines

---
Related: https://lore.kernel.org/r/[email protected]

 drivers/iio/light/bh1750.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/iio/light/bh1750.c +1 -1
index 28347df78cff..460c0013f1a9 100644
@@ -42,7 +42,7 @@ struct bh1750_data {
 	u16 mtreg;
 };
 
-struct bh1750_chip_info {
+static const struct bh1750_chip_info {
 	u16 mtreg_min;
 	u16 mtreg_max;
 	u16 mtreg_default;

drivers/iio/light/bh1750.c struct bh1750_chip_info { · line 64

Quoted 6 lines
drivers/iio/light/bh1750.c +0 -3
@@ -59,9 +59,7 @@ struct bh1750_chip_info {
 
 	u16 int_time_low_mask;
 	u16 int_time_high_mask;
-}
-
-static const bh1750_chip_info_tbl[] = {

Looks like the beginning fo the declaration to me!

Jonathan

Quoted 4 lines
drivers/iio/light/bh1750.c +1 -0
+} bh1750_chip_info_tbl[] = {
 	[BH1710] = { 140, 1022, 300, 400,  250000000, 2, 0x001F, 0x03E0 },
 	[BH1721] = { 140, 1020, 300, 400,  250000000, 2, 0x0010, 0x03E0 },
 	[BH1750] = { 31,  254,  69,  1740, 57500000,  1, 0x001F, 0x00E0 },
#3 lore raw

Hello Jonathan,

Thank you for feedback.

[...]

Quoted 2 lines
Quoted 2 lines

drivers/iio/light/bh1750.c:64:1: warning:
‘static’ is not at beginning of declaration [-Wold-style-declaration]

[...]

Quoted 5 lines

This one has me confused. The warning seems to be false as static is at the beginning of the declaration....

Sure we "could" combine the declaration with the definition as you have done here, but that has nothing much to do with the warning.

[...]

I only moved the "static const" at the front, I haven't changed the code as it's already has been a declaration and definition. There is no semicolon there and the original author put a newline to separate things which makes it look as if these were separate.

Simple example based on the existing code:

https://godbolt.org/z/hV4HP7

I hope this helps to illustrate the change in the patch. I apologise if my approach was incorrect.

As part of the patch I removed the newline in an aim to make it less confusing to anyone who will read the code in the future. Especially, since it makes it a bit awkward to read and when using things like grep.

Krzysztof

#4 lore raw
Quoted 27 lines · On Sun, 8 Sep 2019 15:52:09 +0200 Krzysztof Wilczynski <[email protected]> wrote:

Hello Jonathan,

Thank you for feedback.

[...]

Quoted 2 lines
Quoted 2 lines

drivers/iio/light/bh1750.c:64:1: warning:
‘static’ is not at beginning of declaration [-Wold-style-declaration]

[...]

Quoted 5 lines

This one has me confused. The warning seems to be false as static is at the beginning of the declaration....

Sure we "could" combine the declaration with the definition as you have done here, but that has nothing much to do with the warning.

[...]

I only moved the "static const" at the front, I haven't changed the code as it's already has been a declaration and definition. There is no semicolon there and the original author put a newline to separate things which makes it look as if these were separate.

Simple example based on the existing code:

https://godbolt.org/z/hV4HP7

I hope this helps to illustrate the change in the patch. I apologise if my approach was incorrect.

Quoted 6 lines

As part of the patch I removed the newline in an aim to make it less confusing to anyone who will read the code in the future. Especially, since it makes it a bit awkward to read and when using things like grep.

Krzysztof

I get what you are trying to do, the issue is the code is currently:

struct bh1750_chip_info {
	u16 mtreg_min;
	u16 mtreg_max;
	u16 mtreg_default;
	int mtreg_to_usec;
	int mtreg_to_scale;

	/*
	 * For BH1710/BH1721 all possible integration time values won't fit
	 * into one page so displaying is limited to every second one.
	 * Note, that user can still write proper values which were not
	 * listed.
	 */
	int inc;

	u16 int_time_low_mask;
	u16 int_time_high_mask;
}
static const bh1750_chip_info_tbl[] = {
	[BH1710] = { 140, 1022, 300, 400,  250000000, 2, 0x001F, 0x03E0 },
	[BH1721] = { 140, 1020, 300, 400,  250000000, 2, 0x0010, 0x03E0 },
	[BH1750] = { 31,  254,  69,  1740, 57500000,  1, 0x001F, 0x00E0 },
};

That test is supposed to catch the second block being

const static bh1750_chip_info_tbl[] = {
...

Which it isn't. So the issue here was never that the static keyword wasn't at the front of the declaration but that we could save a tiny bit code by using the pattern

static const struct bh1750_chip_info {
...

} bh1750_chip_info_tbl[] {
	[...] = ...
};

We can do that of course, but that's nothing to do with moving the static keyword to the front of the declaration which is what the patch claims to be doing.

Jonathan

#5 lore raw

Hello Jonathan,

Thank you for the feedback. I really appreciate it.

[...]

Quoted 3 lines

We can do that of course, but that's nothing to do with moving the static keyword to the front of the declaration which is what the patch claims to be doing.

I see your point. I am going to send a v2 that hopefully will be much cleaner and better worded. Thank you!

Krzysztof

#6 lore raw

PATCH v2 1/1 iio: light: bh1750: Resolve compiler warning and make code more readable

In reply to Krzysztof Wilczynski

Separate the declaration from definition of bh1750_chip_info_tbl to make the code more readable. Separating the code will resolve the following compiler warning that can be seen when building with warnings enabled (W=1):

drivers/iio/light/bh1750.c:64:1: warning:
‘static’ is not at beginning of declaration [-Wold-style-declaration]

---
Changes in v2:
Made definition of bh1750_chip_info_tbl separate from declaration as per the review feedback. This also makes the code more readable. Updated wording of the subject and the commit message.

 drivers/iio/light/bh1750.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/light/bh1750.c +2 -2
index 28347df78cff..adb5ab9e3439 100644
@@ -59,9 +59,9 @@ struct bh1750_chip_info {
 
 	u16 int_time_low_mask;
 	u16 int_time_high_mask;
-}
+};
 
-static const bh1750_chip_info_tbl[] = {
+static const struct bh1750_chip_info bh1750_chip_info_tbl[] = {
 	[BH1710] = { 140, 1022, 300, 400,  250000000, 2, 0x001F, 0x03E0 },
 	[BH1721] = { 140, 1020, 300, 400,  250000000, 2, 0x0010, 0x03E0 },
 	[BH1750] = { 31,  254,  69,  1740, 57500000,  1, 0x001F, 0x00E0 },
Signature · 1 line
2.23.0
Acked-by on v2 1/1
#7 lore raw

Re: [PATCH v2] iio: light: bh1750: Resolve compiler warning and make code more readable

Quoted 4 lines · Krzysztof Wilczynski

Separate the declaration from definition of bh1750_chip_info_tbl to make the code more readable. Separating the code will resolve the following compiler warning that can be seen when building with warnings enabled (W=1):

This commit log isn't completely accurate. Before we had a definition of a struct that was instantly (i.e. without repeating the type name) used as type for an array. (And not a declaration and definition of bh1750_chip_info_tbl.)

So I'd write:

Don't mix declaring struct bh1750_chip_info and defining bh1750_chip_info_tbl[] in a single statement. This is hard to read, with warnings enabled gcc warns about the unusual position of the keyword static:

drivers/iio/light/bh1750.c:64:1: warning: ‘static’ is not at beginning of declaration [-Wold-style-declaration]

and with the empty line this looks as if bh1750_chip_info_tbl had no explicit type.

If you want add

	Fixes: 3a11fbb037a1 ("iio: light: add support for ROHM BH1710/BH1715/BH1721/BH1750/BH1751 ambient light sensors")

and add Tomasz Duszynski <[email protected]> to Cc who authored 3a11fbb037a1.

Quoted 15 lines

---
Changes in v2:
Made definition of bh1750_chip_info_tbl separate from declaration as per the review feedback. This also makes the code more readable. Updated wording of the subject and the commit message.

 drivers/iio/light/bh1750.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/light/bh1750.c +0 -0
index 28347df78cff..adb5ab9e3439 100644
@@ -59,9 +59,9 @@ struct bh1750_chip_info {
 

drivers/iio/light/bh1750.c struct bh1750_chip_info { · line 67

Quoted 10 lines
drivers/iio/light/bh1750.c +2 -2
 	u16 int_time_low_mask;
 	u16 int_time_high_mask;
-}
+};
 
-static const bh1750_chip_info_tbl[] = {
+static const struct bh1750_chip_info bh1750_chip_info_tbl[] = {
 	[BH1710] = { 140, 1022, 300, 400,  250000000, 2, 0x001F, 0x03E0 },
 	[BH1721] = { 140, 1020, 300, 400,  250000000, 2, 0x0010, 0x03E0 },
 	[BH1750] = { 31,  254,  69,  1740, 57500000,  1, 0x001F, 0x00E0 },

The patch looks fine. If you adapt the commit log as suggested above take my

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#8 lore raw

PATCH v3 1/1 iio: light: bh1750: Resolve compiler warning and make code more readable

In reply to Krzysztof Wilczynski

Separate the declaration of struct bh1750_chip_info from definition of bh1750_chip_info_tbl[] in a single statement as it makes the code hard to read, and with the extra newline it makes it look as if the bh1750_chip_info_tbl[] had no explicit type.

This change also resolves the following compiler warning about the unusual position of the static keyword that can be seen when building with warnings enabled (W=1):

drivers/iio/light/bh1750.c:64:1: warning:
‘static’ is not at beginning of declaration [-Wold-style-declaration]

Related to commit 3a11fbb037a1 ("iio: light: add support for ROHM BH1710/BH1715/BH1721/BH1750/BH1751 ambient light sensors").

---
Changes in v3:
Updated wording of the commit message as per the review feedback.

Changes in v2:
Made definition of bh1750_chip_info_tbl separate from declaration as per the review feedback. This also makes the code more readable. Updated wording of the subject and the commit message.

 drivers/iio/light/bh1750.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/light/bh1750.c +2 -2
index 28347df78cff..adb5ab9e3439 100644
@@ -59,9 +59,9 @@ struct bh1750_chip_info {
 
 	u16 int_time_low_mask;
 	u16 int_time_high_mask;
-}
+};
 
-static const bh1750_chip_info_tbl[] = {
+static const struct bh1750_chip_info bh1750_chip_info_tbl[] = {
 	[BH1710] = { 140, 1022, 300, 400,  250000000, 2, 0x001F, 0x03E0 },
 	[BH1721] = { 140, 1020, 300, 400,  250000000, 2, 0x0010, 0x03E0 },
 	[BH1750] = { 31,  254,  69,  1740, 57500000,  1, 0x001F, 0x00E0 },
Signature · 1 line
2.23.0
maintainer
#9 lore raw

Re: [PATCH v3] iio: light: bh1750: Resolve compiler warning and make code more readable

Quoted 9 lines · On Fri, 13 Sep 2019 22:24:13 +0200 Krzysztof Wilczynski <[email protected]> wrote:

Separate the declaration of struct bh1750_chip_info from definition of bh1750_chip_info_tbl[] in a single statement as it makes the code hard to read, and with the extra newline it makes it look as if the bh1750_chip_info_tbl[] had no explicit type.

This change also resolves the following compiler warning about the unusual position of the static keyword that can be seen when building with warnings enabled (W=1):

Quoted 7 lines

drivers/iio/light/bh1750.c:64:1: warning:
‘static’ is not at beginning of declaration [-Wold-style-declaration]

Related to commit 3a11fbb037a1 ("iio: light: add support for ROHM BH1710/BH1715/BH1721/BH1750/BH1751 ambient light sensors").

Applied to the togreg branch of iio.git and pushed out as testing for the autobuilders to play with it. Added Uwe's Acked-by as well.

Sorry for the confusing advice I gave earlier, I managed to confuse myself on what the original code said.

Jonathan

Quoted 28 lines

---
Changes in v3:
Updated wording of the commit message as per the review feedback.

Changes in v2:
Made definition of bh1750_chip_info_tbl separate from declaration as per the review feedback. This also makes the code more readable. Updated wording of the subject and the commit message.

 drivers/iio/light/bh1750.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/light/bh1750.c +2 -2
index 28347df78cff..adb5ab9e3439 100644
@@ -59,9 +59,9 @@ struct bh1750_chip_info {
 
 	u16 int_time_low_mask;
 	u16 int_time_high_mask;
-}
+};
 
-static const bh1750_chip_info_tbl[] = {
+static const struct bh1750_chip_info bh1750_chip_info_tbl[] = {
 	[BH1710] = { 140, 1022, 300, 400,  250000000, 2, 0x001F, 0x03E0 },
 	[BH1721] = { 140, 1020, 300, 400,  250000000, 2, 0x0010, 0x03E0 },
 	[BH1750] = { 31,  254,  69,  1740, 57500000,  1, 0x001F, 0x00E0 },
#10 lore raw

Re: [PATCH v3] iio: light: bh1750: Resolve compiler warning and make code more readable

Hello Jonathan,

[...]

Quoted 2 lines

Applied to the togreg branch of iio.git and pushed out as testing for the autobuilders to play with it. Added Uwe's Acked-by as well.

Thank you!

Quoted 2 lines

Sorry for the confusing advice I gave earlier, I managed to confuse myself on what the original code said.

No problem. :) Uwe suggested to me how to clean things up and word the commit message more appropriately, thus a very good learning experience for me.

Thank you again!

Krzysztof