CODING_STYLE: Section about mixed declarations
PATCH 1/1 CODING_STYLE: Section about mixed declarations
We had an unwritten rule about declarations having to be at beginning of blocks. Make it a written rule.
--- CODING_STYLE | 7 +++++++ 1 file changed, 7 insertions(+)
CODING_STYLE +7 -0
@@ -84,3 +84,10 @@ and clarity it comes on a line by itself: Rationale: a consistent (except for functions...) bracing style reduces ambiguity and avoids needless churn when lines are added or removed. Furthermore, it is the QEMU coding style. + +5. Declarations + +Mixed declarations (interleaving statements and declarations within blocks) are +not allowed; declarations should be at beginning of blocks. In other words, +the code should not generate warnings if using GCC's +-Wdeclaration-after-statement option.
Signature · 1 line
1.8.5.3
Quoted 13 lines · Eduardo Habkost
We had an unwritten rule about declarations having to be at beginning of blocks. Make it a written rule.
Signed-off-by Eduardo Habkost <[email protected]>--- CODING_STYLE | 7 +++++++ 1 file changed, 7 insertions(+)CODING_STYLE +0
-0
CODING_STYLE and clarity it comes on a line by itself: · line 93
Quoted 10 lines
CODING_STYLE +7
-0@@ -84,3 +84,10 @@ and clarity it comes on a line by itself: Rationale: a consistent (except for functions...) bracing style reduces ambiguity and avoids needless churn when lines are added or removed. Furthermore, it is the QEMU coding style. + +5. Declarations + +Mixed declarations (interleaving statements and declarations within blocks) are +not allowed; declarations should be at beginning of blocks. In other words, +the code should not generate warnings if using GCC's +-Wdeclaration-after-statement option.
What is the reason that we don't use -Wdeclaration-after-statement in Makefile? Thanks.
Fam
Stefan Weil
Reviewed-byAm 10.02.2014 02:33, schrieb Fam Zheng:
Quoted 9 lines · Eduardo Habkost
Quoted 8 lines
We had an unwritten rule about declarations having to be at beginning of blocks. Make it a written rule.
Signed-off-by Eduardo Habkost <[email protected]>--- CODING_STYLE | 7 +++++++ 1 file changed, 7 insertions(+)
Quoted 13 lines
CODING_STYLE and clarity it comes on a line by itself: · line 91
Quoted 13 lines
CODING_STYLE +5
-0@@ -84,3 +84,10 @@ and clarity it comes on a line by itself: Rationale: a consistent (except for functions...) bracing style reduces ambiguity and avoids needless churn when lines are added or removed. Furthermore, it is the QEMU coding style. + +5. Declarations + +Mixed declarations (interleaving statements and declarations within blocks) are +not allowed; declarations should be at beginning of blocks. In other words,
at the beginning?
Quoted 7 lines
CODING_STYLE and clarity it comes on a line by itself: · line 93
Quoted 2 lines
CODING_STYLE +2
-0+the code should not generate warnings if using GCC's +-Wdeclaration-after-statement option.What is the reason that we don't use -Wdeclaration-after-statement in
Makefile? Thanks.Fam
Maybe that would be a good idea. I did a short test and added that flag in configure (where all gcc flags are checked before they are applied):
configure +0 -0
@@ -331,6 +331,8 @@ ARFLAGS="${ARFLAGS-rv}" # default flags for all hosts QEMU_CFLAGS="-fno-strict-aliasing $QEMU_CFLAGS" QEMU_CFLAGS="-Wall -Wundef -Wwrite-strings -Wmissing-prototypes
$QEMU_CFLAGS"
+QEMU_CFLAGS="-Wdeclaration-after-statement $QEMU_CFLAGS" +QEMU_CFLAGS="-Wno-error=declaration-after-statement $QEMU_CFLAGS"
QEMU_CFLAGS="-Wstrict-prototypes -Wredundant-decls $QEMU_CFLAGS" QEMU_CFLAGS="-D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE
$QEMU_CFLAGS"
QEMU_INCLUDES="-I. -I\$(SRC_PATH) -I\$(SRC_PATH)/include"
The first result already shows 194 different violations. See
http://qemu.weilnetz.de/test/declaration-after-statement.txt for the build protocol. As this test does not cover all source paths, there might be even more violations. So the code will need some fixes before this rule can be checked by -Wdeclaration-after-statement. Until this is one, we could use -Wno-error=declaration-after-statement as I did in my test.
The patch for CODING_STYLE is still useful.
Stefan
Quoted 19 lines · Fam Zheng
Quoted 18 lines · Eduardo Habkost
We had an unwritten rule about declarations having to be at beginning of blocks. Make it a written rule.
Signed-off-by Eduardo Habkost <[email protected]>--- CODING_STYLE | 7 +++++++ 1 file changed, 7 insertions(+)CODING_STYLE +2
-0@@ -84,3 +84,10 @@ and clarity it comes on a line by itself: Rationale: a consistent (except for functions...) bracing style reduces ambiguity and avoids needless churn when lines are added or removed. Furthermore, it is the QEMU coding style. + +5. Declarations
Quoted 8 lines
CODING_STYLE and clarity it comes on a line by itself: · line 93
Quoted 5 lines
CODING_STYLE +5
-0+ +Mixed declarations (interleaving statements and declarations within blocks) are +not allowed; declarations should be at beginning of blocks. In other words, +the code should not generate warnings if using GCC's +-Wdeclaration-after-statement option.What is the reason that we don't use -Wdeclaration-after-statement in
Makefile? Thanks.
We have around 39 files today that don't follow the rule, today, and would generate warnings if we used -Wdeclaration-after-statement.
Signature · 1 line
Eduardo
Quoted 7 lines · Eduardo Habkost
We had an unwritten rule about declarations having to be at beginning of blocks. Make it a written rule.
Signed-off-by Eduardo Habkost <[email protected]>--- CODING_STYLE | 7 +++++++ 1 file changed, 7 insertions(+)
I think C99 mixed declarations and code are a good thing. It's easier to read code when the local variable type is alongside the initialization.
I'll try to avoid mixed declarations and code if the others feel this rule is worthwhile.
Stefan
Ping? Who is willing to apply this?
Quoted 27 lines · Eduardo Habkost
We had an unwritten rule about declarations having to be at beginning of blocks. Make it a written rule.
Signed-off-by Eduardo Habkost <[email protected]>--- CODING_STYLE | 7 +++++++ 1 file changed, 7 insertions(+)CODING_STYLE +7
-0@@ -84,3 +84,10 @@ and clarity it comes on a line by itself: Rationale: a consistent (except for functions...) bracing style reduces ambiguity and avoids needless churn when lines are added or removed. Furthermore, it is the QEMU coding style. + +5. Declarations + +Mixed declarations (interleaving statements and declarations within blocks) are +not allowed; declarations should be at beginning of blocks. In other words, +the code should not generate warnings if using GCC's +-Wdeclaration-after-statement option.Signature · 1 line
1.8.5.3
Signature · 1 line
Eduardo