Skip to contentSkip to conversation

docs/style: permit inline loop variables

RFC PATCH In review Alex Bennée 7 participants 9 messages Aug 22, 2023 – Aug 31, 2023

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 docs/style: permit inline loop variables

I've already wasted enough of my time debugging aliased variables in deeply nested loops. While not scattering variable declarations around is a good aim I think we can make an exception for stuff used inside a loop.

---
 docs/devel/style.rst | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
docs/devel/style.rst +8 -1
index 3cfcdeb9cd..2f68b50079 100644
@@ -204,7 +204,14 @@ Declarations
 
 Mixed declarations (interleaving statements and declarations within
 blocks) are generally not allowed; declarations should be at the beginning
-of blocks.
+of blocks. To avoid accidental re-use it is permissible to declare
+loop variables inside for loops:
+
+.. code-block:: c
+
+    for (int i = 0; i < ARRAY_SIZE(thing); i++) {
+        /* do something loopy */
+    }
 
 Every now and then, an exception is made for declarations inside a
 #ifdef or #ifndef block: if the code looks nicer, such declarations can
Signature · 1 line
2.39.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Alex Bennée

I've already wasted enough of my time debugging aliased variables in deeply nested loops. While not scattering variable declarations around is a good aim I think we can make an exception for stuff used inside a loop.

---
 docs/devel/style.rst | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
Quoted 25 lines
docs/devel/style.rst +8 -2
index 3cfcdeb9cd..2f68b50079 100644
@@ -204,7 +204,14 @@ Declarations
 
 Mixed declarations (interleaving statements and declarations within
 blocks) are generally not allowed; declarations should be at the beginning
-of blocks.
+of blocks. To avoid accidental re-use it is permissible to declare
+loop variables inside for loops:
+
+.. code-block:: c
+
+    for (int i = 0; i < ARRAY_SIZE(thing); i++) {
+        /* do something loopy */
+    }
 
 Every now and then, an exception is made for declarations inside a
 #ifdef or #ifndef block: if the code looks nicer, such declarations can
--

2.39.2

#2 lore raw
In reply to Alex Bennée
Quoted 2 lines · Alex Bennée

I've already wasted enough of my time debugging aliased variables in deeply nested loops.

In theory we could try to enable -Wshadow and deal with all the existing cases of aliasing, which would then allow us to turn it into an error and catch your bugs :-)

Anyway, I think declaration-in-for-loop is OK and we already have quite a lot of instances of it.

-- PMM

#3 lore raw
Quoted 7 lines · Peter Maydell
Quoted 2 lines · Alex Bennée

I've already wasted enough of my time debugging aliased variables in deeply nested loops.

In theory we could try to enable -Wshadow and deal with all the existing cases of aliasing, which would then allow us to turn it into an error and catch your bugs :-)

In practice, a quick compile with -Wshadow -Wno-error=shadow coughs up almost 6000 warnings. There are duplicates since we compile many files multiple times, so I piped through sort -u | wc -l, and got about 1200.

Quoted 2 lines

Anyway, I think declaration-in-for-loop is OK and we already have quite a lot of instances of it.

#4 lore raw
Quoted 7 lines · Markus Armbruster
Quoted 4 lines · Peter Maydell
Quoted 2 lines · Alex Bennée

I've already wasted enough of my time debugging aliased variables in deeply nested loops.

Quoted 7 lines
Quoted 3 lines

In theory we could try to enable -Wshadow and deal with all the existing cases of aliasing, which would then allow us to turn it into an error and catch your bugs :-)

In practice, a quick compile with -Wshadow -Wno-error=shadow coughs up almost 6000 warnings. There are duplicates since we compile many files multiple times, so I piped through sort -u | wc -l, and got about 1200.

-Wshadow=local has only 211 non-duplicate warnings, which is almost tractable...

(A lot of the duplicates are from local variables declared in macros like MAX(), MIN() and QOBJECT(), when those macros are used in a nested way, like MIN(MIN(x,y),z). We could deal with those by using the __COUNTER__ trick, I guess.)

-- PMM

