docs/style: permit inline loop variables
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
@@ -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
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.
Signed-off-by Alex Bennée <[email protected]>--- docs/devel/style.rst | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
Quoted 25 lines
docs/devel/style.rst +8
-2@@ -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
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
Markus Armbruster
Acked-byQuoted 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.
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
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()andQOBJECT(), 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.
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.
Signed-off-by Alex Bennée<[email protected]>--- docs/devel/style.rst | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
r~
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.
Signed-off-by Alex Bennée <[email protected]>--- docs/devel/style.rst | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)docs/devel/style.rst +0
-0@@ -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
-1blocks) 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 */ + }
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.
Signed-off-by Alex Bennée <[email protected]>--- docs/devel/style.rst | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)docs/devel/style.rst +0
-0
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