Skip to contentSkip to conversation

devtools: allow variable declaration inside for loop

PATCH Superseded Ferruh Yigit 5 participants 9 messages May 3, 2023 – Jul 20, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 devtools: allow variable declaration inside for loop

Declaring variable inside for loop is not supported via C89 and it was checked in checkpatch.sh via commit [1].
But as DPDK supported C standard is becoming C99 [2], declaring variable inside loop can be allowed.

[1]
Commit 43e73483a4b8 ("devtools: forbid variable declaration inside for")

[2]
https://dpdk.org/patch/121912

---
Cc: Bruce Richardson <[email protected]>
Cc: David Marchand <[email protected]>

---
 devtools/checkpatches.sh | 8 --------
 1 file changed, 8 deletions(-)
devtools/checkpatches.sh +0 -8
index 15d5d6709445..b5baf6f2b161 100755
@@ -78,14 +78,6 @@ check_forbidden_additions() { # <patch>
 		-f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
 		"$1" || res=1
 
-	# forbid variable declaration inside "for" loop
-	awk -v FOLDERS='.' \
-		-v EXPRESSIONS='for[[:space:]]*\\((char|u?int|unsigned|s?size_t)' \
-		-v RET_ON_FAIL=1 \
-		-v MESSAGE='Declaring a variable inside for()' \
-		-f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
-		"$1" || res=1
-
 	# refrain from new additions of 16/32/64 bits rte_atomicNN_xxx()
 	awk -v FOLDERS="lib drivers app examples" \
 		-v EXPRESSIONS="rte_atomic[0-9][0-9]_.*\\\(" \
Signature · 1 line
2.34.1
#1 lore raw
Quoted 8 lines · Ferruh Yigit

Declaring variable inside for loop is not supported via C89 and it was checked in checkpatch.sh via commit [1]. But as DPDK supported C standard is becoming C99 [2], declaring variable inside loop can be allowed.

[1] Commit 43e73483a4b8 ("devtools: forbid variable declaration inside for")

Quoted 7 lines

[2] https://dpdk.org/patch/121912

Richardson <[email protected]> Cc: David Marchand <[email protected]> --- devtools/checkpatches.sh | 8 -------- 1 file changed, 8 deletions(-)

Definite +1 from me for allowing this. However, is the plan still to move to C99 in this release. I thought we were just going to jump to C11 in 23.11 release? However, I can't see any compilers refusing this if we do relax things a bit now.

I was thinking that our coding standards doc might need an update for this, but I don't see the restriction on not doing this documented there, so it seems no doc change is necessary.

/Bruce

#2 lore raw
Quoted 17 lines · Bruce Richardson
Quoted 8 lines · Ferruh Yigit

Declaring variable inside for loop is not supported via C89 and it was checked in checkpatch.sh via commit [1]. But as DPDK supported C standard is becoming C99 [2], declaring variable inside loop can be allowed.

[1] Commit 43e73483a4b8 ("devtools: forbid variable declaration inside for")

Quoted 7 lines

[2] https://dpdk.org/patch/121912

Richardson <[email protected]> Cc: David Marchand <[email protected]> --- devtools/checkpatches.sh | 8 -------- 1 file changed, 8 deletions(-)

Quoted 4 lines

Definite +1 from me for allowing this. However, is the plan still to move to C99 in this release. I thought we were just going to jump to C11 in 23.11 release? However, I can't see any compilers refusing this if we do relax things a bit now.

I will update the commit log for target as C99/C11 .

Quoted 5 lines

I was thinking that our coding standards doc might need an update for this, but I don't see the restriction on not doing this documented there, so it seems no doc change is necessary.

Commit 43e73483a4b8 refers to following document:

http://doc.dpdk.org/guides/contributing/coding_style.html#local-variables

Which has: "Variables should be declared at the start of a block of code rather than in the middle."

Although it is comparing between declaring start of a block and middle of the code, maybe we can update the document to explicitly state that declaring variable inside for loop is allowed to prevent confusion.

Let me send a new version with documentation update.

#3 lore raw

PATCH v2 1/1 devtools: allow variable declaration inside for loop

In reply to Ferruh Yigit

Declaring variable inside for loop is not supported via C89 and it was checked in checkpatch.sh via commit [1].
But as DPDK supported C standard is becoming C99/C11 [2], declaring variable inside loop can be allowed.

[1]
Commit 43e73483a4b8 ("devtools: forbid variable declaration inside for")

[2]
https://dpdk.org/patch/121912

---
Cc: Bruce Richardson <[email protected]>
Cc: David Marchand <[email protected]>

v2:
* Update coding convention too

---
 devtools/checkpatches.sh                 | 8 --------
 doc/guides/contributing/coding_style.rst | 1 +
 2 files changed, 1 insertion(+), 8 deletions(-)
devtools/checkpatches.sh +0 -8
index 15d5d6709445..b5baf6f2b161 100755
@@ -78,14 +78,6 @@ check_forbidden_additions() { # <patch>
 		-f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
 		"$1" || res=1
 
-	# forbid variable declaration inside "for" loop
-	awk -v FOLDERS='.' \
-		-v EXPRESSIONS='for[[:space:]]*\\((char|u?int|unsigned|s?size_t)' \
-		-v RET_ON_FAIL=1 \
-		-v MESSAGE='Declaring a variable inside for()' \
-		-f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
-		"$1" || res=1
-
 	# refrain from new additions of 16/32/64 bits rte_atomicNN_xxx()
 	awk -v FOLDERS="lib drivers app examples" \
 		-v EXPRESSIONS="rte_atomic[0-9][0-9]_.*\\\(" \
doc/guides/contributing/coding_style.rst +1 -0
index 89db6260cfbf..e18b8d4439ea 100644
@@ -558,6 +558,7 @@ Local Variables
 
 * Variables should be declared at the start of a block of code rather than in the middle.
   The exception to this is when the variable is ``const`` in which case the declaration must be at the point of first use/assignment.
+  Declaring variable inside a for loop is OK.
 * When declaring variables in functions, multiple variables per line are OK.
   However, if multiple declarations would cause the line to exceed a reasonable line length, begin a new set of declarations on the next line rather than using a line continuation.
 * Be careful to not obfuscate the code by initializing variables in the declarations, only the last variable on a line should be initialized.
Signature · 1 line
2.34.1
Acked-by on v2 1/1
#4 lore raw
Quoted 11 lines · Ferruh Yigit

Declaring variable inside for loop is not supported via C89 and it was checked in checkpatch.sh via commit [1].
But as DPDK supported C standard is becoming C99/C11 [2], declaring variable inside loop can be allowed.

[1]
Commit 43e73483a4b8 ("devtools: forbid variable declaration inside for")

[2]
https://dpdk.org/patch/121912

Quoted 8 lines

---
Cc: Bruce Richardson <[email protected]>
Cc: David Marchand <[email protected]>

v2:
* Update coding convention too
---

Quoted 28 lines
 devtools/checkpatches.sh                 | 8 --------
 doc/guides/contributing/coding_style.rst | 1 +
 2 files changed, 1 insertion(+), 8 deletions(-)
devtools/checkpatches.sh +0 -8
index 15d5d6709445..b5baf6f2b161 100755
@@ -78,14 +78,6 @@ check_forbidden_additions() { # <patch>
 		-f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
 		"$1" || res=1
 
-	# forbid variable declaration inside "for" loop
-	awk -v FOLDERS='.' \
-		-v EXPRESSIONS='for[[:space:]]*\\((char|u?int|unsigned|s?size_t)' \
-		-v RET_ON_FAIL=1 \
-		-v MESSAGE='Declaring a variable inside for()' \
-		-f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
-		"$1" || res=1
-
 	# refrain from new additions of 16/32/64 bits rte_atomicNN_xxx()
 	awk -v FOLDERS="lib drivers app examples" \
 		-v EXPRESSIONS="rte_atomic[0-9][0-9]_.*\\\(" \
doc/guides/contributing/coding_style.rst +0 -0
index 89db6260cfbf..e18b8d4439ea 100644

doc/guides/contributing/coding_style.rst Local Variables · line 559

Quoted 2 lines
doc/guides/contributing/coding_style.rst +0 -0
@@ -558,6 +558,7 @@ Local Variables
 
 * Variables should be declared at the start of a block of code rather than in the middle.

I'd love to see this restriction removed in future too. Having a variable declared on first use in the middle of block I find a far easier way of working as a) it saves scrolling to look for variable definitions and b) it makes it far easier when adding/removing blocks of code e.g. commenting out for testing, to have all the code together rather than having variables at the top to add/remove also.

Quoted 8 lines
doc/guides/contributing/coding_style.rst +1 -0
   The exception to this is when the variable is ``const`` in which case the declaration must be at the point of first use/assignment.
+  Declaring variable inside a for loop is OK.
 * When declaring variables in functions, multiple variables per line are OK.
   However, if multiple declarations would cause the line to exceed a reasonable line length, begin a new set of declarations on the next line rather than using a line continuation.
 * Be careful to not obfuscate the code by initializing variables in the declarations, only the last variable on a line should be initialized.
Signature · 1 line
2.34.1
#5 lore raw
Quoted 21 lines

From: Bruce Richardson [mailto:[email protected]]
Sent: Wednesday, 3 May 2023 12.57

Quoted 7 lines · Ferruh Yigit

Declaring variable inside for loop is not supported via C89 and it was checked in checkpatch.sh via commit [1].
But as DPDK supported C standard is becoming C99/C11 [2], declaring variable inside loop can be allowed.

[1]
Commit 43e73483a4b8 ("devtools: forbid variable declaration inside

for")

Quoted 9 lines

[2]
https://dpdk.org/patch/121912

---
Cc: Bruce Richardson <[email protected]>
Cc: David Marchand <[email protected]>

Quoted 5 lines
Quoted 3 lines

v2:
* Update coding convention too
---

[...]

Quoted 5 lines

Review Local Variables · line 559

Quoted 3 lines
(quoted diff) +0 -0
@@ -558,6 +558,7 @@ Local Variables
 
 * Variables should be declared at the start of a block of code rather

than in the middle.

Quoted 10 lines

I'd love to see this restriction removed in future too. Having a variable
declared on first use in the middle of block I find a far easier way of working as a) it saves scrolling to look for variable definitions and b) it
makes it far easier when adding/removing blocks of code e.g. commenting out
for testing, to have all the code together rather than having variables at
the top to add/remove also.

And c) Initializing the variables close to where they are used the first time reduces the risk of initializing them incorrectly. Especially when modifying a block of code, initialization of its variables might be missed if out of sight. (Although this is probably a consequence of "a)".)

I consider it old style to only declare variables at the start of a block of code, and this style of coding should be considered obsolete.

If you are really old (like me?), you might remember when function parameters were provided like this:

int main(argc, argv)
int argc;
char *argv[];

{
	return(0);
}

We have moved on from that to a more modern coding style a long time ago. We should also move on to a more modern coding style regarding variable declarations.

Quoted 15 lines

Review Local Variables · line 560

Quoted 2 lines
(quoted diff) +0 -0
   The exception to this is when the variable is ``const`` in which

case the declaration must be at the point of first use/assignment.

Review Local Variables · line 562

Quoted 2 lines
(quoted diff) +1 -0
+  Declaring variable inside a for loop is OK.
 * When declaring variables in functions, multiple variables per line

are OK.

Review Local Variables · line 563

Quoted 1 line
(quoted diff) +0 -0
   However, if multiple declarations would cause the line to exceed a

reasonable line length, begin a new set of declarations on the next line rather than using a line continuation.

Review Local Variables · line 564

Quoted 1 line
(quoted diff) +0 -0
 * Be careful to not obfuscate the code by initializing variables in

the declarations, only the last variable on a line should be initialized.

Quoted 3 lines

--
2.34.1

#6 lore raw

03/05/2023 14:19, Morten Brørup:

Quoted 53 lines
Quoted 21 lines

From: Bruce Richardson [mailto:[email protected]]
Sent: Wednesday, 3 May 2023 12.57

Quoted 7 lines · Ferruh Yigit

Declaring variable inside for loop is not supported via C89 and it was checked in checkpatch.sh via commit [1].
But as DPDK supported C standard is becoming C99/C11 [2], declaring variable inside loop can be allowed.

[1]
Commit 43e73483a4b8 ("devtools: forbid variable declaration inside

for")

Quoted 9 lines

[2]
https://dpdk.org/patch/121912

---
Cc: Bruce Richardson <[email protected]>
Cc: David Marchand <[email protected]>

Quoted 5 lines
Quoted 3 lines

v2:
* Update coding convention too
---

[...]

Quoted 5 lines

Review Local Variables · line 559

Quoted 3 lines
(quoted diff) +0 -0
@@ -558,6 +558,7 @@ Local Variables
 
 * Variables should be declared at the start of a block of code rather

than in the middle.

Quoted 10 lines

I'd love to see this restriction removed in future too. Having a variable
declared on first use in the middle of block I find a far easier way of working as a) it saves scrolling to look for variable definitions and b) it
makes it far easier when adding/removing blocks of code e.g. commenting out
for testing, to have all the code together rather than having variables at
the top to add/remove also.

