Skip to contentSkip to conversation

kmemcheck: fix sparse warning

PATCH In review Johannes Berg 8 participants 11 messages Jul 6, 2009 – Aug 6, 2009 Sparse checker

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kmemcheck: fix sparse warning

Whether or not the sparse warning

warning: do-while statement is not a compound statement

is justified or not in this case, it is annoying and trivial to fix.

---
 include/linux/kmemcheck.h |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/kmemcheck.h +2 -2
@@ -137,13 +137,13 @@ static inline void kmemcheck_mark_initia
 	int name##_end[0];
 
 #define kmemcheck_annotate_bitfield(ptr, name)				\
-	do if (ptr) {							\
+	do { if (ptr) {							\
 		int _n = (long) &((ptr)->name##_end)			\
 			- (long) &((ptr)->name##_begin);		\
 		BUILD_BUG_ON(_n < 0);					\
 									\
 		kmemcheck_mark_initialized(&((ptr)->name##_begin), _n);	\
-	} while (0)
+	} } while (0)
 
 #define kmemcheck_annotate_variable(var)				\
 	do {								\
Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · Johannes Berg

Whether or not the sparse warning

warning: do-while statement is not a compound statement

is justified or not in this case, it is annoying and trivial to fix.

Looks good to me!

Quoted 24 lines
---
 include/linux/kmemcheck.h |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/kmemcheck.h +2 -2
@@ -137,13 +137,13 @@ static inline void kmemcheck_mark_initia
 	int name##_end[0];
 
 #define kmemcheck_annotate_bitfield(ptr, name)				\
-	do if (ptr) {							\
+	do { if (ptr) {							\
 		int _n = (long) &((ptr)->name##_end)			\
 			- (long) &((ptr)->name##_begin);		\
 		BUILD_BUG_ON(_n < 0);					\
 									\
 		kmemcheck_mark_initialized(&((ptr)->name##_begin), _n);	\
-	} while (0)
+	} } while (0)
 
 #define kmemcheck_annotate_variable(var)				\
 	do {								\
#2 lore raw
In reply to Johannes Berg

2009/7/6 Johannes Berg <[email protected]>:

Quoted 23 lines

Whether or not the sparse warning

warning: do-while statement is not a compound statement

is justified or not in this case, it is annoying and trivial to fix.

---
 include/linux/kmemcheck.h |    4 ++--

 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/kmemcheck.h  2009-07-06 11:41:30.000000000 +0200 renamed from include/linux/kmemcheck.h     2009-07-06 11:41:16.000000000 +0200 +1 -1
@@ -137,13 +137,13 @@ static inline void kmemcheck_mark_initia
        int name##_end[0];
 
  #define kmemcheck_annotate_bitfield(ptr, name)                         \
-       do if (ptr) {                                                   \
+       do { if (ptr) {                                                 \

include/linux/kmemcheck.h  2009-07-06 11:41:30.000000000 +0200 static inline void kmemcheck_mark_initia · line 149

Quoted 10 lines
include/linux/kmemcheck.h  2009-07-06 11:41:30.000000000 +0200 renamed from include/linux/kmemcheck.h     2009-07-06 11:41:16.000000000 +0200 +1 -1
                int _n = (long) &((ptr)->name##_end)                    \
                        - (long) &((ptr)->name##_begin);                \
                BUILD_BUG_ON(_n < 0);                                   \
                                                                        \
                kmemcheck_mark_initialized(&((ptr)->name##_begin), _n); \
-       } while (0)
+       } } while (0)
 
  #define kmemcheck_annotate_variable(var)                               \
        do {                                                            \

I'll change the patch title to "kmemcheck: work around bogus sparse warning" and fix the indentation, sounds ok?

Meanwhile, I Cced sparse mailing list in case somebody else knows anything else about this warning (what it means, whether it's justified in this case, whether it should be fixed in sparse, etc.).

Thanks.

Vegard

#3 lore raw
Quoted 11 lines · Vegard Nossum
Quoted 11 lines

#define kmemcheck_annotate_bitfield(ptr, name) \

include/linux/kmemcheck.h +2 -2
-       do if (ptr) {                                                   \
+       do { if (ptr) {                                                 \
               int _n = (long) &((ptr)->name##_end)                    \
                       - (long) &((ptr)->name##_begin);                \
               BUILD_BUG_ON(_n < 0);                                   \
                                                                       \
               kmemcheck_mark_initialized(&((ptr)->name##_begin), _n); \
-       } while (0)
+       } } while (0)
Quoted 8 lines
Quoted 5 lines
 #define kmemcheck_annotate_variable(var)                               \
       do {                                                            \

I'll change the patch title to "kmemcheck: work around bogus sparse warning" and fix the indentation, sounds ok?

Well, it's debatable whether it's bogus or not, but I don't care as long as it gets fixed.

Quoted 3 lines

Meanwhile, I Cced sparse mailing list in case somebody else knows anything else about this warning (what it means, whether it's justified in this case, whether it should be fixed in sparse, etc.).

Everybody take me off the CC please then :) This has been discussed far too many times already. In this special case, the warning probably isn't all that useful, but ISTR Linus saying that he wanted it this way.

johannes

maintainer
#4 lore raw
Quoted 3 lines · Johannes Berg

Everybody take me off the CC please then :) This has been discussed far too many times already. In this special case, the warning probably isn't all that useful, but ISTR Linus saying that he wanted it this way.

Right. I would keep the warning in sparse. I think it is better with the compound statement. It is easier to read which block is which.

include/linux/kmemcheck.h static inline void kmemcheck_mark_initia · line 140

Quoted 2 lines
include/linux/kmemcheck.h +1 -1
-       do if (ptr) {                                                   \
+       do { if (ptr) {                                                 \

BTW, if does not hurt to put the "if (ptr)" to a separate line.

Chris

#5 lore raw
In reply to Vegard Nossum

[Adding Linus and Chris Li to CC; Linus for further background on -Wdo-while, and Chris Li for Sparse.]

Quoted 7 lines · Vegard Nossum

2009/7/6 Johannes Berg <[email protected]>:

Quoted 6 lines

Whether or not the sparse warning

warning: do-while statement is not a compound statement

is justified or not in this case, it is annoying and trivial to fix.

[...]

Quoted 7 lines

I'll change the patch title to "kmemcheck: work around bogus sparse warning" and fix the indentation, sounds ok?

Meanwhile, I Cced sparse mailing list in case somebody else knows anything else about this warning (what it means, whether it's justified in this case, whether it should be fixed in sparse, etc.).

-Wdo-while gives a warning if you write:

do
statement
while (...);

where "statement" does not consist of a compound statement surrounded by braces. As far as I know, this warning exists primarily because it matched Linus's preference for readability.

However, note that Sparse does not have -Wdo-while enabled by default. Sparse only issues that warning if you use -Wdo-while explicitly, or if you pass -Wall. (The latter often happens due to passing the same warning flags to GCC and Sparse, without using cgcc which filters out -Wall for Sparse for that reason.)

Much discussion has occurred previously suggesting that Sparse's -Wall should match GCC's "all useful warnings" rather than "all possible warnings", and some other option should exist for "all possible warnings" (for instance, -Weverything). This seems very reasonable. Given that Sparse *exists* to give warnings, -Wall's "all useful warnings" approach seems like it matches Sparse's default behavior; thus, I think it would make the most sense to make Sparse just interpret -Wall as "enable all the warnings enabled by default" (which would normally act as a no-op, but would make a difference if you passed "-Wno-foo -Wall" for some default warning foo).

This would improve Sparse's behavior for the case where you invoke sparse $(CFLAGS) directly rather than setting CC=cgcc, if CFLAGS contains -Wall.

- Josh Triplett

#6 lore raw
Quoted 22 lines · Josh Triplett

[Adding Linus and Chris Li to CC; Linus for further background on -Wdo-while, and Chris Li for Sparse.]

Quoted 7 lines · Vegard Nossum

2009/7/6 Johannes Berg <[email protected]>:

Quoted 6 lines

Whether or not the sparse warning

warning: do-while statement is not a compound statement

is justified or not in this case, it is annoying and trivial to fix.

[...]

Quoted 7 lines

I'll change the patch title to "kmemcheck: work around bogus sparse warning" and fix the indentation, sounds ok?

Meanwhile, I Cced sparse mailing list in case somebody else knows anything else about this warning (what it means, whether it's justified in this case, whether it should be fixed in sparse, etc.).

-Wdo-while gives a warning if you write:

Quoted 7 lines

do
   statement
while (...);

where "statement" does not consist of a compound statement surrounded by braces.  As far as I know, this warning exists primarily because it matched Linus's preference for readability.

see:
http://lkml.org/lkml/2008/12/23/180

an related messages/threads

Cheers,
-Hannes
--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#7 lore raw
In reply to Josh Triplett

[ stripped people from To: and Cc: - far too trivial and well-known answer ]

Quoted 2 lines · Josh Triplett

[Adding Linus and Chris Li to CC; Linus for further background on -Wdo-while, and Chris Li for Sparse.]

[....]

Quoted 2 lines

Much discussion has occurred previously suggesting that Sparse's -Wall should match GCC's "all useful warnings" rather than "all possible

this is usually phrased as "all useful warnings in RMS opinion". I'm not sure how far RMS influence is today on that.

Quoted 2 lines

warnings", and some other option should exist for "all possible warnings" (for instance, -Weverything). This seems very reasonable.

gcc's has "-Wextra" since ages which enables lots of other (IMHO) useful warnings.
But it's just a name ....

Bernd

Signature · 3 lines
Firmix Software GmbH                   http://www.firmix.at/
mobil: +43 664 4416156                 fax: +43 1 7890849-55
          Embedded Linux Development and Services
#8 lore raw
In reply to Johannes Berg
Quoted 6 lines · Johannes Berg

Whether or not the sparse warning

warning: do-while statement is not a compound statement

is justified or not in this case, it is annoying and trivial to fix.

Whatever happened to this patch? Did you decide to screw sparse warnings?

johannes

Quoted 24 lines
---
 include/linux/kmemcheck.h |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/kmemcheck.h +2 -2
@@ -137,13 +137,13 @@ static inline void kmemcheck_mark_initia
 	int name##_end[0];
 
 #define kmemcheck_annotate_bitfield(ptr, name)				\
-	do if (ptr) {							\
+	do { if (ptr) {							\
 		int _n = (long) &((ptr)->name##_end)			\
 			- (long) &((ptr)->name##_begin);		\
 		BUILD_BUG_ON(_n < 0);					\
 									\
 		kmemcheck_mark_initialized(&((ptr)->name##_begin), _n);	\
-	} while (0)
+	} } while (0)
 
 #define kmemcheck_annotate_variable(var)				\
 	do {								\
#9 lore raw

Hi,

Quoted 10 lines · Johannes Berg
Quoted 6 lines · Johannes Berg

Whether or not the sparse warning

warning: do-while statement is not a compound statement

is justified or not in this case, it is annoying and trivial to fix.

Whatever happened to this patch? Did you decide to screw sparse warnings?

I thought Vegard applied the patch but I can't see it in kmemcheck.git either. Vegard?

Pekka

Quoted 25 lines
Quoted 24 lines
---
 include/linux/kmemcheck.h |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/kmemcheck.h +2 -2
@@ -137,13 +137,13 @@ static inline void kmemcheck_mark_initia
 	int name##_end[0];
 
 #define kmemcheck_annotate_bitfield(ptr, name)				\
-	do if (ptr) {							\
+	do { if (ptr) {							\
 		int _n = (long) &((ptr)->name##_end)			\
 			- (long) &((ptr)->name##_begin);		\
 		BUILD_BUG_ON(_n < 0);					\
 									\
 		kmemcheck_mark_initialized(&((ptr)->name##_begin), _n);	\
-	} while (0)
+	} } while (0)
 
 #define kmemcheck_annotate_variable(var)				\
 	do {								\
#10 lore raw
In reply to Johannes Berg

Hi,

Quoted 1 line · Johannes Berg
Quoted 1 line

I know I'm colouring the bike-shed but you can avoid the outer do-while with:

Quoted 8 lines

include/linux/kmemcheck.h static inline void kmemcheck_mark_initia · line 140

Quoted 8 lines
include/linux/kmemcheck.h +1 -1
@@ -137,13 +137,13 @@ static inline void kmemcheck_mark_initia
 	int name##_end[0];
 
 #define kmemcheck_annotate_bitfield(ptr, name)				\
-	do if (ptr) {							\
+	do { if (ptr) {							\

+ if (ptr) {

Quoted 7 lines

include/linux/kmemcheck.h static inline void kmemcheck_mark_initia · line 146

Quoted 7 lines
include/linux/kmemcheck.h +1 -1
 		int _n = (long) &((ptr)->name##_end)			\
 			- (long) &((ptr)->name##_begin);		\
 		BUILD_BUG_ON(_n < 0);					\
 									\
 		kmemcheck_mark_initialized(&((ptr)->name##_begin), _n);	\
-	} while (0)
+	} } while (0)

+ } else

Of course, that tailing 'else' may be much too clever to live. A slightly less clever but more idiomatic approach would be to use

+ } else do {} while (0)

but what's the point. Oh well, ignore me :-) I'm just pained by the '} }' due to my wierd sense of aesthetics.

- Hari