* [PATCH v1] devtools: allow variable declaration inside for loop @ 2023-05-03 9:50 Ferruh Yigit 2023-05-03 10:02 ` Bruce Richardson 2023-05-03 10:30 ` [PATCH v2] " Ferruh Yigit 0 siblings, 2 replies; 9+ messages in thread From: Ferruh Yigit @ 2023-05-03 9:50 UTC (permalink / raw) To: Thomas Monjalon; +Cc: dev, Bruce Richardson, David Marchand 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 Signed-off-by: Ferruh Yigit <ferruh.yigit@amd.com> --- Cc: Bruce Richardson <bruce.richardson@intel.com> Cc: David Marchand <david.marchand@redhat.com> --- devtools/checkpatches.sh | 8 -------- 1 file changed, 8 deletions(-) diff --git a/devtools/checkpatches.sh b/devtools/checkpatches.sh index 15d5d6709445..b5baf6f2b161 100755 --- a/devtools/checkpatches.sh +++ b/devtools/checkpatches.sh @@ -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]_.*\\\(" \ -- 2.34.1 ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v1] devtools: allow variable declaration inside for loop 2023-05-03 9:50 [PATCH v1] devtools: allow variable declaration inside for loop Ferruh Yigit @ 2023-05-03 10:02 ` Bruce Richardson 2023-05-03 10:23 ` Ferruh Yigit 2023-05-03 10:30 ` [PATCH v2] " Ferruh Yigit 1 sibling, 1 reply; 9+ messages in thread From: Bruce Richardson @ 2023-05-03 10:02 UTC (permalink / raw) To: Ferruh Yigit; +Cc: Thomas Monjalon, dev, David Marchand On Wed, May 03, 2023 at 10:50:18AM +0100, Ferruh Yigit wrote: > 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 > > Signed-off-by: Ferruh Yigit <ferruh.yigit@amd.com> --- Cc: Bruce > Richardson <bruce.richardson@intel.com> Cc: David Marchand > <david.marchand@redhat.com> --- 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 ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v1] devtools: allow variable declaration inside for loop 2023-05-03 10:02 ` Bruce Richardson @ 2023-05-03 10:23 ` Ferruh Yigit 0 siblings, 0 replies; 9+ messages in thread From: Ferruh Yigit @ 2023-05-03 10:23 UTC (permalink / raw) To: Bruce Richardson; +Cc: Thomas Monjalon, dev, David Marchand On 5/3/2023 11:02 AM, Bruce Richardson wrote: > On Wed, May 03, 2023 at 10:50:18AM +0100, Ferruh Yigit wrote: >> 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 >> >> Signed-off-by: Ferruh Yigit <ferruh.yigit@amd.com> --- Cc: Bruce >> Richardson <bruce.richardson@intel.com> Cc: David Marchand >> <david.marchand@redhat.com> --- 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 will update the commit log for target as C99/C11 . > > 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. ^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v2] devtools: allow variable declaration inside for loop 2023-05-03 9:50 [PATCH v1] devtools: allow variable declaration inside for loop Ferruh Yigit 2023-05-03 10:02 ` Bruce Richardson @ 2023-05-03 10:30 ` Ferruh Yigit 2023-05-03 10:57 ` Bruce Richardson 1 sibling, 1 reply; 9+ messages in thread From: Ferruh Yigit @ 2023-05-03 10:30 UTC (permalink / raw) To: Thomas Monjalon; +Cc: dev, Bruce Richardson, David Marchand 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 Signed-off-by: Ferruh Yigit <ferruh.yigit@amd.com> --- Cc: Bruce Richardson <bruce.richardson@intel.com> Cc: David Marchand <david.marchand@redhat.com> v2: * Update coding convention too --- devtools/checkpatches.sh | 8 -------- doc/guides/contributing/coding_style.rst | 1 + 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/devtools/checkpatches.sh b/devtools/checkpatches.sh index 15d5d6709445..b5baf6f2b161 100755 --- a/devtools/checkpatches.sh +++ b/devtools/checkpatches.sh @@ -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]_.*\\\(" \ diff --git a/doc/guides/contributing/coding_style.rst b/doc/guides/contributing/coding_style.rst index 89db6260cfbf..e18b8d4439ea 100644 --- a/doc/guides/contributing/coding_style.rst +++ b/doc/guides/contributing/coding_style.rst @@ -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. -- 2.34.1 ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v2] devtools: allow variable declaration inside for loop 2023-05-03 10:30 ` [PATCH v2] " Ferruh Yigit @ 2023-05-03 10:57 ` Bruce Richardson 2023-05-03 12:19 ` Morten Brørup 0 siblings, 1 reply; 9+ messages in thread From: Bruce Richardson @ 2023-05-03 10:57 UTC (permalink / raw) To: Ferruh Yigit; +Cc: Thomas Monjalon, dev, David Marchand On Wed, May 03, 2023 at 11:30:53AM +0100, Ferruh Yigit wrote: > 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 > > Signed-off-by: Ferruh Yigit <ferruh.yigit@amd.com> > --- > Cc: Bruce Richardson <bruce.richardson@intel.com> > Cc: David Marchand <david.marchand@redhat.com> > > v2: > * Update coding convention too > --- Acked-by: Bruce Richardson <bruce.richardson@intel.com> > devtools/checkpatches.sh | 8 -------- > doc/guides/contributing/coding_style.rst | 1 + > 2 files changed, 1 insertion(+), 8 deletions(-) > > diff --git a/devtools/checkpatches.sh b/devtools/checkpatches.sh > index 15d5d6709445..b5baf6f2b161 100755 > --- a/devtools/checkpatches.sh > +++ b/devtools/checkpatches.sh > @@ -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]_.*\\\(" \ > diff --git a/doc/guides/contributing/coding_style.rst b/doc/guides/contributing/coding_style.rst > index 89db6260cfbf..e18b8d4439ea 100644 > --- a/doc/guides/contributing/coding_style.rst > +++ b/doc/guides/contributing/coding_style.rst > @@ -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. > 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. > -- > 2.34.1 > ^ permalink raw reply [flat|nested] 9+ messages in thread
* RE: [PATCH v2] devtools: allow variable declaration inside for loop 2023-05-03 10:57 ` Bruce Richardson @ 2023-05-03 12:19 ` Morten Brørup 2023-05-03 15:01 ` Thomas Monjalon 0 siblings, 1 reply; 9+ messages in thread From: Morten Brørup @ 2023-05-03 12:19 UTC (permalink / raw) To: Bruce Richardson, Ferruh Yigit; +Cc: Thomas Monjalon, dev, David Marchand > From: Bruce Richardson [mailto:bruce.richardson@intel.com] > Sent: Wednesday, 3 May 2023 12.57 > > On Wed, May 03, 2023 at 11:30:53AM +0100, Ferruh Yigit wrote: > > 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 > > > > Signed-off-by: Ferruh Yigit <ferruh.yigit@amd.com> > > --- > > Cc: Bruce Richardson <bruce.richardson@intel.com> > > Cc: David Marchand <david.marchand@redhat.com> > > > > v2: > > * Update coding convention too > > --- > > Acked-by: Bruce Richardson <bruce.richardson@intel.com> Acked-by: Morten Brørup <mb@smartsharesystems.com> [...] > > @@ -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. 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. > > > 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. > > -- > > 2.34.1 > > ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v2] devtools: allow variable declaration inside for loop 2023-05-03 12:19 ` Morten Brørup @ 2023-05-03 15:01 ` Thomas Monjalon 2023-05-03 15:06 ` Tyler Retzlaff 0 siblings, 1 reply; 9+ messages in thread From: Thomas Monjalon @ 2023-05-03 15:01 UTC (permalink / raw) To: Bruce Richardson, Ferruh Yigit, Morten Brørup; +Cc: dev, David Marchand 03/05/2023 14:19, Morten Brørup: > > From: Bruce Richardson [mailto:bruce.richardson@intel.com] > > Sent: Wednesday, 3 May 2023 12.57 > > > > On Wed, May 03, 2023 at 11:30:53AM +0100, Ferruh Yigit wrote: > > > 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 > > > > > > Signed-off-by: Ferruh Yigit <ferruh.yigit@amd.com> > > > --- > > > Cc: Bruce Richardson <bruce.richardson@intel.com> > > > Cc: David Marchand <david.marchand@redhat.com> > > > > > > v2: > > > * Update coding convention too > > > --- > > > > Acked-by: Bruce Richardson <bruce.richardson@intel.com> > > Acked-by: Morten Brørup <mb@smartsharesystems.com> > > [...] > > > > @@ -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. > > 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. 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. ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v2] devtools: allow variable declaration inside for loop 2023-05-03 15:01 ` Thomas Monjalon @ 2023-05-03 15:06 ` Tyler Retzlaff 2023-07-20 4:05 ` Thomas Monjalon 0 siblings, 1 reply; 9+ messages in thread From: Tyler Retzlaff @ 2023-05-03 15:06 UTC (permalink / raw) To: Thomas Monjalon Cc: Bruce Richardson, Ferruh Yigit, Morten Brørup, dev, David Marchand On Wed, May 03, 2023 at 05:01:01PM +0200, Thomas Monjalon wrote: > 03/05/2023 14:19, Morten Brørup: > > > From: Bruce Richardson [mailto:bruce.richardson@intel.com] > > > Sent: Wednesday, 3 May 2023 12.57 > > > > > > On Wed, May 03, 2023 at 11:30:53AM +0100, Ferruh Yigit wrote: > > > > 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 > > > > > > > > Signed-off-by: Ferruh Yigit <ferruh.yigit@amd.com> > > > > --- > > > > Cc: Bruce Richardson <bruce.richardson@intel.com> > > > > Cc: David Marchand <david.marchand@redhat.com> > > > > > > > > v2: > > > > * Update coding convention too > > > > --- > > > > > > Acked-by: Bruce Richardson <bruce.richardson@intel.com> > > > > Acked-by: Morten Brørup <mb@smartsharesystems.com> > > > > [...] > > > > > > @@ -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. > > > > 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 > > > > 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. Acked-by: Tyler Retzlaff <roretzla@linux.microsoft.com> +1 for declare in minimum necessary scope +1 for declare at first use (enables more use of const) thank you! ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v2] devtools: allow variable declaration inside for loop 2023-05-03 15:06 ` Tyler Retzlaff @ 2023-07-20 4:05 ` Thomas Monjalon 0 siblings, 0 replies; 9+ messages in thread From: Thomas Monjalon @ 2023-07-20 4:05 UTC (permalink / raw) To: Ferruh Yigit Cc: Bruce Richardson, Morten Brørup, dev, David Marchand, Tyler Retzlaff > > > > > 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 > > > > > > > > > > Signed-off-by: Ferruh Yigit <ferruh.yigit@amd.com> > > > > > --- > > > > > Cc: Bruce Richardson <bruce.richardson@intel.com> > > > > > Cc: David Marchand <david.marchand@redhat.com> > > > > > > > > > > v2: > > > > > * Update coding convention too > > > > > --- > > > > > > > > Acked-by: Bruce Richardson <bruce.richardson@intel.com> > > > > > > Acked-by: Morten Brørup <mb@smartsharesystems.com> > > Acked-by: Tyler Retzlaff <roretzla@linux.microsoft.com> Applied, thanks. ^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2023-07-20 4:05 UTC | newest] Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2023-05-03 9:50 [PATCH v1] devtools: allow variable declaration inside for loop Ferruh Yigit 2023-05-03 10:02 ` Bruce Richardson 2023-05-03 10:23 ` Ferruh Yigit 2023-05-03 10:30 ` [PATCH v2] " Ferruh Yigit 2023-05-03 10:57 ` Bruce Richardson 2023-05-03 12:19 ` Morten Brørup 2023-05-03 15:01 ` Thomas Monjalon 2023-05-03 15:06 ` Tyler Retzlaff 2023-07-20 4:05 ` Thomas Monjalon
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).