#5 lore raw
Quoted 16 lines · Peter Maydell
Quoted 7 lines · Markus Armbruster
Quoted 4 lines · Peter Maydell
Quoted 2 lines · Alex Bennée

I've already wasted enough of my time debugging aliased variables in deeply nested loops.

Quoted 7 lines
Quoted 3 lines

In theory we could try to enable -Wshadow and deal with all the existing cases of aliasing, which would then allow us to turn it into an error and catch your bugs :-)

In practice, a quick compile with -Wshadow -Wno-error=shadow coughs up almost 6000 warnings. There are duplicates since we compile many files multiple times, so I piped through sort -u | wc -l, and got about 1200.

Quoted 7 lines

-Wshadow=local has only 211 non-duplicate warnings, which is almost tractable...

(A lot of the duplicates are from local variables declared in macros like MAX(), MIN() and QOBJECT(), when those macros are used in a nested way, like MIN(MIN(x,y),z). We could deal with those by using the __COUNTER__ trick, I guess.)

Oooh, preprocessor trickery!

I posted "[PATCH 0/7] Steps towards enabling -Wshadow=local". Need help to get the job finished.

Reviewed-by on v1 1/1
#6 lore raw
In reply to Alex Bennée
Quoted 9 lines · Alex Bennée

I've already wasted enough of my time debugging aliased variables in deeply nested loops. While not scattering variable declarations around is a good aim I think we can make an exception for stuff used inside a loop.

---
  docs/devel/style.rst | 9 ++++++++-
  1 file changed, 8 insertions(+), 1 deletion(-)

r~

Reviewed-by on v1 1/1
#7 lore raw
In reply to Alex Bennée
Quoted 17 lines · Alex Bennée

I've already wasted enough of my time debugging aliased variables in deeply nested loops. While not scattering variable declarations around is a good aim I think we can make an exception for stuff used inside a loop.

---
  docs/devel/style.rst | 9 ++++++++-
  1 file changed, 8 insertions(+), 1 deletion(-)
docs/devel/style.rst +0 -0
index 3cfcdeb9cd..2f68b50079 100644
@@ -204,7 +204,14 @@ Declarations
 
  Mixed declarations (interleaving statements and declarations within

docs/devel/style.rst Declarations · line 214

Quoted 10 lines
docs/devel/style.rst +8 -1
  blocks) are generally not allowed; declarations should be at the beginning
-of blocks.
+of blocks. To avoid accidental re-use it is permissible to declare
+loop variables inside for loops:
+
+.. code-block:: c
+
+    for (int i = 0; i < ARRAY_SIZE(thing); i++) {
+        /* do something loopy */
+    }
Reviewed-by on v1 1/1
#8 lore raw
In reply to Alex Bennée
Quoted 15 lines · Alex Bennée

I've already wasted enough of my time debugging aliased variables in deeply nested loops. While not scattering variable declarations around is a good aim I think we can make an exception for stuff used inside a loop.

---
  docs/devel/style.rst | 9 ++++++++-
  1 file changed, 8 insertions(+), 1 deletion(-)
docs/devel/style.rst +0 -0
index 3cfcdeb9cd..2f68b50079 100644

docs/devel/style.rst Declarations · line 212

Quoted 10 lines
docs/devel/style.rst +6 -1
@@ -204,7 +204,14 @@ Declarations
 
  Mixed declarations (interleaving statements and declarations within
  blocks) are generally not allowed; declarations should be at the beginning
-of blocks.
+of blocks. To avoid accidental re-use it is permissible to declare
+loop variables inside for loops:
+
+.. code-block:: c
+
+    for (int i = 0; i < ARRAY_SIZE(thing); i++) {

ARRAY_SIZE() -> sizeof() -> size_t -> unsigned.

Is it a good example to use 'int' here?

Otherwise, glad to declare variables in loops!

Quoted 5 lines
docs/devel/style.rst +2 -0
+        /* do something loopy */
+    }
 
  Every now and then, an exception is made for declarations inside a
  #ifdef or #ifndef block: if the code looks nicer, such declarations can