And c) Initializing the variables close to where they are used the first time reduces the risk of initializing them incorrectly. Especially when modifying a block of code, initialization of its variables might be missed if out of sight. (Although this is probably a consequence of "a)".)

I consider it old style to only declare variables at the start of a block of code, and this style of coding should be considered obsolete.

If you are really old (like me?), you might remember when function parameters were provided like this:

Quoted 8 lines

int main(argc, argv)
int argc;
char *argv[];

{
	return(0);
}

We have moved on from that to a more modern coding style a long time ago. We should also move on to a more modern coding style regarding variable declarations.

Old men are used to look for variable types at the beginning of functions. Having only new code adopting a different style may be confusing a little. Note I'm not against it, just asking for more feedbacks.

#7 lore raw
Quoted 60 lines · Thomas Monjalon

03/05/2023 14:19, Morten Brørup:

Quoted 59 lines
Quoted 21 lines

From: Bruce Richardson [mailto:[email protected]]
Sent: Wednesday, 3 May 2023 12.57

Quoted 7 lines · Ferruh Yigit

Declaring variable inside for loop is not supported via C89 and it was checked in checkpatch.sh via commit [1].
But as DPDK supported C standard is becoming C99/C11 [2], declaring variable inside loop can be allowed.

[1]
Commit 43e73483a4b8 ("devtools: forbid variable declaration inside

for")

Quoted 9 lines

[2]
https://dpdk.org/patch/121912

---
Cc: Bruce Richardson <[email protected]>
Cc: David Marchand <[email protected]>

Quoted 5 lines
Quoted 3 lines

v2:
* Update coding convention too
---

[...]

Quoted 5 lines

Review Local Variables · line 559

Quoted 3 lines
(quoted diff) +0 -0
@@ -558,6 +558,7 @@ Local Variables
 
 * Variables should be declared at the start of a block of code rather

than in the middle.

Quoted 10 lines

I'd love to see this restriction removed in future too. Having a variable
declared on first use in the middle of block I find a far easier way of working as a) it saves scrolling to look for variable definitions and b) it
makes it far easier when adding/removing blocks of code e.g. commenting out
for testing, to have all the code together rather than having variables at
the top to add/remove also.

And c) Initializing the variables close to where they are used the first time reduces the risk of initializing them incorrectly. Especially when modifying a block of code, initialization of its variables might be missed if out of sight. (Although this is probably a consequence of "a)".)

I consider it old style to only declare variables at the start of a block of code, and this style of coding should be considered obsolete.

If you are really old (like me?), you might remember when function parameters were provided like this:

int main(argc, argv)
int argc;
char *argv[];

{
	return(0);
}

heh, k&r C

Quoted 6 lines
Quoted 2 lines

We have moved on from that to a more modern coding style a long time ago. We should also move on to a more modern coding style regarding variable declarations.

Old men are used to look for variable types at the beginning of functions. Having only new code adopting a different style may be confusing a little. Note I'm not against it, just asking for more feedbacks.

+1 for declare in minimum necessary scope
+1 for declare at first use (enables more use of const)

thank you!

#8 lore raw
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 7 lines

Declaring variable inside for loop is not supported via C89 and it was checked in checkpatch.sh via commit [1].
But as DPDK supported C standard is becoming C99/C11 [2], declaring variable inside loop can be allowed.

[1]
Commit 43e73483a4b8 ("devtools: forbid variable declaration inside

for")

Quoted 12 lines

[2]
https://dpdk.org/patch/121912

---
Cc: Bruce Richardson <[email protected]>
Cc: David Marchand <[email protected]>

v2:
* Update coding convention too
---

Quoted 5 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

Applied, thanks.